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_Circ2dTanOnRad.hxx>
18 #include <GccEnt_BadQualifier.hxx>
19 #include <GccEnt_QualifiedCirc.hxx>
20 #include <GccEnt_QualifiedLin.hxx>
21 #include <gp_Circ2d.hxx>
22 #include <gp_Dir2d.hxx>
23 #include <gp_Lin2d.hxx>
24 #include <gp_Pnt2d.hxx>
25 #include <IntAna2d_AnaIntersection.hxx>
26 #include <IntAna2d_IntPoint.hxx>
27 #include <Standard_NegativeValue.hxx>
28 #include <Standard_OutOfRange.hxx>
29 #include <StdFail_NotDone.hxx>
30 #include <TColgp_Array1OfDir2d.hxx>
32 //=========================================================================
33 // Circle tangent to a circle Qualified1 (C1). +
34 // center on a circle OnCirc. +
35 // of radius Radius. +
37 // Initialize the table of solutions cirsol and all fields. +
38 // Eliminate cases not being the solution. +
39 // Create parallel(s) to C1 in the required direction(s). +
40 // Intersect parallel(s) with OnCirc and obtain the +
41 // center point(s) of found solution(s). +
42 // Create solution(s) cirsol. +
43 //=========================================================================
44 GccAna_Circ2dTanOnRad::
45 GccAna_Circ2dTanOnRad (const GccEnt_QualifiedCirc& Qualified1,
46 const gp_Circ2d& OnCirc ,
47 const Standard_Real Radius ,
48 const Standard_Real Tolerance ):
60 gp_Dir2d dirx(1.0,0.0);
61 Standard_Real Tol = Abs(Tolerance);
62 Standard_Integer signe[5];
68 Standard_Real disparal[2];
69 Standard_Integer nparal = 0;
70 Standard_Integer sign = 0;
71 WellDone = Standard_False;
73 if (!(Qualified1.IsEnclosed() || Qualified1.IsEnclosing() ||
74 Qualified1.IsOutside() || Qualified1.IsUnqualified())) {
75 throw GccEnt_BadQualifier();
78 gp_Circ2d C1 = Qualified1.Qualified();
79 TColgp_Array1OfPnt2d Center(1,4);
80 TColgp_Array1OfDir2d dir1on(1,4);
82 if (Radius < 0.0) { throw Standard_NegativeValue(); }
84 Standard_Real R1 = C1.Radius();
85 Standard_Real R2 = OnCirc.Radius();
86 Standard_Real c1x = C1.Location().X();
87 Standard_Real c1y = C1.Location().Y();
88 gp_Pnt2d center1(c1x,c1y);
89 Standard_Real dist = OnCirc.Location().Distance(center1);
90 Standard_Real onx = OnCirc.Location().X();
91 Standard_Real ony = OnCirc.Location().Y();
92 if (Qualified1.IsEnclosed()) {
93 // ============================
94 if ((Radius-R1 > Tol) || (R1-dist-R2-Radius > Tol)) {
95 WellDone=Standard_True;
98 if (Abs(Radius-R1) < Tol) {
99 if (OnCirc.Distance(center1) < Tol) {
103 qualifier1(NbrSol) = Qualified1.Qualifier();
104 TheSame1(NbrSol) = 1;
105 pntcen3(NbrSol) = center1;
106 parcen3(NbrSol)=ElCLib::Parameter(OnCirc,pntcen3(NbrSol));
107 WellDone = Standard_True;
109 else { WellDone = Standard_False; }
111 else if (Abs(R1-dist-R2-Radius) <= Tol) {
112 dir1on(1) = gp_Dir2d(c1x-onx,c1y-ony);
117 disparal[0] = Abs(R1-Radius);
121 else if (Qualified1.IsEnclosing()) {
122 // ==================================
123 if ((Tol<R1-Radius)||(Tol<R1+dist-R2-Radius)||(Radius-R1-dist-R2>Tol)) {
124 WellDone = Standard_True;
127 if (Abs(Radius-R1) < Tol) {
128 if (OnCirc.Distance(center1) < Tol) {
132 qualifier1(NbrSol) = Qualified1.Qualifier();
133 TheSame1(NbrSol) = 1;
134 pntcen3(NbrSol) = center1;
135 parcen3(NbrSol)=ElCLib::Parameter(OnCirc,pntcen3(NbrSol));
136 WellDone = Standard_True;
138 else { WellDone = Standard_True; }
140 else if ((Abs(R1+dist-R2-Radius)<Tol)||(Abs(Radius-R1-dist-R2)<Tol)) {
141 dir1on(1) = gp_Dir2d(c1x-onx,c1y-ony);
142 if (Abs(R1+dist-R2-Radius) < Tol) { sign = 1; }
147 disparal[0] = Abs(R1-Radius);
151 else if (Qualified1.IsOutside()) {
152 // ================================
153 if ((dist-R2-R1-Radius>Tol)||(Radius-dist-R2+R1>Tol)) {
154 WellDone=Standard_True;
157 dir1on(1) = gp_Dir2d(c1x-onx,c1y-ony);
158 if (Abs(R1-dist-R2+Radius) < Tol) {
161 else if (Abs(dist-R1-R2-Radius) < Tol) { sign = 1; }
164 disparal[0] = Abs(R1+Radius);
169 if ((dist-R2-R1-Radius>Tol)||(Radius-dist-R1-R2>Tol)) {
170 WellDone=Standard_True;
172 else if ((R1-dist-R2>Tol)&&(Tol<R1-R2-dist-Radius)) {
173 WellDone = Standard_True;
176 dir1on(1) = gp_Dir2d(c1x-onx,c1y-ony);
177 if (Abs(dist-R1-R2-Radius) < Tol) {
180 else if (dist+R1+R2-Radius < 0.0) { sign = -1; }
181 else if ((R1-dist+R2>0.0) && (R1-R2-dist-Radius>0.0)) { sign = -1; }
184 disparal[0] = Abs(R1+Radius);
185 disparal[1] = Abs(R1-Radius);
189 for (Standard_Integer jj = 0 ; jj < nparal ; jj++) {
190 IntAna2d_AnaIntersection Intp(OnCirc,
191 gp_Circ2d(gp_Ax2d(center1,dirx),disparal[jj]));
193 if (!Intp.IsEmpty()) {
194 for (Standard_Integer i = 1 ; i <= Intp.NbPoints() ; i++) {
196 Center(NbrSol) = gp_Pnt2d(Intp.Point(i).Value());
197 cirsol(NbrSol)=gp_Circ2d(gp_Ax2d(Center(NbrSol),dirx),Radius);
198 // =============================================================
199 Standard_Real distcc1 = Center(NbrSol).Distance(center1);
200 if (!Qualified1.IsUnqualified()) {
201 qualifier1(NbrSol) = Qualified1.Qualifier();
203 else if (Abs(distcc1+Radius-R1) < Tol) {
204 qualifier1(NbrSol) = GccEnt_enclosed;
206 else if (Abs(distcc1-R1-Radius) < Tol) {
207 qualifier1(NbrSol) = GccEnt_outside;
209 else { qualifier1(NbrSol) = GccEnt_enclosing; }
210 dir1on(NbrSol) = gp_Dir2d(c1x-Center(NbrSol).X(),
211 c1y-Center(NbrSol).Y());
212 if ((R1>Radius) && (Center(NbrSol).Distance (center1)<=R1)){
213 signe[NbrSol-1] = -1;
220 WellDone = Standard_True;
224 Center(1) = gp_Pnt2d(OnCirc.Location().XY()+sign*R2*dir1on(1).XY());
225 cirsol(1) = gp_Circ2d(gp_Ax2d(Center(1),dirx),Radius);
226 // =====================================================
227 Standard_Real distcc1 = Center(1).Distance(center1);
228 if (!Qualified1.IsUnqualified()) {
229 qualifier1(NbrSol) = Qualified1.Qualifier();
231 else if (Abs(distcc1+Radius-R1) < Tol) {
232 qualifier1(NbrSol) = GccEnt_enclosed;
234 else if (Abs(distcc1-R1-Radius) < Tol) {
235 qualifier1(NbrSol) = GccEnt_outside;
237 else { qualifier1(NbrSol) = GccEnt_enclosing; }
240 WellDone = Standard_True;
242 Standard_Integer ii = 0;
243 while (signe[ii] != 0) {
244 pnttg1sol(ii+1) = gp_Pnt2d(Center(ii+1).XY()+
245 signe[ii]*Radius*dir1on(ii+1).XY());
246 pntcen3(ii+1) = cirsol(ii+1).Location();
247 pararg1(ii+1)=ElCLib::Parameter(C1,pnttg1sol(ii+1));
248 par1sol(ii+1)=ElCLib::Parameter(cirsol(ii+1),pnttg1sol(ii+1));
249 parcen3(ii+1)=ElCLib::Parameter(OnCirc,pntcen3(ii+1));