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_ExtractPolyDataGeometry.h"
24 #include <vtkCellArray.h>
25 #include <vtkCellData.h>
26 #include <vtkFloatArray.h>
27 #include <vtkImplicitFunction.h>
28 #include <vtkObjectFactory.h>
29 #include <vtkPointData.h>
30 #include <vtkPolyData.h>
32 #include <vtkInformation.h>
33 #include <vtkInformationVector.h>
35 #include <vtkImplicitBoolean.h>
36 #include <vtkImplicitFunctionCollection.h>
38 //----------------------------------------------------------------------------
39 vtkStandardNewMacro(SALOME_ExtractPolyDataGeometry);
42 //----------------------------------------------------------------------------
43 SALOME_ExtractPolyDataGeometry
44 ::SALOME_ExtractPolyDataGeometry():
45 myStoreMapping(false),
46 myIsDoneShallowCopy(false)
49 SALOME_ExtractPolyDataGeometry
50 ::~SALOME_ExtractPolyDataGeometry()
54 //----------------------------------------------------------------------------
56 SALOME_ExtractPolyDataGeometry
57 ::GetImplicitBoolean()
59 return myImplicitBoolean.GetPointer();
64 SALOME_ExtractPolyDataGeometry
65 ::SetImplicitFunction(vtkImplicitFunction* theImplicitFunction)
67 myImplicitBoolean = dynamic_cast<vtkImplicitBoolean*>(theImplicitFunction);
68 Superclass::SetImplicitFunction(theImplicitFunction);
72 //----------------------------------------------------------------------------
74 SALOME_ExtractPolyDataGeometry
75 ::SetStoreMapping(bool theStoreMapping)
77 if(myStoreMapping != theStoreMapping){
78 myStoreMapping = theStoreMapping;
84 SALOME_ExtractPolyDataGeometry
85 ::GetStoreMapping() const
87 return myStoreMapping;
91 //----------------------------------------------------------------------------
93 SALOME_ExtractPolyDataGeometry
94 ::GetElemVTKId(vtkIdType theID)
96 if(!myStoreMapping || myIsDoneShallowCopy)
99 vtkIdType iEnd = myElemVTK2ObjIds.size();
100 for(vtkIdType i = 0; i < iEnd; i++)
101 if(myElemVTK2ObjIds[i] == theID)
108 SALOME_ExtractPolyDataGeometry
109 ::GetNodeVTKId(vtkIdType theID)
111 if(!myStoreMapping || myIsDoneShallowCopy)
114 vtkIdType iEnd = myNodeVTK2ObjIds.size();
115 for(vtkIdType i = 0; i < iEnd; i++)
116 if(myNodeVTK2ObjIds[i] == theID)
123 //----------------------------------------------------------------------------
125 SALOME_ExtractPolyDataGeometry
126 ::GetElemObjId(vtkIdType theVtkID)
128 if(!myStoreMapping || myIsDoneShallowCopy)
131 if(theVtkID < myElemVTK2ObjIds.size())
132 return myElemVTK2ObjIds[theVtkID];
139 SALOME_ExtractPolyDataGeometry
140 ::GetNodeObjId(vtkIdType theVtkID)
142 if(!myStoreMapping || myIsDoneShallowCopy)
145 if(theVtkID < myNodeVTK2ObjIds.size())
146 return myNodeVTK2ObjIds[theVtkID];
152 //----------------------------------------------------------------------------
154 SALOME_ExtractPolyDataGeometry
155 ::RequestData(vtkInformation *request,
156 vtkInformationVector **inputVector,
157 vtkInformationVector *outputVector)
159 myElemVTK2ObjIds.clear();
160 myNodeVTK2ObjIds.clear();
162 myIsDoneShallowCopy = !this->ImplicitFunction;
164 if(!myIsDoneShallowCopy && myImplicitBoolean.GetPointer()){
165 if(vtkImplicitFunctionCollection* aFunction = myImplicitBoolean->GetFunction()){
166 myIsDoneShallowCopy = aFunction->GetNumberOfItems() == 0;
170 if(myIsDoneShallowCopy){
171 GetOutput()->ShallowCopy(GetInput());
175 return RequestData2(request,inputVector,outputVector);
179 //----------------------------------------------------------------------------
181 SALOME_ExtractPolyDataGeometry
182 ::RequestData2(vtkInformation *vtkNotUsed(request),
183 vtkInformationVector **inputVector,
184 vtkInformationVector *outputVector)
186 // get the info objects
187 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
188 vtkInformation *outInfo = outputVector->GetInformationObject(0);
190 // get the input and ouptut
191 vtkDataSet *input = vtkDataSet::SafeDownCast(
192 inInfo->Get(vtkDataObject::DATA_OBJECT()));
193 vtkPolyData *output = vtkPolyData::SafeDownCast(
194 outInfo->Get(vtkDataObject::DATA_OBJECT()));
196 vtkIdType ptId, numPts, numCells, i, cellId, newCellId, newId, *pointMap;
200 vtkFloatingPointType *x;
201 vtkFloatingPointType multiplier;
203 vtkIdList *newCellPts;
204 vtkPointData *pd = input->GetPointData();
205 vtkCellData *cd = input->GetCellData();
206 vtkPointData *outputPD = output->GetPointData();
207 vtkCellData *outputCD = output->GetCellData();
209 numCells = input->GetNumberOfCells();
210 numPts = input->GetNumberOfPoints();
212 if ( ! this->ImplicitFunction )
214 vtkErrorMacro(<<"No implicit function specified");
218 newCellPts = vtkIdList::New();
219 newCellPts->Allocate(VTK_CELL_SIZE);
221 if ( this->ExtractInside )
230 // Loop over all points determining whether they are inside the
231 // implicit function. Copy the points and point data if they are.
233 pointMap = new vtkIdType[numPts]; // maps old point ids into new
234 for (i=0; i < numPts; i++)
239 output->Allocate(numCells/4); //allocate storage for geometry/topology
240 newPts = vtkPoints::New();
241 newPts->Allocate(numPts/4,numPts);
242 outputPD->CopyAllocate(pd);
243 outputCD->CopyAllocate(cd);
244 vtkFloatArray *newScalars = NULL;
247 myElemVTK2ObjIds.reserve(numCells);
248 myNodeVTK2ObjIds.reserve(numPts);
251 if ( ! this->ExtractBoundaryCells )
253 for ( ptId=0; ptId < numPts; ptId++ )
255 x = input->GetPoint(ptId);
256 if ( (this->ImplicitFunction->FunctionValue(x)*multiplier) < 0.0 )
258 newId = newPts->InsertNextPoint(x);
259 pointMap[ptId] = newId;
261 myNodeVTK2ObjIds.push_back(ptId);
262 outputPD->CopyData(pd,ptId,newId);
268 // To extract boundary cells, we have to create supplemental information
269 if ( this->ExtractBoundaryCells )
271 vtkFloatingPointType val;
272 newScalars = vtkFloatArray::New();
273 newScalars->SetNumberOfValues(numPts);
275 for (ptId=0; ptId < numPts; ptId++ )
277 x = input->GetPoint(ptId);
278 val = this->ImplicitFunction->FunctionValue(x) * multiplier;
279 newScalars->SetValue(ptId, val);
282 newId = newPts->InsertNextPoint(x);
283 pointMap[ptId] = newId;
285 myNodeVTK2ObjIds.push_back(ptId);
286 outputPD->CopyData(pd,ptId,newId);
292 // Now loop over all cells to see whether they are inside implicit
293 // function (or on boundary if ExtractBoundaryCells is on).
295 for (cellId=0; cellId < numCells; cellId++)
297 cell = input->GetCell(cellId);
298 cellPts = cell->GetPointIds();
299 numCellPts = cell->GetNumberOfPoints();
302 if ( ! this->ExtractBoundaryCells ) //requires less work
304 for ( npts=0, i=0; i < numCellPts; i++, npts++)
306 ptId = cellPts->GetId(i);
307 if ( pointMap[ptId] < 0 )
309 break; //this cell won't be inserted
313 newCellPts->InsertId(i,pointMap[ptId]);
316 } //if don't want to extract boundary cells
318 else //want boundary cells
320 for ( npts=0, i=0; i < numCellPts; i++ )
322 ptId = cellPts->GetId(i);
323 if ( newScalars->GetValue(ptId) <= 0.0 )
330 for ( i=0; i < numCellPts; i++ )
332 ptId = cellPts->GetId(i);
333 if ( pointMap[ptId] < 0 )
335 x = input->GetPoint(ptId);
336 newId = newPts->InsertNextPoint(x);
337 pointMap[ptId] = newId;
339 myNodeVTK2ObjIds.push_back(ptId);
340 outputPD->CopyData(pd,ptId,newId);
342 newCellPts->InsertId(i,pointMap[ptId]);
344 }//a boundary or interior cell
345 }//if mapping boundary cells
347 if ( npts >= numCellPts || (this->ExtractBoundaryCells && npts > 0) )
349 newCellId = output->InsertNextCell(cell->GetCellType(),newCellPts);
351 myElemVTK2ObjIds.push_back(cellId);
352 outputCD->CopyData(cd,cellId,newCellId);
356 // Update ourselves and release memory
359 newCellPts->Delete();
360 output->SetPoints(newPts);
363 if ( this->ExtractBoundaryCells )
365 newScalars->Delete();
373 //----------------------------------------------------------------------------