Salome HOME
Copyrights update 2015.
[modules/geom.git] / src / OCC2VTK / OCC2VTK_Tools.cxx
1 // Copyright (C) 2007-2015  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
39 #include <TopExp.hxx>
40 #include <vtkAppendPolyData.h>
41 #include <vtkPolyData.h>
42 #include <BRepBuilderAPI_Copy.hxx>
43
44 #define MAX2(X, Y)    (Abs(X) > Abs(Y) ? Abs(X) : Abs(Y))
45 #define MAX3(X, Y, Z) (MAX2(MAX2(X,Y), Z))
46
47
48 #define DEFAULT_DEFLECTION 0.001
49
50 namespace GEOM
51 {
52   void MeshShape(const TopoDS_Shape theShape,
53                  float& theDeflection,
54                  bool theForced ) {
55     
56     Standard_Real aDeflection = theDeflection <= 0 ? DEFAULT_DEFLECTION : theDeflection;
57     
58     //If deflection <= 0, than return default deflection
59     if(theDeflection <= 0)
60       theDeflection = aDeflection;  
61     
62     // Is shape triangulated?
63     Standard_Boolean alreadymeshed = Standard_True;
64     TopExp_Explorer ex;
65     TopLoc_Location aLoc;
66     for (ex.Init(theShape, TopAbs_FACE); ex.More(); ex.Next()) {
67       const TopoDS_Face& aFace = TopoDS::Face(ex.Current());
68       Handle(Poly_Triangulation) aPoly = BRep_Tool::Triangulation(aFace,aLoc);
69       if(aPoly.IsNull()) { 
70         alreadymeshed = Standard_False; 
71         break; 
72       }
73     }
74
75     if(!alreadymeshed || theForced) {
76       Bnd_Box B;
77       BRepBndLib::Add(theShape, B);
78       if ( B.IsVoid() )
79         return; // NPAL15983 (Bug when displaying empty groups) 
80       Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
81       B.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
82
83       // This magic line comes from Prs3d_ShadedShape.gxx in OCCT
84       aDeflection = MAX3(aXmax-aXmin, aYmax-aYmin, aZmax-aZmin) * aDeflection * 4;
85       
86       //Clean triangulation before compute incremental mesh
87       BRepTools::Clean(theShape);
88       
89       //Compute triangulation
90       BRepMesh_IncrementalMesh MESH(theShape,aDeflection); 
91     }
92   }
93
94   void SetShape(const TopoDS_Shape& theShape,
95                 const TopTools_IndexedDataMapOfShapeListOfShape& theEdgeMap,
96                 bool theIsVector,
97                 GEOM_VertexSource* theStandaloneVertexSource,
98                 GEOM_EdgeSource* theIsolatedEdgeSource,
99                 GEOM_EdgeSource* theOneFaceEdgeSource,
100                 GEOM_EdgeSource* theSharedEdgeSource,
101                 GEOM_WireframeFace* theWireframeFaceSource,
102                 GEOM_ShadingFace* theShadingFaceSource)
103   {
104     if (theShape.ShapeType() == TopAbs_COMPOUND) {
105       TopoDS_Iterator anItr(theShape);
106       for (; anItr.More(); anItr.Next()) {
107         SetShape(anItr.Value(),theEdgeMap,theIsVector,
108                  theStandaloneVertexSource,
109                  theIsolatedEdgeSource,
110                  theOneFaceEdgeSource,
111                  theSharedEdgeSource,
112                  theWireframeFaceSource,
113                  theShadingFaceSource);
114       }
115     }
116
117     switch (theShape.ShapeType()) {
118       case TopAbs_WIRE: {
119         TopExp_Explorer anEdgeExp(theShape,TopAbs_EDGE);
120         for (; anEdgeExp.More(); anEdgeExp.Next()){
121           const TopoDS_Edge& anEdge = TopoDS::Edge(anEdgeExp.Current());
122           if (!BRep_Tool::Degenerated(anEdge))
123             theIsolatedEdgeSource->AddEdge(anEdge,theIsVector);
124         }
125         break;
126       }
127       case TopAbs_EDGE: {
128         const TopoDS_Edge& anEdge = TopoDS::Edge(theShape);
129         if (!BRep_Tool::Degenerated(anEdge))
130           theIsolatedEdgeSource->AddEdge(anEdge,theIsVector);
131         break;
132       }
133       case TopAbs_VERTEX: {
134         if ( theStandaloneVertexSource ) {
135           const TopoDS_Vertex& aVertex = TopoDS::Vertex(theShape);
136           theStandaloneVertexSource->AddVertex(aVertex);
137         }
138         break;
139       }
140       default: {
141         TopExp_Explorer aFaceExp (theShape,TopAbs_FACE);
142         for(; aFaceExp.More(); aFaceExp.Next()) {
143           const TopoDS_Face& aFace = TopoDS::Face(aFaceExp.Current());
144           theWireframeFaceSource->AddFace(aFace);
145           theShadingFaceSource->AddFace(aFace);
146           TopExp_Explorer anEdgeExp(aFaceExp.Current(), TopAbs_EDGE);
147           for(; anEdgeExp.More(); anEdgeExp.Next()) {
148             const TopoDS_Edge& anEdge = TopoDS::Edge(anEdgeExp.Current());
149             if(!BRep_Tool::Degenerated(anEdge)){
150               // compute the number of faces
151               int aNbOfFaces = theEdgeMap.FindFromKey(anEdge).Extent();
152               switch(aNbOfFaces){
153               case 0:  // isolated edge
154                 theIsolatedEdgeSource->AddEdge(anEdge,theIsVector);
155                 break;
156               case 1:  // edge in only one face
157                 theOneFaceEdgeSource->AddEdge(anEdge,theIsVector);
158                 break;
159               default: // edge shared by at least two faces
160                 theSharedEdgeSource->AddEdge(anEdge,theIsVector);
161               }
162             }
163           }
164         }
165       }
166     }
167   }
168
169   vtkPolyData* GetData(const TopoDS_Shape& theShape, float theDeflection) {
170     BRepBuilderAPI_Copy aCopy(theShape);
171     if(!aCopy.IsDone()) {
172       return 0;
173     }
174
175     TopoDS_Shape aShape = aCopy.Shape();
176     
177     try {
178       GEOM_VertexSource* myVertexSource = GEOM_VertexSource::New();
179       GEOM_EdgeSource* myIsolatedEdgeSource = GEOM_EdgeSource::New();
180       GEOM_EdgeSource* myOneFaceEdgeSource = GEOM_EdgeSource::New();
181       GEOM_EdgeSource* mySharedEdgeSource = GEOM_EdgeSource::New();
182       GEOM_WireframeFace* myWireframeFaceSource = GEOM_WireframeFace::New();
183       GEOM_ShadingFace* myShadingFaceSource = GEOM_ShadingFace::New();
184       
185       vtkAppendPolyData* myAppendFilter = vtkAppendPolyData::New();
186       myAppendFilter->AddInputConnection( myVertexSource->GetOutputPort() );
187       myAppendFilter->AddInputConnection( myIsolatedEdgeSource->GetOutputPort() );
188       myAppendFilter->AddInputConnection( myOneFaceEdgeSource->GetOutputPort() );
189       myAppendFilter->AddInputConnection( mySharedEdgeSource->GetOutputPort() );      
190       myAppendFilter->AddInputConnection( myShadingFaceSource->GetOutputPort() );
191       
192       bool anIsVector = false;
193       
194       GEOM::MeshShape( aShape, theDeflection );
195       TopExp_Explorer aVertexExp( aShape, TopAbs_VERTEX );
196       for( ; aVertexExp.More(); aVertexExp.Next() ) {
197         const TopoDS_Vertex& aVertex = TopoDS::Vertex( aVertexExp.Current() );
198         myVertexSource->AddVertex( aVertex );
199       }
200       
201       TopTools_IndexedDataMapOfShapeListOfShape anEdgeMap;
202       TopExp::MapShapesAndAncestors( aShape, TopAbs_EDGE, TopAbs_FACE, anEdgeMap );
203       
204       GEOM::SetShape( aShape,
205                       anEdgeMap,
206                       anIsVector,
207                       0,
208                       myIsolatedEdgeSource,
209                       myOneFaceEdgeSource,
210                       mySharedEdgeSource,
211                       myWireframeFaceSource,
212                       myShadingFaceSource );
213       
214       myAppendFilter->Update();
215
216       myVertexSource->Delete();
217       myIsolatedEdgeSource->Delete();
218       myOneFaceEdgeSource->Delete();
219       mySharedEdgeSource->Delete();
220       myWireframeFaceSource->Delete();
221       myShadingFaceSource->Delete();
222
223       vtkPolyData* ret = vtkPolyData::New();
224       ret->ShallowCopy(myAppendFilter->GetOutput());
225       myAppendFilter->Delete();
226       return ret;
227     }
228     catch(Standard_Failure) {
229       return 0;
230     }
231   }  
232 }