Salome HOME
Internal issue 0022865: Restructurization of Partition packages.
[modules/geom.git] / src / OCC2VTK / OCC2VTK_Tools.cxx
index ef0e4ce35e5fdb1dccfc11edb6a36d168be65bbd..fc339279109f82699a1fc2f675c35c3a6ba30bdb 100755 (executable)
@@ -1,20 +1,20 @@
-//  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2011  CEA/DEN, EDF R&D, OPEN CASCADE
 //
-//  This library is free software; you can redistribute it and/or
-//  modify it under the terms of the GNU Lesser General Public
-//  License as published by the Free Software Foundation; either
-//  version 2.1 of the License.
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License.
 //
-//  This library is distributed in the hope that it will be useful,
-//  but WITHOUT ANY WARRANTY; without even the implied warranty of
-//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-//  Lesser General Public License for more details.
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
 //
-//  You should have received a copy of the GNU Lesser General Public
-//  License along with this library; if not, write to the Free Software
-//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 //
-//  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
 
 #include "OCC2VTK_Tools.h" 
@@ -26,6 +26,8 @@
 
 #include <Bnd_Box.hxx>
 #include <BRep_Tool.hxx>
+#include <BRepTools.hxx>
+
 #include <BRepBndLib.hxx>
 #include <BRepMesh_IncrementalMesh.hxx>
 #include <Poly_Triangulation.hxx>
 #define MAX2(X, Y)    (Abs(X) > Abs(Y) ? Abs(X) : Abs(Y))
 #define MAX3(X, Y, Z) (MAX2(MAX2(X,Y), Z))
 
+
+#define DEFAULT_DEFLECTION 0.001
+
 namespace GEOM
 {
   void MeshShape(const TopoDS_Shape theShape,
-                 Standard_Real theDeflection,
-                 Standard_Boolean theForced)
-  {
-    // Mesh the shape if necessary
-    Standard_Boolean alreadymesh = Standard_True;
+                 float& theDeflection,
+                 bool theForced ) {
+    
+    Standard_Real aDeflection = theDeflection <= 0 ? DEFAULT_DEFLECTION : theDeflection;
+    
+    //If deflection <= 0, than return default deflection
+    if(theDeflection <= 0)
+      theDeflection = aDeflection;  
+    
+    // Is shape triangulated?
+    Standard_Boolean alreadymeshed = Standard_True;
     TopExp_Explorer ex;
     TopLoc_Location aLoc;
-
     for (ex.Init(theShape, TopAbs_FACE); ex.More(); ex.Next()) {
       const TopoDS_Face& aFace = TopoDS::Face(ex.Current());
       Handle(Poly_Triangulation) aPoly = BRep_Tool::Triangulation(aFace,aLoc);
-      if(aPoly.IsNull()) { alreadymesh = Standard_False; break; }
-    }
-
-    if(!alreadymesh || theForced) {
-      if(theDeflection<=0) {
-        // Compute default deflection
-        Bnd_Box B;
-        BRepBndLib::Add(theShape, B);
-        if ( B.IsVoid() ) return; // NPAL15983 (Bug when displaying empty groups) 
-        Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
-        B.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
-        theDeflection = MAX3(aXmax-aXmin, aYmax-aYmin, aZmax-aZmin) * 0.001 * 4;
+      if(aPoly.IsNull()) { 
+       alreadymeshed = Standard_False; 
+       break; 
       }
-      BRepMesh_IncrementalMesh MESH(theShape,theDeflection);
     }
-  }
-
-  void MeshShape2(const TopoDS_Shape& theShape,
-                  float& theDeflection, 
-                  bool theIsRelative)
-  {
-    static Standard_Real RELATIVE_DEFLECTION = 0.0001;
-    Standard_Real aDeflection = theDeflection;
 
-    if(theDeflection <= 0) { // Compute default theDeflection
+    if(!alreadymeshed || theForced) {
       Bnd_Box B;
       BRepBndLib::Add(theShape, B);
+      if ( B.IsVoid() )
+       return; // NPAL15983 (Bug when displaying empty groups) 
       Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
       B.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
-      Standard_Real aDiagonal = (aXmax-aXmin)*(aXmax-aXmin) +
-                                (aYmax-aYmin)*(aYmax-aYmin) +
-                                (aZmax-aZmin)*(aZmax-aZmin);
-      aDiagonal = sqrt(aDiagonal);
-      aDeflection = aDiagonal*RELATIVE_DEFLECTION;
 
-      if(theIsRelative)
-        theDeflection = RELATIVE_DEFLECTION;
-      else
-        theDeflection = aDeflection;
+      // This magic line comes from Prs3d_ShadedShape.gxx in OCCT
+      aDeflection = MAX3(aXmax-aXmin, aYmax-aYmin, aZmax-aZmin) * aDeflection * 4;
+      
+      //Clean triangulation before compute incremental mesh
+      BRepTools::Clean(theShape);
+      
+      //Compute triangulation
+      BRepMesh_IncrementalMesh MESH(theShape,aDeflection); 
     }
-    BRepMesh_IncrementalMesh aMesh(theShape,aDeflection);
   }
 
   void SetShape(const TopoDS_Shape& theShape,