Salome HOME
Revert "CEA : Lot2 - Normal to a face"
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Boolean.cpp
index 0a3305bcde991df10043552c0915703f31ceaa26..a503c7d64e9bd45b165fb259f2759d5b177439ed 100644 (file)
@@ -1,60 +1,57 @@
-// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
-
-// File:        GeomAlgoAPI_Boolean.cpp
-// Created:     02 Sept 2014
-// Author:      Vitaly Smetannikov
+// Copyright (C) 2014-2021  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
+//
 
 #include "GeomAlgoAPI_Boolean.h"
 
-#include <BRepAlgoAPI_BooleanOperation.hxx>
-#include <BRepAlgoAPI_Common.hxx>
-#include <BRepAlgoAPI_Cut.hxx>
-#include <BRepAlgoAPI_Fuse.hxx>
-#include <BRepCheck_Analyzer.hxx>
-#include <TopExp_Explorer.hxx>
+#include <GeomAlgoAPI_DFLoader.h>
+#include <GeomAlgoAPI_ShapeTools.h>
+
+#include <BOPAlgo_BOP.hxx>
 #include <TopTools_ListOfShape.hxx>
+#include <TopoDS_Iterator.hxx>
+#include <TopExp_Explorer.hxx>
 
 //=================================================================================================
-std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_Boolean::makeCut(const ListOfShape& theObjects,
-                                                            const ListOfShape& theTools)
+GeomAlgoAPI_Boolean::GeomAlgoAPI_Boolean(const GeomShapePtr theObject,
+                                         const GeomShapePtr theTool,
+                                         const GeomAlgoAPI_Tools::BOPType theOperationType)
 {
-  GeomAlgoAPI_Boolean aBoolAlgo(theObjects, theTools, BOOL_CUT);
-  if(aBoolAlgo.isDone() && !aBoolAlgo.shape()->isNull() && aBoolAlgo.isValid()) {
-    return aBoolAlgo.shape();
-  }
-  return std::shared_ptr<GeomAPI_Shape>();
+  ListOfShape aListWithObject, aListWithTool;
+  aListWithObject.push_back(theObject);
+  aListWithTool.push_back(theTool);
+  build(aListWithObject, aListWithTool, theOperationType);
 }
 
 //=================================================================================================
-std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_Boolean::makeFuse(const ListOfShape& theObjects,
-                                                             const ListOfShape& theTools)
-{
-  GeomAlgoAPI_Boolean aBoolAlgo(theObjects, theTools, BOOL_FUSE);
-  if(aBoolAlgo.isDone() && !aBoolAlgo.shape()->isNull() && aBoolAlgo.isValid()) {
-    return aBoolAlgo.shape();
-  }
-  return std::shared_ptr<GeomAPI_Shape>();
-}
-
-//=================================================================================================
-std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_Boolean::makeCommon(const ListOfShape& theObjects,
-                                                               const ListOfShape& theTools)
+GeomAlgoAPI_Boolean::GeomAlgoAPI_Boolean(const GeomShapePtr theObject,
+                                         const ListOfShape& theTools,
+                                         const GeomAlgoAPI_Tools::BOPType theOperationType)
 {
-  GeomAlgoAPI_Boolean aBoolAlgo(theObjects, theTools, BOOL_COMMON);
-  if(aBoolAlgo.isDone() && !aBoolAlgo.shape()->isNull() && aBoolAlgo.isValid()) {
-    return aBoolAlgo.shape();
-  }
-  return std::shared_ptr<GeomAPI_Shape>();
+  ListOfShape aListWithObject;
+  aListWithObject.push_back(theObject);
+  build(aListWithObject, theTools, theOperationType);
 }
 
 //=================================================================================================
 GeomAlgoAPI_Boolean::GeomAlgoAPI_Boolean(const ListOfShape& theObjects,
                                          const ListOfShape& theTools,
-                                         const OperationType theOperationType)
-: myDone(false),
-  myShape(new GeomAPI_Shape()),
-  myMap(new GeomAPI_DataMapOfShapeShape()),
-  myMkShape(new GeomAlgoAPI_MakeShape())
+                                         const GeomAlgoAPI_Tools::BOPType theOperationType)
 {
   build(theObjects, theTools, theOperationType);
 }
@@ -63,7 +60,7 @@ GeomAlgoAPI_Boolean::GeomAlgoAPI_Boolean(const ListOfShape& theObjects,
 //=================================================================================================
 void GeomAlgoAPI_Boolean::build(const ListOfShape& theObjects,
                                 const ListOfShape& theTools,
-                                const OperationType theOperationType)
+                                const GeomAlgoAPI_Tools::BOPType theOperationType)
 {
   if(theObjects.empty() || theTools.empty()) {
     return;
@@ -71,86 +68,157 @@ void GeomAlgoAPI_Boolean::build(const ListOfShape& theObjects,
 
   // Getting objects.
   TopTools_ListOfShape anObjects;
-  for(ListOfShape::const_iterator anObjectsIt = theObjects.begin(); anObjectsIt != theObjects.end(); anObjectsIt++)
+  for(ListOfShape::const_iterator
+    anObjectsIt = theObjects.begin(); anObjectsIt != theObjects.end(); anObjectsIt++)
   {
     anObjects.Append((*anObjectsIt)->impl<TopoDS_Shape>());
   }
 
   // Getting tools.
   TopTools_ListOfShape aTools;
-  for(ListOfShape::const_iterator aToolsIt = theTools.begin(); aToolsIt != theTools.end(); aToolsIt++)
+  for(ListOfShape::const_iterator
+    aToolsIt = theTools.begin(); aToolsIt != theTools.end(); aToolsIt++)
   {
     aTools.Append((*aToolsIt)->impl<TopoDS_Shape>());
   }
 
   // Creating boolean operation.
-  BRepAlgoAPI_BooleanOperation* anOperation;
+  BOPAlgo_BOP* aBuilder = new BOPAlgo_BOP();
   switch (theOperationType) {
-    case BOOL_CUT: {
-      anOperation = new BRepAlgoAPI_Cut();
+    case GeomAlgoAPI_Tools::BOOL_CUT: {
+      aBuilder->SetOperation(BOPAlgo_CUT);
       break;
     }
-    case BOOL_FUSE: {
-      anOperation = new BRepAlgoAPI_Fuse();
+    case GeomAlgoAPI_Tools::BOOL_FUSE: {
+      aBuilder->SetOperation(BOPAlgo_FUSE);
       break;
     }
-    case BOOL_COMMON: {
-      anOperation = new BRepAlgoAPI_Common();
+    case GeomAlgoAPI_Tools::BOOL_COMMON: {
+      aBuilder->SetOperation(BOPAlgo_COMMON);
       break;
     }
     default: {
       return;
     }
   }
-  myMkShape->setImpl(anOperation);
-  anOperation->SetArguments(anObjects);
-  anOperation->SetTools(aTools);
+  this->setImpl(aBuilder);
+  this->setBuilderType(OCCT_BOPAlgo_Builder);
+  aBuilder->SetArguments(anObjects);
+  aBuilder->SetTools(aTools);
+
+  // Set parallel processing mode (default is false)
+  Standard_Boolean bRunParallel = Standard_True;
+  aBuilder->SetRunParallel(bRunParallel);
+
+  // Set fuzzy value to eliminate thin results
+  static const Standard_Real aFuzzy = 1.e-5;
+  aBuilder->SetFuzzyValue(aFuzzy);
 
   // Building and getting result.
-  anOperation->Build();
-  myDone = anOperation->IsDone() == Standard_True;
-  if(!myDone) {
+  aBuilder->Perform();
+  if (aBuilder->HasErrors())
     return;
-  }
-  TopoDS_Shape aResult = anOperation->Shape();
+  TopoDS_Shape aResult = aBuilder->Shape();
 
-  // fill data map to keep correct orientation of sub-shapes
-  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);
+  if(aResult.ShapeType() == TopAbs_COMPOUND) {
+    aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
+  }
+  if(aResult.ShapeType() == TopAbs_COMPOUND) {
+    std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
+    aGeomShape->setImpl(new TopoDS_Shape(aResult));
+    ListOfShape aResults;
+    aGeomShape = GeomAlgoAPI_ShapeTools::combineShapes(aGeomShape,
+                                                       GeomAPI_Shape::COMPSOLID,
+                                                       aResults);
+    aResult = aGeomShape->impl<TopoDS_Shape>();
   }
-  myShape->setImpl(new TopoDS_Shape(aResult));
-
-}
 
-//=================================================================================================
-const bool GeomAlgoAPI_Boolean::isDone() const
-{
-  return myDone;
+  std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
+  aShape->setImpl(new TopoDS_Shape(aResult));
+  this->setShape(aShape);
+  this->setDone(true);
 }
 
-//=================================================================================================
-const bool GeomAlgoAPI_Boolean::isValid() const
-{
-  BRepCheck_Analyzer aChecker(myShape->impl<TopoDS_Shape>());
-  return (aChecker.IsValid() == Standard_True);
+static bool isHistoryType(TopAbs_ShapeEnum theType) {
+  return theType == TopAbs_VERTEX || theType == TopAbs_EDGE ||
+         theType == TopAbs_FACE || theType == TopAbs_SOLID;
 }
 
-//=================================================================================================
-const std::shared_ptr<GeomAPI_Shape>& GeomAlgoAPI_Boolean::shape() const
+/// searches the corresponding result for theOld
+static void searchResult(const TopoDS_Shape& theOld, const TopoDS_Shape& theResult,
+  BOPAlgo_BOP* theBuilder, TopTools_MapOfShape& theNews)
 {
-  return myShape;
+  if (theResult.ShapeType() == theOld.ShapeType()) { // check some sub-shape produces a sub-result
+    if (theOld.IsSame(theResult)) {
+      theNews.Add(theResult);
+      return;
+    }
+    // searching for new result by sub-shapes of aSubType type
+    TopAbs_ShapeEnum aSubType = TopAbs_ShapeEnum(int(theOld.ShapeType()) + 1);
+    while(aSubType < TopAbs_VERTEX && !isHistoryType(aSubType))
+      aSubType = TopAbs_ShapeEnum(int(aSubType) + 1);
+    if (aSubType == TopAbs_SHAPE)
+      return;
+    TopTools_MapOfShape aResSubs;
+    for(TopExp_Explorer aResExp(theResult, aSubType); aResExp.More(); aResExp.Next())
+      aResSubs.Add(aResExp.Current());
+    for(TopExp_Explorer anExp(theOld, aSubType); anExp.More(); anExp.Next()) {
+      const TopTools_ListOfShape& aNewSubs = theBuilder->Modified(anExp.Current());
+      // searching for this new sub in theResult
+      for(TopTools_ListIteratorOfListOfShape aNewSub(aNewSubs); aNewSub.More(); aNewSub.Next()) {
+        if (aResSubs.Contains(aNewSub.Value())) {
+          theNews.Add(theResult);
+          return;
+        }
+      }
+    }
+  } else if (theResult.ShapeType() < theOld.ShapeType()) { // recursive search among sub-shapes
+    for(TopoDS_Iterator aSubResults(theResult); aSubResults.More(); aSubResults.Next()) {
+      searchResult(theOld, aSubResults.Value(), theBuilder, theNews);
+    }
+  }
 }
 
-//=================================================================================================
-std::shared_ptr<GeomAPI_DataMapOfShapeShape> GeomAlgoAPI_Boolean::mapOfShapes() const
-{
-  return myMap;
+// check the shape is on the higher level of compound or compsolid
+bool isInComp(const TopoDS_Shape& theComp, const TopoDS_Shape& theShape) {
+  if (theComp.ShapeType() == TopAbs_COMPOUND || theComp.ShapeType() == TopAbs_COMPSOLID) {
+    for(TopoDS_Iterator anIter(theComp); anIter.More(); anIter.Next()) {
+      if (isInComp(anIter.Value(), theShape))
+        return true;
+    }
+  } else return theShape.IsSame(theComp);
+  return false;
 }
 
 //=================================================================================================
-std::shared_ptr<GeomAlgoAPI_MakeShape> GeomAlgoAPI_Boolean::makeShape() const
+/// make arguments of Fuse produce result shapes with "modified" evolution
+void GeomAlgoAPI_Boolean::modified(const GeomShapePtr theOldShape, ListOfShape& theNewShapes)
 {
-  return myMkShape;
+  BOPAlgo_BOP* aBuilder = this->implPtr<BOPAlgo_BOP>();
+  if (aBuilder->Operation() == BOPAlgo_FUSE) { // only for fuse and when old is and argument
+    TopoDS_Shape anOld = theOldShape->impl<TopoDS_Shape>();
+    bool isOldComp = anOld.ShapeType() == TopAbs_COMPOUND || anOld.ShapeType() == TopAbs_COMPSOLID;
+    bool aFound = false;
+    TopTools_ListIteratorOfListOfShape anIter(aBuilder->Arguments());
+    for(; !aFound && anIter.More(); anIter.Next())
+      aFound = anOld.IsSame(anIter.Value()) || (!isOldComp && isInComp(anIter.Value(), anOld));
+    for(anIter.Initialize(aBuilder->Tools()); !aFound && anIter.More(); anIter.Next())
+      aFound = anOld.IsSame(anIter.Value()) || (!isOldComp && isInComp(anIter.Value(), anOld));
+    if (aFound) {
+      TopoDS_Shape aResult = aBuilder->Shape();
+      TopTools_MapOfShape aNewsMap;
+      searchResult(anOld, aResult, aBuilder, aNewsMap);
+      if (!aNewsMap.IsEmpty()) {
+        for(TopTools_MapIteratorOfMapOfShape aNewsIter(aNewsMap);
+            aNewsIter.More(); aNewsIter.Next())
+        {
+          GeomShapePtr aShape(new GeomAPI_Shape);
+          aShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aNewsIter.Value()));
+          theNewShapes.push_back(aShape);
+        }
+        return;
+      }
+    }
+  }
+  GeomAlgoAPI_MakeShape::modified(theOldShape, theNewShapes); // default behavior
 }