Salome HOME
Merge remote branch 'origin/akl/22379'
[modules/geom.git] / src / OCC2VTK / GEOM_ShadingFace.cxx
1 // Copyright (C) 2007-2014  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, or (at your option) any later version.
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 #include "OCC2VTK_internal.h"
22  
23 #include <vtkObjectFactory.h> 
24  
25 #include <vtkPoints.h> 
26 #include <vtkCellArray.h> 
27
28 #include <vtkPolyDataMapper.h>  
29 #include <vtkPolyData.h>  
30 #include <vtkInformation.h>
31 #include <vtkInformationVector.h>
32
33 #include <BRep_Tool.hxx>
34 #include <Poly_Triangulation.hxx>
35
36
37 vtkStandardNewMacro(GEOM_ShadingFace);
38  
39 GEOM_ShadingFace::GEOM_ShadingFace() 
40
41   this->SetNumberOfInputPorts(0);
42
43  
44 GEOM_ShadingFace::~GEOM_ShadingFace() 
45
46
47  
48 int GEOM_ShadingFace::RequestData(vtkInformation *vtkNotUsed(request),
49                                   vtkInformationVector **vtkNotUsed(inputVector),
50                                   vtkInformationVector *outputVector)
51 {
52   vtkInformation *outInfo = outputVector->GetInformationObject(0);
53   vtkPolyData *aPolyData = vtkPolyData::SafeDownCast(
54     outInfo->Get(vtkDataObject::DATA_OBJECT()));
55
56   aPolyData->Allocate();
57   vtkPoints* aPts = vtkPoints::New();
58   aPolyData->SetPoints(aPts);
59   aPts->Delete();
60
61   TFaceSet::Iterator anIter(myData->myFaceSet);
62   for(; anIter.More(); anIter.Next()){
63     const TopoDS_Face& aFace = anIter.Value();
64     OCC2VTK(aFace,aPolyData,aPts);
65   }
66   return 1;
67 }
68
69 void  
70 GEOM_ShadingFace:: 
71 OCC2VTK(const TopoDS_Face& theFace,  
72         vtkPolyData* thePolyData, 
73         vtkPoints* thePts) 
74 {
75   TopLoc_Location aLoc;
76   Handle(Poly_Triangulation) aPoly = BRep_Tool::Triangulation(theFace,aLoc);
77   if(aPoly.IsNull()) 
78     return;
79   else{
80     gp_Trsf myTransf;
81     Standard_Boolean identity = true;
82     if(!aLoc.IsIdentity()){
83       identity = false;
84       myTransf = aLoc.Transformation();
85     }           
86       
87     Standard_Integer i; 
88     int aNbOfNodes = thePts->GetNumberOfPoints();
89     const TColgp_Array1OfPnt& Nodes = aPoly->Nodes();
90     Standard_Integer nbNodesInFace = aPoly->NbNodes(); 
91     for(i = 1; i <= nbNodesInFace; i++) {
92       gp_Pnt P = Nodes(i);
93       if(!identity) 
94         P.Transform(myTransf);
95       thePts->InsertNextPoint(P.X(),P.Y(),P.Z());
96     }
97
98     const Poly_Array1OfTriangle& Triangles = aPoly->Triangles();
99     Standard_Integer nbTriInFace = aPoly->NbTriangles();
100     for(i = 1; i <= nbTriInFace; i++){
101       // Get the triangle
102       Standard_Integer N1,N2,N3;
103       Triangles(i).Get(N1,N2,N3);
104       N1 += aNbOfNodes - 1;
105       N2 += aNbOfNodes - 1;
106       N3 += aNbOfNodes - 1;
107       vtkIdType anIds[3] = {N1, N2, N3};
108       thePolyData->InsertNextCell(VTK_TRIANGLE,3,anIds);
109     }
110   } 
111 }