Back to index

salome-geom  6.5.0
OCC2VTK_Tools.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 
00020 #include "OCC2VTK_Tools.h" 
00021 
00022 #include "GEOM_VertexSource.h" 
00023 #include "GEOM_EdgeSource.h" 
00024 #include "GEOM_WireframeFace.h" 
00025 #include "GEOM_ShadingFace.h"
00026 
00027 #include <Bnd_Box.hxx>
00028 #include <BRep_Tool.hxx>
00029 #include <BRepTools.hxx>
00030 
00031 #include <BRepBndLib.hxx>
00032 #include <BRepMesh_IncrementalMesh.hxx>
00033 #include <Poly_Triangulation.hxx>
00034 #include <TopExp_Explorer.hxx>
00035 #include <TopoDS.hxx>
00036 #include <TopTools_ListOfShape.hxx>
00037 
00038 #define MAX2(X, Y)    (Abs(X) > Abs(Y) ? Abs(X) : Abs(Y))
00039 #define MAX3(X, Y, Z) (MAX2(MAX2(X,Y), Z))
00040 
00041 
00042 #define DEFAULT_DEFLECTION 0.001
00043 
00044 namespace GEOM
00045 {
00046   void MeshShape(const TopoDS_Shape theShape,
00047                  float& theDeflection,
00048                  bool theForced ) {
00049     
00050     Standard_Real aDeflection = theDeflection <= 0 ? DEFAULT_DEFLECTION : theDeflection;
00051     
00052     //If deflection <= 0, than return default deflection
00053     if(theDeflection <= 0)
00054       theDeflection = aDeflection;  
00055     
00056     // Is shape triangulated?
00057     Standard_Boolean alreadymeshed = Standard_True;
00058     TopExp_Explorer ex;
00059     TopLoc_Location aLoc;
00060     for (ex.Init(theShape, TopAbs_FACE); ex.More(); ex.Next()) {
00061       const TopoDS_Face& aFace = TopoDS::Face(ex.Current());
00062       Handle(Poly_Triangulation) aPoly = BRep_Tool::Triangulation(aFace,aLoc);
00063       if(aPoly.IsNull()) { 
00064        alreadymeshed = Standard_False; 
00065        break; 
00066       }
00067     }
00068 
00069     if(!alreadymeshed || theForced) {
00070       Bnd_Box B;
00071       BRepBndLib::Add(theShape, B);
00072       if ( B.IsVoid() )
00073        return; // NPAL15983 (Bug when displaying empty groups) 
00074       Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
00075       B.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
00076 
00077       // This magic line comes from Prs3d_ShadedShape.gxx in OCCT
00078       aDeflection = MAX3(aXmax-aXmin, aYmax-aYmin, aZmax-aZmin) * aDeflection * 4;
00079       
00080       //Clean triangulation before compute incremental mesh
00081       BRepTools::Clean(theShape);
00082       
00083       //Compute triangulation
00084       BRepMesh_IncrementalMesh MESH(theShape,aDeflection); 
00085     }
00086   }
00087 
00088   void SetShape(const TopoDS_Shape& theShape,
00089                 const TopTools_IndexedDataMapOfShapeListOfShape& theEdgeMap,
00090                 bool theIsVector,
00091                 GEOM_EdgeSource* theIsolatedEdgeSource,
00092                 GEOM_EdgeSource* theOneFaceEdgeSource,
00093                 GEOM_EdgeSource* theSharedEdgeSource,
00094                 GEOM_WireframeFace* theWireframeFaceSource,
00095                 GEOM_ShadingFace* theShadingFaceSource)
00096   {
00097     if (theShape.ShapeType() == TopAbs_COMPOUND) {
00098       TopoDS_Iterator anItr(theShape);
00099       for (; anItr.More(); anItr.Next()) {
00100         SetShape(anItr.Value(),theEdgeMap,theIsVector,
00101                  theIsolatedEdgeSource,
00102                  theOneFaceEdgeSource,
00103                  theSharedEdgeSource,
00104                  theWireframeFaceSource,
00105                  theShadingFaceSource);
00106       }
00107     }
00108 
00109     switch (theShape.ShapeType()) {
00110       case TopAbs_WIRE: {
00111         TopExp_Explorer anEdgeExp(theShape,TopAbs_EDGE);
00112         for (; anEdgeExp.More(); anEdgeExp.Next()){
00113           const TopoDS_Edge& anEdge = TopoDS::Edge(anEdgeExp.Current());
00114           if (!BRep_Tool::Degenerated(anEdge))
00115             theIsolatedEdgeSource->AddEdge(anEdge,theIsVector);
00116         }
00117         break;
00118       }
00119       case TopAbs_EDGE: {
00120         const TopoDS_Edge& anEdge = TopoDS::Edge(theShape);
00121         if (!BRep_Tool::Degenerated(anEdge))
00122           theIsolatedEdgeSource->AddEdge(anEdge,theIsVector);
00123         break;
00124       }
00125       case TopAbs_VERTEX: {
00126         break;
00127       }
00128       default: {
00129         TopExp_Explorer aFaceExp (theShape,TopAbs_FACE);
00130         for(; aFaceExp.More(); aFaceExp.Next()) {
00131           const TopoDS_Face& aFace = TopoDS::Face(aFaceExp.Current());
00132           theWireframeFaceSource->AddFace(aFace);
00133           theShadingFaceSource->AddFace(aFace);
00134           TopExp_Explorer anEdgeExp(aFaceExp.Current(), TopAbs_EDGE);
00135           for(; anEdgeExp.More(); anEdgeExp.Next()) {
00136             const TopoDS_Edge& anEdge = TopoDS::Edge(anEdgeExp.Current());
00137             if(!BRep_Tool::Degenerated(anEdge)){
00138               // compute the number of faces
00139               int aNbOfFaces = theEdgeMap.FindFromKey(anEdge).Extent();
00140               switch(aNbOfFaces){
00141               case 0:  // isolated edge
00142                 theIsolatedEdgeSource->AddEdge(anEdge,theIsVector);
00143                 break;
00144               case 1:  // edge in only one face
00145                 theOneFaceEdgeSource->AddEdge(anEdge,theIsVector);
00146                 break;
00147               default: // edge shared by at least two faces
00148                 theSharedEdgeSource->AddEdge(anEdge,theIsVector);
00149               }
00150             }
00151           }
00152         }
00153       }
00154     }
00155   }
00156 }