1 // Copyright (c) 1996-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.
16 #include <Adaptor3d_Curve.hxx>
17 #include <Bnd_Box.hxx>
19 #include <BndLib_Add3dCurve.hxx>
21 #include <Geom_BezierCurve.hxx>
22 #include <Geom_BSplineCurve.hxx>
23 #include <GeomAbs_CurveType.hxx>
24 #include <GeomAdaptor_Curve.hxx>
25 #include <gp_Circ.hxx>
26 #include <gp_Elips.hxx>
27 #include <gp_Hypr.hxx>
29 #include <gp_Parab.hxx>
31 #include <Precision.hxx>
32 #include <TColgp_Array1OfPnt.hxx>
33 #include <TColStd_Array1OfInteger.hxx>
34 #include <TColStd_Array1OfReal.hxx>
35 #include <math_MultipleVarFunction.hxx>
36 #include <math_Function.hxx>
37 #include <math_PSO.hxx>
38 #include <math_BrentMinimum.hxx>
40 static Standard_Integer NbSamples(const Adaptor3d_Curve& C,
41 const Standard_Real Umin,
42 const Standard_Real Umax);
44 static Standard_Real AdjustExtr(const Adaptor3d_Curve& C,
45 const Standard_Real UMin,
46 const Standard_Real UMax,
47 const Standard_Real Extr0,
48 const Standard_Integer CoordIndx,
49 const Standard_Real Tol,
50 const Standard_Boolean IsMin);
53 //=======================================================================
54 //function : reduceSplineBox
55 //purpose : This method intended to reduce box in case of
56 // bezier and bspline curve.
57 //=======================================================================
58 static void reduceSplineBox(const Adaptor3d_Curve& theCurve,
59 const Bnd_Box& theOrigBox,
60 Bnd_Box & theReducedBox)
62 // Guaranteed bounding box based on poles of bspline.
64 Standard_Real aPolesXMin, aPolesYMin, aPolesZMin,
65 aPolesXMax, aPolesYMax, aPolesZMax;
67 if (theCurve.GetType() == GeomAbs_BSplineCurve)
69 Handle(Geom_BSplineCurve) aC = theCurve.BSpline();
70 const TColgp_Array1OfPnt& aPoles = aC->Poles();
72 for(Standard_Integer anIdx = aPoles.Lower();
73 anIdx <= aPoles.Upper();
76 aPolesBox.Add(aPoles.Value(anIdx));
79 if (theCurve.GetType() == GeomAbs_BezierCurve)
81 Handle(Geom_BezierCurve) aC = theCurve.Bezier();
82 const TColgp_Array1OfPnt& aPoles = aC->Poles();
84 for(Standard_Integer anIdx = aPoles.Lower();
85 anIdx <= aPoles.Upper();
88 aPolesBox.Add(aPoles.Value(anIdx));
92 aPolesBox.Get(aPolesXMin, aPolesYMin, aPolesZMin,
93 aPolesXMax, aPolesYMax, aPolesZMax);
95 Standard_Real x, y, z, X, Y, Z;
96 theOrigBox.Get(x, y, z, X, Y, Z);
114 theReducedBox.Update(x, y, z, X, Y, Z);
117 //=======================================================================
120 //=======================================================================
121 void BndLib_Add3dCurve::Add( const Adaptor3d_Curve& C,
122 const Standard_Real Tol,
125 BndLib_Add3dCurve::Add(C,
132 static Standard_Real FillBox(Bnd_Box& B, const Adaptor3d_Curve& C,
133 const Standard_Real first, const Standard_Real last,
134 const Standard_Integer N)
137 C.D0(first,P1); B.Add(P1);
138 Standard_Real p = first, dp = last-first, tol= 0.;
139 if(Abs(dp) > Precision::PConfusion()){
142 for(i = 1; i <= N; i++){
143 p += dp; C.D0(p,P2); B.Add(P2);
144 p += dp; C.D0(p,P3); B.Add(P3);
145 gp_Pnt Pc((P1.XYZ()+P3.XYZ())/2.0);
146 tol = Max(tol,Pc.Distance(P2));
150 C.D0(first,P1); B.Add(P1);
151 C.D0(last,P3); B.Add(P3);
157 //=======================================================================
160 //=======================================================================
162 void BndLib_Add3dCurve::Add( const Adaptor3d_Curve& C,
163 const Standard_Real U1,
164 const Standard_Real U2,
165 const Standard_Real Tol,
168 static Standard_Real weakness = 1.5; //OCC566(apo)
169 Standard_Real tol = 0.0;
170 switch (C.GetType()) {
174 BndLib::Add(C.Line(),U1,U2,Tol,B);
179 BndLib::Add(C.Circle(),U1,U2,Tol,B);
182 case GeomAbs_Ellipse:
184 BndLib::Add(C.Ellipse(),U1,U2,Tol,B);
187 case GeomAbs_Hyperbola:
189 BndLib::Add(C.Hyperbola(),U1,U2,Tol,B);
192 case GeomAbs_Parabola:
194 BndLib::Add(C.Parabola(),U1,U2,Tol,B);
197 case GeomAbs_BezierCurve:
199 Handle(Geom_BezierCurve) Bz = C.Bezier();
200 Standard_Integer N = Bz->Degree();
201 GeomAdaptor_Curve GACurve(Bz);
203 tol = FillBox(B1,GACurve,U1,U2,N);
204 B1.Enlarge(weakness*tol);
205 reduceSplineBox(C, B1, B);
209 case GeomAbs_BSplineCurve:
211 Handle(Geom_BSplineCurve) Bs = C.BSpline();
212 if(Abs(Bs->FirstParameter() - U1) > Precision::Parametric(Tol)||
213 Abs(Bs->LastParameter() - U2) > Precision::Parametric(Tol)) {
215 Handle(Geom_Geometry) G = Bs->Copy();
216 Handle(Geom_BSplineCurve) Bsaux (Handle(Geom_BSplineCurve)::DownCast (G));
217 Standard_Real u1 = U1, u2 = U2;
218 //// modified by jgv, 24.10.01 for BUC61031 ////
219 if (Bsaux->IsPeriodic())
220 ElCLib::AdjustPeriodic( Bsaux->FirstParameter(), Bsaux->LastParameter(), Precision::PConfusion(), u1, u2 );
222 ////////////////////////////////////////////////
223 // modified by NIZHNY-EAP Fri Dec 3 14:29:14 1999 ___BEGIN___
224 // To avoid exeption in Segment
225 if(Bsaux->FirstParameter() > U1) u1 = Bsaux->FirstParameter();
226 if(Bsaux->LastParameter() < U2 ) u2 = Bsaux->LastParameter();
227 // modified by NIZHNY-EAP Fri Dec 3 14:29:18 1999 ___END___
229 Standard_Real aSegmentTol = Precision::PConfusion();
230 if (Abs(u2 - u1) < aSegmentTol)
231 aSegmentTol = Abs(u2 - u1) * 0.01;
232 Bsaux->Segment(u1, u2, aSegmentTol);
237 Standard_Integer k, k1 = Bs->FirstUKnotIndex(), k2 = Bs->LastUKnotIndex(),
238 N = Bs->Degree(), NbKnots = Bs->NbKnots();
239 TColStd_Array1OfReal Knots(1,NbKnots);
241 GeomAdaptor_Curve GACurve(Bs);
242 Standard_Real first = Knots(k1), last;
243 for(k = k1 + 1; k <= k2; k++){
245 tol = Max(FillBox(B1,GACurve,first,last,N), tol);
250 B1.Enlarge(weakness*tol);
251 reduceSplineBox(C, B1, B);
260 static Standard_Integer N = 33;
261 tol = FillBox(B1,C,U1,U2,N);
262 B1.Enlarge(weakness*tol);
263 Standard_Real x, y, z, X, Y, Z;
264 B1.Get(x, y, z, X, Y, Z);
265 B.Update(x, y, z, X, Y, Z);
271 //=======================================================================
272 //function : AddOptimal
274 //=======================================================================
276 void BndLib_Add3dCurve::AddOptimal( const Adaptor3d_Curve& C,
277 const Standard_Real Tol,
280 BndLib_Add3dCurve::AddOptimal(C,
286 //=======================================================================
287 //function : AddOptimal
289 //=======================================================================
291 void BndLib_Add3dCurve::AddOptimal( const Adaptor3d_Curve& C,
292 const Standard_Real U1,
293 const Standard_Real U2,
294 const Standard_Real Tol,
297 switch (C.GetType()) {
301 BndLib::Add(C.Line(),U1,U2,Tol,B);
306 BndLib::Add(C.Circle(),U1,U2,Tol,B);
309 case GeomAbs_Ellipse:
311 BndLib::Add(C.Ellipse(),U1,U2,Tol,B);
314 case GeomAbs_Hyperbola:
316 BndLib::Add(C.Hyperbola(),U1,U2,Tol,B);
319 case GeomAbs_Parabola:
321 BndLib::Add(C.Parabola(),U1,U2,Tol,B);
326 AddGenCurv(C, U1, U2, Tol, B);
331 //=======================================================================
332 //function : AddGenCurv
334 //=======================================================================
335 void BndLib_Add3dCurve::AddGenCurv(const Adaptor3d_Curve& C,
336 const Standard_Real UMin,
337 const Standard_Real UMax,
338 const Standard_Real Tol,
341 Standard_Integer Nu = NbSamples(C, UMin, UMax);
343 Standard_Real CoordMin[3] = {RealLast(), RealLast(), RealLast()};
344 Standard_Real CoordMax[3] = {-RealLast(), -RealLast(), -RealLast()};
345 Standard_Real DeflMax[3] = {-RealLast(), -RealLast(), -RealLast()};
348 Standard_Integer i, k;
349 Standard_Real du = (UMax-UMin)/(Nu-1), du2 = du / 2.;
350 NCollection_Array1<gp_XYZ> aPnts(1, Nu);
352 for (i = 1, u = UMin; i <= Nu; i++, u += du)
357 for(k = 0; k < 3; ++k)
359 if(CoordMin[k] > P.Coord(k+1))
361 CoordMin[k] = P.Coord(k+1);
363 if(CoordMax[k] < P.Coord(k+1))
365 CoordMax[k] = P.Coord(k+1);
371 gp_XYZ aPm = 0.5 * (aPnts(i-1) + aPnts(i));
373 gp_XYZ aD = (P.XYZ() - aPm);
374 for(k = 0; k < 3; ++k)
376 if(CoordMin[k] > P.Coord(k+1))
378 CoordMin[k] = P.Coord(k+1);
380 if(CoordMax[k] < P.Coord(k+1))
382 CoordMax[k] = P.Coord(k+1);
384 Standard_Real d = Abs(aD.Coord(k+1));
394 Standard_Real eps = Max(Tol, Precision::Confusion());
395 for(k = 0; k < 3; ++k)
397 Standard_Real d = DeflMax[k];
402 Standard_Real CMin = CoordMin[k];
403 Standard_Real CMax = CoordMax[k];
404 for(i = 1; i <= Nu; ++i)
406 if(aPnts(i).Coord(k+1) - CMin < d)
408 Standard_Real umin, umax;
409 umin = UMin + Max(0, i-2) * du;
410 umax = UMin + Min(Nu-1, i) * du;
411 Standard_Real cmin = AdjustExtr(C, umin, umax,
412 CMin, k + 1, eps, Standard_True);
418 else if(CMax - aPnts(i).Coord(k+1) < d)
420 Standard_Real umin, umax;
421 umin = UMin + Max(0, i-2) * du;
422 umax = UMin + Min(Nu-1, i) * du;
423 Standard_Real cmax = AdjustExtr(C, umin, umax,
424 CMax, k + 1, eps, Standard_False);
435 B.Add(gp_Pnt(CoordMin[0], CoordMin[1], CoordMin[2]));
436 B.Add(gp_Pnt(CoordMax[0], CoordMax[1], CoordMax[2]));
440 class CurvMaxMinCoordMVar : public math_MultipleVarFunction
443 CurvMaxMinCoordMVar(const Adaptor3d_Curve& theCurve,
444 const Standard_Real UMin,
445 const Standard_Real UMax,
446 const Standard_Integer CoordIndx,
447 const Standard_Real Sign)
451 myCoordIndx(CoordIndx),
456 Standard_Boolean Value (const math_Vector& X,
459 if (!CheckInputData(X(1)))
461 return Standard_False;
463 gp_Pnt aP = myCurve.Value(X(1));
465 F = mySign * aP.Coord(myCoordIndx);
467 return Standard_True;
472 Standard_Integer NbVariables() const
478 CurvMaxMinCoordMVar & operator = (const CurvMaxMinCoordMVar & theOther);
480 Standard_Boolean CheckInputData(Standard_Real theParam)
482 if (theParam < myUMin ||
484 return Standard_False;
485 return Standard_True;
488 const Adaptor3d_Curve& myCurve;
489 Standard_Real myUMin;
490 Standard_Real myUMax;
491 Standard_Integer myCoordIndx;
492 Standard_Real mySign;
495 class CurvMaxMinCoord : public math_Function
498 CurvMaxMinCoord(const Adaptor3d_Curve& theCurve,
499 const Standard_Real UMin,
500 const Standard_Real UMax,
501 const Standard_Integer CoordIndx,
502 const Standard_Real Sign)
506 myCoordIndx(CoordIndx),
511 Standard_Boolean Value (const Standard_Real X,
514 if (!CheckInputData(X))
516 return Standard_False;
518 gp_Pnt aP = myCurve.Value(X);
520 F = mySign * aP.Coord(myCoordIndx);
522 return Standard_True;
526 CurvMaxMinCoord & operator = (const CurvMaxMinCoord & theOther);
528 Standard_Boolean CheckInputData(Standard_Real theParam)
530 if (theParam < myUMin ||
532 return Standard_False;
533 return Standard_True;
536 const Adaptor3d_Curve& myCurve;
537 Standard_Real myUMin;
538 Standard_Real myUMax;
539 Standard_Integer myCoordIndx;
540 Standard_Real mySign;
543 //=======================================================================
544 //function : AdjustExtr
546 //=======================================================================
548 Standard_Real AdjustExtr(const Adaptor3d_Curve& C,
549 const Standard_Real UMin,
550 const Standard_Real UMax,
551 const Standard_Real Extr0,
552 const Standard_Integer CoordIndx,
553 const Standard_Real Tol,
554 const Standard_Boolean IsMin)
556 Standard_Real aSign = IsMin ? 1.:-1.;
557 Standard_Real extr = aSign * Extr0;
559 Standard_Real uTol = Max(C.Resolution(Tol), Precision::PConfusion());
560 Standard_Real Du = (C.LastParameter() - C.FirstParameter());
562 Standard_Real reltol = uTol / Max(Abs(UMin), Abs(UMax));
563 if(UMax - UMin < 0.01 * Du)
566 math_BrentMinimum anOptLoc(reltol, 100, uTol);
567 CurvMaxMinCoord aFunc(C, UMin, UMax, CoordIndx, aSign);
568 anOptLoc.Perform(aFunc, UMin, (UMin+UMax)/2., UMax);
569 if(anOptLoc.IsDone())
571 extr = anOptLoc.Minimum();
576 Standard_Integer aNbParticles = Max(8, RealToInt(32 * (UMax - UMin) / Du));
577 Standard_Real maxstep = (UMax - UMin) / (aNbParticles + 1);
579 math_Vector aLowBorder(1,1);
580 math_Vector aUppBorder(1,1);
581 math_Vector aSteps(1,1);
582 aLowBorder(1) = UMin;
583 aUppBorder(1) = UMax;
584 aSteps(1) = Min(0.1 * Du, maxstep);
586 CurvMaxMinCoordMVar aFunc(C, UMin, UMax, CoordIndx, aSign);
587 math_PSO aFinder(&aFunc, aLowBorder, aUppBorder, aSteps, aNbParticles);
588 aFinder.Perform(aSteps, extr, aT);
590 math_BrentMinimum anOptLoc(reltol, 100, uTol);
591 CurvMaxMinCoord aFunc1(C, UMin, UMax, CoordIndx, aSign);
592 anOptLoc.Perform(aFunc1, Max(aT(1) - aSteps(1), UMin), aT(1), Min(aT(1) + aSteps(1), UMax));
594 if(anOptLoc.IsDone())
596 extr = anOptLoc.Minimum();
603 //=======================================================================
604 //function : NbSamples
606 //=======================================================================
608 Standard_Integer NbSamples(const Adaptor3d_Curve& C,
609 const Standard_Real Umin,
610 const Standard_Real Umax)
613 GeomAbs_CurveType Type = C.GetType();
615 case GeomAbs_BezierCurve:
618 //By default parametric range of Bezier curv is [0, 1]
619 Standard_Real du = Umax - Umin;
622 N = RealToInt(du*N) + 1;
627 case GeomAbs_BSplineCurve:
629 const Handle(Geom_BSplineCurve)& BC = C.BSpline();
630 N = 2 * (BC->Degree() + 1)*(BC->NbKnots() -1);
631 Standard_Real umin = BC->FirstParameter(),
632 umax = BC->LastParameter();
633 Standard_Real du = (Umax - Umin) / (umax - umin);
636 N = RealToInt(du*N) + 1;