Salome HOME
Remarks for issue 17291 EDF 591 SMESH : Visualization of the orientation of the norma...
[modules/smesh.git] / src / OBJECT / SMESH_FaceOrientationFilter.cxx
1 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
3 // 
4 //  This library is free software; you can redistribute it and/or 
5 //  modify it under the terms of the GNU Lesser General Public 
6 //  License as published by the Free Software Foundation; either 
7 //  version 2.1 of the License. 
8 // 
9 //  This library is distributed in the hope that it will be useful, 
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 //  Lesser General Public License for more details. 
13 // 
14 //  You should have received a copy of the GNU Lesser General Public 
15 //  License along with this library; if not, write to the Free Software 
16 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
17 // 
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20
21 #include "SMESH_FaceOrientationFilter.h"
22 #include "SMESH_ActorUtils.h"
23
24 #include "SUIT_Session.h"
25 #include "SUIT_ResourceMgr.h"
26
27 #include <vtkCellData.h>
28 #include <vtkDataSet.h>
29 #include <vtkPolyData.h>
30 #include <vtkObjectFactory.h>
31 #include <vtkInformation.h>
32 #include <vtkInformationVector.h>
33
34 #include <vtkFloatArray.h>
35 #include <vtkCellArray.h>
36 #include <vtkMaskPoints.h>
37 #include <vtkCellCenters.h>
38 #include <vtkGlyph3D.h>
39 #include <vtkGlyphSource2D.h>
40
41 #include <QColor>
42
43 #define PI   3.14159265359
44
45 vtkCxxRevisionMacro(SMESH_FaceOrientationFilter, "$Revision$");
46 vtkStandardNewMacro(SMESH_FaceOrientationFilter);
47
48 /*!
49  * \class SMESH_FaceOrientationFilter
50  * Passive filter take a polydata as input and create a dataset as output.
51  */
52
53 SMESH_FaceOrientationFilter::SMESH_FaceOrientationFilter()
54 {
55   SUIT_ResourceMgr* mgr = SUIT_Session::session()->resourceMgr();
56   myOrientationScale = mgr->doubleValue( "SMESH", "orientation_scale", 0.1 );
57   my3dVectors = mgr->booleanValue( "SMESH", "orientation_3d_vectors", false );
58
59   myArrowPolyData = CreateArrowPolyData();
60
61   myFacePolyData = vtkPolyData::New();
62
63   myFaceCenters = vtkCellCenters::New();
64   myFaceCenters->SetInput(myFacePolyData);
65
66   myFaceMaskPoints = vtkMaskPoints::New();
67   myFaceMaskPoints->SetInput(myFaceCenters->GetOutput());
68   myFaceMaskPoints->SetOnRatio(1);
69
70   myGlyphSource = vtkGlyphSource2D::New();
71   myGlyphSource->SetGlyphTypeToThickArrow();
72   myGlyphSource->SetFilled(0);
73   myGlyphSource->SetCenter(0.5, 0.0, 0.0);
74
75   myBaseGlyph = vtkGlyph3D::New();
76   myBaseGlyph->SetInput(myFaceMaskPoints->GetOutput());
77   myBaseGlyph->SetVectorModeToUseVector();
78   myBaseGlyph->SetScaleModeToDataScalingOff();
79   myBaseGlyph->SetColorModeToColorByScalar();
80   myBaseGlyph->SetSource(my3dVectors ? myArrowPolyData : myGlyphSource->GetOutput());
81 }
82
83 SMESH_FaceOrientationFilter::~SMESH_FaceOrientationFilter()
84 {
85   myArrowPolyData->Delete();
86   myFacePolyData->Delete();
87   myFaceCenters->Delete();
88   myFaceMaskPoints->Delete();
89   myGlyphSource->Delete();
90   myBaseGlyph->Delete();
91 }
92
93 vtkPolyData* SMESH_FaceOrientationFilter::CreateArrowPolyData()
94 {
95   vtkPoints* points = vtkPoints::New();
96   vtkCellArray* polys = vtkCellArray::New();
97
98   float l1 = 0.8;
99   float l2 = 1.0;
100   int n = 16;
101   float r1 = 0.04;
102   float r2 = 0.08;
103   float angle = 2. * PI / n;
104   float p[3];
105   vtkIdType c3[3];
106   vtkIdType c4[4];
107
108   float p0[3] = { 0.0, 0.0, 0.0 };
109   float p1[3] = {  l1, 0.0, 0.0 };
110   float p2[3] = {  l2, 0.0, 0.0 };
111
112   points->InsertPoint( 0, p0 );
113   points->InsertPoint( 1, p1 );
114   points->InsertPoint( 2, p2 );
115
116   // shaft
117   for( int i = 0; i < n; i++ )
118   {
119     p[0] = 0;
120     p[1] = r1 * sin( i * angle );
121     p[2] = r1 * cos( i * angle );
122     points->InsertPoint( i + 3, p );
123
124     p[0] = l1;
125     points->InsertPoint( i + 3 + n, p );
126   }
127
128   // insert the last cells outside a loop
129   {
130     c3[0] = 0;
131     c3[1] = 3;
132     c3[2] = 3 + n - 1;
133     polys->InsertNextCell( 3, c3 );
134
135     c4[0] = 3;
136     c4[1] = 3 + n - 1;
137     c4[2] = 3 + 2 * n - 1;
138     c4[3] = 3 + n;
139     polys->InsertNextCell( 4, c4 );
140   }
141   for( int i = 0; i < n - 1; i++ )
142   {
143     c3[0] = 0;
144     c3[1] = i + 3;
145     c3[2] = i + 4;
146     polys->InsertNextCell( 3, c3 );
147
148     c4[0] = i + 3;
149     c4[1] = i + 4;
150     c4[2] = i + 4 + n;
151     c4[3] = i + 3 + n;
152     polys->InsertNextCell( 4, c4 );
153   }
154
155   // cone
156   for( int i = 0; i < n; i++ )
157   {
158     p[0] = l1;
159     p[1] = r2 * sin( i * angle );
160     p[2] = r2 * cos( i * angle );
161     points->InsertPoint( i + 3 + 2 * n, p );
162   }
163
164   // insert the last cells outside a loop
165   {
166     c3[0] = 1;
167     c3[1] = 3 + 2 * n;
168     c3[2] = 3 + 2 * n + n - 1;
169     polys->InsertNextCell( 3, c3 );
170
171     c3[0] = 2;
172     polys->InsertNextCell( 3, c3 );
173   }
174   for( int i = 0; i < n - 1; i++ )
175   {
176     c3[0] = 1;
177     c3[1] = 3 + i + 2 * n;
178     c3[2] = 3 + i + 2 * n + 1;
179     polys->InsertNextCell( 3, c3 );
180
181     c3[0] = 2;
182     polys->InsertNextCell( 3, c3 );
183   }
184
185   vtkPolyData* aPolyData = vtkPolyData::New();
186
187   aPolyData->SetPoints(points);
188   points->Delete();
189
190   aPolyData->SetPolys(polys);
191   polys->Delete();
192
193   return aPolyData;
194 }
195
196 void GetFaceParams( vtkCell* theFace, double theNormal[3], double& theSize ) 
197 {
198   vtkPoints* aPoints = theFace->GetPoints();
199
200   // here we get first 3 points from the face and calculate the normal as a cross-product of vectors
201   double x0 = aPoints->GetPoint(0)[0], y0 = aPoints->GetPoint(0)[1], z0 = aPoints->GetPoint(0)[2];
202   double x1 = aPoints->GetPoint(1)[0], y1 = aPoints->GetPoint(1)[1], z1 = aPoints->GetPoint(1)[2];
203   double x2 = aPoints->GetPoint(2)[0], y2 = aPoints->GetPoint(2)[1], z2 = aPoints->GetPoint(2)[2];
204
205   theNormal[0] = ( y1 - y0 ) * ( z2 - z0 ) - ( z1 - z0 ) * ( y2 - y0 );
206   theNormal[1] = ( z1 - z0 ) * ( x2 - x0 ) - ( x1 - x0 ) * ( z2 - z0 );
207   theNormal[2] = ( x1 - x0 ) * ( y2 - y0 ) - ( y1 - y0 ) * ( x2 - x0 );
208
209   double* aBounds = theFace->GetBounds();
210   theSize = pow( pow( aBounds[1] - aBounds[0], 2 ) +
211                  pow( aBounds[3] - aBounds[2], 2 ) +
212                  pow( aBounds[5] - aBounds[4], 2 ), 0.5 );
213 }
214
215 /*!
216  * Execute method. Output calculation.
217  */
218 int SMESH_FaceOrientationFilter::RequestData(
219   vtkInformation *request,
220   vtkInformationVector **inputVector,
221   vtkInformationVector *outputVector)
222 {
223   // get the info objects
224   vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
225   vtkInformation *outInfo = outputVector->GetInformationObject(0);
226
227   // get the input and ouptut
228   vtkDataSet *input = vtkDataSet::SafeDownCast(
229     inInfo->Get(vtkDataObject::DATA_OBJECT()));
230   vtkPolyData *output = vtkPolyData::SafeDownCast(
231     outInfo->Get(vtkDataObject::DATA_OBJECT()));
232
233   myFacePolyData->Initialize();
234   myFacePolyData->ShallowCopy(input);
235
236   vtkCellArray* aFaces = vtkCellArray::New();
237
238   vtkFloatArray* aVectors = vtkFloatArray::New();
239   aVectors->SetNumberOfComponents(3);
240
241   int anAllFaces = 0;
242   double anAverageSize = 0;
243
244   vtkIdList* aNeighborIds = vtkIdList::New();
245
246   for(int aCellId = 0, aNbCells = input->GetNumberOfCells(); aCellId < aNbCells; aCellId++)
247   {
248     vtkCell* aCell = input->GetCell(aCellId);
249
250     if( aCell->GetNumberOfFaces() == 0 && aCell->GetNumberOfPoints() > 2 ) // cell is a face
251     {
252       double aSize, aNormal[3];
253       GetFaceParams( aCell, aNormal, aSize );
254
255       aFaces->InsertNextCell(aCell);
256       aVectors->InsertNextTuple(aNormal);
257
258       anAllFaces++;
259       anAverageSize += aSize;
260
261       continue;
262     }
263
264     for(int aFaceId = 0, aNbFaces = aCell->GetNumberOfFaces(); aFaceId < aNbFaces; aFaceId++)
265     {
266       vtkCell* aFace = aCell->GetFace(aFaceId);
267
268       input->GetCellNeighbors( aCellId, aFace->PointIds, aNeighborIds );
269       if( aNeighborIds->GetNumberOfIds() > 0 )
270         continue;
271
272       double aSize, aNormal[3];
273       GetFaceParams( aFace, aNormal, aSize );
274
275       aFaces->InsertNextCell(aFace->GetPointIds());
276       aVectors->InsertNextTuple(aNormal);
277
278       anAllFaces++;
279       anAverageSize += aSize;
280     }
281   }
282   aNeighborIds->Delete();
283
284   myFacePolyData->SetPolys(aFaces);
285   aFaces->Delete();
286
287   myFacePolyData->GetCellData()->SetScalars(0);
288   myFacePolyData->GetCellData()->SetVectors(aVectors);
289   aVectors->Delete();
290
291   if( anAllFaces == 0 )
292     return 0;
293
294   anAverageSize /= anAllFaces;
295   anAverageSize *= myOrientationScale;
296
297   myBaseGlyph->SetScaleFactor( anAverageSize );
298   myBaseGlyph->Update();
299
300   output->ShallowCopy( myBaseGlyph->GetOutput() );
301
302   return 1;
303 }
304
305 int SMESH_FaceOrientationFilter::FillInputPortInformation(int, vtkInformation *info)
306 {
307   info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkDataSet");
308   return 1;
309 }