1 // Copyright (C) 2007-2016 CEA/DEN, EDF R&D, OPEN CASCADE
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "SMESH_FaceOrientationFilter.h"
21 #include "SMESH_ActorUtils.h"
23 #include "SUIT_Session.h"
24 #include "SUIT_ResourceMgr.h"
26 #include <VTKViewer_CellCenters.h>
28 #include <vtkCellData.h>
29 #include <vtkDataSet.h>
30 #include <vtkPolyData.h>
31 #include <vtkObjectFactory.h>
32 #include <vtkInformation.h>
33 #include <vtkInformationVector.h>
35 #include <vtkFloatArray.h>
36 #include <vtkCellArray.h>
37 #include <vtkMaskPoints.h>
38 #include <vtkGlyph3D.h>
39 #include <vtkGlyphSource2D.h>
43 #define PI 3.14159265359
45 vtkStandardNewMacro(SMESH_FaceOrientationFilter);
48 * \class SMESH_FaceOrientationFilter
49 * Passive filter take a polydata as input and create a dataset as output.
52 SMESH_FaceOrientationFilter::SMESH_FaceOrientationFilter()
54 SUIT_ResourceMgr* mgr = SUIT_Session::session()->resourceMgr();
55 myOrientationScale = mgr->doubleValue( "SMESH", "orientation_scale", 0.1 );
56 my3dVectors = mgr->booleanValue( "SMESH", "orientation_3d_vectors", false );
58 myArrowPolyData = CreateArrowPolyData();
60 myFacePolyData = vtkPolyData::New();
62 myFaceCenters = VTKViewer_CellCenters::New();
63 myFaceCenters->SetInputData(myFacePolyData);
65 myFaceMaskPoints = vtkMaskPoints::New();
66 myFaceMaskPoints->SetInputConnection(myFaceCenters->GetOutputPort());
67 myFaceMaskPoints->SetOnRatio(1);
69 myGlyphSource = vtkGlyphSource2D::New();
70 myGlyphSource->SetGlyphTypeToThickArrow();
71 myGlyphSource->SetFilled(0);
72 myGlyphSource->SetCenter(0.5, 0.0, 0.0);
74 myBaseGlyph = vtkGlyph3D::New();
75 myBaseGlyph->SetInputConnection(myFaceMaskPoints->GetOutputPort());
76 myBaseGlyph->SetVectorModeToUseVector();
77 myBaseGlyph->SetScaleModeToDataScalingOff();
78 myBaseGlyph->SetColorModeToColorByScalar();
80 myBaseGlyph->SetSourceData(myArrowPolyData);
82 myBaseGlyph->SetSourceConnection(myGlyphSource->GetOutputPort());
85 SMESH_FaceOrientationFilter::~SMESH_FaceOrientationFilter()
87 myArrowPolyData->Delete();
88 myFacePolyData->Delete();
89 myFaceCenters->Delete();
90 myFaceMaskPoints->Delete();
91 myGlyphSource->Delete();
92 myBaseGlyph->Delete();
95 void SMESH_FaceOrientationFilter::SetOrientationScale( double theScale )
97 myOrientationScale = theScale;
101 void SMESH_FaceOrientationFilter::Set3dVectors( bool theState )
103 my3dVectors = theState;
105 myBaseGlyph->SetSourceData(myArrowPolyData);
107 myBaseGlyph->SetSourceConnection(myGlyphSource->GetOutputPort());
111 vtkPolyData* SMESH_FaceOrientationFilter::CreateArrowPolyData()
113 vtkPoints* points = vtkPoints::New();
114 vtkCellArray* polys = vtkCellArray::New();
121 float angle = 2. * PI / n;
126 float p0[3] = { 0.0, 0.0, 0.0 };
127 float p1[3] = { l1, 0.0, 0.0 };
128 float p2[3] = { l2, 0.0, 0.0 };
130 points->InsertPoint( 0, p0 );
131 points->InsertPoint( 1, p1 );
132 points->InsertPoint( 2, p2 );
135 for( int i = 0; i < n; i++ )
138 p[1] = r1 * sin( i * angle );
139 p[2] = r1 * cos( i * angle );
140 points->InsertPoint( i + 3, p );
143 points->InsertPoint( i + 3 + n, p );
146 // insert the last cells outside a loop
151 polys->InsertNextCell( 3, c3 );
155 c4[2] = 3 + 2 * n - 1;
157 polys->InsertNextCell( 4, c4 );
159 for( int i = 0; i < n - 1; i++ )
164 polys->InsertNextCell( 3, c3 );
170 polys->InsertNextCell( 4, c4 );
174 for( int i = 0; i < n; i++ )
177 p[1] = r2 * sin( i * angle );
178 p[2] = r2 * cos( i * angle );
179 points->InsertPoint( i + 3 + 2 * n, p );
182 // insert the last cells outside a loop
186 c3[2] = 3 + 2 * n + n - 1;
187 polys->InsertNextCell( 3, c3 );
190 polys->InsertNextCell( 3, c3 );
192 for( int i = 0; i < n - 1; i++ )
195 c3[1] = 3 + i + 2 * n;
196 c3[2] = 3 + i + 2 * n + 1;
197 polys->InsertNextCell( 3, c3 );
200 polys->InsertNextCell( 3, c3 );
203 vtkPolyData* aPolyData = vtkPolyData::New();
205 aPolyData->SetPoints(points);
208 aPolyData->SetPolys(polys);
214 void GetFaceParams( vtkCell* theFace, double theNormal[3], double& theSize )
216 vtkPoints* aPoints = theFace->GetPoints();
218 // here we get first 3 points from the face and calculate the normal as a cross-product of vectors
219 double x0 = aPoints->GetPoint(0)[0], y0 = aPoints->GetPoint(0)[1], z0 = aPoints->GetPoint(0)[2];
220 double x1 = aPoints->GetPoint(1)[0], y1 = aPoints->GetPoint(1)[1], z1 = aPoints->GetPoint(1)[2];
221 double x2 = aPoints->GetPoint(2)[0], y2 = aPoints->GetPoint(2)[1], z2 = aPoints->GetPoint(2)[2];
223 theNormal[0] = ( y1 - y0 ) * ( z2 - z0 ) - ( z1 - z0 ) * ( y2 - y0 );
224 theNormal[1] = ( z1 - z0 ) * ( x2 - x0 ) - ( x1 - x0 ) * ( z2 - z0 );
225 theNormal[2] = ( x1 - x0 ) * ( y2 - y0 ) - ( y1 - y0 ) * ( x2 - x0 );
227 double* aBounds = theFace->GetBounds();
228 theSize = pow( pow( aBounds[1] - aBounds[0], 2 ) +
229 pow( aBounds[3] - aBounds[2], 2 ) +
230 pow( aBounds[5] - aBounds[4], 2 ), 0.5 );
234 * Execute method. Output calculation.
236 int SMESH_FaceOrientationFilter::RequestData(
237 vtkInformation *request,
238 vtkInformationVector **inputVector,
239 vtkInformationVector *outputVector)
241 // get the info objects
242 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
243 vtkInformation *outInfo = outputVector->GetInformationObject(0);
245 // get the input and ouptut
246 vtkDataSet *input = vtkDataSet::SafeDownCast(
247 inInfo->Get(vtkDataObject::DATA_OBJECT()));
248 vtkPolyData *output = vtkPolyData::SafeDownCast(
249 outInfo->Get(vtkDataObject::DATA_OBJECT()));
251 myFacePolyData->Initialize();
252 myFacePolyData->ShallowCopy(input);
254 vtkCellArray* aFaces = vtkCellArray::New();
256 vtkFloatArray* aVectors = vtkFloatArray::New();
257 aVectors->SetNumberOfComponents(3);
260 double anAverageSize = 0;
262 vtkIdList* aNeighborIds = vtkIdList::New();
264 for(int aCellId = 0, aNbCells = input->GetNumberOfCells(); aCellId < aNbCells; aCellId++)
266 vtkCell* aCell = input->GetCell(aCellId);
268 if( aCell->GetNumberOfFaces() == 0 && aCell->GetNumberOfPoints() > 2 ) // cell is a face
270 double aSize, aNormal[3];
271 GetFaceParams( aCell, aNormal, aSize );
273 aFaces->InsertNextCell(aCell);
274 aVectors->InsertNextTuple(aNormal);
277 anAverageSize += aSize;
282 for(int aFaceId = 0, aNbFaces = aCell->GetNumberOfFaces(); aFaceId < aNbFaces; aFaceId++)
284 vtkCell* aFace = aCell->GetFace(aFaceId);
286 input->GetCellNeighbors( aCellId, aFace->PointIds, aNeighborIds );
287 if( aNeighborIds->GetNumberOfIds() > 0 )
290 double aSize, aNormal[3];
291 GetFaceParams( aFace, aNormal, aSize );
293 aFaces->InsertNextCell(aFace->GetPointIds());
294 aVectors->InsertNextTuple(aNormal);
297 anAverageSize += aSize;
300 aNeighborIds->Delete();
302 myFacePolyData->SetPolys(aFaces);
305 myFacePolyData->GetCellData()->SetScalars(0);
306 myFacePolyData->GetCellData()->SetVectors(aVectors);
309 if( anAllFaces == 0 )
312 anAverageSize /= anAllFaces;
313 anAverageSize *= myOrientationScale;
315 myBaseGlyph->SetScaleFactor( anAverageSize );
316 myBaseGlyph->Update();
318 output->ShallowCopy( myBaseGlyph->GetOutput() );
323 int SMESH_FaceOrientationFilter::FillInputPortInformation(int, vtkInformation *info)
325 info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkDataSet");