Back to index

salome-gui  6.5.0
VTKViewer_ShrinkFilter.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 //  SALOME OBJECT : kernel of SALOME component
00024 //  File   : SALOME_GeometryFilter.cxx
00025 //  Author : Michael ZORIN
00026 
00027 #include "VTKViewer_ShrinkFilter.h"
00028 
00029 #include <vtkCell.h>
00030 #include <vtkCellData.h>
00031 #include <vtkIdList.h>
00032 #include <vtkObjectFactory.h>
00033 #include <vtkPointData.h>
00034 #include <vtkUnstructuredGrid.h>
00035 #include <vtkInformation.h>
00036 #include <vtkInformationVector.h>
00037 
00038 vtkCxxRevisionMacro(VTKViewer_ShrinkFilter, "$Revision: 1.9.2.1.8.3.2.1 $");
00039 vtkStandardNewMacro(VTKViewer_ShrinkFilter);
00040 
00042 VTKViewer_ShrinkFilter::VTKViewer_ShrinkFilter(): 
00043   myStoreMapping(0)
00044 {}
00045 
00047 VTKViewer_ShrinkFilter::~VTKViewer_ShrinkFilter()
00048 {}
00049 
00050 
00052 int VTKViewer_ShrinkFilter::RequestData(
00053   vtkInformation *vtkNotUsed(request),
00054   vtkInformationVector **inputVector,
00055   vtkInformationVector *outputVector)
00056 {
00057   // get the info objects
00058   vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
00059   vtkInformation *outInfo = outputVector->GetInformationObject(0);
00060 
00061   // get the input and ouptut
00062   vtkDataSet *input = vtkDataSet::SafeDownCast(
00063     inInfo->Get(vtkDataObject::DATA_OBJECT()));
00064   vtkUnstructuredGrid *output = vtkUnstructuredGrid::SafeDownCast(
00065     outInfo->Get(vtkDataObject::DATA_OBJECT()));
00066 
00067   vtkPoints *newPts;
00068   int i, j, numIds, abort=0;
00069   vtkIdType cellId, numCells, numPts;
00070   vtkIdType oldId, newId;
00071   vtkFloatingPointType center[3], *p, pt[3];
00072   vtkPointData *pd, *outPD;;
00073   vtkIdList *ptIds, *newPtIds;
00074   vtkIdType tenth;
00075   vtkFloatingPointType decimal;
00076 
00077   vtkDebugMacro(<<"Shrinking cells");
00078 
00079   numCells=input->GetNumberOfCells();
00080   numPts = input->GetNumberOfPoints();
00081   if (numCells < 1 || numPts < 1)
00082     {
00083     vtkErrorMacro(<<"No data to shrink!");
00084     return 0;
00085     }
00086 
00087   ptIds = vtkIdList::New();
00088   ptIds->Allocate(VTK_CELL_SIZE);
00089   newPtIds = vtkIdList::New();
00090   newPtIds->Allocate(VTK_CELL_SIZE);
00091 
00092   output->Allocate(numCells);
00093   newPts = vtkPoints::New();
00094   newPts->Allocate(numPts*8,numPts);
00095   pd = input->GetPointData();
00096   outPD = output->GetPointData();
00097   outPD->CopyAllocate(pd,numPts*8,numPts);
00098 
00099   // Traverse all cells, obtaining node coordinates.  Compute "center" of cell,
00100   // then create new vertices shrunk towards center.
00101   //
00102   tenth   = numCells/10 + 1;
00103   decimal = 0.0;
00104   if(myStoreMapping){
00105     myVTK2ObjIds.clear();
00106     myVTK2ObjIds.reserve(numCells);
00107   }
00108 
00109   for (cellId=0; cellId < numCells && !abort; cellId++)
00110     {
00111     input->GetCellPoints(cellId, ptIds);
00112     numIds = ptIds->GetNumberOfIds();
00113 
00114     //abort/progress methods
00115     if (cellId % tenth == 0) 
00116       {
00117       decimal += 0.1;
00118       this->UpdateProgress (decimal);
00119       abort = this->GetAbortExecute();
00120       }
00121 
00122     // get the center of the cell
00123     center[0] = center[1] = center[2] = 0.0;
00124     for (i=0; i < numIds; i++)
00125       {
00126       p = input->GetPoint(ptIds->GetId(i));
00127       for (j=0; j < 3; j++)
00128         {
00129         center[j] += p[j];
00130         }
00131       }
00132     for (j=0; j<3; j++)
00133       {
00134       center[j] /= numIds;
00135       }
00136 
00137     // Create new points and cells
00138     newPtIds->Reset();
00139     for (i=0; i < numIds; i++)
00140       {
00141       p = input->GetPoint(ptIds->GetId(i));
00142       for (j=0; j < 3; j++)
00143         {
00144         pt[j] = center[j] + this->ShrinkFactor*(p[j] - center[j]);
00145         }
00146 
00147       oldId = ptIds->GetId(i);
00148       newId = newPts->InsertNextPoint(pt);
00149       if(myStoreMapping)
00150         myVTK2ObjIds.push_back(oldId);
00151       newPtIds->InsertId(i,newId);
00152 
00153       outPD->CopyData(pd, oldId, newId);
00154       }
00155     output->InsertNextCell(input->GetCellType(cellId), newPtIds);
00156     }//for all cells
00157 
00158   // Update ourselves and release memory
00159   //
00160   output->GetCellData()->PassData(input->GetCellData());
00161 
00162   output->SetPoints(newPts);
00163   output->Squeeze();
00164 
00165   ptIds->Delete();
00166   newPtIds->Delete();
00167   newPts->Delete();
00168   
00169   return 1;
00170 }
00171 
00173 void VTKViewer_ShrinkFilter::SetStoreMapping(int theStoreMapping){
00174   myStoreMapping = theStoreMapping;
00175   this->Modified();
00176 }
00177 
00178 
00182 vtkIdType VTKViewer_ShrinkFilter::GetNodeObjId(int theVtkID)
00183 {
00184   if ( myVTK2ObjIds.empty() || theVtkID > (int)myVTK2ObjIds.size() )
00185     return -1;
00186   return myVTK2ObjIds.at(theVtkID);
00187 }