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>
40 #include <vtkAppendPolyData.h>
41 #include <vtkPolyData.h>
42 #include <BRepBuilderAPI_Copy.hxx>
44 #define MAX2(X, Y) (Abs(X) > Abs(Y) ? Abs(X) : Abs(Y))
45 #define MAX3(X, Y, Z) (MAX2(MAX2(X,Y), Z))
48 #define DEFAULT_DEFLECTION 0.001
52 void MeshShape(const TopoDS_Shape theShape,
56 Standard_Real aDeflection = theDeflection <= 0 ? DEFAULT_DEFLECTION : theDeflection;
58 //If deflection <= 0, than return default deflection
59 if(theDeflection <= 0)
60 theDeflection = aDeflection;
62 // Is shape triangulated?
63 Standard_Boolean alreadymeshed = Standard_True;
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);
70 alreadymeshed = Standard_False;
75 if(!alreadymeshed || theForced) {
77 BRepBndLib::Add(theShape, B);
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);
83 // This magic line comes from Prs3d_ShadedShape.gxx in OCCT
84 aDeflection = MAX3(aXmax-aXmin, aYmax-aYmin, aZmax-aZmin) * aDeflection * 4;
86 //Clean triangulation before compute incremental mesh
87 BRepTools::Clean(theShape);
89 //Compute triangulation
90 BRepMesh_IncrementalMesh MESH(theShape,aDeflection);
94 void SetShape(const TopoDS_Shape& theShape,
95 const TopTools_IndexedDataMapOfShapeListOfShape& theEdgeMap,
97 GEOM_VertexSource* theStandaloneVertexSource,
98 GEOM_EdgeSource* theIsolatedEdgeSource,
99 GEOM_EdgeSource* theOneFaceEdgeSource,
100 GEOM_EdgeSource* theSharedEdgeSource,
101 GEOM_WireframeFace* theWireframeFaceSource,
102 GEOM_ShadingFace* theShadingFaceSource)
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,
112 theWireframeFaceSource,
113 theShadingFaceSource);
117 switch (theShape.ShapeType()) {
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);
128 const TopoDS_Edge& anEdge = TopoDS::Edge(theShape);
129 if (!BRep_Tool::Degenerated(anEdge))
130 theIsolatedEdgeSource->AddEdge(anEdge,theIsVector);
133 case TopAbs_VERTEX: {
134 if ( theStandaloneVertexSource ) {
135 const TopoDS_Vertex& aVertex = TopoDS::Vertex(theShape);
136 theStandaloneVertexSource->AddVertex(aVertex);
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();
153 case 0: // isolated edge
154 theIsolatedEdgeSource->AddEdge(anEdge,theIsVector);
156 case 1: // edge in only one face
157 theOneFaceEdgeSource->AddEdge(anEdge,theIsVector);
159 default: // edge shared by at least two faces
160 theSharedEdgeSource->AddEdge(anEdge,theIsVector);
169 vtkPolyData* GetData(const TopoDS_Shape& theShape, float theDeflection) {
170 BRepBuilderAPI_Copy aCopy(theShape);
171 if(!aCopy.IsDone()) {
175 TopoDS_Shape aShape = aCopy.Shape();
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();
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() );
192 bool anIsVector = false;
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 );
201 TopTools_IndexedDataMapOfShapeListOfShape anEdgeMap;
202 TopExp::MapShapesAndAncestors( aShape, TopAbs_EDGE, TopAbs_FACE, anEdgeMap );
204 GEOM::SetShape( aShape,
208 myIsolatedEdgeSource,
211 myWireframeFaceSource,
212 myShadingFaceSource );
214 myAppendFilter->Update();
216 myVertexSource->Delete();
217 myIsolatedEdgeSource->Delete();
218 myOneFaceEdgeSource->Delete();
219 mySharedEdgeSource->Delete();
220 myWireframeFaceSource->Delete();
221 myShadingFaceSource->Delete();
223 vtkPolyData* ret = vtkPolyData::New();
224 ret->ShallowCopy(myAppendFilter->GetOutput());
225 myAppendFilter->Delete();
228 catch(Standard_Failure) {