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>
37 #include <vtkInformation.h>
38 #include <vtkInformationVector.h>
43 static int MYDEBUG = 0;
45 static int MYDEBUG = 0;
54 vtkStandardNewMacro(VTKViewer_ExtractUnstructuredGrid);
57 VTKViewer_ExtractUnstructuredGrid::VTKViewer_ExtractUnstructuredGrid():
58 myExtractionMode(eCells), myChangeMode(ePassAll)
62 VTKViewer_ExtractUnstructuredGrid::~VTKViewer_ExtractUnstructuredGrid(){}
65 void VTKViewer_ExtractUnstructuredGrid::RegisterCell(vtkIdType theCellId){
66 // if(0 && MYDEBUG) MESSAGE("RegisterCell - theCellId = "<<theCellId);
67 myCellIds.insert(theCellId);
72 void VTKViewer_ExtractUnstructuredGrid::RegisterCellsWithType(vtkIdType theCellType){
73 // if(0 && MYDEBUG) MESSAGE("RegisterCellsWithType - theCellType = "<<theCellType);
74 myCellTypes.insert(theCellType);
79 void VTKViewer_ExtractUnstructuredGrid::SetStoreMapping(int theStoreMapping){
80 myStoreMapping = theStoreMapping != 0;
84 vtkIdType VTKViewer_ExtractUnstructuredGrid::GetInputId(int theOutId) const
86 if ( myCellIds.empty() && myCellTypes.empty() )
89 if ( myOut2InId.empty() || theOutId > (int)myOut2InId.size() )
91 #if defined __GNUC_2__
92 return myOut2InId[theOutId];
94 return myOut2InId.at(theOutId);
98 vtkIdType VTKViewer_ExtractUnstructuredGrid::GetOutputId(int theInId) const{
99 if(myCellIds.empty() && myCellTypes.empty()) return theInId;
100 TMapId::const_iterator anIter = myIn2OutId.find(theInId);
101 if(anIter == myIn2OutId.end()) return -1;
102 return anIter->second;
106 inline void InsertCell(vtkUnstructuredGrid *theInput,
107 vtkCellArray *theConnectivity,
108 vtkUnsignedCharArray* theCellTypesArray,
110 vtkIdList *theIdList,
111 bool theStoreMapping,
113 VTKViewer_ExtractUnstructuredGrid::TVectorId& theOut2InId,
114 VTKViewer_ExtractUnstructuredGrid::TMapId& theIn2OutId)
116 vtkCell *aCell = theInput->GetCell(theCellId);
117 vtkIdList *aPntIds = aCell->GetPointIds();
118 vtkIdType aNbIds = aPntIds->GetNumberOfIds();
119 theIdList->SetNumberOfIds(aNbIds);
120 for(vtkIdType i = 0; i < aNbIds; i++){
121 theIdList->SetId(i,aPntIds->GetId(i));
123 theConnectivity->InsertNextCell(theIdList);
125 vtkIdType aCellType = aCell->GetCellType();
126 theCellTypesArray->InsertNextValue(aCellType);
128 theOut2InId.push_back(theCellId);
129 theIn2OutId[theCellId] = theOutId;
133 inline void InsertPointCell(vtkCellArray *theConnectivity,
134 vtkUnsignedCharArray* theCellTypesArray,
136 vtkIdList *theIdList,
137 bool theStoreMapping,
139 VTKViewer_ExtractUnstructuredGrid::TVectorId& theOut2InId,
140 VTKViewer_ExtractUnstructuredGrid::TMapId& theIn2OutId)
142 theIdList->SetId(0,theCellId);
143 theConnectivity->InsertNextCell(theIdList);
144 theCellTypesArray->InsertNextValue(VTK_VERTEX);
146 theOut2InId.push_back(theCellId);
147 theIn2OutId[theCellId] = theOutId;
152 // int VTKViewer_ExtractUnstructuredGrid::RequestData(
153 // vtkInformation *vtkNotUsed(request),
154 // vtkInformationVector **inputVector,
155 // vtkInformationVector *outputVector)
156 void VTKViewer_ExtractUnstructuredGrid::Execute()
159 not ported yet to the new executive-based pipeline architecture.
161 // get the info objects
162 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
163 vtkInformation *outInfo = outputVector->GetInformationObject(0);
165 // get the input and ouptut
166 vtkUnstructuredGrid *anInput = vtkUnstructuredGrid::SafeDownCast(
167 inInfo->Get(vtkDataObject::DATA_OBJECT()));
168 vtkUnstructuredGrid *anOutput = vtkUnstructuredGrid::SafeDownCast(
169 outInfo->Get(vtkDataObject::DATA_OBJECT()));
171 vtkUnstructuredGrid *anInput = this->GetInput();
172 vtkUnstructuredGrid *anOutput = this->GetOutput();
174 myOut2InId.clear(); myIn2OutId.clear();
177 MESSAGE("Execute - anInput->GetNumberOfCells() = "<<anInput->GetNumberOfCells());
178 MESSAGE("Execute - myCellTypes.size() = "<<myCellTypes.size());
179 MESSAGE("Execute - myCellIds.size() = "<<myCellIds.size());
180 MESSAGE("Execute - myExtractionMode = "<<myExtractionMode);
181 MESSAGE("Execute - myChangeMode = "<<myChangeMode);
183 if(myExtractionMode == eCells){
184 if(myChangeMode == ePassAll || myCellIds.empty() && myCellTypes.empty() && myChangeMode == eRemoving){
185 if(vtkIdType aNbElems = anInput->GetNumberOfCells()){
186 if(myStoreMapping) myOut2InId.reserve(aNbElems);
187 anOutput->ShallowCopy(anInput);
188 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
190 myOut2InId.push_back(aCellId);
191 myIn2OutId[aCellId] = anOutId;
196 vtkIdList *anIdList = vtkIdList::New();
197 vtkCellArray *aConnectivity = vtkCellArray::New();
198 vtkIdType aNbElems = anInput->GetNumberOfCells();
199 aConnectivity->Allocate(2*aNbElems,0);
200 vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
201 aCellTypesArray->SetNumberOfComponents(1);
202 aCellTypesArray->Allocate(aNbElems*aCellTypesArray->GetNumberOfComponents());
203 if(!myCellIds.empty() && myCellTypes.empty()){
204 if(myStoreMapping) myOut2InId.reserve(myCellIds.size());
205 if(myChangeMode == eAdding){
206 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
207 if(myCellIds.find(aCellId) != myCellIds.end()){
208 InsertCell(anInput,aConnectivity,aCellTypesArray,aCellId,anIdList,
209 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
213 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
214 if(myCellIds.find(aCellId) == myCellIds.end()){
215 InsertCell(anInput,aConnectivity,aCellTypesArray,aCellId,anIdList,
216 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
220 }else if(myCellIds.empty() && !myCellTypes.empty()){
221 if(myChangeMode == eAdding){
222 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
223 vtkIdType aType = anInput->GetCellType(aCellId);
224 if(myCellTypes.find(aType) != myCellTypes.end()){
225 InsertCell(anInput,aConnectivity,aCellTypesArray,aCellId,anIdList,
226 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
230 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
231 vtkIdType aType = anInput->GetCellType(aCellId);
232 if(myCellTypes.find(aType) == myCellTypes.end()){
233 InsertCell(anInput,aConnectivity,aCellTypesArray,aCellId,anIdList,
234 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
238 }else if(!myCellIds.empty() && !myCellTypes.empty()){
239 if(myChangeMode == eAdding){
240 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
241 vtkIdType aType = anInput->GetCellType(aCellId);
242 if(myCellTypes.find(aType) != myCellTypes.end()){
243 if(myCellIds.find(aCellId) != myCellIds.end()){
244 InsertCell(anInput,aConnectivity,aCellTypesArray,aCellId,anIdList,
245 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
250 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
251 vtkIdType aType = anInput->GetCellType(aCellId);
252 if(myCellTypes.find(aType) == myCellTypes.end()){
253 if(myCellIds.find(aCellId) == myCellIds.end()){
254 InsertCell(anInput,aConnectivity,aCellTypesArray,aCellId,anIdList,
255 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
261 if((aNbElems = aConnectivity->GetNumberOfCells())){
262 VTKViewer_CellLocationsArray* aCellLocationsArray = VTKViewer_CellLocationsArray::New();
263 aCellLocationsArray->SetNumberOfComponents(1);
264 aCellLocationsArray->SetNumberOfTuples(aNbElems);
265 aConnectivity->InitTraversal();
266 for(vtkIdType i = 0, *pts, npts; aConnectivity->GetNextCell(npts,pts); i++){
267 aCellLocationsArray->SetValue(i,aConnectivity->GetTraversalLocation(npts));
269 anOutput->SetCells(aCellTypesArray,aCellLocationsArray,aConnectivity);
270 anOutput->SetPoints(anInput->GetPoints());
271 aCellLocationsArray->Delete();
273 aCellTypesArray->Delete();
274 aConnectivity->Delete();
278 vtkIdList *anIdList = vtkIdList::New();
279 anIdList->SetNumberOfIds(1);
280 vtkCellArray *aConnectivity = vtkCellArray::New();
281 vtkIdType aNbElems = anInput->GetNumberOfPoints();
282 aConnectivity->Allocate(2*aNbElems,0);
283 vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
284 aCellTypesArray->SetNumberOfComponents(1);
285 aCellTypesArray->Allocate(aNbElems*aCellTypesArray->GetNumberOfComponents());
286 if(myChangeMode == ePassAll || myCellIds.empty() && myCellTypes.empty() && myChangeMode == eRemoving){
287 if(myStoreMapping) myOut2InId.reserve(aNbElems);
288 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
289 InsertPointCell(aConnectivity,aCellTypesArray,aCellId,anIdList,
290 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
292 }else if(!myCellIds.empty() && myCellTypes.empty()){
293 if(myStoreMapping) myOut2InId.reserve(myCellIds.size());
294 if(myChangeMode == eAdding){
295 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
296 if(myCellIds.find(aCellId) != myCellIds.end()){
297 InsertPointCell(aConnectivity,aCellTypesArray,aCellId,anIdList,
298 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
302 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
303 if(myCellIds.find(aCellId) == myCellIds.end()){
304 InsertPointCell(aConnectivity,aCellTypesArray,aCellId,anIdList,
305 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
309 }else if(myCellIds.empty() && !myCellTypes.empty()){
310 if(myChangeMode == eAdding){
311 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
312 vtkIdType aType = anInput->GetCellType(aCellId);
313 if(myCellTypes.find(aType) != myCellTypes.end()){
314 InsertPointCell(aConnectivity,aCellTypesArray,aCellId,anIdList,
315 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
319 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
320 vtkIdType aType = anInput->GetCellType(aCellId);
321 if(myCellTypes.find(aType) == myCellTypes.end()){
322 InsertPointCell(aConnectivity,aCellTypesArray,aCellId,anIdList,
323 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
327 }else if(!myCellIds.empty() && !myCellTypes.empty()){
328 if(myChangeMode == eAdding){
329 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
330 vtkIdType aType = anInput->GetCellType(aCellId);
331 if(myCellTypes.find(aType) != myCellTypes.end()){
332 if(myCellIds.find(aCellId) != myCellIds.end()){
333 InsertPointCell(aConnectivity,aCellTypesArray,aCellId,anIdList,
334 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
339 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
340 vtkIdType aType = anInput->GetCellType(aCellId);
341 if(myCellTypes.find(aType) == myCellTypes.end()){
342 if(myCellIds.find(aCellId) == myCellIds.end()){
343 InsertPointCell(aConnectivity,aCellTypesArray,aCellId,anIdList,
344 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
350 if((aNbElems = aConnectivity->GetNumberOfCells())){
351 VTKViewer_CellLocationsArray* aCellLocationsArray = VTKViewer_CellLocationsArray::New();
352 aCellLocationsArray->SetNumberOfComponents(1);
353 aCellLocationsArray->SetNumberOfTuples(aNbElems);
354 aConnectivity->InitTraversal();
355 for(vtkIdType i = 0, *pts, npts; aConnectivity->GetNextCell(npts,pts); i++){
356 aCellLocationsArray->SetValue(i,aConnectivity->GetTraversalLocation(npts));
358 anOutput->SetCells(aCellTypesArray,aCellLocationsArray,aConnectivity);
359 anOutput->SetPoints(anInput->GetPoints());
360 aCellLocationsArray->Delete();
362 aCellTypesArray->Delete();
363 aConnectivity->Delete();
367 MESSAGE("Execute - anOutput->GetNumberOfCells() = "<<anOutput->GetNumberOfCells());
369 MESSAGE("Execute - myOut2InId.size() = "<<myOut2InId.size());
370 MESSAGE("Execute - myIn2OutId.size() = "<<myIn2OutId.size());