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