1 // Copyright (C) 2014-2022 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>
27 #include <GeomAPI_Pnt.h>
38 class GeomAPI_PlanarEdges;
42 class GeomDataAPI_Point2D;
43 class ModelAPI_Object;
45 /// \class GeomAlgoAPI_ShapeTools
47 /// \brief Useful tools for working with shapes.
48 class GeomAlgoAPI_ShapeTools
51 /// \return the lenth of the edges of the current shape or 0.0 if it can be computed.
52 GEOMALGOAPI_EXPORT static double length(const std::shared_ptr<GeomAPI_Shape> theShape);
54 /// \return the total volume of the solids of the current shape or 0.0 if it can be computed.
55 GEOMALGOAPI_EXPORT static double volume(const std::shared_ptr<GeomAPI_Shape> theShape);
57 /// \return the total area of the faces of the current shape or 0.0 if it can be computed.
58 GEOMALGOAPI_EXPORT static double area(const std::shared_ptr<GeomAPI_Shape> theShape);
60 /// indicate if two faces are continuous
61 /// with an angular tolerance used for G1 continuity to compare the angle between the normals
62 /// \param theFace1 the first face
63 /// \param theFace2 the second face
64 /// \param thePoint the point for the normal
65 /// \param theAngle the angular tolerance
66 /// \param theError error
67 GEOMALGOAPI_EXPORT static bool isContinuousFaces(const GeomShapePtr& theFace1,
68 const GeomShapePtr& theFace2,
69 const GeomPointPtr& thePoint,
70 const double & theAngle,
71 std::string& theError);
73 /// \return the center of mass of the current face.
74 /// The coordinates returned for the center of mass
75 /// are expressed in the absolute Cartesian coordinate system.
76 /// (This function works only for surfaces).
77 GEOMALGOAPI_EXPORT static std::shared_ptr<GeomAPI_Pnt>
78 centreOfMass(const std::shared_ptr<GeomAPI_Shape> theShape);
80 /// \brief Obtain radius of cylindrical face.
81 /// Return negative value if the face is not a cylinder
82 GEOMALGOAPI_EXPORT static double radius(const std::shared_ptr<GeomAPI_Face>& theCylinder);
84 /// \brief Calculate minimal distance between shapes
85 GEOMALGOAPI_EXPORT static double minimalDistance(const GeomShapePtr& theShape1,
86 const GeomShapePtr& theShape2);
87 GEOMALGOAPI_EXPORT static double minimalDistance(const GeomShapePtr& theShape1,
88 const GeomShapePtr& theShape2,
89 std::array<double, 3> & fromShape1To2);
91 /// \brief Calculate maximal value of all possible distances between shapes.
92 /// The shapes can be two edges or two faces only.
93 GEOMALGOAPI_EXPORT static double shapeProximity(const GeomShapePtr& theShape1,
94 const GeomShapePtr& theShape2);
96 /// \brief Combines faces with common edges to shells, or solids to compsolids.
97 /// \param[in] theCompound compound of shapes.
98 /// \param[in] theType type of combine.
99 /// \param[out] theResults resulting shapes.
100 GEOMALGOAPI_EXPORT static std::shared_ptr<GeomAPI_Shape> combineShapes(
101 const std::shared_ptr<GeomAPI_Shape> theCompound,
102 const GeomAPI_Shape::ShapeType theType, ListOfShape& theResults);
104 /// \brief Groups shapes with shared topology to compounds.
105 /// \param[in] theCompound compound of shapes.
106 /// \return compound of compounds with shared topology.
107 GEOMALGOAPI_EXPORT static std::shared_ptr<GeomAPI_Shape>
108 groupSharedTopology(const std::shared_ptr<GeomAPI_Shape> theCompound);
110 /// \brief Check group of shapes has shared sub-shapes of the given type
111 /// \param[in] theShapes list of shapes
112 /// \param[in] theShapeType type of sub-shapes to check
113 /// \return \c true if shared topology exists
114 GEOMALGOAPI_EXPORT static bool hasSharedTopology(const ListOfShape& theShapes,
115 const GeomAPI_Shape::ShapeType theShapeType);
117 /// \brief Calculates bounding box for theShapes
118 /// \return list of eight points.
119 /// \param[in] theShapes list of shapes.
120 /// \param[in] theEnlarge enlarges bounding box size.
121 GEOMALGOAPI_EXPORT static
122 std::list<std::shared_ptr<GeomAPI_Pnt> > getBoundingBox(const ListOfShape& theShapes,
123 const double theEnlarge = 0.0);
125 /// \brief Enlarges or reduces plane to fit bounding box.
126 /// \return plane that fits to bounding box.
127 /// \param[in] thePlane base plane.
128 /// \param[in] thePoints bounding box points (should be eight).
129 GEOMALGOAPI_EXPORT static std::shared_ptr<GeomAPI_Face> fitPlaneToBox(
130 const std::shared_ptr<GeomAPI_Shape> thePlane,
131 const std::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints);
133 /// \brief Finds the start and end vertices of theShape. theShape can be of the following type:\n
134 /// Vertex: theV1 and theV2 are the same and equal to theShape;\n
135 /// Edge : theV1 is start and theV2 is end vertex;\n
136 /// Wire : theV1 is start vertex of the first edge, theV2 is end vertex of the last edge. If wire
137 /// contains no edges theV1 and theV2 are nullified.\n
138 /// If none of the above theV1 and theV2 are nullified.
139 GEOMALGOAPI_EXPORT static void findBounds(const std::shared_ptr<GeomAPI_Shape> theShape,
140 std::shared_ptr<GeomAPI_Vertex>& theV1,
141 std::shared_ptr<GeomAPI_Vertex>& theV2);
143 /// \brief Creates faces with holes from wires.
144 /// \param[in] theWires base wires.
145 /// \param[out] theFaces resulting faces.
146 GEOMALGOAPI_EXPORT static void makeFacesWithHoles(const std::shared_ptr<GeomAPI_Pnt> theOrigin,
147 const std::shared_ptr<GeomAPI_Dir> theDirection,
148 const ListOfShape& theWires,
149 ListOfShape& theFaces);
151 /// \brief Return a plane for list of shapes if they are all planar.
152 /// \param[in] theShapes shapes to find plane.
153 /// \return plane where all shapes lie or empty pointer if they not planar.
154 GEOMALGOAPI_EXPORT static std::shared_ptr<GeomAPI_Pln> findPlane(const ListOfShape& theShapes);
156 /// \brief Checks that vertex/edge is inside face or vertex inside wire.
157 /// \param[in] theSubShape shape that should be inside.
158 /// \param[in] theBaseShape base shape.
159 /// \return true if edge inside the face.
160 GEOMALGOAPI_EXPORT static bool isSubShapeInsideShape(
161 const std::shared_ptr<GeomAPI_Shape> theSubShape,
162 const std::shared_ptr<GeomAPI_Shape> theBaseShape);
164 /// \return true if theShape is valid.
165 GEOMALGOAPI_EXPORT static bool isShapeValid(const std::shared_ptr<GeomAPI_Shape> theShape);
167 /// \return outer wire for face. If theShape has different type returns empty pointer.
168 GEOMALGOAPI_EXPORT static
169 std::shared_ptr<GeomAPI_Shape> getFaceOuterWire(const std::shared_ptr<GeomAPI_Shape> theFace);
171 /// \return \c true if edges are tangent in the specified point
172 GEOMALGOAPI_EXPORT static bool isTangent(const std::shared_ptr<GeomAPI_Edge> theEdge1,
173 const std::shared_ptr<GeomAPI_Edge> theEdge2,
174 const std::shared_ptr<GeomAPI_Vertex> theTgPoint);
176 /// \return true if edge is parallel to face.
177 GEOMALGOAPI_EXPORT static bool isParallel(const std::shared_ptr<GeomAPI_Edge> theEdge,
178 const std::shared_ptr<GeomAPI_Face> theFace);
180 /// Computes intersection point between the edge curve and a face surface (only one point, with
181 /// preferences to point that belongs to edge and face boundaries.
182 /// \returns null if there is no intersection
183 GEOMALGOAPI_EXPORT static std::list<std::shared_ptr<GeomAPI_Vertex> > intersect(
184 const std::shared_ptr<GeomAPI_Edge> theEdge, const std::shared_ptr<GeomAPI_Face> theFace);
186 typedef std::map<std::shared_ptr<GeomAPI_Pnt>,
187 std::pair<std::list<std::shared_ptr<GeomDataAPI_Point2D> >,
188 std::list<std::shared_ptr<ModelAPI_Object> > > > PointToRefsMap;
189 /// \brief Performs the split of the shape by points.
190 /// \param[in] theBaseShape shape that should be divided.
191 /// \param[in] thePoints container of points to split
192 /// \param[out] theShapes container of shapes after split
193 GEOMALGOAPI_EXPORT static void splitShape(const std::shared_ptr<GeomAPI_Shape>& theBaseShape,
194 const PointToRefsMap& thePointsInfo,
195 std::set<std::shared_ptr<GeomAPI_Shape> >& theShapes);
197 /// \brief Performs the split of the shape by points.
198 /// \param[in] theBaseShape shape that should be divided.
199 /// \param[in] thePoints container of points to split
200 /// \param[out] theShapes container of shapes after split
201 GEOMALGOAPI_EXPORT static void splitShape_p(const std::shared_ptr<GeomAPI_Shape>& theBaseShape,
202 const std::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints,
203 std::set<std::shared_ptr<GeomAPI_Shape> >& theShapes);
205 GEOMALGOAPI_EXPORT static std::shared_ptr<GeomAPI_Shape> findShape(
206 const std::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints,
207 const std::set<std::shared_ptr<GeomAPI_Shape> >& theShapes);
209 #ifdef FEATURE_MULTIROTATION_TWO_DIRECTIONS
210 /// \brief Returns a direction from a shape and an axis.
211 /// \param[in] theBaseShape shape whose center of mass serves as the starting point of the dir.
212 /// \param[in] theAxis axis that serves as a direction for the dir
213 /// \return dir that builds from center of mass of the base shape and the axis
214 GEOMALGOAPI_EXPORT static std::shared_ptr<GeomAPI_Dir> buildDirFromAxisAndShape(
215 const std::shared_ptr<GeomAPI_Shape> theBaseShape,
216 const std::shared_ptr<GeomAPI_Ax1> theAxis);
219 /// \brief Re-approximate a wire to build a single edge
220 GEOMALGOAPI_EXPORT static std::shared_ptr<GeomAPI_Edge> wireToEdge(
221 const std::shared_ptr<GeomAPI_Wire>& theWire);
223 /// \brief Get non-composite sub-shapes of the given shape.
224 /// \param[in] theShape shape that should be exploded
225 /// \return list of sub-shapes (vertices, edges, faces, solids)
226 GEOMALGOAPI_EXPORT static ListOfShape getLowLevelSubShapes(const GeomShapePtr& theShape);
228 /// \brief Calculate prism sizes to ensure that it passes through all objects
229 /// \param[in] theObjects objects to be joined/cutted by the prism
230 /// \param[in] theBaseShapes bases of the prism
231 /// \param[in] theDir direction of the prism
232 /// \param[out] theToSize upper offset of the prism
233 /// \param[out] theFromSize lower offset of the prism
234 GEOMALGOAPI_EXPORT static void computeThroughAll(const ListOfShape& theObjects,
235 const ListOfShape& theBaseShapes,
236 const std::shared_ptr<GeomAPI_Dir> theDir,
237 double& theToSize, double& theFromSize);
239 /// \brief Get shared faces of a shape
240 /// \param[in] theShape shape that should be exploded
241 /// \return list of shared faces
242 GEOMALGOAPI_EXPORT static ListOfShape getSharedFaces(const GeomShapePtr& theShape);