Salome HOME
Merge from V6_main 01/04/2013
[modules/visu.git] / src / CONVERTOR / VISU_UsedPointsFilter.cxx
1 // Copyright (C) 2007-2013  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 #include <vtkInformation.h>
34 #include <vtkInformationVector.h>
35
36 #include <map>
37
38 vtkStandardNewMacro(VISU_UsedPointsFilter);
39
40
41 VISU_UsedPointsFilter::VISU_UsedPointsFilter()
42 {
43 }
44
45 VISU_UsedPointsFilter::~VISU_UsedPointsFilter()
46 {
47 }
48
49
50 int VISU_UsedPointsFilter::RequestData(
51   vtkInformation *vtkNotUsed(request),
52   vtkInformationVector **inputVector,
53   vtkInformationVector *outputVector)
54 {
55   // get the info objects
56   vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
57   vtkInformation *outInfo = outputVector->GetInformationObject(0);
58
59   // get the input and ouptut
60   vtkDataSet *anInput = vtkDataSet::SafeDownCast(
61     inInfo->Get(vtkDataObject::DATA_OBJECT()));
62   vtkUnstructuredGrid *anOutput = vtkUnstructuredGrid::SafeDownCast(
63     outInfo->Get(vtkDataObject::DATA_OBJECT()));
64
65   typedef std::map<vtkIdType, vtkIdType> TId2IdMap;
66   TId2IdMap aId2IdMap;
67
68   vtkPointData *aPointData = anOutput->GetPointData();
69   aPointData->CopyAllocate(anInput->GetPointData());
70
71   vtkPoints* aUsedPoints = vtkPoints::New();
72   vtkIdList *anIdList = vtkIdList::New();
73   vtkIdType iEnd = anInput->GetNumberOfPoints();
74   for(vtkIdType aPointId = 0; aPointId < iEnd; aPointId++){
75     anInput->GetPointCells(aPointId,anIdList);
76     if(anIdList->GetNumberOfIds() > 0){
77       vtkIdType aNewPointId = aUsedPoints->InsertNextPoint(anInput->GetPoint(aPointId));
78       aPointData->CopyData(anInput->GetPointData(), aPointId, aNewPointId);
79       aId2IdMap[aPointId] = aNewPointId;
80     }
81   }
82   aPointData->Squeeze();
83   anOutput->SetPoints(aUsedPoints);
84   aUsedPoints->Delete();
85   anIdList->Delete();
86
87   vtkCellData *aCellData = anOutput->GetCellData();
88   aCellData->CopyAllocate(anInput->GetCellData());
89
90   anOutput->Allocate(anInput->GetNumberOfCells()); 
91   vtkIdList *anOldPointsIds = vtkIdList::New();
92   vtkIdList *aNewPointsIds = vtkIdList::New();
93   aNewPointsIds->Allocate(VTK_CELL_SIZE);
94   iEnd = anInput->GetNumberOfCells();
95   for(vtkIdType aCellId = 0; aCellId < iEnd; aCellId++){
96     anInput->GetCellPoints(aCellId, anOldPointsIds);
97     vtkIdType aNbPointsInCell = anOldPointsIds->GetNumberOfIds();
98     aNewPointsIds->Reset();
99     for(vtkIdType i = 0; i < aNbPointsInCell; i++){
100       vtkIdType anOldId = anOldPointsIds->GetId(i);
101       TId2IdMap::iterator anIter = aId2IdMap.find(anOldId);
102       if(anIter == aId2IdMap.end())
103         goto NEXT_CELL;
104       vtkIdType aNewId = anIter->second;
105       aNewPointsIds->InsertNextId(aNewId);
106     }
107     {
108       vtkIdType aNewCellId = anOutput->InsertNextCell(anInput->GetCellType(aCellId), aNewPointsIds);
109       aCellData->CopyData(anInput->GetCellData(), aCellId, aNewCellId);
110     }
111   NEXT_CELL:
112     continue;
113   }
114   aCellData->Squeeze();
115   anOldPointsIds->Delete();
116   aNewPointsIds->Delete();
117   anOutput->Squeeze();
118   return 1;
119 }
120