1 // Created on: 1993-03-25
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 //Avril 1991 : constructeurs + methodes de lecture.
18 //Mai 1991 : revue des specifs + debut de realisation des classes tool =>
19 // implementation des methodes Set et calcul du point courant.
20 //Juillet 1991 : voir egalement File Geom2d_BSplineCurve_1.cxx
21 //Juin 1992 : mise a plat des valeurs nodales - amelioration des
22 // performances sur calcul du point courant
24 //RLE Aug 1993 Remove Swaps, Init methods, Remove typedefs
25 // 14-Mar-96 : xab implemented MovePointAndTangent
27 //SAMTECH Jan 2002 : add text to Raise()
29 #define No_Standard_OutOfRange
32 #include <BSplCLib.hxx>
33 #include <BSplCLib_KnotDistribution.hxx>
34 #include <BSplCLib_MultDistribution.hxx>
35 #include <Geom2d_BSplineCurve.hxx>
36 #include <Geom2d_Geometry.hxx>
37 #include <Geom2d_UndefinedDerivative.hxx>
39 #include <gp_Pnt2d.hxx>
40 #include <gp_Trsf2d.hxx>
41 #include <gp_Vec2d.hxx>
42 #include <Precision.hxx>
43 #include <Standard_ConstructionError.hxx>
44 #include <Standard_DimensionError.hxx>
45 #include <Standard_DomainError.hxx>
46 #include <Standard_NoSuchObject.hxx>
47 #include <Standard_NotImplemented.hxx>
48 #include <Standard_OutOfRange.hxx>
49 #include <Standard_RangeError.hxx>
50 #include <Standard_Type.hxx>
52 IMPLEMENT_STANDARD_RTTIEXT(Geom2d_BSplineCurve,Geom2d_BoundedCurve)
54 //=======================================================================
55 //function : CheckCurveData
56 //purpose : Internal use only
57 //=======================================================================
58 static void CheckCurveData
59 (const TColgp_Array1OfPnt2d& CPoles,
60 const TColStd_Array1OfReal& CKnots,
61 const TColStd_Array1OfInteger& CMults,
62 const Standard_Integer Degree,
63 const Standard_Boolean Periodic)
65 if (Degree < 1 || Degree > Geom2d_BSplineCurve::MaxDegree()) {
66 throw Standard_ConstructionError("BSpline curve: invalid degree");
69 if (CPoles.Length() < 2) throw Standard_ConstructionError("BSpline curve: at least 2 poles required");
70 if (CKnots.Length() != CMults.Length()) throw Standard_ConstructionError("BSpline curve: Knot and Mult array size mismatch");
72 for (Standard_Integer I = CKnots.Lower(); I < CKnots.Upper(); I++) {
73 if (CKnots (I+1) - CKnots (I) <= Epsilon (Abs(CKnots (I)))) {
74 throw Standard_ConstructionError("BSpline curve: Knots interval values too close");
78 if (CPoles.Length() != BSplCLib::NbPoles(Degree,Periodic,CMults))
79 throw Standard_ConstructionError("BSpline curve: # Poles and degree mismatch");
82 //! Check rationality of an array of weights
83 static Standard_Boolean Rational (const TColStd_Array1OfReal& theWeights)
85 for (Standard_Integer i = theWeights.Lower(); i < theWeights.Upper(); i++)
87 if (Abs (theWeights[i] - theWeights[i + 1]) > gp::Resolution())
92 return Standard_False;
95 //=======================================================================
98 //=======================================================================
100 Handle(Geom2d_Geometry) Geom2d_BSplineCurve::Copy() const
102 Handle(Geom2d_BSplineCurve) C;
104 C = new Geom2d_BSplineCurve(poles->Array1(),
110 C = new Geom2d_BSplineCurve(poles->Array1(),
117 //=======================================================================
118 //function : Geom2d_BSplineCurve
120 //=======================================================================
122 Geom2d_BSplineCurve::Geom2d_BSplineCurve
123 (const TColgp_Array1OfPnt2d& Poles,
124 const TColStd_Array1OfReal& Knots,
125 const TColStd_Array1OfInteger& Mults,
126 const Standard_Integer Degree,
127 const Standard_Boolean Periodic) :
128 rational(Standard_False),
131 maxderivinvok(Standard_False)
135 CheckCurveData(Poles,
143 poles = new TColgp_HArray1OfPnt2d(1,Poles.Length());
144 poles->ChangeArray1() = Poles;
146 knots = new TColStd_HArray1OfReal(1,Knots.Length());
147 knots->ChangeArray1() = Knots;
149 mults = new TColStd_HArray1OfInteger(1,Mults.Length());
150 mults->ChangeArray1() = Mults;
155 //=======================================================================
156 //function : Geom2d_BSplineCurve
158 //=======================================================================
160 Geom2d_BSplineCurve::Geom2d_BSplineCurve
161 (const TColgp_Array1OfPnt2d& Poles,
162 const TColStd_Array1OfReal& Weights,
163 const TColStd_Array1OfReal& Knots,
164 const TColStd_Array1OfInteger& Mults,
165 const Standard_Integer Degree,
166 const Standard_Boolean Periodic) :
167 rational(Standard_True),
170 maxderivinvok(Standard_False)
176 CheckCurveData(Poles,
182 if (Weights.Length() != Poles.Length())
183 throw Standard_ConstructionError("Geom2d_BSplineCurve: Weights and Poles array size mismatch");
186 for (i = Weights.Lower(); i <= Weights.Upper(); i++) {
187 if (Weights(i) <= gp::Resolution()) {
188 throw Standard_ConstructionError("Geom2d_BSplineCurve: Weights values too small");
192 // check really rational
193 rational = Rational(Weights);
197 poles = new TColgp_HArray1OfPnt2d(1,Poles.Length());
198 poles->ChangeArray1() = Poles;
200 weights = new TColStd_HArray1OfReal(1,Weights.Length());
201 weights->ChangeArray1() = Weights;
204 knots = new TColStd_HArray1OfReal(1,Knots.Length());
205 knots->ChangeArray1() = Knots;
207 mults = new TColStd_HArray1OfInteger(1,Mults.Length());
208 mults->ChangeArray1() = Mults;
213 //=======================================================================
214 //function : MaxDegree
216 //=======================================================================
218 Standard_Integer Geom2d_BSplineCurve::MaxDegree ()
220 return BSplCLib::MaxDegree();
223 //=======================================================================
224 //function : IncreaseDegree
226 //=======================================================================
228 void Geom2d_BSplineCurve::IncreaseDegree
229 (const Standard_Integer Degree)
231 if (Degree == deg) return;
233 if (Degree < deg || Degree > Geom2d_BSplineCurve::MaxDegree()) {
234 throw Standard_ConstructionError("BSpline curve: IncreaseDegree: bad degree value");
237 Standard_Integer FromK1 = FirstUKnotIndex ();
238 Standard_Integer ToK2 = LastUKnotIndex ();
240 Standard_Integer Step = Degree - deg;
242 Handle(TColgp_HArray1OfPnt2d) npoles = new
243 TColgp_HArray1OfPnt2d(1,poles->Length() + Step * (ToK2-FromK1));
245 Standard_Integer nbknots = BSplCLib::IncreaseDegreeCountKnots
246 (deg,Degree,periodic,mults->Array1());
248 Handle(TColStd_HArray1OfReal) nknots =
249 new TColStd_HArray1OfReal(1,nbknots);
251 Handle(TColStd_HArray1OfInteger) nmults =
252 new TColStd_HArray1OfInteger(1,nbknots);
254 Handle(TColStd_HArray1OfReal) nweights;
257 nweights = new TColStd_HArray1OfReal(1,npoles->Upper());
260 BSplCLib::IncreaseDegree (deg, Degree, periodic,
261 poles->Array1(), !nweights.IsNull() ? &weights->Array1() : BSplCLib::NoWeights(),
262 knots->Array1(), mults->Array1(),
263 npoles->ChangeArray1(), !nweights.IsNull() ? &nweights->ChangeArray1() : BSplCLib::NoWeights(),
264 nknots->ChangeArray1(), nmults->ChangeArray1());
274 //=======================================================================
275 //function : IncreaseMultiplicity
277 //=======================================================================
279 void Geom2d_BSplineCurve::IncreaseMultiplicity
280 (const Standard_Integer Index,
281 const Standard_Integer M)
283 TColStd_Array1OfReal k(1,1);
284 k(1) = knots->Value(Index);
285 TColStd_Array1OfInteger m(1,1);
286 m(1) = M - mults->Value(Index);
287 InsertKnots(k,m,Epsilon(1.),Standard_True);
290 //=======================================================================
291 //function : IncreaseMultiplicity
293 //=======================================================================
295 void Geom2d_BSplineCurve::IncreaseMultiplicity
296 (const Standard_Integer I1,
297 const Standard_Integer I2,
298 const Standard_Integer M)
300 Handle(TColStd_HArray1OfReal) tk = knots;
301 TColStd_Array1OfReal k((knots->Array1())(I1),I1,I2);
302 TColStd_Array1OfInteger m(I1,I2);
304 for (i = I1; i <= I2; i++)
305 m(i) = M - mults->Value(i);
306 InsertKnots(k,m,Epsilon(1.),Standard_True);
309 //=======================================================================
310 //function : IncrementMultiplicity
312 //=======================================================================
314 void Geom2d_BSplineCurve::IncrementMultiplicity
315 (const Standard_Integer I1,
316 const Standard_Integer I2,
317 const Standard_Integer Step)
319 Handle(TColStd_HArray1OfReal) tk = knots;
320 TColStd_Array1OfReal k((knots->Array1())(I1),I1,I2);
321 TColStd_Array1OfInteger m(I1,I2);
323 InsertKnots(k,m,Epsilon(1.),Standard_True);
326 //=======================================================================
327 //function : InsertKnot
329 //=======================================================================
331 void Geom2d_BSplineCurve::InsertKnot
332 (const Standard_Real U,
333 const Standard_Integer M,
334 const Standard_Real ParametricTolerance)
336 TColStd_Array1OfReal k(1,1);
338 TColStd_Array1OfInteger m(1,1);
340 InsertKnots(k,m,ParametricTolerance);
343 //=======================================================================
344 //function : InsertKnots
346 //=======================================================================
347 void Geom2d_BSplineCurve::InsertKnots(const TColStd_Array1OfReal& Knots,
348 const TColStd_Array1OfInteger& Mults,
349 const Standard_Real Epsilon,
350 const Standard_Boolean Add)
352 // Check and compute new sizes
353 Standard_Integer nbpoles,nbknots;
355 if (!BSplCLib::PrepareInsertKnots(deg,periodic,
356 knots->Array1(),mults->Array1(),
357 Knots,&Mults,nbpoles,nbknots,Epsilon,Add))
358 throw Standard_ConstructionError("Geom2d_BSplineCurve::InsertKnots");
360 if (nbpoles == poles->Length()) return;
362 Handle(TColgp_HArray1OfPnt2d) npoles = new TColgp_HArray1OfPnt2d(1,nbpoles);
363 Handle(TColStd_HArray1OfReal) nknots = knots;
364 Handle(TColStd_HArray1OfInteger) nmults = mults;
366 if (nbknots != knots->Length()) {
367 nknots = new TColStd_HArray1OfReal(1,nbknots);
368 nmults = new TColStd_HArray1OfInteger(1,nbknots);
371 Handle(TColStd_HArray1OfReal) nweights;
374 nweights = new TColStd_HArray1OfReal (1, nbpoles);
377 BSplCLib::InsertKnots (deg, periodic,
378 poles->Array1(), !nweights.IsNull() ? &weights->Array1() : BSplCLib::NoWeights(),
379 knots->Array1(), mults->Array1(),
381 npoles->ChangeArray1(), !nweights.IsNull() ? &nweights->ChangeArray1() : BSplCLib::NoWeights(),
382 nknots->ChangeArray1(), nmults->ChangeArray1(),
391 //=======================================================================
392 //function : RemoveKnot
394 //=======================================================================
396 Standard_Boolean Geom2d_BSplineCurve::RemoveKnot
397 (const Standard_Integer Index,
398 const Standard_Integer M,
399 const Standard_Real Tolerance)
401 if (M < 0) return Standard_True;
403 Standard_Integer I1 = FirstUKnotIndex ();
404 Standard_Integer I2 = LastUKnotIndex ();
406 if (Index < I1 || Index > I2) {
407 throw Standard_OutOfRange("BSpline curve: RemoveKnot: index out of range");
410 const TColgp_Array1OfPnt2d & oldpoles = poles->Array1();
412 Standard_Integer step = mults->Value(Index) - M;
413 if (step <= 0) return Standard_True;
415 Handle(TColgp_HArray1OfPnt2d) npoles =
416 new TColgp_HArray1OfPnt2d(1,oldpoles.Length()-step);
418 Handle(TColStd_HArray1OfReal) nknots = knots;
419 Handle(TColStd_HArray1OfInteger) nmults = mults;
422 nknots = new TColStd_HArray1OfReal(1,knots->Length()-1);
423 nmults = new TColStd_HArray1OfInteger(1,knots->Length()-1);
426 Handle(TColStd_HArray1OfReal) nweights;
429 nweights = new TColStd_HArray1OfReal(1,npoles->Length());
432 if (!BSplCLib::RemoveKnot (Index, M, deg, periodic,
433 poles->Array1(), !nweights.IsNull() ? &weights->Array1() : BSplCLib::NoWeights(),
434 knots->Array1(),mults->Array1(),
435 npoles->ChangeArray1(), !nweights.IsNull() ? &nweights->ChangeArray1() : BSplCLib::NoWeights(),
436 nknots->ChangeArray1(),nmults->ChangeArray1(),
439 return Standard_False;
449 return Standard_True;
452 //=======================================================================
453 //function : InsertPoleAfter
455 //=======================================================================
457 void Geom2d_BSplineCurve::InsertPoleAfter
458 (const Standard_Integer Index,
460 const Standard_Real Weight)
462 if (Index < 0 || Index > poles->Length()) throw Standard_OutOfRange("BSpline curve: InsertPoleAfter: Index and #pole mismatch");
464 if (Weight <= gp::Resolution()) throw Standard_ConstructionError("BSpline curve: InsertPoleAfter: Weight too small");
466 if (knotSet == GeomAbs_NonUniform || knotSet == GeomAbs_PiecewiseBezier) {
467 throw Standard_ConstructionError("BSpline curve: InsertPoleAfter: bad knotSet type");
470 const TColStd_Array1OfReal& cknots = knots->Array1();
471 Standard_Integer nbknots = cknots.Length();
473 Handle(TColStd_HArray1OfReal) nknots =
474 new TColStd_HArray1OfReal(1,nbknots+1);
476 TColStd_Array1OfReal& newknots = nknots->ChangeArray1();
479 for (i = 1; i < nbknots; i++) {
480 newknots (i) = cknots(i);
483 newknots (nbknots+1) = 2 * newknots (nbknots) - newknots(nbknots-1);
485 Handle(TColStd_HArray1OfInteger) nmults =
486 new TColStd_HArray1OfInteger(1,nbknots+1);
488 TColStd_Array1OfInteger& newmults = nmults->ChangeArray1();
489 const TColStd_Array1OfInteger& cmults = mults->Array1();
491 for (i = 2; i <= nbknots; i++) newmults (i) = 1;
492 newmults (1) = cmults(1);
493 newmults (nbknots+1) = cmults(nbknots+1);
495 const TColgp_Array1OfPnt2d& cpoles = poles->Array1();
496 Standard_Integer nbpoles = cpoles.Length();
497 Handle(TColgp_HArray1OfPnt2d) npoles =
498 new TColgp_HArray1OfPnt2d(1, nbpoles+1);
499 TColgp_Array1OfPnt2d& newpoles = npoles->ChangeArray1();
503 for (i = 1; i <= Index; i++)
504 newpoles(i) = cpoles(i);
506 newpoles(Index+1) = P;
508 for (i = Index+1; i <= nbpoles; i++)
509 newpoles(i+1) = cpoles(i);
513 Handle(TColStd_HArray1OfReal) nweights;
514 Standard_Boolean rat = IsRational() || Abs(Weight-1.) > gp::Resolution();
517 nweights = new TColStd_HArray1OfReal(1,nbpoles+1);
518 TColStd_Array1OfReal& newweights = nweights->ChangeArray1();
520 for (i = 1; i <= Index; i++)
522 newweights(i) = weights->Value(i);
526 newweights(Index+1) = Weight;
528 for (i = Index+1; i <= nbpoles; i++)
530 newweights(i+1) = weights->Value(i);
532 newweights(i+1) = 1.;
543 //=======================================================================
544 //function : InsertPoleBefore
546 //=======================================================================
548 void Geom2d_BSplineCurve::InsertPoleBefore
549 (const Standard_Integer Index,
551 const Standard_Real Weight)
553 InsertPoleAfter(Index-1,P,Weight);
556 //=======================================================================
557 //function : RemovePole
559 //=======================================================================
561 void Geom2d_BSplineCurve::RemovePole
562 (const Standard_Integer Index)
564 if (Index < 1 || Index > poles->Length()) throw Standard_OutOfRange("BSpline curve: RemovePole: Index and #pole mismatch");
566 if (poles->Length() <= 2) throw Standard_ConstructionError("BSpline curve: RemovePole: #pole is already minimum");
568 if (knotSet == GeomAbs_NonUniform || knotSet == GeomAbs_PiecewiseBezier)
569 throw Standard_ConstructionError("BSpline curve: RemovePole: bad knotSet type");
572 Handle(TColStd_HArray1OfReal) nknots =
573 new TColStd_HArray1OfReal(1,knots->Length()-1);
574 TColStd_Array1OfReal& newknots = nknots->ChangeArray1();
576 Handle(TColStd_HArray1OfInteger) nmults =
577 new TColStd_HArray1OfInteger(1,mults->Length()-1);
578 TColStd_Array1OfInteger& newmults = nmults->ChangeArray1();
580 for (i = 1; i < newknots.Length(); i++) {
581 newknots (i) = knots->Value (i);
584 newmults(1) = mults->Value(1);
585 newknots(newknots.Upper()) = knots->Value (knots->Upper());
586 newmults(newmults.Upper()) = mults->Value (mults->Upper());
589 Handle(TColgp_HArray1OfPnt2d) npoles =
590 new TColgp_HArray1OfPnt2d(1, poles->Upper()-1);
591 TColgp_Array1OfPnt2d& newpoles = npoles->ChangeArray1();
593 for (i = 1; i < Index; i++)
594 newpoles(i) = poles->Value(i);
595 for (i = Index; i < newpoles.Length(); i++)
596 newpoles(i) = poles->Value(i+1);
598 Handle(TColStd_HArray1OfReal) nweights;
600 nweights = new TColStd_HArray1OfReal(1,newpoles.Length());
601 TColStd_Array1OfReal& newweights = nweights->ChangeArray1();
602 for (i = 1; i < Index; i++)
603 newweights(i) = weights->Value(i);
604 for (i = Index; i < newweights.Length(); i++)
605 newweights(i) = weights->Value(i+1);
615 //=======================================================================
618 //=======================================================================
620 void Geom2d_BSplineCurve::Reverse ()
622 BSplCLib::Reverse(knots->ChangeArray1());
623 BSplCLib::Reverse(mults->ChangeArray1());
624 Standard_Integer last;
626 last = flatknots->Upper() - deg - 1;
628 last = poles->Upper();
629 BSplCLib::Reverse(poles->ChangeArray1(),last);
631 BSplCLib::Reverse(weights->ChangeArray1(),last);
635 //=======================================================================
636 //function : ReversedParameter
638 //=======================================================================
640 Standard_Real Geom2d_BSplineCurve::ReversedParameter( const Standard_Real U) const
642 return (FirstParameter() + LastParameter() - U);
645 //=======================================================================
648 //=======================================================================
649 void Geom2d_BSplineCurve::Segment(const Standard_Real aU1,
650 const Standard_Real aU2,
651 const Standard_Real theTolerance)
654 throw Standard_DomainError("Geom2d_BSplineCurve::Segment");
656 Standard_Real AbsUMax = Max(Abs(FirstParameter()),Abs(LastParameter()));
657 Standard_Real Eps = Max (Epsilon(AbsUMax), theTolerance);
658 Standard_Real NewU1, NewU2;
659 Standard_Real U, DU=0;
660 Standard_Integer i, k, index;
663 // Checking the input bounds aUj (j=1,2).
664 // For the case when aUj==knot(i),
665 // in order to prevent the insertion of a new knot that will be too closed
666 // to the existing knot,
667 // we assign Uj=knot(i)
668 Standard_Integer n1, n2;
669 Standard_Real U1, U2;
675 for (i=n1; i<=n2; ++i) {
677 if (Abs(U-aU1)<=Eps) {
680 else if (Abs(U-aU2)<=Eps) {
684 // Henceforward we use U1, U2 as bounds of the segment
687 TColStd_Array1OfReal Knots(1,2);
688 TColStd_Array1OfInteger Mults(1,2);
690 // define param ditance to keep (eap, Apr 18 2002, occ311)
692 Standard_Real Period = LastParameter() - FirstParameter();
694 if (DU - Period > Precision::PConfusion())
695 throw Standard_DomainError("Geom2d_BSplineCurve::Segment");
701 BSplCLib::LocateParameter(deg,knots->Array1(),mults->Array1(),
702 U1,periodic,knots->Lower(),knots->Upper(),
705 BSplCLib::LocateParameter(deg,knots->Array1(),mults->Array1(),
706 U2,periodic,knots->Lower(),knots->Upper(),
708 Knots(1) = Min( NewU1, NewU2);
709 Knots(2) = Max( NewU1, NewU2);
710 Mults(1) = Mults( 2) = deg;
711 InsertKnots(Knots, Mults, Eps);
713 if (periodic) { // set the origine at NewU1
715 BSplCLib::LocateParameter(deg,knots->Array1(),mults->Array1(),
716 U1,periodic,knots->Lower(),knots->Upper(),
718 // Eps = Epsilon(knots->Value(index+1));
719 if ( Abs(knots->Value(index+1)-U) <= Eps) {
727 // compute index1 and index2 to set the new knots and mults
728 Standard_Integer index1 = 0, index2 = 0;
729 Standard_Integer FromU1 = knots->Lower();
730 Standard_Integer ToU2 = knots->Upper();
731 BSplCLib::LocateParameter(deg,knots->Array1(),mults->Array1(),
732 NewU1,periodic,FromU1,ToU2,index1,U);
733 BSplCLib::LocateParameter(deg,knots->Array1(),mults->Array1(),
734 NewU2,periodic,FromU1,ToU2,index2,U);
735 // Eps = Epsilon(knots->Value(index2+1));
736 if ( Abs(knots->Value(index2+1)-U) <= Eps){
740 Standard_Integer nbknots = index2 - index1 + 1;
742 Handle(TColStd_HArray1OfReal)
743 nknots = new TColStd_HArray1OfReal(1,nbknots);
744 Handle(TColStd_HArray1OfInteger)
745 nmults = new TColStd_HArray1OfInteger(1,nbknots);
747 // to restore changed U1
748 if (DU > 0) {// if was periodic
754 for ( i = index1; i<= index2; i++) {
755 nknots->SetValue(k, knots->Value(i) - DU);
756 nmults->SetValue(k, mults->Value(i));
759 nmults->SetValue( 1, deg + 1);
760 nmults->SetValue(nbknots, deg + 1);
763 // compute index1 and index2 to set the new poles and weights
764 Standard_Integer pindex1
765 = BSplCLib::PoleIndex(deg,index1,periodic,mults->Array1());
766 Standard_Integer pindex2
767 = BSplCLib::PoleIndex(deg,index2,periodic,mults->Array1());
770 pindex2 = Min( pindex2+1, poles->Length());
772 Standard_Integer nbpoles = pindex2 - pindex1 + 1;
774 Handle(TColStd_HArray1OfReal)
775 nweights = new TColStd_HArray1OfReal(1,nbpoles);
776 Handle(TColgp_HArray1OfPnt2d)
777 npoles = new TColgp_HArray1OfPnt2d(1,nbpoles);
781 nweights = new TColStd_HArray1OfReal( 1, nbpoles);
782 for ( i = pindex1; i <= pindex2; i++) {
783 npoles->SetValue(k, poles->Value(i));
784 nweights->SetValue(k, weights->Value(i));
789 for ( i = pindex1; i <= pindex2; i++) {
790 npoles->SetValue(k, poles->Value(i));
804 //=======================================================================
807 //=======================================================================
809 void Geom2d_BSplineCurve::SetKnot
810 (const Standard_Integer Index,
811 const Standard_Real K)
813 if (Index < 1 || Index > knots->Length()) throw Standard_OutOfRange("BSpline curve: SetKnot: Index and #knots mismatch");
814 Standard_Real DK = Abs(Epsilon (K));
816 if (K >= knots->Value(2) - DK) throw Standard_ConstructionError("BSpline curve: SetKnot: K out of range");
818 else if (Index == knots->Length()) {
819 if (K <= knots->Value (knots->Length()-1) + DK) {
820 throw Standard_ConstructionError("BSpline curve: SetKnot: K out of range");
824 if (K <= knots->Value(Index-1) + DK ||
825 K >= knots->Value(Index+1) - DK ) {
826 throw Standard_ConstructionError("BSpline curve: SetKnot: K out of range");
829 if (K != knots->Value (Index)) {
830 knots->SetValue (Index, K);
836 //=======================================================================
837 //function : SetKnots
839 //=======================================================================
841 void Geom2d_BSplineCurve::SetKnots
842 (const TColStd_Array1OfReal& K)
844 CheckCurveData(poles->Array1(),K,mults->Array1(),deg,periodic);
845 knots->ChangeArray1() = K;
850 //=======================================================================
853 //=======================================================================
855 void Geom2d_BSplineCurve::SetKnot
856 (const Standard_Integer Index,
857 const Standard_Real K,
858 const Standard_Integer M)
860 IncreaseMultiplicity (Index, M);
864 //=======================================================================
865 //function : SetPeriodic
867 //=======================================================================
869 void Geom2d_BSplineCurve::SetPeriodic ()
871 Standard_Integer first = FirstUKnotIndex();
872 Standard_Integer last = LastUKnotIndex();
874 Handle(TColStd_HArray1OfReal) tk = knots;
875 TColStd_Array1OfReal cknots((knots->Array1())(first),first,last);
876 knots = new TColStd_HArray1OfReal(1,cknots.Length());
877 knots->ChangeArray1() = cknots;
879 Handle(TColStd_HArray1OfInteger) tm = mults;
880 TColStd_Array1OfInteger cmults((mults->Array1())(first),first,last);
881 cmults(first) = cmults(last) = Min(deg, Max( cmults(first), cmults(last)));
882 mults = new TColStd_HArray1OfInteger(1,cmults.Length());
883 mults->ChangeArray1() = cmults;
885 // compute new number of poles;
886 Standard_Integer nbp = BSplCLib::NbPoles(deg,Standard_True,cmults);
888 Handle(TColgp_HArray1OfPnt2d) tp = poles;
889 TColgp_Array1OfPnt2d cpoles((poles->Array1())(1),1,nbp);
890 poles = new TColgp_HArray1OfPnt2d(1,nbp);
891 poles->ChangeArray1() = cpoles;
894 Handle(TColStd_HArray1OfReal) tw = weights;
895 TColStd_Array1OfReal cweights((weights->Array1())(1),1,nbp);
896 weights = new TColStd_HArray1OfReal(1,nbp);
897 weights->ChangeArray1() = cweights;
900 periodic = Standard_True;
906 //=======================================================================
907 //function : SetOrigin
909 //=======================================================================
911 void Geom2d_BSplineCurve::SetOrigin(const Standard_Integer Index)
914 throw Standard_NoSuchObject("Geom2d_BSplineCurve::SetOrigin");
915 Standard_Integer i,k;
916 Standard_Integer first = FirstUKnotIndex();
917 Standard_Integer last = LastUKnotIndex();
919 if ((Index < first) || (Index > last))
920 throw Standard_DomainError("Geom2d_BSplineCurve::SetOrigin");
922 Standard_Integer nbknots = knots->Length();
923 Standard_Integer nbpoles = poles->Length();
925 Handle(TColStd_HArray1OfReal) nknots =
926 new TColStd_HArray1OfReal(1,nbknots);
927 TColStd_Array1OfReal& newknots = nknots->ChangeArray1();
929 Handle(TColStd_HArray1OfInteger) nmults =
930 new TColStd_HArray1OfInteger(1,nbknots);
931 TColStd_Array1OfInteger& newmults = nmults->ChangeArray1();
933 // set the knots and mults
934 Standard_Real period = knots->Value(last) - knots->Value(first);
936 for ( i = Index; i <= last ; i++) {
937 newknots(k) = knots->Value(i);
938 newmults(k) = mults->Value(i);
941 for ( i = first+1; i <= Index; i++) {
942 newknots(k) = knots->Value(i) + period;
943 newmults(k) = mults->Value(i);
947 Standard_Integer index = 1;
948 for (i = first+1; i <= Index; i++)
949 index += mults->Value(i);
951 // set the poles and weights
952 Handle(TColgp_HArray1OfPnt2d) npoles =
953 new TColgp_HArray1OfPnt2d(1,nbpoles);
954 Handle(TColStd_HArray1OfReal) nweights =
955 new TColStd_HArray1OfReal(1,nbpoles);
956 TColgp_Array1OfPnt2d & newpoles = npoles->ChangeArray1();
957 TColStd_Array1OfReal & newweights = nweights->ChangeArray1();
958 first = poles->Lower();
959 last = poles->Upper();
962 for ( i = index; i <= last; i++) {
963 newpoles(k) = poles->Value(i);
964 newweights(k) = weights->Value(i);
967 for ( i = first; i < index; i++) {
968 newpoles(k) = poles->Value(i);
969 newweights(k) = weights->Value(i);
975 for ( i = index; i <= last; i++) {
976 newpoles(k) = poles->Value(i);
979 for ( i = first; i < index; i++) {
980 newpoles(k) = poles->Value(i);
994 //=======================================================================
995 //function : SetNotPeriodic
997 //=======================================================================
999 void Geom2d_BSplineCurve::SetNotPeriodic ()
1002 Standard_Integer NbKnots, NbPoles;
1003 BSplCLib::PrepareUnperiodize( deg, mults->Array1(),NbKnots,NbPoles);
1005 Handle(TColgp_HArray1OfPnt2d) npoles
1006 = new TColgp_HArray1OfPnt2d(1,NbPoles);
1008 Handle(TColStd_HArray1OfReal) nknots
1009 = new TColStd_HArray1OfReal(1,NbKnots);
1011 Handle(TColStd_HArray1OfInteger) nmults
1012 = new TColStd_HArray1OfInteger(1,NbKnots);
1014 Handle(TColStd_HArray1OfReal) nweights;
1016 nweights = new TColStd_HArray1OfReal(1,NbPoles);
1019 BSplCLib::Unperiodize (deg,
1020 mults->Array1(), knots->Array1(), poles->Array1(),
1021 !nweights.IsNull() ? &weights->Array1() : BSplCLib::NoWeights(),
1022 nmults->ChangeArray1(), nknots->ChangeArray1(), npoles->ChangeArray1(),
1023 !nweights.IsNull() ? &nweights->ChangeArray1() : BSplCLib::NoWeights());
1028 periodic = Standard_False;
1034 //=======================================================================
1035 //function : SetPole
1037 //=======================================================================
1039 void Geom2d_BSplineCurve::SetPole
1040 (const Standard_Integer Index,
1043 if (Index < 1 || Index > poles->Length()) throw Standard_OutOfRange("BSpline curve: SetPole: index and #pole mismatch");
1044 poles->SetValue (Index, P);
1048 //=======================================================================
1049 //function : SetPole
1051 //=======================================================================
1053 void Geom2d_BSplineCurve::SetPole
1054 (const Standard_Integer Index,
1056 const Standard_Real W)
1062 //=======================================================================
1063 //function : SetWeight
1065 //=======================================================================
1067 void Geom2d_BSplineCurve::SetWeight
1068 (const Standard_Integer Index,
1069 const Standard_Real W)
1071 if (Index < 1 || Index > poles->Length()) throw Standard_OutOfRange("BSpline curve: SetWeight: Index and #pole mismatch");
1073 if (W <= gp::Resolution ()) throw Standard_ConstructionError("BSpline curve: SetWeight: Weight too small");
1076 Standard_Boolean rat = IsRational() || (Abs(W - 1.) > gp::Resolution());
1079 if (rat && !IsRational()) {
1080 weights = new TColStd_HArray1OfReal(1,poles->Length());
1084 weights->SetValue (Index, W);
1087 rat = Rational(weights->Array1());
1088 if (!rat) weights.Nullify();
1091 rational = !weights.IsNull();
1097 //=======================================================================
1098 //function : MovePoint
1100 //=======================================================================
1102 void Geom2d_BSplineCurve::MovePoint(const Standard_Real U,
1104 const Standard_Integer Index1,
1105 const Standard_Integer Index2,
1106 Standard_Integer& FirstModifiedPole,
1107 Standard_Integer& LastmodifiedPole)
1109 if (Index1 < 1 || Index1 > poles->Length() ||
1110 Index2 < 1 || Index2 > poles->Length() || Index1 > Index2) {
1111 throw Standard_OutOfRange("BSpline curve: MovePoint: Index and #pole mismatch");
1113 TColgp_Array1OfPnt2d npoles(1, poles->Length());
1116 gp_Vec2d Displ(P0, P);
1118 BSplCLib::MovePoint (U, Displ, Index1, Index2, deg, poles->Array1(),
1119 rational ? &weights->Array1() : BSplCLib::NoWeights(), flatknots->Array1(),
1120 FirstModifiedPole, LastmodifiedPole, npoles);
1121 if (FirstModifiedPole) {
1122 poles->ChangeArray1() = npoles;
1127 //=======================================================================
1128 //function : MovePointAndTangent
1130 //=======================================================================
1132 void Geom2d_BSplineCurve::
1133 MovePointAndTangent(const Standard_Real U,
1135 const gp_Vec2d& Tangent,
1136 const Standard_Real Tolerance,
1137 const Standard_Integer StartingCondition,
1138 const Standard_Integer EndingCondition,
1139 Standard_Integer& ErrorStatus)
1141 Standard_Integer ii ;
1144 // for the time being do not deal with periodic curves
1148 TColgp_Array1OfPnt2d new_poles(1, poles->Length());
1152 gp_Vec2d delta_derivative;
1155 gp_Vec2d delta(P0, P);
1156 for (ii = 1 ; ii <= 2 ; ii++) {
1157 delta_derivative.SetCoord(ii,
1158 Tangent.Coord(ii)- delta_derivative.Coord(ii)) ;
1161 BSplCLib::MovePointAndTangent (U,
1169 rational ? &weights->Array1() : BSplCLib::NoWeights(),
1170 flatknots->Array1(),
1174 poles->ChangeArray1() = new_poles;
1179 //=======================================================================
1180 //function : UpdateKnots
1182 //=======================================================================
1184 void Geom2d_BSplineCurve::UpdateKnots()
1187 rational = !weights.IsNull();
1189 Standard_Integer MaxKnotMult = 0;
1190 BSplCLib::KnotAnalysis (deg,
1194 knotSet, MaxKnotMult);
1196 if (knotSet == GeomAbs_Uniform && !periodic) {
1200 flatknots = new TColStd_HArray1OfReal
1201 (1, BSplCLib::KnotSequenceLength(mults->Array1(),deg,periodic));
1203 BSplCLib::KnotSequence (knots->Array1(),
1206 flatknots->ChangeArray1());
1209 if (MaxKnotMult == 0) smooth = GeomAbs_CN;
1211 switch (deg - MaxKnotMult) {
1212 case 0 : smooth = GeomAbs_C0; break;
1213 case 1 : smooth = GeomAbs_C1; break;
1214 case 2 : smooth = GeomAbs_C2; break;
1215 case 3 : smooth = GeomAbs_C3; break;
1216 default : smooth = GeomAbs_C3; break;
1221 //=======================================================================
1222 //function : Normalizes the parameters if the curve is periodic
1223 //purpose : that is compute the cache so that it is valid
1224 //=======================================================================
1226 void Geom2d_BSplineCurve::PeriodicNormalization(Standard_Real& Parameter) const
1228 Standard_Real Period ;
1231 Period = flatknots->Value(flatknots->Upper() - deg) - flatknots->Value (deg + 1) ;
1232 while (Parameter > flatknots->Value(flatknots->Upper()-deg)) {
1233 Parameter -= Period ;
1235 while (Parameter < flatknots->Value((deg + 1))) {
1236 Parameter += Period ;
1241 //=======================================================================
1242 //function : DumpJson
1244 //=======================================================================
1245 void Geom2d_BSplineCurve::DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth) const
1247 OCCT_DUMP_TRANSIENT_CLASS_BEGIN (theOStream)
1249 OCCT_DUMP_BASE_CLASS (theOStream, theDepth, Geom2d_BoundedCurve)
1251 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, rational)
1252 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, periodic)
1253 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, knotSet)
1254 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, smooth)
1255 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, deg)
1256 if (!poles.IsNull())
1257 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, poles->Size())
1259 if (!weights.IsNull())
1260 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, weights->Size())
1261 if (!flatknots.IsNull())
1262 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, flatknots->Size())
1263 if (!knots.IsNull())
1264 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, knots->Size())
1265 if (!mults.IsNull())
1266 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, mults->Size())
1268 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, maxderivinv)
1269 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, maxderivinvok)