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)
656 throw Standard_DomainError("Geom2d_BSplineCurve::Segment");
658 Standard_Real AbsUMax = Max(Abs(FirstParameter()),Abs(LastParameter()));
659 Standard_Real Eps = Max (Epsilon(AbsUMax), Precision::PConfusion());
660 Standard_Real NewU1, NewU2;
661 Standard_Real U, DU=0;
662 Standard_Integer i, k, index;
665 // Checking the input bounds aUj (j=1,2).
666 // For the case when aUj==knot(i),
667 // in order to prevent the insertion of a new knot that will be too closed
668 // to the existing knot,
669 // we assign Uj=knot(i)
670 Standard_Integer n1, n2;
671 Standard_Real U1, U2;
677 for (i=n1; i<=n2; ++i) {
679 if (Abs(U-aU1)<=Eps) {
682 else if (Abs(U-aU2)<=Eps) {
686 // Henceforward we use U1, U2 as bounds of the segment
689 TColStd_Array1OfReal Knots(1,2);
690 TColStd_Array1OfInteger Mults(1,2);
692 // define param ditance to keep (eap, Apr 18 2002, occ311)
694 Standard_Real Period = LastParameter() - FirstParameter();
696 if (DU - Period > Precision::PConfusion())
697 throw Standard_DomainError("Geom2d_BSplineCurve::Segment");
703 BSplCLib::LocateParameter(deg,knots->Array1(),mults->Array1(),
704 U1,periodic,knots->Lower(),knots->Upper(),
707 BSplCLib::LocateParameter(deg,knots->Array1(),mults->Array1(),
708 U2,periodic,knots->Lower(),knots->Upper(),
710 Knots(1) = Min( NewU1, NewU2);
711 Knots(2) = Max( NewU1, NewU2);
712 Mults(1) = Mults( 2) = deg;
713 InsertKnots(Knots, Mults, Eps);
715 if (periodic) { // set the origine at NewU1
717 BSplCLib::LocateParameter(deg,knots->Array1(),mults->Array1(),
718 U1,periodic,knots->Lower(),knots->Upper(),
720 // Eps = Epsilon(knots->Value(index+1));
721 if ( Abs(knots->Value(index+1)-U) <= Eps) {
729 // compute index1 and index2 to set the new knots and mults
730 Standard_Integer index1 = 0, index2 = 0;
731 Standard_Integer FromU1 = knots->Lower();
732 Standard_Integer ToU2 = knots->Upper();
733 BSplCLib::LocateParameter(deg,knots->Array1(),mults->Array1(),
734 NewU1,periodic,FromU1,ToU2,index1,U);
735 BSplCLib::LocateParameter(deg,knots->Array1(),mults->Array1(),
736 NewU2,periodic,FromU1,ToU2,index2,U);
737 // Eps = Epsilon(knots->Value(index2+1));
738 if ( Abs(knots->Value(index2+1)-U) <= Eps){
742 Standard_Integer nbknots = index2 - index1 + 1;
744 Handle(TColStd_HArray1OfReal)
745 nknots = new TColStd_HArray1OfReal(1,nbknots);
746 Handle(TColStd_HArray1OfInteger)
747 nmults = new TColStd_HArray1OfInteger(1,nbknots);
749 // to restore changed U1
750 if (DU > 0) {// if was periodic
756 for ( i = index1; i<= index2; i++) {
757 nknots->SetValue(k, knots->Value(i) - DU);
758 nmults->SetValue(k, mults->Value(i));
761 nmults->SetValue( 1, deg + 1);
762 nmults->SetValue(nbknots, deg + 1);
765 // compute index1 and index2 to set the new poles and weights
766 Standard_Integer pindex1
767 = BSplCLib::PoleIndex(deg,index1,periodic,mults->Array1());
768 Standard_Integer pindex2
769 = BSplCLib::PoleIndex(deg,index2,periodic,mults->Array1());
772 pindex2 = Min( pindex2+1, poles->Length());
774 Standard_Integer nbpoles = pindex2 - pindex1 + 1;
776 Handle(TColStd_HArray1OfReal)
777 nweights = new TColStd_HArray1OfReal(1,nbpoles);
778 Handle(TColgp_HArray1OfPnt2d)
779 npoles = new TColgp_HArray1OfPnt2d(1,nbpoles);
783 nweights = new TColStd_HArray1OfReal( 1, nbpoles);
784 for ( i = pindex1; i <= pindex2; i++) {
785 npoles->SetValue(k, poles->Value(i));
786 nweights->SetValue(k, weights->Value(i));
791 for ( i = pindex1; i <= pindex2; i++) {
792 npoles->SetValue(k, poles->Value(i));
806 //=======================================================================
809 //=======================================================================
811 void Geom2d_BSplineCurve::SetKnot
812 (const Standard_Integer Index,
813 const Standard_Real K)
815 if (Index < 1 || Index > knots->Length()) throw Standard_OutOfRange("BSpline curve : SetKnot: Index and #pole mismatch");
816 Standard_Real DK = Abs(Epsilon (K));
818 if (K >= knots->Value(2) - DK) throw Standard_ConstructionError("BSpline curve :SetKnot :K out of range");
820 else if (Index == knots->Length()) {
821 if (K <= knots->Value (knots->Length()-1) + DK) {
822 throw Standard_ConstructionError("BSpline curve : SetKnot : K out of range");
826 if (K <= knots->Value(Index-1) + DK ||
827 K >= knots->Value(Index+1) - DK ) {
828 throw Standard_ConstructionError("BSpline curve : SetKnot: K out of range");
831 if (K != knots->Value (Index)) {
832 knots->SetValue (Index, K);
838 //=======================================================================
839 //function : SetKnots
841 //=======================================================================
843 void Geom2d_BSplineCurve::SetKnots
844 (const TColStd_Array1OfReal& K)
846 CheckCurveData(poles->Array1(),K,mults->Array1(),deg,periodic);
847 knots->ChangeArray1() = K;
852 //=======================================================================
855 //=======================================================================
857 void Geom2d_BSplineCurve::SetKnot
858 (const Standard_Integer Index,
859 const Standard_Real K,
860 const Standard_Integer M)
862 IncreaseMultiplicity (Index, M);
866 //=======================================================================
867 //function : SetPeriodic
869 //=======================================================================
871 void Geom2d_BSplineCurve::SetPeriodic ()
873 Standard_Integer first = FirstUKnotIndex();
874 Standard_Integer last = LastUKnotIndex();
876 Handle(TColStd_HArray1OfReal) tk = knots;
877 TColStd_Array1OfReal cknots((knots->Array1())(first),first,last);
878 knots = new TColStd_HArray1OfReal(1,cknots.Length());
879 knots->ChangeArray1() = cknots;
881 Handle(TColStd_HArray1OfInteger) tm = mults;
882 TColStd_Array1OfInteger cmults((mults->Array1())(first),first,last);
883 cmults(first) = cmults(last) = Min(deg, Max( cmults(first), cmults(last)));
884 mults = new TColStd_HArray1OfInteger(1,cmults.Length());
885 mults->ChangeArray1() = cmults;
887 // compute new number of poles;
888 Standard_Integer nbp = BSplCLib::NbPoles(deg,Standard_True,cmults);
890 Handle(TColgp_HArray1OfPnt2d) tp = poles;
891 TColgp_Array1OfPnt2d cpoles((poles->Array1())(1),1,nbp);
892 poles = new TColgp_HArray1OfPnt2d(1,nbp);
893 poles->ChangeArray1() = cpoles;
896 Handle(TColStd_HArray1OfReal) tw = weights;
897 TColStd_Array1OfReal cweights((weights->Array1())(1),1,nbp);
898 weights = new TColStd_HArray1OfReal(1,nbp);
899 weights->ChangeArray1() = cweights;
902 periodic = Standard_True;
908 //=======================================================================
909 //function : SetOrigin
911 //=======================================================================
913 void Geom2d_BSplineCurve::SetOrigin(const Standard_Integer Index)
916 throw Standard_NoSuchObject("Geom2d_BSplineCurve::SetOrigin");
917 Standard_Integer i,k;
918 Standard_Integer first = FirstUKnotIndex();
919 Standard_Integer last = LastUKnotIndex();
921 if ((Index < first) || (Index > last))
922 throw Standard_DomainError("Geom2d_BSplineCurve::SetOrigin");
924 Standard_Integer nbknots = knots->Length();
925 Standard_Integer nbpoles = poles->Length();
927 Handle(TColStd_HArray1OfReal) nknots =
928 new TColStd_HArray1OfReal(1,nbknots);
929 TColStd_Array1OfReal& newknots = nknots->ChangeArray1();
931 Handle(TColStd_HArray1OfInteger) nmults =
932 new TColStd_HArray1OfInteger(1,nbknots);
933 TColStd_Array1OfInteger& newmults = nmults->ChangeArray1();
935 // set the knots and mults
936 Standard_Real period = knots->Value(last) - knots->Value(first);
938 for ( i = Index; i <= last ; i++) {
939 newknots(k) = knots->Value(i);
940 newmults(k) = mults->Value(i);
943 for ( i = first+1; i <= Index; i++) {
944 newknots(k) = knots->Value(i) + period;
945 newmults(k) = mults->Value(i);
949 Standard_Integer index = 1;
950 for (i = first+1; i <= Index; i++)
951 index += mults->Value(i);
953 // set the poles and weights
954 Handle(TColgp_HArray1OfPnt2d) npoles =
955 new TColgp_HArray1OfPnt2d(1,nbpoles);
956 Handle(TColStd_HArray1OfReal) nweights =
957 new TColStd_HArray1OfReal(1,nbpoles);
958 TColgp_Array1OfPnt2d & newpoles = npoles->ChangeArray1();
959 TColStd_Array1OfReal & newweights = nweights->ChangeArray1();
960 first = poles->Lower();
961 last = poles->Upper();
964 for ( i = index; i <= last; i++) {
965 newpoles(k) = poles->Value(i);
966 newweights(k) = weights->Value(i);
969 for ( i = first; i < index; i++) {
970 newpoles(k) = poles->Value(i);
971 newweights(k) = weights->Value(i);
977 for ( i = index; i <= last; i++) {
978 newpoles(k) = poles->Value(i);
981 for ( i = first; i < index; i++) {
982 newpoles(k) = poles->Value(i);
996 //=======================================================================
997 //function : SetNotPeriodic
999 //=======================================================================
1001 void Geom2d_BSplineCurve::SetNotPeriodic ()
1004 Standard_Integer NbKnots, NbPoles;
1005 BSplCLib::PrepareUnperiodize( deg, mults->Array1(),NbKnots,NbPoles);
1007 Handle(TColgp_HArray1OfPnt2d) npoles
1008 = new TColgp_HArray1OfPnt2d(1,NbPoles);
1010 Handle(TColStd_HArray1OfReal) nknots
1011 = new TColStd_HArray1OfReal(1,NbKnots);
1013 Handle(TColStd_HArray1OfInteger) nmults
1014 = new TColStd_HArray1OfInteger(1,NbKnots);
1016 Handle(TColStd_HArray1OfReal) nweights;
1018 nweights = new TColStd_HArray1OfReal(1,NbPoles);
1021 BSplCLib::Unperiodize (deg,
1022 mults->Array1(), knots->Array1(), poles->Array1(),
1023 !nweights.IsNull() ? &weights->Array1() : BSplCLib::NoWeights(),
1024 nmults->ChangeArray1(), nknots->ChangeArray1(), npoles->ChangeArray1(),
1025 !nweights.IsNull() ? &nweights->ChangeArray1() : BSplCLib::NoWeights());
1030 periodic = Standard_False;
1036 //=======================================================================
1037 //function : SetPole
1039 //=======================================================================
1041 void Geom2d_BSplineCurve::SetPole
1042 (const Standard_Integer Index,
1045 if (Index < 1 || Index > poles->Length()) throw Standard_OutOfRange("BSpline curve : SetPole : index and #pole mismatch");
1046 poles->SetValue (Index, P);
1050 //=======================================================================
1051 //function : SetPole
1053 //=======================================================================
1055 void Geom2d_BSplineCurve::SetPole
1056 (const Standard_Integer Index,
1058 const Standard_Real W)
1064 //=======================================================================
1065 //function : SetWeight
1067 //=======================================================================
1069 void Geom2d_BSplineCurve::SetWeight
1070 (const Standard_Integer Index,
1071 const Standard_Real W)
1073 if (Index < 1 || Index > poles->Length()) throw Standard_OutOfRange("BSpline curve : SetWeight: Index and #pole mismatch");
1075 if (W <= gp::Resolution ()) throw Standard_ConstructionError("BSpline curve : SetWeight: Weight too small");
1078 Standard_Boolean rat = IsRational() || (Abs(W - 1.) > gp::Resolution());
1081 if (rat && !IsRational()) {
1082 weights = new TColStd_HArray1OfReal(1,poles->Length());
1086 weights->SetValue (Index, W);
1089 rat = Rational(weights->Array1());
1090 if (!rat) weights.Nullify();
1093 rational = !weights.IsNull();
1099 //=======================================================================
1100 //function : MovePoint
1102 //=======================================================================
1104 void Geom2d_BSplineCurve::MovePoint(const Standard_Real U,
1106 const Standard_Integer Index1,
1107 const Standard_Integer Index2,
1108 Standard_Integer& FirstModifiedPole,
1109 Standard_Integer& LastmodifiedPole)
1111 if (Index1 < 1 || Index1 > poles->Length() ||
1112 Index2 < 1 || Index2 > poles->Length() || Index1 > Index2) {
1113 throw Standard_OutOfRange("BSpline curve : MovePoint: Index and #pole mismatch");
1115 TColgp_Array1OfPnt2d npoles(1, poles->Length());
1118 gp_Vec2d Displ(P0, P);
1120 BSplCLib::MovePoint (U, Displ, Index1, Index2, deg, poles->Array1(),
1121 rational ? &weights->Array1() : BSplCLib::NoWeights(), flatknots->Array1(),
1122 FirstModifiedPole, LastmodifiedPole, npoles);
1123 if (FirstModifiedPole) {
1124 poles->ChangeArray1() = npoles;
1129 //=======================================================================
1130 //function : MovePointAndTangent
1132 //=======================================================================
1134 void Geom2d_BSplineCurve::
1135 MovePointAndTangent(const Standard_Real U,
1137 const gp_Vec2d& Tangent,
1138 const Standard_Real Tolerance,
1139 const Standard_Integer StartingCondition,
1140 const Standard_Integer EndingCondition,
1141 Standard_Integer& ErrorStatus)
1143 Standard_Integer ii ;
1146 // for the time being do not deal with periodic curves
1150 TColgp_Array1OfPnt2d new_poles(1, poles->Length());
1154 gp_Vec2d delta_derivative;
1157 gp_Vec2d delta(P0, P);
1158 for (ii = 1 ; ii <= 2 ; ii++) {
1159 delta_derivative.SetCoord(ii,
1160 Tangent.Coord(ii)- delta_derivative.Coord(ii)) ;
1163 BSplCLib::MovePointAndTangent (U,
1171 rational ? &weights->Array1() : BSplCLib::NoWeights(),
1172 flatknots->Array1(),
1176 poles->ChangeArray1() = new_poles;
1181 //=======================================================================
1182 //function : UpdateKnots
1184 //=======================================================================
1186 void Geom2d_BSplineCurve::UpdateKnots()
1189 rational = !weights.IsNull();
1191 Standard_Integer MaxKnotMult = 0;
1192 BSplCLib::KnotAnalysis (deg,
1196 knotSet, MaxKnotMult);
1198 if (knotSet == GeomAbs_Uniform && !periodic) {
1202 flatknots = new TColStd_HArray1OfReal
1203 (1, BSplCLib::KnotSequenceLength(mults->Array1(),deg,periodic));
1205 BSplCLib::KnotSequence (knots->Array1(),
1208 flatknots->ChangeArray1());
1211 if (MaxKnotMult == 0) smooth = GeomAbs_CN;
1213 switch (deg - MaxKnotMult) {
1214 case 0 : smooth = GeomAbs_C0; break;
1215 case 1 : smooth = GeomAbs_C1; break;
1216 case 2 : smooth = GeomAbs_C2; break;
1217 case 3 : smooth = GeomAbs_C3; break;
1218 default : smooth = GeomAbs_C3; break;
1223 //=======================================================================
1224 //function : Normalizes the parameters if the curve is periodic
1225 //purpose : that is compute the cache so that it is valid
1226 //=======================================================================
1228 void Geom2d_BSplineCurve::PeriodicNormalization(Standard_Real& Parameter) const
1230 Standard_Real Period ;
1233 Period = flatknots->Value(flatknots->Upper() - deg) - flatknots->Value (deg + 1) ;
1234 while (Parameter > flatknots->Value(flatknots->Upper()-deg)) {
1235 Parameter -= Period ;
1237 while (Parameter < flatknots->Value((deg + 1))) {
1238 Parameter += Period ;