Expand description
Uniform cubic spline interpolation & inversion.
This crate supports the following types of splines:
- B-spline
- Bezier
- Catmull-Rom
- Hermite
- Linear
- Power
The crate uses generics to allow interpolation of any type for which certain traits are defined.
I.e. you can use this crate to interpolate splines in 1D, 2D, 3D, etc.
§Version 0.4 Changes
- Trait-based architecture: New
Splinetrait enables specialized implementations. - Error handling: Functions return
Result<T, SplineError>instead of panicking. - Clone-based design: Uses
Cloneinstead ofCopyto support more types likenalgebra::Vector3. - Extensibility: External crates can implement
Splinefor their own types.
§Example
Using a combination of spline_inverse() and spline() it is possible
to compute a full spline-with-non-uniform-abscissæ:
use uniform_cubic_splines::prelude::*;
// We want to evaluate the spline at knot value 0.3.
let x = 0.3;
// The first and last points are never interpolated.
let knot_spacing = [0.0, 0.0, 0.1, 0.3, 1.0, 1.0];
let knots = [0.0, 0.0, 1.3, 4.2, 3.2, 3.2];
let v = spline_inverse::<CatmullRom, _>(x, &knot_spacing).unwrap();
let y = spline::<CatmullRom, _, _>(v, &knots).unwrap();
assert!(y - 4.2 < 1e-6);§Background
The code was originally a Rust port of the resp. implementations found in the Open Shading Language C++ source. However, it has since diverged significantly with extensive optimizations:
- Binary search optimization:
spline_inverseuses binary search for splines with >12 segments, achieving O(log n) complexity instead of O(n) - Improved range detection: Evaluates actual spline values at segment boundaries rather than just checking control points
- Adaptive root-finding: Illinois-modified Regula Falsi with automatic bisection fallback
If you come from a background of computer graphics/shading languages used in offline rendering this crate should feel familiar while providing better performance.
§no-std
The crate does not depend on the standard library (i.e. is marked no_std).
§Cargo Features
monotonic_check(enabled by default) — Thespline_inverse()/spline_inverse_with()code will check if the knot vector is monotonic. Disabling improves performance by ~5-10% but requires monotonic input.nightly_f16— Enable f16 support on nightly Rust. Requires patched num-traits with nightly_f16 feature.nightly_f128— Enable f128 support on nightly Rust. Requires patched num-traits with nightly_f128 feature.
Re-exports§
pub use basis::*;
Modules§
Structs§
- Spline
Inverse Options - Options for
spline_inverse_with()function.
Enums§
- Spline
Error - Errors that can occur during spline operations.
Traits§
- Spline
- Trait for types that can be interpolated using splines.
Functions§
- is_
len_ ok Deprecated - Returns
trueif aknotsslice you want to feed intospline()has the correct length for the choosenBasis. - spline
- As
xvaries from0to1, this function returns the value of a cubic interpolation of uniformly spacedknots. - spline_
inverse - Computes the inverse of the
spline()function. - spline_
inverse_ segment - Computes the inverse of a single spline segment.
- spline_
inverse_ segment_ with - Computes the inverse of a single spline segment with custom options.
- spline_
inverse_ with - Computes the inverse of the
spline()function with control over iterations & precision via resp.SplineInverseOptions. - spline_
segment - Evaluates a spline for a single segment defined by 4 control points.
This is the performance-critical inner loop of the
splinefunction.
Type Aliases§
- Spline
Result - Result type for spline operations.