1 // Created on: 1993-12-13
2 // Created by: Christophe MARION
3 // Copyright (c) 1993-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
19 #include <Extrema_ExtPElC2d.hxx>
20 #include <Extrema_POnCurv2d.hxx>
21 #include <gp_Circ2d.hxx>
22 #include <gp_Elips2d.hxx>
23 #include <gp_Hypr2d.hxx>
24 #include <gp_Lin2d.hxx>
25 #include <gp_Parab2d.hxx>
26 #include <gp_Pnt2d.hxx>
27 #include <math_DirectPolynomialRoots.hxx>
28 #include <math_TrigonometricFunctionRoots.hxx>
29 #include <Precision.hxx>
30 #include <Standard_NotImplemented.hxx>
31 #include <Standard_OutOfRange.hxx>
32 #include <StdFail_NotDone.hxx>
34 //=============================================================================
35 Extrema_ExtPElC2d::Extrema_ExtPElC2d()
37 myDone = Standard_False;
40 for (Standard_Integer i = 0; i < 4; i++)
42 mySqDist[i] = RealLast();
43 myIsMin[i] = Standard_False;
47 //=============================================================================
49 Extrema_ExtPElC2d::Extrema_ExtPElC2d
52 const Standard_Real Tol,
53 const Standard_Real Uinf,
54 const Standard_Real Usup)
56 Perform(P, L, Tol, Uinf, Usup);
59 void Extrema_ExtPElC2d::Perform(const gp_Pnt2d& P,
61 const Standard_Real Tol,
62 const Standard_Real Uinf,
63 const Standard_Real Usup)
65 myDone = Standard_True;
69 gp_Vec2d V1 = gp_Vec2d(L.Direction());
72 Standard_Real Mydist = V1.Dot(V);
73 if ((Mydist >= Uinf -Tol) &&
74 (Mydist <= Usup+ Tol)){
76 MyP = OR.Translated(Mydist*V1);
77 Extrema_POnCurv2d MyPOnCurve(Mydist, MyP);
78 mySqDist[0] = P.SquareDistance(MyP);
79 myPoint[0] = MyPOnCurve;
80 myIsMin[0] = Standard_True;
84 //=============================================================================
86 Extrema_ExtPElC2d::Extrema_ExtPElC2d
89 const Standard_Real Tol,
90 const Standard_Real Uinf,
91 const Standard_Real Usup)
93 Perform(P, C, Tol, Uinf, Usup);
96 void Extrema_ExtPElC2d::Perform(const gp_Pnt2d& P,
98 const Standard_Real Tol,
99 const Standard_Real Uinf,
100 const Standard_Real Usup)
102 // gp_Pnt2d OC, P1, P2, OL;
103 gp_Pnt2d OC(C.Location());
106 if (OC.IsEqual(P, Precision::Confusion())) {
107 myDone = Standard_False;
111 Standard_Real radius, U1, U2;
114 myDone = Standard_True;
115 gp_Dir2d V(gp_Vec2d(P, OC));
117 P1 = OC.Translated(radius*V);
118 U1 = ElCLib::Parameter(C, P1);
120 P2 = OC.Translated(-radius*V);
121 Standard_Real myuinf = Uinf;
122 ElCLib::AdjustPeriodic(Uinf, Uinf+2*M_PI, Precision::PConfusion(), myuinf, U1);
123 ElCLib::AdjustPeriodic(Uinf, Uinf+2*M_PI, Precision::PConfusion(), myuinf, U2);
124 if (((U1-2*M_PI-Uinf) < Tol) && ((U1-2*M_PI-Uinf) > -Tol))
127 P1 = OC.XY() + radius * (cos(U1) * C.XAxis().Direction().XY() + sin(U1) * C.YAxis().Direction().XY());
130 if (((U2-2*M_PI-Uinf) < Tol) && ((U2-2*M_PI-Uinf) > -Tol))
133 P2 = OC.XY() + radius * (cos(U2) * C.XAxis().Direction().XY() + sin(U2) * C.YAxis().Direction().XY());
136 if (((Uinf-U1) < Tol) && ((U1-Usup) < Tol))
138 Extrema_POnCurv2d MyPOnCurve(U1, P1);
139 mySqDist[0] = P.SquareDistance(P1);
140 myPoint[0] = MyPOnCurve;
141 myIsMin[0] = Standard_True;
145 if (((Uinf-U2) < Tol) && ((U2-Usup) < Tol))
147 Extrema_POnCurv2d MyPOnCurve(U2, P2);
148 mySqDist[myNbExt] = P.SquareDistance(P2);
149 myPoint[myNbExt] = MyPOnCurve;
150 myIsMin[myNbExt] = Standard_True;
156 //=============================================================================
159 Extrema_ExtPElC2d::Extrema_ExtPElC2d (const gp_Pnt2d& P,
161 const Standard_Real Tol,
162 const Standard_Real Uinf,
163 const Standard_Real Usup)
165 Perform(P, E, Tol, Uinf, Usup);
170 void Extrema_ExtPElC2d::Perform (const gp_Pnt2d& P,
172 const Standard_Real Tol,
173 const Standard_Real Uinf,
174 const Standard_Real Usup)
176 myDone = Standard_False;
178 // gp_Pnt2d OR, P1, P2;
182 Standard_Integer NoSol, NbSol;
183 Standard_Real A = E.MajorRadius();
184 Standard_Real B = E.MinorRadius();
187 if (OR.IsEqual(P, Precision::Confusion()) &&
192 Standard_Real X = V.Dot(gp_Vec2d(E.XAxis().Direction()));
193 Standard_Real Y = V.Dot(gp_Vec2d(E.YAxis().Direction()));
195 math_TrigonometricFunctionRoots Sol(0.,(B*B-A*A)/2.,-B*Y,A*X,0.,Uinf,Usup);
197 if (!Sol.IsDone()) { return; }
200 NbSol = Sol.NbSolutions();
202 for (NoSol = 1; NoSol <= NbSol; NoSol++) {
203 Us = Sol.Value(NoSol);
204 Cu = ElCLib::Value(Us, E);
205 mySqDist[myNbExt] = Cu.SquareDistance(P);
206 myIsMin[myNbExt] = (NoSol == 0);
207 myPoint[myNbExt] = Extrema_POnCurv2d(Us,Cu);
210 myDone = Standard_True;
213 //=============================================================================
215 Extrema_ExtPElC2d::Extrema_ExtPElC2d (const gp_Pnt2d& P,
217 const Standard_Real Tol,
218 const Standard_Real Uinf,
219 const Standard_Real Usup)
221 Perform(P, C, Tol, Uinf, Usup);
225 void Extrema_ExtPElC2d::Perform(const gp_Pnt2d& P,
227 const Standard_Real Tol,
228 const Standard_Real Uinf,
229 const Standard_Real Usup)
231 gp_Pnt2d O = H.Location();
232 myDone = Standard_False;
235 Standard_Real R = H.MajorRadius();
236 Standard_Real r = H.MinorRadius();
238 Standard_Real Tol2 = Tol * Tol;
239 Standard_Real X = OPp.Dot(gp_Vec2d(H.XAxis().Direction()));
240 Standard_Real Y = OPp.Dot(gp_Vec2d(H.YAxis().Direction()));
241 Standard_Real C1 = (R*R+r*r)/4.;
242 math_DirectPolynomialRoots Sol(C1,-(X*R+Y*r)/2.,0.,(X*R-Y*r)/2.,-C1);
243 if (!Sol.IsDone()) { return; }
245 Standard_Real Us, Vs;
246 Standard_Integer NbSol = Sol.NbSolutions();
247 Standard_Boolean DejaEnr;
248 Standard_Integer NoExt;
250 for (Standard_Integer NoSol = 1; NoSol <= NbSol; NoSol++) {
251 Vs = Sol.Value(NoSol);
254 if ((Us >= Uinf) && (Us <= Usup)) {
255 Cu = ElCLib::Value(Us,H);
256 DejaEnr = Standard_False;
257 for (NoExt = 0; NoExt < myNbExt; NoExt++) {
258 if (TbExt[NoExt].SquareDistance(Cu) < Tol2) {
259 DejaEnr = Standard_True;
265 mySqDist[myNbExt] = Cu.SquareDistance(P);
266 myIsMin[myNbExt] = (NoSol == 0);
267 myPoint[myNbExt] = Extrema_POnCurv2d(Us,Cu);
270 } // if ((Us >= Uinf) && (Us <= Usup))
272 } // for (Standard_Integer NoSol = 1; ...
273 myDone = Standard_True;
276 //=============================================================================
278 Extrema_ExtPElC2d::Extrema_ExtPElC2d (const gp_Pnt2d& P,
280 const Standard_Real Tol,
281 const Standard_Real Uinf,
282 const Standard_Real Usup)
284 Perform(P, C, Tol, Uinf, Usup);
288 void Extrema_ExtPElC2d::Perform(const gp_Pnt2d& P,
290 const Standard_Real Tol,
291 const Standard_Real Uinf,
292 const Standard_Real Usup)
294 myDone = Standard_False;
296 gp_Pnt2d O = C.Location();
298 Standard_Real Tol2 = Tol * Tol;
299 Standard_Real F = C.Focal();
301 Standard_Real X = OPp.Dot(gp_Vec2d(C.MirrorAxis().Direction()));
302 Standard_Real Y = OPp.Dot(gp_Vec2d(C.Axis().YAxis().Direction()));
304 math_DirectPolynomialRoots Sol(1./(4.*F),0.,2.*F-X,-2.*F*Y);
305 if (!Sol.IsDone()) { return; }
308 Standard_Integer NbSol = Sol.NbSolutions();
309 Standard_Boolean DejaEnr;
310 Standard_Integer NoExt;
312 for (Standard_Integer NoSol = 1; NoSol <= NbSol; NoSol++) {
313 Us = Sol.Value(NoSol);
314 if ((Us >= Uinf) && (Us <= Usup)) {
315 Cu = ElCLib::Value(Us,C);
316 DejaEnr = Standard_False;
317 for (NoExt = 0; NoExt < myNbExt; NoExt++) {
318 if (TbExt[NoExt].SquareDistance(Cu) < Tol2) {
319 DejaEnr = Standard_True;
325 mySqDist[myNbExt] = Cu.SquareDistance(P);
326 myIsMin[myNbExt] = (NoSol == 0);
327 myPoint[myNbExt] = Extrema_POnCurv2d(Us,Cu);
330 } // if ((Us >= Uinf) && (Us <= Usup))
331 } // for (Standard_Integer NoSol = 1; ...
332 myDone = Standard_True;
334 //=============================================================================
336 Standard_Boolean Extrema_ExtPElC2d::IsDone () const { return myDone; }
337 //=============================================================================
339 Standard_Integer Extrema_ExtPElC2d::NbExt () const
341 if (!IsDone()) { throw StdFail_NotDone(); }
344 //=============================================================================
346 Standard_Real Extrema_ExtPElC2d::SquareDistance (const Standard_Integer N) const
348 if ((N < 1) || (N > NbExt())) { throw Standard_OutOfRange(); }
349 return mySqDist[N-1];
351 //=============================================================================
353 Standard_Boolean Extrema_ExtPElC2d::IsMin (const Standard_Integer N) const
355 if ((N < 1) || (N > NbExt())) { throw Standard_OutOfRange(); }
358 //=============================================================================
360 const Extrema_POnCurv2d& Extrema_ExtPElC2d::Point (const Standard_Integer N) const
362 if ((N < 1) || (N > NbExt())) { throw Standard_OutOfRange(); }
365 //=============================================================================