8e17cc1a3c265eea4984bf2a10ad791548eac7af
[occt.git] / src / GccAna / GccAna_Circ2d3Tan_6.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
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.
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 <GccAna_LinPnt2dBisec.hxx>
24 #include <GccAna_Lin2dBisec.hxx>
25 #include <GccInt_IType.hxx>
26 #include <GccInt_BLine.hxx>
27 #include <GccInt_BParab.hxx>
28 #include <IntAna2d_Conic.hxx>
29 #include <GccEnt_BadQualifier.hxx>
30
31 //=========================================================================
32 //   Creation of a circle tangent to two straight lines and a point.      +
33 //=========================================================================
34
35 GccAna_Circ2d3Tan::
36    GccAna_Circ2d3Tan (const GccEnt_QualifiedLin&  Qualified1 ,
37                       const GccEnt_QualifiedLin&  Qualified2 ,
38                       const gp_Pnt2d&             Point3     ,
39                       const Standard_Real         Tolerance  ):
40
41    cirsol(1,2)     ,
42    qualifier1(1,2) ,
43    qualifier2(1,2) ,
44    qualifier3(1,2) ,
45    TheSame1(1,2)   ,
46    TheSame2(1,2)   ,
47    TheSame3(1,2)   ,
48    pnttg1sol(1,2)  ,
49    pnttg2sol(1,2)  ,
50    pnttg3sol(1,2)  ,
51    par1sol(1,2)    ,  
52    par2sol(1,2)    ,
53    par3sol(1,2)    ,
54    pararg1(1,2)    ,
55    pararg2(1,2)    ,
56    pararg3(1,2)    
57 {
58
59    gp_Dir2d dirx(1.0,0.0);
60    WellDone = Standard_False;
61    Standard_Real Tol = Abs(Tolerance);
62    NbrSol = 0;
63    if (!(Qualified1.IsEnclosed() ||
64          Qualified1.IsOutside() || Qualified1.IsUnqualified()) ||
65        !(Qualified2.IsEnclosed() ||
66          Qualified2.IsOutside() || Qualified2.IsUnqualified())) {
67      GccEnt_BadQualifier::Raise();
68      return;
69    }
70
71    pnttg3sol.Init(Point3);
72
73 //=========================================================================
74 //   Processing.                                                          +
75 //=========================================================================
76
77    gp_Lin2d L1  = Qualified1.Qualified();
78    gp_Lin2d L2  = Qualified2.Qualified();
79    gp_Pnt2d origin1(L1.Location());
80    gp_Dir2d dir1(L1.Direction());
81    gp_Dir2d normL1(-dir1.Y(),dir1.X());
82    gp_Pnt2d origin2(L2.Location());
83    gp_Dir2d dir2(L2.Direction());
84    gp_Dir2d normL2(-dir2.Y(),dir2.X());
85
86    GccAna_Lin2dBisec Bis1(L1,L2);
87    GccAna_LinPnt2dBisec Bis2(L1,Point3);
88    if (Bis1.IsDone() && Bis2.IsDone()) {
89      Standard_Integer nbsolution1 = Bis1.NbSolutions();
90      Handle(GccInt_Bisec) Sol2 = Bis2.ThisSolution();
91      for (Standard_Integer i = 1 ; i <=  nbsolution1; i++) {
92 #ifdef DEB
93        gp_Lin2d Sol1(Bis1.ThisSolution(i));
94 #else
95        Bis1.ThisSolution(i) ;
96 #endif
97        GccInt_IType typ2 = Sol2->ArcType();
98        IntAna2d_AnaIntersection Intp;
99        if (typ2 == GccInt_Lin) {
100          Intp.Perform(Bis1.ThisSolution(i),Sol2->Line());
101        }
102        else if (typ2 == GccInt_Par) {
103          Intp.Perform(Bis1.ThisSolution(i),IntAna2d_Conic(Sol2->Parabola()));
104        }
105        if (Intp.IsDone()) {
106          if (!Intp.IsEmpty()) {
107            for (Standard_Integer j = 1 ; j <= Intp.NbPoints() ; j++) {
108              gp_Pnt2d Center(Intp.Point(j).Value());
109              Standard_Real dist1 = L1.Distance(Center);
110              Standard_Real dist2 = L2.Distance(Center);
111              Standard_Real dist3 = Center.Distance(Point3);
112              Standard_Real Radius=0;
113              Standard_Integer nbsol3 = 0;
114              Standard_Boolean ok = Standard_False;
115              if (Qualified1.IsEnclosed()) {
116                if ((((origin1.X()-Center.X())*(-dir1.Y()))+
117                     ((origin1.Y()-Center.Y())*(dir1.X())))<=0){
118                  ok = Standard_True;
119                  Radius = dist1;
120                }
121              }
122              else if (Qualified1.IsOutside()) {
123                if ((((origin1.X()-Center.X())*(-dir1.Y()))+
124                     ((origin1.Y()-Center.Y())*(dir1.X())))>=0){
125                  ok = Standard_True;
126                  Radius = dist1;
127                }
128              }
129              else if (Qualified1.IsUnqualified()) {
130                ok = Standard_True;
131                Radius = dist1;
132              }
133              if (Qualified2.IsEnclosed()) {
134                if ((((origin2.X()-Center.X())*(-dir2.Y()))+
135                     ((origin2.Y()-Center.Y())*(dir2.X())))<=0){
136                  if (Abs(dist2-Radius) < Tol) { }
137                  else { ok = Standard_False; }
138                }
139              }
140              else if (Qualified2.IsOutside() && ok) {
141                if ((((origin2.X()-Center.X())*(-dir2.Y()))+
142                     ((origin2.Y()-Center.Y())*(dir2.X())))>=0){
143                  if (Abs(dist2-Radius) < Tol) { }
144                  else { ok = Standard_False; }
145                }
146              }
147              else if (Qualified2.IsUnqualified() && ok) {
148                if (Abs(dist2-Radius) < Tol) { }
149                else { ok = Standard_False; }
150              }
151              if (ok) {
152                if (Abs(dist3-Radius) < Tol) { nbsol3 = 1; }
153                else { ok = Standard_False; }
154              }
155              if (ok) {
156                for (Standard_Integer k = 1 ; k <= nbsol3 ; k++) {
157                  NbrSol++;
158                  cirsol(NbrSol) = gp_Circ2d(gp_Ax2d(Center,dirx),Radius);
159 //               =======================================================
160                  gp_Dir2d dc1(origin1.XY()-Center.XY());
161                  if (!Qualified1.IsUnqualified()) { 
162                    qualifier1(NbrSol) = Qualified1.Qualifier();
163                  }
164                  else if (dc1.Dot(normL1) > 0.0) {
165                    qualifier1(NbrSol) = GccEnt_outside;
166                  }
167                  else { qualifier1(NbrSol) = GccEnt_enclosed; }
168                  gp_Dir2d dc2(origin2.XY()-Center.XY());
169                  if (!Qualified2.IsUnqualified()) { 
170                    qualifier2(NbrSol) = Qualified2.Qualifier();
171                  }
172                  else if (dc2.Dot(normL2) > 0.0) {
173                    qualifier2(NbrSol) = GccEnt_outside;
174                  }
175                  else { qualifier2(NbrSol) = GccEnt_enclosed; }
176                  qualifier3(NbrSol) = GccEnt_noqualifier;
177                  TheSame1(NbrSol) = 0;
178                  gp_Dir2d dc(origin1.XY()-Center.XY());
179                  Standard_Real sign = dc.Dot(gp_Dir2d(-dir1.Y(),dir1.X()));
180                  dc = gp_Dir2d(sign*gp_XY(-dir1.Y(),dir1.X()));
181                  pnttg1sol(NbrSol) = gp_Pnt2d(Center.XY()+Radius*dc.XY());
182                  par1sol(NbrSol)=ElCLib::Parameter(cirsol(NbrSol),
183                                                   pnttg1sol(NbrSol));
184                  pararg1(NbrSol)=ElCLib::Parameter(L1,pnttg1sol(NbrSol));
185                  TheSame2(NbrSol) = 0;
186                  dc = gp_Dir2d(origin2.XY()-Center.XY());
187                  sign = dc.Dot(gp_Dir2d(-dir2.Y(),dir2.X()));
188                  dc = gp_Dir2d(sign*gp_XY(-dir2.Y(),dir2.X()));
189                  pnttg2sol(NbrSol) = gp_Pnt2d(Center.XY()+Radius*dc.XY());
190                  par2sol(NbrSol)=ElCLib::Parameter(cirsol(NbrSol),
191                                                   pnttg2sol(NbrSol));
192                  pararg2(NbrSol)=ElCLib::Parameter(L2,pnttg2sol(NbrSol));
193                  TheSame3(NbrSol) = 0;
194                  par3sol(NbrSol)=ElCLib::Parameter(cirsol(NbrSol),
195                                                   pnttg3sol(NbrSol));
196                  pararg3(NbrSol) = 0.;
197                }
198              }
199            }
200          }
201          WellDone = Standard_True;
202        }
203      }
204    }
205  }
206