Crate cubic_spline

Crate cubic_spline 

Source
Expand description

Interpolation method for computation of cubic spline points within the range of a discrete set of known points.

Demo

§Example

use cubic_spline::{Points, Point, SplineOpts, TryFrom};

let source = vec![(10.0, 200.0), (256.0, 390.0), (512.0, 10.0), (778.0, 200.0)];

let opts = SplineOpts::new()
  .tension(0.5);

let mut points = Points::try_from(&source).expect("expect valid points but");
let result = points.calc_spline(&opts).expect("cant construct spline points");

assert_eq!(result.get_ref().len(), 49);

let inner_vec: &mut Vec<Point> = points.get_mut();
inner_vec.push(Point::new(7.7, 1.3));
inner_vec[1].x += 0.79;
inner_vec.last_mut().iter_mut().for_each(|mut p| {p.tension = Some(0.7);});

points.invert_vertically(400.0);

assert_eq!(points.get_ref()[1].y, 10.0);

let calculated_points = points
  .calc_spline(&opts.num_of_segments(33))
  .unwrap();

assert_eq!(calculated_points.into_inner().len(), 133);

For information on how a curve can be constructed and which points to accept, see the appropriate structures.

§Custom points

If you already have some points you can implement From trait for Point struct and pass your points directly.

§Example

use cubic_spline::{SplineOpts, Point, Points};

#[derive(Default)]
struct MyPoint {
  vertical: u8,
  horizontal: u8,
  color: String,
}

impl<'a> From<&'a MyPoint> for Point {
  fn from(p: &'a MyPoint) -> Self {
    Point::new(p.horizontal as f64, p.vertical as f64)
  }
}

let my_points: Vec<MyPoint> = vec![MyPoint::default(),MyPoint::default()];
let spline = Points::from(&my_points)
  .calc_spline(&SplineOpts::default())
  .unwrap();

assert_eq!(spline.get_ref().len(), 17);

Structs§

Point
The point in 2d coordinate system.
Points
Wrapper for your source points. Prepares and validates points before calculating spline. Create it with try_from/try_into. Or if you are very confident in the validity of your points use usual From/Into traits, which will return an empty Vec on error.
SplineOpts
A list of options indicating how the spline should be calculated

Enums§

Error
This type represents all possible errors that can occur when constructing new spline points.

Constants§

DEFAULT_APPROX_EQ_PRECISION
Default precision used for point comparison in approx_eq method.
DEFAULT_SEGMENTS
Default number of segments. Will be used if not specified in SplineOpts.
DEFAULT_TENSION
Default tension of curve between passed points. Will be used if not specified in SplineOpts.

Traits§

TryFrom
The same as TryFrom from std/core, except that it allows generic implementations. Details are in this issue
TryInto
The same as TryInto from std/core, except that it allows generic implementations. Details are in this issue

Functions§

calc_spline
The main function that does all the work.

Type Aliases§

Result
Alias for a Result with the error type cubic_spline::Error.