1 // Created on: 1991-08-09
3 // Copyright (c) 1991-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 // Modified RLE 9 Sep 1993
18 // pmn : modified 28-01-97 : fixed a mistake in LocateParameter (PRO6973)
19 // pmn : modified 4-11-96 : fixed a mistake in BuildKnots (PRO6124)
20 // pmn : modified 28-Jun-96 : fixed a mistake in AntiBoorScheme
21 // xab : modified 15-Jun-95 : fixed a mistake in IsRational
22 // xab : modified 15-Mar-95 : removed Epsilon comparison in IsRational
23 // added RationalDerivatives.
24 // xab : 30-Mar-95 : fixed coupling with lti in RationalDerivatives
25 // xab : 15-Mar-96 : fixed a typo in Eval with extrapolation
26 // jct : 15-Apr-97 : added TangExtendToConstraint
27 // jct : 24-Apr-97 : correction on computation of Tbord and NewFlatKnots
28 // in TangExtendToConstraint; Continuity can be equal to 0
30 #include <BSplCLib.hxx>
33 #include <gp_Pnt2d.hxx>
35 #include <gp_Vec2d.hxx>
36 #include <math_Matrix.hxx>
37 #include <NCollection_LocalArray.hxx>
39 #include <Precision.hxx>
40 #include <Standard_NotImplemented.hxx>
44 typedef TColgp_Array1OfPnt Array1OfPnt;
45 typedef TColStd_Array1OfReal Array1OfReal;
46 typedef TColStd_Array1OfInteger Array1OfInteger;
48 //=======================================================================
49 //class : BSplCLib_LocalMatrix
50 //purpose: Auxiliary class optimizing creation of matrix buffer for
51 // evaluation of bspline (using stack allocation for main matrix)
52 //=======================================================================
54 class BSplCLib_LocalMatrix : public math_Matrix
57 BSplCLib_LocalMatrix (Standard_Integer DerivativeRequest, Standard_Integer Order)
58 : math_Matrix (myBuffer, 1, DerivativeRequest + 1, 1, Order)
60 Standard_OutOfRange_Raise_if (DerivativeRequest > BSplCLib::MaxDegree() ||
61 Order > BSplCLib::MaxDegree()+1 || BSplCLib::MaxDegree() > 25,
62 "BSplCLib: bspline degree is greater than maximum supported");
66 // local buffer, to be sufficient for addressing by index [Degree+1][Degree+1]
67 // (see math_Matrix implementation)
68 Standard_Real myBuffer[27*27];
71 //=======================================================================
74 //=======================================================================
76 void BSplCLib::Hunt (const TColStd_Array1OfReal& theArray,
77 const Standard_Real theX,
78 Standard_Integer& theXPos)
80 // replaced by simple dichotomy (RLE)
81 if (theArray.First() > theX)
83 theXPos = theArray.Lower() - 1;
86 else if (theArray.Last() < theX)
88 theXPos = theArray.Upper() + 1;
92 theXPos = theArray.Lower();
93 if (theArray.Length() <= 1)
98 Standard_Integer aHi = theArray.Upper();
99 while (aHi - theXPos != 1)
101 const Standard_Integer aMid = (aHi + theXPos) / 2;
102 if (theArray.Value (aMid) < theX)
113 //=======================================================================
114 //function : FirstUKnotIndex
116 //=======================================================================
118 Standard_Integer BSplCLib::FirstUKnotIndex (const Standard_Integer Degree,
119 const TColStd_Array1OfInteger& Mults)
121 Standard_Integer Index = Mults.Lower();
122 Standard_Integer SigmaMult = Mults(Index);
124 while (SigmaMult <= Degree) {
126 SigmaMult += Mults (Index);
131 //=======================================================================
132 //function : LastUKnotIndex
134 //=======================================================================
136 Standard_Integer BSplCLib::LastUKnotIndex (const Standard_Integer Degree,
137 const Array1OfInteger& Mults)
139 Standard_Integer Index = Mults.Upper();
140 Standard_Integer SigmaMult = Mults(Index);
142 while (SigmaMult <= Degree) {
144 SigmaMult += Mults.Value (Index);
149 //=======================================================================
150 //function : FlatIndex
152 //=======================================================================
154 Standard_Integer BSplCLib::FlatIndex
155 (const Standard_Integer Degree,
156 const Standard_Integer Index,
157 const TColStd_Array1OfInteger& Mults,
158 const Standard_Boolean Periodic)
160 Standard_Integer i, index = Index;
161 const Standard_Integer MLower = Mults.Lower();
162 const Standard_Integer *pmu = &Mults(MLower);
165 for (i = MLower + 1; i <= Index; i++)
170 index += pmu[MLower] - 1;
174 //=======================================================================
175 //function : LocateParameter
176 //purpose : Processing of nodes with multiplicities
177 //pmn 28-01-97 -> compute eventual of the period.
178 //=======================================================================
180 void BSplCLib::LocateParameter
181 (const Standard_Integer , //Degree,
182 const Array1OfReal& Knots,
183 const Array1OfInteger& , //Mults,
184 const Standard_Real U,
185 const Standard_Boolean IsPeriodic,
186 const Standard_Integer FromK1,
187 const Standard_Integer ToK2,
188 Standard_Integer& KnotIndex,
191 Standard_Real uf = 0, ul=1;
193 uf = Knots(Knots.Lower());
194 ul = Knots(Knots.Upper());
196 BSplCLib::LocateParameter(Knots,U,IsPeriodic,FromK1,ToK2,
197 KnotIndex,NewU, uf, ul);
200 //=======================================================================
201 //function : LocateParameter
202 //purpose : For plane nodes
203 // pmn 28-01-97 -> There is a need of the degre to calculate
204 // the eventual period
205 //=======================================================================
207 void BSplCLib::LocateParameter
208 (const Standard_Integer Degree,
209 const Array1OfReal& Knots,
210 const Standard_Real U,
211 const Standard_Boolean IsPeriodic,
212 const Standard_Integer FromK1,
213 const Standard_Integer ToK2,
214 Standard_Integer& KnotIndex,
218 BSplCLib::LocateParameter(Knots, U, IsPeriodic, FromK1, ToK2,
220 Knots(Knots.Lower() + Degree),
221 Knots(Knots.Upper() - Degree));
223 BSplCLib::LocateParameter(Knots, U, IsPeriodic, FromK1, ToK2,
229 //=======================================================================
230 //function : LocateParameter
231 //purpose : Effective computation
232 // pmn 28-01-97 : Add limits of the period as input argument,
233 // as it is imposible to produce them at this level.
234 //=======================================================================
236 void BSplCLib::LocateParameter
237 (const TColStd_Array1OfReal& Knots,
238 const Standard_Real U,
239 const Standard_Boolean IsPeriodic,
240 const Standard_Integer FromK1,
241 const Standard_Integer ToK2,
242 Standard_Integer& KnotIndex,
244 const Standard_Real UFirst,
245 const Standard_Real ULast)
248 Let Knots are distributed as follows (the array is sorted in ascending order):
250 K1, K1,..., K1, K1, K2, K2,..., K2, K2,..., Kn, Kn,..., Kn
251 M1 times M2 times Mn times
253 NbKnots = sum(M1+M2+...+Mn)
254 If U <= K1 then KnotIndex should be equal to M1.
255 If U >= Kn then KnotIndex should be equal to NbKnots-Mn-1.
256 If Ki <= U < K(i+1) then KnotIndex should be equal to sum (M1+M2+...+Mi).
259 Standard_Integer First,Last;
268 Standard_Integer Last1 = Last - 1;
270 if (IsPeriodic && (NewU < UFirst || NewU > ULast))
271 NewU = ElCLib::InPeriod(NewU, UFirst, ULast);
273 BSplCLib::Hunt (Knots, NewU, KnotIndex);
276 const Standard_Integer KLower = Knots.Lower(),
277 KUpper = Knots.Upper();
279 const Standard_Real Eps = Epsilon(Min(Abs(Knots(KUpper)), Abs(U)));
281 const Standard_Real *knots = &Knots(KLower);
283 if ( KnotIndex < Knots.Upper()) {
284 val = NewU - knots[KnotIndex + 1];
285 if (val < 0) val = - val;
286 // <= to be coherent with Segment where Eps corresponds to a bit of error.
287 if (val <= Eps) KnotIndex++;
289 if (KnotIndex < First) KnotIndex = First;
290 if (KnotIndex > Last1) KnotIndex = Last1;
292 if (KnotIndex != Last1) {
293 Standard_Real K1 = knots[KnotIndex];
294 Standard_Real K2 = knots[KnotIndex + 1];
296 if (val < 0) val = - val;
301 if(KnotIndex >= Knots.Upper())
305 K2 = knots[KnotIndex + 1];
307 if (val < 0) val = - val;
312 //=======================================================================
313 //function : LocateParameter
314 //purpose : the index is recomputed only if out of range
315 //pmn 28-01-97 -> eventual computation of the period.
316 //=======================================================================
318 void BSplCLib::LocateParameter
319 (const Standard_Integer Degree,
320 const TColStd_Array1OfReal& Knots,
321 const TColStd_Array1OfInteger* Mults,
322 const Standard_Real U,
323 const Standard_Boolean Periodic,
324 Standard_Integer& KnotIndex,
327 Standard_Integer first,last;
330 first = Knots.Lower();
331 last = Knots.Upper();
334 first = FirstUKnotIndex(Degree,*Mults);
335 last = LastUKnotIndex (Degree,*Mults);
339 first = Knots.Lower() + Degree;
340 last = Knots.Upper() - Degree;
342 if ( KnotIndex < first || KnotIndex > last)
343 BSplCLib::LocateParameter(Knots, U, Periodic, first, last,
344 KnotIndex, NewU, Knots(first), Knots(last));
349 //=======================================================================
350 //function : MaxKnotMult
352 //=======================================================================
354 Standard_Integer BSplCLib::MaxKnotMult
355 (const Array1OfInteger& Mults,
356 const Standard_Integer FromK1,
357 const Standard_Integer ToK2)
359 Standard_Integer MLower = Mults.Lower();
360 const Standard_Integer *pmu = &Mults(MLower);
362 Standard_Integer MaxMult = pmu[FromK1];
364 for (Standard_Integer i = FromK1; i <= ToK2; i++) {
365 if (MaxMult < pmu[i]) MaxMult = pmu[i];
370 //=======================================================================
371 //function : MinKnotMult
373 //=======================================================================
375 Standard_Integer BSplCLib::MinKnotMult
376 (const Array1OfInteger& Mults,
377 const Standard_Integer FromK1,
378 const Standard_Integer ToK2)
380 Standard_Integer MLower = Mults.Lower();
381 const Standard_Integer *pmu = &Mults(MLower);
383 Standard_Integer MinMult = pmu[FromK1];
385 for (Standard_Integer i = FromK1; i <= ToK2; i++) {
386 if (MinMult > pmu[i]) MinMult = pmu[i];
391 //=======================================================================
394 //=======================================================================
396 Standard_Integer BSplCLib::NbPoles(const Standard_Integer Degree,
397 const Standard_Boolean Periodic,
398 const TColStd_Array1OfInteger& Mults)
400 Standard_Integer i,sigma = 0;
401 Standard_Integer f = Mults.Lower();
402 Standard_Integer l = Mults.Upper();
403 const Standard_Integer * pmu = &Mults(f);
405 Standard_Integer Mf = pmu[f];
406 Standard_Integer Ml = pmu[l];
407 if (Mf <= 0) return 0;
408 if (Ml <= 0) return 0;
410 if (Mf > Degree) return 0;
411 if (Ml > Degree) return 0;
412 if (Mf != Ml ) return 0;
416 Standard_Integer Deg1 = Degree + 1;
417 if (Mf > Deg1) return 0;
418 if (Ml > Deg1) return 0;
419 sigma = Mf + Ml - Deg1;
422 for (i = f + 1; i < l; i++) {
423 if (pmu[i] <= 0 ) return 0;
424 if (pmu[i] > Degree) return 0;
430 //=======================================================================
431 //function : KnotSequenceLength
433 //=======================================================================
435 Standard_Integer BSplCLib::KnotSequenceLength
436 (const TColStd_Array1OfInteger& Mults,
437 const Standard_Integer Degree,
438 const Standard_Boolean Periodic)
440 Standard_Integer i,l = 0;
441 Standard_Integer MLower = Mults.Lower();
442 Standard_Integer MUpper = Mults.Upper();
443 const Standard_Integer * pmu = &Mults(MLower);
446 for (i = MLower; i <= MUpper; i++)
448 if (Periodic) l += 2 * (Degree + 1 - pmu[MLower]);
452 //=======================================================================
453 //function : KnotSequence
455 //=======================================================================
457 void BSplCLib::KnotSequence
458 (const TColStd_Array1OfReal& Knots,
459 const TColStd_Array1OfInteger& Mults,
460 TColStd_Array1OfReal& KnotSeq,
461 const Standard_Boolean Periodic)
463 BSplCLib::KnotSequence(Knots,Mults,0,Periodic,KnotSeq);
466 //=======================================================================
467 //function : KnotSequence
469 //=======================================================================
471 void BSplCLib::KnotSequence
472 (const TColStd_Array1OfReal& Knots,
473 const TColStd_Array1OfInteger& Mults,
474 const Standard_Integer Degree,
475 const Standard_Boolean Periodic,
476 TColStd_Array1OfReal& KnotSeq)
479 Standard_Integer Mult;
480 Standard_Integer MLower = Mults.Lower();
481 const Standard_Integer * pmu = &Mults(MLower);
483 Standard_Integer KLower = Knots.Lower();
484 Standard_Integer KUpper = Knots.Upper();
485 const Standard_Real * pkn = &Knots(KLower);
487 Standard_Integer M1 = Degree + 1 - pmu[MLower]; // for periodic
488 Standard_Integer i,j,index = Periodic ? M1 + 1 : 1;
490 for (i = KLower; i <= KUpper; i++) {
494 for (j = 1; j <= Mult; j++) {
500 Standard_Real period = pkn[KUpper] - pkn[KLower];
505 for (i = M1; i >= 1; i--) {
506 KnotSeq(i) = pkn[j] - period;
516 for (i = index; i <= KnotSeq.Upper(); i++) {
517 KnotSeq(i) = pkn[j] + period;
527 //=======================================================================
528 //function : KnotsLength
530 //=======================================================================
531 Standard_Integer BSplCLib::KnotsLength(const TColStd_Array1OfReal& SeqKnots,
532 // const Standard_Boolean Periodic)
533 const Standard_Boolean )
535 Standard_Integer sizeMult = 1;
536 Standard_Real val = SeqKnots(1);
537 for (Standard_Integer jj=2;
538 jj<=SeqKnots.Length();jj++)
540 // test on strict equality on nodes
541 if (SeqKnots(jj)!=val)
550 //=======================================================================
553 //=======================================================================
554 void BSplCLib::Knots(const TColStd_Array1OfReal& SeqKnots,
555 TColStd_Array1OfReal &knots,
556 TColStd_Array1OfInteger &mult,
557 // const Standard_Boolean Periodic)
558 const Standard_Boolean )
560 Standard_Real val = SeqKnots(1);
561 Standard_Integer kk=1;
565 for (Standard_Integer jj=2;jj<=SeqKnots.Length();jj++)
567 // test on strict equality on nodes
568 if (SeqKnots(jj)!=val)
582 //=======================================================================
583 //function : KnotForm
585 //=======================================================================
587 BSplCLib_KnotDistribution BSplCLib::KnotForm
588 (const Array1OfReal& Knots,
589 const Standard_Integer FromK1,
590 const Standard_Integer ToK2)
592 Standard_Real DU0,DU1,Ui,Uj,Eps0,val;
593 BSplCLib_KnotDistribution KForm = BSplCLib_Uniform;
595 if (FromK1 + 1 > Knots.Upper())
597 return BSplCLib_Uniform;
601 if (Ui < 0) Ui = - Ui;
602 Uj = Knots(FromK1 + 1);
603 if (Uj < 0) Uj = - Uj;
605 if (DU0 < 0) DU0 = - DU0;
606 Eps0 = Epsilon (Ui) + Epsilon (Uj) + Epsilon (DU0);
607 Standard_Integer i = FromK1 + 1;
609 while (KForm != BSplCLib_NonUniform && i < ToK2) {
611 if (Ui < 0) Ui = - Ui;
614 if (Uj < 0) Uj = - Uj;
616 if (DU1 < 0) DU1 = - DU1;
618 if (val < 0) val = -val;
619 if (val > Eps0) KForm = BSplCLib_NonUniform;
621 Eps0 = Epsilon (Ui) + Epsilon (Uj) + Epsilon (DU0);
626 //=======================================================================
627 //function : MultForm
629 //=======================================================================
631 BSplCLib_MultDistribution BSplCLib::MultForm
632 (const Array1OfInteger& Mults,
633 const Standard_Integer FromK1,
634 const Standard_Integer ToK2)
636 Standard_Integer First,Last;
645 if (First + 1 > Mults.Upper())
647 return BSplCLib_Constant;
650 Standard_Integer FirstMult = Mults(First);
651 BSplCLib_MultDistribution MForm = BSplCLib_Constant;
652 Standard_Integer i = First + 1;
653 Standard_Integer Mult = Mults(i);
655 // while (MForm != BSplCLib_NonUniform && i <= Last) { ???????????JR????????
656 while (MForm != BSplCLib_NonConstant && i <= Last) {
657 if (i == First + 1) {
658 if (Mult != FirstMult) MForm = BSplCLib_QuasiConstant;
660 else if (i == Last) {
661 if (MForm == BSplCLib_QuasiConstant) {
662 if (FirstMult != Mults(i)) MForm = BSplCLib_NonConstant;
665 if (Mult != Mults(i)) MForm = BSplCLib_NonConstant;
669 if (Mult != Mults(i)) MForm = BSplCLib_NonConstant;
677 //=======================================================================
678 //function : KnotAnalysis
680 //=======================================================================
682 void BSplCLib::KnotAnalysis (const Standard_Integer Degree,
683 const Standard_Boolean Periodic,
684 const TColStd_Array1OfReal& CKnots,
685 const TColStd_Array1OfInteger& CMults,
686 GeomAbs_BSplKnotDistribution& KnotForm,
687 Standard_Integer& MaxKnotMult)
689 KnotForm = GeomAbs_NonUniform;
691 BSplCLib_KnotDistribution KSet =
692 BSplCLib::KnotForm (CKnots, 1, CKnots.Length());
695 if (KSet == BSplCLib_Uniform) {
696 BSplCLib_MultDistribution MSet =
697 BSplCLib::MultForm (CMults, 1, CMults.Length());
699 case BSplCLib_NonConstant :
701 case BSplCLib_Constant :
702 if (CKnots.Length() == 2) {
703 KnotForm = GeomAbs_PiecewiseBezier;
706 if (CMults (1) == 1) KnotForm = GeomAbs_Uniform;
709 case BSplCLib_QuasiConstant :
710 if (CMults (1) == Degree + 1) {
711 Standard_Real M = CMults (2);
712 if (M == Degree ) KnotForm = GeomAbs_PiecewiseBezier;
713 else if (M == 1) KnotForm = GeomAbs_QuasiUniform;
719 Standard_Integer FirstKM =
720 Periodic ? CKnots.Lower() : BSplCLib::FirstUKnotIndex (Degree,CMults);
721 Standard_Integer LastKM =
722 Periodic ? CKnots.Upper() : BSplCLib::LastUKnotIndex (Degree,CMults);
724 if (LastKM - FirstKM != 1) {
725 Standard_Integer Multi;
726 for (Standard_Integer i = FirstKM + 1; i < LastKM; i++) {
728 MaxKnotMult = Max (MaxKnotMult, Multi);
733 //=======================================================================
734 //function : Reparametrize
736 //=======================================================================
738 void BSplCLib::Reparametrize
739 (const Standard_Real U1,
740 const Standard_Real U2,
743 Standard_Integer Lower = Knots.Lower();
744 Standard_Integer Upper = Knots.Upper();
745 Standard_Real UFirst = Min (U1, U2);
746 Standard_Real ULast = Max (U1, U2);
747 Standard_Real NewLength = ULast - UFirst;
748 BSplCLib_KnotDistribution KSet = BSplCLib::KnotForm (Knots, Lower, Upper);
749 if (KSet == BSplCLib_Uniform) {
750 Standard_Real DU = NewLength / (Upper - Lower);
751 Knots (Lower) = UFirst;
753 for (Standard_Integer i = Lower + 1; i <= Upper; i++) {
754 Knots (i) = Knots (i-1) + DU;
760 Standard_Real K1 = Knots (Lower);
761 Standard_Real Length = Knots (Upper) - Knots (Lower);
762 Knots (Lower) = UFirst;
764 for (Standard_Integer i = Lower + 1; i <= Upper; i++) {
766 Ratio = (K2 - K1) / Length;
767 Knots (i) = Knots (i-1) + (NewLength * Ratio);
770 Standard_Real Eps = Epsilon( Abs(Knots(i-1)) );
771 if (Knots(i) - Knots(i-1) <= Eps)
772 Knots(i) = NextAfter (Knots(i-1) + Eps, RealLast());
779 //=======================================================================
782 //=======================================================================
784 void BSplCLib::Reverse(TColStd_Array1OfReal& Knots)
786 Standard_Integer first = Knots.Lower();
787 Standard_Integer last = Knots.Upper();
788 Standard_Real kfirst = Knots(first);
789 Standard_Real klast = Knots(last);
790 Standard_Real tfirst = kfirst;
791 Standard_Real tlast = klast;
795 while (first <= last) {
796 tfirst += klast - Knots(last);
797 tlast -= Knots(first) - kfirst;
798 kfirst = Knots(first);
800 Knots(first) = tfirst;
807 //=======================================================================
810 //=======================================================================
812 void BSplCLib::Reverse(TColStd_Array1OfInteger& Mults)
814 Standard_Integer first = Mults.Lower();
815 Standard_Integer last = Mults.Upper();
816 Standard_Integer temp;
818 while (first < last) {
820 Mults(first) = Mults(last);
827 //=======================================================================
830 //=======================================================================
832 void BSplCLib::Reverse(TColStd_Array1OfReal& Weights,
833 const Standard_Integer L)
835 Standard_Integer i, l = L;
836 l = Weights.Lower()+(l-Weights.Lower())%(Weights.Upper()-Weights.Lower()+1);
838 TColStd_Array1OfReal temp(0,Weights.Length()-1);
840 for (i = Weights.Lower(); i <= l; i++)
841 temp(l-i) = Weights(i);
843 for (i = l+1; i <= Weights.Upper(); i++)
844 temp(l-Weights.Lower()+Weights.Upper()-i+1) = Weights(i);
846 for (i = Weights.Lower(); i <= Weights.Upper(); i++)
847 Weights(i) = temp(i-Weights.Lower());
850 //=======================================================================
851 //function : IsRational
853 //=======================================================================
855 Standard_Boolean BSplCLib::IsRational(const TColStd_Array1OfReal& Weights,
856 const Standard_Integer I1,
857 const Standard_Integer I2,
858 // const Standard_Real Epsi)
859 const Standard_Real )
861 Standard_Integer i, f = Weights.Lower(), l = Weights.Length();
862 Standard_Integer I3 = I2 - f;
863 const Standard_Real * WG = &Weights(f);
866 for (i = I1 - f; i < I3; i++) {
867 if (WG[f + (i % l)] != WG[f + ((i + 1) % l)]) return Standard_True;
869 return Standard_False ;
872 //=======================================================================
874 //purpose : evaluate point and derivatives
875 //=======================================================================
877 void BSplCLib::Eval(const Standard_Real U,
878 const Standard_Integer Degree,
879 Standard_Real& Knots,
880 const Standard_Integer Dimension,
881 Standard_Real& Poles)
883 Standard_Integer step,i,Dms,Dm1,Dpi,Sti;
884 Standard_Real X, Y, *poles, *knots = &Knots;
892 for (step = - 1; step < Dm1; step++) {
898 for (i = 0; i < Dms; i++) {
901 X = (knots[Dpi] - U) / (knots[Dpi] - knots[Sti]);
903 poles[0] *= X; poles[0] += Y * poles[1];
911 for (step = - 1; step < Dm1; step++) {
917 for (i = 0; i < Dms; i++) {
920 X = (knots[Dpi] - U) / (knots[Dpi] - knots[Sti]);
922 poles[0] *= X; poles[0] += Y * poles[2];
923 poles[1] *= X; poles[1] += Y * poles[3];
931 for (step = - 1; step < Dm1; step++) {
937 for (i = 0; i < Dms; i++) {
940 X = (knots[Dpi] - U) / (knots[Dpi] - knots[Sti]);
942 poles[0] *= X; poles[0] += Y * poles[3];
943 poles[1] *= X; poles[1] += Y * poles[4];
944 poles[2] *= X; poles[2] += Y * poles[5];
952 for (step = - 1; step < Dm1; step++) {
958 for (i = 0; i < Dms; i++) {
961 X = (knots[Dpi] - U) / (knots[Dpi] - knots[Sti]);
963 poles[0] *= X; poles[0] += Y * poles[4];
964 poles[1] *= X; poles[1] += Y * poles[5];
965 poles[2] *= X; poles[2] += Y * poles[6];
966 poles[3] *= X; poles[3] += Y * poles[7];
975 for (step = - 1; step < Dm1; step++) {
981 for (i = 0; i < Dms; i++) {
984 X = (knots[Dpi] - U) / (knots[Dpi] - knots[Sti]);
987 for (k = 0; k < Dimension; k++) {
989 poles[k] += Y * poles[k + Dimension];
998 //=======================================================================
999 //function : BoorScheme
1001 //=======================================================================
1003 void BSplCLib::BoorScheme(const Standard_Real U,
1004 const Standard_Integer Degree,
1005 Standard_Real& Knots,
1006 const Standard_Integer Dimension,
1007 Standard_Real& Poles,
1008 const Standard_Integer Depth,
1009 const Standard_Integer Length)
1012 // Compute the values
1016 // for i = 0 to Depth,
1017 // j = 0 to Length - i
1019 // The Boor scheme is :
1022 // P(i,j) = x * P(i-1,j) + (1-x) * P(i-1,j+1)
1024 // where x = (knot(i+j+Degree) - U) / (knot(i+j+Degree) - knot(i+j))
1027 // The values are stored in the array Poles
1028 // They are alternatively written if the odd and even positions.
1030 // The successives contents of the array are
1031 // ***** means unitialised, l = Degree + Length
1033 // P(0,0) ****** P(0,1) ...... P(0,l-1) ******** P(0,l)
1034 // P(0,0) P(1,0) P(0,1) ...... P(0,l-1) P(1,l-1) P(0,l)
1035 // P(0,0) P(1,0) P(2,0) ...... P(2,l-1) P(1,l-1) P(0,l)
1038 Standard_Integer i,k,step;
1039 Standard_Real *knots = &Knots;
1040 Standard_Real *pole, *firstpole = &Poles - 2 * Dimension;
1041 // the steps of recursion
1043 for (step = 0; step < Depth; step++) {
1044 firstpole += Dimension;
1046 // compute the new row of poles
1048 for (i = step; i < Length; i++) {
1049 pole += 2 * Dimension;
1051 Standard_Real X = (knots[i+Degree-step] - U)
1052 / (knots[i+Degree-step] - knots[i]);
1053 Standard_Real Y = 1. - X;
1055 // P(i,j) = X * P(i-1,j) + (1-X) * P(i-1,j+1)
1057 for (k = 0; k < Dimension; k++)
1058 pole[k] = X * pole[k - Dimension] + Y * pole[k + Dimension];
1063 //=======================================================================
1064 //function : AntiBoorScheme
1066 //=======================================================================
1068 Standard_Boolean BSplCLib::AntiBoorScheme(const Standard_Real U,
1069 const Standard_Integer Degree,
1070 Standard_Real& Knots,
1071 const Standard_Integer Dimension,
1072 Standard_Real& Poles,
1073 const Standard_Integer Depth,
1074 const Standard_Integer Length,
1075 const Standard_Real Tolerance)
1077 // do the Boor scheme reverted.
1079 Standard_Integer i,k,step, half_length;
1080 Standard_Real *knots = &Knots;
1081 Standard_Real z,X,Y,*pole, *firstpole = &Poles + (Depth-1) * Dimension;
1083 // Test the special case length = 1
1084 // only verification of the central point
1087 X = (knots[Degree] - U) / (knots[Degree] - knots[0]);
1090 for (k = 0; k < Dimension; k++) {
1091 z = X * firstpole[k] + Y * firstpole[k+2*Dimension];
1092 if (Abs(z - firstpole[k+Dimension]) > Tolerance)
1093 return Standard_False;
1095 return Standard_True;
1099 // the steps of recursion
1101 for (step = Depth-1; step >= 0; step--) {
1102 firstpole -= Dimension;
1105 // first step from left to right
1107 for (i = step; i < Length-1; i++) {
1108 pole += 2 * Dimension;
1110 X = (knots[i+Degree-step] - U) / (knots[i+Degree-step] - knots[i]);
1113 for (k = 0; k < Dimension; k++)
1114 pole[k+Dimension] = (pole[k] - X*pole[k-Dimension]) / Y;
1118 // second step from right to left
1119 pole += 4* Dimension;
1120 half_length = (Length - 1 + step) / 2 ;
1122 // only do half of the way from right to left
1123 // otherwise it start degenerating because of
1127 for (i = Length-1; i > half_length ; i--) {
1128 pole -= 2 * Dimension;
1131 X = (knots[i+Degree-step] - U) / (knots[i+Degree-step] - knots[i]);
1134 for (k = 0; k < Dimension; k++) {
1135 z = (pole[k] - Y * pole[k+Dimension]) / X;
1136 if (Abs(z-pole[k-Dimension]) > Tolerance)
1137 return Standard_False;
1138 pole[k-Dimension] += z;
1139 pole[k-Dimension] /= 2.;
1143 return Standard_True;
1146 //=======================================================================
1147 //function : Derivative
1149 //=======================================================================
1151 void BSplCLib::Derivative(const Standard_Integer Degree,
1152 Standard_Real& Knots,
1153 const Standard_Integer Dimension,
1154 const Standard_Integer Length,
1155 const Standard_Integer Order,
1156 Standard_Real& Poles)
1158 Standard_Integer i,k,step,span = Degree;
1159 Standard_Real *knot = &Knots;
1161 for (step = 1; step <= Order; step++) {
1162 Standard_Real* pole = &Poles;
1164 for (i = step; i < Length; i++) {
1165 Standard_Real coef = - span / (knot[i+span] - knot[i]);
1167 for (k = 0; k < Dimension; k++) {
1168 pole[k] -= pole[k+Dimension];
1177 //=======================================================================
1180 //=======================================================================
1182 void BSplCLib::Bohm(const Standard_Real U,
1183 const Standard_Integer Degree,
1184 const Standard_Integer N,
1185 Standard_Real& Knots,
1186 const Standard_Integer Dimension,
1187 Standard_Real& Poles)
1189 // First phase independant of U, compute the poles of the derivatives
1190 Standard_Integer i,j,iDim,min,Dmi,DDmi,jDmi,Degm1;
1191 Standard_Real *knot = &Knots, *pole, coef, *tbis, *psav, *psDD, *psDDmDim;
1193 if (N < Degree) min = N;
1196 DDmi = (Degree << 1) + 1;
1197 switch (Dimension) {
1199 psDD = psav + Degree;
1200 psDDmDim = psDD - 1;
1202 for (i = 0; i < Degree; i++) {
1208 for (j = Degm1; j >= i; j--) {
1211 *pole = (knot[jDmi] == knot[j]) ? 0.0 : *pole / (knot[jDmi] - knot[j]);
1216 // Second phase, dependant of U
1219 for (i = 0; i < Degree; i++) {
1225 for (j = i; j >= 0; j--) {
1226 *pole += coef * (*tbis);
1231 // multiply by the degrees
1236 for (i = 1; i <= min; i++) {
1237 *pole *= coef; pole++;
1244 psDD = psav + (Degree << 1);
1245 psDDmDim = psDD - 2;
1247 for (i = 0; i < Degree; i++) {
1253 for (j = Degm1; j >= i; j--) {
1255 coef = (knot[jDmi] == knot[j]) ? 0.0 : 1. / (knot[jDmi] - knot[j]);
1256 *pole -= *tbis; *pole *= coef; pole++; tbis++;
1257 *pole -= *tbis; *pole *= coef;
1262 // Second phase, dependant of U
1265 for (i = 0; i < Degree; i++) {
1271 for (j = i; j >= 0; j--) {
1272 *pole += coef * (*tbis); pole++; tbis++;
1273 *pole += coef * (*tbis);
1278 // multiply by the degrees
1283 for (i = 1; i <= min; i++) {
1284 *pole *= coef; pole++;
1285 *pole *= coef; pole++;
1292 psDD = psav + (Degree << 1) + Degree;
1293 psDDmDim = psDD - 3;
1295 for (i = 0; i < Degree; i++) {
1301 for (j = Degm1; j >= i; j--) {
1303 coef = (knot[jDmi] == knot[j]) ? 0.0 : 1. / (knot[jDmi] - knot[j]);
1304 *pole -= *tbis; *pole *= coef; pole++; tbis++;
1305 *pole -= *tbis; *pole *= coef; pole++; tbis++;
1306 *pole -= *tbis; *pole *= coef;
1311 // Second phase, dependant of U
1314 for (i = 0; i < Degree; i++) {
1320 for (j = i; j >= 0; j--) {
1321 *pole += coef * (*tbis); pole++; tbis++;
1322 *pole += coef * (*tbis); pole++; tbis++;
1323 *pole += coef * (*tbis);
1328 // multiply by the degrees
1333 for (i = 1; i <= min; i++) {
1334 *pole *= coef; pole++;
1335 *pole *= coef; pole++;
1336 *pole *= coef; pole++;
1343 psDD = psav + (Degree << 2);
1344 psDDmDim = psDD - 4;
1346 for (i = 0; i < Degree; i++) {
1352 for (j = Degm1; j >= i; j--) {
1354 coef = (knot[jDmi] == knot[j]) ? 0.0 : 1. /(knot[jDmi] - knot[j]) ;
1355 *pole -= *tbis; *pole *= coef; pole++; tbis++;
1356 *pole -= *tbis; *pole *= coef; pole++; tbis++;
1357 *pole -= *tbis; *pole *= coef; pole++; tbis++;
1358 *pole -= *tbis; *pole *= coef;
1363 // Second phase, dependant of U
1366 for (i = 0; i < Degree; i++) {
1372 for (j = i; j >= 0; j--) {
1373 *pole += coef * (*tbis); pole++; tbis++;
1374 *pole += coef * (*tbis); pole++; tbis++;
1375 *pole += coef * (*tbis); pole++; tbis++;
1376 *pole += coef * (*tbis);
1381 // multiply by the degrees
1386 for (i = 1; i <= min; i++) {
1387 *pole *= coef; pole++;
1388 *pole *= coef; pole++;
1389 *pole *= coef; pole++;
1390 *pole *= coef; pole++;
1398 Standard_Integer Dim2 = Dimension << 1;
1399 psDD = psav + Degree * Dimension;
1400 psDDmDim = psDD - Dimension;
1402 for (i = 0; i < Degree; i++) {
1408 for (j = Degm1; j >= i; j--) {
1410 coef = (knot[jDmi] == knot[j]) ? 0.0 : 1. / (knot[jDmi] - knot[j]);
1412 for (k = 0; k < Dimension; k++) {
1413 *pole -= *tbis; *pole *= coef; pole++; tbis++;
1419 // Second phase, dependant of U
1422 for (i = 0; i < Degree; i++) {
1425 tbis = pole + Dimension;
1428 for (j = i; j >= 0; j--) {
1430 for (k = 0; k < Dimension; k++) {
1431 *pole += coef * (*tbis); pole++; tbis++;
1437 // multiply by the degrees
1440 pole = psav + Dimension;
1442 for (i = 1; i <= min; i++) {
1444 for (k = 0; k < Dimension; k++) {
1445 *pole *= coef; pole++;
1454 //=======================================================================
1455 //function : BuildKnots
1457 //=======================================================================
1459 void BSplCLib::BuildKnots(const Standard_Integer Degree,
1460 const Standard_Integer Index,
1461 const Standard_Boolean Periodic,
1462 const TColStd_Array1OfReal& Knots,
1463 const TColStd_Array1OfInteger* Mults,
1466 Standard_Integer KLower = Knots.Lower();
1467 const Standard_Real * pkn = &Knots(KLower);
1469 Standard_Real *knot = &LK;
1470 if (Mults == NULL) {
1473 Standard_Integer j = Index ;
1474 knot[0] = pkn[j]; j++;
1479 Standard_Integer j = Index - 1;
1480 knot[0] = pkn[j]; j++;
1481 knot[1] = pkn[j]; j++;
1482 knot[2] = pkn[j]; j++;
1487 Standard_Integer j = Index - 2;
1488 knot[0] = pkn[j]; j++;
1489 knot[1] = pkn[j]; j++;
1490 knot[2] = pkn[j]; j++;
1491 knot[3] = pkn[j]; j++;
1492 knot[4] = pkn[j]; j++;
1497 Standard_Integer j = Index - 3;
1498 knot[0] = pkn[j]; j++;
1499 knot[1] = pkn[j]; j++;
1500 knot[2] = pkn[j]; j++;
1501 knot[3] = pkn[j]; j++;
1502 knot[4] = pkn[j]; j++;
1503 knot[5] = pkn[j]; j++;
1504 knot[6] = pkn[j]; j++;
1509 Standard_Integer j = Index - 4;
1510 knot[0] = pkn[j]; j++;
1511 knot[1] = pkn[j]; j++;
1512 knot[2] = pkn[j]; j++;
1513 knot[3] = pkn[j]; j++;
1514 knot[4] = pkn[j]; j++;
1515 knot[5] = pkn[j]; j++;
1516 knot[6] = pkn[j]; j++;
1517 knot[7] = pkn[j]; j++;
1518 knot[8] = pkn[j]; j++;
1523 Standard_Integer j = Index - 5;
1524 knot[ 0] = pkn[j]; j++;
1525 knot[ 1] = pkn[j]; j++;
1526 knot[ 2] = pkn[j]; j++;
1527 knot[ 3] = pkn[j]; j++;
1528 knot[ 4] = pkn[j]; j++;
1529 knot[ 5] = pkn[j]; j++;
1530 knot[ 6] = pkn[j]; j++;
1531 knot[ 7] = pkn[j]; j++;
1532 knot[ 8] = pkn[j]; j++;
1533 knot[ 9] = pkn[j]; j++;
1534 knot[10] = pkn[j]; j++;
1539 Standard_Integer i,j;
1540 Standard_Integer Deg2 = Degree << 1;
1543 for (i = 0; i < Deg2; i++) {
1552 Standard_Integer Deg1 = Degree - 1;
1553 Standard_Integer KUpper = Knots.Upper();
1554 Standard_Integer MLower = Mults->Lower();
1555 Standard_Integer MUpper = Mults->Upper();
1556 const Standard_Integer * pmu = &(*Mults)(MLower);
1558 Standard_Real dknot = 0;
1559 Standard_Integer ilow = Index , mlow = 0;
1560 Standard_Integer iupp = Index + 1, mupp = 0;
1561 Standard_Real loffset = 0., uoffset = 0.;
1562 Standard_Boolean getlow = Standard_True, getupp = Standard_True;
1564 dknot = pkn[KUpper] - pkn[KLower];
1565 if (iupp > MUpper) {
1570 // Find the knots around Index
1572 for (i = 0; i < Degree; i++) {
1575 if (mlow > pmu[ilow]) {
1578 getlow = (ilow >= MLower);
1579 if (Periodic && !getlow) {
1582 getlow = Standard_True;
1586 knot[Deg1 - i] = pkn[ilow] - loffset;
1590 if (mupp > pmu[iupp]) {
1593 getupp = (iupp <= MUpper);
1594 if (Periodic && !getupp) {
1597 getupp = Standard_True;
1601 knot[Degree + i] = pkn[iupp] + uoffset;
1607 //=======================================================================
1608 //function : PoleIndex
1610 //=======================================================================
1612 Standard_Integer BSplCLib::PoleIndex(const Standard_Integer Degree,
1613 const Standard_Integer Index,
1614 const Standard_Boolean Periodic,
1615 const TColStd_Array1OfInteger& Mults)
1617 Standard_Integer i, pindex = 0;
1619 for (i = Mults.Lower(); i <= Index; i++)
1622 pindex -= Mults(Mults.Lower());
1624 pindex -= Degree + 1;
1629 //=======================================================================
1630 //function : BuildBoor
1631 //purpose : builds the local array for boor
1632 //=======================================================================
1634 void BSplCLib::BuildBoor(const Standard_Integer Index,
1635 const Standard_Integer Length,
1636 const Standard_Integer Dimension,
1637 const TColStd_Array1OfReal& Poles,
1640 Standard_Real *poles = &LP;
1641 Standard_Integer i,k, ip = Poles.Lower() + Index * Dimension;
1643 for (i = 0; i < Length+1; i++) {
1645 for (k = 0; k < Dimension; k++) {
1646 poles[k] = Poles(ip);
1648 if (ip > Poles.Upper()) ip = Poles.Lower();
1650 poles += 2 * Dimension;
1654 //=======================================================================
1655 //function : BoorIndex
1657 //=======================================================================
1659 Standard_Integer BSplCLib::BoorIndex(const Standard_Integer Index,
1660 const Standard_Integer Length,
1661 const Standard_Integer Depth)
1663 if (Index <= Depth) return Index;
1664 if (Index <= Length) return 2 * Index - Depth;
1665 return Length + Index - Depth;
1668 //=======================================================================
1669 //function : GetPole
1671 //=======================================================================
1673 void BSplCLib::GetPole(const Standard_Integer Index,
1674 const Standard_Integer Length,
1675 const Standard_Integer Depth,
1676 const Standard_Integer Dimension,
1678 Standard_Integer& Position,
1679 TColStd_Array1OfReal& Pole)
1682 Standard_Real* pole = &LP + BoorIndex(Index,Length,Depth) * Dimension;
1684 for (k = 0; k < Dimension; k++) {
1685 Pole(Position) = pole[k];
1688 if (Position > Pole.Upper()) Position = Pole.Lower();
1691 //=======================================================================
1692 //function : PrepareInsertKnots
1694 //=======================================================================
1696 Standard_Boolean BSplCLib::PrepareInsertKnots
1697 (const Standard_Integer Degree,
1698 const Standard_Boolean Periodic,
1699 const TColStd_Array1OfReal& Knots,
1700 const TColStd_Array1OfInteger& Mults,
1701 const TColStd_Array1OfReal& AddKnots,
1702 const TColStd_Array1OfInteger* AddMults,
1703 Standard_Integer& NbPoles,
1704 Standard_Integer& NbKnots,
1705 const Standard_Real Tolerance,
1706 const Standard_Boolean Add)
1708 Standard_Boolean addflat = AddMults == NULL;
1710 Standard_Integer first,last;
1712 first = Knots.Lower();
1713 last = Knots.Upper();
1716 first = FirstUKnotIndex(Degree,Mults);
1717 last = LastUKnotIndex(Degree,Mults);
1719 Standard_Real adeltaK1 = Knots(first)-AddKnots(AddKnots.Lower());
1720 Standard_Real adeltaK2 = AddKnots(AddKnots.Upper())-Knots(last);
1721 if (adeltaK1 > Tolerance) return Standard_False;
1722 if (adeltaK2 > Tolerance) return Standard_False;
1724 Standard_Integer sigma = 0, mult, amult;
1726 Standard_Integer k = Knots.Lower() - 1;
1727 Standard_Integer ak = AddKnots.Lower();
1729 if(Periodic && AddKnots.Length() > 1)
1731 //gka for case when segments was produced on full period only one knot
1732 //was added in the end of curve
1733 if(fabs(adeltaK1) <= gp::Resolution() &&
1734 fabs(adeltaK2) <= gp::Resolution())
1738 Standard_Integer aLastKnotMult = Mults (Knots.Upper());
1739 Standard_Real au,oldau = AddKnots(ak),Eps;
1741 while (ak <= AddKnots.Upper()) {
1743 if (au < oldau) return Standard_False;
1746 Eps = Max(Tolerance,Epsilon(au));
1748 while ((k < Knots.Upper()) && (Knots(k+1) - au <= Eps)) {
1754 if (addflat) amult = 1;
1755 else amult = Max(0,(*AddMults)(ak));
1757 while ((ak < AddKnots.Upper()) &&
1758 (Abs(au - AddKnots(ak+1)) <= Eps)) {
1761 if (addflat) amult++;
1762 else amult += Max(0,(*AddMults)(ak));
1767 if (Abs(au - Knots(k)) <= Eps) {
1768 // identic to existing knot
1771 if (mult + amult > Degree)
1772 amult = Max(0,Degree - mult);
1776 else if (amult > mult) {
1777 if (amult > Degree) amult = Degree;
1778 if (k == Knots.Upper () && Periodic)
1780 aLastKnotMult = Max (amult, mult);
1781 sigma += 2 * (aLastKnotMult - mult);
1785 sigma += amult - mult;
1789 // on periodic curves if this is the last knot
1790 // the multiplicity is added twice to account for the first knot
1791 if (k == Knots.Upper() && Periodic) {
1795 sigma += amult - mult;
1800 // not identic to existing knot
1802 if (amult > Degree) amult = Degree;
1811 // count the last knots
1812 while (k < Knots.Upper()) {
1819 //for periodic B-Spline the requirement is that multiplicites of the first
1820 //and last knots must be equal (see Geom_BSplineCurve constructor for
1822 //respectively AddMults() must meet this requirement if AddKnots() contains
1823 //knot(s) coincident with first or last
1824 NbPoles = sigma - aLastKnotMult;
1827 NbPoles = sigma - Degree - 1;
1830 return Standard_True;
1833 //=======================================================================
1835 //purpose : copy reals from an array to an other
1837 // NbValues are copied from OldPoles(OldFirst)
1838 // to NewPoles(NewFirst)
1840 // Periodicity is handled.
1841 // OldFirst and NewFirst are updated
1842 // to the position after the last copied pole.
1844 //=======================================================================
1846 static void Copy(const Standard_Integer NbPoles,
1847 Standard_Integer& OldFirst,
1848 const TColStd_Array1OfReal& OldPoles,
1849 Standard_Integer& NewFirst,
1850 TColStd_Array1OfReal& NewPoles)
1852 // reset the index in the range for periodicity
1854 OldFirst = OldPoles.Lower() +
1855 (OldFirst - OldPoles.Lower()) % (OldPoles.Upper() - OldPoles.Lower() + 1);
1857 NewFirst = NewPoles.Lower() +
1858 (NewFirst - NewPoles.Lower()) % (NewPoles.Upper() - NewPoles.Lower() + 1);
1863 for (i = 1; i <= NbPoles; i++) {
1864 NewPoles(NewFirst) = OldPoles(OldFirst);
1866 if (OldFirst > OldPoles.Upper()) OldFirst = OldPoles.Lower();
1868 if (NewFirst > NewPoles.Upper()) NewFirst = NewPoles.Lower();
1872 //=======================================================================
1873 //function : InsertKnots
1874 //purpose : insert an array of knots and multiplicities
1875 //=======================================================================
1877 void BSplCLib::InsertKnots
1878 (const Standard_Integer Degree,
1879 const Standard_Boolean Periodic,
1880 const Standard_Integer Dimension,
1881 const TColStd_Array1OfReal& Poles,
1882 const TColStd_Array1OfReal& Knots,
1883 const TColStd_Array1OfInteger& Mults,
1884 const TColStd_Array1OfReal& AddKnots,
1885 const TColStd_Array1OfInteger* AddMults,
1886 TColStd_Array1OfReal& NewPoles,
1887 TColStd_Array1OfReal& NewKnots,
1888 TColStd_Array1OfInteger& NewMults,
1889 const Standard_Real Tolerance,
1890 const Standard_Boolean Add)
1892 Standard_Boolean addflat = AddMults == NULL;
1894 Standard_Integer i,k,mult,firstmult;
1895 Standard_Integer index,kn,curnk,curk;
1896 Standard_Integer p,np, curp, curnp, length, depth;
1898 Standard_Integer need;
1901 // -------------------
1902 // create local arrays
1903 // -------------------
1905 Standard_Real *knots = new Standard_Real[2*Degree];
1906 Standard_Real *poles = new Standard_Real[(2*Degree+1)*Dimension];
1908 //----------------------------
1909 // loop on the knots to insert
1910 //----------------------------
1912 curk = Knots.Lower()-1; // current position in Knots
1913 curnk = NewKnots.Lower()-1; // current position in NewKnots
1914 curp = Poles.Lower(); // current position in Poles
1915 curnp = NewPoles.Lower(); // current position in NewPoles
1917 // NewKnots, NewMults, NewPoles contains the current state of the curve
1919 // index is the first pole of the current curve for insertion schema
1921 if (Periodic) index = -Mults(Mults.Lower());
1922 else index = -Degree-1;
1924 // on Periodic curves the first knot and the last knot are inserted later
1925 // (they are the same knot)
1926 firstmult = 0; // multiplicity of the first-last knot for periodic
1929 // kn current knot to insert in AddKnots
1931 for (kn = AddKnots.Lower(); kn <= AddKnots.Upper(); kn++) {
1934 Eps = Max(Tolerance,Epsilon(u));
1936 //-----------------------------------
1937 // find the position in the old knots
1938 // and copy to the new knots
1939 //-----------------------------------
1941 while (curk < Knots.Upper() && Knots(curk+1) - u <= Eps) {
1943 NewKnots(curnk) = Knots(curk);
1944 index += NewMults(curnk) = Mults(curk);
1947 //-----------------------------------
1948 // Slice the knots and the mults
1949 // to the current size of the new curve
1950 //-----------------------------------
1952 i = curnk + Knots.Upper() - curk;
1953 TColStd_Array1OfReal nknots(NewKnots(NewKnots.Lower()),NewKnots.Lower(),i);
1954 TColStd_Array1OfInteger nmults(NewMults(NewMults.Lower()),NewMults.Lower(),i);
1956 //-----------------------------------
1957 // copy enough knots
1958 // to compute the insertion schema
1959 //-----------------------------------
1965 while (mult < Degree && k < Knots.Upper()) {
1967 nknots(i) = Knots(k);
1968 mult += nmults(i) = Mults(k);
1971 // copy knots at the end for periodic curve
1977 while (mult < Degree && i > curnk) {
1978 nknots(i) = Knots(k);
1979 mult += nmults(i) = Mults(k);
1983 nmults(nmults.Upper()) = nmults(nmults.Lower());
1988 //------------------------------------
1989 // do the boor scheme on the new curve
1990 // to insert the new knot
1991 //------------------------------------
1993 Standard_Boolean sameknot = (Abs(u-NewKnots(curnk)) <= Eps);
1995 if (sameknot) length = Max(0,Degree - NewMults(curnk));
1996 else length = Degree;
1998 if (addflat) depth = 1;
1999 else depth = Min(Degree,(*AddMults)(kn));
2003 if ((NewMults(curnk) + depth) > Degree)
2004 depth = Degree - NewMults(curnk);
2007 depth = Max(0,depth-NewMults(curnk));
2011 // on periodic curve the first and last knot are delayed to the end
2012 if (curk == Knots.Lower() || (curk == Knots.Upper())) {
2013 if (firstmult == 0) // do that only once
2019 if (depth <= 0) continue;
2021 BuildKnots(Degree,curnk,Periodic,nknots,&nmults,*knots);
2025 need = NewPoles.Lower()+(index+length+1)*Dimension - curnp;
2026 need = Min(need,Poles.Upper() - curp + 1);
2030 Copy(need,p,Poles,np,NewPoles);
2034 // slice the poles to the current number of poles in case of periodic
2035 TColStd_Array1OfReal npoles(NewPoles(NewPoles.Lower()),NewPoles.Lower(),curnp-1);
2037 BuildBoor(index,length,Dimension,npoles,*poles);
2038 BoorScheme(u,Degree,*knots,Dimension,*poles,depth,length);
2040 //-------------------
2041 // copy the new poles
2042 //-------------------
2044 curnp += depth * Dimension; // number of poles is increased by depth
2045 TColStd_Array1OfReal ThePoles(NewPoles(NewPoles.Lower()),NewPoles.Lower(),curnp-1);
2046 np = NewKnots.Lower()+(index+1)*Dimension;
2048 for (i = 1; i <= length + depth; i++)
2049 GetPole(i,length,depth,Dimension,*poles,np,ThePoles);
2051 //-------------------
2053 //-------------------
2057 NewMults(curnk) += depth;
2061 NewKnots(curnk) = u;
2062 NewMults(curnk) = depth;
2066 //------------------------------
2067 // copy the last poles and knots
2068 //------------------------------
2070 Copy(Poles.Upper() - curp + 1,curp,Poles,curnp,NewPoles);
2072 while (curk < Knots.Upper()) {
2074 NewKnots(curnk) = Knots(curk);
2075 NewMults(curnk) = Mults(curk);
2078 //------------------------------
2079 // process the first-last knot
2080 // on periodic curves
2081 //------------------------------
2083 if (firstmult > 0) {
2084 curnk = NewKnots.Lower();
2085 if (NewMults(curnk) + firstmult > Degree) {
2086 firstmult = Degree - NewMults(curnk);
2088 if (firstmult > 0) {
2090 length = Degree - NewMults(curnk);
2093 BuildKnots(Degree,curnk,Periodic,NewKnots,&NewMults,*knots);
2094 TColStd_Array1OfReal npoles(NewPoles(NewPoles.Lower()),
2096 NewPoles.Upper()-depth*Dimension);
2097 BuildBoor(0,length,Dimension,npoles,*poles);
2098 BoorScheme(NewKnots(curnk),Degree,*knots,Dimension,*poles,depth,length);
2100 //---------------------------
2101 // copy the new poles
2102 // but rotate them with depth
2103 //---------------------------
2105 np = NewPoles.Lower();
2107 for (i = depth; i < length + depth; i++)
2108 GetPole(i,length,depth,Dimension,*poles,np,NewPoles);
2110 np = NewPoles.Upper() - depth*Dimension + 1;
2112 for (i = 0; i < depth; i++)
2113 GetPole(i,length,depth,Dimension,*poles,np,NewPoles);
2115 NewMults(NewMults.Lower()) += depth;
2116 NewMults(NewMults.Upper()) += depth;
2119 // free local arrays
2124 //=======================================================================
2125 //function : RemoveKnot
2127 //=======================================================================
2129 Standard_Boolean BSplCLib::RemoveKnot
2130 (const Standard_Integer Index,
2131 const Standard_Integer Mult,
2132 const Standard_Integer Degree,
2133 const Standard_Boolean Periodic,
2134 const Standard_Integer Dimension,
2135 const TColStd_Array1OfReal& Poles,
2136 const TColStd_Array1OfReal& Knots,
2137 const TColStd_Array1OfInteger& Mults,
2138 TColStd_Array1OfReal& NewPoles,
2139 TColStd_Array1OfReal& NewKnots,
2140 TColStd_Array1OfInteger& NewMults,
2141 const Standard_Real Tolerance)
2143 Standard_Integer index,i,j,k,p,np;
2145 Standard_Integer TheIndex = Index;
2148 Standard_Integer first,last;
2150 first = Knots.Lower();
2151 last = Knots.Upper();
2154 first = BSplCLib::FirstUKnotIndex(Degree,Mults) + 1;
2155 last = BSplCLib::LastUKnotIndex(Degree,Mults) - 1;
2157 if (Index < first) return Standard_False;
2158 if (Index > last) return Standard_False;
2160 if ( Periodic && (Index == first)) TheIndex = last;
2162 Standard_Integer depth = Mults(TheIndex) - Mult;
2163 Standard_Integer length = Degree - Mult;
2165 // -------------------
2166 // create local arrays
2167 // -------------------
2169 Standard_Real *knots = new Standard_Real[4*Degree];
2170 Standard_Real *poles = new Standard_Real[(2*Degree+1)*Dimension];
2173 // ------------------------------------
2174 // build the knots for anti Boor Scheme
2175 // ------------------------------------
2177 // the new sequence of knots
2178 // is obtained from the knots at Index-1 and Index
2180 BSplCLib::BuildKnots(Degree,TheIndex-1,Periodic,Knots,&Mults,*knots);
2181 index = PoleIndex(Degree,TheIndex-1,Periodic,Mults);
2182 BSplCLib::BuildKnots(Degree,TheIndex,Periodic,Knots,&Mults,knots[2*Degree]);
2186 for (i = 0; i < Degree - Mult; i++)
2187 knots[i] = knots[i+Mult];
2189 for (i = Degree-Mult; i < 2*Degree; i++)
2190 knots[i] = knots[2*Degree+i];
2193 // ------------------------------------
2194 // build the poles for anti Boor Scheme
2195 // ------------------------------------
2197 p = Poles.Lower()+index * Dimension;
2199 for (i = 0; i <= length + depth; i++) {
2200 j = Dimension * BoorIndex(i,length,depth);
2202 for (k = 0; k < Dimension; k++) {
2203 poles[j+k] = Poles(p+k);
2206 if (p > Poles.Upper()) p = Poles.Lower();
2214 Standard_Boolean result = AntiBoorScheme(Knots(TheIndex),Degree,*knots,
2216 depth,length,Tolerance);
2227 np = NewPoles.Lower();
2229 // unmodified poles before
2230 Copy((index+1)*Dimension,p,Poles,np,NewPoles);
2235 for (i = 1; i <= length; i++)
2236 BSplCLib::GetPole(i,length,0,Dimension,*poles,np,NewPoles);
2237 p += (length + depth) * Dimension ;
2239 // unmodified poles after
2240 if (p != Poles.Lower()) {
2241 i = Poles.Upper() - p + 1;
2242 Copy(i,p,Poles,np,NewPoles);
2250 NewMults(TheIndex) = Mult;
2252 if (TheIndex == first) NewMults(last) = Mult;
2253 if (TheIndex == last) NewMults(first) = Mult;
2257 if (!Periodic || (TheIndex != first && TheIndex != last)) {
2259 for (i = Knots.Lower(); i < TheIndex; i++) {
2260 NewKnots(i) = Knots(i);
2261 NewMults(i) = Mults(i);
2264 for (i = TheIndex+1; i <= Knots.Upper(); i++) {
2265 NewKnots(i-1) = Knots(i);
2266 NewMults(i-1) = Mults(i);
2270 // The interesting case of a Periodic curve
2271 // where the first and last knot is removed.
2273 for (i = first; i < last-1; i++) {
2274 NewKnots(i) = Knots(i+1);
2275 NewMults(i) = Mults(i+1);
2277 NewKnots(last-1) = NewKnots(first) + Knots(last) - Knots(first);
2278 NewMults(last-1) = NewMults(first);
2284 // free local arrays
2291 //=======================================================================
2292 //function : IncreaseDegreeCountKnots
2294 //=======================================================================
2296 Standard_Integer BSplCLib::IncreaseDegreeCountKnots
2297 (const Standard_Integer Degree,
2298 const Standard_Integer NewDegree,
2299 const Standard_Boolean Periodic,
2300 const TColStd_Array1OfInteger& Mults)
2302 if (Periodic) return Mults.Length();
2303 Standard_Integer f = FirstUKnotIndex(Degree,Mults);
2304 Standard_Integer l = LastUKnotIndex(Degree,Mults);
2305 Standard_Integer m,i,removed = 0, step = NewDegree - Degree;
2308 m = Degree + (f - i + 1) * step + 1;
2310 while (m > NewDegree+1) {
2312 m -= Mults(i) + step;
2315 if (m < NewDegree+1) removed--;
2318 m = Degree + (i - l + 1) * step + 1;
2320 while (m > NewDegree+1) {
2322 m -= Mults(i) + step;
2325 if (m < NewDegree+1) removed--;
2327 return Mults.Length() - removed;
2330 //=======================================================================
2331 //function : IncreaseDegree
2333 //=======================================================================
2335 void BSplCLib::IncreaseDegree
2336 (const Standard_Integer Degree,
2337 const Standard_Integer NewDegree,
2338 const Standard_Boolean Periodic,
2339 const Standard_Integer Dimension,
2340 const TColStd_Array1OfReal& Poles,
2341 const TColStd_Array1OfReal& Knots,
2342 const TColStd_Array1OfInteger& Mults,
2343 TColStd_Array1OfReal& NewPoles,
2344 TColStd_Array1OfReal& NewKnots,
2345 TColStd_Array1OfInteger& NewMults)
2347 // Degree elevation of a BSpline Curve
2349 // This algorithms loops on degree incrementation from Degree to NewDegree.
2350 // The variable curDeg is the current degree to increment.
2352 // Before degree incrementations a "working curve" is created.
2353 // It has the same knot, poles and multiplicities.
2355 // If the curve is periodic knots are added on the working curve before
2356 // and after the existing knots to make it a non-periodic curves.
2357 // The poles are also copied.
2359 // The first and last multiplicity of the working curve are set to Degree+1,
2360 // null poles are added if necessary.
2362 // Then the degree is incremented on the working curve.
2363 // The knots are unchanged but all multiplicities will be incremented.
2365 // Each degree incrementation is achieved by averaging curDeg+1 curves.
2367 // See : Degree elevation of B-spline curves
2368 // Hartmut PRAUTZSCH
2372 //-------------------------
2373 // create the working curve
2374 //-------------------------
2376 Standard_Integer i,k,f,l,m,pf,pl,firstknot;
2378 pf = 0; // number of null poles added at beginning
2379 pl = 0; // number of null poles added at end
2381 Standard_Integer nbwknots = Knots.Length();
2382 f = FirstUKnotIndex(Degree,Mults);
2383 l = LastUKnotIndex (Degree,Mults);
2386 // Periodic curves are transformed in non-periodic curves
2388 nbwknots += f - Mults.Lower();
2392 for (i = Mults.Lower(); i <= f; i++)
2395 nbwknots += Mults.Upper() - l;
2399 for (i = l; i <= Mults.Upper(); i++)
2403 // copy the knots and multiplicities
2404 TColStd_Array1OfReal wknots(1,nbwknots);
2405 TColStd_Array1OfInteger wmults(1,nbwknots);
2411 // copy the knots for a periodic curve
2412 Standard_Real period = Knots(Knots.Upper()) - Knots(Knots.Lower());
2415 for (k = l; k < Knots.Upper(); k++) {
2417 wknots(i) = Knots(k) - period;
2418 wmults(i) = Mults(k);
2421 for (k = Knots.Lower(); k <= Knots.Upper(); k++) {
2423 wknots(i) = Knots(k);
2424 wmults(i) = Mults(k);
2427 for (k = Knots.Lower()+1; k <= f; k++) {
2429 wknots(i) = Knots(k) + period;
2430 wmults(i) = Mults(k);
2434 // set the first and last mults to Degree+1
2435 // and add null poles
2437 pf += Degree + 1 - wmults(1);
2438 wmults(1) = Degree + 1;
2439 pl += Degree + 1 - wmults(nbwknots);
2440 wmults(nbwknots) = Degree + 1;
2442 //---------------------------
2443 // poles of the working curve
2444 //---------------------------
2446 Standard_Integer nbwpoles = 0;
2448 for (i = 1; i <= nbwknots; i++) nbwpoles += wmults(i);
2449 nbwpoles -= Degree + 1;
2451 // we provide space for degree elevation
2452 TColStd_Array1OfReal
2453 wpoles(1,(nbwpoles + (nbwknots-1) * (NewDegree - Degree)) * Dimension);
2455 for (i = 1; i <= pf * Dimension; i++)
2460 for (i = pf * Dimension + 1; i <= (nbwpoles - pl) * Dimension; i++) {
2461 wpoles(i) = Poles(k);
2463 if (k > Poles.Upper()) k = Poles.Lower();
2466 for (i = (nbwpoles-pl)*Dimension+1; i <= nbwpoles*Dimension; i++)
2470 //-----------------------------------------------------------
2471 // Declare the temporary arrays used in degree incrementation
2472 //-----------------------------------------------------------
2474 Standard_Integer nbwp = nbwpoles + (nbwknots-1) * (NewDegree - Degree);
2475 // Arrays for storing the temporary curves
2476 TColStd_Array1OfReal tempc1(1,nbwp * Dimension);
2477 TColStd_Array1OfReal tempc2(1,nbwp * Dimension);
2479 // Array for storing the knots to insert
2480 TColStd_Array1OfReal iknots(1,nbwknots);
2482 // Arrays for receiving the knots after insertion
2483 TColStd_Array1OfReal nknots(1,nbwknots);
2487 //------------------------------
2488 // Loop on degree incrementation
2489 //------------------------------
2491 Standard_Integer step,curDeg;
2492 Standard_Integer nbp = nbwpoles;
2495 for (curDeg = Degree; curDeg < NewDegree; curDeg++) {
2497 nbp = nbwp; // current number of poles
2498 nbwp = nbp + nbwknots - 1; // new number of poles
2500 // For the averaging
2501 TColStd_Array1OfReal nwpoles(1,nbwp * Dimension);
2502 nwpoles.Init(0.0e0) ;
2505 for (step = 0; step <= curDeg; step++) {
2507 // Compute the bspline of rank step.
2509 // if not the first time, decrement the multiplicities back
2511 for (i = 1; i <= nbwknots; i++)
2515 // Poles are the current poles
2516 // but the poles congruent to step are duplicated.
2518 Standard_Integer offset = 0;
2520 for (i = 0; i < nbp; i++) {
2523 for (k = 0; k < Dimension; k++) {
2524 tempc1((offset-1)*Dimension+k+1) =
2525 wpoles(NewPoles.Lower()+i*Dimension+k);
2527 if (i % (curDeg+1) == step) {
2530 for (k = 0; k < Dimension; k++) {
2531 tempc1((offset-1)*Dimension+k+1) =
2532 wpoles(NewPoles.Lower()+i*Dimension+k);
2537 // Knots multiplicities are increased
2538 // For each knot where the sum of multiplicities is congruent to step
2540 Standard_Integer stepmult = step+1;
2541 Standard_Integer nbknots = 0;
2542 Standard_Integer smult = 0;
2544 for (k = 1; k <= nbwknots; k++) {
2546 if (smult >= stepmult) {
2547 // this knot is increased
2548 stepmult += curDeg+1;
2552 // this knot is inserted
2554 iknots(nbknots) = wknots(k);
2558 // the curve is obtained by inserting the knots
2559 // to raise the multiplicities
2561 // we build "slices" of the arrays to set the correct size
2563 TColStd_Array1OfReal aknots(iknots(1),1,nbknots);
2564 TColStd_Array1OfReal curve (tempc1(1),1,offset * Dimension);
2565 TColStd_Array1OfReal ncurve(tempc2(1),1,nbwp * Dimension);
2566 // InsertKnots(curDeg+1,Standard_False,Dimension,curve,wknots,wmults,
2567 // aknots,NoMults(),ncurve,nknots,wmults,Epsilon(1.));
2569 InsertKnots(curDeg+1,Standard_False,Dimension,curve,wknots,wmults,
2570 aknots,NoMults(),ncurve,nknots,wmults,0.0);
2572 // add to the average
2574 for (i = 1; i <= nbwp * Dimension; i++)
2575 nwpoles(i) += ncurve(i);
2578 // add to the average
2580 for (i = 1; i <= nbwp * Dimension; i++)
2581 nwpoles(i) += tempc1(i);
2585 // The result is the average
2587 for (i = 1; i <= nbwp * Dimension; i++) {
2588 wpoles(i) = nwpoles(i) / (curDeg+1);
2596 // index in new knots of the first knot of the curve
2598 firstknot = Mults.Upper() - l + 1;
2602 // the new curve starts at index firstknot
2603 // so we must remove knots until the sum of multiplicities
2604 // from the first to the start is NewDegree+1
2606 // m is the current sum of multiplicities
2609 for (k = 1; k <= firstknot; k++)
2612 // compute the new first knot (k), pf will be the index of the first pole
2616 while (m > NewDegree+1) {
2621 if (m < NewDegree+1) {
2623 wmults(k) += m - NewDegree - 1;
2624 pf += m - NewDegree - 1;
2627 // on a periodic curve the knots start with firstknot
2633 for (i = NewKnots.Lower(); i <= NewKnots.Upper(); i++) {
2634 NewKnots(i) = wknots(k);
2635 NewMults(i) = wmults(k);
2642 for (i = NewPoles.Lower(); i <= NewPoles.Upper(); i++) {
2644 NewPoles(i) = wpoles(pf);
2648 //=======================================================================
2649 //function : PrepareUnperiodize
2651 //=======================================================================
2653 void BSplCLib::PrepareUnperiodize
2654 (const Standard_Integer Degree,
2655 const TColStd_Array1OfInteger& Mults,
2656 Standard_Integer& NbKnots,
2657 Standard_Integer& NbPoles)
2660 // initialize NbKnots and NbPoles
2661 NbKnots = Mults.Length();
2662 NbPoles = - Degree - 1;
2664 for (i = Mults.Lower(); i <= Mults.Upper(); i++)
2665 NbPoles += Mults(i);
2667 Standard_Integer sigma, k;
2668 // Add knots at the beginning of the curve to raise Multiplicities
2670 sigma = Mults(Mults.Lower());
2671 k = Mults.Upper() - 1;
2673 while ( sigma < Degree + 1) {
2675 NbPoles += Mults(k);
2679 // We must add exactly until Degree + 1 ->
2680 // Supress the excedent.
2681 if ( sigma > Degree + 1)
2682 NbPoles -= sigma - Degree - 1;
2684 // Add knots at the end of the curve to raise Multiplicities
2686 sigma = Mults(Mults.Upper());
2687 k = Mults.Lower() + 1;
2689 while ( sigma < Degree + 1) {
2691 NbPoles += Mults(k);
2695 // We must add exactly until Degree + 1 ->
2696 // Supress the excedent.
2697 if ( sigma > Degree + 1)
2698 NbPoles -= sigma - Degree - 1;
2701 //=======================================================================
2702 //function : Unperiodize
2704 //=======================================================================
2706 void BSplCLib::Unperiodize
2707 (const Standard_Integer Degree,
2708 const Standard_Integer , // Dimension,
2709 const TColStd_Array1OfInteger& Mults,
2710 const TColStd_Array1OfReal& Knots,
2711 const TColStd_Array1OfReal& Poles,
2712 TColStd_Array1OfInteger& NewMults,
2713 TColStd_Array1OfReal& NewKnots,
2714 TColStd_Array1OfReal& NewPoles)
2716 Standard_Integer sigma, k, index = 0;
2717 // evaluation of index : number of knots to insert before knot(1) to
2718 // raise sum of multiplicities to <Degree + 1>
2719 sigma = Mults(Mults.Lower());
2720 k = Mults.Upper() - 1;
2722 while ( sigma < Degree + 1) {
2728 Standard_Real period = Knots(Knots.Upper()) - Knots(Knots.Lower());
2730 // set the 'interior' knots;
2732 for ( k = 1; k <= Knots.Length(); k++) {
2733 NewKnots ( k + index ) = Knots( k);
2734 NewMults ( k + index ) = Mults( k);
2737 // set the 'starting' knots;
2739 for ( k = 1; k <= index; k++) {
2740 NewKnots( k) = NewKnots( k + Knots.Length() - 1) - period;
2741 NewMults( k) = NewMults( k + Knots.Length() - 1);
2743 NewMults( 1) -= sigma - Degree -1;
2745 // set the 'ending' knots;
2746 sigma = NewMults( index + Knots.Length() );
2748 for ( k = Knots.Length() + index + 1; k <= NewKnots.Length(); k++) {
2749 NewKnots( k) = NewKnots( k - Knots.Length() + 1) + period;
2750 NewMults( k) = NewMults( k - Knots.Length() + 1);
2751 sigma += NewMults( k - Knots.Length() + 1);
2753 NewMults(NewMults.Length()) -= sigma - Degree - 1;
2755 for ( k = 1 ; k <= NewPoles.Length(); k++) {
2756 NewPoles(k ) = Poles( (k-1) % Poles.Length() + 1);
2760 //=======================================================================
2761 //function : PrepareTrimming
2763 //=======================================================================
2765 void BSplCLib::PrepareTrimming(const Standard_Integer Degree,
2766 const Standard_Boolean Periodic,
2767 const TColStd_Array1OfReal& Knots,
2768 const TColStd_Array1OfInteger& Mults,
2769 const Standard_Real U1,
2770 const Standard_Real U2,
2771 Standard_Integer& NbKnots,
2772 Standard_Integer& NbPoles)
2775 Standard_Real NewU1, NewU2;
2776 Standard_Integer index1 = 0, index2 = 0;
2778 // Eval index1, index2 : position of U1 and U2 in the Array Knots
2779 // such as Knots(index1-1) <= U1 < Knots(index1)
2780 // Knots(index2-1) <= U2 < Knots(index2)
2781 LocateParameter( Degree, Knots, Mults, U1, Periodic,
2782 Knots.Lower(), Knots.Upper(), index1, NewU1);
2783 LocateParameter( Degree, Knots, Mults, U2, Periodic,
2784 Knots.Lower(), Knots.Upper(), index2, NewU2);
2786 if ( Abs(Knots(index2) - U2) <= Epsilon( U1))
2790 NbKnots = index2 - index1 + 3;
2793 NbPoles = Degree + 1;
2795 for ( i = index1; i <= index2; i++)
2796 NbPoles += Mults(i);
2799 //=======================================================================
2800 //function : Trimming
2802 //=======================================================================
2804 void BSplCLib::Trimming(const Standard_Integer Degree,
2805 const Standard_Boolean Periodic,
2806 const Standard_Integer Dimension,
2807 const TColStd_Array1OfReal& Knots,
2808 const TColStd_Array1OfInteger& Mults,
2809 const TColStd_Array1OfReal& Poles,
2810 const Standard_Real U1,
2811 const Standard_Real U2,
2812 TColStd_Array1OfReal& NewKnots,
2813 TColStd_Array1OfInteger& NewMults,
2814 TColStd_Array1OfReal& NewPoles)
2816 Standard_Integer i, nbpoles=0, nbknots=0;
2817 Standard_Real kk[2];
2818 Standard_Integer mm[2];
2819 TColStd_Array1OfReal K( kk[0], 1, 2 );
2820 TColStd_Array1OfInteger M( mm[0], 1, 2 );
2822 K(1) = U1; K(2) = U2;
2823 mm[0] = mm[1] = Degree;
2824 if (!PrepareInsertKnots( Degree, Periodic, Knots, Mults, K, &M,
2825 nbpoles, nbknots, Epsilon( U1), 0))
2826 throw Standard_OutOfRange();
2828 TColStd_Array1OfReal TempPoles(1, nbpoles*Dimension);
2829 TColStd_Array1OfReal TempKnots(1, nbknots);
2830 TColStd_Array1OfInteger TempMults(1, nbknots);
2833 // do not allow the multiplicities to Add : they must be less than Degree
2835 InsertKnots(Degree, Periodic, Dimension, Poles, Knots, Mults,
2836 K, &M, TempPoles, TempKnots, TempMults, Epsilon(U1),
2839 // find in TempPoles the index of the pole corresponding to U1
2840 Standard_Integer Kindex = 0, Pindex;
2841 Standard_Real NewU1;
2842 LocateParameter( Degree, TempKnots, TempMults, U1, Periodic,
2843 TempKnots.Lower(), TempKnots.Upper(), Kindex, NewU1);
2844 Pindex = PoleIndex ( Degree, Kindex, Periodic, TempMults);
2845 Pindex *= Dimension;
2847 for ( i = 1; i <= NewPoles.Length(); i++) NewPoles(i) = TempPoles(Pindex + i);
2849 for ( i = 1; i <= NewKnots.Length(); i++) {
2850 NewKnots(i) = TempKnots( Kindex+i-1);
2851 NewMults(i) = TempMults( Kindex+i-1);
2853 NewMults(1) = Min(Degree, NewMults(1)) + 1 ;
2854 NewMults(NewMults.Length())= Min(Degree, NewMults(NewMults.Length())) + 1 ;
2857 //=======================================================================
2858 //function : Solves a LU factored Matrix
2860 //=======================================================================
2863 BSplCLib::SolveBandedSystem(const math_Matrix& Matrix,
2864 const Standard_Integer UpperBandWidth,
2865 const Standard_Integer LowerBandWidth,
2866 const Standard_Integer ArrayDimension,
2867 Standard_Real& Array)
2869 Standard_Integer ii,
2876 Standard_Real *PolesArray = &Array ;
2877 Standard_Real Inverse ;
2880 if (Matrix.LowerCol() != 1 ||
2881 Matrix.UpperCol() != UpperBandWidth + LowerBandWidth + 1) {
2886 for (ii = Matrix.LowerRow() + 1; ii <= Matrix.UpperRow() ; ii++) {
2887 MinIndex = (ii - LowerBandWidth >= Matrix.LowerRow() ?
2888 ii - LowerBandWidth : Matrix.LowerRow()) ;
2890 for ( jj = MinIndex ; jj < ii ; jj++) {
2892 for (kk = 0 ; kk < ArrayDimension ; kk++) {
2893 PolesArray[(ii-1) * ArrayDimension + kk] +=
2894 PolesArray[(jj-1) * ArrayDimension + kk] * Matrix(ii, jj - ii + LowerBandWidth + 1) ;
2899 for (ii = Matrix.UpperRow() ; ii >= Matrix.LowerRow() ; ii--) {
2900 MaxIndex = (ii + UpperBandWidth <= Matrix.UpperRow() ?
2901 ii + UpperBandWidth : Matrix.UpperRow()) ;
2903 for (jj = MaxIndex ; jj > ii ; jj--) {
2905 for (kk = 0 ; kk < ArrayDimension ; kk++) {
2906 PolesArray[(ii-1) * ArrayDimension + kk] -=
2907 PolesArray[(jj - 1) * ArrayDimension + kk] *
2908 Matrix(ii, jj - ii + LowerBandWidth + 1) ;
2912 //fixing a bug PRO18577 to avoid divizion by zero
2914 Standard_Real divizor = Matrix(ii,LowerBandWidth + 1) ;
2915 Standard_Real Toler = 1.0e-16;
2916 if ( Abs(divizor) > Toler )
2917 Inverse = 1.0e0 / divizor ;
2920 // cout << " BSplCLib::SolveBandedSystem() : zero determinant " << endl;
2925 for (kk = 0 ; kk < ArrayDimension ; kk++) {
2926 PolesArray[(ii-1) * ArrayDimension + kk] *= Inverse ;
2930 return (ReturnCode) ;
2933 //=======================================================================
2934 //function : Solves a LU factored Matrix
2936 //=======================================================================
2939 BSplCLib::SolveBandedSystem(const math_Matrix& Matrix,
2940 const Standard_Integer UpperBandWidth,
2941 const Standard_Integer LowerBandWidth,
2942 const Standard_Boolean HomogeneousFlag,
2943 const Standard_Integer ArrayDimension,
2944 Standard_Real& Poles,
2945 Standard_Real& Weights)
2947 Standard_Integer ii,
2952 Standard_Real Inverse,
2953 *PolesArray = &Poles,
2954 *WeightsArray = &Weights ;
2956 if (Matrix.LowerCol() != 1 ||
2957 Matrix.UpperCol() != UpperBandWidth + LowerBandWidth + 1) {
2961 if (HomogeneousFlag == Standard_False) {
2963 for (ii = 0 ; ii < Matrix.UpperRow() - Matrix.LowerRow() + 1; ii++) {
2965 for (kk = 0 ; kk < ArrayDimension ; kk++) {
2966 PolesArray[ii * ArrayDimension + kk] *=
2972 BSplCLib::SolveBandedSystem(Matrix,
2977 if (ErrorCode != 0) {
2982 BSplCLib::SolveBandedSystem(Matrix,
2987 if (ErrorCode != 0) {
2991 if (HomogeneousFlag == Standard_False) {
2993 for (ii = 0 ; ii < Matrix.UpperRow() - Matrix.LowerRow() + 1 ; ii++) {
2994 Inverse = 1.0e0 / WeightsArray[ii] ;
2996 for (kk = 0 ; kk < ArrayDimension ; kk++) {
2997 PolesArray[ii * ArrayDimension + kk] *= Inverse ;
3001 FINISH : return (ReturnCode) ;
3004 //=======================================================================
3005 //function : BuildSchoenbergPoints
3007 //=======================================================================
3009 void BSplCLib::BuildSchoenbergPoints(const Standard_Integer Degree,
3010 const TColStd_Array1OfReal& FlatKnots,
3011 TColStd_Array1OfReal& Parameters)
3013 Standard_Integer ii,
3015 Standard_Real Inverse ;
3016 Inverse = 1.0e0 / (Standard_Real)Degree ;
3018 for (ii = Parameters.Lower() ; ii <= Parameters.Upper() ; ii++) {
3019 Parameters(ii) = 0.0e0 ;
3021 for (jj = 1 ; jj <= Degree ; jj++) {
3022 Parameters(ii) += FlatKnots(jj + ii) ;
3024 Parameters(ii) *= Inverse ;
3028 //=======================================================================
3029 //function : Interpolate
3031 //=======================================================================
3033 void BSplCLib::Interpolate(const Standard_Integer Degree,
3034 const TColStd_Array1OfReal& FlatKnots,
3035 const TColStd_Array1OfReal& Parameters,
3036 const TColStd_Array1OfInteger& ContactOrderArray,
3037 const Standard_Integer ArrayDimension,
3038 Standard_Real& Poles,
3039 Standard_Integer& InversionProblem)
3041 Standard_Integer ErrorCode,
3044 // Standard_Real *PolesArray = &Poles ;
3045 math_Matrix InterpolationMatrix(1, Parameters.Length(),
3046 1, 2 * Degree + 1) ;
3048 BSplCLib::BuildBSpMatrix(Parameters,
3052 InterpolationMatrix,
3056 throw Standard_OutOfRange("BSplCLib::Interpolate");
3059 BSplCLib::FactorBandedMatrix(InterpolationMatrix,
3064 throw Standard_OutOfRange("BSplCLib::Interpolate");
3067 BSplCLib::SolveBandedSystem(InterpolationMatrix,
3073 throw Standard_OutOfRange("BSplCLib::Interpolate");
3076 //=======================================================================
3077 //function : Interpolate
3079 //=======================================================================
3081 void BSplCLib::Interpolate(const Standard_Integer Degree,
3082 const TColStd_Array1OfReal& FlatKnots,
3083 const TColStd_Array1OfReal& Parameters,
3084 const TColStd_Array1OfInteger& ContactOrderArray,
3085 const Standard_Integer ArrayDimension,
3086 Standard_Real& Poles,
3087 Standard_Real& Weights,
3088 Standard_Integer& InversionProblem)
3090 Standard_Integer ErrorCode,
3094 math_Matrix InterpolationMatrix(1, Parameters.Length(),
3095 1, 2 * Degree + 1) ;
3097 BSplCLib::BuildBSpMatrix(Parameters,
3101 InterpolationMatrix,
3105 throw Standard_OutOfRange("BSplCLib::Interpolate");
3108 BSplCLib::FactorBandedMatrix(InterpolationMatrix,
3113 throw Standard_OutOfRange("BSplCLib::Interpolate");
3116 BSplCLib::SolveBandedSystem(InterpolationMatrix,
3124 throw Standard_OutOfRange("BSplCLib::Interpolate");
3127 //=======================================================================
3128 //function : Evaluates a Bspline function : uses the ExtrapMode
3129 //purpose : the function is extrapolated using the Taylor expansion
3130 // of degree ExtrapMode[0] to the left and the Taylor
3131 // expansion of degree ExtrapMode[1] to the right
3132 // this evaluates the numerator by multiplying by the weights
3133 // and evaluating it but does not call RationalDerivatives after
3134 //=======================================================================
3137 (const Standard_Real Parameter,
3138 const Standard_Boolean PeriodicFlag,
3139 const Standard_Integer DerivativeRequest,
3140 Standard_Integer& ExtrapMode,
3141 const Standard_Integer Degree,
3142 const TColStd_Array1OfReal& FlatKnots,
3143 const Standard_Integer ArrayDimension,
3144 Standard_Real& Poles,
3145 Standard_Real& Weights,
3146 Standard_Real& PolesResults,
3147 Standard_Real& WeightsResults)
3149 Standard_Integer ii,
3158 ExtrapolatingFlag[2],
3161 FirstNonZeroBsplineIndex,
3162 LocalRequest = DerivativeRequest ;
3163 Standard_Real *PResultArray,
3171 PolesArray = &Poles ;
3172 WeightsArray = &Weights ;
3173 ExtrapModeArray = &ExtrapMode ;
3174 PResultArray = &PolesResults ;
3175 WResultArray = &WeightsResults ;
3176 LocalParameter = Parameter ;
3177 ExtrapolatingFlag[0] =
3178 ExtrapolatingFlag[1] = 0 ;
3180 // check if we are extrapolating to a degree which is smaller than
3181 // the degree of the Bspline
3184 Period = FlatKnots(FlatKnots.Upper() - 1) - FlatKnots(2) ;
3186 while (LocalParameter > FlatKnots(FlatKnots.Upper() - 1)) {
3187 LocalParameter -= Period ;
3190 while (LocalParameter < FlatKnots(2)) {
3191 LocalParameter += Period ;
3194 if (Parameter < FlatKnots(2) &&
3195 LocalRequest < ExtrapModeArray[0] &&
3196 ExtrapModeArray[0] < Degree) {
3197 LocalRequest = ExtrapModeArray[0] ;
3198 LocalParameter = FlatKnots(2) ;
3199 ExtrapolatingFlag[0] = 1 ;
3201 if (Parameter > FlatKnots(FlatKnots.Upper()-1) &&
3202 LocalRequest < ExtrapModeArray[1] &&
3203 ExtrapModeArray[1] < Degree) {
3204 LocalRequest = ExtrapModeArray[1] ;
3205 LocalParameter = FlatKnots(FlatKnots.Upper()-1) ;
3206 ExtrapolatingFlag[1] = 1 ;
3208 Delta = Parameter - LocalParameter ;
3209 if (LocalRequest >= Order) {
3210 LocalRequest = Degree ;
3213 Modulus = FlatKnots.Length() - Degree -1 ;
3216 Modulus = FlatKnots.Length() - Degree ;
3219 BSplCLib_LocalMatrix BsplineBasis (LocalRequest, Order);
3221 BSplCLib::EvalBsplineBasis(LocalRequest,
3225 FirstNonZeroBsplineIndex,
3227 if (ErrorCode != 0) {
3230 if (ExtrapolatingFlag[0] == 0 && ExtrapolatingFlag[1] == 0) {
3234 for (ii = 1 ; ii <= LocalRequest + 1 ; ii++) {
3235 Index1 = FirstNonZeroBsplineIndex ;
3237 for (kk = 0 ; kk < ArrayDimension ; kk++) {
3238 PResultArray[Index + kk] = 0.0e0 ;
3240 WResultArray[Index] = 0.0e0 ;
3242 for (jj = 1 ; jj <= Order ; jj++) {
3244 for (kk = 0 ; kk < ArrayDimension ; kk++) {
3245 PResultArray[Index + kk] +=
3246 PolesArray[(Index1-1) * ArrayDimension + kk]
3247 * WeightsArray[Index1-1] * BsplineBasis(ii,jj) ;
3249 WResultArray[Index2] += WeightsArray[Index1-1] * BsplineBasis(ii,jj) ;
3251 Index1 = Index1 % Modulus ;
3254 Index += ArrayDimension ;
3260 // store Taylor expansion in LocalRealArray
3262 NewRequest = DerivativeRequest ;
3263 if (NewRequest > Degree) {
3264 NewRequest = Degree ;
3266 NCollection_LocalArray<Standard_Real> LocalRealArray((LocalRequest + 1)*ArrayDimension);
3270 for (ii = 1 ; ii <= LocalRequest + 1 ; ii++) {
3271 Index1 = FirstNonZeroBsplineIndex ;
3273 for (kk = 0 ; kk < ArrayDimension ; kk++) {
3274 LocalRealArray[Index + kk] = 0.0e0 ;
3277 for (jj = 1 ; jj <= Order ; jj++) {
3279 for (kk = 0 ; kk < ArrayDimension ; kk++) {
3280 LocalRealArray[Index + kk] +=
3281 PolesArray[(Index1-1)*ArrayDimension + kk] *
3282 WeightsArray[Index1-1] * BsplineBasis(ii,jj) ;
3284 Index1 = Index1 % Modulus ;
3288 for (kk = 0 ; kk < ArrayDimension ; kk++) {
3289 LocalRealArray[Index + kk] *= Inverse ;
3291 Index += ArrayDimension ;
3292 Inverse /= (Standard_Real) ii ;
3294 PLib::EvalPolynomial(Delta,
3303 for (ii = 1 ; ii <= LocalRequest + 1 ; ii++) {
3304 Index1 = FirstNonZeroBsplineIndex ;
3305 LocalRealArray[Index] = 0.0e0 ;
3307 for (jj = 1 ; jj <= Order ; jj++) {
3308 LocalRealArray[Index] +=
3309 WeightsArray[Index1-1] * BsplineBasis(ii,jj) ;
3310 Index1 = Index1 % Modulus ;
3313 LocalRealArray[Index + kk] *= Inverse ;
3315 Inverse /= (Standard_Real) ii ;
3317 PLib::EvalPolynomial(Delta,
3327 //=======================================================================
3328 //function : Evaluates a Bspline function : uses the ExtrapMode
3329 //purpose : the function is extrapolated using the Taylor expansion
3330 // of degree ExtrapMode[0] to the left and the Taylor
3331 // expansion of degree ExtrapMode[1] to the right
3332 // WARNING : the array Results is supposed to have at least
3333 // (DerivativeRequest + 1) * ArrayDimension slots and the
3335 //=======================================================================
3338 (const Standard_Real Parameter,
3339 const Standard_Boolean PeriodicFlag,
3340 const Standard_Integer DerivativeRequest,
3341 Standard_Integer& ExtrapMode,
3342 const Standard_Integer Degree,
3343 const TColStd_Array1OfReal& FlatKnots,
3344 const Standard_Integer ArrayDimension,
3345 Standard_Real& Poles,
3346 Standard_Real& Results)
3348 Standard_Integer ii,
3356 ExtrapolatingFlag[2],
3359 FirstNonZeroBsplineIndex,
3360 LocalRequest = DerivativeRequest ;
3362 Standard_Real *ResultArray,
3369 PolesArray = &Poles ;
3370 ExtrapModeArray = &ExtrapMode ;
3371 ResultArray = &Results ;
3372 LocalParameter = Parameter ;
3373 ExtrapolatingFlag[0] =
3374 ExtrapolatingFlag[1] = 0 ;
3376 // check if we are extrapolating to a degree which is smaller than
3377 // the degree of the Bspline
3380 Period = FlatKnots(FlatKnots.Upper() - 1) - FlatKnots(2) ;
3382 while (LocalParameter > FlatKnots(FlatKnots.Upper() - 1)) {
3383 LocalParameter -= Period ;
3386 while (LocalParameter < FlatKnots(2)) {
3387 LocalParameter += Period ;
3390 if (Parameter < FlatKnots(2) &&
3391 LocalRequest < ExtrapModeArray[0] &&
3392 ExtrapModeArray[0] < Degree) {
3393 LocalRequest = ExtrapModeArray[0] ;
3394 LocalParameter = FlatKnots(2) ;
3395 ExtrapolatingFlag[0] = 1 ;
3397 if (Parameter > FlatKnots(FlatKnots.Upper()-1) &&
3398 LocalRequest < ExtrapModeArray[1] &&
3399 ExtrapModeArray[1] < Degree) {
3400 LocalRequest = ExtrapModeArray[1] ;
3401 LocalParameter = FlatKnots(FlatKnots.Upper()-1) ;
3402 ExtrapolatingFlag[1] = 1 ;
3404 Delta = Parameter - LocalParameter ;
3405 if (LocalRequest >= Order) {
3406 LocalRequest = Degree ;
3410 Modulus = FlatKnots.Length() - Degree -1 ;
3413 Modulus = FlatKnots.Length() - Degree ;
3416 BSplCLib_LocalMatrix BsplineBasis (LocalRequest, Order);
3419 BSplCLib::EvalBsplineBasis(LocalRequest,
3423 FirstNonZeroBsplineIndex,
3425 if (ErrorCode != 0) {
3428 if (ExtrapolatingFlag[0] == 0 && ExtrapolatingFlag[1] == 0) {
3431 for (ii = 1 ; ii <= LocalRequest + 1 ; ii++) {
3432 Index1 = FirstNonZeroBsplineIndex ;
3434 for (kk = 0 ; kk < ArrayDimension ; kk++) {
3435 ResultArray[Index + kk] = 0.0e0 ;
3438 for (jj = 1 ; jj <= Order ; jj++) {
3440 for (kk = 0 ; kk < ArrayDimension ; kk++) {
3441 ResultArray[Index + kk] +=
3442 PolesArray[(Index1-1) * ArrayDimension + kk] * BsplineBasis(ii,jj) ;
3444 Index1 = Index1 % Modulus ;
3447 Index += ArrayDimension ;
3452 // store Taylor expansion in LocalRealArray
3454 NewRequest = DerivativeRequest ;
3455 if (NewRequest > Degree) {
3456 NewRequest = Degree ;
3458 NCollection_LocalArray<Standard_Real> LocalRealArray((LocalRequest + 1)*ArrayDimension);
3463 for (ii = 1 ; ii <= LocalRequest + 1 ; ii++) {
3464 Index1 = FirstNonZeroBsplineIndex ;
3466 for (kk = 0 ; kk < ArrayDimension ; kk++) {
3467 LocalRealArray[Index + kk] = 0.0e0 ;
3470 for (jj = 1 ; jj <= Order ; jj++) {
3472 for (kk = 0 ; kk < ArrayDimension ; kk++) {
3473 LocalRealArray[Index + kk] +=
3474 PolesArray[(Index1-1)*ArrayDimension + kk] * BsplineBasis(ii,jj) ;
3476 Index1 = Index1 % Modulus ;
3480 for (kk = 0 ; kk < ArrayDimension ; kk++) {
3481 LocalRealArray[Index + kk] *= Inverse ;
3483 Index += ArrayDimension ;
3484 Inverse /= (Standard_Real) ii ;
3486 PLib::EvalPolynomial(Delta,
3496 //=======================================================================
3497 //function : TangExtendToConstraint
3498 //purpose : Extends a Bspline function using the tangency map
3502 //=======================================================================
3504 void BSplCLib::TangExtendToConstraint
3505 (const TColStd_Array1OfReal& FlatKnots,
3506 const Standard_Real C1Coefficient,
3507 const Standard_Integer NumPoles,
3508 Standard_Real& Poles,
3509 const Standard_Integer CDimension,
3510 const Standard_Integer CDegree,
3511 const TColStd_Array1OfReal& ConstraintPoint,
3512 const Standard_Integer Continuity,
3513 const Standard_Boolean After,
3514 Standard_Integer& NbPolesResult,
3515 Standard_Integer& NbKnotsResult,
3516 Standard_Real& KnotsResult,
3517 Standard_Real& PolesResult)
3520 if (CDegree<Continuity+1) {
3521 cout<<"The BSpline degree must be greater than the order of continuity"<<endl;
3524 Standard_Real * Padr = &Poles ;
3525 Standard_Real * KRadr = &KnotsResult ;
3526 Standard_Real * PRadr = &PolesResult ;
3528 ////////////////////////////////////////////////////////////////////////
3530 // 1. calculation of extension nD
3532 ////////////////////////////////////////////////////////////////////////
3535 Standard_Integer Csize = Continuity + 2;
3536 math_Matrix MatCoefs(1,Csize, 1,Csize);
3538 PLib::HermiteCoefficients(0, 1, // Limits
3539 Continuity, 0, // Orders of constraints
3543 PLib::HermiteCoefficients(0, 1, // Limits
3544 0, Continuity, // Orders of constraints
3549 // position at the node of connection
3550 Standard_Real Tbord ;
3552 Tbord = FlatKnots(FlatKnots.Upper()-CDegree);
3555 Tbord = FlatKnots(FlatKnots.Lower()+CDegree);
3557 Standard_Boolean periodic_flag = Standard_False ;
3558 Standard_Integer ipos, extrap_mode[2], derivative_request = Max(Continuity,1);
3559 extrap_mode[0] = extrap_mode[1] = CDegree;
3560 TColStd_Array1OfReal EvalBS(1, CDimension * (derivative_request+1)) ;
3561 Standard_Real * Eadr = (Standard_Real *) &EvalBS(1) ;
3562 BSplCLib::Eval(Tbord,periodic_flag,derivative_request,extrap_mode[0],
3563 CDegree,FlatKnots,CDimension,Poles,*Eadr);
3565 // norm of the tangent at the node of connection
3566 math_Vector Tgte(1,CDimension);
3568 for (ipos=1;ipos<=CDimension;ipos++) {
3569 Tgte(ipos) = EvalBS(ipos+CDimension);
3571 Standard_Real L1=Tgte.Norm();
3574 // matrix of constraints
3575 math_Matrix Contraintes(1,Csize,1,CDimension);
3578 for (ipos=1;ipos<=CDimension;ipos++) {
3579 Contraintes(1,ipos) = EvalBS(ipos);
3580 Contraintes(2,ipos) = C1Coefficient * EvalBS(ipos+CDimension);
3581 if(Continuity >= 2) Contraintes(3,ipos) = EvalBS(ipos+2*CDimension) * Pow(C1Coefficient,2);
3582 if(Continuity >= 3) Contraintes(4,ipos) = EvalBS(ipos+3*CDimension) * Pow(C1Coefficient,3);
3583 Contraintes(Continuity+2,ipos) = ConstraintPoint(ipos);
3588 for (ipos=1;ipos<=CDimension;ipos++) {
3589 Contraintes(1,ipos) = ConstraintPoint(ipos);
3590 Contraintes(2,ipos) = EvalBS(ipos);
3591 if(Continuity >= 1) Contraintes(3,ipos) = C1Coefficient * EvalBS(ipos+CDimension);
3592 if(Continuity >= 2) Contraintes(4,ipos) = EvalBS(ipos+2*CDimension) * Pow(C1Coefficient,2);
3593 if(Continuity >= 3) Contraintes(5,ipos) = EvalBS(ipos+3*CDimension) * Pow(C1Coefficient,3);
3597 // calculate the coefficients of extension
3598 Standard_Integer ii, jj, kk;
3599 TColStd_Array1OfReal ExtraCoeffs(1,Csize*CDimension);
3600 ExtraCoeffs.Init(0.);
3602 for (ii=1; ii<=Csize; ii++) {
3604 for (jj=1; jj<=Csize; jj++) {
3606 for (kk=1; kk<=CDimension; kk++) {
3607 ExtraCoeffs(kk+(jj-1)*CDimension) += MatCoefs(ii,jj)*Contraintes(ii,kk);
3612 // calculate the poles of extension
3613 TColStd_Array1OfReal ExtrapPoles(1,Csize*CDimension);
3614 Standard_Real * EPadr = &ExtrapPoles(1) ;
3615 PLib::CoefficientsPoles(CDimension,
3616 ExtraCoeffs, PLib::NoWeights(),
3617 ExtrapPoles, PLib::NoWeights());
3619 // calculate the nodes of extension with multiplicities
3620 TColStd_Array1OfReal ExtrapNoeuds(1,2);
3621 ExtrapNoeuds(1) = 0.;
3622 ExtrapNoeuds(2) = 1.;
3623 TColStd_Array1OfInteger ExtrapMults(1,2);
3624 ExtrapMults(1) = Csize;
3625 ExtrapMults(2) = Csize;
3627 // flat nodes of extension
3628 TColStd_Array1OfReal FK2(1, Csize*2);
3629 BSplCLib::KnotSequence(ExtrapNoeuds,ExtrapMults,FK2);
3631 // norm of the tangent at the connection point
3633 BSplCLib::Eval(0.,periodic_flag,1,extrap_mode[0],
3634 Csize-1,FK2,CDimension,*EPadr,*Eadr);
3637 BSplCLib::Eval(1.,periodic_flag,1,extrap_mode[0],
3638 Csize-1,FK2,CDimension,*EPadr,*Eadr);
3641 for (ipos=1;ipos<=CDimension;ipos++) {
3642 Tgte(ipos) = EvalBS(ipos+CDimension);
3644 Standard_Real L2 = Tgte.Norm();
3646 // harmonisation of degrees
3647 TColStd_Array1OfReal NewP2(1, (CDegree+1)*CDimension);
3648 TColStd_Array1OfReal NewK2(1, 2);
3649 TColStd_Array1OfInteger NewM2(1, 2);
3650 if (Csize-1<CDegree) {
3651 BSplCLib::IncreaseDegree(Csize-1,CDegree,Standard_False,CDimension,
3652 ExtrapPoles,ExtrapNoeuds,ExtrapMults,
3656 NewP2 = ExtrapPoles;
3657 NewK2 = ExtrapNoeuds;
3658 NewM2 = ExtrapMults;
3661 // flat nodes of extension after harmonization of degrees
3662 TColStd_Array1OfReal NewFK2(1, (CDegree+1)*2);
3663 BSplCLib::KnotSequence(NewK2,NewM2,NewFK2);
3666 ////////////////////////////////////////////////////////////////////////
3668 // 2. concatenation C0
3670 ////////////////////////////////////////////////////////////////////////
3672 // ratio of reparametrization
3673 Standard_Real Ratio=1, Delta;
3674 if ( (L1 > Precision::Confusion()) && (L2 > Precision::Confusion()) ) {
3677 if ( (Ratio < 1.e-5) || (Ratio > 1.e5) ) Ratio = 1;
3680 // do not touch the first BSpline
3681 Delta = Ratio*NewFK2(NewFK2.Lower()) - FlatKnots(FlatKnots.Upper());
3684 // do not touch the second BSpline
3685 Delta = Ratio*NewFK2(NewFK2.Upper()) - FlatKnots(FlatKnots.Lower());
3688 // result of the concatenation
3689 Standard_Integer NbP1 = NumPoles, NbP2 = CDegree+1;
3690 Standard_Integer NbK1 = FlatKnots.Length(), NbK2 = 2*(CDegree+1);
3691 TColStd_Array1OfReal NewPoles (1, (NbP1+ NbP2-1)*CDimension);
3692 TColStd_Array1OfReal NewFlats (1, NbK1+NbK2-CDegree-2);
3695 Standard_Integer indNP, indP, indEP;
3698 for (ii=1; ii<=NbP1+NbP2-1; ii++) {
3700 for (jj=1; jj<=CDimension; jj++) {
3701 indNP = (ii-1)*CDimension+jj;
3702 indP = (ii-1)*CDimension+jj-1;
3703 indEP = (ii-NbP1)*CDimension+jj;
3704 if (ii<NbP1) NewPoles(indNP) = Padr[indP];
3705 else NewPoles(indNP) = NewP2(indEP);
3711 for (ii=1; ii<=NbP1+NbP2-1; ii++) {
3713 for (jj=1; jj<=CDimension; jj++) {
3714 indNP = (ii-1)*CDimension+jj;
3715 indEP = (ii-1)*CDimension+jj;
3716 indP = (ii-NbP2)*CDimension+jj-1;
3717 if (ii<NbP2) NewPoles(indNP) = NewP2(indEP);
3718 else NewPoles(indNP) = Padr[indP];
3725 // start with the nodes of the initial surface
3727 for (ii=1; ii<NbK1; ii++) {
3728 NewFlats(ii) = FlatKnots(FlatKnots.Lower()+ii-1);
3730 // continue with the reparameterized nodes of the extension
3732 for (ii=1; ii<=NbK2-CDegree-1; ii++) {
3733 NewFlats(NbK1+ii-1) = Ratio*NewFK2(NewFK2.Lower()+ii+CDegree) - Delta;
3737 // start with the reparameterized nodes of the extension
3739 for (ii=1; ii<NbK2-CDegree; ii++) {
3740 NewFlats(ii) = Ratio*NewFK2(NewFK2.Lower()+ii-1) - Delta;
3742 // continue with the nodes of the initial surface
3744 for (ii=2; ii<=NbK1; ii++) {
3745 NewFlats(NbK2+ii-CDegree-2) = FlatKnots(FlatKnots.Lower()+ii-1);
3750 ////////////////////////////////////////////////////////////////////////
3752 // 3. reduction of multiplicite at the node of connection
3754 ////////////////////////////////////////////////////////////////////////
3756 // number of separate nodes
3757 Standard_Integer KLength = 1;
3759 for (ii=2; ii<=NbK1+NbK2-CDegree-2;ii++) {
3760 if (NewFlats(ii) != NewFlats(ii-1)) KLength++;
3763 // flat nodes --> nodes + multiplicities
3764 TColStd_Array1OfReal NewKnots (1, KLength);
3765 TColStd_Array1OfInteger NewMults (1, KLength);
3768 NewKnots(jj) = NewFlats(1);
3770 for (ii=2; ii<=NbK1+NbK2-CDegree-2;ii++) {
3771 if (NewFlats(ii) == NewFlats(ii-1)) NewMults(jj)++;
3774 NewKnots(jj) = NewFlats(ii);
3778 // reduction of multiplicity at the second or the last but one node
3779 Standard_Integer Index = 2, M = CDegree;
3780 if (After) Index = KLength-1;
3781 TColStd_Array1OfReal ResultPoles (1, (NbP1+ NbP2-1)*CDimension);
3782 TColStd_Array1OfReal ResultKnots (1, KLength);
3783 TColStd_Array1OfInteger ResultMults (1, KLength);
3784 Standard_Real Tol = 1.e-6;
3785 Standard_Boolean Ok = Standard_True;
3787 while ( (M>CDegree-Continuity) && Ok) {
3788 Ok = RemoveKnot(Index, M-1, CDegree, Standard_False, CDimension,
3789 NewPoles, NewKnots, NewMults,
3790 ResultPoles, ResultKnots, ResultMults, Tol);
3795 // number of poles of the concatenation
3796 NbPolesResult = NbP1 + NbP2 - 1;
3797 // the poles of the concatenation
3798 Standard_Integer PLength = NbPolesResult*CDimension;
3800 for (jj=1; jj<=PLength; jj++) {
3801 PRadr[jj-1] = NewPoles(jj);
3804 // flat nodes of the concatenation
3805 Standard_Integer ideb = 0;
3807 for (jj=0; jj<NewKnots.Length(); jj++) {
3808 for (ii=0; ii<NewMults(jj+1); ii++) {
3809 KRadr[ideb+ii] = NewKnots(jj+1);
3811 ideb += NewMults(jj+1);
3813 NbKnotsResult = ideb;
3817 // number of poles of the result
3818 NbPolesResult = NbP1 + NbP2 - 1 - CDegree + M;
3819 // the poles of the result
3820 Standard_Integer PLength = NbPolesResult*CDimension;
3822 for (jj=0; jj<PLength; jj++) {
3823 PRadr[jj] = ResultPoles(jj+1);
3826 // flat nodes of the result
3827 Standard_Integer ideb = 0;
3829 for (jj=0; jj<ResultKnots.Length(); jj++) {
3830 for (ii=0; ii<ResultMults(jj+1); ii++) {
3831 KRadr[ideb+ii] = ResultKnots(jj+1);
3833 ideb += ResultMults(jj+1);
3835 NbKnotsResult = ideb;
3839 //=======================================================================
3840 //function : Resolution
3843 // Let C(t) = SUM Ci Bi(t) a Bspline curve of degree d
3845 // with nodes tj for j = 1,n+d+1
3849 // Then C (t) = SUM d * --------- Bi (t)
3850 // i = 2,n ti+d - ti
3853 // for the base of BSpline Bi (t) of degree d-1.
3855 // Consequently the upper bound of the norm of the derivative from C is :
3859 // d * Max | --------- |
3860 // i = 2,n | ti+d - ti |
3863 // In the rational case set C(t) = -----
3867 // D(t) = SUM Di Bi(t)
3870 // N(t) = SUM Di * Ci Bi(t)
3873 // N'(t) - D'(t) C(t)
3874 // C'(t) = -----------------------
3878 // N'(t) - D'(t) C(t) =
3880 // Di * (Ci - C(t)) - Di-1 * (Ci-1 - C(t)) d-1
3881 // SUM d * ---------------------------------------- * Bi (t) =
3885 // Di * (Ci - Cj) - Di-1 * (Ci-1 - Cj) d-1
3886 // SUM SUM d * ----------------------------------- * Betaj(t) * Bi (t)
3887 //i=2,n j=1,n ti+d - ti
3892 // Betaj(t) = --------
3895 // Betaj(t) form a partition >= 0 of the entity with support
3896 // tj, tj+d+1. Consequently if Rj = {j-d, ...., j+d+d+1}
3897 // obtain an upper bound of the derivative of C by taking :
3904 // Di * (Ci - Cj) - Di-1 * (Ci-1 - Cj)
3905 // Max Max d * -----------------------------------
3906 // j=1,n i dans Rj ti+d - ti
3908 // --------------------------------------------------------
3914 //=======================================================================
3916 void BSplCLib::Resolution( Standard_Real& Poles,
3917 const Standard_Integer ArrayDimension,
3918 const Standard_Integer NumPoles,
3919 const TColStd_Array1OfReal* Weights,
3920 const TColStd_Array1OfReal& FlatKnots,
3921 const Standard_Integer Degree,
3922 const Standard_Real Tolerance3D,
3923 Standard_Real& UTolerance)
3925 Standard_Integer ii,num_poles,ii_index,jj_index,ii_inDim;
3926 Standard_Integer lower,upper,ii_minus,jj,ii_miDim;
3927 Standard_Integer Deg1 = Degree + 1;
3928 Standard_Integer Deg2 = (Degree << 1) + 1;
3929 Standard_Real value,factor,W,min_weights,inverse;
3930 Standard_Real pa_ii_inDim_0, pa_ii_inDim_1, pa_ii_inDim_2, pa_ii_inDim_3;
3931 Standard_Real pa_ii_miDim_0, pa_ii_miDim_1, pa_ii_miDim_2, pa_ii_miDim_3;
3932 Standard_Real wg_ii_index, wg_ii_minus;
3933 Standard_Real *PA,max_derivative;
3934 const Standard_Real * FK = &FlatKnots(FlatKnots.Lower());
3936 max_derivative = 0.0e0;
3937 num_poles = FlatKnots.Length() - Deg1;
3938 switch (ArrayDimension) {
3940 if (Weights != NULL) {
3941 const Standard_Real * WG = &(*Weights)(Weights->Lower());
3942 min_weights = WG[0];
3944 for (ii = 1 ; ii < NumPoles ; ii++) {
3946 if (W < min_weights) min_weights = W;
3949 for (ii = 1 ; ii < num_poles ; ii++) {
3950 ii_index = ii % NumPoles;
3951 ii_inDim = ii_index << 1;
3952 ii_minus = (ii - 1) % NumPoles;
3953 ii_miDim = ii_minus << 1;
3954 pa_ii_inDim_0 = PA[ii_inDim]; ii_inDim++;
3955 pa_ii_inDim_1 = PA[ii_inDim];
3956 pa_ii_miDim_0 = PA[ii_miDim]; ii_miDim++;
3957 pa_ii_miDim_1 = PA[ii_miDim];
3958 wg_ii_index = WG[ii_index];
3959 wg_ii_minus = WG[ii_minus];
3960 inverse = FK[ii + Degree] - FK[ii];
3961 inverse = 1.0e0 / inverse;
3963 if (lower < 0) lower = 0;
3965 if (upper > num_poles) upper = num_poles;
3967 for (jj = lower ; jj < upper ; jj++) {
3968 jj_index = jj % NumPoles;
3969 jj_index = jj_index << 1;
3971 factor = (((PA[jj_index] - pa_ii_inDim_0) * wg_ii_index) -
3972 ((PA[jj_index] - pa_ii_miDim_0) * wg_ii_minus));
3973 if (factor < 0) factor = - factor;
3974 value += factor; jj_index++;
3975 factor = (((PA[jj_index] - pa_ii_inDim_1) * wg_ii_index) -
3976 ((PA[jj_index] - pa_ii_miDim_1) * wg_ii_minus));
3977 if (factor < 0) factor = - factor;
3980 if (max_derivative < value) max_derivative = value;
3983 max_derivative /= min_weights;
3987 for (ii = 1 ; ii < num_poles ; ii++) {
3988 ii_index = ii % NumPoles;
3989 ii_index = ii_index << 1;
3990 ii_minus = (ii - 1) % NumPoles;
3991 ii_minus = ii_minus << 1;
3992 inverse = FK[ii + Degree] - FK[ii];
3993 inverse = 1.0e0 / inverse;
3995 factor = PA[ii_index] - PA[ii_minus];
3996 if (factor < 0) factor = - factor;
3997 value += factor; ii_index++; ii_minus++;
3998 factor = PA[ii_index] - PA[ii_minus];
3999 if (factor < 0) factor = - factor;
4002 if (max_derivative < value) max_derivative = value;
4008 if (Weights != NULL) {
4009 const Standard_Real * WG = &(*Weights)(Weights->Lower());
4010 min_weights = WG[0];
4012 for (ii = 1 ; ii < NumPoles ; ii++) {
4014 if (W < min_weights) min_weights = W;
4017 for (ii = 1 ; ii < num_poles ; ii++) {
4018 ii_index = ii % NumPoles;
4019 ii_inDim = (ii_index << 1) + ii_index;
4020 ii_minus = (ii - 1) % NumPoles;
4021 ii_miDim = (ii_minus << 1) + ii_minus;
4022 pa_ii_inDim_0 = PA[ii_inDim]; ii_inDim++;
4023 pa_ii_inDim_1 = PA[ii_inDim]; ii_inDim++;
4024 pa_ii_inDim_2 = PA[ii_inDim];
4025 pa_ii_miDim_0 = PA[ii_miDim]; ii_miDim++;
4026 pa_ii_miDim_1 = PA[ii_miDim]; ii_miDim++;
4027 pa_ii_miDim_2 = PA[ii_miDim];
4028 wg_ii_index = WG[ii_index];
4029 wg_ii_minus = WG[ii_minus];
4030 inverse = FK[ii + Degree] - FK[ii];
4031 inverse = 1.0e0 / inverse;
4033 if (lower < 0) lower = 0;
4035 if (upper > num_poles) upper = num_poles;
4037 for (jj = lower ; jj < upper ; jj++) {
4038 jj_index = jj % NumPoles;
4039 jj_index = (jj_index << 1) + jj_index;
4041 factor = (((PA[jj_index] - pa_ii_inDim_0) * wg_ii_index) -
4042 ((PA[jj_index] - pa_ii_miDim_0) * wg_ii_minus));
4043 if (factor < 0) factor = - factor;
4044 value += factor; jj_index++;
4045 factor = (((PA[jj_index] - pa_ii_inDim_1) * wg_ii_index) -
4046 ((PA[jj_index] - pa_ii_miDim_1) * wg_ii_minus));
4047 if (factor < 0) factor = - factor;
4048 value += factor; jj_index++;
4049 factor = (((PA[jj_index] - pa_ii_inDim_2) * wg_ii_index) -
4050 ((PA[jj_index] - pa_ii_miDim_2) * wg_ii_minus));
4051 if (factor < 0) factor = - factor;
4054 if (max_derivative < value) max_derivative = value;
4057 max_derivative /= min_weights;
4061 for (ii = 1 ; ii < num_poles ; ii++) {
4062 ii_index = ii % NumPoles;
4063 ii_index = (ii_index << 1) + ii_index;
4064 ii_minus = (ii - 1) % NumPoles;
4065 ii_minus = (ii_minus << 1) + ii_minus;
4066 inverse = FK[ii + Degree] - FK[ii];
4067 inverse = 1.0e0 / inverse;
4069 factor = PA[ii_index] - PA[ii_minus];
4070 if (factor < 0) factor = - factor;
4071 value += factor; ii_index++; ii_minus++;
4072 factor = PA[ii_index] - PA[ii_minus];
4073 if (factor < 0) factor = - factor;
4074 value += factor; ii_index++; ii_minus++;
4075 factor = PA[ii_index] - PA[ii_minus];
4076 if (factor < 0) factor = - factor;
4079 if (max_derivative < value) max_derivative = value;
4085 if (Weights != NULL) {
4086 const Standard_Real * WG = &(*Weights)(Weights->Lower());
4087 min_weights = WG[0];
4089 for (ii = 1 ; ii < NumPoles ; ii++) {
4091 if (W < min_weights) min_weights = W;
4094 for (ii = 1 ; ii < num_poles ; ii++) {
4095 ii_index = ii % NumPoles;
4096 ii_inDim = ii_index << 2;
4097 ii_minus = (ii - 1) % NumPoles;
4098 ii_miDim = ii_minus << 2;
4099 pa_ii_inDim_0 = PA[ii_inDim]; ii_inDim++;
4100 pa_ii_inDim_1 = PA[ii_inDim]; ii_inDim++;
4101 pa_ii_inDim_2 = PA[ii_inDim]; ii_inDim++;
4102 pa_ii_inDim_3 = PA[ii_inDim];
4103 pa_ii_miDim_0 = PA[ii_miDim]; ii_miDim++;
4104 pa_ii_miDim_1 = PA[ii_miDim]; ii_miDim++;
4105 pa_ii_miDim_2 = PA[ii_miDim]; ii_miDim++;
4106 pa_ii_miDim_3 = PA[ii_miDim];
4107 wg_ii_index = WG[ii_index];
4108 wg_ii_minus = WG[ii_minus];
4109 inverse = FK[ii + Degree] - FK[ii];
4110 inverse = 1.0e0 / inverse;
4112 if (lower < 0) lower = 0;
4114 if (upper > num_poles) upper = num_poles;
4116 for (jj = lower ; jj < upper ; jj++) {
4117 jj_index = jj % NumPoles;
4118 jj_index = jj_index << 2;
4120 factor = (((PA[jj_index] - pa_ii_inDim_0) * wg_ii_index) -
4121 ((PA[jj_index] - pa_ii_miDim_0) * wg_ii_minus));
4122 if (factor < 0) factor = - factor;
4123 value += factor; jj_index++;
4124 factor = (((PA[jj_index] - pa_ii_inDim_1) * wg_ii_index) -
4125 ((PA[jj_index] - pa_ii_miDim_1) * wg_ii_minus));
4126 if (factor < 0) factor = - factor;
4127 value += factor; jj_index++;
4128 factor = (((PA[jj_index] - pa_ii_inDim_2) * wg_ii_index) -
4129 ((PA[jj_index] - pa_ii_miDim_2) * wg_ii_minus));
4130 if (factor < 0) factor = - factor;
4131 value += factor; jj_index++;
4132 factor = (((PA[jj_index] - pa_ii_inDim_3) * wg_ii_index) -
4133 ((PA[jj_index] - pa_ii_miDim_3) * wg_ii_minus));
4134 if (factor < 0) factor = - factor;
4137 if (max_derivative < value) max_derivative = value;
4140 max_derivative /= min_weights;
4144 for (ii = 1 ; ii < num_poles ; ii++) {
4145 ii_index = ii % NumPoles;
4146 ii_index = ii_index << 2;
4147 ii_minus = (ii - 1) % NumPoles;
4148 ii_minus = ii_minus << 2;
4149 inverse = FK[ii + Degree] - FK[ii];
4150 inverse = 1.0e0 / inverse;
4152 factor = PA[ii_index] - PA[ii_minus];
4153 if (factor < 0) factor = - factor;
4154 value += factor; ii_index++; ii_minus++;
4155 factor = PA[ii_index] - PA[ii_minus];
4156 if (factor < 0) factor = - factor;
4157 value += factor; ii_index++; ii_minus++;
4158 factor = PA[ii_index] - PA[ii_minus];
4159 if (factor < 0) factor = - factor;
4160 value += factor; ii_index++; ii_minus++;
4161 factor = PA[ii_index] - PA[ii_minus];
4162 if (factor < 0) factor = - factor;
4165 if (max_derivative < value) max_derivative = value;
4171 Standard_Integer kk;
4172 if (Weights != NULL) {
4173 const Standard_Real * WG = &(*Weights)(Weights->Lower());
4174 min_weights = WG[0];
4176 for (ii = 1 ; ii < NumPoles ; ii++) {
4178 if (W < min_weights) min_weights = W;
4181 for (ii = 1 ; ii < num_poles ; ii++) {
4182 ii_index = ii % NumPoles;
4183 ii_inDim = ii_index * ArrayDimension;
4184 ii_minus = (ii - 1) % NumPoles;
4185 ii_miDim = ii_minus * ArrayDimension;
4186 wg_ii_index = WG[ii_index];
4187 wg_ii_minus = WG[ii_minus];
4188 inverse = FK[ii + Degree] - FK[ii];
4189 inverse = 1.0e0 / inverse;
4191 if (lower < 0) lower = 0;
4193 if (upper > num_poles) upper = num_poles;
4195 for (jj = lower ; jj < upper ; jj++) {
4196 jj_index = jj % NumPoles;
4197 jj_index *= ArrayDimension;
4200 for (kk = 0 ; kk < ArrayDimension ; kk++) {
4201 factor = (((PA[jj_index + kk] - PA[ii_inDim + kk]) * wg_ii_index) -
4202 ((PA[jj_index + kk] - PA[ii_miDim + kk]) * wg_ii_minus));
4203 if (factor < 0) factor = - factor;
4207 if (max_derivative < value) max_derivative = value;
4210 max_derivative /= min_weights;
4214 for (ii = 1 ; ii < num_poles ; ii++) {
4215 ii_index = ii % NumPoles;
4216 ii_index *= ArrayDimension;
4217 ii_minus = (ii - 1) % NumPoles;
4218 ii_minus *= ArrayDimension;
4219 inverse = FK[ii + Degree] - FK[ii];
4220 inverse = 1.0e0 / inverse;
4223 for (kk = 0 ; kk < ArrayDimension ; kk++) {
4224 factor = PA[ii_index + kk] - PA[ii_minus + kk];
4225 if (factor < 0) factor = - factor;
4229 if (max_derivative < value) max_derivative = value;
4234 max_derivative *= Degree;
4235 if (max_derivative > RealSmall())
4236 UTolerance = Tolerance3D / max_derivative;
4238 UTolerance = Tolerance3D / RealSmall();
4241 //=======================================================================
4242 // function: FlatBezierKnots
4244 //=======================================================================
4246 // array of flat knots for bezier curve of maximum 25 degree
4247 static const Standard_Real knots[52] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4248 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
4249 const Standard_Real& BSplCLib::FlatBezierKnots (const Standard_Integer Degree)
4251 Standard_OutOfRange_Raise_if (Degree < 1 || Degree > MaxDegree() || MaxDegree() != 25,
4252 "Bezier curve degree greater than maximal supported");
4254 return knots[25-Degree];