Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/geom.git] / src / OCC2VTK / GEOM_ShadingFace.cxx
1 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "GEOM_ShadingFace.h" 
21  
22 #include <vtkObjectFactory.h> 
23  
24 #include <vtkPoints.h> 
25 #include <vtkCellArray.h> 
26
27 #include <vtkPolyDataMapper.h>  
28 #include <vtkPolyData.h>  
29
30 #include <BRep_Tool.hxx>
31 #include <Poly_Triangulation.hxx>
32  
33
34 vtkStandardNewMacro(GEOM_ShadingFace);
35  
36 GEOM_ShadingFace::GEOM_ShadingFace() 
37
38
39  
40 GEOM_ShadingFace::~GEOM_ShadingFace() 
41
42
43  
44 void
45 GEOM_ShadingFace:: 
46 Execute()
47 {
48   vtkPolyData* aPolyData = GetOutput();
49   aPolyData->Allocate();
50   vtkPoints* aPts = vtkPoints::New();
51   aPolyData->SetPoints(aPts);
52   aPts->Delete();
53
54   TFaceSet::Iterator anIter(myFaceSet);
55   for(; anIter.More(); anIter.Next()){
56     const TopoDS_Face& aFace = anIter.Value();
57     OCC2VTK(aFace,aPolyData,aPts);
58   }
59 }
60
61 void  
62 GEOM_ShadingFace:: 
63 OCC2VTK(const TopoDS_Face& theFace,  
64         vtkPolyData* thePolyData, 
65         vtkPoints* thePts) 
66 {
67   TopLoc_Location aLoc;
68   Handle(Poly_Triangulation) aPoly = BRep_Tool::Triangulation(theFace,aLoc);
69   if(aPoly.IsNull()) 
70     return;
71   else{
72     gp_Trsf myTransf;
73     Standard_Boolean identity = true;
74     if(!aLoc.IsIdentity()){
75       identity = false;
76       myTransf = aLoc.Transformation();
77     }           
78       
79     Standard_Integer i; 
80     int aNbOfNodes = thePts->GetNumberOfPoints();
81     const TColgp_Array1OfPnt& Nodes = aPoly->Nodes();
82     Standard_Integer nbNodesInFace = aPoly->NbNodes(); 
83     for(i = 1; i <= nbNodesInFace; i++) {
84       gp_Pnt P = Nodes(i);
85       if(!identity) 
86         P.Transform(myTransf);
87       thePts->InsertNextPoint(P.X(),P.Y(),P.Z());
88     }
89
90     const Poly_Array1OfTriangle& Triangles = aPoly->Triangles();
91     Standard_Integer nbTriInFace = aPoly->NbTriangles();
92     for(i = 1; i <= nbTriInFace; i++){
93       // Get the triangle
94       Standard_Integer N1,N2,N3;
95       Triangles(i).Get(N1,N2,N3);
96       N1 += aNbOfNodes - 1;
97       N2 += aNbOfNodes - 1;
98       N3 += aNbOfNodes - 1;
99       vtkIdType anIds[3] = {N1, N2, N3};
100       thePolyData->InsertNextCell(VTK_TRIANGLE,3,anIds);
101     }
102   } 
103 }