1 // Copyright (C) 2007-2022 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 vtkIdType i, numIds, abort=0;
69 vtkIdType cellId, numCells, numPts;
70 vtkIdType oldId, newId;
71 double center[3], *p, pt[3];
72 vtkPointData *pd, *outPD;;
73 vtkIdList *ptIds, *newPtIds;
77 vtkDebugMacro(<<"Shrinking cells");
79 numCells=input->GetNumberOfCells();
80 numPts = input->GetNumberOfPoints();
81 if (numCells < 1 || numPts < 1)
83 vtkErrorMacro(<<"No data to shrink!");
87 ptIds = vtkIdList::New();
88 ptIds->Allocate(VTK_CELL_SIZE);
89 newPtIds = vtkIdList::New();
90 newPtIds->Allocate(VTK_CELL_SIZE);
92 output->Allocate(numCells);
93 newPts = vtkPoints::New();
94 newPts->Allocate(numPts*8,numPts);
95 pd = input->GetPointData();
96 outPD = output->GetPointData();
97 outPD->CopyAllocate(pd,numPts*8,numPts);
99 // Traverse all cells, obtaining node coordinates. Compute "center" of cell,
100 // then create new vertices shrunk towards center.
102 tenth = numCells/10 + 1;
105 myVTK2ObjIds.clear();
106 myVTK2ObjIds.reserve(numCells);
109 for (cellId=0; cellId < numCells && !abort; cellId++)
111 input->GetCellPoints(cellId, ptIds);
112 numIds = ptIds->GetNumberOfIds();
114 //abort/progress methods
115 if (cellId % tenth == 0)
118 this->UpdateProgress (decimal);
119 abort = this->GetAbortExecute();
122 // get the center of the cell
123 center[0] = center[1] = center[2] = 0.0;
124 for (i=0; i < numIds; i++)
126 p = input->GetPoint(ptIds->GetId(i));
127 for (j=0; j < 3; j++)
137 // Create new points and cells
139 for (i=0; i < numIds; i++)
141 p = input->GetPoint(ptIds->GetId(i));
142 for (j=0; j < 3; j++)
144 pt[j] = center[j] + this->ShrinkFactor*(p[j] - center[j]);
147 oldId = ptIds->GetId(i);
148 newId = newPts->InsertNextPoint(pt);
150 myVTK2ObjIds.push_back(oldId);
151 newPtIds->InsertId(i,newId);
153 outPD->CopyData(pd, oldId, newId);
155 output->InsertNextCell(input->GetCellType(cellId), newPtIds);
158 // Update ourselves and release memory
160 output->GetCellData()->PassData(input->GetCellData());
162 output->SetPoints(newPts);
172 /*!Sets store mapping.*/
173 void VTKViewer_ShrinkFilter::SetStoreMapping(int theStoreMapping){
174 myStoreMapping = theStoreMapping;
179 /*!Return node object id by vtk node id.
180 *\retval -1 - if no object, else return id.
182 vtkIdType VTKViewer_ShrinkFilter::GetNodeObjId(vtkIdType theVtkID)
184 if ( myVTK2ObjIds.empty() || theVtkID > (vtkIdType)myVTK2ObjIds.size() )
186 return myVTK2ObjIds.at(theVtkID);