Salome HOME
64c34bd857040dc3b7b1a0bc9feb3d513e1dc4c7
[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 <TopoDS_Shape.hxx>
27 #include <TopoDS_Vertex.hxx>
28
29 #include <TopTools_ListOfShape.hxx>
30
31 #include <TopAbs.hxx>
32
33 #include <gp_Ax3.hxx>
34 #include <gp_Vec.hxx>
35
36 #include <NCollection_DataMap.hxx>
37
38 #include <functional>
39
40 inline Standard_Boolean IsEqual (const TopoDS_Shape& S1, const TopoDS_Shape& S2)
41 {
42   return S1.IsSame(S2);
43 }
44
45 class GEOMUtils {
46
47  public:
48   /*!
49    * \brief Get Local Coordinate System, corresponding to the given shape.
50    *
51    * Origin of the LCS is situated at the shape's center of mass.
52    * Axes of the LCS are obtained from shape's location or,
53    * if the shape is a planar face, from position of its plane.
54    */
55   Standard_EXPORT static gp_Ax3 GetPosition (const TopoDS_Shape& theShape);
56
57   /*!
58    * \brief Get vector, defined by the given edge.
59    * \param theShape The edge.
60    * \param doConsiderOrientation If True, take into account the edge orientation.
61    * \note It is recommended to use doConsiderOrientation=Standard_False, because
62    *       the same edge can have different orientation depending on the way it was
63    *       extracted from a shape.
64    */
65   Standard_EXPORT static gp_Vec GetVector (const TopoDS_Shape& theShape,
66                                            Standard_Boolean doConsiderOrientation);
67
68   /*!
69    * \brief Sort shapes in the list by their coordinates.
70    * \param SL The list of shapes to sort.
71    */
72   struct CompareShapes : public std::binary_function<TopoDS_Shape, TopoDS_Shape, bool>
73   {
74     CompareShapes (bool isOldSorting)
75       : myIsOldSorting(isOldSorting) {}
76
77     bool operator() (const TopoDS_Shape& lhs, const TopoDS_Shape& rhs);
78
79     typedef NCollection_DataMap<TopoDS_Shape, std::pair<double, double> > GEOMUtils_DataMapOfShapeDouble;
80     GEOMUtils_DataMapOfShapeDouble myMap;
81     bool myIsOldSorting;
82   };
83
84   /*!
85    * \brief Sort shapes by their centers of mass, using formula X*999 + Y*99 + Z*0.9
86    */
87   Standard_EXPORT static void SortShapes (TopTools_ListOfShape& SL,
88                                           const Standard_Boolean isOldSorting = Standard_True);
89
90   /*!
91    * \brief Convert TopoDS_COMPSOLID to TopoDS_COMPOUND.
92    *
93    * If the argument shape is not of type TopoDS_COMPSOLID, this method returns it as is.
94    *
95    * \param theCompsolid The compsolid to be converted.
96    * \retval TopoDS_Shape Returns the resulting compound.
97    */
98   Standard_EXPORT static TopoDS_Shape CompsolidToCompound (const TopoDS_Shape& theCompsolid);
99
100   /*!
101    * \brief Recursively extract all shapes from compounds and compsolids of the given shape into theList.
102    *
103    * If theShape is not compound or compsolid, theList will contain only theShape itself.
104    *
105    * \param theShape The shape to be exploded.
106    * \param theList Output parameter.
107    */
108   Standard_EXPORT static void AddSimpleShapes (const TopoDS_Shape& theShape,
109                                                TopTools_ListOfShape& theList);
110
111   /*!
112    * \brief Build a triangulation on \a theShape if it is absent.
113    * \param theShape The shape to check/build triangulation on.
114    * \retval bool Returns false if the shape has no faces, i.e. impossible to build triangulation.
115    */
116   Standard_EXPORT static bool CheckTriangulation (const TopoDS_Shape& theShape);
117
118   /*!
119    * \brief Return type of shape for explode. In case of compound it will be a type of its first sub shape.
120    * \param theShape The shape to get type of.
121    * \retval TopAbs_ShapeEnum Return type of shape for explode.
122    */
123   Standard_EXPORT static TopAbs_ShapeEnum GetTypeOfSimplePart (const TopoDS_Shape& theShape);
124
125   /*!
126    * \brief Find an edge of theShape, closest to thePoint.
127    *
128    * \param theShape The shape to explore.
129    * \param thePoint The point near the required edge.
130    * \retval TopoDS_Shape Returns the found edge or an empty shape if multiple edges found.
131    */
132   Standard_EXPORT static TopoDS_Shape GetEdgeNearPoint (const TopoDS_Shape&  theShape,
133                                                         const TopoDS_Vertex& thePoint);
134
135 };
136
137 #endif