1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 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
22 // SALOME OBJECT : kernel of SALOME component
23 // File : SALOME_GeometryFilter.cxx
24 // Author : Michael ZORIN
28 #include "VTKViewer_ShrinkFilter.h"
31 #include <vtkCellData.h>
32 #include <vtkIdList.h>
33 #include <vtkObjectFactory.h>
34 #include <vtkPointData.h>
35 #include <vtkUnstructuredGrid.h>
36 #include <vtkInformation.h>
37 #include <vtkInformationVector.h>
39 vtkCxxRevisionMacro(VTKViewer_ShrinkFilter, "$Revision$");
40 vtkStandardNewMacro(VTKViewer_ShrinkFilter);
42 /*!Constructor. Sets store mapping to zero.*/
43 VTKViewer_ShrinkFilter::VTKViewer_ShrinkFilter():
48 VTKViewer_ShrinkFilter::~VTKViewer_ShrinkFilter()
52 /*!Execute method. Calculate output.*/
53 int VTKViewer_ShrinkFilter::RequestData(
54 vtkInformation *vtkNotUsed(request),
55 vtkInformationVector **inputVector,
56 vtkInformationVector *outputVector)
58 // get the info objects
59 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
60 vtkInformation *outInfo = outputVector->GetInformationObject(0);
62 // get the input and ouptut
63 vtkDataSet *input = vtkDataSet::SafeDownCast(
64 inInfo->Get(vtkDataObject::DATA_OBJECT()));
65 vtkUnstructuredGrid *output = vtkUnstructuredGrid::SafeDownCast(
66 outInfo->Get(vtkDataObject::DATA_OBJECT()));
69 int i, j, numIds, abort=0;
70 vtkIdType cellId, numCells, numPts;
71 vtkIdType oldId, newId;
72 vtkFloatingPointType center[3], *p, pt[3];
73 vtkPointData *pd, *outPD;;
74 vtkIdList *ptIds, *newPtIds;
76 vtkFloatingPointType decimal;
78 vtkDebugMacro(<<"Shrinking cells");
80 numCells=input->GetNumberOfCells();
81 numPts = input->GetNumberOfPoints();
82 if (numCells < 1 || numPts < 1)
84 vtkErrorMacro(<<"No data to shrink!");
88 ptIds = vtkIdList::New();
89 ptIds->Allocate(VTK_CELL_SIZE);
90 newPtIds = vtkIdList::New();
91 newPtIds->Allocate(VTK_CELL_SIZE);
93 output->Allocate(numCells);
94 newPts = vtkPoints::New();
95 newPts->Allocate(numPts*8,numPts);
96 pd = input->GetPointData();
97 outPD = output->GetPointData();
98 outPD->CopyAllocate(pd,numPts*8,numPts);
100 // Traverse all cells, obtaining node coordinates. Compute "center" of cell,
101 // then create new vertices shrunk towards center.
103 tenth = numCells/10 + 1;
106 myVTK2ObjIds.clear();
107 myVTK2ObjIds.reserve(numCells);
110 for (cellId=0; cellId < numCells && !abort; cellId++)
112 input->GetCellPoints(cellId, ptIds);
113 numIds = ptIds->GetNumberOfIds();
115 //abort/progress methods
116 if (cellId % tenth == 0)
119 this->UpdateProgress (decimal);
120 abort = this->GetAbortExecute();
123 // get the center of the cell
124 center[0] = center[1] = center[2] = 0.0;
125 for (i=0; i < numIds; i++)
127 p = input->GetPoint(ptIds->GetId(i));
128 for (j=0; j < 3; j++)
138 // Create new points and cells
140 for (i=0; i < numIds; i++)
142 p = input->GetPoint(ptIds->GetId(i));
143 for (j=0; j < 3; j++)
145 pt[j] = center[j] + this->ShrinkFactor*(p[j] - center[j]);
148 oldId = ptIds->GetId(i);
149 newId = newPts->InsertNextPoint(pt);
151 myVTK2ObjIds.push_back(oldId);
152 newPtIds->InsertId(i,newId);
154 outPD->CopyData(pd, oldId, newId);
156 output->InsertNextCell(input->GetCellType(cellId), newPtIds);
159 // Update ourselves and release memory
161 output->GetCellData()->PassData(input->GetCellData());
163 output->SetPoints(newPts);
173 /*!Sets store mapping.*/
174 void VTKViewer_ShrinkFilter::SetStoreMapping(int theStoreMapping){
175 myStoreMapping = theStoreMapping;
180 /*!Return node object id by vtk node id.
181 *\retval -1 - if no object, else return id.
183 vtkIdType VTKViewer_ShrinkFilter::GetNodeObjId(int theVtkID)
185 if ( myVTK2ObjIds.empty() || theVtkID > (int)myVTK2ObjIds.size() )
187 return myVTK2ObjIds.at(theVtkID);