Salome HOME
Remove unnecessary dependency on VTK from GEOM engine
[modules/geom.git] / src / GEOMUtils / GEOMUtils.hxx
1 // Copyright (C) 2007-2015  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 Compute numerical functor for the shape.
65    *
66    * Resulting value can be used to sort out shapes according to some parameter.
67    * 
68    * Returns a pair of two values (dist, functor) where
69    * - \a dist is a some value that is computed according to the center of mass of given shape;
70    * - \a functor is a numerical functor value
71    *
72    * The numerical functor is computed according to the shape's topological properties as follows:
73    * - orientation for vertices 
74    * - length for edges and wires
75    * - area for faces and shells
76    * - volume for solids, compounds, compsolids
77    *
78    * If \a isOldSorting parameter is set to \c true, for all cases linear properties of the shape
79    * are used (to support backward compatibility in some methods). By default, this parameter is
80    * set to \c false.
81    */
82   Standard_EXPORT std::pair<double, double> ShapeToDouble (const TopoDS_Shape& theShape,
83                                                            bool isOldSorting = false);
84
85   /*!
86    * \brief Get Local Coordinate System, corresponding to the given shape.
87    *
88    * Origin of the LCS is situated at the shape's center of mass.
89    * Axes of the LCS are obtained from shape's location or,
90    * if the shape is a planar face, from position of its plane.
91    */
92   Standard_EXPORT gp_Ax3 GetPosition (const TopoDS_Shape& theShape);
93
94   /*!
95    * \brief Get vector, defined by the given edge.
96    * \param theShape The edge.
97    * \param doConsiderOrientation If True, take into account the edge orientation.
98    * \note It is recommended to use doConsiderOrientation=Standard_False, because
99    *       the same edge can have different orientation depending on the way it was
100    *       extracted from a shape.
101    */
102   Standard_EXPORT gp_Vec GetVector (const TopoDS_Shape& theShape,
103                                     Standard_Boolean doConsiderOrientation);
104
105   /*!
106    * \brief Sort shapes in the list by their coordinates.
107    * \param SL The list of shapes to sort.
108    */
109   struct CompareShapes : public std::binary_function<TopoDS_Shape, TopoDS_Shape, bool>
110   {
111     CompareShapes (bool isOldSorting)
112       : myIsOldSorting(isOldSorting) {}
113
114     bool operator() (const TopoDS_Shape& lhs, const TopoDS_Shape& rhs);
115
116     typedef NCollection_DataMap<TopoDS_Shape, std::pair<double, double> > GEOMUtils_DataMapOfShapeDouble;
117     GEOMUtils_DataMapOfShapeDouble myMap;
118     bool myIsOldSorting;
119   };
120
121   /*!
122    * \brief Sort shapes by their centers of mass, using formula X*999 + Y*99 + Z*0.9
123    */
124   Standard_EXPORT void SortShapes (TopTools_ListOfShape& SL,
125                                    const Standard_Boolean isOldSorting = Standard_True);
126
127   /*!
128    * \brief Convert TopoDS_COMPSOLID to TopoDS_COMPOUND.
129    *
130    * If the argument shape is not of type TopoDS_COMPSOLID, this method returns it as is.
131    *
132    * \param theCompsolid The compsolid to be converted.
133    * \retval TopoDS_Shape Returns the resulting compound.
134    */
135   Standard_EXPORT TopoDS_Shape CompsolidToCompound (const TopoDS_Shape& theCompsolid);
136
137   /*!
138    * \brief Recursively extract all shapes from compounds and compsolids of the given shape into theList.
139    *
140    * If theShape is not compound or compsolid, theList will contain only theShape itself.
141    *
142    * \param theShape The shape to be exploded.
143    * \param theList Output parameter.
144    */
145   Standard_EXPORT void AddSimpleShapes (const TopoDS_Shape& theShape,
146                                         TopTools_ListOfShape& theList);
147
148   /*!
149    * \brief Build a triangulation on \a theShape if it is absent.
150    * \param theShape The shape to check/build triangulation on.
151    * \retval bool Returns false if the shape has no faces, i.e. impossible to build triangulation.
152    */
153   Standard_EXPORT bool CheckTriangulation (const TopoDS_Shape& theShape);
154   
155   /*!
156    * \brief Return type of shape for explode. In case of compound it will be a type of its first sub shape.
157    * \param theShape The shape to get type of.
158    * \retval TopAbs_ShapeEnum Return type of shape for explode.
159    */
160   Standard_EXPORT TopAbs_ShapeEnum GetTypeOfSimplePart (const TopoDS_Shape& theShape);
161
162   /*!
163    * \brief Find an edge of theShape, closest to thePoint.
164    *
165    * \param theShape The shape to explore.
166    * \param thePoint The point near the required edge.
167    * \retval TopoDS_Shape Returns the found edge or an empty shape if multiple edges found.
168    */
169   Standard_EXPORT TopoDS_Shape GetEdgeNearPoint (const TopoDS_Shape&  theShape,
170                                                  const TopoDS_Vertex& thePoint);
171
172   /*!
173    * \brief Compute precise bounding box of the shape based on the rough bounding box.
174    *
175    * \param theShape the shape.
176    * \param theBox rough bounding box on input; precise bounding box on output.
177    * \retval Standard_True in case of success; Standard_False otherwise.
178    */
179   Standard_EXPORT Standard_Boolean PreciseBoundingBox(const TopoDS_Shape &theShape, Bnd_Box &theBox);
180
181   /*!
182    * \brief Computes minumal distance between two shapes for singular cases
183    *        (workaround for bugs 19899, 19908 and 19910 from Mantis).
184    *
185    * \param aSh1 the first shape
186    * \param aSh2 the second shape
187    * \param Ptmp1 the output result point on the first shape
188    * \param Ptmp2 the output result point on the second shape
189    * \retval negative value if it is not a singular case; actual distance for singular case.
190    */
191   Standard_EXPORT Standard_Real GetMinDistanceSingular(const TopoDS_Shape& aSh1,
192                                                        const TopoDS_Shape& aSh2,
193                                                        gp_Pnt& Ptmp1, gp_Pnt& Ptmp2);
194   
195   /*!
196    * \brief Computes minumal distance between two shapes.
197    *
198    * \param theShape1 the first shape
199    * \param theShape2 the second shape
200    * \param thePnt1 the output result point on the first shape
201    * \param thePnt2 the output result point on the second shape
202    * \retval negative value in case of failure; otherwise the real distance.
203    */
204   Standard_EXPORT Standard_Real GetMinDistance(const TopoDS_Shape& theShape1,
205                                                const TopoDS_Shape& theShape2,
206                                                gp_Pnt& thePnt1, gp_Pnt& thePnt2);
207   
208   /*!
209    * \brief Returns the point clicked in 3D view.
210    *
211    * \param x The X coordinate in the view.
212    * \param y The Y coordinate in the view.
213    * \param theView View where the given point takes place.
214    * \retval gp_Pnt Returns the point clicked in 3D view
215    */
216   Standard_EXPORT gp_Pnt ConvertClickToPoint( int x, int y, Handle(V3d_View) theView );
217
218   /*!
219    * \brief Convert dependency tree data to the string representation
220    *
221    * \param tree dependency tree data
222    * \param dependencyStr output string
223    */
224   Standard_EXPORT void ConvertTreeToString( const TreeModel& tree,
225                                             std::string& dependencyStr );
226
227   /*!
228    * \brief Restore dependency tree data from the string representation
229    *
230    * \param dependencyStr string representation of tree data
231    * \param tree output dependency tree data
232    */
233   Standard_EXPORT void ConvertStringToTree( const std::string& dependencyStr,
234                                             TreeModel& tree );
235
236   /*!
237    * \brief Check shape
238    *
239    * \param shape input shape object
240    * \param checkGeometry when set to \c true, causes check of underlying geometry
241    *        in addition to the topology
242    * \return \c true if shape is valid or \c false otherwise
243    */
244   Standard_EXPORT bool CheckShape( TopoDS_Shape& shape, bool checkGeometry = false );
245   
246   /*!
247    * \brief Limit shape tolerance to the given value
248    *
249    * \param shape shape being fixed
250    * \param type topology type which tolerance is to be limited; TopAbs_SHAPE means
251    *             all types of topology
252    * \param tolerance expected tolerance value (1e-7 by default)
253    * \param checkGeometry check geometry validity of result
254    * \return \c true if resulting shape is valid
255    *
256    * \note Resulting tolerance of the shape is not mandatory equal to requested value
257    *       as it might be changed by fixshape operation in order to get valid shape where possible
258    * \note By default, result only checked for topology validity; check of geometry can be done by
259    *       passing \c true to \a checkGeometry parameter
260    */
261   Standard_EXPORT bool FixShapeTolerance( TopoDS_Shape& shape,
262                                           TopAbs_ShapeEnum type,
263                                           Standard_Real tolerance = Precision::Confusion(),
264                                           bool checkGeometry = false );
265
266   /*!
267    * \brief Limit shape tolerance to the given value
268    * This is overloaded function, it behaves exactly as previous one
269    */
270   Standard_EXPORT bool FixShapeTolerance( TopoDS_Shape& shape,
271                                           Standard_Real tolerance = Precision::Confusion(),
272                                           bool checkGeometry = false );
273
274   /*!
275    * \brief Limit shape tolerance to the given value
276    * This is overloaded function, it behaves exactly as previous one
277    */
278   Standard_EXPORT bool FixShapeTolerance( TopoDS_Shape& shape,
279                                           bool checkGeometry );
280
281   /*!
282    * \brief Fix curves of the given shape
283    * 
284    * The function checks each curve of the input shape in the following way:
285    * - compute deviation of the curve from the underlying surface in a set of points
286    *   computed with the certain discretization step value
287    * - find maximum tolerance between computed deviation values
288    * - limit tolerance of the curve with the computed maximum value
289    * 
290    * \param shape shape being fixed
291    * \return \c true if resulting shape is valid
292    */
293   Standard_EXPORT bool FixShapeCurves( TopoDS_Shape& shape );
294
295   /*!
296    * \brief Write shape to the BREP file
297    *
298    * \param source shape
299    * \return \c true if file was written or \c false otherwise
300    */
301   Standard_EXPORT bool Write( const TopoDS_Shape& shape,
302                               const char* fileName );
303   
304   /*!
305    * \brief Extract single SOLID from COMPSOLID or COMPOUND.
306    *
307    * If the argument shape is a COMPUND or COMPSOLID and there's
308    * only single simple-shape type inside, this sub-shape is returned as a result;
309    * otherwise, the shape is not changed.
310    *
311    * \param shape compound or compsolid being processed.
312    * \retval TopoDS_Shape resulting shape
313    */
314   Standard_EXPORT TopoDS_Shape ReduceCompound( const TopoDS_Shape& shape );
315
316   /*!
317    * \brief Generate triangulation for the shape.
318    *
319    * \param shape shape being meshed
320    * \param deflection deflection coefficient to be used
321    * \param forced if \c true, causes generation of mesh regardless it is already present in the shape
322    */
323   Standard_EXPORT void MeshShape( const TopoDS_Shape shape,
324                                   double deflection, bool forced = true );
325
326   /*!
327    * \brief Get default deflection coefficient used for triangulation
328    * \return default deflection value
329    */
330   Standard_EXPORT double DefaultDeflection();
331 };
332
333 #endif