1 // Copyright (C) 2007-2016 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, 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>
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)
63 if( theVtkID < 0 || theVtkID >= (int)myElemVTK2ObjIds.size()) return -1;
64 return myElemVTK2ObjIds[theVtkID];
68 vtkIdType SMESH_ExtractGeometry::GetNodeObjId(int theVtkID)
70 if ( theVtkID < 0 || theVtkID >= (int)myNodeVTK2ObjIds.size()) return -1;
71 return myNodeVTK2ObjIds[theVtkID];
75 int SMESH_ExtractGeometry::RequestData(vtkInformation *vtkNotUsed(request),
76 vtkInformationVector **inputVector,
77 vtkInformationVector *outputVector)
79 // get the info objects
80 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
81 vtkInformation *outInfo = outputVector->GetInformationObject(0);
83 // get the input and output
85 vtkDataSet::SafeDownCast(inInfo->Get(vtkDataObject::DATA_OBJECT()));
86 vtkUnstructuredGrid *output =
87 vtkUnstructuredGrid::SafeDownCast(outInfo->Get(vtkDataObject::DATA_OBJECT()));
89 vtkIdType ptId, numPts, numCells, i, cellId, newCellId, newId, *pointMap;
96 vtkIdList *newCellPts;
97 vtkPointData *pd = input->GetPointData();
98 vtkCellData *cd = input->GetCellData();
99 vtkPointData *outputPD = output->GetPointData();
100 vtkCellData *outputCD = output->GetCellData();
102 numCells = input->GetNumberOfCells();
103 numPts = input->GetNumberOfPoints();
105 vtkDebugMacro(<< "Extracting geometry");
107 if ( ! this->ImplicitFunction )
109 vtkErrorMacro(<<"No implicit function specified");
113 newCellPts = vtkIdList::New();
114 newCellPts->Allocate(VTK_CELL_SIZE);
116 if ( this->ExtractInside )
125 // Loop over all points determining whether they are inside the
126 // implicit function. Copy the points and point data if they are.
128 pointMap = new vtkIdType[numPts]; // maps old point ids into new
129 for (i=0; i < numPts; i++)
134 output->Allocate(numCells/4); //allocate storage for geometry/topology
135 newPts = vtkPoints::New();
136 newPts->Allocate(numPts/4,numPts);
137 outputPD->CopyAllocate(pd);
138 outputCD->CopyAllocate(cd);
139 vtkFloatArray *newScalars = NULL;
142 myElemVTK2ObjIds.clear();
143 myElemVTK2ObjIds.reserve(numCells);
144 myNodeVTK2ObjIds.clear();
145 myNodeVTK2ObjIds.reserve(numPts);
148 if ( ! this->ExtractBoundaryCells )
150 for ( ptId=0; ptId < numPts; ptId++ )
152 x = input->GetPoint(ptId);
153 if ( (this->ImplicitFunction->FunctionValue(x)*multiplier) < 0.0 )
155 newId = newPts->InsertNextPoint(x);
156 pointMap[ptId] = newId;
157 myNodeVTK2ObjIds.push_back(ptId);
158 outputPD->CopyData(pd,ptId,newId);
164 // To extract boundary cells, we have to create supplemental information
165 if ( this->ExtractBoundaryCells )
168 newScalars = vtkFloatArray::New();
169 newScalars->SetNumberOfValues(numPts);
171 for (ptId=0; ptId < numPts; ptId++ )
173 x = input->GetPoint(ptId);
174 val = this->ImplicitFunction->FunctionValue(x) * multiplier;
175 newScalars->SetValue(ptId, val);
178 newId = newPts->InsertNextPoint(x);
179 pointMap[ptId] = newId;
180 myNodeVTK2ObjIds.push_back(ptId);
181 outputPD->CopyData(pd,ptId,newId);
187 // Now loop over all cells to see whether they are inside implicit
188 // function (or on boundary if ExtractBoundaryCells is on).
190 for (cellId=0; cellId < numCells; cellId++)
192 cell = input->GetCell(cellId);
193 cellPts = cell->GetPointIds();
194 numCellPts = cell->GetNumberOfPoints();
197 if ( ! this->ExtractBoundaryCells ) //requires less work
199 for ( npts=0, i=0; i < numCellPts; i++, npts++)
201 ptId = cellPts->GetId(i);
202 if ( pointMap[ptId] < 0 )
204 break; //this cell won't be inserted
208 newCellPts->InsertId(i,pointMap[ptId]);
211 } //if don't want to extract boundary cells
213 else //want boundary cells
215 for ( npts=0, i=0; i < numCellPts; i++ )
217 ptId = cellPts->GetId(i);
218 if ( newScalars->GetValue(ptId) <= 0.0 )
225 for ( i=0; i < numCellPts; i++ )
227 ptId = cellPts->GetId(i);
228 if ( pointMap[ptId] < 0 )
230 x = input->GetPoint(ptId);
231 newId = newPts->InsertNextPoint(x);
232 pointMap[ptId] = newId;
233 myNodeVTK2ObjIds.push_back(ptId);
234 outputPD->CopyData(pd,ptId,newId);
236 newCellPts->InsertId(i,pointMap[ptId]);
238 }//a boundary or interior cell
239 }//if mapping boundary cells
241 if ( npts >= numCellPts || (this->ExtractBoundaryCells && npts > 0) )
243 if(cell->GetCellType() == VTK_POLYHEDRON) {
245 vtkUnstructuredGrid::SafeDownCast(input)->GetFaceStream( cellId ,newCellPts );
246 vtkUnstructuredGrid::ConvertFaceStreamPointIds(newCellPts, pointMap);
248 newCellId = output->InsertNextCell(cell->GetCellType(),newCellPts);
249 myElemVTK2ObjIds.push_back(cellId);
250 outputCD->CopyData(cd,cellId,newCellId);
254 // Update ourselves and release memory
257 newCellPts->Delete();
258 output->SetPoints(newPts);
261 if ( this->ExtractBoundaryCells )
263 newScalars->Delete();