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 "SMESH_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>
32 #include <vtkInformation.h>
33 #include <vtkInformationVector.h>
38 static int MYDEBUG = 0;
40 static int MYDEBUG = 0;
50 vtkStandardNewMacro(SMESH_ExtractGeometry);
53 SMESH_ExtractGeometry::SMESH_ExtractGeometry()
57 SMESH_ExtractGeometry::~SMESH_ExtractGeometry(){}
60 vtkIdType SMESH_ExtractGeometry::GetElemObjId(int theVtkID){
61 if( theVtkID < 0 || theVtkID >= myElemVTK2ObjIds.size()) return -1;
62 return myElemVTK2ObjIds[theVtkID];
66 vtkIdType SMESH_ExtractGeometry::GetNodeObjId(int theVtkID){
67 if ( theVtkID < 0 || theVtkID >= myNodeVTK2ObjIds.size()) return -1;
68 return myNodeVTK2ObjIds[theVtkID];
72 int SMESH_ExtractGeometry::RequestData(
73 vtkInformation *vtkNotUsed(request),
74 vtkInformationVector **inputVector,
75 vtkInformationVector *outputVector)
77 // get the info objects
78 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
79 vtkInformation *outInfo = outputVector->GetInformationObject(0);
81 // get the input and ouptut
82 vtkDataSet *input = vtkDataSet::SafeDownCast(
83 inInfo->Get(vtkDataObject::DATA_OBJECT()));
84 vtkUnstructuredGrid *output = vtkUnstructuredGrid::SafeDownCast(
85 outInfo->Get(vtkDataObject::DATA_OBJECT()));
87 vtkIdType ptId, numPts, numCells, i, cellId, newCellId, newId, *pointMap;
91 vtkFloatingPointType *x;
92 vtkFloatingPointType multiplier;
94 vtkIdList *newCellPts;
95 vtkPointData *pd = input->GetPointData();
96 vtkCellData *cd = input->GetCellData();
97 vtkPointData *outputPD = output->GetPointData();
98 vtkCellData *outputCD = output->GetCellData();
100 numCells = input->GetNumberOfCells();
101 numPts = input->GetNumberOfPoints();
103 vtkDebugMacro(<< "Extracting geometry");
105 if ( ! this->ImplicitFunction )
107 vtkErrorMacro(<<"No implicit function specified");
111 newCellPts = vtkIdList::New();
112 newCellPts->Allocate(VTK_CELL_SIZE);
114 if ( this->ExtractInside )
123 // Loop over all points determining whether they are inside the
124 // implicit function. Copy the points and point data if they are.
126 pointMap = new vtkIdType[numPts]; // maps old point ids into new
127 for (i=0; i < numPts; i++)
132 output->Allocate(numCells/4); //allocate storage for geometry/topology
133 newPts = vtkPoints::New();
134 newPts->Allocate(numPts/4,numPts);
135 outputPD->CopyAllocate(pd);
136 outputCD->CopyAllocate(cd);
137 vtkFloatArray *newScalars = NULL;
140 myElemVTK2ObjIds.clear();
141 myElemVTK2ObjIds.reserve(numCells);
142 myNodeVTK2ObjIds.clear();
143 myNodeVTK2ObjIds.reserve(numPts);
146 if ( ! this->ExtractBoundaryCells )
148 for ( ptId=0; ptId < numPts; ptId++ )
150 x = input->GetPoint(ptId);
151 if ( (this->ImplicitFunction->FunctionValue(x)*multiplier) < 0.0 )
153 newId = newPts->InsertNextPoint(x);
154 pointMap[ptId] = newId;
155 myNodeVTK2ObjIds.push_back(ptId);
156 outputPD->CopyData(pd,ptId,newId);
162 // To extract boundary cells, we have to create supplemental information
163 if ( this->ExtractBoundaryCells )
165 vtkFloatingPointType val;
166 newScalars = vtkFloatArray::New();
167 newScalars->SetNumberOfValues(numPts);
169 for (ptId=0; ptId < numPts; ptId++ )
171 x = input->GetPoint(ptId);
172 val = this->ImplicitFunction->FunctionValue(x) * multiplier;
173 newScalars->SetValue(ptId, val);
176 newId = newPts->InsertNextPoint(x);
177 pointMap[ptId] = newId;
178 myNodeVTK2ObjIds.push_back(ptId);
179 outputPD->CopyData(pd,ptId,newId);
185 // Now loop over all cells to see whether they are inside implicit
186 // function (or on boundary if ExtractBoundaryCells is on).
188 for (cellId=0; cellId < numCells; cellId++)
190 cell = input->GetCell(cellId);
191 cellPts = cell->GetPointIds();
192 numCellPts = cell->GetNumberOfPoints();
195 if ( ! this->ExtractBoundaryCells ) //requires less work
197 for ( npts=0, i=0; i < numCellPts; i++, npts++)
199 ptId = cellPts->GetId(i);
200 if ( pointMap[ptId] < 0 )
202 break; //this cell won't be inserted
206 newCellPts->InsertId(i,pointMap[ptId]);
209 } //if don't want to extract boundary cells
211 else //want boundary cells
213 for ( npts=0, i=0; i < numCellPts; i++ )
215 ptId = cellPts->GetId(i);
216 if ( newScalars->GetValue(ptId) <= 0.0 )
223 for ( i=0; i < numCellPts; i++ )
225 ptId = cellPts->GetId(i);
226 if ( pointMap[ptId] < 0 )
228 x = input->GetPoint(ptId);
229 newId = newPts->InsertNextPoint(x);
230 pointMap[ptId] = newId;
231 myNodeVTK2ObjIds.push_back(ptId);
232 outputPD->CopyData(pd,ptId,newId);
234 newCellPts->InsertId(i,pointMap[ptId]);
236 }//a boundary or interior cell
237 }//if mapping boundary cells
239 if ( npts >= numCellPts || (this->ExtractBoundaryCells && npts > 0) )
241 newCellId = output->InsertNextCell(cell->GetCellType(),newCellPts);
242 myElemVTK2ObjIds.push_back(cellId);
243 outputCD->CopyData(cd,cellId,newCellId);
247 // Update ourselves and release memory
250 newCellPts->Delete();
251 output->SetPoints(newPts);
254 if ( this->ExtractBoundaryCells )
256 newScalars->Delete();