b-spline
B-spline interpolation
B-spline interpolation of control points of any dimensionality using de Boor's algorithm.
The interpolator can take an optional weight vector, making the resulting curve a Non-Uniform Rational B-Spline (NURBS) curve if you wish so.
The knot vector is optional too, and when not provided an unclamped uniform knot vector will be generated internally.
Install
$ npm install b-spline
Examples
Unclamped knot vector
var bspline = require('b-spline');
var points = [
[-1.0, 0.0],
[-0.5, 0.5],
[ 0.5, -0.5],
[ 1.0, 0.0]
];
var degree = 2;
for(var t=0; t<1; t+=0.01) {
var point = bspline(t, degree, points);
}
Clamped knot vector
var bspline = require('b-spline');
var points = [
[-1.0, 0.0],
[-0.5, 0.5],
[ 0.5, -0.5],
[ 1.0, 0.0]
];
var degree = 2;
var knots = [
0, 0, 0, 1, 2, 2, 2
];
for(var t=0; t<1; t+=0.01) {
var point = bspline(t, degree, points, knots);
}
Closed curves
var bspline = require('b-spline');
var points = [
[-1.0, 0.0],
[-0.5, 0.5],
[ 0.5, -0.5],
[ 1.0, 0.0],
[-1.0, 0.0],
[-0.5, 0.5],
[ 0.5, -0.5]
];
var degree = 2;
var originalNumPoints = points.length - (degree + 1);
var knots = [
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
];
var maxT = 1.0 - 1.0 / (originalNumPoints + 1);
for(var t=0; t<1; t+=0.01) {
var point = bspline(t * maxT, degree, points, knots);
}
Non-uniform rational
var bspline = require('b-spline');
var points = [
[ 0.0, -0.5],
[-0.5, -0.5],
[-0.5, 0.0],
[-0.5, 0.5],
[ 0.0, 0.5],
[ 0.5, 0.5],
[ 0.5, 0.0],
[ 0.5, -0.5],
[ 0.0, -0.5]
]
var knots = [
0, 0, 0, 1/4, 1/4, 1/2, 1/2, 3/4, 3/4, 1, 1, 1
];
var w = Math.pow(2, 0.5) / 2;
var weights = [
1, w, 1, w, 1, w, 1, w, 1
]
var degree = 2;
for(var t=0; t<1; t+=0.01) {
var point = bspline(t, degree, points, knots, weights);
}
Usage
bspline(t, degree, points[, knots, weights])
t
position along the curve in the [0, 1] rangedegree
degree of the curve. Must be less than or equal to the number of control points minus 1. 1 is linear, 2 is quadratic, 3 is cubic, and so on.points
control points that will be interpolated. Can be vectors of any dimensionality ([x, y]
, [x, y, z]
, ...)knots
optional knot vector. Allow to modulate the control points interpolation spans on t
. Must be a non-decreasing sequence of number of points + degree + 1
length values.weights
optional control points weights. Must be the same length as the control point array.