]> SALOME platform Git repositories - modules/gui.git/blob - src/VTKViewer/VTKViewer_ShrinkFilter.cxx
Salome HOME
0084d939d5d9a7325426460c963f3875053815b1
[modules/gui.git] / src / VTKViewer / VTKViewer_ShrinkFilter.cxx
1 //  Copyright (C) 2007-2008  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 //  SALOME OBJECT : kernel of SALOME component
23 //  File   : SALOME_GeometryFilter.cxx
24 //  Author : Michael ZORIN
25 //  Module : SALOME
26 //  $Header$
27 //
28 #include "VTKViewer_ShrinkFilter.h"
29
30 #include <vtkCell.h>
31 #include <vtkCellData.h>
32 #include <vtkIdList.h>
33 #include <vtkObjectFactory.h>
34 #include <vtkPointData.h>
35 #include <vtkUnstructuredGrid.h>
36 #include <vtkInformation.h>
37 #include <vtkInformationVector.h>
38
39 vtkCxxRevisionMacro(VTKViewer_ShrinkFilter, "$Revision$");
40 vtkStandardNewMacro(VTKViewer_ShrinkFilter);
41
42 /*!Constructor. Sets store mapping to zero.*/
43 VTKViewer_ShrinkFilter::VTKViewer_ShrinkFilter(): 
44   myStoreMapping(0)
45 {}
46
47 /*!Destructor.*/
48 VTKViewer_ShrinkFilter::~VTKViewer_ShrinkFilter()
49 {}
50
51
52 /*!Execute method. Calculate output.*/
53 int VTKViewer_ShrinkFilter::RequestData(
54   vtkInformation *vtkNotUsed(request),
55   vtkInformationVector **inputVector,
56   vtkInformationVector *outputVector)
57 {
58   // get the info objects
59   vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
60   vtkInformation *outInfo = outputVector->GetInformationObject(0);
61
62   // get the input and ouptut
63   vtkDataSet *input = vtkDataSet::SafeDownCast(
64     inInfo->Get(vtkDataObject::DATA_OBJECT()));
65   vtkUnstructuredGrid *output = vtkUnstructuredGrid::SafeDownCast(
66     outInfo->Get(vtkDataObject::DATA_OBJECT()));
67
68   vtkPoints *newPts;
69   int i, j, numIds, abort=0;
70   vtkIdType cellId, numCells, numPts;
71   vtkIdType oldId, newId;
72   vtkFloatingPointType center[3], *p, pt[3];
73   vtkPointData *pd, *outPD;;
74   vtkIdList *ptIds, *newPtIds;
75   vtkIdType tenth;
76   vtkFloatingPointType decimal;
77
78   vtkDebugMacro(<<"Shrinking cells");
79
80   numCells=input->GetNumberOfCells();
81   numPts = input->GetNumberOfPoints();
82   if (numCells < 1 || numPts < 1)
83     {
84     vtkErrorMacro(<<"No data to shrink!");
85     return 0;
86     }
87
88   ptIds = vtkIdList::New();
89   ptIds->Allocate(VTK_CELL_SIZE);
90   newPtIds = vtkIdList::New();
91   newPtIds->Allocate(VTK_CELL_SIZE);
92
93   output->Allocate(numCells);
94   newPts = vtkPoints::New();
95   newPts->Allocate(numPts*8,numPts);
96   pd = input->GetPointData();
97   outPD = output->GetPointData();
98   outPD->CopyAllocate(pd,numPts*8,numPts);
99
100   // Traverse all cells, obtaining node coordinates.  Compute "center" of cell,
101   // then create new vertices shrunk towards center.
102   //
103   tenth   = numCells/10 + 1;
104   decimal = 0.0;
105   if(myStoreMapping){
106     myVTK2ObjIds.clear();
107     myVTK2ObjIds.reserve(numCells);
108   }
109
110   for (cellId=0; cellId < numCells && !abort; cellId++)
111     {
112     input->GetCellPoints(cellId, ptIds);
113     numIds = ptIds->GetNumberOfIds();
114
115     //abort/progress methods
116     if (cellId % tenth == 0) 
117       {
118       decimal += 0.1;
119       this->UpdateProgress (decimal);
120       abort = this->GetAbortExecute();
121       }
122
123     // get the center of the cell
124     center[0] = center[1] = center[2] = 0.0;
125     for (i=0; i < numIds; i++)
126       {
127       p = input->GetPoint(ptIds->GetId(i));
128       for (j=0; j < 3; j++)
129         {
130         center[j] += p[j];
131         }
132       }
133     for (j=0; j<3; j++)
134       {
135       center[j] /= numIds;
136       }
137
138     // Create new points and cells
139     newPtIds->Reset();
140     for (i=0; i < numIds; i++)
141       {
142       p = input->GetPoint(ptIds->GetId(i));
143       for (j=0; j < 3; j++)
144         {
145         pt[j] = center[j] + this->ShrinkFactor*(p[j] - center[j]);
146         }
147
148       oldId = ptIds->GetId(i);
149       newId = newPts->InsertNextPoint(pt);
150       if(myStoreMapping)
151         myVTK2ObjIds.push_back(oldId);
152       newPtIds->InsertId(i,newId);
153
154       outPD->CopyData(pd, oldId, newId);
155       }
156     output->InsertNextCell(input->GetCellType(cellId), newPtIds);
157     }//for all cells
158
159   // Update ourselves and release memory
160   //
161   output->GetCellData()->PassData(input->GetCellData());
162
163   output->SetPoints(newPts);
164   output->Squeeze();
165
166   ptIds->Delete();
167   newPtIds->Delete();
168   newPts->Delete();
169   
170   return 1;
171 }
172
173 /*!Sets store mapping.*/
174 void VTKViewer_ShrinkFilter::SetStoreMapping(int theStoreMapping){
175   myStoreMapping = theStoreMapping;
176   this->Modified();
177 }
178
179
180 /*!Return node object id by vtk node id.
181  *\retval -1 - if no object, else return id.
182  */
183 vtkIdType VTKViewer_ShrinkFilter::GetNodeObjId(int theVtkID)
184 {
185   if ( myVTK2ObjIds.empty() || theVtkID > (int)myVTK2ObjIds.size() )
186     return -1;
187   return myVTK2ObjIds.at(theVtkID);
188 }