Commit | Line | Data |
---|---|---|
b311480e | 1 | // Created on: 1994-01-04 |
2 | // Created by: Christophe MARION | |
3 | // Copyright (c) 1994-1999 Matra Datavision | |
973c2be1 | 4 | // Copyright (c) 1999-2014 OPEN CASCADE SAS |
b311480e | 5 | // |
973c2be1 | 6 | // This file is part of Open CASCADE Technology software library. |
b311480e | 7 | // |
d5f74e42 | 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 | |
973c2be1 | 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. | |
b311480e | 13 | // |
973c2be1 | 14 | // Alternatively, this file may be used under the terms of Open CASCADE |
15 | // commercial license or contractual agreement. | |
7fd59977 | 16 | |
7fd59977 | 17 | |
7fd59977 | 18 | #include <ElCLib.hxx> |
42cf5bc1 | 19 | #include <Extrema_ExtElC2d.hxx> |
20 | #include <Extrema_ExtPElC2d.hxx> | |
21 | #include <Extrema_POnCurv2d.hxx> | |
22 | #include <gp_Circ2d.hxx> | |
23 | #include <gp_Elips2d.hxx> | |
24 | #include <gp_Hypr2d.hxx> | |
25 | #include <gp_Lin2d.hxx> | |
26 | #include <gp_Parab2d.hxx> | |
7fd59977 | 27 | #include <math_DirectPolynomialRoots.hxx> |
42cf5bc1 | 28 | #include <math_TrigonometricFunctionRoots.hxx> |
7fd59977 | 29 | #include <Precision.hxx> |
42cf5bc1 | 30 | #include <Standard_NotImplemented.hxx> |
31 | #include <Standard_OutOfRange.hxx> | |
32 | #include <StdFail_InfiniteSolutions.hxx> | |
33 | #include <StdFail_NotDone.hxx> | |
7fd59977 | 34 | |
35 | //============================================================================= | |
7fd59977 | 36 | Extrema_ExtElC2d::Extrema_ExtElC2d () { myDone = Standard_False; } |
37 | //============================================================================= | |
38 | ||
15a954de | 39 | //======================================================================= |
40 | //function : Extrema_ExtElC2d | |
41 | //purpose : | |
42 | //======================================================================= | |
43 | Extrema_ExtElC2d::Extrema_ExtElC2d (const gp_Lin2d& C1, | |
44 | const gp_Lin2d& C2, | |
45 | const Standard_Real) | |
7fd59977 | 46 | /*----------------------------------------------------------------------------- |
0d969553 Y |
47 | Function: |
48 | Find min distance between 2 straight lines. | |
49 | ||
50 | Method: | |
51 | Let D1 and D2 be 2 directions of straight lines C1 and C2. | |
52 | 2 cases are considered: | |
53 | 1- if Angle(D1,D2) < AngTol, the straight lines are parallel. | |
54 | The distance is the distance between any point of C1 and straight line C2. | |
55 | 2- if Angle(D1,D2) > AngTol: | |
56 | Let P = C1(u1) and P =C2(u2) the point intersection: | |
7fd59977 | 57 | |
58 | -----------------------------------------------------------------------------*/ | |
59 | { | |
60 | myDone = Standard_False; | |
61 | myIsPar = Standard_False; | |
62 | myNbExt = 0; | |
63 | ||
15a954de | 64 | gp_Vec2d D1(C1.Direction()); |
65 | gp_Vec2d D2(C2.Direction()); | |
66 | if (D1.IsParallel(D2, Precision::Angular())) | |
67 | { | |
7fd59977 | 68 | myIsPar = Standard_True; |
69 | mySqDist[0] = C2.SquareDistance(C1.Location()); | |
70 | } | |
15a954de | 71 | else |
72 | { | |
73 | // Vector from P1 to P2 (P2 - P1). | |
74 | gp_Vec2d aP1P2(C1.Location(), C2.Location()); | |
75 | ||
76 | // Solve linear system using Cramer's rule: | |
77 | // D1.X * t1 + D2.X * (-t2) = P2.X - P1.X | |
78 | // D1.Y * t1 + D2.Y * (-t2) = P2.Y - P1.Y | |
79 | ||
80 | // There is no division by zero since lines are not parallel. | |
81 | Standard_Real aDelim = 1 / (D1^D2); | |
82 | ||
83 | Standard_Real aParam1 = (aP1P2 ^ D2) * aDelim; | |
84 | Standard_Real aParam2 = -(D1 ^ aP1P2) * aDelim; // -1.0 coefficient before t2. | |
85 | ||
86 | gp_Pnt2d P1 = ElCLib::Value(aParam1, C1); | |
87 | gp_Pnt2d P2 = ElCLib::Value(aParam2, C2); | |
88 | ||
89 | mySqDist[myNbExt] = 0.0; | |
90 | myPoint[myNbExt][0] = Extrema_POnCurv2d(aParam1,P1); | |
91 | myPoint[myNbExt][1] = Extrema_POnCurv2d(aParam2,P2); | |
92 | myNbExt = 1; | |
7fd59977 | 93 | } |
15a954de | 94 | |
7fd59977 | 95 | myDone = Standard_True; |
96 | } | |
97 | //============================================================================= | |
98 | ||
99 | Extrema_ExtElC2d::Extrema_ExtElC2d (const gp_Lin2d& C1, | |
100 | const gp_Circ2d& C2, | |
101 | const Standard_Real) | |
102 | /*----------------------------------------------------------------------------- | |
0d969553 Y |
103 | Function: |
104 | Find extreme distances between straight line C1 and circle C2. | |
105 | ||
106 | Method: | |
107 | Let P1=C1(u1) and P2=C2(u2) be two solution points | |
108 | D the direction of straight line C1 | |
109 | T the tangent at point P2; | |
110 | Then, ( P1P2.D = 0. (1) | |
7fd59977 | 111 | ( P1P2.T = 0. (2) |
112 | -----------------------------------------------------------------------------*/ | |
113 | { | |
114 | myIsPar = Standard_False; | |
115 | myDone = Standard_False; | |
116 | myNbExt = 0; | |
117 | ||
0d969553 | 118 | // Calculate T1 in the reference of the circle ... |
7fd59977 | 119 | gp_Dir2d D = C1.Direction(); |
120 | gp_Dir2d x2, y2; | |
121 | x2 = C2.XAxis().Direction(); | |
122 | y2 = C2.YAxis().Direction(); | |
123 | ||
124 | Standard_Real Dx = D.Dot(x2); | |
125 | Standard_Real Dy = D.Dot(y2); | |
126 | Standard_Real U1, teta[2]; | |
127 | gp_Pnt2d O1=C1.Location(); | |
7fd59977 | 128 | gp_Pnt2d P1, P2; |
7fd59977 | 129 | |
130 | if (Abs(Dy) <= RealEpsilon()) { | |
c6541a0c | 131 | teta[0] = M_PI/2.0; |
7fd59977 | 132 | } |
133 | else teta[0] = ATan(-Dx/Dy); | |
c6541a0c D |
134 | teta[1] = teta[0]+ M_PI; |
135 | if (teta[0] < 0.0) teta[0] = teta[0] + 2.0*M_PI; | |
7fd59977 | 136 | |
137 | P2 = ElCLib::Value(teta[0], C2); | |
138 | U1 = (gp_Vec2d(O1, P2)).Dot(D); | |
139 | P1 = ElCLib::Value(U1, C1); | |
140 | mySqDist[myNbExt] = P1.SquareDistance(P2); | |
141 | myPoint[myNbExt][0] = Extrema_POnCurv2d(U1,P1); | |
142 | myPoint[myNbExt][1] = Extrema_POnCurv2d(teta[0],P2); | |
143 | myNbExt++; | |
144 | ||
145 | P2 = ElCLib::Value(teta[1], C2); | |
146 | U1 = (gp_Vec2d(O1, P2)).Dot(D); | |
147 | P1 = ElCLib::Value(U1, C1); | |
148 | mySqDist[myNbExt] = P1.SquareDistance(P2); | |
149 | myPoint[myNbExt][0] = Extrema_POnCurv2d(U1,P1); | |
150 | myPoint[myNbExt][1] = Extrema_POnCurv2d(teta[1],P2); | |
151 | myNbExt++; | |
152 | myDone = Standard_True; | |
153 | } | |
154 | ||
155 | ||
156 | // ============================================================================= | |
157 | Extrema_ExtElC2d::Extrema_ExtElC2d (const gp_Lin2d& C1, | |
158 | const gp_Elips2d& C2) | |
159 | { | |
160 | myDone = Standard_True; | |
161 | myIsPar = Standard_False; | |
162 | myDone = Standard_False; | |
163 | myNbExt = 0; | |
164 | ||
0d969553 | 165 | // Calculate T1 in the reference of the ellipse ... |
7fd59977 | 166 | gp_Dir2d D = C1.Direction(); |
167 | gp_Dir2d x2, y2; | |
168 | x2 = C2.XAxis().Direction(); | |
169 | y2 = C2.YAxis().Direction(); | |
170 | ||
171 | Standard_Real Dx = D.Dot(x2); | |
172 | Standard_Real Dy = D.Dot(y2); | |
173 | Standard_Real U1, teta[2], r1 = C2.MajorRadius(), r2 = C2.MinorRadius(); | |
7fd59977 | 174 | gp_Pnt2d O1=C1.Location(), P1, P2; |
7fd59977 | 175 | |
176 | if (Abs(Dy) <= RealEpsilon()) { | |
c6541a0c | 177 | teta[0] = M_PI/2.0; |
7fd59977 | 178 | } |
179 | else teta[0] = ATan(-Dx*r2/(Dy*r1)); | |
180 | ||
c6541a0c D |
181 | teta[1] = teta[0] + M_PI; |
182 | if (teta[0] < 0.0) teta[0] += 2.0*M_PI; | |
7fd59977 | 183 | P2 = ElCLib::Value(teta[0], C2); |
184 | U1 = (gp_Vec2d(O1, P2)).Dot(D); | |
185 | P1 = ElCLib::Value(U1, C1); | |
186 | mySqDist[myNbExt] = P1.SquareDistance(P2); | |
187 | myPoint[myNbExt][0] = Extrema_POnCurv2d(U1,P1); | |
188 | myPoint[myNbExt][1] = Extrema_POnCurv2d(teta[0],P2); | |
189 | myNbExt++; | |
190 | ||
191 | ||
192 | P2 = ElCLib::Value(teta[1], C2); | |
193 | U1 = (gp_Vec2d(O1, P2)).Dot(D); | |
194 | P1 = ElCLib::Value(U1, C1); | |
195 | mySqDist[myNbExt] = P1.SquareDistance(P2); | |
196 | myPoint[myNbExt][0] = Extrema_POnCurv2d(U1,P1); | |
197 | myPoint[myNbExt][1] = Extrema_POnCurv2d(teta[1],P2); | |
198 | myNbExt++; | |
199 | myDone = Standard_True; | |
200 | } | |
201 | ||
202 | ||
203 | ||
204 | //============================================================================= | |
205 | ||
206 | Extrema_ExtElC2d::Extrema_ExtElC2d (const gp_Lin2d& C1, const gp_Hypr2d& C2) | |
207 | { | |
208 | myIsPar = Standard_False; | |
209 | myDone = Standard_False; | |
210 | myNbExt = 0; | |
211 | ||
0d969553 | 212 | // Calculate T1 in the reference of the parabole ... |
7fd59977 | 213 | gp_Dir2d D = C1.Direction(); |
214 | gp_Dir2d x2, y2; | |
215 | x2 = C2.XAxis().Direction(); | |
216 | y2 = C2.YAxis().Direction(); | |
217 | Standard_Real Dx = D.Dot(x2); | |
218 | Standard_Real Dy = D.Dot(y2); | |
219 | ||
220 | Standard_Real U1, v2, U2=0, R = C2.MajorRadius(), r = C2.MinorRadius(); | |
221 | gp_Pnt2d P1, P2; | |
222 | if (Abs(Dy) < RealEpsilon()) { return;} | |
223 | if (Abs(R - r*Dx/Dy) < RealEpsilon()) return; | |
224 | ||
225 | v2 = (R + r*Dx/Dy)/(R - r*Dx/Dy); | |
226 | if (v2 > 0.0) U2 = Log(Sqrt(v2)); | |
227 | P2 = ElCLib::Value(U2, C2); | |
228 | ||
229 | U1 = (gp_Vec2d(C1.Location(), P2)).Dot(D); | |
230 | P1 = ElCLib::Value(U1, C1); | |
231 | mySqDist[myNbExt] = P1.SquareDistance(P2); | |
232 | myPoint[myNbExt][0] = Extrema_POnCurv2d(U1,P1); | |
233 | myPoint[myNbExt][1] = Extrema_POnCurv2d(U2,P2); | |
234 | myNbExt++; | |
235 | myDone = Standard_True; | |
236 | } | |
237 | ||
238 | ||
239 | ||
240 | //============================================================================ | |
241 | ||
242 | Extrema_ExtElC2d::Extrema_ExtElC2d (const gp_Lin2d& C1, const gp_Parab2d& C2) | |
243 | { | |
244 | myIsPar = Standard_False; | |
245 | myDone = Standard_False; | |
246 | myNbExt = 0; | |
247 | ||
0d969553 | 248 | // Calculate T1 in the reference of the parabole ... |
7fd59977 | 249 | gp_Dir2d D = C1.Direction(); |
250 | gp_Dir2d x2, y2; | |
251 | x2 = C2.MirrorAxis().Direction(); | |
252 | y2 = C2.Axis().YAxis().Direction(); | |
253 | Standard_Real Dx = D.Dot(x2); | |
254 | Standard_Real Dy = D.Dot(y2); | |
255 | ||
256 | Standard_Real U1, U2, P = C2.Parameter(); | |
257 | gp_Pnt2d P1, P2; | |
258 | if (Abs(Dy) < RealEpsilon()) { return; } | |
259 | U2 = Dx*P/Dy; | |
260 | P2 = ElCLib::Value(U2, C2); | |
261 | ||
262 | U1 = (gp_Vec2d(C1.Location(), P2)).Dot(D); | |
263 | P1 = ElCLib::Value(U1, C1); | |
264 | mySqDist[myNbExt] = P1.SquareDistance(P2); | |
265 | myPoint[myNbExt][0] = Extrema_POnCurv2d(U1,P1); | |
266 | myPoint[myNbExt][1] = Extrema_POnCurv2d(U2,P2); | |
267 | myNbExt++; | |
268 | myDone = Standard_True; | |
269 | } | |
270 | ||
271 | ||
272 | ||
273 | //============================================================================ | |
274 | ||
275 | Extrema_ExtElC2d::Extrema_ExtElC2d (const gp_Circ2d& C1, const gp_Circ2d& C2) | |
276 | { | |
277 | myIsPar = Standard_False; | |
278 | myDone = Standard_False; | |
279 | myNbExt = 0; | |
280 | myDone = Standard_True; | |
281 | ||
282 | gp_Pnt2d O1 = C1.Location(); | |
283 | gp_Pnt2d O2 = C2.Location(); | |
284 | ||
285 | gp_Vec2d DO1O2 (O1, O2); | |
286 | if (DO1O2.Magnitude() < Precision::Confusion()) { | |
287 | myIsPar = Standard_True; | |
288 | return; | |
289 | } | |
290 | ||
291 | Standard_Integer NoSol, kk; | |
292 | Standard_Real U1, U2; | |
293 | Standard_Real r1 = C1.Radius(), r2 = C2.Radius(); | |
294 | Standard_Real Usol2[2], Usol1[2]; | |
295 | gp_Pnt2d P1[2], P2[2]; | |
296 | gp_Dir2d O1O2(DO1O2); | |
297 | ||
298 | P1[0] = O1.Translated(r1*O1O2); | |
299 | Usol1[0] = ElCLib::Parameter(C1, P1[0]); | |
300 | P1[1] = O1.Translated(-r1*O1O2); | |
301 | Usol1[1] = ElCLib::Parameter(C1, P1[1]); | |
302 | ||
303 | P2[0] = O2.Translated(r2*O1O2); | |
304 | Usol2[0] = ElCLib::Parameter(C2, P2[0]); | |
305 | P2[1] = O2.Translated(-r2*O1O2); | |
306 | Usol2[1] = ElCLib::Parameter(C2, P2[1]); | |
307 | ||
308 | for (NoSol = 0; NoSol <= 1; NoSol++) { | |
309 | U1 = Usol1[NoSol]; | |
310 | for (kk = 0; kk <= 1; kk++) { | |
311 | U2 = Usol2[kk]; | |
312 | mySqDist[myNbExt] = P2[kk].SquareDistance(P1[NoSol]); | |
313 | myPoint[myNbExt][0] = Extrema_POnCurv2d(U1, P1[NoSol]); | |
314 | myPoint[myNbExt][1] = Extrema_POnCurv2d(U2, P2[kk]); | |
315 | myNbExt++; | |
316 | } | |
317 | } | |
318 | } | |
319 | //=========================================================================== | |
320 | ||
321 | Extrema_ExtElC2d::Extrema_ExtElC2d (const gp_Circ2d& C1, const gp_Elips2d& C2) | |
322 | { | |
323 | myIsPar = Standard_False; | |
324 | myDone = Standard_False; | |
325 | myNbExt = 0; | |
326 | ||
327 | Standard_Integer i, j; | |
328 | ||
329 | Extrema_ExtPElC2d ExtElip(C1.Location(), C2, | |
c6541a0c | 330 | Precision::Confusion(), 0.0, 2.0*M_PI); |
7fd59977 | 331 | |
332 | if (ExtElip.IsDone()) { | |
333 | for (i = 1; i <= ExtElip.NbExt(); i++) { | |
334 | Extrema_ExtPElC2d ExtCirc(ExtElip.Point(i).Value(), C1, | |
c6541a0c | 335 | Precision::Confusion(), 0.0, 2.0*M_PI); |
7fd59977 | 336 | if (ExtCirc.IsDone()) { |
337 | for (j = 1; j <= ExtCirc.NbExt(); j++) { | |
338 | mySqDist[myNbExt] = ExtCirc.SquareDistance(j); | |
339 | myPoint[myNbExt][0] = ExtCirc.Point(j); | |
340 | myPoint[myNbExt][1] = ExtElip.Point(i); | |
341 | myNbExt++; | |
342 | } | |
343 | } | |
344 | myDone = Standard_True; | |
345 | } | |
346 | } | |
347 | } | |
348 | //============================================================================ | |
349 | ||
350 | Extrema_ExtElC2d::Extrema_ExtElC2d (const gp_Circ2d& C1, const gp_Hypr2d& C2) | |
351 | { | |
352 | myIsPar = Standard_False; | |
353 | myDone = Standard_False; | |
354 | myNbExt = 0; | |
355 | ||
356 | Standard_Integer i, j; | |
357 | ||
358 | Extrema_ExtPElC2d ExtHyp(C1.Location(), C2, Precision::Confusion(), | |
359 | RealFirst(), RealLast()); | |
360 | ||
361 | if (ExtHyp.IsDone()) { | |
362 | for (i = 1; i <= ExtHyp.NbExt(); i++) { | |
363 | Extrema_ExtPElC2d ExtCirc(ExtHyp.Point(i).Value(), C1, | |
c6541a0c | 364 | Precision::Confusion(), 0.0, 2.0*M_PI); |
7fd59977 | 365 | if (ExtCirc.IsDone()) { |
366 | for (j = 1; j <= ExtCirc.NbExt(); j++) { | |
367 | mySqDist[myNbExt] = ExtCirc.SquareDistance(j); | |
368 | myPoint[myNbExt][0] = ExtCirc.Point(j); | |
369 | myPoint[myNbExt][1] = ExtHyp.Point(i); | |
370 | myNbExt++; | |
371 | } | |
372 | } | |
373 | myDone = Standard_True; | |
374 | } | |
375 | } | |
376 | } | |
377 | //============================================================================ | |
378 | ||
379 | Extrema_ExtElC2d::Extrema_ExtElC2d (const gp_Circ2d& C1, const gp_Parab2d& C2) | |
380 | { | |
381 | myIsPar = Standard_False; | |
382 | myDone = Standard_False; | |
383 | myNbExt = 0; | |
384 | ||
385 | Standard_Integer i, j; | |
386 | ||
387 | Extrema_ExtPElC2d ExtParab(C1.Location(), C2, Precision::Confusion(), | |
388 | RealFirst(), RealLast()); | |
389 | ||
390 | if (ExtParab.IsDone()) { | |
391 | for (i = 1; i <= ExtParab.NbExt(); i++) { | |
392 | Extrema_ExtPElC2d ExtCirc(ExtParab.Point(i).Value(), | |
c6541a0c | 393 | C1, Precision::Confusion(), 0.0, 2.0*M_PI); |
7fd59977 | 394 | if (ExtCirc.IsDone()) { |
395 | for (j = 1; j <= ExtCirc.NbExt(); j++) { | |
396 | mySqDist[myNbExt] = ExtCirc.SquareDistance(j); | |
397 | myPoint[myNbExt][0] = ExtCirc.Point(j); | |
398 | myPoint[myNbExt][1] = ExtParab.Point(i); | |
399 | myNbExt++; | |
400 | } | |
401 | } | |
402 | myDone = Standard_True; | |
403 | } | |
404 | } | |
405 | } | |
406 | //============================================================================ | |
407 | ||
408 | Extrema_ExtElC2d::Extrema_ExtElC2d (const gp_Elips2d&, const gp_Elips2d&) | |
409 | { | |
410 | Standard_NotImplemented::Raise(); | |
411 | } | |
412 | //============================================================================ | |
413 | ||
414 | Extrema_ExtElC2d::Extrema_ExtElC2d (const gp_Elips2d&, const gp_Hypr2d&) | |
415 | { | |
416 | Standard_NotImplemented::Raise(); | |
417 | } | |
418 | //============================================================================ | |
419 | ||
420 | Extrema_ExtElC2d::Extrema_ExtElC2d (const gp_Elips2d&, const gp_Parab2d&) | |
421 | { | |
422 | Standard_NotImplemented::Raise(); | |
423 | } | |
424 | //============================================================================ | |
425 | ||
426 | Extrema_ExtElC2d::Extrema_ExtElC2d (const gp_Hypr2d&, const gp_Hypr2d&) | |
427 | { | |
428 | Standard_NotImplemented::Raise(); | |
429 | } | |
430 | //============================================================================ | |
431 | ||
432 | Extrema_ExtElC2d::Extrema_ExtElC2d (const gp_Hypr2d&, const gp_Parab2d&) | |
433 | { | |
434 | Standard_NotImplemented::Raise(); | |
435 | } | |
436 | //============================================================================ | |
437 | ||
438 | Extrema_ExtElC2d::Extrema_ExtElC2d (const gp_Parab2d&, const gp_Parab2d&) | |
439 | { | |
440 | Standard_NotImplemented::Raise(); | |
441 | } | |
442 | //============================================================================ | |
443 | ||
444 | Standard_Boolean Extrema_ExtElC2d::IsDone () const { return myDone; } | |
445 | //============================================================================ | |
446 | ||
447 | Standard_Boolean Extrema_ExtElC2d::IsParallel () const | |
448 | { | |
449 | if (!IsDone()) { StdFail_NotDone::Raise(); } | |
450 | return myIsPar; | |
451 | } | |
452 | //============================================================================ | |
453 | ||
454 | Standard_Integer Extrema_ExtElC2d::NbExt () const | |
455 | { | |
456 | if (IsParallel()) { StdFail_InfiniteSolutions::Raise(); } | |
457 | return myNbExt; | |
458 | } | |
459 | //============================================================================ | |
460 | ||
461 | Standard_Real Extrema_ExtElC2d::SquareDistance (const Standard_Integer N) const | |
462 | { | |
463 | if (!(N == 1 && myDone)) { | |
464 | if (N < 1 || N > NbExt()) { Standard_OutOfRange::Raise(); } | |
465 | } | |
466 | return mySqDist[N-1]; | |
467 | } | |
468 | //============================================================================ | |
469 | ||
470 | void Extrema_ExtElC2d::Points (const Standard_Integer N, | |
471 | Extrema_POnCurv2d& P1, | |
472 | Extrema_POnCurv2d& P2) const | |
473 | { | |
474 | if (N < 1 || N > NbExt()) { Standard_OutOfRange::Raise(); } | |
475 | P1 = myPoint[N-1][0]; | |
476 | P2 = myPoint[N-1][1]; | |
477 | } | |
478 | //============================================================================ |