Salome HOME
Refactoring: split base class of Boolean operation (separate methods related to versi...
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_VersionedBoolean.h
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #ifndef FeaturesPlugin_VersionedBoolean_H_
21 #define FeaturesPlugin_VersionedBoolean_H_
22
23 #include "FeaturesPlugin.h"
24 #include "FeaturesPlugin_Tools.h"
25
26 #include <GeomAlgoAPI_Tools.h>
27
28 #include <ModelAPI_Feature.h>
29
30 class ModelAPI_Attribute;
31 class ModelAPI_Result;
32 class GeomAlgoAPI_MakeShapeList;
33
34 /// \class FeaturesPlugin_VersionedBoolean
35 /// \ingroup Plugins
36 /// \brief Feature controls a version of Boolean operations.
37 class FeaturesPlugin_VersionedBoolean : public ModelAPI_Feature
38 {
39 public:
40   /// Attribute name of the version of Boolean feature
41   inline static const std::string& VERSION_ID()
42   {
43     static const std::string MY_VERSION_ID("version");
44     return MY_VERSION_ID;
45   }
46
47 protected:
48
49   /// Use plugin manager for features creation.
50   FeaturesPlugin_VersionedBoolean() {}
51
52   /// Initialize version field of the Boolean feature.
53   /// The version is initialized for newly created features,
54   /// not read from previously stored document.
55   void initVersion(const int theVersion,
56                    const std::shared_ptr<ModelAPI_Attribute> theObjectsAttr,
57                    const std::shared_ptr<ModelAPI_Attribute> theToolsAttr);
58
59   /// Auxiliary class to store hierarchy of Boolean operation objects/tools
60   /// and their parent shapes (compounds or compsolids)
61   class ObjectHierarchy {
62     typedef std::pair<GeomShapePtr, ListOfShape> ShapeAndSubshapes;
63     typedef std::map<GeomShapePtr, GeomShapePtr, GeomAPI_Shape::Comparator> MapShapeToParent;
64     typedef std::map<GeomShapePtr, size_t, GeomAPI_Shape::Comparator> MapShapeToIndex;
65     typedef std::set<GeomShapePtr, GeomAPI_Shape::Comparator> SetOfShape;
66
67     ListOfShape myObjects; ///< list of objects/tools of Boolean operation
68     MapShapeToParent myParent; ///< refer a shape to compound/compsolid containing it
69     /// indices of compounds/compsolids to keep the order of parent shapes
70     /// corresponding to the order of objects
71     MapShapeToIndex  myParentIndices;
72     /// list of shape and its subshapes stored according to the index of parent shape
73     std::vector<ShapeAndSubshapes> mySubshapes;
74
75     SetOfShape myProcessedObjects;
76
77   public:
78     /// Add object of Boolean opration
79     void AddObject(const GeomShapePtr& theObject);
80
81     /// Maps shape and its parent
82     void AddParent(const GeomShapePtr& theShape, const GeomShapePtr& theParent);
83
84     /// Return parent shape for the given, or empty if it is a high-level shape.
85     /// By default, the parent and all its subshapes are marked as processed for further skip.
86     GeomShapePtr Parent(const GeomShapePtr& theShape, bool theMarkProcessed = true);
87
88     /// Split compound/compsolid shape for subshapes selected for Boolean operation and the other.
89     void SplitCompound(const GeomShapePtr& theCompShape,
90                        ListOfShape& theUsed,
91                        ListOfShape& theNotUsed) const;
92
93     /// Generates the list of top-level compounds, which contain the objects of Boolean operation.
94     /// The generated list will contain only shapes unused during the Boolean operation.
95     void CompoundsOfUnusedObjects(ListOfShape& theDestination) const;
96
97     /// Return \c true if there is no object in hierarchy
98     bool IsEmpty() const;
99
100     /// Return list of objects
101     const ListOfShape& Objects() const { return myObjects; }
102     /// Separate objects of the given range of types and all other objects
103     void ObjectsByType(ListOfShape& theShapesByType, ListOfShape& theOtherShapes,
104         const GeomAPI_Shape::ShapeType theMinType = GeomAPI_Shape::COMPOUND,
105         const GeomAPI_Shape::ShapeType theMaxType = GeomAPI_Shape::SHAPE) const;
106
107   private:
108     GeomShapePtr collectUnusedSubs(const GeomShapePtr theTopLevelCompound,
109                                    const SetOfShape& theUsed) const;
110
111   public:
112     class Iterator {
113       friend class ObjectHierarchy;
114
115       ObjectHierarchy* myHierarchy;
116       ListOfShape::iterator myObject;
117
118       Iterator() {}
119       Iterator(ObjectHierarchy* theHierarchy, bool isBegin = true);
120
121       void SkipAlreadyProcessed();
122
123     public:
124       bool operator==(const Iterator&) const;
125       bool operator!=(const Iterator&) const;
126
127       Iterator& operator++();
128       Iterator  operator++(int);
129
130       GeomShapePtr operator*() const;
131     };
132
133     Iterator Begin();
134     Iterator End();
135   };
136
137   /// Process SelectionList attribute and fill the objects hierarchy.
138   bool processAttribute(const std::string& theAttributeName,
139                         ObjectHierarchy& theObjects,
140                         ListOfShape& thePlanesList);
141
142   /// Perform Boolean operation of the object with the tools.
143   /// In case of theResultCompound is not empty, the result of Boolean operation
144   /// is added to this compound, and corresponding ResultBody is not generated.
145   /// \return \c false if something went wrong
146   bool processObject(const GeomAlgoAPI_Tools::BOPType theBooleanType,
147                      const GeomShapePtr& theObject,
148                      const ListOfShape& theTools,
149                      const ListOfShape& thePlanes,
150                      int& theResultIndex,
151                      std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
152                      ListOfShape& theResultShapesList,
153                      GeomShapePtr theResulCompound = GeomShapePtr());
154
155   /// Perform Boolean operation of the Compsolid with the tools
156   /// In case of theResultCompound is not empty, the result of Boolean operation
157   /// is added to this compound, and corresponding ResultBody is not generated.
158   /// \return \c false if something went wrong
159   bool processCompsolid(const GeomAlgoAPI_Tools::BOPType theBooleanType,
160                         const ObjectHierarchy& theCompsolidHierarchy,
161                         const GeomShapePtr& theCompsolid,
162                         const ListOfShape& theTools,
163                         const ListOfShape& thePlanes,
164                         int& theResultIndex,
165                         std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
166                         ListOfShape& theResultShapesList,
167                         GeomShapePtr theResulCompound = GeomShapePtr());
168
169   /// Perform Boolean operation of the Compound with the tools
170   /// In case of theResultCompound is not empty, the result of Boolean operation
171   /// is added to this compound, and corresponding ResultBody is not generated.
172   /// \return \c false if something went wrong
173   bool processCompound(const GeomAlgoAPI_Tools::BOPType theBooleanType,
174                        const ObjectHierarchy& theCompoundHierarchy,
175                        const GeomShapePtr& theCompound,
176                        const ListOfShape& theTools,
177                        int& theResultIndex,
178                        std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
179                        ListOfShape& theResultShapesList,
180                        GeomShapePtr theResulCompound = GeomShapePtr());
181
182   /// Process unused sub-shapes of compounds.
183   /// Keep the compound hierarchy, but merge top-level compounds
184   /// into a single compound and add the result of the FUSE operation.
185   GeomShapePtr keepUnusedSubsOfCompound(
186       const GeomShapePtr& theResult,
187       const ObjectHierarchy& theObjectsHierarchy,
188       const ObjectHierarchy& theToolsHierarchy,
189       std::shared_ptr<GeomAlgoAPI_MakeShapeList> theMakeShapeList);
190
191   /// Return version of the feature
192   int version();
193
194 private:
195   void parentForShape(const GeomShapePtr& theShape,
196                       const std::shared_ptr<ModelAPI_Result>& theContext,
197                       ObjectHierarchy& theShapesHierarchy);
198 };
199
200 #endif