Salome HOME
1b0327e46644d22b0b2a759c82673a6c8d3310ef
[modules/geom.git] / src / GEOMUtils / GEOMUtils.hxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #ifndef _GEOMUtils_HXX_
24 #define _GEOMUtils_HXX_
25
26 #include <Standard_Macro.hxx>
27 #include <TopoDS_Shape.hxx>
28 #include <TopoDS_Vertex.hxx>
29
30 #include <TopTools_ListOfShape.hxx>
31
32 #include <TopAbs.hxx>
33
34 #include <gp_Ax3.hxx>
35 #include <gp_Vec.hxx>
36
37 #include <NCollection_DataMap.hxx>
38
39 #include <functional>
40
41 class Bnd_Box;
42
43 inline Standard_Boolean IsEqual (const TopoDS_Shape& S1, const TopoDS_Shape& S2)
44 {
45   return S1.IsSame(S2);
46 }
47
48 class GEOMUtils {
49
50  public:
51   /*!
52    * \brief Get Local Coordinate System, corresponding to the given shape.
53    *
54    * Origin of the LCS is situated at the shape's center of mass.
55    * Axes of the LCS are obtained from shape's location or,
56    * if the shape is a planar face, from position of its plane.
57    */
58   Standard_EXPORT static gp_Ax3 GetPosition (const TopoDS_Shape& theShape);
59
60   /*!
61    * \brief Get vector, defined by the given edge.
62    * \param theShape The edge.
63    * \param doConsiderOrientation If True, take into account the edge orientation.
64    * \note It is recommended to use doConsiderOrientation=Standard_False, because
65    *       the same edge can have different orientation depending on the way it was
66    *       extracted from a shape.
67    */
68   Standard_EXPORT static gp_Vec GetVector (const TopoDS_Shape& theShape,
69                                            Standard_Boolean doConsiderOrientation);
70
71   /*!
72    * \brief Sort shapes in the list by their coordinates.
73    * \param SL The list of shapes to sort.
74    */
75   struct CompareShapes : public std::binary_function<TopoDS_Shape, TopoDS_Shape, bool>
76   {
77     CompareShapes (bool isOldSorting)
78       : myIsOldSorting(isOldSorting) {}
79
80     bool operator() (const TopoDS_Shape& lhs, const TopoDS_Shape& rhs);
81
82     typedef NCollection_DataMap<TopoDS_Shape, std::pair<double, double> > GEOMUtils_DataMapOfShapeDouble;
83     GEOMUtils_DataMapOfShapeDouble myMap;
84     bool myIsOldSorting;
85   };
86
87   /*!
88    * \brief Sort shapes by their centers of mass, using formula X*999 + Y*99 + Z*0.9
89    */
90   Standard_EXPORT static void SortShapes (TopTools_ListOfShape& SL,
91                                           const Standard_Boolean isOldSorting = Standard_True);
92
93   /*!
94    * \brief Convert TopoDS_COMPSOLID to TopoDS_COMPOUND.
95    *
96    * If the argument shape is not of type TopoDS_COMPSOLID, this method returns it as is.
97    *
98    * \param theCompsolid The compsolid to be converted.
99    * \retval TopoDS_Shape Returns the resulting compound.
100    */
101   Standard_EXPORT static TopoDS_Shape CompsolidToCompound (const TopoDS_Shape& theCompsolid);
102
103   /*!
104    * \brief Recursively extract all shapes from compounds and compsolids of the given shape into theList.
105    *
106    * If theShape is not compound or compsolid, theList will contain only theShape itself.
107    *
108    * \param theShape The shape to be exploded.
109    * \param theList Output parameter.
110    */
111   Standard_EXPORT static void AddSimpleShapes (const TopoDS_Shape& theShape,
112                                                TopTools_ListOfShape& theList);
113
114   /*!
115    * \brief Build a triangulation on \a theShape if it is absent.
116    * \param theShape The shape to check/build triangulation on.
117    * \retval bool Returns false if the shape has no faces, i.e. impossible to build triangulation.
118    */
119   Standard_EXPORT static bool CheckTriangulation (const TopoDS_Shape& theShape);
120
121   /*!
122    * \brief Return type of shape for explode. In case of compound it will be a type of its first sub shape.
123    * \param theShape The shape to get type of.
124    * \retval TopAbs_ShapeEnum Return type of shape for explode.
125    */
126   Standard_EXPORT static TopAbs_ShapeEnum GetTypeOfSimplePart (const TopoDS_Shape& theShape);
127
128   /*!
129    * \brief Find an edge of theShape, closest to thePoint.
130    *
131    * \param theShape The shape to explore.
132    * \param thePoint The point near the required edge.
133    * \retval TopoDS_Shape Returns the found edge or an empty shape if multiple edges found.
134    */
135   Standard_EXPORT static TopoDS_Shape GetEdgeNearPoint (const TopoDS_Shape&  theShape,
136                                                         const TopoDS_Vertex& thePoint);
137
138   /*!
139    * \brief Compute precise bounding box of the shape based on the rough bounding box.
140    *
141    * \param theShape the shape.
142    * \param theBox rough bounding box on input; precise bounding box on output.
143    * \retval Standard_True in case of success; Standard_False otherwise.
144    */
145   Standard_EXPORT static Standard_Boolean PreciseBoundingBox
146                           (const TopoDS_Shape &theShape, Bnd_Box &theBox);
147
148   /*!
149    * \brief Computes minumal distance between two shapes for singular cases
150    *        (workaround for bugs 19899, 19908 and 19910 from Mantis).
151    *
152    * \param aSh1 the first shape
153    * \param aSh2 the second shape
154    * \param Ptmp1 the output result point on the first shape
155    * \param Ptmp2 the output result point on the second shape
156    * \retval negative value if it is not a singular case; actual distance for singular case.
157    */
158   Standard_EXPORT static Standard_Real GetMinDistanceSingular
159                                (const TopoDS_Shape& aSh1,
160                                 const TopoDS_Shape& aSh2,
161                                 gp_Pnt& Ptmp1, gp_Pnt& Ptmp2);
162
163   /*!
164    * \brief Computes minumal distance between two shapes.
165    *
166    * \param theShape1 the first shape
167    * \param theShape2 the second shape
168    * \param thePnt1 the output result point on the first shape
169    * \param thePnt2 the output result point on the second shape
170    * \retval negative value in case of failure; otherwise the real distance.
171    */
172   Standard_EXPORT static Standard_Real GetMinDistance
173                                (const TopoDS_Shape& theShape1,
174                                 const TopoDS_Shape& theShape2,
175                                 gp_Pnt& thePnt1, gp_Pnt& thePnt2);
176
177 };
178
179 #endif