1 // Copyright (C) 2007-2021 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, or (at your option) any later version.
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
23 // SALOME OBJECT : kernel of SALOME component
24 // File : SALOME_GeometryFilter.cxx
25 // Author : Michael ZORIN
27 #include "VTKViewer_ShrinkFilter.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>
38 vtkStandardNewMacro(VTKViewer_ShrinkFilter)
40 /*!Constructor. Sets store mapping to zero.*/
41 VTKViewer_ShrinkFilter::VTKViewer_ShrinkFilter():
46 VTKViewer_ShrinkFilter::~VTKViewer_ShrinkFilter()
50 /*!Execute method. Calculate output.*/
51 int VTKViewer_ShrinkFilter::RequestData(
52 vtkInformation *vtkNotUsed(request),
53 vtkInformationVector **inputVector,
54 vtkInformationVector *outputVector)
56 // get the info objects
57 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
58 vtkInformation *outInfo = outputVector->GetInformationObject(0);
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()));
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;
76 vtkDebugMacro(<<"Shrinking cells");
78 numCells=input->GetNumberOfCells();
79 numPts = input->GetNumberOfPoints();
80 if (numCells < 1 || numPts < 1)
82 vtkErrorMacro(<<"No data to shrink!");
86 ptIds = vtkIdList::New();
87 ptIds->Allocate(VTK_CELL_SIZE);
88 newPtIds = vtkIdList::New();
89 newPtIds->Allocate(VTK_CELL_SIZE);
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);
98 // Traverse all cells, obtaining node coordinates. Compute "center" of cell,
99 // then create new vertices shrunk towards center.
101 tenth = numCells/10 + 1;
104 myVTK2ObjIds.clear();
105 myVTK2ObjIds.reserve(numCells);
108 for (cellId=0; cellId < numCells && !abort; cellId++)
110 input->GetCellPoints(cellId, ptIds);
111 numIds = ptIds->GetNumberOfIds();
113 //abort/progress methods
114 if (cellId % tenth == 0)
117 this->UpdateProgress (decimal);
118 abort = this->GetAbortExecute();
121 // get the center of the cell
122 center[0] = center[1] = center[2] = 0.0;
123 for (i=0; i < numIds; i++)
125 p = input->GetPoint(ptIds->GetId(i));
126 for (j=0; j < 3; j++)
136 // Create new points and cells
138 for (i=0; i < numIds; i++)
140 p = input->GetPoint(ptIds->GetId(i));
141 for (j=0; j < 3; j++)
143 pt[j] = center[j] + this->ShrinkFactor*(p[j] - center[j]);
146 oldId = ptIds->GetId(i);
147 newId = newPts->InsertNextPoint(pt);
149 myVTK2ObjIds.push_back(oldId);
150 newPtIds->InsertId(i,newId);
152 outPD->CopyData(pd, oldId, newId);
154 output->InsertNextCell(input->GetCellType(cellId), newPtIds);
157 // Update ourselves and release memory
159 output->GetCellData()->PassData(input->GetCellData());
161 output->SetPoints(newPts);
171 /*!Sets store mapping.*/
172 void VTKViewer_ShrinkFilter::SetStoreMapping(int theStoreMapping){
173 myStoreMapping = theStoreMapping;
178 /*!Return node object id by vtk node id.
179 *\retval -1 - if no object, else return id.
181 vtkIdType VTKViewer_ShrinkFilter::GetNodeObjId(int theVtkID)
183 if ( myVTK2ObjIds.empty() || theVtkID > (int)myVTK2ObjIds.size() )
185 return myVTK2ObjIds.at(theVtkID);