0026937: Eliminate NO_CXX_EXCEPTION macro support
[occt.git] / src / GccAna / GccAna_Circ2d3Tan_1.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
16 #include <ElCLib.hxx>
17 #include <GccAna_Circ2d3Tan.hxx>
18 #include <GccAna_CircLin2dBisec.hxx>
19 #include <GccEnt_BadQualifier.hxx>
20 #include <GccEnt_QualifiedCirc.hxx>
21 #include <GccEnt_QualifiedLin.hxx>
22 #include <GccInt_BLine.hxx>
23 #include <GccInt_BParab.hxx>
24 #include <GccInt_IType.hxx>
25 #include <gp_Circ2d.hxx>
26 #include <gp_Dir2d.hxx>
27 #include <gp_Lin2d.hxx>
28 #include <gp_Pnt2d.hxx>
29 #include <IntAna2d_AnaIntersection.hxx>
30 #include <IntAna2d_Conic.hxx>
31 #include <IntAna2d_IntPoint.hxx>
32 #include <Precision.hxx>
33 #include <Standard_OutOfRange.hxx>
34 #include <StdFail_NotDone.hxx>
35 #include <TColStd_Array1OfReal.hxx>
36
37 //=========================================================================
38 //   Creation of a circle tangent to two circles and a straight line.     +
39 //=========================================================================
40 GccAna_Circ2d3Tan::
41    GccAna_Circ2d3Tan (const GccEnt_QualifiedCirc& Qualified1,
42                       const GccEnt_QualifiedCirc& Qualified2,
43                       const GccEnt_QualifiedLin&  Qualified3,
44                       const Standard_Real         Tolerance ):
45
46 //=========================================================================
47 //   Initialization of fields.                                           +
48 //=========================================================================
49
50    cirsol(1,16)     , 
51    qualifier1(1,16) ,
52    qualifier2(1,16) ,
53    qualifier3(1,16),
54    TheSame1(1,16)   ,
55    TheSame2(1,16)   ,
56    TheSame3(1,16)   ,
57    pnttg1sol(1,16)  ,
58    pnttg2sol(1,16)  ,
59    pnttg3sol(1,16)  ,
60    par1sol(1,16)    ,
61    par2sol(1,16)    ,
62    par3sol(1,16)    ,
63    pararg1(1,16)    ,
64    pararg2(1,16)    ,
65    pararg3(1,16)    
66 {
67
68   gp_Dir2d dirx(1.0,0.0);
69   Standard_Real Tol = Abs(Tolerance);
70   WellDone = Standard_False;
71   NbrSol = 0;
72   if (!(Qualified1.IsEnclosed() || Qualified1.IsEnclosing() || 
73         Qualified1.IsOutside() || Qualified1.IsUnqualified()) ||
74       !(Qualified2.IsEnclosed() || Qualified2.IsEnclosing() || 
75         Qualified2.IsOutside() || Qualified2.IsUnqualified()) ||
76       !(Qualified3.IsEnclosed() || 
77         Qualified3.IsOutside() || Qualified3.IsUnqualified())) {
78     throw GccEnt_BadQualifier();
79     return;
80   }
81
82 //=========================================================================
83 //   Processing.                                                          +
84 //=========================================================================
85
86   gp_Circ2d C1 = Qualified1.Qualified();
87   gp_Circ2d C2 = Qualified2.Qualified();
88   gp_Lin2d L3 = Qualified3.Qualified();
89   Standard_Real R1 = C1.Radius();
90   Standard_Real R2 = C2.Radius();
91   gp_Pnt2d center1(C1.Location());
92 #ifdef OCCT_DEBUG
93   gp_Pnt2d center2(C2.Location());
94 #else
95   C2.Location() ;
96 #endif
97   gp_Pnt2d origin3(L3.Location());
98   gp_Dir2d dir3(L3.Direction());
99   gp_Dir2d normL3(-dir3.Y(),dir3.X());
100   
101   TColStd_Array1OfReal Radius(1,2);
102   GccAna_CircLin2dBisec Bis1(C1,L3);
103   GccAna_CircLin2dBisec Bis2(C2,L3);
104   if (Bis1.IsDone() && Bis2.IsDone()) {
105     Standard_Integer nbsolution1 = Bis1.NbSolutions();
106     Standard_Integer nbsolution2 = Bis2.NbSolutions();
107     for (Standard_Integer i = 1 ; i <=  nbsolution1; i++) {
108       Handle(GccInt_Bisec) Sol1 = Bis1.ThisSolution(i);
109       GccInt_IType typ1 = Sol1->ArcType();
110       IntAna2d_AnaIntersection Intp;
111       for (Standard_Integer k = 1 ; k <=  nbsolution2; k++) {
112         Handle(GccInt_Bisec) Sol2 = Bis2.ThisSolution(k);
113         GccInt_IType typ2 = Sol2->ArcType();
114         if (typ1 == GccInt_Lin) {
115           if (typ2 == GccInt_Lin) {
116             Intp.Perform(Sol1->Line(),Sol2->Line());
117           }
118           else if (typ2 == GccInt_Par) {
119             Intp.Perform(Sol1->Line(),IntAna2d_Conic(Sol2->Parabola()));
120           }
121         }
122         else if (typ1 == GccInt_Par) {
123           if (typ2 == GccInt_Lin) {
124             Intp.Perform(Sol2->Line(),IntAna2d_Conic(Sol1->Parabola()));
125           }
126           else if (typ2 == GccInt_Par) {
127             Intp.Perform(Sol1->Parabola(),IntAna2d_Conic(Sol2->Parabola()));
128           }
129         }
130         if (Intp.IsDone()) {
131           if (!Intp.IsEmpty()) {
132             for (Standard_Integer j = 1 ; j <= Intp.NbPoints() ; j++) {
133               Standard_Real Rradius=0;
134               gp_Pnt2d Center(Intp.Point(j).Value());
135
136 // pop : if the coordinates are too great, no creation           
137               if (Center.X() > 1e10 || 
138                   Center.Y() > 1e10  ) break;         
139
140               Standard_Real dist1 = Center.Distance(C1.Location());
141                Standard_Real dist2 = Center.Distance(C2.Location());
142               Standard_Real dist3 = L3.Distance(Center);
143
144 // pop : if the coordinates are too great, no creation                   
145               if (dist3 > 1e10  ) break;        
146
147               Standard_Integer nbsol1 = 0;
148               Standard_Integer nbsol2 = 0;
149                Standard_Integer nbsol3 = 0;
150               Standard_Boolean ok = Standard_False;
151               if (Qualified1.IsEnclosed()) {
152                 if (dist1-R1 < Tolerance) {
153                   Radius(1) = Abs(R1-dist1);
154                    nbsol1 = 1;
155                   ok = Standard_True;
156                 }
157               }
158               else if (Qualified1.IsOutside()) {
159                 if (R1-dist1 < Tolerance) {
160                   Radius(1) = Abs(R1-dist1);
161                   nbsol1 = 1;
162                   ok = Standard_True;
163                 }
164               }
165               else if (Qualified1.IsEnclosing()) {
166                 ok = Standard_True;
167                 nbsol1 = 1;
168                 Radius(1) = Abs(R1-dist1);
169                }
170               else if (Qualified1.IsUnqualified()) {
171                 ok = Standard_True;
172                 nbsol1 = 2;
173                 Radius(1) = Abs(R1-dist1);
174                 Radius(2) = R1+dist1;
175               }
176               if (Qualified2.IsEnclosed() && ok) {
177                 if (dist2-R2 < Tolerance) {
178                   for (Standard_Integer ii = 1 ; ii <= nbsol1 ; ii++) {
179                     if (Abs(Radius(ii)-Abs(R2-dist2)) < Tol) {
180                       Radius(1) = Abs(R2-dist2);
181                       ok = Standard_True;
182                       nbsol2 = 1;
183                     }
184                   }
185                 }
186               }
187               else if (Qualified2.IsOutside() && ok) {
188                 if (R2-dist2 < Tolerance) {
189                   for (Standard_Integer ii = 1 ; ii <= nbsol1 ; ii++) {
190                     if (Abs(Radius(ii)-Abs(R2-dist2)) < Tol) {
191                       Radius(1) = Abs(R2-dist2);
192                       ok = Standard_True;
193                       nbsol2 = 1;
194                     }
195                   }
196                 }
197               }
198               else if (Qualified2.IsEnclosing() && ok) {
199                 for (Standard_Integer ii = 1 ; ii <= nbsol1 ; ii++) {
200                   if (Abs(Radius(ii)-R2-dist2) < Tol) {
201                     Radius(1) = R2+dist2;
202                     ok = Standard_True;
203                     nbsol2 = 1;
204                   }
205                 }
206               }
207               else if (Qualified2.IsUnqualified() && ok) {
208                 for (Standard_Integer ii = 1 ; ii <= nbsol1 ; ii++) {
209                   if (Abs(Radius(ii)-Abs(R2-dist2)) < Tol) {
210                      Rradius = Abs(R2-dist2);
211                      ok = Standard_True;
212                      nbsol2++;
213                    }
214                   else if (Abs(Radius(ii)-R2-dist2) < Tol) {
215                     Rradius = R2+dist2;
216                     ok = Standard_True;
217                     nbsol2++;
218                   }
219                 }
220                 if (nbsol2 == 1) {
221                   Radius(1) = Rradius;
222                 }
223                 else if (nbsol2 == 2) {
224                   Radius(1) = Abs(R2-dist2);
225                   Radius(2) = R2+dist2;
226                 }
227               }
228               if (Qualified3.IsEnclosed() && ok) {
229                 if ((((L3.Location().X()-Center.X())*(-L3.Direction().Y()))+
230                     ((L3.Location().Y()-Center.Y())*(L3.Direction().X())))<=0){
231                   ok = Standard_True;
232                   nbsol3 = 1;
233                 }
234               }
235               else if (Qualified2.IsOutside() && ok) {
236                 if ((((L3.Location().X()-Center.X())*(-L3.Direction().Y()))+
237                     ((L3.Location().Y()-Center.Y())*(L3.Direction().X())))>=0){
238                   ok = Standard_True;
239                   nbsol3 = 1;
240                 }
241               }
242               else if (Qualified2.IsUnqualified() && ok) {
243                 ok = Standard_True;
244                 nbsol3 = 1;
245               }
246               if (ok) {
247                 for (Standard_Integer ind3 = 1 ; ind3 <= nbsol3 ; ind3++) {
248                   NbrSol++;
249                   cirsol(NbrSol) = gp_Circ2d(gp_Ax2d(Center,dirx),Radius(ind3));
250 //                ==========================================================
251                   Standard_Real distcc1 = Center.Distance(center1);
252                   if (!Qualified1.IsUnqualified()) { 
253                     qualifier1(NbrSol) = Qualified1.Qualifier();
254                   }
255                   else if (Abs(distcc1+Radius(ind3)-R1) < Tol) {
256                     qualifier1(NbrSol) = GccEnt_enclosed;
257                   }
258                   else if (Abs(distcc1-R1-Radius(ind3)) < Tol) {
259                     qualifier1(NbrSol) = GccEnt_outside;
260                   }
261                   else { qualifier1(NbrSol) = GccEnt_enclosing; }
262                   Standard_Real distcc2 = Center.Distance(center1);
263                   if (!Qualified2.IsUnqualified()) { 
264                     qualifier2(NbrSol) = Qualified2.Qualifier();
265                   }
266                   else if (Abs(distcc2+Radius(ind3)-R2) < Tol) {
267                     qualifier2(NbrSol) = GccEnt_enclosed;
268                   }
269                   else if (Abs(distcc2-R2-Radius(ind3)) < Tol) {
270                     qualifier2(NbrSol) = GccEnt_outside;
271                   }
272                   else { qualifier2(NbrSol) = GccEnt_enclosing; }
273                   gp_Dir2d dc3(origin3.XY()-Center.XY());
274                   if (!Qualified3.IsUnqualified()) { 
275                     qualifier3(NbrSol) = Qualified3.Qualifier();
276                   }
277                   else if (dc3.Dot(normL3) > 0.0) {
278                     qualifier3(NbrSol) = GccEnt_outside;
279                   }
280                   else { qualifier3(NbrSol) = GccEnt_enclosed; }
281                   if (Center.Distance(C1.Location()) <= Tolerance &&
282                       Abs(Radius(ind3)-R1) <= Tolerance) {
283                     TheSame1(NbrSol) = 1;
284                   }
285                   else {
286                     TheSame1(NbrSol) = 0;
287                     gp_Dir2d dc(C1.Location().XY()-Center.XY());
288                     pnttg1sol(NbrSol)=gp_Pnt2d(Center.XY()+Radius(ind3)*dc.XY());
289                     // POP for protection if cirsol(NbrSol).Location == pnttg1sol(NbrSol)
290                     if (cirsol(NbrSol).Location().IsEqual(pnttg1sol(NbrSol),Precision::Confusion()))
291                       par1sol(NbrSol)=1;
292                     else
293                       par1sol(NbrSol)=ElCLib::Parameter(cirsol(NbrSol),
294                                                         pnttg1sol(NbrSol));
295                     // POP for protection if C1.Location == pnttg1sol(NbrSol)
296                     if (C1.Location().IsEqual(pnttg1sol(NbrSol),Precision::Confusion()))
297                       pararg1(NbrSol)=1;
298                     else
299                       pararg1(NbrSol)=ElCLib::Parameter(C1,pnttg1sol(NbrSol));
300                   }
301                   if (Center.Distance(C2.Location()) <= Tolerance &&
302                       Abs(Radius(ind3)-R2) <= Tolerance) {
303                     TheSame2(NbrSol) = 1;
304                   }
305                   else {
306                     TheSame2(NbrSol) = 0;
307                     gp_Dir2d dc(C2.Location().XY()-Center.XY());
308                     pnttg2sol(NbrSol)=gp_Pnt2d(Center.XY()+Radius(ind3)*dc.XY());
309                     // POP for protection if cirsol(NbrSol).Location == pnttg1sol(NbrSol)
310                     if (cirsol(NbrSol).Location().IsEqual(pnttg1sol(NbrSol),Precision::Confusion()))
311                       par1sol(NbrSol)=1;
312                     else
313                       par2sol(NbrSol)=ElCLib::Parameter(cirsol(NbrSol),
314                                                         pnttg2sol(NbrSol));
315                     // POP for protection if C2.Location == pnttg2sol(NbrSol)
316                     if (C2.Location().IsEqual(pnttg2sol(NbrSol),Precision::Confusion()))
317                       pararg2(NbrSol)=1;
318                     else
319                       pararg2(NbrSol)=ElCLib::Parameter(C2,pnttg2sol(NbrSol));
320                   }
321                   TheSame3(NbrSol) = 0;
322                   gp_Dir2d dc(L3.Location().XY()-Center.XY());
323                   Standard_Real sign = dc.Dot(gp_Dir2d(-L3.Direction().Y(),
324                                                        L3.Direction().X()));
325                   dc = gp_Dir2d(sign*gp_XY(-L3.Direction().Y(),
326                                            L3.Direction().X()));
327                    pnttg3sol(NbrSol) = gp_Pnt2d(Center.XY()+Radius(ind3)*dc.XY());
328                   par3sol(NbrSol)=ElCLib::Parameter(cirsol(NbrSol),
329                                                     pnttg3sol(NbrSol));
330                   pararg3(NbrSol)=ElCLib::Parameter(L3,pnttg3sol(NbrSol));
331                 }
332               }
333             }
334            }
335            WellDone = Standard_True;
336         }
337        }
338     }
339   }
340 }