1 // Copyright (C) 2007-2012 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "SMESH_ExtractGeometry.h"
26 #include <vtkCellData.h>
27 #include <vtkFloatArray.h>
28 #include <vtkIdList.h>
29 #include <vtkImplicitFunction.h>
30 #include <vtkObjectFactory.h>
31 #include <vtkPointData.h>
32 #include <vtkUnstructuredGrid.h>
33 #include <vtkInformation.h>
34 #include <vtkInformationVector.h>
39 //static int MYDEBUG = 0;
41 //static int MYDEBUG = 0;
51 vtkStandardNewMacro(SMESH_ExtractGeometry);
54 SMESH_ExtractGeometry::SMESH_ExtractGeometry()
58 SMESH_ExtractGeometry::~SMESH_ExtractGeometry(){}
61 vtkIdType SMESH_ExtractGeometry::GetElemObjId(int theVtkID){
62 if( theVtkID < 0 || theVtkID >= myElemVTK2ObjIds.size()) return -1;
63 return myElemVTK2ObjIds[theVtkID];
67 vtkIdType SMESH_ExtractGeometry::GetNodeObjId(int theVtkID){
68 if ( theVtkID < 0 || theVtkID >= myNodeVTK2ObjIds.size()) return -1;
69 return myNodeVTK2ObjIds[theVtkID];
73 int SMESH_ExtractGeometry::RequestData(
74 vtkInformation *vtkNotUsed(request),
75 vtkInformationVector **inputVector,
76 vtkInformationVector *outputVector)
78 // get the info objects
79 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
80 vtkInformation *outInfo = outputVector->GetInformationObject(0);
82 // get the input and ouptut
83 vtkDataSet *input = vtkDataSet::SafeDownCast(
84 inInfo->Get(vtkDataObject::DATA_OBJECT()));
85 vtkUnstructuredGrid *output = vtkUnstructuredGrid::SafeDownCast(
86 outInfo->Get(vtkDataObject::DATA_OBJECT()));
88 vtkIdType ptId, numPts, numCells, i, cellId, newCellId, newId, *pointMap;
92 vtkFloatingPointType *x;
93 vtkFloatingPointType multiplier;
95 vtkIdList *newCellPts;
96 vtkPointData *pd = input->GetPointData();
97 vtkCellData *cd = input->GetCellData();
98 vtkPointData *outputPD = output->GetPointData();
99 vtkCellData *outputCD = output->GetCellData();
101 numCells = input->GetNumberOfCells();
102 numPts = input->GetNumberOfPoints();
104 vtkDebugMacro(<< "Extracting geometry");
106 if ( ! this->ImplicitFunction )
108 vtkErrorMacro(<<"No implicit function specified");
112 newCellPts = vtkIdList::New();
113 newCellPts->Allocate(VTK_CELL_SIZE);
115 if ( this->ExtractInside )
124 // Loop over all points determining whether they are inside the
125 // implicit function. Copy the points and point data if they are.
127 pointMap = new vtkIdType[numPts]; // maps old point ids into new
128 for (i=0; i < numPts; i++)
133 output->Allocate(numCells/4); //allocate storage for geometry/topology
134 newPts = vtkPoints::New();
135 newPts->Allocate(numPts/4,numPts);
136 outputPD->CopyAllocate(pd);
137 outputCD->CopyAllocate(cd);
138 vtkFloatArray *newScalars = NULL;
141 myElemVTK2ObjIds.clear();
142 myElemVTK2ObjIds.reserve(numCells);
143 myNodeVTK2ObjIds.clear();
144 myNodeVTK2ObjIds.reserve(numPts);
147 if ( ! this->ExtractBoundaryCells )
149 for ( ptId=0; ptId < numPts; ptId++ )
151 x = input->GetPoint(ptId);
152 if ( (this->ImplicitFunction->FunctionValue(x)*multiplier) < 0.0 )
154 newId = newPts->InsertNextPoint(x);
155 pointMap[ptId] = newId;
156 myNodeVTK2ObjIds.push_back(ptId);
157 outputPD->CopyData(pd,ptId,newId);
163 // To extract boundary cells, we have to create supplemental information
164 if ( this->ExtractBoundaryCells )
166 vtkFloatingPointType val;
167 newScalars = vtkFloatArray::New();
168 newScalars->SetNumberOfValues(numPts);
170 for (ptId=0; ptId < numPts; ptId++ )
172 x = input->GetPoint(ptId);
173 val = this->ImplicitFunction->FunctionValue(x) * multiplier;
174 newScalars->SetValue(ptId, val);
177 newId = newPts->InsertNextPoint(x);
178 pointMap[ptId] = newId;
179 myNodeVTK2ObjIds.push_back(ptId);
180 outputPD->CopyData(pd,ptId,newId);
186 // Now loop over all cells to see whether they are inside implicit
187 // function (or on boundary if ExtractBoundaryCells is on).
189 for (cellId=0; cellId < numCells; cellId++)
191 cell = input->GetCell(cellId);
192 cellPts = cell->GetPointIds();
193 numCellPts = cell->GetNumberOfPoints();
196 if ( ! this->ExtractBoundaryCells ) //requires less work
198 for ( npts=0, i=0; i < numCellPts; i++, npts++)
200 ptId = cellPts->GetId(i);
201 if ( pointMap[ptId] < 0 )
203 break; //this cell won't be inserted
207 newCellPts->InsertId(i,pointMap[ptId]);
210 } //if don't want to extract boundary cells
212 else //want boundary cells
214 for ( npts=0, i=0; i < numCellPts; i++ )
216 ptId = cellPts->GetId(i);
217 if ( newScalars->GetValue(ptId) <= 0.0 )
224 for ( i=0; i < numCellPts; i++ )
226 ptId = cellPts->GetId(i);
227 if ( pointMap[ptId] < 0 )
229 x = input->GetPoint(ptId);
230 newId = newPts->InsertNextPoint(x);
231 pointMap[ptId] = newId;
232 myNodeVTK2ObjIds.push_back(ptId);
233 outputPD->CopyData(pd,ptId,newId);
235 newCellPts->InsertId(i,pointMap[ptId]);
237 }//a boundary or interior cell
238 }//if mapping boundary cells
240 if ( npts >= numCellPts || (this->ExtractBoundaryCells && npts > 0) )
242 if(cell->GetCellType() == VTK_POLYHEDRON) {
244 vtkUnstructuredGrid::SafeDownCast(input)->GetFaceStream( cellId ,newCellPts );
245 vtkUnstructuredGrid::ConvertFaceStreamPointIds(newCellPts, pointMap);
247 newCellId = output->InsertNextCell(cell->GetCellType(),newCellPts);
248 myElemVTK2ObjIds.push_back(cellId);
249 outputCD->CopyData(cd,cellId,newCellId);
253 // Update ourselves and release memory
256 newCellPts->Delete();
257 output->SetPoints(newPts);
260 if ( this->ExtractBoundaryCells )
262 newScalars->Delete();