Salome HOME
Merge from V5_1_main 14/05/2010
[modules/visu.git] / src / CONVERTOR / VISU_ExtractUnstructuredGrid.cxx
1 //  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  VISU CONVERTOR :
24 // File:    VISU_ExtractUnstructuredGrid.cxx
25 // Author:  Alexey PETROV
26 // Module : VISU
27 //
28 #include "VISU_ExtractUnstructuredGrid.hxx"
29 #include "VISU_ConvertorUtils.hxx"
30
31 #include <vtkUnstructuredGrid.h>
32 #include <vtkObjectFactory.h>
33 #include <vtkIdList.h>
34 #include <vtkCell.h>
35 #include <vtkInformation.h>
36 #include <vtkInformationVector.h>
37
38 using namespace std;
39
40 #ifdef _DEBUG_
41 static int MYDEBUG = 0;
42 #else
43 static int MYDEBUG = 0;
44 #endif
45
46 vtkStandardNewMacro(VISU_ExtractUnstructuredGrid);
47
48 VISU_ExtractUnstructuredGrid::VISU_ExtractUnstructuredGrid(){}
49
50 VISU_ExtractUnstructuredGrid::~VISU_ExtractUnstructuredGrid(){}
51
52 void VISU_ExtractUnstructuredGrid::RemoveCell(vtkIdType theCellId){
53   myRemovedCellIds.insert(theCellId);
54   Modified();
55 }
56
57 void VISU_ExtractUnstructuredGrid::RemoveCellsWithType(vtkIdType theCellType){
58   myRemovedCellTypes.insert(theCellType);
59   Modified();
60 }
61
62 namespace{
63   inline void InsertCell(vtkUnstructuredGrid *theInput, 
64                           vtkUnstructuredGrid *theOutput,
65                           vtkIdType theCellId, vtkIdList *theCellIds)
66   {
67     theCellIds->Reset();
68     vtkCell *aCell = theInput->GetCell(theCellId);
69     vtkIdType aNbIds = aCell->PointIds->GetNumberOfIds();
70     for(vtkIdType i = 0; i < aNbIds; i++)
71       theCellIds->InsertNextId(aCell->GetPointIds()->GetId(i));
72     theOutput->InsertNextCell(theInput->GetCellType(theCellId), theCellIds);
73   }
74 }
75
76 int VISU_ExtractUnstructuredGrid::RequestData(
77   vtkInformation *vtkNotUsed(request),
78   vtkInformationVector **inputVector,
79   vtkInformationVector *outputVector)
80 {
81   // get the info objects
82   vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
83   vtkInformation *outInfo = outputVector->GetInformationObject(0);
84
85   // get the input and ouptut
86   vtkUnstructuredGrid *anInput = vtkUnstructuredGrid::SafeDownCast(
87     inInfo->Get(vtkDataObject::DATA_OBJECT()));
88   vtkUnstructuredGrid *anOutput = vtkUnstructuredGrid::SafeDownCast(
89     outInfo->Get(vtkDataObject::DATA_OBJECT()));
90   
91
92   vtkIdType aNbCells = anInput->GetNumberOfCells();
93   anOutput->Allocate(aNbCells);
94   MSG(MYDEBUG,"Execute - anInput->GetNumberOfCells() = "<<anInput->GetNumberOfCells());
95   vtkIdList *aCellIds = vtkIdList::New();
96   MSG(MYDEBUG,"Execute - myRemovedCellIds.empty() = "<<myRemovedCellIds.empty()<<
97       "; myRemovedCellTypes.empty() = "<<myRemovedCellTypes.empty());
98   if(myRemovedCellIds.empty() && myRemovedCellTypes.empty())
99     anOutput->CopyStructure(anInput);
100   else if(!myRemovedCellIds.empty() && myRemovedCellTypes.empty()){
101     for(vtkIdType aCellId = 0; aCellId < aNbCells; aCellId++)
102       if(myRemovedCellIds.find(aCellId) == myRemovedCellIds.end())
103         InsertCell(anInput,anOutput,aCellId,aCellIds);
104   }else if(myRemovedCellIds.empty() && !myRemovedCellTypes.empty()){
105     for(vtkIdType aCellId = 0; aCellId < aNbCells; aCellId++)
106       if(myRemovedCellTypes.find(anInput->GetCellType(aCellId)) == myRemovedCellTypes.end())
107         InsertCell(anInput,anOutput,aCellId,aCellIds);
108   }else if(!myRemovedCellIds.empty() && !myRemovedCellTypes.empty())
109     for(vtkIdType aCellId = 0; aCellId < aNbCells; aCellId++)
110       if(myRemovedCellTypes.find(anInput->GetCellType(aCellId)) == myRemovedCellTypes.end())
111         if(myRemovedCellIds.find(aCellId) == myRemovedCellIds.end())
112           InsertCell(anInput,anOutput,aCellId,aCellIds);
113   aCellIds->Delete();
114   anOutput->SetPoints(anInput->GetPoints());
115   MSG(MYDEBUG,"Execute - anOutput->GetNumberOfCells() = "<<anOutput->GetNumberOfCells());
116   
117   return 1;
118 }