Salome HOME
538b826af34553bd3f2b4f1030ea3e094dc56f14
[modules/shaper.git] / src / GeomAPI / GeomAPI_ShapeHierarchy.h
1 // Copyright (C) 2014-2020  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 GeomAPI_ShapeHierarchy_H_
21 #define GeomAPI_ShapeHierarchy_H_
22
23 #include "GeomAPI.h"
24 #include "GeomAPI_Shape.h"
25
26 #include <map>
27 #include <set>
28 #include <vector>
29
30 /// \class GeomAPI_ShapeHierarchy
31 /// \ingroup Plugins
32 /// \brief Storage for the hierarchy of shapes and their parents (compounds or compsolids)
33 class GeomAPI_ShapeHierarchy
34 {
35   typedef std::pair<GeomShapePtr, ListOfShape> ShapeAndSubshapes;
36   typedef std::map<GeomShapePtr, GeomShapePtr, GeomAPI_Shape::Comparator> MapShapeToParent;
37   typedef std::map<GeomShapePtr, size_t, GeomAPI_Shape::Comparator> MapShapeToIndex;
38   typedef std::set<GeomShapePtr, GeomAPI_Shape::Comparator> SetOfShape;
39
40   ListOfShape myObjects; ///< list of objects of some operation
41   MapShapeToParent myParent; ///< refer a shape to compound/compsolid containing it
42   /// indices of compounds/compsolids to keep the order of parent shapes
43   /// corresponding to the order of objects
44   MapShapeToIndex  myParentIndices;
45   /// list of shape and its subshapes stored according to the index of parent shape
46   std::vector<ShapeAndSubshapes> mySubshapes;
47
48   SetOfShape myProcessedObjects;
49
50 public:
51   /// Add an object of the operation (low-level shape in the hierarchy)
52   GEOMAPI_EXPORT void addObject(const GeomShapePtr& theObject);
53
54   /// Store link between shape and its parent.
55   /// Has to be called by high-level algorithm, because the parent compound/compsolid
56   /// is usually stored as a top-level result
57   GEOMAPI_EXPORT void addParent(const GeomShapePtr& theShape, const GeomShapePtr& theParent);
58
59   /// Return parent shape for the given, or empty if it is a high-level shape.
60   /// By default, the parent and all its subshapes are marked as processed for further skip.
61   GEOMAPI_EXPORT GeomShapePtr parent(const GeomShapePtr& theShape, bool theMarkProcessed = true);
62
63   /// Mark the shape as already processed
64   GEOMAPI_EXPORT void markProcessed(const GeomShapePtr& theShape);
65   /// Mark list ofshapes as already processed
66   GEOMAPI_EXPORT void markProcessed(const ListOfShape& theShapes);
67
68   /// Split compound/compsolid shape for subshapes selected for operation and the others.
69   GEOMAPI_EXPORT void splitCompound(const GeomShapePtr& theCompShape,
70                                     ListOfShape& theUsed,
71                                     ListOfShape& theNotUsed) const;
72
73   /// Generates the list of top-level compounds, which exclude the objects of operation.
74   GEOMAPI_EXPORT void compoundsOfUnusedObjects(ListOfShape& theDestination) const;
75
76   /// Return \c true if there is no object in hierarchy
77   GEOMAPI_EXPORT bool empty() const;
78
79   /// Return list of objects
80   const ListOfShape& objects() const { return myObjects; }
81   /// Separate objects of the given range of types and all other objects
82   GEOMAPI_EXPORT void objectsByType(ListOfShape& theShapesByType, ListOfShape& theOtherShapes,
83       const GeomAPI_Shape::ShapeType theMinType = GeomAPI_Shape::COMPOUND,
84       const GeomAPI_Shape::ShapeType theMaxType = GeomAPI_Shape::SHAPE) const;
85
86 private:
87   GeomShapePtr collectUnusedSubs(const GeomShapePtr theTopLevelCompound,
88                                  const SetOfShape& theUsed) const;
89
90 public:
91   class iterator : public std::iterator<std::forward_iterator_tag, GeomShapePtr>
92   {
93   public:
94     GEOMAPI_EXPORT iterator() {}
95
96   protected:
97     iterator(GeomAPI_ShapeHierarchy* theHierarchy, bool isBegin = true);
98
99     void skipAlreadyProcessed();
100
101   public:
102     GEOMAPI_EXPORT bool operator==(const iterator&) const;
103     GEOMAPI_EXPORT bool operator!=(const iterator&) const;
104
105     GEOMAPI_EXPORT iterator& operator++();
106     GEOMAPI_EXPORT iterator  operator++(int);
107
108     GEOMAPI_EXPORT GeomShapePtr operator*() const;
109
110   private:
111     GeomAPI_ShapeHierarchy* myHierarchy;
112     ListOfShape::iterator myObject;
113
114     friend class GeomAPI_ShapeHierarchy;
115   };
116
117   GEOMAPI_EXPORT iterator begin();
118   GEOMAPI_EXPORT iterator end();
119 };
120
121 #endif