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     /// Marke the shape as already processed
91     void MarkProcessed(const GeomShapePtr& theShape);
92     /// Marke list ofshapes as already processed
93     void MarkProcessed(const ListOfShape& theShapes);
94
95     /// Split compound/compsolid shape for subshapes selected for Boolean operation and the other.
96     void SplitCompound(const GeomShapePtr& theCompShape,
97                        ListOfShape& theUsed,
98                        ListOfShape& theNotUsed) const;
99
100     /// Generates the list of top-level compounds, which contain the objects of Boolean operation.
101     /// The generated list will contain only shapes unused during the Boolean operation.
102     void CompoundsOfUnusedObjects(ListOfShape& theDestination) const;
103
104     /// Return \c true if there is no object in hierarchy
105     bool IsEmpty() const;
106
107     /// Return list of objects
108     const ListOfShape& Objects() const { return myObjects; }
109     /// Separate objects of the given range of types and all other objects
110     void ObjectsByType(ListOfShape& theShapesByType, ListOfShape& theOtherShapes,
111         const GeomAPI_Shape::ShapeType theMinType = GeomAPI_Shape::COMPOUND,
112         const GeomAPI_Shape::ShapeType theMaxType = GeomAPI_Shape::SHAPE) const;
113
114   private:
115     GeomShapePtr collectUnusedSubs(const GeomShapePtr theTopLevelCompound,
116                                    const SetOfShape& theUsed) const;
117
118   public:
119     class Iterator {
120       friend class ObjectHierarchy;
121
122       ObjectHierarchy* myHierarchy;
123       ListOfShape::iterator myObject;
124
125       Iterator() {}
126       Iterator(ObjectHierarchy* theHierarchy, bool isBegin = true);
127
128       void SkipAlreadyProcessed();
129
130     public:
131       bool operator==(const Iterator&) const;
132       bool operator!=(const Iterator&) const;
133
134       Iterator& operator++();
135       Iterator  operator++(int);
136
137       GeomShapePtr operator*() const;
138     };
139
140     Iterator Begin();
141     Iterator End();
142   };
143
144   /// Process SelectionList attribute and fill the objects hierarchy.
145   bool processAttribute(const std::string& theAttributeName,
146                         ObjectHierarchy& theObjects,
147                         ListOfShape& thePlanesList);
148
149   /// Perform Boolean operation of the object with the tools.
150   /// In case of theResultCompound is not empty, the result of Boolean operation
151   /// is added to this compound, and corresponding ResultBody is not generated.
152   /// \return \c false if something went wrong
153   bool processObject(const GeomAlgoAPI_Tools::BOPType theBooleanType,
154                      const GeomShapePtr& theObject,
155                      const ListOfShape& theTools,
156                      const ListOfShape& thePlanes,
157                      int& theResultIndex,
158                      std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
159                      ListOfShape& theResultShapesList,
160                      GeomShapePtr theResulCompound = GeomShapePtr());
161
162   /// Perform Boolean operation of the Compsolid with the tools
163   /// In case of theResultCompound is not empty, the result of Boolean operation
164   /// is added to this compound, and corresponding ResultBody is not generated.
165   /// \return \c false if something went wrong
166   bool processCompsolid(const GeomAlgoAPI_Tools::BOPType theBooleanType,
167                         ObjectHierarchy& theCompsolidHierarchy,
168                         const GeomShapePtr& theCompsolid,
169                         const ListOfShape& theTools,
170                         const ListOfShape& thePlanes,
171                         int& theResultIndex,
172                         std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
173                         ListOfShape& theResultShapesList,
174                         GeomShapePtr theResulCompound = GeomShapePtr());
175
176   /// Perform Boolean operation of the Compound with the tools
177   /// In case of theResultCompound is not empty, the result of Boolean operation
178   /// is added to this compound, and corresponding ResultBody is not generated.
179   /// \return \c false if something went wrong
180   bool processCompound(const GeomAlgoAPI_Tools::BOPType theBooleanType,
181                        ObjectHierarchy& theCompoundHierarchy,
182                        const GeomShapePtr& theCompound,
183                        const ListOfShape& theTools,
184                        int& theResultIndex,
185                        std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
186                        ListOfShape& theResultShapesList,
187                        GeomShapePtr theResulCompound = GeomShapePtr());
188
189   /// Resize planes to fit them to the bounding box of the given lins of objects.
190   static void resizePlanes(const ListOfShape& theObjects,
191                            ListOfShape& thePlanes,
192                            std::shared_ptr<GeomAlgoAPI_MakeShapeList>& theMakeShapeList);
193
194   /// Process unused sub-shapes of compounds.
195   /// Keep the compound hierarchy, but merge top-level compounds
196   /// into a single compound and add the result of the FUSE operation.
197   GeomShapePtr keepUnusedSubsOfCompound(
198       const GeomShapePtr& theResult,
199       const ObjectHierarchy& theObjectsHierarchy,
200       const ObjectHierarchy& theToolsHierarchy,
201       std::shared_ptr<GeomAlgoAPI_MakeShapeList> theMakeShapeList);
202
203   /// Return version of the feature
204   int version();
205
206 private:
207   void parentForShape(const GeomShapePtr& theShape,
208                       const std::shared_ptr<ModelAPI_Result>& theContext,
209                       ObjectHierarchy& theShapesHierarchy);
210 };
211
212 #endif