Loading files
3d097a032This file is pinned to the selected release. Download the original to open it in your design tool.
// Created on: 1991-08-09
// Created by: JCV
// Copyright (c) 1991-1999 Matra Datavision
// Copyright (c) 1999-2014 OPEN CASCADE SAS
//
// This file is part of Open CASCADE Technology software library.
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License version 2.1 as published
// by the Free Software Foundation, with special exception defined in the file
// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
// distribution for complete text of the license and disclaimer of any warranty.
//
// Alternatively, this file may be used under the terms of Open CASCADE
// commercial license or contractual agreement.
// Modified RLE 9 Sep 1993
// pmn : modified 28-01-97 : fixed a mistake in LocateParameter (PRO6973)
// pmn : modified 4-11-96 : fixed a mistake in BuildKnots (PRO6124)
// pmn : modified 28-Jun-96 : fixed a mistake in AntiBoorScheme
// xab : modified 15-Jun-95 : fixed a mistake in IsRational
// xab : modified 15-Mar-95 : removed Epsilon comparison in IsRational
// added RationalDerivatives.
// xab : 30-Mar-95 : fixed coupling with lti in RationalDerivatives
// xab : 15-Mar-96 : fixed a typo in Eval with extrapolation
// jct : 15-Apr-97 : added TangExtendToConstraint
// jct : 24-Apr-97 : correction on computation of Tbord and NewFlatKnots
// in TangExtendToConstraint; Continuity can be equal to 0
#include <BSplCLib.hxx>
#include <ElCLib.hxx>
#include <gp_Pnt.hxx>
#include <math_Matrix.hxx>
#include <NCollection_LocalArray.hxx>
#include <PLib.hxx>
#include <Precision.hxx>
#include <Standard_NotImplemented.hxx>
#include <BSplCLib_CurveComputation.pxx>
#include <math_Vector.hxx>
#include <algorithm>
typedef gp_Pnt Pnt;
typedef gp_Vec Vec;
typedef NCollection_Array1<double> Array1OfReal;
typedef NCollection_Array1<int> Array1OfInteger;
//=======================================================================
// class : BSplCLib_LocalMatrix
// purpose: Auxiliary class optimizing creation of matrix buffer for
// evaluation of bspline (using stack allocation for main matrix)
//=======================================================================
class BSplCLib_LocalMatrix : public math_Matrix
{
public:
BSplCLib_LocalMatrix(int DerivativeRequest, int Order)
: math_Matrix(myBuffer, 1, DerivativeRequest + 1, 1, Order)
{
Standard_OutOfRange_Raise_if(DerivativeRequest > BSplCLib::MaxDegree()
|| Order > BSplCLib::MaxDegree() + 1
|| BSplCLib::MaxDegree() > 25,
"BSplCLib: bspline degree is greater than maximum supported");
}
private:
// local buffer, to be sufficient for addressing by index [Degree+1][Degree+1]
// (see math_Matrix implementation)
double myBuffer[27 * 27]{};
};
//=================================================================================================
void BSplCLib::Hunt(const NCollection_Array1<double>& theArray, const double theX, int& theXPos)
{
// replaced by simple dichotomy (RLE)
if (theArray.First() > theX)
{
theXPos = theArray.Lower() - 1;
return;
}
else if (theArray.Last() < theX)
{
theXPos = theArray.Upper() + 1;
return;
}
theXPos = theArray.Lower();
if (theArray.Length() <= 1)
{
return;
}
int aHi = theArray.Upper();
while (aHi - theXPos != 1)
{
const int aMid = (aHi + theXPos) / 2;
if (theArray.Value(aMid) < theX)
{
theXPos = aMid;
}
else
{
aHi = aMid;
}
}
}
//=================================================================================================
int BSplCLib::FirstUKnotIndex(const int Degree, const NCollection_Array1<int>& Mults)
{
int Index = Mults.Lower();
int SigmaMult = Mults(Index);
while (SigmaMult <= Degree)
{
Index++;
SigmaMult += Mults(Index);
}
return Index;
}
//=================================================================================================
int BSplCLib::LastUKnotIndex(const int Degree, const Array1OfInteger& Mults)
{
int Index = Mults.Upper();
int SigmaMult = Mults(Index);
while (SigmaMult <= Degree)
{
Index--;
SigmaMult += Mults.Value(Index);
}
return Index;
}
//=================================================================================================
int BSplCLib::FlatIndex(const int Degree,
const int Index,
const NCollection_Array1<int>& Mults,
const bool Periodic)
{
int i, index = Index;
const int MLower = Mults.Lower();
const int* pmu = &Mults(MLower);
pmu -= MLower;
for (i = MLower + 1; i <= Index; i++)
{
index += pmu[i] - 1;
}
if (Periodic)
{
index += Degree;
}
else
{
index += pmu[MLower] - 1;
}
return index;
}
//=================================================================================================
void BSplCLib::LocateParameter(const int, // Degree,
const Array1OfReal& Knots,
const Array1OfInteger&, // Mults,
const double U,
const bool IsPeriodic,
const int FromK1,
const int ToK2,
int& KnotIndex,
double& NewU)
{
double uf = 0, ul = 1;
if (IsPeriodic)
{
uf = Knots(Knots.Lower());
ul = Knots(Knots.Upper());
}
BSplCLib::LocateParameter(Knots, U, IsPeriodic, FromK1, ToK2, KnotIndex, NewU, uf, ul);
}
//=================================================================================================
void BSplCLib::LocateParameter(const int Degree,
const Array1OfReal& Knots,
const double U,
const bool IsPeriodic,
const int FromK1,
const int ToK2,
int& KnotIndex,
double& NewU)
{
if (IsPeriodic)
{
BSplCLib::LocateParameter(Knots,
U,
IsPeriodic,
FromK1,
ToK2,
KnotIndex,
NewU,
Knots(Knots.Lower() + Degree),
Knots(Knots.Upper() - Degree));
}
else
{
BSplCLib::LocateParameter(Knots, U, IsPeriodic, FromK1, ToK2, KnotIndex, NewU, 0., 1.);
}
}
//=================================================================================================
void BSplCLib::LocateParameter(const NCollection_Array1<double>& Knots,
const double U,
const bool IsPeriodic,
const int FromK1,
const int ToK2,
int& KnotIndex,
double& NewU,
const double UFirst,
const double ULast)
{
/*
Let Knots are distributed as follows (the array is sorted in ascending order):
K1, K1,..., K1, K1, K2, K2,..., K2, K2,..., Kn, Kn,..., Kn
M1 times M2 times Mn times
NbKnots = sum(M1+M2+...+Mn)
If U <= K1 then KnotIndex should be equal to M1.
If U >= Kn then KnotIndex should be equal to NbKnots-Mn-1.
If Ki <= U < K(i+1) then KnotIndex should be equal to sum (M1+M2+...+Mi).
*/
int First, Last;
if (FromK1 < ToK2)
{
First = FromK1;
Last = ToK2;
}
else
{
First = ToK2;
Last = FromK1;
}
int Last1 = Last - 1;
NewU = U;
if (IsPeriodic && (NewU < UFirst || NewU > ULast))
{
NewU = ElCLib::InPeriod(NewU, UFirst, ULast);
}
BSplCLib::Hunt(Knots, NewU, KnotIndex);
double val;
const int KLower = Knots.Lower(), KUpper = Knots.Upper();
const double Eps = Epsilon(std::min(std::abs(Knots(KUpper)), std::abs(U)));
const double* knots = &Knots(KLower);
knots -= KLower;
if (KnotIndex < Knots.Upper())
{
val = NewU - knots[KnotIndex + 1];
if (val < 0)
{
val = -val;
}
// <= to be coherent with Segment where Eps corresponds to a bit of error.
if (val <= Eps)
{
KnotIndex++;
}
}
if (KnotIndex < First)
{
KnotIndex = First;
}
if (KnotIndex > Last1)
{
KnotIndex = Last1;
}
if (KnotIndex != Last1)
{
double K1 = knots[KnotIndex];
double K2 = knots[KnotIndex + 1];
val = K2 - K1;
if (val < 0)
{
val = -val;
}
while (val <= Eps)
{
KnotIndex++;
if (KnotIndex >= Knots.Upper())
{
break;
}
K1 = K2;
K2 = knots[KnotIndex + 1];
val = K2 - K1;
if (val < 0)
{
val = -val;
}
}
}
}
//=================================================================================================
void BSplCLib::LocateParameter(const int Degree,
const NCollection_Array1<double>& Knots,
const NCollection_Array1<int>* Mults,
const double U,
const bool Periodic,
int& KnotIndex,
double& NewU)
{
int first, last;
if (Mults)
{
if (Periodic)
{
first = Knots.Lower();
last = Knots.Upper();
}
else
{
first = FirstUKnotIndex(Degree, *Mults);
last = LastUKnotIndex(Degree, *Mults);
}
}
else
{
first = Knots.Lower() + Degree;
last = Knots.Upper() - Degree;
}
if (KnotIndex < first || KnotIndex > last)
{
BSplCLib::LocateParameter(Knots,
U,
Periodic,
first,
last,
KnotIndex,
NewU,
Knots(first),
Knots(last));
}
else
{
NewU = U;
}
}
//=================================================================================================
int BSplCLib::MaxKnotMult(const Array1OfInteger& Mults, const int FromK1, const int ToK2)
{
int aMaxMult = Mults(FromK1);
for (int anIndex = FromK1 + 1; anIndex <= ToK2; ++anIndex)
{
aMaxMult = std::max(aMaxMult, Mults(anIndex));
}
return aMaxMult;
}
//=================================================================================================
int BSplCLib::MinKnotMult(const Array1OfInteger& Mults, const int FromK1, const int ToK2)
{
int aMinMult = Mults(FromK1);
for (int anIndex = FromK1 + 1; anIndex <= ToK2; ++anIndex)
{
aMinMult = std::min(aMinMult, Mults(anIndex));
}
return aMinMult;
}
//=================================================================================================
int BSplCLib::NbPoles(const int Degree, const bool Periodic, const NCollection_Array1<int>& Mults)
{
const int f = Mults.Lower();
const int l = Mults.Upper();
const int* pmu = &Mults(f);
const int Mf = pmu[0];
const int Ml = pmu[l - f];
if (Mf <= 0)
{
return 0;
}
if (Ml <= 0)
{
return 0;
}
int sigma;
if (Periodic)
{
if (Mf > Degree)
{
return 0;
}
if (Ml > Degree)
{
return 0;
}
if (Mf != Ml)
{
return 0;
}
sigma = Mf;
}
else
{
const int Deg1 = Degree + 1;
if (Mf > Deg1)
{
return 0;
}
if (Ml > Deg1)
{
return 0;
}
sigma = Mf + Ml - Deg1;
}
for (int i = 1; i < l - f; i++)
{
if (pmu[i] <= 0)
{
return 0;
}
if (pmu[i] > Degree)
{
return 0;
}
sigma += pmu[i];
}
return sigma;
}
//=================================================================================================
int BSplCLib::KnotSequenceLength(const NCollection_Array1<int>& Mults,
const int Degree,
const bool Periodic)
{
int i, l = 0;
int MLower = Mults.Lower();
int MUpper = Mults.Upper();
const int* pmu = &Mults(MLower);
pmu -= MLower;
for (i = MLower; i <= MUpper; i++)
{
l += pmu[i];
}
if (Periodic)
{
l += 2 * (Degree + 1 - pmu[MLower]);
}
return l;
}
//=================================================================================================
void BSplCLib::KnotSequence(const NCollection_Array1<double>& Knots,
const NCollection_Array1<int>& Mults,
NCollection_Array1<double>& KnotSeq,
const bool Periodic)
{
BSplCLib::KnotSequence(Knots, Mults, 0, Periodic, KnotSeq);
}
//=================================================================================================
void BSplCLib::KnotSequence(const NCollection_Array1<double>& Knots,
const NCollection_Array1<int>& Mults,
const int Degree,
const bool Periodic,
NCollection_Array1<double>& KnotSeq)
{
double K;
int Mult;
int MLower = Mults.Lower();
const int* pmu = &Mults(MLower);
pmu -= MLower;
int KLower = Knots.Lower();
int KUpper = Knots.Upper();
const double* pkn = &Knots(KLower);
pkn -= KLower;
int M1 = Degree + 1 - pmu[MLower]; // for periodic
int i, j, index = Periodic ? M1 + 1 : 1;
for (i = KLower; i <= KUpper; i++)
{
Mult = pmu[i];
K = pkn[i];
for (j = 1; j <= Mult; j++)
{
KnotSeq(index) = K;
index++;
}
}
if (Periodic)
{
double period = pkn[KUpper] - pkn[KLower];
int m;
m = 1;
j = KUpper - 1;
for (i = M1; i >= 1; i--)
{
KnotSeq(i) = pkn[j] - period;
m++;
if (m > pmu[j])
{
j--;
m = 1;
}
}
m = 1;
j = KLower + 1;
for (i = index; i <= KnotSeq.Upper(); i++)
{
KnotSeq(i) = pkn[j] + period;
m++;
if (m > pmu[j])
{
j++;
m = 1;
}
}
}
}
//=================================================================================================
int BSplCLib::KnotsLength(const NCollection_Array1<double>& SeqKnots,
// const bool Periodic)
const bool)
{
int sizeMult = 1;
double val = SeqKnots(1);
for (int jj = 2; jj <= SeqKnots.Length(); jj++)
{
// test on strict equality on nodes
if (SeqKnots(jj) != val)
{
val = SeqKnots(jj);
sizeMult++;
}
}
return sizeMult;
}
//=================================================================================================
void BSplCLib::Knots(const NCollection_Array1<double>& SeqKnots,
NCollection_Array1<double>& knots,
NCollection_Array1<int>& mult,
// const bool Periodic)
const bool)
{
double val = SeqKnots(1);
int kk = 1;
knots(kk) = val;
mult(kk) = 1;
for (int jj = 2; jj <= SeqKnots.Length(); jj++)
{
// test on strict equality on nodes
if (SeqKnots(jj) != val)
{
val = SeqKnots(jj);
kk++;
knots(kk) = val;
mult(kk) = 1;
}
else
{
mult(kk)++;
}
}
}
//=================================================================================================
BSplCLib_KnotDistribution BSplCLib::KnotForm(const Array1OfReal& Knots,
const int FromK1,
const int ToK2)
{
if (FromK1 + 1 > Knots.Upper())
{
return BSplCLib_Uniform;
}
double aUi = std::abs(Knots(FromK1));
double aUj = std::abs(Knots(FromK1 + 1));
double aDU0 = std::abs(aUj - aUi);
double anEps = Epsilon(aUi) + Epsilon(aUj) + Epsilon(aDU0);
for (int i = FromK1 + 1; i < ToK2; i++)
{
aUi = std::abs(Knots(i));
aUj = std::abs(Knots(i + 1));
const double aDU1 = std::abs(aUj - aUi);
if (std::abs(aDU1 - aDU0) > anEps)
{
return BSplCLib_NonUniform;
}
aDU0 = aDU1;
anEps = Epsilon(aUi) + Epsilon(aUj) + Epsilon(aDU0);
}
return BSplCLib_Uniform;
}
//=================================================================================================
BSplCLib_MultDistribution BSplCLib::MultForm(const Array1OfInteger& Mults,
const int FromK1,
const int ToK2)
{
const auto [aFirst, aLast] = std::minmax(FromK1, ToK2);
if (aFirst + 1 > Mults.Upper())
{
return BSplCLib_Constant;
}
const int aFirstMult = Mults(aFirst);
BSplCLib_MultDistribution aForm = BSplCLib_Constant;
int aMult = Mults(aFirst + 1);
for (int i = aFirst + 1; i <= aLast && aForm != BSplCLib_NonConstant; i++)
{
if (i == aFirst + 1)
{
if (aMult != aFirstMult)
{
aForm = BSplCLib_QuasiConstant;
}
}
else if (i == aLast)
{
if (aForm == BSplCLib_QuasiConstant)
{
if (aFirstMult != Mults(i))
{
aForm = BSplCLib_NonConstant;
}
}
else
{
if (aMult != Mults(i))
{
aForm = BSplCLib_NonConstant;
}
}
}
else
{
if (aMult != Mults(i))
{
aForm = BSplCLib_NonConstant;
}
aMult = Mults(i);
}
}
return aForm;
}
//=================================================================================================
void BSplCLib::KnotAnalysis(const int Degree,
const bool Periodic,
const NCollection_Array1<double>& CKnots,
const NCollection_Array1<int>& CMults,
GeomAbs_BSplKnotDistribution& KnotForm,
int& MaxKnotMult)
{
KnotForm = GeomAbs_NonUniform;
BSplCLib_KnotDistribution KSet = BSplCLib::KnotForm(CKnots, 1, CKnots.Length());
if (KSet == BSplCLib_Uniform)
{
BSplCLib_MultDistribution MSet = BSplCLib::MultForm(CMults, 1, CMults.Length());
switch (MSet)
{
case BSplCLib_NonConstant:
break;
case BSplCLib_Constant:
if (CKnots.Length() == 2)
{
KnotForm = GeomAbs_PiecewiseBezier;
}
else
{
if (CMults(1) == 1)
{
KnotForm = GeomAbs_Uniform;
}
}
break;
case BSplCLib_QuasiConstant:
if (CMults(1) == Degree + 1)
{
double M = CMults(2);
if (M == Degree)
{
KnotForm = GeomAbs_PiecewiseBezier;
}
else if (M == 1)
{
KnotForm = GeomAbs_QuasiUniform;
}
}
break;
}
}
int FirstKM = Periodic ? CKnots.Lower() : BSplCLib::FirstUKnotIndex(Degree, CMults);
int LastKM = Periodic ? CKnots.Upper() : BSplCLib::LastUKnotIndex(Degree, CMults);
MaxKnotMult = 0;
if (LastKM - FirstKM != 1)
{
int Multi;
for (int i = FirstKM + 1; i < LastKM; i++)
{
Multi = CMults(i);
MaxKnotMult = std::max(MaxKnotMult, Multi);
}
}
}
//=================================================================================================
void BSplCLib::Reparametrize(const double U1, const double U2, Array1OfReal& Knots)
{
int Lower = Knots.Lower();
int Upper = Knots.Upper();
double UFirst = std::min(U1, U2);
double ULast = std::max(U1, U2);
double NewLength = ULast - UFirst;
BSplCLib_KnotDistribution KSet = BSplCLib::KnotForm(Knots, Lower, Upper);
if (KSet == BSplCLib_Uniform)
{
double DU = NewLength / (Upper - Lower);
Knots(Lower) = UFirst;
for (int i = Lower + 1; i <= Upper; i++)
{
Knots(i) = Knots(i - 1) + DU;
}
}
else
{
double K2;
double Ratio;
double K1 = Knots(Lower);
double Length = Knots(Upper) - Knots(Lower);
Knots(Lower) = UFirst;
for (int i = Lower + 1; i <= Upper; i++)
{
K2 = Knots(i);
Ratio = (K2 - K1) / Length;
Knots(i) = Knots(i - 1) + (NewLength * Ratio);
// for CheckCurveData
double Eps = Epsilon(std::abs(Knots(i - 1)));
if (Knots(i) - Knots(i - 1) <= Eps)
{
Knots(i) = std::nextafter(Knots(i - 1) + Eps, RealLast());
}
K1 = K2;
}
}
}
//=================================================================================================
void BSplCLib::Reverse(NCollection_Array1<double>& Knots)
{
int first = Knots.Lower();
int last = Knots.Upper();
double kfirst = Knots(first);
double klast = Knots(last);
double tfirst = kfirst;
double tlast = klast;
first++;
last--;
while (first <= last)
{
tfirst += klast - Knots(last);
tlast -= Knots(first) - kfirst;
kfirst = Knots(first);
klast = Knots(last);
Knots(first) = tfirst;
Knots(last) = tlast;
first++;
last--;
}
}
//=================================================================================================
void BSplCLib::Reverse(NCollection_Array1<int>& Mults)
{
std::reverse(&Mults(Mults.Lower()), &Mults(Mults.Upper()) + 1);
}
//=================================================================================================
void BSplCLib::Reverse(NCollection_Array1<double>& Weights, const int L)
{
BSplCLib_Reverse(Weights, L);
}
//=================================================================================================
bool BSplCLib::IsRational(const NCollection_Array1<double>& Weights,
const int I1,
const int I2,
// const double Epsi)
const double)
{
int i, f = Weights.Lower(), l = Weights.Length();
int I3 = I2 - f;
const double* WG = &Weights(f);
WG -= f;
for (i = I1 - f; i < I3; i++)
{
if (WG[f + (i % l)] != WG[f + ((i + 1) % l)])
{
return true;
}
}
return false;
}
//=================================================================================================
void BSplCLib::Eval(const double U,
const int Degree,
double& Knots,
const int Dimension,
double& Poles)
{
int step, i, Dms, Dm1, Dpi, Sti;
double X, Y, *poles, *knots = &Knots;
Dm1 = Dms = Degree;
Dm1--;
Dms++;
switch (Dimension)
{
case 1: {
for (step = -1; step < Dm1; step++)
{
Dms--;
poles = &Poles;
Dpi = Dm1;
Sti = step;
for (i = 0; i < Dms; i++)
{
Dpi++;
Sti++;
X = (knots[Dpi] - U) / (knots[Dpi] - knots[Sti]);
Y = 1 - X;
poles[0] *= X;
poles[0] += Y * poles[1];
poles += 1;
}
}
break;
}
case 2: {
for (step = -1; step < Dm1; step++)
{
Dms--;
poles = &Poles;
Dpi = Dm1;
Sti = step;
for (i = 0; i < Dms; i++)
{
Dpi++;
Sti++;
X = (knots[Dpi] - U) / (knots[Dpi] - knots[Sti]);
Y = 1 - X;
poles[0] *= X;
poles[0] += Y * poles[2];
poles[1] *= X;
poles[1] += Y * poles[3];
poles += 2;
}
}
break;
}
case 3: {
for (step = -1; step < Dm1; step++)
{
Dms--;
poles = &Poles;
Dpi = Dm1;
Sti = step;
for (i = 0; i < Dms; i++)
{
Dpi++;
Sti++;
X = (knots[Dpi] - U) / (knots[Dpi] - knots[Sti]);
Y = 1 - X;
poles[0] *= X;
poles[0] += Y * poles[3];
poles[1] *= X;
poles[1] += Y * poles[4];
poles[2] *= X;
poles[2] += Y * poles[5];
poles += 3;
}
}
break;
}
case 4: {
for (step = -1; step < Dm1; step++)
{
Dms--;
poles = &Poles;
Dpi = Dm1;
Sti = step;
for (i = 0; i < Dms; i++)
{
Dpi++;
Sti++;
X = (knots[Dpi] - U) / (knots[Dpi] - knots[Sti]);
Y = 1 - X;
poles[0] *= X;
poles[0] += Y * poles[4];
poles[1] *= X;
poles[1] += Y * poles[5];
poles[2] *= X;
poles[2] += Y * poles[6];
poles[3] *= X;
poles[3] += Y * poles[7];
poles += 4;
}
}
break;
}
default: {
int k;
for (step = -1; step < Dm1; step++)
{
Dms--;
poles = &Poles;
Dpi = Dm1;
Sti = step;
for (i = 0; i < Dms; i++)
{
Dpi++;
Sti++;
X = (knots[Dpi] - U) / (knots[Dpi] - knots[Sti]);
Y = 1 - X;
for (k = 0; k < Dimension; k++)
{
poles[k] *= X;
poles[k] += Y * poles[k + Dimension];
}
poles += Dimension;
}
}
}
}
}
//=================================================================================================
void BSplCLib::BoorScheme(const double U,
const int Degree,
double& Knots,
const int Dimension,
double& Poles,
const int Depth,
const int Length)
{
//
// Compute the values
//
// P(i,j) (U).
//
// for i = 0 to Depth,
// j = 0 to Length - i
//
// The Boor scheme is :
//
// P(0,j) = Pole(j)
// P(i,j) = x * P(i-1,j) + (1-x) * P(i-1,j+1)
//
// where x = (knot(i+j+Degree) - U) / (knot(i+j+Degree) - knot(i+j))
//
//
// The values are stored in the array Poles
// They are alternatively written if the odd and even positions.
//
// The successives contents of the array are
// ***** means uninitialised, l = Degree + Length
//
// P(0,0) ****** P(0,1) ...... P(0,l-1) ******** P(0,l)
// P(0,0) P(1,0) P(0,1) ...... P(0,l-1) P(1,l-1) P(0,l)
// P(0,0) P(1,0) P(2,0) ...... P(2,l-1) P(1,l-1) P(0,l)
//
int i, k, step;
double* knots = &Knots;
double *pole, *firstpole = &Poles - 2 * Dimension;
// the steps of recursion
for (step = 0; step < Depth; step++)
{
firstpole += Dimension;
pole = firstpole;
// compute the new row of poles
for (i = step; i < Length; i++)
{
pole += 2 * Dimension;
// coefficient
double X = (knots[i + Degree - step] - U) / (knots[i + Degree - step] - knots[i]);
double Y = 1. - X;
// Value
// P(i,j) = X * P(i-1,j) + (1-X) * P(i-1,j+1)
for (k = 0; k < Dimension; k++)
{
pole[k] = X * pole[k - Dimension] + Y * pole[k + Dimension];
}
}
}
}
//=================================================================================================
bool BSplCLib::AntiBoorScheme(const double U,
const int Degree,
double& Knots,
const int Dimension,
double& Poles,
const int Depth,
const int Length,
const double Tolerance)
{
// do the Boor scheme reverted.
int i, k, step, half_length;
double* knots = &Knots;
double z, X, Y, *pole, *firstpole = &Poles + (Depth - 1) * Dimension;
// Test the special case length = 1
// only verification of the central point
if (Length == 1)
{
X = (knots[Degree] - U) / (knots[Degree] - knots[0]);
Y = 1. - X;
for (k = 0; k < Dimension; k++)
{
z = X * firstpole[k] + Y * firstpole[k + 2 * Dimension];
if (std::abs(z - firstpole[k + Dimension]) > Tolerance)
{
return false;
}
}
return true;
}
// General case
// the steps of recursion
for (step = Depth - 1; step >= 0; step--)
{
firstpole -= Dimension;
pole = firstpole;
// first step from left to right
for (i = step; i < Length - 1; i++)
{
pole += 2 * Dimension;
X = (knots[i + Degree - step] - U) / (knots[i + Degree - step] - knots[i]);
Y = 1. - X;
for (k = 0; k < Dimension; k++)
{
pole[k + Dimension] = (pole[k] - X * pole[k - Dimension]) / Y;
}
}
// second step from right to left
pole += 4 * Dimension;
half_length = (Length - 1 + step) / 2;
//
// only do half of the way from right to left
// otherwise it start degenerating because of
// overflows
//
for (i = Length - 1; i > half_length; i--)
{
pole -= 2 * Dimension;
// coefficient
X = (knots[i + Degree - step] - U) / (knots[i + Degree - step] - knots[i]);
Y = 1. - X;
for (k = 0; k < Dimension; k++)
{
z = (pole[k] - Y * pole[k + Dimension]) / X;
if (std::abs(z - pole[k - Dimension]) > Tolerance)
{
return false;
}
pole[k - Dimension] += z;
pole[k - Dimension] /= 2.;
}
}
}
return true;
}
//=================================================================================================
void BSplCLib::Derivative(const int Degree,
double& Knots,
const int Dimension,
const int Length,
const int Order,
double& Poles)
{
int i, k, step, span = Degree;
double* knot = &Knots;
for (step = 1; step <= Order; step++)
{
double* pole = &Poles;
for (i = step; i < Length; i++)
{
double coef = -span / (knot[i + span] - knot[i]);
for (k = 0; k < Dimension; k++)
{
pole[k] -= pole[k + Dimension];
pole[k] *= coef;
}
pole += Dimension;
}
span--;
}
}
//=================================================================================================
void BSplCLib::Bohm(const double U,
const int Degree,
const int N,
double& Knots,
const int Dimension,
double& Poles)
{
// First phase independent of U, compute the poles of the derivatives
int i, j, iDim, min, Dmi, DDmi, jDmi, Degm1;
double *knot = &Knots, *pole, coef, *tbis, *psav, *psDD, *psDDmDim;
psav = &Poles;
if (N < Degree)
{
min = N;
}
else
{
min = Degree;
}
Degm1 = Degree - 1;
DDmi = (Degree << 1) + 1;
switch (Dimension)
{
case 1: {
psDD = psav + Degree;
psDDmDim = psDD - 1;
for (i = 0; i < Degree; i++)
{
DDmi--;
pole = psDD;
tbis = psDDmDim;
jDmi = DDmi;
for (j = Degm1; j >= i; j--)
{
jDmi--;
*pole -= *tbis;
*pole = (knot[jDmi] == knot[j]) ? 0.0 : *pole / (knot[jDmi] - knot[j]);
pole--;
tbis--;
}
}
// Second phase, dependant of U
iDim = -1;
for (i = 0; i < Degree; i++)
{
iDim += 1;
pole = psav + iDim;
tbis = pole + 1;
coef = U - knot[i];
for (j = i; j >= 0; j--)
{
*pole += coef * (*tbis);
pole--;
tbis--;
}
}
// multiply by the degrees
coef = Degree;
Dmi = Degree;
pole = psav + 1;
for (i = 1; i <= min; i++)
{
*pole *= coef;
pole++;
Dmi--;
coef *= Dmi;
}
break;
}
case 2: {
psDD = psav + (Degree << 1);
psDDmDim = psDD - 2;
for (i = 0; i < Degree; i++)
{
DDmi--;
pole = psDD;
tbis = psDDmDim;
jDmi = DDmi;
for (j = Degm1; j >= i; j--)
{
jDmi--;
coef = (knot[jDmi] == knot[j]) ? 0.0 : 1. / (knot[jDmi] - knot[j]);
*pole -= *tbis;
*pole *= coef;
pole++;
tbis++;
*pole -= *tbis;
*pole *= coef;
pole -= 3;
tbis -= 3;
}
}
// Second phase, dependant of U
iDim = -2;
for (i = 0; i < Degree; i++)
{
iDim += 2;
pole = psav + iDim;
tbis = pole + 2;
coef = U - knot[i];
for (j = i; j >= 0; j--)
{
*pole += coef * (*tbis);
pole++;
tbis++;
*pole += coef * (*tbis);
pole -= 3;
tbis -= 3;
}
}
// multiply by the degrees
coef = Degree;
Dmi = Degree;
pole = psav + 2;
for (i = 1; i <= min; i++)
{
*pole *= coef;
pole++;
*pole *= coef;
pole++;
Dmi--;
coef *= Dmi;
}
break;
}
case 3: {
psDD = psav + (Degree << 1) + Degree;
psDDmDim = psDD - 3;
for (i = 0; i < Degree; i++)
{
DDmi--;
pole = psDD;
tbis = psDDmDim;
jDmi = DDmi;
for (j = Degm1; j >= i; j--)
{
jDmi--;
coef = (knot[jDmi] == knot[j]) ? 0.0 : 1. / (knot[jDmi] - knot[j]);
*pole -= *tbis;
*pole *= coef;
pole++;
tbis++;
*pole -= *tbis;
*pole *= coef;
pole++;
tbis++;
*pole -= *tbis;
*pole *= coef;
pole -= 5;
tbis -= 5;
}
}
// Second phase, dependant of U
iDim = -3;
for (i = 0; i < Degree; i++)
{
iDim += 3;
pole = psav + iDim;
tbis = pole + 3;
coef = U - knot[i];
for (j = i; j >= 0; j--)
{
*pole += coef * (*tbis);
pole++;
tbis++;
*pole += coef * (*tbis);
pole++;
tbis++;
*pole += coef * (*tbis);
pole -= 5;
tbis -= 5;
}
}
// multiply by the degrees
coef = Degree;
Dmi = Degree;
pole = psav + 3;
for (i = 1; i <= min; i++)
{
*pole *= coef;
pole++;
*pole *= coef;
pole++;
*pole *= coef;
pole++;
Dmi--;
coef *= Dmi;
}
break;
}
case 4: {
psDD = psav + (Degree << 2);
psDDmDim = psDD - 4;
for (i = 0; i < Degree; i++)
{
DDmi--;
pole = psDD;
tbis = psDDmDim;
jDmi = DDmi;
for (j = Degm1; j >= i; j--)
{
jDmi--;
coef = (knot[jDmi] == knot[j]) ? 0.0 : 1. / (knot[jDmi] - knot[j]);
*pole -= *tbis;
*pole *= coef;
pole++;
tbis++;
*pole -= *tbis;
*pole *= coef;
pole++;
tbis++;
*pole -= *tbis;
*pole *= coef;
pole++;
tbis++;
*pole -= *tbis;
*pole *= coef;
pole -= 7;
tbis -= 7;
}
}
// Second phase, dependant of U
iDim = -4;
for (i = 0; i < Degree; i++)
{
iDim += 4;
pole = psav + iDim;
tbis = pole + 4;
coef = U - knot[i];
for (j = i; j >= 0; j--)
{
*pole += coef * (*tbis);
pole++;
tbis++;
*pole += coef * (*tbis);
pole++;
tbis++;
*pole += coef * (*tbis);
pole++;
tbis++;
*pole += coef * (*tbis);
pole -= 7;
tbis -= 7;
}
}
// multiply by the degrees
coef = Degree;
Dmi = Degree;
pole = psav + 4;
for (i = 1; i <= min; i++)
{
*pole *= coef;
pole++;
*pole *= coef;
pole++;
*pole *= coef;
pole++;
*pole *= coef;
pole++;
Dmi--;
coef *= Dmi;
}
break;
}
default: {
int k;
int Dim2 = Dimension << 1;
psDD = psav + Degree * Dimension;
psDDmDim = psDD - Dimension;
for (i = 0; i < Degree; i++)
{
DDmi--;
pole = psDD;
tbis = psDDmDim;
jDmi = DDmi;
for (j = Degm1; j >= i; j--)
{
jDmi--;
coef = (knot[jDmi] == knot[j]) ? 0.0 : 1. / (knot[jDmi] - knot[j]);
for (k = 0; k < Dimension; k++)
{
*pole -= *tbis;
*pole *= coef;
pole++;
tbis++;
}
pole -= Dim2;
tbis -= Dim2;
}
}
// Second phase, dependant of U
iDim = -Dimension;
for (i = 0; i < Degree; i++)
{
iDim += Dimension;
pole = psav + iDim;
tbis = pole + Dimension;
coef = U - knot[i];
for (j = i; j >= 0; j--)
{
for (k = 0; k < Dimension; k++)
{
*pole += coef * (*tbis);
pole++;
tbis++;
}
pole -= Dim2;
tbis -= Dim2;
}
}
// multiply by the degrees
coef = Degree;
Dmi = Degree;
pole = psav + Dimension;
for (i = 1; i <= min; i++)
{
for (k = 0; k < Dimension; k++)
{
*pole *= coef;
pole++;
}
Dmi--;
coef *= Dmi;
}
}
}
}
//=================================================================================================
void BSplCLib::BuildKnots(const int Degree,
const int Index,
const bool Periodic,
const NCollection_Array1<double>& Knots,
const NCollection_Array1<int>* Mults,
double& LK)
{
int KLower = Knots.Lower();
const double* pkn = &Knots(KLower);
pkn -= KLower;
double* knot = &LK;
if (Mults == nullptr)
{
switch (Degree)
{
case 1: {
int j = Index;
knot[0] = pkn[j];
j++;
knot[1] = pkn[j];
break;
}
case 2: {
int j = Index - 1;
knot[0] = pkn[j];
j++;
knot[1] = pkn[j];
j++;
knot[2] = pkn[j];
j++;
knot[3] = pkn[j];
break;
}
case 3: {
int j = Index - 2;
knot[0] = pkn[j];
j++;
knot[1] = pkn[j];
j++;
knot[2] = pkn[j];
j++;
knot[3] = pkn[j];
j++;
knot[4] = pkn[j];
j++;
knot[5] = pkn[j];
break;
}
case 4: {
int j = Index - 3;
knot[0] = pkn[j];
j++;
knot[1] = pkn[j];
j++;
knot[2] = pkn[j];
j++;
knot[3] = pkn[j];
j++;
knot[4] = pkn[j];
j++;
knot[5] = pkn[j];
j++;
knot[6] = pkn[j];
j++;
knot[7] = pkn[j];
break;
}
case 5: {
int j = Index - 4;
knot[0] = pkn[j];
j++;
knot[1] = pkn[j];
j++;
knot[2] = pkn[j];
j++;
knot[3] = pkn[j];
j++;
knot[4] = pkn[j];
j++;
knot[5] = pkn[j];
j++;
knot[6] = pkn[j];
j++;
knot[7] = pkn[j];
j++;
knot[8] = pkn[j];
j++;
knot[9] = pkn[j];
break;
}
case 6: {
int j = Index - 5;
knot[0] = pkn[j];
j++;
knot[1] = pkn[j];
j++;
knot[2] = pkn[j];
j++;
knot[3] = pkn[j];
j++;
knot[4] = pkn[j];
j++;
knot[5] = pkn[j];
j++;
knot[6] = pkn[j];
j++;
knot[7] = pkn[j];
j++;
knot[8] = pkn[j];
j++;
knot[9] = pkn[j];
j++;
knot[10] = pkn[j];
j++;
knot[11] = pkn[j];
break;
}
default: {
int i, j;
int Deg2 = Degree << 1;
j = Index - Degree;
for (i = 0; i < Deg2; i++)
{
j++;
knot[i] = pkn[j];
}
}
}
}
else
{
int i;
int Deg1 = Degree - 1;
int KUpper = Knots.Upper();
int MLower = Mults->Lower();
int MUpper = Mults->Upper();
const int* pmu = &(*Mults)(MLower);
pmu -= MLower;
double dknot = 0;
int ilow = Index, mlow = 0;
int iupp = Index + 1, mupp = 0;
double loffset = 0., uoffset = 0.;
bool getlow = true, getupp = true;
if (Periodic)
{
dknot = pkn[KUpper] - pkn[KLower];
if (iupp > MUpper)
{
iupp = MLower + 1;
uoffset = dknot;
}
}
// Find the knots around Index
for (i = 0; i < Degree; i++)
{
if (getlow)
{
mlow++;
if (mlow > pmu[ilow])
{
mlow = 1;
ilow--;
getlow = (ilow >= MLower);
if (Periodic && !getlow)
{
ilow = MUpper - 1;
loffset = dknot;
getlow = true;
}
}
if (getlow)
{
knot[Deg1 - i] = pkn[ilow] - loffset;
}
}
if (getupp)
{
mupp++;
if (mupp > pmu[iupp])
{
mupp = 1;
iupp++;
getupp = (iupp <= MUpper);
if (Periodic && !getupp)
{
iupp = MLower + 1;
uoffset = dknot;
getupp = true;
}
}
if (getupp)
{
knot[Degree + i] = pkn[iupp] + uoffset;
}
}
}
}
}
//=================================================================================================
int BSplCLib::PoleIndex(const int Degree,
const int Index,
const bool Periodic,
const NCollection_Array1<int>& Mults)
{
int i, pindex = 0;
for (i = Mults.Lower(); i <= Index; i++)
{
pindex += Mults(i);
}
if (Periodic)
{
pindex -= Mults(Mults.Lower());
}
else
{
pindex -= Degree + 1;
}
return pindex;
}
//=================================================================================================
void BSplCLib::BuildBoor(const int Index,
const int Length,
const int Dimension,
const NCollection_Array1<double>& Poles,
double& LP)
{
double* poles = &LP;
int i, k, ip = Poles.Lower() + Index * Dimension;
for (i = 0; i < Length + 1; i++)
{
for (k = 0; k < Dimension; k++)
{
poles[k] = Poles(ip);
ip++;
if (ip > Poles.Upper())
{
ip = Poles.Lower();
}
}
poles += 2 * Dimension;
}
}
//=================================================================================================
int BSplCLib::BoorIndex(const int Index, const int Length, const int Depth)
{
if (Index <= Depth)
{
return Index;
}
if (Index <= Length)
{
return 2 * Index - Depth;
}
return Length + Index - Depth;
}
//=================================================================================================
void BSplCLib::GetPole(const int Index,
const int Length,
const int Depth,
const int Dimension,
double& LP,
int& Position,
NCollection_Array1<double>& Pole)
{
int k;
double* pole = &LP + BoorIndex(Index, Length, Depth) * Dimension;
for (k = 0; k < Dimension; k++)
{
Pole(Position) = pole[k];
Position++;
}
if (Position > Pole.Upper())
{
Position = Pole.Lower();
}
}
//=================================================================================================
bool BSplCLib::PrepareInsertKnots(const int Degree,
const bool Periodic,
const NCollection_Array1<double>& Knots,
const NCollection_Array1<int>& Mults,
const NCollection_Array1<double>& AddKnots,
const NCollection_Array1<int>* AddMults,
int& NbPoles,
int& NbKnots,
const double Tolerance,
const bool Add)
{
bool addflat = AddMults == nullptr;
int first, last;
if (Periodic)
{
first = Knots.Lower();
last = Knots.Upper();
}
else
{
first = FirstUKnotIndex(Degree, Mults);
last = LastUKnotIndex(Degree, Mults);
}
double adeltaK1 = Knots(first) - AddKnots(AddKnots.Lower());
double adeltaK2 = AddKnots(AddKnots.Upper()) - Knots(last);
if (adeltaK1 > Tolerance)
{
return false;
}
if (adeltaK2 > Tolerance)
{
return false;
}
int sigma = 0, mult, amult;
NbKnots = 0;
int k = Knots.Lower() - 1;
int ak = AddKnots.Lower();
if (Periodic && AddKnots.Length() > 1)
{
// gka for case when segments was produced on full period only one knot
// was added in the end of curve
if (std::abs(adeltaK1) <= gp::Resolution() && std::abs(adeltaK2) <= gp::Resolution())
{
ak++;
}
}
int aLastKnotMult = Mults(Knots.Upper());
double au, oldau = AddKnots(ak), Eps;
while (ak <= AddKnots.Upper())
{
au = AddKnots(ak);
if (au < oldau)
{
return false;
}
oldau = au;
Eps = std::max(Tolerance, Epsilon(au));
while ((k < Knots.Upper()) && (Knots(k + 1) - au <= Eps))
{
k++;
NbKnots++;
sigma += Mults(k);
}
if (addflat)
{
amult = 1;
}
else
{
amult = std::max(0, (*AddMults)(ak));
}
while ((ak < AddKnots.Upper()) && (std::abs(au - AddKnots(ak + 1)) <= Eps))
{
ak++;
if (Add)
{
if (addflat)
{
amult++;
}
else
{
amult += std::max(0, (*AddMults)(ak));
}
}
}
if (std::abs(au - Knots(k)) <= Eps)
{
// identic to existing knot
mult = Mults(k);
if (Add)
{
if (mult + amult > Degree)
{
amult = std::max(0, Degree - mult);
}
sigma += amult;
}
else if (amult > mult)
{
if (amult > Degree)
{
amult = Degree;
}
if (k == Knots.Upper() && Periodic)
{
aLastKnotMult = std::max(amult, mult);
sigma += 2 * (aLastKnotMult - mult);
}
else
{
sigma += amult - mult;
}
}
/*
// on periodic curves if this is the last knot
// the multiplicity is added twice to account for the first knot
if (k == Knots.Upper() && Periodic) {
if (Add)
sigma += amult;
else
sigma += amult - mult;
}
*/
}
else
{
// not identic to existing knot
if (amult > 0)
{
if (amult > Degree)
{
amult = Degree;
}
NbKnots++;
sigma += amult;
}
}
ak++;
}
// count the last knots
while (k < Knots.Upper())
{
k++;
NbKnots++;
sigma += Mults(k);
}
if (Periodic)
{
// for periodic B-Spline the requirement is that multiplicities of the first
// and last knots must be equal (see Geom_BSplineCurve constructor for
// instance);
// respectively AddMults() must meet this requirement if AddKnots() contains
// knot(s) coincident with first or last
NbPoles = sigma - aLastKnotMult;
}
else
{
NbPoles = sigma - Degree - 1;
}
return true;
}
//=================================================================================================
static void Copy(const int NbPoles,
int& OldFirst,
const NCollection_Array1<double>& OldPoles,
int& NewFirst,
NCollection_Array1<double>& NewPoles)
{
// reset the index in the range for periodicity
OldFirst =
OldPoles.Lower() + (OldFirst - OldPoles.Lower()) % (OldPoles.Upper() - OldPoles.Lower() + 1);
NewFirst =
NewPoles.Lower() + (NewFirst - NewPoles.Lower()) % (NewPoles.Upper() - NewPoles.Lower() + 1);
// copy
int i;
for (i = 1; i <= NbPoles; i++)
{
NewPoles(NewFirst) = OldPoles(OldFirst);
OldFirst++;
if (OldFirst > OldPoles.Upper())
{
OldFirst = OldPoles.Lower();
}
NewFirst++;
if (NewFirst > NewPoles.Upper())
{
NewFirst = NewPoles.Lower();
}
}
}
//=================================================================================================
void BSplCLib::InsertKnots(const int Degree,
const bool Periodic,
const int Dimension,
const NCollection_Array1<double>& Poles,
const NCollection_Array1<double>& Knots,
const NCollection_Array1<int>& Mults,
const NCollection_Array1<double>& AddKnots,
const NCollection_Array1<int>* AddMults,
NCollection_Array1<double>& NewPoles,
NCollection_Array1<double>& NewKnots,
NCollection_Array1<int>& NewMults,
const double Tolerance,
const bool Add)
{
bool addflat = AddMults == nullptr;
int i, k, mult, firstmult;
int index, kn, curnk, curk;
int p, np, curp, curnp, length, depth;
double u;
int need;
double Eps;
// -------------------
// create local arrays
// -------------------
// Use stack-based allocation for small arrays (MaxDegree=25, typical Dimension=4)
NCollection_LocalArray<double, 64> knots(2 * Degree);
NCollection_LocalArray<double, 256> poles(static_cast<size_t>(2 * Degree + 1) * Dimension);
//----------------------------
// loop on the knots to insert
//----------------------------
curk = Knots.Lower() - 1; // current position in Knots
curnk = NewKnots.Lower() - 1; // current position in NewKnots
curp = Poles.Lower(); // current position in Poles
curnp = NewPoles.Lower(); // current position in NewPoles
// NewKnots, NewMults, NewPoles contains the current state of the curve
// index is the first pole of the current curve for insertion schema
if (Periodic)
{
index = -Mults(Mults.Lower());
}
else
{
index = -Degree - 1;
}
// on Periodic curves the first knot and the last knot are inserted later
// (they are the same knot)
firstmult = 0; // multiplicity of the first-last knot for periodic
// kn current knot to insert in AddKnots
for (kn = AddKnots.Lower(); kn <= AddKnots.Upper(); kn++)
{
u = AddKnots(kn);
Eps = std::max(Tolerance, Epsilon(u));
//-----------------------------------
// find the position in the old knots
// and copy to the new knots
//-----------------------------------
while (curk < Knots.Upper() && Knots(curk + 1) - u <= Eps)
{
curk++;
curnk++;
NewKnots(curnk) = Knots(curk);
index += NewMults(curnk) = Mults(curk);
}
//-----------------------------------
// Slice the knots and the mults
// to the current size of the new curve
//-----------------------------------
i = curnk + Knots.Upper() - curk;
NCollection_Array1<double> nknots(NewKnots(NewKnots.Lower()), NewKnots.Lower(), i);
NCollection_Array1<int> nmults(NewMults(NewMults.Lower()), NewMults.Lower(), i);
//-----------------------------------
// copy enough knots
// to compute the insertion schema
//-----------------------------------
k = curk;
i = curnk;
mult = 0;
while (mult < Degree && k < Knots.Upper())
{
k++;
i++;
nknots(i) = Knots(k);
mult += nmults(i) = Mults(k);
}
// copy knots at the end for periodic curve
if (Periodic)
{
mult = 0;
k = Knots.Upper();
i = nknots.Upper();
while (mult < Degree && i > curnk)
{
nknots(i) = Knots(k);
mult += nmults(i) = Mults(k);
k--;
i--;
}
nmults(nmults.Upper()) = nmults(nmults.Lower());
}
//------------------------------------
// do the boor scheme on the new curve
// to insert the new knot
//------------------------------------
bool sameknot = (std::abs(u - NewKnots(curnk)) <= Eps);
if (sameknot)
{
length = std::max(0, Degree - NewMults(curnk));
}
else
{
length = Degree;
}
if (addflat)
{
depth = 1;
}
else
{
depth = std::min(Degree, (*AddMults)(kn));
}
if (sameknot)
{
if (Add)
{
if ((NewMults(curnk) + depth) > Degree)
{
depth = Degree - NewMults(curnk);
}
}
else
{
depth = std::max(0, depth - NewMults(curnk));
}
if (Periodic)
{
// on periodic curve the first and last knot are delayed to the end
if (curk == Knots.Lower() || (curk == Knots.Upper()))
{
if (firstmult == 0)
{ // do that only once
firstmult += depth;
}
depth = 0;
}
}
}
if (depth <= 0)
{
continue;
}
BuildKnots(Degree, curnk, Periodic, nknots, &nmults, *knots);
// copy the poles
need = NewPoles.Lower() + (index + length + 1) * Dimension - curnp;
need = std::min(need, Poles.Upper() - curp + 1);
p = curp;
np = curnp;
Copy(need, p, Poles, np, NewPoles);
curp += need;
curnp += need;
// slice the poles to the current number of poles in case of periodic
NCollection_Array1<double> npoles(NewPoles(NewPoles.Lower()), NewPoles.Lower(), curnp - 1);
BuildBoor(index, length, Dimension, npoles, *poles);
BoorScheme(u, Degree, *knots, Dimension, *poles, depth, length);
//-------------------
// copy the new poles
//-------------------
curnp += depth * Dimension; // number of poles is increased by depth
NCollection_Array1<double> ThePoles(NewPoles(NewPoles.Lower()), NewPoles.Lower(), curnp - 1);
np = NewKnots.Lower() + (index + 1) * Dimension;
for (i = 1; i <= length + depth; i++)
{
GetPole(i, length, depth, Dimension, *poles, np, ThePoles);
}
//-------------------
// insert the knot
//-------------------
index += depth;
if (sameknot)
{
NewMults(curnk) += depth;
}
else
{
curnk++;
NewKnots(curnk) = u;
NewMults(curnk) = depth;
}
}
//------------------------------
// copy the last poles and knots
//------------------------------
Copy(Poles.Upper() - curp + 1, curp, Poles, curnp, NewPoles);
while (curk < Knots.Upper())
{
curk++;
curnk++;
NewKnots(curnk) = Knots(curk);
NewMults(curnk) = Mults(curk);
}
//------------------------------
// process the first-last knot
// on periodic curves
//------------------------------
if (firstmult > 0)
{
curnk = NewKnots.Lower();
if (NewMults(curnk) + firstmult > Degree)
{
firstmult = Degree - NewMults(curnk);
}
if (firstmult > 0)
{
length = Degree - NewMults(curnk);
depth = firstmult;
BuildKnots(Degree, curnk, Periodic, NewKnots, &NewMults, *knots);
NCollection_Array1<double> npoles(NewPoles(NewPoles.Lower()),
NewPoles.Lower(),
NewPoles.Upper() - depth * Dimension);
BuildBoor(0, length, Dimension, npoles, *poles);
BoorScheme(NewKnots(curnk), Degree, *knots, Dimension, *poles, depth, length);
//---------------------------
// copy the new poles
// but rotate them with depth
//---------------------------
np = NewPoles.Lower();
for (i = depth; i < length + depth; i++)
{
GetPole(i, length, depth, Dimension, *poles, np, NewPoles);
}
np = NewPoles.Upper() - depth * Dimension + 1;
for (i = 0; i < depth; i++)
{
GetPole(i, length, depth, Dimension, *poles, np, NewPoles);
}
NewMults(NewMults.Lower()) += depth;
NewMults(NewMults.Upper()) += depth;
}
}
}
//=================================================================================================
bool BSplCLib::RemoveKnot(const int Index,
const int Mult,
const int Degree,
const bool Periodic,
const int Dimension,
const NCollection_Array1<double>& Poles,
const NCollection_Array1<double>& Knots,
const NCollection_Array1<int>& Mults,
NCollection_Array1<double>& NewPoles,
NCollection_Array1<double>& NewKnots,
NCollection_Array1<int>& NewMults,
const double Tolerance)
{
int index, i, j, k, p, np;
int TheIndex = Index;
// protection
int first, last;
if (Periodic)
{
first = Knots.Lower();
last = Knots.Upper();
}
else
{
first = BSplCLib::FirstUKnotIndex(Degree, Mults) + 1;
last = BSplCLib::LastUKnotIndex(Degree, Mults) - 1;
}
if (Index < first)
{
return false;
}
if (Index > last)
{
return false;
}
if (Periodic && (Index == first))
{
TheIndex = last;
}
int depth = Mults(TheIndex) - Mult;
int length = Degree - Mult;
// -------------------
// create local arrays
// -------------------
// Use stack-based allocation for small arrays (MaxDegree=25, typical Dimension=4)
NCollection_LocalArray<double, 128> knots(4 * Degree);
NCollection_LocalArray<double, 256> poles(static_cast<size_t>(2 * Degree + 1) * Dimension);
// ------------------------------------
// build the knots for anti Boor Scheme
// ------------------------------------
// the new sequence of knots
// is obtained from the knots at Index-1 and Index
BSplCLib::BuildKnots(Degree, TheIndex - 1, Periodic, Knots, &Mults, *knots);
index = PoleIndex(Degree, TheIndex - 1, Periodic, Mults);
BSplCLib::BuildKnots(Degree, TheIndex, Periodic, Knots, &Mults, knots[2 * Degree]);
index += Mult;
for (i = 0; i < Degree - Mult; i++)
{
knots[i] = knots[i + Mult];
}
for (i = Degree - Mult; i < 2 * Degree; i++)
{
knots[i] = knots[2 * Degree + i];
}
// ------------------------------------
// build the poles for anti Boor Scheme
// ------------------------------------
p = Poles.Lower() + index * Dimension;
for (i = 0; i <= length + depth; i++)
{
j = Dimension * BoorIndex(i, length, depth);
for (k = 0; k < Dimension; k++)
{
poles[j + k] = Poles(p + k);
}
p += Dimension;
if (p > Poles.Upper())
{
p = Poles.Lower();
}
}
// ----------------
// Anti Boor Scheme
// ----------------
bool result =
AntiBoorScheme(Knots(TheIndex), Degree, *knots, Dimension, *poles, depth, length, Tolerance);
// ----------------
// copy the results
// ----------------
if (result)
{
// poles
p = Poles.Lower();
np = NewPoles.Lower();
// unmodified poles before
Copy((index + 1) * Dimension, p, Poles, np, NewPoles);
// modified
for (i = 1; i <= length; i++)
{
BSplCLib::GetPole(i, length, 0, Dimension, *poles, np, NewPoles);
}
p += (length + depth) * Dimension;
// unmodified poles after
if (p != Poles.Lower())
{
i = Poles.Upper() - p + 1;
Copy(i, p, Poles, np, NewPoles);
}
// knots and mults
if (Mult > 0)
{
NewKnots = Knots;
NewMults = Mults;
NewMults(TheIndex) = Mult;
if (Periodic)
{
if (TheIndex == first)
{
NewMults(last) = Mult;
}
if (TheIndex == last)
{
NewMults(first) = Mult;
}
}
}
else
{
if (!Periodic || (TheIndex != first && TheIndex != last))
{
for (i = Knots.Lower(); i < TheIndex; i++)
{
NewKnots(i) = Knots(i);
NewMults(i) = Mults(i);
}
for (i = TheIndex + 1; i <= Knots.Upper(); i++)
{
NewKnots(i - 1) = Knots(i);
NewMults(i - 1) = Mults(i);
}
}
else
{
// The interesting case of a Periodic curve
// where the first and last knot is removed.
for (i = first; i < last - 1; i++)
{
NewKnots(i) = Knots(i + 1);
NewMults(i) = Mults(i + 1);
}
NewKnots(last - 1) = NewKnots(first) + Knots(last) - Knots(first);
NewMults(last - 1) = NewMults(first);
}
}
}
return result;
}
//=================================================================================================
int BSplCLib::IncreaseDegreeCountKnots(const int Degree,
const int NewDegree,
const bool Periodic,
const NCollection_Array1<int>& Mults)
{
if (Periodic)
{
return Mults.Length();
}
int f = FirstUKnotIndex(Degree, Mults);
int l = LastUKnotIndex(Degree, Mults);
int m, i, removed = 0, step = NewDegree - Degree;
i = Mults.Lower();
m = Degree + (f - i + 1) * step + 1;
while (m > NewDegree + 1)
{
removed++;
m -= Mults(i) + step;
i++;
}
if (m < NewDegree + 1)
{
removed--;
}
i = Mults.Upper();
m = Degree + (i - l + 1) * step + 1;
while (m > NewDegree + 1)
{
removed++;
m -= Mults(i) + step;
i--;
}
if (m < NewDegree + 1)
{
removed--;
}
return Mults.Length() - removed;
}
//=================================================================================================
void BSplCLib::IncreaseDegree(const int Degree,
const int NewDegree,
const bool Periodic,
const int Dimension,
const NCollection_Array1<double>& Poles,
const NCollection_Array1<double>& Knots,
const NCollection_Array1<int>& Mults,
NCollection_Array1<double>& NewPoles,
NCollection_Array1<double>& NewKnots,
NCollection_Array1<int>& NewMults)
{
// Degree elevation of a BSpline Curve
// This algorithms loops on degree incrementation from Degree to NewDegree.
// The variable curDeg is the current degree to increment.
// Before degree incrementations a "working curve" is created.
// It has the same knot, poles and multiplicities.
// If the curve is periodic knots are added on the working curve before
// and after the existing knots to make it a non-periodic curves.
// The poles are also copied.
// The first and last multiplicity of the working curve are set to Degree+1,
// null poles are added if necessary.
// Then the degree is incremented on the working curve.
// The knots are unchanged but all multiplicities will be incremented.
// Each degree incrementation is achieved by averaging curDeg+1 curves.
// See : Degree elevation of B-spline curves
// Hartmut PRAUTZSCH
// CAGD 1 (1984)
//-------------------------
// create the working curve
//-------------------------
int i, k, f, l, m, pf, pl, firstknot;
pf = 0; // number of null poles added at beginning
pl = 0; // number of null poles added at end
int nbwknots = Knots.Length();
f = FirstUKnotIndex(Degree, Mults);
l = LastUKnotIndex(Degree, Mults);
if (Periodic)
{
// Periodic curves are transformed in non-periodic curves
nbwknots += f - Mults.Lower();
pf = -Degree - 1;
for (i = Mults.Lower(); i <= f; i++)
{
pf += Mults(i);
}
nbwknots += Mults.Upper() - l;
pl = -Degree - 1;
for (i = l; i <= Mults.Upper(); i++)
{
pl += Mults(i);
}
}
// copy the knots and multiplicities
NCollection_Array1<double> wknots(1, nbwknots);
NCollection_Array1<int> wmults(1, nbwknots);
if (!Periodic)
{
wknots = Knots;
wmults = Mults;
}
else
{
// copy the knots for a periodic curve
double period = Knots(Knots.Upper()) - Knots(Knots.Lower());
i = 0;
for (k = l; k < Knots.Upper(); k++)
{
i++;
wknots(i) = Knots(k) - period;
wmults(i) = Mults(k);
}
for (k = Knots.Lower(); k <= Knots.Upper(); k++)
{
i++;
wknots(i) = Knots(k);
wmults(i) = Mults(k);
}
for (k = Knots.Lower() + 1; k <= f; k++)
{
i++;
wknots(i) = Knots(k) + period;
wmults(i) = Mults(k);
}
}
// set the first and last mults to Degree+1
// and add null poles
pf += Degree + 1 - wmults(1);
wmults(1) = Degree + 1;
pl += Degree + 1 - wmults(nbwknots);
wmults(nbwknots) = Degree + 1;
//---------------------------
// poles of the working curve
//---------------------------
int nbwpoles = 0;
for (i = 1; i <= nbwknots; i++)
{
nbwpoles += wmults(i);
}
nbwpoles -= Degree + 1;
// we provide space for degree elevation
NCollection_Array1<double> wpoles(1,
(nbwpoles + (nbwknots - 1) * (NewDegree - Degree)) * Dimension);
for (i = 1; i <= pf * Dimension; i++)
{
wpoles(i) = 0;
}
k = Poles.Lower();
for (i = pf * Dimension + 1; i <= (nbwpoles - pl) * Dimension; i++)
{
wpoles(i) = Poles(k);
k++;
if (k > Poles.Upper())
{
k = Poles.Lower();
}
}
for (i = (nbwpoles - pl) * Dimension + 1; i <= nbwpoles * Dimension; i++)
{
wpoles(i) = 0;
}
//-----------------------------------------------------------
// Declare the temporary arrays used in degree incrementation
//-----------------------------------------------------------
// Maximum number of poles after all degree elevations
const int nbwpMax = nbwpoles + (nbwknots - 1) * (NewDegree - Degree);
// Arrays for storing the temporary curves
NCollection_Array1<double> tempc1(1, nbwpMax * Dimension);
NCollection_Array1<double> tempc2(1, nbwpMax * Dimension);
// Array for storing the knots to insert
NCollection_Array1<double> iknots(1, nbwknots);
// Arrays for receiving the knots after insertion
NCollection_Array1<double> nknots(1, nbwknots);
// Pre-allocate nwpoles with maximum size to avoid repeated heap allocations in loop
NCollection_Array1<double> nwpoles(1, nbwpMax * Dimension);
//------------------------------
// Loop on degree incrementation
//------------------------------
int step, curDeg;
int nbp = nbwpoles;
int nbwp = nbp;
for (curDeg = Degree; curDeg < NewDegree; curDeg++)
{
nbp = nbwp; // current number of poles
nbwp = nbp + nbwknots - 1; // new number of poles
// Initialize the portion of nwpoles that will be used this iteration
for (int idx = 1; idx <= nbwp * Dimension; idx++)
{
nwpoles(idx) = 0.0;
}
for (step = 0; step <= curDeg; step++)
{
// Compute the bspline of rank step.
// if not the first time, decrement the multiplicities back
if (step != 0)
{
for (i = 1; i <= nbwknots; i++)
{
wmults(i)--;
}
}
// Poles are the current poles
// but the poles congruent to step are duplicated.
int offset = 0;
for (i = 0; i < nbp; i++)
{
offset++;
for (k = 0; k < Dimension; k++)
{
tempc1((offset - 1) * Dimension + k + 1) = wpoles(NewPoles.Lower() + i * Dimension + k);
}
if (i % (curDeg + 1) == step)
{
offset++;
for (k = 0; k < Dimension; k++)
{
tempc1((offset - 1) * Dimension + k + 1) = wpoles(NewPoles.Lower() + i * Dimension + k);
}
}
}
// Knots multiplicities are increased
// For each knot where the sum of multiplicities is congruent to step
int stepmult = step + 1;
int nbknots = 0;
int smult = 0;
for (k = 1; k <= nbwknots; k++)
{
smult += wmults(k);
if (smult >= stepmult)
{
// this knot is increased
stepmult += curDeg + 1;
wmults(k)++;
}
else
{
// this knot is inserted
nbknots++;
iknots(nbknots) = wknots(k);
}
}
// the curve is obtained by inserting the knots
// to raise the multiplicities
// we build "slices" of the arrays to set the correct size
if (nbknots > 0)
{
NCollection_Array1<double> aknots(iknots(1), 1, nbknots);
NCollection_Array1<double> curve(tempc1(1), 1, offset * Dimension);
NCollection_Array1<double> ncurve(tempc2(1), 1, nbwp * Dimension);
// InsertKnots(curDeg+1,false,Dimension,curve,wknots,wmults,
// aknots,NoMults(),ncurve,nknots,wmults,Epsilon(1.));
InsertKnots(curDeg + 1,
false,
Dimension,
curve,
wknots,
wmults,
aknots,
NoMults(),
ncurve,
nknots,
wmults,
0.0);
// add to the average
for (i = 1; i <= nbwp * Dimension; i++)
{
nwpoles(i) += ncurve(i);
}
}
else
{
// add to the average
for (i = 1; i <= nbwp * Dimension; i++)
{
nwpoles(i) += tempc1(i);
}
}
}
// The result is the average
for (i = 1; i <= nbwp * Dimension; i++)
{
wpoles(i) = nwpoles(i) / (curDeg + 1);
}
}
//-----------------
// Copy the results
//-----------------
// index in new knots of the first knot of the curve
if (Periodic)
{
firstknot = Mults.Upper() - l + 1;
}
else
{
firstknot = f;
}
// the new curve starts at index firstknot
// so we must remove knots until the sum of multiplicities
// from the first to the start is NewDegree+1
// m is the current sum of multiplicities
m = 0;
for (k = 1; k <= firstknot; k++)
{
m += wmults(k);
}
// compute the new first knot (k), pf will be the index of the first pole
k = 1;
pf = 0;
while (m > NewDegree + 1)
{
k++;
m -= wmults(k);
pf += wmults(k);
}
if (m < NewDegree + 1)
{
k--;
wmults(k) += m - NewDegree - 1;
pf += m - NewDegree - 1;
}
// on a periodic curve the knots start with firstknot
if (Periodic)
{
k = firstknot;
}
// copy knots
for (i = NewKnots.Lower(); i <= NewKnots.Upper(); i++)
{
NewKnots(i) = wknots(k);
NewMults(i) = wmults(k);
k++;
}
// copy poles
pf *= Dimension;
for (i = NewPoles.Lower(); i <= NewPoles.Upper(); i++)
{
pf++;
NewPoles(i) = wpoles(pf);
}
}
//=================================================================================================
void BSplCLib::PrepareUnperiodize(const int Degree,
const NCollection_Array1<int>& Mults,
int& NbKnots,
int& NbPoles)
{
int i;
// initialize NbKnots and NbPoles
NbKnots = Mults.Length();
NbPoles = -Degree - 1;
for (i = Mults.Lower(); i <= Mults.Upper(); i++)
{
NbPoles += Mults(i);
}
int sigma, k;
// Add knots at the beginning of the curve to raise Multiplicities
// to Degre + 1;
sigma = Mults(Mults.Lower());
k = Mults.Upper() - 1;
while (sigma < Degree + 1)
{
sigma += Mults(k);
NbPoles += Mults(k);
k--;
NbKnots++;
}
// We must add exactly until Degree + 1 ->
// Suppress the excedent.
if (sigma > Degree + 1)
{
NbPoles -= sigma - Degree - 1;
}
// Add knots at the end of the curve to raise Multiplicities
// to Degre + 1;
sigma = Mults(Mults.Upper());
k = Mults.Lower() + 1;
while (sigma < Degree + 1)
{
sigma += Mults(k);
NbPoles += Mults(k);
k++;
NbKnots++;
}
// We must add exactly until Degree + 1 ->
// Suppress the excedent.
if (sigma > Degree + 1)
{
NbPoles -= sigma - Degree - 1;
}
}
//=================================================================================================
void BSplCLib::Unperiodize(const int Degree,
const int, // Dimension,
const NCollection_Array1<int>& Mults,
const NCollection_Array1<double>& Knots,
const NCollection_Array1<double>& Poles,
NCollection_Array1<int>& NewMults,
NCollection_Array1<double>& NewKnots,
NCollection_Array1<double>& NewPoles)
{
int sigma, k, index = 0;
// evaluation of index : number of knots to insert before knot(1) to
// raise sum of multiplicities to <Degree + 1>
sigma = Mults(Mults.Lower());
k = Mults.Upper() - 1;
while (sigma < Degree + 1)
{
sigma += Mults(k);
k--;
index++;
}
double period = Knots(Knots.Upper()) - Knots(Knots.Lower());
// set the 'interior' knots;
for (k = 1; k <= Knots.Length(); k++)
{
NewKnots(k + index) = Knots(k);
NewMults(k + index) = Mults(k);
}
// set the 'starting' knots;
for (k = 1; k <= index; k++)
{
NewKnots(k) = NewKnots(k + Knots.Length() - 1) - period;
NewMults(k) = NewMults(k + Knots.Length() - 1);
}
NewMults(1) -= sigma - Degree - 1;
// set the 'ending' knots;
sigma = NewMults(index + Knots.Length());
for (k = Knots.Length() + index + 1; k <= NewKnots.Length(); k++)
{
NewKnots(k) = NewKnots(k - Knots.Length() + 1) + period;
NewMults(k) = NewMults(k - Knots.Length() + 1);
sigma += NewMults(k - Knots.Length() + 1);
}
NewMults(NewMults.Length()) -= sigma - Degree - 1;
for (k = 1; k <= NewPoles.Length(); k++)
{
NewPoles(k) = Poles((k - 1) % Poles.Length() + 1);
}
}
//=================================================================================================
void BSplCLib::PrepareTrimming(const int Degree,
const bool Periodic,
const NCollection_Array1<double>& Knots,
const NCollection_Array1<int>& Mults,
const double U1,
const double U2,
int& NbKnots,
int& NbPoles)
{
int i;
double NewU1, NewU2;
int index1 = 0, index2 = 0;
// Eval index1, index2 : position of U1 and U2 in the Array Knots
// such as Knots(index1-1) <= U1 < Knots(index1)
// Knots(index2-1) <= U2 < Knots(index2)
LocateParameter(Degree, Knots, Mults, U1, Periodic, Knots.Lower(), Knots.Upper(), index1, NewU1);
LocateParameter(Degree, Knots, Mults, U2, Periodic, Knots.Lower(), Knots.Upper(), index2, NewU2);
index1++;
if (std::abs(Knots(index2) - U2) <= Epsilon(U1))
{
index2--;
}
// eval NbKnots:
NbKnots = index2 - index1 + 3;
// eval NbPoles:
NbPoles = Degree + 1;
for (i = index1; i <= index2; i++)
{
NbPoles += Mults(i);
}
}
//=================================================================================================
void BSplCLib::Trimming(const int Degree,
const bool Periodic,
const int Dimension,
const NCollection_Array1<double>& Knots,
const NCollection_Array1<int>& Mults,
const NCollection_Array1<double>& Poles,
const double U1,
const double U2,
NCollection_Array1<double>& NewKnots,
NCollection_Array1<int>& NewMults,
NCollection_Array1<double>& NewPoles)
{
int i, nbpoles = 0, nbknots = 0;
double kk[2] = {U1, U2};
int mm[2] = {Degree, Degree};
NCollection_Array1<double> K(kk[0], 1, 2);
NCollection_Array1<int> M(mm[0], 1, 2);
if (!PrepareInsertKnots(Degree,
Periodic,
Knots,
Mults,
K,
&M,
nbpoles,
nbknots,
Epsilon(U1),
false))
{
throw Standard_OutOfRange();
}
NCollection_Array1<double> TempPoles(1, nbpoles * Dimension);
NCollection_Array1<double> TempKnots(1, nbknots);
NCollection_Array1<int> TempMults(1, nbknots);
//
// do not allow the multiplicities to Add : they must be less than Degree
//
InsertKnots(Degree,
Periodic,
Dimension,
Poles,
Knots,
Mults,
K,
&M,
TempPoles,
TempKnots,
TempMults,
Epsilon(U1),
false);
// find in TempPoles the index of the pole corresponding to U1
int Kindex = 0, Pindex;
double NewU1;
LocateParameter(Degree,
TempKnots,
TempMults,
U1,
Periodic,
TempKnots.Lower(),
TempKnots.Upper(),
Kindex,
NewU1);
Pindex = PoleIndex(Degree, Kindex, Periodic, TempMults);
Pindex *= Dimension;
for (i = 1; i <= NewPoles.Length(); i++)
{
NewPoles(i) = TempPoles(Pindex + i);
}
for (i = 1; i <= NewKnots.Length(); i++)
{
NewKnots(i) = TempKnots(Kindex + i - 1);
NewMults(i) = TempMults(Kindex + i - 1);
}
NewMults(1) = std::min(Degree, NewMults(1)) + 1;
NewMults(NewMults.Length()) = std::min(Degree, NewMults(NewMults.Length())) + 1;
}
//=================================================================================================
int BSplCLib::SolveBandedSystem(const math_Matrix& Matrix,
const int UpperBandWidth,
const int LowerBandWidth,
const int ArrayDimension,
double& Array)
{
if (Matrix.LowerCol() != 1 || Matrix.UpperCol() != UpperBandWidth + LowerBandWidth + 1)
{
return 1;
}
double* PolesArray = &Array;
const int aLRow = Matrix.LowerRow();
const int aNCols = Matrix.ColNumber();
const double* aMatData = &Matrix(aLRow, 1);
for (int ii = aLRow + 1; ii <= Matrix.UpperRow(); ii++)
{
const int aMinIndex = (ii - LowerBandWidth >= aLRow ? ii - LowerBandWidth : aLRow);
const double* aRowII = aMatData + (ii - aLRow) * aNCols;
for (int jj = aMinIndex; jj < ii; jj++)
{
const double aCoeff = aRowII[jj - ii + LowerBandWidth];
for (int kk = 0; kk < ArrayDimension; kk++)
{
PolesArray[(ii - 1) * ArrayDimension + kk] +=
PolesArray[(jj - 1) * ArrayDimension + kk] * aCoeff;
}
}
}
for (int ii = Matrix.UpperRow(); ii >= aLRow; ii--)
{
const int aMaxIndex =
(ii + UpperBandWidth <= Matrix.UpperRow() ? ii + UpperBandWidth : Matrix.UpperRow());
const double* aRowII = aMatData + (ii - aLRow) * aNCols;
for (int jj = aMaxIndex; jj > ii; jj--)
{
const double aCoeff = aRowII[jj - ii + LowerBandWidth];
for (int kk = 0; kk < ArrayDimension; kk++)
{
PolesArray[(ii - 1) * ArrayDimension + kk] -=
PolesArray[(jj - 1) * ArrayDimension + kk] * aCoeff;
}
}
// Fixing a bug PRO18577 to avoid division by zero
const double aDivisor = aRowII[LowerBandWidth];
constexpr double THE_TOLERANCE = 1.0e-16;
if (std::abs(aDivisor) <= THE_TOLERANCE)
{
return 1;
}
const double anInverse = 1.0 / aDivisor;
for (int kk = 0; kk < ArrayDimension; kk++)
{
PolesArray[(ii - 1) * ArrayDimension + kk] *= anInverse;
}
}
return 0;
}
//=================================================================================================
int BSplCLib::SolveBandedSystem(const math_Matrix& Matrix,
const int UpperBandWidth,
const int LowerBandWidth,
const bool HomogeneousFlag,
const int ArrayDimension,
double& Poles,
double& Weights)
{
if (Matrix.LowerCol() != 1 || Matrix.UpperCol() != UpperBandWidth + LowerBandWidth + 1)
{
return 1;
}
double* aPolesArray = &Poles;
double* aWeightsArray = &Weights;
const int aNumRows = Matrix.UpperRow() - Matrix.LowerRow() + 1;
if (!HomogeneousFlag)
{
for (int ii = 0; ii < aNumRows; ii++)
{
for (int kk = 0; kk < ArrayDimension; kk++)
{
aPolesArray[ii * ArrayDimension + kk] *= aWeightsArray[ii];
}
}
}
int anErrorCode =
BSplCLib::SolveBandedSystem(Matrix, UpperBandWidth, LowerBandWidth, ArrayDimension, Poles);
if (anErrorCode != 0)
{
return 2;
}
anErrorCode = BSplCLib::SolveBandedSystem(Matrix, UpperBandWidth, LowerBandWidth, 1, Weights);
if (anErrorCode != 0)
{
return 3;
}
if (!HomogeneousFlag)
{
for (int ii = 0; ii < aNumRows; ii++)
{
const double anInverse = 1.0 / aWeightsArray[ii];
for (int kk = 0; kk < ArrayDimension; kk++)
{
aPolesArray[ii * ArrayDimension + kk] *= anInverse;
}
}
}
return 0;
}
//=================================================================================================
void BSplCLib::BuildSchoenbergPoints(const int Degree,
const NCollection_Array1<double>& FlatKnots,
NCollection_Array1<double>& Parameters)
{
int ii, jj;
double Inverse;
Inverse = 1.0e0 / (double)Degree;
for (ii = Parameters.Lower(); ii <= Parameters.Upper(); ii++)
{
Parameters(ii) = 0.0e0;
for (jj = 1; jj <= Degree; jj++)
{
Parameters(ii) += FlatKnots(jj + ii);
}
Parameters(ii) *= Inverse;
}
}
//=================================================================================================
void BSplCLib::Interpolate(const int Degree,
const NCollection_Array1<double>& FlatKnots,
const NCollection_Array1<double>& Parameters,
const NCollection_Array1<int>& ContactOrderArray,
const int ArrayDimension,
double& Poles,
int& InversionProblem)
{
int ErrorCode, UpperBandWidth, LowerBandWidth;
constexpr int THE_STACK_LIMIT = 2048;
const int aMatSize = Parameters.Length() * (2 * Degree + 1);
double aStackBuf[THE_STACK_LIMIT];
math_Matrix InterpolationMatrix =
(aMatSize <= THE_STACK_LIMIT)
? math_Matrix(aStackBuf, 1, Parameters.Length(), 1, 2 * Degree + 1)
: math_Matrix(1, Parameters.Length(), 1, 2 * Degree + 1);
ErrorCode = BSplCLib::BuildBSpMatrix(Parameters,
ContactOrderArray,
FlatKnots,
Degree,
InterpolationMatrix,
UpperBandWidth,
LowerBandWidth);
if (ErrorCode)
{
throw Standard_OutOfRange("BSplCLib::Interpolate");
}
ErrorCode = BSplCLib::FactorBandedMatrix(InterpolationMatrix,
UpperBandWidth,
LowerBandWidth,
InversionProblem);
if (ErrorCode)
{
throw Standard_OutOfRange("BSplCLib::Interpolate");
}
ErrorCode = BSplCLib::SolveBandedSystem(InterpolationMatrix,
UpperBandWidth,
LowerBandWidth,
ArrayDimension,
Poles);
if (ErrorCode)
{
throw Standard_OutOfRange("BSplCLib::Interpolate");
}
}
//=================================================================================================
void BSplCLib::Interpolate(const int Degree,
const NCollection_Array1<double>& FlatKnots,
const NCollection_Array1<double>& Parameters,
const NCollection_Array1<int>& ContactOrderArray,
const int ArrayDimension,
double& Poles,
double& Weights,
int& InversionProblem)
{
int ErrorCode, UpperBandWidth, LowerBandWidth;
constexpr int THE_STACK_LIMIT = 2048;
const int aMatSize = Parameters.Length() * (2 * Degree + 1);
double aStackBuf[THE_STACK_LIMIT];
math_Matrix InterpolationMatrix =
(aMatSize <= THE_STACK_LIMIT)
? math_Matrix(aStackBuf, 1, Parameters.Length(), 1, 2 * Degree + 1)
: math_Matrix(1, Parameters.Length(), 1, 2 * Degree + 1);
ErrorCode = BSplCLib::BuildBSpMatrix(Parameters,
ContactOrderArray,
FlatKnots,
Degree,
InterpolationMatrix,
UpperBandWidth,
LowerBandWidth);
if (ErrorCode)
{
throw Standard_OutOfRange("BSplCLib::Interpolate");
}
ErrorCode = BSplCLib::FactorBandedMatrix(InterpolationMatrix,
UpperBandWidth,
LowerBandWidth,
InversionProblem);
if (ErrorCode)
{
throw Standard_OutOfRange("BSplCLib::Interpolate");
}
ErrorCode = BSplCLib::SolveBandedSystem(InterpolationMatrix,
UpperBandWidth,
LowerBandWidth,
false,
ArrayDimension,
Poles,
Weights);
if (ErrorCode)
{
throw Standard_OutOfRange("BSplCLib::Interpolate");
}
}
//=================================================================================================
void BSplCLib::Eval(const double Parameter,
const bool PeriodicFlag,
const int DerivativeRequest,
int& ExtrapMode,
const int Degree,
const NCollection_Array1<double>& FlatKnots,
const int ArrayDimension,
double& Poles,
double& Weights,
double& PolesResults,
double& WeightsResults)
{
int ii, jj, kk = 0, Index, Index1, Index2, *ExtrapModeArray, Modulus, NewRequest,
ExtrapolatingFlag[2], ErrorCode, Order = Degree + 1, FirstNonZeroBsplineIndex,
LocalRequest = DerivativeRequest;
double *PResultArray, *WResultArray, *PolesArray, *WeightsArray, LocalParameter, Period, Inverse,
Delta;
PolesArray = &Poles;
WeightsArray = &Weights;
ExtrapModeArray = &ExtrapMode;
PResultArray = &PolesResults;
WResultArray = &WeightsResults;
LocalParameter = Parameter;
ExtrapolatingFlag[0] = ExtrapolatingFlag[1] = 0;
//
// check if we are extrapolating to a degree which is smaller than
// the degree of the Bspline
//
if (PeriodicFlag)
{
Period = FlatKnots(FlatKnots.Upper() - 1) - FlatKnots(2);
while (LocalParameter > FlatKnots(FlatKnots.Upper() - 1))
{
LocalParameter -= Period;
}
while (LocalParameter < FlatKnots(2))
{
LocalParameter += Period;
}
}
if (Parameter < FlatKnots(2) && LocalRequest < ExtrapModeArray[0] && ExtrapModeArray[0] < Degree)
{
LocalRequest = ExtrapModeArray[0];
LocalParameter = FlatKnots(2);
ExtrapolatingFlag[0] = 1;
}
if (Parameter > FlatKnots(FlatKnots.Upper() - 1) && LocalRequest < ExtrapModeArray[1]
&& ExtrapModeArray[1] < Degree)
{
LocalRequest = ExtrapModeArray[1];
LocalParameter = FlatKnots(FlatKnots.Upper() - 1);
ExtrapolatingFlag[1] = 1;
}
Delta = Parameter - LocalParameter;
if (LocalRequest >= Order)
{
LocalRequest = Degree;
}
if (PeriodicFlag)
{
Modulus = FlatKnots.Length() - Degree - 1;
}
else
{
Modulus = FlatKnots.Length() - Degree;
}
BSplCLib_LocalMatrix BsplineBasis(LocalRequest, Order);
ErrorCode = BSplCLib::EvalBsplineBasis(LocalRequest,
Order,
FlatKnots,
LocalParameter,
FirstNonZeroBsplineIndex,
BsplineBasis);
if (ErrorCode != 0)
{
return;
}
if (ExtrapolatingFlag[0] == 0 && ExtrapolatingFlag[1] == 0)
{
Index = 0;
Index2 = 0;
for (ii = 1; ii <= LocalRequest + 1; ii++)
{
Index1 = FirstNonZeroBsplineIndex;
for (kk = 0; kk < ArrayDimension; kk++)
{
PResultArray[Index + kk] = 0.0e0;
}
WResultArray[Index] = 0.0e0;
for (jj = 1; jj <= Order; jj++)
{
for (kk = 0; kk < ArrayDimension; kk++)
{
PResultArray[Index + kk] += PolesArray[(Index1 - 1) * ArrayDimension + kk]
* WeightsArray[Index1 - 1] * BsplineBasis(ii, jj);
}
WResultArray[Index2] += WeightsArray[Index1 - 1] * BsplineBasis(ii, jj);
Index1 = Index1 % Modulus;
Index1 += 1;
}
Index += ArrayDimension;
Index2 += 1;
}
}
else
{
//
// store Taylor expansion in LocalRealArray
//
NewRequest = DerivativeRequest;
if (NewRequest > Degree)
{
NewRequest = Degree;
}
NCollection_LocalArray<double> LocalRealArray(static_cast<size_t>(LocalRequest + 1)
* ArrayDimension);
Index = 0;
Inverse = 1.0e0;
for (ii = 1; ii <= LocalRequest + 1; ii++)
{
Index1 = FirstNonZeroBsplineIndex;
for (kk = 0; kk < ArrayDimension; kk++)
{
LocalRealArray[Index + kk] = 0.0e0;
}
for (jj = 1; jj <= Order; jj++)
{
for (kk = 0; kk < ArrayDimension; kk++)
{
LocalRealArray[Index + kk] += PolesArray[(Index1 - 1) * ArrayDimension + kk]
* WeightsArray[Index1 - 1] * BsplineBasis(ii, jj);
}
Index1 = Index1 % Modulus;
Index1 += 1;
}
for (kk = 0; kk < ArrayDimension; kk++)
{
LocalRealArray[Index + kk] *= Inverse;
}
Index += ArrayDimension;
Inverse /= (double)ii;
}
PLib::EvalPolynomial(Delta,
NewRequest,
Degree,
ArrayDimension,
LocalRealArray[0],
PolesResults);
Index = 0;
Inverse = 1.0e0;
for (ii = 1; ii <= LocalRequest + 1; ii++)
{
Index1 = FirstNonZeroBsplineIndex;
LocalRealArray[Index] = 0.0e0;
for (jj = 1; jj <= Order; jj++)
{
LocalRealArray[Index] += WeightsArray[Index1 - 1] * BsplineBasis(ii, jj);
Index1 = Index1 % Modulus;
Index1 += 1;
}
LocalRealArray[Index] *= Inverse;
Index += 1;
Inverse /= (double)ii;
}
PLib::EvalPolynomial(Delta, NewRequest, Degree, 1, LocalRealArray[0], WeightsResults);
}
}
//=================================================================================================
void BSplCLib::Eval(const double Parameter,
const bool PeriodicFlag,
const int DerivativeRequest,
int& ExtrapMode,
const int Degree,
const NCollection_Array1<double>& FlatKnots,
const int ArrayDimension,
double& Poles,
double& Results)
{
int ii, jj, kk, Index, Index1, *ExtrapModeArray, Modulus, NewRequest, ExtrapolatingFlag[2],
ErrorCode, Order = Degree + 1, FirstNonZeroBsplineIndex, LocalRequest = DerivativeRequest;
double *ResultArray, *PolesArray, LocalParameter, Period, Inverse, Delta;
PolesArray = &Poles;
ExtrapModeArray = &ExtrapMode;
ResultArray = &Results;
LocalParameter = Parameter;
ExtrapolatingFlag[0] = ExtrapolatingFlag[1] = 0;
//
// check if we are extrapolating to a degree which is smaller than
// the degree of the Bspline
//
if (PeriodicFlag)
{
Period = FlatKnots(FlatKnots.Upper() - 1) - FlatKnots(2);
while (LocalParameter > FlatKnots(FlatKnots.Upper() - 1))
{
LocalParameter -= Period;
}
while (LocalParameter < FlatKnots(2))
{
LocalParameter += Period;
}
}
if (Parameter < FlatKnots(2) && LocalRequest < ExtrapModeArray[0] && ExtrapModeArray[0] < Degree)
{
LocalRequest = ExtrapModeArray[0];
LocalParameter = FlatKnots(2);
ExtrapolatingFlag[0] = 1;
}
if (Parameter > FlatKnots(FlatKnots.Upper() - 1) && LocalRequest < ExtrapModeArray[1]
&& ExtrapModeArray[1] < Degree)
{
LocalRequest = ExtrapModeArray[1];
LocalParameter = FlatKnots(FlatKnots.Upper() - 1);
ExtrapolatingFlag[1] = 1;
}
Delta = Parameter - LocalParameter;
if (LocalRequest >= Order)
{
LocalRequest = Degree;
}
if (PeriodicFlag)
{
Modulus = FlatKnots.Length() - Degree - 1;
}
else
{
Modulus = FlatKnots.Length() - Degree;
}
BSplCLib_LocalMatrix BsplineBasis(LocalRequest, Order);
ErrorCode = BSplCLib::EvalBsplineBasis(LocalRequest,
Order,
FlatKnots,
LocalParameter,
FirstNonZeroBsplineIndex,
BsplineBasis);
if (ErrorCode != 0)
{
return;
}
if (ExtrapolatingFlag[0] == 0 && ExtrapolatingFlag[1] == 0)
{
Index = 0;
for (ii = 1; ii <= LocalRequest + 1; ii++)
{
Index1 = FirstNonZeroBsplineIndex;
for (kk = 0; kk < ArrayDimension; kk++)
{
ResultArray[Index + kk] = 0.0e0;
}
for (jj = 1; jj <= Order; jj++)
{
for (kk = 0; kk < ArrayDimension; kk++)
{
ResultArray[Index + kk] +=
PolesArray[(Index1 - 1) * ArrayDimension + kk] * BsplineBasis(ii, jj);
}
Index1 = Index1 % Modulus;
Index1 += 1;
}
Index += ArrayDimension;
}
}
else
{
//
// store Taylor expansion in LocalRealArray
//
NewRequest = DerivativeRequest;
if (NewRequest > Degree)
{
NewRequest = Degree;
}
NCollection_LocalArray<double> LocalRealArray(static_cast<size_t>(LocalRequest + 1)
* ArrayDimension);
Index = 0;
Inverse = 1.0e0;
for (ii = 1; ii <= LocalRequest + 1; ii++)
{
Index1 = FirstNonZeroBsplineIndex;
for (kk = 0; kk < ArrayDimension; kk++)
{
LocalRealArray[Index + kk] = 0.0e0;
}
for (jj = 1; jj <= Order; jj++)
{
for (kk = 0; kk < ArrayDimension; kk++)
{
LocalRealArray[Index + kk] +=
PolesArray[(Index1 - 1) * ArrayDimension + kk] * BsplineBasis(ii, jj);
}
Index1 = Index1 % Modulus;
Index1 += 1;
}
for (kk = 0; kk < ArrayDimension; kk++)
{
LocalRealArray[Index + kk] *= Inverse;
}
Index += ArrayDimension;
Inverse /= (double)ii;
}
PLib::EvalPolynomial(Delta, NewRequest, Degree, ArrayDimension, LocalRealArray[0], Results);
}
}
//=================================================================================================
void BSplCLib::TangExtendToConstraint(const NCollection_Array1<double>& FlatKnots,
const double C1Coefficient,
const int NumPoles,
double& Poles,
const int CDimension,
const int CDegree,
const NCollection_Array1<double>& ConstraintPoint,
const int Continuity,
const bool After,
int& NbPolesResult,
int& NbKnotsResult,
double& KnotsResult,
double& PolesResult)
{
#ifdef OCCT_DEBUG
if (CDegree < Continuity + 1)
{
std::cout << "The BSpline degree must be greater than the order of continuity" << std::endl;
}
#endif
double* Padr = &Poles;
double* KRadr = &KnotsResult;
double* PRadr = &PolesResult;
////////////////////////////////////////////////////////////////////////
//
// 1. calculation of extension nD
//
////////////////////////////////////////////////////////////////////////
// Hermite matrix
int Csize = Continuity + 2;
math_Matrix MatCoefs(1, Csize, 1, Csize);
if (After)
{
PLib::HermiteCoefficients(0,
1, // Limits
Continuity,
0, // Orders of constraints
MatCoefs);
}
else
{
PLib::HermiteCoefficients(0,
1, // Limits
0,
Continuity, // Orders of constraints
MatCoefs);
}
// position at the node of connection
double Tbord;
if (After)
{
Tbord = FlatKnots(FlatKnots.Upper() - CDegree);
}
else
{
Tbord = FlatKnots(FlatKnots.Lower() + CDegree);
}
bool periodic_flag = false;
int ipos, extrap_mode[2], derivative_request = std::max(Continuity, 1);
extrap_mode[0] = extrap_mode[1] = CDegree;
// Use math_Vector for stack allocation
math_Vector EvalBS(1, CDimension * (derivative_request + 1));
double* Eadr = &EvalBS(1);
BSplCLib::Eval(Tbord,
periodic_flag,
derivative_request,
extrap_mode[0],
CDegree,
FlatKnots,
CDimension,
Poles,
*Eadr);
// norm of the tangent at the node of connection
math_Vector Tgte(1, CDimension);
for (ipos = 1; ipos <= CDimension; ipos++)
{
Tgte(ipos) = EvalBS(ipos + CDimension);
}
double L1 = Tgte.Norm();
// matrix of constraints
math_Matrix Contraintes(1, Csize, 1, CDimension);
// Precompute powers of C1Coefficient for efficiency (avoid std::pow overhead)
const double C1Coeff2 = C1Coefficient * C1Coefficient;
const double C1Coeff3 = C1Coeff2 * C1Coefficient;
if (After)
{
for (ipos = 1; ipos <= CDimension; ipos++)
{
Contraintes(1, ipos) = EvalBS(ipos);
Contraintes(2, ipos) = C1Coefficient * EvalBS(ipos + CDimension);
if (Continuity >= 2)
{
Contraintes(3, ipos) = EvalBS(ipos + 2 * CDimension) * C1Coeff2;
}
if (Continuity >= 3)
{
Contraintes(4, ipos) = EvalBS(ipos + 3 * CDimension) * C1Coeff3;
}
Contraintes(Continuity + 2, ipos) = ConstraintPoint(ipos);
}
}
else
{
for (ipos = 1; ipos <= CDimension; ipos++)
{
Contraintes(1, ipos) = ConstraintPoint(ipos);
Contraintes(2, ipos) = EvalBS(ipos);
if (Continuity >= 1)
{
Contraintes(3, ipos) = C1Coefficient * EvalBS(ipos + CDimension);
}
if (Continuity >= 2)
{
Contraintes(4, ipos) = EvalBS(ipos + 2 * CDimension) * C1Coeff2;
}
if (Continuity >= 3)
{
Contraintes(5, ipos) = EvalBS(ipos + 3 * CDimension) * C1Coeff3;
}
}
}
// calculate the coefficients of extension
int ii, jj, kk;
NCollection_Array1<double> ExtraCoeffs(1, Csize * CDimension);
ExtraCoeffs.Init(0.);
for (ii = 1; ii <= Csize; ii++)
{
for (jj = 1; jj <= Csize; jj++)
{
for (kk = 1; kk <= CDimension; kk++)
{
ExtraCoeffs(kk + (jj - 1) * CDimension) += MatCoefs(ii, jj) * Contraintes(ii, kk);
}
}
}
// calculate the poles of extension
NCollection_Array1<double> ExtrapPoles(1, Csize * CDimension);
double* EPadr = &ExtrapPoles(1);
PLib::CoefficientsPoles(CDimension,
ExtraCoeffs,
PLib::NoWeights(),
ExtrapPoles,
PLib::NoWeights());
// calculate the nodes of extension with multiplicities
NCollection_Array1<double> ExtrapNoeuds(1, 2);
ExtrapNoeuds(1) = 0.;
ExtrapNoeuds(2) = 1.;
NCollection_Array1<int> ExtrapMults(1, 2);
ExtrapMults(1) = Csize;
ExtrapMults(2) = Csize;
// flat nodes of extension
NCollection_Array1<double> FK2(1, Csize * 2);
BSplCLib::KnotSequence(ExtrapNoeuds, ExtrapMults, FK2);
// norm of the tangent at the connection point
if (After)
{
BSplCLib::Eval(0., periodic_flag, 1, extrap_mode[0], Csize - 1, FK2, CDimension, *EPadr, *Eadr);
}
else
{
BSplCLib::Eval(1., periodic_flag, 1, extrap_mode[0], Csize - 1, FK2, CDimension, *EPadr, *Eadr);
}
for (ipos = 1; ipos <= CDimension; ipos++)
{
Tgte(ipos) = EvalBS(ipos + CDimension);
}
double L2 = Tgte.Norm();
// harmonisation of degrees
NCollection_Array1<double> NewP2(1, (CDegree + 1) * CDimension);
NCollection_Array1<double> NewK2(1, 2);
NCollection_Array1<int> NewM2(1, 2);
if (Csize - 1 < CDegree)
{
BSplCLib::IncreaseDegree(Csize - 1,
CDegree,
false,
CDimension,
ExtrapPoles,
ExtrapNoeuds,
ExtrapMults,
NewP2,
NewK2,
NewM2);
}
else
{
NewP2 = ExtrapPoles;
NewK2 = ExtrapNoeuds;
NewM2 = ExtrapMults;
}
// flat nodes of extension after harmonization of degrees
NCollection_Array1<double> NewFK2(1, (CDegree + 1) * 2);
BSplCLib::KnotSequence(NewK2, NewM2, NewFK2);
////////////////////////////////////////////////////////////////////////
//
// 2. concatenation C0
//
////////////////////////////////////////////////////////////////////////
// ratio of reparametrization
double Ratio = 1, Delta;
if ((L1 > Precision::Confusion()) && (L2 > Precision::Confusion()))
{
Ratio = L2 / L1;
}
if ((Ratio < 1.e-5) || (Ratio > 1.e5))
{
Ratio = 1;
}
if (After)
{
// do not touch the first BSpline
Delta = Ratio * NewFK2(NewFK2.Lower()) - FlatKnots(FlatKnots.Upper());
}
else
{
// do not touch the second BSpline
Delta = Ratio * NewFK2(NewFK2.Upper()) - FlatKnots(FlatKnots.Lower());
}
// result of the concatenation
int NbP1 = NumPoles, NbP2 = CDegree + 1;
int NbK1 = FlatKnots.Length(), NbK2 = 2 * (CDegree + 1);
NCollection_Array1<double> NewPoles(1, (NbP1 + NbP2 - 1) * CDimension);
NCollection_Array1<double> NewFlats(1, NbK1 + NbK2 - CDegree - 2);
// poles
int indNP, indP, indEP;
if (After)
{
for (ii = 1; ii <= NbP1 + NbP2 - 1; ii++)
{
for (jj = 1; jj <= CDimension; jj++)
{
indNP = (ii - 1) * CDimension + jj;
indP = (ii - 1) * CDimension + jj - 1;
indEP = (ii - NbP1) * CDimension + jj;
if (ii < NbP1)
{
NewPoles(indNP) = Padr[indP];
}
else
{
NewPoles(indNP) = NewP2(indEP);
}
}
}
}
else
{
for (ii = 1; ii <= NbP1 + NbP2 - 1; ii++)
{
for (jj = 1; jj <= CDimension; jj++)
{
indNP = (ii - 1) * CDimension + jj;
indEP = (ii - 1) * CDimension + jj;
indP = (ii - NbP2) * CDimension + jj - 1;
if (ii < NbP2)
{
NewPoles(indNP) = NewP2(indEP);
}
else
{
NewPoles(indNP) = Padr[indP];
}
}
}
}
// flat nodes
if (After)
{
// start with the nodes of the initial surface
for (ii = 1; ii < NbK1; ii++)
{
NewFlats(ii) = FlatKnots(FlatKnots.Lower() + ii - 1);
}
// continue with the reparameterized nodes of the extension
for (ii = 1; ii <= NbK2 - CDegree - 1; ii++)
{
NewFlats(NbK1 + ii - 1) = Ratio * NewFK2(NewFK2.Lower() + ii + CDegree) - Delta;
}
}
else
{
// start with the reparameterized nodes of the extension
for (ii = 1; ii < NbK2 - CDegree; ii++)
{
NewFlats(ii) = Ratio * NewFK2(NewFK2.Lower() + ii - 1) - Delta;
}
// continue with the nodes of the initial surface
for (ii = 2; ii <= NbK1; ii++)
{
NewFlats(NbK2 + ii - CDegree - 2) = FlatKnots(FlatKnots.Lower() + ii - 1);
}
}
////////////////////////////////////////////////////////////////////////
//
// 3. reduction of multiplicite at the node of connection
//
////////////////////////////////////////////////////////////////////////
// number of separate nodes
int KLength = 1;
for (ii = 2; ii <= NbK1 + NbK2 - CDegree - 2; ii++)
{
if (NewFlats(ii) != NewFlats(ii - 1))
{
KLength++;
}
}
// flat nodes --> nodes + multiplicities
NCollection_Array1<double> NewKnots(1, KLength);
NCollection_Array1<int> NewMults(1, KLength);
NewMults.Init(1);
jj = 1;
NewKnots(jj) = NewFlats(1);
for (ii = 2; ii <= NbK1 + NbK2 - CDegree - 2; ii++)
{
if (NewFlats(ii) == NewFlats(ii - 1))
{
NewMults(jj)++;
}
else
{
jj++;
NewKnots(jj) = NewFlats(ii);
}
}
// reduction of multiplicity at the second or the last but one node
int Index = 2, M = CDegree;
if (After)
{
Index = KLength - 1;
}
NCollection_Array1<double> ResultPoles(1, (NbP1 + NbP2 - 1) * CDimension);
NCollection_Array1<double> ResultKnots(1, KLength);
NCollection_Array1<int> ResultMults(1, KLength);
double Tol = 1.e-6;
bool Ok = true;
while ((M > CDegree - Continuity) && Ok)
{
Ok = RemoveKnot(Index,
M - 1,
CDegree,
false,
CDimension,
NewPoles,
NewKnots,
NewMults,
ResultPoles,
ResultKnots,
ResultMults,
Tol);
if (Ok)
{
M--;
}
}
if (M == CDegree)
{
// number of poles of the concatenation
NbPolesResult = NbP1 + NbP2 - 1;
// the poles of the concatenation
int PLength = NbPolesResult * CDimension;
for (jj = 1; jj <= PLength; jj++)
{
PRadr[jj - 1] = NewPoles(jj);
}
// flat nodes of the concatenation
int ideb = 0;
for (jj = 0; jj < NewKnots.Length(); jj++)
{
for (ii = 0; ii < NewMults(jj + 1); ii++)
{
KRadr[ideb + ii] = NewKnots(jj + 1);
}
ideb += NewMults(jj + 1);
}
NbKnotsResult = ideb;
}
else
{
// number of poles of the result
NbPolesResult = NbP1 + NbP2 - 1 - CDegree + M;
// the poles of the result
int PLength = NbPolesResult * CDimension;
for (jj = 0; jj < PLength; jj++)
{
PRadr[jj] = ResultPoles(jj + 1);
}
// flat nodes of the result
int ideb = 0;
for (jj = 0; jj < ResultKnots.Length(); jj++)
{
for (ii = 0; ii < ResultMults(jj + 1); ii++)
{
KRadr[ideb + ii] = ResultKnots(jj + 1);
}
ideb += ResultMults(jj + 1);
}
NbKnotsResult = ideb;
}
}
//=================================================================================================
// Mathematical basis:
// d
// Let C(t) = SUM Ci Bi(t) a Bspline curve of degree d
// i = 1,n
// with nodes tj for j = 1,n+d+1
//
//
// ' C1 - Ci-1 d-1
// Then C (t) = SUM d * --------- Bi (t)
// i = 2,n ti+d - ti
//
// d-1
// for the base of BSpline Bi (t) of degree d-1.
//
// Consequently the upper bound of the norm of the derivative from C is :
//
//
// | Ci - Ci-1 |
// d * Max | --------- |
// i = 2,n | ti+d - ti |
//
// N(t)
// In the rational case set C(t) = -----
// D(t)
//
//
// D(t) = SUM Di Bi(t)
// i=1,n
//
// N(t) = SUM Di * Ci Bi(t)
// i =1,n
//
// N'(t) - D'(t) C(t)
// C'(t) = -----------------------
// D(t)
//
//
// N'(t) - D'(t) C(t) =
//
// Di * (Ci - C(t)) - Di-1 * (Ci-1 - C(t)) d-1
// SUM d * ---------------------------------------- * Bi (t) =
// i=2,n ti+d - ti
//
//
// Di * (Ci - Cj) - Di-1 * (Ci-1 - Cj) d-1
// SUM SUM d * ----------------------------------- * Betaj(t) * Bi (t)
// i=2,n j=1,n ti+d - ti
//
//
//
// Dj Bj(t)
// Betaj(t) = --------
// D(t)
//
// Betaj(t) form a partition >= 0 of the entity with support
// tj, tj+d+1. Consequently if Rj = {j-d, ...., j+d+d+1}
// obtain an upper bound of the derivative of C by taking :
//
//
//
//
//
//
// Di * (Ci - Cj) - Di-1 * (Ci-1 - Cj)
// Max Max d * -----------------------------------
// j=1,n i dans Rj ti+d - ti
//
// --------------------------------------------------------
//
// Min Di
// i =1,n
//
//
//=================================================================================================
void BSplCLib::Resolution(double& Poles,
const int ArrayDimension,
const int NumPoles,
const NCollection_Array1<double>* Weights,
const NCollection_Array1<double>& FlatKnots,
const int Degree,
const double Tolerance3D,
double& UTolerance)
{
int ii, num_poles, ii_index, jj_index, ii_inDim;
int lower, upper, ii_minus, jj, ii_miDim;
int Deg1 = Degree + 1;
int Deg2 = (Degree << 1) + 1;
double value, factor, W, min_weights, inverse;
double pa_ii_inDim_0, pa_ii_inDim_1, pa_ii_inDim_2, pa_ii_inDim_3;
double pa_ii_miDim_0, pa_ii_miDim_1, pa_ii_miDim_2, pa_ii_miDim_3;
double wg_ii_index, wg_ii_minus;
double * PA, max_derivative;
const double* FK = &FlatKnots(FlatKnots.Lower());
PA = &Poles;
max_derivative = 0.0e0;
num_poles = FlatKnots.Length() - Deg1;
switch (ArrayDimension)
{
case 2: {
if (Weights != nullptr)
{
const double* WG = &(*Weights)(Weights->Lower());
min_weights = WG[0];
for (ii = 1; ii < NumPoles; ii++)
{
W = WG[ii];
if (W < min_weights)
{
min_weights = W;
}
}
for (ii = 1; ii < num_poles; ii++)
{
ii_index = ii % NumPoles;
ii_inDim = ii_index << 1;
ii_minus = (ii - 1) % NumPoles;
ii_miDim = ii_minus << 1;
pa_ii_inDim_0 = PA[ii_inDim];
ii_inDim++;
pa_ii_inDim_1 = PA[ii_inDim];
pa_ii_miDim_0 = PA[ii_miDim];
ii_miDim++;
pa_ii_miDim_1 = PA[ii_miDim];
wg_ii_index = WG[ii_index];
wg_ii_minus = WG[ii_minus];
inverse = FK[ii + Degree] - FK[ii];
inverse = 1.0e0 / inverse;
lower = ii - Deg1;
if (lower < 0)
{
lower = 0;
}
upper = Deg2 + ii;
if (upper > num_poles)
{
upper = num_poles;
}
for (jj = lower; jj < upper; jj++)
{
jj_index = jj % NumPoles;
jj_index = jj_index << 1;
value = 0.0e0;
factor = (((PA[jj_index] - pa_ii_inDim_0) * wg_ii_index)
- ((PA[jj_index] - pa_ii_miDim_0) * wg_ii_minus));
if (factor < 0)
{
factor = -factor;
}
value += factor;
jj_index++;
factor = (((PA[jj_index] - pa_ii_inDim_1) * wg_ii_index)
- ((PA[jj_index] - pa_ii_miDim_1) * wg_ii_minus));
if (factor < 0)
{
factor = -factor;
}
value += factor;
value *= inverse;
if (max_derivative < value)
{
max_derivative = value;
}
}
}
max_derivative /= min_weights;
}
else
{
for (ii = 1; ii < num_poles; ii++)
{
ii_index = ii % NumPoles;
ii_index = ii_index << 1;
ii_minus = (ii - 1) % NumPoles;
ii_minus = ii_minus << 1;
inverse = FK[ii + Degree] - FK[ii];
inverse = 1.0e0 / inverse;
value = 0.0e0;
factor = PA[ii_index] - PA[ii_minus];
if (factor < 0)
{
factor = -factor;
}
value += factor;
ii_index++;
ii_minus++;
factor = PA[ii_index] - PA[ii_minus];
if (factor < 0)
{
factor = -factor;
}
value += factor;
value *= inverse;
if (max_derivative < value)
{
max_derivative = value;
}
}
}
break;
}
case 3: {
if (Weights != nullptr)
{
const double* WG = &(*Weights)(Weights->Lower());
min_weights = WG[0];
for (ii = 1; ii < NumPoles; ii++)
{
W = WG[ii];
if (W < min_weights)
{
min_weights = W;
}
}
for (ii = 1; ii < num_poles; ii++)
{
ii_index = ii % NumPoles;
ii_inDim = (ii_index << 1) + ii_index;
ii_minus = (ii - 1) % NumPoles;
ii_miDim = (ii_minus << 1) + ii_minus;
pa_ii_inDim_0 = PA[ii_inDim];
ii_inDim++;
pa_ii_inDim_1 = PA[ii_inDim];
ii_inDim++;
pa_ii_inDim_2 = PA[ii_inDim];
pa_ii_miDim_0 = PA[ii_miDim];
ii_miDim++;
pa_ii_miDim_1 = PA[ii_miDim];
ii_miDim++;
pa_ii_miDim_2 = PA[ii_miDim];
wg_ii_index = WG[ii_index];
wg_ii_minus = WG[ii_minus];
inverse = FK[ii + Degree] - FK[ii];
inverse = 1.0e0 / inverse;
lower = ii - Deg1;
if (lower < 0)
{
lower = 0;
}
upper = Deg2 + ii;
if (upper > num_poles)
{
upper = num_poles;
}
for (jj = lower; jj < upper; jj++)
{
jj_index = jj % NumPoles;
jj_index = (jj_index << 1) + jj_index;
value = 0.0e0;
factor = (((PA[jj_index] - pa_ii_inDim_0) * wg_ii_index)
- ((PA[jj_index] - pa_ii_miDim_0) * wg_ii_minus));
if (factor < 0)
{
factor = -factor;
}
value += factor;
jj_index++;
factor = (((PA[jj_index] - pa_ii_inDim_1) * wg_ii_index)
- ((PA[jj_index] - pa_ii_miDim_1) * wg_ii_minus));
if (factor < 0)
{
factor = -factor;
}
value += factor;
jj_index++;
factor = (((PA[jj_index] - pa_ii_inDim_2) * wg_ii_index)
- ((PA[jj_index] - pa_ii_miDim_2) * wg_ii_minus));
if (factor < 0)
{
factor = -factor;
}
value += factor;
value *= inverse;
if (max_derivative < value)
{
max_derivative = value;
}
}
}
max_derivative /= min_weights;
}
else
{
for (ii = 1; ii < num_poles; ii++)
{
ii_index = ii % NumPoles;
ii_index = (ii_index << 1) + ii_index;
ii_minus = (ii - 1) % NumPoles;
ii_minus = (ii_minus << 1) + ii_minus;
inverse = FK[ii + Degree] - FK[ii];
inverse = 1.0e0 / inverse;
value = 0.0e0;
factor = PA[ii_index] - PA[ii_minus];
if (factor < 0)
{
factor = -factor;
}
value += factor;
ii_index++;
ii_minus++;
factor = PA[ii_index] - PA[ii_minus];
if (factor < 0)
{
factor = -factor;
}
value += factor;
ii_index++;
ii_minus++;
factor = PA[ii_index] - PA[ii_minus];
if (factor < 0)
{
factor = -factor;
}
value += factor;
value *= inverse;
if (max_derivative < value)
{
max_derivative = value;
}
}
}
break;
}
case 4: {
if (Weights != nullptr)
{
const double* WG = &(*Weights)(Weights->Lower());
min_weights = WG[0];
for (ii = 1; ii < NumPoles; ii++)
{
W = WG[ii];
if (W < min_weights)
{
min_weights = W;
}
}
for (ii = 1; ii < num_poles; ii++)
{
ii_index = ii % NumPoles;
ii_inDim = ii_index << 2;
ii_minus = (ii - 1) % NumPoles;
ii_miDim = ii_minus << 2;
pa_ii_inDim_0 = PA[ii_inDim];
ii_inDim++;
pa_ii_inDim_1 = PA[ii_inDim];
ii_inDim++;
pa_ii_inDim_2 = PA[ii_inDim];
ii_inDim++;
pa_ii_inDim_3 = PA[ii_inDim];
pa_ii_miDim_0 = PA[ii_miDim];
ii_miDim++;
pa_ii_miDim_1 = PA[ii_miDim];
ii_miDim++;
pa_ii_miDim_2 = PA[ii_miDim];
ii_miDim++;
pa_ii_miDim_3 = PA[ii_miDim];
wg_ii_index = WG[ii_index];
wg_ii_minus = WG[ii_minus];
inverse = FK[ii + Degree] - FK[ii];
inverse = 1.0e0 / inverse;
lower = ii - Deg1;
if (lower < 0)
{
lower = 0;
}
upper = Deg2 + ii;
if (upper > num_poles)
{
upper = num_poles;
}
for (jj = lower; jj < upper; jj++)
{
jj_index = jj % NumPoles;
jj_index = jj_index << 2;
value = 0.0e0;
factor = (((PA[jj_index] - pa_ii_inDim_0) * wg_ii_index)
- ((PA[jj_index] - pa_ii_miDim_0) * wg_ii_minus));
if (factor < 0)
{
factor = -factor;
}
value += factor;
jj_index++;
factor = (((PA[jj_index] - pa_ii_inDim_1) * wg_ii_index)
- ((PA[jj_index] - pa_ii_miDim_1) * wg_ii_minus));
if (factor < 0)
{
factor = -factor;
}
value += factor;
jj_index++;
factor = (((PA[jj_index] - pa_ii_inDim_2) * wg_ii_index)
- ((PA[jj_index] - pa_ii_miDim_2) * wg_ii_minus));
if (factor < 0)
{
factor = -factor;
}
value += factor;
jj_index++;
factor = (((PA[jj_index] - pa_ii_inDim_3) * wg_ii_index)
- ((PA[jj_index] - pa_ii_miDim_3) * wg_ii_minus));
if (factor < 0)
{
factor = -factor;
}
value += factor;
value *= inverse;
if (max_derivative < value)
{
max_derivative = value;
}
}
}
max_derivative /= min_weights;
}
else
{
for (ii = 1; ii < num_poles; ii++)
{
ii_index = ii % NumPoles;
ii_index = ii_index << 2;
ii_minus = (ii - 1) % NumPoles;
ii_minus = ii_minus << 2;
inverse = FK[ii + Degree] - FK[ii];
inverse = 1.0e0 / inverse;
value = 0.0e0;
factor = PA[ii_index] - PA[ii_minus];
if (factor < 0)
{
factor = -factor;
}
value += factor;
ii_index++;
ii_minus++;
factor = PA[ii_index] - PA[ii_minus];
if (factor < 0)
{
factor = -factor;
}
value += factor;
ii_index++;
ii_minus++;
factor = PA[ii_index] - PA[ii_minus];
if (factor < 0)
{
factor = -factor;
}
value += factor;
ii_index++;
ii_minus++;
factor = PA[ii_index] - PA[ii_minus];
if (factor < 0)
{
factor = -factor;
}
value += factor;
value *= inverse;
if (max_derivative < value)
{
max_derivative = value;
}
}
}
break;
}
default: {
int kk;
if (Weights != nullptr)
{
const double* WG = &(*Weights)(Weights->Lower());
min_weights = WG[0];
for (ii = 1; ii < NumPoles; ii++)
{
W = WG[ii];
if (W < min_weights)
{
min_weights = W;
}
}
for (ii = 1; ii < num_poles; ii++)
{
ii_index = ii % NumPoles;
ii_inDim = ii_index * ArrayDimension;
ii_minus = (ii - 1) % NumPoles;
ii_miDim = ii_minus * ArrayDimension;
wg_ii_index = WG[ii_index];
wg_ii_minus = WG[ii_minus];
inverse = FK[ii + Degree] - FK[ii];
inverse = 1.0e0 / inverse;
lower = ii - Deg1;
if (lower < 0)
{
lower = 0;
}
upper = Deg2 + ii;
if (upper > num_poles)
{
upper = num_poles;
}
for (jj = lower; jj < upper; jj++)
{
jj_index = jj % NumPoles;
jj_index *= ArrayDimension;
value = 0.0e0;
for (kk = 0; kk < ArrayDimension; kk++)
{
factor = (((PA[jj_index + kk] - PA[ii_inDim + kk]) * wg_ii_index)
- ((PA[jj_index + kk] - PA[ii_miDim + kk]) * wg_ii_minus));
if (factor < 0)
{
factor = -factor;
}
value += factor;
}
value *= inverse;
if (max_derivative < value)
{
max_derivative = value;
}
}
}
max_derivative /= min_weights;
}
else
{
for (ii = 1; ii < num_poles; ii++)
{
ii_index = ii % NumPoles;
ii_index *= ArrayDimension;
ii_minus = (ii - 1) % NumPoles;
ii_minus *= ArrayDimension;
inverse = FK[ii + Degree] - FK[ii];
inverse = 1.0e0 / inverse;
value = 0.0e0;
for (kk = 0; kk < ArrayDimension; kk++)
{
factor = PA[ii_index + kk] - PA[ii_minus + kk];
if (factor < 0)
{
factor = -factor;
}
value += factor;
}
value *= inverse;
if (max_derivative < value)
{
max_derivative = value;
}
}
}
}
}
max_derivative *= Degree;
if (max_derivative > RealSmall())
{
UTolerance = Tolerance3D / max_derivative;
}
else
{
UTolerance = Tolerance3D / RealSmall();
}
}
//=================================================================================================
int BSplCLib::Intervals(const NCollection_Array1<double>& theKnots,
const NCollection_Array1<int>& theMults,
int theDegree,
bool isPeriodic,
int theContinuity,
double theFirst,
double theLast,
double theTolerance,
NCollection_Array1<double>* theIntervals)
{
// remove all knots with multiplicity less or equal than (degree - continuity) except first and
// last
int aFirstIndex = isPeriodic ? 1 : FirstUKnotIndex(theDegree, theMults);
int aLastIndex = isPeriodic ? theKnots.Length() : LastUKnotIndex(theDegree, theMults);
NCollection_Array1<double> aNewKnots(1, aLastIndex - aFirstIndex + 1);
int aNbNewKnots = 0;
for (int anIndex = aFirstIndex; anIndex <= aLastIndex; anIndex++)
{
if (theMults(anIndex) > (theDegree - theContinuity) || anIndex == aFirstIndex
|| anIndex == aLastIndex)
{
aNbNewKnots++;
aNewKnots(aNbNewKnots) = theKnots[anIndex];
}
}
aNewKnots.Resize(1, aNbNewKnots, true);
// the range boundaries
double aCurFirst = theFirst;
double aCurLast = theLast;
double aPeriod = 0.0;
int aFirstPeriod = 0;
int aLastPeriod = 0;
// move boundaries into period
if (isPeriodic)
{
double aLower = theKnots.First();
double anUpper = theKnots.Last();
aPeriod = anUpper - aLower;
while (aCurFirst < aLower)
{
aCurFirst += aPeriod;
aFirstPeriod--;
}
while (aCurLast < aLower)
{
aCurLast += aPeriod;
aLastPeriod--;
}
while (aCurFirst >= anUpper)
{
aCurFirst -= aPeriod;
aFirstPeriod += 1;
}
while (aCurLast >= anUpper)
{
aCurLast -= aPeriod;
aLastPeriod += 1;
}
}
// locate the left and nearest knot for boundaries
int anIndex1 = 0;
int anIndex2 = 0;
double aDummyDouble;
// we use version of LocateParameter that doesn't need multiplicities
LocateParameter(theDegree,
aNewKnots,
NCollection_Array1<int>(),
aCurFirst,
false,
1,
aNbNewKnots,
anIndex1,
aDummyDouble);
LocateParameter(theDegree,
aNewKnots,
NCollection_Array1<int>(),
aCurLast,
false,
1,
aNbNewKnots,
anIndex2,
aDummyDouble);
// the case when the beginning of the range coincides with the next knot
if (anIndex1 < aNbNewKnots && std::abs(aNewKnots[anIndex1 + 1] - aCurFirst) < theTolerance)
{
anIndex1 += 1;
}
// the case when the ending of the range coincides with the current knot
if (aNbNewKnots && std::abs(aNewKnots[anIndex2] - aCurLast) < theTolerance)
{
anIndex2 -= 1;
}
int aNbIntervals = anIndex2 - anIndex1 + 1 + (aLastPeriod - aFirstPeriod) * (aNbNewKnots - 1);
// fill the interval array
if (theIntervals)
{
theIntervals->Resize(1, aNbIntervals + 1, false);
if (isPeriodic && aLastPeriod != aFirstPeriod)
{
int anIndex = 1;
// part from the begging of range to the end of the first period
for (int i = anIndex1; i < aNewKnots.Length(); i++, anIndex++)
{
theIntervals->ChangeValue(anIndex) = aNewKnots[i] + aFirstPeriod * aPeriod;
}
// full periods
for (int aPeriodNum = aFirstPeriod + 1; aPeriodNum < aLastPeriod; aPeriodNum++)
{
for (int i = 1; i < aNewKnots.Length(); i++, anIndex++)
{
theIntervals->ChangeValue(anIndex) = aNewKnots[i] + aPeriodNum * aPeriod;
}
}
// part from the begging of the last period to the end of range
for (int i = 1; i <= anIndex2; i++, anIndex++)
{
theIntervals->ChangeValue(anIndex) = aNewKnots[i] + aLastPeriod * aPeriod;
}
}
else
{
int anIndex = 1;
for (int i = anIndex1; i <= anIndex2; i++, anIndex++)
{
theIntervals->ChangeValue(anIndex) = aNewKnots[i] + aFirstPeriod * aPeriod;
}
}
// update the first position (the begging of range doesn't coincide with the knot at anIndex1 in
// general)
theIntervals->ChangeValue(1) = theFirst;
// write the ending of the range (we didn't write it at all)
theIntervals->ChangeValue(aNbIntervals + 1) = theLast;
}
return aNbIntervals;
}
//=================================================================================================
// array of flat knots for bezier curve of maximum 25 degree
static const double 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, 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};
const double& BSplCLib::FlatBezierKnots(const int Degree)
{
Standard_OutOfRange_Raise_if(Degree < 1 || Degree > MaxDegree() || MaxDegree() != 25,
"Bezier curve degree greater than maximal supported");
return knots[25 - Degree];
}
//=================================================================================================
namespace
{
struct UnitWeightsArray
{
double Data[2049];
constexpr UnitWeightsArray()
: Data{}
{
for (int i = 0; i < 2049; ++i)
{
Data[i] = 1.0;
}
}
};
static constexpr UnitWeightsArray THE_UNIT_WEIGHTS{};
} // namespace
NCollection_Array1<double> BSplCLib::UnitWeights(const int theNbElems)
{
if (theNbElems <= MaxUnitWeightsSize())
{
// Non-owning view over global static array (zero allocation).
return NCollection_Array1<double>(THE_UNIT_WEIGHTS.Data[0], 1, theNbElems);
}
// Rare: allocate fresh array.
NCollection_Array1<double> aResult(1, theNbElems);
aResult.Init(1.0);
return aResult;
}
//=================================================================================================
const double* BSplCLib::UnitWeightsData()
{
return THE_UNIT_WEIGHTS.Data;
}