1 // Created on: 1993-06-04
2 // Created by: Bruno DUMORTIER
3 // Copyright (c) 1993-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 // 20/02/97 : PMN -> Positionement local sur BSpline (PRO6902)
18 // 10/07/97 : PMN -> Pas de calcul de resolution dans Nb(Intervals) (PRO9248)
19 // 20/10/97 : JPI -> traitement des offset curves
21 #define No_Standard_RangeError
22 #define No_Standard_OutOfRange
25 #include <Adaptor2d_HCurve2d.hxx>
26 #include <BSplCLib.hxx>
27 #include <BSplCLib_Cache.hxx>
28 #include <CSLib_Offset.hxx>
29 #include <Geom2d_BezierCurve.hxx>
30 #include <Geom2d_BSplineCurve.hxx>
31 #include <Geom2d_Circle.hxx>
32 #include <Geom2d_Curve.hxx>
33 #include <Geom2d_Ellipse.hxx>
34 #include <Geom2d_Hyperbola.hxx>
35 #include <Geom2d_Line.hxx>
36 #include <Geom2d_OffsetCurve.hxx>
37 #include <Geom2d_Parabola.hxx>
38 #include <Geom2d_TrimmedCurve.hxx>
39 #include <Geom2d_UndefinedDerivative.hxx>
40 #include <Geom2d_UndefinedValue.hxx>
41 #include <Geom2dAdaptor_Curve.hxx>
42 #include <Geom2dAdaptor_HCurve.hxx>
43 #include <GeomAbs_Shape.hxx>
45 #include <gp_Circ2d.hxx>
46 #include <gp_Elips2d.hxx>
47 #include <gp_Hypr2d.hxx>
48 #include <gp_Lin2d.hxx>
49 #include <gp_Parab2d.hxx>
50 #include <gp_Pnt2d.hxx>
51 #include <gp_Vec2d.hxx>
52 #include <Precision.hxx>
53 #include <Standard_ConstructionError.hxx>
54 #include <Standard_DomainError.hxx>
55 #include <Standard_NoSuchObject.hxx>
56 #include <Standard_NotImplemented.hxx>
57 #include <Standard_NullObject.hxx>
58 #include <Standard_OutOfRange.hxx>
59 #include <TColgp_Array1OfPnt2d.hxx>
60 #include <TColStd_Array1OfInteger.hxx>
61 #include <TColStd_Array1OfReal.hxx>
62 #include <TColStd_HArray1OfInteger.hxx>
64 //#include <Geom2dConvert_BSplineCurveKnotSplitting.hxx>
65 #define myBspl Handle(Geom2d_BSplineCurve)::DownCast (myCurve)
66 #define PosTol Precision::PConfusion()/2
68 static const int maxDerivOrder = 3;
69 static const Standard_Real MinStep = 1e-7;
71 static gp_Vec2d dummyDerivative; // used as empty value for unused derivatives in AdjustDerivative
73 // Recalculate derivatives in the singular point
74 // Returns true is the direction of derivatives is changed
75 static Standard_Boolean AdjustDerivative(const Handle(Adaptor2d_HCurve2d)& theAdaptor, Standard_Integer theMaxDerivative,
76 Standard_Real theU, gp_Vec2d& theD1, gp_Vec2d& theD2 = dummyDerivative,
77 gp_Vec2d& theD3 = dummyDerivative, gp_Vec2d& theD4 = dummyDerivative);
79 //=======================================================================
80 //function : LocalContinuity
81 //purpose : Computes the Continuity of a BSplineCurve
82 // between the parameters U1 and U2
83 // The continuity is C(d-m)
85 // m = max multiplicity of the Knots between U1 and U2
86 //=======================================================================
88 GeomAbs_Shape Geom2dAdaptor_Curve::LocalContinuity(const Standard_Real U1,
89 const Standard_Real U2)
92 Standard_NoSuchObject_Raise_if(myTypeCurve!=GeomAbs_BSplineCurve," ");
93 Standard_Integer Nb = myBspl->NbKnots();
94 Standard_Integer Index1 = 0;
95 Standard_Integer Index2 = 0;
96 Standard_Real newFirst, newLast;
97 TColStd_Array1OfReal TK(1,Nb);
98 TColStd_Array1OfInteger TM(1,Nb);
100 myBspl->Multiplicities(TM);
101 BSplCLib::LocateParameter(myBspl->Degree(),TK,TM,U1,myBspl->IsPeriodic(),
102 1,Nb,Index1,newFirst);
103 BSplCLib::LocateParameter(myBspl->Degree(),TK,TM,U2,myBspl->IsPeriodic(),
104 1,Nb,Index2,newLast);
105 if ( Abs(newFirst-TK(Index1+1))<Precision::PConfusion()) {
106 if (Index1 < Nb)Index1++;
108 if ( Abs(newLast-TK(Index2))<Precision::PConfusion())
110 Standard_Integer MultMax;
111 // attention aux courbes peridiques.
112 if ( (myBspl->IsPeriodic()) && (Index1 == Nb) )
115 if ( Index2 - Index1 <= 0) {
116 MultMax = 100; // CN entre 2 Noeuds consecutifs
119 MultMax = TM(Index1+1);
120 for(Standard_Integer i = Index1+1;i<=Index2;i++) {
121 if ( TM(i)>MultMax) MultMax=TM(i);
123 MultMax = myBspl->Degree() - MultMax;
128 else if ( MultMax == 1) {
131 else if ( MultMax == 2) {
134 else if ( MultMax == 3) {
143 //=======================================================================
144 //function : Geom2dAdaptor_Curve
146 //=======================================================================
148 Geom2dAdaptor_Curve::Geom2dAdaptor_Curve()
149 : myTypeCurve(GeomAbs_OtherCurve),
155 //=======================================================================
156 //function : Geom2dAdaptor_Curve
158 //=======================================================================
160 Geom2dAdaptor_Curve::Geom2dAdaptor_Curve(const Handle(Geom2d_Curve)& theCrv)
161 : myTypeCurve(GeomAbs_OtherCurve),
168 //=======================================================================
169 //function : Geom2dAdaptor_Curve
171 //=======================================================================
173 Geom2dAdaptor_Curve::Geom2dAdaptor_Curve(const Handle(Geom2d_Curve)& theCrv,
174 const Standard_Real theUFirst,
175 const Standard_Real theULast)
176 : myTypeCurve(GeomAbs_OtherCurve),
180 Load(theCrv, theUFirst, theULast);
184 //=======================================================================
187 //=======================================================================
189 void Geom2dAdaptor_Curve::load(const Handle(Geom2d_Curve)& C,
190 const Standard_Real UFirst,
191 const Standard_Real ULast)
199 Handle(Standard_Type) TheType = C->DynamicType();
200 if ( TheType == STANDARD_TYPE(Geom2d_TrimmedCurve)) {
201 Load(Handle(Geom2d_TrimmedCurve)::DownCast (C)->BasisCurve(),
204 else if ( TheType == STANDARD_TYPE(Geom2d_Circle)) {
205 myTypeCurve = GeomAbs_Circle;
207 else if ( TheType ==STANDARD_TYPE(Geom2d_Line)) {
208 myTypeCurve = GeomAbs_Line;
210 else if ( TheType == STANDARD_TYPE(Geom2d_Ellipse)) {
211 myTypeCurve = GeomAbs_Ellipse;
213 else if ( TheType == STANDARD_TYPE(Geom2d_Parabola)) {
214 myTypeCurve = GeomAbs_Parabola;
216 else if ( TheType == STANDARD_TYPE(Geom2d_Hyperbola)) {
217 myTypeCurve = GeomAbs_Hyperbola;
219 else if ( TheType == STANDARD_TYPE(Geom2d_BezierCurve)) {
220 myTypeCurve = GeomAbs_BezierCurve;
222 else if ( TheType == STANDARD_TYPE(Geom2d_BSplineCurve)) {
223 myTypeCurve = GeomAbs_BSplineCurve;
224 // Create cache for B-spline
225 myCurveCache = new BSplCLib_Cache(myBspl->Degree(), myBspl->IsPeriodic(),
226 myBspl->KnotSequence(), myBspl->Poles(), myBspl->Weights());
228 else if ( TheType == STANDARD_TYPE(Geom2d_OffsetCurve))
230 myTypeCurve = GeomAbs_OffsetCurve;
231 // Create nested adaptor for base curve
232 Handle(Geom2d_Curve) aBase = Handle(Geom2d_OffsetCurve)::DownCast(myCurve)->BasisCurve();
233 myOffsetBaseCurveAdaptor = new Geom2dAdaptor_HCurve(aBase);
236 myTypeCurve = GeomAbs_OtherCurve;
242 // -- Global methods - Apply to the whole curve.
245 //=======================================================================
246 //function : Continuity
248 //=======================================================================
250 GeomAbs_Shape Geom2dAdaptor_Curve::Continuity() const
252 if (myTypeCurve == GeomAbs_BSplineCurve) {
253 return LocalContinuity(myFirst, myLast);
255 else if (myTypeCurve == GeomAbs_OffsetCurve){
257 Handle(Geom2d_OffsetCurve)::DownCast (myCurve)->GetBasisCurveContinuity();
259 case GeomAbs_CN: return GeomAbs_CN;
260 case GeomAbs_C3: return GeomAbs_C2;
261 case GeomAbs_C2: return GeomAbs_C1;
262 case GeomAbs_C1: return GeomAbs_C0;
263 case GeomAbs_G1: return GeomAbs_G1;
264 case GeomAbs_G2: return GeomAbs_G2;
267 Standard_NoSuchObject::Raise("Geom2dAdaptor_Curve::Continuity");
271 else if (myTypeCurve == GeomAbs_OtherCurve) {
272 Standard_NoSuchObject::Raise("Geom2dAdaptor_Curve::Continuity");
282 //=======================================================================
283 //function : NbIntervals
285 //=======================================================================
287 Standard_Integer Geom2dAdaptor_Curve::NbIntervals(const GeomAbs_Shape S) const
289 Standard_Integer myNbIntervals = 1;
290 Standard_Integer NbSplit;
291 if (myTypeCurve == GeomAbs_BSplineCurve) {
292 Standard_Integer FirstIndex = myBspl->FirstUKnotIndex();
293 Standard_Integer LastIndex = myBspl->LastUKnotIndex();
294 TColStd_Array1OfInteger Inter (1, LastIndex-FirstIndex+1);
295 if ( S > Continuity()) {
296 Standard_Integer Cont;
300 Standard_DomainError::Raise("Geom2dAdaptor_Curve::NbIntervals");
310 if ( S == GeomAbs_C1) Cont = 1;
311 else if ( S == GeomAbs_C2) Cont = 2;
312 else if ( S == GeomAbs_C3) Cont = 3;
313 else Cont = myBspl->Degree();
314 Standard_Integer Degree = myBspl->Degree();
315 Standard_Integer NbKnots = myBspl->NbKnots();
316 TColStd_Array1OfInteger Mults (1, NbKnots);
317 myBspl->Multiplicities (Mults);
319 Standard_Integer Index = FirstIndex;
320 Inter (NbSplit) = Index;
323 while (Index < LastIndex)
325 if (Degree - Mults (Index) < Cont)
327 Inter (NbSplit) = Index;
332 Inter (NbSplit) = Index;
334 Standard_Integer NbInt = NbSplit-1;
336 Standard_Integer Nb = myBspl->NbKnots();
337 Standard_Integer Index1 = 0;
338 Standard_Integer Index2 = 0;
339 Standard_Real newFirst, newLast;
340 TColStd_Array1OfReal TK(1,Nb);
341 TColStd_Array1OfInteger TM(1,Nb);
343 myBspl->Multiplicities(TM);
344 BSplCLib::LocateParameter(myBspl->Degree(),TK,TM,myFirst,
345 myBspl->IsPeriodic(),
346 1,Nb,Index1,newFirst);
347 BSplCLib::LocateParameter(myBspl->Degree(),TK,TM,myLast,
348 myBspl->IsPeriodic(),
349 1,Nb,Index2,newLast);
351 // On decale eventuellement les indices
352 // On utilise une "petite" tolerance, la resolution ne doit
353 // servir que pour les tres longue courbes....(PRO9248)
354 Standard_Real Eps = Min(Resolution(Precision::Confusion()),
355 Precision::PConfusion());
356 if ( Abs(newFirst-TK(Index1+1))< Eps) Index1++;
357 if ( newLast-TK(Index2)> Eps) Index2++;
360 for ( Standard_Integer i=1; i<=NbInt; i++)
361 if (Inter(i)>Index1 && Inter(i)<Index2) myNbIntervals++;
367 else if (myTypeCurve == GeomAbs_OffsetCurve){
368 GeomAbs_Shape BaseS=GeomAbs_C0;
372 Standard_DomainError::Raise("GeomAdaptor_Curve::NbIntervals");
374 case GeomAbs_C0: BaseS = GeomAbs_C1; break;
375 case GeomAbs_C1: BaseS = GeomAbs_C2; break;
376 case GeomAbs_C2: BaseS = GeomAbs_C3; break;
377 default: BaseS = GeomAbs_CN;
379 myNbIntervals = myOffsetBaseCurveAdaptor->NbIntervals(BaseS);
382 return myNbIntervals;
385 //=======================================================================
386 //function : Intervals
388 //=======================================================================
390 void Geom2dAdaptor_Curve::Intervals(TColStd_Array1OfReal& T,
391 const GeomAbs_Shape S ) const
393 Standard_Integer myNbIntervals = 1;
394 Standard_Integer NbSplit;
395 if (myTypeCurve == GeomAbs_BSplineCurve) {
396 Standard_Integer FirstIndex = myBspl->FirstUKnotIndex();
397 Standard_Integer LastIndex = myBspl->LastUKnotIndex();
398 TColStd_Array1OfInteger Inter (1, LastIndex-FirstIndex+1);
399 if ( S > Continuity()) {
400 Standard_Integer Cont;
404 Standard_DomainError::Raise("Geom2dAdaptor_Curve::NbIntervals");
414 if ( S == GeomAbs_C1) Cont = 1;
415 else if ( S == GeomAbs_C2) Cont = 2;
416 else if ( S == GeomAbs_C3) Cont = 3;
417 else Cont = myBspl->Degree();
418 Standard_Integer Degree = myBspl->Degree();
419 Standard_Integer NbKnots = myBspl->NbKnots();
420 TColStd_Array1OfInteger Mults (1, NbKnots);
421 myBspl->Multiplicities (Mults);
423 Standard_Integer Index = FirstIndex;
424 Inter (NbSplit) = Index;
427 while (Index < LastIndex)
429 if (Degree - Mults (Index) < Cont)
431 Inter (NbSplit) = Index;
436 Inter (NbSplit) = Index;
437 Standard_Integer NbInt = NbSplit-1;
439 Standard_Integer Nb = myBspl->NbKnots();
440 Standard_Integer Index1 = 0;
441 Standard_Integer Index2 = 0;
442 Standard_Real newFirst, newLast;
443 TColStd_Array1OfReal TK(1,Nb);
444 TColStd_Array1OfInteger TM(1,Nb);
446 myBspl->Multiplicities(TM);
447 BSplCLib::LocateParameter(myBspl->Degree(),TK,TM,myFirst,
448 myBspl->IsPeriodic(),
449 1,Nb,Index1,newFirst);
450 BSplCLib::LocateParameter(myBspl->Degree(),TK,TM,myLast,
451 myBspl->IsPeriodic(),
452 1,Nb,Index2,newLast);
455 // On decale eventuellement les indices
456 // On utilise une "petite" tolerance, la resolution ne doit
457 // servir que pour les tres longue courbes....(PRO9248)
458 Standard_Real Eps = Min(Resolution(Precision::Confusion()),
459 Precision::PConfusion());
460 if ( Abs(newFirst-TK(Index1+1))< Eps) Index1++;
461 if ( newLast-TK(Index2)> Eps) Index2++;
465 for ( Standard_Integer i=1; i<=NbInt; i++) {
466 if (Inter(i) > Index1 && Inter(i)<Index2 ) {
468 Inter(myNbIntervals) = Inter(i);
471 Inter(myNbIntervals+1) = Index2;
473 Standard_Integer ii = T.Lower() - 1;
474 for (Standard_Integer I=1;I<=myNbIntervals+1;I++) {
475 T(ii + I) = TK(Inter(I));
482 else if (myTypeCurve == GeomAbs_OffsetCurve){
483 GeomAbs_Shape BaseS=GeomAbs_C0;
487 Standard_DomainError::Raise("GeomAdaptor_Curve::NbIntervals");
489 case GeomAbs_C0: BaseS = GeomAbs_C1; break;
490 case GeomAbs_C1: BaseS = GeomAbs_C2; break;
491 case GeomAbs_C2: BaseS = GeomAbs_C3; break;
492 default: BaseS = GeomAbs_CN;
494 myNbIntervals = myOffsetBaseCurveAdaptor->NbIntervals(BaseS);
495 myOffsetBaseCurveAdaptor->Intervals(T, BaseS);
498 T( T.Lower() ) = myFirst;
499 T( T.Lower() + myNbIntervals ) = myLast;
502 //=======================================================================
505 //=======================================================================
507 Handle(Adaptor2d_HCurve2d) Geom2dAdaptor_Curve::Trim
508 (const Standard_Real First,
509 const Standard_Real Last,
510 // const Standard_Real Tol) const
511 const Standard_Real ) const
513 Handle(Geom2dAdaptor_HCurve) HE = new Geom2dAdaptor_HCurve(myCurve,First,Last);
518 //=======================================================================
519 //function : IsClosed
521 //=======================================================================
523 Standard_Boolean Geom2dAdaptor_Curve::IsClosed() const
525 if (!Precision::IsPositiveInfinite(myLast) &&
526 !Precision::IsNegativeInfinite(myFirst)) {
527 gp_Pnt2d Pd = Value(myFirst);
528 gp_Pnt2d Pf = Value(myLast);
529 return ( Pd.Distance(Pf) <= Precision::Confusion());
532 return Standard_False;
535 //=======================================================================
536 //function : IsPeriodic
538 //=======================================================================
540 Standard_Boolean Geom2dAdaptor_Curve::IsPeriodic() const
542 if (myCurve->IsPeriodic())
545 return Standard_False;
548 //=======================================================================
551 //=======================================================================
553 Standard_Real Geom2dAdaptor_Curve::Period() const
555 return myCurve->LastParameter() - myCurve->FirstParameter();
558 //=======================================================================
559 //function : RebuildCache
561 //=======================================================================
562 void Geom2dAdaptor_Curve::RebuildCache(const Standard_Real theParameter) const
564 myCurveCache->BuildCache(theParameter, myBspl->Degree(),
565 myBspl->IsPeriodic(), myBspl->KnotSequence(),
566 myBspl->Poles(), myBspl->Weights());
569 //=======================================================================
572 //=======================================================================
574 gp_Pnt2d Geom2dAdaptor_Curve::Value(const Standard_Real U) const
576 if (myTypeCurve == GeomAbs_BSplineCurve)
577 return ValueBSpline(U);
578 else if (myTypeCurve == GeomAbs_OffsetCurve)
579 return ValueOffset(U);
581 return myCurve->Value(U);
584 //=======================================================================
585 //function : ValueBSpline
586 //purpose : Computes the point of parameter U on the B-spline curve
587 //=======================================================================
588 gp_Pnt2d Geom2dAdaptor_Curve::ValueBSpline(const Standard_Real theU) const
590 if (theU == myFirst || theU == myLast)
592 Standard_Integer Ideb = 0, Ifin = 0;
595 myBspl->LocateU(myFirst, PosTol, Ideb, Ifin);
597 if (Ideb>=Ifin) Ifin = Ideb+1;
601 myBspl->LocateU(myLast, PosTol, Ideb, Ifin);
602 if (Ifin>myBspl->NbKnots()) Ifin = myBspl->NbKnots();
603 if (Ideb>=Ifin) Ideb = Ifin-1;
605 return myBspl->LocalValue(theU, Ideb, Ifin);
607 else if (!myCurveCache.IsNull()) // use cached B-spline data
609 if (!myCurveCache->IsCacheValid(theU))
612 myCurveCache->D0(theU, aRes);
615 return myCurve->Value(theU);
618 //=======================================================================
619 //function : ValueOffset
620 //purpose : Computes the point of parameter U on the offset curve
621 //=======================================================================
622 gp_Pnt2d Geom2dAdaptor_Curve::ValueOffset(const Standard_Real theU) const
626 myOffsetBaseCurveAdaptor->D1(theU, aP, aD1);
627 Standard_Boolean isDirectionChange = Standard_False;
628 const Standard_Real aTol = gp::Resolution();
629 if(aD1.SquareMagnitude() <= aTol)
630 isDirectionChange = AdjustDerivative(myOffsetBaseCurveAdaptor, 1, theU, aD1);
632 Standard_Real anOffset = Handle(Geom2d_OffsetCurve)::DownCast(myCurve)->Offset();
633 CSLib_Offset::D0(aP, aD1, anOffset, isDirectionChange, aP);
637 //=======================================================================
640 //=======================================================================
642 void Geom2dAdaptor_Curve::D0(const Standard_Real U, gp_Pnt2d& P) const
644 if (myTypeCurve == GeomAbs_BSplineCurve)
649 else if (myTypeCurve == GeomAbs_OffsetCurve)
658 //=======================================================================
659 //function : D0BSpline
660 //purpose : Computes the point of parameter theU on the B-spline curve
661 //=======================================================================
662 void Geom2dAdaptor_Curve::D0BSpline(const Standard_Real theU, gp_Pnt2d& theP) const
664 if (theU == myFirst || theU == myLast)
666 Standard_Integer Ideb = 0, Ifin = 0;
667 if (theU == myFirst) {
668 myBspl->LocateU(myFirst, PosTol, Ideb, Ifin);
670 if (Ideb>=Ifin) Ifin = Ideb+1;
672 if (theU == myLast) {
673 myBspl->LocateU(myLast, PosTol, Ideb, Ifin);
674 if (Ifin>myBspl->NbKnots()) Ifin = myBspl->NbKnots();
675 if (Ideb>=Ifin) Ideb = Ifin-1;
677 myBspl->LocalD0(theU, Ideb, Ifin, theP);
680 else if (!myCurveCache.IsNull()) // use cached B-spline data
682 if (!myCurveCache->IsCacheValid(theU))
684 myCurveCache->D0(theU, theP);
687 myCurve->D0(theU, theP);
690 //=======================================================================
691 //function : D0Offset
692 //purpose : Computes the point of parameter theU on the offset curve
693 //=======================================================================
694 void Geom2dAdaptor_Curve::D0Offset(const Standard_Real theU, gp_Pnt2d& theP) const
696 theP = ValueOffset(theU);
699 //=======================================================================
702 //=======================================================================
704 void Geom2dAdaptor_Curve::D1(const Standard_Real U,
705 gp_Pnt2d& P, gp_Vec2d& V) const
707 if (myTypeCurve == GeomAbs_BSplineCurve)
712 else if (myTypeCurve == GeomAbs_OffsetCurve)
718 myCurve->D1(U, P, V);
721 //=======================================================================
722 //function : D1BSpline
723 //purpose : Computes the point of parameter theU on the B-spline curve and its derivative
724 //=======================================================================
725 void Geom2dAdaptor_Curve::D1BSpline(const Standard_Real theU, gp_Pnt2d& theP, gp_Vec2d& theV) const
727 if (theU == myFirst || theU == myLast)
729 Standard_Integer Ideb = 0, Ifin = 0;
730 if (theU == myFirst) {
731 myBspl->LocateU(myFirst, PosTol, Ideb, Ifin);
733 if (Ideb>=Ifin) Ifin = Ideb+1;
735 if (theU == myLast) {
736 myBspl->LocateU(myLast, PosTol, Ideb, Ifin);
737 if (Ifin>myBspl->NbKnots()) Ifin = myBspl->NbKnots();
738 if (Ideb>=Ifin) Ideb = Ifin-1;
740 myBspl->LocalD1(theU, Ideb, Ifin, theP, theV);
743 else if (!myCurveCache.IsNull()) // use cached B-spline data
745 if (!myCurveCache->IsCacheValid(theU))
747 myCurveCache->D1(theU, theP, theV);
750 myCurve->D1(theU, theP, theV);
753 //=======================================================================
754 //function : D1Offset
755 //purpose : Computes the point of parameter theU on the offset curve and its derivative
756 //=======================================================================
757 void Geom2dAdaptor_Curve::D1Offset(const Standard_Real theU, gp_Pnt2d& theP, gp_Vec2d& theV) const
759 // P(u) = p(u) + Offset * Ndir / R
760 // with R = || p' ^ Z|| and Ndir = P' ^ Z
762 // P'(u) = p'(u) + (Offset / R**2) * (DNdir/DU * R - Ndir * (DR/R))
765 myOffsetBaseCurveAdaptor->D2 (theU, theP, theV, V2);
767 Standard_Boolean IsDirectionChange = Standard_False;
768 if(theV.SquareMagnitude() <= gp::Resolution())
769 IsDirectionChange = AdjustDerivative(myOffsetBaseCurveAdaptor, 2, theU, theV, V2);
771 Standard_Real anOffset = Handle(Geom2d_OffsetCurve)::DownCast(myCurve)->Offset();
772 CSLib_Offset::D1(theP, theV, V2, anOffset, IsDirectionChange, theP, theV);
775 //=======================================================================
778 //=======================================================================
780 void Geom2dAdaptor_Curve::D2(const Standard_Real U,
781 gp_Pnt2d& P, gp_Vec2d& V1, gp_Vec2d& V2) const
783 if (myTypeCurve == GeomAbs_BSplineCurve)
785 D2BSpline(U, P, V1, V2);
788 else if (myTypeCurve == GeomAbs_OffsetCurve)
790 D2Offset(U, P, V1, V2);
794 myCurve->D2(U, P, V1, V2);
797 //=======================================================================
798 //function : D2BSpline
799 //purpose : Computes the point of parameter theU on the B-spline curve and its first and second derivatives
800 //=======================================================================
801 void Geom2dAdaptor_Curve::D2BSpline(const Standard_Real theU, gp_Pnt2d& theP,
802 gp_Vec2d& theV1, gp_Vec2d& theV2) const
804 if (theU == myFirst || theU == myLast)
806 Standard_Integer Ideb = 0, Ifin = 0;
807 if (theU == myFirst) {
808 myBspl->LocateU(myFirst, PosTol, Ideb, Ifin);
810 if (Ideb>=Ifin) Ifin = Ideb+1;
812 if (theU == myLast) {
813 myBspl->LocateU(myLast, PosTol, Ideb, Ifin);
814 if (Ifin>myBspl->NbKnots()) Ifin = myBspl->NbKnots();
815 if (Ideb>=Ifin) Ideb = Ifin-1;
817 myBspl->LocalD2(theU, Ideb, Ifin, theP, theV1, theV2);
820 else if (!myCurveCache.IsNull()) // use cached B-spline data
822 if (!myCurveCache->IsCacheValid(theU))
824 myCurveCache->D2(theU, theP, theV1, theV2);
827 myCurve->D2(theU, theP, theV1, theV2);
829 //=======================================================================
830 //function : D2Offset
831 //purpose : Computes the point of parameter theU on the offset curve and its first and second derivatives
832 //=======================================================================
833 void Geom2dAdaptor_Curve::D2Offset(const Standard_Real theU, gp_Pnt2d& theP,
834 gp_Vec2d& theV1, gp_Vec2d& theV2) const
836 // P(u) = p(u) + Offset * Ndir / R
837 // with R = || p' ^ Z|| and Ndir = P' ^ Z
839 // P'(u) = p'(u) + (Offset / R**2) * (DNdir/DU * R - Ndir * (DR/R))
841 // P"(u) = p"(u) + (Offset / R) * (D2Ndir/DU - DNdir * (2.0 * Dr/ R**2) +
842 // Ndir * ( (3.0 * Dr**2 / R**4) - (D2r / R**2)))
845 myOffsetBaseCurveAdaptor->D3 (theU, theP, theV1, theV2, V3);
847 Standard_Boolean IsDirectionChange = Standard_False;
848 if(theV1.SquareMagnitude() <= gp::Resolution())
849 IsDirectionChange = AdjustDerivative(myOffsetBaseCurveAdaptor, 3, theU, theV1, theV2, V3);
851 Standard_Real anOffset = Handle(Geom2d_OffsetCurve)::DownCast(myCurve)->Offset();
852 CSLib_Offset::D2(theP, theV1, theV2, V3, anOffset, IsDirectionChange, theP, theV1, theV2);
855 //=======================================================================
858 //=======================================================================
860 void Geom2dAdaptor_Curve::D3(const Standard_Real U,
861 gp_Pnt2d& P, gp_Vec2d& V1,
862 gp_Vec2d& V2, gp_Vec2d& V3) const
864 if (myTypeCurve == GeomAbs_BSplineCurve)
866 D3BSpline(U, P, V1, V2, V3);
869 else if (myTypeCurve == GeomAbs_OffsetCurve)
871 D3Offset(U, P, V1, V2, V3);
875 myCurve->D3(U, P, V1, V2, V3);
878 //=======================================================================
879 //function : D3BSpline
880 //purpose : Computes the point of parameter theU on the B-spline curve and its 1st - 3rd derivatives
881 //=======================================================================
882 void Geom2dAdaptor_Curve::D3BSpline(const Standard_Real theU, gp_Pnt2d& theP,
883 gp_Vec2d& theV1, gp_Vec2d& theV2, gp_Vec2d& theV3) const
885 if (theU == myFirst || theU == myLast)
887 Standard_Integer Ideb = 0, Ifin = 0;
888 if (theU == myFirst) {
889 myBspl->LocateU(myFirst, PosTol, Ideb, Ifin);
891 if (Ideb>=Ifin) Ifin = Ideb+1;
893 if (theU == myLast) {
894 myBspl->LocateU(myLast, PosTol, Ideb, Ifin);
895 if (Ifin>myBspl->NbKnots()) Ifin = myBspl->NbKnots();
896 if (Ideb>=Ifin) Ideb = Ifin-1;
898 myBspl->LocalD3(theU, Ideb, Ifin, theP, theV1, theV2, theV3);
901 else if (!myCurveCache.IsNull()) // use cached B-spline data
903 if (!myCurveCache->IsCacheValid(theU))
905 myCurveCache->D3(theU, theP, theV1, theV2, theV3);
908 myCurve->D3(theU, theP, theV1, theV2, theV3);
910 //=======================================================================
911 //function : D3Offset
912 //purpose : Computes the point of parameter theU on the offset curve and its 1st - 3rd derivatives
913 //=======================================================================
914 void Geom2dAdaptor_Curve::D3Offset(const Standard_Real theU, gp_Pnt2d& theP,
915 gp_Vec2d& theV1, gp_Vec2d& theV2, gp_Vec2d& theV3) const
917 // P(u) = p(u) + Offset * Ndir / R
918 // with R = || p' ^ Z|| and Ndir = P' ^ Z
920 // P'(u) = p'(u) + (Offset / R**2) * (DNdir/DU * R - Ndir * (DR/R))
922 // P"(u) = p"(u) + (Offset / R) * (D2Ndir/DU - DNdir * (2.0 * Dr/ R**2) +
923 // Ndir * ( (3.0 * Dr**2 / R**4) - (D2r / R**2)))
925 //P"'(u) = p"'(u) + (Offset / R) * (D3Ndir - (3.0 * Dr/R**2 ) * D2Ndir -
926 // (3.0 * D2r / R2) * DNdir) + (3.0 * Dr * Dr / R4) * DNdir -
927 // (D3r/R2) * Ndir + (6.0 * Dr * Dr / R4) * Ndir +
928 // (6.0 * Dr * D2r / R4) * Ndir - (15.0 * Dr* Dr* Dr /R6) * Ndir
930 Standard_Boolean IsDirectionChange = Standard_False;
932 myOffsetBaseCurveAdaptor->D3 (theU, theP, theV1, theV2, theV3);
933 gp_Vec2d V4 = myOffsetBaseCurveAdaptor->DN (theU, 4);
935 if(theV1.SquareMagnitude() <= gp::Resolution())
936 IsDirectionChange = AdjustDerivative(myOffsetBaseCurveAdaptor, 4, theU, theV1, theV2, theV3, V4);
938 Standard_Real anOffset = Handle(Geom2d_OffsetCurve)::DownCast(myCurve)->Offset();
939 CSLib_Offset::D3(theP, theV1, theV2, theV3, V4, anOffset, IsDirectionChange,
940 theP, theV1, theV2, theV3);
943 //=======================================================================
946 //=======================================================================
948 gp_Vec2d Geom2dAdaptor_Curve::DN(const Standard_Real U,
949 const Standard_Integer N) const
951 if (myTypeCurve == GeomAbs_BSplineCurve)
952 return DNBSpline(U, N);
953 else if (myTypeCurve == GeomAbs_OffsetCurve)
954 return DNOffset(U, N);
956 return myCurve->DN(U, N);
959 gp_Vec2d Geom2dAdaptor_Curve::DNBSpline(const Standard_Real U,
960 const Standard_Integer N) const
962 if (U==myFirst || U==myLast)
964 Standard_Integer Ideb = 0, Ifin = 0;
966 myBspl->LocateU(myFirst, PosTol, Ideb, Ifin);
968 if (Ideb>=Ifin) Ifin = Ideb+1;
971 myBspl->LocateU(myLast, PosTol, Ideb, Ifin);
972 if (Ifin>myBspl->NbKnots()) Ifin = myBspl->NbKnots();
973 if (Ideb>=Ifin) Ideb = Ifin-1;
975 return myBspl->LocalDN( U, Ideb, Ifin, N);
978 return myCurve->DN( U, N);
981 gp_Vec2d Geom2dAdaptor_Curve::DNOffset(const Standard_Real U,
982 const Standard_Integer N) const
990 D1Offset(U, aPnt, aVN);
993 D2Offset(U, aPnt, aVec, aVN);
996 D3Offset(U, aPnt, aVec, aVec, aVN);
999 aVN = myCurve->DN(U, N);
1004 //=======================================================================
1005 //function : Resolution
1007 //=======================================================================
1009 Standard_Real Geom2dAdaptor_Curve::Resolution(const Standard_Real Ruv) const {
1010 switch ( myTypeCurve) {
1013 case GeomAbs_Circle: {
1014 Standard_Real R = Handle(Geom2d_Circle)::DownCast (myCurve)->Circ2d().Radius();
1016 return 2*ASin(Ruv/(2*R));
1020 case GeomAbs_Ellipse: {
1021 return Ruv / Handle(Geom2d_Ellipse)::DownCast (myCurve)->MajorRadius();
1023 case GeomAbs_BezierCurve: {
1025 Handle(Geom2d_BezierCurve)::DownCast (myCurve)->Resolution(Ruv,res);
1028 case GeomAbs_BSplineCurve: {
1030 Handle(Geom2d_BSplineCurve)::DownCast (myCurve)->Resolution(Ruv,res);
1034 return Precision::Parametric(Ruv);
1040 // -- The following methods must be called when GetType returned
1041 // -- the corresponding type.
1044 //=======================================================================
1047 //=======================================================================
1049 gp_Lin2d Geom2dAdaptor_Curve::Line() const
1051 Standard_NoSuchObject_Raise_if(myTypeCurve != GeomAbs_Line, "");
1052 return Handle(Geom2d_Line)::DownCast (myCurve)->Lin2d();
1055 //=======================================================================
1058 //=======================================================================
1060 gp_Circ2d Geom2dAdaptor_Curve::Circle() const
1062 Standard_NoSuchObject_Raise_if(myTypeCurve != GeomAbs_Circle, "");
1063 return Handle(Geom2d_Circle)::DownCast (myCurve)->Circ2d();
1066 //=======================================================================
1067 //function : Ellipse
1069 //=======================================================================
1071 gp_Elips2d Geom2dAdaptor_Curve::Ellipse() const
1073 Standard_NoSuchObject_Raise_if(myTypeCurve != GeomAbs_Ellipse, "");
1074 return Handle(Geom2d_Ellipse)::DownCast (myCurve)->Elips2d();
1077 //=======================================================================
1078 //function : Hyperbola
1080 //=======================================================================
1082 gp_Hypr2d Geom2dAdaptor_Curve::Hyperbola() const
1084 Standard_NoSuchObject_Raise_if(myTypeCurve != GeomAbs_Hyperbola, "");
1085 return Handle(Geom2d_Hyperbola)::DownCast (myCurve)->Hypr2d();
1088 //=======================================================================
1089 //function : Parabola
1091 //=======================================================================
1093 gp_Parab2d Geom2dAdaptor_Curve::Parabola() const
1095 Standard_NoSuchObject_Raise_if(myTypeCurve != GeomAbs_Parabola, "");
1096 return Handle(Geom2d_Parabola)::DownCast (myCurve)->Parab2d();
1099 //=======================================================================
1102 //=======================================================================
1104 Standard_Integer Geom2dAdaptor_Curve::Degree() const
1106 if (myTypeCurve == GeomAbs_BezierCurve)
1107 return Handle(Geom2d_BezierCurve)::DownCast (myCurve)->Degree();
1108 else if (myTypeCurve == GeomAbs_BSplineCurve)
1109 return Handle(Geom2d_BSplineCurve)::DownCast (myCurve)->Degree();
1111 Standard_NoSuchObject::Raise();
1116 //=======================================================================
1117 //function : IsRational
1119 //=======================================================================
1121 Standard_Boolean Geom2dAdaptor_Curve::IsRational() const {
1122 switch( myTypeCurve) {
1123 case GeomAbs_BSplineCurve:
1124 return Handle(Geom2d_BSplineCurve)::DownCast (myCurve)->IsRational();
1125 case GeomAbs_BezierCurve:
1126 return Handle(Geom2d_BezierCurve)::DownCast (myCurve)->IsRational();
1128 return Standard_False;
1132 //=======================================================================
1133 //function : NbPoles
1135 //=======================================================================
1137 Standard_Integer Geom2dAdaptor_Curve::NbPoles() const
1139 if (myTypeCurve == GeomAbs_BezierCurve)
1140 return Handle(Geom2d_BezierCurve)::DownCast (myCurve)->NbPoles();
1141 else if (myTypeCurve == GeomAbs_BSplineCurve)
1142 return Handle(Geom2d_BSplineCurve)::DownCast (myCurve)->NbPoles();
1144 Standard_NoSuchObject::Raise();
1149 //=======================================================================
1150 //function : NbKnots
1152 //=======================================================================
1154 Standard_Integer Geom2dAdaptor_Curve::NbKnots() const {
1155 if ( myTypeCurve != GeomAbs_BSplineCurve)
1156 Standard_NoSuchObject::Raise("Geom2dAdaptor_Curve::NbKnots");
1157 return Handle(Geom2d_BSplineCurve)::DownCast (myCurve)->NbKnots();
1161 //=======================================================================
1164 //=======================================================================
1166 Handle(Geom2d_BezierCurve) Geom2dAdaptor_Curve::Bezier() const
1168 return Handle(Geom2d_BezierCurve)::DownCast (myCurve);
1171 //=======================================================================
1172 //function : BSpline
1174 //=======================================================================
1176 Handle(Geom2d_BSplineCurve) Geom2dAdaptor_Curve::BSpline() const
1178 return Handle(Geom2d_BSplineCurve)::DownCast (myCurve);
1181 static Standard_Integer nbPoints(const Handle(Geom2d_Curve)& theCurve)
1184 Standard_Integer nbs = 10;
1186 if(theCurve->IsKind(STANDARD_TYPE( Geom2d_Line)) )
1188 else if(theCurve->IsKind(STANDARD_TYPE( Geom2d_BezierCurve)))
1190 nbs = 3 + Handle(Geom2d_BezierCurve)::DownCast (theCurve)->NbPoles();
1192 else if(theCurve->IsKind(STANDARD_TYPE( Geom2d_BSplineCurve))) {
1193 nbs = Handle(Geom2d_BSplineCurve)::DownCast (theCurve)->NbKnots();
1194 nbs*= Handle(Geom2d_BSplineCurve)::DownCast (theCurve)->Degree();
1195 if(nbs < 2.0) nbs=2;
1197 else if (theCurve->IsKind(STANDARD_TYPE(Geom2d_OffsetCurve)))
1199 Handle(Geom2d_Curve) aCurve = Handle(Geom2d_OffsetCurve)::DownCast (theCurve)->BasisCurve();
1200 return Max(nbs, nbPoints(aCurve));
1203 else if (theCurve->IsKind(STANDARD_TYPE(Geom2d_TrimmedCurve)))
1205 Handle(Geom2d_Curve) aCurve = Handle(Geom2d_TrimmedCurve)::DownCast (theCurve)->BasisCurve();
1206 return Max(nbs, nbPoints(aCurve));
1214 Standard_Integer Geom2dAdaptor_Curve::NbSamples() const
1216 return nbPoints(myCurve);
1220 // ============= Auxiliary functions ===================
1221 Standard_Boolean AdjustDerivative(const Handle(Adaptor2d_HCurve2d)& theAdaptor, Standard_Integer theMaxDerivative,
1222 Standard_Real theU, gp_Vec2d& theD1, gp_Vec2d& theD2,
1223 gp_Vec2d& theD3, gp_Vec2d& theD4)
1225 static const Standard_Real aTol = gp::Resolution();
1227 Standard_Boolean IsDirectionChange = Standard_False;
1228 const Standard_Real anUinfium = theAdaptor->FirstParameter();
1229 const Standard_Real anUsupremum = theAdaptor->LastParameter();
1231 const Standard_Real DivisionFactor = 1.e-3;
1233 if((anUsupremum >= RealLast()) || (anUinfium <= RealFirst()))
1236 du = anUsupremum - anUinfium;
1238 const Standard_Real aDelta = Max(du * DivisionFactor, MinStep);
1240 //Derivative is approximated by Taylor-series
1241 Standard_Integer anIndex = 1; //Derivative order
1246 V = theAdaptor->DN(theU, ++anIndex);
1248 while((V.Magnitude() <= aTol) && anIndex < maxDerivOrder);
1252 if(theU-anUinfium < aDelta)
1258 theAdaptor->D0(Min(theU, u),P1);
1259 theAdaptor->D0(Max(theU, u),P2);
1261 gp_Vec2d V1(P1, P2);
1262 IsDirectionChange = V.Dot(V1) < 0.0;
1263 Standard_Real aSign = IsDirectionChange ? -1.0 : 1.0;
1266 gp_Vec2d* aDeriv[3] = {&theD2, &theD3, &theD4};
1267 for (Standard_Integer i = 1; i < theMaxDerivative; i++)
1268 *(aDeriv[i-1]) = theAdaptor->DN(theU, anIndex + i) * aSign;
1270 return IsDirectionChange;