Salome HOME
b1f154948890d96c8de783aec26bd0c4a4ff70ee
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_ShapeTools.h
1 // Copyright (C) 2014-2021  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 GeomAlgoAPI_ShapeTools_H_
21 #define GeomAlgoAPI_ShapeTools_H_
22
23 #include "GeomAlgoAPI.h"
24
25 #include <GeomAPI_Shape.h>
26 #include <GeomAPI_Vertex.h>
27 #include <map>
28 #include <set>
29 #ifdef WIN32
30  #include<array>
31 #endif WIN32
32
33 class GeomAPI_Ax1;
34 class GeomAPI_Edge;
35 class GeomAPI_Dir;
36 class GeomAPI_Face;
37 class GeomAPI_PlanarEdges;
38 class GeomAPI_Pln;
39 class GeomAPI_Pnt;
40 class GeomAPI_Wire;
41 class GeomDataAPI_Point2D;
42 class ModelAPI_Object;
43
44 /// \class GeomAlgoAPI_ShapeTools
45 /// \ingroup DataAlgo
46 /// \brief Useful tools for working with shapes.
47 class GeomAlgoAPI_ShapeTools
48 {
49 public:
50   /// \return the lenth of the edges of the current shape or 0.0 if it can be computed.
51   GEOMALGOAPI_EXPORT static double length(const std::shared_ptr<GeomAPI_Shape> theShape);
52
53   /// \return the total volume of the solids of the current shape or 0.0 if it can be computed.
54   GEOMALGOAPI_EXPORT static double volume(const std::shared_ptr<GeomAPI_Shape> theShape);
55
56   /// \return the total area of the faces of the current shape or 0.0 if it can be computed.
57   GEOMALGOAPI_EXPORT static double area(const std::shared_ptr<GeomAPI_Shape> theShape);
58
59   /// \return the center of mass of the current face.
60   /// The coordinates returned for the center of mass
61   /// are expressed in the absolute Cartesian coordinate system.
62   /// (This function works only for surfaces).
63   GEOMALGOAPI_EXPORT static std::shared_ptr<GeomAPI_Pnt>
64     centreOfMass(const std::shared_ptr<GeomAPI_Shape> theShape);
65
66   /// \brief Obtain radius of cylindrical face.
67   ///        Return negative value if the face is not a cylinder
68   GEOMALGOAPI_EXPORT static double radius(const std::shared_ptr<GeomAPI_Face>& theCylinder);
69
70   /// \brief Calculate minimal distance between shapes
71   GEOMALGOAPI_EXPORT static double minimalDistance(const GeomShapePtr& theShape1,
72                                                    const GeomShapePtr& theShape2);
73   GEOMALGOAPI_EXPORT static double minimalDistance(const GeomShapePtr& theShape1,
74                                                    const GeomShapePtr& theShape2,
75                                                    std::array<double, 3> & fromShape1To2);
76
77   /// \brief Combines faces with common edges to shells, or solids to compsolids.
78   /// \param[in] theCompound compound of shapes.
79   /// \param[in] theType type of combine.
80   /// \param[out] theResults resulting shapes.
81   GEOMALGOAPI_EXPORT static std::shared_ptr<GeomAPI_Shape> combineShapes(
82     const std::shared_ptr<GeomAPI_Shape> theCompound,
83     const GeomAPI_Shape::ShapeType theType, ListOfShape& theResults);
84
85   /// \brief Groups shapes with shared topology to compounds.
86   /// \param[in] theCompound compound of shapes.
87   /// \return compound of compounds with shared topology.
88   GEOMALGOAPI_EXPORT static std::shared_ptr<GeomAPI_Shape>
89     groupSharedTopology(const std::shared_ptr<GeomAPI_Shape> theCompound);
90
91   /// \brief Check group of shapes has shared sub-shapes of the given type
92   /// \param[in] theShapes    list of shapes
93   /// \param[in] theShapeType type of sub-shapes to check
94   /// \return \c true if shared topology exists
95   GEOMALGOAPI_EXPORT static bool hasSharedTopology(const ListOfShape& theShapes,
96                                                    const GeomAPI_Shape::ShapeType theShapeType);
97
98   /// \brief Calculates bounding box for theShapes
99   /// \return list of eight points.
100   /// \param[in] theShapes list of shapes.
101   /// \param[in] theEnlarge enlarges bounding box size.
102   GEOMALGOAPI_EXPORT static
103     std::list<std::shared_ptr<GeomAPI_Pnt> > getBoundingBox(const ListOfShape& theShapes,
104                                                             const double theEnlarge = 0.0);
105
106   /// \brief Enlarges or reduces plane to fit bounding box.
107   /// \return plane that fits to bounding box.
108   /// \param[in] thePlane base plane.
109   /// \param[in] thePoints bounding box points (should be eight).
110   GEOMALGOAPI_EXPORT static std::shared_ptr<GeomAPI_Face> fitPlaneToBox(
111     const std::shared_ptr<GeomAPI_Shape> thePlane,
112     const std::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints);
113
114   /// \brief Finds the start and end vertices of theShape. theShape can be of the following type:\n
115   /// Vertex: theV1 and theV2 are the same and equal to theShape;\n
116   /// Edge : theV1 is start and theV2 is end vertex;\n
117   /// Wire : theV1 is start vertex of the first edge, theV2 is end vertex of the last edge. If wire
118   /// contains no edges theV1 and theV2 are nullified.\n
119   /// If none of the above theV1 and theV2 are nullified.
120   GEOMALGOAPI_EXPORT static void findBounds(const std::shared_ptr<GeomAPI_Shape> theShape,
121                                             std::shared_ptr<GeomAPI_Vertex>& theV1,
122                                             std::shared_ptr<GeomAPI_Vertex>& theV2);
123
124   /// \brief Creates faces with holes from wires.
125   /// \param[in] theWires base wires.
126   /// \param[out] theFaces resulting faces.
127   GEOMALGOAPI_EXPORT static void makeFacesWithHoles(const std::shared_ptr<GeomAPI_Pnt> theOrigin,
128                                                 const std::shared_ptr<GeomAPI_Dir> theDirection,
129                                                 const ListOfShape& theWires,
130                                                 ListOfShape& theFaces);
131
132   /// \brief Return a plane for list of shapes if they are all planar.
133   /// \param[in] theShapes shapes to find plane.
134   /// \return plane where all shapes lie or empty pointer if they not planar.
135   GEOMALGOAPI_EXPORT static std::shared_ptr<GeomAPI_Pln> findPlane(const ListOfShape& theShapes);
136
137   /// \brief Checks that vertex/edge is inside face or vertex inside wire.
138   /// \param[in] theSubShape shape that should be inside.
139   /// \param[in] theBaseShape base shape.
140   /// \return true if edge inside the face.
141   GEOMALGOAPI_EXPORT static bool isSubShapeInsideShape(
142     const std::shared_ptr<GeomAPI_Shape> theSubShape,
143     const std::shared_ptr<GeomAPI_Shape> theBaseShape);
144
145   /// \return true if theShape is valid.
146   GEOMALGOAPI_EXPORT static bool isShapeValid(const std::shared_ptr<GeomAPI_Shape> theShape);
147
148   /// \return outer wire for face. If theShape has different type returns empty pointer.
149   GEOMALGOAPI_EXPORT static
150     std::shared_ptr<GeomAPI_Shape> getFaceOuterWire(const std::shared_ptr<GeomAPI_Shape> theFace);
151
152   /// \return \c true if edges are tangent in the specified point
153   GEOMALGOAPI_EXPORT static bool isTangent(const std::shared_ptr<GeomAPI_Edge> theEdge1,
154                                            const std::shared_ptr<GeomAPI_Edge> theEdge2,
155                                            const std::shared_ptr<GeomAPI_Vertex> theTgPoint);
156
157   /// \return true if edge is parallel to face.
158   GEOMALGOAPI_EXPORT static bool isParallel(const std::shared_ptr<GeomAPI_Edge> theEdge,
159                                             const std::shared_ptr<GeomAPI_Face> theFace);
160
161   /// Computes intersection point between the edge curve and a face surface (only one point, with
162   /// preferences to point that belongs to edge and face boundaries.
163   /// \returns null if there is no intersection
164   GEOMALGOAPI_EXPORT static std::list<std::shared_ptr<GeomAPI_Vertex> > intersect(
165     const std::shared_ptr<GeomAPI_Edge> theEdge, const std::shared_ptr<GeomAPI_Face> theFace);
166
167   typedef std::map<std::shared_ptr<GeomAPI_Pnt>,
168                    std::pair<std::list<std::shared_ptr<GeomDataAPI_Point2D> >,
169                              std::list<std::shared_ptr<ModelAPI_Object> > > > PointToRefsMap;
170   /// \brief Performs the split of the shape by points.
171   /// \param[in] theBaseShape shape that should be divided.
172   /// \param[in] thePoints container of points to split
173   /// \param[out] theShapes container of shapes after split
174   GEOMALGOAPI_EXPORT static void splitShape(const std::shared_ptr<GeomAPI_Shape>& theBaseShape,
175                                       const PointToRefsMap& thePointsInfo,
176                                       std::set<std::shared_ptr<GeomAPI_Shape> >& theShapes);
177
178   /// \brief Performs the split of the shape by points.
179   /// \param[in] theBaseShape shape that should be divided.
180   /// \param[in] thePoints container of points to split
181   /// \param[out] theShapes container of shapes after split
182   GEOMALGOAPI_EXPORT static void splitShape_p(const std::shared_ptr<GeomAPI_Shape>& theBaseShape,
183                                       const std::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints,
184                                       std::set<std::shared_ptr<GeomAPI_Shape> >& theShapes);
185
186   GEOMALGOAPI_EXPORT static std::shared_ptr<GeomAPI_Shape> findShape(
187                                     const std::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints,
188                                     const std::set<std::shared_ptr<GeomAPI_Shape> >& theShapes);
189
190 #ifdef FEATURE_MULTIROTATION_TWO_DIRECTIONS
191   /// \brief Returns a direction from a shape and an axis.
192   /// \param[in] theBaseShape shape whose center of mass serves as the starting point of the dir.
193   /// \param[in] theAxis axis that serves as a direction for the dir
194   /// \return dir that builds from center of mass of the base shape and the axis
195   GEOMALGOAPI_EXPORT static std::shared_ptr<GeomAPI_Dir> buildDirFromAxisAndShape(
196                                     const std::shared_ptr<GeomAPI_Shape> theBaseShape,
197                                     const std::shared_ptr<GeomAPI_Ax1> theAxis);
198 #endif
199
200   /// \brief Re-approximate a wire to build a single edge
201   GEOMALGOAPI_EXPORT static std::shared_ptr<GeomAPI_Edge> wireToEdge(
202       const std::shared_ptr<GeomAPI_Wire>& theWire);
203
204   /// \brief Get non-composite sub-shapes of the given shape.
205   /// \param[in] theShape shape that should be exploded
206   /// \return list of sub-shapes (vertices, edges, faces, solids)
207   GEOMALGOAPI_EXPORT static ListOfShape getLowLevelSubShapes(const GeomShapePtr& theShape);
208
209   /// \brief Calculate prism sizes to ensure that it passes through all objects
210   /// \param[in] theObjects objects to be joined/cutted by the prism
211   /// \param[in] theBaseShapes bases of the prism
212   /// \param[in] theDir direction of the prism
213   /// \param[out] theToSize upper offset of the prism
214   /// \param[out] theFromSize lower offset of the prism
215   GEOMALGOAPI_EXPORT static void computeThroughAll(const ListOfShape& theObjects,
216                                                    const ListOfShape& theBaseShapes,
217                                                    const std::shared_ptr<GeomAPI_Dir> theDir,
218                                                    double& theToSize, double& theFromSize);
219 };
220
221 #endif