Rust library for vectorized, N-dimensional B-spline curves and their derivatives based on nalgebra.
use bsplines::{Curve, DataPoints};
use nalgebra::dmatrix;
// Thirteen noisy 3D data points, one column per point.
// They run along -y, then +z, then -x.
let data = DataPoints::new(dmatrix![
1.9, 2.1, 2.1, 1.9, 2.1, 2.2, 2.2, 1.8, 1.9, 1.0,-0.2,-1.1,-2.0; // x
1.9, 0.8,-0.2,-0.8,-1.9,-2.0,-1.8,-1.9,-2.0,-2.0,-2.1,-1.9,-2.1; // y
-2.0,-1.9,-2.1,-1.9,-2.1,-0.9, 0.1, 0.9, 2.1, 2.2, 2.2, 1.8, 1.9; // z
]);
// Interpolate the data with a cubic curve and evaluate it.
let curve = Curve::interpolate(&data, 3)?;
let point = curve.evaluate(0.5)?;
let velocity = curve.evaluate_derivative(0.5, 1)?;
// Or approximate it with a penalized least-squares fit.
let fitted = Curve::fit(&data, 3).loose_ends().penalized(0.5, 2).build()?;The fitted curve (red) follows the three straight legs of the data (black) and rounds only the two corners. The plot also shows its control polygon.
Curves can also be built directly from control points (Curve::with_uniform_knots,
Curve::new) and manipulated afterwards: knot insertion, splitting, merging with
derivative continuity, and reversal. See the documentation
for the full API.
- Use iterators and simplify loops
- Add benchmarks and improve performance