Salome HOME
Fix crashes in Boolean operations
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_ShapeTools.h
1 // Copyright (C) 2014-2017  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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #ifndef GeomAlgoAPI_ShapeTools_H_
22 #define GeomAlgoAPI_ShapeTools_H_
23
24 #include "GeomAlgoAPI.h"
25
26 #include <GeomAPI_Shape.h>
27 #include <GeomAPI_Vertex.h>
28
29 #include <map>
30 #include <set>
31
32 class GeomAPI_Ax1;
33 class GeomAPI_Edge;
34 class GeomAPI_Dir;
35 class GeomAPI_Face;
36 class GeomAPI_PlanarEdges;
37 class GeomAPI_Pln;
38 class GeomAPI_Pnt;
39 class GeomDataAPI_Point2D;
40 class ModelAPI_Object;
41
42 /// \class GeomAlgoAPI_ShapeTools
43 /// \ingroup DataAlgo
44 /// \brief Useful tools for working with shapes.
45 class GeomAlgoAPI_ShapeTools
46 {
47 public:
48   /// \return the total volume of the solids of the current shape or 0.0 if it can be computed.
49   GEOMALGOAPI_EXPORT static double volume(const std::shared_ptr<GeomAPI_Shape> theShape);
50
51   /// \return the total area of the faces of the current shape or 0.0 if it can be computed.
52   GEOMALGOAPI_EXPORT static double area(const std::shared_ptr<GeomAPI_Shape> theShape);
53
54   /// \return the centre of mass of the current face.
55   /// The coordinates returned for the center of mass
56   /// are expressed in the absolute Cartesian coordinate system.
57   /// (This function works only for surfaces).
58   GEOMALGOAPI_EXPORT static std::shared_ptr<GeomAPI_Pnt>
59     centreOfMass(const std::shared_ptr<GeomAPI_Shape> theShape);
60
61   /// \brief Combines faces with common edges to shells, or solids to compsolids.
62   /// \param[in] theCompound compound of shapes.
63   /// \param[in] theType type of combine.
64   /// \param[out] theCombinedShapes resulting shapes.
65   /// \param[out] theFreeShapes shapes that does not have common subshapes.
66   GEOMALGOAPI_EXPORT static std::shared_ptr<GeomAPI_Shape> combineShapes(
67     const std::shared_ptr<GeomAPI_Shape> theCompound,
68     const GeomAPI_Shape::ShapeType theType,
69     ListOfShape& theCombinedShapes,
70     ListOfShape& theFreeShapes);
71
72   /// \brief Groups shapes with shared topology to compounds.
73   /// \param[in] theCompound compound of shapes.
74   /// \return compound of compounds with shared topology.
75   GEOMALGOAPI_EXPORT static std::shared_ptr<GeomAPI_Shape>
76     groupSharedTopology(const std::shared_ptr<GeomAPI_Shape> theCompound);
77
78   /// \brief Calculates bounding box for theShapes
79   /// \return list of eight points.
80   /// \param[in] theShapes list of shapes.
81   /// \param[in] theEnlarge enlarges bounding box size.
82   GEOMALGOAPI_EXPORT static
83     std::list<std::shared_ptr<GeomAPI_Pnt> > getBoundingBox(const ListOfShape& theShapes,
84                                                             const double theEnlarge = 0.0);
85
86   /// \return infinite plane received from theFace plane.
87   GEOMALGOAPI_EXPORT static std::shared_ptr<GeomAPI_Shape>
88     faceToInfinitePlane(const std::shared_ptr<GeomAPI_Shape> theFace);
89
90   /// \brief Enlarges or reduces plane to fit bounding box.
91   /// \return plane that fits to bounding box.
92   /// \param[in] thePlane base plane.
93   /// \param[in] thePoints bounding box points (shoud be eight).
94   GEOMALGOAPI_EXPORT static std::shared_ptr<GeomAPI_Face> fitPlaneToBox(
95     const std::shared_ptr<GeomAPI_Shape> thePlane,
96     const std::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints);
97
98   /// \brief Finds the start and end vertices of theShape. theShape can be of the following type:\n
99   /// Vertex: theV1 and theV2 are the same and equal to theShape;\n
100   /// Edge : theV1 is start and theV2 is end vertex;\n
101   /// Wire : theV1 is start vertex of the first edge, theV2 is end vertex of the last edge. If wire
102   /// contains no edges theV1 and theV2 are nullified.\n
103   /// If none of the above theV1 and theV2 are nullified.
104   GEOMALGOAPI_EXPORT static void findBounds(const std::shared_ptr<GeomAPI_Shape> theShape,
105                                             std::shared_ptr<GeomAPI_Vertex>& theV1,
106                                             std::shared_ptr<GeomAPI_Vertex>& theV2);
107
108   /// \brief Creates faces with holes from wires.
109   /// \param[in] theWires base wires.
110   /// \param[out] theFaces resulting faces.
111   GEOMALGOAPI_EXPORT static void makeFacesWithHoles(const std::shared_ptr<GeomAPI_Pnt> theOrigin,
112                                                 const std::shared_ptr<GeomAPI_Dir> theDirection,
113                                                 const ListOfShape& theWires,
114                                                 ListOfShape& theFaces);
115
116   /// \brief Return a plane for list of shapes if they are all planar.
117   /// \param[in] theShapes shapes to find plane.
118   /// \return plane where all shapes lie or empty ptr if they not planar.
119   GEOMALGOAPI_EXPORT static std::shared_ptr<GeomAPI_Pln> findPlane(const ListOfShape& theShapes);
120
121   /// \brief Checks that vertex/edge is inside face or vertext inside wire.
122   /// \param[in] theSubShape shape that should be inside.
123   /// \param[in] theBaseShape base shape.
124   /// \return true if edge inside the face.
125   GEOMALGOAPI_EXPORT static bool isSubShapeInsideShape(
126     const std::shared_ptr<GeomAPI_Shape> theSubShape,
127     const std::shared_ptr<GeomAPI_Shape> theBaseShape);
128
129   /// \return true if theShape is valid.
130   GEOMALGOAPI_EXPORT static bool isShapeValid(const std::shared_ptr<GeomAPI_Shape> theShape);
131
132   /// \return outer wire for face. If theShape has different type returns empty pointer.
133   GEOMALGOAPI_EXPORT static
134     std::shared_ptr<GeomAPI_Shape> getFaceOuterWire(const std::shared_ptr<GeomAPI_Shape> theFace);
135
136   /// \return true if edge is parallel to face.
137   GEOMALGOAPI_EXPORT static bool isParallel(const std::shared_ptr<GeomAPI_Edge> theEdge,
138                                             const std::shared_ptr<GeomAPI_Face> theFace);
139
140   // Computes intersection point between the edge curve and a face surface (only one point, with
141   // preferences to point that belongs to edge and face boundaries.
142   /// \returns null if there is no intersection
143   GEOMALGOAPI_EXPORT static std::list<std::shared_ptr<GeomAPI_Vertex> > intersect(
144     const std::shared_ptr<GeomAPI_Edge> theEdge, const std::shared_ptr<GeomAPI_Face> theFace,
145     const bool thePointsOutsideFace);
146
147   typedef std::map<std::shared_ptr<GeomAPI_Pnt>,
148                    std::pair<std::list<std::shared_ptr<GeomDataAPI_Point2D> >,
149                              std::list<std::shared_ptr<ModelAPI_Object> > > > PointToRefsMap;
150   /// \brief Performs the split of the shape by points.
151   /// \param[in] theBaseShape shape that should be splitted.
152   /// \param[in] thePoints container of points to split
153   /// \param[out] theShapes container of shapes after split
154   GEOMALGOAPI_EXPORT static void splitShape(const std::shared_ptr<GeomAPI_Shape>& theBaseShape,
155                                       const PointToRefsMap& thePointsInfo,
156                                       std::set<std::shared_ptr<GeomAPI_Shape> >& theShapes);
157
158   /// \brief Performs the split of the shape by points.
159   /// \param[in] theBaseShape shape that should be splitted.
160   /// \param[in] thePoints container of points to split
161   /// \param[out] theShapes container of shapes after split
162   GEOMALGOAPI_EXPORT static void splitShape_p(const std::shared_ptr<GeomAPI_Shape>& theBaseShape,
163                                       const std::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints,
164                                       std::set<std::shared_ptr<GeomAPI_Shape> >& theShapes);
165
166   GEOMALGOAPI_EXPORT static std::shared_ptr<GeomAPI_Shape> findShape(
167                                     const std::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints,
168                                     const std::set<std::shared_ptr<GeomAPI_Shape> >& theShapes);
169
170   /// \brief Returns a dir from a shape and an axis.
171   /// \param[in] theBaseShape shape whose center of mass serves as the starting point of the dir.
172   /// \param[in] theAxis axis that serves as a direction for the dir
173   /// \return dir that builds from center of mass of the base shape and the axis
174   GEOMALGOAPI_EXPORT static std::shared_ptr<GeomAPI_Dir> buildDirFromAxisAndShape(
175                                     const std::shared_ptr<GeomAPI_Shape> theBaseShape,
176                                     const std::shared_ptr<GeomAPI_Ax1> theAxis);
177 };
178
179 #endif