Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/geom.git] / src / OCC2VTK / OCC2VTK_Tools.cxx
1 // Copyright (C) 2007-2012  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.
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_EdgeSource* theIsolatedEdgeSource,
92                 GEOM_EdgeSource* theOneFaceEdgeSource,
93                 GEOM_EdgeSource* theSharedEdgeSource,
94                 GEOM_WireframeFace* theWireframeFaceSource,
95                 GEOM_ShadingFace* theShadingFaceSource)
96   {
97     if (theShape.ShapeType() == TopAbs_COMPOUND) {
98       TopoDS_Iterator anItr(theShape);
99       for (; anItr.More(); anItr.Next()) {
100         SetShape(anItr.Value(),theEdgeMap,theIsVector,
101                  theIsolatedEdgeSource,
102                  theOneFaceEdgeSource,
103                  theSharedEdgeSource,
104                  theWireframeFaceSource,
105                  theShadingFaceSource);
106       }
107     }
108
109     switch (theShape.ShapeType()) {
110       case TopAbs_WIRE: {
111         TopExp_Explorer anEdgeExp(theShape,TopAbs_EDGE);
112         for (; anEdgeExp.More(); anEdgeExp.Next()){
113           const TopoDS_Edge& anEdge = TopoDS::Edge(anEdgeExp.Current());
114           if (!BRep_Tool::Degenerated(anEdge))
115             theIsolatedEdgeSource->AddEdge(anEdge,theIsVector);
116         }
117         break;
118       }
119       case TopAbs_EDGE: {
120         const TopoDS_Edge& anEdge = TopoDS::Edge(theShape);
121         if (!BRep_Tool::Degenerated(anEdge))
122           theIsolatedEdgeSource->AddEdge(anEdge,theIsVector);
123         break;
124       }
125       case TopAbs_VERTEX: {
126         break;
127       }
128       default: {
129         TopExp_Explorer aFaceExp (theShape,TopAbs_FACE);
130         for(; aFaceExp.More(); aFaceExp.Next()) {
131           const TopoDS_Face& aFace = TopoDS::Face(aFaceExp.Current());
132           theWireframeFaceSource->AddFace(aFace);
133           theShadingFaceSource->AddFace(aFace);
134           TopExp_Explorer anEdgeExp(aFaceExp.Current(), TopAbs_EDGE);
135           for(; anEdgeExp.More(); anEdgeExp.Next()) {
136             const TopoDS_Edge& anEdge = TopoDS::Edge(anEdgeExp.Current());
137             if(!BRep_Tool::Degenerated(anEdge)){
138               // compute the number of faces
139               int aNbOfFaces = theEdgeMap.FindFromKey(anEdge).Extent();
140               switch(aNbOfFaces){
141               case 0:  // isolated edge
142                 theIsolatedEdgeSource->AddEdge(anEdge,theIsVector);
143                 break;
144               case 1:  // edge in only one face
145                 theOneFaceEdgeSource->AddEdge(anEdge,theIsVector);
146                 break;
147               default: // edge shared by at least two faces
148                 theSharedEdgeSource->AddEdge(anEdge,theIsVector);
149               }
150             }
151           }
152         }
153       }
154     }
155   }
156 }