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
23 // File: VISU_ExtractUnstructuredGrid.cxx
24 // Author: Alexey PETROV
27 #include "VTKViewer_ExtractUnstructuredGrid.h"
28 #include "VTKViewer_CellLocationsArray.h"
30 #include <vtkUnsignedCharArray.h>
31 #include <vtkUnstructuredGrid.h>
32 #include <vtkObjectFactory.h>
33 #include <vtkCellArray.h>
34 #include <vtkIdList.h>
36 #include <vtkInformation.h>
37 #include <vtkInformationVector.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
85 if ( myCellIds.empty() && myCellTypes.empty() )
88 if ( myOut2InId.empty() || theOutId > (int)myOut2InId.size() )
90 #if defined __GNUC_2__
91 return myOut2InId[theOutId];
93 return myOut2InId.at(theOutId);
97 vtkIdType VTKViewer_ExtractUnstructuredGrid::GetOutputId(int theInId) const{
98 if(myCellIds.empty() && myCellTypes.empty()) return theInId;
99 TMapId::const_iterator anIter = myIn2OutId.find(theInId);
100 if(anIter == myIn2OutId.end()) return -1;
101 return anIter->second;
105 inline void InsertCell(vtkUnstructuredGrid *theInput,
106 vtkCellArray *theConnectivity,
107 vtkUnsignedCharArray* theCellTypesArray,
109 vtkIdList *theIdList,
110 bool theStoreMapping,
112 VTKViewer_ExtractUnstructuredGrid::TVectorId& theOut2InId,
113 VTKViewer_ExtractUnstructuredGrid::TMapId& theIn2OutId)
115 vtkCell *aCell = theInput->GetCell(theCellId);
116 vtkIdList *aPntIds = aCell->GetPointIds();
117 vtkIdType aNbIds = aPntIds->GetNumberOfIds();
118 theIdList->SetNumberOfIds(aNbIds);
119 for(vtkIdType i = 0; i < aNbIds; i++){
120 theIdList->SetId(i,aPntIds->GetId(i));
122 theConnectivity->InsertNextCell(theIdList);
124 vtkIdType aCellType = aCell->GetCellType();
125 theCellTypesArray->InsertNextValue(aCellType);
127 theOut2InId.push_back(theCellId);
128 theIn2OutId[theCellId] = theOutId;
132 inline void InsertPointCell(vtkCellArray *theConnectivity,
133 vtkUnsignedCharArray* theCellTypesArray,
135 vtkIdList *theIdList,
136 bool theStoreMapping,
138 VTKViewer_ExtractUnstructuredGrid::TVectorId& theOut2InId,
139 VTKViewer_ExtractUnstructuredGrid::TMapId& theIn2OutId)
141 theIdList->SetId(0,theCellId);
142 theConnectivity->InsertNextCell(theIdList);
143 theCellTypesArray->InsertNextValue(VTK_VERTEX);
145 theOut2InId.push_back(theCellId);
146 theIn2OutId[theCellId] = theOutId;
151 // int VTKViewer_ExtractUnstructuredGrid::RequestData(
152 // vtkInformation *vtkNotUsed(request),
153 // vtkInformationVector **inputVector,
154 // vtkInformationVector *outputVector)
155 void VTKViewer_ExtractUnstructuredGrid::Execute()
158 not ported yet to the new executive-based pipeline architecture.
160 // get the info objects
161 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
162 vtkInformation *outInfo = outputVector->GetInformationObject(0);
164 // get the input and ouptut
165 vtkUnstructuredGrid *anInput = vtkUnstructuredGrid::SafeDownCast(
166 inInfo->Get(vtkDataObject::DATA_OBJECT()));
167 vtkUnstructuredGrid *anOutput = vtkUnstructuredGrid::SafeDownCast(
168 outInfo->Get(vtkDataObject::DATA_OBJECT()));
170 vtkUnstructuredGrid *anInput = this->GetInput();
171 vtkUnstructuredGrid *anOutput = this->GetOutput();
173 myOut2InId.clear(); myIn2OutId.clear();
176 MESSAGE("Execute - anInput->GetNumberOfCells() = "<<anInput->GetNumberOfCells());
177 MESSAGE("Execute - myCellTypes.size() = "<<myCellTypes.size());
178 MESSAGE("Execute - myCellIds.size() = "<<myCellIds.size());
179 MESSAGE("Execute - myExtractionMode = "<<myExtractionMode);
180 MESSAGE("Execute - myChangeMode = "<<myChangeMode);
182 if(myExtractionMode == eCells){
183 if(myChangeMode == ePassAll || myCellIds.empty() && myCellTypes.empty() && myChangeMode == eRemoving){
184 if(vtkIdType aNbElems = anInput->GetNumberOfCells()){
185 if(myStoreMapping) myOut2InId.reserve(aNbElems);
186 anOutput->ShallowCopy(anInput);
187 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
189 myOut2InId.push_back(aCellId);
190 myIn2OutId[aCellId] = anOutId;
195 vtkIdList *anIdList = vtkIdList::New();
196 vtkCellArray *aConnectivity = vtkCellArray::New();
197 vtkIdType aNbElems = anInput->GetNumberOfCells();
198 aConnectivity->Allocate(2*aNbElems,0);
199 vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
200 aCellTypesArray->SetNumberOfComponents(1);
201 aCellTypesArray->Allocate(aNbElems*aCellTypesArray->GetNumberOfComponents());
202 if(!myCellIds.empty() && myCellTypes.empty()){
203 if(myStoreMapping) myOut2InId.reserve(myCellIds.size());
204 if(myChangeMode == eAdding){
205 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
206 if(myCellIds.find(aCellId) != myCellIds.end()){
207 InsertCell(anInput,aConnectivity,aCellTypesArray,aCellId,anIdList,
208 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
212 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
213 if(myCellIds.find(aCellId) == myCellIds.end()){
214 InsertCell(anInput,aConnectivity,aCellTypesArray,aCellId,anIdList,
215 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
219 }else if(myCellIds.empty() && !myCellTypes.empty()){
220 if(myChangeMode == eAdding){
221 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
222 vtkIdType aType = anInput->GetCellType(aCellId);
223 if(myCellTypes.find(aType) != myCellTypes.end()){
224 InsertCell(anInput,aConnectivity,aCellTypesArray,aCellId,anIdList,
225 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
229 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
230 vtkIdType aType = anInput->GetCellType(aCellId);
231 if(myCellTypes.find(aType) == myCellTypes.end()){
232 InsertCell(anInput,aConnectivity,aCellTypesArray,aCellId,anIdList,
233 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
237 }else if(!myCellIds.empty() && !myCellTypes.empty()){
238 if(myChangeMode == eAdding){
239 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
240 vtkIdType aType = anInput->GetCellType(aCellId);
241 if(myCellTypes.find(aType) != myCellTypes.end()){
242 if(myCellIds.find(aCellId) != myCellIds.end()){
243 InsertCell(anInput,aConnectivity,aCellTypesArray,aCellId,anIdList,
244 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
249 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
250 vtkIdType aType = anInput->GetCellType(aCellId);
251 if(myCellTypes.find(aType) == myCellTypes.end()){
252 if(myCellIds.find(aCellId) == myCellIds.end()){
253 InsertCell(anInput,aConnectivity,aCellTypesArray,aCellId,anIdList,
254 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
260 if((aNbElems = aConnectivity->GetNumberOfCells())){
261 VTKViewer_CellLocationsArray* aCellLocationsArray = VTKViewer_CellLocationsArray::New();
262 aCellLocationsArray->SetNumberOfComponents(1);
263 aCellLocationsArray->SetNumberOfTuples(aNbElems);
264 aConnectivity->InitTraversal();
265 for(vtkIdType i = 0, *pts, npts; aConnectivity->GetNextCell(npts,pts); i++){
266 aCellLocationsArray->SetValue(i,aConnectivity->GetTraversalLocation(npts));
268 anOutput->SetCells(aCellTypesArray,aCellLocationsArray,aConnectivity);
269 anOutput->SetPoints(anInput->GetPoints());
270 aCellLocationsArray->Delete();
272 aCellTypesArray->Delete();
273 aConnectivity->Delete();
277 vtkIdList *anIdList = vtkIdList::New();
278 anIdList->SetNumberOfIds(1);
279 vtkCellArray *aConnectivity = vtkCellArray::New();
280 vtkIdType aNbElems = anInput->GetNumberOfPoints();
281 aConnectivity->Allocate(2*aNbElems,0);
282 vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
283 aCellTypesArray->SetNumberOfComponents(1);
284 aCellTypesArray->Allocate(aNbElems*aCellTypesArray->GetNumberOfComponents());
285 if(myChangeMode == ePassAll || myCellIds.empty() && myCellTypes.empty() && myChangeMode == eRemoving){
286 if(myStoreMapping) myOut2InId.reserve(aNbElems);
287 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
288 InsertPointCell(aConnectivity,aCellTypesArray,aCellId,anIdList,
289 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
291 }else if(!myCellIds.empty() && myCellTypes.empty()){
292 if(myStoreMapping) myOut2InId.reserve(myCellIds.size());
293 if(myChangeMode == eAdding){
294 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
295 if(myCellIds.find(aCellId) != myCellIds.end()){
296 InsertPointCell(aConnectivity,aCellTypesArray,aCellId,anIdList,
297 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
301 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
302 if(myCellIds.find(aCellId) == myCellIds.end()){
303 InsertPointCell(aConnectivity,aCellTypesArray,aCellId,anIdList,
304 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
308 }else if(myCellIds.empty() && !myCellTypes.empty()){
309 if(myChangeMode == eAdding){
310 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
311 vtkIdType aType = anInput->GetCellType(aCellId);
312 if(myCellTypes.find(aType) != myCellTypes.end()){
313 InsertPointCell(aConnectivity,aCellTypesArray,aCellId,anIdList,
314 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
318 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
319 vtkIdType aType = anInput->GetCellType(aCellId);
320 if(myCellTypes.find(aType) == myCellTypes.end()){
321 InsertPointCell(aConnectivity,aCellTypesArray,aCellId,anIdList,
322 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
326 }else if(!myCellIds.empty() && !myCellTypes.empty()){
327 if(myChangeMode == eAdding){
328 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
329 vtkIdType aType = anInput->GetCellType(aCellId);
330 if(myCellTypes.find(aType) != myCellTypes.end()){
331 if(myCellIds.find(aCellId) != myCellIds.end()){
332 InsertPointCell(aConnectivity,aCellTypesArray,aCellId,anIdList,
333 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
338 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
339 vtkIdType aType = anInput->GetCellType(aCellId);
340 if(myCellTypes.find(aType) == myCellTypes.end()){
341 if(myCellIds.find(aCellId) == myCellIds.end()){
342 InsertPointCell(aConnectivity,aCellTypesArray,aCellId,anIdList,
343 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
349 if((aNbElems = aConnectivity->GetNumberOfCells())){
350 VTKViewer_CellLocationsArray* aCellLocationsArray = VTKViewer_CellLocationsArray::New();
351 aCellLocationsArray->SetNumberOfComponents(1);
352 aCellLocationsArray->SetNumberOfTuples(aNbElems);
353 aConnectivity->InitTraversal();
354 for(vtkIdType i = 0, *pts, npts; aConnectivity->GetNextCell(npts,pts); i++){
355 aCellLocationsArray->SetValue(i,aConnectivity->GetTraversalLocation(npts));
357 anOutput->SetCells(aCellTypesArray,aCellLocationsArray,aConnectivity);
358 anOutput->SetPoints(anInput->GetPoints());
359 aCellLocationsArray->Delete();
361 aCellTypesArray->Delete();
362 aConnectivity->Delete();
366 MESSAGE("Execute - anOutput->GetNumberOfCells() = "<<anOutput->GetNumberOfCells());
368 MESSAGE("Execute - myOut2InId.size() = "<<myOut2InId.size());
369 MESSAGE("Execute - myIn2OutId.size() = "<<myIn2OutId.size());