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