Salome HOME
Task 3.2. Concealment into multi-level Compounds
[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                             = std::shared_ptr<ModelAPI_Attribute>(),
58                    const std::shared_ptr<ModelAPI_Attribute> theToolsAttr
59                             = std::shared_ptr<ModelAPI_Attribute>());
60
61   /// Auxiliary class to store hierarchy of Boolean operation objects/tools
62   /// and their parent shapes (compounds or compsolids)
63   class ObjectHierarchy {
64     typedef std::pair<GeomShapePtr, ListOfShape> ShapeAndSubshapes;
65     typedef std::map<GeomShapePtr, GeomShapePtr, GeomAPI_Shape::Comparator> MapShapeToParent;
66     typedef std::map<GeomShapePtr, size_t, GeomAPI_Shape::Comparator> MapShapeToIndex;
67     typedef std::set<GeomShapePtr, GeomAPI_Shape::Comparator> SetOfShape;
68
69     ListOfShape myObjects; ///< list of objects/tools of Boolean operation
70     MapShapeToParent myParent; ///< refer a shape to compound/compsolid containing it
71     /// indices of compounds/compsolids to keep the order of parent shapes
72     /// corresponding to the order of objects
73     MapShapeToIndex  myParentIndices;
74     /// list of shape and its subshapes stored according to the index of parent shape
75     std::vector<ShapeAndSubshapes> mySubshapes;
76
77     SetOfShape myProcessedObjects;
78
79   public:
80     /// Add object of Boolean opration
81     void AddObject(const GeomShapePtr& theObject);
82
83     /// Maps shape and its parent
84     void AddParent(const GeomShapePtr& theShape, const GeomShapePtr& theParent);
85
86     /// Return parent shape for the given, or empty if it is a high-level shape.
87     /// By default, the parent and all its subshapes are marked as processed for further skip.
88     GeomShapePtr Parent(const GeomShapePtr& theShape, bool theMarkProcessed = true);
89
90     /// Split compound/compsolid shape for subshapes selected for Boolean operation and the other.
91     void SplitCompound(const GeomShapePtr& theCompShape,
92                        ListOfShape& theUsed,
93                        ListOfShape& theNotUsed) const;
94
95     /// Generates the list of top-level compounds, which contain the objects of Boolean operation.
96     /// The generated list will contain only shapes unused during the Boolean operation.
97     void CompoundsOfUnusedObjects(ListOfShape& theDestination) const;
98
99     /// Return \c true if there is no object in hierarchy
100     bool IsEmpty() const;
101
102     /// Return list of objects
103     const ListOfShape& Objects() const { return myObjects; }
104     /// Separate objects of the given range of types and all other objects
105     void ObjectsByType(ListOfShape& theShapesByType, ListOfShape& theOtherShapes,
106         const GeomAPI_Shape::ShapeType theMinType = GeomAPI_Shape::COMPOUND,
107         const GeomAPI_Shape::ShapeType theMaxType = GeomAPI_Shape::SHAPE) const;
108
109   private:
110     GeomShapePtr collectUnusedSubs(const GeomShapePtr theTopLevelCompound,
111                                    const SetOfShape& theUsed) const;
112
113   public:
114     class Iterator {
115       friend class ObjectHierarchy;
116
117       ObjectHierarchy* myHierarchy;
118       ListOfShape::iterator myObject;
119
120       Iterator() {}
121       Iterator(ObjectHierarchy* theHierarchy, bool isBegin = true);
122
123       void SkipAlreadyProcessed();
124
125     public:
126       bool operator==(const Iterator&) const;
127       bool operator!=(const Iterator&) const;
128
129       Iterator& operator++();
130       Iterator  operator++(int);
131
132       GeomShapePtr operator*() const;
133     };
134
135     Iterator Begin();
136     Iterator End();
137   };
138
139   /// Process SelectionList attribute and fill the objects hierarchy.
140   bool processAttribute(const std::string& theAttributeName,
141                         ObjectHierarchy& theObjects,
142                         ListOfShape& thePlanesList);
143
144   /// Perform Boolean operation of the object with the tools.
145   /// In case of theResultCompound is not empty, the result of Boolean operation
146   /// is added to this compound, and corresponding ResultBody is not generated.
147   /// \return \c false if something went wrong
148   bool processObject(const GeomAlgoAPI_Tools::BOPType theBooleanType,
149                      const GeomShapePtr& theObject,
150                      const ListOfShape& theTools,
151                      const ListOfShape& thePlanes,
152                      int& theResultIndex,
153                      std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
154                      ListOfShape& theResultShapesList,
155                      GeomShapePtr theResulCompound = GeomShapePtr());
156
157   /// Perform Boolean operation of the Compsolid with the tools
158   /// In case of theResultCompound is not empty, the result of Boolean operation
159   /// is added to this compound, and corresponding ResultBody is not generated.
160   /// \return \c false if something went wrong
161   bool processCompsolid(const GeomAlgoAPI_Tools::BOPType theBooleanType,
162                         const ObjectHierarchy& theCompsolidHierarchy,
163                         const GeomShapePtr& theCompsolid,
164                         const ListOfShape& theTools,
165                         const ListOfShape& thePlanes,
166                         int& theResultIndex,
167                         std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
168                         ListOfShape& theResultShapesList,
169                         GeomShapePtr theResulCompound = GeomShapePtr());
170
171   /// Perform Boolean operation of the Compound with the tools
172   /// In case of theResultCompound is not empty, the result of Boolean operation
173   /// is added to this compound, and corresponding ResultBody is not generated.
174   /// \return \c false if something went wrong
175   bool processCompound(const GeomAlgoAPI_Tools::BOPType theBooleanType,
176                        const ObjectHierarchy& theCompoundHierarchy,
177                        const GeomShapePtr& theCompound,
178                        const ListOfShape& theTools,
179                        int& theResultIndex,
180                        std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
181                        ListOfShape& theResultShapesList,
182                        GeomShapePtr theResulCompound = GeomShapePtr());
183
184   /// Resize planes to fit them to the bounding box of the given lins of objects.
185   static void resizePlanes(const ListOfShape& theObjects,
186                            ListOfShape& thePlanes,
187                            std::shared_ptr<GeomAlgoAPI_MakeShapeList>& theMakeShapeList);
188
189   /// Process unused sub-shapes of compounds.
190   /// Keep the compound hierarchy, but merge top-level compounds
191   /// into a single compound and add the result of the FUSE operation.
192   GeomShapePtr keepUnusedSubsOfCompound(
193       const GeomShapePtr& theResult,
194       const ObjectHierarchy& theObjectsHierarchy,
195       const ObjectHierarchy& theToolsHierarchy,
196       std::shared_ptr<GeomAlgoAPI_MakeShapeList> theMakeShapeList);
197
198   /// Return version of the feature
199   int version();
200
201 private:
202   void parentForShape(const GeomShapePtr& theShape,
203                       const std::shared_ptr<ModelAPI_Result>& theContext,
204                       ObjectHierarchy& theShapesHierarchy);
205 };
206
207 #endif