Add more implementors for Interpolate.

This commit is contained in:
Dimitri Sabadie 2021-07-11 17:44:05 +02:00
parent 322d271499
commit b3836975c3
No known key found for this signature in database
GPG Key ID: B313786A66884DCD
4 changed files with 84 additions and 2 deletions

View File

@ -2,6 +2,7 @@
<!-- vim-markdown-toc GFM --> <!-- vim-markdown-toc GFM -->
* [4.0.2](#402)
* [4.0.1](#401) * [4.0.1](#401)
* [4.0](#40) * [4.0](#40)
* [Major changes](#major-changes) * [Major changes](#major-changes)
@ -40,6 +41,12 @@
<!-- vim-markdown-toc --> <!-- vim-markdown-toc -->
# 4.0.2
> Jul 11, 2021
- Add more implementors for `Interpolate`.
# 4.0.1 # 4.0.1
> Jul 11, 2021 > Jul 11, 2021

View File

@ -1,6 +1,6 @@
[package] [package]
name = "splines" name = "splines"
version = "4.0.1" version = "4.0.2"
license = "BSD-3-Clause" license = "BSD-3-Clause"
authors = ["Dimitri Sabadie <dimitri.sabadie@gmail.com>"] authors = ["Dimitri Sabadie <dimitri.sabadie@gmail.com>"]
description = "Spline interpolation made easy" description = "Spline interpolation made easy"

View File

@ -1,4 +1,4 @@
use crate::impl_Interpolate; use crate::{impl_Interpolate, impl_InterpolateT!};
use cgmath::{Quaternion, Vector1, Vector2, Vector3, Vector4}; use cgmath::{Quaternion, Vector1, Vector2, Vector3, Vector4};
@ -13,3 +13,10 @@ impl_Interpolate!(f64, Vector2<f64>, std::f64::consts::PI);
impl_Interpolate!(f64, Vector3<f64>, std::f64::consts::PI); impl_Interpolate!(f64, Vector3<f64>, std::f64::consts::PI);
impl_Interpolate!(f64, Vector4<f64>, std::f64::consts::PI); impl_Interpolate!(f64, Vector4<f64>, std::f64::consts::PI);
impl_Interpolate!(f64, Quaternion<f64>, std::f64::consts::PI); impl_Interpolate!(f64, Quaternion<f64>, std::f64::consts::PI);
impl_InterpolateT!(f64, Vector1<f32>, std::f32::consts::PI);
impl_InterpolateT!(f64, Vector2<f32>, std::f32::consts::PI);
impl_InterpolateT!(f64, Vector3<f32>, std::f32::consts::PI);
impl_InterpolateT!(f64, Vector4<f32>, std::f32::consts::PI);
impl_InterpolateT!(f64, Quaternion<f32>, std::f32::consts::PI);

View File

@ -163,5 +163,73 @@ macro_rules! impl_Interpolate {
}; };
} }
#[macro_export]
macro_rules! impl_InterpolateT {
($t:ty, $v:ty, $pi:expr) => {
impl $crate::interpolate::Interpolate<$t> for $v {
fn step(t: $t, threshold: $t, a: Self, b: Self) -> Self {
if t < threshold {
a
} else {
b
}
}
fn cosine(t: $t, a: Self, b: Self) -> Self {
let cos_nt = (1. - (t * $pi).cos()) * 0.5;
<Self as $crate::interpolate::Interpolate<$t>>::lerp(cos_nt, a, b)
}
fn lerp(t: $t, a: Self, b: Self) -> Self {
let t = Self::from(t);
a * (1. - t) + b * t
}
fn cubic_hermite(t: $t, x: ($t, Self), a: ($t, Self), b: ($t, Self), y: ($t, Self)) -> Self {
// sampler stuff
let t = Self::from(t);
let two_t = t * 2.;
let three_t = t * 3.;
let t2 = t * t;
let t3 = t2 * t;
let two_t3 = t3 * two_t;
let three_t2 = t2 * three_t;
// tangents
let m0 = (b.1 - x.1) / (Self::from(b.0 - x.0));
let m1 = (y.1 - a.1) / (Self::from(y.0 - a.0));
a.1 * (two_t3 - three_t2 + 1.)
+ m0 * (t3 - t2 * two_t + t)
+ b.1 * (three_t2 - two_t3)
+ m1 * (t3 - t2)
}
fn quadratic_bezier(t: $t, a: Self, u: Self, b: Self) -> Self {
let t = Self::from(t);
let one_t = 1. - t;
let one_t2 = one_t * one_t;
u + (a - u) * one_t2 + (b - u) * t * t
}
fn cubic_bezier(t: $t, a: Self, u: Self, v: Self, b: Self) -> Self {
let t = Self::from(t);
let one_t = 1. - t;
let one_t2 = one_t * one_t;
let one_t3 = one_t2 * one_t;
let t2 = t * t;
a * one_t3 + (u * one_t2 * t + v * one_t * t2) * 3. + b * t2 * t
}
fn cubic_bezier_mirrored(t: $t, a: Self, u: Self, v: Self, b: Self) -> Self {
<Self as $crate::interpolate::Interpolate<$t>>::cubic_bezier(t, a, u, b + b - v, b)
}
}
};
}
impl_Interpolate!(f32, f32, std::f32::consts::PI); impl_Interpolate!(f32, f32, std::f32::consts::PI);
impl_Interpolate!(f64, f64, std::f64::consts::PI); impl_Interpolate!(f64, f64, std::f64::consts::PI);
impl_InterpolateT!(f32, f64, std::f32::consts::PI);