1 // Copyright (c) 1995-1999 Matra Datavision
2 // Copyright (c) 1999-2012 OPEN CASCADE SAS
4 // The content of this file is subject to the Open CASCADE Technology Public
5 // License Version 6.5 (the "License"). You may not use the content of this file
6 // except in compliance with the License. Please obtain a copy of the License
7 // at http://www.opencascade.org and read it completely before using this file.
9 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
10 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12 // The Original Code and all software distributed under the License is
13 // distributed on an "AS IS" basis, without warranty of any kind, and the
14 // Initial Developer hereby disclaims all such warranties, including without
15 // limitation, any warranties of merchantability, fitness for a particular
16 // purpose or non-infringement. Please see the License for the specific terms
17 // and conditions governing the rights and limitations under the License.
22 #include <Extrema_ExtElC.ixx>
23 #include <StdFail_InfiniteSolutions.hxx>
24 #include <StdFail_NotDone.hxx>
26 #include <math_TrigonometricFunctionRoots.hxx>
27 #include <math_DirectPolynomialRoots.hxx>
28 #include <Standard_OutOfRange.hxx>
29 #include <Standard_NotImplemented.hxx>
30 #include <Precision.hxx>
31 #include <Extrema_ExtPElC.hxx>
32 #include <IntAna_QuadQuadGeo.hxx>
33 #include <Extrema_ExtPElC.hxx>
43 void RefineDir(gp_Dir& aDir);
45 //=======================================================================
46 //class : ExtremaExtElC_TrigonometricRoots
48 //== Classe Interne (Donne des racines classees d un polynome trigo)
49 //== Code duplique avec IntAna_IntQuadQuad.cxx (lbr le 26 mars 98)
50 //== Solution fiable aux problemes de coefficients proches de 0
51 //== avec essai de rattrapage si coeff<1.e-10 (jct le 27 avril 98)
52 //=======================================================================
53 class ExtremaExtElC_TrigonometricRoots {
55 Standard_Real Roots[4];
56 Standard_Boolean done;
57 Standard_Integer NbRoots;
58 Standard_Boolean infinite_roots;
60 ExtremaExtElC_TrigonometricRoots(const Standard_Real CC,
61 const Standard_Real SC,
62 const Standard_Real C,
63 const Standard_Real S,
64 const Standard_Real Cte,
65 const Standard_Real Binf,
66 const Standard_Real Bsup);
68 Standard_Boolean IsDone() {
72 Standard_Boolean IsARoot(Standard_Real u) {
73 Standard_Real PIpPI, aEps;
77 for(Standard_Integer i=0 ; i<NbRoots; i++) {
78 if(Abs(u - Roots[i])<=aEps) {
79 return Standard_True ;
81 if(Abs(u - Roots[i]-PIpPI)<=aEps) {
85 return Standard_False;
88 Standard_Integer NbSolutions() {
90 StdFail_NotDone::Raise();
95 Standard_Boolean InfiniteRoots() {
97 StdFail_NotDone::Raise();
99 return infinite_roots;
102 Standard_Real Value(const Standard_Integer& n) {
103 if((!done)||(n>NbRoots)) {
104 StdFail_NotDone::Raise();
109 //=======================================================================
110 //function : ExtremaExtElC_TrigonometricRoots
112 //=======================================================================
113 ExtremaExtElC_TrigonometricRoots::
114 ExtremaExtElC_TrigonometricRoots(const Standard_Real CC,
115 const Standard_Real SC,
116 const Standard_Real C,
117 const Standard_Real S,
118 const Standard_Real Cte,
119 const Standard_Real Binf,
120 const Standard_Real Bsup)
122 Standard_Integer i, nbessai;
123 Standard_Real cc ,sc, c, s, cte;
132 while (nbessai<=2 && !done) {
133 //-- F= AA*CN*CN+2*BB*CN*SN+CC*CN+DD*SN+EE;
134 math_TrigonometricFunctionRoots MTFR(cc,sc,c,s,cte,Binf,Bsup);
138 if(MTFR.InfiniteRoots()) {
139 infinite_roots=Standard_True;
142 Standard_Boolean Triee;
143 Standard_Integer j, SvNbRoots;
144 Standard_Real aTwoPI, aMaxCoef, aPrecision;
147 NbRoots=MTFR.NbSolutions();
148 for(i=0;i<NbRoots;++i) {
149 Roots[i]=MTFR.Value(i+1);
151 Roots[i]=Roots[i]+aTwoPI;
153 if(Roots[i]>aTwoPI) {
154 Roots[i]=Roots[i]-aTwoPI;
158 //-- La recherche directe donne n importe quoi.
159 aMaxCoef = Max(CC,SC);
160 aMaxCoef = Max(aMaxCoef,C);
161 aMaxCoef = Max(aMaxCoef,S);
162 aMaxCoef = Max(aMaxCoef,Cte);
163 aPrecision = Max(1.e-8, 1.e-12*aMaxCoef);
166 for(i=0; i<SvNbRoots; ++i) {
168 Standard_Real co=cos(Roots[i]);
169 Standard_Real si=sin(Roots[i]);
170 y=co*(CC*co + (SC+SC)*si + C) + S*si + Cte;
171 // modified by OCC Tue Oct 3 18:43:00 2006
172 if(Abs(y)>aPrecision) {
182 for(i=1, j=0; i<SvNbRoots; ++i, ++j) {
183 if(Roots[i]<Roots[j]) {
184 Triee=Standard_False;
193 infinite_roots=Standard_False;
194 if(NbRoots==0) { //--!!!!! Detect case Pol = Cte ( 1e-50 ) !!!!
195 if((Abs(CC) + Abs(SC) + Abs(C) + Abs(S)) < 1e-10) {
196 if(Abs(Cte) < 1e-10) {
197 infinite_roots=Standard_True;
202 } // if(MTFR.IsDone()) {
204 // try to set very small coefficients to ZERO
222 } // while (nbessai<=2 && !done) {
225 //=======================================================================
226 //function : Extrema_ExtElC
228 //=======================================================================
229 Extrema_ExtElC::Extrema_ExtElC ()
231 myDone = Standard_False;
233 //=======================================================================
234 //function : Extrema_ExtElC
236 //=======================================================================
237 Extrema_ExtElC::Extrema_ExtElC (const gp_Lin& C1,
241 // Find min distance between 2 straight lines.
244 // Let D1 and D2, be 2 directions of straight lines C1 and C2.
245 // 2 cases are considered:
246 // 1- if Angle(D1,D2) < AngTol, straight lines are parallel.
247 // The distance is the distance between a point of C1 and the straight line C2.
248 // 2- if Angle(D1,D2) > AngTol:
249 // Let P1=C1(u1) and P2=C2(u2) be 2 solution points:
250 // Then, ( P1P2.D1 = 0. (1)
251 // ( P1P2.D2 = 0. (2)
252 // Let O1 and O2 be the origins of C1 and C2;
253 // THen, (1) <=> (O1P2-u1*D1).D1 = 0. as O1P1 = u1*D1
254 // <=> u1 = O1P2.D1 as D1.D1 = 1.
255 // (2) <=> (P1O2+u2*D2).D2 = 0. as O2P2 = u2*D2
256 // <=> u2 = O2P1.D2 as D2.D2 = 1.
257 // <=> u2 = (O2O1+O1P1).D2
258 // <=> u2 = O2O1.D2+((O1P2.T1)T1).T2) as O1P1 = u1*T1 = (O1P2.T1)T1
259 // <=> u2 = O2O1.D2+(((O1O2+O2P2).D1)D1).D2)
260 // <=> u2 = O2O1.D2+((O1O2.D1)D1).D2)+(O2P2.D1)(D1.D2)
261 // <=> u2 = ((O1O2.D1)D1-O1O2).D2 + u2*(D2.D1)(D1.D2)
262 // <=> u2 = (((O1O2.D1)D1-O1O2).D2) / 1.-(D1.D2)**2
264 myDone = Standard_False;
267 gp_Dir D1 = C1.Position().Direction();
268 gp_Dir D2 = C2.Position().Direction();
269 // MSV 16/01/2000: avoid "divide by zero"
270 Standard_Real D1DotD2 = D1.Dot(D2);
271 Standard_Real aSin = 1.-D1DotD2*D1DotD2;
272 if (aSin < gp::Resolution() ||
273 D1.IsParallel(D2, Precision::Angular())) {
274 myIsPar = Standard_True;
275 mySqDist[0] = C2.SquareDistance(C1.Location());
276 mySqDist[1] = mySqDist[0];
279 myIsPar = Standard_False;
280 gp_Pnt O1 = C1.Location();
281 gp_Pnt O2 = C2.Location();
283 Standard_Real U2 = (D1.XYZ()*(O1O2.Dot(D1))-(O1O2.XYZ())).Dot(D2.XYZ());
284 if( Precision::IsInfinite(U2) ) {
285 myIsPar = Standard_True;
286 mySqDist[0] = C2.SquareDistance(C1.Location());
287 mySqDist[1] = mySqDist[0];
291 if( Precision::IsInfinite(U2) ) {
292 myIsPar = Standard_True;
293 mySqDist[0] = C2.SquareDistance(C1.Location());
294 mySqDist[1] = mySqDist[0];
297 gp_Pnt P2(ElCLib::Value(U2,C2));
298 Standard_Real U1 = (gp_Vec(O1,P2)).Dot(D1);
299 if( Precision::IsInfinite(U1) ) {
300 myIsPar = Standard_True;
301 mySqDist[0] = C2.SquareDistance(C1.Location());
302 mySqDist[1] = mySqDist[0];
305 gp_Pnt P1(ElCLib::Value(U1,C1));
306 mySqDist[myNbExt] = P1.SquareDistance(P2);
307 myPoint[myNbExt][0] = Extrema_POnCurv(U1,P1);
308 myPoint[myNbExt][1] = Extrema_POnCurv(U2,P2);
314 myDone = Standard_True;
316 //=======================================================================
317 //function : Extrema_ExtElC
319 // Find extreme distances between straight line C1 and circle C2.
322 // Let P1=C1(u1) and P2=C2(u2) be two solution points
323 // D the direction of straight line C1
324 // T tangent at point P2;
325 // Then, ( P1P2.D = 0. (1)
327 // Let O1 and O2 be the origins of C1 and C2;
328 // Then, (1) <=> (O1P2-u1*D).D = 0. as O1P1 = u1*D
329 // <=> u1 = O1P2.D as D.D = 1.
330 // (2) <=> P1O2.T = 0. as O2P2.T = 0.
331 // <=> ((P2O1.D)D+O1O2).T = 0. as P1O1 = -u1*D = (P2O1.D)D
332 // <=> (((P2O2+O2O1).D)D+O1O2).T = 0.
333 // <=> ((P2O2.D)(D.T)+((O2O1.D)D-O2O1).T = 0.
334 // We are in the reference of the circle; let:
335 // Cos = Cos(u2) and Sin = Sin(u2),
336 // P2 (R*Cos,R*Sin,0.),
337 // T (-R*Sin,R*Cos,0.),
339 // V (Vx,Vy,Vz) = (O2O1.D)D-O2O1;
340 // Then, the equation by Cos and Sin is as follows:
341 // -(2*R*R*Dx*Dy) * Cos**2 + A1
342 // R*R*(Dx**2-Dy**2) * Cos*Sin + 2* A2
346 //Use the algorithm math_TrigonometricFunctionRoots to solve this equation.
347 //=======================================================================
348 Extrema_ExtElC::Extrema_ExtElC (const gp_Lin& C1,
352 Standard_Real Dx,Dy,Dz,aRO2O1, aTolRO2O1;
353 Standard_Real R, A1, A2, A3, A4, A5, aTol;
354 gp_Dir x2, y2, z2, D, D1;
356 myIsPar = Standard_False;
357 myDone = Standard_False;
360 // Calculate T1 in the reference of the circle ...
363 x2 = C2.XAxis().Direction();
364 y2 = C2.YAxis().Direction();
365 z2 = C2.Axis().Direction();
370 D.SetCoord(Dx, Dy, Dz);
374 // Calcul de V dans le repere du cercle:
375 gp_Pnt O1 = C1.Location();
376 gp_Pnt O2 = C2.Location();
379 aTolRO2O1=gp::Resolution();
380 aRO2O1=O2O1.Magnitude();
381 if (aRO2O1 > aTolRO2O1) {
384 O2O1.Multiply(1./aRO2O1);
385 aDO2O1.SetCoord(O2O1.Dot(x2), O2O1.Dot(y2), O2O1.Dot(z2));
387 O2O1.SetXYZ(aRO2O1*aDO2O1.XYZ());
390 O2O1.SetCoord(O2O1.Dot(x2), O2O1.Dot(y2), O2O1.Dot(z2));
393 gp_XYZ Vxyz = (D.XYZ()*(O2O1.Dot(D)))-O2O1.XYZ();
395 //modified by NIZNHY-PKV Tue Mar 20 10:36:38 2012
400 A2 = R*R*(Dx*Dx-Dy*Dy)/2.;
407 if(fabs(A5) <= aTol) {
410 if(fabs(A1) <= aTol) {
413 if(fabs(A2) <= aTol) {
416 if(fabs(A3) <= aTol) {
419 if(fabs(A4) <= aTol) {
425 // Calculate the coefficients of the equation by Cos and Sin ...
430 A2 = 0.5*R*(Dx*Dx-Dy*Dy);// /2.;
434 if (A1>=-aTol && A1<=aTol) {
437 if (A2>=-aTol && A2<=aTol) {
440 if (A3>=-aTol && A3<=aTol) {
443 if (A4>=-aTol && A4<=aTol) {
446 if (A5>=-aTol && A5<=aTol) {
449 //modified by NIZNHY-PKV Tue Mar 20 10:36:40 2012t
451 ExtremaExtElC_TrigonometricRoots Sol(A1, A2, A3, A4, A5, 0., M_PI+M_PI);
455 if (Sol.InfiniteRoots()) {
456 myIsPar = Standard_True;
458 myDone = Standard_True;
461 // Storage of solutions ...
462 Standard_Integer NoSol, NbSol;
466 NbSol = Sol.NbSolutions();
467 for (NoSol=1; NoSol<=NbSol; ++NoSol) {
468 U2 = Sol.Value(NoSol);
469 P2 = ElCLib::Value(U2,C2);
470 U1 = (gp_Vec(O1,P2)).Dot(D1);
471 P1 = ElCLib::Value(U1,C1);
472 mySqDist[myNbExt] = P1.SquareDistance(P2);
473 //modified by NIZNHY-PKV Wed Mar 21 08:11:33 2012f
474 //myPoint[myNbExt][0] = Extrema_POnCurv(U1,P1);
475 //myPoint[myNbExt][1] = Extrema_POnCurv(U2,P2);
476 myPoint[myNbExt][0].SetValues(U1,P1);
477 myPoint[myNbExt][1].SetValues(U2,P2);
478 //modified by NIZNHY-PKV Wed Mar 21 08:11:36 2012t
481 myDone = Standard_True;
483 //=======================================================================
484 //function : Extrema_ExtElC
486 //=======================================================================
487 Extrema_ExtElC::Extrema_ExtElC (const gp_Lin& C1,
490 /*-----------------------------------------------------------------------------
492 Find extreme distances between straight line C1 and ellipse C2.
495 Let P1=C1(u1) and P2=C2(u2) two solution points
496 D the direction of straight line C1
497 T the tangent to point P2;
498 Then, ( P1P2.D = 0. (1)
500 Let O1 and O2 be the origins of C1 and C2;
501 Then, (1) <=> (O1P2-u1*D).D = 0. as O1P1 = u1*D
502 <=> u1 = O1P2.D as D.D = 1.
503 (2) <=> P1O2.T = 0. as O2P2.T = 0.
504 <=> ((P2O1.D)D+O1O2).T = 0. as P1O1 = -u1*D = (P2O1.D)D
505 <=> (((P2O2+O2O1).D)D+O1O2).T = 0.
506 <=> ((P2O2.D)(D.T)+((O2O1.D)D-O2O1).T = 0.
507 We are in the reference of the ellipse; let:
508 Cos = Cos(u2) and Sin = Sin(u2),
509 P2 (MajR*Cos,MinR*Sin,0.),
510 T (-MajR*Sin,MinR*Cos,0.),
512 V (Vx,Vy,Vz) = (O2O1.D)D-O2O1;
513 Then, get the following equation by Cos and Sin:
514 -(2*MajR*MinR*Dx*Dy) * Cos**2 +
515 (MajR*MajR*Dx**2-MinR*MinR*Dy**2) * Cos*Sin +
519 Use algorithm math_TrigonometricFunctionRoots to solve this equation.
520 -----------------------------------------------------------------------------*/
521 myIsPar = Standard_False;
522 myDone = Standard_False;
525 // Calculate T1 the reference of the ellipse ...
526 gp_Dir D = C1.Direction();
529 x2 = C2.XAxis().Direction();
530 y2 = C2.YAxis().Direction();
531 z2 = C2.Axis().Direction();
532 Standard_Real Dx = D.Dot(x2);
533 Standard_Real Dy = D.Dot(y2);
534 Standard_Real Dz = D.Dot(z2);
535 D.SetCoord(Dx,Dy,Dz);
538 gp_Pnt O1 = C1.Location();
539 gp_Pnt O2 = C2.Location();
541 O2O1.SetCoord(O2O1.Dot(x2), O2O1.Dot(y2), O2O1.Dot(z2));
542 gp_XYZ Vxyz = (D.XYZ()*(O2O1.Dot(D)))-O2O1.XYZ();
544 // Calculate the coefficients of the equation by Cos and Sin ...
545 Standard_Real MajR = C2.MajorRadius();
546 Standard_Real MinR = C2.MinorRadius();
547 Standard_Real A5 = MajR*MinR*Dx*Dy;
548 Standard_Real A1 = -2.*A5;
549 Standard_Real R2 = MajR*MajR;
550 Standard_Real r2 = MinR*MinR;
551 Standard_Real A2 =(R2*Dx*Dx -r2*Dy*Dy -R2 +r2)/2.0;
552 Standard_Real A3 = MinR*Vxyz.Y();
553 Standard_Real A4 = -MajR*Vxyz.X();
555 Standard_Real aEps=1.e-12;
557 if(fabs(A5) <= aEps) A5 = 0.;
558 if(fabs(A1) <= aEps) A1 = 0.;
559 if(fabs(A2) <= aEps) A2 = 0.;
560 if(fabs(A3) <= aEps) A3 = 0.;
561 if(fabs(A4) <= aEps) A4 = 0.;
563 ExtremaExtElC_TrigonometricRoots Sol(A1,A2,A3,A4,A5,0.,M_PI+M_PI);
564 if (!Sol.IsDone()) { return; }
566 // Storage of solutions ...
569 Standard_Integer NbSol = Sol.NbSolutions();
570 for (Standard_Integer NoSol = 1; NoSol <= NbSol; NoSol++) {
571 U2 = Sol.Value(NoSol);
572 P2 = ElCLib::Value(U2,C2);
573 U1 = (gp_Vec(O1,P2)).Dot(D1);
574 P1 = ElCLib::Value(U1,C1);
575 mySqDist[myNbExt] = P1.SquareDistance(P2);
576 myPoint[myNbExt][0] = Extrema_POnCurv(U1,P1);
577 myPoint[myNbExt][1] = Extrema_POnCurv(U2,P2);
580 myDone = Standard_True;
583 //=======================================================================
584 //function : Extrema_ExtElC
586 //=======================================================================
587 Extrema_ExtElC::Extrema_ExtElC (const gp_Lin& C1,
590 /*-----------------------------------------------------------------------------
592 Find extrema between straight line C1 and hyperbola C2.
595 Let P1=C1(u1) and P2=C2(u2) be two solution points
596 D the direction of straight line C1
597 T the tangent at point P2;
598 Then, ( P1P2.D = 0. (1)
600 Let O1 and O2 be the origins of C1 and C2;
601 Then, (1) <=> (O1P2-u1*D).D = 0. as O1P1 = u1*D
602 <=> u1 = O1P2.D as D.D = 1.
603 (2) <=> (P1O2 + O2P2).T= 0.
604 <=> ((P2O1.D)D+O1O2 + O2P2).T = 0. as P1O1 = -u1*D = (P2O1.D)D
605 <=> (((P2O2+O2O1).D)D+O1O2 + O2P2).T = 0.
606 <=> (P2O2.D)(D.T)+((O2O1.D)D-O2O1).T + O2P2.T= 0.
607 We are in the reference of the hyperbola; let:
608 by writing P (R* Chu, r* Shu, 0.0)
609 and Chu = (v**2 + 1)/(2*v) ,
610 Shu = (V**2 - 1)/(2*v)
614 V (Vx,Vy,Vz) = (O2O1.D)D-O2O1;
616 Then we obtain the following equation by v:
617 (-2*R*r*Dx*Dy - R*R*Dx*Dx-r*r*Dy*Dy + R*R + r*r) * v**4 +
618 (2*R*Vx + 2*r*Vy) * v**3 +
619 (-2*R*Vx + 2*r*Vy) * v +
620 (-2*R*r*Dx*Dy - (R*R*Dx*Dx-r*r*Dy*Dy + R*R + r*r)) = 0
623 Use the algorithm math_DirectPolynomialRoots to solve this equation.
624 -----------------------------------------------------------------------------*/
625 myIsPar = Standard_False;
626 myDone = Standard_False;
629 // Calculate T1 in the reference of the hyperbola...
630 gp_Dir D = C1.Direction();
633 x2 = C2.XAxis().Direction();
634 y2 = C2.YAxis().Direction();
635 z2 = C2.Axis().Direction();
636 Standard_Real Dx = D.Dot(x2);
637 Standard_Real Dy = D.Dot(y2);
638 Standard_Real Dz = D.Dot(z2);
639 D.SetCoord(Dx,Dy,Dz);
642 gp_Pnt O1 = C1.Location();
643 gp_Pnt O2 = C2.Location();
645 O2O1.SetCoord(O2O1.Dot(x2), O2O1.Dot(y2), O2O1.Dot(z2));
646 gp_XYZ Vxyz = (D.XYZ()*(O2O1.Dot(D)))-O2O1.XYZ();
647 Standard_Real Vx = Vxyz.X();
648 Standard_Real Vy = Vxyz.Y();
650 // Calculate coefficients of the equation by v
651 Standard_Real R = C2.MajorRadius();
652 Standard_Real r = C2.MinorRadius();
653 Standard_Real a = -2*R*r*Dx*Dy;
654 Standard_Real b = -R*R*Dx*Dx - r*r*Dy*Dy + R*R + r*r;
655 Standard_Real A1 = a + b;
656 Standard_Real A2 = 2*R*Vx + 2*r*Vy;
657 Standard_Real A4 = -2*R*Vx + 2*r*Vy;
658 Standard_Real A5 = a - b;
660 math_DirectPolynomialRoots Sol(A1,A2,0.0,A4, A5);
661 if (!Sol.IsDone()) { return; }
663 // Store solutions ...
665 Standard_Real U1,U2, v;
666 Standard_Integer NbSol = Sol.NbSolutions();
667 for (Standard_Integer NoSol = 1; NoSol <= NbSol; NoSol++) {
668 v = Sol.Value(NoSol);
671 P2 = ElCLib::Value(U2,C2);
672 U1 = (gp_Vec(O1,P2)).Dot(D1);
673 P1 = ElCLib::Value(U1,C1);
674 mySqDist[myNbExt] = P1.SquareDistance(P2);
675 myPoint[myNbExt][0] = Extrema_POnCurv(U1,P1);
676 myPoint[myNbExt][1] = Extrema_POnCurv(U2,P2);
680 myDone = Standard_True;
682 //=======================================================================
683 //function : Extrema_ExtElC
685 //=======================================================================
686 Extrema_ExtElC::Extrema_ExtElC (const gp_Lin& C1,
689 /*-----------------------------------------------------------------------------
691 Find extreme distances between straight line C1 and parabole C2.
694 Let P1=C1(u1) and P2=C2(u2) be two solution points
695 D the direction of straight line C1
696 T the tangent to point P2;
697 Then, ( P1P2.D = 0. (1)
699 Let O1 and O2 be the origins of C1 and C2;
700 Then, (1) <=> (O1P2-u1*D).D = 0. as O1P1 = u1*D
701 <=> u1 = O1P2.D as D.D = 1.
702 (2) <=> (P1O2 + O2P2).T= 0.
703 <=> ((P2O1.D)D+O1O2 + O2P2).T = 0. as P1O1 = -u1*D = (P2O1.D)D
704 <=> (((P2O2+O2O1).D)D+O1O2 + O2P2).T = 0.
705 <=> (P2O2.D)(D.T)+((O2O1.D)D-O2O1).T + O2P2.T = 0.
706 We are in the reference of the parabola; let:
710 V (Vx,Vy,Vz) = (O2O1.D)D-O2O1;
712 Then, get the following equation by y:
713 ((1-Dx*Dx)/(2*p*p)) * y*y*y + A1
714 (-3*Dx*Dy/(2*p)) * y*y + A2
715 (1-Dy*Dy + Vx/p) * y + A3
718 Use the algorithm math_DirectPolynomialRoots to solve this equation.
719 -----------------------------------------------------------------------------*/
720 myIsPar = Standard_False;
721 myDone = Standard_False;
724 // Calculate T1 in the reference of the parabola...
725 gp_Dir D = C1.Direction();
728 x2 = C2.XAxis().Direction();
729 y2 = C2.YAxis().Direction();
730 z2 = C2.Axis().Direction();
731 Standard_Real Dx = D.Dot(x2);
732 Standard_Real Dy = D.Dot(y2);
733 Standard_Real Dz = D.Dot(z2);
734 D.SetCoord(Dx,Dy,Dz);
737 gp_Pnt O1 = C1.Location();
738 gp_Pnt O2 = C2.Location();
740 O2O1.SetCoord(O2O1.Dot(x2), O2O1.Dot(y2), O2O1.Dot(z2));
741 gp_XYZ Vxyz = (D.XYZ()*(O2O1.Dot(D)))-O2O1.XYZ();
743 // Calculate coefficients of the equation by y
744 Standard_Real P = C2.Parameter();
745 Standard_Real A1 = (1-Dx*Dx)/(2.0*P*P);
746 Standard_Real A2 = (-3.0*Dx*Dy/(2.0*P));
747 Standard_Real A3 = (1 - Dy*Dy + Vxyz.X()/P);
748 Standard_Real A4 = Vxyz.Y();
750 math_DirectPolynomialRoots Sol(A1,A2,A3,A4);
751 if (!Sol.IsDone()) { return; }
753 // Storage of solutions ...
756 Standard_Integer NbSol = Sol.NbSolutions();
757 for (Standard_Integer NoSol = 1; NoSol <= NbSol; NoSol++) {
758 U2 = Sol.Value(NoSol);
759 P2 = ElCLib::Value(U2,C2);
760 U1 = (gp_Vec(O1,P2)).Dot(D1);
761 P1 = ElCLib::Value(U1,C1);
762 mySqDist[myNbExt] = P1.SquareDistance(P2);
763 myPoint[myNbExt][0] = Extrema_POnCurv(U1,P1);
764 myPoint[myNbExt][1] = Extrema_POnCurv(U2,P2);
767 myDone = Standard_True;
769 //=======================================================================
770 //function : Extrema_ExtElC
772 //=======================================================================
773 Extrema_ExtElC::Extrema_ExtElC (const gp_Circ& C1,
776 Standard_Boolean bIsSamePlane, bIsSameAxe;
777 Standard_Real aTolD, aTolD2, aTolA, aD2, aDC2;
781 myIsPar = Standard_False;
782 myDone = Standard_False;
785 aTolA=Precision::Angular();
786 aTolD=Precision::Confusion();
790 aDc1=C1.Axis().Direction();
793 aDc2=C2.Axis().Direction();
794 gp_Pln aPlc1(aPc1, aDc1);
796 aD2=aPlc1.SquareDistance(aPc2);
797 bIsSamePlane=aDc1.IsParallel(aDc2, aTolA) && aD2<aTolD2;
802 aDC2=aPc1.SquareDistance(aPc2);
803 bIsSameAxe=aDC2<aTolD2;
806 myIsPar = Standard_True;
807 Standard_Real dR = C1.Radius() - C2.Radius();
808 Standard_Real dC = C1.Location().Distance(C2.Location());
809 mySqDist[0] = dR*dR + dC*dC;
810 dR = C1.Radius() + C2.Radius();
811 mySqDist[1] = dR*dR + dC*dC;
812 myDone = Standard_True;
815 Standard_Boolean bIn, bOut;
816 Standard_Integer j1, j2;
817 Standard_Real aR1, aR2, aD12, aT11, aT12, aT21, aT22;
819 gp_Pnt aP11, aP12, aP21, aP22;
821 myDone = Standard_True;
837 aR1=aC1.Radius(); // max radius
838 aR2=aC2.Radius(); // min radius
843 aD12=aPc1.Distance(aPc2);
844 gp_Vec aVec12(aPc1, aPc2);
845 gp_Dir aDir12(aVec12);
847 // 1. Four common solutions
850 aP11.SetXYZ(aPc1.XYZ()-aR1*aDir12.XYZ());
851 aP12.SetXYZ(aPc1.XYZ()+aR1*aDir12.XYZ());
852 aP21.SetXYZ(aPc2.XYZ()-aR2*aDir12.XYZ());
853 aP22.SetXYZ(aPc2.XYZ()+aR2*aDir12.XYZ());
855 aT11=ElCLib::Parameter(aC1, aP11);
856 aT12=ElCLib::Parameter(aC1, aP12);
857 aT21=ElCLib::Parameter(aC2, aP21);
858 aT22=ElCLib::Parameter(aC2, aP22);
861 myPoint[0][j1].SetValues(aT11, aP11);
862 myPoint[0][j2].SetValues(aT21, aP21);
863 mySqDist[0]=aP11.SquareDistance(aP21);
865 myPoint[1][j1].SetValues(aT11, aP11);
866 myPoint[1][j2].SetValues(aT22, aP22);
867 mySqDist[1]=aP11.SquareDistance(aP22);
870 myPoint[2][j1].SetValues(aT12, aP12);
871 myPoint[2][j2].SetValues(aT21, aP21);
872 mySqDist[2]=aP12.SquareDistance(aP21);
875 myPoint[3][j1].SetValues(aT12, aP12);
876 myPoint[3][j2].SetValues(aT22, aP22);
877 mySqDist[3]=aP12.SquareDistance(aP22);
879 // 2. Check for intersections
880 bOut=aD12>(aR1+aR2+aTolD);
881 bIn =aD12<(aR1-aR2-aTolD);
883 Standard_Boolean bNbExt6;
884 Standard_Real aAlpha, aBeta, aT[2], aVal, aDist2;
885 gp_Pnt aPt, aPL1, aPL2;
888 aAlpha=0.5*(aR1*aR1-aR2*aR2+aD12*aD12)/aD12;
889 aVal=aR1*aR1-aAlpha*aAlpha;
890 if (aVal<0.) {// see pkv/900/L4 for details
894 //aBeta=Sqrt(aR1*aR1-aAlpha*aAlpha);
896 aPt.SetXYZ(aPc1.XYZ()+aAlpha*aDir12.XYZ());
899 aPL1.SetXYZ(aPt.XYZ()+aBeta*aDLt.XYZ());
900 aPL2.SetXYZ(aPt.XYZ()-aBeta*aDLt.XYZ());
902 aDist2=aPL1.SquareDistance(aPL2);
903 bNbExt6=aDist2>aTolD2;
905 myNbExt=5;// just in case. see pkv/900/L4 for details
906 aT[j1]=ElCLib::Parameter(aC1, aPL1);
907 aT[j2]=ElCLib::Parameter(aC2, aPL1);
908 myPoint[4][j1].SetValues(aT[j1], aPL1);
909 myPoint[4][j2].SetValues(aT[j2], aPL1);
914 aT[j1]=ElCLib::Parameter(aC1, aPL2);
915 aT[j2]=ElCLib::Parameter(aC2, aPL2);
916 myPoint[5][j1].SetValues(aT[j1], aPL2);
917 myPoint[5][j2].SetValues(aT[j2], aPL2);
921 }// if (!bOut || !bIn) {
924 //=======================================================================
925 //function : Extrema_ExtElC
927 //=======================================================================
928 Extrema_ExtElC::Extrema_ExtElC (const gp_Circ&, const gp_Elips&)
930 Standard_NotImplemented::Raise();
932 //=======================================================================
933 //function : Extrema_ExtElC
935 //=======================================================================
936 Extrema_ExtElC::Extrema_ExtElC (const gp_Circ&, const gp_Hypr&)
938 Standard_NotImplemented::Raise();
940 //=======================================================================
941 //function : Extrema_ExtElC
943 //=======================================================================
944 Extrema_ExtElC::Extrema_ExtElC (const gp_Circ&, const gp_Parab&)
946 Standard_NotImplemented::Raise();
948 //=======================================================================
949 //function : Extrema_ExtElC
951 //=======================================================================
952 Extrema_ExtElC::Extrema_ExtElC (const gp_Elips&, const gp_Elips&)
954 Standard_NotImplemented::Raise();
956 //=======================================================================
957 //function : Extrema_ExtElC
959 //=======================================================================
960 Extrema_ExtElC::Extrema_ExtElC (const gp_Elips&, const gp_Hypr&)
962 Standard_NotImplemented::Raise();
964 //=======================================================================
965 //function : Extrema_ExtElC
967 //=======================================================================
968 Extrema_ExtElC::Extrema_ExtElC (const gp_Elips&, const gp_Parab&)
970 Standard_NotImplemented::Raise();
972 //=======================================================================
973 //function : Extrema_ExtElC
975 //=======================================================================
976 Extrema_ExtElC::Extrema_ExtElC (const gp_Hypr&, const gp_Hypr&)
978 Standard_NotImplemented::Raise();
980 //=======================================================================
981 //function : Extrema_ExtElC
983 //=======================================================================
984 Extrema_ExtElC::Extrema_ExtElC (const gp_Hypr&, const gp_Parab&)
986 Standard_NotImplemented::Raise();
988 //=======================================================================
989 //function : Extrema_ExtElC
991 //=======================================================================
992 Extrema_ExtElC::Extrema_ExtElC (const gp_Parab&, const gp_Parab&)
994 Standard_NotImplemented::Raise();
996 //=======================================================================
999 //=======================================================================
1000 Standard_Boolean Extrema_ExtElC::IsDone () const {
1003 //=======================================================================
1004 //function : IsParallel
1006 //=======================================================================
1007 Standard_Boolean Extrema_ExtElC::IsParallel () const
1010 StdFail_NotDone::Raise();
1014 //=======================================================================
1017 //=======================================================================
1018 Standard_Integer Extrema_ExtElC::NbExt () const
1021 StdFail_InfiniteSolutions::Raise();
1025 //=======================================================================
1026 //function : SquareDistance
1028 //=======================================================================
1029 Standard_Real Extrema_ExtElC::SquareDistance (const Standard_Integer N) const
1032 StdFail_NotDone::Raise();
1035 if (N < 1 || N > 2) {
1036 Standard_OutOfRange::Raise();
1040 if (N < 1 || N > NbExt()) {
1041 Standard_OutOfRange::Raise();
1044 return mySqDist[N-1];
1046 //=======================================================================
1049 //=======================================================================
1050 void Extrema_ExtElC::Points (const Standard_Integer N,
1051 Extrema_POnCurv& P1,
1052 Extrema_POnCurv& P2) const
1054 if (N < 1 || N > NbExt()) {
1055 Standard_OutOfRange::Raise();
1057 P1 = myPoint[N-1][0];
1058 P2 = myPoint[N-1][1];
1062 //=======================================================================
1063 //function : RefineDir
1065 //=======================================================================
1066 void RefineDir(gp_Dir& aDir)
1068 Standard_Integer i, j, k, iK;
1069 Standard_Real aCx[3], aEps, aX1, aX2, aOne;
1073 aDir.Coord(aCx[0], aCx[1], aCx[2]);
1075 for (i=0; i<iK; ++i) {
1076 aOne=(aCx[i]>0.) ? 1. : -1.;
1080 if (aCx[i]>aX1 && aCx[i]<aX2) {
1086 aDir.SetCoord(aCx[0], aCx[1], aCx[2]);