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());
92 gp_Pnt2d center2(C2.Location());
95 gp_Pnt2d origin3(L3.Location());
96 gp_Dir2d dir3(L3.Direction());
97 gp_Dir2d normL3(-dir3.Y(),dir3.X());
99 TColStd_Array1OfReal Radius(1,2);
100 GccAna_CircLin2dBisec Bis1(C1,L3);
101 GccAna_CircLin2dBisec Bis2(C2,L3);
102 if (Bis1.IsDone() && Bis2.IsDone()) {
103 Standard_Integer nbsolution1 = Bis1.NbSolutions();
104 Standard_Integer nbsolution2 = Bis2.NbSolutions();
105 for (Standard_Integer i = 1 ; i <= nbsolution1; i++) {
106 Handle(GccInt_Bisec) Sol1 = Bis1.ThisSolution(i);
107 GccInt_IType typ1 = Sol1->ArcType();
108 IntAna2d_AnaIntersection Intp;
109 for (Standard_Integer k = 1 ; k <= nbsolution2; k++) {
110 Handle(GccInt_Bisec) Sol2 = Bis2.ThisSolution(k);
111 GccInt_IType typ2 = Sol2->ArcType();
112 if (typ1 == GccInt_Lin) {
113 if (typ2 == GccInt_Lin) {
114 Intp.Perform(Sol1->Line(),Sol2->Line());
116 else if (typ2 == GccInt_Par) {
117 Intp.Perform(Sol1->Line(),IntAna2d_Conic(Sol2->Parabola()));
120 else if (typ1 == GccInt_Par) {
121 if (typ2 == GccInt_Lin) {
122 Intp.Perform(Sol2->Line(),IntAna2d_Conic(Sol1->Parabola()));
124 else if (typ2 == GccInt_Par) {
125 Intp.Perform(Sol1->Parabola(),IntAna2d_Conic(Sol2->Parabola()));
129 if (!Intp.IsEmpty()) {
130 for (Standard_Integer j = 1 ; j <= Intp.NbPoints() ; j++) {
131 Standard_Real Rradius=0;
132 gp_Pnt2d Center(Intp.Point(j).Value());
134 // pop : if the coordinates are too great, no creation
135 if (Center.X() > 1e10 ||
136 Center.Y() > 1e10 ) break;
138 Standard_Real dist1 = Center.Distance(C1.Location());
139 Standard_Real dist2 = Center.Distance(C2.Location());
140 Standard_Real dist3 = L3.Distance(Center);
142 // pop : if the coordinates are too great, no creation
143 if (dist3 > 1e10 ) break;
145 Standard_Integer nbsol1 = 0;
146 Standard_Integer nbsol2 = 0;
147 Standard_Integer nbsol3 = 0;
148 Standard_Boolean ok = Standard_False;
149 if (Qualified1.IsEnclosed()) {
150 if (dist1-R1 < Tolerance) {
151 Radius(1) = Abs(R1-dist1);
156 else if (Qualified1.IsOutside()) {
157 if (R1-dist1 < Tolerance) {
158 Radius(1) = Abs(R1-dist1);
163 else if (Qualified1.IsEnclosing()) {
166 Radius(1) = Abs(R1-dist1);
168 else if (Qualified1.IsUnqualified()) {
171 Radius(1) = Abs(R1-dist1);
172 Radius(2) = R1+dist1;
174 if (Qualified2.IsEnclosed() && ok) {
175 if (dist2-R2 < Tolerance) {
176 for (Standard_Integer ii = 1 ; ii <= nbsol1 ; ii++) {
177 if (Abs(Radius(ii)-Abs(R2-dist2)) < Tol) {
178 Radius(1) = Abs(R2-dist2);
185 else if (Qualified2.IsOutside() && ok) {
186 if (R2-dist2 < Tolerance) {
187 for (Standard_Integer ii = 1 ; ii <= nbsol1 ; ii++) {
188 if (Abs(Radius(ii)-Abs(R2-dist2)) < Tol) {
189 Radius(1) = Abs(R2-dist2);
196 else if (Qualified2.IsEnclosing() && ok) {
197 for (Standard_Integer ii = 1 ; ii <= nbsol1 ; ii++) {
198 if (Abs(Radius(ii)-R2-dist2) < Tol) {
199 Radius(1) = R2+dist2;
205 else if (Qualified2.IsUnqualified() && ok) {
206 for (Standard_Integer ii = 1 ; ii <= nbsol1 ; ii++) {
207 if (Abs(Radius(ii)-Abs(R2-dist2)) < Tol) {
208 Rradius = Abs(R2-dist2);
212 else if (Abs(Radius(ii)-R2-dist2) < Tol) {
221 else if (nbsol2 == 2) {
222 Radius(1) = Abs(R2-dist2);
223 Radius(2) = R2+dist2;
226 if (Qualified3.IsEnclosed() && ok) {
227 if ((((L3.Location().X()-Center.X())*(-L3.Direction().Y()))+
228 ((L3.Location().Y()-Center.Y())*(L3.Direction().X())))<=0){
233 else if (Qualified2.IsOutside() && ok) {
234 if ((((L3.Location().X()-Center.X())*(-L3.Direction().Y()))+
235 ((L3.Location().Y()-Center.Y())*(L3.Direction().X())))>=0){
240 else if (Qualified2.IsUnqualified() && ok) {
245 for (Standard_Integer ind3 = 1 ; ind3 <= nbsol3 ; ind3++) {
247 cirsol(NbrSol) = gp_Circ2d(gp_Ax2d(Center,dirx),Radius(ind3));
248 // ==========================================================
249 Standard_Real distcc1 = Center.Distance(center1);
250 if (!Qualified1.IsUnqualified()) {
251 qualifier1(NbrSol) = Qualified1.Qualifier();
253 else if (Abs(distcc1+Radius(ind3)-R1) < Tol) {
254 qualifier1(NbrSol) = GccEnt_enclosed;
256 else if (Abs(distcc1-R1-Radius(ind3)) < Tol) {
257 qualifier1(NbrSol) = GccEnt_outside;
259 else { qualifier1(NbrSol) = GccEnt_enclosing; }
260 Standard_Real distcc2 = Center.Distance(center1);
261 if (!Qualified2.IsUnqualified()) {
262 qualifier2(NbrSol) = Qualified2.Qualifier();
264 else if (Abs(distcc2+Radius(ind3)-R2) < Tol) {
265 qualifier2(NbrSol) = GccEnt_enclosed;
267 else if (Abs(distcc2-R2-Radius(ind3)) < Tol) {
268 qualifier2(NbrSol) = GccEnt_outside;
270 else { qualifier2(NbrSol) = GccEnt_enclosing; }
271 gp_Dir2d dc3(origin3.XY()-Center.XY());
272 if (!Qualified3.IsUnqualified()) {
273 qualifier3(NbrSol) = Qualified3.Qualifier();
275 else if (dc3.Dot(normL3) > 0.0) {
276 qualifier3(NbrSol) = GccEnt_outside;
278 else { qualifier3(NbrSol) = GccEnt_enclosed; }
279 if (Center.Distance(C1.Location()) <= Tolerance &&
280 Abs(Radius(ind3)-R1) <= Tolerance) {
281 TheSame1(NbrSol) = 1;
284 TheSame1(NbrSol) = 0;
285 gp_Dir2d dc(C1.Location().XY()-Center.XY());
286 pnttg1sol(NbrSol)=gp_Pnt2d(Center.XY()+Radius(ind3)*dc.XY());
287 // POP for protection if cirsol(NbrSol).Location == pnttg1sol(NbrSol)
288 if (cirsol(NbrSol).Location().IsEqual(pnttg1sol(NbrSol),Precision::Confusion()))
291 par1sol(NbrSol)=ElCLib::Parameter(cirsol(NbrSol),
293 // POP for protection if C1.Location == pnttg1sol(NbrSol)
294 if (C1.Location().IsEqual(pnttg1sol(NbrSol),Precision::Confusion()))
297 pararg1(NbrSol)=ElCLib::Parameter(C1,pnttg1sol(NbrSol));
299 if (Center.Distance(C2.Location()) <= Tolerance &&
300 Abs(Radius(ind3)-R2) <= Tolerance) {
301 TheSame2(NbrSol) = 1;
304 TheSame2(NbrSol) = 0;
305 gp_Dir2d dc(C2.Location().XY()-Center.XY());
306 pnttg2sol(NbrSol)=gp_Pnt2d(Center.XY()+Radius(ind3)*dc.XY());
307 // POP for protection if cirsol(NbrSol).Location == pnttg1sol(NbrSol)
308 if (cirsol(NbrSol).Location().IsEqual(pnttg1sol(NbrSol),Precision::Confusion()))
311 par2sol(NbrSol)=ElCLib::Parameter(cirsol(NbrSol),
313 // POP for protection if C2.Location == pnttg2sol(NbrSol)
314 if (C2.Location().IsEqual(pnttg2sol(NbrSol),Precision::Confusion()))
317 pararg2(NbrSol)=ElCLib::Parameter(C2,pnttg2sol(NbrSol));
319 TheSame3(NbrSol) = 0;
320 gp_Dir2d dc(L3.Location().XY()-Center.XY());
321 Standard_Real sign = dc.Dot(gp_Dir2d(-L3.Direction().Y(),
322 L3.Direction().X()));
323 dc = gp_Dir2d(sign*gp_XY(-L3.Direction().Y(),
324 L3.Direction().X()));
325 pnttg3sol(NbrSol) = gp_Pnt2d(Center.XY()+Radius(ind3)*dc.XY());
326 par3sol(NbrSol)=ElCLib::Parameter(cirsol(NbrSol),
328 pararg3(NbrSol)=ElCLib::Parameter(L3,pnttg3sol(NbrSol));
333 WellDone = Standard_True;