1 // Created on: 1992-10-19
2 // Created by: Laurent PAINNOT
3 // Copyright (c) 1992-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
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
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.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
17 // Modified by cma, Fri Dec 10 18:11:56 1993
19 #include Extrema_EPC_hxx
21 #include TheSequenceOfPOnC_hxx
23 #include TheVector_hxx
24 #include TheExtPElC_hxx
25 #include <StdFail_NotDone.hxx>
26 #include <Standard_Failure.hxx>
27 #include <GeomAbs_CurveType.hxx>
28 #include <Precision.hxx>
30 #include <TColStd_Array1OfReal.hxx>
31 #include <TColStd_HArray1OfReal.hxx>
32 #include <NCollection_Array1.hxx>
35 //=======================================================================
38 //=======================================================================
39 void Extrema_GExtPC::Perform(const ThePoint& P)
44 Standard_Integer i, NbExt, n;
47 Standard_Real t3d = Precision::Confusion();
48 if (Precision::IsInfinite(myuinf)) mydist1 = RealLast();
50 Pf = TheCurveTool::Value(*((TheCurve*)myC), myuinf);
51 mydist1 = P.SquareDistance(Pf);
54 if (Precision::IsInfinite(myusup)) mydist2 = RealLast();
56 Pl = TheCurveTool::Value(*((TheCurve*)myC), myusup);
57 mydist2 = P.SquareDistance(Pl);
60 TheCurve & aCurve = *((TheCurve*)myC);
65 myExtPElC.Perform(P, TheCurveTool::Circle(aCurve), t3d, myuinf, myusup);
70 myExtPElC.Perform(P, TheCurveTool::Ellipse(aCurve), t3d, myuinf, myusup);
73 case GeomAbs_Parabola:
75 myExtPElC.Perform(P, TheCurveTool::Parabola(aCurve), t3d,myuinf,myusup);
78 case GeomAbs_Hyperbola:
80 myExtPElC.Perform(P,TheCurveTool::Hyperbola(aCurve),t3d, myuinf, myusup);
85 myExtPElC.Perform(P, TheCurveTool::Line(aCurve), t3d, myuinf, myusup);
88 case GeomAbs_BezierCurve:
92 mysample = (TheCurveTool::Bezier(aCurve))->NbPoles() * 2;
93 myExtPC.Initialize(aCurve);
97 case GeomAbs_BSplineCurve:
99 const Standard_Integer
100 aFirstIdx = TheCurveTool::BSpline(aCurve)->FirstUKnotIndex(),
101 aLastIdx = TheCurveTool::BSpline(aCurve)->LastUKnotIndex();
102 // const reference can not be used due to implementation of BRep_Adaptor.
103 TColStd_Array1OfReal aKnots(aFirstIdx, aLastIdx);
104 TheCurveTool::BSpline(aCurve)->Knots(aKnots);
106 // Workaround to work with:
107 // blend, where knots may be moved from parameter space.
108 Standard_Real aPeriodJump = 0.0;
109 // Avoid problem with too close knots.
110 const Standard_Real aTolCoeff = (myusup - myuinf) * Precision::PConfusion();
111 if (TheCurveTool::IsPeriodic(aCurve))
113 Standard_Integer aPeriodShift =
114 Standard_Integer ((myuinf - aKnots(aFirstIdx)) / TheCurveTool::Period(aCurve));
116 if (myuinf < aKnots(aFirstIdx) - aTolCoeff)
119 aPeriodJump = TheCurveTool::Period(aCurve) * aPeriodShift;
122 Standard_Integer anIdx;
124 // Find first and last used knot.
125 Standard_Integer aFirstUsedKnot = aFirstIdx,
126 aLastUsedKnot = aLastIdx;
127 for(anIdx = aFirstIdx; anIdx <= aLastIdx; anIdx++)
129 Standard_Real aKnot = aKnots(anIdx) + aPeriodJump;
130 if (myuinf >= aKnot - aTolCoeff)
131 aFirstUsedKnot = anIdx;
136 for(anIdx = aLastIdx; anIdx >= aFirstIdx; anIdx--)
138 Standard_Real aKnot = aKnots(anIdx) + aPeriodJump;
139 if (myusup <= aKnot + aTolCoeff)
140 aLastUsedKnot = anIdx;
145 if (aFirstUsedKnot == aLastUsedKnot)
148 // Some bounds lies out of curve param space.
149 // In this case build one interval with [myuinf, myusup].
150 // Parameters of these indexes will be redefined.
151 aFirstUsedKnot = aFirstIdx;
152 aLastUsedKnot = aFirstIdx + 1;
155 mysample = (TheCurveTool::BSpline(aCurve))->Degree() + 1;
159 //BSpline of first degree, direct seaching extrema for each knot interval
161 Standard_Real tmin = 0., distmin = RealLast();
162 Standard_Real aMin1 = 0., aMin2 = 0.;
163 myExtPC.Initialize(aCurve);
164 for (anIdx = aFirstUsedKnot; anIdx < aLastUsedKnot; anIdx++)
166 Standard_Real aF = aKnots(anIdx) + aPeriodJump,
167 aL = aKnots(anIdx + 1) + aPeriodJump;
169 if (anIdx == aFirstUsedKnot)
174 if (anIdx == aLastUsedKnot - 1)
181 TheCurveTool::D0(aCurve, aF, aP1);
182 TheCurveTool::D0(aCurve, aL, aP2);
183 TheVector aBase1(P, aP1), aBase2(P, aP2);
184 TheVector aV(aP2, aP1);
185 Standard_Real aVal1 = aV.Dot(aBase1); // Derivative of (C(u) - P)^2
186 Standard_Real aVal2 = aV.Dot(aBase2); // Derivative of (C(u) - P)^2
187 if (anIdx == aFirstUsedKnot)
189 aMin1 = P.SquareDistance(aP1);
201 aMin2 = P.SquareDistance(aP2);
202 Standard_Real aMinSqDist = Min(aMin1, aMin2);
203 Standard_Real aMinDer = Min(Abs(aVal1), Abs(aVal2));
204 if (!(Precision::IsInfinite(aVal1) || Precision::IsInfinite(aVal2)))
206 // Derivatives have opposite signs - min or max inside of interval (sufficient condition).
207 if (aVal1 * aVal2 <= 0.0 ||
208 aMinSqDist < 100. * Precision::SquareConfusion() ||
209 2.*aMinDer < Precision::Confusion())
217 if (!Precision::IsInfinite(distmin))
219 Standard_Boolean isToAdd = Standard_True;
220 NbExt = mypoint.Length();
221 for (i = 1; i <= NbExt && isToAdd; i++)
223 Standard_Real t = mypoint.Value(i).Parameter();
224 isToAdd = (distmin < mySqDist(i)) && (Abs(t - tmin) > mytolu);
228 ThePOnC PC(tmin, aPmin);
229 mySqDist.Append(distmin);
230 myismin.Append(Standard_True);
238 // Fill sample points.
239 Standard_Integer aValIdx = 1;
240 NCollection_Array1<Standard_Real> aVal(1, (mysample)* (aLastUsedKnot - aFirstUsedKnot) + 1);
241 NCollection_Array1<Standard_Real> aParam(1, (mysample)* (aLastUsedKnot - aFirstUsedKnot) + 1);
242 for (anIdx = aFirstUsedKnot; anIdx < aLastUsedKnot; anIdx++)
244 Standard_Real aF = aKnots(anIdx) + aPeriodJump,
245 aL = aKnots(anIdx + 1) + aPeriodJump;
247 if (anIdx == aFirstUsedKnot)
249 if (anIdx == aLastUsedKnot - 1)
252 Standard_Real aStep = (aL - aF) / mysample;
253 for (Standard_Integer aPntIdx = 0; aPntIdx < mysample; aPntIdx++)
255 Standard_Real aCurrentParam = aF + aStep * aPntIdx;
256 aVal(aValIdx) = TheCurveTool::Value(aCurve, aCurrentParam).SquareDistance(P);
257 aParam(aValIdx) = aCurrentParam;
262 aVal(aValIdx) = TheCurveTool::Value(aCurve, myusup).SquareDistance(P);
263 aParam(aValIdx) = myusup;
265 myExtPC.Initialize(aCurve);
268 for (anIdx = aVal.Lower() + 1; anIdx < aVal.Upper(); anIdx++)
270 if (aVal(anIdx) <= Precision::SquareConfusion())
272 mySqDist.Append(aVal(anIdx));
273 myismin.Append(Standard_True);
274 mypoint.Append(ThePOnC(aParam(anIdx), TheCurveTool::Value(aCurve, aParam(anIdx))));
276 if ((aVal(anIdx) >= aVal(anIdx + 1) &&
277 aVal(anIdx) >= aVal(anIdx - 1)) ||
278 (aVal(anIdx) <= aVal(anIdx + 1) &&
279 aVal(anIdx) <= aVal(anIdx - 1)))
281 myintuinf = aParam(anIdx - 1);
282 myintusup = aParam(anIdx + 1);
288 // Solve on the first and last intervals.
289 if (mydist1 > Precision::SquareConfusion() && !Precision::IsPositiveInfinite(mydist1))
293 TheCurveTool::D1(aCurve, aParam.Value(aParam.Lower()), aP1, aV1);
294 TheCurveTool::D1(aCurve, aParam.Value(aParam.Lower() + 1), aP2, aV2);
295 TheVector aBase1(P, aP1), aBase2(P, aP2);
296 Standard_Real aVal1 = aV1.Dot(aBase1); // Derivative of (C(u) - P)^2
297 Standard_Real aVal2 = aV2.Dot(aBase2); // Derivative of (C(u) - P)^2
298 if (!(Precision::IsInfinite(aVal1) || Precision::IsInfinite(aVal2)))
300 // Derivatives have opposite signs - min or max inside of interval (sufficient condition).
301 // Necessary condition - when point lies on curve.
302 // Necessary condition - when derivative of point is too small.
303 if (aVal1 * aVal2 <= 0.0 ||
304 aBase1.Dot(aBase2) <= 0.0 ||
305 2.0 * Abs(aVal1) < Precision::Confusion())
307 myintuinf = aParam(aVal.Lower());
308 myintusup = aParam(aVal.Lower() + 1);
314 if (mydist2 > Precision::SquareConfusion() && !Precision::IsPositiveInfinite(mydist2))
318 TheCurveTool::D1(aCurve, aParam.Value(aParam.Upper() - 1), aP1, aV1);
319 TheCurveTool::D1(aCurve, aParam.Value(aParam.Upper()), aP2, aV2);
320 TheVector aBase1(P, aP1), aBase2(P, aP2);
321 Standard_Real aVal1 = aV1.Dot(aBase1); // Derivative of (C(u) - P)^2
322 Standard_Real aVal2 = aV2.Dot(aBase2); // Derivative of (C(u) - P)^2
324 if (!(Precision::IsInfinite(aVal1) || Precision::IsInfinite(aVal2)))
326 // Derivatives have opposite signs - min or max inside of interval (sufficient condition).
327 // Necessary condition - when point lies on curve.
328 // Necessary condition - when derivative of point is too small.
329 if (aVal1 * aVal2 <= 0.0 ||
330 aBase1.Dot(aBase2) <= 0.0 ||
331 2.0 * Abs(aVal2) < Precision::Confusion())
333 myintuinf = aParam(aVal.Upper() - 1);
334 myintusup = aParam(aVal.Upper());
340 mydone = Standard_True;
345 const Standard_Integer aMaxSample = 17;
346 Standard_Boolean IntExtIsDone = Standard_False;
347 Standard_Boolean IntIsNotValid;
348 Handle(TColStd_HArray1OfReal) theHInter;
349 n = TheCurveTool::NbIntervals(aCurve, GeomAbs_C2);
352 theHInter = new TColStd_HArray1OfReal(1, n + 1);
353 TheCurveTool::Intervals(aCurve, theHInter->ChangeArray1(), GeomAbs_C2);
357 theHInter = TheCurveTool::DeflCurvIntervals(aCurve);
358 n = theHInter->Length() - 1;
360 mysample = Max(mysample / n, aMaxSample);
361 Standard_Real maxint = 0.;
362 for (i = 1; i <= n; ++i)
364 Standard_Real dt = theHInter->Value(i + 1) - theHInter->Value(i);
370 Standard_Boolean isPeriodic = TheCurveTool::IsPeriodic(aCurve);
373 Standard_Real s1 = 0.0 ;
374 Standard_Real s2 = 0.0;
375 myExtPC.Initialize(aCurve);
376 for (i = 1; i <= n; i++)
378 myintuinf = theHInter->Value(i);
379 myintusup = theHInter->Value(i+1);
380 mysample = Max(RealToInt(aMaxSample*(myintusup - myintuinf) / maxint), 3);
382 Standard_Real anInfToCheck = myintuinf;
383 Standard_Real aSupToCheck = myintusup;
386 Standard_Real aPeriod = TheCurveTool::Period(aCurve);
387 anInfToCheck = ElCLib::InPeriod(myintuinf, myuinf, myuinf+aPeriod);
388 aSupToCheck = myintusup+(anInfToCheck-myintuinf);
390 IntIsNotValid = (myuinf > aSupToCheck) || (myusup < anInfToCheck);
392 if(IntIsNotValid) continue;
394 if (myuinf >= anInfToCheck) anInfToCheck = myuinf;
395 if (myusup <= aSupToCheck) aSupToCheck = myusup;
396 if((aSupToCheck - anInfToCheck) <= mytolu) continue;
400 TheCurveTool::D1(aCurve, myintuinf, PP, V1);
401 s1 = (TheVector(P, PP))*V1;
403 mySqDist.Append(PP.SquareDistance(P));
404 myismin.Append((s1 < 0.0));
405 mypoint.Append(ThePOnC(myintuinf, PP));
409 TheCurveTool::D1(aCurve, myintusup, PP, V1);
410 s2 = (TheVector(P, PP))*V1;
414 IntExtIsDone = IntExtIsDone || mydone;
417 mydone = IntExtIsDone;
423 if (type == GeomAbs_BSplineCurve ||
424 type == GeomAbs_OffsetCurve ||
425 type == GeomAbs_OtherCurve)
427 // Additional checking if the point is on the first or last point of the curve
428 // and does not added yet.
429 if (mydist1 < Precision::SquareConfusion() ||
430 mydist2 < Precision::SquareConfusion())
432 Standard_Boolean isFirstAdded = Standard_False;
433 Standard_Boolean isLastAdded = Standard_False;
434 Standard_Integer aNbPoints = mypoint.Length();
435 for (i = 1; i <= aNbPoints; i++)
437 U = mypoint.Value(i).Parameter();
438 if (Abs(U - myuinf) < mytolu)
439 isFirstAdded = Standard_True;
440 else if (Abs(myusup - U) < mytolu)
441 isLastAdded = Standard_True;
443 if (!isFirstAdded && mydist1 < Precision::SquareConfusion())
445 mySqDist.Prepend(mydist1);
446 myismin.Prepend(Standard_True);
447 mypoint.Prepend(ThePOnC(myuinf, Pf));
449 if (!isLastAdded && mydist2 < Precision::SquareConfusion())
451 mySqDist.Append(mydist2);
452 myismin.Append(Standard_True);
453 mypoint.Append(ThePOnC(myusup, Pl));
455 mydone = Standard_True;
460 // In analytical case
461 mydone = myExtPElC.IsDone();
464 NbExt = myExtPElC.NbExt();
465 for (i = 1; i <= NbExt; i++)
467 // Verification de la validite des parametres:
468 ThePOnC PC = myExtPElC.Point(i);
470 if (TheCurveTool::IsPeriodic(aCurve))
472 U = ElCLib::InPeriod(U, myuinf, myuinf+TheCurveTool::Period(aCurve));
474 if ((U >= myuinf-mytolu) && (U <= myusup+mytolu))
476 PC.SetValues(U, myExtPElC.Point(i).Value());
477 mySqDist.Append(myExtPElC.SquareDistance(i));
478 myismin.Append(myExtPElC.IsMin(i));
487 //=======================================================================
488 //function : Initialize
490 //=======================================================================
492 void Extrema_GExtPC::Initialize(const TheCurve& C,
493 const Standard_Real Uinf,
494 const Standard_Real Usup,
495 const Standard_Real TolF)
497 myC = (Standard_Address)&C;
498 myintuinf = myuinf = Uinf;
499 myintusup = myusup = Usup;
501 mytolu = TheCurveTool::Resolution(*((TheCurve*)myC), Precision::Confusion());
502 type = TheCurveTool::GetType(C);
503 mydone = Standard_False;
504 mydist1 = RealLast();
505 mydist2 = RealLast();
510 //=======================================================================
511 //function : IntervalPerform
513 //=======================================================================
515 void Extrema_GExtPC::IntervalPerform(const ThePoint& P)
519 myExtPC.Initialize(mysample, myintuinf, myintusup, mytolu, mytolf);
521 mydone = myExtPC.IsDone();
524 Standard_Integer NbExt = myExtPC.NbExt();
525 for (i = 1; i <= NbExt; i++)
527 // Verification de la validite des parametres pour le cas trimme:
528 ThePOnC PC = myExtPC.Point(i);
530 if (TheCurveTool::IsPeriodic(*((TheCurve*)myC)))
532 U = ElCLib::InPeriod(U, myuinf, myuinf+TheCurveTool::Period(*((TheCurve*)myC)));
534 if ((U >= myuinf - mytolu) && (U <= myusup + mytolu))
536 AddSol(U, PC.Value(),
537 myExtPC.SquareDistance(i),
545 //=======================================================================
548 //=======================================================================
550 void Extrema_GExtPC::AddSol(const Standard_Real theU, const ThePoint& theP,
551 const Standard_Real theSqDist,
552 const Standard_Boolean isMin)
554 Standard_Integer i, NbExt = mypoint.Length();
555 for (i = 1; i <= NbExt; i++)
557 Standard_Real t = mypoint.Value(i).Parameter();
558 if (Abs(t - theU) <= mytolu)
563 ThePOnC PC(theU, theP);
564 mySqDist.Append(theSqDist);
565 myismin.Append(isMin);
571 //=======================================================================
572 //function : Extrema_GExtPC
574 //=======================================================================
576 Extrema_GExtPC::Extrema_GExtPC()
579 mydone = Standard_False;
580 mydist1 = RealLast();
581 mydist2 = RealLast();
585 myintuinf = myintusup = myuinf = myusup = Precision::Infinite();
586 type = GeomAbs_OtherCurve;
589 //=======================================================================
590 //function : Extrema_GExtPC
592 //=======================================================================
594 Extrema_GExtPC::Extrema_GExtPC(const ThePoint& P,
596 const Standard_Real Uinf,
597 const Standard_Real Usup,
598 const Standard_Real TolF)
600 Initialize(C, Uinf, Usup, TolF);
604 //=======================================================================
605 //function : Extrema_GExtPC
607 //=======================================================================
609 Extrema_GExtPC::Extrema_GExtPC(const ThePoint& P,
611 const Standard_Real TolF)
613 Initialize(C, TheCurveTool::FirstParameter(C),
614 TheCurveTool::LastParameter(C), TolF);
619 //=======================================================================
622 //=======================================================================
624 Standard_Boolean Extrema_GExtPC::IsDone() const
630 //=======================================================================
633 //=======================================================================
635 Standard_Real Extrema_GExtPC::SquareDistance(const Standard_Integer N) const
637 if ((N < 1) || (N > NbExt())) throw Standard_OutOfRange();
638 return mySqDist.Value(N);
642 //=======================================================================
645 //=======================================================================
647 Standard_Integer Extrema_GExtPC::NbExt() const
649 if (!IsDone()) throw StdFail_NotDone();
650 return mySqDist.Length();
654 //=======================================================================
657 //=======================================================================
659 Standard_Boolean Extrema_GExtPC::IsMin(const Standard_Integer N) const
661 if ((N < 1) || (N > NbExt())) throw Standard_OutOfRange();
662 return myismin.Value(N);
667 //=======================================================================
670 //=======================================================================
672 const ThePOnC & Extrema_GExtPC::Point(const Standard_Integer N) const
674 if ((N < 1) || (N > NbExt())) throw Standard_OutOfRange();
675 return mypoint.Value(N);
679 //=======================================================================
680 //function : TrimmedDistances
682 //=======================================================================
684 void Extrema_GExtPC::TrimmedSquareDistances(Standard_Real& dist1,
685 Standard_Real& dist2,