Salome HOME
An attempt of porting to current OCCT.
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Partition.cpp
index c9baa888eb48bf84dc31fddf53fa9c2e4a5bed2e..4222649a85985b9020fecba800ecdfe539c93779 100644 (file)
@@ -1,8 +1,22 @@
-// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
-
-// File:        GeomAlgoAPI_Partition.cpp
-// Created:     21 Aug 2015
-// Author:      Sergey POKHODENKO
+// Copyright (C) 2014-2017  CEA/DEN, EDF R&D
+//
+// 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<mailto:webmaster.salome@opencascade.com>
+//
 
 #include "GeomAlgoAPI_Partition.h"
 
 
 #include <GEOMAlgo_Splitter.hxx>
 
-#include <Bnd_Box.hxx>
-#include <BRep_Tool.hxx>
-#include <BRepBndLib.hxx>
-#include <BRepCheck_Analyzer.hxx>
-#include <BRepLib_MakeFace.hxx>
-#include <BRepTools.hxx>
-#include <Geom_Plane.hxx>
-#include <GeomLib_IsPlanarSurface.hxx>
-#include <GeomLib_Tool.hxx>
-#include <IntAna_IntConicQuad.hxx>
-#include <IntAna_Quadric.hxx>
-#include <Precision.hxx>
 #include <TopExp_Explorer.hxx>
-#include <TopoDS.hxx>
 #include <TopoDS_Builder.hxx>
-#include <TopoDS_Face.hxx>
-#include <TopTools_ListOfShape.hxx>
+#include <TopTools_MapOfShape.hxx>
 
 //=================================================================================================
 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_Partition::make(const ListOfShape& theObjects,
                                                            const ListOfShape& theTools)
 {
-  GeomAlgoAPI_Partition aBoolAlgo(theObjects, theTools);
-  if(aBoolAlgo.isDone() && !aBoolAlgo.shape()->isNull() && aBoolAlgo.isValid()) {
-    return aBoolAlgo.shape();
+  GeomAlgoAPI_Partition aPartitionAlgo(theObjects, theTools);
+  if(aPartitionAlgo.isDone() && !aPartitionAlgo.shape()->isNull() && aPartitionAlgo.isValid()) {
+    return aPartitionAlgo.shape();
   }
   return std::shared_ptr<GeomAPI_Shape>();
 }
@@ -43,11 +43,25 @@ std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_Partition::make(const ListOfShape& th
 //=================================================================================================
 GeomAlgoAPI_Partition::GeomAlgoAPI_Partition(const ListOfShape& theObjects,
                                              const ListOfShape& theTools)
-: myDone(false)
 {
   build(theObjects, theTools);
 }
 
+static void prepareShapes(const TopoDS_Shape&   theShape,
+                           TopTools_ListOfShape& theSimpleList)
+{
+  if (theShape.ShapeType() != TopAbs_COMPOUND) {
+      theSimpleList.Append(theShape);
+    return;
+  }
+
+  // explode compound on simple shapes to allow their intersections
+  TopoDS_Iterator It (theShape, Standard_True, Standard_True);
+  for (; It.More(); It.Next()) {
+    TopoDS_Shape curSh = It.Value();
+    prepareShapes(curSh, theSimpleList);
+  }
+}
 
 //=================================================================================================
 void GeomAlgoAPI_Partition::build(const ListOfShape& theObjects,
@@ -59,140 +73,57 @@ void GeomAlgoAPI_Partition::build(const ListOfShape& theObjects,
 
   // Creating partition operation.
   GEOMAlgo_Splitter* anOperation = new GEOMAlgo_Splitter;
-  myMkShape.reset(new GeomAlgoAPI_MakeShape(anOperation, GeomAlgoAPI_MakeShape::BOPAlgoBuilder));
-
-  // Bounding box of all objects.
-  Bnd_Box aBndBox;
+  this->setImpl(anOperation);
+  this->setBuilderType(OCCT_BOPAlgo_Builder);
 
+  TopTools_MapOfShape ShapesMap;
   // Getting objects.
-  for (ListOfShape::const_iterator anObjectsIt = theObjects.begin(); anObjectsIt != theObjects.end(); anObjectsIt++) {
+  for(ListOfShape::const_iterator
+       anObjectsIt = theObjects.begin(); anObjectsIt != theObjects.end(); anObjectsIt++) {
     const TopoDS_Shape& aShape = (*anObjectsIt)->impl<TopoDS_Shape>();
-    BRepBndLib::Add(aShape, aBndBox);
-    anOperation->AddArgument(aShape);
-  }
-
-  // We enlarge bounding box just to be sure that plane will be large enough to cut all objects.
-  aBndBox.Enlarge(1.0);
-  Standard_Real aXArr[2] = {aBndBox.CornerMin().X(), aBndBox.CornerMax().X()};
-  Standard_Real aYArr[2] = {aBndBox.CornerMin().Y(), aBndBox.CornerMax().Y()};
-  Standard_Real aZArr[2] = {aBndBox.CornerMin().Z(), aBndBox.CornerMax().Z()};
-  gp_Pnt aPoints[8];
-  int aNum = 0;
-  for(int i = 0; i < 2; i++) {
-    for(int j = 0; j < 2; j++) {
-      for(int k = 0; k < 2; k++) {
-        aPoints[aNum] = gp_Pnt(aXArr[i], aYArr[j], aZArr[k]);
-        aNum++;
+    // #2240: decompose compounds to get the valid result
+    TopTools_ListOfShape aSimpleShapes;
+    prepareShapes(aShape, aSimpleShapes);
+    TopTools_ListIteratorOfListOfShape aSimpleIter(aSimpleShapes);
+    for (; aSimpleIter.More(); aSimpleIter.Next()) {
+      const TopoDS_Shape& aSimpleSh = aSimpleIter.Value();
+      if (ShapesMap.Add(aSimpleSh)) {
+        anOperation->AddArgument(aSimpleSh);
       }
     }
   }
 
   // Getting tools.
-  for (ListOfShape::const_iterator aToolsIt = theTools.begin(); aToolsIt != theTools.end(); aToolsIt++) {
-    TopoDS_Shape aShape = (*aToolsIt)->impl<TopoDS_Shape>();
-    if(aShape.ShapeType() == TopAbs_FACE) {
-      TopoDS_Face aFace = TopoDS::Face(aShape);
-      Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
-      if (!aSurf.IsNull()) {
-        GeomLib_IsPlanarSurface isPlanar(aSurf);
-        if(isPlanar.IsPlanar()) {
-          Standard_Real UMin, UMax, VMin, VMax;
-          BRepTools::UVBounds(aFace, UMin, UMax, VMin, VMax);
-          if(UMin == -Precision::Infinite() && UMax == Precision::Infinite() &&
-            VMin == -Precision::Infinite() && VMax == Precision::Infinite()) {
-            const gp_Pln& aFacePln = isPlanar.Plan();
-            Handle(Geom_Plane) aFacePlane = new Geom_Plane(aFacePln);
-            IntAna_Quadric aQuadric(aFacePln);
-            UMin = UMax = VMin = VMax = 0;
-            for(int i = 0; i < 8; i++) {
-              gp_Lin aLin(aPoints[i], aFacePln.Axis().Direction());
-              IntAna_IntConicQuad anIntAna(aLin, aQuadric);
-              const gp_Pnt& aPntOnFace = anIntAna.Point(1);
-              Standard_Real aPntU(0), aPntV(0);
-              GeomLib_Tool::Parameters(aFacePlane, aPntOnFace, Precision::Confusion(), aPntU, aPntV);
-              if(aPntU < UMin) UMin = aPntU;
-              if(aPntU > UMax) UMax = aPntU;
-              if(aPntV < VMin) VMin = aPntV;
-              if(aPntV > VMax) VMax = aPntV;
-            }
-            aShape = BRepLib_MakeFace(aFacePln, UMin, UMax, VMin, VMax).Face();
-          }
-        }
-      }
-    }
+  for (ListOfShape::const_iterator
+       aToolsIt = theTools.begin(); aToolsIt != theTools.end(); aToolsIt++) {
+    const TopoDS_Shape& aShape = (*aToolsIt)->impl<TopoDS_Shape>();
     anOperation->AddTool(aShape);
   }
 
   // Building and getting result.
   anOperation->Perform();
-  TopoDS_Shape aResult = anOperation->Shape();
-  myDone = !aResult.IsNull();
-  if (!myDone) {
+#ifdef USE_OCCT_720
+  if (anOperation->HasErrors())
+    return;
+#else
+  if(anOperation->ErrorStatus() != 0) {
     return;
   }
+#endif
+  TopoDS_Shape aResult = anOperation->Shape();
 
   if(aResult.ShapeType() == TopAbs_COMPOUND) {
-    aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
-  }
-  if(aResult.ShapeType() == TopAbs_COMPOUND) {
-    std::shared_ptr<GeomAPI_Shape> aCompound(new GeomAPI_Shape);
-    aCompound->setImpl(new TopoDS_Shape(aResult));
-    ListOfShape aCompSolids, aFreeSolids;
-    GeomAlgoAPI_ShapeTools::combineShapes(aCompound, GeomAPI_Shape::COMPSOLID, aCompSolids, aFreeSolids);
-    if(aCompSolids.size() == 1 && aFreeSolids.size() == 0) {
-      aResult = aCompSolids.front()->impl<TopoDS_Shape>();
-    } else if (aCompSolids.size() > 1 || (aCompSolids.size() >= 1 && aFreeSolids.size() >= 1)) {
-      TopoDS_Compound aResultComp;
-      TopoDS_Builder aBuilder;
-      aBuilder.MakeCompound(aResultComp);
-      for(ListOfShape::const_iterator anIter = aCompSolids.cbegin(); anIter != aCompSolids.cend(); anIter++) {
-        aBuilder.Add(aResultComp, (*anIter)->impl<TopoDS_Shape>());
-      }
-      for(ListOfShape::const_iterator anIter = aFreeSolids.cbegin(); anIter != aFreeSolids.cend(); anIter++) {
-        aBuilder.Add(aResultComp, (*anIter)->impl<TopoDS_Shape>());
-      }
-      aResult = aResultComp;
-    }
+    std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
+    aGeomShape->setImpl(new TopoDS_Shape(aResult));
+    aResult = GeomAlgoAPI_ShapeTools::groupSharedTopology(aGeomShape)->impl<TopoDS_Shape>();
   }
 
-  // fill data map to keep correct orientation of sub-shapes
-  myMap.reset(new GeomAPI_DataMapOfShapeShape());
-  for (TopExp_Explorer Exp(aResult,TopAbs_FACE); Exp.More(); Exp.Next()) {
-    std::shared_ptr<GeomAPI_Shape> aCurrentShape(new GeomAPI_Shape());
-    aCurrentShape->setImpl(new TopoDS_Shape(Exp.Current()));
-    myMap->bind(aCurrentShape, aCurrentShape);
+  // Setting result.
+  if(aResult.IsNull()) {
+    return;
   }
-  myShape.reset(new GeomAPI_Shape());
-  myShape->setImpl(new TopoDS_Shape(aResult));
-}
-
-//=================================================================================================
-const bool GeomAlgoAPI_Partition::isDone() const
-{
-  return myDone;
-}
-
-//=================================================================================================
-const bool GeomAlgoAPI_Partition::isValid() const
-{
-  BRepCheck_Analyzer aChecker(myShape->impl<TopoDS_Shape>());
-  return (aChecker.IsValid() == Standard_True);
-}
-
-//=================================================================================================
-const std::shared_ptr<GeomAPI_Shape>& GeomAlgoAPI_Partition::shape() const
-{
-  return myShape;
-}
-
-//=================================================================================================
-std::shared_ptr<GeomAPI_DataMapOfShapeShape> GeomAlgoAPI_Partition::mapOfShapes() const
-{
-  return myMap;
-}
-
-//=================================================================================================
-std::shared_ptr<GeomAlgoAPI_MakeShape> GeomAlgoAPI_Partition::makeShape() const
-{
-  return myMkShape;
+  std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
+  aShape->setImpl(new TopoDS_Shape(aResult));
+  this->setShape(aShape);
+  this->setDone(true);
 }