53 #define ACG_BSPLINEBASIS_C 57 #include "BSplineBasis.hh" 69 template<
typename Scalar>
73 const std::vector<Scalar>& _knots)
78 int hi = _knots.size() - 1 - _degree;
80 Scalar upperBound = _knots[hi];
83 return Vec2i(hi-1-_degree, hi - 1);
85 int mid = (lo + hi) >> 1;
87 while (_t < _knots[mid] || _t >= _knots[mid + 1])
97 return Vec2i(mid - _degree, mid);
117 template<
typename Scalar>
122 const std::vector<Scalar>& _knots)
129 int p = _span[1] - _span[0];
137 static std::vector<Scalar> left(p+1);
138 static std::vector<Scalar> right(p+1);
140 if (left.size() < size_t(p+1))
147 for (
int j = 1; j <= p; ++j)
149 left[j] = _t - _knots[i + 1 - j];
150 right[j] = _knots[i + j] - _t;
154 for (
int r = 0; r < j; ++r)
156 Scalar tmp = _N[r] / (right[r + 1] + left[j - r]);
157 _N[r] = saved + right[r + 1] * tmp;
158 saved = left[j - r] * tmp;
165 template<
typename Scalar>
171 const std::vector<Scalar>& _knots,
172 std::vector<Scalar>* _functionVals )
176 int p = _span[1] - _span[0];
182 static std::vector< std::vector<Scalar> > ndu(p1);
183 static std::vector<Scalar> left(p1);
184 static std::vector<Scalar> right(p1);
185 static std::vector<Scalar> a(2*p1);
187 if (ndu[0].size() <
size_t(p1))
190 for (
int i = 0; i < p1; ++i)
204 for (
int j = 1; j <= p; ++j)
206 left[j]= _t - _knots[i + 1 - j];
207 right[j]= _knots[i + j] - _t;
210 for (
int r = 0; r < j; ++r)
213 ndu[j][r] = right[r+1] + left[j-r];
214 Scalar tmp = ndu[r][j-1] / ndu[j][r];
216 ndu[r][j] = saved + right[r+1] * tmp;
217 saved = left[j-r] * tmp;
225 for (
int j = 0; j <= p; ++j)
226 (*_functionVals)[j] = ndu[j][p];
233 for (
int r = 0; r <= p; ++r)
238 for (
int k = 1; k <= _der; ++k)
241 int rk = r - k, pk = p - k;
245 a[s2 + 0] = a[s1 + 0] / ndu[pk+1][rk];
246 d = a[s2] * ndu[rk][pk];
249 int j1 = (rk >= -1) ? 1 : -rk;
250 int j2 = (r - 1 <= pk) ? k-1 : p-r;
252 for (
int j = j1; j <= j2; ++j)
254 a[s2 + j] = (a[s1 + j] - a[s1 + j-1]) / ndu[pk+1][rk+j];
255 d += a[s2 + j] * ndu[rk+j][pk];
260 a[s2 + k] = -a[s1 + k-1] / ndu[pk+1][r];
261 d += a[s2 + k] * ndu[r][pk];
275 for (
int k = 1; k <= _der; ++k)
277 Scalar rf = Scalar(r);
278 for (
int j = 0; j <= p; ++j)
287 template<
typename Scalar>
292 const std::vector<Scalar>& _knots)
294 int m = _knots.size() - 1;
297 if ((_i == 0 && _t == _knots[0]) || (_i == m - _degree - 1 && _t == _knots[m]))
301 if (_t >= _knots[_i] && _t < _knots[_i + 1])
307 double Nin1 = basisFunction(_i, _degree - 1, _t, _knots);
308 double Nin2 = basisFunction(_i + 1, _degree - 1, _t, _knots);
312 if ((_knots[_i + _degree] - _knots[_i]) != 0)
313 fac1 = (_t - _knots[_i]) / (_knots[_i + _degree] - _knots[_i]);
317 if ((_knots[_i + 1 + _degree] - _knots[_i + 1]) != 0)
318 fac2 = (_knots[_i + 1 + _degree] - _t) / (_knots[_i + 1 + _degree] - _knots[_i + 1]);
322 return (fac1*Nin1 + fac2*Nin2);
326 template<
typename Scalar>
332 const std::vector<Scalar>& _knots)
334 assert(_degree >= 0);
339 return basisFunction(_i, _degree, _t, _knots);
341 Scalar Nin1 = derivativeBasisFunction(_i, _degree - 1, _t, _der - 1, _knots);
342 Scalar Nin2 = derivativeBasisFunction(_i + 1, _degree - 1, _t, _der - 1, _knots);
345 if (std::abs(_knots[_i + _degree] - _knots[_i]) > 1e-6)
346 fac1 = Scalar(_degree) / (_knots[_i + _degree] - _knots[_i]);
349 if (std::abs(_knots[_i + _degree + 1] - _knots[_i + 1]) > 1e-6)
350 fac2 = Scalar(_degree) / (_knots[_i + _degree + 1] - _knots[_i + 1]);
352 return (fac1*Nin1 - fac2*Nin2);
Scalar bsplineBasisFunction(int _i, int _degree, Scalar _t, const std::vector< Scalar > &_knots)
Evaluate a single basis function.
VectorT< signed int, 2 > Vec2i
void bsplineBasisDerivatives(std::vector< Scalar > &_ders, const Vec2i &_span, Scalar _t, int _der, const std::vector< Scalar > &_knots, std::vector< Scalar > *_functionVals)
Compute derivatives of basis functions in a span.
Namespace providing different geometric functions concerning angles.
Vec2i bsplineSpan(Scalar _t, int _degree, const std::vector< Scalar > &_knots)
Find the span of a parameter value.
void bsplineBasisFunctions(std::vector< Scalar > &_N, const Vec2i &_span, Scalar _t, const std::vector< Scalar > &_knots)
Evaluate basis functions in a span.
Scalar bsplineBasisDerivative(int _i, int _degree, Scalar _t, int _der, const std::vector< Scalar > &_knots)
Compute derivative of a single basis function.