Commit | Line | Data |
---|---|---|
b311480e | 1 | // Copyright (c) 1995-1999 Matra Datavision |
973c2be1 | 2 | // Copyright (c) 1999-2014 OPEN CASCADE SAS |
b311480e | 3 | // |
973c2be1 | 4 | // This file is part of Open CASCADE Technology software library. |
b311480e | 5 | // |
973c2be1 | 6 | // This library is free software; you can redistribute it and / or modify it |
7 | // under the terms of the GNU Lesser General Public 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. | |
b311480e | 11 | // |
973c2be1 | 12 | // Alternatively, this file may be used under the terms of Open CASCADE |
13 | // commercial license or contractual agreement. | |
b311480e | 14 | |
7fd59977 | 15 | // PRO12736 : bug quand OnLine // Ox, JCT 20/03/98 |
16 | ||
17 | //======================================================================== | |
0d969553 Y |
18 | // circular tangent to element of type : - Circle. + |
19 | // - Line. + | |
7fd59977 | 20 | // - Point. + |
0d969553 Y |
21 | // center on second element of type : - Circle. + |
22 | // - Line. + | |
23 | // of given radius : Radius. + | |
7fd59977 | 24 | //======================================================================== |
25 | ||
26 | #include <GccAna_Circ2dTanOnRad.ixx> | |
27 | ||
28 | #include <ElCLib.hxx> | |
29 | #include <math_DirectPolynomialRoots.hxx> | |
30 | #include <TColStd_Array1OfReal.hxx> | |
31 | #include <Standard_NegativeValue.hxx> | |
32 | #include <gp_Dir2d.hxx> | |
33 | #include <Standard_OutOfRange.hxx> | |
34 | #include <StdFail_NotDone.hxx> | |
35 | #include <GccEnt_BadQualifier.hxx> | |
36 | ||
37 | typedef math_DirectPolynomialRoots Roots; | |
38 | ||
39 | //========================================================================= | |
0d969553 Y |
40 | // Circle tangent : to circle Qualified1 (C1). + |
41 | // center : on straight line OnLine. + | |
42 | // of radius : Radius. + | |
7fd59977 | 43 | // + |
0d969553 Y |
44 | // Initialise the table of solutions cirsol and all fields. + |
45 | // Eliminate depending on the qualifier the cases not being solutions. + | |
46 | // Solve the equation of the second degree indicating that the found center + | |
47 | // point (xc,yc) is at a distance Radius from circle C1 and + | |
48 | // on straight line OnLine. + | |
49 | // The solutions aret represented by circles : + | |
50 | // - with center Pntcen(xc,yc) + | |
51 | // - with radius Radius. + | |
7fd59977 | 52 | //========================================================================= |
53 | ||
54 | GccAna_Circ2dTanOnRad:: | |
55 | GccAna_Circ2dTanOnRad (const GccEnt_QualifiedCirc& Qualified1, | |
56 | const gp_Lin2d& OnLine , | |
57 | const Standard_Real Radius , | |
58 | const Standard_Real Tolerance ) : | |
59 | cirsol(1,4) , | |
60 | qualifier1(1,4) , | |
61 | TheSame1(1,4) , | |
62 | pnttg1sol(1,4) , | |
63 | pntcen3(1,4) , | |
64 | par1sol(1,4) , | |
65 | pararg1(1,4) , | |
66 | parcen3(1,4) | |
67 | { | |
68 | ||
69 | TheSame1.Init(0); | |
70 | gp_Dir2d dirx(1.0,0.0); | |
71 | Standard_Real Tol = Abs(Tolerance); | |
72 | WellDone = Standard_False; | |
73 | NbrSol = 0; | |
74 | if (!(Qualified1.IsEnclosed() || Qualified1.IsEnclosing() || | |
75 | Qualified1.IsOutside() || Qualified1.IsUnqualified())) { | |
76 | GccEnt_BadQualifier::Raise(); | |
77 | return; | |
78 | } | |
79 | TColStd_Array1OfReal Coef(1,2); | |
80 | gp_Circ2d C1 = Qualified1.Qualified(); | |
81 | ||
82 | if (Radius < 0.0) { Standard_NegativeValue::Raise(); } | |
83 | else { | |
84 | Standard_Integer nbsol = 0; | |
85 | Standard_Integer signe = 0; | |
86 | gp_Pnt2d Center; | |
87 | Standard_Real xc; | |
88 | Standard_Real yc; | |
89 | Standard_Real R1 = C1.Radius(); | |
90 | Standard_Real dist = OnLine.Distance(C1.Location()); | |
91 | Standard_Real xdir = (OnLine.Direction()).X(); | |
92 | Standard_Real ydir = (OnLine.Direction()).Y(); | |
93 | Standard_Real lxloc = (OnLine.Location()).X(); | |
94 | Standard_Real lyloc = (OnLine.Location()).Y(); | |
95 | gp_Pnt2d center1(C1.Location()); | |
96 | Standard_Real x1 = center1.X(); | |
97 | Standard_Real y1 = center1.Y(); | |
98 | Standard_Real xbid = 0.; | |
99 | if (Qualified1.IsEnclosed()) { | |
100 | // ============================ | |
101 | if (Tol < Radius-R1+dist) { WellDone = Standard_True; } | |
102 | else { | |
103 | if (Abs(Radius-R1+dist) < Tol) { | |
104 | WellDone = Standard_True; | |
105 | NbrSol = 1; | |
106 | if (-ydir*(x1-lxloc)+xdir*(y1-lyloc)<0.0) { | |
107 | Center = gp_Pnt2d(x1-ydir*dist,y1+xdir*dist); | |
108 | } | |
109 | else { Center = gp_Pnt2d(x1+ydir*dist,y1-xdir*dist); } | |
110 | signe = 1; | |
111 | } | |
112 | else { | |
113 | Coef(1) = (R1-Radius)*(R1-Radius); | |
114 | nbsol = 1; | |
115 | } | |
116 | } | |
117 | } | |
118 | else if (Qualified1.IsEnclosing()) { | |
119 | // ================================== | |
120 | if (R1+dist-Radius > Tol) { WellDone = Standard_True; } | |
121 | else { | |
122 | if (R1+dist-Radius > 0.0) { | |
123 | WellDone = Standard_True; | |
124 | NbrSol = 1; | |
125 | if (-ydir*(x1-lxloc)+xdir*(y1-lyloc)<0.0) { | |
126 | Center = gp_Pnt2d(x1-ydir*dist,y1+xdir*dist); | |
127 | } | |
128 | else { Center = gp_Pnt2d(x1+ydir*dist,y1-xdir*dist); } | |
129 | signe = -1; | |
130 | } | |
131 | else { | |
132 | Coef(1) = (Radius-R1)*(Radius-R1); | |
133 | nbsol = 1; | |
134 | } | |
135 | } | |
136 | } | |
137 | else { | |
138 | // ==== | |
139 | if (dist-R1-Radius > Tol) { WellDone = Standard_False; } | |
140 | else { | |
141 | if (Abs(dist-R1-Radius) < Tol) { | |
142 | WellDone = Standard_True; | |
143 | NbrSol = 1; | |
144 | if (-ydir*(x1-lxloc)+xdir*(y1-lyloc)<0.0) { | |
145 | Center = gp_Pnt2d(x1-ydir*dist,y1+xdir*dist); | |
146 | } | |
147 | else { Center = gp_Pnt2d(x1+ydir*dist,y1-xdir*dist); } | |
148 | signe = -1; | |
149 | } | |
150 | else { | |
151 | if (Qualified1.IsOutside()) { | |
152 | // =========================== | |
153 | Coef(1) = (Radius+R1)*(Radius+R1); | |
154 | nbsol = 1; | |
155 | } | |
156 | else { | |
157 | // ==== | |
158 | Coef(1) = (Radius-R1)*(Radius-R1); | |
159 | Coef(2) = (Radius+R1)*(Radius+R1); | |
160 | nbsol = 2; | |
161 | } | |
162 | } | |
163 | } | |
164 | } | |
165 | if (signe != 0) { | |
166 | cirsol(1) = gp_Circ2d(gp_Ax2d(Center,dirx),Radius); | |
167 | // ================================================== | |
168 | Standard_Real distcc1 = Center.Distance(center1); | |
169 | if (!Qualified1.IsUnqualified()) { | |
170 | qualifier1(1) = Qualified1.Qualifier(); | |
171 | } | |
172 | else if (Abs(distcc1+Radius-R1) < Tol) { | |
173 | qualifier1(1) = GccEnt_enclosed; | |
174 | } | |
175 | else if (Abs(distcc1-R1-Radius) < Tol) { | |
176 | qualifier1(1) = GccEnt_outside; | |
177 | } | |
178 | else { qualifier1(1) = GccEnt_enclosing; } | |
179 | if (Abs(Radius-R1) <= Tol) { TheSame1(1) = 1; } | |
180 | else { | |
181 | gp_Dir2d dir1cen(Center.X()-x1,Center.Y()-y1); | |
182 | pnttg1sol(1) = gp_Pnt2d(Center.XY()+signe*Radius*dir1cen.XY()); | |
183 | par1sol(1)=ElCLib::Parameter(cirsol(1),pnttg1sol(1)); | |
184 | pararg1(1)=ElCLib::Parameter(C1,pnttg1sol(1)); | |
185 | } | |
186 | pntcen3(1) = cirsol(NbrSol).Location(); | |
187 | parcen3(1)=ElCLib::Parameter(OnLine,pntcen3(1)); | |
188 | } | |
189 | else if (nbsol > 0) { | |
190 | for (Standard_Integer j = 1 ; j <= nbsol ; j++) { | |
191 | Standard_Real A,B,C; | |
192 | OnLine.Coefficients(A,B,C); | |
193 | Standard_Real D = A; | |
194 | Standard_Real x0,y0; | |
195 | if ( Abs(D) <= Tol ) { | |
196 | A = B; | |
197 | B = D; | |
198 | xbid = x1; | |
199 | x0 = y1; | |
200 | y0 = x1; | |
201 | } | |
202 | else{ | |
203 | x0 = x1; | |
204 | y0 = y1; | |
205 | } | |
206 | Roots Sol((B*B+A*A)/(A*A), | |
207 | 2.0*(B*C/(A*A)+(B/A)*x0-y0), | |
208 | x0*x0+y0*y0+C*C/(A*A)-Coef(j)+2.0*C*x0/A); | |
209 | if (Sol.IsDone()) { | |
210 | for (Standard_Integer i = 1 ; i <= Sol.NbSolutions() ; i++) { | |
211 | ||
212 | if ( Abs(D) > Tol ) { | |
213 | yc = Sol.Value(i); | |
214 | xc = -(B/A)*yc-C/A; | |
215 | } | |
216 | else { | |
217 | xc = Sol.Value(i); | |
218 | yc = -(B/A)*xc-C/A; | |
219 | } | |
220 | Center = gp_Pnt2d(xc,yc); | |
221 | if (OnLine.Distance(Center)>Tol) | |
222 | continue; | |
223 | NbrSol++; | |
224 | cirsol(NbrSol) = gp_Circ2d(gp_Ax2d(Center,dirx),Radius); | |
225 | // ======================================================= | |
226 | Standard_Real distcc1 = Center.Distance(center1); | |
227 | if (!Qualified1.IsUnqualified()) { | |
228 | qualifier1(NbrSol) = Qualified1.Qualifier(); | |
229 | } | |
230 | else if (Abs(distcc1+Radius-R1) < Tol) { | |
231 | qualifier1(NbrSol) = GccEnt_enclosed; | |
232 | } | |
233 | else if (Abs(distcc1-R1-Radius) < Tol) { | |
234 | qualifier1(NbrSol) = GccEnt_outside; | |
235 | } | |
236 | else { qualifier1(NbrSol) = GccEnt_enclosing; } | |
237 | gp_Dir2d dir1cen(Center.X()-x1,Center.Y()-y1); | |
238 | if ((Radius > R1) || (Center.Distance(center1) > R1)) { | |
239 | pnttg1sol(NbrSol) = gp_Pnt2d(Center.XY()+Radius*dir1cen.XY()); | |
240 | } | |
241 | else { | |
242 | pnttg1sol(NbrSol) = gp_Pnt2d(Center.XY()-Radius*dir1cen.XY()); | |
243 | } | |
244 | pntcen3(NbrSol) = cirsol(NbrSol).Location(); | |
245 | par1sol(NbrSol)=ElCLib::Parameter(cirsol(NbrSol), | |
246 | pnttg1sol(NbrSol)); | |
247 | pararg1(NbrSol)=ElCLib::Parameter(C1,pnttg1sol(NbrSol)); | |
248 | parcen3(NbrSol)=ElCLib::Parameter(OnLine,pntcen3(NbrSol)); | |
249 | } | |
250 | WellDone = Standard_True; | |
251 | } | |
252 | } | |
253 | } | |
254 | } | |
255 | } | |
256 | ||
257 | Standard_Boolean GccAna_Circ2dTanOnRad:: | |
258 | IsDone () const { return WellDone; } | |
259 | ||
260 | Standard_Integer GccAna_Circ2dTanOnRad:: | |
261 | NbSolutions () const { return NbrSol; } | |
262 | ||
263 | gp_Circ2d GccAna_Circ2dTanOnRad::ThisSolution (const Standard_Integer Index) const | |
264 | { | |
265 | if (Index > NbrSol || Index <= 0) { | |
266 | Standard_OutOfRange::Raise(); | |
267 | } | |
268 | return cirsol(Index); | |
269 | } | |
270 | ||
271 | void GccAna_Circ2dTanOnRad:: | |
272 | WhichQualifier(const Standard_Integer Index , | |
273 | GccEnt_Position& Qualif1 ) const | |
274 | { | |
275 | if (!WellDone) { StdFail_NotDone::Raise(); } | |
276 | else if (Index <= 0 ||Index > NbrSol) { Standard_OutOfRange::Raise(); } | |
277 | else { | |
278 | Qualif1 = qualifier1(Index); | |
279 | } | |
280 | } | |
281 | ||
282 | void GccAna_Circ2dTanOnRad:: | |
283 | Tangency1 (const Standard_Integer Index, | |
284 | Standard_Real& ParSol, | |
285 | Standard_Real& ParArg, | |
286 | gp_Pnt2d& PntSol) const{ | |
287 | if (!WellDone) { | |
288 | StdFail_NotDone::Raise(); | |
289 | } | |
290 | else if (Index <= 0 ||Index > NbrSol) { | |
291 | Standard_OutOfRange::Raise(); | |
292 | } | |
293 | else { | |
294 | ParSol = par1sol(Index); | |
295 | ParArg = pararg1(Index); | |
296 | PntSol = gp_Pnt2d(pnttg1sol(Index)); | |
297 | } | |
298 | } | |
299 | ||
300 | ||
301 | void GccAna_Circ2dTanOnRad:: | |
302 | CenterOn3 (const Standard_Integer Index, | |
303 | Standard_Real& ParArg, | |
304 | gp_Pnt2d& PntSol) const{ | |
305 | if (!WellDone) { | |
306 | StdFail_NotDone::Raise(); | |
307 | } | |
308 | else if (Index <= 0 ||Index > NbrSol) { | |
309 | Standard_OutOfRange::Raise(); | |
310 | } | |
311 | else { | |
312 | ParArg = parcen3(Index); | |
313 | PntSol = pnttg1sol(Index); | |
314 | } | |
315 | } | |
316 | ||
317 | Standard_Boolean GccAna_Circ2dTanOnRad::IsTheSame1 (const Standard_Integer Index) const | |
318 | { | |
319 | if (!WellDone) | |
320 | StdFail_NotDone::Raise(); | |
321 | ||
322 | if (Index <= 0 ||Index > NbrSol) | |
323 | Standard_OutOfRange::Raise(); | |
324 | ||
325 | if (TheSame1(Index) == 0) | |
326 | return Standard_False; | |
327 | ||
328 | return Standard_True; | |
329 | } |