1 // Copyright (C) 2007-2010 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.
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 <BRepBndLib.hxx>
30 #include <BRepMesh_IncrementalMesh.hxx>
31 #include <Poly_Triangulation.hxx>
32 #include <TopExp_Explorer.hxx>
34 #include <TopTools_ListOfShape.hxx>
36 #define MAX2(X, Y) (Abs(X) > Abs(Y) ? Abs(X) : Abs(Y))
37 #define MAX3(X, Y, Z) (MAX2(MAX2(X,Y), Z))
41 void MeshShape(const TopoDS_Shape theShape,
42 Standard_Real theDeflection,
43 Standard_Boolean theForced)
45 // Mesh the shape if necessary
46 Standard_Boolean alreadymesh = Standard_True;
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; }
56 if(!alreadymesh || theForced) {
57 if(theDeflection<=0) {
58 // Compute default deflection
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;
66 BRepMesh_IncrementalMesh MESH(theShape,theDeflection);
70 void MeshShape2(const TopoDS_Shape& theShape,
74 static Standard_Real RELATIVE_DEFLECTION = 0.0001;
75 Standard_Real aDeflection = theDeflection;
77 if(theDeflection <= 0) { // Compute default theDeflection
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;
89 theDeflection = RELATIVE_DEFLECTION;
91 theDeflection = aDeflection;
94 BRepMesh_IncrementalMesh aMesh(theShape,aDeflection);
97 void SetShape(const TopoDS_Shape& theShape,
98 const TopTools_IndexedDataMapOfShapeListOfShape& theEdgeMap,
100 GEOM_EdgeSource* theIsolatedEdgeSource,
101 GEOM_EdgeSource* theOneFaceEdgeSource,
102 GEOM_EdgeSource* theSharedEdgeSource,
103 GEOM_WireframeFace* theWireframeFaceSource,
104 GEOM_ShadingFace* theShadingFaceSource)
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,
113 theWireframeFaceSource,
114 theShadingFaceSource);
118 switch (theShape.ShapeType()) {
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);
129 const TopoDS_Edge& anEdge = TopoDS::Edge(theShape);
130 if (!BRep_Tool::Degenerated(anEdge))
131 theIsolatedEdgeSource->AddEdge(anEdge,theIsVector);
134 case TopAbs_VERTEX: {
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();
150 case 0: // isolated edge
151 theIsolatedEdgeSource->AddEdge(anEdge,theIsVector);
153 case 1: // edge in only one face
154 theOneFaceEdgeSource->AddEdge(anEdge,theIsVector);
156 default: // edge shared by at least two faces
157 theSharedEdgeSource->AddEdge(anEdge,theIsVector);