From 3e85a1f026ef5d2f00d6be1adc74c00e7ae48618 Mon Sep 17 00:00:00 2001 From: Dimitri Sabadie Date: Fri, 5 Mar 2021 02:05:36 +0100 Subject: [PATCH] Update and fix implementors for the new API. --- Cargo.toml | 4 +- src/cgmath.rs | 101 ++++++---------------------------------------- src/glam.rs | 92 +++-------------------------------------- src/nalgebra.rs | 84 ++++++++------------------------------ tests/cgmath.rs | 6 +-- tests/nalgebra.rs | 6 +-- 6 files changed, 41 insertions(+), 252 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index bbd20bf..02422e9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,7 +23,7 @@ maintenance = { status = "actively-developed" } default = ["std"] impl-cgmath = ["cgmath"] impl-glam = ["glam"] -impl-nalgebra = ["nalgebra", "num-traits", "simba"] +impl-nalgebra = ["nalgebra"] serialization = ["serde", "serde_derive"] std = [] @@ -31,10 +31,8 @@ std = [] cgmath = { version = ">=0.17, <0.19", optional = true } glam = { version = ">=0.10, <0.12", optional = true } nalgebra = { version = ">=0.21, <0.25", optional = true } -num-traits = { version = "0.2", optional = true } serde = { version = "1", optional = true } serde_derive = { version = "1", optional = true } -simba = { version = ">=0.1.2, <0.5", optional = true } [dev-dependencies] float-cmp = ">=0.6, < 0.9" diff --git a/src/cgmath.rs b/src/cgmath.rs index 958c512..7f3f17e 100644 --- a/src/cgmath.rs +++ b/src/cgmath.rs @@ -1,92 +1,15 @@ -use cgmath::{ - BaseFloat, BaseNum, InnerSpace, Quaternion, Vector1, Vector2, Vector3, Vector4, VectorSpace, -}; +use crate::impl_Interpolate; -use crate::interpolate::{ - cubic_bezier_def, cubic_hermite_def, quadratic_bezier_def, Additive, Interpolate, Linear, One, -}; +use cgmath::{Quaternion, Vector1, Vector2, Vector3, Vector4}; -macro_rules! impl_interpolate_vec { - ($($t:tt)*) => { - impl Linear for $($t)* where T: BaseNum { - #[inline(always)] - fn outer_mul(self, t: T) -> Self { - self * t - } +impl_Interpolate!(f32, Vector1, std::f32::consts::PI); +impl_Interpolate!(f32, Vector2, std::f32::consts::PI); +impl_Interpolate!(f32, Vector3, std::f32::consts::PI); +impl_Interpolate!(f32, Vector4, std::f32::consts::PI); +impl_Interpolate!(f32, Quaternion, std::f32::consts::PI); - #[inline(always)] - fn outer_div(self, t: T) -> Self { - self / t - } - } - - impl Interpolate for $($t)* - where Self: InnerSpace, 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 Linear for Quaternion -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 Interpolate for Quaternion -where - Self: InnerSpace, - 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) - } -} +impl_Interpolate!(f64, Vector1, std::f64::consts::PI); +impl_Interpolate!(f64, Vector2, std::f64::consts::PI); +impl_Interpolate!(f64, Vector3, std::f64::consts::PI); +impl_Interpolate!(f64, Vector4, std::f64::consts::PI); +impl_Interpolate!(f64, Quaternion, std::f64::consts::PI); diff --git a/src/glam.rs b/src/glam.rs index c9e510b..4333c14 100644 --- a/src/glam.rs +++ b/src/glam.rs @@ -1,88 +1,8 @@ +use crate::impl_Interpolate; use glam::{Quat, Vec2, Vec3, Vec3A, Vec4}; -use crate::interpolate::{ - cubic_bezier_def, cubic_hermite_def, quadratic_bezier_def, Interpolate, Linear, -}; - -macro_rules! impl_interpolate_vec { - ($($t:tt)*) => { - impl Linear 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 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 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 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) - } -} +impl_Interpolate!(f32, Vec2, std::f32::consts::PI); +impl_Interpolate!(f32, Vec3, std::f32::consts::PI); +impl_Interpolate!(f32, Vec3A, std::f32::consts::PI); +impl_Interpolate!(f32, Vec4, std::f32::consts::PI); +impl_Interpolate!(f32, Quat, std::f32::consts::PI); diff --git a/src/nalgebra.rs b/src/nalgebra.rs index feb5f13..37da59a 100644 --- a/src/nalgebra.rs +++ b/src/nalgebra.rs @@ -1,70 +1,18 @@ -use nalgebra::{Scalar, Vector, Vector1, Vector2, Vector3, Vector4, Vector5, Vector6}; -use num_traits as nt; -use simba::scalar::{ClosedAdd, ClosedDiv, ClosedMul, ClosedSub}; -use std::ops::Mul; +use crate::impl_Interpolate; +use nalgebra::{Quaternion, Vector1, Vector2, Vector3, Vector4, Vector5, Vector6}; -use crate::interpolate::{ - cubic_bezier_def, cubic_hermite_def, quadratic_bezier_def, Additive, Interpolate, Linear, One, -}; +impl_Interpolate!(f32, Vector1, std::f32::consts::PI); +impl_Interpolate!(f32, Vector2, std::f32::consts::PI); +impl_Interpolate!(f32, Vector3, std::f32::consts::PI); +impl_Interpolate!(f32, Vector4, std::f32::consts::PI); +impl_Interpolate!(f32, Vector5, std::f32::consts::PI); +impl_Interpolate!(f32, Vector6, std::f32::consts::PI); +impl_Interpolate!(f32, Quaternion, std::f32::consts::PI); -macro_rules! impl_interpolate_vector { - ($($t:tt)*) => { - // implement Linear - impl Linear for $($t)* - where T: Scalar + - Copy + - ClosedAdd + - 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 Interpolate for $($t)* - where Self: Linear, - T: Additive + One + Mul, - V: nt::One + - nt::Zero + - Additive + - Scalar + - ClosedAdd + - ClosedMul + - ClosedSub + - Interpolate { - #[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); +impl_Interpolate!(f64, Vector1, std::f64::consts::PI); +impl_Interpolate!(f64, Vector2, std::f64::consts::PI); +impl_Interpolate!(f64, Vector3, std::f64::consts::PI); +impl_Interpolate!(f64, Vector4, std::f64::consts::PI); +impl_Interpolate!(f64, Vector5, std::f64::consts::PI); +impl_Interpolate!(f64, Vector6, std::f64::consts::PI); +impl_Interpolate!(f64, Quaternion, std::f64::consts::PI); diff --git a/tests/cgmath.rs b/tests/cgmath.rs index 5c3a8ef..6d334f6 100644 --- a/tests/cgmath.rs +++ b/tests/cgmath.rs @@ -11,9 +11,9 @@ fn cgmath_vector_interpolation() { let mid = cg::Vector2::new(0.5, 0.5); let end = cg::Vector2::new(1.0, 1.0); - assert_eq!(Interpolate::lerp(start, end, 0.0), start); - assert_eq!(Interpolate::lerp(start, end, 1.0), end); - assert_eq!(Interpolate::lerp(start, end, 0.5), mid); + assert_eq!(Interpolate::lerp(0., start, end), start); + assert_eq!(Interpolate::lerp(1., start, end), end); + assert_eq!(Interpolate::lerp(0.5, start, end), mid); } #[test] diff --git a/tests/nalgebra.rs b/tests/nalgebra.rs index 6bf9186..f89e7df 100644 --- a/tests/nalgebra.rs +++ b/tests/nalgebra.rs @@ -10,7 +10,7 @@ fn nalgebra_vector_interpolation() { let mid = na::Vector2::new(0.5, 0.5); let end = na::Vector2::new(1.0, 1.0); - assert_eq!(Interpolate::lerp(start, end, 0.0), start); - assert_eq!(Interpolate::lerp(start, end, 1.0), end); - assert_eq!(Interpolate::lerp(start, end, 0.5), mid); + assert_eq!(Interpolate::lerp(0., start, end), start); + assert_eq!(Interpolate::lerp(1., start, end), end); + assert_eq!(Interpolate::lerp(0.5, start, end), mid); }