Salome HOME
Copyright update 2020
[modules/gui.git] / src / VTKViewer / VTKViewer_ShrinkFilter.cxx
1 // Copyright (C) 2007-2020  CEA/DEN, EDF R&D, 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   int i, j, numIds, abort=0;
68   vtkIdType cellId, numCells, numPts;
69   vtkIdType oldId, newId;
70   double center[3], *p, pt[3];
71   vtkPointData *pd, *outPD;;
72   vtkIdList *ptIds, *newPtIds;
73   vtkIdType tenth;
74   double decimal;
75
76   vtkDebugMacro(<<"Shrinking cells");
77
78   numCells=input->GetNumberOfCells();
79   numPts = input->GetNumberOfPoints();
80   if (numCells < 1 || numPts < 1)
81     {
82     vtkErrorMacro(<<"No data to shrink!");
83     return 0;
84     }
85
86   ptIds = vtkIdList::New();
87   ptIds->Allocate(VTK_CELL_SIZE);
88   newPtIds = vtkIdList::New();
89   newPtIds->Allocate(VTK_CELL_SIZE);
90
91   output->Allocate(numCells);
92   newPts = vtkPoints::New();
93   newPts->Allocate(numPts*8,numPts);
94   pd = input->GetPointData();
95   outPD = output->GetPointData();
96   outPD->CopyAllocate(pd,numPts*8,numPts);
97
98   // Traverse all cells, obtaining node coordinates.  Compute "center" of cell,
99   // then create new vertices shrunk towards center.
100   //
101   tenth   = numCells/10 + 1;
102   decimal = 0.0;
103   if(myStoreMapping){
104     myVTK2ObjIds.clear();
105     myVTK2ObjIds.reserve(numCells);
106   }
107
108   for (cellId=0; cellId < numCells && !abort; cellId++)
109     {
110     input->GetCellPoints(cellId, ptIds);
111     numIds = ptIds->GetNumberOfIds();
112
113     //abort/progress methods
114     if (cellId % tenth == 0) 
115       {
116       decimal += 0.1;
117       this->UpdateProgress (decimal);
118       abort = this->GetAbortExecute();
119       }
120
121     // get the center of the cell
122     center[0] = center[1] = center[2] = 0.0;
123     for (i=0; i < numIds; i++)
124       {
125       p = input->GetPoint(ptIds->GetId(i));
126       for (j=0; j < 3; j++)
127         {
128         center[j] += p[j];
129         }
130       }
131     for (j=0; j<3; j++)
132       {
133       center[j] /= numIds;
134       }
135
136     // Create new points and cells
137     newPtIds->Reset();
138     for (i=0; i < numIds; i++)
139       {
140       p = input->GetPoint(ptIds->GetId(i));
141       for (j=0; j < 3; j++)
142         {
143         pt[j] = center[j] + this->ShrinkFactor*(p[j] - center[j]);
144         }
145
146       oldId = ptIds->GetId(i);
147       newId = newPts->InsertNextPoint(pt);
148       if(myStoreMapping)
149         myVTK2ObjIds.push_back(oldId);
150       newPtIds->InsertId(i,newId);
151
152       outPD->CopyData(pd, oldId, newId);
153       }
154     output->InsertNextCell(input->GetCellType(cellId), newPtIds);
155     }//for all cells
156
157   // Update ourselves and release memory
158   //
159   output->GetCellData()->PassData(input->GetCellData());
160
161   output->SetPoints(newPts);
162   output->Squeeze();
163
164   ptIds->Delete();
165   newPtIds->Delete();
166   newPts->Delete();
167   
168   return 1;
169 }
170
171 /*!Sets store mapping.*/
172 void VTKViewer_ShrinkFilter::SetStoreMapping(int theStoreMapping){
173   myStoreMapping = theStoreMapping;
174   this->Modified();
175 }
176
177
178 /*!Return node object id by vtk node id.
179  *\retval -1 - if no object, else return id.
180  */
181 vtkIdType VTKViewer_ShrinkFilter::GetNodeObjId(int theVtkID)
182 {
183   if ( myVTK2ObjIds.empty() || theVtkID > (int)myVTK2ObjIds.size() )
184     return -1;
185   return myVTK2ObjIds.at(theVtkID);
186 }