Skip to content

Instantly share code, notes, and snippets.

@mourner mourner/bspline.js
Created Aug 13, 2018

Embed
What would you like to do?
export default class BSpline {
constructor(degree = 3, dimensions = 2) {
this.degree = degree;
this.dim = dimensions;
this.temp = new Float64Array(dimensions * (degree + 1));
this.current = this.temp.subarray(dimensions * degree);
}
approximate(coords, knots, step) {
const {degree, dim, temp} = this;
const min = knots[degree];
const max = knots[knots.length - degree - 1];
const n = Math.ceil((max - min) / step) + 1;
const result = new Float64Array(n * dim);
for (let m = 0, t = min, k = degree; m < n * dim; m += dim) {
while (t > knots[k + 1]) k++;
for (let i = 0; i < temp.length; i++) {
temp[i] = coords[dim * (k - degree) + i];
}
for (let i = 0; i < degree; i++) {
for (let j = degree; j > i; j--) {
const t0 = knots[j + k - degree];
const t1 = knots[j + k - i];
const a = (t - t0) / (t1 - t0);
for (let q = j * dim; q < j * dim + dim; q++) {
temp[q] = a * temp[q] + (1 - a) * temp[q - dim];
}
}
}
for (let q = 0; q < dim; q++) result[m + q] = this.current[q];
t += step;
if (t > max) t = max;
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.