1 // Created on: 1991-08-09
3 // Copyright (c) 1991-1999 Matra Datavision
4 // Copyright (c) 1999-2012 OPEN CASCADE SAS
6 // The content of this file is subject to the Open CASCADE Technology Public
7 // License Version 6.5 (the "License"). You may not use the content of this file
8 // except in compliance with the License. Please obtain a copy of the License
9 // at http://www.opencascade.org and read it completely before using this file.
11 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
14 // The Original Code and all software distributed under the License is
15 // distributed on an "AS IS" basis, without warranty of any kind, and the
16 // Initial Developer hereby disclaims all such warranties, including without
17 // limitation, any warranties of merchantability, fitness for a particular
18 // purpose or non-infringement. Please see the License for the specific terms
19 // and conditions governing the rights and limitations under the License.
22 // Modified RLE 9 Sep 1993
23 // pmn : modified 28-01-97 : fixed a mistake in LocateParameter (PRO6973)
24 // pmn : modified 4-11-96 : fixed a mistake in BuildKnots (PRO6124)
25 // pmn : modified 28-Jun-96 : fixed a mistake in AntiBoorScheme
26 // xab : modified 15-Jun-95 : fixed a mistake in IsRational
27 // xab : modified 15-Mar-95 : removed Epsilon comparison in IsRational
28 // added RationalDerivatives.
29 // xab : 30-Mar-95 : fixed coupling with lti in RationalDerivatives
30 // xab : 15-Mar-96 : fixed a typo in Eval with extrapolation
31 // jct : 15-Apr-97 : added TangExtendToConstraint
32 // jct : 24-Apr-97 : correction on computation of Tbord and NewFlatKnots
33 // in TangExtendToConstraint; Continuity can be equal to 0
35 #include <BSplCLib.ixx>
37 #include <PLib_LocalArray.hxx>
38 #include <Precision.hxx>
39 #include <Standard_NotImplemented.hxx>
43 typedef TColgp_Array1OfPnt Array1OfPnt;
44 typedef TColStd_Array1OfReal Array1OfReal;
45 typedef TColStd_Array1OfInteger Array1OfInteger;
47 //=======================================================================
48 //class : BSplCLib_LocalMatrix
49 //purpose: Auxiliary class optimizing creation of matrix buffer for
50 // evaluation of bspline (using stack allocation for main matrix)
51 //=======================================================================
53 class BSplCLib_LocalMatrix : public math_Matrix
56 BSplCLib_LocalMatrix (Standard_Integer DerivativeRequest, Standard_Integer Order)
57 : math_Matrix (myBuffer, 1, DerivativeRequest + 1, 1, Order)
59 Standard_OutOfRange_Raise_if (DerivativeRequest > BSplCLib::MaxDegree() ||
60 Order > BSplCLib::MaxDegree()+1 || BSplCLib::MaxDegree() > 25,
61 "BSplCLib: bspline degree is greater than maximum supported");
65 // local buffer, to be sufficient for addressing by index [Degree+1][Degree+1]
66 // (see math_Matrix implementation)
67 Standard_Real myBuffer[27*27];
70 typedef PLib_LocalArray BSplCLib_LocalArray;
72 //=======================================================================
75 //=======================================================================
77 void BSplCLib::Hunt (const Array1OfReal& XX,
78 const Standard_Real X,
79 Standard_Integer& Ilc)
81 // replaced by simple dichotomy (RLE)
83 const Standard_Real *px = &XX(Ilc);
90 Standard_Integer Ihi = XX.Upper();
97 while (Ihi - Ilc != 1) {
98 Im = (Ihi + Ilc) >> 1;
99 if (X > px[Im]) Ilc = Im;
104 //=======================================================================
105 //function : FirstUKnotIndex
107 //=======================================================================
109 Standard_Integer BSplCLib::FirstUKnotIndex (const Standard_Integer Degree,
110 const TColStd_Array1OfInteger& Mults)
112 Standard_Integer Index = Mults.Lower();
113 Standard_Integer SigmaMult = Mults(Index);
115 while (SigmaMult <= Degree) {
117 SigmaMult += Mults (Index);
122 //=======================================================================
123 //function : LastUKnotIndex
125 //=======================================================================
127 Standard_Integer BSplCLib::LastUKnotIndex (const Standard_Integer Degree,
128 const Array1OfInteger& Mults)
130 Standard_Integer Index = Mults.Upper();
131 Standard_Integer SigmaMult = Mults(Index);
133 while (SigmaMult <= Degree) {
135 SigmaMult += Mults.Value (Index);
140 //=======================================================================
141 //function : FlatIndex
143 //=======================================================================
145 Standard_Integer BSplCLib::FlatIndex
146 (const Standard_Integer Degree,
147 const Standard_Integer Index,
148 const TColStd_Array1OfInteger& Mults,
149 const Standard_Boolean Periodic)
151 Standard_Integer i, index = Index;
152 const Standard_Integer MLower = Mults.Lower();
153 const Standard_Integer *pmu = &Mults(MLower);
156 for (i = MLower + 1; i <= Index; i++)
161 index += pmu[MLower] - 1;
165 //=======================================================================
166 //function : LocateParameter
167 //purpose : Processing of nodes with multiplicities
168 //pmn 28-01-97 -> compute eventual of the period.
169 //=======================================================================
171 void BSplCLib::LocateParameter
172 (const Standard_Integer , //Degree,
173 const Array1OfReal& Knots,
174 const Array1OfInteger& , //Mults,
175 const Standard_Real U,
176 const Standard_Boolean IsPeriodic,
177 const Standard_Integer FromK1,
178 const Standard_Integer ToK2,
179 Standard_Integer& KnotIndex,
182 Standard_Real uf = 0, ul=1;
184 uf = Knots(Knots.Lower());
185 ul = Knots(Knots.Upper());
187 BSplCLib::LocateParameter(Knots,U,IsPeriodic,FromK1,ToK2,
188 KnotIndex,NewU, uf, ul);
191 //=======================================================================
192 //function : LocateParameter
193 //purpose : For plane nodes
194 // pmn 28-01-97 -> There is a need of the degre to calculate
195 // the eventual period
196 //=======================================================================
198 void BSplCLib::LocateParameter
199 (const Standard_Integer Degree,
200 const Array1OfReal& Knots,
201 const Standard_Real U,
202 const Standard_Boolean IsPeriodic,
203 const Standard_Integer FromK1,
204 const Standard_Integer ToK2,
205 Standard_Integer& KnotIndex,
209 BSplCLib::LocateParameter(Knots, U, IsPeriodic, FromK1, ToK2,
211 Knots(Knots.Lower() + Degree),
212 Knots(Knots.Upper() - Degree));
214 BSplCLib::LocateParameter(Knots, U, IsPeriodic, FromK1, ToK2,
220 //=======================================================================
221 //function : LocateParameter
222 //purpose : Effective computation
223 // pmn 28-01-97 : Add limits of the period as input argument,
224 // as it is imposible to produce them at this level.
225 //=======================================================================
227 void BSplCLib::LocateParameter
228 (const TColStd_Array1OfReal& Knots,
229 const Standard_Real U,
230 const Standard_Boolean IsPeriodic,
231 const Standard_Integer FromK1,
232 const Standard_Integer ToK2,
233 Standard_Integer& KnotIndex,
235 const Standard_Real UFirst,
236 const Standard_Real ULast)
238 Standard_Integer First,Last;
247 Standard_Integer Last1 = Last - 1;
250 Standard_Real Period = ULast - UFirst;
252 while (NewU > ULast )
255 while (NewU < UFirst)
259 BSplCLib::Hunt (Knots, NewU, KnotIndex);
261 Standard_Real Eps = Epsilon(U);
263 if (Eps < 0) Eps = - Eps;
264 Standard_Integer KLower = Knots.Lower();
265 const Standard_Real *knots = &Knots(KLower);
267 if ( KnotIndex < Knots.Upper()) {
268 val = NewU - knots[KnotIndex + 1];
269 if (val < 0) val = - val;
270 // <= to be coherent with Segment where Eps corresponds to a bit of error.
271 if (val <= Eps) KnotIndex++;
273 if (KnotIndex < First) KnotIndex = First;
274 if (KnotIndex > Last1) KnotIndex = Last1;
276 if (KnotIndex != Last1) {
277 Standard_Real K1 = knots[KnotIndex];
278 Standard_Real K2 = knots[KnotIndex + 1];
280 if (val < 0) val = - val;
285 K2 = knots[KnotIndex + 1];
287 if (val < 0) val = - val;
292 //=======================================================================
293 //function : LocateParameter
294 //purpose : the index is recomputed only if out of range
295 //pmn 28-01-97 -> eventual computation of the period.
296 //=======================================================================
298 void BSplCLib::LocateParameter
299 (const Standard_Integer Degree,
300 const TColStd_Array1OfReal& Knots,
301 const TColStd_Array1OfInteger& Mults,
302 const Standard_Real U,
303 const Standard_Boolean Periodic,
304 Standard_Integer& KnotIndex,
307 Standard_Integer first,last;
310 first = Knots.Lower();
311 last = Knots.Upper();
314 first = FirstUKnotIndex(Degree,Mults);
315 last = LastUKnotIndex (Degree,Mults);
319 first = Knots.Lower() + Degree;
320 last = Knots.Upper() - Degree;
322 if ( KnotIndex < first || KnotIndex > last)
323 BSplCLib::LocateParameter(Knots, U, Periodic, first, last,
324 KnotIndex, NewU, Knots(first), Knots(last));
329 //=======================================================================
330 //function : MaxKnotMult
332 //=======================================================================
334 Standard_Integer BSplCLib::MaxKnotMult
335 (const Array1OfInteger& Mults,
336 const Standard_Integer FromK1,
337 const Standard_Integer ToK2)
339 Standard_Integer MLower = Mults.Lower();
340 const Standard_Integer *pmu = &Mults(MLower);
342 Standard_Integer MaxMult = pmu[FromK1];
344 for (Standard_Integer i = FromK1; i <= ToK2; i++) {
345 if (MaxMult < pmu[i]) MaxMult = pmu[i];
350 //=======================================================================
351 //function : MinKnotMult
353 //=======================================================================
355 Standard_Integer BSplCLib::MinKnotMult
356 (const Array1OfInteger& Mults,
357 const Standard_Integer FromK1,
358 const Standard_Integer ToK2)
360 Standard_Integer MLower = Mults.Lower();
361 const Standard_Integer *pmu = &Mults(MLower);
363 Standard_Integer MinMult = pmu[FromK1];
365 for (Standard_Integer i = FromK1; i <= ToK2; i++) {
366 if (MinMult > pmu[i]) MinMult = pmu[i];
371 //=======================================================================
374 //=======================================================================
376 Standard_Integer BSplCLib::NbPoles(const Standard_Integer Degree,
377 const Standard_Boolean Periodic,
378 const TColStd_Array1OfInteger& Mults)
380 Standard_Integer i,sigma = 0;
381 Standard_Integer f = Mults.Lower();
382 Standard_Integer l = Mults.Upper();
383 const Standard_Integer * pmu = &Mults(f);
385 Standard_Integer Mf = pmu[f];
386 Standard_Integer Ml = pmu[l];
387 if (Mf <= 0) return 0;
388 if (Ml <= 0) return 0;
390 if (Mf > Degree) return 0;
391 if (Ml > Degree) return 0;
392 if (Mf != Ml ) return 0;
396 Standard_Integer Deg1 = Degree + 1;
397 if (Mf > Deg1) return 0;
398 if (Ml > Deg1) return 0;
399 sigma = Mf + Ml - Deg1;
402 for (i = f + 1; i < l; i++) {
403 if (pmu[i] <= 0 ) return 0;
404 if (pmu[i] > Degree) return 0;
410 //=======================================================================
411 //function : KnotSequenceLength
413 //=======================================================================
415 Standard_Integer BSplCLib::KnotSequenceLength
416 (const TColStd_Array1OfInteger& Mults,
417 const Standard_Integer Degree,
418 const Standard_Boolean Periodic)
420 Standard_Integer i,l = 0;
421 Standard_Integer MLower = Mults.Lower();
422 Standard_Integer MUpper = Mults.Upper();
423 const Standard_Integer * pmu = &Mults(MLower);
426 for (i = MLower; i <= MUpper; i++)
428 if (Periodic) l += 2 * (Degree + 1 - pmu[MLower]);
432 //=======================================================================
433 //function : KnotSequence
435 //=======================================================================
437 void BSplCLib::KnotSequence
438 (const TColStd_Array1OfReal& Knots,
439 const TColStd_Array1OfInteger& Mults,
440 TColStd_Array1OfReal& KnotSeq)
442 BSplCLib::KnotSequence(Knots,Mults,0,Standard_False,KnotSeq);
445 //=======================================================================
446 //function : KnotSequence
448 //=======================================================================
450 void BSplCLib::KnotSequence
451 (const TColStd_Array1OfReal& Knots,
452 const TColStd_Array1OfInteger& Mults,
453 const Standard_Integer Degree,
454 const Standard_Boolean Periodic,
455 TColStd_Array1OfReal& KnotSeq)
458 Standard_Integer Mult;
459 Standard_Integer MLower = Mults.Lower();
460 const Standard_Integer * pmu = &Mults(MLower);
462 Standard_Integer KLower = Knots.Lower();
463 Standard_Integer KUpper = Knots.Upper();
464 const Standard_Real * pkn = &Knots(KLower);
466 Standard_Integer M1 = Degree + 1 - pmu[MLower]; // for periodic
467 Standard_Integer i,j,index = Periodic ? M1 + 1 : 1;
469 for (i = KLower; i <= KUpper; i++) {
473 for (j = 1; j <= Mult; j++) {
479 Standard_Real period = pkn[KUpper] - pkn[KLower];
484 for (i = M1; i >= 1; i--) {
485 KnotSeq(i) = pkn[j] - period;
495 for (i = index; i <= KnotSeq.Upper(); i++) {
496 KnotSeq(i) = pkn[j] + period;
506 //=======================================================================
507 //function : KnotsLength
509 //=======================================================================
510 Standard_Integer BSplCLib::KnotsLength(const TColStd_Array1OfReal& SeqKnots,
511 // const Standard_Boolean Periodic)
512 const Standard_Boolean )
514 Standard_Integer sizeMult = 1;
515 Standard_Real val = SeqKnots(1);
516 for (Standard_Integer jj=2;
517 jj<=SeqKnots.Length();jj++)
519 // test on strict equality on nodes
520 if (SeqKnots(jj)!=val)
529 //=======================================================================
532 //=======================================================================
533 void BSplCLib::Knots(const TColStd_Array1OfReal& SeqKnots,
534 TColStd_Array1OfReal &knots,
535 TColStd_Array1OfInteger &mult,
536 // const Standard_Boolean Periodic)
537 const Standard_Boolean )
539 Standard_Real val = SeqKnots(1);
540 Standard_Integer kk=1;
544 for (Standard_Integer jj=2;jj<=SeqKnots.Length();jj++)
546 // test on strict equality on nodes
547 if (SeqKnots(jj)!=val)
561 //=======================================================================
562 //function : KnotForm
564 //=======================================================================
566 BSplCLib_KnotDistribution BSplCLib::KnotForm
567 (const Array1OfReal& Knots,
568 const Standard_Integer FromK1,
569 const Standard_Integer ToK2)
571 Standard_Real DU0,DU1,Ui,Uj,Eps0,val;
572 BSplCLib_KnotDistribution KForm = BSplCLib_Uniform;
574 Standard_Integer KLower = Knots.Lower();
575 const Standard_Real * pkn = &Knots(KLower);
578 if (Ui < 0) Ui = - Ui;
579 Uj = pkn[FromK1 + 1];
580 if (Uj < 0) Uj = - Uj;
582 if (DU0 < 0) DU0 = - DU0;
583 Eps0 = Epsilon (Ui) + Epsilon (Uj) + Epsilon (DU0);
584 Standard_Integer i = FromK1 + 1;
586 while (KForm != BSplCLib_NonUniform && i < ToK2) {
588 if (Ui < 0) Ui = - Ui;
591 if (Uj < 0) Uj = - Uj;
593 if (DU1 < 0) DU1 = - DU1;
595 if (val < 0) val = -val;
596 if (val > Eps0) KForm = BSplCLib_NonUniform;
598 Eps0 = Epsilon (Ui) + Epsilon (Uj) + Epsilon (DU0);
603 //=======================================================================
604 //function : MultForm
606 //=======================================================================
608 BSplCLib_MultDistribution BSplCLib::MultForm
609 (const Array1OfInteger& Mults,
610 const Standard_Integer FromK1,
611 const Standard_Integer ToK2)
613 Standard_Integer First,Last;
622 Standard_Integer MLower = Mults.Lower();
623 const Standard_Integer *pmu = &Mults(MLower);
625 Standard_Integer FirstMult = pmu[First];
626 BSplCLib_MultDistribution MForm = BSplCLib_Constant;
627 Standard_Integer i = First + 1;
628 Standard_Integer Mult = pmu[i];
630 // while (MForm != BSplCLib_NonUniform && i <= Last) { ???????????JR????????
631 while (MForm != BSplCLib_NonConstant && i <= Last) {
632 if (i == First + 1) {
633 if (Mult != FirstMult) MForm = BSplCLib_QuasiConstant;
635 else if (i == Last) {
636 if (MForm == BSplCLib_QuasiConstant) {
637 if (FirstMult != pmu[i]) MForm = BSplCLib_NonConstant;
640 if (Mult != pmu[i]) MForm = BSplCLib_NonConstant;
644 if (Mult != pmu[i]) MForm = BSplCLib_NonConstant;
652 //=======================================================================
653 //function : Reparametrize
655 //=======================================================================
657 void BSplCLib::Reparametrize
658 (const Standard_Real U1,
659 const Standard_Real U2,
662 Standard_Integer Lower = Knots.Lower();
663 Standard_Integer Upper = Knots.Upper();
664 Standard_Real UFirst = Min (U1, U2);
665 Standard_Real ULast = Max (U1, U2);
666 Standard_Real NewLength = ULast - UFirst;
667 BSplCLib_KnotDistribution KSet = BSplCLib::KnotForm (Knots, Lower, Upper);
668 if (KSet == BSplCLib_Uniform) {
669 Standard_Real DU = NewLength / (Upper - Lower);
670 Knots (Lower) = UFirst;
672 for (Standard_Integer i = Lower + 1; i <= Upper; i++) {
673 Knots (i) = Knots (i-1) + DU;
679 Standard_Real K1 = Knots (Lower);
680 Standard_Real Length = Knots (Upper) - Knots (Lower);
681 Knots (Lower) = UFirst;
683 for (Standard_Integer i = Lower + 1; i <= Upper; i++) {
685 Ratio = (K2 - K1) / Length;
686 Knots (i) = Knots (i-1) + (NewLength * Ratio);
689 Standard_Real Eps = Epsilon( Abs(Knots(i-1)) );
690 if (Knots(i) - Knots(i-1) <= Eps)
691 Knots(i) = NextAfter (Knots(i-1) + Eps, RealLast());
698 //=======================================================================
701 //=======================================================================
703 void BSplCLib::Reverse(TColStd_Array1OfReal& Knots)
705 Standard_Integer first = Knots.Lower();
706 Standard_Integer last = Knots.Upper();
707 Standard_Real kfirst = Knots(first);
708 Standard_Real klast = Knots(last);
709 Standard_Real tfirst = kfirst;
710 Standard_Real tlast = klast;
714 while (first <= last) {
715 tfirst += klast - Knots(last);
716 tlast -= Knots(first) - kfirst;
717 kfirst = Knots(first);
719 Knots(first) = tfirst;
726 //=======================================================================
729 //=======================================================================
731 void BSplCLib::Reverse(TColStd_Array1OfInteger& Mults)
733 Standard_Integer first = Mults.Lower();
734 Standard_Integer last = Mults.Upper();
735 Standard_Integer temp;
737 while (first < last) {
739 Mults(first) = Mults(last);
746 //=======================================================================
749 //=======================================================================
751 void BSplCLib::Reverse(TColStd_Array1OfReal& Weights,
752 const Standard_Integer L)
754 Standard_Integer i, l = L;
755 l = Weights.Lower()+(l-Weights.Lower())%(Weights.Upper()-Weights.Lower()+1);
757 TColStd_Array1OfReal temp(0,Weights.Length()-1);
759 for (i = Weights.Lower(); i <= l; i++)
760 temp(l-i) = Weights(i);
762 for (i = l+1; i <= Weights.Upper(); i++)
763 temp(l-Weights.Lower()+Weights.Upper()-i+1) = Weights(i);
765 for (i = Weights.Lower(); i <= Weights.Upper(); i++)
766 Weights(i) = temp(i-Weights.Lower());
769 //=======================================================================
770 //function : IsRational
772 //=======================================================================
774 Standard_Boolean BSplCLib::IsRational(const TColStd_Array1OfReal& Weights,
775 const Standard_Integer I1,
776 const Standard_Integer I2,
777 // const Standard_Real Epsi)
778 const Standard_Real )
780 Standard_Integer i, f = Weights.Lower(), l = Weights.Length();
781 Standard_Integer I3 = I2 - f;
782 const Standard_Real * WG = &Weights(f);
785 for (i = I1 - f; i < I3; i++) {
786 if (WG[f + (i % l)] != WG[f + ((i + 1) % l)]) return Standard_True;
788 return Standard_False ;
791 //=======================================================================
793 //purpose : evaluate point and derivatives
794 //=======================================================================
796 void BSplCLib::Eval(const Standard_Real U,
797 const Standard_Integer Degree,
798 Standard_Real& Knots,
799 const Standard_Integer Dimension,
800 Standard_Real& Poles)
802 Standard_Integer step,i,Dms,Dm1,Dpi,Sti;
803 Standard_Real X, Y, *poles, *knots = &Knots;
811 for (step = - 1; step < Dm1; step++) {
817 for (i = 0; i < Dms; i++) {
820 X = (knots[Dpi] - U) / (knots[Dpi] - knots[Sti]);
822 poles[0] *= X; poles[0] += Y * poles[1];
830 for (step = - 1; step < Dm1; step++) {
836 for (i = 0; i < Dms; i++) {
839 X = (knots[Dpi] - U) / (knots[Dpi] - knots[Sti]);
841 poles[0] *= X; poles[0] += Y * poles[2];
842 poles[1] *= X; poles[1] += Y * poles[3];
850 for (step = - 1; step < Dm1; step++) {
856 for (i = 0; i < Dms; i++) {
859 X = (knots[Dpi] - U) / (knots[Dpi] - knots[Sti]);
861 poles[0] *= X; poles[0] += Y * poles[3];
862 poles[1] *= X; poles[1] += Y * poles[4];
863 poles[2] *= X; poles[2] += Y * poles[5];
871 for (step = - 1; step < Dm1; step++) {
877 for (i = 0; i < Dms; i++) {
880 X = (knots[Dpi] - U) / (knots[Dpi] - knots[Sti]);
882 poles[0] *= X; poles[0] += Y * poles[4];
883 poles[1] *= X; poles[1] += Y * poles[5];
884 poles[2] *= X; poles[2] += Y * poles[6];
885 poles[3] *= X; poles[3] += Y * poles[7];
894 for (step = - 1; step < Dm1; step++) {
900 for (i = 0; i < Dms; i++) {
903 X = (knots[Dpi] - U) / (knots[Dpi] - knots[Sti]);
906 for (k = 0; k < Dimension; k++) {
908 poles[k] += Y * poles[k + Dimension];
917 //=======================================================================
918 //function : BoorScheme
920 //=======================================================================
922 void BSplCLib::BoorScheme(const Standard_Real U,
923 const Standard_Integer Degree,
924 Standard_Real& Knots,
925 const Standard_Integer Dimension,
926 Standard_Real& Poles,
927 const Standard_Integer Depth,
928 const Standard_Integer Length)
931 // Compute the values
935 // for i = 0 to Depth,
936 // j = 0 to Length - i
938 // The Boor scheme is :
941 // P(i,j) = x * P(i-1,j) + (1-x) * P(i-1,j+1)
943 // where x = (knot(i+j+Degree) - U) / (knot(i+j+Degree) - knot(i+j))
946 // The values are stored in the array Poles
947 // They are alternatively written if the odd and even positions.
949 // The successives contents of the array are
950 // ***** means unitialised, l = Degree + Length
952 // P(0,0) ****** P(0,1) ...... P(0,l-1) ******** P(0,l)
953 // P(0,0) P(1,0) P(0,1) ...... P(0,l-1) P(1,l-1) P(0,l)
954 // P(0,0) P(1,0) P(2,0) ...... P(2,l-1) P(1,l-1) P(0,l)
957 Standard_Integer i,k,step;
958 Standard_Real *knots = &Knots;
959 Standard_Real *pole, *firstpole = &Poles - 2 * Dimension;
960 // the steps of recursion
962 for (step = 0; step < Depth; step++) {
963 firstpole += Dimension;
965 // compute the new row of poles
967 for (i = step; i < Length; i++) {
968 pole += 2 * Dimension;
970 Standard_Real X = (knots[i+Degree-step] - U)
971 / (knots[i+Degree-step] - knots[i]);
972 Standard_Real Y = 1. - X;
974 // P(i,j) = X * P(i-1,j) + (1-X) * P(i-1,j+1)
976 for (k = 0; k < Dimension; k++)
977 pole[k] = X * pole[k - Dimension] + Y * pole[k + Dimension];
982 //=======================================================================
983 //function : AntiBoorScheme
985 //=======================================================================
987 Standard_Boolean BSplCLib::AntiBoorScheme(const Standard_Real U,
988 const Standard_Integer Degree,
989 Standard_Real& Knots,
990 const Standard_Integer Dimension,
991 Standard_Real& Poles,
992 const Standard_Integer Depth,
993 const Standard_Integer Length,
994 const Standard_Real Tolerance)
996 // do the Boor scheme reverted.
998 Standard_Integer i,k,step, half_length;
999 Standard_Real *knots = &Knots;
1000 Standard_Real z,X,Y,*pole, *firstpole = &Poles + (Depth-1) * Dimension;
1002 // Test the special case length = 1
1003 // only verification of the central point
1006 X = (knots[Degree] - U) / (knots[Degree] - knots[0]);
1009 for (k = 0; k < Dimension; k++) {
1010 z = X * firstpole[k] + Y * firstpole[k+2*Dimension];
1011 if (Abs(z - firstpole[k+Dimension]) > Tolerance)
1012 return Standard_False;
1014 return Standard_True;
1018 // the steps of recursion
1020 for (step = Depth-1; step >= 0; step--) {
1021 firstpole -= Dimension;
1024 // first step from left to right
1026 for (i = step; i < Length-1; i++) {
1027 pole += 2 * Dimension;
1029 X = (knots[i+Degree-step] - U) / (knots[i+Degree-step] - knots[i]);
1032 for (k = 0; k < Dimension; k++)
1033 pole[k+Dimension] = (pole[k] - X*pole[k-Dimension]) / Y;
1037 // second step from right to left
1038 pole += 4* Dimension;
1039 half_length = (Length - 1 + step) / 2 ;
1041 // only do half of the way from right to left
1042 // otherwise it start degenerating because of
1046 for (i = Length-1; i > half_length ; i--) {
1047 pole -= 2 * Dimension;
1050 X = (knots[i+Degree-step] - U) / (knots[i+Degree-step] - knots[i]);
1053 for (k = 0; k < Dimension; k++) {
1054 z = (pole[k] - Y * pole[k+Dimension]) / X;
1055 if (Abs(z-pole[k-Dimension]) > Tolerance)
1056 return Standard_False;
1057 pole[k-Dimension] += z;
1058 pole[k-Dimension] /= 2.;
1062 return Standard_True;
1065 //=======================================================================
1066 //function : Derivative
1068 //=======================================================================
1070 void BSplCLib::Derivative(const Standard_Integer Degree,
1071 Standard_Real& Knots,
1072 const Standard_Integer Dimension,
1073 const Standard_Integer Length,
1074 const Standard_Integer Order,
1075 Standard_Real& Poles)
1077 Standard_Integer i,k,step,span = Degree;
1078 Standard_Real *knot = &Knots;
1080 for (step = 1; step <= Order; step++) {
1081 Standard_Real* pole = &Poles;
1083 for (i = step; i < Length; i++) {
1084 Standard_Real coef = - span / (knot[i+span] - knot[i]);
1086 for (k = 0; k < Dimension; k++) {
1087 pole[k] -= pole[k+Dimension];
1096 //=======================================================================
1099 //=======================================================================
1101 void BSplCLib::Bohm(const Standard_Real U,
1102 const Standard_Integer Degree,
1103 const Standard_Integer N,
1104 Standard_Real& Knots,
1105 const Standard_Integer Dimension,
1106 Standard_Real& Poles)
1108 // First phase independant of U, compute the poles of the derivatives
1109 Standard_Integer i,j,iDim,min,Dmi,DDmi,jDmi,Degm1;
1110 Standard_Real *knot = &Knots, *pole, coef, *tbis, *psav, *psDD, *psDDmDim;
1112 if (N < Degree) min = N;
1115 DDmi = (Degree << 1) + 1;
1116 switch (Dimension) {
1118 psDD = psav + Degree;
1119 psDDmDim = psDD - 1;
1121 for (i = 0; i < Degree; i++) {
1127 for (j = Degm1; j >= i; j--) {
1129 *pole -= *tbis; *pole /= (knot[jDmi] - knot[j]);
1134 // Second phase, dependant of U
1137 for (i = 0; i < Degree; i++) {
1143 for (j = i; j >= 0; j--) {
1144 *pole += coef * (*tbis);
1149 // multiply by the degrees
1154 for (i = 1; i <= min; i++) {
1155 *pole *= coef; pole++;
1162 psDD = psav + (Degree << 1);
1163 psDDmDim = psDD - 2;
1165 for (i = 0; i < Degree; i++) {
1171 for (j = Degm1; j >= i; j--) {
1173 coef = 1. / (knot[jDmi] - knot[j]);
1174 *pole -= *tbis; *pole *= coef; pole++; tbis++;
1175 *pole -= *tbis; *pole *= coef;
1180 // Second phase, dependant of U
1183 for (i = 0; i < Degree; i++) {
1189 for (j = i; j >= 0; j--) {
1190 *pole += coef * (*tbis); pole++; tbis++;
1191 *pole += coef * (*tbis);
1196 // multiply by the degrees
1201 for (i = 1; i <= min; i++) {
1202 *pole *= coef; pole++;
1203 *pole *= coef; pole++;
1210 psDD = psav + (Degree << 1) + Degree;
1211 psDDmDim = psDD - 3;
1213 for (i = 0; i < Degree; i++) {
1219 for (j = Degm1; j >= i; j--) {
1221 coef = 1. / (knot[jDmi] - knot[j]);
1222 *pole -= *tbis; *pole *= coef; pole++; tbis++;
1223 *pole -= *tbis; *pole *= coef; pole++; tbis++;
1224 *pole -= *tbis; *pole *= coef;
1229 // Second phase, dependant of U
1232 for (i = 0; i < Degree; i++) {
1238 for (j = i; j >= 0; j--) {
1239 *pole += coef * (*tbis); pole++; tbis++;
1240 *pole += coef * (*tbis); pole++; tbis++;
1241 *pole += coef * (*tbis);
1246 // multiply by the degrees
1251 for (i = 1; i <= min; i++) {
1252 *pole *= coef; pole++;
1253 *pole *= coef; pole++;
1254 *pole *= coef; pole++;
1261 psDD = psav + (Degree << 2);
1262 psDDmDim = psDD - 4;
1264 for (i = 0; i < Degree; i++) {
1270 for (j = Degm1; j >= i; j--) {
1272 coef = 1. / (knot[jDmi] - knot[j]);
1273 *pole -= *tbis; *pole *= coef; pole++; tbis++;
1274 *pole -= *tbis; *pole *= coef; pole++; tbis++;
1275 *pole -= *tbis; *pole *= coef; pole++; tbis++;
1276 *pole -= *tbis; *pole *= coef;
1281 // Second phase, dependant of U
1284 for (i = 0; i < Degree; i++) {
1290 for (j = i; j >= 0; j--) {
1291 *pole += coef * (*tbis); pole++; tbis++;
1292 *pole += coef * (*tbis); pole++; tbis++;
1293 *pole += coef * (*tbis); pole++; tbis++;
1294 *pole += coef * (*tbis);
1299 // multiply by the degrees
1304 for (i = 1; i <= min; i++) {
1305 *pole *= coef; pole++;
1306 *pole *= coef; pole++;
1307 *pole *= coef; pole++;
1308 *pole *= coef; pole++;
1316 Standard_Integer Dim2 = Dimension << 1;
1317 psDD = psav + Degree * Dimension;
1318 psDDmDim = psDD - Dimension;
1320 for (i = 0; i < Degree; i++) {
1326 for (j = Degm1; j >= i; j--) {
1328 coef = 1. / (knot[jDmi] - knot[j]);
1330 for (k = 0; k < Dimension; k++) {
1331 *pole -= *tbis; *pole *= coef; pole++; tbis++;
1337 // Second phase, dependant of U
1340 for (i = 0; i < Degree; i++) {
1343 tbis = pole + Dimension;
1346 for (j = i; j >= 0; j--) {
1348 for (k = 0; k < Dimension; k++) {
1349 *pole += coef * (*tbis); pole++; tbis++;
1355 // multiply by the degrees
1358 pole = psav + Dimension;
1360 for (i = 1; i <= min; i++) {
1362 for (k = 0; k < Dimension; k++) {
1363 *pole *= coef; pole++;
1372 //=======================================================================
1373 //function : BuildKnots
1375 //=======================================================================
1377 void BSplCLib::BuildKnots(const Standard_Integer Degree,
1378 const Standard_Integer Index,
1379 const Standard_Boolean Periodic,
1380 const TColStd_Array1OfReal& Knots,
1381 const TColStd_Array1OfInteger& Mults,
1384 Standard_Integer KLower = Knots.Lower();
1385 const Standard_Real * pkn = &Knots(KLower);
1387 Standard_Real *knot = &LK;
1388 if (&Mults == NULL) {
1391 Standard_Integer j = Index ;
1392 knot[0] = pkn[j]; j++;
1397 Standard_Integer j = Index - 1;
1398 knot[0] = pkn[j]; j++;
1399 knot[1] = pkn[j]; j++;
1400 knot[2] = pkn[j]; j++;
1405 Standard_Integer j = Index - 2;
1406 knot[0] = pkn[j]; j++;
1407 knot[1] = pkn[j]; j++;
1408 knot[2] = pkn[j]; j++;
1409 knot[3] = pkn[j]; j++;
1410 knot[4] = pkn[j]; j++;
1415 Standard_Integer j = Index - 3;
1416 knot[0] = pkn[j]; j++;
1417 knot[1] = pkn[j]; j++;
1418 knot[2] = pkn[j]; j++;
1419 knot[3] = pkn[j]; j++;
1420 knot[4] = pkn[j]; j++;
1421 knot[5] = pkn[j]; j++;
1422 knot[6] = pkn[j]; j++;
1427 Standard_Integer j = Index - 4;
1428 knot[0] = pkn[j]; j++;
1429 knot[1] = pkn[j]; j++;
1430 knot[2] = pkn[j]; j++;
1431 knot[3] = pkn[j]; j++;
1432 knot[4] = pkn[j]; j++;
1433 knot[5] = pkn[j]; j++;
1434 knot[6] = pkn[j]; j++;
1435 knot[7] = pkn[j]; j++;
1436 knot[8] = pkn[j]; j++;
1441 Standard_Integer j = Index - 5;
1442 knot[ 0] = pkn[j]; j++;
1443 knot[ 1] = pkn[j]; j++;
1444 knot[ 2] = pkn[j]; j++;
1445 knot[ 3] = pkn[j]; j++;
1446 knot[ 4] = pkn[j]; j++;
1447 knot[ 5] = pkn[j]; j++;
1448 knot[ 6] = pkn[j]; j++;
1449 knot[ 7] = pkn[j]; j++;
1450 knot[ 8] = pkn[j]; j++;
1451 knot[ 9] = pkn[j]; j++;
1452 knot[10] = pkn[j]; j++;
1457 Standard_Integer i,j;
1458 Standard_Integer Deg2 = Degree << 1;
1461 for (i = 0; i < Deg2; i++) {
1470 Standard_Integer Deg1 = Degree - 1;
1471 Standard_Integer KUpper = Knots.Upper();
1472 Standard_Integer MLower = Mults.Lower();
1473 Standard_Integer MUpper = Mults.Upper();
1474 const Standard_Integer * pmu = &Mults(MLower);
1476 Standard_Real dknot = 0;
1477 Standard_Integer ilow = Index , mlow = 0;
1478 Standard_Integer iupp = Index + 1, mupp = 0;
1479 Standard_Real loffset = 0., uoffset = 0.;
1480 Standard_Boolean getlow = Standard_True, getupp = Standard_True;
1482 dknot = pkn[KUpper] - pkn[KLower];
1483 if (iupp > MUpper) {
1488 // Find the knots around Index
1490 for (i = 0; i < Degree; i++) {
1493 if (mlow > pmu[ilow]) {
1496 getlow = (ilow >= MLower);
1497 if (Periodic && !getlow) {
1500 getlow = Standard_True;
1504 knot[Deg1 - i] = pkn[ilow] - loffset;
1508 if (mupp > pmu[iupp]) {
1511 getupp = (iupp <= MUpper);
1512 if (Periodic && !getupp) {
1515 getupp = Standard_True;
1519 knot[Degree + i] = pkn[iupp] + uoffset;
1525 //=======================================================================
1526 //function : PoleIndex
1528 //=======================================================================
1530 Standard_Integer BSplCLib::PoleIndex(const Standard_Integer Degree,
1531 const Standard_Integer Index,
1532 const Standard_Boolean Periodic,
1533 const TColStd_Array1OfInteger& Mults)
1535 Standard_Integer i, pindex = 0;
1537 for (i = Mults.Lower(); i <= Index; i++)
1540 pindex -= Mults(Mults.Lower());
1542 pindex -= Degree + 1;
1547 //=======================================================================
1548 //function : BuildBoor
1549 //purpose : builds the local array for boor
1550 //=======================================================================
1552 void BSplCLib::BuildBoor(const Standard_Integer Index,
1553 const Standard_Integer Length,
1554 const Standard_Integer Dimension,
1555 const TColStd_Array1OfReal& Poles,
1558 Standard_Real *poles = &LP;
1559 Standard_Integer i,k, ip = Poles.Lower() + Index * Dimension;
1561 for (i = 0; i < Length+1; i++) {
1563 for (k = 0; k < Dimension; k++) {
1564 poles[k] = Poles(ip);
1566 if (ip > Poles.Upper()) ip = Poles.Lower();
1568 poles += 2 * Dimension;
1572 //=======================================================================
1573 //function : BoorIndex
1575 //=======================================================================
1577 Standard_Integer BSplCLib::BoorIndex(const Standard_Integer Index,
1578 const Standard_Integer Length,
1579 const Standard_Integer Depth)
1581 if (Index <= Depth) return Index;
1582 if (Index <= Length) return 2 * Index - Depth;
1583 return Length + Index - Depth;
1586 //=======================================================================
1587 //function : GetPole
1589 //=======================================================================
1591 void BSplCLib::GetPole(const Standard_Integer Index,
1592 const Standard_Integer Length,
1593 const Standard_Integer Depth,
1594 const Standard_Integer Dimension,
1596 Standard_Integer& Position,
1597 TColStd_Array1OfReal& Pole)
1600 Standard_Real* pole = &LP + BoorIndex(Index,Length,Depth) * Dimension;
1602 for (k = 0; k < Dimension; k++) {
1603 Pole(Position) = pole[k];
1606 if (Position > Pole.Upper()) Position = Pole.Lower();
1609 //=======================================================================
1610 //function : PrepareInsertKnots
1612 //=======================================================================
1614 Standard_Boolean BSplCLib::PrepareInsertKnots
1615 (const Standard_Integer Degree,
1616 const Standard_Boolean Periodic,
1617 const TColStd_Array1OfReal& Knots,
1618 const TColStd_Array1OfInteger& Mults,
1619 const TColStd_Array1OfReal& AddKnots,
1620 const TColStd_Array1OfInteger& AddMults,
1621 Standard_Integer& NbPoles,
1622 Standard_Integer& NbKnots,
1623 const Standard_Real Tolerance,
1624 const Standard_Boolean Add)
1626 Standard_Boolean addflat = &AddMults == NULL;
1628 Standard_Integer first,last;
1630 first = Knots.Lower();
1631 last = Knots.Upper();
1634 first = FirstUKnotIndex(Degree,Mults);
1635 last = LastUKnotIndex(Degree,Mults);
1637 Standard_Real adeltaK1 = Knots(first)-AddKnots(AddKnots.Lower());
1638 Standard_Real adeltaK2 = AddKnots(AddKnots.Upper())-Knots(last);
1639 if (adeltaK1 > Tolerance) return Standard_False;
1640 if (adeltaK2 > Tolerance) return Standard_False;
1642 Standard_Integer sigma = 0, mult, amult, lastmult = 0;
1644 Standard_Integer k = Knots.Lower() - 1;
1645 Standard_Integer ak = AddKnots.Lower();
1647 if(Periodic && AddKnots.Length() > 1)
1649 //gka for case when segments was produced on full period only one knot
1650 //was added in the end of curve
1651 if(fabs(adeltaK1) <= Precision::PConfusion() &&
1652 fabs(adeltaK2) <= Precision::PConfusion())
1656 Standard_Real au,oldau = AddKnots(ak),Eps;
1658 while (ak <= AddKnots.Upper()) {
1660 if (au < oldau) return Standard_False;
1663 Eps = Max(Tolerance,Epsilon(au));
1665 while ((k < Knots.Upper()) && (Knots(k+1) - au <= Eps)) {
1671 if (addflat) amult = 1;
1672 else amult = Max(0,AddMults(ak));
1674 while ((ak < AddKnots.Upper()) &&
1675 (Abs(au - AddKnots(ak+1)) <= Eps)) {
1678 if (addflat) amult++;
1679 else amult += Max(0,AddMults(ak));
1684 if (Abs(au - Knots(k)) <= Eps) {
1685 // identic to existing knot
1687 lastmult = mult;//gka
1689 if (mult + amult > Degree)
1690 amult = Max(0,Degree - mult);
1692 //lastmult = mult + amult;
1695 else if (amult > mult) {
1696 if (amult > Degree) amult = Degree;
1697 sigma += amult - mult;
1698 //lastmult = amult;//gka modified
1701 // on periodic curves if this is the last knot
1702 // the multiplicity is added twice to account for the first knot
1703 if (k == Knots.Upper() && Periodic) {
1707 sigma += amult - mult;
1712 // not identic to existing knot
1714 if (amult > Degree) amult = Degree;
1724 // count the last knots
1725 if (lastmult == 0)// || k < Knots.Upper())
1726 lastmult = Mults(Knots.Upper());
1728 while (k < Knots.Upper()) {
1735 NbPoles = sigma - lastmult;
1738 NbPoles = sigma - Degree - 1;
1741 return Standard_True;
1744 //=======================================================================
1746 //purpose : copy reals from an array to an other
1748 // NbValues are copied from OldPoles(OldFirst)
1749 // to NewPoles(NewFirst)
1751 // Periodicity is handled.
1752 // OldFirst and NewFirst are updated
1753 // to the position after the last copied pole.
1755 //=======================================================================
1757 static void Copy(const Standard_Integer NbPoles,
1758 Standard_Integer& OldFirst,
1759 const TColStd_Array1OfReal& OldPoles,
1760 Standard_Integer& NewFirst,
1761 TColStd_Array1OfReal& NewPoles)
1763 // reset the index in the range for periodicity
1765 OldFirst = OldPoles.Lower() +
1766 (OldFirst - OldPoles.Lower()) % (OldPoles.Upper() - OldPoles.Lower() + 1);
1768 NewFirst = NewPoles.Lower() +
1769 (NewFirst - NewPoles.Lower()) % (NewPoles.Upper() - NewPoles.Lower() + 1);
1774 for (i = 1; i <= NbPoles; i++) {
1775 NewPoles(NewFirst) = OldPoles(OldFirst);
1777 if (OldFirst > OldPoles.Upper()) OldFirst = OldPoles.Lower();
1779 if (NewFirst > NewPoles.Upper()) NewFirst = NewPoles.Lower();
1783 //=======================================================================
1784 //function : InsertKnots
1785 //purpose : insert an array of knots and multiplicities
1786 //=======================================================================
1788 void BSplCLib::InsertKnots
1789 (const Standard_Integer Degree,
1790 const Standard_Boolean Periodic,
1791 const Standard_Integer Dimension,
1792 const TColStd_Array1OfReal& Poles,
1793 const TColStd_Array1OfReal& Knots,
1794 const TColStd_Array1OfInteger& Mults,
1795 const TColStd_Array1OfReal& AddKnots,
1796 const TColStd_Array1OfInteger& AddMults,
1797 TColStd_Array1OfReal& NewPoles,
1798 TColStd_Array1OfReal& NewKnots,
1799 TColStd_Array1OfInteger& NewMults,
1800 const Standard_Real Tolerance,
1801 const Standard_Boolean Add)
1803 Standard_Boolean addflat = &AddMults == NULL;
1805 Standard_Integer i,k,mult,firstmult;
1806 Standard_Integer index,kn,curnk,curk;
1807 Standard_Integer p,np, curp, curnp, length, depth;
1809 Standard_Integer need;
1812 // -------------------
1813 // create local arrays
1814 // -------------------
1816 Standard_Real *knots = new Standard_Real[2*Degree];
1817 Standard_Real *poles = new Standard_Real[(2*Degree+1)*Dimension];
1819 //----------------------------
1820 // loop on the knots to insert
1821 //----------------------------
1823 curk = Knots.Lower()-1; // current position in Knots
1824 curnk = NewKnots.Lower()-1; // current position in NewKnots
1825 curp = Poles.Lower(); // current position in Poles
1826 curnp = NewPoles.Lower(); // current position in NewPoles
1828 // NewKnots, NewMults, NewPoles contains the current state of the curve
1830 // index is the first pole of the current curve for insertion schema
1832 if (Periodic) index = -Mults(Mults.Lower());
1833 else index = -Degree-1;
1835 // on Periodic curves the first knot and the last knot are inserted later
1836 // (they are the same knot)
1837 firstmult = 0; // multiplicity of the first-last knot for periodic
1840 // kn current knot to insert in AddKnots
1842 for (kn = AddKnots.Lower(); kn <= AddKnots.Upper(); kn++) {
1845 Eps = Max(Tolerance,Epsilon(u));
1847 //-----------------------------------
1848 // find the position in the old knots
1849 // and copy to the new knots
1850 //-----------------------------------
1852 while (curk < Knots.Upper() && Knots(curk+1) - u <= Eps) {
1854 NewKnots(curnk) = Knots(curk);
1855 index += NewMults(curnk) = Mults(curk);
1858 //-----------------------------------
1859 // Slice the knots and the mults
1860 // to the current size of the new curve
1861 //-----------------------------------
1863 i = curnk + Knots.Upper() - curk;
1864 TColStd_Array1OfReal nknots(NewKnots(NewKnots.Lower()),NewKnots.Lower(),i);
1865 TColStd_Array1OfInteger nmults(NewMults(NewMults.Lower()),NewMults.Lower(),i);
1867 //-----------------------------------
1868 // copy enough knots
1869 // to compute the insertion schema
1870 //-----------------------------------
1876 while (mult < Degree && k < Knots.Upper()) {
1878 nknots(i) = Knots(k);
1879 mult += nmults(i) = Mults(k);
1882 // copy knots at the end for periodic curve
1888 while (mult < Degree && i > curnk) {
1889 nknots(i) = Knots(k);
1890 mult += nmults(i) = Mults(k);
1894 nmults(nmults.Upper()) = nmults(nmults.Lower());
1899 //------------------------------------
1900 // do the boor scheme on the new curve
1901 // to insert the new knot
1902 //------------------------------------
1904 Standard_Boolean sameknot = (Abs(u-NewKnots(curnk)) <= Eps);
1906 if (sameknot) length = Max(0,Degree - NewMults(curnk));
1907 else length = Degree;
1909 if (addflat) depth = 1;
1910 else depth = Min(Degree,AddMults(kn));
1914 if ((NewMults(curnk) + depth) > Degree)
1915 depth = Degree - NewMults(curnk);
1918 depth = Max(0,depth-NewMults(curnk));
1922 // on periodic curve the first and last knot are delayed to the end
1923 if (curk == Knots.Lower() || (curk == Knots.Upper())) {
1929 if (depth <= 0) continue;
1931 BuildKnots(Degree,curnk,Periodic,nknots,nmults,*knots);
1935 need = NewPoles.Lower()+(index+length+1)*Dimension - curnp;
1936 need = Min(need,Poles.Upper() - curp + 1);
1940 Copy(need,p,Poles,np,NewPoles);
1944 // slice the poles to the current number of poles in case of periodic
1945 TColStd_Array1OfReal npoles(NewPoles(NewPoles.Lower()),NewPoles.Lower(),curnp-1);
1947 BuildBoor(index,length,Dimension,npoles,*poles);
1948 BoorScheme(u,Degree,*knots,Dimension,*poles,depth,length);
1950 //-------------------
1951 // copy the new poles
1952 //-------------------
1954 curnp += depth * Dimension; // number of poles is increased by depth
1955 TColStd_Array1OfReal ThePoles(NewPoles(NewPoles.Lower()),NewPoles.Lower(),curnp-1);
1956 np = NewKnots.Lower()+(index+1)*Dimension;
1958 for (i = 1; i <= length + depth; i++)
1959 GetPole(i,length,depth,Dimension,*poles,np,ThePoles);
1961 //-------------------
1963 //-------------------
1967 NewMults(curnk) += depth;
1971 NewKnots(curnk) = u;
1972 NewMults(curnk) = depth;
1976 //------------------------------
1977 // copy the last poles and knots
1978 //------------------------------
1980 Copy(Poles.Upper() - curp + 1,curp,Poles,curnp,NewPoles);
1982 while (curk < Knots.Upper()) {
1984 NewKnots(curnk) = Knots(curk);
1985 NewMults(curnk) = Mults(curk);
1988 //------------------------------
1989 // process the first-last knot
1990 // on periodic curves
1991 //------------------------------
1993 if (firstmult > 0) {
1994 curnk = NewKnots.Lower();
1995 if (NewMults(curnk) + firstmult > Degree) {
1996 firstmult = Degree - NewMults(curnk);
1998 if (firstmult > 0) {
2000 length = Degree - NewMults(curnk);
2003 BuildKnots(Degree,curnk,Periodic,NewKnots,NewMults,*knots);
2004 TColStd_Array1OfReal npoles(NewPoles(NewPoles.Lower()),
2006 NewPoles.Upper()-depth*Dimension);
2007 BuildBoor(0,length,Dimension,npoles,*poles);
2008 BoorScheme(NewKnots(curnk),Degree,*knots,Dimension,*poles,depth,length);
2010 //---------------------------
2011 // copy the new poles
2012 // but rotate them with depth
2013 //---------------------------
2015 np = NewPoles.Lower();
2017 for (i = depth; i < length + depth; i++)
2018 GetPole(i,length,depth,Dimension,*poles,np,NewPoles);
2020 np = NewPoles.Upper() - depth*Dimension + 1;
2022 for (i = 0; i < depth; i++)
2023 GetPole(i,length,depth,Dimension,*poles,np,NewPoles);
2025 NewMults(NewMults.Lower()) += depth;
2026 NewMults(NewMults.Upper()) += depth;
2029 // free local arrays
2034 //=======================================================================
2035 //function : RemoveKnot
2037 //=======================================================================
2039 Standard_Boolean BSplCLib::RemoveKnot
2040 (const Standard_Integer Index,
2041 const Standard_Integer Mult,
2042 const Standard_Integer Degree,
2043 const Standard_Boolean Periodic,
2044 const Standard_Integer Dimension,
2045 const TColStd_Array1OfReal& Poles,
2046 const TColStd_Array1OfReal& Knots,
2047 const TColStd_Array1OfInteger& Mults,
2048 TColStd_Array1OfReal& NewPoles,
2049 TColStd_Array1OfReal& NewKnots,
2050 TColStd_Array1OfInteger& NewMults,
2051 const Standard_Real Tolerance)
2053 Standard_Integer index,i,j,k,p,np;
2055 Standard_Integer TheIndex = Index;
2058 Standard_Integer first,last;
2060 first = Knots.Lower();
2061 last = Knots.Upper();
2064 first = BSplCLib::FirstUKnotIndex(Degree,Mults) + 1;
2065 last = BSplCLib::LastUKnotIndex(Degree,Mults) - 1;
2067 if (Index < first) return Standard_False;
2068 if (Index > last) return Standard_False;
2070 if ( Periodic && (Index == first)) TheIndex = last;
2072 Standard_Integer depth = Mults(TheIndex) - Mult;
2073 Standard_Integer length = Degree - Mult;
2075 // -------------------
2076 // create local arrays
2077 // -------------------
2079 Standard_Real *knots = new Standard_Real[4*Degree];
2080 Standard_Real *poles = new Standard_Real[(2*Degree+1)*Dimension];
2083 // ------------------------------------
2084 // build the knots for anti Boor Scheme
2085 // ------------------------------------
2087 // the new sequence of knots
2088 // is obtained from the knots at Index-1 and Index
2090 BSplCLib::BuildKnots(Degree,TheIndex-1,Periodic,Knots,Mults,*knots);
2091 index = PoleIndex(Degree,TheIndex-1,Periodic,Mults);
2092 BSplCLib::BuildKnots(Degree,TheIndex,Periodic,Knots,Mults,knots[2*Degree]);
2096 for (i = 0; i < Degree - Mult; i++)
2097 knots[i] = knots[i+Mult];
2099 for (i = Degree-Mult; i < 2*Degree; i++)
2100 knots[i] = knots[2*Degree+i];
2103 // ------------------------------------
2104 // build the poles for anti Boor Scheme
2105 // ------------------------------------
2107 p = Poles.Lower()+index * Dimension;
2109 for (i = 0; i <= length + depth; i++) {
2110 j = Dimension * BoorIndex(i,length,depth);
2112 for (k = 0; k < Dimension; k++) {
2113 poles[j+k] = Poles(p+k);
2116 if (p > Poles.Upper()) p = Poles.Lower();
2124 Standard_Boolean result = AntiBoorScheme(Knots(TheIndex),Degree,*knots,
2126 depth,length,Tolerance);
2137 np = NewPoles.Lower();
2139 // unmodified poles before
2140 Copy((index+1)*Dimension,p,Poles,np,NewPoles);
2145 for (i = 1; i <= length; i++)
2146 BSplCLib::GetPole(i,length,0,Dimension,*poles,np,NewPoles);
2147 p += (length + depth) * Dimension ;
2149 // unmodified poles after
2150 if (p != Poles.Lower()) {
2151 i = Poles.Upper() - p + 1;
2152 Copy(i,p,Poles,np,NewPoles);
2160 NewMults(TheIndex) = Mult;
2162 if (TheIndex == first) NewMults(last) = Mult;
2163 if (TheIndex == last) NewMults(first) = Mult;
2167 if (!Periodic || (TheIndex != first && TheIndex != last)) {
2169 for (i = Knots.Lower(); i < TheIndex; i++) {
2170 NewKnots(i) = Knots(i);
2171 NewMults(i) = Mults(i);
2174 for (i = TheIndex+1; i <= Knots.Upper(); i++) {
2175 NewKnots(i-1) = Knots(i);
2176 NewMults(i-1) = Mults(i);
2180 // The interesting case of a Periodic curve
2181 // where the first and last knot is removed.
2183 for (i = first; i < last-1; i++) {
2184 NewKnots(i) = Knots(i+1);
2185 NewMults(i) = Mults(i+1);
2187 NewKnots(last-1) = NewKnots(first) + Knots(last) - Knots(first);
2188 NewMults(last-1) = NewMults(first);
2194 // free local arrays
2201 //=======================================================================
2202 //function : IncreaseDegreeCountKnots
2204 //=======================================================================
2206 Standard_Integer BSplCLib::IncreaseDegreeCountKnots
2207 (const Standard_Integer Degree,
2208 const Standard_Integer NewDegree,
2209 const Standard_Boolean Periodic,
2210 const TColStd_Array1OfInteger& Mults)
2212 if (Periodic) return Mults.Length();
2213 Standard_Integer f = FirstUKnotIndex(Degree,Mults);
2214 Standard_Integer l = LastUKnotIndex(Degree,Mults);
2215 Standard_Integer m,i,removed = 0, step = NewDegree - Degree;
2218 m = Degree + (f - i + 1) * step + 1;
2220 while (m > NewDegree+1) {
2222 m -= Mults(i) + step;
2225 if (m < NewDegree+1) removed--;
2228 m = Degree + (i - l + 1) * step + 1;
2230 while (m > NewDegree+1) {
2232 m -= Mults(i) + step;
2235 if (m < NewDegree+1) removed--;
2237 return Mults.Length() - removed;
2240 //=======================================================================
2241 //function : IncreaseDegree
2243 //=======================================================================
2245 void BSplCLib::IncreaseDegree
2246 (const Standard_Integer Degree,
2247 const Standard_Integer NewDegree,
2248 const Standard_Boolean Periodic,
2249 const Standard_Integer Dimension,
2250 const TColStd_Array1OfReal& Poles,
2251 const TColStd_Array1OfReal& Knots,
2252 const TColStd_Array1OfInteger& Mults,
2253 TColStd_Array1OfReal& NewPoles,
2254 TColStd_Array1OfReal& NewKnots,
2255 TColStd_Array1OfInteger& NewMults)
2257 // Degree elevation of a BSpline Curve
2259 // This algorithms loops on degree incrementation from Degree to NewDegree.
2260 // The variable curDeg is the current degree to increment.
2262 // Before degree incrementations a "working curve" is created.
2263 // It has the same knot, poles and multiplicities.
2265 // If the curve is periodic knots are added on the working curve before
2266 // and after the existing knots to make it a non-periodic curves.
2267 // The poles are also copied.
2269 // The first and last multiplicity of the working curve are set to Degree+1,
2270 // null poles are added if necessary.
2272 // Then the degree is incremented on the working curve.
2273 // The knots are unchanged but all multiplicities will be incremented.
2275 // Each degree incrementation is achieved by averaging curDeg+1 curves.
2277 // See : Degree elevation of B-spline curves
2278 // Hartmut PRAUTZSCH
2282 //-------------------------
2283 // create the working curve
2284 //-------------------------
2286 Standard_Integer i,k,f,l,m,pf,pl,firstknot;
2288 pf = 0; // number of null poles added at beginning
2289 pl = 0; // number of null poles added at end
2291 Standard_Integer nbwknots = Knots.Length();
2292 f = FirstUKnotIndex(Degree,Mults);
2293 l = LastUKnotIndex (Degree,Mults);
2296 // Periodic curves are transformed in non-periodic curves
2298 nbwknots += f - Mults.Lower();
2302 for (i = Mults.Lower(); i <= f; i++)
2305 nbwknots += Mults.Upper() - l;
2309 for (i = l; i <= Mults.Upper(); i++)
2313 // copy the knots and multiplicities
2314 TColStd_Array1OfReal wknots(1,nbwknots);
2315 TColStd_Array1OfInteger wmults(1,nbwknots);
2321 // copy the knots for a periodic curve
2322 Standard_Real period = Knots(Knots.Upper()) - Knots(Knots.Lower());
2325 for (k = l; k < Knots.Upper(); k++) {
2327 wknots(i) = Knots(k) - period;
2328 wmults(i) = Mults(k);
2331 for (k = Knots.Lower(); k <= Knots.Upper(); k++) {
2333 wknots(i) = Knots(k);
2334 wmults(i) = Mults(k);
2337 for (k = Knots.Lower()+1; k <= f; k++) {
2339 wknots(i) = Knots(k) + period;
2340 wmults(i) = Mults(k);
2344 // set the first and last mults to Degree+1
2345 // and add null poles
2347 pf += Degree + 1 - wmults(1);
2348 wmults(1) = Degree + 1;
2349 pl += Degree + 1 - wmults(nbwknots);
2350 wmults(nbwknots) = Degree + 1;
2352 //---------------------------
2353 // poles of the working curve
2354 //---------------------------
2356 Standard_Integer nbwpoles = 0;
2358 for (i = 1; i <= nbwknots; i++) nbwpoles += wmults(i);
2359 nbwpoles -= Degree + 1;
2361 // we provide space for degree elevation
2362 TColStd_Array1OfReal
2363 wpoles(1,(nbwpoles + (nbwknots-1) * (NewDegree - Degree)) * Dimension);
2365 for (i = 1; i <= pf * Dimension; i++)
2370 for (i = pf * Dimension + 1; i <= (nbwpoles - pl) * Dimension; i++) {
2371 wpoles(i) = Poles(k);
2373 if (k > Poles.Upper()) k = Poles.Lower();
2376 for (i = (nbwpoles-pl)*Dimension+1; i <= nbwpoles*Dimension; i++)
2380 //-----------------------------------------------------------
2381 // Declare the temporary arrays used in degree incrementation
2382 //-----------------------------------------------------------
2384 Standard_Integer nbwp = nbwpoles + (nbwknots-1) * (NewDegree - Degree);
2385 // Arrays for storing the temporary curves
2386 TColStd_Array1OfReal tempc1(1,nbwp * Dimension);
2387 TColStd_Array1OfReal tempc2(1,nbwp * Dimension);
2389 // Array for storing the knots to insert
2390 TColStd_Array1OfReal iknots(1,nbwknots);
2392 // Arrays for receiving the knots after insertion
2393 TColStd_Array1OfReal nknots(1,nbwknots);
2397 //------------------------------
2398 // Loop on degree incrementation
2399 //------------------------------
2401 Standard_Integer step,curDeg;
2402 Standard_Integer nbp = nbwpoles;
2405 for (curDeg = Degree; curDeg < NewDegree; curDeg++) {
2407 nbp = nbwp; // current number of poles
2408 nbwp = nbp + nbwknots - 1; // new number of poles
2410 // For the averaging
2411 TColStd_Array1OfReal nwpoles(1,nbwp * Dimension);
2412 nwpoles.Init(0.0e0) ;
2415 for (step = 0; step <= curDeg; step++) {
2417 // Compute the bspline of rank step.
2419 // if not the first time, decrement the multiplicities back
2421 for (i = 1; i <= nbwknots; i++)
2425 // Poles are the current poles
2426 // but the poles congruent to step are duplicated.
2428 Standard_Integer offset = 0;
2430 for (i = 0; i < nbp; i++) {
2433 for (k = 0; k < Dimension; k++) {
2434 tempc1((offset-1)*Dimension+k+1) =
2435 wpoles(NewPoles.Lower()+i*Dimension+k);
2437 if (i % (curDeg+1) == step) {
2440 for (k = 0; k < Dimension; k++) {
2441 tempc1((offset-1)*Dimension+k+1) =
2442 wpoles(NewPoles.Lower()+i*Dimension+k);
2447 // Knots multiplicities are increased
2448 // For each knot where the sum of multiplicities is congruent to step
2450 Standard_Integer stepmult = step+1;
2451 Standard_Integer nbknots = 0;
2452 Standard_Integer smult = 0;
2454 for (k = 1; k <= nbwknots; k++) {
2456 if (smult >= stepmult) {
2457 // this knot is increased
2458 stepmult += curDeg+1;
2462 // this knot is inserted
2464 iknots(nbknots) = wknots(k);
2468 // the curve is obtained by inserting the knots
2469 // to raise the multiplicities
2471 // we build "slices" of the arrays to set the correct size
2473 TColStd_Array1OfReal aknots(iknots(1),1,nbknots);
2474 TColStd_Array1OfReal curve (tempc1(1),1,offset * Dimension);
2475 TColStd_Array1OfReal ncurve(tempc2(1),1,nbwp * Dimension);
2476 // InsertKnots(curDeg+1,Standard_False,Dimension,curve,wknots,wmults,
2477 // aknots,NoMults(),ncurve,nknots,wmults,Epsilon(1.));
2479 InsertKnots(curDeg+1,Standard_False,Dimension,curve,wknots,wmults,
2480 aknots,NoMults(),ncurve,nknots,wmults,0.0);
2482 // add to the average
2484 for (i = 1; i <= nbwp * Dimension; i++)
2485 nwpoles(i) += ncurve(i);
2488 // add to the average
2490 for (i = 1; i <= nbwp * Dimension; i++)
2491 nwpoles(i) += tempc1(i);
2495 // The result is the average
2497 for (i = 1; i <= nbwp * Dimension; i++) {
2498 wpoles(i) = nwpoles(i) / (curDeg+1);
2506 // index in new knots of the first knot of the curve
2508 firstknot = Mults.Upper() - l + 1;
2512 // the new curve starts at index firstknot
2513 // so we must remove knots until the sum of multiplicities
2514 // from the first to the start is NewDegree+1
2516 // m is the current sum of multiplicities
2519 for (k = 1; k <= firstknot; k++)
2522 // compute the new first knot (k), pf will be the index of the first pole
2526 while (m > NewDegree+1) {
2531 if (m < NewDegree+1) {
2533 wmults(k) += m - NewDegree - 1;
2534 pf += m - NewDegree - 1;
2537 // on a periodic curve the knots start with firstknot
2543 for (i = NewKnots.Lower(); i <= NewKnots.Upper(); i++) {
2544 NewKnots(i) = wknots(k);
2545 NewMults(i) = wmults(k);
2552 for (i = NewPoles.Lower(); i <= NewPoles.Upper(); i++) {
2554 NewPoles(i) = wpoles(pf);
2558 //=======================================================================
2559 //function : PrepareUnperiodize
2561 //=======================================================================
2563 void BSplCLib::PrepareUnperiodize
2564 (const Standard_Integer Degree,
2565 const TColStd_Array1OfInteger& Mults,
2566 Standard_Integer& NbKnots,
2567 Standard_Integer& NbPoles)
2570 // initialize NbKnots and NbPoles
2571 NbKnots = Mults.Length();
2572 NbPoles = - Degree - 1;
2574 for (i = Mults.Lower(); i <= Mults.Upper(); i++)
2575 NbPoles += Mults(i);
2577 Standard_Integer sigma, k;
2578 // Add knots at the beginning of the curve to raise Multiplicities
2580 sigma = Mults(Mults.Lower());
2581 k = Mults.Upper() - 1;
2583 while ( sigma < Degree + 1) {
2585 NbPoles += Mults(k);
2589 // We must add exactly until Degree + 1 ->
2590 // Supress the excedent.
2591 if ( sigma > Degree + 1)
2592 NbPoles -= sigma - Degree - 1;
2594 // Add knots at the end of the curve to raise Multiplicities
2596 sigma = Mults(Mults.Upper());
2597 k = Mults.Lower() + 1;
2599 while ( sigma < Degree + 1) {
2601 NbPoles += Mults(k);
2605 // We must add exactly until Degree + 1 ->
2606 // Supress the excedent.
2607 if ( sigma > Degree + 1)
2608 NbPoles -= sigma - Degree - 1;
2611 //=======================================================================
2612 //function : Unperiodize
2614 //=======================================================================
2616 void BSplCLib::Unperiodize
2617 (const Standard_Integer Degree,
2618 const Standard_Integer , // Dimension,
2619 const TColStd_Array1OfInteger& Mults,
2620 const TColStd_Array1OfReal& Knots,
2621 const TColStd_Array1OfReal& Poles,
2622 TColStd_Array1OfInteger& NewMults,
2623 TColStd_Array1OfReal& NewKnots,
2624 TColStd_Array1OfReal& NewPoles)
2626 Standard_Integer sigma, k, index = 0;
2627 // evaluation of index : number of knots to insert before knot(1) to
2628 // raise sum of multiplicities to <Degree + 1>
2629 sigma = Mults(Mults.Lower());
2630 k = Mults.Upper() - 1;
2632 while ( sigma < Degree + 1) {
2638 Standard_Real period = Knots(Knots.Upper()) - Knots(Knots.Lower());
2640 // set the 'interior' knots;
2642 for ( k = 1; k <= Knots.Length(); k++) {
2643 NewKnots ( k + index ) = Knots( k);
2644 NewMults ( k + index ) = Mults( k);
2647 // set the 'starting' knots;
2649 for ( k = 1; k <= index; k++) {
2650 NewKnots( k) = NewKnots( k + Knots.Length() - 1) - period;
2651 NewMults( k) = NewMults( k + Knots.Length() - 1);
2653 NewMults( 1) -= sigma - Degree -1;
2655 // set the 'ending' knots;
2656 sigma = NewMults( index + Knots.Length() );
2658 for ( k = Knots.Length() + index + 1; k <= NewKnots.Length(); k++) {
2659 NewKnots( k) = NewKnots( k - Knots.Length() + 1) + period;
2660 NewMults( k) = NewMults( k - Knots.Length() + 1);
2661 sigma += NewMults( k - Knots.Length() + 1);
2663 NewMults(NewMults.Length()) -= sigma - Degree - 1;
2665 for ( k = 1 ; k <= NewPoles.Length(); k++) {
2666 NewPoles(k ) = Poles( (k-1) % Poles.Length() + 1);
2670 //=======================================================================
2671 //function : PrepareTrimming
2673 //=======================================================================
2675 void BSplCLib::PrepareTrimming(const Standard_Integer Degree,
2676 const Standard_Boolean Periodic,
2677 const TColStd_Array1OfReal& Knots,
2678 const TColStd_Array1OfInteger& Mults,
2679 const Standard_Real U1,
2680 const Standard_Real U2,
2681 Standard_Integer& NbKnots,
2682 Standard_Integer& NbPoles)
2685 Standard_Real NewU1, NewU2;
2686 Standard_Integer index1 = 0, index2 = 0;
2688 // Eval index1, index2 : position of U1 and U2 in the Array Knots
2689 // such as Knots(index1-1) <= U1 < Knots(index1)
2690 // Knots(index2-1) <= U2 < Knots(index2)
2691 LocateParameter( Degree, Knots, Mults, U1, Periodic,
2692 Knots.Lower(), Knots.Upper(), index1, NewU1);
2693 LocateParameter( Degree, Knots, Mults, U2, Periodic,
2694 Knots.Lower(), Knots.Upper(), index2, NewU2);
2696 if ( Abs(Knots(index2) - U2) <= Epsilon( U1))
2700 NbKnots = index2 - index1 + 3;
2703 NbPoles = Degree + 1;
2705 for ( i = index1; i <= index2; i++)
2706 NbPoles += Mults(i);
2709 //=======================================================================
2710 //function : Trimming
2712 //=======================================================================
2714 void BSplCLib::Trimming(const Standard_Integer Degree,
2715 const Standard_Boolean Periodic,
2716 const Standard_Integer Dimension,
2717 const TColStd_Array1OfReal& Knots,
2718 const TColStd_Array1OfInteger& Mults,
2719 const TColStd_Array1OfReal& Poles,
2720 const Standard_Real U1,
2721 const Standard_Real U2,
2722 TColStd_Array1OfReal& NewKnots,
2723 TColStd_Array1OfInteger& NewMults,
2724 TColStd_Array1OfReal& NewPoles)
2726 Standard_Integer i, nbpoles, nbknots;
2727 Standard_Real kk[2];
2728 Standard_Integer mm[2];
2729 TColStd_Array1OfReal K( kk[0], 1, 2 );
2730 TColStd_Array1OfInteger M( mm[0], 1, 2 );
2732 K(1) = U1; K(2) = U2;
2733 mm[0] = mm[1] = Degree;
2734 if (!PrepareInsertKnots( Degree, Periodic, Knots, Mults, K, M,
2735 nbpoles, nbknots, Epsilon( U1), 0))
2736 Standard_OutOfRange::Raise();
2738 TColStd_Array1OfReal TempPoles(1, nbpoles*Dimension);
2739 TColStd_Array1OfReal TempKnots(1, nbknots);
2740 TColStd_Array1OfInteger TempMults(1, nbknots);
2743 // do not allow the multiplicities to Add : they must be less than Degree
2745 InsertKnots(Degree, Periodic, Dimension, Poles, Knots, Mults,
2746 K, M, TempPoles, TempKnots, TempMults, Epsilon(U1),
2749 // find in TempPoles the index of the pole corresponding to U1
2750 Standard_Integer Kindex = 0, Pindex;
2751 Standard_Real NewU1;
2752 LocateParameter( Degree, TempKnots, TempMults, U1, Periodic,
2753 TempKnots.Lower(), TempKnots.Upper(), Kindex, NewU1);
2754 Pindex = PoleIndex ( Degree, Kindex, Periodic, TempMults);
2755 Pindex *= Dimension;
2757 for ( i = 1; i <= NewPoles.Length(); i++) NewPoles(i) = TempPoles(Pindex + i);
2759 for ( i = 1; i <= NewKnots.Length(); i++) {
2760 NewKnots(i) = TempKnots( Kindex+i-1);
2761 NewMults(i) = TempMults( Kindex+i-1);
2763 NewMults(1) = Min(Degree, NewMults(1)) + 1 ;
2764 NewMults(NewMults.Length())= Min(Degree, NewMults(NewMults.Length())) + 1 ;
2767 //=======================================================================
2768 //function : Solves a LU factored Matrix
2770 //=======================================================================
2773 BSplCLib::SolveBandedSystem(const math_Matrix& Matrix,
2774 const Standard_Integer UpperBandWidth,
2775 const Standard_Integer LowerBandWidth,
2776 const Standard_Integer ArrayDimension,
2777 Standard_Real& Array)
2779 Standard_Integer ii,
2786 Standard_Real *PolesArray = &Array ;
2787 Standard_Real Inverse ;
2790 if (Matrix.LowerCol() != 1 ||
2791 Matrix.UpperCol() != UpperBandWidth + LowerBandWidth + 1) {
2796 for (ii = Matrix.LowerRow() + 1; ii <= Matrix.UpperRow() ; ii++) {
2797 MinIndex = (ii - LowerBandWidth >= Matrix.LowerRow() ?
2798 ii - LowerBandWidth : Matrix.LowerRow()) ;
2800 for ( jj = MinIndex ; jj < ii ; jj++) {
2802 for (kk = 0 ; kk < ArrayDimension ; kk++) {
2803 PolesArray[(ii-1) * ArrayDimension + kk] +=
2804 PolesArray[(jj-1) * ArrayDimension + kk] * Matrix(ii, jj - ii + LowerBandWidth + 1) ;
2809 for (ii = Matrix.UpperRow() ; ii >= Matrix.LowerRow() ; ii--) {
2810 MaxIndex = (ii + UpperBandWidth <= Matrix.UpperRow() ?
2811 ii + UpperBandWidth : Matrix.UpperRow()) ;
2813 for (jj = MaxIndex ; jj > ii ; jj--) {
2815 for (kk = 0 ; kk < ArrayDimension ; kk++) {
2816 PolesArray[(ii-1) * ArrayDimension + kk] -=
2817 PolesArray[(jj - 1) * ArrayDimension + kk] *
2818 Matrix(ii, jj - ii + LowerBandWidth + 1) ;
2822 //fixing a bug PRO18577 to avoid divizion by zero
2824 Standard_Real divizor = Matrix(ii,LowerBandWidth + 1) ;
2825 Standard_Real Toler = 1.0e-16;
2826 if ( Abs(divizor) > Toler )
2827 Inverse = 1.0e0 / divizor ;
2830 // cout << " BSplCLib::SolveBandedSystem() : zero determinant " << endl;
2835 for (kk = 0 ; kk < ArrayDimension ; kk++) {
2836 PolesArray[(ii-1) * ArrayDimension + kk] *= Inverse ;
2840 return (ReturnCode) ;
2843 //=======================================================================
2844 //function : Solves a LU factored Matrix
2846 //=======================================================================
2849 BSplCLib::SolveBandedSystem(const math_Matrix& Matrix,
2850 const Standard_Integer UpperBandWidth,
2851 const Standard_Integer LowerBandWidth,
2852 const Standard_Boolean HomogeneousFlag,
2853 const Standard_Integer ArrayDimension,
2854 Standard_Real& Poles,
2855 Standard_Real& Weights)
2857 Standard_Integer ii,
2862 Standard_Real Inverse,
2863 *PolesArray = &Poles,
2864 *WeightsArray = &Weights ;
2866 if (Matrix.LowerCol() != 1 ||
2867 Matrix.UpperCol() != UpperBandWidth + LowerBandWidth + 1) {
2871 if (HomogeneousFlag == Standard_False) {
2873 for (ii = 0 ; ii < Matrix.UpperRow() - Matrix.LowerRow() + 1; ii++) {
2875 for (kk = 0 ; kk < ArrayDimension ; kk++) {
2876 PolesArray[ii * ArrayDimension + kk] *=
2882 BSplCLib::SolveBandedSystem(Matrix,
2887 if (ErrorCode != 0) {
2892 BSplCLib::SolveBandedSystem(Matrix,
2897 if (ErrorCode != 0) {
2901 if (HomogeneousFlag == Standard_False) {
2903 for (ii = 0 ; ii < Matrix.UpperRow() - Matrix.LowerRow() + 1 ; ii++) {
2904 Inverse = 1.0e0 / WeightsArray[ii] ;
2906 for (kk = 0 ; kk < ArrayDimension ; kk++) {
2907 PolesArray[ii * ArrayDimension + kk] *= Inverse ;
2911 FINISH : return (ReturnCode) ;
2914 //=======================================================================
2915 //function : BuildSchoenbergPoints
2917 //=======================================================================
2919 void BSplCLib::BuildSchoenbergPoints(const Standard_Integer Degree,
2920 const TColStd_Array1OfReal& FlatKnots,
2921 TColStd_Array1OfReal& Parameters)
2923 Standard_Integer ii,
2925 Standard_Real Inverse ;
2926 Inverse = 1.0e0 / (Standard_Real)Degree ;
2928 for (ii = Parameters.Lower() ; ii <= Parameters.Upper() ; ii++) {
2929 Parameters(ii) = 0.0e0 ;
2931 for (jj = 1 ; jj <= Degree ; jj++) {
2932 Parameters(ii) += FlatKnots(jj + ii) ;
2934 Parameters(ii) *= Inverse ;
2938 //=======================================================================
2939 //function : Interpolate
2941 //=======================================================================
2943 void BSplCLib::Interpolate(const Standard_Integer Degree,
2944 const TColStd_Array1OfReal& FlatKnots,
2945 const TColStd_Array1OfReal& Parameters,
2946 const TColStd_Array1OfInteger& ContactOrderArray,
2947 const Standard_Integer ArrayDimension,
2948 Standard_Real& Poles,
2949 Standard_Integer& InversionProblem)
2951 Standard_Integer ErrorCode,
2954 // Standard_Real *PolesArray = &Poles ;
2955 math_Matrix InterpolationMatrix(1, Parameters.Length(),
2956 1, 2 * Degree + 1) ;
2958 BSplCLib::BuildBSpMatrix(Parameters,
2962 InterpolationMatrix,
2965 Standard_OutOfRange_Raise_if (ErrorCode != 0, "BSplCLib::Interpolate") ;
2968 BSplCLib::FactorBandedMatrix(InterpolationMatrix,
2972 Standard_OutOfRange_Raise_if (ErrorCode != 0, "BSplCLib::Interpolate") ;
2975 BSplCLib::SolveBandedSystem(InterpolationMatrix,
2981 Standard_OutOfRange_Raise_if (ErrorCode != 0,"BSplCLib::Interpolate") ;
2984 //=======================================================================
2985 //function : Interpolate
2987 //=======================================================================
2989 void BSplCLib::Interpolate(const Standard_Integer Degree,
2990 const TColStd_Array1OfReal& FlatKnots,
2991 const TColStd_Array1OfReal& Parameters,
2992 const TColStd_Array1OfInteger& ContactOrderArray,
2993 const Standard_Integer ArrayDimension,
2994 Standard_Real& Poles,
2995 Standard_Real& Weights,
2996 Standard_Integer& InversionProblem)
2998 Standard_Integer ErrorCode,
3002 math_Matrix InterpolationMatrix(1, Parameters.Length(),
3003 1, 2 * Degree + 1) ;
3005 BSplCLib::BuildBSpMatrix(Parameters,
3009 InterpolationMatrix,
3012 Standard_OutOfRange_Raise_if (ErrorCode != 0, "BSplCLib::Interpolate") ;
3015 BSplCLib::FactorBandedMatrix(InterpolationMatrix,
3019 Standard_OutOfRange_Raise_if (ErrorCode != 0, "BSplCLib::Interpolate") ;
3022 BSplCLib::SolveBandedSystem(InterpolationMatrix,
3030 Standard_OutOfRange_Raise_if (ErrorCode != 0,"BSplCLib::Interpolate") ;
3033 //=======================================================================
3034 //function : Evaluates a Bspline function : uses the ExtrapMode
3035 //purpose : the function is extrapolated using the Taylor expansion
3036 // of degree ExtrapMode[0] to the left and the Taylor
3037 // expansion of degree ExtrapMode[1] to the right
3038 // this evaluates the numerator by multiplying by the weights
3039 // and evaluating it but does not call RationalDerivatives after
3040 //=======================================================================
3043 (const Standard_Real Parameter,
3044 const Standard_Boolean PeriodicFlag,
3045 const Standard_Integer DerivativeRequest,
3046 Standard_Integer& ExtrapMode,
3047 const Standard_Integer Degree,
3048 const TColStd_Array1OfReal& FlatKnots,
3049 const Standard_Integer ArrayDimension,
3050 Standard_Real& Poles,
3051 Standard_Real& Weights,
3052 Standard_Real& PolesResults,
3053 Standard_Real& WeightsResults)
3055 Standard_Integer ii,
3064 ExtrapolatingFlag[2],
3068 FirstNonZeroBsplineIndex,
3069 LocalRequest = DerivativeRequest ;
3070 Standard_Real *PResultArray,
3078 PolesArray = &Poles ;
3079 WeightsArray = &Weights ;
3080 ExtrapModeArray = &ExtrapMode ;
3081 PResultArray = &PolesResults ;
3082 WResultArray = &WeightsResults ;
3083 LocalParameter = Parameter ;
3084 ExtrapolatingFlag[0] =
3085 ExtrapolatingFlag[1] = 0 ;
3087 // check if we are extrapolating to a degree which is smaller than
3088 // the degree of the Bspline
3091 Period = FlatKnots(FlatKnots.Upper() - 1) - FlatKnots(2) ;
3093 while (LocalParameter > FlatKnots(FlatKnots.Upper() - 1)) {
3094 LocalParameter -= Period ;
3097 while (LocalParameter < FlatKnots(2)) {
3098 LocalParameter += Period ;
3101 if (Parameter < FlatKnots(2) &&
3102 LocalRequest < ExtrapModeArray[0] &&
3103 ExtrapModeArray[0] < Degree) {
3104 LocalRequest = ExtrapModeArray[0] ;
3105 LocalParameter = FlatKnots(2) ;
3106 ExtrapolatingFlag[0] = 1 ;
3108 if (Parameter > FlatKnots(FlatKnots.Upper()-1) &&
3109 LocalRequest < ExtrapModeArray[1] &&
3110 ExtrapModeArray[1] < Degree) {
3111 LocalRequest = ExtrapModeArray[1] ;
3112 LocalParameter = FlatKnots(FlatKnots.Upper()-1) ;
3113 ExtrapolatingFlag[1] = 1 ;
3115 Delta = Parameter - LocalParameter ;
3116 if (LocalRequest >= Order) {
3117 LocalRequest = Degree ;
3120 Modulus = FlatKnots.Length() - Degree -1 ;
3123 Modulus = FlatKnots.Length() - Degree ;
3126 BSplCLib_LocalMatrix BsplineBasis (LocalRequest, Order);
3128 BSplCLib::EvalBsplineBasis(1,
3133 FirstNonZeroBsplineIndex,
3135 if (ErrorCode != 0) {
3139 if (ExtrapolatingFlag[0] == 0 && ExtrapolatingFlag[1] == 0) {
3143 for (ii = 1 ; ii <= LocalRequest + 1 ; ii++) {
3144 Index1 = FirstNonZeroBsplineIndex ;
3146 for (kk = 0 ; kk < ArrayDimension ; kk++) {
3147 PResultArray[Index + kk] = 0.0e0 ;
3149 WResultArray[Index] = 0.0e0 ;
3151 for (jj = 1 ; jj <= Order ; jj++) {
3153 for (kk = 0 ; kk < ArrayDimension ; kk++) {
3154 PResultArray[Index + kk] +=
3155 PolesArray[(Index1-1) * ArrayDimension + kk]
3156 * WeightsArray[Index1-1] * BsplineBasis(ii,jj) ;
3158 WResultArray[Index2] += WeightsArray[Index1-1] * BsplineBasis(ii,jj) ;
3160 Index1 = Index1 % Modulus ;
3163 Index += ArrayDimension ;
3169 // store Taylor expansion in LocalRealArray
3171 NewRequest = DerivativeRequest ;
3172 if (NewRequest > Degree) {
3173 NewRequest = Degree ;
3175 BSplCLib_LocalArray LocalRealArray((LocalRequest + 1)*ArrayDimension);
3179 for (ii = 1 ; ii <= LocalRequest + 1 ; ii++) {
3180 Index1 = FirstNonZeroBsplineIndex ;
3182 for (kk = 0 ; kk < ArrayDimension ; kk++) {
3183 LocalRealArray[Index + kk] = 0.0e0 ;
3186 for (jj = 1 ; jj <= Order ; jj++) {
3188 for (kk = 0 ; kk < ArrayDimension ; kk++) {
3189 LocalRealArray[Index + kk] +=
3190 PolesArray[(Index1-1)*ArrayDimension + kk] *
3191 WeightsArray[Index1-1] * BsplineBasis(ii,jj) ;
3193 Index1 = Index1 % Modulus ;
3197 for (kk = 0 ; kk < ArrayDimension ; kk++) {
3198 LocalRealArray[Index + kk] *= Inverse ;
3200 Index += ArrayDimension ;
3201 Inverse /= (Standard_Real) ii ;
3203 PLib::EvalPolynomial(Delta,
3212 for (ii = 1 ; ii <= LocalRequest + 1 ; ii++) {
3213 Index1 = FirstNonZeroBsplineIndex ;
3214 LocalRealArray[Index] = 0.0e0 ;
3216 for (jj = 1 ; jj <= Order ; jj++) {
3217 LocalRealArray[Index] +=
3218 WeightsArray[Index1-1] * BsplineBasis(ii,jj) ;
3219 Index1 = Index1 % Modulus ;
3222 LocalRealArray[Index + kk] *= Inverse ;
3224 Inverse /= (Standard_Real) ii ;
3226 PLib::EvalPolynomial(Delta,
3236 //=======================================================================
3237 //function : Evaluates a Bspline function : uses the ExtrapMode
3238 //purpose : the function is extrapolated using the Taylor expansion
3239 // of degree ExtrapMode[0] to the left and the Taylor
3240 // expansion of degree ExtrapMode[1] to the right
3241 // WARNING : the array Results is supposed to have at least
3242 // (DerivativeRequest + 1) * ArrayDimension slots and the
3244 //=======================================================================
3247 (const Standard_Real Parameter,
3248 const Standard_Boolean PeriodicFlag,
3249 const Standard_Integer DerivativeRequest,
3250 Standard_Integer& ExtrapMode,
3251 const Standard_Integer Degree,
3252 const TColStd_Array1OfReal& FlatKnots,
3253 const Standard_Integer ArrayDimension,
3254 Standard_Real& Poles,
3255 Standard_Real& Results)
3257 Standard_Integer ii,
3265 ExtrapolatingFlag[2],
3269 FirstNonZeroBsplineIndex,
3270 LocalRequest = DerivativeRequest ;
3272 Standard_Real *ResultArray,
3279 PolesArray = &Poles ;
3280 ExtrapModeArray = &ExtrapMode ;
3281 ResultArray = &Results ;
3282 LocalParameter = Parameter ;
3283 ExtrapolatingFlag[0] =
3284 ExtrapolatingFlag[1] = 0 ;
3286 // check if we are extrapolating to a degree which is smaller than
3287 // the degree of the Bspline
3290 Period = FlatKnots(FlatKnots.Upper() - 1) - FlatKnots(2) ;
3292 while (LocalParameter > FlatKnots(FlatKnots.Upper() - 1)) {
3293 LocalParameter -= Period ;
3296 while (LocalParameter < FlatKnots(2)) {
3297 LocalParameter += Period ;
3300 if (Parameter < FlatKnots(2) &&
3301 LocalRequest < ExtrapModeArray[0] &&
3302 ExtrapModeArray[0] < Degree) {
3303 LocalRequest = ExtrapModeArray[0] ;
3304 LocalParameter = FlatKnots(2) ;
3305 ExtrapolatingFlag[0] = 1 ;
3307 if (Parameter > FlatKnots(FlatKnots.Upper()-1) &&
3308 LocalRequest < ExtrapModeArray[1] &&
3309 ExtrapModeArray[1] < Degree) {
3310 LocalRequest = ExtrapModeArray[1] ;
3311 LocalParameter = FlatKnots(FlatKnots.Upper()-1) ;
3312 ExtrapolatingFlag[1] = 1 ;
3314 Delta = Parameter - LocalParameter ;
3315 if (LocalRequest >= Order) {
3316 LocalRequest = Degree ;
3320 Modulus = FlatKnots.Length() - Degree -1 ;
3323 Modulus = FlatKnots.Length() - Degree ;
3326 BSplCLib_LocalMatrix BsplineBasis (LocalRequest, Order);
3329 BSplCLib::EvalBsplineBasis(1,
3334 FirstNonZeroBsplineIndex,
3336 if (ErrorCode != 0) {
3340 if (ExtrapolatingFlag[0] == 0 && ExtrapolatingFlag[1] == 0) {
3343 for (ii = 1 ; ii <= LocalRequest + 1 ; ii++) {
3344 Index1 = FirstNonZeroBsplineIndex ;
3346 for (kk = 0 ; kk < ArrayDimension ; kk++) {
3347 ResultArray[Index + kk] = 0.0e0 ;
3350 for (jj = 1 ; jj <= Order ; jj++) {
3352 for (kk = 0 ; kk < ArrayDimension ; kk++) {
3353 ResultArray[Index + kk] +=
3354 PolesArray[(Index1-1) * ArrayDimension + kk] * BsplineBasis(ii,jj) ;
3356 Index1 = Index1 % Modulus ;
3359 Index += ArrayDimension ;
3364 // store Taylor expansion in LocalRealArray
3366 NewRequest = DerivativeRequest ;
3367 if (NewRequest > Degree) {
3368 NewRequest = Degree ;
3370 BSplCLib_LocalArray LocalRealArray((LocalRequest + 1)*ArrayDimension);
3375 for (ii = 1 ; ii <= LocalRequest + 1 ; ii++) {
3376 Index1 = FirstNonZeroBsplineIndex ;
3378 for (kk = 0 ; kk < ArrayDimension ; kk++) {
3379 LocalRealArray[Index + kk] = 0.0e0 ;
3382 for (jj = 1 ; jj <= Order ; jj++) {
3384 for (kk = 0 ; kk < ArrayDimension ; kk++) {
3385 LocalRealArray[Index + kk] +=
3386 PolesArray[(Index1-1)*ArrayDimension + kk] * BsplineBasis(ii,jj) ;
3388 Index1 = Index1 % Modulus ;
3392 for (kk = 0 ; kk < ArrayDimension ; kk++) {
3393 LocalRealArray[Index + kk] *= Inverse ;
3395 Index += ArrayDimension ;
3396 Inverse /= (Standard_Real) ii ;
3398 PLib::EvalPolynomial(Delta,
3408 //=======================================================================
3409 //function : TangExtendToConstraint
3410 //purpose : Extends a Bspline function using the tangency map
3414 //=======================================================================
3416 void BSplCLib::TangExtendToConstraint
3417 (const TColStd_Array1OfReal& FlatKnots,
3418 const Standard_Real C1Coefficient,
3419 const Standard_Integer NumPoles,
3420 Standard_Real& Poles,
3421 const Standard_Integer CDimension,
3422 const Standard_Integer CDegree,
3423 const TColStd_Array1OfReal& ConstraintPoint,
3424 const Standard_Integer Continuity,
3425 const Standard_Boolean After,
3426 Standard_Integer& NbPolesResult,
3427 Standard_Integer& NbKnotsResult,
3428 Standard_Real& KnotsResult,
3429 Standard_Real& PolesResult)
3432 if (CDegree<Continuity+1) {
3433 cout<<"The BSpline degree must be greater than the order of continuity"<<endl;
3436 Standard_Real * Padr = &Poles ;
3437 Standard_Real * KRadr = &KnotsResult ;
3438 Standard_Real * PRadr = &PolesResult ;
3440 ////////////////////////////////////////////////////////////////////////
3442 // 1. calculation of extension nD
3444 ////////////////////////////////////////////////////////////////////////
3447 Standard_Integer Csize = Continuity + 2;
3448 math_Matrix MatCoefs(1,Csize, 1,Csize);
3450 PLib::HermiteCoefficients(0, 1, // Limits
3451 Continuity, 0, // Orders of constraints
3455 PLib::HermiteCoefficients(0, 1, // Limits
3456 0, Continuity, // Orders of constraints
3461 // position at the node of connection
3462 Standard_Real Tbord ;
3464 Tbord = FlatKnots(FlatKnots.Upper()-CDegree);
3467 Tbord = FlatKnots(FlatKnots.Lower()+CDegree);
3469 Standard_Boolean periodic_flag = Standard_False ;
3470 Standard_Integer ipos, extrap_mode[2], derivative_request = Max(Continuity,1);
3471 extrap_mode[0] = extrap_mode[1] = CDegree;
3472 TColStd_Array1OfReal EvalBS(1, CDimension * (derivative_request+1)) ;
3473 Standard_Real * Eadr = (Standard_Real *) &EvalBS(1) ;
3474 BSplCLib::Eval(Tbord,periodic_flag,derivative_request,extrap_mode[0],
3475 CDegree,FlatKnots,CDimension,Poles,*Eadr);
3477 // norm of the tangent at the node of connection
3478 math_Vector Tgte(1,CDimension);
3480 for (ipos=1;ipos<=CDimension;ipos++) {
3481 Tgte(ipos) = EvalBS(ipos+CDimension);
3483 Standard_Real L1=Tgte.Norm();
3486 // matrix of constraints
3487 math_Matrix Contraintes(1,Csize,1,CDimension);
3490 for (ipos=1;ipos<=CDimension;ipos++) {
3491 Contraintes(1,ipos) = EvalBS(ipos);
3492 Contraintes(2,ipos) = C1Coefficient * EvalBS(ipos+CDimension);
3493 if(Continuity >= 2) Contraintes(3,ipos) = EvalBS(ipos+2*CDimension) * Pow(C1Coefficient,2);
3494 if(Continuity >= 3) Contraintes(4,ipos) = EvalBS(ipos+3*CDimension) * Pow(C1Coefficient,3);
3495 Contraintes(Continuity+2,ipos) = ConstraintPoint(ipos);
3500 for (ipos=1;ipos<=CDimension;ipos++) {
3501 Contraintes(1,ipos) = ConstraintPoint(ipos);
3502 Contraintes(2,ipos) = EvalBS(ipos);
3503 if(Continuity >= 1) Contraintes(3,ipos) = C1Coefficient * EvalBS(ipos+CDimension);
3504 if(Continuity >= 2) Contraintes(4,ipos) = EvalBS(ipos+2*CDimension) * Pow(C1Coefficient,2);
3505 if(Continuity >= 3) Contraintes(5,ipos) = EvalBS(ipos+3*CDimension) * Pow(C1Coefficient,3);
3509 // calculate the coefficients of extension
3510 Standard_Integer ii, jj, kk;
3511 TColStd_Array1OfReal ExtraCoeffs(1,Csize*CDimension);
3512 ExtraCoeffs.Init(0.);
3514 for (ii=1; ii<=Csize; ii++) {
3516 for (jj=1; jj<=Csize; jj++) {
3518 for (kk=1; kk<=CDimension; kk++) {
3519 ExtraCoeffs(kk+(jj-1)*CDimension) += MatCoefs(ii,jj)*Contraintes(ii,kk);
3524 // calculate the poles of extension
3525 TColStd_Array1OfReal ExtrapPoles(1,Csize*CDimension);
3526 Standard_Real * EPadr = &ExtrapPoles(1) ;
3527 PLib::CoefficientsPoles(CDimension,
3528 ExtraCoeffs, PLib::NoWeights(),
3529 ExtrapPoles, PLib::NoWeights());
3531 // calculate the nodes of extension with multiplicities
3532 TColStd_Array1OfReal ExtrapNoeuds(1,2);
3533 ExtrapNoeuds(1) = 0.;
3534 ExtrapNoeuds(2) = 1.;
3535 TColStd_Array1OfInteger ExtrapMults(1,2);
3536 ExtrapMults(1) = Csize;
3537 ExtrapMults(2) = Csize;
3539 // flat nodes of extension
3540 TColStd_Array1OfReal FK2(1, Csize*2);
3541 BSplCLib::KnotSequence(ExtrapNoeuds,ExtrapMults,FK2);
3543 // norm of the tangent at the connection point
3545 BSplCLib::Eval(0.,periodic_flag,1,extrap_mode[0],
3546 Csize-1,FK2,CDimension,*EPadr,*Eadr);
3549 BSplCLib::Eval(1.,periodic_flag,1,extrap_mode[0],
3550 Csize-1,FK2,CDimension,*EPadr,*Eadr);
3553 for (ipos=1;ipos<=CDimension;ipos++) {
3554 Tgte(ipos) = EvalBS(ipos+CDimension);
3556 Standard_Real L2 = Tgte.Norm();
3558 // harmonisation of degrees
3559 TColStd_Array1OfReal NewP2(1, (CDegree+1)*CDimension);
3560 TColStd_Array1OfReal NewK2(1, 2);
3561 TColStd_Array1OfInteger NewM2(1, 2);
3562 if (Csize-1<CDegree) {
3563 BSplCLib::IncreaseDegree(Csize-1,CDegree,Standard_False,CDimension,
3564 ExtrapPoles,ExtrapNoeuds,ExtrapMults,
3568 NewP2 = ExtrapPoles;
3569 NewK2 = ExtrapNoeuds;
3570 NewM2 = ExtrapMults;
3573 // flat nodes of extension after harmonization of degrees
3574 TColStd_Array1OfReal NewFK2(1, (CDegree+1)*2);
3575 BSplCLib::KnotSequence(NewK2,NewM2,NewFK2);
3578 ////////////////////////////////////////////////////////////////////////
3580 // 2. concatenation C0
3582 ////////////////////////////////////////////////////////////////////////
3584 // ratio of reparametrization
3585 Standard_Real Ratio=1, Delta;
3586 if ( (L1 > Precision::Confusion()) && (L2 > Precision::Confusion()) ) {
3589 if ( (Ratio < 1.e-5) || (Ratio > 1.e5) ) Ratio = 1;
3592 // do not touch the first BSpline
3593 Delta = Ratio*NewFK2(NewFK2.Lower()) - FlatKnots(FlatKnots.Upper());
3596 // do not touch the second BSpline
3597 Delta = Ratio*NewFK2(NewFK2.Upper()) - FlatKnots(FlatKnots.Lower());
3600 // result of the concatenation
3601 Standard_Integer NbP1 = NumPoles, NbP2 = CDegree+1;
3602 Standard_Integer NbK1 = FlatKnots.Length(), NbK2 = 2*(CDegree+1);
3603 TColStd_Array1OfReal NewPoles (1, (NbP1+ NbP2-1)*CDimension);
3604 TColStd_Array1OfReal NewFlats (1, NbK1+NbK2-CDegree-2);
3607 Standard_Integer indNP, indP, indEP;
3610 for (ii=1; ii<=NbP1+NbP2-1; ii++) {
3612 for (jj=1; jj<=CDimension; jj++) {
3613 indNP = (ii-1)*CDimension+jj;
3614 indP = (ii-1)*CDimension+jj-1;
3615 indEP = (ii-NbP1)*CDimension+jj;
3616 if (ii<NbP1) NewPoles(indNP) = Padr[indP];
3617 else NewPoles(indNP) = NewP2(indEP);
3623 for (ii=1; ii<=NbP1+NbP2-1; ii++) {
3625 for (jj=1; jj<=CDimension; jj++) {
3626 indNP = (ii-1)*CDimension+jj;
3627 indEP = (ii-1)*CDimension+jj;
3628 indP = (ii-NbP2)*CDimension+jj-1;
3629 if (ii<NbP2) NewPoles(indNP) = NewP2(indEP);
3630 else NewPoles(indNP) = Padr[indP];
3637 // start with the nodes of the initial surface
3639 for (ii=1; ii<NbK1; ii++) {
3640 NewFlats(ii) = FlatKnots(FlatKnots.Lower()+ii-1);
3642 // continue with the reparameterized nodes of the extension
3644 for (ii=1; ii<=NbK2-CDegree-1; ii++) {
3645 NewFlats(NbK1+ii-1) = Ratio*NewFK2(NewFK2.Lower()+ii+CDegree) - Delta;
3649 // start with the reparameterized nodes of the extension
3651 for (ii=1; ii<NbK2-CDegree; ii++) {
3652 NewFlats(ii) = Ratio*NewFK2(NewFK2.Lower()+ii-1) - Delta;
3654 // continue with the nodes of the initial surface
3656 for (ii=2; ii<=NbK1; ii++) {
3657 NewFlats(NbK2+ii-CDegree-2) = FlatKnots(FlatKnots.Lower()+ii-1);
3662 ////////////////////////////////////////////////////////////////////////
3664 // 3. reduction of multiplicite at the node of connection
3666 ////////////////////////////////////////////////////////////////////////
3668 // number of separate nodes
3669 Standard_Integer KLength = 1;
3671 for (ii=2; ii<=NbK1+NbK2-CDegree-2;ii++) {
3672 if (NewFlats(ii) != NewFlats(ii-1)) KLength++;
3675 // flat nodes --> nodes + multiplicities
3676 TColStd_Array1OfReal NewKnots (1, KLength);
3677 TColStd_Array1OfInteger NewMults (1, KLength);
3680 NewKnots(jj) = NewFlats(1);
3682 for (ii=2; ii<=NbK1+NbK2-CDegree-2;ii++) {
3683 if (NewFlats(ii) == NewFlats(ii-1)) NewMults(jj)++;
3686 NewKnots(jj) = NewFlats(ii);
3690 // reduction of multiplicity at the second or the last but one node
3691 Standard_Integer Index = 2, M = CDegree;
3692 if (After) Index = KLength-1;
3693 TColStd_Array1OfReal ResultPoles (1, (NbP1+ NbP2-1)*CDimension);
3694 TColStd_Array1OfReal ResultKnots (1, KLength);
3695 TColStd_Array1OfInteger ResultMults (1, KLength);
3696 Standard_Real Tol = 1.e-6;
3697 Standard_Boolean Ok = Standard_True;
3699 while ( (M>CDegree-Continuity) && Ok) {
3700 Ok = RemoveKnot(Index, M-1, CDegree, Standard_False, CDimension,
3701 NewPoles, NewKnots, NewMults,
3702 ResultPoles, ResultKnots, ResultMults, Tol);
3707 // number of poles of the concatenation
3708 NbPolesResult = NbP1 + NbP2 - 1;
3709 // the poles of the concatenation
3710 Standard_Integer PLength = NbPolesResult*CDimension;
3712 for (jj=1; jj<=PLength; jj++) {
3713 PRadr[jj-1] = NewPoles(jj);
3716 // flat nodes of the concatenation
3717 Standard_Integer ideb = 0;
3719 for (jj=0; jj<NewKnots.Length(); jj++) {
3720 for (ii=0; ii<NewMults(jj+1); ii++) {
3721 KRadr[ideb+ii] = NewKnots(jj+1);
3723 ideb += NewMults(jj+1);
3725 NbKnotsResult = ideb;
3729 // number of poles of the result
3730 NbPolesResult = NbP1 + NbP2 - 1 - CDegree + M;
3731 // the poles of the result
3732 Standard_Integer PLength = NbPolesResult*CDimension;
3734 for (jj=0; jj<PLength; jj++) {
3735 PRadr[jj] = ResultPoles(jj+1);
3738 // flat nodes of the result
3739 Standard_Integer ideb = 0;
3741 for (jj=0; jj<ResultKnots.Length(); jj++) {
3742 for (ii=0; ii<ResultMults(jj+1); ii++) {
3743 KRadr[ideb+ii] = ResultKnots(jj+1);
3745 ideb += ResultMults(jj+1);
3747 NbKnotsResult = ideb;
3751 //=======================================================================
3752 //function : Resolution
3755 // Let C(t) = SUM Ci Bi(t) a Bspline curve of degree d
3757 // with nodes tj for j = 1,n+d+1
3761 // Then C (t) = SUM d * --------- Bi (t)
3762 // i = 2,n ti+d - ti
3765 // for the base of BSpline Bi (t) of degree d-1.
3767 // Consequently the upper bound of the norm of the derivative from C is :
3771 // d * Max | --------- |
3772 // i = 2,n | ti+d - ti |
3775 // In the rational case set C(t) = -----
3779 // D(t) = SUM Di Bi(t)
3782 // N(t) = SUM Di * Ci Bi(t)
3785 // N'(t) - D'(t) C(t)
3786 // C'(t) = -----------------------
3790 // N'(t) - D'(t) C(t) =
3792 // Di * (Ci - C(t)) - Di-1 * (Ci-1 - C(t)) d-1
3793 // SUM d * ---------------------------------------- * Bi (t) =
3797 // Di * (Ci - Cj) - Di-1 * (Ci-1 - Cj) d-1
3798 // SUM SUM d * ----------------------------------- * Betaj(t) * Bi (t)
3799 //i=2,n j=1,n ti+d - ti
3804 // Betaj(t) = --------
3807 // Betaj(t) form a partition >= 0 of the entity with support
3808 // tj, tj+d+1. Consequently if Rj = {j-d, ...., j+d+d+1}
3809 // obtain an upper bound of the derivative of C by taking :
3816 // Di * (Ci - Cj) - Di-1 * (Ci-1 - Cj)
3817 // Max Max d * -----------------------------------
3818 // j=1,n i dans Rj ti+d - ti
3820 // --------------------------------------------------------
3826 //=======================================================================
3828 void BSplCLib::Resolution( Standard_Real& Poles,
3829 const Standard_Integer ArrayDimension,
3830 const Standard_Integer NumPoles,
3831 const TColStd_Array1OfReal& Weights,
3832 const TColStd_Array1OfReal& FlatKnots,
3833 const Standard_Integer Degree,
3834 const Standard_Real Tolerance3D,
3835 Standard_Real& UTolerance)
3837 Standard_Integer ii,num_poles,ii_index,jj_index,ii_inDim;
3838 Standard_Integer lower,upper,ii_minus,jj,ii_miDim;
3839 Standard_Integer Deg1 = Degree + 1;
3840 Standard_Integer Deg2 = (Degree << 1) + 1;
3841 Standard_Real value,factor,W,min_weights,inverse;
3842 Standard_Real pa_ii_inDim_0, pa_ii_inDim_1, pa_ii_inDim_2, pa_ii_inDim_3;
3843 Standard_Real pa_ii_miDim_0, pa_ii_miDim_1, pa_ii_miDim_2, pa_ii_miDim_3;
3844 Standard_Real wg_ii_index, wg_ii_minus;
3845 Standard_Real *PA,max_derivative;
3846 const Standard_Real * FK = &FlatKnots(FlatKnots.Lower());
3848 max_derivative = 0.0e0;
3849 num_poles = FlatKnots.Length() - Deg1;
3850 switch (ArrayDimension) {
3852 if (&Weights != NULL) {
3853 const Standard_Real * WG = &Weights(Weights.Lower());
3854 min_weights = WG[0];
3856 for (ii = 1 ; ii < NumPoles ; ii++) {
3858 if (W < min_weights) min_weights = W;
3861 for (ii = 1 ; ii < num_poles ; ii++) {
3862 ii_index = ii % NumPoles;
3863 ii_inDim = ii_index << 1;
3864 ii_minus = (ii - 1) % NumPoles;
3865 ii_miDim = ii_minus << 1;
3866 pa_ii_inDim_0 = PA[ii_inDim]; ii_inDim++;
3867 pa_ii_inDim_1 = PA[ii_inDim];
3868 pa_ii_miDim_0 = PA[ii_miDim]; ii_miDim++;
3869 pa_ii_miDim_1 = PA[ii_miDim];
3870 wg_ii_index = WG[ii_index];
3871 wg_ii_minus = WG[ii_minus];
3872 inverse = FK[ii + Degree] - FK[ii];
3873 inverse = 1.0e0 / inverse;
3875 if (lower < 0) lower = 0;
3877 if (upper > num_poles) upper = num_poles;
3879 for (jj = lower ; jj < upper ; jj++) {
3880 jj_index = jj % NumPoles;
3881 jj_index = jj_index << 1;
3883 factor = (((PA[jj_index] - pa_ii_inDim_0) * wg_ii_index) -
3884 ((PA[jj_index] - pa_ii_miDim_0) * wg_ii_minus));
3885 if (factor < 0) factor = - factor;
3886 value += factor; jj_index++;
3887 factor = (((PA[jj_index] - pa_ii_inDim_1) * wg_ii_index) -
3888 ((PA[jj_index] - pa_ii_miDim_1) * wg_ii_minus));
3889 if (factor < 0) factor = - factor;
3892 if (max_derivative < value) max_derivative = value;
3895 max_derivative /= min_weights;
3899 for (ii = 1 ; ii < num_poles ; ii++) {
3900 ii_index = ii % NumPoles;
3901 ii_index = ii_index << 1;
3902 ii_minus = (ii - 1) % NumPoles;
3903 ii_minus = ii_minus << 1;
3904 inverse = FK[ii + Degree] - FK[ii];
3905 inverse = 1.0e0 / inverse;
3907 factor = PA[ii_index] - PA[ii_minus];
3908 if (factor < 0) factor = - factor;
3909 value += factor; ii_index++; ii_minus++;
3910 factor = PA[ii_index] - PA[ii_minus];
3911 if (factor < 0) factor = - factor;
3914 if (max_derivative < value) max_derivative = value;
3920 if (&Weights != NULL) {
3921 const Standard_Real * WG = &Weights(Weights.Lower());
3922 min_weights = WG[0];
3924 for (ii = 1 ; ii < NumPoles ; ii++) {
3926 if (W < min_weights) min_weights = W;
3929 for (ii = 1 ; ii < num_poles ; ii++) {
3930 ii_index = ii % NumPoles;
3931 ii_inDim = (ii_index << 1) + ii_index;
3932 ii_minus = (ii - 1) % NumPoles;
3933 ii_miDim = (ii_minus << 1) + ii_minus;
3934 pa_ii_inDim_0 = PA[ii_inDim]; ii_inDim++;
3935 pa_ii_inDim_1 = PA[ii_inDim]; ii_inDim++;
3936 pa_ii_inDim_2 = PA[ii_inDim];
3937 pa_ii_miDim_0 = PA[ii_miDim]; ii_miDim++;
3938 pa_ii_miDim_1 = PA[ii_miDim]; ii_miDim++;
3939 pa_ii_miDim_2 = PA[ii_miDim];
3940 wg_ii_index = WG[ii_index];
3941 wg_ii_minus = WG[ii_minus];
3942 inverse = FK[ii + Degree] - FK[ii];
3943 inverse = 1.0e0 / inverse;
3945 if (lower < 0) lower = 0;
3947 if (upper > num_poles) upper = num_poles;
3949 for (jj = lower ; jj < upper ; jj++) {
3950 jj_index = jj % NumPoles;
3951 jj_index = (jj_index << 1) + jj_index;
3953 factor = (((PA[jj_index] - pa_ii_inDim_0) * wg_ii_index) -
3954 ((PA[jj_index] - pa_ii_miDim_0) * wg_ii_minus));
3955 if (factor < 0) factor = - factor;
3956 value += factor; jj_index++;
3957 factor = (((PA[jj_index] - pa_ii_inDim_1) * wg_ii_index) -
3958 ((PA[jj_index] - pa_ii_miDim_1) * wg_ii_minus));
3959 if (factor < 0) factor = - factor;
3960 value += factor; jj_index++;
3961 factor = (((PA[jj_index] - pa_ii_inDim_2) * wg_ii_index) -
3962 ((PA[jj_index] - pa_ii_miDim_2) * wg_ii_minus));
3963 if (factor < 0) factor = - factor;
3966 if (max_derivative < value) max_derivative = value;
3969 max_derivative /= min_weights;
3973 for (ii = 1 ; ii < num_poles ; ii++) {
3974 ii_index = ii % NumPoles;
3975 ii_index = (ii_index << 1) + ii_index;
3976 ii_minus = (ii - 1) % NumPoles;
3977 ii_minus = (ii_minus << 1) + ii_minus;
3978 inverse = FK[ii + Degree] - FK[ii];
3979 inverse = 1.0e0 / inverse;
3981 factor = PA[ii_index] - PA[ii_minus];
3982 if (factor < 0) factor = - factor;
3983 value += factor; ii_index++; ii_minus++;
3984 factor = PA[ii_index] - PA[ii_minus];
3985 if (factor < 0) factor = - factor;
3986 value += factor; ii_index++; ii_minus++;
3987 factor = PA[ii_index] - PA[ii_minus];
3988 if (factor < 0) factor = - factor;
3991 if (max_derivative < value) max_derivative = value;
3997 if (&Weights != NULL) {
3998 const Standard_Real * WG = &Weights(Weights.Lower());
3999 min_weights = WG[0];
4001 for (ii = 1 ; ii < NumPoles ; ii++) {
4003 if (W < min_weights) min_weights = W;
4006 for (ii = 1 ; ii < num_poles ; ii++) {
4007 ii_index = ii % NumPoles;
4008 ii_inDim = ii_index << 2;
4009 ii_minus = (ii - 1) % NumPoles;
4010 ii_miDim = ii_minus << 2;
4011 pa_ii_inDim_0 = PA[ii_inDim]; ii_inDim++;
4012 pa_ii_inDim_1 = PA[ii_inDim]; ii_inDim++;
4013 pa_ii_inDim_2 = PA[ii_inDim]; ii_inDim++;
4014 pa_ii_inDim_3 = PA[ii_inDim];
4015 pa_ii_miDim_0 = PA[ii_miDim]; ii_miDim++;
4016 pa_ii_miDim_1 = PA[ii_miDim]; ii_miDim++;
4017 pa_ii_miDim_2 = PA[ii_miDim]; ii_miDim++;
4018 pa_ii_miDim_3 = PA[ii_miDim];
4019 wg_ii_index = WG[ii_index];
4020 wg_ii_minus = WG[ii_minus];
4021 inverse = FK[ii + Degree] - FK[ii];
4022 inverse = 1.0e0 / inverse;
4024 if (lower < 0) lower = 0;
4026 if (upper > num_poles) upper = num_poles;
4028 for (jj = lower ; jj < upper ; jj++) {
4029 jj_index = jj % NumPoles;
4030 jj_index = jj_index << 2;
4032 factor = (((PA[jj_index] - pa_ii_inDim_0) * wg_ii_index) -
4033 ((PA[jj_index] - pa_ii_miDim_0) * wg_ii_minus));
4034 if (factor < 0) factor = - factor;
4035 value += factor; jj_index++;
4036 factor = (((PA[jj_index] - pa_ii_inDim_1) * wg_ii_index) -
4037 ((PA[jj_index] - pa_ii_miDim_1) * wg_ii_minus));
4038 if (factor < 0) factor = - factor;
4039 value += factor; jj_index++;
4040 factor = (((PA[jj_index] - pa_ii_inDim_2) * wg_ii_index) -
4041 ((PA[jj_index] - pa_ii_miDim_2) * wg_ii_minus));
4042 if (factor < 0) factor = - factor;
4043 value += factor; jj_index++;
4044 factor = (((PA[jj_index] - pa_ii_inDim_3) * wg_ii_index) -
4045 ((PA[jj_index] - pa_ii_miDim_3) * wg_ii_minus));
4046 if (factor < 0) factor = - factor;
4049 if (max_derivative < value) max_derivative = value;
4052 max_derivative /= min_weights;
4056 for (ii = 1 ; ii < num_poles ; ii++) {
4057 ii_index = ii % NumPoles;
4058 ii_index = ii_index << 2;
4059 ii_minus = (ii - 1) % NumPoles;
4060 ii_minus = ii_minus << 2;
4061 inverse = FK[ii + Degree] - FK[ii];
4062 inverse = 1.0e0 / inverse;
4064 factor = PA[ii_index] - PA[ii_minus];
4065 if (factor < 0) factor = - factor;
4066 value += factor; ii_index++; ii_minus++;
4067 factor = PA[ii_index] - PA[ii_minus];
4068 if (factor < 0) factor = - factor;
4069 value += factor; ii_index++; ii_minus++;
4070 factor = PA[ii_index] - PA[ii_minus];
4071 if (factor < 0) factor = - factor;
4072 value += factor; ii_index++; ii_minus++;
4073 factor = PA[ii_index] - PA[ii_minus];
4074 if (factor < 0) factor = - factor;
4077 if (max_derivative < value) max_derivative = value;
4083 Standard_Integer kk;
4084 if (&Weights != NULL) {
4085 const Standard_Real * WG = &Weights(Weights.Lower());
4086 min_weights = WG[0];
4088 for (ii = 1 ; ii < NumPoles ; ii++) {
4090 if (W < min_weights) min_weights = W;
4093 for (ii = 1 ; ii < num_poles ; ii++) {
4094 ii_index = ii % NumPoles;
4095 ii_inDim = ii_index * ArrayDimension;
4096 ii_minus = (ii - 1) % NumPoles;
4097 ii_miDim = ii_minus * ArrayDimension;
4098 wg_ii_index = WG[ii_index];
4099 wg_ii_minus = WG[ii_minus];
4100 inverse = FK[ii + Degree] - FK[ii];
4101 inverse = 1.0e0 / inverse;
4103 if (lower < 0) lower = 0;
4105 if (upper > num_poles) upper = num_poles;
4107 for (jj = lower ; jj < upper ; jj++) {
4108 jj_index = jj % NumPoles;
4109 jj_index *= ArrayDimension;
4112 for (kk = 0 ; kk < ArrayDimension ; kk++) {
4113 factor = (((PA[jj_index + kk] - PA[ii_inDim + kk]) * wg_ii_index) -
4114 ((PA[jj_index + kk] - PA[ii_miDim + kk]) * wg_ii_minus));
4115 if (factor < 0) factor = - factor;
4119 if (max_derivative < value) max_derivative = value;
4122 max_derivative /= min_weights;
4126 for (ii = 1 ; ii < num_poles ; ii++) {
4127 ii_index = ii % NumPoles;
4128 ii_index *= ArrayDimension;
4129 ii_minus = (ii - 1) % NumPoles;
4130 ii_minus *= ArrayDimension;
4131 inverse = FK[ii + Degree] - FK[ii];
4132 inverse = 1.0e0 / inverse;
4135 for (kk = 0 ; kk < ArrayDimension ; kk++) {
4136 factor = PA[ii_index + kk] - PA[ii_minus + kk];
4137 if (factor < 0) factor = - factor;
4141 if (max_derivative < value) max_derivative = value;
4146 max_derivative *= Degree;
4147 if (max_derivative > RealSmall())
4148 UTolerance = Tolerance3D / max_derivative;
4150 UTolerance = Tolerance3D / RealSmall();
4153 //=======================================================================
4154 // function: FlatBezierKnots
4156 //=======================================================================
4158 // array of flat knots for bezier curve of maximum 25 degree
4159 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,
4160 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 };
4161 const Standard_Real& BSplCLib::FlatBezierKnots (const Standard_Integer Degree)
4163 Standard_OutOfRange_Raise_if (Degree < 1 || Degree > MaxDegree() || MaxDegree() != 25,
4164 "Bezier curve degree greater than maximal supported");
4166 return knots[25-Degree];