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 //=======================================================================
84 //purpose : check rationality of an array of weights
85 //=======================================================================
87 static Standard_Boolean Rational(const TColStd_Array1OfReal& W)
89 Standard_Integer i, n = W.Length();
90 Standard_Boolean rat = Standard_False;
91 for (i = 1; i < n; i++) {
92 rat = Abs(W(i) - W(i+1)) > gp::Resolution();
98 //=======================================================================
101 //=======================================================================
103 Handle(Geom2d_Geometry) Geom2d_BSplineCurve::Copy() const
105 Handle(Geom2d_BSplineCurve) C;
107 C = new Geom2d_BSplineCurve(poles->Array1(),
113 C = new Geom2d_BSplineCurve(poles->Array1(),
120 //=======================================================================
121 //function : Geom2d_BSplineCurve
123 //=======================================================================
125 Geom2d_BSplineCurve::Geom2d_BSplineCurve
126 (const TColgp_Array1OfPnt2d& Poles,
127 const TColStd_Array1OfReal& Knots,
128 const TColStd_Array1OfInteger& Mults,
129 const Standard_Integer Degree,
130 const Standard_Boolean Periodic) :
131 rational(Standard_False),
134 maxderivinvok(Standard_False)
138 CheckCurveData(Poles,
146 poles = new TColgp_HArray1OfPnt2d(1,Poles.Length());
147 poles->ChangeArray1() = Poles;
149 knots = new TColStd_HArray1OfReal(1,Knots.Length());
150 knots->ChangeArray1() = Knots;
152 mults = new TColStd_HArray1OfInteger(1,Mults.Length());
153 mults->ChangeArray1() = Mults;
158 //=======================================================================
159 //function : Geom2d_BSplineCurve
161 //=======================================================================
163 Geom2d_BSplineCurve::Geom2d_BSplineCurve
164 (const TColgp_Array1OfPnt2d& Poles,
165 const TColStd_Array1OfReal& Weights,
166 const TColStd_Array1OfReal& Knots,
167 const TColStd_Array1OfInteger& Mults,
168 const Standard_Integer Degree,
169 const Standard_Boolean Periodic) :
170 rational(Standard_True),
173 maxderivinvok(Standard_False)
179 CheckCurveData(Poles,
185 if (Weights.Length() != Poles.Length())
186 throw Standard_ConstructionError("Geom2d_BSplineCurve :Weights and Poles array size mismatch");
189 for (i = Weights.Lower(); i <= Weights.Upper(); i++) {
190 if (Weights(i) <= gp::Resolution()) {
191 throw Standard_ConstructionError("Geom2d_BSplineCurve: Weights values too small");
195 // check really rational
196 rational = Rational(Weights);
200 poles = new TColgp_HArray1OfPnt2d(1,Poles.Length());
201 poles->ChangeArray1() = Poles;
203 weights = new TColStd_HArray1OfReal(1,Weights.Length());
204 weights->ChangeArray1() = Weights;
207 knots = new TColStd_HArray1OfReal(1,Knots.Length());
208 knots->ChangeArray1() = Knots;
210 mults = new TColStd_HArray1OfInteger(1,Mults.Length());
211 mults->ChangeArray1() = Mults;
216 //=======================================================================
217 //function : MaxDegree
219 //=======================================================================
221 Standard_Integer Geom2d_BSplineCurve::MaxDegree ()
223 return BSplCLib::MaxDegree();
226 //=======================================================================
227 //function : IncreaseDegree
229 //=======================================================================
231 void Geom2d_BSplineCurve::IncreaseDegree
232 (const Standard_Integer Degree)
234 if (Degree == deg) return;
236 if (Degree < deg || Degree > Geom2d_BSplineCurve::MaxDegree()) {
237 throw Standard_ConstructionError("BSpline curve : IncreaseDegree : bad degree value");
240 Standard_Integer FromK1 = FirstUKnotIndex ();
241 Standard_Integer ToK2 = LastUKnotIndex ();
243 Standard_Integer Step = Degree - deg;
245 Handle(TColgp_HArray1OfPnt2d) npoles = new
246 TColgp_HArray1OfPnt2d(1,poles->Length() + Step * (ToK2-FromK1));
248 Standard_Integer nbknots = BSplCLib::IncreaseDegreeCountKnots
249 (deg,Degree,periodic,mults->Array1());
251 Handle(TColStd_HArray1OfReal) nknots =
252 new TColStd_HArray1OfReal(1,nbknots);
254 Handle(TColStd_HArray1OfInteger) nmults =
255 new TColStd_HArray1OfInteger(1,nbknots);
257 Handle(TColStd_HArray1OfReal) nweights;
260 nweights = new TColStd_HArray1OfReal(1,npoles->Upper());
263 BSplCLib::IncreaseDegree (deg, Degree, periodic,
264 poles->Array1(), !nweights.IsNull() ? &weights->Array1() : BSplCLib::NoWeights(),
265 knots->Array1(), mults->Array1(),
266 npoles->ChangeArray1(), !nweights.IsNull() ? &nweights->ChangeArray1() : BSplCLib::NoWeights(),
267 nknots->ChangeArray1(), nmults->ChangeArray1());
277 //=======================================================================
278 //function : IncreaseMultiplicity
280 //=======================================================================
282 void Geom2d_BSplineCurve::IncreaseMultiplicity
283 (const Standard_Integer Index,
284 const Standard_Integer M)
286 TColStd_Array1OfReal k(1,1);
287 k(1) = knots->Value(Index);
288 TColStd_Array1OfInteger m(1,1);
289 m(1) = M - mults->Value(Index);
290 InsertKnots(k,m,Epsilon(1.),Standard_True);
293 //=======================================================================
294 //function : IncreaseMultiplicity
296 //=======================================================================
298 void Geom2d_BSplineCurve::IncreaseMultiplicity
299 (const Standard_Integer I1,
300 const Standard_Integer I2,
301 const Standard_Integer M)
303 Handle(TColStd_HArray1OfReal) tk = knots;
304 TColStd_Array1OfReal k((knots->Array1())(I1),I1,I2);
305 TColStd_Array1OfInteger m(I1,I2);
307 for (i = I1; i <= I2; i++)
308 m(i) = M - mults->Value(i);
309 InsertKnots(k,m,Epsilon(1.),Standard_True);
312 //=======================================================================
313 //function : IncrementMultiplicity
315 //=======================================================================
317 void Geom2d_BSplineCurve::IncrementMultiplicity
318 (const Standard_Integer I1,
319 const Standard_Integer I2,
320 const Standard_Integer Step)
322 Handle(TColStd_HArray1OfReal) tk = knots;
323 TColStd_Array1OfReal k((knots->Array1())(I1),I1,I2);
324 TColStd_Array1OfInteger m(I1,I2);
326 InsertKnots(k,m,Epsilon(1.),Standard_True);
329 //=======================================================================
330 //function : InsertKnot
332 //=======================================================================
334 void Geom2d_BSplineCurve::InsertKnot
335 (const Standard_Real U,
336 const Standard_Integer M,
337 const Standard_Real ParametricTolerance)
339 TColStd_Array1OfReal k(1,1);
341 TColStd_Array1OfInteger m(1,1);
343 InsertKnots(k,m,ParametricTolerance);
346 //=======================================================================
347 //function : InsertKnots
349 //=======================================================================
350 void Geom2d_BSplineCurve::InsertKnots(const TColStd_Array1OfReal& Knots,
351 const TColStd_Array1OfInteger& Mults,
352 const Standard_Real Epsilon,
353 const Standard_Boolean Add)
355 // Check and compute new sizes
356 Standard_Integer nbpoles,nbknots;
358 if (!BSplCLib::PrepareInsertKnots(deg,periodic,
359 knots->Array1(),mults->Array1(),
360 Knots,&Mults,nbpoles,nbknots,Epsilon,Add))
361 throw Standard_ConstructionError("Geom2d_BSplineCurve::InsertKnots");
363 if (nbpoles == poles->Length()) return;
365 Handle(TColgp_HArray1OfPnt2d) npoles = new TColgp_HArray1OfPnt2d(1,nbpoles);
366 Handle(TColStd_HArray1OfReal) nknots = knots;
367 Handle(TColStd_HArray1OfInteger) nmults = mults;
369 if (nbknots != knots->Length()) {
370 nknots = new TColStd_HArray1OfReal(1,nbknots);
371 nmults = new TColStd_HArray1OfInteger(1,nbknots);
374 Handle(TColStd_HArray1OfReal) nweights;
377 nweights = new TColStd_HArray1OfReal (1, nbpoles);
380 BSplCLib::InsertKnots (deg, periodic,
381 poles->Array1(), !nweights.IsNull() ? &weights->Array1() : BSplCLib::NoWeights(),
382 knots->Array1(), mults->Array1(),
384 npoles->ChangeArray1(), !nweights.IsNull() ? &nweights->ChangeArray1() : BSplCLib::NoWeights(),
385 nknots->ChangeArray1(), nmults->ChangeArray1(),
394 //=======================================================================
395 //function : RemoveKnot
397 //=======================================================================
399 Standard_Boolean Geom2d_BSplineCurve::RemoveKnot
400 (const Standard_Integer Index,
401 const Standard_Integer M,
402 const Standard_Real Tolerance)
404 if (M < 0) return Standard_True;
406 Standard_Integer I1 = FirstUKnotIndex ();
407 Standard_Integer I2 = LastUKnotIndex ();
409 if (Index < I1 || Index > I2) {
410 throw Standard_OutOfRange("BSpline curve : RemoveKnot : index out of range");
413 const TColgp_Array1OfPnt2d & oldpoles = poles->Array1();
415 Standard_Integer step = mults->Value(Index) - M;
416 if (step <= 0) return Standard_True;
418 Handle(TColgp_HArray1OfPnt2d) npoles =
419 new TColgp_HArray1OfPnt2d(1,oldpoles.Length()-step);
421 Handle(TColStd_HArray1OfReal) nknots = knots;
422 Handle(TColStd_HArray1OfInteger) nmults = mults;
425 nknots = new TColStd_HArray1OfReal(1,knots->Length()-1);
426 nmults = new TColStd_HArray1OfInteger(1,knots->Length()-1);
429 Handle(TColStd_HArray1OfReal) nweights;
432 nweights = new TColStd_HArray1OfReal(1,npoles->Length());
435 if (!BSplCLib::RemoveKnot (Index, M, deg, periodic,
436 poles->Array1(), !nweights.IsNull() ? &weights->Array1() : BSplCLib::NoWeights(),
437 knots->Array1(),mults->Array1(),
438 npoles->ChangeArray1(), !nweights.IsNull() ? &nweights->ChangeArray1() : BSplCLib::NoWeights(),
439 nknots->ChangeArray1(),nmults->ChangeArray1(),
442 return Standard_False;
452 return Standard_True;
455 //=======================================================================
456 //function : InsertPoleAfter
458 //=======================================================================
460 void Geom2d_BSplineCurve::InsertPoleAfter
461 (const Standard_Integer Index,
463 const Standard_Real Weight)
465 if (Index < 0 || Index > poles->Length()) throw Standard_OutOfRange("BSpline curve : InsertPoleAfter: Index and #pole mismatch");
467 if (Weight <= gp::Resolution()) throw Standard_ConstructionError("BSpline curve : InsertPoleAfter: Weight too small");
469 if (knotSet == GeomAbs_NonUniform || knotSet == GeomAbs_PiecewiseBezier) {
470 throw Standard_ConstructionError("BSpline curve : InsertPoleAfter : bad knotSet type");
473 const TColStd_Array1OfReal& cknots = knots->Array1();
474 Standard_Integer nbknots = cknots.Length();
476 Handle(TColStd_HArray1OfReal) nknots =
477 new TColStd_HArray1OfReal(1,nbknots+1);
479 TColStd_Array1OfReal& newknots = nknots->ChangeArray1();
482 for (i = 1; i < nbknots; i++) {
483 newknots (i) = cknots(i);
486 newknots (nbknots+1) = 2 * newknots (nbknots) - newknots(nbknots-1);
488 Handle(TColStd_HArray1OfInteger) nmults =
489 new TColStd_HArray1OfInteger(1,nbknots+1);
491 TColStd_Array1OfInteger& newmults = nmults->ChangeArray1();
492 const TColStd_Array1OfInteger& cmults = mults->Array1();
494 for (i = 2; i <= nbknots; i++) newmults (i) = 1;
495 newmults (1) = cmults(1);
496 newmults (nbknots+1) = cmults(nbknots+1);
498 const TColgp_Array1OfPnt2d& cpoles = poles->Array1();
499 Standard_Integer nbpoles = cpoles.Length();
500 Handle(TColgp_HArray1OfPnt2d) npoles =
501 new TColgp_HArray1OfPnt2d(1, nbpoles+1);
502 TColgp_Array1OfPnt2d& newpoles = npoles->ChangeArray1();
506 for (i = 1; i <= Index; i++)
507 newpoles(i) = cpoles(i);
509 newpoles(Index+1) = P;
511 for (i = Index+1; i <= nbpoles; i++)
512 newpoles(i+1) = cpoles(i);
516 Handle(TColStd_HArray1OfReal) nweights;
517 Standard_Boolean rat = IsRational() || Abs(Weight-1.) > gp::Resolution();
520 nweights = new TColStd_HArray1OfReal(1,nbpoles+1);
521 TColStd_Array1OfReal& newweights = nweights->ChangeArray1();
523 for (i = 1; i <= Index; i++)
525 newweights(i) = weights->Value(i);
529 newweights(Index+1) = Weight;
531 for (i = Index+1; i <= nbpoles; i++)
533 newweights(i+1) = weights->Value(i);
535 newweights(i+1) = 1.;
546 //=======================================================================
547 //function : InsertPoleBefore
549 //=======================================================================
551 void Geom2d_BSplineCurve::InsertPoleBefore
552 (const Standard_Integer Index,
554 const Standard_Real Weight)
556 InsertPoleAfter(Index-1,P,Weight);
559 //=======================================================================
560 //function : RemovePole
562 //=======================================================================
564 void Geom2d_BSplineCurve::RemovePole
565 (const Standard_Integer Index)
567 if (Index < 1 || Index > poles->Length()) throw Standard_OutOfRange("BSpline curve :RemovePole : Index and #pole mismatch");
569 if (poles->Length() <= 2) throw Standard_ConstructionError("BSpline curve : RemovePole : #pole is already minimum");
571 if (knotSet == GeomAbs_NonUniform || knotSet == GeomAbs_PiecewiseBezier)
572 throw Standard_ConstructionError("BSpline curve : RemovePole: bad knotSet type");
575 Handle(TColStd_HArray1OfReal) nknots =
576 new TColStd_HArray1OfReal(1,knots->Length()-1);
577 TColStd_Array1OfReal& newknots = nknots->ChangeArray1();
579 Handle(TColStd_HArray1OfInteger) nmults =
580 new TColStd_HArray1OfInteger(1,mults->Length()-1);
581 TColStd_Array1OfInteger& newmults = nmults->ChangeArray1();
583 for (i = 1; i < newknots.Length(); i++) {
584 newknots (i) = knots->Value (i);
587 newmults(1) = mults->Value(1);
588 newknots(newknots.Upper()) = knots->Value (knots->Upper());
589 newmults(newmults.Upper()) = mults->Value (mults->Upper());
592 Handle(TColgp_HArray1OfPnt2d) npoles =
593 new TColgp_HArray1OfPnt2d(1, poles->Upper()-1);
594 TColgp_Array1OfPnt2d& newpoles = npoles->ChangeArray1();
596 for (i = 1; i < Index; i++)
597 newpoles(i) = poles->Value(i);
598 for (i = Index; i < newpoles.Length(); i++)
599 newpoles(i) = poles->Value(i+1);
601 Handle(TColStd_HArray1OfReal) nweights;
603 nweights = new TColStd_HArray1OfReal(1,newpoles.Length());
604 TColStd_Array1OfReal& newweights = nweights->ChangeArray1();
605 for (i = 1; i < Index; i++)
606 newweights(i) = weights->Value(i);
607 for (i = Index; i < newweights.Length(); i++)
608 newweights(i) = weights->Value(i+1);
618 //=======================================================================
621 //=======================================================================
623 void Geom2d_BSplineCurve::Reverse ()
625 BSplCLib::Reverse(knots->ChangeArray1());
626 BSplCLib::Reverse(mults->ChangeArray1());
627 Standard_Integer last;
629 last = flatknots->Upper() - deg - 1;
631 last = poles->Upper();
632 BSplCLib::Reverse(poles->ChangeArray1(),last);
634 BSplCLib::Reverse(weights->ChangeArray1(),last);
638 //=======================================================================
639 //function : ReversedParameter
641 //=======================================================================
643 Standard_Real Geom2d_BSplineCurve::ReversedParameter( const Standard_Real U) const
645 return (FirstParameter() + LastParameter() - U);
648 //=======================================================================
651 //=======================================================================
652 void Geom2d_BSplineCurve::Segment(const Standard_Real aU1,
653 const Standard_Real aU2,
654 const Standard_Real theTolerance)
657 throw Standard_DomainError("Geom2d_BSplineCurve::Segment");
659 Standard_Real AbsUMax = Max(Abs(FirstParameter()),Abs(LastParameter()));
660 Standard_Real Eps = Max (Epsilon(AbsUMax), theTolerance);
661 Standard_Real NewU1, NewU2;
662 Standard_Real U, DU=0;
663 Standard_Integer i, k, index;
666 // Checking the input bounds aUj (j=1,2).
667 // For the case when aUj==knot(i),
668 // in order to prevent the insertion of a new knot that will be too closed
669 // to the existing knot,
670 // we assign Uj=knot(i)
671 Standard_Integer n1, n2;
672 Standard_Real U1, U2;
678 for (i=n1; i<=n2; ++i) {
680 if (Abs(U-aU1)<=Eps) {
683 else if (Abs(U-aU2)<=Eps) {
687 // Henceforward we use U1, U2 as bounds of the segment
690 TColStd_Array1OfReal Knots(1,2);
691 TColStd_Array1OfInteger Mults(1,2);
693 // define param ditance to keep (eap, Apr 18 2002, occ311)
695 Standard_Real Period = LastParameter() - FirstParameter();
697 if (DU - Period > Precision::PConfusion())
698 throw Standard_DomainError("Geom2d_BSplineCurve::Segment");
704 BSplCLib::LocateParameter(deg,knots->Array1(),mults->Array1(),
705 U1,periodic,knots->Lower(),knots->Upper(),
708 BSplCLib::LocateParameter(deg,knots->Array1(),mults->Array1(),
709 U2,periodic,knots->Lower(),knots->Upper(),
711 Knots(1) = Min( NewU1, NewU2);
712 Knots(2) = Max( NewU1, NewU2);
713 Mults(1) = Mults( 2) = deg;
714 InsertKnots(Knots, Mults, Eps);
716 if (periodic) { // set the origine at NewU1
718 BSplCLib::LocateParameter(deg,knots->Array1(),mults->Array1(),
719 U1,periodic,knots->Lower(),knots->Upper(),
721 // Eps = Epsilon(knots->Value(index+1));
722 if ( Abs(knots->Value(index+1)-U) <= Eps) {
730 // compute index1 and index2 to set the new knots and mults
731 Standard_Integer index1 = 0, index2 = 0;
732 Standard_Integer FromU1 = knots->Lower();
733 Standard_Integer ToU2 = knots->Upper();
734 BSplCLib::LocateParameter(deg,knots->Array1(),mults->Array1(),
735 NewU1,periodic,FromU1,ToU2,index1,U);
736 BSplCLib::LocateParameter(deg,knots->Array1(),mults->Array1(),
737 NewU2,periodic,FromU1,ToU2,index2,U);
738 // Eps = Epsilon(knots->Value(index2+1));
739 if ( Abs(knots->Value(index2+1)-U) <= Eps){
743 Standard_Integer nbknots = index2 - index1 + 1;
745 Handle(TColStd_HArray1OfReal)
746 nknots = new TColStd_HArray1OfReal(1,nbknots);
747 Handle(TColStd_HArray1OfInteger)
748 nmults = new TColStd_HArray1OfInteger(1,nbknots);
750 // to restore changed U1
751 if (DU > 0) {// if was periodic
757 for ( i = index1; i<= index2; i++) {
758 nknots->SetValue(k, knots->Value(i) - DU);
759 nmults->SetValue(k, mults->Value(i));
762 nmults->SetValue( 1, deg + 1);
763 nmults->SetValue(nbknots, deg + 1);
766 // compute index1 and index2 to set the new poles and weights
767 Standard_Integer pindex1
768 = BSplCLib::PoleIndex(deg,index1,periodic,mults->Array1());
769 Standard_Integer pindex2
770 = BSplCLib::PoleIndex(deg,index2,periodic,mults->Array1());
773 pindex2 = Min( pindex2+1, poles->Length());
775 Standard_Integer nbpoles = pindex2 - pindex1 + 1;
777 Handle(TColStd_HArray1OfReal)
778 nweights = new TColStd_HArray1OfReal(1,nbpoles);
779 Handle(TColgp_HArray1OfPnt2d)
780 npoles = new TColgp_HArray1OfPnt2d(1,nbpoles);
784 nweights = new TColStd_HArray1OfReal( 1, nbpoles);
785 for ( i = pindex1; i <= pindex2; i++) {
786 npoles->SetValue(k, poles->Value(i));
787 nweights->SetValue(k, weights->Value(i));
792 for ( i = pindex1; i <= pindex2; i++) {
793 npoles->SetValue(k, poles->Value(i));
807 //=======================================================================
810 //=======================================================================
812 void Geom2d_BSplineCurve::SetKnot
813 (const Standard_Integer Index,
814 const Standard_Real K)
816 if (Index < 1 || Index > knots->Length()) throw Standard_OutOfRange("BSpline curve : SetKnot: Index and #pole mismatch");
817 Standard_Real DK = Abs(Epsilon (K));
819 if (K >= knots->Value(2) - DK) throw Standard_ConstructionError("BSpline curve :SetKnot :K out of range");
821 else if (Index == knots->Length()) {
822 if (K <= knots->Value (knots->Length()-1) + DK) {
823 throw Standard_ConstructionError("BSpline curve : SetKnot : K out of range");
827 if (K <= knots->Value(Index-1) + DK ||
828 K >= knots->Value(Index+1) - DK ) {
829 throw Standard_ConstructionError("BSpline curve : SetKnot: K out of range");
832 if (K != knots->Value (Index)) {
833 knots->SetValue (Index, K);
839 //=======================================================================
840 //function : SetKnots
842 //=======================================================================
844 void Geom2d_BSplineCurve::SetKnots
845 (const TColStd_Array1OfReal& K)
847 CheckCurveData(poles->Array1(),K,mults->Array1(),deg,periodic);
848 knots->ChangeArray1() = K;
853 //=======================================================================
856 //=======================================================================
858 void Geom2d_BSplineCurve::SetKnot
859 (const Standard_Integer Index,
860 const Standard_Real K,
861 const Standard_Integer M)
863 IncreaseMultiplicity (Index, M);
867 //=======================================================================
868 //function : SetPeriodic
870 //=======================================================================
872 void Geom2d_BSplineCurve::SetPeriodic ()
874 Standard_Integer first = FirstUKnotIndex();
875 Standard_Integer last = LastUKnotIndex();
877 Handle(TColStd_HArray1OfReal) tk = knots;
878 TColStd_Array1OfReal cknots((knots->Array1())(first),first,last);
879 knots = new TColStd_HArray1OfReal(1,cknots.Length());
880 knots->ChangeArray1() = cknots;
882 Handle(TColStd_HArray1OfInteger) tm = mults;
883 TColStd_Array1OfInteger cmults((mults->Array1())(first),first,last);
884 cmults(first) = cmults(last) = Min(deg, Max( cmults(first), cmults(last)));
885 mults = new TColStd_HArray1OfInteger(1,cmults.Length());
886 mults->ChangeArray1() = cmults;
888 // compute new number of poles;
889 Standard_Integer nbp = BSplCLib::NbPoles(deg,Standard_True,cmults);
891 Handle(TColgp_HArray1OfPnt2d) tp = poles;
892 TColgp_Array1OfPnt2d cpoles((poles->Array1())(1),1,nbp);
893 poles = new TColgp_HArray1OfPnt2d(1,nbp);
894 poles->ChangeArray1() = cpoles;
897 Handle(TColStd_HArray1OfReal) tw = weights;
898 TColStd_Array1OfReal cweights((weights->Array1())(1),1,nbp);
899 weights = new TColStd_HArray1OfReal(1,nbp);
900 weights->ChangeArray1() = cweights;
903 periodic = Standard_True;
909 //=======================================================================
910 //function : SetOrigin
912 //=======================================================================
914 void Geom2d_BSplineCurve::SetOrigin(const Standard_Integer Index)
917 throw Standard_NoSuchObject("Geom2d_BSplineCurve::SetOrigin");
918 Standard_Integer i,k;
919 Standard_Integer first = FirstUKnotIndex();
920 Standard_Integer last = LastUKnotIndex();
922 if ((Index < first) || (Index > last))
923 throw Standard_DomainError("Geom2d_BSplineCurve::SetOrigin");
925 Standard_Integer nbknots = knots->Length();
926 Standard_Integer nbpoles = poles->Length();
928 Handle(TColStd_HArray1OfReal) nknots =
929 new TColStd_HArray1OfReal(1,nbknots);
930 TColStd_Array1OfReal& newknots = nknots->ChangeArray1();
932 Handle(TColStd_HArray1OfInteger) nmults =
933 new TColStd_HArray1OfInteger(1,nbknots);
934 TColStd_Array1OfInteger& newmults = nmults->ChangeArray1();
936 // set the knots and mults
937 Standard_Real period = knots->Value(last) - knots->Value(first);
939 for ( i = Index; i <= last ; i++) {
940 newknots(k) = knots->Value(i);
941 newmults(k) = mults->Value(i);
944 for ( i = first+1; i <= Index; i++) {
945 newknots(k) = knots->Value(i) + period;
946 newmults(k) = mults->Value(i);
950 Standard_Integer index = 1;
951 for (i = first+1; i <= Index; i++)
952 index += mults->Value(i);
954 // set the poles and weights
955 Handle(TColgp_HArray1OfPnt2d) npoles =
956 new TColgp_HArray1OfPnt2d(1,nbpoles);
957 Handle(TColStd_HArray1OfReal) nweights =
958 new TColStd_HArray1OfReal(1,nbpoles);
959 TColgp_Array1OfPnt2d & newpoles = npoles->ChangeArray1();
960 TColStd_Array1OfReal & newweights = nweights->ChangeArray1();
961 first = poles->Lower();
962 last = poles->Upper();
965 for ( i = index; i <= last; i++) {
966 newpoles(k) = poles->Value(i);
967 newweights(k) = weights->Value(i);
970 for ( i = first; i < index; i++) {
971 newpoles(k) = poles->Value(i);
972 newweights(k) = weights->Value(i);
978 for ( i = index; i <= last; i++) {
979 newpoles(k) = poles->Value(i);
982 for ( i = first; i < index; i++) {
983 newpoles(k) = poles->Value(i);
997 //=======================================================================
998 //function : SetNotPeriodic
1000 //=======================================================================
1002 void Geom2d_BSplineCurve::SetNotPeriodic ()
1005 Standard_Integer NbKnots, NbPoles;
1006 BSplCLib::PrepareUnperiodize( deg, mults->Array1(),NbKnots,NbPoles);
1008 Handle(TColgp_HArray1OfPnt2d) npoles
1009 = new TColgp_HArray1OfPnt2d(1,NbPoles);
1011 Handle(TColStd_HArray1OfReal) nknots
1012 = new TColStd_HArray1OfReal(1,NbKnots);
1014 Handle(TColStd_HArray1OfInteger) nmults
1015 = new TColStd_HArray1OfInteger(1,NbKnots);
1017 Handle(TColStd_HArray1OfReal) nweights;
1019 nweights = new TColStd_HArray1OfReal(1,NbPoles);
1022 BSplCLib::Unperiodize (deg,
1023 mults->Array1(), knots->Array1(), poles->Array1(),
1024 !nweights.IsNull() ? &weights->Array1() : BSplCLib::NoWeights(),
1025 nmults->ChangeArray1(), nknots->ChangeArray1(), npoles->ChangeArray1(),
1026 !nweights.IsNull() ? &nweights->ChangeArray1() : BSplCLib::NoWeights());
1031 periodic = Standard_False;
1037 //=======================================================================
1038 //function : SetPole
1040 //=======================================================================
1042 void Geom2d_BSplineCurve::SetPole
1043 (const Standard_Integer Index,
1046 if (Index < 1 || Index > poles->Length()) throw Standard_OutOfRange("BSpline curve : SetPole : index and #pole mismatch");
1047 poles->SetValue (Index, P);
1051 //=======================================================================
1052 //function : SetPole
1054 //=======================================================================
1056 void Geom2d_BSplineCurve::SetPole
1057 (const Standard_Integer Index,
1059 const Standard_Real W)
1065 //=======================================================================
1066 //function : SetWeight
1068 //=======================================================================
1070 void Geom2d_BSplineCurve::SetWeight
1071 (const Standard_Integer Index,
1072 const Standard_Real W)
1074 if (Index < 1 || Index > poles->Length()) throw Standard_OutOfRange("BSpline curve : SetWeight: Index and #pole mismatch");
1076 if (W <= gp::Resolution ()) throw Standard_ConstructionError("BSpline curve : SetWeight: Weight too small");
1079 Standard_Boolean rat = IsRational() || (Abs(W - 1.) > gp::Resolution());
1082 if (rat && !IsRational()) {
1083 weights = new TColStd_HArray1OfReal(1,poles->Length());
1087 weights->SetValue (Index, W);
1090 rat = Rational(weights->Array1());
1091 if (!rat) weights.Nullify();
1094 rational = !weights.IsNull();
1100 //=======================================================================
1101 //function : MovePoint
1103 //=======================================================================
1105 void Geom2d_BSplineCurve::MovePoint(const Standard_Real U,
1107 const Standard_Integer Index1,
1108 const Standard_Integer Index2,
1109 Standard_Integer& FirstModifiedPole,
1110 Standard_Integer& LastmodifiedPole)
1112 if (Index1 < 1 || Index1 > poles->Length() ||
1113 Index2 < 1 || Index2 > poles->Length() || Index1 > Index2) {
1114 throw Standard_OutOfRange("BSpline curve : MovePoint: Index and #pole mismatch");
1116 TColgp_Array1OfPnt2d npoles(1, poles->Length());
1119 gp_Vec2d Displ(P0, P);
1121 BSplCLib::MovePoint (U, Displ, Index1, Index2, deg, poles->Array1(),
1122 rational ? &weights->Array1() : BSplCLib::NoWeights(), flatknots->Array1(),
1123 FirstModifiedPole, LastmodifiedPole, npoles);
1124 if (FirstModifiedPole) {
1125 poles->ChangeArray1() = npoles;
1130 //=======================================================================
1131 //function : MovePointAndTangent
1133 //=======================================================================
1135 void Geom2d_BSplineCurve::
1136 MovePointAndTangent(const Standard_Real U,
1138 const gp_Vec2d& Tangent,
1139 const Standard_Real Tolerance,
1140 const Standard_Integer StartingCondition,
1141 const Standard_Integer EndingCondition,
1142 Standard_Integer& ErrorStatus)
1144 Standard_Integer ii ;
1147 // for the time being do not deal with periodic curves
1151 TColgp_Array1OfPnt2d new_poles(1, poles->Length());
1155 gp_Vec2d delta_derivative;
1158 gp_Vec2d delta(P0, P);
1159 for (ii = 1 ; ii <= 2 ; ii++) {
1160 delta_derivative.SetCoord(ii,
1161 Tangent.Coord(ii)- delta_derivative.Coord(ii)) ;
1164 BSplCLib::MovePointAndTangent (U,
1172 rational ? &weights->Array1() : BSplCLib::NoWeights(),
1173 flatknots->Array1(),
1177 poles->ChangeArray1() = new_poles;
1182 //=======================================================================
1183 //function : UpdateKnots
1185 //=======================================================================
1187 void Geom2d_BSplineCurve::UpdateKnots()
1190 rational = !weights.IsNull();
1192 Standard_Integer MaxKnotMult = 0;
1193 BSplCLib::KnotAnalysis (deg,
1197 knotSet, MaxKnotMult);
1199 if (knotSet == GeomAbs_Uniform && !periodic) {
1203 flatknots = new TColStd_HArray1OfReal
1204 (1, BSplCLib::KnotSequenceLength(mults->Array1(),deg,periodic));
1206 BSplCLib::KnotSequence (knots->Array1(),
1209 flatknots->ChangeArray1());
1212 if (MaxKnotMult == 0) smooth = GeomAbs_CN;
1214 switch (deg - MaxKnotMult) {
1215 case 0 : smooth = GeomAbs_C0; break;
1216 case 1 : smooth = GeomAbs_C1; break;
1217 case 2 : smooth = GeomAbs_C2; break;
1218 case 3 : smooth = GeomAbs_C3; break;
1219 default : smooth = GeomAbs_C3; break;
1224 //=======================================================================
1225 //function : Normalizes the parameters if the curve is periodic
1226 //purpose : that is compute the cache so that it is valid
1227 //=======================================================================
1229 void Geom2d_BSplineCurve::PeriodicNormalization(Standard_Real& Parameter) const
1231 Standard_Real Period ;
1234 Period = flatknots->Value(flatknots->Upper() - deg) - flatknots->Value (deg + 1) ;
1235 while (Parameter > flatknots->Value(flatknots->Upper()-deg)) {
1236 Parameter -= Period ;
1238 while (Parameter < flatknots->Value((deg + 1))) {
1239 Parameter += Period ;