1 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
21 #include "SALOME_ExtractGeometry.h"
24 #include <vtkCellData.h>
25 #include <vtkFloatArray.h>
26 #include <vtkIdList.h>
27 #include <vtkImplicitFunction.h>
28 #include <vtkObjectFactory.h>
29 #include <vtkPointData.h>
30 #include <vtkUnstructuredGrid.h>
32 #include <vtkImplicitBoolean.h>
33 #include <vtkImplicitFunctionCollection.h>
44 //----------------------------------------------------------------------------
45 vtkStandardNewMacro(SALOME_ExtractGeometry);
48 //----------------------------------------------------------------------------
49 SALOME_ExtractGeometry
50 ::SALOME_ExtractGeometry():
51 myStoreMapping(false),
52 myIsDoneShallowCopy(false)
55 SALOME_ExtractGeometry
56 ::~SALOME_ExtractGeometry()
60 //----------------------------------------------------------------------------
62 SALOME_ExtractGeometry
65 unsigned long int aTime = vtkExtractGeometry::GetMTime();
70 //----------------------------------------------------------------------------
72 SALOME_ExtractGeometry
73 ::GetImplicitBoolean()
75 return myImplicitBoolean.GetPointer();
80 SALOME_ExtractGeometry
81 ::SetImplicitFunction(vtkImplicitFunction* theImplicitFunction)
83 myImplicitBoolean = dynamic_cast<vtkImplicitBoolean*>(theImplicitFunction);
84 vtkExtractGeometry::SetImplicitFunction(theImplicitFunction);
88 //----------------------------------------------------------------------------
90 SALOME_ExtractGeometry
91 ::SetStoreMapping(bool theStoreMapping)
93 myStoreMapping = theStoreMapping;
98 SALOME_ExtractGeometry
99 ::GetStoreMapping() const
101 return myStoreMapping;
105 //----------------------------------------------------------------------------
107 SALOME_ExtractGeometry
108 ::GetElemVTKId(vtkIdType theID)
110 if(!myStoreMapping||myIsDoneShallowCopy){
113 vtkIdType iEnd = myElemVTK2ObjIds.size();
114 for(vtkIdType i = 0; i < iEnd; i++)
115 if(myElemVTK2ObjIds[i] == theID)
122 SALOME_ExtractGeometry
123 ::GetNodeVTKId(vtkIdType theID)
125 if (!myStoreMapping||myIsDoneShallowCopy){
128 vtkIdType iEnd = myNodeVTK2ObjIds.size();
129 for(vtkIdType i = 0; i < iEnd; i++)
130 if(myNodeVTK2ObjIds[i] == theID)
137 //----------------------------------------------------------------------------
139 SALOME_ExtractGeometry
140 ::GetElemObjId(int theVtkID)
142 if (!myStoreMapping||myIsDoneShallowCopy){
146 if (theVtkID<myElemVTK2ObjIds.size()){
147 return myElemVTK2ObjIds[theVtkID];
154 SALOME_ExtractGeometry
155 ::GetNodeObjId(int theVtkID)
157 if (!myStoreMapping||myIsDoneShallowCopy){
161 if (theVtkID<myNodeVTK2ObjIds.size()){
162 return myNodeVTK2ObjIds[theVtkID];
168 //----------------------------------------------------------------------------
170 SALOME_ExtractGeometry
173 myElemVTK2ObjIds.clear();
174 myNodeVTK2ObjIds.clear();
176 myIsDoneShallowCopy = !this->ImplicitFunction;
178 if(!myIsDoneShallowCopy && myImplicitBoolean.GetPointer()){
179 if(vtkImplicitFunctionCollection* aFunction = myImplicitBoolean->GetFunction()){
180 myIsDoneShallowCopy = aFunction->GetNumberOfItems() == 0;
184 if(myIsDoneShallowCopy){
185 GetOutput()->ShallowCopy(GetInput());
194 SALOME_ExtractGeometry
197 vtkIdType ptId, numPts, numCells, i, cellId, newCellId, newId, *pointMap;
201 vtkFloatingPointType *x;
202 vtkFloatingPointType multiplier;
204 vtkIdList *newCellPts;
205 vtkDataSet *input = this->GetInput();
206 vtkPointData *pd = input->GetPointData();
207 vtkCellData *cd = input->GetCellData();
208 vtkUnstructuredGrid *output = this->GetOutput();
209 vtkPointData *outputPD = output->GetPointData();
210 vtkCellData *outputCD = output->GetCellData();
212 numCells = input->GetNumberOfCells();
213 numPts = input->GetNumberOfPoints();
215 if ( ! this->ImplicitFunction )
217 vtkErrorMacro(<<"No implicit function specified");
221 newCellPts = vtkIdList::New();
222 newCellPts->Allocate(VTK_CELL_SIZE);
224 if ( this->ExtractInside )
233 // Loop over all points determining whether they are inside the
234 // implicit function. Copy the points and point data if they are.
236 pointMap = new vtkIdType[numPts]; // maps old point ids into new
237 for (i=0; i < numPts; i++)
242 output->Allocate(numCells/4); //allocate storage for geometry/topology
243 newPts = vtkPoints::New();
244 newPts->Allocate(numPts/4,numPts);
245 outputPD->CopyAllocate(pd);
246 outputCD->CopyAllocate(cd);
247 vtkFloatArray *newScalars = NULL;
250 myElemVTK2ObjIds.reserve(numCells);
251 myNodeVTK2ObjIds.reserve(numPts);
254 if ( ! this->ExtractBoundaryCells )
256 for ( ptId=0; ptId < numPts; ptId++ )
258 x = input->GetPoint(ptId);
259 if ( (this->ImplicitFunction->FunctionValue(x)*multiplier) < 0.0 )
261 newId = newPts->InsertNextPoint(x);
262 pointMap[ptId] = newId;
264 myNodeVTK2ObjIds.push_back(ptId);
265 outputPD->CopyData(pd,ptId,newId);
271 // To extract boundary cells, we have to create supplemental information
272 if ( this->ExtractBoundaryCells )
274 vtkFloatingPointType val;
275 newScalars = vtkFloatArray::New();
276 newScalars->SetNumberOfValues(numPts);
278 for (ptId=0; ptId < numPts; ptId++ )
280 x = input->GetPoint(ptId);
281 val = this->ImplicitFunction->FunctionValue(x) * multiplier;
282 newScalars->SetValue(ptId, val);
285 newId = newPts->InsertNextPoint(x);
286 pointMap[ptId] = newId;
288 myNodeVTK2ObjIds.push_back(ptId);
289 outputPD->CopyData(pd,ptId,newId);
295 // Now loop over all cells to see whether they are inside implicit
296 // function (or on boundary if ExtractBoundaryCells is on).
298 for (cellId=0; cellId < numCells; cellId++)
300 cell = input->GetCell(cellId);
301 cellPts = cell->GetPointIds();
302 numCellPts = cell->GetNumberOfPoints();
305 if ( ! this->ExtractBoundaryCells ) //requires less work
307 for ( npts=0, i=0; i < numCellPts; i++, npts++)
309 ptId = cellPts->GetId(i);
310 if ( pointMap[ptId] < 0 )
312 break; //this cell won't be inserted
316 newCellPts->InsertId(i,pointMap[ptId]);
319 } //if don't want to extract boundary cells
321 else //want boundary cells
323 for ( npts=0, i=0; i < numCellPts; i++ )
325 ptId = cellPts->GetId(i);
326 if ( newScalars->GetValue(ptId) <= 0.0 )
333 for ( i=0; i < numCellPts; i++ )
335 ptId = cellPts->GetId(i);
336 if ( pointMap[ptId] < 0 )
338 x = input->GetPoint(ptId);
339 newId = newPts->InsertNextPoint(x);
340 pointMap[ptId] = newId;
342 myNodeVTK2ObjIds.push_back(ptId);
343 outputPD->CopyData(pd,ptId,newId);
345 newCellPts->InsertId(i,pointMap[ptId]);
347 }//a boundary or interior cell
348 }//if mapping boundary cells
350 if ( npts >= numCellPts || (this->ExtractBoundaryCells && npts > 0) )
352 newCellId = output->InsertNextCell(cell->GetCellType(),newCellPts);
354 myElemVTK2ObjIds.push_back(cellId);
355 outputCD->CopyData(cd,cellId,newCellId);
359 // Update ourselves and release memory
362 newCellPts->Delete();
363 output->SetPoints(newPts);
366 if ( this->ExtractBoundaryCells )
368 newScalars->Delete();