1 // Copyright (c) 1995-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
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.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
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>
37 //=========================================================================
38 // Creation of a circle tangent to two circles and a straight line. +
39 //=========================================================================
41 GccAna_Circ2d3Tan (const GccEnt_QualifiedCirc& Qualified1,
42 const GccEnt_QualifiedCirc& Qualified2,
43 const GccEnt_QualifiedLin& Qualified3,
44 const Standard_Real Tolerance ):
46 //=========================================================================
47 // Initialization of fields. +
48 //=========================================================================
68 gp_Dir2d dirx(1.0,0.0);
69 Standard_Real Tol = Abs(Tolerance);
70 WellDone = Standard_False;
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();
82 //=========================================================================
84 //=========================================================================
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());
93 gp_Pnt2d center2(C2.Location());
97 gp_Pnt2d origin3(L3.Location());
98 gp_Dir2d dir3(L3.Direction());
99 gp_Dir2d normL3(-dir3.Y(),dir3.X());
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());
118 else if (typ2 == GccInt_Par) {
119 Intp.Perform(Sol1->Line(),IntAna2d_Conic(Sol2->Parabola()));
122 else if (typ1 == GccInt_Par) {
123 if (typ2 == GccInt_Lin) {
124 Intp.Perform(Sol2->Line(),IntAna2d_Conic(Sol1->Parabola()));
126 else if (typ2 == GccInt_Par) {
127 Intp.Perform(Sol1->Parabola(),IntAna2d_Conic(Sol2->Parabola()));
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());
136 // pop : if the coordinates are too great, no creation
137 if (Center.X() > 1e10 ||
138 Center.Y() > 1e10 ) break;
140 Standard_Real dist1 = Center.Distance(C1.Location());
141 Standard_Real dist2 = Center.Distance(C2.Location());
142 Standard_Real dist3 = L3.Distance(Center);
144 // pop : if the coordinates are too great, no creation
145 if (dist3 > 1e10 ) break;
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);
158 else if (Qualified1.IsOutside()) {
159 if (R1-dist1 < Tolerance) {
160 Radius(1) = Abs(R1-dist1);
165 else if (Qualified1.IsEnclosing()) {
168 Radius(1) = Abs(R1-dist1);
170 else if (Qualified1.IsUnqualified()) {
173 Radius(1) = Abs(R1-dist1);
174 Radius(2) = R1+dist1;
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);
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);
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;
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);
214 else if (Abs(Radius(ii)-R2-dist2) < Tol) {
223 else if (nbsol2 == 2) {
224 Radius(1) = Abs(R2-dist2);
225 Radius(2) = R2+dist2;
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){
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){
242 else if (Qualified2.IsUnqualified() && ok) {
247 for (Standard_Integer ind3 = 1 ; ind3 <= nbsol3 ; ind3++) {
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();
255 else if (Abs(distcc1+Radius(ind3)-R1) < Tol) {
256 qualifier1(NbrSol) = GccEnt_enclosed;
258 else if (Abs(distcc1-R1-Radius(ind3)) < Tol) {
259 qualifier1(NbrSol) = GccEnt_outside;
261 else { qualifier1(NbrSol) = GccEnt_enclosing; }
262 Standard_Real distcc2 = Center.Distance(center1);
263 if (!Qualified2.IsUnqualified()) {
264 qualifier2(NbrSol) = Qualified2.Qualifier();
266 else if (Abs(distcc2+Radius(ind3)-R2) < Tol) {
267 qualifier2(NbrSol) = GccEnt_enclosed;
269 else if (Abs(distcc2-R2-Radius(ind3)) < Tol) {
270 qualifier2(NbrSol) = GccEnt_outside;
272 else { qualifier2(NbrSol) = GccEnt_enclosing; }
273 gp_Dir2d dc3(origin3.XY()-Center.XY());
274 if (!Qualified3.IsUnqualified()) {
275 qualifier3(NbrSol) = Qualified3.Qualifier();
277 else if (dc3.Dot(normL3) > 0.0) {
278 qualifier3(NbrSol) = GccEnt_outside;
280 else { qualifier3(NbrSol) = GccEnt_enclosed; }
281 if (Center.Distance(C1.Location()) <= Tolerance &&
282 Abs(Radius(ind3)-R1) <= Tolerance) {
283 TheSame1(NbrSol) = 1;
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()))
293 par1sol(NbrSol)=ElCLib::Parameter(cirsol(NbrSol),
295 // POP for protection if C1.Location == pnttg1sol(NbrSol)
296 if (C1.Location().IsEqual(pnttg1sol(NbrSol),Precision::Confusion()))
299 pararg1(NbrSol)=ElCLib::Parameter(C1,pnttg1sol(NbrSol));
301 if (Center.Distance(C2.Location()) <= Tolerance &&
302 Abs(Radius(ind3)-R2) <= Tolerance) {
303 TheSame2(NbrSol) = 1;
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()))
313 par2sol(NbrSol)=ElCLib::Parameter(cirsol(NbrSol),
315 // POP for protection if C2.Location == pnttg2sol(NbrSol)
316 if (C2.Location().IsEqual(pnttg2sol(NbrSol),Precision::Confusion()))
319 pararg2(NbrSol)=ElCLib::Parameter(C2,pnttg2sol(NbrSol));
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),
330 pararg3(NbrSol)=ElCLib::Parameter(L3,pnttg3sol(NbrSol));
335 WellDone = Standard_True;