Salome HOME
Merge remote-tracking branch 'origin/master' into rnc/t_shape_plugin
[modules/geom.git] / src / OCC2VTK / OCC2VTK_Tools.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 "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
27 #include <Bnd_Box.hxx>
28 #include <BRep_Tool.hxx>
29 #include <BRepTools.hxx>
30
31 #include <BRepBndLib.hxx>
32 #include <BRepMesh_IncrementalMesh.hxx>
33 #include <Poly_Triangulation.hxx>
34 #include <TopExp_Explorer.hxx>
35 #include <TopoDS.hxx>
36 #include <TopTools_ListOfShape.hxx>
37
38 #define MAX2(X, Y)    (Abs(X) > Abs(Y) ? Abs(X) : Abs(Y))
39 #define MAX3(X, Y, Z) (MAX2(MAX2(X,Y), Z))
40
41
42 #define DEFAULT_DEFLECTION 0.001
43
44 namespace GEOM
45 {
46   void MeshShape(const TopoDS_Shape theShape,
47                  float& theDeflection,
48                  bool theForced ) {
49     
50     Standard_Real aDeflection = theDeflection <= 0 ? DEFAULT_DEFLECTION : theDeflection;
51     
52     //If deflection <= 0, than return default deflection
53     if(theDeflection <= 0)
54       theDeflection = aDeflection;  
55     
56     // Is shape triangulated?
57     Standard_Boolean alreadymeshed = Standard_True;
58     TopExp_Explorer ex;
59     TopLoc_Location aLoc;
60     for (ex.Init(theShape, TopAbs_FACE); ex.More(); ex.Next()) {
61       const TopoDS_Face& aFace = TopoDS::Face(ex.Current());
62       Handle(Poly_Triangulation) aPoly = BRep_Tool::Triangulation(aFace,aLoc);
63       if(aPoly.IsNull()) { 
64         alreadymeshed = Standard_False; 
65         break; 
66       }
67     }
68
69     if(!alreadymeshed || theForced) {
70       Bnd_Box B;
71       BRepBndLib::Add(theShape, B);
72       if ( B.IsVoid() )
73         return; // NPAL15983 (Bug when displaying empty groups) 
74       Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
75       B.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
76
77       // This magic line comes from Prs3d_ShadedShape.gxx in OCCT
78       aDeflection = MAX3(aXmax-aXmin, aYmax-aYmin, aZmax-aZmin) * aDeflection * 4;
79       
80       //Clean triangulation before compute incremental mesh
81       BRepTools::Clean(theShape);
82       
83       //Compute triangulation
84       BRepMesh_IncrementalMesh MESH(theShape,aDeflection); 
85     }
86   }
87
88   void SetShape(const TopoDS_Shape& theShape,
89                 const TopTools_IndexedDataMapOfShapeListOfShape& theEdgeMap,
90                 bool theIsVector,
91                 GEOM_VertexSource* theStandaloneVertexSource,
92                 GEOM_EdgeSource* theIsolatedEdgeSource,
93                 GEOM_EdgeSource* theOneFaceEdgeSource,
94                 GEOM_EdgeSource* theSharedEdgeSource,
95                 GEOM_WireframeFace* theWireframeFaceSource,
96                 GEOM_ShadingFace* theShadingFaceSource)
97   {
98     if (theShape.ShapeType() == TopAbs_COMPOUND) {
99       TopoDS_Iterator anItr(theShape);
100       for (; anItr.More(); anItr.Next()) {
101         SetShape(anItr.Value(),theEdgeMap,theIsVector,
102                  theStandaloneVertexSource,
103                  theIsolatedEdgeSource,
104                  theOneFaceEdgeSource,
105                  theSharedEdgeSource,
106                  theWireframeFaceSource,
107                  theShadingFaceSource);
108       }
109     }
110
111     switch (theShape.ShapeType()) {
112       case TopAbs_WIRE: {
113         TopExp_Explorer anEdgeExp(theShape,TopAbs_EDGE);
114         for (; anEdgeExp.More(); anEdgeExp.Next()){
115           const TopoDS_Edge& anEdge = TopoDS::Edge(anEdgeExp.Current());
116           if (!BRep_Tool::Degenerated(anEdge))
117             theIsolatedEdgeSource->AddEdge(anEdge,theIsVector);
118         }
119         break;
120       }
121       case TopAbs_EDGE: {
122         const TopoDS_Edge& anEdge = TopoDS::Edge(theShape);
123         if (!BRep_Tool::Degenerated(anEdge))
124           theIsolatedEdgeSource->AddEdge(anEdge,theIsVector);
125         break;
126       }
127       case TopAbs_VERTEX: {
128         if ( theStandaloneVertexSource ) {
129           const TopoDS_Vertex& aVertex = TopoDS::Vertex(theShape);
130           theStandaloneVertexSource->AddVertex(aVertex);
131         }
132         break;
133       }
134       default: {
135         TopExp_Explorer aFaceExp (theShape,TopAbs_FACE);
136         for(; aFaceExp.More(); aFaceExp.Next()) {
137           const TopoDS_Face& aFace = TopoDS::Face(aFaceExp.Current());
138           theWireframeFaceSource->AddFace(aFace);
139           theShadingFaceSource->AddFace(aFace);
140           TopExp_Explorer anEdgeExp(aFaceExp.Current(), TopAbs_EDGE);
141           for(; anEdgeExp.More(); anEdgeExp.Next()) {
142             const TopoDS_Edge& anEdge = TopoDS::Edge(anEdgeExp.Current());
143             if(!BRep_Tool::Degenerated(anEdge)){
144               // compute the number of faces
145               int aNbOfFaces = theEdgeMap.FindFromKey(anEdge).Extent();
146               switch(aNbOfFaces){
147               case 0:  // isolated edge
148                 theIsolatedEdgeSource->AddEdge(anEdge,theIsVector);
149                 break;
150               case 1:  // edge in only one face
151                 theOneFaceEdgeSource->AddEdge(anEdge,theIsVector);
152                 break;
153               default: // edge shared by at least two faces
154                 theSharedEdgeSource->AddEdge(anEdge,theIsVector);
155               }
156             }
157           }
158         }
159       }
160     }
161   }
162 }