1 // Copyright (C) 2007-2023 CEA, EDF, 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, or (at your option) any later version.
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>
45 vtkStandardNewMacro(SMESH_ExtractGeometry)
48 SMESH_ExtractGeometry::SMESH_ExtractGeometry()
52 SMESH_ExtractGeometry::~SMESH_ExtractGeometry()
55 vtkIdType SMESH_ExtractGeometry::GetElemObjId(vtkIdType theVtkID)
57 if( theVtkID < 0 || theVtkID >= myElemVTK2ObjIds.size()) return -1;
58 return myElemVTK2ObjIds[theVtkID];
62 vtkIdType SMESH_ExtractGeometry::GetNodeObjId(vtkIdType theVtkID)
64 if ( theVtkID < 0 || theVtkID >= myNodeVTK2ObjIds.size()) return -1;
65 return myNodeVTK2ObjIds[theVtkID];
69 int SMESH_ExtractGeometry::RequestData(vtkInformation *vtkNotUsed(request),
70 vtkInformationVector **inputVector,
71 vtkInformationVector *outputVector)
73 // get the info objects
74 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
75 vtkInformation *outInfo = outputVector->GetInformationObject(0);
77 // get the input and output
79 vtkDataSet::SafeDownCast(inInfo->Get(vtkDataObject::DATA_OBJECT()));
80 vtkUnstructuredGrid *output =
81 vtkUnstructuredGrid::SafeDownCast(outInfo->Get(vtkDataObject::DATA_OBJECT()));
83 vtkIdType ptId, numPts, numCells, i, cellId, newCellId, newId, *pointMap;
90 vtkIdList *newCellPts;
91 vtkPointData *pd = input->GetPointData();
92 vtkCellData *cd = input->GetCellData();
93 vtkPointData *outputPD = output->GetPointData();
94 vtkCellData *outputCD = output->GetCellData();
96 numCells = input->GetNumberOfCells();
97 numPts = input->GetNumberOfPoints();
99 vtkDebugMacro(<< "Extracting geometry");
101 if ( ! this->ImplicitFunction )
103 vtkErrorMacro(<<"No implicit function specified");
107 newCellPts = vtkIdList::New();
108 newCellPts->Allocate(VTK_CELL_SIZE);
110 if ( this->ExtractInside )
119 // Loop over all points determining whether they are inside the
120 // implicit function. Copy the points and point data if they are.
122 pointMap = new vtkIdType[numPts]; // maps old point ids into new
123 for (i=0; i < numPts; i++)
128 output->Allocate(numCells/4); //allocate storage for geometry/topology
129 newPts = vtkPoints::New();
130 newPts->Allocate(numPts/4,numPts);
131 outputPD->CopyAllocate(pd);
132 outputCD->CopyAllocate(cd);
133 vtkFloatArray *newScalars = NULL;
136 myElemVTK2ObjIds.clear();
137 myElemVTK2ObjIds.reserve(numCells);
138 myNodeVTK2ObjIds.clear();
139 myNodeVTK2ObjIds.reserve(numPts);
142 if ( ! this->ExtractBoundaryCells )
144 for ( ptId=0; ptId < numPts; ptId++ )
146 x = input->GetPoint(ptId);
147 if ( (this->ImplicitFunction->FunctionValue(x)*multiplier) < 0.0 )
149 newId = newPts->InsertNextPoint(x);
150 pointMap[ptId] = newId;
151 myNodeVTK2ObjIds.push_back(ptId);
152 outputPD->CopyData(pd,ptId,newId);
158 // To extract boundary cells, we have to create supplemental information
159 if ( this->ExtractBoundaryCells )
162 newScalars = vtkFloatArray::New();
163 newScalars->SetNumberOfValues(numPts);
165 for (ptId=0; ptId < numPts; ptId++ )
167 x = input->GetPoint(ptId);
168 val = this->ImplicitFunction->FunctionValue(x) * multiplier;
169 newScalars->SetValue(ptId, val);
172 newId = newPts->InsertNextPoint(x);
173 pointMap[ptId] = newId;
174 myNodeVTK2ObjIds.push_back(ptId);
175 outputPD->CopyData(pd,ptId,newId);
181 // Now loop over all cells to see whether they are inside implicit
182 // function (or on boundary if ExtractBoundaryCells is on).
184 for (cellId=0; cellId < numCells; cellId++)
186 cell = input->GetCell(cellId);
187 cellPts = cell->GetPointIds();
188 numCellPts = cell->GetNumberOfPoints();
191 if ( ! this->ExtractBoundaryCells ) //requires less work
193 for ( npts=0, i=0; i < numCellPts; i++, npts++)
195 ptId = cellPts->GetId(i);
196 if ( pointMap[ptId] < 0 )
198 break; //this cell won't be inserted
202 newCellPts->InsertId(i,pointMap[ptId]);
205 } //if don't want to extract boundary cells
207 else //want boundary cells
209 for ( npts=0, i=0; i < numCellPts; i++ )
211 ptId = cellPts->GetId(i);
212 if ( newScalars->GetValue(ptId) <= 0.0 )
219 for ( i=0; i < numCellPts; i++ )
221 ptId = cellPts->GetId(i);
222 if ( pointMap[ptId] < 0 )
224 x = input->GetPoint(ptId);
225 newId = newPts->InsertNextPoint(x);
226 pointMap[ptId] = newId;
227 myNodeVTK2ObjIds.push_back(ptId);
228 outputPD->CopyData(pd,ptId,newId);
230 newCellPts->InsertId(i,pointMap[ptId]);
232 }//a boundary or interior cell
233 }//if mapping boundary cells
235 if ( npts >= numCellPts || (this->ExtractBoundaryCells && npts > 0) )
237 if(cell->GetCellType() == VTK_POLYHEDRON) {
239 vtkUnstructuredGrid::SafeDownCast(input)->GetFaceStream( cellId ,newCellPts );
240 vtkUnstructuredGrid::ConvertFaceStreamPointIds(newCellPts, pointMap);
242 newCellId = output->InsertNextCell(cell->GetCellType(),newCellPts);
243 myElemVTK2ObjIds.push_back(cellId);
244 outputCD->CopyData(cd,cellId,newCellId);
248 // Update ourselves and release memory
251 newCellPts->Delete();
252 output->SetPoints(newPts);
255 if ( this->ExtractBoundaryCells )
257 newScalars->Delete();