Salome HOME
Fix valgrind error: Mismatched free() / delete / delete []
[modules/geom.git] / src / GEOMAlgo / GEOMAlgo_FinderShapeOn.cxx
index ce9b55f8da7676577efd1f4c75b09dfab73a4e74..f3834064aea1e6cd97eff3ab1d9dec5377a39b78 100644 (file)
@@ -1,10 +1,32 @@
-// File:       GEOMAlgo_FinderShapeOn.cxx
-// Created:    Tue Jan 11 14:44:31 2005
-// Author:     Peter KURNEV
-//             <pkv@irinox>
+// Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
+//
+// Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
+//
+// 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, or (at your option) any later version.
+//
+// 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
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
+
+// File:        GEOMAlgo_FinderShapeOn.cxx
+// Created:     Tue Jan 11 14:44:31 2005
+// Author:      Peter KURNEV
 
+#include <GEOMAlgo_FinderShapeOn.hxx>
 
-#include <GEOMAlgo_FinderShapeOn.ixx>
+#include <Precision.hxx>
 
 #include <gp_Pnt.hxx>
 
 #include <TopTools_IndexedMapOfShape.hxx>
 #include <TopTools_DataMapOfShapeShape.hxx>
 
+#include <Bnd_Box.hxx>
+#include <BRepBndLib.hxx>
+#include <BRepMesh_IncrementalMesh.hxx>
+
 #include <BRep_Builder.hxx>
 #include <BRep_Tool.hxx>
 
@@ -34,7 +60,6 @@
 #include <BRepLib_MakeFace.hxx>
 #include <BRepLib_FaceError.hxx>
 
-#include <BOPTools_DSFiller.hxx>
 
 #include <GEOMAlgo_WireSolid.hxx>
 #include <GEOMAlgo_ShellSolid.hxx>
 #include <GEOMAlgo_ShapeSolid.hxx>
 #include <GEOMAlgo_SolidSolid.hxx>
 #include <GEOMAlgo_SurfaceTools.hxx>
-#include <GEOMAlgo_Tools.hxx>
+#include <GEOMAlgo_AlgoTools.hxx>
+
+#include <BOPAlgo_PaveFiller.hxx>
+#include <BOPCol_ListOfShape.hxx>
 
 //=======================================================================
 //function : GEOMAlgo_FinderShapeOn
-//purpose  : 
+//purpose  :
 //=======================================================================
 GEOMAlgo_FinderShapeOn::GEOMAlgo_FinderShapeOn()
 :
@@ -59,14 +87,14 @@ GEOMAlgo_FinderShapeOn::GEOMAlgo_FinderShapeOn()
 }
 //=======================================================================
 //function : ~
-//purpose  : 
+//purpose  :
 //=======================================================================
 GEOMAlgo_FinderShapeOn::~GEOMAlgo_FinderShapeOn()
 {
 }
 //=======================================================================
 //function : SetSurface
-//purpose  : 
+//purpose  :
 //=======================================================================
 void GEOMAlgo_FinderShapeOn::SetSurface(const Handle(Geom_Surface)& aS)
 {
@@ -74,7 +102,7 @@ void GEOMAlgo_FinderShapeOn::SetSurface(const Handle(Geom_Surface)& aS)
 }
 //=======================================================================
 //function : Surface
-//purpose  : 
+//purpose  :
 //=======================================================================
 const Handle(Geom_Surface)& GEOMAlgo_FinderShapeOn::Surface() const
 {
@@ -82,7 +110,7 @@ const Handle(Geom_Surface)& GEOMAlgo_FinderShapeOn::Surface() const
 }
 //=======================================================================
 //function : SetShapeType
-//purpose  : 
+//purpose  :
 //=======================================================================
 void GEOMAlgo_FinderShapeOn::SetShapeType(const TopAbs_ShapeEnum aType)
 {
@@ -90,7 +118,7 @@ void GEOMAlgo_FinderShapeOn::SetShapeType(const TopAbs_ShapeEnum aType)
 }
 //=======================================================================
 //function : ShapeType
-//purpose  : 
+//purpose  :
 //=======================================================================
 TopAbs_ShapeEnum GEOMAlgo_FinderShapeOn::ShapeType()const
 {
@@ -98,7 +126,7 @@ TopAbs_ShapeEnum GEOMAlgo_FinderShapeOn::ShapeType()const
 }
 //=======================================================================
 //function : SetState
-//purpose  : 
+//purpose  :
 //=======================================================================
 void GEOMAlgo_FinderShapeOn::SetState(const GEOMAlgo_State aState)
 {
@@ -106,7 +134,7 @@ void GEOMAlgo_FinderShapeOn::SetState(const GEOMAlgo_State aState)
 }
 //=======================================================================
 //function : State
-//purpose  : 
+//purpose  :
 //=======================================================================
 GEOMAlgo_State GEOMAlgo_FinderShapeOn::State() const
 {
@@ -114,7 +142,7 @@ GEOMAlgo_State GEOMAlgo_FinderShapeOn::State() const
 }
 //=======================================================================
 // function: Shapes
-// purpose: 
+// purpose:
 //=======================================================================
 const TopTools_ListOfShape& GEOMAlgo_FinderShapeOn::Shapes() const
 {
@@ -140,7 +168,7 @@ const TopTools_ListOfShape& GEOMAlgo_FinderShapeOn::Shapes() const
 }
 //=======================================================================
 //function : Perform
-//purpose  : 
+//purpose  :
 //=======================================================================
 void GEOMAlgo_FinderShapeOn::Perform()
 {
@@ -158,6 +186,9 @@ void GEOMAlgo_FinderShapeOn::Perform()
     return;
   }
   //
+  // Initialize the context
+  GEOMAlgo_ShapeAlgo::Perform();
+  //
   myIsAnalytic=GEOMAlgo_SurfaceTools::IsAnalytic(mySurface);
   //
   MakeArgument1();
@@ -183,10 +214,10 @@ void GEOMAlgo_FinderShapeOn::Perform()
 }
 //=======================================================================
 //function : FindVertices
-//purpose  : 
+//purpose  :
 //=======================================================================
 void GEOMAlgo_FinderShapeOn::FindVertices()
-{ 
+{
   Standard_Integer i, aNb, iErr;
   TopAbs_State aSt;
   TopAbs_Orientation aOr;
@@ -201,7 +232,7 @@ void GEOMAlgo_FinderShapeOn::FindVertices()
   TopExp::MapShapes(myShape, myShapeType, aM);
   aNb=aM.Extent();
   if (!aNb) {
-    myWarningStatus=10; // No found subshapes of type myShapeType
+    myWarningStatus=10; // No found sub-shapes of type myShapeType
     return;
   }
   //
@@ -218,7 +249,7 @@ void GEOMAlgo_FinderShapeOn::FindVertices()
 }
 //=======================================================================
 //function : Find
-//purpose  : 
+//purpose  :
 //=======================================================================
 void GEOMAlgo_FinderShapeOn::Find()
 {
@@ -230,11 +261,11 @@ void GEOMAlgo_FinderShapeOn::Find()
   //
   aNb=aM.Extent();
   if (!aNb) {
-    myWarningStatus=10; // No found subshapes of type myShapeType
+    myWarningStatus=10; // No found sub-shapes of type myShapeType
     return;
   }
   //
-  bICS=GEOMAlgo_Tools::IsCompositeShape(myArg2);
+  bICS=GEOMAlgo_AlgoTools::IsCompositeShape(myArg2);
   if (!bICS || myIsAnalytic) {
     TopoDS_Compound aCmp;
     BRep_Builder aBB;
@@ -260,32 +291,30 @@ void GEOMAlgo_FinderShapeOn::Find()
 }
 //=======================================================================
 //function : Find
-//purpose  : 
+//purpose  :
 //=======================================================================
 void GEOMAlgo_FinderShapeOn::Find(const TopoDS_Shape& aS)
 {
   myErrorStatus=0;
   //
-  Standard_Boolean bIsDone;
   Standard_Integer i, iErr;
   TopAbs_State aSts[]={TopAbs_IN, TopAbs_OUT, TopAbs_ON};
   TopTools_ListIteratorOfListOfShape aIt;
-  BOPTools_DSFiller aDF;
+  BOPCol_ListOfShape aLS;
+  BOPAlgo_PaveFiller aPF;
   //
   // 1. Prepare DSFiller
-  aDF.SetShapes (myArg1, aS);
-  bIsDone=aDF.IsDone();
-  if (!bIsDone) {
-    myErrorStatus=30; // wrong args are used for DSFiller
-    return;
-  }
-  aDF.Perform();
-  bIsDone=aDF.IsDone();
-  if (!bIsDone) {
-    myErrorStatus=31; // DSFiller failed
+  aLS.Append(myArg1);
+  aLS.Append(aS);
+  aPF.SetArguments(aLS);
+  //
+  aPF.Perform();
+  iErr=aPF.ErrorStatus();
+  if (iErr) {
+    myErrorStatus=31; //  PaveFiller is failed
     return;
   }
-  // 
+  //
   // 2. Find shapes
   GEOMAlgo_ShapeSolid* pSS;
   GEOMAlgo_VertexSolid aVXS;
@@ -310,11 +339,11 @@ void GEOMAlgo_FinderShapeOn::Find(const TopoDS_Shape& aS)
       pSS=&aSLS;
       break;
     default:
-      myErrorStatus=12; // unallowed subshape type
+      myErrorStatus=12; // unallowed sub-shape type
       return;
   }
   //
-  pSS->SetFiller(aDF);
+  pSS->SetFiller(aPF);
   pSS->Perform();
   iErr=pSS->ErrorStatus();
   if (iErr) {
@@ -326,21 +355,21 @@ void GEOMAlgo_FinderShapeOn::Find(const TopoDS_Shape& aS)
     const TopTools_ListOfShape& aLS=pSS->Shapes(aSts[i]);
     aIt.Initialize(aLS);
     for (; aIt.More(); aIt.Next()) {
-      const TopoDS_Shape& aSImage=aIt.Value(); 
-      if (myImages.IsBound(aSImage)) { 
-       const TopoDS_Shape& aSx=myImages.Find(aSImage); 
-       myMSS.Add(aSx, aSts[i]);
+      const TopoDS_Shape& aSImage=aIt.Value();
+      if (myImages.IsBound(aSImage)) {
+        const TopoDS_Shape& aSx=myImages.Find(aSImage);
+        myMSS.Add(aSx, aSts[i]);
       }
       else {
-       myErrorStatus=33;// can not find original shape
-       return; 
+        myErrorStatus=33;// can not find original shape
+        return;
       }
     }
   }
 }
 //=======================================================================
 //function : MakeArgument1
-//purpose  : 
+//purpose  :
 //=======================================================================
 void GEOMAlgo_FinderShapeOn::MakeArgument1()
 {
@@ -358,7 +387,8 @@ void GEOMAlgo_FinderShapeOn::MakeArgument1()
   //
   // Argument 1
   if (!myIsAnalytic) {
-    aMF.Init(mySurface, Standard_True);
+    aMF.Init(mySurface, Standard_True, Precision::Confusion());
+
     aFErr=aMF.Error();
     if (aFErr!=BRepLib_FaceDone) {
       myErrorStatus=20; // can not build the face
@@ -372,6 +402,7 @@ void GEOMAlgo_FinderShapeOn::MakeArgument1()
     aM.Add(aF);
     TopExp::MapShapes(aF, TopAbs_VERTEX, aM);
     TopExp::MapShapes(aF, TopAbs_EDGE, aM);
+
     aNb=aM.Extent();
     for (i=1; i<=aNb; ++i) {
       const TopoDS_Shape& aS=aM(i);
@@ -379,24 +410,24 @@ void GEOMAlgo_FinderShapeOn::MakeArgument1()
       switch (aType) {
       case TopAbs_VERTEX: {
         const TopoDS_Vertex& aVx=TopoDS::Vertex(aS);
-       aBB.UpdateVertex(aVx, myTolerance);
+        aBB.UpdateVertex(aVx, myTolerance);
       }
-       break;
+        break;
       case TopAbs_EDGE: {
         const TopoDS_Edge& aEx=TopoDS::Edge(aS);
-       aBB.UpdateEdge(aEx, myTolerance);
+        aBB.UpdateEdge(aEx, myTolerance);
       }
-       break;
+        break;
       case TopAbs_FACE: {
         const TopoDS_Face& aFx=TopoDS::Face(aS);
-       aBB.UpdateFace(aFx, myTolerance);
+        aBB.UpdateFace(aFx, myTolerance);
       }
-       break;
+        break;
       default:
-       break;
+        break;
       }
     }
-  } //  
+  } //
   else {
     aBB.MakeFace(aFace, mySurface, myTolerance);
   }
@@ -410,7 +441,7 @@ void GEOMAlgo_FinderShapeOn::MakeArgument1()
 }
 //=======================================================================
 //function : MakeArgument2
-//purpose  : 
+//purpose  :
 //=======================================================================
 void GEOMAlgo_FinderShapeOn::MakeArgument2()
 {
@@ -427,7 +458,7 @@ void GEOMAlgo_FinderShapeOn::MakeArgument2()
 }
 //=======================================================================
 //function : CheckData
-//purpose  : 
+//purpose  :
 //=======================================================================
 void GEOMAlgo_FinderShapeOn::CheckData()
 {
@@ -444,14 +475,14 @@ void GEOMAlgo_FinderShapeOn::CheckData()
   }
   //
   if (!(myShapeType==TopAbs_VERTEX ||
-       myShapeType==TopAbs_EDGE ||
-       myShapeType==TopAbs_FACE ||
-       myShapeType==TopAbs_SOLID)) {
-    myErrorStatus=12; // unallowed subshape type
+        myShapeType==TopAbs_EDGE ||
+        myShapeType==TopAbs_FACE ||
+        myShapeType==TopAbs_SOLID)) {
+    myErrorStatus=12; // unallowed sub-shape type
     return;
   }
   //
-  if (myState==GEOMAlgo_ST_UNKNOWN || 
+  if (myState==GEOMAlgo_ST_UNKNOWN ||
       myState==GEOMAlgo_ST_INOUT) {
     myErrorStatus=13; // unallowed state type
     return;
@@ -460,12 +491,12 @@ void GEOMAlgo_FinderShapeOn::CheckData()
 //
 //=======================================================================
 //function : CopySource
-//purpose  : 
+//purpose  :
 //=======================================================================
 void GEOMAlgo_FinderShapeOn::CopySource(const TopoDS_Shape& aE,
-                                       TopTools_DataMapOfShapeShape& aImages,
-                                       TopTools_DataMapOfShapeShape& aOriginals,
-                                       TopoDS_Shape& aEx)
+                                        TopTools_DataMapOfShapeShape& aImages,
+                                        TopTools_DataMapOfShapeShape& aOriginals,
+                                        TopoDS_Shape& aEx)
 {
   Standard_Boolean bFree;
   TopAbs_ShapeEnum aType;
@@ -500,7 +531,7 @@ void GEOMAlgo_FinderShapeOn::CopySource(const TopoDS_Shape& aE,
     const TopoDS_Shape& aV=aIt.Value();
     TopoDS_Shape aVx;
     //
-    CopySource (aV, aImages, aOriginals, aVx);  
+    CopySource (aV, aImages, aOriginals, aVx);
     //
     aVx.Orientation(aV.Orientation());
     BB.Add(aEx, aVx);
@@ -508,14 +539,67 @@ void GEOMAlgo_FinderShapeOn::CopySource(const TopoDS_Shape& aE,
   //
   aEx.Free(bFree);
 }
+//
+//=======================================================================
+//function : BuildTriangulation
+//purpose  :
+//=======================================================================
+Standard_Boolean
+  GEOMAlgo_FinderShapeOn::BuildTriangulation (const TopoDS_Shape& theShape)
+{
+  // calculate deflection
+  Standard_Real aDeviationCoefficient = 0.001;
+
+  Bnd_Box B;
+  BRepBndLib::Add(theShape, B);
+  Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
+  B.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
+
+  Standard_Real dx = aXmax - aXmin, dy = aYmax - aYmin, dz = aZmax - aZmin;
+  Standard_Real aDeflection = Max(Max(dx, dy), dz) * aDeviationCoefficient * 4;
+  Standard_Real aHLRAngle = 0.349066;
+
+  // build triangulation
+  BRepMesh_IncrementalMesh Inc (theShape, aDeflection, Standard_False, aHLRAngle);
+
+  // check triangulation
+  bool isTriangulation = true;
+
+  TopExp_Explorer exp (theShape, TopAbs_FACE);
+  if (exp.More())
+  {
+    TopLoc_Location aTopLoc;
+    Handle(Poly_Triangulation) aTRF;
+    aTRF = BRep_Tool::Triangulation(TopoDS::Face(exp.Current()), aTopLoc);
+    if (aTRF.IsNull()) {
+      isTriangulation = false;
+    }
+  }
+  else // no faces, try edges
+  {
+    TopExp_Explorer expe (theShape, TopAbs_EDGE);
+    if (!expe.More()) {
+      isTriangulation = false;
+    }
+    else {
+      TopLoc_Location aLoc;
+      Handle(Poly_Polygon3D) aPE = BRep_Tool::Polygon3D(TopoDS::Edge(expe.Current()), aLoc);
+      if (aPE.IsNull()) {
+        isTriangulation = false;
+      }
+    }
+  }
+
+  return isTriangulation;
+}
 
 //
 // myErrorStatus :
 //
 // 10 -mySurface=NULL
 // 11 -myShape=NULL
-// 12 -unallowed type of subshapes 
-// 13 -unallowed state  
+// 12 -unallowed type of sub-shapes
+// 13 -unallowed state
 // 20 -can not build the face
 // 30 -wrong args are used for DSFiller
 // 31 -DSFiller failed
@@ -524,5 +608,4 @@ void GEOMAlgo_FinderShapeOn::CopySource(const TopoDS_Shape& aE,
 //
 // myWarningStatus
 //
-// 10 -subshapes of type myShapeType can not be fond in myShape
-
+// 10 - sub-shapes of type myShapeType can not be fond in myShape