summaryrefslogtreecommitdiff
path: root/src/GccAna/GccAna_Circ2d3Tan_4.cxx
blob: 23ee203136e02ce4d41bead86c3dd179369bcd3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// File GccAna_Circ2d3Tan.cxx_4, REG 08/07/91
// cas de 2 cercles concentriques JCT 28/11/97

#include <ElCLib.hxx>
#include <GccAna_Circ2d3Tan.jxx>
#include <IntAna2d_AnaIntersection.hxx>
#include <IntAna2d_IntPoint.hxx>
#include <gp_Lin2d.hxx>
#include <gp_Circ2d.hxx>
#include <gp_Dir2d.hxx>
#include <TColStd_Array1OfReal.hxx>
#include <GccAna_Circ2dBisec.hxx>
#include <GccAna_CircPnt2dBisec.hxx>
#include <GccInt_IType.hxx>
#include <GccInt_BCirc.hxx>
#include <GccInt_BLine.hxx>
#include <GccInt_BElips.hxx>
#include <GccInt_BHyper.hxx>
#include <IntAna2d_Conic.hxx>
#include <GccEnt_BadQualifier.hxx>

static Standard_Integer MaxSol = 20;
//=========================================================================
//   Creation d un cercle tangent a deux cercles et a un point.           +
//=========================================================================

GccAna_Circ2d3Tan::
   GccAna_Circ2d3Tan (const GccEnt_QualifiedCirc& Qualified1 ,
                      const GccEnt_QualifiedCirc& Qualified2 ,
                      const gp_Pnt2d&             Point3     ,
		      const Standard_Real         Tolerance  ):

//=========================================================================
//   Initialisation des champs.                                           +
//=========================================================================

   cirsol(1,MaxSol)     ,
   qualifier1(1,MaxSol) ,
   qualifier2(1,MaxSol) ,
   qualifier3(1,MaxSol) ,
   TheSame1(1,MaxSol)   ,
   TheSame2(1,MaxSol)   ,
   TheSame3(1,MaxSol)   ,
   pnttg1sol(1,MaxSol)  ,
   pnttg2sol(1,MaxSol)  ,
   pnttg3sol(1,MaxSol)  ,
   par1sol(1,MaxSol)    ,
   par2sol(1,MaxSol)    ,
   par3sol(1,MaxSol)    ,
   pararg1(1,MaxSol)    ,
   pararg2(1,MaxSol)    ,
   pararg3(1,MaxSol)    
{

   gp_Dir2d dirx(1.0,0.0);
   Standard_Real Tol = Abs(Tolerance);
   WellDone = Standard_False;
   NbrSol = 0;
   if (!(Qualified1.IsEnclosed() || Qualified1.IsEnclosing() || 
	 Qualified1.IsOutside() || Qualified1.IsUnqualified()) ||
       !(Qualified2.IsEnclosed() || Qualified2.IsEnclosing() || 
	 Qualified2.IsOutside() || Qualified2.IsUnqualified())) {
     GccEnt_BadQualifier::Raise();
     return;
   }

//=========================================================================
//   Traitement.                                                          +
//=========================================================================

   gp_Circ2d C1(Qualified1.Qualified());
   gp_Circ2d C2(Qualified2.Qualified());
   Standard_Real R1 = C1.Radius();
   Standard_Real R2 = C2.Radius();
   gp_Pnt2d center1(C1.Location());
   gp_Pnt2d center2(C2.Location());

   TColStd_Array1OfReal Radius(1,2);
   GccAna_Circ2dBisec Bis1(C1,C2);
   GccAna_CircPnt2dBisec Bis2(C1,Point3);
   if (Bis1.IsDone() && Bis2.IsDone()) {
     Standard_Integer nbsolution1 = Bis1.NbSolutions();
     Standard_Integer nbsolution2 = Bis2.NbSolutions();
     for (Standard_Integer i = 1 ; i <=  nbsolution1; i++) {
       Handle(GccInt_Bisec) Sol1 = Bis1.ThisSolution(i);
       GccInt_IType typ1 = Sol1->ArcType();
       IntAna2d_AnaIntersection Intp;
       for (Standard_Integer k = 1 ; k <=  nbsolution2; k++) {
	 Handle(GccInt_Bisec) Sol2 = Bis2.ThisSolution(k);
	 GccInt_IType typ2 = Sol2->ArcType();
	 if (typ1 == GccInt_Cir) {
	   if (typ2 == GccInt_Cir) {
	     Intp.Perform(Sol1->Circle(),Sol2->Circle());
	   }
	   else if (typ2 == GccInt_Lin) {
	     Intp.Perform(Sol2->Line(),Sol1->Circle());
	   }
	   else if (typ2 == GccInt_Hpr) {
	     Intp.Perform(Sol1->Circle(),IntAna2d_Conic(Sol2->Hyperbola()));
	   }
	   else if (typ2 == GccInt_Ell) {
	     Intp.Perform(Sol1->Circle(),IntAna2d_Conic(Sol2->Ellipse()));
	   }
	 }
	 else if (typ1 == GccInt_Ell) {
	   if (typ2 == GccInt_Cir) {
	     Intp.Perform(Sol2->Circle(),IntAna2d_Conic(Sol1->Ellipse()));
	   }
	   else if (typ2 == GccInt_Lin) {
	     Intp.Perform(Sol2->Line(),IntAna2d_Conic(Sol1->Ellipse()));
	   }
	   else if (typ2 == GccInt_Hpr) {
	     Intp.Perform(Sol1->Ellipse(),IntAna2d_Conic(Sol2->Hyperbola()));
	   }
	   else if (typ2 == GccInt_Ell) {
	     Intp.Perform(Sol1->Ellipse(),IntAna2d_Conic(Sol2->Ellipse()));
	   }
	 }
	 else if (typ1 == GccInt_Lin) {
	   if (typ2 == GccInt_Cir) {
	     Intp.Perform(Sol1->Line(),Sol2->Circle());
	   }
	   else if (typ2 == GccInt_Lin) {
	     Intp.Perform(Sol1->Line(),Sol2->Line());
	   }
	   else if (typ2 == GccInt_Hpr) {
	     Intp.Perform(Sol1->Line(),IntAna2d_Conic(Sol2->Hyperbola()));
	   }
	   else if (typ2 == GccInt_Ell) {
	     Intp.Perform(Sol1->Line(),IntAna2d_Conic(Sol2->Ellipse()));
	   }
	 }
	 else if (typ1 == GccInt_Hpr) {
	   if (typ2 == GccInt_Cir) {
	     Intp.Perform(Sol2->Circle(),IntAna2d_Conic(Sol1->Hyperbola()));
	   }
	   else if (typ2 == GccInt_Lin) {
	     Intp.Perform(Sol2->Line(),IntAna2d_Conic(Sol1->Hyperbola()));
	   }
	   else if (typ2 == GccInt_Hpr) {
	     Intp.Perform(Sol2->Hyperbola(),IntAna2d_Conic(Sol1->Hyperbola()));
	   }
	   else if (typ2 == GccInt_Ell) {
	     Intp.Perform(Sol2->Ellipse(),IntAna2d_Conic(Sol1->Hyperbola()));
	   }
	 }
	 if (Intp.IsDone()) {
	   if (!Intp.IsEmpty()) {
	     for (Standard_Integer j = 1 ; j <= Intp.NbPoints() ; j++) {
	       Standard_Real Rradius=0;
	       gp_Pnt2d Center(Intp.Point(j).Value());
	       Standard_Real dist1 = Center.Distance(center1);
	       Standard_Real dist2 = Center.Distance(center2);
	       Standard_Real dist3 = Center.Distance(Point3);
	       Standard_Integer nbsol1 = 0;
	       Standard_Integer nbsol2 = 0;
	       Standard_Integer nbsol3 = 0;
	       Standard_Boolean ok = Standard_False;
	       if (Qualified1.IsEnclosed()) {
		 if (dist1-R1 < Tolerance) {
		   Radius(1) = Abs(R1-dist1);
		   nbsol1 = 1;
		   ok = Standard_True;
		 }
	       }
	       else if (Qualified1.IsOutside()) {
		 if (R1-dist1 < Tolerance) {
		   Radius(1) = Abs(R1-dist1);
		   nbsol1 = 1;
		   ok = Standard_True;
		 }
	       }
	       else if (Qualified1.IsEnclosing()) {
		 ok = Standard_True;
		 nbsol1 = 1;
		 Radius(1) = R1+dist1;
	       }
	       else if (Qualified1.IsUnqualified()) {
		 ok = Standard_True;
		 nbsol1 = 2;
		 Radius(1) = Abs(R1-dist1);
		 Radius(2) = R1+dist1;
	       }
	       if (Qualified2.IsEnclosed() && ok) {
		 if (dist2-R2 < Tolerance) {
		   for (Standard_Integer ii = 1 ; ii <= nbsol1 ; ii++) {
		     if (Abs(Radius(ii)-Abs(R2-dist2)) < Tol) {
		       Radius(1) = Abs(R2-dist2);
		       ok = Standard_True;
		       nbsol2 = 1;
		     }
		   }
		 }
	       }
	       else if (Qualified2.IsOutside() && ok) {
		 if (R2-dist2 < Tolerance) {
		   for (Standard_Integer ii = 1 ; ii <= nbsol1 ; ii++) {
		     if (Abs(Radius(ii)-Abs(R2-dist2)) < Tol) {
		       Radius(1) = Abs(R2-dist2);
		       ok = Standard_True;
		       nbsol2 = 1;
		     }
		   }
		 }
	       }
	       else if (Qualified2.IsEnclosing() && ok) {
		 for (Standard_Integer ii = 1 ; ii <= nbsol1 ; ii++) {
		   if (Abs(Radius(ii)-R2-dist2) < Tol) {
		     Radius(1) = R2+dist2;
		     ok = Standard_True;
		     nbsol2 = 1;
		   }
		 }
	       }
	       else if (Qualified2.IsUnqualified() && ok) {
		 for (Standard_Integer ii = 1 ; ii <= nbsol1 ; ii++) {
		   if (Abs(Radius(ii)-Abs(R2-dist2)) < Tol) {
		     Rradius = Abs(R2-dist2);
		     ok = Standard_True;
		     nbsol2++;
		   }
		   else if (Abs(Radius(ii)-R2-dist2) < Tol) {
		     Rradius = R2+dist2;
		     ok = Standard_True;
		     nbsol2++;
		   }
		 }
		 if (nbsol2 == 1) {
		   Radius(1) = Rradius;
		 }
		 else if (nbsol2 == 2) {
		   Radius(1) = Abs(R2-dist2);
		   Radius(2) = R2+dist2;
		 }
	       }
	       for (Standard_Integer ii = 1 ; ii <= nbsol2 ; ii++) {
		 if (Abs(dist3-Radius(ii)) <= Tol) {
		   nbsol3++;
		   ok = Standard_True;
		 }
	       }
	       if (ok) {
		 for (Standard_Integer k1 = 1 ; k1 <= nbsol3 ; k1++) {
		   NbrSol++;
		   cirsol(NbrSol) = gp_Circ2d(gp_Ax2d(Center,dirx),Radius(k1));
//                 ==========================================================
		   Standard_Real distcc1 = Center.Distance(center1);
		   if (!Qualified1.IsUnqualified()) { 
		     qualifier1(NbrSol) = Qualified1.Qualifier();
		   }
		   else if (Abs(distcc1+Radius(k1)-R1) < Tol) {
		     qualifier1(NbrSol) = GccEnt_enclosed;
		   }
		   else if (Abs(distcc1-R1-Radius(k1)) < Tol) {
		     qualifier1(NbrSol) = GccEnt_outside;
		   }
		   else { qualifier1(NbrSol) = GccEnt_enclosing; }

//		   Standard_Real distcc2 = Center.Distance(center1);
		   Standard_Real distcc2 = Center.Distance(center2);
		   if (!Qualified2.IsUnqualified()) { 
		     qualifier2(NbrSol) = Qualified2.Qualifier();
		   }
		   else if (Abs(distcc2+Radius(k1)-R2) < Tol) {
		     qualifier2(NbrSol) = GccEnt_enclosed;
		   }
		   else if (Abs(distcc2-R2-Radius(k1)) < Tol) {
		     qualifier2(NbrSol) = GccEnt_outside;
		   }
		   else { qualifier2(NbrSol) = GccEnt_enclosing; }
		   qualifier3(NbrSol) = GccEnt_noqualifier;
		   if (Center.Distance(center1) <= Tolerance &&
		       Abs(Radius(k1)-R1) <= Tolerance) {
		     TheSame1(NbrSol) = 1;
		   }
		   else {
		     TheSame1(NbrSol) = 0;
		     gp_Dir2d dc(center1.XY()-Center.XY());
		     pnttg1sol(NbrSol)=gp_Pnt2d(Center.XY()+Radius(k1)*dc.XY());
		     par1sol(NbrSol)=ElCLib::Parameter(cirsol(NbrSol),
						      pnttg1sol(NbrSol));
		     pararg1(NbrSol)=ElCLib::Parameter(C1,
						      pnttg1sol(NbrSol));
		   }
		   if (Center.Distance(center2) <= Tolerance &&
		       Abs(Radius(k1)-R2) <= Tolerance) {
		     TheSame2(NbrSol) = 1;
		   }
		   else {
		     TheSame2(NbrSol) = 0;
		     gp_Dir2d dc(center2.XY()-Center.XY());
		     // cas des cercles concentriques : 
		     // le 2eme point de tangence est de l'autre cote du cercle solution
		     Standard_Real alpha = 1.;
		     if (center1.Distance(center2)<=Tolerance) alpha = -1;
		     pnttg2sol(NbrSol)=gp_Pnt2d(Center.XY()+alpha*Radius(k1)*dc.XY());
		     par2sol(NbrSol)=ElCLib::Parameter(cirsol(NbrSol),
						      pnttg2sol(NbrSol));
		     pararg2(NbrSol)=ElCLib::Parameter(C2,pnttg2sol(NbrSol));
		   }
		   TheSame3(NbrSol) = 0;
		   pnttg3sol(NbrSol) = Point3;
		   par3sol(NbrSol)=ElCLib::Parameter(cirsol(NbrSol),
						    pnttg3sol(NbrSol));
		   pararg3(NbrSol) = 0.;
		   WellDone = Standard_True;
		   if (NbrSol==MaxSol) break;
		 }
	       }
	     }
	   }
	   WellDone = Standard_True;
	   if (NbrSol==MaxSol) break;
	 }
	 if (NbrSol==MaxSol) break;
       }
       if (NbrSol==MaxSol) break;
     }
   }

   // Debug grossier pour que le point soit sur les cercles solutions.

   Standard_Integer kk ;
   for ( kk = 1; kk <= NbrSol; kk++) {
     gp_Circ2d CC = cirsol(kk);
     Standard_Real NR = CC.Location().Distance(Point3);
     if (Abs(NR - CC.Radius()) > Tol) {
       cirsol(kk).SetRadius(NR);
     }
   }

   // Debug grossier pour eliminer solution multiple.
   // ca arrive dans le cas d intersection ligne hyperbole.
   Standard_Real Tol2 = Tol*Tol;
   for (kk = 1; kk <NbrSol; kk++) {
     gp_Pnt2d PK = cirsol(kk).Location();
     for (Standard_Integer ll = kk+1 ; ll <= NbrSol; ll++) {
       gp_Pnt2d PL = cirsol(ll).Location();
       if (PK.SquareDistance(PL) < Tol2) {
	 for (Standard_Integer mm = ll+1 ; mm <= NbrSol; mm++) {
	   cirsol(mm - 1)   = cirsol (mm);   
	   pnttg1sol(mm-1)  = pnttg1sol(mm);
	   pnttg2sol(mm-1)  = pnttg2sol(mm);
	   pnttg3sol(mm-1)  = pnttg3sol(mm);
	   par1sol(mm-1)    = par1sol(mm);
	   par2sol(mm-1)    = par2sol(mm);
	   par3sol(mm-1)    = par3sol(mm);
	   pararg1(mm-1)    = pararg1(mm);
	   pararg2(mm-1)    = pararg2(mm);
	   pararg3(mm-1)    = pararg3(mm);
	   qualifier1(mm-1) = qualifier1(mm);
	   qualifier2(mm-1) = qualifier2(mm);
	   qualifier3(mm-1) = qualifier3(mm);
	 }
	 NbrSol--;
       }
     }
   }
 }