Salome HOME
0aa717bd24ea9ecf2387be04e4d4b8dd5fc61c52
[modules/gui.git] / src / VTKViewer / VTKViewer_CellCenters.cxx
1 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "VTKViewer_CellCenters.h"
24
25 #include <vtkCell.h>
26 #include <vtkCellData.h>
27 #include <vtkDataSet.h>
28 #include <vtkInformation.h>
29 #include <vtkInformationVector.h>
30 #include <vtkObjectFactory.h>
31 #include <vtkPointData.h>
32 #include <vtkPoints.h>
33 #include <vtkPolyData.h>
34 #include <vtkCellArray.h>
35
36 vtkCxxRevisionMacro(VTKViewer_CellCenters, "$Revision$");
37 vtkStandardNewMacro(VTKViewer_CellCenters);
38
39 /*!
40  * Class       : VTKViewer_CellCenters
41  * Description : Filter computing geometrical centers of given cells
42  *               (differs from native vtk filter by small fix for VTK_CONVEX_POINT_SET cells)
43  */
44
45 /*!
46   Constructor
47 */
48 VTKViewer_CellCenters::VTKViewer_CellCenters()
49 {
50 }
51
52 /*!
53   Redefined main method
54 */
55 int VTKViewer_CellCenters::RequestData(
56   vtkInformation *vtkNotUsed(request),
57   vtkInformationVector **inputVector,
58   vtkInformationVector *outputVector)
59 {
60   // get the info objects
61   vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
62   vtkInformation *outInfo = outputVector->GetInformationObject(0);
63
64   // get the input and ouptut
65   vtkDataSet *input = vtkDataSet::SafeDownCast(
66     inInfo->Get(vtkDataObject::DATA_OBJECT()));
67   vtkPolyData *output = vtkPolyData::SafeDownCast(
68     outInfo->Get(vtkDataObject::DATA_OBJECT()));
69
70   vtkIdType cellId, numCells;
71   int subId;
72   vtkCellData *inCD;
73   vtkPointData *outPD;
74   vtkPoints *newPts;
75   vtkCell *cell;
76   double x[3], pcoords[3];
77   double *weights;
78
79   inCD=input->GetCellData();
80   outPD=output->GetPointData();
81
82   if ( (numCells = input->GetNumberOfCells()) < 1 )
83     {
84     vtkWarningMacro(<<"No cells to generate center points for");
85     return 1;
86     }
87
88   newPts = vtkPoints::New();
89   newPts->SetNumberOfPoints(numCells);
90   weights = new double [input->GetMaxCellSize()];
91
92   int abort=0;
93   vtkIdType progressInterval = numCells/10 + 1;
94   int hasEmptyCells = 0;
95   for (cellId=0; cellId < numCells && !abort; cellId++)
96     {
97     if ( ! (cellId % progressInterval) ) 
98       {
99       vtkDebugMacro(<<"Processing #" << cellId);
100       this->UpdateProgress (0.5*cellId/numCells);
101       abort = this->GetAbortExecute();
102       }
103
104     cell = input->GetCell(cellId);
105     if (cell->GetCellType() != VTK_EMPTY_CELL)
106       {
107         // fix for VTK_CONVEX_POINT_SET cells
108         if (cell->GetCellType() == VTK_CONVEX_POINT_SET )
109         {
110           x[0] = x[1] = x[2] = 0;
111           vtkPoints* aPoints = cell->GetPoints();
112           int aNbPoints = aPoints->GetNumberOfPoints();
113           for( int i = 0; i < aNbPoints; i++ )
114           {
115             double aCoord[3];
116             aPoints->GetPoint( i, aCoord );
117             x[0] += aCoord[0];
118             x[1] += aCoord[1];
119             x[2] += aCoord[2];
120           }
121           x[0] /= aNbPoints;
122           x[1] /= aNbPoints;
123           x[2] /= aNbPoints;
124         }
125         else
126         {
127           subId = cell->GetParametricCenter(pcoords);
128           cell->EvaluateLocation(subId, pcoords, x, weights);
129         }
130         newPts->SetPoint(cellId,x);
131       }
132     else
133       {
134       hasEmptyCells = 1;
135       }
136     }
137
138   if ( this->VertexCells )
139     {
140     vtkIdType pts[1];
141     vtkCellData *outCD=output->GetCellData();
142     vtkCellArray *verts = vtkCellArray::New();
143     verts->Allocate(verts->EstimateSize(1,numCells),1);
144
145     for (cellId=0; cellId < numCells && !abort; cellId++)
146       {
147       if ( ! (cellId % progressInterval) ) 
148         {
149         vtkDebugMacro(<<"Processing #" << cellId);
150         this->UpdateProgress (0.5+0.5*cellId/numCells);
151         abort = this->GetAbortExecute();
152         }
153
154       cell = input->GetCell(cellId);
155       if (cell->GetCellType() != VTK_EMPTY_CELL)
156         {
157         pts[0] = cellId;
158         verts->InsertNextCell(1,pts);
159         }
160       }
161
162     output->SetVerts(verts);
163     verts->Delete();
164     if (!hasEmptyCells)
165       {
166       outCD->PassData(inCD); //only if verts are generated
167       }
168     }
169
170   // clean up and update output
171   output->SetPoints(newPts);
172   newPts->Delete();
173
174   if (!hasEmptyCells)
175     {
176     outPD->PassData(inCD); //because number of points = number of cells
177     }
178   if (weights)
179     {
180     delete [] weights;
181     }
182
183   return 1;
184 }