0030675: Visualization - remove redundant proxy classes in hierarchy of PrsMgr_Presen...
[occt.git] / src / GeomLib / GeomLib_Tool.cxx
1 // Created on: 2003-03-18
2 // Created by: Oleg FEDYAEV
3 // Copyright (c) 2003-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16
17 #include <ElCLib.hxx>
18 #include <ElSLib.hxx>
19 #include <Extrema_ExtPC.hxx>
20 #include <Extrema_ExtPC2d.hxx>
21 #include <Extrema_ExtPS.hxx>
22 #include <Geom2d_BezierCurve.hxx>
23 #include <Geom2d_BSplineCurve.hxx>
24 #include <Geom2d_Circle.hxx>
25 #include <Geom2d_Curve.hxx>
26 #include <Geom2d_Ellipse.hxx>
27 #include <Geom2d_Hyperbola.hxx>
28 #include <Geom2d_Line.hxx>
29 #include <Geom2d_OffsetCurve.hxx>
30 #include <Geom2d_Parabola.hxx>
31 #include <Geom2d_TrimmedCurve.hxx>
32 #include <Geom2dAdaptor_Curve.hxx>
33 #include <Geom_BezierCurve.hxx>
34 #include <Geom_BezierSurface.hxx>
35 #include <Geom_BSplineCurve.hxx>
36 #include <Geom_BSplineSurface.hxx>
37 #include <Geom_Circle.hxx>
38 #include <Geom_ConicalSurface.hxx>
39 #include <Geom_Curve.hxx>
40 #include <Geom_CylindricalSurface.hxx>
41 #include <Geom_Ellipse.hxx>
42 #include <Geom_Hyperbola.hxx>
43 #include <Geom_Line.hxx>
44 #include <Geom_OffsetCurve.hxx>
45 #include <Geom_OffsetSurface.hxx>
46 #include <Geom_Parabola.hxx>
47 #include <Geom_Plane.hxx>
48 #include <Geom_RectangularTrimmedSurface.hxx>
49 #include <Geom_SphericalSurface.hxx>
50 #include <Geom_Surface.hxx>
51 #include <Geom_SurfaceOfLinearExtrusion.hxx>
52 #include <Geom_SurfaceOfRevolution.hxx>
53 #include <Geom_ToroidalSurface.hxx>
54 #include <Geom_TrimmedCurve.hxx>
55 #include <GeomAdaptor_Curve.hxx>
56 #include <GeomAdaptor_Surface.hxx>
57 #include <GeomLib_Tool.hxx>
58 #include <gp_Circ.hxx>
59 #include <gp_Circ2d.hxx>
60 #include <gp_Cone.hxx>
61 #include <gp_Cylinder.hxx>
62 #include <gp_Elips.hxx>
63 #include <gp_Elips2d.hxx>
64 #include <gp_Hypr.hxx>
65 #include <gp_Hypr2d.hxx>
66 #include <gp_Lin.hxx>
67 #include <gp_Lin2d.hxx>
68 #include <gp_Parab.hxx>
69 #include <gp_Parab2d.hxx>
70 #include <gp_Pln.hxx>
71 #include <gp_Pnt.hxx>
72 #include <gp_Pnt2d.hxx>
73 #include <gp_Sphere.hxx>
74 #include <gp_Torus.hxx>
75 #include <gp_Vec.hxx>
76
77 // The functions Parameter(s) are used to compute parameter(s) of point
78 // on curves and surfaces. The main rule is that tested point must lied
79 // on curves or surfaces otherwise the resulted parameter(s) may be wrong.
80 // To make search process more common the MaxDist value is used to define
81 // the proximity of point to curve or surface. It is clear that this MaxDist
82 // value can't be too high to be not in conflict with previous rule.
83 static const Standard_Real PARTOLERANCE = 1.e-9;
84
85 //=======================================================================
86 //function : Parameter
87 //purpose  : Get parameter on curve of given point
88 //           return FALSE if point is far from curve than MaxDist
89 //           or computation fails
90 //=======================================================================
91
92 Standard_Boolean GeomLib_Tool::Parameter(const Handle(Geom_Curve)& Curve,
93                                          const gp_Pnt&             Point,
94                                          const Standard_Real       MaxDist,
95                                                Standard_Real&            U)
96 {
97   if( Curve.IsNull() ) return Standard_False;
98   //
99   U = 0.;
100   Standard_Real aTol = MaxDist * MaxDist;
101   //
102   GeomAdaptor_Curve aGAC(Curve);
103   Extrema_ExtPC extrema(Point,aGAC);
104   //
105   if( !extrema.IsDone() ) return Standard_False;
106   //
107   Standard_Integer n = extrema.NbExt();
108   if( n <= 0 ) return Standard_False;
109   //
110   Standard_Integer i = 0, iMin = 0;
111   Standard_Real Dist2Min = RealLast();
112   for( i = 1; i <= n; i++ )
113   {
114     if (extrema.SquareDistance(i) < Dist2Min)
115     {
116       iMin = i;
117       Dist2Min = extrema.SquareDistance(i);
118     }
119   }
120   if( iMin != 0 && Dist2Min <= aTol ) 
121   {
122     U = (extrema.Point(iMin)).Parameter();
123   }
124   else 
125   {
126     return Standard_False;
127   }
128  
129   return Standard_True;
130
131 }
132
133 //=======================================================================
134 //function : Parameters
135 //purpose  : Get parameters on surface of given point
136 //           return FALSE if point is far from surface than MaxDist
137 //           or computation fails
138 //=======================================================================
139
140 Standard_Boolean GeomLib_Tool::Parameters(const Handle(Geom_Surface)& Surface,
141                                           const gp_Pnt&               Point,
142                                           const Standard_Real         MaxDist,
143                                                 Standard_Real&              U,
144                                                 Standard_Real&              V)
145 {
146   if( Surface.IsNull() ) return Standard_False;
147   //
148   U = 0.;
149   V = 0.;
150   Standard_Real aTol = MaxDist * MaxDist;
151   //
152   GeomAdaptor_Surface aGAS(Surface);
153   Standard_Real aTolU = PARTOLERANCE, aTolV = PARTOLERANCE;
154   //
155   Extrema_ExtPS extrema(Point,aGAS,aTolU,aTolV);
156   //
157   if( !extrema.IsDone() ) return Standard_False;
158   //
159   Standard_Integer n = extrema.NbExt();
160   if( n <= 0 ) return Standard_False;
161   //
162   Standard_Real Dist2Min = RealLast();
163   Standard_Integer i = 0, iMin = 0;
164   for( i = 1; i <= n; i++ )
165   {
166     if( extrema.SquareDistance(i) < Dist2Min )
167     {
168       Dist2Min = extrema.SquareDistance(i);
169       iMin = i;
170     }
171   }
172   if( iMin != 0 && Dist2Min <= aTol)
173   {
174     extrema.Point(iMin).Parameter(U,V);
175   }
176   else
177   {
178     return Standard_False;
179   }
180
181   return Standard_True;
182
183 }
184
185 //=======================================================================
186 //function : Parameter
187 //purpose  : Get parameter on curve of given point
188 //           return FALSE if point is far from curve than MaxDist
189 //           or computation fails
190 //=======================================================================
191
192 Standard_Boolean GeomLib_Tool::Parameter(const Handle(Geom2d_Curve)& Curve,
193                                          const gp_Pnt2d&             Point,
194                                          const Standard_Real         MaxDist,
195                                                Standard_Real&              U)
196 {
197   if( Curve.IsNull() ) return Standard_False;
198   //
199   U = 0.;
200   Standard_Real aTol = MaxDist * MaxDist;
201   //
202   Geom2dAdaptor_Curve aGAC(Curve);
203   Extrema_ExtPC2d extrema(Point,aGAC);
204   if( !extrema.IsDone() ) return Standard_False;
205   Standard_Integer n = extrema.NbExt();
206   if( n <= 0 ) return Standard_False;
207   Standard_Integer i = 0, iMin = 0;
208   Standard_Real Dist2Min = RealLast();
209   for ( i = 1; i <= n; i++ )
210   {
211     if( extrema.SquareDistance(i) < Dist2Min )
212     {
213       Dist2Min = extrema.SquareDistance(i);
214       iMin = i;
215     }
216   }
217   if( iMin != 0 && Dist2Min <= aTol )
218   {
219     U = (extrema.Point(iMin)).Parameter();
220   }
221   else
222   {
223     return Standard_False;
224   }
225
226   return Standard_True;
227 }