1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 #include "SMESH_FaceOrientationFilter.h"
23 #include "SMESH_ActorUtils.h"
25 #include "SUIT_Session.h"
26 #include "SUIT_ResourceMgr.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 <vtkCellCenters.h>
39 #include <vtkGlyph3D.h>
40 #include <vtkGlyphSource2D.h>
44 #define PI 3.14159265359
46 vtkCxxRevisionMacro(SMESH_FaceOrientationFilter, "$Revision$");
47 vtkStandardNewMacro(SMESH_FaceOrientationFilter);
50 * \class SMESH_FaceOrientationFilter
51 * Passive filter take a polydata as input and create a dataset as output.
54 SMESH_FaceOrientationFilter::SMESH_FaceOrientationFilter()
56 SUIT_ResourceMgr* mgr = SUIT_Session::session()->resourceMgr();
57 myOrientationScale = mgr->doubleValue( "SMESH", "orientation_scale", 0.1 );
58 my3dVectors = mgr->booleanValue( "SMESH", "orientation_3d_vectors", false );
60 myArrowPolyData = CreateArrowPolyData();
62 myFacePolyData = vtkPolyData::New();
64 myFaceCenters = vtkCellCenters::New();
65 myFaceCenters->SetInput(myFacePolyData);
67 myFaceMaskPoints = vtkMaskPoints::New();
68 myFaceMaskPoints->SetInput(myFaceCenters->GetOutput());
69 myFaceMaskPoints->SetOnRatio(1);
71 myGlyphSource = vtkGlyphSource2D::New();
72 myGlyphSource->SetGlyphTypeToThickArrow();
73 myGlyphSource->SetFilled(0);
74 myGlyphSource->SetCenter(0.5, 0.0, 0.0);
76 myBaseGlyph = vtkGlyph3D::New();
77 myBaseGlyph->SetInput(myFaceMaskPoints->GetOutput());
78 myBaseGlyph->SetVectorModeToUseVector();
79 myBaseGlyph->SetScaleModeToDataScalingOff();
80 myBaseGlyph->SetColorModeToColorByScalar();
81 myBaseGlyph->SetSource(my3dVectors ? myArrowPolyData : myGlyphSource->GetOutput());
84 SMESH_FaceOrientationFilter::~SMESH_FaceOrientationFilter()
86 myArrowPolyData->Delete();
87 myFacePolyData->Delete();
88 myFaceCenters->Delete();
89 myFaceMaskPoints->Delete();
90 myGlyphSource->Delete();
91 myBaseGlyph->Delete();
94 vtkPolyData* SMESH_FaceOrientationFilter::CreateArrowPolyData()
96 vtkPoints* points = vtkPoints::New();
97 vtkCellArray* polys = vtkCellArray::New();
104 float angle = 2. * PI / n;
109 float p0[3] = { 0.0, 0.0, 0.0 };
110 float p1[3] = { l1, 0.0, 0.0 };
111 float p2[3] = { l2, 0.0, 0.0 };
113 points->InsertPoint( 0, p0 );
114 points->InsertPoint( 1, p1 );
115 points->InsertPoint( 2, p2 );
118 for( int i = 0; i < n; i++ )
121 p[1] = r1 * sin( i * angle );
122 p[2] = r1 * cos( i * angle );
123 points->InsertPoint( i + 3, p );
126 points->InsertPoint( i + 3 + n, p );
129 // insert the last cells outside a loop
134 polys->InsertNextCell( 3, c3 );
138 c4[2] = 3 + 2 * n - 1;
140 polys->InsertNextCell( 4, c4 );
142 for( int i = 0; i < n - 1; i++ )
147 polys->InsertNextCell( 3, c3 );
153 polys->InsertNextCell( 4, c4 );
157 for( int i = 0; i < n; i++ )
160 p[1] = r2 * sin( i * angle );
161 p[2] = r2 * cos( i * angle );
162 points->InsertPoint( i + 3 + 2 * n, p );
165 // insert the last cells outside a loop
169 c3[2] = 3 + 2 * n + n - 1;
170 polys->InsertNextCell( 3, c3 );
173 polys->InsertNextCell( 3, c3 );
175 for( int i = 0; i < n - 1; i++ )
178 c3[1] = 3 + i + 2 * n;
179 c3[2] = 3 + i + 2 * n + 1;
180 polys->InsertNextCell( 3, c3 );
183 polys->InsertNextCell( 3, c3 );
186 vtkPolyData* aPolyData = vtkPolyData::New();
188 aPolyData->SetPoints(points);
191 aPolyData->SetPolys(polys);
197 void GetFaceParams( vtkCell* theFace, double theNormal[3], double& theSize )
199 vtkPoints* aPoints = theFace->GetPoints();
201 // here we get first 3 points from the face and calculate the normal as a cross-product of vectors
202 double x0 = aPoints->GetPoint(0)[0], y0 = aPoints->GetPoint(0)[1], z0 = aPoints->GetPoint(0)[2];
203 double x1 = aPoints->GetPoint(1)[0], y1 = aPoints->GetPoint(1)[1], z1 = aPoints->GetPoint(1)[2];
204 double x2 = aPoints->GetPoint(2)[0], y2 = aPoints->GetPoint(2)[1], z2 = aPoints->GetPoint(2)[2];
206 theNormal[0] = ( y1 - y0 ) * ( z2 - z0 ) - ( z1 - z0 ) * ( y2 - y0 );
207 theNormal[1] = ( z1 - z0 ) * ( x2 - x0 ) - ( x1 - x0 ) * ( z2 - z0 );
208 theNormal[2] = ( x1 - x0 ) * ( y2 - y0 ) - ( y1 - y0 ) * ( x2 - x0 );
210 double* aBounds = theFace->GetBounds();
211 theSize = pow( pow( aBounds[1] - aBounds[0], 2 ) +
212 pow( aBounds[3] - aBounds[2], 2 ) +
213 pow( aBounds[5] - aBounds[4], 2 ), 0.5 );
217 * Execute method. Output calculation.
219 int SMESH_FaceOrientationFilter::RequestData(
220 vtkInformation *request,
221 vtkInformationVector **inputVector,
222 vtkInformationVector *outputVector)
224 // get the info objects
225 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
226 vtkInformation *outInfo = outputVector->GetInformationObject(0);
228 // get the input and ouptut
229 vtkDataSet *input = vtkDataSet::SafeDownCast(
230 inInfo->Get(vtkDataObject::DATA_OBJECT()));
231 vtkPolyData *output = vtkPolyData::SafeDownCast(
232 outInfo->Get(vtkDataObject::DATA_OBJECT()));
234 myFacePolyData->Initialize();
235 myFacePolyData->ShallowCopy(input);
237 vtkCellArray* aFaces = vtkCellArray::New();
239 vtkFloatArray* aVectors = vtkFloatArray::New();
240 aVectors->SetNumberOfComponents(3);
243 double anAverageSize = 0;
245 vtkIdList* aNeighborIds = vtkIdList::New();
247 for(int aCellId = 0, aNbCells = input->GetNumberOfCells(); aCellId < aNbCells; aCellId++)
249 vtkCell* aCell = input->GetCell(aCellId);
251 if( aCell->GetNumberOfFaces() == 0 && aCell->GetNumberOfPoints() > 2 ) // cell is a face
253 double aSize, aNormal[3];
254 GetFaceParams( aCell, aNormal, aSize );
256 aFaces->InsertNextCell(aCell);
257 aVectors->InsertNextTuple(aNormal);
260 anAverageSize += aSize;
265 for(int aFaceId = 0, aNbFaces = aCell->GetNumberOfFaces(); aFaceId < aNbFaces; aFaceId++)
267 vtkCell* aFace = aCell->GetFace(aFaceId);
269 input->GetCellNeighbors( aCellId, aFace->PointIds, aNeighborIds );
270 if( aNeighborIds->GetNumberOfIds() > 0 )
273 double aSize, aNormal[3];
274 GetFaceParams( aFace, aNormal, aSize );
276 aFaces->InsertNextCell(aFace->GetPointIds());
277 aVectors->InsertNextTuple(aNormal);
280 anAverageSize += aSize;
283 aNeighborIds->Delete();
285 myFacePolyData->SetPolys(aFaces);
288 myFacePolyData->GetCellData()->SetScalars(0);
289 myFacePolyData->GetCellData()->SetVectors(aVectors);
292 if( anAllFaces == 0 )
295 anAverageSize /= anAllFaces;
296 anAverageSize *= myOrientationScale;
298 myBaseGlyph->SetScaleFactor( anAverageSize );
299 myBaseGlyph->Update();
301 output->ShallowCopy( myBaseGlyph->GetOutput() );
306 int SMESH_FaceOrientationFilter::FillInputPortInformation(int, vtkInformation *info)
308 info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkDataSet");