Update and fix implementors for the new API.
This commit is contained in:
parent
0ccc3c0956
commit
3e85a1f026
@ -23,7 +23,7 @@ maintenance = { status = "actively-developed" }
|
|||||||
default = ["std"]
|
default = ["std"]
|
||||||
impl-cgmath = ["cgmath"]
|
impl-cgmath = ["cgmath"]
|
||||||
impl-glam = ["glam"]
|
impl-glam = ["glam"]
|
||||||
impl-nalgebra = ["nalgebra", "num-traits", "simba"]
|
impl-nalgebra = ["nalgebra"]
|
||||||
serialization = ["serde", "serde_derive"]
|
serialization = ["serde", "serde_derive"]
|
||||||
std = []
|
std = []
|
||||||
|
|
||||||
@ -31,10 +31,8 @@ std = []
|
|||||||
cgmath = { version = ">=0.17, <0.19", optional = true }
|
cgmath = { version = ">=0.17, <0.19", optional = true }
|
||||||
glam = { version = ">=0.10, <0.12", optional = true }
|
glam = { version = ">=0.10, <0.12", optional = true }
|
||||||
nalgebra = { version = ">=0.21, <0.25", optional = true }
|
nalgebra = { version = ">=0.21, <0.25", optional = true }
|
||||||
num-traits = { version = "0.2", optional = true }
|
|
||||||
serde = { version = "1", optional = true }
|
serde = { version = "1", optional = true }
|
||||||
serde_derive = { version = "1", optional = true }
|
serde_derive = { version = "1", optional = true }
|
||||||
simba = { version = ">=0.1.2, <0.5", optional = true }
|
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
float-cmp = ">=0.6, < 0.9"
|
float-cmp = ">=0.6, < 0.9"
|
||||||
|
101
src/cgmath.rs
101
src/cgmath.rs
@ -1,92 +1,15 @@
|
|||||||
use cgmath::{
|
use crate::impl_Interpolate;
|
||||||
BaseFloat, BaseNum, InnerSpace, Quaternion, Vector1, Vector2, Vector3, Vector4, VectorSpace,
|
|
||||||
};
|
|
||||||
|
|
||||||
use crate::interpolate::{
|
use cgmath::{Quaternion, Vector1, Vector2, Vector3, Vector4};
|
||||||
cubic_bezier_def, cubic_hermite_def, quadratic_bezier_def, Additive, Interpolate, Linear, One,
|
|
||||||
};
|
|
||||||
|
|
||||||
macro_rules! impl_interpolate_vec {
|
impl_Interpolate!(f32, Vector1<f32>, std::f32::consts::PI);
|
||||||
($($t:tt)*) => {
|
impl_Interpolate!(f32, Vector2<f32>, std::f32::consts::PI);
|
||||||
impl<T> Linear<T> for $($t)*<T> where T: BaseNum {
|
impl_Interpolate!(f32, Vector3<f32>, std::f32::consts::PI);
|
||||||
#[inline(always)]
|
impl_Interpolate!(f32, Vector4<f32>, std::f32::consts::PI);
|
||||||
fn outer_mul(self, t: T) -> Self {
|
impl_Interpolate!(f32, Quaternion<f32>, std::f32::consts::PI);
|
||||||
self * t
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline(always)]
|
impl_Interpolate!(f64, Vector1<f64>, std::f64::consts::PI);
|
||||||
fn outer_div(self, t: T) -> Self {
|
impl_Interpolate!(f64, Vector2<f64>, std::f64::consts::PI);
|
||||||
self / t
|
impl_Interpolate!(f64, Vector3<f64>, std::f64::consts::PI);
|
||||||
}
|
impl_Interpolate!(f64, Vector4<f64>, std::f64::consts::PI);
|
||||||
}
|
impl_Interpolate!(f64, Quaternion<f64>, std::f64::consts::PI);
|
||||||
|
|
||||||
impl<T> Interpolate<T> for $($t)*<T>
|
|
||||||
where Self: InnerSpace<Scalar = T>, T: Additive + BaseFloat + One {
|
|
||||||
#[inline(always)]
|
|
||||||
fn lerp(a: Self, b: Self, t: T) -> Self {
|
|
||||||
a.lerp(b, t)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline(always)]
|
|
||||||
fn cubic_hermite(x: (Self, T), a: (Self, T), b: (Self, T), y: (Self, T), t: T) -> Self {
|
|
||||||
cubic_hermite_def(x, a, b, y, t)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline(always)]
|
|
||||||
fn quadratic_bezier(a: Self, u: Self, b: Self, t: T) -> Self {
|
|
||||||
quadratic_bezier_def(a, u, b, t)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline(always)]
|
|
||||||
fn cubic_bezier(a: Self, u: Self, v: Self, b: Self, t: T) -> Self {
|
|
||||||
cubic_bezier_def(a, u, v, b, t)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl_interpolate_vec!(Vector1);
|
|
||||||
impl_interpolate_vec!(Vector2);
|
|
||||||
impl_interpolate_vec!(Vector3);
|
|
||||||
impl_interpolate_vec!(Vector4);
|
|
||||||
|
|
||||||
impl<T> Linear<T> for Quaternion<T>
|
|
||||||
where
|
|
||||||
T: BaseFloat,
|
|
||||||
{
|
|
||||||
#[inline(always)]
|
|
||||||
fn outer_mul(self, t: T) -> Self {
|
|
||||||
self * t
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline(always)]
|
|
||||||
fn outer_div(self, t: T) -> Self {
|
|
||||||
self / t
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> Interpolate<T> for Quaternion<T>
|
|
||||||
where
|
|
||||||
Self: InnerSpace<Scalar = T>,
|
|
||||||
T: Additive + BaseFloat + One,
|
|
||||||
{
|
|
||||||
#[inline(always)]
|
|
||||||
fn lerp(a: Self, b: Self, t: T) -> Self {
|
|
||||||
a.nlerp(b, t)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline(always)]
|
|
||||||
fn cubic_hermite(x: (Self, T), a: (Self, T), b: (Self, T), y: (Self, T), t: T) -> Self {
|
|
||||||
cubic_hermite_def(x, a, b, y, t)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline(always)]
|
|
||||||
fn quadratic_bezier(a: Self, u: Self, b: Self, t: T) -> Self {
|
|
||||||
quadratic_bezier_def(a, u, b, t)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline(always)]
|
|
||||||
fn cubic_bezier(a: Self, u: Self, v: Self, b: Self, t: T) -> Self {
|
|
||||||
cubic_bezier_def(a, u, v, b, t)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
92
src/glam.rs
92
src/glam.rs
@ -1,88 +1,8 @@
|
|||||||
|
use crate::impl_Interpolate;
|
||||||
use glam::{Quat, Vec2, Vec3, Vec3A, Vec4};
|
use glam::{Quat, Vec2, Vec3, Vec3A, Vec4};
|
||||||
|
|
||||||
use crate::interpolate::{
|
impl_Interpolate!(f32, Vec2, std::f32::consts::PI);
|
||||||
cubic_bezier_def, cubic_hermite_def, quadratic_bezier_def, Interpolate, Linear,
|
impl_Interpolate!(f32, Vec3, std::f32::consts::PI);
|
||||||
};
|
impl_Interpolate!(f32, Vec3A, std::f32::consts::PI);
|
||||||
|
impl_Interpolate!(f32, Vec4, std::f32::consts::PI);
|
||||||
macro_rules! impl_interpolate_vec {
|
impl_Interpolate!(f32, Quat, std::f32::consts::PI);
|
||||||
($($t:tt)*) => {
|
|
||||||
impl Linear<f32> for $($t)* {
|
|
||||||
#[inline(always)]
|
|
||||||
fn outer_mul(self, t: f32) -> Self {
|
|
||||||
self * t
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline(always)]
|
|
||||||
fn outer_div(self, t: f32) -> Self {
|
|
||||||
self / t
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Interpolate<f32> for $($t)* {
|
|
||||||
#[inline(always)]
|
|
||||||
fn lerp(a: Self, b: Self, t: f32) -> Self {
|
|
||||||
a.lerp(b, t)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline(always)]
|
|
||||||
fn cubic_hermite(
|
|
||||||
x: (Self, f32),
|
|
||||||
a: (Self, f32),
|
|
||||||
b: (Self, f32),
|
|
||||||
y: (Self, f32),
|
|
||||||
t: f32,
|
|
||||||
) -> Self {
|
|
||||||
cubic_hermite_def(x, a, b, y, t)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline(always)]
|
|
||||||
fn quadratic_bezier(a: Self, u: Self, b: Self, t: f32) -> Self {
|
|
||||||
quadratic_bezier_def(a, u, b, t)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline(always)]
|
|
||||||
fn cubic_bezier(a: Self, u: Self, v: Self, b: Self, t: f32) -> Self {
|
|
||||||
cubic_bezier_def(a, u, v, b, t)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl_interpolate_vec!(Vec2);
|
|
||||||
impl_interpolate_vec!(Vec3);
|
|
||||||
impl_interpolate_vec!(Vec3A);
|
|
||||||
impl_interpolate_vec!(Vec4);
|
|
||||||
|
|
||||||
impl Linear<f32> for Quat {
|
|
||||||
#[inline(always)]
|
|
||||||
fn outer_mul(self, t: f32) -> Self {
|
|
||||||
self * t
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline(always)]
|
|
||||||
fn outer_div(self, t: f32) -> Self {
|
|
||||||
self / t
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Interpolate<f32> for Quat {
|
|
||||||
#[inline(always)]
|
|
||||||
fn lerp(a: Self, b: Self, t: f32) -> Self {
|
|
||||||
a.lerp(b, t)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline(always)]
|
|
||||||
fn cubic_hermite(x: (Self, f32), a: (Self, f32), b: (Self, f32), y: (Self, f32), t: f32) -> Self {
|
|
||||||
cubic_hermite_def(x, a, b, y, t)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline(always)]
|
|
||||||
fn quadratic_bezier(a: Self, u: Self, b: Self, t: f32) -> Self {
|
|
||||||
quadratic_bezier_def(a, u, b, t)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline(always)]
|
|
||||||
fn cubic_bezier(a: Self, u: Self, v: Self, b: Self, t: f32) -> Self {
|
|
||||||
cubic_bezier_def(a, u, v, b, t)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -1,70 +1,18 @@
|
|||||||
use nalgebra::{Scalar, Vector, Vector1, Vector2, Vector3, Vector4, Vector5, Vector6};
|
use crate::impl_Interpolate;
|
||||||
use num_traits as nt;
|
use nalgebra::{Quaternion, Vector1, Vector2, Vector3, Vector4, Vector5, Vector6};
|
||||||
use simba::scalar::{ClosedAdd, ClosedDiv, ClosedMul, ClosedSub};
|
|
||||||
use std::ops::Mul;
|
|
||||||
|
|
||||||
use crate::interpolate::{
|
impl_Interpolate!(f32, Vector1<f32>, std::f32::consts::PI);
|
||||||
cubic_bezier_def, cubic_hermite_def, quadratic_bezier_def, Additive, Interpolate, Linear, One,
|
impl_Interpolate!(f32, Vector2<f32>, std::f32::consts::PI);
|
||||||
};
|
impl_Interpolate!(f32, Vector3<f32>, std::f32::consts::PI);
|
||||||
|
impl_Interpolate!(f32, Vector4<f32>, std::f32::consts::PI);
|
||||||
|
impl_Interpolate!(f32, Vector5<f32>, std::f32::consts::PI);
|
||||||
|
impl_Interpolate!(f32, Vector6<f32>, std::f32::consts::PI);
|
||||||
|
impl_Interpolate!(f32, Quaternion<f32>, std::f32::consts::PI);
|
||||||
|
|
||||||
macro_rules! impl_interpolate_vector {
|
impl_Interpolate!(f64, Vector1<f64>, std::f64::consts::PI);
|
||||||
($($t:tt)*) => {
|
impl_Interpolate!(f64, Vector2<f64>, std::f64::consts::PI);
|
||||||
// implement Linear
|
impl_Interpolate!(f64, Vector3<f64>, std::f64::consts::PI);
|
||||||
impl<T> Linear<T> for $($t)*<T>
|
impl_Interpolate!(f64, Vector4<f64>, std::f64::consts::PI);
|
||||||
where T: Scalar +
|
impl_Interpolate!(f64, Vector5<f64>, std::f64::consts::PI);
|
||||||
Copy +
|
impl_Interpolate!(f64, Vector6<f64>, std::f64::consts::PI);
|
||||||
ClosedAdd +
|
impl_Interpolate!(f64, Quaternion<f64>, std::f64::consts::PI);
|
||||||
ClosedSub +
|
|
||||||
ClosedMul +
|
|
||||||
ClosedDiv {
|
|
||||||
#[inline(always)]
|
|
||||||
fn outer_mul(self, t: T) -> Self {
|
|
||||||
self * t
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline(always)]
|
|
||||||
fn outer_div(self, t: T) -> Self {
|
|
||||||
self / t
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T, V> Interpolate<T> for $($t)*<V>
|
|
||||||
where Self: Linear<T>,
|
|
||||||
T: Additive + One + Mul<T, Output = T>,
|
|
||||||
V: nt::One +
|
|
||||||
nt::Zero +
|
|
||||||
Additive +
|
|
||||||
Scalar +
|
|
||||||
ClosedAdd +
|
|
||||||
ClosedMul +
|
|
||||||
ClosedSub +
|
|
||||||
Interpolate<T> {
|
|
||||||
#[inline(always)]
|
|
||||||
fn lerp(a: Self, b: Self, t: T) -> Self {
|
|
||||||
Vector::zip_map(&a, &b, |c1, c2| Interpolate::lerp(c1, c2, t))
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline(always)]
|
|
||||||
fn cubic_hermite(x: (Self, T), a: (Self, T), b: (Self, T), y: (Self, T), t: T) -> Self {
|
|
||||||
cubic_hermite_def(x, a, b, y, t)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline(always)]
|
|
||||||
fn quadratic_bezier(a: Self, u: Self, b: Self, t: T) -> Self {
|
|
||||||
quadratic_bezier_def(a, u, b, t)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline(always)]
|
|
||||||
fn cubic_bezier(a: Self, u: Self, v: Self, b: Self, t: T) -> Self {
|
|
||||||
cubic_bezier_def(a, u, v, b, t)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl_interpolate_vector!(Vector1);
|
|
||||||
impl_interpolate_vector!(Vector2);
|
|
||||||
impl_interpolate_vector!(Vector3);
|
|
||||||
impl_interpolate_vector!(Vector4);
|
|
||||||
impl_interpolate_vector!(Vector5);
|
|
||||||
impl_interpolate_vector!(Vector6);
|
|
||||||
|
@ -11,9 +11,9 @@ fn cgmath_vector_interpolation() {
|
|||||||
let mid = cg::Vector2::new(0.5, 0.5);
|
let mid = cg::Vector2::new(0.5, 0.5);
|
||||||
let end = cg::Vector2::new(1.0, 1.0);
|
let end = cg::Vector2::new(1.0, 1.0);
|
||||||
|
|
||||||
assert_eq!(Interpolate::lerp(start, end, 0.0), start);
|
assert_eq!(Interpolate::lerp(0., start, end), start);
|
||||||
assert_eq!(Interpolate::lerp(start, end, 1.0), end);
|
assert_eq!(Interpolate::lerp(1., start, end), end);
|
||||||
assert_eq!(Interpolate::lerp(start, end, 0.5), mid);
|
assert_eq!(Interpolate::lerp(0.5, start, end), mid);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -10,7 +10,7 @@ fn nalgebra_vector_interpolation() {
|
|||||||
let mid = na::Vector2::new(0.5, 0.5);
|
let mid = na::Vector2::new(0.5, 0.5);
|
||||||
let end = na::Vector2::new(1.0, 1.0);
|
let end = na::Vector2::new(1.0, 1.0);
|
||||||
|
|
||||||
assert_eq!(Interpolate::lerp(start, end, 0.0), start);
|
assert_eq!(Interpolate::lerp(0., start, end), start);
|
||||||
assert_eq!(Interpolate::lerp(start, end, 1.0), end);
|
assert_eq!(Interpolate::lerp(1., start, end), end);
|
||||||
assert_eq!(Interpolate::lerp(start, end, 0.5), mid);
|
assert_eq!(Interpolate::lerp(0.5, start, end), mid);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user