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 "SALOME_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(SALOME_ExtractUnstructuredGrid);
56 SALOME_ExtractUnstructuredGrid::SALOME_ExtractUnstructuredGrid():
57 myExtractionMode(eCells), myChangeMode(ePassAll)
61 SALOME_ExtractUnstructuredGrid::~SALOME_ExtractUnstructuredGrid(){}
65 SALOME_ExtractUnstructuredGrid::
66 SetModeOfChanging(SALOME_ExtractUnstructuredGrid::EChanging theChangeMode)
68 if(theChangeMode != myChangeMode){
69 myChangeMode = theChangeMode;
76 SALOME_ExtractUnstructuredGrid::
77 SetModeOfExtraction(SALOME_ExtractUnstructuredGrid::EExtraction theExtractionMode)
79 if(theExtractionMode != myExtractionMode){
80 myExtractionMode = theExtractionMode;
87 SALOME_ExtractUnstructuredGrid::
88 ClearRegisteredCellsWithType()
90 if(IsCellsWithTypeRegistered()){
97 void SALOME_ExtractUnstructuredGrid::RegisterCell(vtkIdType theCellId){
98 if(0 && MYDEBUG) MESSAGE("RegisterCell - theCellId = "<<theCellId);
99 myCellIds.insert(theCellId);
104 void SALOME_ExtractUnstructuredGrid::RegisterCellsWithType(vtkIdType theCellType){
105 if(0 && MYDEBUG) MESSAGE("RegisterCellsWithType - theCellType = "<<theCellType);
106 myCellTypes.insert(theCellType);
111 void SALOME_ExtractUnstructuredGrid::SetStoreMapping(int theStoreMapping){
112 myStoreMapping = theStoreMapping;
116 vtkIdType SALOME_ExtractUnstructuredGrid::GetInputId(int theOutId) const{
117 if(myCellIds.empty() && myCellTypes.empty()) return theOutId;
118 if(myOut2InId.empty() || theOutId > myOut2InId.size()) return -1;
119 #if defined __GNUC_2__
120 return myOut2InId[theOutId];
122 return myOut2InId.at(theOutId);
126 vtkIdType SALOME_ExtractUnstructuredGrid::GetOutputId(int theInId) const{
127 if(myCellIds.empty() && myCellTypes.empty()) return theInId;
128 TMapId::const_iterator anIter = myIn2OutId.find(theInId);
129 if(anIter == myIn2OutId.end()) return -1;
130 return anIter->second;
134 inline void InsertCell(vtkUnstructuredGrid *theInput,
135 vtkCellArray *theConnectivity,
136 vtkUnsignedCharArray* theCellTypesArray,
138 vtkIdList *theIdList,
139 bool theStoreMapping,
141 SALOME_ExtractUnstructuredGrid::TVectorId& theOut2InId,
142 SALOME_ExtractUnstructuredGrid::TMapId& theIn2OutId)
144 vtkCell *aCell = theInput->GetCell(theCellId);
145 vtkIdList *aPntIds = aCell->GetPointIds();
146 vtkIdType aNbIds = aPntIds->GetNumberOfIds();
147 theIdList->SetNumberOfIds(aNbIds);
148 for(vtkIdType i = 0; i < aNbIds; i++){
149 theIdList->SetId(i,aPntIds->GetId(i));
151 theConnectivity->InsertNextCell(theIdList);
153 vtkIdType aCellType = aCell->GetCellType();
154 theCellTypesArray->InsertNextValue(aCellType);
156 theOut2InId.push_back(theCellId);
157 theIn2OutId[theCellId] = theOutId;
161 inline void InsertPointCell(vtkCellArray *theConnectivity,
162 vtkUnsignedCharArray* theCellTypesArray,
164 vtkIdList *theIdList,
165 bool theStoreMapping,
167 SALOME_ExtractUnstructuredGrid::TVectorId& theOut2InId,
168 SALOME_ExtractUnstructuredGrid::TMapId& theIn2OutId)
170 theIdList->SetId(0,theCellId);
171 theConnectivity->InsertNextCell(theIdList);
172 theCellTypesArray->InsertNextValue(VTK_VERTEX);
174 theOut2InId.push_back(theCellId);
175 theIn2OutId[theCellId] = theOutId;
179 void SALOME_ExtractUnstructuredGrid::Execute(){
180 vtkUnstructuredGrid *anInput = this->GetInput();
181 vtkUnstructuredGrid *anOutput = this->GetOutput();
182 myOut2InId.clear(); myIn2OutId.clear();
185 MESSAGE("Execute - anInput->GetNumberOfCells() = "<<anInput->GetNumberOfCells());
186 MESSAGE("Execute - myCellTypes.size() = "<<myCellTypes.size());
187 MESSAGE("Execute - myCellIds.size() = "<<myCellIds.size());
188 MESSAGE("Execute - myExtractionMode = "<<myExtractionMode);
189 MESSAGE("Execute - myChangeMode = "<<myChangeMode);
191 if(myExtractionMode == eCells){
192 if(myChangeMode == ePassAll || myCellIds.empty() && myCellTypes.empty() && myChangeMode == eRemoving){
193 if(vtkIdType aNbElems = anInput->GetNumberOfCells()){
194 if(myStoreMapping) myOut2InId.reserve(aNbElems);
195 anOutput->ShallowCopy(anInput);
196 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
198 myOut2InId.push_back(aCellId);
199 myIn2OutId[aCellId] = anOutId;
204 vtkIdList *anIdList = vtkIdList::New();
205 vtkCellArray *aConnectivity = vtkCellArray::New();
206 vtkIdType aNbElems = anInput->GetNumberOfCells();
207 aConnectivity->Allocate(2*aNbElems,0);
208 vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
209 aCellTypesArray->SetNumberOfComponents(1);
210 aCellTypesArray->Allocate(aNbElems*aCellTypesArray->GetNumberOfComponents());
211 if(!myCellIds.empty() && myCellTypes.empty()){
212 if(myStoreMapping) myOut2InId.reserve(myCellIds.size());
213 if(myChangeMode == eAdding){
214 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
215 if(myCellIds.find(aCellId) != myCellIds.end()){
216 InsertCell(anInput,aConnectivity,aCellTypesArray,aCellId,anIdList,
217 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
221 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
222 if(myCellIds.find(aCellId) == myCellIds.end()){
223 InsertCell(anInput,aConnectivity,aCellTypesArray,aCellId,anIdList,
224 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
228 }else if(myCellIds.empty() && !myCellTypes.empty()){
229 if(myChangeMode == eAdding){
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 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
239 vtkIdType aType = anInput->GetCellType(aCellId);
240 if(myCellTypes.find(aType) == myCellTypes.end()){
241 InsertCell(anInput,aConnectivity,aCellTypesArray,aCellId,anIdList,
242 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
246 }else if(!myCellIds.empty() && !myCellTypes.empty()){
247 if(myChangeMode == eAdding){
248 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
249 vtkIdType aType = anInput->GetCellType(aCellId);
250 if(myCellTypes.find(aType) != myCellTypes.end()){
251 if(myCellIds.find(aCellId) != myCellIds.end()){
252 InsertCell(anInput,aConnectivity,aCellTypesArray,aCellId,anIdList,
253 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
258 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
259 vtkIdType aType = anInput->GetCellType(aCellId);
260 if(myCellTypes.find(aType) == myCellTypes.end()){
261 if(myCellIds.find(aCellId) == myCellIds.end()){
262 InsertCell(anInput,aConnectivity,aCellTypesArray,aCellId,anIdList,
263 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
269 if((aNbElems = aConnectivity->GetNumberOfCells())){
270 vtkIntArray* aCellLocationsArray = vtkIntArray::New();
271 aCellLocationsArray->SetNumberOfComponents(1);
272 aCellLocationsArray->SetNumberOfTuples(aNbElems);
273 aConnectivity->InitTraversal();
274 for(vtkIdType i = 0, *pts, npts; aConnectivity->GetNextCell(npts,pts); i++){
275 aCellLocationsArray->SetValue(i,aConnectivity->GetTraversalLocation(npts));
277 anOutput->SetCells(aCellTypesArray,aCellLocationsArray,aConnectivity);
278 anOutput->SetPoints(anInput->GetPoints());
279 aCellLocationsArray->Delete();
281 aCellTypesArray->Delete();
282 aConnectivity->Delete();
286 vtkIdList *anIdList = vtkIdList::New();
287 anIdList->SetNumberOfIds(1);
288 vtkCellArray *aConnectivity = vtkCellArray::New();
289 vtkIdType aNbElems = anInput->GetNumberOfPoints();
290 aConnectivity->Allocate(2*aNbElems,0);
291 vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
292 aCellTypesArray->SetNumberOfComponents(1);
293 aCellTypesArray->Allocate(aNbElems*aCellTypesArray->GetNumberOfComponents());
294 if(myChangeMode == ePassAll || myCellIds.empty() && myCellTypes.empty() && myChangeMode == eRemoving){
295 if(myStoreMapping) myOut2InId.reserve(aNbElems);
296 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
297 InsertPointCell(aConnectivity,aCellTypesArray,aCellId,anIdList,
298 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
300 }else if(!myCellIds.empty() && myCellTypes.empty()){
301 if(myStoreMapping) myOut2InId.reserve(myCellIds.size());
302 if(myChangeMode == eAdding){
303 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
304 if(myCellIds.find(aCellId) != myCellIds.end()){
305 InsertPointCell(aConnectivity,aCellTypesArray,aCellId,anIdList,
306 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
310 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
311 if(myCellIds.find(aCellId) == myCellIds.end()){
312 InsertPointCell(aConnectivity,aCellTypesArray,aCellId,anIdList,
313 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
317 }else if(myCellIds.empty() && !myCellTypes.empty()){
318 if(myChangeMode == eAdding){
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 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
328 vtkIdType aType = anInput->GetCellType(aCellId);
329 if(myCellTypes.find(aType) == myCellTypes.end()){
330 InsertPointCell(aConnectivity,aCellTypesArray,aCellId,anIdList,
331 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
335 }else if(!myCellIds.empty() && !myCellTypes.empty()){
336 if(myChangeMode == eAdding){
337 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
338 vtkIdType aType = anInput->GetCellType(aCellId);
339 if(myCellTypes.find(aType) != myCellTypes.end()){
340 if(myCellIds.find(aCellId) != myCellIds.end()){
341 InsertPointCell(aConnectivity,aCellTypesArray,aCellId,anIdList,
342 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
347 for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
348 vtkIdType aType = anInput->GetCellType(aCellId);
349 if(myCellTypes.find(aType) == myCellTypes.end()){
350 if(myCellIds.find(aCellId) == myCellIds.end()){
351 InsertPointCell(aConnectivity,aCellTypesArray,aCellId,anIdList,
352 myStoreMapping,anOutId,myOut2InId,myIn2OutId);
358 if((aNbElems = aConnectivity->GetNumberOfCells())){
359 vtkIntArray* aCellLocationsArray = vtkIntArray::New();
360 aCellLocationsArray->SetNumberOfComponents(1);
361 aCellLocationsArray->SetNumberOfTuples(aNbElems);
362 aConnectivity->InitTraversal();
363 for(vtkIdType i = 0, *pts, npts; aConnectivity->GetNextCell(npts,pts); i++){
364 aCellLocationsArray->SetValue(i,aConnectivity->GetTraversalLocation(npts));
366 anOutput->SetCells(aCellTypesArray,aCellLocationsArray,aConnectivity);
367 anOutput->SetPoints(anInput->GetPoints());
368 aCellLocationsArray->Delete();
370 aCellTypesArray->Delete();
371 aConnectivity->Delete();
375 MESSAGE("Execute - anOutput->GetNumberOfCells() = "<<anOutput->GetNumberOfCells());
377 MESSAGE("Execute - myOut2InId.size() = "<<myOut2InId.size());
378 MESSAGE("Execute - myIn2OutId.size() = "<<myIn2OutId.size());