0026937: Eliminate NO_CXX_EXCEPTION macro support
[occt.git] / src / GccAna / GccAna_Circ2d3Tan_5.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 // init. de MinRad et MaxRad (PRO15604), JCT 09/10/98
16
17 #include <ElCLib.hxx>
18 #include <GccAna_Circ2d3Tan.hxx>
19 #include <GccAna_CircLin2dBisec.hxx>
20 #include <GccAna_LinPnt2dBisec.hxx>
21 #include <GccEnt_BadQualifier.hxx>
22 #include <GccEnt_QualifiedCirc.hxx>
23 #include <GccEnt_QualifiedLin.hxx>
24 #include <GccInt_BLine.hxx>
25 #include <GccInt_BParab.hxx>
26 #include <GccInt_IType.hxx>
27 #include <gp_Circ2d.hxx>
28 #include <gp_Dir2d.hxx>
29 #include <gp_Lin2d.hxx>
30 #include <gp_Pnt2d.hxx>
31 #include <IntAna2d_AnaIntersection.hxx>
32 #include <IntAna2d_Conic.hxx>
33 #include <IntAna2d_IntPoint.hxx>
34 #include <Standard_OutOfRange.hxx>
35 #include <StdFail_NotDone.hxx>
36 #include <TColStd_Array1OfReal.hxx>
37
38 //===========================================================================
39 //   Creation of a circle tangent to a circle, a straight line and a point. +
40 //===========================================================================
41 GccAna_Circ2d3Tan::
42    GccAna_Circ2d3Tan (const GccEnt_QualifiedCirc& Qualified1 ,
43                       const GccEnt_QualifiedLin&  Qualified2 ,
44                       const gp_Pnt2d&             Point3     ,
45                       const Standard_Real         Tolerance  ):
46
47 //=========================================================================
48 //   Initialization of fields.                                           +
49 //=========================================================================
50
51    cirsol(1,4)     ,
52    qualifier1(1,4) ,
53    qualifier2(1,4) ,
54    qualifier3(1,4) ,
55    TheSame1(1,4)   ,  
56    TheSame2(1,4)   ,
57    TheSame3(1,4)   ,
58    pnttg1sol(1,4)  ,
59    pnttg2sol(1,4)  ,
60    pnttg3sol(1,4)  ,
61    par1sol(1,4)    ,
62    par2sol(1,4)    ,
63    par3sol(1,4)    ,
64    pararg1(1,4)    ,
65    pararg2(1,4)    ,
66    pararg3(1,4)    
67 {
68
69    gp_Dir2d dirx(1.0,0.0);
70    Standard_Real Tol = Abs(Tolerance);
71    Standard_Real MaxRad = 1e10, MinRad = 1e-6;
72    WellDone = Standard_False;
73    NbrSol = 0;
74    if (!(Qualified1.IsEnclosed() || Qualified1.IsEnclosing() || 
75          Qualified1.IsOutside() || Qualified1.IsUnqualified()) ||
76        !(Qualified2.IsEnclosed() ||
77          Qualified2.IsOutside() || Qualified2.IsUnqualified())) {
78      throw GccEnt_BadQualifier();
79      return;
80    }
81
82 //=========================================================================
83 //   Processing.                                                          +
84 //=========================================================================
85
86    gp_Circ2d C1(Qualified1.Qualified());
87    gp_Lin2d L2(Qualified2.Qualified());
88    Standard_Real R1      = C1.Radius();
89    gp_Pnt2d center1(C1.Location());
90    gp_Pnt2d origin2(L2.Location());
91    gp_Dir2d dir2(L2.Direction());
92    gp_Dir2d normL2(-dir2.Y(),dir2.X());
93
94    TColStd_Array1OfReal Radius(1,2);
95    GccAna_CircLin2dBisec Bis1(C1,L2);
96    GccAna_LinPnt2dBisec Bis2(L2,Point3);
97    if (Bis1.IsDone() && Bis2.IsDone()) {
98      Standard_Integer nbsolution1 = Bis1.NbSolutions();
99      for (Standard_Integer i = 1 ; i <=  nbsolution1; i++) {
100        Handle(GccInt_Bisec) Sol1 = Bis1.ThisSolution(i);
101        Handle(GccInt_Bisec) Sol2 = Bis2.ThisSolution();
102        GccInt_IType typ1 = Sol1->ArcType();
103        GccInt_IType typ2 = Sol2->ArcType();
104        IntAna2d_AnaIntersection Intp;
105        if (typ1 == GccInt_Lin) {
106          if (typ2 == GccInt_Lin) {
107            Intp.Perform(Sol1->Line(),Sol2->Line());
108          }
109          else if (typ2 == GccInt_Par) {
110            Intp.Perform(Sol1->Line(),IntAna2d_Conic(Sol2->Parabola()));
111          }
112        }
113        else if (typ1 == GccInt_Par) {
114          if (typ2 == GccInt_Lin) {
115            Intp.Perform(Sol2->Line(),IntAna2d_Conic(Sol1->Parabola()));
116          }
117          else if (typ2 == GccInt_Par) {
118            Intp.Perform(Sol1->Parabola(),IntAna2d_Conic(Sol2->Parabola()));
119          }
120        }
121        if (Intp.IsDone()) {
122          if (!Intp.IsEmpty()) {
123            for (Standard_Integer j = 1 ; j <= Intp.NbPoints() ; j++) {
124              gp_Pnt2d Center(Intp.Point(j).Value());
125              Standard_Real dist1 = Center.Distance(C1.Location());
126              Standard_Real dist2 = L2.Distance(Center);
127              Standard_Real dist3 = Center.Distance(Point3);
128              Standard_Integer nbsol1 = 0;
129              Standard_Integer nbsol3 = 0;
130              Standard_Boolean ok = Standard_False;
131              if (Qualified1.IsEnclosed()) {
132                if (dist1-R1 < Tolerance) {
133                  Radius(1) = Abs(R1-dist1);
134                  nbsol1 = 1;
135                  ok = Standard_True;
136                }
137              }
138              else if (Qualified1.IsOutside()) {
139                if (R1-dist1 < Tolerance) {
140                  Radius(1) = Abs(R1-dist1);
141                  nbsol1 = 1;
142                  ok = Standard_True;
143                }
144              }
145              else if (Qualified1.IsEnclosing()) {
146                ok = Standard_True;
147                nbsol1 = 1;
148                Radius(1) = Abs(R1-dist1);
149              }
150              else if (Qualified1.IsUnqualified()) {
151                ok = Standard_True;
152                nbsol1 = 2;
153                Radius(1) = Abs(R1-dist1);
154                Radius(2) = R1+dist1;
155              }
156              if (Qualified2.IsEnclosed() && ok) {
157                if ((((L2.Location().X()-Center.X())*(-L2.Direction().Y()))+
158                     ((L2.Location().Y()-Center.Y())*(L2.Direction().X())))<=0){
159                  for (Standard_Integer ii = 1 ; ii <= nbsol1 ; ii++) {
160                    if (Abs(dist2-Radius(ii)) < Tol) { 
161                      ok = Standard_True;
162                      Radius(1) = Radius(ii);
163                    }
164                  }
165                }
166              }
167              else if (Qualified2.IsOutside() && ok) {
168                if ((((L2.Location().X()-Center.X())*(-L2.Direction().Y()))+
169                     ((L2.Location().Y()-Center.Y())*(L2.Direction().X())))>=0){
170                  for (Standard_Integer ii = 1 ; ii <= nbsol1 ; ii++) {
171                    if (Abs(dist2-Radius(ii)) < Tol) { 
172                      ok = Standard_True;
173                      Radius(1) = Radius(ii);
174                    }
175                  }
176                }
177              }
178              else if (Qualified2.IsUnqualified() && ok) {
179                for (Standard_Integer ii = 1 ; ii <= nbsol1 ; ii++) {
180                  if (Abs(dist2-Radius(ii)) < Tol) { 
181                    ok = Standard_True;
182                    Radius(1) = Radius(ii);
183                  }
184                }
185              }
186              if (Abs(dist3-Radius(1)) <= Tol && ok) {
187                ok = Standard_True;
188                nbsol3 = 1;
189              }
190              if (ok) {
191                for (Standard_Integer k = 1 ; k <= nbsol3 ; k++) {
192                  if (NbrSol==4)
193                    break;
194 // pop : if the radius is too great - no creation                
195                  if (Radius(k) > MaxRad) break;
196                  if (Abs(Radius(k)) < MinRad) break;
197
198                  NbrSol++;
199                  cirsol(NbrSol) = gp_Circ2d(gp_Ax2d(Center,dirx),Radius(k));
200 //               ==========================================================
201                  Standard_Real distcc1 = Center.Distance(center1);
202                  if (!Qualified1.IsUnqualified()) { 
203                    qualifier1(NbrSol) = Qualified1.Qualifier();
204                  }
205                  else if (Abs(distcc1+Radius(k)-R1) < Tol) {
206                    qualifier1(NbrSol) = GccEnt_enclosed;
207                  }
208                  else if (Abs(distcc1-R1-Radius(k)) < Tol) {
209                    qualifier1(NbrSol) = GccEnt_outside;
210                  }
211                  else { qualifier1(NbrSol) = GccEnt_enclosing; }
212                  gp_Dir2d dc2(origin2.XY()-Center.XY());
213                  if (!Qualified2.IsUnqualified()) { 
214                    qualifier2(NbrSol) = Qualified2.Qualifier();
215                  }
216                  else if (dc2.Dot(normL2) > 0.0) {
217                    qualifier2(NbrSol) = GccEnt_outside;
218                  }
219                  else { qualifier2(NbrSol) = GccEnt_enclosed; }
220                  qualifier3(NbrSol) = GccEnt_noqualifier;
221                  if (Center.Distance(C1.Location()) <= Tolerance &&
222                      Abs(Radius(k)-R1) <= Tolerance) {
223                    TheSame1(NbrSol) = 1;
224                  }
225                  else {
226                    TheSame1(NbrSol) = 0;
227 //  modified by NIZHNY-EAP Mon Nov  1 13:48:21 1999 ___BEGIN___
228 //                 gp_Dir2d dc(C1.Location().XY()-Center.XY());
229                    gp_Dir2d dc(Center.XY()-C1.Location().XY());
230 //  modified by NIZHNY-EAP Mon Nov  1 13:48:55 1999 ___END___
231                    pnttg1sol(NbrSol)=gp_Pnt2d(Center.XY()+Radius(k)*dc.XY());
232                    par1sol(NbrSol)=ElCLib::Parameter(cirsol(NbrSol),
233                                                           pnttg1sol(NbrSol));
234                    pararg1(NbrSol)=ElCLib::Parameter(C1,pnttg1sol(NbrSol));
235                  }
236                  TheSame2(NbrSol) = 0;
237                  TheSame3(NbrSol) = 0;
238                  gp_Dir2d dc(L2.Location().XY()-Center.XY());
239                  Standard_Real sign = dc.Dot(gp_Dir2d(-L2.Direction().Y(),
240                                              L2.Direction().X()));
241                  dc = gp_Dir2d(sign*gp_XY(-L2.Direction().Y(),
242                                           L2.Direction().X()));
243                  pnttg2sol(NbrSol) = gp_Pnt2d(Center.XY()+Radius(k)*dc.XY());
244                  par2sol(NbrSol)=ElCLib::Parameter(cirsol(NbrSol),
245                                                   pnttg2sol(NbrSol));
246                  pararg2(NbrSol)=ElCLib::Parameter(L2,pnttg2sol(NbrSol));
247                  pnttg3sol(NbrSol) = Point3;
248                  par3sol(NbrSol)=ElCLib::Parameter(cirsol(NbrSol),
249                                                   pnttg3sol(NbrSol));
250                  pararg3(NbrSol) = 0.;
251                }
252              }
253            }
254          }
255          WellDone = Standard_True;
256        }
257        if (NbrSol==4)
258          break;
259      }
260    }
261  }