Salome HOME
0022616: [CEA 1038] Improve the quality of stl and vtk exports
[modules/geom.git] / src / GEOMUtils / GEOMUtils.hxx
1 // Copyright (C) 2007-2014  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, or (at your option) any later version.
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 <V3d_View.hxx>
38
39 #include <NCollection_DataMap.hxx>
40
41 #include <functional>
42
43 #include <map>
44 #include <vector>
45 #include <string>
46 #include <utility>
47
48 class Bnd_Box;
49
50 inline Standard_Boolean IsEqual (const TopoDS_Shape& S1, const TopoDS_Shape& S2)
51 {
52   return S1.IsSame(S2);
53 }
54
55 namespace GEOMUtils
56 {
57
58   typedef std::vector<std::string> NodeLinks;
59   typedef std::map<std::string, NodeLinks> LevelInfo;
60   typedef std::vector<LevelInfo> LevelsList;
61   typedef std::map<std::string,std::pair<LevelsList,LevelsList> > TreeModel;
62
63   /*!
64    * \brief Get Local Coordinate System, corresponding to the given shape.
65    *
66    * Origin of the LCS is situated at the shape's center of mass.
67    * Axes of the LCS are obtained from shape's location or,
68    * if the shape is a planar face, from position of its plane.
69    */
70   Standard_EXPORT gp_Ax3 GetPosition (const TopoDS_Shape& theShape);
71
72   /*!
73    * \brief Get vector, defined by the given edge.
74    * \param theShape The edge.
75    * \param doConsiderOrientation If True, take into account the edge orientation.
76    * \note It is recommended to use doConsiderOrientation=Standard_False, because
77    *       the same edge can have different orientation depending on the way it was
78    *       extracted from a shape.
79    */
80   Standard_EXPORT gp_Vec GetVector (const TopoDS_Shape& theShape,
81                                     Standard_Boolean doConsiderOrientation);
82
83   /*!
84    * \brief Sort shapes in the list by their coordinates.
85    * \param SL The list of shapes to sort.
86    */
87   struct CompareShapes : public std::binary_function<TopoDS_Shape, TopoDS_Shape, bool>
88   {
89     CompareShapes (bool isOldSorting)
90       : myIsOldSorting(isOldSorting) {}
91
92     bool operator() (const TopoDS_Shape& lhs, const TopoDS_Shape& rhs);
93
94     typedef NCollection_DataMap<TopoDS_Shape, std::pair<double, double> > GEOMUtils_DataMapOfShapeDouble;
95     GEOMUtils_DataMapOfShapeDouble myMap;
96     bool myIsOldSorting;
97   };
98
99   /*!
100    * \brief Sort shapes by their centers of mass, using formula X*999 + Y*99 + Z*0.9
101    */
102   Standard_EXPORT void SortShapes (TopTools_ListOfShape& SL,
103                                    const Standard_Boolean isOldSorting = Standard_True);
104
105   /*!
106    * \brief Convert TopoDS_COMPSOLID to TopoDS_COMPOUND.
107    *
108    * If the argument shape is not of type TopoDS_COMPSOLID, this method returns it as is.
109    *
110    * \param theCompsolid The compsolid to be converted.
111    * \retval TopoDS_Shape Returns the resulting compound.
112    */
113   Standard_EXPORT TopoDS_Shape CompsolidToCompound (const TopoDS_Shape& theCompsolid);
114
115   /*!
116    * \brief Recursively extract all shapes from compounds and compsolids of the given shape into theList.
117    *
118    * If theShape is not compound or compsolid, theList will contain only theShape itself.
119    *
120    * \param theShape The shape to be exploded.
121    * \param theList Output parameter.
122    */
123   Standard_EXPORT void AddSimpleShapes (const TopoDS_Shape& theShape,
124                                         TopTools_ListOfShape& theList);
125
126   /*!
127    * \brief Build a triangulation on \a theShape if it is absent.
128    * \param theShape The shape to check/build triangulation on.
129    * \retval bool Returns false if the shape has no faces, i.e. impossible to build triangulation.
130    */
131   Standard_EXPORT bool CheckTriangulation (const TopoDS_Shape& theShape);
132   
133   /*!
134    * \brief Return type of shape for explode. In case of compound it will be a type of its first sub shape.
135    * \param theShape The shape to get type of.
136    * \retval TopAbs_ShapeEnum Return type of shape for explode.
137    */
138   Standard_EXPORT TopAbs_ShapeEnum GetTypeOfSimplePart (const TopoDS_Shape& theShape);
139
140   /*!
141    * \brief Find an edge of theShape, closest to thePoint.
142    *
143    * \param theShape The shape to explore.
144    * \param thePoint The point near the required edge.
145    * \retval TopoDS_Shape Returns the found edge or an empty shape if multiple edges found.
146    */
147   Standard_EXPORT TopoDS_Shape GetEdgeNearPoint (const TopoDS_Shape&  theShape,
148                                                  const TopoDS_Vertex& thePoint);
149
150   /*!
151    * \brief Compute precise bounding box of the shape based on the rough bounding box.
152    *
153    * \param theShape the shape.
154    * \param theBox rough bounding box on input; precise bounding box on output.
155    * \retval Standard_True in case of success; Standard_False otherwise.
156    */
157   Standard_EXPORT Standard_Boolean PreciseBoundingBox(const TopoDS_Shape &theShape, Bnd_Box &theBox);
158
159   /*!
160    * \brief Computes minumal distance between two shapes for singular cases
161    *        (workaround for bugs 19899, 19908 and 19910 from Mantis).
162    *
163    * \param aSh1 the first shape
164    * \param aSh2 the second shape
165    * \param Ptmp1 the output result point on the first shape
166    * \param Ptmp2 the output result point on the second shape
167    * \retval negative value if it is not a singular case; actual distance for singular case.
168    */
169   Standard_EXPORT Standard_Real GetMinDistanceSingular(const TopoDS_Shape& aSh1,
170                                                        const TopoDS_Shape& aSh2,
171                                                        gp_Pnt& Ptmp1, gp_Pnt& Ptmp2);
172   
173   /*!
174    * \brief Computes minumal distance between two shapes.
175    *
176    * \param theShape1 the first shape
177    * \param theShape2 the second shape
178    * \param thePnt1 the output result point on the first shape
179    * \param thePnt2 the output result point on the second shape
180    * \retval negative value in case of failure; otherwise the real distance.
181    */
182   Standard_EXPORT Standard_Real GetMinDistance(const TopoDS_Shape& theShape1,
183                                                const TopoDS_Shape& theShape2,
184                                                gp_Pnt& thePnt1, gp_Pnt& thePnt2);
185   
186   /*!
187    * \brief Returns the point clicked in 3D view.
188    *
189    * \param x The X coordinate in the view.
190    * \param y The Y coordinate in the view.
191    * \param theView View where the given point takes place.
192    * \retval gp_Pnt Returns the point clicked in 3D view
193    */
194   Standard_EXPORT gp_Pnt ConvertClickToPoint( int x, int y, Handle(V3d_View) theView );
195
196   Standard_EXPORT void ConvertTreeToString( const TreeModel &theTree,
197                                             std::string &DependencyStr );
198
199   Standard_EXPORT void ConvertStringToTree( const std::string &theDependencyStr,
200                                             TreeModel &tree );
201
202 };
203
204 #endif