Salome HOME
8392385ffed243b1703bb930e20c7143ecd594dc
[modules/visu.git] / src / CONVERTOR / VISU_UsedPointsFilter.cxx
1 //  VISU CONVERTOR :
2 //
3 //  Copyright (C) 2003  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 //  File   : VISU_UsedPointsFilter.cxx
24 //  Author : 
25 //  Module : VISU
26
27
28 #include "VISU_UsedPointsFilter.hxx"
29
30 #include <vtkObjectFactory.h>
31 #include <vtkUnstructuredGrid.h>
32 #include <vtkPointData.h>
33 #include <vtkCellData.h>
34 #include <vtkCell.h>
35 #include <vtkIdList.h>
36
37 #include <map>
38
39 vtkStandardNewMacro(VISU_UsedPointsFilter);
40
41
42 VISU_UsedPointsFilter::VISU_UsedPointsFilter()
43 {
44 }
45
46 VISU_UsedPointsFilter::~VISU_UsedPointsFilter()
47 {
48 }
49
50 void VISU_UsedPointsFilter::Execute(){
51   vtkDataSet *anInput = this->GetInput();
52   vtkUnstructuredGrid *anOutput = this->GetOutput();
53
54   typedef std::map<vtkIdType, vtkIdType> TId2IdMap;
55   TId2IdMap aId2IdMap;
56
57   vtkPointData *aPointData = anOutput->GetPointData();
58   aPointData->CopyAllocate(anInput->GetPointData());
59
60   vtkPoints* aUsedPoints = vtkPoints::New();
61   vtkIdList *anIdList = vtkIdList::New();
62   vtkIdType iEnd = anInput->GetNumberOfPoints();
63   for(vtkIdType aPointId = 0; aPointId < iEnd; aPointId++){
64     anInput->GetPointCells(aPointId,anIdList);
65     if(anIdList->GetNumberOfIds() > 0){
66       vtkIdType aNewPointId = aUsedPoints->InsertNextPoint(anInput->GetPoint(aPointId));
67       aPointData->CopyData(anInput->GetPointData(), aPointId, aNewPointId);
68       aId2IdMap[aPointId] = aNewPointId;
69     }
70   }
71   aPointData->Squeeze();
72   anOutput->SetPoints(aUsedPoints);
73   aUsedPoints->Delete();
74   anIdList->Delete();
75
76   vtkCellData *aCellData = anOutput->GetCellData();
77   aCellData->CopyAllocate(anInput->GetCellData());
78
79   anOutput->Allocate(anInput->GetNumberOfCells()); 
80   vtkIdList *anOldPointsIds = vtkIdList::New();
81   vtkIdList *aNewPointsIds = vtkIdList::New();
82   aNewPointsIds->Allocate(VTK_CELL_SIZE);
83   iEnd = anInput->GetNumberOfCells();
84   for(vtkIdType aCellId = 0; aCellId < iEnd; aCellId++){
85     anInput->GetCellPoints(aCellId, anOldPointsIds);
86     vtkIdType aNbPointsInCell = anOldPointsIds->GetNumberOfIds();
87     aNewPointsIds->Reset();
88     for(vtkIdType i = 0; i < aNbPointsInCell; i++){
89       vtkIdType anOldId = anOldPointsIds->GetId(i);
90       TId2IdMap::iterator anIter = aId2IdMap.find(anOldId);
91       if(anIter == aId2IdMap.end())
92         goto NEXT_CELL;
93       vtkIdType aNewId = anIter->second;
94       aNewPointsIds->InsertNextId(aNewId);
95     }
96     {
97       vtkIdType aNewCellId = anOutput->InsertNextCell(anInput->GetCellType(aCellId), aNewPointsIds);
98       aCellData->CopyData(anInput->GetCellData(), aCellId, aNewCellId);
99     }
100   NEXT_CELL:
101     continue;
102   }
103   aCellData->Squeeze();
104   anOldPointsIds->Delete();
105   aNewPointsIds->Delete();
106   anOutput->Squeeze();
107 }
108