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