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