Salome HOME
Fix compilation error (conflict of OK name between OCCT Plate_Plate.hxx and GEOM...
[modules/geom.git] / src / OCC2VTK / OCC2VTK_Tools.cxx
1 // Copyright (C) 2007-2024  CEA, EDF, 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 "OCC2VTK_Tools.h" 
21
22 #include "GEOM_VertexSource.h" 
23 #include "GEOM_EdgeSource.h" 
24 #include "GEOM_WireframeFace.h" 
25 #include "GEOM_ShadingFace.h"
26 #include "GEOMUtils.hxx"
27
28 #include <Bnd_Box.hxx>
29 #include <BRep_Tool.hxx>
30 #include <BRepTools.hxx>
31
32 #include <BRepBndLib.hxx>
33 #include <Poly_Triangulation.hxx>
34 #include <TopExp_Explorer.hxx>
35 #include <TopoDS.hxx>
36 #include <TopTools_ListOfShape.hxx>
37
38
39 #include <TopExp.hxx>
40 #include <vtkAppendPolyData.h>
41 #include <vtkPolyData.h>
42 #include <BRepBuilderAPI_Copy.hxx>
43
44 namespace GEOM
45 {
46   void ShapeToVTK( const TopoDS_Shape& theShape,
47                    const TopTools_IndexedDataMapOfShapeListOfShape& theEdgeMap,
48                    bool theIsVector,
49                    GEOM_VertexSource* theStandaloneVertexSource,
50                    GEOM_EdgeSource* theIsolatedEdgeSource,
51                    GEOM_EdgeSource* theOneFaceEdgeSource,
52                    GEOM_EdgeSource* theSharedEdgeSource,
53                    GEOM_WireframeFace* theWireframeFaceSource,
54                    GEOM_ShadingFace* theShadingFaceSource )
55   {
56     if (theShape.ShapeType() == TopAbs_COMPOUND) {
57       TopoDS_Iterator anItr(theShape);
58       for (; anItr.More(); anItr.Next()) {
59         ShapeToVTK( anItr.Value(),theEdgeMap,theIsVector,
60                     theStandaloneVertexSource,
61                     theIsolatedEdgeSource,
62                     theOneFaceEdgeSource,
63                     theSharedEdgeSource,
64                     theWireframeFaceSource,
65                     theShadingFaceSource );
66       }
67     }
68
69     switch (theShape.ShapeType()) {
70       case TopAbs_WIRE: {
71         TopExp_Explorer anEdgeExp(theShape,TopAbs_EDGE);
72         for (; anEdgeExp.More(); anEdgeExp.Next()){
73           const TopoDS_Edge& anEdge = TopoDS::Edge(anEdgeExp.Current());
74           if (!BRep_Tool::Degenerated(anEdge))
75             theIsolatedEdgeSource->AddEdge(anEdge,theIsVector);
76         }
77         break;
78       }
79       case TopAbs_EDGE: {
80         const TopoDS_Edge& anEdge = TopoDS::Edge(theShape);
81         if (!BRep_Tool::Degenerated(anEdge))
82           theIsolatedEdgeSource->AddEdge(anEdge,theIsVector);
83         break;
84       }
85       case TopAbs_VERTEX: {
86         if ( theStandaloneVertexSource ) {
87           const TopoDS_Vertex& aVertex = TopoDS::Vertex(theShape);
88           theStandaloneVertexSource->AddVertex(aVertex);
89         }
90         break;
91       }
92       default: {
93         TopExp_Explorer aFaceExp (theShape,TopAbs_FACE);
94         for(; aFaceExp.More(); aFaceExp.Next()) {
95           const TopoDS_Face& aFace = TopoDS::Face(aFaceExp.Current());
96           theWireframeFaceSource->AddFace(aFace);
97           theShadingFaceSource->AddFace(aFace);
98           TopExp_Explorer anEdgeExp(aFaceExp.Current(), TopAbs_EDGE);
99           for(; anEdgeExp.More(); anEdgeExp.Next()) {
100             const TopoDS_Edge& anEdge = TopoDS::Edge(anEdgeExp.Current());
101             if(!BRep_Tool::Degenerated(anEdge)){
102               // compute the number of faces
103               int aNbOfFaces = theEdgeMap.FindFromKey(anEdge).Extent();
104               switch(aNbOfFaces){
105               case 0:  // isolated edge
106                 theIsolatedEdgeSource->AddEdge(anEdge,theIsVector);
107                 break;
108               case 1:  // edge in only one face
109                 theOneFaceEdgeSource->AddEdge(anEdge,theIsVector);
110                 break;
111               default: // edge shared by at least two faces
112                 theSharedEdgeSource->AddEdge(anEdge,theIsVector);
113               }
114             }
115           }
116         }
117       }
118     }
119   }
120
121   vtkPolyData* GetVTKData( const TopoDS_Shape& theShape, float theDeflection )
122   {
123     vtkPolyData* ret = 0;
124
125     BRepBuilderAPI_Copy aCopy(theShape);
126     if (aCopy.IsDone() ) {
127
128       TopoDS_Shape aShape = aCopy.Shape();
129       
130       try {
131         GEOM_VertexSource* myVertexSource = GEOM_VertexSource::New();
132         GEOM_EdgeSource* myIsolatedEdgeSource = GEOM_EdgeSource::New();
133         GEOM_EdgeSource* myOneFaceEdgeSource = GEOM_EdgeSource::New();
134         GEOM_EdgeSource* mySharedEdgeSource = GEOM_EdgeSource::New();
135         GEOM_WireframeFace* myWireframeFaceSource = GEOM_WireframeFace::New();
136         GEOM_ShadingFace* myShadingFaceSource = GEOM_ShadingFace::New();
137         
138         vtkAppendPolyData* myAppendFilter = vtkAppendPolyData::New();
139         myAppendFilter->AddInputConnection( myVertexSource->GetOutputPort() );
140         myAppendFilter->AddInputConnection( myIsolatedEdgeSource->GetOutputPort() );
141         myAppendFilter->AddInputConnection( myOneFaceEdgeSource->GetOutputPort() );
142         myAppendFilter->AddInputConnection( mySharedEdgeSource->GetOutputPort() );      
143         myAppendFilter->AddInputConnection( myShadingFaceSource->GetOutputPort() );
144         
145         bool anIsVector = false;
146         
147         GEOMUtils::MeshShape( aShape, theDeflection );
148         TopExp_Explorer aVertexExp( aShape, TopAbs_VERTEX );
149         for( ; aVertexExp.More(); aVertexExp.Next() ) {
150           const TopoDS_Vertex& aVertex = TopoDS::Vertex( aVertexExp.Current() );
151           myVertexSource->AddVertex( aVertex );
152         }
153       
154         TopTools_IndexedDataMapOfShapeListOfShape anEdgeMap;
155         TopExp::MapShapesAndAncestors( aShape, TopAbs_EDGE, TopAbs_FACE, anEdgeMap );
156         
157         ShapeToVTK( aShape,
158                     anEdgeMap,
159                     anIsVector,
160                     0,
161                     myIsolatedEdgeSource,
162                     myOneFaceEdgeSource,
163                     mySharedEdgeSource,
164                     myWireframeFaceSource,
165                     myShadingFaceSource );
166         
167         myAppendFilter->Update();
168         
169         myVertexSource->Delete();
170         myIsolatedEdgeSource->Delete();
171         myOneFaceEdgeSource->Delete();
172         mySharedEdgeSource->Delete();
173         myWireframeFaceSource->Delete();
174         myShadingFaceSource->Delete();
175         
176         ret = vtkPolyData::New();
177         ret->ShallowCopy(myAppendFilter->GetOutput());
178         myAppendFilter->Delete();
179       }
180       catch(Standard_Failure&) {
181       }
182     }
183     return ret;
184   }
185 }