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_EdgeSource* theIsolatedEdgeSource,
92 GEOM_EdgeSource* theOneFaceEdgeSource,
93 GEOM_EdgeSource* theSharedEdgeSource,
94 GEOM_WireframeFace* theWireframeFaceSource,
95 GEOM_ShadingFace* theShadingFaceSource)
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,
104 theWireframeFaceSource,
105 theShadingFaceSource);
109 switch (theShape.ShapeType()) {
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);
120 const TopoDS_Edge& anEdge = TopoDS::Edge(theShape);
121 if (!BRep_Tool::Degenerated(anEdge))
122 theIsolatedEdgeSource->AddEdge(anEdge,theIsVector);
125 case TopAbs_VERTEX: {
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();
141 case 0: // isolated edge
142 theIsolatedEdgeSource->AddEdge(anEdge,theIsVector);
144 case 1: // edge in only one face
145 theOneFaceEdgeSource->AddEdge(anEdge,theIsVector);
147 default: // edge shared by at least two faces
148 theSharedEdgeSource->AddEdge(anEdge,theIsVector);