Skip to content

Instantly share code, notes, and snippets.

@mourner
Created August 13, 2018 23:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mourner/7e12316ae3cf1f82560b224254c911cb to your computer and use it in GitHub Desktop.
Save mourner/7e12316ae3cf1f82560b224254c911cb to your computer and use it in GitHub Desktop.
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