#splines #graphics #inversion #cubic #curves

no-std uniform-cubic-splines

Simple uniform cubic spline evaluation and inversion

7 releases

0.1.7 Mar 19, 2023
0.1.6 Feb 23, 2023
0.1.5 Nov 30, 2021
0.1.4 Feb 11, 2021
0.1.1 Oct 30, 2020

#221 in Math

Download history 14/week @ 2023-02-09 31/week @ 2023-02-16 28/week @ 2023-02-23 14/week @ 2023-03-02 8/week @ 2023-03-09 38/week @ 2023-03-16 14/week @ 2023-03-23 8/week @ 2023-03-30 32/week @ 2023-04-06 9/week @ 2023-04-13 15/week @ 2023-04-20 1/week @ 2023-04-27 24/week @ 2023-05-04 7/week @ 2023-05-11 11/week @ 2023-05-18 19/week @ 2023-05-25

61 downloads per month

MIT OR Apache-2.0 OR Zlib

26KB
318 lines

uniform-cubic-splines

Uniform cubic spline interpolation & inversion.

Documentation Crate

This crate supports the following types of splines:

Curve widget with 1D Catmull-Rom spline

Curve widget with a 1D Catmull-Rom spline with non-uniform knot spacing and knot multiplicity using this crate for interpolation (drawn using tiny-skia).

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.

[dependencies]
uniform-cubic-splines = { version = "0.1" }

Example

Using a combination of spline() and spline_inverse() it is possible to compute a full spline-with-nonuniform-abscissæ:

use uniform_cubic_splines::{
    basis::CatmullRom, spline_inverse, spline,
};

// 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, None, None).unwrap();
let y = spline::<CatmullRom, _, _>(v, &knots);

assert!(y - 4.2 < 1e-6);

Features

  • monotonic_check -- The spline_inverse() code will check if the knot vector is monotonic. This check can be made a lot faster if the unstable feature is enabled.
  • unstable -- The monotonic_check feature will be faster but requite a nightly toolchain.
  • use_std -- The monotonic_check accelleration will be detected at runtime.

Background

The code is a Rust port of the implementation found in the Open Shading Language C++ source.

If you come from a background of computer graphics/shading languages used in offline rendering this crate should feel like home.

Dependencies

~180KB