1 // Copyright (C) 2007-2008 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
22 #include "SALOME_ExtractGeometry.h"
25 #include <vtkCellData.h>
26 #include <vtkFloatArray.h>
27 #include <vtkIdList.h>
28 #include <vtkImplicitFunction.h>
29 #include <vtkObjectFactory.h>
30 #include <vtkPointData.h>
31 #include <vtkUnstructuredGrid.h>
33 #include <vtkInformation.h>
34 #include <vtkInformationVector.h>
36 #include <vtkImplicitBoolean.h>
37 #include <vtkImplicitFunctionCollection.h>
46 //----------------------------------------------------------------------------
47 vtkStandardNewMacro(SALOME_ExtractGeometry);
50 //----------------------------------------------------------------------------
51 SALOME_ExtractGeometry
52 ::SALOME_ExtractGeometry():
53 myStoreMapping(false),
54 myIsDoneShallowCopy(false)
57 SALOME_ExtractGeometry
58 ::~SALOME_ExtractGeometry()
62 //----------------------------------------------------------------------------
64 SALOME_ExtractGeometry
65 ::GetImplicitBoolean()
67 return myImplicitBoolean.GetPointer();
72 SALOME_ExtractGeometry
73 ::SetImplicitFunction(vtkImplicitFunction* theImplicitFunction)
75 myImplicitBoolean = dynamic_cast<vtkImplicitBoolean*>(theImplicitFunction);
76 Superclass::SetImplicitFunction(theImplicitFunction);
80 //----------------------------------------------------------------------------
82 SALOME_ExtractGeometry
83 ::SetStoreMapping(bool theStoreMapping)
85 if(myStoreMapping != theStoreMapping){
86 myStoreMapping = theStoreMapping;
92 SALOME_ExtractGeometry
93 ::GetStoreMapping() const
95 return myStoreMapping;
99 //----------------------------------------------------------------------------
101 SALOME_ExtractGeometry
102 ::GetElemVTKId(vtkIdType theID)
104 if(!myStoreMapping || myIsDoneShallowCopy)
107 vtkIdType iEnd = myElemVTK2ObjIds.size();
108 for(vtkIdType i = 0; i < iEnd; i++)
109 if(myElemVTK2ObjIds[i] == theID)
116 SALOME_ExtractGeometry
117 ::GetNodeVTKId(vtkIdType theID)
119 if(!myStoreMapping || myIsDoneShallowCopy)
122 vtkIdType iEnd = myNodeVTK2ObjIds.size();
123 for(vtkIdType i = 0; i < iEnd; i++)
124 if(myNodeVTK2ObjIds[i] == theID)
131 //----------------------------------------------------------------------------
133 SALOME_ExtractGeometry
134 ::GetElemObjId(vtkIdType theVtkID)
136 if(!myStoreMapping || myIsDoneShallowCopy)
139 if(theVtkID < myElemVTK2ObjIds.size())
140 return myElemVTK2ObjIds[theVtkID];
147 SALOME_ExtractGeometry
148 ::GetNodeObjId(vtkIdType theVtkID)
150 if(!myStoreMapping || myIsDoneShallowCopy)
153 if(theVtkID < myNodeVTK2ObjIds.size())
154 return myNodeVTK2ObjIds[theVtkID];
160 //----------------------------------------------------------------------------
162 SALOME_ExtractGeometry
163 ::RequestData(vtkInformation *request,
164 vtkInformationVector **inputVector,
165 vtkInformationVector *outputVector)
167 // get the info objects
168 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
169 vtkInformation *outInfo = outputVector->GetInformationObject(0);
171 // get the input and ouptut
172 vtkDataSet *input = vtkDataSet::SafeDownCast(
173 inInfo->Get(vtkDataObject::DATA_OBJECT()));
174 vtkUnstructuredGrid *output = vtkUnstructuredGrid::SafeDownCast(
175 outInfo->Get(vtkDataObject::DATA_OBJECT()));
177 myElemVTK2ObjIds.clear();
178 myNodeVTK2ObjIds.clear();
180 myIsDoneShallowCopy = !this->ImplicitFunction;
182 if(!myIsDoneShallowCopy && myImplicitBoolean.GetPointer())
183 if(vtkImplicitFunctionCollection* aFunction = myImplicitBoolean->GetFunction())
184 myIsDoneShallowCopy = aFunction->GetNumberOfItems() == 0;
186 if(myIsDoneShallowCopy){
187 output->ShallowCopy(input);
191 return RequestData2(request,inputVector,outputVector);
195 //----------------------------------------------------------------------------
197 SALOME_ExtractGeometry
198 ::RequestData2(vtkInformation *vtkNotUsed(request),
199 vtkInformationVector **inputVector,
200 vtkInformationVector *outputVector)
202 // get the info objects
203 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
204 vtkInformation *outInfo = outputVector->GetInformationObject(0);
206 // get the input and ouptut
207 vtkDataSet *input = vtkDataSet::SafeDownCast(
208 inInfo->Get(vtkDataObject::DATA_OBJECT()));
209 vtkUnstructuredGrid *output = vtkUnstructuredGrid::SafeDownCast(
210 outInfo->Get(vtkDataObject::DATA_OBJECT()));
212 vtkIdType ptId, numPts, numCells, i, cellId, newCellId, newId, *pointMap;
216 vtkFloatingPointType *x;
217 vtkFloatingPointType multiplier;
219 vtkIdList *newCellPts;
220 vtkPointData *pd = input->GetPointData();
221 vtkCellData *cd = input->GetCellData();
222 vtkPointData *outputPD = output->GetPointData();
223 vtkCellData *outputCD = output->GetCellData();
225 numCells = input->GetNumberOfCells();
226 numPts = input->GetNumberOfPoints();
228 if ( ! this->ImplicitFunction )
230 vtkErrorMacro(<<"No implicit function specified");
234 newCellPts = vtkIdList::New();
235 newCellPts->Allocate(VTK_CELL_SIZE);
237 if ( this->ExtractInside )
246 // Loop over all points determining whether they are inside the
247 // implicit function. Copy the points and point data if they are.
249 pointMap = new vtkIdType[numPts]; // maps old point ids into new
250 for (i=0; i < numPts; i++)
255 output->Allocate(numCells/4); //allocate storage for geometry/topology
256 newPts = vtkPoints::New();
257 newPts->Allocate(numPts/4,numPts);
258 outputPD->CopyAllocate(pd);
259 outputCD->CopyAllocate(cd);
260 vtkFloatArray *newScalars = NULL;
263 myElemVTK2ObjIds.reserve(numCells);
264 myNodeVTK2ObjIds.reserve(numPts);
267 if ( ! this->ExtractBoundaryCells )
269 for ( ptId=0; ptId < numPts; ptId++ )
271 x = input->GetPoint(ptId);
272 if ( (this->ImplicitFunction->FunctionValue(x)*multiplier) < 0.0 )
274 newId = newPts->InsertNextPoint(x);
275 pointMap[ptId] = newId;
277 myNodeVTK2ObjIds.push_back(ptId);
278 outputPD->CopyData(pd,ptId,newId);
284 // To extract boundary cells, we have to create supplemental information
285 if ( this->ExtractBoundaryCells )
287 vtkFloatingPointType val;
288 newScalars = vtkFloatArray::New();
289 newScalars->SetNumberOfValues(numPts);
291 for (ptId=0; ptId < numPts; ptId++ )
293 x = input->GetPoint(ptId);
294 val = this->ImplicitFunction->FunctionValue(x) * multiplier;
295 newScalars->SetValue(ptId, val);
298 newId = newPts->InsertNextPoint(x);
299 pointMap[ptId] = newId;
301 myNodeVTK2ObjIds.push_back(ptId);
302 outputPD->CopyData(pd,ptId,newId);
308 // Now loop over all cells to see whether they are inside implicit
309 // function (or on boundary if ExtractBoundaryCells is on).
311 for (cellId=0; cellId < numCells; cellId++)
313 cell = input->GetCell(cellId);
314 cellPts = cell->GetPointIds();
315 numCellPts = cell->GetNumberOfPoints();
318 if ( ! this->ExtractBoundaryCells ) //requires less work
320 for ( npts=0, i=0; i < numCellPts; i++, npts++)
322 ptId = cellPts->GetId(i);
323 if ( pointMap[ptId] < 0 )
325 break; //this cell won't be inserted
329 newCellPts->InsertId(i,pointMap[ptId]);
332 } //if don't want to extract boundary cells
334 else //want boundary cells
336 for ( npts=0, i=0; i < numCellPts; i++ )
338 ptId = cellPts->GetId(i);
339 if ( newScalars->GetValue(ptId) <= 0.0 )
346 for ( i=0; i < numCellPts; i++ )
348 ptId = cellPts->GetId(i);
349 if ( pointMap[ptId] < 0 )
351 x = input->GetPoint(ptId);
352 newId = newPts->InsertNextPoint(x);
353 pointMap[ptId] = newId;
355 myNodeVTK2ObjIds.push_back(ptId);
356 outputPD->CopyData(pd,ptId,newId);
358 newCellPts->InsertId(i,pointMap[ptId]);
360 }//a boundary or interior cell
361 }//if mapping boundary cells
363 if ( npts >= numCellPts || (this->ExtractBoundaryCells && npts > 0) )
365 newCellId = output->InsertNextCell(cell->GetCellType(),newCellPts);
367 myElemVTK2ObjIds.push_back(cellId);
368 outputCD->CopyData(cd,cellId,newCellId);
372 // Update ourselves and release memory
375 newCellPts->Delete();
376 output->SetPoints(newPts);
379 if ( this->ExtractBoundaryCells )
381 newScalars->Delete();