#spline #cubic #graphics #inversion #spline-curve #cubic-spline

no-std uniform-cubic-splines

Simple uniform cubic spline evaluation and inversion

13 releases

0.3.1 Apr 11, 2025
0.2.0 Mar 12, 2025
0.1.10 Sep 15, 2024
0.1.8 Oct 15, 2023
0.1.3 Nov 14, 2020

#210 in Math

Download history 8/week @ 2025-01-01 4/week @ 2025-01-08 30/week @ 2025-01-22 39/week @ 2025-02-05 28/week @ 2025-02-12 27/week @ 2025-02-19 11/week @ 2025-02-26 39/week @ 2025-03-05 149/week @ 2025-03-12 9/week @ 2025-03-19 24/week @ 2025-03-26 104/week @ 2025-04-02 160/week @ 2025-04-09 18/week @ 2025-04-16

308 downloads per month
Used in 2 crates (via sl-map-apis)

MIT OR Apache-2.0 OR Zlib

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

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

Features

  • monotonic_check -- The spline_inverse()/spline_inverse_with() code will check if the knot vector is monotonic (on by default).

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

~160KB