0023948: Wrong intersection between a surface of revolution and a plane.
[occt.git] / src / GccAna / GccAna_Circ2d3Tan_7.cxx
1 // Copyright (c) 1995-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 //
4 // This file is part of Open CASCADE Technology software library.
5 //
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License 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.
11 //
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
14
15 #include <GccAna_Circ2d3Tan.jxx>
16
17 #include <ElCLib.hxx>
18 #include <IntAna2d_AnaIntersection.hxx>
19 #include <IntAna2d_IntPoint.hxx>
20 #include <gp_Lin2d.hxx>
21 #include <gp_Circ2d.hxx>
22 #include <gp_Dir2d.hxx>
23 #include <TColStd_Array1OfReal.hxx>
24 #include <GccAna_CircPnt2dBisec.hxx>
25 #include <GccAna_Pnt2dBisec.hxx>
26 #include <GccInt_IType.hxx>
27 #include <GccInt_BCirc.hxx>
28 #include <GccInt_BLine.hxx>
29 #include <GccInt_BElips.hxx>
30 #include <GccInt_BHyper.hxx>
31 #include <IntAna2d_Conic.hxx>
32 #include <GccEnt_BadQualifier.hxx>
33 #include <Precision.hxx>
34
35 //=======================================================================
36 //   Creation of a circle tangent to a circle and two points.           +
37 //=======================================================================
38
39 GccAna_Circ2d3Tan::
40    GccAna_Circ2d3Tan (const GccEnt_QualifiedCirc& Qualified1 ,
41                       const gp_Pnt2d&             Point2     ,
42                       const gp_Pnt2d&             Point3     ,
43                       const Standard_Real         Tolerance  ):
44
45    cirsol(1,2)     ,
46    qualifier1(1,2) ,
47    qualifier2(1,2) ,
48    qualifier3(1,2) ,
49    TheSame1(1,2)   ,
50    TheSame2(1,2)   ,
51    TheSame3(1,2)   ,
52    pnttg1sol(1,2)  ,
53    pnttg2sol(1,2)  ,
54    pnttg3sol(1,2)  ,
55    par1sol(1,2)    ,
56    par2sol(1,2)    ,
57    par3sol(1,2)    ,
58    pararg1(1,2)    ,
59    pararg2(1,2)    ,
60    pararg3(1,2)    
61 {
62
63    gp_Dir2d dirx(1.0,0.0);
64    Standard_Real Tol = Abs(Tolerance);
65    WellDone = Standard_False;
66    NbrSol = 0;
67    if (!(Qualified1.IsEnclosed() || Qualified1.IsEnclosing() || 
68          Qualified1.IsOutside() || Qualified1.IsUnqualified())) {
69      GccEnt_BadQualifier::Raise();
70      return;
71    }
72
73 //=========================================================================
74 //   Processing.                                                          +
75 //=========================================================================
76
77    gp_Circ2d C1 = Qualified1.Qualified();
78    Standard_Real R1 = C1.Radius();
79    gp_Pnt2d center1(C1.Location());
80    TColStd_Array1OfReal Radius(1,2);
81
82    if (Point2.IsEqual(Point3,Precision::Confusion())) {
83      WellDone = Standard_False;
84      return ;
85    }
86
87    GccAna_Pnt2dBisec Bis1(Point2,Point3);
88    GccAna_CircPnt2dBisec Bis2(C1,Point2);
89
90    if (Bis1.IsDone() && Bis2.IsDone()) {
91      Standard_Integer nbsolution2 = Bis2.NbSolutions();
92      for (Standard_Integer i = 1 ; i <=  nbsolution2; i++) {
93        Handle(GccInt_Bisec) Sol2 = Bis2.ThisSolution(i);
94        GccInt_IType typ2 = Sol2->ArcType();
95        gp_Lin2d Sol1(Bis1.ThisSolution());
96        IntAna2d_AnaIntersection Intp;
97        if (typ2 == GccInt_Cir) {
98          Intp.Perform(Sol1,Sol2->Circle());
99        }
100        else if (typ2 == GccInt_Lin) {
101          Intp.Perform(Sol1,Sol2->Line());
102        }
103        else if (typ2 == GccInt_Hpr) {
104          Intp.Perform(Sol1,IntAna2d_Conic(Sol2->Hyperbola()));
105        }
106        else if (typ2 == GccInt_Ell) {
107          Intp.Perform(Sol1,IntAna2d_Conic(Sol2->Ellipse()));
108        }
109
110        if (Intp.IsDone()) {
111          if (!Intp.IsEmpty()) {
112            for (Standard_Integer j = 1 ; j <= Intp.NbPoints() ; j++) {
113              gp_Pnt2d Center(Intp.Point(j).Value());
114              Standard_Real dist1 = Center.Distance(center1);
115              Standard_Real dist2 = Center.Distance(Point2);
116              Standard_Real dist3 = Center.Distance(Point3);
117              Standard_Integer nbsol1 = 0;
118 //           Standard_Integer nbsol2 = 0;
119              Standard_Integer nbsol3 = 0;
120              Standard_Boolean ok = Standard_False;
121              if (Qualified1.IsEnclosed()) {
122                if (dist1-R1 < Tolerance) {
123                  Radius(1) = Abs(R1-dist1);
124                  nbsol1 = 1;
125                  ok = Standard_True;
126                }
127              }
128              else if (Qualified1.IsOutside()) {
129                if (R1-dist1 < Tolerance) {
130                  Radius(1) = Abs(R1-dist1);
131                  nbsol1 = 1;
132                  ok = Standard_True;
133                }
134              }
135              else if (Qualified1.IsEnclosing()) {
136                ok = Standard_True;
137                nbsol1 = 1;
138                Radius(1) = R1+dist1;
139              }
140              else if (Qualified1.IsUnqualified()) {
141                ok = Standard_True;
142                nbsol1 = 2;
143                Radius(1) = Abs(R1-dist1);
144                Radius(2) = R1+dist1;
145              }
146              if (ok) {
147                ok = Standard_False;
148                for (Standard_Integer ii = 1 ; ii <= nbsol1 ; ii++) {
149 //pop            if (Abs(dist2-Radius(ii))<=Tol && Abs(dist2-Radius(ii))<=Tol){
150                  if (Abs(dist2-Radius(ii))<=Tol && Abs(dist3-Radius(ii))<=Tol){
151                    nbsol3 = ii;
152                    ok = Standard_True;
153                  }
154                }
155              }
156
157              if (ok) {
158 //             for (Standard_Integer k = 1 ; k <= nbsol3 ; k++) {
159                  if (NbrSol>=2) break;
160                  NbrSol++;
161 //               cirsol(NbrSol) = gp_Circ2d(gp_Ax2d(Center,dirx),Radius(k));
162                  cirsol(NbrSol) = gp_Circ2d(gp_Ax2d(Center,dirx),Radius(nbsol3));
163 //               ==========================================================
164                  Standard_Real distcc1 = Center.Distance(center1);
165                  if (!Qualified1.IsUnqualified()) { 
166                    qualifier1(NbrSol) = Qualified1.Qualifier();
167                  }
168                  else if (Abs(distcc1+Radius(nbsol3)-R1) < Tol) {
169                    qualifier1(NbrSol) = GccEnt_enclosed;
170                  }
171                  else if (Abs(distcc1-R1-Radius(nbsol3)) < Tol) {
172                    qualifier1(NbrSol) = GccEnt_outside;
173                  }
174                  else { qualifier1(NbrSol) = GccEnt_enclosing; }
175                  qualifier2(NbrSol) = GccEnt_noqualifier;
176                  qualifier3(NbrSol) = GccEnt_noqualifier;
177                  if (Center.Distance(center1) <= Tolerance &&
178                      Abs(Radius(nbsol3)-R1) <= Tolerance) {
179                    TheSame1(NbrSol) = 1;
180                  }
181                  else {
182                    TheSame1(NbrSol) = 0;
183                    gp_Dir2d dc(center1.XY()-Center.XY());
184                    pnttg1sol(NbrSol)=gp_Pnt2d(Center.XY()+Radius(nbsol3)*dc.XY());
185                    par1sol(NbrSol)=ElCLib::Parameter(cirsol(NbrSol),
186                                                     pnttg1sol(NbrSol));
187                    pararg1(NbrSol)=ElCLib::Parameter(C1,pnttg1sol(NbrSol));
188                  }
189
190                  TheSame2(NbrSol) = 0;
191                  pnttg2sol(NbrSol) = Point2;
192                  par2sol(NbrSol)=ElCLib::Parameter(cirsol(NbrSol),
193                                                   pnttg2sol(NbrSol));
194                  pararg2(NbrSol)=0.;
195                  TheSame3(NbrSol) = 0;
196                  pnttg3sol(NbrSol) = Point3;
197                  par3sol(NbrSol)=ElCLib::Parameter(cirsol(NbrSol),
198                                                   pnttg3sol(NbrSol));
199                  pararg3(NbrSol) = 0.;
200                //}
201              }
202            }
203          }
204          WellDone = Standard_True;
205        }
206      }
207    }
208  }
209
210