1 // Copyright (c) 1995-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
6 // This library is free software; you can redistribute it and / or modify it
7 // under the terms of the GNU Lesser General Public version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
17 #include <Extrema_ExtElC.ixx>
18 #include <StdFail_InfiniteSolutions.hxx>
19 #include <StdFail_NotDone.hxx>
21 #include <math_TrigonometricFunctionRoots.hxx>
22 #include <math_DirectPolynomialRoots.hxx>
23 #include <Standard_OutOfRange.hxx>
24 #include <Standard_NotImplemented.hxx>
25 #include <Precision.hxx>
26 #include <Extrema_ExtPElC.hxx>
27 #include <IntAna_QuadQuadGeo.hxx>
28 #include <Extrema_ExtPElC.hxx>
38 void RefineDir(gp_Dir& aDir);
40 //=======================================================================
41 //class : ExtremaExtElC_TrigonometricRoots
43 //== Classe Interne (Donne des racines classees d un polynome trigo)
44 //== Code duplique avec IntAna_IntQuadQuad.cxx (lbr le 26 mars 98)
45 //== Solution fiable aux problemes de coefficients proches de 0
46 //== avec essai de rattrapage si coeff<1.e-10 (jct le 27 avril 98)
47 //=======================================================================
48 class ExtremaExtElC_TrigonometricRoots {
50 Standard_Real Roots[4];
51 Standard_Boolean done;
52 Standard_Integer NbRoots;
53 Standard_Boolean infinite_roots;
55 ExtremaExtElC_TrigonometricRoots(const Standard_Real CC,
56 const Standard_Real SC,
57 const Standard_Real C,
58 const Standard_Real S,
59 const Standard_Real Cte,
60 const Standard_Real Binf,
61 const Standard_Real Bsup);
63 Standard_Boolean IsDone() {
67 Standard_Boolean IsARoot(Standard_Real u) {
68 Standard_Real PIpPI, aEps;
72 for(Standard_Integer i=0 ; i<NbRoots; i++) {
73 if(Abs(u - Roots[i])<=aEps) {
74 return Standard_True ;
76 if(Abs(u - Roots[i]-PIpPI)<=aEps) {
80 return Standard_False;
83 Standard_Integer NbSolutions() {
85 StdFail_NotDone::Raise();
90 Standard_Boolean InfiniteRoots() {
92 StdFail_NotDone::Raise();
94 return infinite_roots;
97 Standard_Real Value(const Standard_Integer& n) {
98 if((!done)||(n>NbRoots)) {
99 StdFail_NotDone::Raise();
104 //=======================================================================
105 //function : ExtremaExtElC_TrigonometricRoots
107 //=======================================================================
108 ExtremaExtElC_TrigonometricRoots::
109 ExtremaExtElC_TrigonometricRoots(const Standard_Real CC,
110 const Standard_Real SC,
111 const Standard_Real C,
112 const Standard_Real S,
113 const Standard_Real Cte,
114 const Standard_Real Binf,
115 const Standard_Real Bsup)
117 Standard_Integer i, nbessai;
118 Standard_Real cc ,sc, c, s, cte;
127 while (nbessai<=2 && !done) {
128 //-- F= AA*CN*CN+2*BB*CN*SN+CC*CN+DD*SN+EE;
129 math_TrigonometricFunctionRoots MTFR(cc,sc,c,s,cte,Binf,Bsup);
133 if(MTFR.InfiniteRoots()) {
134 infinite_roots=Standard_True;
137 Standard_Boolean Triee;
138 Standard_Integer j, SvNbRoots;
139 Standard_Real aTwoPI, aMaxCoef, aPrecision;
142 NbRoots=MTFR.NbSolutions();
143 for(i=0;i<NbRoots;++i) {
144 Roots[i]=MTFR.Value(i+1);
146 Roots[i]=Roots[i]+aTwoPI;
148 if(Roots[i]>aTwoPI) {
149 Roots[i]=Roots[i]-aTwoPI;
153 //-- La recherche directe donne n importe quoi.
154 aMaxCoef = Max(CC,SC);
155 aMaxCoef = Max(aMaxCoef,C);
156 aMaxCoef = Max(aMaxCoef,S);
157 aMaxCoef = Max(aMaxCoef,Cte);
158 aPrecision = Max(1.e-8, 1.e-12*aMaxCoef);
161 for(i=0; i<SvNbRoots; ++i) {
163 Standard_Real co=cos(Roots[i]);
164 Standard_Real si=sin(Roots[i]);
165 y=co*(CC*co + (SC+SC)*si + C) + S*si + Cte;
166 // modified by OCC Tue Oct 3 18:43:00 2006
167 if(Abs(y)>aPrecision) {
177 for(i=1, j=0; i<SvNbRoots; ++i, ++j) {
178 if(Roots[i]<Roots[j]) {
179 Triee=Standard_False;
188 infinite_roots=Standard_False;
189 if(NbRoots==0) { //--!!!!! Detect case Pol = Cte ( 1e-50 ) !!!!
190 if((Abs(CC) + Abs(SC) + Abs(C) + Abs(S)) < 1e-10) {
191 if(Abs(Cte) < 1e-10) {
192 infinite_roots=Standard_True;
197 } // if(MTFR.IsDone()) {
199 // try to set very small coefficients to ZERO
217 } // while (nbessai<=2 && !done) {
220 //=======================================================================
221 //function : Extrema_ExtElC
223 //=======================================================================
224 Extrema_ExtElC::Extrema_ExtElC ()
226 myDone = Standard_False;
228 //=======================================================================
229 //function : Extrema_ExtElC
231 //=======================================================================
232 Extrema_ExtElC::Extrema_ExtElC (const gp_Lin& C1,
236 // Find min distance between 2 straight lines.
239 // Let D1 and D2, be 2 directions of straight lines C1 and C2.
240 // 2 cases are considered:
241 // 1- if Angle(D1,D2) < AngTol, straight lines are parallel.
242 // The distance is the distance between a point of C1 and the straight line C2.
243 // 2- if Angle(D1,D2) > AngTol:
244 // Let P1=C1(u1) and P2=C2(u2) be 2 solution points:
245 // Then, ( P1P2.D1 = 0. (1)
246 // ( P1P2.D2 = 0. (2)
247 // Let O1 and O2 be the origins of C1 and C2;
248 // THen, (1) <=> (O1P2-u1*D1).D1 = 0. as O1P1 = u1*D1
249 // <=> u1 = O1P2.D1 as D1.D1 = 1.
250 // (2) <=> (P1O2+u2*D2).D2 = 0. as O2P2 = u2*D2
251 // <=> u2 = O2P1.D2 as D2.D2 = 1.
252 // <=> u2 = (O2O1+O1P1).D2
253 // <=> u2 = O2O1.D2+((O1P2.T1)T1).T2) as O1P1 = u1*T1 = (O1P2.T1)T1
254 // <=> u2 = O2O1.D2+(((O1O2+O2P2).D1)D1).D2)
255 // <=> u2 = O2O1.D2+((O1O2.D1)D1).D2)+(O2P2.D1)(D1.D2)
256 // <=> u2 = ((O1O2.D1)D1-O1O2).D2 + u2*(D2.D1)(D1.D2)
257 // <=> u2 = (((O1O2.D1)D1-O1O2).D2) / 1.-(D1.D2)**2
259 myDone = Standard_False;
262 gp_Dir D1 = C1.Position().Direction();
263 gp_Dir D2 = C2.Position().Direction();
264 // MSV 16/01/2000: avoid "divide by zero"
265 Standard_Real D1DotD2 = D1.Dot(D2);
266 Standard_Real aSin = 1.-D1DotD2*D1DotD2;
267 if (aSin < gp::Resolution() ||
268 D1.IsParallel(D2, Precision::Angular())) {
269 myIsPar = Standard_True;
270 mySqDist[0] = C2.SquareDistance(C1.Location());
271 mySqDist[1] = mySqDist[0];
274 myIsPar = Standard_False;
275 gp_Pnt O1 = C1.Location();
276 gp_Pnt O2 = C2.Location();
278 Standard_Real U2 = (D1.XYZ()*(O1O2.Dot(D1))-(O1O2.XYZ())).Dot(D2.XYZ());
279 if( Precision::IsInfinite(U2) ) {
280 myIsPar = Standard_True;
281 mySqDist[0] = C2.SquareDistance(C1.Location());
282 mySqDist[1] = mySqDist[0];
286 if( Precision::IsInfinite(U2) ) {
287 myIsPar = Standard_True;
288 mySqDist[0] = C2.SquareDistance(C1.Location());
289 mySqDist[1] = mySqDist[0];
292 gp_Pnt P2(ElCLib::Value(U2,C2));
293 Standard_Real U1 = (gp_Vec(O1,P2)).Dot(D1);
294 if( Precision::IsInfinite(U1) ) {
295 myIsPar = Standard_True;
296 mySqDist[0] = C2.SquareDistance(C1.Location());
297 mySqDist[1] = mySqDist[0];
300 gp_Pnt P1(ElCLib::Value(U1,C1));
301 mySqDist[myNbExt] = P1.SquareDistance(P2);
302 myPoint[myNbExt][0] = Extrema_POnCurv(U1,P1);
303 myPoint[myNbExt][1] = Extrema_POnCurv(U2,P2);
309 myDone = Standard_True;
311 //=======================================================================
312 //function : Extrema_ExtElC
314 // Find extreme distances between straight line C1 and circle C2.
317 // Let P1=C1(u1) and P2=C2(u2) be two solution points
318 // D the direction of straight line C1
319 // T tangent at point P2;
320 // Then, ( P1P2.D = 0. (1)
322 // Let O1 and O2 be the origins of C1 and C2;
323 // Then, (1) <=> (O1P2-u1*D).D = 0. as O1P1 = u1*D
324 // <=> u1 = O1P2.D as D.D = 1.
325 // (2) <=> P1O2.T = 0. as O2P2.T = 0.
326 // <=> ((P2O1.D)D+O1O2).T = 0. as P1O1 = -u1*D = (P2O1.D)D
327 // <=> (((P2O2+O2O1).D)D+O1O2).T = 0.
328 // <=> ((P2O2.D)(D.T)+((O2O1.D)D-O2O1).T = 0.
329 // We are in the reference of the circle; let:
330 // Cos = Cos(u2) and Sin = Sin(u2),
331 // P2 (R*Cos,R*Sin,0.),
332 // T (-R*Sin,R*Cos,0.),
334 // V (Vx,Vy,Vz) = (O2O1.D)D-O2O1;
335 // Then, the equation by Cos and Sin is as follows:
336 // -(2*R*R*Dx*Dy) * Cos**2 + A1
337 // R*R*(Dx**2-Dy**2) * Cos*Sin + 2* A2
341 //Use the algorithm math_TrigonometricFunctionRoots to solve this equation.
342 //=======================================================================
343 Extrema_ExtElC::Extrema_ExtElC (const gp_Lin& C1,
347 Standard_Real Dx,Dy,Dz,aRO2O1, aTolRO2O1;
348 Standard_Real R, A1, A2, A3, A4, A5, aTol;
349 gp_Dir x2, y2, z2, D, D1;
351 myIsPar = Standard_False;
352 myDone = Standard_False;
355 // Calculate T1 in the reference of the circle ...
358 x2 = C2.XAxis().Direction();
359 y2 = C2.YAxis().Direction();
360 z2 = C2.Axis().Direction();
365 D.SetCoord(Dx, Dy, Dz);
369 // Calcul de V dans le repere du cercle:
370 gp_Pnt O1 = C1.Location();
371 gp_Pnt O2 = C2.Location();
374 aTolRO2O1=gp::Resolution();
375 aRO2O1=O2O1.Magnitude();
376 if (aRO2O1 > aTolRO2O1) {
379 O2O1.Multiply(1./aRO2O1);
380 aDO2O1.SetCoord(O2O1.Dot(x2), O2O1.Dot(y2), O2O1.Dot(z2));
382 O2O1.SetXYZ(aRO2O1*aDO2O1.XYZ());
385 O2O1.SetCoord(O2O1.Dot(x2), O2O1.Dot(y2), O2O1.Dot(z2));
388 gp_XYZ Vxyz = (D.XYZ()*(O2O1.Dot(D)))-O2O1.XYZ();
390 //modified by NIZNHY-PKV Tue Mar 20 10:36:38 2012
395 A2 = R*R*(Dx*Dx-Dy*Dy)/2.;
402 if(fabs(A5) <= aTol) {
405 if(fabs(A1) <= aTol) {
408 if(fabs(A2) <= aTol) {
411 if(fabs(A3) <= aTol) {
414 if(fabs(A4) <= aTol) {
420 // Calculate the coefficients of the equation by Cos and Sin ...
425 A2 = 0.5*R*(Dx*Dx-Dy*Dy);// /2.;
429 if (A1>=-aTol && A1<=aTol) {
432 if (A2>=-aTol && A2<=aTol) {
435 if (A3>=-aTol && A3<=aTol) {
438 if (A4>=-aTol && A4<=aTol) {
441 if (A5>=-aTol && A5<=aTol) {
444 //modified by NIZNHY-PKV Tue Mar 20 10:36:40 2012t
446 ExtremaExtElC_TrigonometricRoots Sol(A1, A2, A3, A4, A5, 0., M_PI+M_PI);
450 if (Sol.InfiniteRoots()) {
451 myIsPar = Standard_True;
453 myDone = Standard_True;
456 // Storage of solutions ...
457 Standard_Integer NoSol, NbSol;
461 NbSol = Sol.NbSolutions();
462 for (NoSol=1; NoSol<=NbSol; ++NoSol) {
463 U2 = Sol.Value(NoSol);
464 P2 = ElCLib::Value(U2,C2);
465 U1 = (gp_Vec(O1,P2)).Dot(D1);
466 P1 = ElCLib::Value(U1,C1);
467 mySqDist[myNbExt] = P1.SquareDistance(P2);
468 //modified by NIZNHY-PKV Wed Mar 21 08:11:33 2012f
469 //myPoint[myNbExt][0] = Extrema_POnCurv(U1,P1);
470 //myPoint[myNbExt][1] = Extrema_POnCurv(U2,P2);
471 myPoint[myNbExt][0].SetValues(U1,P1);
472 myPoint[myNbExt][1].SetValues(U2,P2);
473 //modified by NIZNHY-PKV Wed Mar 21 08:11:36 2012t
476 myDone = Standard_True;
478 //=======================================================================
479 //function : Extrema_ExtElC
481 //=======================================================================
482 Extrema_ExtElC::Extrema_ExtElC (const gp_Lin& C1,
485 /*-----------------------------------------------------------------------------
487 Find extreme distances between straight line C1 and ellipse C2.
490 Let P1=C1(u1) and P2=C2(u2) two solution points
491 D the direction of straight line C1
492 T the tangent to point P2;
493 Then, ( P1P2.D = 0. (1)
495 Let O1 and O2 be the origins of C1 and C2;
496 Then, (1) <=> (O1P2-u1*D).D = 0. as O1P1 = u1*D
497 <=> u1 = O1P2.D as D.D = 1.
498 (2) <=> P1O2.T = 0. as O2P2.T = 0.
499 <=> ((P2O1.D)D+O1O2).T = 0. as P1O1 = -u1*D = (P2O1.D)D
500 <=> (((P2O2+O2O1).D)D+O1O2).T = 0.
501 <=> ((P2O2.D)(D.T)+((O2O1.D)D-O2O1).T = 0.
502 We are in the reference of the ellipse; let:
503 Cos = Cos(u2) and Sin = Sin(u2),
504 P2 (MajR*Cos,MinR*Sin,0.),
505 T (-MajR*Sin,MinR*Cos,0.),
507 V (Vx,Vy,Vz) = (O2O1.D)D-O2O1;
508 Then, get the following equation by Cos and Sin:
509 -(2*MajR*MinR*Dx*Dy) * Cos**2 +
510 (MajR*MajR*Dx**2-MinR*MinR*Dy**2) * Cos*Sin +
514 Use algorithm math_TrigonometricFunctionRoots to solve this equation.
515 -----------------------------------------------------------------------------*/
516 myIsPar = Standard_False;
517 myDone = Standard_False;
520 // Calculate T1 the reference of the ellipse ...
521 gp_Dir D = C1.Direction();
524 x2 = C2.XAxis().Direction();
525 y2 = C2.YAxis().Direction();
526 z2 = C2.Axis().Direction();
527 Standard_Real Dx = D.Dot(x2);
528 Standard_Real Dy = D.Dot(y2);
529 Standard_Real Dz = D.Dot(z2);
530 D.SetCoord(Dx,Dy,Dz);
533 gp_Pnt O1 = C1.Location();
534 gp_Pnt O2 = C2.Location();
536 O2O1.SetCoord(O2O1.Dot(x2), O2O1.Dot(y2), O2O1.Dot(z2));
537 gp_XYZ Vxyz = (D.XYZ()*(O2O1.Dot(D)))-O2O1.XYZ();
539 // Calculate the coefficients of the equation by Cos and Sin ...
540 Standard_Real MajR = C2.MajorRadius();
541 Standard_Real MinR = C2.MinorRadius();
542 Standard_Real A5 = MajR*MinR*Dx*Dy;
543 Standard_Real A1 = -2.*A5;
544 Standard_Real R2 = MajR*MajR;
545 Standard_Real r2 = MinR*MinR;
546 Standard_Real A2 =(R2*Dx*Dx -r2*Dy*Dy -R2 +r2)/2.0;
547 Standard_Real A3 = MinR*Vxyz.Y();
548 Standard_Real A4 = -MajR*Vxyz.X();
550 Standard_Real aEps=1.e-12;
552 if(fabs(A5) <= aEps) A5 = 0.;
553 if(fabs(A1) <= aEps) A1 = 0.;
554 if(fabs(A2) <= aEps) A2 = 0.;
555 if(fabs(A3) <= aEps) A3 = 0.;
556 if(fabs(A4) <= aEps) A4 = 0.;
558 ExtremaExtElC_TrigonometricRoots Sol(A1,A2,A3,A4,A5,0.,M_PI+M_PI);
559 if (!Sol.IsDone()) { return; }
561 // Storage of solutions ...
564 Standard_Integer NbSol = Sol.NbSolutions();
565 for (Standard_Integer NoSol = 1; NoSol <= NbSol; NoSol++) {
566 U2 = Sol.Value(NoSol);
567 P2 = ElCLib::Value(U2,C2);
568 U1 = (gp_Vec(O1,P2)).Dot(D1);
569 P1 = ElCLib::Value(U1,C1);
570 mySqDist[myNbExt] = P1.SquareDistance(P2);
571 myPoint[myNbExt][0] = Extrema_POnCurv(U1,P1);
572 myPoint[myNbExt][1] = Extrema_POnCurv(U2,P2);
575 myDone = Standard_True;
578 //=======================================================================
579 //function : Extrema_ExtElC
581 //=======================================================================
582 Extrema_ExtElC::Extrema_ExtElC (const gp_Lin& C1,
585 /*-----------------------------------------------------------------------------
587 Find extrema between straight line C1 and hyperbola C2.
590 Let P1=C1(u1) and P2=C2(u2) be two solution points
591 D the direction of straight line C1
592 T the tangent at point P2;
593 Then, ( P1P2.D = 0. (1)
595 Let O1 and O2 be the origins of C1 and C2;
596 Then, (1) <=> (O1P2-u1*D).D = 0. as O1P1 = u1*D
597 <=> u1 = O1P2.D as D.D = 1.
598 (2) <=> (P1O2 + O2P2).T= 0.
599 <=> ((P2O1.D)D+O1O2 + O2P2).T = 0. as P1O1 = -u1*D = (P2O1.D)D
600 <=> (((P2O2+O2O1).D)D+O1O2 + O2P2).T = 0.
601 <=> (P2O2.D)(D.T)+((O2O1.D)D-O2O1).T + O2P2.T= 0.
602 We are in the reference of the hyperbola; let:
603 by writing P (R* Chu, r* Shu, 0.0)
604 and Chu = (v**2 + 1)/(2*v) ,
605 Shu = (V**2 - 1)/(2*v)
609 V (Vx,Vy,Vz) = (O2O1.D)D-O2O1;
611 Then we obtain the following equation by v:
612 (-2*R*r*Dx*Dy - R*R*Dx*Dx-r*r*Dy*Dy + R*R + r*r) * v**4 +
613 (2*R*Vx + 2*r*Vy) * v**3 +
614 (-2*R*Vx + 2*r*Vy) * v +
615 (-2*R*r*Dx*Dy - (R*R*Dx*Dx-r*r*Dy*Dy + R*R + r*r)) = 0
618 Use the algorithm math_DirectPolynomialRoots to solve this equation.
619 -----------------------------------------------------------------------------*/
620 myIsPar = Standard_False;
621 myDone = Standard_False;
624 // Calculate T1 in the reference of the hyperbola...
625 gp_Dir D = C1.Direction();
628 x2 = C2.XAxis().Direction();
629 y2 = C2.YAxis().Direction();
630 z2 = C2.Axis().Direction();
631 Standard_Real Dx = D.Dot(x2);
632 Standard_Real Dy = D.Dot(y2);
633 Standard_Real Dz = D.Dot(z2);
634 D.SetCoord(Dx,Dy,Dz);
637 gp_Pnt O1 = C1.Location();
638 gp_Pnt O2 = C2.Location();
640 O2O1.SetCoord(O2O1.Dot(x2), O2O1.Dot(y2), O2O1.Dot(z2));
641 gp_XYZ Vxyz = (D.XYZ()*(O2O1.Dot(D)))-O2O1.XYZ();
642 Standard_Real Vx = Vxyz.X();
643 Standard_Real Vy = Vxyz.Y();
645 // Calculate coefficients of the equation by v
646 Standard_Real R = C2.MajorRadius();
647 Standard_Real r = C2.MinorRadius();
648 Standard_Real a = -2*R*r*Dx*Dy;
649 Standard_Real b = -R*R*Dx*Dx - r*r*Dy*Dy + R*R + r*r;
650 Standard_Real A1 = a + b;
651 Standard_Real A2 = 2*R*Vx + 2*r*Vy;
652 Standard_Real A4 = -2*R*Vx + 2*r*Vy;
653 Standard_Real A5 = a - b;
655 math_DirectPolynomialRoots Sol(A1,A2,0.0,A4, A5);
656 if (!Sol.IsDone()) { return; }
658 // Store solutions ...
660 Standard_Real U1,U2, v;
661 Standard_Integer NbSol = Sol.NbSolutions();
662 for (Standard_Integer NoSol = 1; NoSol <= NbSol; NoSol++) {
663 v = Sol.Value(NoSol);
666 P2 = ElCLib::Value(U2,C2);
667 U1 = (gp_Vec(O1,P2)).Dot(D1);
668 P1 = ElCLib::Value(U1,C1);
669 mySqDist[myNbExt] = P1.SquareDistance(P2);
670 myPoint[myNbExt][0] = Extrema_POnCurv(U1,P1);
671 myPoint[myNbExt][1] = Extrema_POnCurv(U2,P2);
675 myDone = Standard_True;
677 //=======================================================================
678 //function : Extrema_ExtElC
680 //=======================================================================
681 Extrema_ExtElC::Extrema_ExtElC (const gp_Lin& C1,
684 /*-----------------------------------------------------------------------------
686 Find extreme distances between straight line C1 and parabole C2.
689 Let P1=C1(u1) and P2=C2(u2) be two solution points
690 D the direction of straight line C1
691 T the tangent to point P2;
692 Then, ( P1P2.D = 0. (1)
694 Let O1 and O2 be the origins of C1 and C2;
695 Then, (1) <=> (O1P2-u1*D).D = 0. as O1P1 = u1*D
696 <=> u1 = O1P2.D as D.D = 1.
697 (2) <=> (P1O2 + O2P2).T= 0.
698 <=> ((P2O1.D)D+O1O2 + O2P2).T = 0. as P1O1 = -u1*D = (P2O1.D)D
699 <=> (((P2O2+O2O1).D)D+O1O2 + O2P2).T = 0.
700 <=> (P2O2.D)(D.T)+((O2O1.D)D-O2O1).T + O2P2.T = 0.
701 We are in the reference of the parabola; let:
705 V (Vx,Vy,Vz) = (O2O1.D)D-O2O1;
707 Then, get the following equation by y:
708 ((1-Dx*Dx)/(2*p*p)) * y*y*y + A1
709 (-3*Dx*Dy/(2*p)) * y*y + A2
710 (1-Dy*Dy + Vx/p) * y + A3
713 Use the algorithm math_DirectPolynomialRoots to solve this equation.
714 -----------------------------------------------------------------------------*/
715 myIsPar = Standard_False;
716 myDone = Standard_False;
719 // Calculate T1 in the reference of the parabola...
720 gp_Dir D = C1.Direction();
723 x2 = C2.XAxis().Direction();
724 y2 = C2.YAxis().Direction();
725 z2 = C2.Axis().Direction();
726 Standard_Real Dx = D.Dot(x2);
727 Standard_Real Dy = D.Dot(y2);
728 Standard_Real Dz = D.Dot(z2);
729 D.SetCoord(Dx,Dy,Dz);
732 gp_Pnt O1 = C1.Location();
733 gp_Pnt O2 = C2.Location();
735 O2O1.SetCoord(O2O1.Dot(x2), O2O1.Dot(y2), O2O1.Dot(z2));
736 gp_XYZ Vxyz = (D.XYZ()*(O2O1.Dot(D)))-O2O1.XYZ();
738 // Calculate coefficients of the equation by y
739 Standard_Real P = C2.Parameter();
740 Standard_Real A1 = (1-Dx*Dx)/(2.0*P*P);
741 Standard_Real A2 = (-3.0*Dx*Dy/(2.0*P));
742 Standard_Real A3 = (1 - Dy*Dy + Vxyz.X()/P);
743 Standard_Real A4 = Vxyz.Y();
745 math_DirectPolynomialRoots Sol(A1,A2,A3,A4);
746 if (!Sol.IsDone()) { return; }
748 // Storage of solutions ...
751 Standard_Integer NbSol = Sol.NbSolutions();
752 for (Standard_Integer NoSol = 1; NoSol <= NbSol; NoSol++) {
753 U2 = Sol.Value(NoSol);
754 P2 = ElCLib::Value(U2,C2);
755 U1 = (gp_Vec(O1,P2)).Dot(D1);
756 P1 = ElCLib::Value(U1,C1);
757 mySqDist[myNbExt] = P1.SquareDistance(P2);
758 myPoint[myNbExt][0] = Extrema_POnCurv(U1,P1);
759 myPoint[myNbExt][1] = Extrema_POnCurv(U2,P2);
762 myDone = Standard_True;
764 //=======================================================================
765 //function : Extrema_ExtElC
767 //=======================================================================
768 Extrema_ExtElC::Extrema_ExtElC (const gp_Circ& C1,
771 Standard_Boolean bIsSamePlane, bIsSameAxe;
772 Standard_Real aTolD, aTolD2, aTolA, aD2, aDC2;
776 myIsPar = Standard_False;
777 myDone = Standard_False;
780 aTolA=Precision::Angular();
781 aTolD=Precision::Confusion();
785 aDc1=C1.Axis().Direction();
788 aDc2=C2.Axis().Direction();
789 gp_Pln aPlc1(aPc1, aDc1);
791 aD2=aPlc1.SquareDistance(aPc2);
792 bIsSamePlane=aDc1.IsParallel(aDc2, aTolA) && aD2<aTolD2;
797 aDC2=aPc1.SquareDistance(aPc2);
798 bIsSameAxe=aDC2<aTolD2;
801 myIsPar = Standard_True;
802 Standard_Real dR = C1.Radius() - C2.Radius();
803 Standard_Real dC = C1.Location().Distance(C2.Location());
804 mySqDist[0] = dR*dR + dC*dC;
805 dR = C1.Radius() + C2.Radius();
806 mySqDist[1] = dR*dR + dC*dC;
807 myDone = Standard_True;
810 Standard_Boolean bIn, bOut;
811 Standard_Integer j1, j2;
812 Standard_Real aR1, aR2, aD12, aT11, aT12, aT21, aT22;
814 gp_Pnt aP11, aP12, aP21, aP22;
816 myDone = Standard_True;
832 aR1=aC1.Radius(); // max radius
833 aR2=aC2.Radius(); // min radius
838 aD12=aPc1.Distance(aPc2);
839 gp_Vec aVec12(aPc1, aPc2);
840 gp_Dir aDir12(aVec12);
842 // 1. Four common solutions
845 aP11.SetXYZ(aPc1.XYZ()-aR1*aDir12.XYZ());
846 aP12.SetXYZ(aPc1.XYZ()+aR1*aDir12.XYZ());
847 aP21.SetXYZ(aPc2.XYZ()-aR2*aDir12.XYZ());
848 aP22.SetXYZ(aPc2.XYZ()+aR2*aDir12.XYZ());
850 aT11=ElCLib::Parameter(aC1, aP11);
851 aT12=ElCLib::Parameter(aC1, aP12);
852 aT21=ElCLib::Parameter(aC2, aP21);
853 aT22=ElCLib::Parameter(aC2, aP22);
856 myPoint[0][j1].SetValues(aT11, aP11);
857 myPoint[0][j2].SetValues(aT21, aP21);
858 mySqDist[0]=aP11.SquareDistance(aP21);
860 myPoint[1][j1].SetValues(aT11, aP11);
861 myPoint[1][j2].SetValues(aT22, aP22);
862 mySqDist[1]=aP11.SquareDistance(aP22);
865 myPoint[2][j1].SetValues(aT12, aP12);
866 myPoint[2][j2].SetValues(aT21, aP21);
867 mySqDist[2]=aP12.SquareDistance(aP21);
870 myPoint[3][j1].SetValues(aT12, aP12);
871 myPoint[3][j2].SetValues(aT22, aP22);
872 mySqDist[3]=aP12.SquareDistance(aP22);
874 // 2. Check for intersections
875 bOut=aD12>(aR1+aR2+aTolD);
876 bIn =aD12<(aR1-aR2-aTolD);
878 Standard_Boolean bNbExt6;
879 Standard_Real aAlpha, aBeta, aT[2], aVal, aDist2;
880 gp_Pnt aPt, aPL1, aPL2;
883 aAlpha=0.5*(aR1*aR1-aR2*aR2+aD12*aD12)/aD12;
884 aVal=aR1*aR1-aAlpha*aAlpha;
885 if (aVal<0.) {// see pkv/900/L4 for details
889 //aBeta=Sqrt(aR1*aR1-aAlpha*aAlpha);
891 aPt.SetXYZ(aPc1.XYZ()+aAlpha*aDir12.XYZ());
894 aPL1.SetXYZ(aPt.XYZ()+aBeta*aDLt.XYZ());
895 aPL2.SetXYZ(aPt.XYZ()-aBeta*aDLt.XYZ());
897 aDist2=aPL1.SquareDistance(aPL2);
898 bNbExt6=aDist2>aTolD2;
900 myNbExt=5;// just in case. see pkv/900/L4 for details
901 aT[j1]=ElCLib::Parameter(aC1, aPL1);
902 aT[j2]=ElCLib::Parameter(aC2, aPL1);
903 myPoint[4][j1].SetValues(aT[j1], aPL1);
904 myPoint[4][j2].SetValues(aT[j2], aPL1);
909 aT[j1]=ElCLib::Parameter(aC1, aPL2);
910 aT[j2]=ElCLib::Parameter(aC2, aPL2);
911 myPoint[5][j1].SetValues(aT[j1], aPL2);
912 myPoint[5][j2].SetValues(aT[j2], aPL2);
916 }// if (!bOut || !bIn) {
919 //=======================================================================
920 //function : Extrema_ExtElC
922 //=======================================================================
923 Extrema_ExtElC::Extrema_ExtElC (const gp_Circ&, const gp_Elips&)
925 Standard_NotImplemented::Raise();
927 //=======================================================================
928 //function : Extrema_ExtElC
930 //=======================================================================
931 Extrema_ExtElC::Extrema_ExtElC (const gp_Circ&, const gp_Hypr&)
933 Standard_NotImplemented::Raise();
935 //=======================================================================
936 //function : Extrema_ExtElC
938 //=======================================================================
939 Extrema_ExtElC::Extrema_ExtElC (const gp_Circ&, const gp_Parab&)
941 Standard_NotImplemented::Raise();
943 //=======================================================================
944 //function : Extrema_ExtElC
946 //=======================================================================
947 Extrema_ExtElC::Extrema_ExtElC (const gp_Elips&, const gp_Elips&)
949 Standard_NotImplemented::Raise();
951 //=======================================================================
952 //function : Extrema_ExtElC
954 //=======================================================================
955 Extrema_ExtElC::Extrema_ExtElC (const gp_Elips&, const gp_Hypr&)
957 Standard_NotImplemented::Raise();
959 //=======================================================================
960 //function : Extrema_ExtElC
962 //=======================================================================
963 Extrema_ExtElC::Extrema_ExtElC (const gp_Elips&, const gp_Parab&)
965 Standard_NotImplemented::Raise();
967 //=======================================================================
968 //function : Extrema_ExtElC
970 //=======================================================================
971 Extrema_ExtElC::Extrema_ExtElC (const gp_Hypr&, const gp_Hypr&)
973 Standard_NotImplemented::Raise();
975 //=======================================================================
976 //function : Extrema_ExtElC
978 //=======================================================================
979 Extrema_ExtElC::Extrema_ExtElC (const gp_Hypr&, const gp_Parab&)
981 Standard_NotImplemented::Raise();
983 //=======================================================================
984 //function : Extrema_ExtElC
986 //=======================================================================
987 Extrema_ExtElC::Extrema_ExtElC (const gp_Parab&, const gp_Parab&)
989 Standard_NotImplemented::Raise();
991 //=======================================================================
994 //=======================================================================
995 Standard_Boolean Extrema_ExtElC::IsDone () const {
998 //=======================================================================
999 //function : IsParallel
1001 //=======================================================================
1002 Standard_Boolean Extrema_ExtElC::IsParallel () const
1005 StdFail_NotDone::Raise();
1009 //=======================================================================
1012 //=======================================================================
1013 Standard_Integer Extrema_ExtElC::NbExt () const
1016 StdFail_InfiniteSolutions::Raise();
1020 //=======================================================================
1021 //function : SquareDistance
1023 //=======================================================================
1024 Standard_Real Extrema_ExtElC::SquareDistance (const Standard_Integer N) const
1027 StdFail_NotDone::Raise();
1030 if (N < 1 || N > 2) {
1031 Standard_OutOfRange::Raise();
1035 if (N < 1 || N > NbExt()) {
1036 Standard_OutOfRange::Raise();
1039 return mySqDist[N-1];
1041 //=======================================================================
1044 //=======================================================================
1045 void Extrema_ExtElC::Points (const Standard_Integer N,
1046 Extrema_POnCurv& P1,
1047 Extrema_POnCurv& P2) const
1049 if (N < 1 || N > NbExt()) {
1050 Standard_OutOfRange::Raise();
1052 P1 = myPoint[N-1][0];
1053 P2 = myPoint[N-1][1];
1057 //=======================================================================
1058 //function : RefineDir
1060 //=======================================================================
1061 void RefineDir(gp_Dir& aDir)
1063 Standard_Integer i, j, k, iK;
1064 Standard_Real aCx[3], aEps, aX1, aX2, aOne;
1068 aDir.Coord(aCx[0], aCx[1], aCx[2]);
1070 for (i=0; i<iK; ++i) {
1071 aOne=(aCx[i]>0.) ? 1. : -1.;
1075 if (aCx[i]>aX1 && aCx[i]<aX2) {
1081 aDir.SetCoord(aCx[0], aCx[1], aCx[2]);