Salome HOME
Get rid of compilation warnings. Part I.
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_VersionedBoolean.cpp
index 9021a192f5a6ae0f620e2143893c2cc2388e7af7..2cf25d034fc5b231d39d3dcfd9b2fc4f57809141 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2019  CEA/DEN, EDF R&D
+// Copyright (C) 2014-2020  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
@@ -38,6 +38,7 @@
 #include <GeomAlgoAPI_ShapeBuilder.h>
 #include <GeomAlgoAPI_ShapeTools.h>
 #include <GeomAlgoAPI_Tools.h>
+#include <GeomAlgoAPI_UnifySameDomain.h>
 #include <GeomAPI_Face.h>
 #include <GeomAPI_ShapeExplorer.h>
 #include <GeomAPI_ShapeIterator.h>
 #include <algorithm>
 #include <map>
 
+static void performBoolean(const GeomAlgoAPI_Tools::BOPType theBooleanType,
+                           GeomMakeShapePtr& theBooleanAlgo,
+                           const ListOfShape& theObjects,
+                           const ListOfShape& theTools)
+{
+  if (theBooleanType == GeomAlgoAPI_Tools::BOOL_PARTITION)
+    theBooleanAlgo.reset(new GeomAlgoAPI_Partition(theObjects, theTools));
+  else {
+    // separate processing of FUSE, if only objects are given
+    if (theBooleanType == GeomAlgoAPI_Tools::BOOL_FUSE && theTools.empty()) {
+      if (theObjects.front()->shapeType() == GeomAPI_Shape::FACE)
+        theBooleanAlgo.reset(new GeomAlgoAPI_UnifySameDomain(theObjects));
+      else {
+        ListOfShape anObjects = theObjects;
+        ListOfShape aTools;
+        aTools.splice(aTools.begin(), anObjects, anObjects.begin());
+        theBooleanAlgo.reset(new GeomAlgoAPI_Boolean(anObjects, aTools, theBooleanType));
+      }
+    }
+    else
+      theBooleanAlgo.reset(new GeomAlgoAPI_Boolean(theObjects, theTools, theBooleanType));
+  }
+}
+
 //=================================================================================================
-void FeaturesPlugin_VersionedBoolean::initVersion(const int theVersion,
+void FeaturesPlugin_VersionedBoolean::initVersion(const std::string& theVersion,
                                                   const AttributePtr theObjectsAttr,
                                                   const AttributePtr theToolsAttr)
 {
-  AttributePtr aVerAttr = data()->addAttribute(VERSION_ID(), ModelAPI_AttributeInteger::typeId());
-  aVerAttr->setIsArgument(false);
-  ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), VERSION_ID());
-  if (!integer(VERSION_ID())->isInitialized() &&
-      (!theObjectsAttr || !theObjectsAttr->isInitialized()) &&
-      (!theToolsAttr || !theToolsAttr->isInitialized())) {
+  AttributeIntegerPtr anOldVersionAttr = integer("version");
+  if (anOldVersionAttr && anOldVersionAttr->isInitialized() && data()->version().empty()) {
+    // move value to the common version interface in ModelAPI_Data
+    data()->setVersion(BOP_VERSION_9_4());
+  }
+  else if ((!theObjectsAttr || !theObjectsAttr->isInitialized()) &&
+           (!theToolsAttr || !theToolsAttr->isInitialized())) {
     // this is a newly created feature (not read from file),
     // so, initialize the latest version
-    integer(VERSION_ID())->setValue(theVersion);
+    data()->setVersion(theVersion);
   }
 }
 
 //=================================================================================================
-void FeaturesPlugin_VersionedBoolean::parentForShape(const GeomShapePtr& theShape,
-                                            const ResultPtr& theContext,
-                                            ObjectHierarchy& theShapesHierarchy)
-{
-  ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(theContext);
-  if (aResCompSolidPtr.get()) {
-    std::shared_ptr<GeomAPI_Shape> aContextShape = aResCompSolidPtr->shape();
-    if (aContextShape->shapeType() <= GeomAPI_Shape::COMPSOLID) {
-      theShapesHierarchy.AddParent(theShape, aContextShape);
-      parentForShape(aContextShape, aResCompSolidPtr, theShapesHierarchy);
-    }
-  }
-}
-
 bool FeaturesPlugin_VersionedBoolean::processAttribute(const std::string& theAttributeName,
-                                              ObjectHierarchy& theObjects,
+                                              GeomAPI_ShapeHierarchy& theObjects,
                                               ListOfShape& thePlanesList)
 {
   AttributeSelectionListPtr anObjectsSelList = selectionList(theAttributeName);
@@ -96,10 +108,10 @@ bool FeaturesPlugin_VersionedBoolean::processAttribute(const std::string& theAtt
         return false;
     }
 
-    theObjects.AddObject(anObject);
+    theObjects.addObject(anObject);
 
     ResultPtr aContext = anObjectAttr->context();
-    parentForShape(anObject, aContext, theObjects);
+    ModelAPI_Tools::fillShapeHierarchy(anObject, aContext, theObjects);
   }
   return true;
 }
@@ -198,7 +210,7 @@ bool FeaturesPlugin_VersionedBoolean::processObject(
 //=================================================================================================
 bool FeaturesPlugin_VersionedBoolean::processCompsolid(
     const GeomAlgoAPI_Tools::BOPType theBooleanType,
-    const ObjectHierarchy& theCompsolidHierarchy,
+    GeomAPI_ShapeHierarchy& theCompsolidHierarchy,
     const GeomShapePtr& theCompsolid,
     const ListOfShape& theTools,
     const ListOfShape& thePlanes,
@@ -209,7 +221,7 @@ bool FeaturesPlugin_VersionedBoolean::processCompsolid(
 {
   ListOfShape aUsedInOperationSolids;
   ListOfShape aNotUsedSolids;
-  theCompsolidHierarchy.SplitCompound(theCompsolid, aUsedInOperationSolids, aNotUsedSolids);
+  theCompsolidHierarchy.splitCompound(theCompsolid, aUsedInOperationSolids, aNotUsedSolids);
 
   std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
 
@@ -220,12 +232,7 @@ bool FeaturesPlugin_VersionedBoolean::processCompsolid(
   aToolsWithPlanes.insert(aToolsWithPlanes.end(), aPlanesCopy.begin(), aPlanesCopy.end());
 
   std::shared_ptr<GeomAlgoAPI_MakeShape> aBoolAlgo;
-  if (theBooleanType == GeomAlgoAPI_Tools::BOOL_PARTITION)
-    aBoolAlgo.reset(new GeomAlgoAPI_Partition(aUsedInOperationSolids, aToolsWithPlanes));
-  else
-    aBoolAlgo.reset(new GeomAlgoAPI_Boolean(aUsedInOperationSolids,
-                                            aToolsWithPlanes,
-                                            theBooleanType));
+  performBoolean(theBooleanType, aBoolAlgo, aUsedInOperationSolids, aToolsWithPlanes);
 
   // Checking that the algorithm worked properly.
   std::string anError;
@@ -239,6 +246,8 @@ bool FeaturesPlugin_VersionedBoolean::processCompsolid(
 
   // Add result to not used solids from compsolid.
   if (!aNotUsedSolids.empty()) {
+    theCompsolidHierarchy.markProcessed(aNotUsedSolids);
+
     ListOfShape aShapesToAdd = aNotUsedSolids;
     aShapesToAdd.push_back(aBoolAlgo->shape());
     std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
@@ -296,7 +305,7 @@ bool FeaturesPlugin_VersionedBoolean::processCompsolid(
 //=================================================================================================
 bool FeaturesPlugin_VersionedBoolean::processCompound(
     const GeomAlgoAPI_Tools::BOPType theBooleanType,
-    const ObjectHierarchy& theCompoundHierarchy,
+    GeomAPI_ShapeHierarchy& theCompoundHierarchy,
     const GeomShapePtr& theCompound,
     const ListOfShape& theTools,
     int& theResultIndex,
@@ -306,7 +315,7 @@ bool FeaturesPlugin_VersionedBoolean::processCompound(
 {
   ListOfShape aUsedInOperationShapes;
   ListOfShape aNotUsedShapes;
-  theCompoundHierarchy.SplitCompound(theCompound, aUsedInOperationShapes, aNotUsedShapes);
+  theCompoundHierarchy.splitCompound(theCompound, aUsedInOperationShapes, aNotUsedShapes);
   if (theResultCompound) {
     // Not necessary to keep all subs of the current compound,
     // all unused solids are already stored in the result compound.
@@ -314,10 +323,8 @@ bool FeaturesPlugin_VersionedBoolean::processCompound(
   }
 
   std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
-  std::shared_ptr<GeomAlgoAPI_Boolean> aBoolAlgo(
-      new GeomAlgoAPI_Boolean(aUsedInOperationShapes,
-                              theTools,
-                              theBooleanType));
+  std::shared_ptr<GeomAlgoAPI_MakeShape> aBoolAlgo;
+  performBoolean(theBooleanType, aBoolAlgo, aUsedInOperationShapes, theTools);
 
   // Checking that the algorithm worked properly.
   std::string anError;
@@ -331,6 +338,8 @@ bool FeaturesPlugin_VersionedBoolean::processCompound(
 
   // Add result to not used shape from compound.
   if (!aNotUsedShapes.empty()) {
+    theCompoundHierarchy.markProcessed(aNotUsedShapes);
+
     ListOfShape aShapesForResult = aNotUsedShapes;
     if (aResultShape->shapeType() == GeomAPI_Shape::COMPOUND) {
       for (GeomAPI_ShapeIterator aResultIt(aResultShape); aResultIt.more(); aResultIt.next()) {
@@ -385,29 +394,22 @@ bool FeaturesPlugin_VersionedBoolean::processCompound(
 //==================================================================================================
 GeomShapePtr FeaturesPlugin_VersionedBoolean::keepUnusedSubsOfCompound(
     const GeomShapePtr& theResult,
-    const ObjectHierarchy& theObjectsHierarchy,
-    const ObjectHierarchy& theToolsHierarchy,
+    const GeomAPI_ShapeHierarchy& theObjectsHierarchy,
+    const GeomAPI_ShapeHierarchy& theToolsHierarchy,
     std::shared_ptr<GeomAlgoAPI_MakeShapeList> theMakeShapeList)
 {
   ListOfShape aCompounds;
-  theObjectsHierarchy.CompoundsOfUnusedObjects(aCompounds);
-  theToolsHierarchy.CompoundsOfUnusedObjects(aCompounds);
+  theObjectsHierarchy.compoundsOfUnusedObjects(aCompounds);
+  theToolsHierarchy.compoundsOfUnusedObjects(aCompounds);
 
   GeomShapePtr aResultShape = theResult;
   if (!aCompounds.empty()) {
-    aResultShape = aCompounds.front();
-    aCompounds.pop_front();
-
-    std::shared_ptr<GeomAlgoAPI_ShapeBuilder> aBuilder(new GeomAlgoAPI_ShapeBuilder);
-    for (ListOfShape::iterator anIt = aCompounds.begin(); anIt != aCompounds.end(); ++anIt) {
-      for (GeomAPI_ShapeIterator aSub(*anIt); aSub.more(); aSub.next())
-        aBuilder->add(aResultShape, aSub.current());
-    }
-
-    if (theResult)
+    aResultShape = GeomAlgoAPI_CompoundBuilder::compound(aCompounds);
+    if (theResult) {
+      std::shared_ptr<GeomAlgoAPI_ShapeBuilder> aBuilder(new GeomAlgoAPI_ShapeBuilder);
       aBuilder->add(aResultShape, theResult);
-
-    theMakeShapeList->appendAlgo(aBuilder);
+      theMakeShapeList->appendAlgo(aBuilder);
+    }
   }
   return aResultShape;
 }
@@ -434,234 +436,3 @@ void FeaturesPlugin_VersionedBoolean::resizePlanes(
     *anIt = aTool;
   }
 }
-
-//=================================================================================================
-int FeaturesPlugin_VersionedBoolean::version()
-{
-  AttributeIntegerPtr aVersionAttr = integer(VERSION_ID());
-  int aVersion = 0;
-  if (aVersionAttr && aVersionAttr->isInitialized())
-    aVersion = aVersionAttr->value();
-  return aVersion;
-}
-
-//=================================================================================================
-
-void FeaturesPlugin_VersionedBoolean::ObjectHierarchy::AddObject(const GeomShapePtr& theObject)
-{
-  myObjects.push_back(theObject);
-}
-
-void FeaturesPlugin_VersionedBoolean::ObjectHierarchy::AddParent(const GeomShapePtr& theShape,
-                                                                 const GeomShapePtr& theParent)
-{
-  myParent[theShape] = theParent;
-
-  MapShapeToIndex::iterator aFound = myParentIndices.find(theParent);
-  size_t anIndex = myParentIndices.size();
-  if (aFound == myParentIndices.end()) {
-    myParentIndices[theParent] = anIndex;
-    mySubshapes.push_back(ShapeAndSubshapes(theParent, ListOfShape()));
-  } else
-    anIndex = aFound->second;
-
-  mySubshapes[anIndex].second.push_back(theShape);
-}
-
-GeomShapePtr FeaturesPlugin_VersionedBoolean::ObjectHierarchy::Parent(const GeomShapePtr& theShape,
-                                                                      bool theMarkProcessed)
-{
-  MapShapeToParent::const_iterator aFound = myParent.find(theShape);
-  GeomShapePtr aParent;
-  if (aFound != myParent.end()) {
-    aParent = aFound->second;
-    if (theMarkProcessed) {
-      // mark the parent and all its subs as processed by Boolean algorithm
-      myProcessedObjects.insert(aParent);
-      const ListOfShape& aSubs = mySubshapes[myParentIndices[aParent]].second;
-      for (ListOfShape::const_iterator anIt = aSubs.begin(); anIt != aSubs.end(); ++anIt)
-        myProcessedObjects.insert(*anIt);
-    }
-  }
-  return aParent;
-}
-
-void FeaturesPlugin_VersionedBoolean::ObjectHierarchy::ObjectsByType(
-    ListOfShape& theShapesByType,
-    ListOfShape& theOtherShapes,
-    const GeomAPI_Shape::ShapeType theMinType,
-    const GeomAPI_Shape::ShapeType theMaxType) const
-{
-  if (theMinType > theMaxType)
-    return ObjectsByType(theShapesByType, theOtherShapes, theMaxType, theMinType);
-
-  // no need to select objects if whole range is specified
-  if (theMinType == GeomAPI_Shape::COMPOUND && theMaxType == GeomAPI_Shape::SHAPE) {
-    theShapesByType.insert(theShapesByType.end(), myObjects.begin(), myObjects.end());
-    return;
-  }
-
-  for (ListOfShape::const_iterator anIt = myObjects.begin(); anIt != myObjects.end(); ++anIt) {
-    GeomAPI_Shape::ShapeType aType = (*anIt)->shapeType();
-    if (aType >= theMinType && aType <= theMaxType)
-      theShapesByType.push_back(*anIt);
-    else
-      theOtherShapes.push_back(*anIt);
-  }
-}
-
-
-void FeaturesPlugin_VersionedBoolean::ObjectHierarchy::SplitCompound(
-    const GeomShapePtr& theCompShape,
-    ListOfShape& theUsed,
-    ListOfShape& theNotUsed) const
-{
-  theUsed.clear();
-  theNotUsed.clear();
-
-  MapShapeToIndex::const_iterator aFoundIndex = myParentIndices.find(theCompShape);
-  if (aFoundIndex == myParentIndices.end())
-    return; // no such shape
-
-  theUsed = mySubshapes[aFoundIndex->second].second;
-  SetOfShape aSubsSet;
-  aSubsSet.insert(theUsed.begin(), theUsed.end());
-
-  for (GeomAPI_ShapeIterator anExp(theCompShape); anExp.more(); anExp.next()) {
-    GeomShapePtr aCurrent = anExp.current();
-    if (aSubsSet.find(aCurrent) == aSubsSet.end())
-      theNotUsed.push_back(aCurrent);
-  }
-}
-
-bool FeaturesPlugin_VersionedBoolean::ObjectHierarchy::IsEmpty() const
-{
-  return myObjects.empty();
-}
-
-void FeaturesPlugin_VersionedBoolean::ObjectHierarchy::CompoundsOfUnusedObjects(
-    ListOfShape& theDestination) const
-{
-  SetOfShape aUsedObjects;
-  aUsedObjects.insert(myObjects.begin(), myObjects.end());
-
-  for (std::vector<ShapeAndSubshapes>::const_iterator anIt = mySubshapes.begin();
-       anIt != mySubshapes.end(); ++anIt) {
-    MapShapeToParent::const_iterator aParent = myParent.find(anIt->first);
-    if ((aParent == myParent.end() || !aParent->second) &&
-         anIt->first->shapeType() ==  GeomAPI_Shape::COMPOUND) {
-      // this is a top-level compound
-      GeomShapePtr aCompound = collectUnusedSubs(anIt->first, aUsedObjects);
-      // add to destination non-empty compounds only
-      if (aCompound)
-        theDestination.push_back(aCompound);
-    }
-  }
-}
-
-GeomShapePtr FeaturesPlugin_VersionedBoolean::ObjectHierarchy::collectUnusedSubs(
-    GeomShapePtr theTopLevelCompound,
-    const SetOfShape& theUsed) const
-{
-  GeomShapePtr aResult = theTopLevelCompound->emptyCopied();
-  bool isResultEmpty = true;
-
-  for (GeomAPI_ShapeIterator aSub(theTopLevelCompound); aSub.more(); aSub.next()) {
-    GeomShapePtr aCurrent = aSub.current();
-    if (theUsed.find(aCurrent) != theUsed.end())
-      continue; // already used
-
-    MapShapeToIndex::const_iterator aFoundIndex = myParentIndices.find(aCurrent);
-    if (aCurrent->shapeType() > GeomAPI_Shape::COMPOUND ||
-        aFoundIndex == myParentIndices.end()) {
-      bool isAddShape = true;
-      // check compsolid is fully unused in the Boolean operation
-      if (aCurrent->shapeType() == GeomAPI_Shape::COMPSOLID) {
-        for (GeomAPI_ShapeIterator anIt(aCurrent); isAddShape && anIt.more(); anIt.next())
-          isAddShape = theUsed.find(anIt.current()) == theUsed.end();
-      }
-
-      if (isAddShape) { // low-level shape, add it
-        GeomAlgoAPI_ShapeBuilder::add(aResult, aCurrent);
-        isResultEmpty = false;
-      }
-    } else {
-      GeomShapePtr aCompound = collectUnusedSubs(aCurrent, theUsed);
-      if (aCompound) {
-        GeomAlgoAPI_ShapeBuilder::add(aResult, aCompound);
-        isResultEmpty = false;
-      }
-    }
-  }
-  return isResultEmpty ? GeomShapePtr() : aResult;
-}
-
-
-FeaturesPlugin_VersionedBoolean::ObjectHierarchy::Iterator
-FeaturesPlugin_VersionedBoolean::ObjectHierarchy::Begin()
-{
-  return Iterator(this);
-}
-
-FeaturesPlugin_VersionedBoolean::ObjectHierarchy::Iterator
-FeaturesPlugin_VersionedBoolean::ObjectHierarchy::End()
-{
-  return Iterator(this, false);
-}
-
-FeaturesPlugin_VersionedBoolean::ObjectHierarchy::Iterator::Iterator(
-    FeaturesPlugin_VersionedBoolean::ObjectHierarchy* theHierarchy, bool isBegin)
-  : myHierarchy(theHierarchy)
-{
-  if (isBegin) {
-    myObject = myHierarchy->myObjects.begin();
-    SkipAlreadyProcessed();
-  } else
-    myObject = myHierarchy->myObjects.end();
-}
-
-void FeaturesPlugin_VersionedBoolean::ObjectHierarchy::Iterator::SkipAlreadyProcessed()
-{
-  while (myObject != myHierarchy->myObjects.end() &&
-         myHierarchy->myProcessedObjects.find(*myObject) != myHierarchy->myProcessedObjects.end())
-    ++myObject;
-}
-
-bool FeaturesPlugin_VersionedBoolean::ObjectHierarchy::Iterator::operator==(
-    const Iterator& theOther) const
-{
-  return myObject == theOther.myObject;
-}
-
-bool FeaturesPlugin_VersionedBoolean::ObjectHierarchy::Iterator::operator!=(
-    const Iterator& theOther) const
-{
-  return !operator==(theOther);
-}
-
-FeaturesPlugin_VersionedBoolean::ObjectHierarchy::Iterator&
-FeaturesPlugin_VersionedBoolean::ObjectHierarchy::Iterator::operator++()
-{
-  ++myObject;
-  SkipAlreadyProcessed();
-  return *this;
-}
-
-FeaturesPlugin_VersionedBoolean::ObjectHierarchy::Iterator
-FeaturesPlugin_VersionedBoolean::ObjectHierarchy::Iterator::operator++(int)
-{
-  Iterator aCurrent;
-  aCurrent.myHierarchy = myHierarchy;
-  aCurrent.myObject = myObject;
-
-  // increase iterator
-  operator++();
-
-  return aCurrent;
-}
-
-GeomShapePtr FeaturesPlugin_VersionedBoolean::ObjectHierarchy::Iterator::operator*() const
-{
-  myHierarchy->myProcessedObjects.insert(*myObject);
-  return *myObject;
-}