Back to index

salome-gui  6.5.0
VTKViewer_CellCenters.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 #include "VTKViewer_CellCenters.h"
00024 
00025 #include <vtkCell.h>
00026 #include <vtkCellData.h>
00027 #include <vtkDataSet.h>
00028 #include <vtkInformation.h>
00029 #include <vtkInformationVector.h>
00030 #include <vtkObjectFactory.h>
00031 #include <vtkPointData.h>
00032 #include <vtkPoints.h>
00033 #include <vtkPolyData.h>
00034 #include <vtkCellArray.h>
00035 
00036 vtkCxxRevisionMacro(VTKViewer_CellCenters, "$Revision: 1.1.6.3.14.1 $");
00037 vtkStandardNewMacro(VTKViewer_CellCenters);
00038 
00048 VTKViewer_CellCenters::VTKViewer_CellCenters()
00049 {
00050 }
00051 
00055 int VTKViewer_CellCenters::RequestData(
00056   vtkInformation *vtkNotUsed(request),
00057   vtkInformationVector **inputVector,
00058   vtkInformationVector *outputVector)
00059 {
00060   // get the info objects
00061   vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
00062   vtkInformation *outInfo = outputVector->GetInformationObject(0);
00063 
00064   // get the input and ouptut
00065   vtkDataSet *input = vtkDataSet::SafeDownCast(
00066     inInfo->Get(vtkDataObject::DATA_OBJECT()));
00067   vtkPolyData *output = vtkPolyData::SafeDownCast(
00068     outInfo->Get(vtkDataObject::DATA_OBJECT()));
00069 
00070   vtkIdType cellId, numCells;
00071   int subId;
00072   vtkCellData *inCD;
00073   vtkPointData *outPD;
00074   vtkPoints *newPts;
00075   vtkCell *cell;
00076   double x[3], pcoords[3];
00077   double *weights;
00078 
00079   inCD=input->GetCellData();
00080   outPD=output->GetPointData();
00081 
00082   if ( (numCells = input->GetNumberOfCells()) < 1 )
00083     {
00084     vtkWarningMacro(<<"No cells to generate center points for");
00085     return 1;
00086     }
00087 
00088   newPts = vtkPoints::New();
00089   newPts->SetNumberOfPoints(numCells);
00090   weights = new double [input->GetMaxCellSize()];
00091 
00092   int abort=0;
00093   vtkIdType progressInterval = numCells/10 + 1;
00094   int hasEmptyCells = 0;
00095   for (cellId=0; cellId < numCells && !abort; cellId++)
00096     {
00097     if ( ! (cellId % progressInterval) ) 
00098       {
00099       vtkDebugMacro(<<"Processing #" << cellId);
00100       this->UpdateProgress (0.5*cellId/numCells);
00101       abort = this->GetAbortExecute();
00102       }
00103 
00104     cell = input->GetCell(cellId);
00105     if (cell->GetCellType() != VTK_EMPTY_CELL)
00106       {
00107         // fix for VTK_CONVEX_POINT_SET cells
00108         if (cell->GetCellType() == VTK_CONVEX_POINT_SET )
00109         {
00110           x[0] = x[1] = x[2] = 0;
00111           vtkPoints* aPoints = cell->GetPoints();
00112           int aNbPoints = aPoints->GetNumberOfPoints();
00113           for( int i = 0; i < aNbPoints; i++ )
00114           {
00115             double aCoord[3];
00116             aPoints->GetPoint( i, aCoord );
00117             x[0] += aCoord[0];
00118             x[1] += aCoord[1];
00119             x[2] += aCoord[2];
00120           }
00121           x[0] /= aNbPoints;
00122           x[1] /= aNbPoints;
00123           x[2] /= aNbPoints;
00124         }
00125         else
00126         {
00127           subId = cell->GetParametricCenter(pcoords);
00128           cell->EvaluateLocation(subId, pcoords, x, weights);
00129         }
00130         newPts->SetPoint(cellId,x);
00131       }
00132     else
00133       {
00134       hasEmptyCells = 1;
00135       }
00136     }
00137 
00138   if ( this->VertexCells )
00139     {
00140     vtkIdType pts[1];
00141     vtkCellData *outCD=output->GetCellData();
00142     vtkCellArray *verts = vtkCellArray::New();
00143     verts->Allocate(verts->EstimateSize(1,numCells),1);
00144 
00145     for (cellId=0; cellId < numCells && !abort; cellId++)
00146       {
00147       if ( ! (cellId % progressInterval) ) 
00148         {
00149         vtkDebugMacro(<<"Processing #" << cellId);
00150         this->UpdateProgress (0.5+0.5*cellId/numCells);
00151         abort = this->GetAbortExecute();
00152         }
00153 
00154       cell = input->GetCell(cellId);
00155       if (cell->GetCellType() != VTK_EMPTY_CELL)
00156         {
00157         pts[0] = cellId;
00158         verts->InsertNextCell(1,pts);
00159         }
00160       }
00161 
00162     output->SetVerts(verts);
00163     verts->Delete();
00164     if (!hasEmptyCells)
00165       {
00166       outCD->PassData(inCD); //only if verts are generated
00167       }
00168     }
00169 
00170   // clean up and update output
00171   output->SetPoints(newPts);
00172   newPts->Delete();
00173 
00174   if (!hasEmptyCells)
00175     {
00176     outPD->PassData(inCD); //because number of points = number of cells
00177     }
00178   if (weights)
00179     {
00180     delete [] weights;
00181     }
00182 
00183   return 1;
00184 }