Salome HOME
Merge from V5_1_main 14/05/2010
[modules/visu.git] / src / CONVERTOR / VISU_UsedPointsFilter.cxx
1 //  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  This library is free software; you can redistribute it and/or
4 //  modify it under the terms of the GNU Lesser General Public
5 //  License as published by the Free Software Foundation; either
6 //  version 2.1 of the License.
7 //
8 //  This library is distributed in the hope that it will be useful,
9 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 //  Lesser General Public License for more details.
12 //
13 //  You should have received a copy of the GNU Lesser General Public
14 //  License along with this library; if not, write to the Free Software
15 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 //  VISU CONVERTOR :
21 //  File   : VISU_UsedPointsFilter.cxx
22 //  Author : 
23 //  Module : VISU
24 //
25 #include "VISU_UsedPointsFilter.hxx"
26
27 #include <vtkObjectFactory.h>
28 #include <vtkUnstructuredGrid.h>
29 #include <vtkPointData.h>
30 #include <vtkCellData.h>
31 #include <vtkCell.h>
32 #include <vtkIdList.h>
33
34 #include <map>
35
36 vtkStandardNewMacro(VISU_UsedPointsFilter);
37
38
39 VISU_UsedPointsFilter::VISU_UsedPointsFilter()
40 {
41 }
42
43 VISU_UsedPointsFilter::~VISU_UsedPointsFilter()
44 {
45 }
46
47 void VISU_UsedPointsFilter::Execute(){
48   vtkDataSet *anInput = this->GetInput();
49   vtkUnstructuredGrid *anOutput = this->GetOutput();
50
51   typedef std::map<vtkIdType, vtkIdType> TId2IdMap;
52   TId2IdMap aId2IdMap;
53
54   vtkPointData *aPointData = anOutput->GetPointData();
55   aPointData->CopyAllocate(anInput->GetPointData());
56
57   vtkPoints* aUsedPoints = vtkPoints::New();
58   vtkIdList *anIdList = vtkIdList::New();
59   vtkIdType iEnd = anInput->GetNumberOfPoints();
60   for(vtkIdType aPointId = 0; aPointId < iEnd; aPointId++){
61     anInput->GetPointCells(aPointId,anIdList);
62     if(anIdList->GetNumberOfIds() > 0){
63       vtkIdType aNewPointId = aUsedPoints->InsertNextPoint(anInput->GetPoint(aPointId));
64       aPointData->CopyData(anInput->GetPointData(), aPointId, aNewPointId);
65       aId2IdMap[aPointId] = aNewPointId;
66     }
67   }
68   aPointData->Squeeze();
69   anOutput->SetPoints(aUsedPoints);
70   aUsedPoints->Delete();
71   anIdList->Delete();
72
73   vtkCellData *aCellData = anOutput->GetCellData();
74   aCellData->CopyAllocate(anInput->GetCellData());
75
76   anOutput->Allocate(anInput->GetNumberOfCells()); 
77   vtkIdList *anOldPointsIds = vtkIdList::New();
78   vtkIdList *aNewPointsIds = vtkIdList::New();
79   aNewPointsIds->Allocate(VTK_CELL_SIZE);
80   iEnd = anInput->GetNumberOfCells();
81   for(vtkIdType aCellId = 0; aCellId < iEnd; aCellId++){
82     anInput->GetCellPoints(aCellId, anOldPointsIds);
83     vtkIdType aNbPointsInCell = anOldPointsIds->GetNumberOfIds();
84     aNewPointsIds->Reset();
85     for(vtkIdType i = 0; i < aNbPointsInCell; i++){
86       vtkIdType anOldId = anOldPointsIds->GetId(i);
87       TId2IdMap::iterator anIter = aId2IdMap.find(anOldId);
88       if(anIter == aId2IdMap.end())
89         goto NEXT_CELL;
90       vtkIdType aNewId = anIter->second;
91       aNewPointsIds->InsertNextId(aNewId);
92     }
93     {
94       vtkIdType aNewCellId = anOutput->InsertNextCell(anInput->GetCellType(aCellId), aNewPointsIds);
95       aCellData->CopyData(anInput->GetCellData(), aCellId, aNewCellId);
96     }
97   NEXT_CELL:
98     continue;
99   }
100   aCellData->Squeeze();
101   anOldPointsIds->Delete();
102   aNewPointsIds->Delete();
103   anOutput->Squeeze();
104 }
105