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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
23 // File: VISU_ExtractUnstructuredGrid.cxx
24 // Author: Alexey PETROV
28 #include "VTKViewer_ExtractUnstructuredGrid.h"
29 //#include "utilities.h"
31 #include <vtkUnsignedCharArray.h>
32 #include <vtkUnstructuredGrid.h>
33 #include <vtkObjectFactory.h>
34 #include <vtkCellArray.h>
35 #include <vtkIntArray.h>
36 #include <vtkIdList.h>
42 static int MYDEBUG = 0;
44 static int MYDEBUG = 0;
53 vtkStandardNewMacro(VTKViewer_ExtractUnstructuredGrid);
56 VTKViewer_ExtractUnstructuredGrid::VTKViewer_ExtractUnstructuredGrid():
57 myExtractionMode(eCells), myChangeMode(ePassAll)
61 VTKViewer_ExtractUnstructuredGrid::~VTKViewer_ExtractUnstructuredGrid(){}
64 void VTKViewer_ExtractUnstructuredGrid::RegisterCell(vtkIdType theCellId){
65 // if(0 && MYDEBUG) MESSAGE("RegisterCell - theCellId = "<<theCellId);
66 myCellIds.insert(theCellId);
71 void VTKViewer_ExtractUnstructuredGrid::RegisterCellsWithType(vtkIdType theCellType){
72 // if(0 && MYDEBUG) MESSAGE("RegisterCellsWithType - theCellType = "<<theCellType);
73 myCellTypes.insert(theCellType);
78 void VTKViewer_ExtractUnstructuredGrid::SetStoreMapping(int theStoreMapping){
79 myStoreMapping = theStoreMapping != 0;
83 vtkIdType VTKViewer_ExtractUnstructuredGrid::GetInputId(int theOutId) const{
84 if(myCellIds.empty() && myCellTypes.empty()) return theOutId;
85 if(myOut2InId.empty() || theOutId > myOut2InId.size()) return -1;
86 #if defined __GNUC_2__
87 return myOut2InId[theOutId];
89 return myOut2InId.at(theOutId);
93 vtkIdType VTKViewer_ExtractUnstructuredGrid::GetOutputId(int theInId) const{
94 if(myCellIds.empty() && myCellTypes.empty()) return theInId;
95 TMapId::const_iterator anIter = myIn2OutId.find(theInId);
96 if(anIter == myIn2OutId.end()) return -1;
97 return anIter->second;
101 inline void InsertCell(vtkUnstructuredGrid *theInput,
102 vtkCellArray *theConnectivity,
103 vtkUnsignedCharArray* theCellTypesArray,
105 vtkIdList *theIdList,
106 bool theStoreMapping,
108 VTKViewer_ExtractUnstructuredGrid::TVectorId& theOut2InId,
109 VTKViewer_ExtractUnstructuredGrid::TMapId& theIn2OutId)
111 vtkCell *aCell = theInput->GetCell(theCellId);
112 vtkIdList *aPntIds = aCell->GetPointIds();
113 vtkIdType aNbIds = aPntIds->GetNumberOfIds();
114 theIdList->SetNumberOfIds(aNbIds);
115 for(vtkIdType i = 0; i < aNbIds; i++){
116 theIdList->SetId(i,aPntIds->GetId(i));
118 theConnectivity->InsertNextCell(theIdList);
120 vtkIdType aCellType = aCell->GetCellType();
121 theCellTypesArray->InsertNextValue(aCellType);
123 theOut2InId.push_back(theCellId);
124 theIn2OutId[theCellId] = theOutId;
128 inline void InsertPointCell(vtkCellArray *theConnectivity,
129 vtkUnsignedCharArray* theCellTypesArray,
131 vtkIdList *theIdList,
132 bool theStoreMapping,
134 VTKViewer_ExtractUnstructuredGrid::TVectorId& theOut2InId,
135 VTKViewer_ExtractUnstructuredGrid::TMapId& theIn2OutId)
137 theIdList->SetId(0,theCellId);
138 theConnectivity->InsertNextCell(theIdList);
139 theCellTypesArray->InsertNextValue(VTK_VERTEX);
141 theOut2InId.push_back(theCellId);
142 theIn2OutId[theCellId] = theOutId;
146 void VTKViewer_ExtractUnstructuredGrid::Execute(){
147 vtkUnstructuredGrid *anInput = this->GetInput();
148 vtkUnstructuredGrid *anOutput = this->GetOutput();
149 myOut2InId.clear(); myIn2OutId.clear();
152 MESSAGE("Execute - anInput->GetNumberOfCells() = "<<anInput->GetNumberOfCells());
153 MESSAGE("Execute - myCellTypes.size() = "<<myCellTypes.size());
154 MESSAGE("Execute - myCellIds.size() = "<<myCellIds.size());
155 MESSAGE("Execute - myExtractionMode = "<<myExtractionMode);
156 MESSAGE("Execute - myChangeMode = "<<myChangeMode);
158 if(myExtractionMode == eCells){
159 if(myChangeMode == ePassAll || myCellIds.empty() && myCellTypes.empty() && myChangeMode == eRemoving){
160 if(vtkIdType aNbElems = anInput->GetNumberOfCells()){
161 if(myStoreMapping) myOut2InId.reserve(aNbElems);
162 anOutput->ShallowCopy(anInput);
163 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
165 myOut2InId.push_back(aCellId);
166 myIn2OutId[aCellId] = anOutId;
171 vtkIdList *anIdList = vtkIdList::New();
172 vtkCellArray *aConnectivity = vtkCellArray::New();
173 vtkIdType aNbElems = anInput->GetNumberOfCells();
174 aConnectivity->Allocate(2*aNbElems,0);
175 vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
176 aCellTypesArray->SetNumberOfComponents(1);
177 aCellTypesArray->Allocate(aNbElems*aCellTypesArray->GetNumberOfComponents());
178 if(!myCellIds.empty() && myCellTypes.empty()){
179 if(myStoreMapping) myOut2InId.reserve(myCellIds.size());
180 if(myChangeMode == eAdding){
181 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
182 if(myCellIds.find(aCellId) != myCellIds.end()){
183 InsertCell(anInput,aConnectivity,aCellTypesArray,aCellId,anIdList,
184 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
188 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
189 if(myCellIds.find(aCellId) == myCellIds.end()){
190 InsertCell(anInput,aConnectivity,aCellTypesArray,aCellId,anIdList,
191 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
195 }else if(myCellIds.empty() && !myCellTypes.empty()){
196 if(myChangeMode == eAdding){
197 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
198 vtkIdType aType = anInput->GetCellType(aCellId);
199 if(myCellTypes.find(aType) != myCellTypes.end()){
200 InsertCell(anInput,aConnectivity,aCellTypesArray,aCellId,anIdList,
201 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
205 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
206 vtkIdType aType = anInput->GetCellType(aCellId);
207 if(myCellTypes.find(aType) == myCellTypes.end()){
208 InsertCell(anInput,aConnectivity,aCellTypesArray,aCellId,anIdList,
209 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
213 }else if(!myCellIds.empty() && !myCellTypes.empty()){
214 if(myChangeMode == eAdding){
215 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
216 vtkIdType aType = anInput->GetCellType(aCellId);
217 if(myCellTypes.find(aType) != myCellTypes.end()){
218 if(myCellIds.find(aCellId) != myCellIds.end()){
219 InsertCell(anInput,aConnectivity,aCellTypesArray,aCellId,anIdList,
220 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
225 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
226 vtkIdType aType = anInput->GetCellType(aCellId);
227 if(myCellTypes.find(aType) == myCellTypes.end()){
228 if(myCellIds.find(aCellId) == myCellIds.end()){
229 InsertCell(anInput,aConnectivity,aCellTypesArray,aCellId,anIdList,
230 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
236 if((aNbElems = aConnectivity->GetNumberOfCells())){
237 vtkIntArray* aCellLocationsArray = vtkIntArray::New();
238 aCellLocationsArray->SetNumberOfComponents(1);
239 aCellLocationsArray->SetNumberOfTuples(aNbElems);
240 aConnectivity->InitTraversal();
241 for(vtkIdType i = 0, *pts, npts; aConnectivity->GetNextCell(npts,pts); i++){
242 aCellLocationsArray->SetValue(i,aConnectivity->GetTraversalLocation(npts));
244 anOutput->SetCells(aCellTypesArray,aCellLocationsArray,aConnectivity);
245 anOutput->SetPoints(anInput->GetPoints());
246 aCellLocationsArray->Delete();
248 aCellTypesArray->Delete();
249 aConnectivity->Delete();
253 vtkIdList *anIdList = vtkIdList::New();
254 anIdList->SetNumberOfIds(1);
255 vtkCellArray *aConnectivity = vtkCellArray::New();
256 vtkIdType aNbElems = anInput->GetNumberOfPoints();
257 aConnectivity->Allocate(2*aNbElems,0);
258 vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
259 aCellTypesArray->SetNumberOfComponents(1);
260 aCellTypesArray->Allocate(aNbElems*aCellTypesArray->GetNumberOfComponents());
261 if(myChangeMode == ePassAll || myCellIds.empty() && myCellTypes.empty() && myChangeMode == eRemoving){
262 if(myStoreMapping) myOut2InId.reserve(aNbElems);
263 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
264 InsertPointCell(aConnectivity,aCellTypesArray,aCellId,anIdList,
265 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
267 }else if(!myCellIds.empty() && myCellTypes.empty()){
268 if(myStoreMapping) myOut2InId.reserve(myCellIds.size());
269 if(myChangeMode == eAdding){
270 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
271 if(myCellIds.find(aCellId) != myCellIds.end()){
272 InsertPointCell(aConnectivity,aCellTypesArray,aCellId,anIdList,
273 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
277 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
278 if(myCellIds.find(aCellId) == myCellIds.end()){
279 InsertPointCell(aConnectivity,aCellTypesArray,aCellId,anIdList,
280 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
284 }else if(myCellIds.empty() && !myCellTypes.empty()){
285 if(myChangeMode == eAdding){
286 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
287 vtkIdType aType = anInput->GetCellType(aCellId);
288 if(myCellTypes.find(aType) != myCellTypes.end()){
289 InsertPointCell(aConnectivity,aCellTypesArray,aCellId,anIdList,
290 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
294 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
295 vtkIdType aType = anInput->GetCellType(aCellId);
296 if(myCellTypes.find(aType) == myCellTypes.end()){
297 InsertPointCell(aConnectivity,aCellTypesArray,aCellId,anIdList,
298 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
302 }else if(!myCellIds.empty() && !myCellTypes.empty()){
303 if(myChangeMode == eAdding){
304 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
305 vtkIdType aType = anInput->GetCellType(aCellId);
306 if(myCellTypes.find(aType) != myCellTypes.end()){
307 if(myCellIds.find(aCellId) != myCellIds.end()){
308 InsertPointCell(aConnectivity,aCellTypesArray,aCellId,anIdList,
309 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
314 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
315 vtkIdType aType = anInput->GetCellType(aCellId);
316 if(myCellTypes.find(aType) == myCellTypes.end()){
317 if(myCellIds.find(aCellId) == myCellIds.end()){
318 InsertPointCell(aConnectivity,aCellTypesArray,aCellId,anIdList,
319 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
325 if((aNbElems = aConnectivity->GetNumberOfCells())){
326 vtkIntArray* aCellLocationsArray = vtkIntArray::New();
327 aCellLocationsArray->SetNumberOfComponents(1);
328 aCellLocationsArray->SetNumberOfTuples(aNbElems);
329 aConnectivity->InitTraversal();
330 for(vtkIdType i = 0, *pts, npts; aConnectivity->GetNextCell(npts,pts); i++){
331 aCellLocationsArray->SetValue(i,aConnectivity->GetTraversalLocation(npts));
333 anOutput->SetCells(aCellTypesArray,aCellLocationsArray,aConnectivity);
334 anOutput->SetPoints(anInput->GetPoints());
335 aCellLocationsArray->Delete();
337 aCellTypesArray->Delete();
338 aConnectivity->Delete();
342 MESSAGE("Execute - anOutput->GetNumberOfCells() = "<<anOutput->GetNumberOfCells());
344 MESSAGE("Execute - myOut2InId.size() = "<<myOut2InId.size());
345 MESSAGE("Execute - myIn2OutId.size() = "<<myIn2OutId.size());