Salome HOME
Porting to Mandrake 10.1 and new products:
[modules/kernel.git] / src / VTKFilter / SALOME_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 "SALOME_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 using namespace std;
38
39 #ifdef _DEBUG_
40 static int MYDEBUG = 0;
41 static int MYDEBUGWITHFILES = 0;
42 #else
43 static int MYDEBUG = 0;
44 static int MYDEBUGWITHFILES = 0;
45 #endif
46
47 vtkCxxRevisionMacro(SALOME_ShrinkFilter, "$Revision$");
48 vtkStandardNewMacro(SALOME_ShrinkFilter);
49
50
51 SALOME_ShrinkFilter::SALOME_ShrinkFilter(): 
52   myStoreMapping(0)
53 {}
54
55
56 SALOME_ShrinkFilter::~SALOME_ShrinkFilter()
57 {}
58
59
60 void SALOME_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
168 void SALOME_ShrinkFilter::SetStoreMapping(int theStoreMapping){
169   myStoreMapping = theStoreMapping;
170   this->Modified();
171 }
172
173
174 vtkIdType SALOME_ShrinkFilter::GetNodeObjId(int theVtkID){
175   if(myVTK2ObjIds.empty() || theVtkID > myVTK2ObjIds.size()) return -1;
176   return myVTK2ObjIds[theVtkID];
177 }