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