3 // Copyright (C) 2003 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
23 // File: VISU_ExtractUnstructuredGrid.cxx
24 // Author: Alexey PETROV
28 #include "VTKViewer_ExtractUnstructuredGrid.h"
29 #include "VTKViewer_CellLocationsArray.h"
31 #include <vtkUnsignedCharArray.h>
32 #include <vtkUnstructuredGrid.h>
33 #include <vtkObjectFactory.h>
34 #include <vtkCellArray.h>
35 #include <vtkIdList.h>
41 static int MYDEBUG = 0;
43 static int MYDEBUG = 0;
52 vtkStandardNewMacro(VTKViewer_ExtractUnstructuredGrid);
55 VTKViewer_ExtractUnstructuredGrid::VTKViewer_ExtractUnstructuredGrid():
56 myExtractionMode(eCells), myChangeMode(ePassAll)
60 VTKViewer_ExtractUnstructuredGrid::~VTKViewer_ExtractUnstructuredGrid(){}
63 void VTKViewer_ExtractUnstructuredGrid::RegisterCell(vtkIdType theCellId){
64 // if(0 && MYDEBUG) MESSAGE("RegisterCell - theCellId = "<<theCellId);
65 myCellIds.insert(theCellId);
70 void VTKViewer_ExtractUnstructuredGrid::RegisterCellsWithType(vtkIdType theCellType){
71 // if(0 && MYDEBUG) MESSAGE("RegisterCellsWithType - theCellType = "<<theCellType);
72 myCellTypes.insert(theCellType);
77 void VTKViewer_ExtractUnstructuredGrid::SetStoreMapping(int theStoreMapping){
78 myStoreMapping = theStoreMapping != 0;
82 vtkIdType VTKViewer_ExtractUnstructuredGrid::GetInputId(int theOutId) const{
83 if(myCellIds.empty() && myCellTypes.empty()) return theOutId;
84 if(myOut2InId.empty() || theOutId > myOut2InId.size()) return -1;
85 #if defined __GNUC_2__
86 return myOut2InId[theOutId];
88 return myOut2InId.at(theOutId);
92 vtkIdType VTKViewer_ExtractUnstructuredGrid::GetOutputId(int theInId) const{
93 if(myCellIds.empty() && myCellTypes.empty()) return theInId;
94 TMapId::const_iterator anIter = myIn2OutId.find(theInId);
95 if(anIter == myIn2OutId.end()) return -1;
96 return anIter->second;
100 inline void InsertCell(vtkUnstructuredGrid *theInput,
101 vtkCellArray *theConnectivity,
102 vtkUnsignedCharArray* theCellTypesArray,
104 vtkIdList *theIdList,
105 bool theStoreMapping,
107 VTKViewer_ExtractUnstructuredGrid::TVectorId& theOut2InId,
108 VTKViewer_ExtractUnstructuredGrid::TMapId& theIn2OutId)
110 vtkCell *aCell = theInput->GetCell(theCellId);
111 vtkIdList *aPntIds = aCell->GetPointIds();
112 vtkIdType aNbIds = aPntIds->GetNumberOfIds();
113 theIdList->SetNumberOfIds(aNbIds);
114 for(vtkIdType i = 0; i < aNbIds; i++){
115 theIdList->SetId(i,aPntIds->GetId(i));
117 theConnectivity->InsertNextCell(theIdList);
119 vtkIdType aCellType = aCell->GetCellType();
120 theCellTypesArray->InsertNextValue(aCellType);
122 theOut2InId.push_back(theCellId);
123 theIn2OutId[theCellId] = theOutId;
127 inline void InsertPointCell(vtkCellArray *theConnectivity,
128 vtkUnsignedCharArray* theCellTypesArray,
130 vtkIdList *theIdList,
131 bool theStoreMapping,
133 VTKViewer_ExtractUnstructuredGrid::TVectorId& theOut2InId,
134 VTKViewer_ExtractUnstructuredGrid::TMapId& theIn2OutId)
136 theIdList->SetId(0,theCellId);
137 theConnectivity->InsertNextCell(theIdList);
138 theCellTypesArray->InsertNextValue(VTK_VERTEX);
140 theOut2InId.push_back(theCellId);
141 theIn2OutId[theCellId] = theOutId;
145 void VTKViewer_ExtractUnstructuredGrid::Execute(){
146 vtkUnstructuredGrid *anInput = this->GetInput();
147 vtkUnstructuredGrid *anOutput = this->GetOutput();
148 myOut2InId.clear(); myIn2OutId.clear();
151 MESSAGE("Execute - anInput->GetNumberOfCells() = "<<anInput->GetNumberOfCells());
152 MESSAGE("Execute - myCellTypes.size() = "<<myCellTypes.size());
153 MESSAGE("Execute - myCellIds.size() = "<<myCellIds.size());
154 MESSAGE("Execute - myExtractionMode = "<<myExtractionMode);
155 MESSAGE("Execute - myChangeMode = "<<myChangeMode);
157 if(myExtractionMode == eCells){
158 if(myChangeMode == ePassAll || myCellIds.empty() && myCellTypes.empty() && myChangeMode == eRemoving){
159 if(vtkIdType aNbElems = anInput->GetNumberOfCells()){
160 if(myStoreMapping) myOut2InId.reserve(aNbElems);
161 anOutput->ShallowCopy(anInput);
162 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
164 myOut2InId.push_back(aCellId);
165 myIn2OutId[aCellId] = anOutId;
170 vtkIdList *anIdList = vtkIdList::New();
171 vtkCellArray *aConnectivity = vtkCellArray::New();
172 vtkIdType aNbElems = anInput->GetNumberOfCells();
173 aConnectivity->Allocate(2*aNbElems,0);
174 vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
175 aCellTypesArray->SetNumberOfComponents(1);
176 aCellTypesArray->Allocate(aNbElems*aCellTypesArray->GetNumberOfComponents());
177 if(!myCellIds.empty() && myCellTypes.empty()){
178 if(myStoreMapping) myOut2InId.reserve(myCellIds.size());
179 if(myChangeMode == eAdding){
180 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
181 if(myCellIds.find(aCellId) != myCellIds.end()){
182 InsertCell(anInput,aConnectivity,aCellTypesArray,aCellId,anIdList,
183 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
187 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
188 if(myCellIds.find(aCellId) == myCellIds.end()){
189 InsertCell(anInput,aConnectivity,aCellTypesArray,aCellId,anIdList,
190 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
194 }else if(myCellIds.empty() && !myCellTypes.empty()){
195 if(myChangeMode == eAdding){
196 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
197 vtkIdType aType = anInput->GetCellType(aCellId);
198 if(myCellTypes.find(aType) != myCellTypes.end()){
199 InsertCell(anInput,aConnectivity,aCellTypesArray,aCellId,anIdList,
200 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
204 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
205 vtkIdType aType = anInput->GetCellType(aCellId);
206 if(myCellTypes.find(aType) == myCellTypes.end()){
207 InsertCell(anInput,aConnectivity,aCellTypesArray,aCellId,anIdList,
208 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
212 }else if(!myCellIds.empty() && !myCellTypes.empty()){
213 if(myChangeMode == eAdding){
214 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
215 vtkIdType aType = anInput->GetCellType(aCellId);
216 if(myCellTypes.find(aType) != myCellTypes.end()){
217 if(myCellIds.find(aCellId) != myCellIds.end()){
218 InsertCell(anInput,aConnectivity,aCellTypesArray,aCellId,anIdList,
219 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
224 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
225 vtkIdType aType = anInput->GetCellType(aCellId);
226 if(myCellTypes.find(aType) == myCellTypes.end()){
227 if(myCellIds.find(aCellId) == myCellIds.end()){
228 InsertCell(anInput,aConnectivity,aCellTypesArray,aCellId,anIdList,
229 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
235 if((aNbElems = aConnectivity->GetNumberOfCells())){
236 VTKViewer_CellLocationsArray* aCellLocationsArray = VTKViewer_CellLocationsArray::New();
237 aCellLocationsArray->SetNumberOfComponents(1);
238 aCellLocationsArray->SetNumberOfTuples(aNbElems);
239 aConnectivity->InitTraversal();
240 for(vtkIdType i = 0, *pts, npts; aConnectivity->GetNextCell(npts,pts); i++){
241 aCellLocationsArray->SetValue(i,aConnectivity->GetTraversalLocation(npts));
243 anOutput->SetCells(aCellTypesArray,aCellLocationsArray,aConnectivity);
244 anOutput->SetPoints(anInput->GetPoints());
245 aCellLocationsArray->Delete();
247 aCellTypesArray->Delete();
248 aConnectivity->Delete();
252 vtkIdList *anIdList = vtkIdList::New();
253 anIdList->SetNumberOfIds(1);
254 vtkCellArray *aConnectivity = vtkCellArray::New();
255 vtkIdType aNbElems = anInput->GetNumberOfPoints();
256 aConnectivity->Allocate(2*aNbElems,0);
257 vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
258 aCellTypesArray->SetNumberOfComponents(1);
259 aCellTypesArray->Allocate(aNbElems*aCellTypesArray->GetNumberOfComponents());
260 if(myChangeMode == ePassAll || myCellIds.empty() && myCellTypes.empty() && myChangeMode == eRemoving){
261 if(myStoreMapping) myOut2InId.reserve(aNbElems);
262 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
263 InsertPointCell(aConnectivity,aCellTypesArray,aCellId,anIdList,
264 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
266 }else if(!myCellIds.empty() && myCellTypes.empty()){
267 if(myStoreMapping) myOut2InId.reserve(myCellIds.size());
268 if(myChangeMode == eAdding){
269 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
270 if(myCellIds.find(aCellId) != myCellIds.end()){
271 InsertPointCell(aConnectivity,aCellTypesArray,aCellId,anIdList,
272 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
276 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
277 if(myCellIds.find(aCellId) == myCellIds.end()){
278 InsertPointCell(aConnectivity,aCellTypesArray,aCellId,anIdList,
279 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
283 }else if(myCellIds.empty() && !myCellTypes.empty()){
284 if(myChangeMode == eAdding){
285 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
286 vtkIdType aType = anInput->GetCellType(aCellId);
287 if(myCellTypes.find(aType) != myCellTypes.end()){
288 InsertPointCell(aConnectivity,aCellTypesArray,aCellId,anIdList,
289 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
293 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
294 vtkIdType aType = anInput->GetCellType(aCellId);
295 if(myCellTypes.find(aType) == myCellTypes.end()){
296 InsertPointCell(aConnectivity,aCellTypesArray,aCellId,anIdList,
297 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
301 }else if(!myCellIds.empty() && !myCellTypes.empty()){
302 if(myChangeMode == eAdding){
303 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
304 vtkIdType aType = anInput->GetCellType(aCellId);
305 if(myCellTypes.find(aType) != myCellTypes.end()){
306 if(myCellIds.find(aCellId) != myCellIds.end()){
307 InsertPointCell(aConnectivity,aCellTypesArray,aCellId,anIdList,
308 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
313 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
314 vtkIdType aType = anInput->GetCellType(aCellId);
315 if(myCellTypes.find(aType) == myCellTypes.end()){
316 if(myCellIds.find(aCellId) == myCellIds.end()){
317 InsertPointCell(aConnectivity,aCellTypesArray,aCellId,anIdList,
318 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
324 if((aNbElems = aConnectivity->GetNumberOfCells())){
325 VTKViewer_CellLocationsArray* aCellLocationsArray = VTKViewer_CellLocationsArray::New();
326 aCellLocationsArray->SetNumberOfComponents(1);
327 aCellLocationsArray->SetNumberOfTuples(aNbElems);
328 aConnectivity->InitTraversal();
329 for(vtkIdType i = 0, *pts, npts; aConnectivity->GetNextCell(npts,pts); i++){
330 aCellLocationsArray->SetValue(i,aConnectivity->GetTraversalLocation(npts));
332 anOutput->SetCells(aCellTypesArray,aCellLocationsArray,aConnectivity);
333 anOutput->SetPoints(anInput->GetPoints());
334 aCellLocationsArray->Delete();
336 aCellTypesArray->Delete();
337 aConnectivity->Delete();
341 MESSAGE("Execute - anOutput->GetNumberOfCells() = "<<anOutput->GetNumberOfCells());
343 MESSAGE("Execute - myOut2InId.size() = "<<myOut2InId.size());
344 MESSAGE("Execute - myIn2OutId.size() = "<<myIn2OutId.size());