From ff1cda766d48797d513f435b53c22c0a82718f5f Mon Sep 17 00:00:00 2001 From: Andrea Catania Date: Tue, 5 Nov 2019 07:16:38 +0100 Subject: [PATCH] Code formatted with --- benches/common/macros.rs | 8 +- benches/construction.rs | 4 +- benches/mat.rs | 2 +- benches/quat.rs | 2 +- benches/vec.rs | 2 +- build.rs | 29 +- src/angle.rs | 6 +- src/euler.rs | 12 +- src/lib.rs | 6 +- src/macros.rs | 71 ++-- src/matrix.rs | 89 +++-- src/num.rs | 10 +- src/projection.rs | 11 +- src/quaternion.rs | 20 +- src/quaternion_simd.rs | 12 +- src/rotation.rs | 11 +- src/structure.rs | 6 +- src/transform.rs | 8 +- src/vector.rs | 4 +- src/vector_simd.rs | 24 +- tests/matrix.rs | 821 +++++++++++++++++++++++++++------------ tests/quaternion.rs | 8 +- tests/transform.rs | 6 +- tests/vector.rs | 34 +- tests/vector4f32.rs | 42 +- 25 files changed, 804 insertions(+), 444 deletions(-) diff --git a/benches/common/macros.rs b/benches/common/macros.rs index 265a1af..b8b52b0 100644 --- a/benches/common/macros.rs +++ b/benches/common/macros.rs @@ -28,9 +28,7 @@ macro_rules! bench_binop { bh.iter(|| { i = (i + 1) & (LEN - 1); - unsafe { - test::black_box(elems1.get_unchecked(i).$binop(*elems2.get_unchecked(i))) - } + unsafe { test::black_box(elems1.get_unchecked(i).$binop(*elems2.get_unchecked(i))) } }) } }; @@ -50,9 +48,7 @@ macro_rules! bench_unop { bh.iter(|| { i = (i + 1) & (LEN - 1); - unsafe { - test::black_box(elems.get_unchecked_mut(i).$unop()) - } + unsafe { test::black_box(elems.get_unchecked_mut(i).$unop()) } }) } }; diff --git a/benches/construction.rs b/benches/construction.rs index 97244e8..9f325ac 100644 --- a/benches/construction.rs +++ b/benches/construction.rs @@ -20,9 +20,9 @@ extern crate cgmath; extern crate rand; extern crate test; -use rand::{IsaacRng, Rng, FromEntropy}; -use test::Bencher; use cgmath::*; +use rand::{FromEntropy, IsaacRng, Rng}; +use test::Bencher; #[path = "common/macros.rs"] #[macro_use] diff --git a/benches/mat.rs b/benches/mat.rs index f6fbf6e..3edfeec 100644 --- a/benches/mat.rs +++ b/benches/mat.rs @@ -20,7 +20,7 @@ extern crate cgmath; extern crate rand; extern crate test; -use rand::{IsaacRng, Rng, FromEntropy}; +use rand::{FromEntropy, IsaacRng, Rng}; use std::ops::*; use test::Bencher; diff --git a/benches/quat.rs b/benches/quat.rs index 0176216..3a50186 100644 --- a/benches/quat.rs +++ b/benches/quat.rs @@ -19,7 +19,7 @@ extern crate cgmath; extern crate rand; extern crate test; -use rand::{IsaacRng, Rng, FromEntropy}; +use rand::{FromEntropy, IsaacRng, Rng}; use std::ops::*; use test::Bencher; diff --git a/benches/vec.rs b/benches/vec.rs index c1e62d6..43b51b5 100644 --- a/benches/vec.rs +++ b/benches/vec.rs @@ -20,7 +20,7 @@ extern crate cgmath; extern crate rand; extern crate test; -use rand::{IsaacRng, Rng, FromEntropy}; +use rand::{FromEntropy, IsaacRng, Rng}; use std::ops::*; use test::Bencher; diff --git a/build.rs b/build.rs index 2f53369..e2cb378 100644 --- a/build.rs +++ b/build.rs @@ -1,7 +1,7 @@ +use std::env; use std::fs::File; use std::io::Write; use std::path::Path; -use std::env; use std::string::String; /// Generate the name of the swizzle function and what it returns. @@ -11,14 +11,18 @@ fn gen_swizzle_nth<'a>(variables: &'a str, mut i: usize, upto: usize) -> Option< debug_assert!(i > 0); // zeroth permutation is empty let mut swizzle_impl = String::new(); let mut swizzle = String::new(); - let n = variables.len()+1; + let n = variables.len() + 1; for _ in 0..upto { - if i == 0 { break; } - if i % n == 0 { return None; } - let c = variables.as_bytes()[i%n - 1] as char; + if i == 0 { + break; + } + if i % n == 0 { + return None; + } + let c = variables.as_bytes()[i % n - 1] as char; swizzle.push(c); swizzle_impl.push_str(&format!("self.{}, ", c)); - i = i/n; + i = i / n; } Some((swizzle, swizzle_impl)) } @@ -31,15 +35,16 @@ fn gen_swizzle_nth<'a>(variables: &'a str, mut i: usize, upto: usize) -> Option< #[cfg(feature = "swizzle")] fn gen_swizzle_functions(variables: &'static str, upto: usize) -> String { let mut result = String::new(); - let nn = (variables.len()+1).pow(upto as u32); + let nn = (variables.len() + 1).pow(upto as u32); for i in 1..nn { if let Some((swizzle_name, swizzle_impl)) = gen_swizzle_nth(variables, i, upto) { let dim = format!("{}", swizzle_name.len()); - result.push_str( - &format!(" + result.push_str(&format!( + " /// Swizzle operator that creates a new type with dimension {2} from variables `{0}`. #[inline] pub fn {0}(&self) -> $vector_type{2}<$S> {{ $vector_type{2}::new({1}) }}\n", - swizzle_name, swizzle_impl, dim)); + swizzle_name, swizzle_impl, dim + )); } } result @@ -50,7 +55,6 @@ fn gen_swizzle_functions(_: &'static str, _: usize) -> String { String::new() } - /// This script generates the macro for building swizzle operators for multidimensional /// vectors and points. This macro is included in macros.rs fn main() { @@ -92,5 +96,6 @@ macro_rules! impl_swizzle_functions {{ xyzw4 = gen_swizzle_functions("xyzw", 4)); let mut f = File::create(swizzle_file_path) .expect("Unable to create file that defines the swizzle operator macro."); - f.write_all(data.as_bytes()).expect("Unable to write swizzle operator macro."); + f.write_all(data.as_bytes()) + .expect("Unable to write swizzle operator macro."); } diff --git a/src/angle.rs b/src/angle.rs index 75e46c9..a99598e 100644 --- a/src/angle.rs +++ b/src/angle.rs @@ -15,17 +15,17 @@ //! Angle units for type-safe, self-documenting code. -use std::fmt; use std::f64; +use std::fmt; use std::iter; use std::ops::*; +use num_traits::{cast, Bounded}; #[cfg(feature = "rand")] use rand::{ + distributions::{uniform::SampleUniform, Distribution, Standard}, Rng, - distributions::{Distribution, Standard, uniform::SampleUniform}, }; -use num_traits::{cast, Bounded}; use structure::*; diff --git a/src/euler.rs b/src/euler.rs index ea09969..1e66bb2 100644 --- a/src/euler.rs +++ b/src/euler.rs @@ -13,21 +13,21 @@ // See the License for the specific language governing permissions and // limitations under the License. +use num_traits::cast; #[cfg(feature = "rand")] use rand::{ - Rng, distributions::{Distribution, Standard}, + Rng, }; -use num_traits::cast; use structure::*; use angle::Rad; use approx; -use quaternion::Quaternion; #[cfg(feature = "mint")] use mint; use num::BaseFloat; +use quaternion::Quaternion; /// A set of [Euler angles] representing a rotation in three-dimensional space. /// @@ -191,8 +191,10 @@ impl approx::UlpsEq for Euler { #[cfg(feature = "rand")] impl Distribution> for Standard - where Standard: Distribution, - A: Angle { +where + Standard: Distribution, + A: Angle, +{ fn sample(&self, rng: &mut R) -> Euler { Euler { x: rng.gen(), diff --git a/src/lib.rs b/src/lib.rs index 9e406f9..b9642cb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -58,9 +58,9 @@ extern crate approx; #[cfg(feature = "mint")] pub extern crate mint; +pub extern crate num_traits; #[cfg(feature = "rand")] extern crate rand; -pub extern crate num_traits; #[cfg(feature = "serde")] #[macro_use] @@ -77,11 +77,11 @@ pub use structure::*; pub use matrix::{Matrix2, Matrix3, Matrix4}; pub use quaternion::Quaternion; -pub use vector::{dot, Vector1, Vector2, Vector3, Vector4, vec1, vec2, vec3, vec4}; +pub use vector::{dot, vec1, vec2, vec3, vec4, Vector1, Vector2, Vector3, Vector4}; pub use angle::{Deg, Rad}; pub use euler::Euler; -pub use point::{Point1, Point2, Point3, point1, point2, point3}; +pub use point::{point1, point2, point3, Point1, Point2, Point3}; pub use rotation::*; pub use transform::*; diff --git a/src/macros.rs b/src/macros.rs index bbcf924..e3e798c 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -139,14 +139,30 @@ macro_rules! impl_assignment_operator { } macro_rules! fold_array { - (&$method:ident, { $x:expr }) => { *$x }; - (&$method:ident, { $x:expr, $y:expr }) => { $x.$method(&$y) }; - (&$method:ident, { $x:expr, $y:expr, $z:expr }) => { $x.$method(&$y).$method(&$z) }; - (&$method:ident, { $x:expr, $y:expr, $z:expr, $w:expr }) => { $x.$method(&$y).$method(&$z).$method(&$w) }; - ($method:ident, { $x:expr }) => { $x }; - ($method:ident, { $x:expr, $y:expr }) => { $x.$method($y) }; - ($method:ident, { $x:expr, $y:expr, $z:expr }) => { $x.$method($y).$method($z) }; - ($method:ident, { $x:expr, $y:expr, $z:expr, $w:expr }) => { $x.$method($y).$method($z).$method($w) }; + (&$method:ident, { $x:expr }) => { + *$x + }; + (&$method:ident, { $x:expr, $y:expr }) => { + $x.$method(&$y) + }; + (&$method:ident, { $x:expr, $y:expr, $z:expr }) => { + $x.$method(&$y).$method(&$z) + }; + (&$method:ident, { $x:expr, $y:expr, $z:expr, $w:expr }) => { + $x.$method(&$y).$method(&$z).$method(&$w) + }; + ($method:ident, { $x:expr }) => { + $x + }; + ($method:ident, { $x:expr, $y:expr }) => { + $x.$method($y) + }; + ($method:ident, { $x:expr, $y:expr, $z:expr }) => { + $x.$method($y).$method($z) + }; + ($method:ident, { $x:expr, $y:expr, $z:expr, $w:expr }) => { + $x.$method($y).$method($z).$method($w) + }; } /// Generate array conversion implementations for a compound array type @@ -252,17 +268,19 @@ macro_rules! impl_index_operators { #[inline] fn index<'a>(&'a self, i: $I) -> &'a $Output { - let v: &[$S; $n] = self.as_ref(); &v[i] + let v: &[$S; $n] = self.as_ref(); + &v[i] } } impl<$S> IndexMut<$I> for $VectorN<$S> { #[inline] fn index_mut<'a>(&'a mut self, i: $I) -> &'a mut $Output { - let v: &mut [$S; $n] = self.as_mut(); &mut v[i] + let v: &mut [$S; $n] = self.as_mut(); + &mut v[i] } } - } + }; } /// Generates a binary operator implementation for the permutations of by-ref and by-val, for simd @@ -272,11 +290,11 @@ macro_rules! impl_operator_simd { ([$Simd:ident]; $Op:ident for $Lhs:ty { fn $op:ident($x:ident) -> $Output:ty { $body:expr } }) => { - impl $Op for $Lhs { #[inline] fn $op(self) -> $Output { - let $x: $Simd = self.into(); $body + let $x: $Simd = self.into(); + $body } } }; @@ -287,15 +305,16 @@ macro_rules! impl_operator_simd { impl $Op<$Rhs> for $Lhs { #[inline] fn $op(self, other: $Rhs) -> $Output { - let ($lhs, $rhs): ($Simd, $Simd) = (self.into(), $Simd::splat(other)); $body + let ($lhs, $rhs): ($Simd, $Simd) = (self.into(), $Simd::splat(other)); + $body } } - impl<'a> $Op<$Rhs> for &'a $Lhs { #[inline] fn $op(self, other: $Rhs) -> $Output { - let ($lhs, $rhs): ($Simd, $Simd) = ((*self).into(), $Simd::splat(other)); $body + let ($lhs, $rhs): ($Simd, $Simd) = ((*self).into(), $Simd::splat(other)); + $body } } }; @@ -304,33 +323,35 @@ macro_rules! impl_operator_simd { ([$Simd:ident]; $Op:ident<$Rhs:ty> for $Lhs:ty { fn $op:ident($lhs:ident, $rhs:ident) -> $Output:ty { $body:expr } }) => { - impl $Op<$Rhs> for $Lhs { #[inline] fn $op(self, other: $Rhs) -> $Output { - let ($lhs, $rhs): ($Simd, $Simd) = (self.into(), other.into()); $body + let ($lhs, $rhs): ($Simd, $Simd) = (self.into(), other.into()); + $body } } - impl<'a> $Op<&'a $Rhs> for $Lhs { #[inline] fn $op(self, other: &'a $Rhs) -> $Output { - let ($lhs, $rhs): ($Simd, $Simd) = (self.into(), (*other).into()); $body + let ($lhs, $rhs): ($Simd, $Simd) = (self.into(), (*other).into()); + $body } } impl<'a> $Op<$Rhs> for &'a $Lhs { #[inline] fn $op(self, other: $Rhs) -> $Output { - let ($lhs, $rhs): ($Simd, $Simd) = ((*self).into(), other.into()); $body + let ($lhs, $rhs): ($Simd, $Simd) = ((*self).into(), other.into()); + $body } } impl<'a, 'b> $Op<&'a $Rhs> for &'b $Lhs { #[inline] fn $op(self, other: &'a $Rhs) -> $Output { - let ($lhs, $rhs): ($Simd, $Simd) = ((*self).into(), (*other).into()); $body + let ($lhs, $rhs): ($Simd, $Simd) = ((*self).into(), (*other).into()); + $body } } }; @@ -342,14 +363,16 @@ macro_rules! impl_operator_simd { impl $Op<$Rhs> for $Lhs { #[inline] fn $op(self, other: $Rhs) -> $Output { - let ($lhs, $rhs): ($Simd, $Simd) = ($Simd::splat(self), other.into()); $body + let ($lhs, $rhs): ($Simd, $Simd) = ($Simd::splat(self), other.into()); + $body } } impl<'a> $Op<&'a $Rhs> for $Lhs { #[inline] fn $op(self, other: &'a $Rhs) -> $Output { - let ($lhs, $rhs): ($Simd, $Simd) = ($Simd::splat(self), (*other).into()); $body + let ($lhs, $rhs): ($Simd, $Simd) = ($Simd::splat(self), (*other).into()); + $body } } }; diff --git a/src/matrix.rs b/src/matrix.rs index a46549a..3b64f91 100644 --- a/src/matrix.rs +++ b/src/matrix.rs @@ -13,12 +13,12 @@ // See the License for the specific language governing permissions and // limitations under the License. +use num_traits::{cast, NumCast}; #[cfg(feature = "rand")] use rand::{ + distributions::{Distribution, Standard}, Rng, - distributions::{Standard, Distribution}, }; -use num_traits::{cast, NumCast}; use std::fmt; use std::iter; use std::mem; @@ -285,7 +285,12 @@ impl Matrix4 { /// Create a new matrix, providing columns. #[inline] - pub const fn from_cols(c0: Vector4, c1: Vector4, c2: Vector4, c3: Vector4) -> Matrix4 { + pub const fn from_cols( + c0: Vector4, + c1: Vector4, + c2: Vector4, + c3: Vector4, + ) -> Matrix4 { Matrix4 { x: c0, y: c1, @@ -696,21 +701,28 @@ impl SquareMatrix for Matrix3 { self[1].cross(self[2]) / det, self[2].cross(self[0]) / det, self[0].cross(self[1]) / det, - ).transpose(), + ) + .transpose(), ) } } fn is_diagonal(&self) -> bool { - ulps_eq!(self[0][1], &S::zero()) && ulps_eq!(self[0][2], &S::zero()) - && ulps_eq!(self[1][0], &S::zero()) && ulps_eq!(self[1][2], &S::zero()) - && ulps_eq!(self[2][0], &S::zero()) && ulps_eq!(self[2][1], &S::zero()) + ulps_eq!(self[0][1], &S::zero()) + && ulps_eq!(self[0][2], &S::zero()) + && ulps_eq!(self[1][0], &S::zero()) + && ulps_eq!(self[1][2], &S::zero()) + && ulps_eq!(self[2][0], &S::zero()) + && ulps_eq!(self[2][1], &S::zero()) } fn is_symmetric(&self) -> bool { - ulps_eq!(self[0][1], &self[1][0]) && ulps_eq!(self[0][2], &self[2][0]) - && ulps_eq!(self[1][0], &self[0][1]) && ulps_eq!(self[1][2], &self[2][1]) - && ulps_eq!(self[2][0], &self[0][2]) && ulps_eq!(self[2][1], &self[1][2]) + ulps_eq!(self[0][1], &self[1][0]) + && ulps_eq!(self[0][2], &self[2][0]) + && ulps_eq!(self[1][0], &self[0][1]) + && ulps_eq!(self[1][2], &self[2][1]) + && ulps_eq!(self[2][0], &self[0][2]) + && ulps_eq!(self[2][1], &self[1][2]) } } @@ -863,21 +875,33 @@ impl SquareMatrix for Matrix4 { } fn is_diagonal(&self) -> bool { - ulps_eq!(self[0][1], &S::zero()) && ulps_eq!(self[0][2], &S::zero()) - && ulps_eq!(self[0][3], &S::zero()) && ulps_eq!(self[1][0], &S::zero()) - && ulps_eq!(self[1][2], &S::zero()) && ulps_eq!(self[1][3], &S::zero()) - && ulps_eq!(self[2][0], &S::zero()) && ulps_eq!(self[2][1], &S::zero()) - && ulps_eq!(self[2][3], &S::zero()) && ulps_eq!(self[3][0], &S::zero()) - && ulps_eq!(self[3][1], &S::zero()) && ulps_eq!(self[3][2], &S::zero()) + ulps_eq!(self[0][1], &S::zero()) + && ulps_eq!(self[0][2], &S::zero()) + && ulps_eq!(self[0][3], &S::zero()) + && ulps_eq!(self[1][0], &S::zero()) + && ulps_eq!(self[1][2], &S::zero()) + && ulps_eq!(self[1][3], &S::zero()) + && ulps_eq!(self[2][0], &S::zero()) + && ulps_eq!(self[2][1], &S::zero()) + && ulps_eq!(self[2][3], &S::zero()) + && ulps_eq!(self[3][0], &S::zero()) + && ulps_eq!(self[3][1], &S::zero()) + && ulps_eq!(self[3][2], &S::zero()) } fn is_symmetric(&self) -> bool { - ulps_eq!(self[0][1], &self[1][0]) && ulps_eq!(self[0][2], &self[2][0]) - && ulps_eq!(self[0][3], &self[3][0]) && ulps_eq!(self[1][0], &self[0][1]) - && ulps_eq!(self[1][2], &self[2][1]) && ulps_eq!(self[1][3], &self[3][1]) - && ulps_eq!(self[2][0], &self[0][2]) && ulps_eq!(self[2][1], &self[1][2]) - && ulps_eq!(self[2][3], &self[3][2]) && ulps_eq!(self[3][0], &self[0][3]) - && ulps_eq!(self[3][1], &self[1][3]) && ulps_eq!(self[3][2], &self[2][3]) + ulps_eq!(self[0][1], &self[1][0]) + && ulps_eq!(self[0][2], &self[2][0]) + && ulps_eq!(self[0][3], &self[3][0]) + && ulps_eq!(self[1][0], &self[0][1]) + && ulps_eq!(self[1][2], &self[2][1]) + && ulps_eq!(self[1][3], &self[3][1]) + && ulps_eq!(self[2][0], &self[0][2]) + && ulps_eq!(self[2][1], &self[1][2]) + && ulps_eq!(self[2][3], &self[3][2]) + && ulps_eq!(self[3][0], &self[0][3]) + && ulps_eq!(self[3][1], &self[1][3]) + && ulps_eq!(self[3][2], &self[2][3]) } } @@ -1294,7 +1318,7 @@ macro_rules! index_operators { From::from(&mut v[i]) } } - } + }; } index_operators!(Matrix2, 2, Vector2, usize); @@ -1581,9 +1605,10 @@ impl fmt::Debug for Matrix4 { #[cfg(feature = "rand")] impl Distribution> for Standard - where - Standard: Distribution>, - S: BaseFloat { +where + Standard: Distribution>, + S: BaseFloat, +{ #[inline] fn sample(&self, rng: &mut R) -> Matrix2 { Matrix2 { @@ -1595,8 +1620,10 @@ impl Distribution> for Standard #[cfg(feature = "rand")] impl Distribution> for Standard - where Standard: Distribution>, - S: BaseFloat { +where + Standard: Distribution>, + S: BaseFloat, +{ #[inline] fn sample(&self, rng: &mut R) -> Matrix3 { Matrix3 { @@ -1609,8 +1636,10 @@ impl Distribution> for Standard #[cfg(feature = "rand")] impl Distribution> for Standard - where Standard: Distribution>, - S: BaseFloat { +where + Standard: Distribution>, + S: BaseFloat, +{ #[inline] fn sample(&self, rng: &mut R) -> Matrix4 { Matrix4 { diff --git a/src/num.rs b/src/num.rs index 113a4f2..3278721 100644 --- a/src/num.rs +++ b/src/num.rs @@ -36,8 +36,7 @@ pub trait BaseNum: { } -impl BaseNum for T -where +impl BaseNum for T where T: Copy + Clone + fmt::Debug @@ -48,7 +47,7 @@ where + SubAssign + MulAssign + DivAssign - + RemAssign, + + RemAssign { } @@ -62,12 +61,11 @@ pub trait BaseFloat: { } -impl BaseFloat for T -where +impl BaseFloat for T where T: BaseNum + Float + approx::AbsDiffEq + approx::RelativeEq - + approx::UlpsEq, + + approx::UlpsEq { } diff --git a/src/projection.rs b/src/projection.rs index 5219ee3..68cb938 100644 --- a/src/projection.rs +++ b/src/projection.rs @@ -13,8 +13,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -use num_traits::Zero; use num_traits::cast; +use num_traits::Zero; use structure::Angle; @@ -38,7 +38,8 @@ pub fn perspective>>( aspect: aspect, near: near, far: far, - }.into() + } + .into() } /// Create a perspective matrix from a view frustum. @@ -54,7 +55,8 @@ pub fn frustum(left: S, right: S, bottom: S, top: S, near: S, far: top: top, near: near, far: far, - }.into() + } + .into() } /// Create an orthographic projection matrix. @@ -70,7 +72,8 @@ pub fn ortho(left: S, right: S, bottom: S, top: S, near: S, far: S top: top, near: near, far: far, - }.into() + } + .into() } /// A perspective projection based on a vertical field-of-view angle. diff --git a/src/quaternion.rs b/src/quaternion.rs index f267002..57a6131 100644 --- a/src/quaternion.rs +++ b/src/quaternion.rs @@ -17,12 +17,12 @@ use std::iter; use std::mem; use std::ops::*; +use num_traits::{cast, NumCast}; #[cfg(feature = "rand")] use rand::{ - Rng, distributions::{Distribution, Standard}, + Rng, }; -use num_traits::{cast, NumCast}; use structure::*; @@ -626,17 +626,19 @@ macro_rules! index_operators { #[inline] fn index<'a>(&'a self, i: $I) -> &'a $Output { - let v: &[$S; 4] = self.as_ref(); &v[i] + let v: &[$S; 4] = self.as_ref(); + &v[i] } } impl<$S: BaseFloat> IndexMut<$I> for Quaternion<$S> { #[inline] fn index_mut<'a>(&'a mut self, i: $I) -> &'a mut $Output { - let v: &mut [$S; 4] = self.as_mut(); &mut v[i] + let v: &mut [$S; 4] = self.as_mut(); + &mut v[i] } } - } + }; } index_operators!(S, S, usize); @@ -647,9 +649,11 @@ index_operators!(S, [S], RangeFull); #[cfg(feature = "rand")] impl Distribution> for Standard - where Standard: Distribution, - Standard: Distribution>, - S: BaseFloat { +where + Standard: Distribution, + Standard: Distribution>, + S: BaseFloat, +{ #[inline] fn sample(&self, rng: &mut R) -> Quaternion { Quaternion::from_sv(rng.gen(), rng.gen()) diff --git a/src/quaternion_simd.rs b/src/quaternion_simd.rs index 83ccad8..bb88056 100644 --- a/src/quaternion_simd.rs +++ b/src/quaternion_simd.rs @@ -54,7 +54,7 @@ impl InnerSpace for Quaternion { } } -impl_operator_simd!{ +impl_operator_simd! { [Simdf32x4]; Neg for Quaternion { fn neg(lhs) -> Quaternion { (-lhs).into() @@ -62,7 +62,7 @@ impl_operator_simd!{ } } -impl_operator_simd!{@rs +impl_operator_simd! {@rs [Simdf32x4]; Mul for Quaternion { fn mul(lhs, rhs) -> Quaternion { (lhs * rhs).into() @@ -78,7 +78,7 @@ impl MulAssign for Quaternion { } } -impl_operator_simd!{@rs +impl_operator_simd! {@rs [Simdf32x4]; Div for Quaternion { fn div(lhs, rhs) -> Quaternion { (lhs / rhs).into() @@ -94,7 +94,7 @@ impl DivAssign for Quaternion { } } -impl_operator_simd!{ +impl_operator_simd! { [Simdf32x4]; Add> for Quaternion { fn add(lhs, rhs) -> Quaternion { (lhs + rhs).into() @@ -111,7 +111,7 @@ impl AddAssign for Quaternion { } } -impl_operator_simd!{ +impl_operator_simd! { [Simdf32x4]; Sub> for Quaternion { fn sub(lhs, rhs) -> Quaternion { (lhs - rhs).into() @@ -128,7 +128,7 @@ impl SubAssign for Quaternion { } } -impl_operator_simd!{ +impl_operator_simd! { [Simdf32x4]; Mul> for Quaternion { fn mul(lhs, rhs) -> Quaternion { { diff --git a/src/rotation.rs b/src/rotation.rs index a8e28ca..a1de85b 100644 --- a/src/rotation.rs +++ b/src/rotation.rs @@ -62,17 +62,18 @@ where } /// A two-dimensional rotation. -pub trait Rotation2 - : Rotation> + Into> + Into> { +pub trait Rotation2: + Rotation> + Into> + Into> +{ /// Create a rotation by a given angle. Thus is a redundant case of both /// from_axis_angle() and from_euler() for 2D space. fn from_angle>>(theta: A) -> Self; } /// A three-dimensional rotation. -pub trait Rotation3 - : Rotation> + Into> + Into> + Into> + From>> - { +pub trait Rotation3: + Rotation> + Into> + Into> + Into> + From>> +{ /// Create a rotation using an angle around a given axis. /// /// The specified axis **must be normalized**, or it represents an invalid rotation. diff --git a/src/structure.rs b/src/structure.rs index 8721e51..255449c 100644 --- a/src/structure.rs +++ b/src/structure.rs @@ -607,7 +607,11 @@ where #[inline] fn normalize_signed(self) -> Self { let rem = self.normalize(); - if Self::turn_div_2() < rem { rem - Self::full_turn() } else { rem } + if Self::turn_div_2() < rem { + rem - Self::full_turn() + } else { + rem + } } /// Return the angle rotated by half a turn. diff --git a/src/transform.rs b/src/transform.rs index f3d3113..fbe4c37 100644 --- a/src/transform.rs +++ b/src/transform.rs @@ -225,10 +225,10 @@ where #[cfg(feature = "serde")] #[doc(hidden)] mod serde_ser { - use structure::VectorSpace; use super::Decomposed; - use serde::{self, Serialize}; use serde::ser::SerializeStruct; + use serde::{self, Serialize}; + use structure::VectorSpace; impl Serialize for Decomposed where @@ -252,11 +252,11 @@ mod serde_ser { #[cfg(feature = "serde")] #[doc(hidden)] mod serde_de { - use structure::VectorSpace; use super::Decomposed; use serde::{self, Deserialize}; - use std::marker::PhantomData; use std::fmt; + use std::marker::PhantomData; + use structure::VectorSpace; enum DecomposedField { Scale, diff --git a/src/vector.rs b/src/vector.rs index 1e58c4a..5da7d44 100644 --- a/src/vector.rs +++ b/src/vector.rs @@ -13,12 +13,12 @@ // See the License for the specific language governing permissions and // limitations under the License. +use num_traits::{Bounded, NumCast}; #[cfg(feature = "rand")] use rand::{ - Rng, distributions::{Distribution, Standard}, + Rng, }; -use num_traits::{Bounded, NumCast}; use std::fmt; use std::iter; use std::mem; diff --git a/src/vector_simd.rs b/src/vector_simd.rs index 3c41d1a..70592ee 100644 --- a/src/vector_simd.rs +++ b/src/vector_simd.rs @@ -69,7 +69,7 @@ impl Into for Vector4 { } } -impl_operator_simd!{ +impl_operator_simd! { [Simdf32x4]; Add> for Vector4 { fn add(lhs, rhs) -> Vector4 { (lhs + rhs).into() @@ -77,7 +77,7 @@ impl_operator_simd!{ } } -impl_operator_simd!{ +impl_operator_simd! { [Simdf32x4]; Sub> for Vector4 { fn sub(lhs, rhs) -> Vector4 { (lhs - rhs).into() @@ -85,7 +85,7 @@ impl_operator_simd!{ } } -impl_operator_simd!{@rs +impl_operator_simd! {@rs [Simdf32x4]; Mul for Vector4 { fn mul(lhs, rhs) -> Vector4 { (lhs * rhs).into() @@ -93,7 +93,7 @@ impl_operator_simd!{@rs } } -impl_operator_simd!{@rs +impl_operator_simd! {@rs [Simdf32x4]; Div for Vector4 { fn div(lhs, rhs) -> Vector4 { (lhs / rhs).into() @@ -101,7 +101,7 @@ impl_operator_simd!{@rs } } -impl_operator_simd!{ +impl_operator_simd! { [Simdf32x4]; Neg for Vector4 { fn neg(lhs) -> Vector4 { (-lhs).into() @@ -262,7 +262,7 @@ impl Into for Vector4 { } } -impl_operator_simd!{ +impl_operator_simd! { [Simdi32x4]; Add> for Vector4 { fn add(lhs, rhs) -> Vector4 { (lhs + rhs).into() @@ -270,7 +270,7 @@ impl_operator_simd!{ } } -impl_operator_simd!{ +impl_operator_simd! { [Simdi32x4]; Sub> for Vector4 { fn sub(lhs, rhs) -> Vector4 { (lhs - rhs).into() @@ -278,7 +278,7 @@ impl_operator_simd!{ } } -impl_operator_simd!{@rs +impl_operator_simd! {@rs [Simdi32x4]; Mul for Vector4 { fn mul(lhs, rhs) -> Vector4 { (lhs * rhs).into() @@ -286,7 +286,7 @@ impl_operator_simd!{@rs } } -impl_operator_simd!{ +impl_operator_simd! { [Simdi32x4]; Neg for Vector4 { fn neg(lhs) -> Vector4 { (-lhs).into() @@ -342,7 +342,7 @@ impl Into for Vector4 { } } -impl_operator_simd!{ +impl_operator_simd! { [Simdu32x4]; Add> for Vector4 { fn add(lhs, rhs) -> Vector4 { (lhs + rhs).into() @@ -350,7 +350,7 @@ impl_operator_simd!{ } } -impl_operator_simd!{ +impl_operator_simd! { [Simdu32x4]; Sub> for Vector4 { fn sub(lhs, rhs) -> Vector4 { (lhs - rhs).into() @@ -358,7 +358,7 @@ impl_operator_simd!{ } } -impl_operator_simd!{@rs +impl_operator_simd! {@rs [Simdu32x4]; Mul for Vector4 { fn mul(lhs, rhs) -> Vector4 { (lhs * rhs).into() diff --git a/tests/matrix.rs b/tests/matrix.rs index 7798ce2..734c3bb 100644 --- a/tests/matrix.rs +++ b/tests/matrix.rs @@ -21,63 +21,75 @@ pub mod matrix2 { use cgmath::*; - const A: Matrix2 = Matrix2 { x: Vector2 { x: 1.0f64, y: 3.0f64 }, - y: Vector2 { x: 2.0f64, y: 4.0f64 } }; - const B: Matrix2 = Matrix2 { x: Vector2 { x: 2.0f64, y: 4.0f64 }, - y: Vector2 { x: 3.0f64, y: 5.0f64 } }; - const C: Matrix2 = Matrix2 { x: Vector2 { x: 2.0f64, y: 1.0f64 }, - y: Vector2 { x: 1.0f64, y: 2.0f64 } }; + const A: Matrix2 = Matrix2 { + x: Vector2 { + x: 1.0f64, + y: 3.0f64, + }, + y: Vector2 { + x: 2.0f64, + y: 4.0f64, + }, + }; + const B: Matrix2 = Matrix2 { + x: Vector2 { + x: 2.0f64, + y: 4.0f64, + }, + y: Vector2 { + x: 3.0f64, + y: 5.0f64, + }, + }; + const C: Matrix2 = Matrix2 { + x: Vector2 { + x: 2.0f64, + y: 1.0f64, + }, + y: Vector2 { + x: 1.0f64, + y: 2.0f64, + }, + }; - const V: Vector2 = Vector2 { x: 1.0f64, y: 2.0f64 }; + const V: Vector2 = Vector2 { + x: 1.0f64, + y: 2.0f64, + }; const F: f64 = 0.5; #[test] fn test_neg() { - assert_eq!(-A, - Matrix2::new(-1.0f64, -3.0f64, - -2.0f64, -4.0f64)); + assert_eq!(-A, Matrix2::new(-1.0f64, -3.0f64, -2.0f64, -4.0f64)); } #[test] fn test_mul_scalar() { - let result = Matrix2::new(0.5f64, 1.5f64, - 1.0f64, 2.0f64) ; + let result = Matrix2::new(0.5f64, 1.5f64, 1.0f64, 2.0f64); assert_eq!(A * F, result); assert_eq!(F * A, result); } #[test] fn test_div_scalar() { - assert_eq!(A / F, - Matrix2::new(2.0f64, 6.0f64, - 4.0f64, 8.0f64)); - assert_eq!(4.0f64 / C, - Matrix2::new(2.0f64, 4.0f64, - 4.0f64, 2.0f64)); + assert_eq!(A / F, Matrix2::new(2.0f64, 6.0f64, 4.0f64, 8.0f64)); + assert_eq!(4.0f64 / C, Matrix2::new(2.0f64, 4.0f64, 4.0f64, 2.0f64)); } #[test] fn test_rem_scalar() { - assert_eq!(A % 3.0f64, - Matrix2::new(1.0f64, 0.0f64, - 2.0f64, 1.0f64)); - assert_eq!(3.0f64 % A, - Matrix2::new(0.0f64, 0.0f64, - 1.0f64, 3.0f64)); + assert_eq!(A % 3.0f64, Matrix2::new(1.0f64, 0.0f64, 2.0f64, 1.0f64)); + assert_eq!(3.0f64 % A, Matrix2::new(0.0f64, 0.0f64, 1.0f64, 3.0f64)); } #[test] fn test_add_matrix() { - assert_eq!(A + B, - Matrix2::new(3.0f64, 7.0f64, - 5.0f64, 9.0f64)); + assert_eq!(A + B, Matrix2::new(3.0f64, 7.0f64, 5.0f64, 9.0f64)); } #[test] fn test_sub_matrix() { - assert_eq!(A - B, - Matrix2::new(-1.0f64, -1.0f64, - -1.0f64, -1.0f64)); + assert_eq!(A - B, Matrix2::new(-1.0f64, -1.0f64, -1.0f64, -1.0f64)); } #[test] @@ -87,9 +99,7 @@ pub mod matrix2 { #[test] fn test_mul_matrix() { - assert_eq!(A * B, - Matrix2::new(10.0f64, 22.0f64, - 13.0f64, 29.0f64)); + assert_eq!(A * B, Matrix2::new(10.0f64, 22.0f64, 13.0f64, 29.0f64)); assert_eq!(A * B, &A * &B); } @@ -118,9 +128,10 @@ pub mod matrix2 { #[test] fn test_transpose() { - assert_eq!(A.transpose(), - Matrix2::::new(1.0f64, 2.0f64, - 3.0f64, 4.0f64)); + assert_eq!( + A.transpose(), + Matrix2::::new(1.0f64, 2.0f64, 3.0f64, 4.0f64) + ); } #[test] @@ -134,11 +145,13 @@ pub mod matrix2 { fn test_invert() { assert!(Matrix2::::identity().invert().unwrap().is_identity()); - assert_eq!(A.invert().unwrap(), - Matrix2::new(-2.0f64, 1.5f64, - 1.0f64, -0.5f64)); - assert!(Matrix2::new(0.0f64, 2.0f64, - 0.0f64, 5.0f64).invert().is_none()); + assert_eq!( + A.invert().unwrap(), + Matrix2::new(-2.0f64, 1.5f64, 1.0f64, -0.5f64) + ); + assert!(Matrix2::new(0.0f64, 2.0f64, 0.0f64, 5.0f64) + .invert() + .is_none()); } #[test] @@ -180,98 +193,161 @@ pub mod matrix2 { fn test_look_at() { // rot should rotate unit_x() to look at the input vector let rot = Matrix2::look_at(V, Vector2::unit_y()); - assert_eq!(rot * Vector2::unit_x(), - V.normalize()); + assert_eq!(rot * Vector2::unit_x(), V.normalize()); let new_up = Vector2::new(-V.y, V.x).normalize(); - assert_eq!(rot * Vector2::unit_y(), - new_up); + assert_eq!(rot * Vector2::unit_y(), new_up); let rot_down = Matrix2::look_at(V, -1.0 * Vector2::unit_y()); - assert_eq!(rot_down * Vector2::unit_x(), - V.normalize()); - assert_eq!(rot_down * Vector2::unit_y(), - -1.0 * new_up); + assert_eq!(rot_down * Vector2::unit_x(), V.normalize()); + assert_eq!(rot_down * Vector2::unit_y(), -1.0 * new_up); let rot2 = Matrix2::look_at(-V, Vector2::unit_y()); - assert_eq!(rot2 * Vector2::unit_x(), - (-V).normalize()); + assert_eq!(rot2 * Vector2::unit_x(), (-V).normalize()); } } pub mod matrix3 { use cgmath::*; - const A: Matrix3 = Matrix3 { x: Vector3 { x: 1.0f64, y: 4.0f64, z: 7.0f64 }, - y: Vector3 { x: 2.0f64, y: 5.0f64, z: 8.0f64 }, - z: Vector3 { x: 3.0f64, y: 6.0f64, z: 9.0f64 } }; - const B: Matrix3 = Matrix3 { x: Vector3 { x: 2.0f64, y: 5.0f64, z: 8.0f64 }, - y: Vector3 { x: 3.0f64, y: 6.0f64, z: 9.0f64 }, - z: Vector3 { x: 4.0f64, y: 7.0f64, z: 10.0f64 } }; - const C: Matrix3 = Matrix3 { x: Vector3 { x: 2.0f64, y: 4.0f64, z: 6.0f64 }, - y: Vector3 { x: 0.0f64, y: 2.0f64, z: 4.0f64 }, - z: Vector3 { x: 0.0f64, y: 0.0f64, z: 1.0f64 } }; - const D: Matrix3 = Matrix3 { x: Vector3 { x: 3.0f64, y: 2.0f64, z: 1.0f64 }, - y: Vector3 { x: 2.0f64, y: 3.0f64, z: 2.0f64 }, - z: Vector3 { x: 1.0f64, y: 2.0f64, z: 3.0f64 } }; + const A: Matrix3 = Matrix3 { + x: Vector3 { + x: 1.0f64, + y: 4.0f64, + z: 7.0f64, + }, + y: Vector3 { + x: 2.0f64, + y: 5.0f64, + z: 8.0f64, + }, + z: Vector3 { + x: 3.0f64, + y: 6.0f64, + z: 9.0f64, + }, + }; + const B: Matrix3 = Matrix3 { + x: Vector3 { + x: 2.0f64, + y: 5.0f64, + z: 8.0f64, + }, + y: Vector3 { + x: 3.0f64, + y: 6.0f64, + z: 9.0f64, + }, + z: Vector3 { + x: 4.0f64, + y: 7.0f64, + z: 10.0f64, + }, + }; + const C: Matrix3 = Matrix3 { + x: Vector3 { + x: 2.0f64, + y: 4.0f64, + z: 6.0f64, + }, + y: Vector3 { + x: 0.0f64, + y: 2.0f64, + z: 4.0f64, + }, + z: Vector3 { + x: 0.0f64, + y: 0.0f64, + z: 1.0f64, + }, + }; + const D: Matrix3 = Matrix3 { + x: Vector3 { + x: 3.0f64, + y: 2.0f64, + z: 1.0f64, + }, + y: Vector3 { + x: 2.0f64, + y: 3.0f64, + z: 2.0f64, + }, + z: Vector3 { + x: 1.0f64, + y: 2.0f64, + z: 3.0f64, + }, + }; - const V: Vector3 = Vector3 { x: 1.0f64, y: 2.0f64, z: 3.0f64 }; + const V: Vector3 = Vector3 { + x: 1.0f64, + y: 2.0f64, + z: 3.0f64, + }; const F: f64 = 0.5; #[test] fn test_neg() { - assert_eq!(-A, - Matrix3::new(-1.0f64, -4.0f64, -7.0f64, - -2.0f64, -5.0f64, -8.0f64, - -3.0f64, -6.0f64, -9.0f64)); + assert_eq!( + -A, + Matrix3::new( + -1.0f64, -4.0f64, -7.0f64, -2.0f64, -5.0f64, -8.0f64, -3.0f64, -6.0f64, -9.0f64 + ) + ); } #[test] fn test_mul_scalar() { - let result = Matrix3::new(0.5f64, 2.0f64, 3.5f64, - 1.0f64, 2.5f64, 4.0f64, - 1.5f64, 3.0f64, 4.5f64); + let result = Matrix3::new( + 0.5f64, 2.0f64, 3.5f64, 1.0f64, 2.5f64, 4.0f64, 1.5f64, 3.0f64, 4.5f64, + ); assert_eq!(A * F, result); assert_eq!(F * A, result); } #[test] fn test_div_scalar() { - assert_eq!(A / F, - Matrix3::new(2.0f64, 8.0f64, 14.0f64, - 4.0f64, 10.0f64, 16.0f64, - 6.0f64, 12.0f64, 18.0f64)); - assert_eq!(6.0f64 / D, - Matrix3::new(2.0f64, 3.0f64, 6.0f64, - 3.0f64, 2.0f64, 3.0f64, - 6.0f64, 3.0f64, 2.0f64)); + assert_eq!( + A / F, + Matrix3::new( + 2.0f64, 8.0f64, 14.0f64, 4.0f64, 10.0f64, 16.0f64, 6.0f64, 12.0f64, 18.0f64 + ) + ); + assert_eq!( + 6.0f64 / D, + Matrix3::new(2.0f64, 3.0f64, 6.0f64, 3.0f64, 2.0f64, 3.0f64, 6.0f64, 3.0f64, 2.0f64) + ); } #[test] fn test_rem_scalar() { - assert_eq!(A % 3.0f64, - Matrix3::new(1.0f64, 1.0f64, 1.0f64, - 2.0f64, 2.0f64, 2.0f64, - 0.0f64, 0.0f64, 0.0f64)); - assert_eq!(9.0f64 % A, - Matrix3::new(0.0f64, 1.0f64, 2.0f64, - 1.0f64, 4.0f64, 1.0f64, - 0.0f64, 3.0f64, 0.0f64)); + assert_eq!( + A % 3.0f64, + Matrix3::new(1.0f64, 1.0f64, 1.0f64, 2.0f64, 2.0f64, 2.0f64, 0.0f64, 0.0f64, 0.0f64) + ); + assert_eq!( + 9.0f64 % A, + Matrix3::new(0.0f64, 1.0f64, 2.0f64, 1.0f64, 4.0f64, 1.0f64, 0.0f64, 3.0f64, 0.0f64) + ); } #[test] fn test_add_matrix() { - assert_eq!(A + B, - Matrix3::new(3.0f64, 9.0f64, 15.0f64, - 5.0f64, 11.0f64, 17.0f64, - 7.0f64, 13.0f64, 19.0f64)); + assert_eq!( + A + B, + Matrix3::new( + 3.0f64, 9.0f64, 15.0f64, 5.0f64, 11.0f64, 17.0f64, 7.0f64, 13.0f64, 19.0f64 + ) + ); } #[test] fn test_sub_matrix() { - assert_eq!(A - B, - Matrix3::new(-1.0f64, -1.0f64, -1.0f64, - -1.0f64, -1.0f64, -1.0f64, - -1.0f64, -1.0f64, -1.0f64)); + assert_eq!( + A - B, + Matrix3::new( + -1.0f64, -1.0f64, -1.0f64, -1.0f64, -1.0f64, -1.0f64, -1.0f64, -1.0f64, -1.0f64 + ) + ); } #[test] @@ -281,10 +357,12 @@ pub mod matrix3 { #[test] fn test_mul_matrix() { - assert_eq!(A * B, - Matrix3::new(36.0f64, 81.0f64, 126.0f64, - 42.0f64, 96.0f64, 150.0f64, - 48.0f64, 111.0f64, 174.0f64)); + assert_eq!( + A * B, + Matrix3::new( + 36.0f64, 81.0f64, 126.0f64, 42.0f64, 96.0f64, 150.0f64, 48.0f64, 111.0f64, 174.0f64 + ) + ); assert_eq!(A * B, &A * &B); } @@ -313,10 +391,12 @@ pub mod matrix3 { #[test] fn test_transpose() { - assert_eq!(A.transpose(), - Matrix3::::new(1.0f64, 2.0f64, 3.0f64, - 4.0f64, 5.0f64, 6.0f64, - 7.0f64, 8.0f64, 9.0f64)); + assert_eq!( + A.transpose(), + Matrix3::::new( + 1.0f64, 2.0f64, 3.0f64, 4.0f64, 5.0f64, 6.0f64, 7.0f64, 8.0f64, 9.0f64 + ) + ); } #[test] @@ -332,10 +412,10 @@ pub mod matrix3 { assert_eq!(A.invert(), None); - assert_eq!(C.invert().unwrap(), - Matrix3::new(0.5f64, -1.0f64, 1.0f64, - 0.0f64, 0.5f64, -2.0f64, - 0.0f64, 0.0f64, 1.0f64)); + assert_eq!( + C.invert().unwrap(), + Matrix3::new(0.5f64, -1.0f64, 1.0f64, 0.0f64, 0.5f64, -2.0f64, 0.0f64, 0.0f64, 1.0f64) + ); } #[test] @@ -371,13 +451,26 @@ pub mod matrix3 { fn check_from_axis_angle_x(pitch: Rad) { let found = Matrix3::from_angle_x(pitch); - let expected = Matrix3::from(Euler { x: pitch, y: Rad(0.0), z: Rad(0.0) }); + let expected = Matrix3::from(Euler { + x: pitch, + y: Rad(0.0), + z: Rad(0.0), + }); assert_relative_eq!(found, expected, epsilon = 0.001); } - #[test] fn test_zero() { check_from_axis_angle_x(Rad(0.0)); } - #[test] fn test_pos_1() { check_from_axis_angle_x(Rad(1.0)); } - #[test] fn test_neg_1() { check_from_axis_angle_x(Rad(-1.0)); } + #[test] + fn test_zero() { + check_from_axis_angle_x(Rad(0.0)); + } + #[test] + fn test_pos_1() { + check_from_axis_angle_x(Rad(1.0)); + } + #[test] + fn test_neg_1() { + check_from_axis_angle_x(Rad(-1.0)); + } } mod from_axis_y { @@ -385,13 +478,26 @@ pub mod matrix3 { fn check_from_axis_angle_y(yaw: Rad) { let found = Matrix3::from_angle_y(yaw); - let expected = Matrix3::from(Euler { x: Rad(0.0), y: yaw, z: Rad(0.0) }); + let expected = Matrix3::from(Euler { + x: Rad(0.0), + y: yaw, + z: Rad(0.0), + }); assert_relative_eq!(found, expected, epsilon = 0.001); } - #[test] fn test_zero() { check_from_axis_angle_y(Rad(0.0)); } - #[test] fn test_pos_1() { check_from_axis_angle_y(Rad(1.0)); } - #[test] fn test_neg_1() { check_from_axis_angle_y(Rad(-1.0)); } + #[test] + fn test_zero() { + check_from_axis_angle_y(Rad(0.0)); + } + #[test] + fn test_pos_1() { + check_from_axis_angle_y(Rad(1.0)); + } + #[test] + fn test_neg_1() { + check_from_axis_angle_y(Rad(-1.0)); + } } mod from_axis_z { @@ -399,13 +505,26 @@ pub mod matrix3 { fn check_from_axis_angle_z(roll: Rad) { let found = Matrix3::from_angle_z(roll); - let expected = Matrix3::from(Euler { x: Rad(0.0), y: Rad(0.0), z: roll }); + let expected = Matrix3::from(Euler { + x: Rad(0.0), + y: Rad(0.0), + z: roll, + }); assert_relative_eq!(found, expected, epsilon = 0.001); } - #[test] fn test_zero() { check_from_axis_angle_z(Rad(0.0)); } - #[test] fn test_pos_1() { check_from_axis_angle_z(Rad(1.0)); } - #[test] fn test_neg_1() { check_from_axis_angle_z(Rad(-1.0)); } + #[test] + fn test_zero() { + check_from_axis_angle_z(Rad(0.0)); + } + #[test] + fn test_pos_1() { + check_from_axis_angle_z(Rad(1.0)); + } + #[test] + fn test_neg_1() { + check_from_axis_angle_z(Rad(-1.0)); + } } mod from_axis_angle { @@ -414,13 +533,26 @@ pub mod matrix3 { fn check_from_axis_angle_x(pitch: Rad) { let found = Matrix3::from_axis_angle(Vector3::unit_x(), pitch); - let expected = Matrix3::from(Euler { x: pitch, y: Rad(0.0), z: Rad(0.0) }); + let expected = Matrix3::from(Euler { + x: pitch, + y: Rad(0.0), + z: Rad(0.0), + }); assert_relative_eq!(found, expected, epsilon = 0.001); } - #[test] fn test_zero() { check_from_axis_angle_x(Rad(0.0)); } - #[test] fn test_pos_1() { check_from_axis_angle_x(Rad(1.0)); } - #[test] fn test_neg_1() { check_from_axis_angle_x(Rad(-1.0)); } + #[test] + fn test_zero() { + check_from_axis_angle_x(Rad(0.0)); + } + #[test] + fn test_pos_1() { + check_from_axis_angle_x(Rad(1.0)); + } + #[test] + fn test_neg_1() { + check_from_axis_angle_x(Rad(-1.0)); + } } mod axis_y { @@ -428,13 +560,26 @@ pub mod matrix3 { fn check_from_axis_angle_y(yaw: Rad) { let found = Matrix3::from_axis_angle(Vector3::unit_y(), yaw); - let expected = Matrix3::from(Euler { x: Rad(0.0), y: yaw, z: Rad(0.0) }); + let expected = Matrix3::from(Euler { + x: Rad(0.0), + y: yaw, + z: Rad(0.0), + }); assert_relative_eq!(found, expected, epsilon = 0.001); } - #[test] fn test_zero() { check_from_axis_angle_y(Rad(0.0)); } - #[test] fn test_pos_1() { check_from_axis_angle_y(Rad(1.0)); } - #[test] fn test_neg_1() { check_from_axis_angle_y(Rad(-1.0)); } + #[test] + fn test_zero() { + check_from_axis_angle_y(Rad(0.0)); + } + #[test] + fn test_pos_1() { + check_from_axis_angle_y(Rad(1.0)); + } + #[test] + fn test_neg_1() { + check_from_axis_angle_y(Rad(-1.0)); + } } mod axis_z { @@ -442,13 +587,26 @@ pub mod matrix3 { fn check_from_axis_angle_z(roll: Rad) { let found = Matrix3::from_axis_angle(Vector3::unit_z(), roll); - let expected = Matrix3::from(Euler { x: Rad(0.0), y: Rad(0.0), z: roll }); + let expected = Matrix3::from(Euler { + x: Rad(0.0), + y: Rad(0.0), + z: roll, + }); assert_relative_eq!(found, expected, epsilon = 0.001); } - #[test] fn test_zero() { check_from_axis_angle_z(Rad(0.0)); } - #[test] fn test_pos_1() { check_from_axis_angle_z(Rad(1.0)); } - #[test] fn test_neg_1() { check_from_axis_angle_z(Rad(-1.0)); } + #[test] + fn test_zero() { + check_from_axis_angle_z(Rad(0.0)); + } + #[test] + fn test_pos_1() { + check_from_axis_angle_z(Rad(1.0)); + } + #[test] + fn test_neg_1() { + check_from_axis_angle_z(Rad(-1.0)); + } } } @@ -488,7 +646,6 @@ pub mod matrix3 { assert_ulps_eq!(vec3(0.0, -1.0, 0.0), rot * vec); } - // tests that the Y rotation is done after the X #[test] fn test_x_then_y() { @@ -541,7 +698,10 @@ pub mod matrix3 { let vec = vec3(0.0, 0.0, 1.0); let rot = Matrix3::from_axis_angle(vec3(1.0, 1.0, 0.0).normalize(), Deg(90.0)); - assert_ulps_eq!(vec3(2.0f32.sqrt() / 2.0, -2.0f32.sqrt() / 2.0, 0.0), rot * vec); + assert_ulps_eq!( + vec3(2.0f32.sqrt() / 2.0, -2.0f32.sqrt() / 2.0, 0.0), + rot * vec + ); } #[test] @@ -549,7 +709,10 @@ pub mod matrix3 { let vec = vec3(1.0, 0.0, 0.0); let rot = Matrix3::from_axis_angle(vec3(0.0, 1.0, 1.0).normalize(), Deg(-90.0)); - assert_ulps_eq!(vec3(0.0, -2.0f32.sqrt() / 2.0, 2.0f32.sqrt() / 2.0), rot * vec); + assert_ulps_eq!( + vec3(0.0, -2.0f32.sqrt() / 2.0, 2.0f32.sqrt() / 2.0), + rot * vec + ); } #[test] @@ -557,7 +720,10 @@ pub mod matrix3 { let vec = vec3(0.0, 1.0, 0.0); let rot = Matrix3::from_axis_angle(vec3(1.0, 0.0, 1.0).normalize(), Deg(90.0)); - assert_ulps_eq!(vec3(-2.0f32.sqrt() / 2.0, 0.0, 2.0f32.sqrt() / 2.0), rot * vec); + assert_ulps_eq!( + vec3(-2.0f32.sqrt() / 2.0, 0.0, 2.0f32.sqrt() / 2.0), + rot * vec + ); } } } @@ -565,89 +731,196 @@ pub mod matrix3 { pub mod matrix4 { use cgmath::*; - const A: Matrix4 = Matrix4 { x: Vector4 { x: 1.0f64, y: 5.0f64, z: 9.0f64, w: 13.0f64 }, - y: Vector4 { x: 2.0f64, y: 6.0f64, z: 10.0f64, w: 14.0f64 }, - z: Vector4 { x: 3.0f64, y: 7.0f64, z: 11.0f64, w: 15.0f64 }, - w: Vector4 { x: 4.0f64, y: 8.0f64, z: 12.0f64, w: 16.0f64 } }; - const B: Matrix4 = Matrix4 { x: Vector4 { x: 2.0f64, y: 6.0f64, z: 10.0f64, w: 14.0f64 }, - y: Vector4 { x: 3.0f64, y: 7.0f64, z: 11.0f64, w: 15.0f64 }, - z: Vector4 { x: 4.0f64, y: 8.0f64, z: 12.0f64, w: 16.0f64 }, - w: Vector4 { x: 5.0f64, y: 9.0f64, z: 13.0f64, w: 17.0f64 } }; - const C: Matrix4 = Matrix4 { x: Vector4 { x: 3.0f64, y: 2.0f64, z: 1.0f64, w: 1.0f64 }, - y: Vector4 { x: 2.0f64, y: 3.0f64, z: 2.0f64, w: 2.0f64 }, - z: Vector4 { x: 1.0f64, y: 2.0f64, z: 3.0f64, w: 3.0f64 }, - w: Vector4 { x: 0.0f64, y: 1.0f64, z: 1.0f64, w: 0.0f64 } }; - const D: Matrix4 = Matrix4 { x: Vector4 { x: 4.0f64, y: 3.0f64, z: 2.0f64, w: 1.0f64 }, - y: Vector4 { x: 3.0f64, y: 4.0f64, z: 3.0f64, w: 2.0f64 }, - z: Vector4 { x: 2.0f64, y: 3.0f64, z: 4.0f64, w: 3.0f64 }, - w: Vector4 { x: 1.0f64, y: 2.0f64, z: 3.0f64, w: 4.0f64 } }; + const A: Matrix4 = Matrix4 { + x: Vector4 { + x: 1.0f64, + y: 5.0f64, + z: 9.0f64, + w: 13.0f64, + }, + y: Vector4 { + x: 2.0f64, + y: 6.0f64, + z: 10.0f64, + w: 14.0f64, + }, + z: Vector4 { + x: 3.0f64, + y: 7.0f64, + z: 11.0f64, + w: 15.0f64, + }, + w: Vector4 { + x: 4.0f64, + y: 8.0f64, + z: 12.0f64, + w: 16.0f64, + }, + }; + const B: Matrix4 = Matrix4 { + x: Vector4 { + x: 2.0f64, + y: 6.0f64, + z: 10.0f64, + w: 14.0f64, + }, + y: Vector4 { + x: 3.0f64, + y: 7.0f64, + z: 11.0f64, + w: 15.0f64, + }, + z: Vector4 { + x: 4.0f64, + y: 8.0f64, + z: 12.0f64, + w: 16.0f64, + }, + w: Vector4 { + x: 5.0f64, + y: 9.0f64, + z: 13.0f64, + w: 17.0f64, + }, + }; + const C: Matrix4 = Matrix4 { + x: Vector4 { + x: 3.0f64, + y: 2.0f64, + z: 1.0f64, + w: 1.0f64, + }, + y: Vector4 { + x: 2.0f64, + y: 3.0f64, + z: 2.0f64, + w: 2.0f64, + }, + z: Vector4 { + x: 1.0f64, + y: 2.0f64, + z: 3.0f64, + w: 3.0f64, + }, + w: Vector4 { + x: 0.0f64, + y: 1.0f64, + z: 1.0f64, + w: 0.0f64, + }, + }; + const D: Matrix4 = Matrix4 { + x: Vector4 { + x: 4.0f64, + y: 3.0f64, + z: 2.0f64, + w: 1.0f64, + }, + y: Vector4 { + x: 3.0f64, + y: 4.0f64, + z: 3.0f64, + w: 2.0f64, + }, + z: Vector4 { + x: 2.0f64, + y: 3.0f64, + z: 4.0f64, + w: 3.0f64, + }, + w: Vector4 { + x: 1.0f64, + y: 2.0f64, + z: 3.0f64, + w: 4.0f64, + }, + }; - const V: Vector4 = Vector4 { x: 1.0f64, y: 2.0f64, z: 3.0f64, w: 4.0f64 }; + const V: Vector4 = Vector4 { + x: 1.0f64, + y: 2.0f64, + z: 3.0f64, + w: 4.0f64, + }; const F: f64 = 0.5; #[test] fn test_neg() { - assert_eq!(-A, - Matrix4::new(-1.0f64, -5.0f64, -9.0f64, -13.0f64, - -2.0f64, -6.0f64, -10.0f64, -14.0f64, - -3.0f64, -7.0f64, -11.0f64, -15.0f64, - -4.0f64, -8.0f64, -12.0f64, -16.0f64)); + assert_eq!( + -A, + Matrix4::new( + -1.0f64, -5.0f64, -9.0f64, -13.0f64, -2.0f64, -6.0f64, -10.0f64, -14.0f64, -3.0f64, + -7.0f64, -11.0f64, -15.0f64, -4.0f64, -8.0f64, -12.0f64, -16.0f64 + ) + ); } #[test] fn test_mul_scalar() { - let result = Matrix4::new(0.5f64, 2.5f64, 4.5f64, 6.5f64, - 1.0f64, 3.0f64, 5.0f64, 7.0f64, - 1.5f64, 3.5f64, 5.5f64, 7.5f64, - 2.0f64, 4.0f64, 6.0f64, 8.0f64); + let result = Matrix4::new( + 0.5f64, 2.5f64, 4.5f64, 6.5f64, 1.0f64, 3.0f64, 5.0f64, 7.0f64, 1.5f64, 3.5f64, 5.5f64, + 7.5f64, 2.0f64, 4.0f64, 6.0f64, 8.0f64, + ); assert_eq!(A * F, result); assert_eq!(F * A, result); } #[test] fn test_div_scalar() { - assert_eq!(A / F, - Matrix4::new(2.0f64, 10.0f64, 18.0f64, 26.0f64, - 4.0f64, 12.0f64, 20.0f64, 28.0f64, - 6.0f64, 14.0f64, 22.0f64, 30.0f64, - 8.0f64, 16.0f64, 24.0f64, 32.0f64)); - assert_eq!(12.0f64 / D, - Matrix4::new( 3.0f64, 4.0f64, 6.0f64, 12.0f64, - 4.0f64, 3.0f64, 4.0f64, 6.0f64, - 6.0f64, 4.0f64, 3.0f64, 4.0f64, - 12.0f64, 6.0f64, 4.0f64, 3.0f64)); + assert_eq!( + A / F, + Matrix4::new( + 2.0f64, 10.0f64, 18.0f64, 26.0f64, 4.0f64, 12.0f64, 20.0f64, 28.0f64, 6.0f64, + 14.0f64, 22.0f64, 30.0f64, 8.0f64, 16.0f64, 24.0f64, 32.0f64 + ) + ); + assert_eq!( + 12.0f64 / D, + Matrix4::new( + 3.0f64, 4.0f64, 6.0f64, 12.0f64, 4.0f64, 3.0f64, 4.0f64, 6.0f64, 6.0f64, 4.0f64, + 3.0f64, 4.0f64, 12.0f64, 6.0f64, 4.0f64, 3.0f64 + ) + ); } #[test] fn test_rem_scalar() { - assert_eq!(A % 4.0f64, - Matrix4::new(1.0f64, 1.0f64, 1.0f64, 1.0f64, - 2.0f64, 2.0f64, 2.0f64, 2.0f64, - 3.0f64, 3.0f64, 3.0f64, 3.0f64, - 0.0f64, 0.0f64, 0.0f64, 0.0f64)); - assert_eq!(16.0f64 % A, - Matrix4::new(0.0f64, 1.0f64, 7.0f64, 3.0f64, - 0.0f64, 4.0f64, 6.0f64, 2.0f64, - 1.0f64, 2.0f64, 5.0f64, 1.0f64, - 0.0f64, 0.0f64, 4.0f64, 0.0f64)); + assert_eq!( + A % 4.0f64, + Matrix4::new( + 1.0f64, 1.0f64, 1.0f64, 1.0f64, 2.0f64, 2.0f64, 2.0f64, 2.0f64, 3.0f64, 3.0f64, + 3.0f64, 3.0f64, 0.0f64, 0.0f64, 0.0f64, 0.0f64 + ) + ); + assert_eq!( + 16.0f64 % A, + Matrix4::new( + 0.0f64, 1.0f64, 7.0f64, 3.0f64, 0.0f64, 4.0f64, 6.0f64, 2.0f64, 1.0f64, 2.0f64, + 5.0f64, 1.0f64, 0.0f64, 0.0f64, 4.0f64, 0.0f64 + ) + ); } #[test] fn test_add_matrix() { - assert_eq!(A + B, - Matrix4::new(3.0f64, 11.0f64, 19.0f64, 27.0f64, - 5.0f64, 13.0f64, 21.0f64, 29.0f64, - 7.0f64, 15.0f64, 23.0f64, 31.0f64, - 9.0f64, 17.0f64, 25.0f64, 33.0f64)); + assert_eq!( + A + B, + Matrix4::new( + 3.0f64, 11.0f64, 19.0f64, 27.0f64, 5.0f64, 13.0f64, 21.0f64, 29.0f64, 7.0f64, + 15.0f64, 23.0f64, 31.0f64, 9.0f64, 17.0f64, 25.0f64, 33.0f64 + ) + ); } #[test] fn test_sub_matrix() { - assert_eq!(A - B, - Matrix4::new(-1.0f64, -1.0f64, -1.0f64, -1.0f64, - -1.0f64, -1.0f64, -1.0f64, -1.0f64, - -1.0f64, -1.0f64, -1.0f64, -1.0f64, - -1.0f64, -1.0f64, -1.0f64, -1.0f64)); + assert_eq!( + A - B, + Matrix4::new( + -1.0f64, -1.0f64, -1.0f64, -1.0f64, -1.0f64, -1.0f64, -1.0f64, -1.0f64, -1.0f64, + -1.0f64, -1.0f64, -1.0f64, -1.0f64, -1.0f64, -1.0f64, -1.0f64 + ) + ); } #[test] @@ -657,11 +930,13 @@ pub mod matrix4 { #[test] fn test_mul_matrix() { - assert_eq!(A * B, - Matrix4::new(100.0f64, 228.0f64, 356.0f64, 484.0f64, - 110.0f64, 254.0f64, 398.0f64, 542.0f64, - 120.0f64, 280.0f64, 440.0f64, 600.0f64, - 130.0f64, 306.0f64, 482.0f64, 658.0f64)); + assert_eq!( + A * B, + Matrix4::new( + 100.0f64, 228.0f64, 356.0f64, 484.0f64, 110.0f64, 254.0f64, 398.0f64, 542.0f64, + 120.0f64, 280.0f64, 440.0f64, 600.0f64, 130.0f64, 306.0f64, 482.0f64, 658.0f64 + ) + ); assert_eq!(A * B, &A * &B); } @@ -690,11 +965,13 @@ pub mod matrix4 { #[test] fn test_transpose() { - assert_eq!(A.transpose(), - Matrix4::::new( 1.0f64, 2.0f64, 3.0f64, 4.0f64, - 5.0f64, 6.0f64, 7.0f64, 8.0f64, - 9.0f64, 10.0f64, 11.0f64, 12.0f64, - 13.0f64, 14.0f64, 15.0f64, 16.0f64)); + assert_eq!( + A.transpose(), + Matrix4::::new( + 1.0f64, 2.0f64, 3.0f64, 4.0f64, 5.0f64, 6.0f64, 7.0f64, 8.0f64, 9.0f64, 10.0f64, + 11.0f64, 12.0f64, 13.0f64, 14.0f64, 15.0f64, 16.0f64 + ) + ); } #[test] @@ -708,34 +985,92 @@ pub mod matrix4 { fn test_invert() { assert!(Matrix4::::identity().invert().unwrap().is_identity()); - assert_ulps_eq!(&C.invert().unwrap(), &( - Matrix4::new( 5.0f64, -4.0f64, 1.0f64, 0.0f64, - -4.0f64, 8.0f64, -4.0f64, 0.0f64, - 4.0f64, -8.0f64, 4.0f64, 8.0f64, - -3.0f64, 4.0f64, 1.0f64, -8.0f64) * 0.125f64)); + assert_ulps_eq!( + &C.invert().unwrap(), + &(Matrix4::new( + 5.0f64, -4.0f64, 1.0f64, 0.0f64, -4.0f64, 8.0f64, -4.0f64, 0.0f64, 4.0f64, -8.0f64, + 4.0f64, 8.0f64, -3.0f64, 4.0f64, 1.0f64, -8.0f64 + ) * 0.125f64) + ); - let mat_c = Matrix4::new(-0.131917f64, -0.76871f64, 0.625846f64, 0.0f64, - -0., 0.631364f64, 0.775487f64, 0.0f64, - -0.991261f64, 0.1023f64, -0.083287f64, 0.0f64, - 0., -1.262728f64, -1.550973f64, 1.0f64); + let mat_c = Matrix4::new( + -0.131917f64, + -0.76871f64, + 0.625846f64, + 0.0f64, + -0., + 0.631364f64, + 0.775487f64, + 0.0f64, + -0.991261f64, + 0.1023f64, + -0.083287f64, + 0.0f64, + 0., + -1.262728f64, + -1.550973f64, + 1.0f64, + ); assert!((mat_c.invert().unwrap() * mat_c).is_identity()); - let mat_d = Matrix4::new( 0.065455f64, -0.720002f64, 0.690879f64, 0.0f64, - -0., 0.692364f64, 0.721549f64, 0.0f64, - -0.997856f64, -0.047229f64, 0.045318f64, 0.0f64, - 0., -1.384727f64, -1.443098f64, 1.0f64); + let mat_d = Matrix4::new( + 0.065455f64, + -0.720002f64, + 0.690879f64, + 0.0f64, + -0., + 0.692364f64, + 0.721549f64, + 0.0f64, + -0.997856f64, + -0.047229f64, + 0.045318f64, + 0.0f64, + 0., + -1.384727f64, + -1.443098f64, + 1.0f64, + ); assert!((mat_d.invert().unwrap() * mat_d).is_identity()); - let mat_e = Matrix4::new( 0.409936f64, 0.683812f64, -0.603617f64, 0.0f64, - 0., 0.661778f64, 0.7497f64, 0.0f64, - 0.912114f64, -0.307329f64, 0.271286f64, 0.0f64, - -0., -1.323555f64, -1.499401f64, 1.0f64); + let mat_e = Matrix4::new( + 0.409936f64, + 0.683812f64, + -0.603617f64, + 0.0f64, + 0., + 0.661778f64, + 0.7497f64, + 0.0f64, + 0.912114f64, + -0.307329f64, + 0.271286f64, + 0.0f64, + -0., + -1.323555f64, + -1.499401f64, + 1.0f64, + ); assert!((mat_e.invert().unwrap() * mat_e).is_identity()); - let mat_f = Matrix4::new(-0.160691f64, -0.772608f64, 0.614211f64, 0.0f64, - -0., 0.622298f64, 0.78278f64, 0.0f64, - -0.987005f64, 0.125786f64, -0.099998f64, 0.0f64, - 0., -1.244597f64, -1.565561f64, 1.0f64); + let mat_f = Matrix4::new( + -0.160691f64, + -0.772608f64, + 0.614211f64, + 0.0f64, + -0., + 0.622298f64, + 0.78278f64, + 0.0f64, + -0.987005f64, + 0.125786f64, + -0.099998f64, + 0.0f64, + 0., + -1.244597f64, + -1.565561f64, + 1.0f64, + ); assert!((mat_f.invert().unwrap() * mat_f).is_identity()); } @@ -769,29 +1104,27 @@ pub mod matrix4 { #[test] fn test_cast() { - assert_ulps_eq!(Matrix2::new(0.2f64, 1.5, 4.7, 2.3).cast().unwrap(), Matrix2::new(0.2f32, 1.5, 4.7, 2.3)); - assert_ulps_eq!(Matrix3::new( - 0.2f64, 1.5, 4.7, - 2.3, 5.7, 2.1, - 4.6, 5.2, 6.6, - ).cast().unwrap(), Matrix3::new( - 0.2f32, 1.5, 4.7, - 2.3, 5.7, 2.1, - 4.6, 5.2, 6.6, - )); - - assert_ulps_eq!(Matrix4::new( - 0.2f64, 1.5, 4.7, 2.5, - 2.3, 5.7, 2.1, 1.1, - 4.6, 5.2, 6.6, 0.2, - 3.2, 1.8, 0.4, 2.9, - ).cast().unwrap(), Matrix4::new( - 0.2f32, 1.5, 4.7, 2.5, - 2.3, 5.7, 2.1, 1.1, - 4.6, 5.2, 6.6, 0.2, - 3.2, 1.8, 0.4, 2.9, - )); + assert_ulps_eq!( + Matrix2::new(0.2f64, 1.5, 4.7, 2.3).cast().unwrap(), + Matrix2::new(0.2f32, 1.5, 4.7, 2.3) + ); + assert_ulps_eq!( + Matrix3::new(0.2f64, 1.5, 4.7, 2.3, 5.7, 2.1, 4.6, 5.2, 6.6,) + .cast() + .unwrap(), + Matrix3::new(0.2f32, 1.5, 4.7, 2.3, 5.7, 2.1, 4.6, 5.2, 6.6,) + ); + assert_ulps_eq!( + Matrix4::new( + 0.2f64, 1.5, 4.7, 2.5, 2.3, 5.7, 2.1, 1.1, 4.6, 5.2, 6.6, 0.2, 3.2, 1.8, 0.4, 2.9, + ) + .cast() + .unwrap(), + Matrix4::new( + 0.2f32, 1.5, 4.7, 2.5, 2.3, 5.7, 2.1, 1.1, 4.6, 5.2, 6.6, 0.2, 3.2, 1.8, 0.4, 2.9, + ) + ); } mod from { diff --git a/tests/quaternion.rs b/tests/quaternion.rs index e831d2c..daddb91 100644 --- a/tests/quaternion.rs +++ b/tests/quaternion.rs @@ -17,7 +17,7 @@ extern crate approx; extern crate cgmath; macro_rules! impl_test_mul { - ($s:expr, $v:expr) => ( + ($s:expr, $v:expr) => { // point * scalar ops assert_eq!($v * $s, Quaternion::from_sv($v.s * $s, $v.v * $s)); assert_eq!($s * $v, Quaternion::from_sv($s * $v.s, $s * $v.v)); @@ -25,17 +25,17 @@ macro_rules! impl_test_mul { assert_eq!($s * &$v, $s * $v); // commutativity assert_eq!($v * $s, $s * $v); - ) + }; } macro_rules! impl_test_div { - ($s:expr, $v:expr) => ( + ($s:expr, $v:expr) => { // point / scalar ops assert_eq!($v / $s, Quaternion::from_sv($v.s / $s, $v.v / $s)); assert_eq!($s / $v, Quaternion::from_sv($s / $v.s, $s / $v.v)); assert_eq!(&$v / $s, $v / $s); assert_eq!($s / &$v, $s / $v); - ) + }; } mod operators { diff --git a/tests/transform.rs b/tests/transform.rs index 7e0863a..7670bc1 100644 --- a/tests/transform.rs +++ b/tests/transform.rs @@ -29,7 +29,8 @@ fn test_invert() { rot: Quaternion::new(0.5f64, 0.5, 0.5, 0.5), disp: Vector3::new(6.0f64, -7.0, 8.0), }; - let ti = t.inverse_transform() + let ti = t + .inverse_transform() .expect("Expected successful inversion"); let vt = t.transform_vector(v); assert_ulps_eq!(&v, &ti.transform_vector(vt)); @@ -43,7 +44,8 @@ fn test_inverse_vector() { rot: Quaternion::new(0.5f64, 0.5, 0.5, 0.5), disp: Vector3::new(6.0f64, -7.0, 8.0), }; - let vt = t.inverse_transform_vector(v) + let vt = t + .inverse_transform_vector(v) .expect("Expected successful inversion"); assert_ulps_eq!(v, t.transform_vector(vt)); } diff --git a/tests/vector.rs b/tests/vector.rs index 6acd6a1..e602932 100644 --- a/tests/vector.rs +++ b/tests/vector.rs @@ -212,14 +212,8 @@ fn test_is_perpendicular() { assert!( Vector3::new(0.0f64, 1.0f64, 0.0f64).is_perpendicular(Vector3::new(0.0f64, 0.0f64, 1.0f64)) ); - assert!( - Vector4::new(1.0f64, 0.0f64, 0.0f64, 0.0f64).is_perpendicular(Vector4::new( - 0.0f64, - 0.0f64, - 0.0f64, - 1.0f64 - )) - ); + assert!(Vector4::new(1.0f64, 0.0f64, 0.0f64, 0.0f64) + .is_perpendicular(Vector4::new(0.0f64, 0.0f64, 0.0f64, 1.0f64))); } #[cfg(test)] @@ -292,30 +286,18 @@ fn test_angle() { ); assert_ulps_eq!( - Vector4::new(1.0f64, 0.0f64, 1.0f64, 0.0f64).angle(Vector4::new( - 0.0f64, - 1.0f64, - 0.0f64, - 1.0f64 - )), + Vector4::new(1.0f64, 0.0f64, 1.0f64, 0.0f64) + .angle(Vector4::new(0.0f64, 1.0f64, 0.0f64, 1.0f64)), &Rad(f64::consts::FRAC_PI_2) ); assert_ulps_eq!( - Vector4::new(10.0f64, 0.0f64, 10.0f64, 0.0f64).angle(Vector4::new( - 0.0f64, - 5.0f64, - 0.0f64, - 5.0f64 - )), + Vector4::new(10.0f64, 0.0f64, 10.0f64, 0.0f64) + .angle(Vector4::new(0.0f64, 5.0f64, 0.0f64, 5.0f64)), &Rad(f64::consts::FRAC_PI_2) ); assert_ulps_eq!( - Vector4::new(-1.0f64, 0.0f64, -1.0f64, 0.0f64).angle(Vector4::new( - 0.0f64, - 1.0f64, - 0.0f64, - 1.0f64 - )), + Vector4::new(-1.0f64, 0.0f64, -1.0f64, 0.0f64) + .angle(Vector4::new(0.0f64, 1.0f64, 0.0f64, 1.0f64)), &Rad(f64::consts::FRAC_PI_2) ); } diff --git a/tests/vector4f32.rs b/tests/vector4f32.rs index ec10445..ad746c5 100644 --- a/tests/vector4f32.rs +++ b/tests/vector4f32.rs @@ -135,12 +135,8 @@ fn test_rem() { #[test] fn test_dot() { assert_eq!( - Vector4::new(1.0f32, 2.0f32, 3.0f32, 4.0f32).dot(Vector4::new( - 5.0f32, - 6.0f32, - 7.0f32, - 8.0f32 - )), + Vector4::new(1.0f32, 2.0f32, 3.0f32, 4.0f32) + .dot(Vector4::new(5.0f32, 6.0f32, 7.0f32, 8.0f32)), 70.0f32 ); } @@ -164,14 +160,8 @@ fn test_product() { #[test] fn test_is_perpendicular() { - assert!( - Vector4::new(1.0f32, 0.0f32, 0.0f32, 0.0f32).is_perpendicular(Vector4::new( - 0.0f32, - 0.0f32, - 0.0f32, - 1.0f32 - )) - ); + assert!(Vector4::new(1.0f32, 0.0f32, 0.0f32, 0.0f32) + .is_perpendicular(Vector4::new(0.0f32, 0.0f32, 0.0f32, 1.0f32))); } #[cfg(test)] @@ -210,30 +200,18 @@ mod test_magnitude { #[test] fn test_angle() { assert_ulps_eq!( - Vector4::new(1.0f32, 0.0f32, 1.0f32, 0.0f32).angle(Vector4::new( - 0.0f32, - 1.0f32, - 0.0f32, - 1.0f32 - )), + Vector4::new(1.0f32, 0.0f32, 1.0f32, 0.0f32) + .angle(Vector4::new(0.0f32, 1.0f32, 0.0f32, 1.0f32)), &Rad(f32::consts::FRAC_PI_2) ); assert_ulps_eq!( - Vector4::new(10.0f32, 0.0f32, 10.0f32, 0.0f32).angle(Vector4::new( - 0.0f32, - 5.0f32, - 0.0f32, - 5.0f32 - )), + Vector4::new(10.0f32, 0.0f32, 10.0f32, 0.0f32) + .angle(Vector4::new(0.0f32, 5.0f32, 0.0f32, 5.0f32)), &Rad(f32::consts::FRAC_PI_2) ); assert_ulps_eq!( - Vector4::new(-1.0f32, 0.0f32, -1.0f32, 0.0f32).angle(Vector4::new( - 0.0f32, - 1.0f32, - 0.0f32, - 1.0f32 - )), + Vector4::new(-1.0f32, 0.0f32, -1.0f32, 0.0f32) + .angle(Vector4::new(0.0f32, 1.0f32, 0.0f32, 1.0f32)), &Rad(f32::consts::FRAC_PI_2) ); }