1 // Copyright (C) 2007-2014 CEA/DEN, EDF R&D, OPEN CASCADE
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "OCC2VTK_Tools.h"
22 #include "GEOM_VertexSource.h"
23 #include "GEOM_EdgeSource.h"
24 #include "GEOM_WireframeFace.h"
25 #include "GEOM_ShadingFace.h"
27 #include <Bnd_Box.hxx>
28 #include <BRep_Tool.hxx>
29 #include <BRepTools.hxx>
31 #include <BRepBndLib.hxx>
32 #include <BRepMesh_IncrementalMesh.hxx>
33 #include <Poly_Triangulation.hxx>
34 #include <TopExp_Explorer.hxx>
36 #include <TopTools_ListOfShape.hxx>
38 #define MAX2(X, Y) (Abs(X) > Abs(Y) ? Abs(X) : Abs(Y))
39 #define MAX3(X, Y, Z) (MAX2(MAX2(X,Y), Z))
42 #define DEFAULT_DEFLECTION 0.001
46 void MeshShape(const TopoDS_Shape theShape,
50 Standard_Real aDeflection = theDeflection <= 0 ? DEFAULT_DEFLECTION : theDeflection;
52 //If deflection <= 0, than return default deflection
53 if(theDeflection <= 0)
54 theDeflection = aDeflection;
56 // Is shape triangulated?
57 Standard_Boolean alreadymeshed = Standard_True;
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);
64 alreadymeshed = Standard_False;
69 if(!alreadymeshed || theForced) {
71 BRepBndLib::Add(theShape, B);
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);
77 // This magic line comes from Prs3d_ShadedShape.gxx in OCCT
78 aDeflection = MAX3(aXmax-aXmin, aYmax-aYmin, aZmax-aZmin) * aDeflection * 4;
80 //Clean triangulation before compute incremental mesh
81 BRepTools::Clean(theShape);
83 //Compute triangulation
84 BRepMesh_IncrementalMesh MESH(theShape,aDeflection);
88 void SetShape(const TopoDS_Shape& theShape,
89 const TopTools_IndexedDataMapOfShapeListOfShape& theEdgeMap,
91 GEOM_VertexSource* theStandaloneVertexSource,
92 GEOM_EdgeSource* theIsolatedEdgeSource,
93 GEOM_EdgeSource* theOneFaceEdgeSource,
94 GEOM_EdgeSource* theSharedEdgeSource,
95 GEOM_WireframeFace* theWireframeFaceSource,
96 GEOM_ShadingFace* theShadingFaceSource)
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,
106 theWireframeFaceSource,
107 theShadingFaceSource);
111 switch (theShape.ShapeType()) {
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);
122 const TopoDS_Edge& anEdge = TopoDS::Edge(theShape);
123 if (!BRep_Tool::Degenerated(anEdge))
124 theIsolatedEdgeSource->AddEdge(anEdge,theIsVector);
127 case TopAbs_VERTEX: {
128 if ( theStandaloneVertexSource ) {
129 const TopoDS_Vertex& aVertex = TopoDS::Vertex(theShape);
130 theStandaloneVertexSource->AddVertex(aVertex);
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();
147 case 0: // isolated edge
148 theIsolatedEdgeSource->AddEdge(anEdge,theIsVector);
150 case 1: // edge in only one face
151 theOneFaceEdgeSource->AddEdge(anEdge,theIsVector);
153 default: // edge shared by at least two faces
154 theSharedEdgeSource->AddEdge(anEdge,theIsVector);