• Stars
    star
    287
  • Rank 144,232 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 9 years ago
  • Updated almost 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

B-spline interpolation

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;

// As we don't provide a knot vector, one will be generated 
// internally and have the following form :
//
// var knots = [0, 1, 2, 3, 4, 5, 6];
//
// Knot vectors must have `number of points + degree + 1` knots.
// Here we have 4 points and the degree is 2, so the knot vector 
// length will be 7.
//
// This knot vector is called "uniform" as the knots are all spaced uniformly,
// ie. the knot spans are all equal (here 1).

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;

// B-splines with clamped knot vectors pass through 
// the two end control points.
//
// A clamped knot vector must have `degree + 1` equal knots 
// at both its beginning and end.

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');

// Closed curves are built by repeating the `degree + 1` first 
// control points at the end of the curve

var points = [
  [-1.0,  0.0],
  [-0.5,  0.5],
  [ 0.5, -0.5],
  [ 1.0,  0.0],

  // repeat the first `degree + 1` points
  [-1.0,  0.0],
  [-0.5,  0.5],
  [ 0.5, -0.5]
];

var degree = 2;
// The number of control points without the last repeated
// points
var originalNumPoints = points.length - (degree + 1);

// and using an unclamped knot vector

var knots = [
  0, 1, 2, 3, 4, 5, 6, 7, 8, 9
];

/*
Disclaimer: If you are using a unclamped knot vector
with closed curves, you may want to remap the t value
to properly loop the curve.

To do that, remap t value from [0.0, 1.0] to
[0.0, 1.0 - 1.0 / (n + 1)] where 'n' is the number of
the original control points used (discard the last repeated points).

In this case, the number of points is 4 (discarded the last 3 points)
*/
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]  // P0
]

// Here the curve is called non-uniform as the knots 
// are not equally spaced

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;

// and rational as its control points have varying weights

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] range
  • degree 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.

More Repositories

1

node-castv2

An implementation of the Chromecast CASTV2 protocol
JavaScript
758
star
2

node-castv2-client

A Chromecast client based on the new (CASTV2) protocol
JavaScript
638
star
3

node-upnp-mediarenderer-client

An UPnP/DLNA MediaRenderer client
JavaScript
121
star
4

node-google-search-scraper

Google search scraper with captcha solving support
JavaScript
82
star
5

duckduckgo

Simple duckduckgo results scraping
Python
65
star
6

pop-buffer

Progressive encoding for 3D meshes
JavaScript
59
star
7

styx

Simple, high-performance event streaming broker
Go
52
star
8

cubic-hermite-spline

Cubic Hermite spline interpolation
JavaScript
44
star
9

parse-cube-lut

Cube LUT (IRIDAS/Adobe) parser
JavaScript
34
star
10

rbf

Radial Basis Function (RBF) interpolation
JavaScript
32
star
11

apply-cube-lut

Apply a Cube (IRIDAS/Adobe) LUT to an image
JavaScript
31
star
12

node-upnp-device-client

A simple and versatile UPnP device client
JavaScript
31
star
13

node-rtmpdump

A streamable wrapper around the rtmpdump CLI
JavaScript
19
star
14

parse-wavefront-obj

Wavefront OBJ parser
JavaScript
18
star
15

t411

T411 API client
JavaScript
17
star
16

quantize-vertices

Quantizes vertices to any bit precision
JavaScript
14
star
17

bezier-curve

Bezier curve interpolation
JavaScript
13
star
18

merge-vertices

Merges mesh vertices having identical coordinates
JavaScript
10
star
19

parse-stl

STL (ASCII and binary) file parser
JavaScript
9
star
20

parse-3ds

Parses 3D Studio .3DS files
JavaScript
7
star
21

serialize-stl

STL (ASCII and binary) file serialization
JavaScript
7
star
22

node-sse-emitter

Server-Sent Events as simple as they can get
JavaScript
6
star
23

remove-orphan-vertices

Removes orphan vertices in a simplicial complex
JavaScript
6
star
24

serialize-wavefront-obj

Wavefront OBJ serializer
JavaScript
5
star
25

merge-meshes

Merges multiple meshes into one
JavaScript
5
star
26

vertices-bounding-box

Computes the bounding box of a set of vertices
JavaScript
4
star
27

rescale-vertices

Rescales vertices to the dimensions of a target bounding box
JavaScript
4
star
28

node-host-filtering-proxy

A straightforward host filtering HTTP proxy
JavaScript
4
star
29

ndarray-from-image

Extracts an image RGBA pixels as a ndarray
JavaScript
3
star
30

remove-degenerate-cells

Removes degenerate cells in a simplicial complex
JavaScript
3
star
31

serialize-stl-binary

STL binary serialization
JavaScript
3
star
32

node-generate-source-map

Generates an identity source-map from a javascript file
JavaScript
3
star
33

canvas-from-ndarray

Updates a canvas RGBA pixels from an ndarray
JavaScript
2
star
34

talk-react-bdxio

React.js et l'avenir de l'interface utilisateur HTML5
CSS
2
star
35

parse-stl-binary

STL binary parser
JavaScript
2
star
36

talk-react-bordeauxjs

Keep calm and React !
2
star
37

require

Experiments with module loading in the browser
JavaScript
1
star
38

react-starter

JavaScript
1
star
39

serialize-stl-ascii

STL ASCII serialization
JavaScript
1
star
40

talk-ansible-best

Ansible
JavaScript
1
star
41

eventemitter

Lightweight isomorphic event emitter
JavaScript
1
star
42

set-canvas-pixels

Sets a canvas pixels from an array of RGBA pixel values
JavaScript
1
star
43

get-canvas-pixels

Get an array of RGBA pixel values from a canvas
JavaScript
1
star
44

node-url-stream

Transforms a stream of URLs into a stream of their body content
JavaScript
1
star
45

router

Lightweight isomorphic router
JavaScript
1
star
46

ndarray-from-canvas

Extracts a canvas RGBA pixels as a ndarray
JavaScript
1
star
47

parse-stl-ascii

STL ASCII parser
JavaScript
1
star