97d6ea42c59e129eb92a8dfc2fa1b0d7e31faac2
[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 nbsol1 = 0;
114              Standard_Integer nbsol2 = 0;
115              Standard_Integer nbsol3 = 0;
116              Standard_Boolean ok = Standard_False;
117              if (Qualified1.IsEnclosed()) {
118                if ((((origin1.X()-Center.X())*(-dir1.Y()))+
119                     ((origin1.Y()-Center.Y())*(dir1.X())))<=0){
120                  ok = Standard_True;
121                  nbsol1 = 1;
122                  Radius = dist1;
123                }
124              }
125              else if (Qualified1.IsOutside()) {
126                if ((((origin1.X()-Center.X())*(-dir1.Y()))+
127                     ((origin1.Y()-Center.Y())*(dir1.X())))>=0){
128                  ok = Standard_True;
129                  nbsol1 = 1;
130                  Radius = dist1;
131                }
132              }
133              else if (Qualified1.IsUnqualified()) {
134                ok = Standard_True;
135                nbsol1 = 1;
136                Radius = dist1;
137              }
138              if (Qualified2.IsEnclosed()) {
139                if ((((origin2.X()-Center.X())*(-dir2.Y()))+
140                     ((origin2.Y()-Center.Y())*(dir2.X())))<=0){
141                  if (Abs(dist2-Radius) < Tol) { nbsol2 = 1; }
142                  else { ok = Standard_False; }
143                }
144              }
145              else if (Qualified2.IsOutside() && ok) {
146                if ((((origin2.X()-Center.X())*(-dir2.Y()))+
147                     ((origin2.Y()-Center.Y())*(dir2.X())))>=0){
148                  if (Abs(dist2-Radius) < Tol) { nbsol2 = 1; }
149                  else { ok = Standard_False; }
150                }
151              }
152              else if (Qualified2.IsUnqualified() && ok) {
153                if (Abs(dist2-Radius) < Tol) { nbsol2 = 1; }
154                else { ok = Standard_False; }
155              }
156              if (ok) {
157                if (Abs(dist3-Radius) < Tol) { nbsol3 = 1; }
158                else { ok = Standard_False; }
159              }
160              if (ok) {
161                for (Standard_Integer k = 1 ; k <= nbsol3 ; k++) {
162                  NbrSol++;
163                  cirsol(NbrSol) = gp_Circ2d(gp_Ax2d(Center,dirx),Radius);
164 //               =======================================================
165                  gp_Dir2d dc1(origin1.XY()-Center.XY());
166                  if (!Qualified1.IsUnqualified()) { 
167                    qualifier1(NbrSol) = Qualified1.Qualifier();
168                  }
169                  else if (dc1.Dot(normL1) > 0.0) {
170                    qualifier1(NbrSol) = GccEnt_outside;
171                  }
172                  else { qualifier1(NbrSol) = GccEnt_enclosed; }
173                  gp_Dir2d dc2(origin2.XY()-Center.XY());
174                  if (!Qualified2.IsUnqualified()) { 
175                    qualifier2(NbrSol) = Qualified2.Qualifier();
176                  }
177                  else if (dc2.Dot(normL2) > 0.0) {
178                    qualifier2(NbrSol) = GccEnt_outside;
179                  }
180                  else { qualifier2(NbrSol) = GccEnt_enclosed; }
181                  qualifier3(NbrSol) = GccEnt_noqualifier;
182                  TheSame1(NbrSol) = 0;
183                  gp_Dir2d dc(origin1.XY()-Center.XY());
184                  Standard_Real sign = dc.Dot(gp_Dir2d(-dir1.Y(),dir1.X()));
185                  dc = gp_Dir2d(sign*gp_XY(-dir1.Y(),dir1.X()));
186                  pnttg1sol(NbrSol) = gp_Pnt2d(Center.XY()+Radius*dc.XY());
187                  par1sol(NbrSol)=ElCLib::Parameter(cirsol(NbrSol),
188                                                   pnttg1sol(NbrSol));
189                  pararg1(NbrSol)=ElCLib::Parameter(L1,pnttg1sol(NbrSol));
190                  TheSame2(NbrSol) = 0;
191                  dc = gp_Dir2d(origin2.XY()-Center.XY());
192                  sign = dc.Dot(gp_Dir2d(-dir2.Y(),dir2.X()));
193                  dc = gp_Dir2d(sign*gp_XY(-dir2.Y(),dir2.X()));
194                  pnttg2sol(NbrSol) = gp_Pnt2d(Center.XY()+Radius*dc.XY());
195                  par2sol(NbrSol)=ElCLib::Parameter(cirsol(NbrSol),
196                                                   pnttg2sol(NbrSol));
197                  pararg2(NbrSol)=ElCLib::Parameter(L2,pnttg2sol(NbrSol));
198                  TheSame3(NbrSol) = 0;
199                  par3sol(NbrSol)=ElCLib::Parameter(cirsol(NbrSol),
200                                                   pnttg3sol(NbrSol));
201                  pararg3(NbrSol) = 0.;
202                }
203              }
204            }
205          }
206          WellDone = Standard_True;
207        }
208      }
209    }
210  }
211