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