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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
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>
39 static int MYDEBUG = 0;
40 static int MYDEBUGWITHFILES = 0;
42 static int MYDEBUG = 0;
43 static int MYDEBUGWITHFILES = 0;
46 vtkCxxRevisionMacro(VTKViewer_ShrinkFilter, "$Revision$");
47 vtkStandardNewMacro(VTKViewer_ShrinkFilter);
49 /*!Constructor. Sets store mapping to zero.*/
50 VTKViewer_ShrinkFilter::VTKViewer_ShrinkFilter():
55 VTKViewer_ShrinkFilter::~VTKViewer_ShrinkFilter()
59 /*!Execute method. Calculate output.*/
60 void VTKViewer_ShrinkFilter::Execute()
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();
74 vtkDebugMacro(<<"Shrinking cells");
76 numCells=input->GetNumberOfCells();
77 numPts = input->GetNumberOfPoints();
78 if (numCells < 1 || numPts < 1)
80 vtkErrorMacro(<<"No data to shrink!");
84 ptIds = vtkIdList::New();
85 ptIds->Allocate(VTK_CELL_SIZE);
86 newPtIds = vtkIdList::New();
87 newPtIds->Allocate(VTK_CELL_SIZE);
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);
96 // Traverse all cells, obtaining node coordinates. Compute "center" of cell,
97 // then create new vertices shrunk towards center.
99 tenth = numCells/10 + 1;
102 myVTK2ObjIds.clear();
103 myVTK2ObjIds.reserve(numCells);
106 for (cellId=0; cellId < numCells && !abort; cellId++)
108 input->GetCellPoints(cellId, ptIds);
109 numIds = ptIds->GetNumberOfIds();
111 //abort/progress methods
112 if (cellId % tenth == 0)
115 this->UpdateProgress (decimal);
116 abort = this->GetAbortExecute();
119 // get the center of the cell
120 center[0] = center[1] = center[2] = 0.0;
121 for (i=0; i < numIds; i++)
123 p = input->GetPoint(ptIds->GetId(i));
124 for (j=0; j < 3; j++)
134 // Create new points and cells
136 for (i=0; i < numIds; i++)
138 p = input->GetPoint(ptIds->GetId(i));
139 for (j=0; j < 3; j++)
141 pt[j] = center[j] + this->ShrinkFactor*(p[j] - center[j]);
144 oldId = ptIds->GetId(i);
145 newId = newPts->InsertNextPoint(pt);
147 myVTK2ObjIds.push_back(oldId);
148 newPtIds->InsertId(i,newId);
150 outPD->CopyData(pd, oldId, newId);
152 output->InsertNextCell(input->GetCellType(cellId), newPtIds);
155 // Update ourselves and release memory
157 output->GetCellData()->PassData(input->GetCellData());
159 output->SetPoints(newPts);
167 /*!Sets store mapping.*/
168 void VTKViewer_ShrinkFilter::SetStoreMapping(int theStoreMapping){
169 myStoreMapping = theStoreMapping;
174 /*!Return node object id by vtk node id.
175 *\retval -1 - if no object, else return id.
177 vtkIdType VTKViewer_ShrinkFilter::GetNodeObjId(int theVtkID){
178 if(myVTK2ObjIds.empty() || theVtkID > myVTK2ObjIds.size()) return -1;
179 return myVTK2ObjIds.at(theVtkID);