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