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