1 // SALOME OBJECT : kernel of SALOME component
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
24 // File : SALOME_GeometryFilter.cxx
25 // Author : Michael ZORIN
29 #include "VTKViewer_ShrinkFilter.h"
32 #include <vtkCellData.h>
33 #include <vtkIdList.h>
34 #include <vtkObjectFactory.h>
35 #include <vtkPointData.h>
36 #include <vtkUnstructuredGrid.h>
38 vtkCxxRevisionMacro(VTKViewer_ShrinkFilter, "$Revision$");
39 vtkStandardNewMacro(VTKViewer_ShrinkFilter);
41 /*!Constructor. Sets store mapping to zero.*/
42 VTKViewer_ShrinkFilter::VTKViewer_ShrinkFilter():
47 VTKViewer_ShrinkFilter::~VTKViewer_ShrinkFilter()
51 /*!Execute method. Calculate output.*/
52 void VTKViewer_ShrinkFilter::Execute()
55 int i, j, numIds, abort=0;
56 vtkIdType cellId, numCells, numPts;
57 vtkIdType oldId, newId;
58 vtkFloatingPointType center[3], *p, pt[3];
59 vtkPointData *pd, *outPD;;
60 vtkIdList *ptIds, *newPtIds;
61 vtkDataSet *input= this->GetInput();
62 vtkUnstructuredGrid *output = this->GetOutput();
64 vtkFloatingPointType decimal;
66 vtkDebugMacro(<<"Shrinking cells");
68 numCells=input->GetNumberOfCells();
69 numPts = input->GetNumberOfPoints();
70 if (numCells < 1 || numPts < 1)
72 vtkErrorMacro(<<"No data to shrink!");
76 ptIds = vtkIdList::New();
77 ptIds->Allocate(VTK_CELL_SIZE);
78 newPtIds = vtkIdList::New();
79 newPtIds->Allocate(VTK_CELL_SIZE);
81 output->Allocate(numCells);
82 newPts = vtkPoints::New();
83 newPts->Allocate(numPts*8,numPts);
84 pd = input->GetPointData();
85 outPD = output->GetPointData();
86 outPD->CopyAllocate(pd,numPts*8,numPts);
88 // Traverse all cells, obtaining node coordinates. Compute "center" of cell,
89 // then create new vertices shrunk towards center.
91 tenth = numCells/10 + 1;
95 myVTK2ObjIds.reserve(numCells);
98 for (cellId=0; cellId < numCells && !abort; cellId++)
100 input->GetCellPoints(cellId, ptIds);
101 numIds = ptIds->GetNumberOfIds();
103 //abort/progress methods
104 if (cellId % tenth == 0)
107 this->UpdateProgress (decimal);
108 abort = this->GetAbortExecute();
111 // get the center of the cell
112 center[0] = center[1] = center[2] = 0.0;
113 for (i=0; i < numIds; i++)
115 p = input->GetPoint(ptIds->GetId(i));
116 for (j=0; j < 3; j++)
126 // Create new points and cells
128 for (i=0; i < numIds; i++)
130 p = input->GetPoint(ptIds->GetId(i));
131 for (j=0; j < 3; j++)
133 pt[j] = center[j] + this->ShrinkFactor*(p[j] - center[j]);
136 oldId = ptIds->GetId(i);
137 newId = newPts->InsertNextPoint(pt);
139 myVTK2ObjIds.push_back(oldId);
140 newPtIds->InsertId(i,newId);
142 outPD->CopyData(pd, oldId, newId);
144 output->InsertNextCell(input->GetCellType(cellId), newPtIds);
147 // Update ourselves and release memory
149 output->GetCellData()->PassData(input->GetCellData());
151 output->SetPoints(newPts);
159 /*!Sets store mapping.*/
160 void VTKViewer_ShrinkFilter::SetStoreMapping(int theStoreMapping){
161 myStoreMapping = theStoreMapping;
166 /*!Return node object id by vtk node id.
167 *\retval -1 - if no object, else return id.
169 vtkIdType VTKViewer_ShrinkFilter::GetNodeObjId(int theVtkID){
170 if(myVTK2ObjIds.empty() || theVtkID > myVTK2ObjIds.size()) return -1;
171 return myVTK2ObjIds.at(theVtkID);