0033661: Data Exchange, Step Import - Tessellated GDTs are not imported
[occt.git] / src / GccAna / GccAna_Circ2d2TanOn_6.cxx
1 // Created on: 1992-01-02
2 // Created by: Remi GILET
3 // Copyright (c) 1992-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17
18 #include <ElCLib.hxx>
19 #include <GccAna_Circ2d2TanOn.hxx>
20 #include <GccAna_Circ2dBisec.hxx>
21 #include <GccEnt_BadQualifier.hxx>
22 #include <GccEnt_QualifiedCirc.hxx>
23 #include <GccInt_BLine.hxx>
24 #include <GccInt_IType.hxx>
25 #include <gp_Ax2d.hxx>
26 #include <gp_Circ2d.hxx>
27 #include <gp_Dir2d.hxx>
28 #include <gp_Pnt2d.hxx>
29 #include <IntAna2d_AnaIntersection.hxx>
30 #include <IntAna2d_Conic.hxx>
31 #include <IntAna2d_IntPoint.hxx>
32 #include <TColStd_Array1OfReal.hxx>
33
34 //=========================================================================
35 //   Creation of a circle tangent to two circles C1 and C2.               +
36 //                        centered on a circle.                           +
37 //  We start with distinguishing various boundary cases that will be      +
38 //  processed separately.                                            +
39 //  In the general case:                                                  +
40 //  ====================                                                  +
41 //  We calculate bissectrices to C1 and C2 that give us all           +
42 //  possible locations of centers of all circles tangent to C1 and C2.                                                   +
43 //  We intersect these bissectrices with circle OnCirc which gives us   +
44 //  points among which we choose the solutions.   +
45 //  The choice is made basing in Qualifiers of C1 and C2.  +
46 //=========================================================================
47 GccAna_Circ2d2TanOn::
48    GccAna_Circ2d2TanOn (const GccEnt_QualifiedCirc& Qualified1 ,
49                         const GccEnt_QualifiedCirc& Qualified2 ,
50                         const gp_Circ2d&            OnCirc     ,
51                         const Standard_Real         Tolerance  ):
52    cirsol(1,8)     ,
53    qualifier1(1,8) ,
54    qualifier2(1,8) ,
55    TheSame1(1,8) ,
56    TheSame2(1,8) ,
57    pnttg1sol(1,8)  ,
58    pnttg2sol(1,8)  ,
59    pntcen(1,8)     ,
60    par1sol(1,8)    ,
61    par2sol(1,8)    ,
62    pararg1(1,8)    ,
63    pararg2(1,8)    ,
64    parcen3(1,8)    
65 {
66   TheSame1.Init(0);
67   TheSame2.Init(0);
68   WellDone = Standard_False;
69   NbrSol = 0;
70   if (!(Qualified1.IsEnclosed() || Qualified1.IsEnclosing() || 
71         Qualified1.IsOutside() || Qualified1.IsUnqualified()) ||
72       !(Qualified2.IsEnclosed() || Qualified2.IsEnclosing() || 
73         Qualified2.IsOutside() || Qualified2.IsUnqualified())) {
74     throw GccEnt_BadQualifier();
75     return;
76   }
77   Standard_Real Tol= Abs(Tolerance);
78   gp_Circ2d C1 = Qualified1.Qualified();
79   gp_Circ2d C2 = Qualified2.Qualified();
80   gp_Dir2d dirx(1.,0.);
81   TColStd_Array1OfReal Radius(1,2);
82   TColStd_Array1OfReal Rradius(1,2);
83   gp_Pnt2d center1(C1.Location());
84   gp_Pnt2d center2(C2.Location());
85
86   Standard_Real R1 = C1.Radius();
87   Standard_Real R2 = C2.Radius();
88
89 //=========================================================================
90 //   Processing of boundary cases.                                          +
91 //=========================================================================
92
93   Standard_Integer nbsol1 = 1;
94   Standard_Integer nbsol2 = 0;
95   Standard_Real Ron = OnCirc.Radius();
96   Standard_Real distcco = OnCirc.Location().Distance(center1);
97   gp_Dir2d dircc(OnCirc.Location().XY()-center1.XY());
98   gp_Pnt2d pinterm(center1.XY()+(distcco-Ron)*dircc.XY());
99   Standard_Real distcc2 =pinterm.Distance(center2);
100   Standard_Real distcc1 =pinterm.Distance(center1);
101   Standard_Real d1 = Abs(distcc2-R2-Abs(distcc1-R1));
102   Standard_Real d2 = Abs(distcc2+R2-Abs(distcc1-R1));
103   Standard_Real d3 = Abs(distcc2-R2-(distcc1+R1));
104   Standard_Real d4 = Abs(distcc2+R2-(distcc1+R1));
105   if ( d1 > Tol || d2 > Tol || d3 > Tol || d4 > Tol) {
106     pinterm = gp_Pnt2d(center1.XY()+(distcco+Ron)*dircc.XY());
107     distcc2 =pinterm.Distance(center2);
108     distcc1 =pinterm.Distance(center1);
109     d1 = Abs(distcc2-R2-Abs(distcc1-R1));
110     d2 = Abs(distcc2+R2-Abs(distcc1-R1));
111     d3 = Abs(distcc2-R2-(distcc1+R1));
112     d4 = Abs(distcc2+R2-(distcc1+R1));
113     if ( d1 > Tol || d2 > Tol || d3 > Tol || d4 > Tol) { nbsol1 = 0; }
114   }
115   if (nbsol1 > 0) {
116     if (Qualified1.IsEnclosed() || Qualified1.IsOutside()) {
117       nbsol1 = 1;
118       Radius(1) = Abs(distcc1-R1);
119     }
120     else if (Qualified1.IsEnclosing()) {
121       nbsol1 = 1;
122       Radius(1) = R1+distcc1;
123     }
124     else if (Qualified1.IsUnqualified()) {
125       nbsol1 = 2;
126       Radius(1) = Abs(distcc1-R1);
127       Radius(2) = R1+distcc1;
128     }
129     if (Qualified2.IsEnclosed() || Qualified2.IsOutside()) {
130       nbsol2 = 1;
131       Rradius(1) = Abs(distcc2-R2);
132     }
133     else if (Qualified2.IsEnclosing()) {
134       nbsol2 = 1;
135       Rradius(1) = R2+distcc2;
136     }
137     else if (Qualified2.IsUnqualified()) {
138       nbsol2 = 2;
139       Rradius(1) = Abs(distcc2-R2);
140       Rradius(2) = R2+distcc2;
141     }
142     for (Standard_Integer i = 1 ; i <= nbsol1 ; i++) {
143       for (Standard_Integer j = 1 ; j <= nbsol2 ; j++) {
144         if (Abs(Radius(i)-Rradius(j)) <= Tol) {
145           WellDone = Standard_True;
146           NbrSol++;
147           cirsol(NbrSol) = gp_Circ2d(gp_Ax2d(pinterm,dirx),Radius(i));
148 //        ===========================================================
149           gp_Dir2d dc1(center1.XY()-pinterm.XY());
150           gp_Dir2d dc2(center2.XY()-pinterm.XY());
151           distcc1 = pinterm.Distance(center1);
152           distcc2 = pinterm.Distance(center2);
153           if (!Qualified1.IsUnqualified()) { 
154             qualifier1(NbrSol) = Qualified1.Qualifier();
155           }
156           else if (Abs(distcc1+Radius(i)-R1) < Tol) {
157             qualifier1(NbrSol) = GccEnt_enclosed;
158           }
159           else if (Abs(distcc1-R1-Radius(i)) < Tol) {
160             qualifier1(NbrSol) = GccEnt_outside;
161           }
162           else { qualifier1(NbrSol) = GccEnt_enclosing; }
163           if (!Qualified2.IsUnqualified()) { 
164             qualifier2(NbrSol) = Qualified2.Qualifier();
165           }
166           else if (Abs(distcc2+Radius(i)-R2) < Tol) {
167             qualifier2(NbrSol) = GccEnt_enclosed;
168           }
169           else if (Abs(distcc2-R2-Radius(i)) < Tol) {
170             qualifier2(NbrSol) = GccEnt_outside;
171           }
172           else { qualifier2(NbrSol) = GccEnt_enclosing; }
173           pnttg1sol(NbrSol) = gp_Pnt2d(pinterm.XY()+Radius(i)*dc1.XY());
174           pnttg2sol(NbrSol) = gp_Pnt2d(pinterm.XY()+Radius(i)*dc2.XY());
175           pntcen(NbrSol) = cirsol(NbrSol).Location();
176           par1sol(NbrSol)=ElCLib::Parameter(cirsol(NbrSol),pnttg1sol(NbrSol));
177           pararg1(NbrSol)=ElCLib::Parameter(C1,pnttg1sol(NbrSol));
178           par2sol(NbrSol)=ElCLib::Parameter(cirsol(NbrSol),pnttg2sol(NbrSol));
179           pararg2(NbrSol)=ElCLib::Parameter(C2,pnttg2sol(NbrSol));
180           parcen3(NbrSol)=ElCLib::Parameter(OnCirc,pntcen(NbrSol));
181         }
182       }
183     }
184     if (WellDone) { return; }
185   }
186   
187 //=========================================================================
188 //   General case.                                                         +
189 //=========================================================================
190
191   GccAna_Circ2dBisec Bis(C1,C2);
192   if (Bis.IsDone()) {
193     TColStd_Array1OfReal Rbid(1,2);
194     TColStd_Array1OfReal RBid(1,2);
195     Standard_Integer nbsolution = Bis.NbSolutions();
196     for (Standard_Integer i = 1 ; i <=  nbsolution ; i++) {
197       Handle(GccInt_Bisec) Sol = Bis.ThisSolution(i);
198       GccInt_IType typ = Sol->ArcType();
199       IntAna2d_AnaIntersection Intp;
200       if (typ == GccInt_Cir) {
201         Intp.Perform(OnCirc,Sol->Circle());
202       }
203       else if (typ == GccInt_Lin) {
204         Intp.Perform(Sol->Line(),OnCirc);
205       }
206       else if (typ == GccInt_Hpr) {
207         Intp.Perform(OnCirc,IntAna2d_Conic(Sol->Hyperbola()));
208       }
209       else if (typ == GccInt_Ell) {
210         Intp.Perform(OnCirc,IntAna2d_Conic(Sol->Ellipse()));
211       }
212       if (Intp.IsDone()) {
213         if ((!Intp.IsEmpty())&&(!Intp.ParallelElements())&&
214             (!Intp.IdenticalElements())) {
215           for (Standard_Integer j = 1 ; j <= Intp.NbPoints() ; j++) {
216             gp_Pnt2d Center(Intp.Point(j).Value());
217             Standard_Real dist1 = Center.Distance(center1);
218             Standard_Real dist2 = Center.Distance(center2);
219             Standard_Integer nbsol = 0;
220             Standard_Integer nsol = 0;
221             Standard_Integer nnsol = 0;
222             R1 = C1.Radius();
223             R2 = C2.Radius();
224             if (Qualified1.IsEnclosed()) {
225               if (dist1-R1 < Tol) { 
226                 nbsol = 1;
227                 Rbid(1) = Abs(R1-dist1);
228               }
229             }
230             else if (Qualified1.IsOutside()) {
231               if (R1-dist1 < Tol) { 
232                 nbsol = 1;
233                 Rbid(1) = Abs(dist1-R1);
234               }
235             }
236             else if (Qualified1.IsEnclosing()) {
237               nbsol = 1;
238               Rbid(1) = dist1+R1;
239             }
240             else if (Qualified1.IsUnqualified()) {
241               nbsol = 2;
242               Rbid(1) = dist1+R1;
243               Rbid(1) = Abs(dist1-R1);
244             }
245             if (Qualified2.IsEnclosed() && nbsol != 0) {
246               if (dist2-R2 < Tol) {
247                 nsol = 1;
248                 RBid(1) = Abs(R2-dist2);
249               }
250             }
251             else if (Qualified2.IsOutside() && nbsol != 0) {
252               if (R2-dist2 < Tol) {
253                 nsol = 1;
254                 RBid(1) = Abs(R2-dist2);
255               }
256             }
257             else if (Qualified2.IsEnclosing() && nbsol != 0) {
258               nsol = 1;
259               RBid(1) = dist2+R2;
260             }
261             else if (Qualified2.IsUnqualified() && nbsol != 0) {
262               nsol = 2;
263               RBid(1) = dist2+R2;
264               RBid(2) = Abs(R2-dist2);
265             }
266             for (Standard_Integer isol = 1; isol <= nbsol ; isol++) {
267               for (Standard_Integer jsol = 1; jsol <= nsol ; jsol++) {
268                 if (Abs(Rbid(isol)-RBid(jsol)) <= Tol) {
269                   nnsol++;
270                   Radius(nnsol) = (RBid(jsol)+Rbid(isol))/2.;
271                 }
272               }
273             }
274             if (nnsol > 0) {
275               for (Standard_Integer k = 1 ; k <= nnsol ; k++) {
276                 NbrSol++;
277                 cirsol(NbrSol) = gp_Circ2d(gp_Ax2d(Center,dirx),Radius(k));
278 //              ==========================================================
279                 distcc1 = Center.Distance(center1);
280                 distcc2 = Center.Distance(center2);
281                 if (!Qualified1.IsUnqualified()) { 
282                   qualifier1(NbrSol) = Qualified1.Qualifier();
283                 }
284                 else if (Abs(distcc1+Radius(k)-R1) < Tol) {
285                   qualifier1(NbrSol) = GccEnt_enclosed;
286                 }
287                 else if (Abs(distcc1-R1-Radius(k)) < Tol) {
288                   qualifier1(NbrSol) = GccEnt_outside;
289                 }
290                 else { qualifier1(NbrSol) = GccEnt_enclosing; }
291                 if (!Qualified2.IsUnqualified()) { 
292                   qualifier2(NbrSol) = Qualified2.Qualifier();
293                 }
294                 else if (Abs(distcc2+Radius(k)-R2) < Tol) {
295                   qualifier2(NbrSol) = GccEnt_enclosed;
296                 }
297                 else if (Abs(distcc2-R2-Radius(k)) < Tol) {
298                   qualifier2(NbrSol) = GccEnt_outside;
299                 }
300                 else { qualifier2(NbrSol) = GccEnt_enclosing; }
301                 if (Center.Distance(center1) <= Tolerance &&
302                     Abs(Radius(k)-C1.Radius()) <= Tolerance) {
303                   TheSame1(NbrSol) = 1;
304                 }
305                 else {
306                   TheSame1(NbrSol) = 0;
307                   gp_Dir2d dc1(center1.XY()-Center.XY());
308                   pnttg1sol(NbrSol)=gp_Pnt2d(Center.XY()+Radius(k)*dc1.XY());
309                   par1sol(NbrSol)=ElCLib::Parameter(cirsol(NbrSol),
310                                                     pnttg1sol(NbrSol));
311                   pararg1(NbrSol)=ElCLib::Parameter(C1,pnttg1sol(NbrSol));
312                 }
313                 if (Center.Distance(center2) <= Tolerance &&
314                     Abs(Radius(k)-C2.Radius()) <= Tolerance) {
315                   TheSame2(NbrSol) = 1;
316                 }
317                 else {
318                   TheSame2(NbrSol) = 0;
319                   gp_Dir2d dc2(center2.XY()-Center.XY());
320                   pnttg2sol(NbrSol)=gp_Pnt2d(Center.XY()+Radius(k)*dc2.XY());
321                   par2sol(NbrSol)=ElCLib::Parameter(cirsol(NbrSol),
322                                                     pnttg2sol(NbrSol));
323                   pararg2(NbrSol)=ElCLib::Parameter(C2,pnttg2sol(NbrSol));
324                 }
325                 pntcen(NbrSol) = Center;
326                 parcen3(NbrSol)=ElCLib::Parameter(OnCirc,pntcen(NbrSol));
327               }
328             }
329           }
330         }
331         WellDone = Standard_True;
332       }
333     }
334   }
335 }
336