From 19a833debdd272ca90d90134f469ca9020a2e9b3 Mon Sep 17 00:00:00 2001 From: Pyfisch Date: Sun, 14 Jan 2018 13:54:54 +0100 Subject: [PATCH] Run rustfmt on entire codebase --- src/approxeq.rs | 1 - src/length.rs | 60 ++++--- src/lib.rs | 19 +- src/num.rs | 22 ++- src/point.rs | 147 +++++++++------- src/rect.rs | 143 ++++++++------- src/rotation.rs | 415 +++++++++++++++++++++++++++++++------------- src/scale.rs | 44 +++-- src/side_offsets.rs | 59 ++++--- src/size.rs | 52 +++--- src/trig.rs | 2 - src/vector.rs | 186 +++++++++++++------- 12 files changed, 742 insertions(+), 408 deletions(-) diff --git a/src/approxeq.rs b/src/approxeq.rs index 81f4beb..9fb11cc 100644 --- a/src/approxeq.rs +++ b/src/approxeq.rs @@ -7,7 +7,6 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. - /// Trait for testing approximate equality pub trait ApproxEq { fn approx_epsilon() -> Eps; diff --git a/src/length.rs b/src/length.rs index 1c9add0..1d59097 100644 --- a/src/length.rs +++ b/src/length.rs @@ -15,8 +15,8 @@ use num_traits::{NumCast, Saturating}; use num::One; use serde::{Deserialize, Deserializer, Serialize, Serializer}; use std::cmp::Ordering; -use std::ops::{Add, Sub, Mul, Div, Neg}; -use std::ops::{AddAssign, SubAssign, MulAssign, DivAssign}; +use std::ops::{Add, Div, Mul, Neg, Sub}; +use std::ops::{AddAssign, DivAssign, MulAssign, SubAssign}; use std::marker::PhantomData; use std::fmt; @@ -42,15 +42,29 @@ impl Clone for Length { impl Copy for Length {} -impl<'de, Unit, T> Deserialize<'de> for Length where T: Deserialize<'de> { +impl<'de, Unit, T> Deserialize<'de> for Length +where + T: Deserialize<'de>, +{ fn deserialize(deserializer: D) -> Result - where D: Deserializer<'de> { - Ok(Length(try!(Deserialize::deserialize(deserializer)), PhantomData)) + where + D: Deserializer<'de>, + { + Ok(Length( + try!(Deserialize::deserialize(deserializer)), + PhantomData, + )) } } -impl Serialize for Length where T: Serialize { - fn serialize(&self, serializer: S) -> Result where S: Serializer { +impl Serialize for Length +where + T: Serialize, +{ + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { self.0.serialize(serializer) } } @@ -80,7 +94,7 @@ impl fmt::Display for Length { } // length + length -impl> Add for Length { +impl> Add for Length { type Output = Length; fn add(self, other: Length) -> Length { Length::new(self.get() + other.get()) @@ -95,7 +109,7 @@ impl> AddAssign for Length { } // length - length -impl> Sub> for Length { +impl> Sub> for Length { type Output = Length; fn sub(self, other: Length) -> ::Output { Length::new(self.get() - other.get()) @@ -121,7 +135,7 @@ impl Saturating for Length { } // length / length -impl> Div> for Length { +impl> Div> for Length { type Output = TypedScale; #[inline] fn div(self, other: Length) -> TypedScale { @@ -130,7 +144,7 @@ impl> Div> for Length, U> Mul for Length { +impl, U> Mul for Length { type Output = Self; #[inline] fn mul(self, scale: T) -> Self { @@ -139,7 +153,7 @@ impl, U> Mul for Length { } // length *= scalar -impl, U> MulAssign for Length { +impl, U> MulAssign for Length { #[inline] fn mul_assign(&mut self, scale: T) { *self = *self * scale @@ -147,7 +161,7 @@ impl, U> MulAssign for Length { } // length / scalar -impl, U> Div for Length { +impl, U> Div for Length { type Output = Self; #[inline] fn div(self, scale: T) -> Self { @@ -156,7 +170,7 @@ impl, U> Div for Length { } // length /= scalar -impl, U> DivAssign for Length { +impl, U> DivAssign for Length { #[inline] fn div_assign(&mut self, scale: T) { *self = *self / scale @@ -164,7 +178,7 @@ impl, U> DivAssign for Length { } // length * scaleFactor -impl> Mul> for Length { +impl> Mul> for Length { type Output = Length; #[inline] fn mul(self, scale: TypedScale) -> Length { @@ -173,7 +187,7 @@ impl> Mul> for Len } // length / scaleFactor -impl> Div> for Length { +impl> Div> for Length { type Output = Length; #[inline] fn div(self, scale: TypedScale) -> Length { @@ -182,7 +196,7 @@ impl> Div> for Len } // -length -impl > Neg for Length { +impl> Neg for Length { type Output = Length; #[inline] fn neg(self) -> Length { @@ -198,7 +212,9 @@ impl Length { } impl PartialEq for Length { - fn eq(&self, other: &Self) -> bool { self.get().eq(&other.get()) } + fn eq(&self, other: &Self) -> bool { + self.get().eq(&other.get()) + } } impl PartialOrd for Length { @@ -210,7 +226,9 @@ impl PartialOrd for Length { impl Eq for Length {} impl Ord for Length { - fn cmp(&self, other: &Self) -> Ordering { self.get().cmp(&other.get()) } + fn cmp(&self, other: &Self) -> Ordering { + self.get().cmp(&other.get()) + } } impl Zero for Length { @@ -220,7 +238,9 @@ impl Zero for Length { } impl Length -where T: Copy + One + Add + Sub + Mul { +where + T: Copy + One + Add + Sub + Mul, +{ /// Linearly interpolate between this length and another length. /// /// `t` is expected to be between zero and one. diff --git a/src/lib.rs b/src/lib.rs index c99cd80..6149bac 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -59,29 +59,24 @@ extern crate log; extern crate serde; +extern crate num_traits; #[cfg(test)] extern crate rand; #[cfg(feature = "unstable")] extern crate test; -extern crate num_traits; pub use length::Length; pub use scale::TypedScale; pub use transform2d::{Transform2D, TypedTransform2D}; pub use transform3d::{Transform3D, TypedTransform3D}; -pub use point::{ - Point2D, TypedPoint2D, point2, - Point3D, TypedPoint3D, point3, -}; -pub use vector::{ - Vector2D, TypedVector2D, vec2, - Vector3D, TypedVector3D, vec3, -}; +pub use point::{Point2D, Point3D, TypedPoint2D, TypedPoint3D, point2, point3}; +pub use vector::{TypedVector2D, TypedVector3D, Vector2D, Vector3D, vec2, vec3}; -pub use rect::{Rect, TypedRect, rect}; -pub use rotation::{TypedRotation2D, Rotation2D, TypedRotation3D, Rotation3D, Angle}; +pub use rect::{rect, Rect, TypedRect}; +pub use rotation::{Angle, Rotation2D, Rotation3D, TypedRotation2D, TypedRotation3D}; pub use side_offsets::{SideOffsets2D, TypedSideOffsets2D}; -#[cfg(feature = "unstable")] pub use side_offsets::SideOffsets2DSimdI32; +#[cfg(feature = "unstable")] +pub use side_offsets::SideOffsets2DSimdI32; pub use size::{Size2D, TypedSize2D, size2}; pub use trig::Trig; diff --git a/src/num.rs b/src/num.rs index 5b57a19..1df86d7 100644 --- a/src/num.rs +++ b/src/num.rs @@ -10,13 +10,14 @@ use num_traits; - pub trait Zero { fn zero() -> Self; } impl Zero for T { - fn zero() -> T { num_traits::Zero::zero() } + fn zero() -> T { + num_traits::Zero::zero() + } } pub trait One { @@ -24,13 +25,20 @@ pub trait One { } impl One for T { - fn one() -> T { num_traits::One::one() } + fn one() -> T { + num_traits::One::one() + } } - -pub trait Round : Copy { fn round(self) -> Self; } -pub trait Floor : Copy { fn floor(self) -> Self; } -pub trait Ceil : Copy { fn ceil(self) -> Self; } +pub trait Round: Copy { + fn round(self) -> Self; +} +pub trait Floor: Copy { + fn floor(self) -> Self; +} +pub trait Ceil: Copy { + fn ceil(self) -> Self; +} macro_rules! num_int { ($ty:ty) => ( diff --git a/src/point.rs b/src/point.rs index a2a2fe9..23ba2ae 100644 --- a/src/point.rs +++ b/src/point.rs @@ -16,7 +16,7 @@ use num::*; use num_traits::{Float, NumCast}; use vector::{TypedVector2D, TypedVector3D, vec2, vec3}; use std::fmt; -use std::ops::{Add, Mul, Sub, Div, AddAssign, SubAssign, MulAssign, DivAssign}; +use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign}; use std::marker::PhantomData; define_matrix! { @@ -67,7 +67,11 @@ impl TypedPoint2D { /// Constructor taking scalar values directly. #[inline] pub fn new(x: T, y: T) -> Self { - TypedPoint2D { x: x, y: y, _unit: PhantomData } + TypedPoint2D { + x: x, + y: y, + _unit: PhantomData, + } } /// Constructor taking properly typed Lengths instead of scalar values. @@ -98,11 +102,15 @@ impl TypedPoint2D { /// Returns self.x as a Length carrying the unit. #[inline] - pub fn x_typed(&self) -> Length { Length::new(self.x) } + pub fn x_typed(&self) -> Length { + Length::new(self.x) + } /// Returns self.y as a Length carrying the unit. #[inline] - pub fn y_typed(&self) -> Length { Length::new(self.y) } + pub fn y_typed(&self) -> Length { + Length::new(self.y) + } /// Drop the units, preserving only the numeric value. #[inline] @@ -122,14 +130,14 @@ impl TypedPoint2D { } } -impl, U> TypedPoint2D { +impl, U> TypedPoint2D { #[inline] pub fn add_size(&self, other: &TypedSize2D) -> Self { point2(self.x + other.width, self.y + other.height) } } -impl, U> Add> for TypedPoint2D { +impl, U> Add> for TypedPoint2D { type Output = Self; #[inline] fn add(self, other: TypedSize2D) -> Self { @@ -137,21 +145,21 @@ impl, U> Add> for TypedPoint2D, U> AddAssign> for TypedPoint2D { +impl, U> AddAssign> for TypedPoint2D { #[inline] fn add_assign(&mut self, other: TypedVector2D) { *self = *self + other } } -impl, U> SubAssign> for TypedPoint2D { +impl, U> SubAssign> for TypedPoint2D { #[inline] fn sub_assign(&mut self, other: TypedVector2D) { *self = *self - other } } -impl, U> Add> for TypedPoint2D { +impl, U> Add> for TypedPoint2D { type Output = Self; #[inline] fn add(self, other: TypedVector2D) -> Self { @@ -159,7 +167,7 @@ impl, U> Add> for TypedPoint2D, U> Sub for TypedPoint2D { +impl, U> Sub for TypedPoint2D { type Output = TypedVector2D; #[inline] fn sub(self, other: Self) -> TypedVector2D { @@ -167,7 +175,7 @@ impl, U> Sub for TypedPoint2D { } } -impl, U> Sub> for TypedPoint2D { +impl, U> Sub> for TypedPoint2D { type Output = Self; #[inline] fn sub(self, other: TypedVector2D) -> Self { @@ -178,7 +186,7 @@ impl, U> Sub> for TypedPoint2D TypedPoint2D { #[inline] pub fn min(self, other: Self) -> Self { - point2(self.x.min(other.x), self.y.min(other.y)) + point2(self.x.min(other.x), self.y.min(other.y)) } #[inline] @@ -187,7 +195,7 @@ impl TypedPoint2D { } } -impl, U> Mul for TypedPoint2D { +impl, U> Mul for TypedPoint2D { type Output = Self; #[inline] fn mul(self, scale: T) -> Self { @@ -195,14 +203,14 @@ impl, U> Mul for TypedPoint2D { } } -impl, U> MulAssign for TypedPoint2D { +impl, U> MulAssign for TypedPoint2D { #[inline] fn mul_assign(&mut self, scale: T) { *self = *self * scale } } -impl, U> Div for TypedPoint2D { +impl, U> Div for TypedPoint2D { type Output = Self; #[inline] fn div(self, scale: T) -> Self { @@ -210,14 +218,14 @@ impl, U> Div for TypedPoint2D { } } -impl, U> DivAssign for TypedPoint2D { +impl, U> DivAssign for TypedPoint2D { #[inline] fn div_assign(&mut self, scale: T) { *self = *self / scale } } -impl, U1, U2> Mul> for TypedPoint2D { +impl, U1, U2> Mul> for TypedPoint2D { type Output = TypedPoint2D; #[inline] fn mul(self, scale: TypedScale) -> TypedPoint2D { @@ -225,7 +233,7 @@ impl, U1, U2> Mul> for TypedPoi } } -impl, U1, U2> Div> for TypedPoint2D { +impl, U1, U2> Div> for TypedPoint2D { type Output = TypedPoint2D; #[inline] fn div(self, scale: TypedScale) -> TypedPoint2D { @@ -279,7 +287,7 @@ impl TypedPoint2D { pub fn cast(&self) -> Option> { match (NumCast::from(self.x), NumCast::from(self.y)) { (Some(x), Some(y)) => Some(point2(x, y)), - _ => None + _ => None, } } @@ -329,21 +337,20 @@ impl TypedPoint2D { } impl TypedPoint2D -where T: Copy + One + Add + Sub + Mul { +where + T: Copy + One + Add + Sub + Mul, +{ /// Linearly interpolate between this point and another point. /// /// `t` is expected to be between zero and one. #[inline] pub fn lerp(&self, other: Self, t: T) -> Self { let one_t = T::one() - t; - point2( - one_t * self.x + t * other.x, - one_t * self.y + t * other.y, - ) + point2(one_t * self.x + t * other.x, one_t * self.y + t * other.y) } } -impl, U> ApproxEq> for TypedPoint2D { +impl, U> ApproxEq> for TypedPoint2D { #[inline] fn approx_epsilon() -> Self { point2(T::approx_epsilon(), T::approx_epsilon()) @@ -372,7 +379,6 @@ impl From<[T; 2]> for TypedPoint2D { } } - define_matrix! { /// A 3d Point tagged with a unit. pub struct TypedPoint3D { @@ -403,7 +409,9 @@ impl TypedPoint3D { } impl TypedPoint3D -where T: Copy + One + Add + Sub + Mul { +where + T: Copy + One + Add + Sub + Mul, +{ /// Linearly interpolate between this point and another point. /// /// `t` is expected to be between zero and one. @@ -434,7 +442,12 @@ impl TypedPoint3D { /// Constructor taking scalar values directly. #[inline] pub fn new(x: T, y: T, z: T) -> Self { - TypedPoint3D { x: x, y: y, z: z, _unit: PhantomData } + TypedPoint3D { + x: x, + y: y, + z: z, + _unit: PhantomData, + } } /// Constructor taking properly typed Lengths instead of scalar values. @@ -471,18 +484,26 @@ impl TypedPoint3D { /// Returns self.x as a Length carrying the unit. #[inline] - pub fn x_typed(&self) -> Length { Length::new(self.x) } + pub fn x_typed(&self) -> Length { + Length::new(self.x) + } /// Returns self.y as a Length carrying the unit. #[inline] - pub fn y_typed(&self) -> Length { Length::new(self.y) } + pub fn y_typed(&self) -> Length { + Length::new(self.y) + } /// Returns self.z as a Length carrying the unit. #[inline] - pub fn z_typed(&self) -> Length { Length::new(self.z) } + pub fn z_typed(&self) -> Length { + Length::new(self.z) + } #[inline] - pub fn to_array(&self) -> [T; 3] { [self.x, self.y, self.z] } + pub fn to_array(&self) -> [T; 3] { + [self.x, self.y, self.z] + } /// Drop the units, preserving only the numeric value. #[inline] @@ -503,21 +524,21 @@ impl TypedPoint3D { } } -impl, U> AddAssign> for TypedPoint3D { +impl, U> AddAssign> for TypedPoint3D { #[inline] fn add_assign(&mut self, other: TypedVector3D) { *self = *self + other } } -impl, U> SubAssign> for TypedPoint3D { +impl, U> SubAssign> for TypedPoint3D { #[inline] fn sub_assign(&mut self, other: TypedVector3D) { *self = *self - other } } -impl, U> Add> for TypedPoint3D { +impl, U> Add> for TypedPoint3D { type Output = Self; #[inline] fn add(self, other: TypedVector3D) -> Self { @@ -525,7 +546,7 @@ impl, U> Add> for TypedPoint3D, U> Sub for TypedPoint3D { +impl, U> Sub for TypedPoint3D { type Output = TypedVector3D; #[inline] fn sub(self, other: Self) -> TypedVector3D { @@ -533,7 +554,7 @@ impl, U> Sub for TypedPoint3D { } } -impl, U> Sub> for TypedPoint3D { +impl, U> Sub> for TypedPoint3D { type Output = Self; #[inline] fn sub(self, other: TypedVector3D) -> Self { @@ -541,7 +562,7 @@ impl, U> Sub> for TypedPoint3D, U> Mul for TypedPoint3D { +impl, U> Mul for TypedPoint3D { type Output = Self; #[inline] fn mul(self, scale: T) -> Self { @@ -549,7 +570,7 @@ impl, U> Mul for TypedPoint3D { } } -impl, U> Div for TypedPoint3D { +impl, U> Div for TypedPoint3D { type Output = Self; #[inline] fn div(self, scale: T) -> Self { @@ -560,12 +581,20 @@ impl, U> Div for TypedPoint3D { impl TypedPoint3D { #[inline] pub fn min(self, other: Self) -> Self { - point3(self.x.min(other.x), self.y.min(other.y), self.z.min(other.z)) + point3( + self.x.min(other.x), + self.y.min(other.y), + self.z.min(other.z), + ) } #[inline] pub fn max(self, other: Self) -> Self { - point3(self.x.max(other.x), self.y.max(other.y), self.z.max(other.z)) + point3( + self.x.max(other.x), + self.y.max(other.y), + self.z.max(other.z), + ) } } @@ -610,11 +639,13 @@ impl TypedPoint3D { /// geometrically. Consider using `round()`, `ceil()` or `floor()` before casting. #[inline] pub fn cast(&self) -> Option> { - match (NumCast::from(self.x), - NumCast::from(self.y), - NumCast::from(self.z)) { + match ( + NumCast::from(self.x), + NumCast::from(self.y), + NumCast::from(self.z), + ) { (Some(x), Some(y), Some(z)) => Some(point3(x, y, z)), - _ => None + _ => None, } } @@ -663,23 +694,24 @@ impl TypedPoint3D { } } -impl, U> ApproxEq> for TypedPoint3D { +impl, U> ApproxEq> for TypedPoint3D { #[inline] fn approx_epsilon() -> Self { - point3(T::approx_epsilon(), T::approx_epsilon(), T::approx_epsilon()) + point3( + T::approx_epsilon(), + T::approx_epsilon(), + T::approx_epsilon(), + ) } #[inline] fn approx_eq(&self, other: &Self) -> bool { - self.x.approx_eq(&other.x) - && self.y.approx_eq(&other.y) - && self.z.approx_eq(&other.z) + self.x.approx_eq(&other.x) && self.y.approx_eq(&other.y) && self.z.approx_eq(&other.z) } #[inline] fn approx_eq_eps(&self, other: &Self, eps: &Self) -> bool { - self.x.approx_eq_eps(&other.x, &eps.x) - && self.y.approx_eq_eps(&other.y, &eps.y) + self.x.approx_eq_eps(&other.x, &eps.x) && self.y.approx_eq_eps(&other.y, &eps.y) && self.z.approx_eq_eps(&other.z, &eps.z) } } @@ -696,7 +728,6 @@ impl From<[T; 3]> for TypedPoint3D { } } - pub fn point2(x: T, y: T) -> TypedPoint2D { TypedPoint2D::new(x, y) } @@ -741,7 +772,7 @@ mod point2d { #[cfg(test)] mod typedpoint2d { - use super::{TypedPoint2D, Point2D, point2}; + use super::{Point2D, TypedPoint2D, point2}; use scale::TypedScale; use vector::vec2; @@ -785,8 +816,8 @@ mod typedpoint2d { for i in 0..100 { // We don't care about these values as long as they are not the same. - let x = i as f32 *0.012345; - let y = i as f32 *0.987654; + let x = i as f32 * 0.012345; + let y = i as f32 * 0.987654; let p: Point2D = point2(x, y); assert_eq!(p.to_vector().to_point(), p); } @@ -828,8 +859,8 @@ mod point3d { use point3; for i in 0..100 { // We don't care about these values as long as they are not the same. - let x = i as f32 *0.012345; - let y = i as f32 *0.987654; + let x = i as f32 * 0.012345; + let y = i as f32 * 0.987654; let z = x * y; let p: Point3D = point3(x, y, z); assert_eq!(p.to_vector().to_point(), p); diff --git a/src/rect.rs b/src/rect.rs index fc86480..3e1e00d 100644 --- a/src/rect.rs +++ b/src/rect.rs @@ -20,7 +20,7 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer}; use std::cmp::PartialOrd; use std::fmt; use std::hash::{Hash, Hasher}; -use std::ops::{Add, Sub, Mul, Div}; +use std::ops::{Add, Div, Mul, Sub}; /// A 2d Rectangle optionally tagged with a unit. #[repr(C)] @@ -34,7 +34,8 @@ pub type Rect = TypedRect; impl<'de, T: Copy + Deserialize<'de>, U> Deserialize<'de> for TypedRect { fn deserialize(deserializer: D) -> Result - where D: Deserializer<'de> + where + D: Deserializer<'de>, { let (origin, size) = try!(Deserialize::deserialize(deserializer)); Ok(TypedRect::new(origin, size)) @@ -43,14 +44,14 @@ impl<'de, T: Copy + Deserialize<'de>, U> Deserialize<'de> for TypedRect { impl Serialize for TypedRect { fn serialize(&self, serializer: S) -> Result - where S: Serializer + where + S: Serializer, { (&self.origin, &self.size).serialize(serializer) } } -impl Hash for TypedRect -{ +impl Hash for TypedRect { fn hash(&self, h: &mut H) { self.origin.hash(h); self.size.hash(h); @@ -60,7 +61,9 @@ impl Hash for TypedRect impl Copy for TypedRect {} impl Clone for TypedRect { - fn clone(&self) -> Self { *self } + fn clone(&self) -> Self { + *self + } } impl PartialEq> for TypedRect { @@ -72,7 +75,7 @@ impl PartialEq> for TypedRect { impl Eq for TypedRect {} impl fmt::Debug for TypedRect { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "TypedRect({:?} at {:?})", self.size, self.origin) } } @@ -94,13 +97,15 @@ impl TypedRect { } impl TypedRect -where T: Copy + Clone + Zero + PartialOrd + PartialEq + Add + Sub { +where + T: Copy + Clone + Zero + PartialOrd + PartialEq + Add + Sub, +{ #[inline] pub fn intersects(&self, other: &Self) -> bool { - self.origin.x < other.origin.x + other.size.width && - other.origin.x < self.origin.x + self.size.width && - self.origin.y < other.origin.y + other.size.height && - other.origin.y < self.origin.y + self.size.height + self.origin.x < other.origin.x + other.size.width + && other.origin.x < self.origin.x + self.size.width + && self.origin.y < other.origin.y + other.size.height + && other.origin.y < self.origin.y + self.size.height } #[inline] @@ -149,13 +154,17 @@ where T: Copy + Clone + Zero + PartialOrd + PartialEq + Add + Sub + Sub) -> bool { - self.origin.x <= other.x && other.x < self.origin.x + self.size.width && - self.origin.y <= other.y && other.y < self.origin.y + self.size.height + self.origin.x <= other.x && other.x < self.origin.x + self.size.width + && self.origin.y <= other.y && other.y < self.origin.y + self.size.height } /// Returns true if this rectangle contains the interior of rect. Always @@ -179,9 +188,9 @@ where T: Copy + Clone + Zero + PartialOrd + PartialEq + Add + Sub bool { - rect.is_empty() || - (self.min_x() <= rect.min_x() && rect.max_x() <= self.max_x() && - self.min_y() <= rect.min_y() && rect.max_y() <= self.max_y()) + rect.is_empty() + || (self.min_x() <= rect.min_x() && rect.max_x() <= self.max_x() + && self.min_y() <= rect.min_y() && rect.max_y() <= self.max_y()) } #[inline] @@ -189,7 +198,10 @@ where T: Copy + Clone + Zero + PartialOrd + PartialEq + Add + Sub Self { TypedRect::new( TypedPoint2D::new(self.origin.x - width, self.origin.y - height), - TypedSize2D::new(self.size.width + width + width, self.size.height + height + height), + TypedSize2D::new( + self.size.width + width + width, + self.size.height + height + height, + ), ) } @@ -233,7 +245,7 @@ where T: Copy + Clone + Zero + PartialOrd + PartialEq + Add + Sub> + I: IntoIterator>, { let mut points = points.into_iter(); @@ -245,7 +257,7 @@ where T: Copy + Clone + Zero + PartialOrd + PartialEq + Add + Sub + Sub TypedRect -where T: Copy + One + Add + Sub + Mul { +where + T: Copy + One + Add + Sub + Mul, +{ /// Linearly interpolate between this rectangle and another rectangle. /// /// `t` is expected to be between zero and one. @@ -279,7 +295,9 @@ where T: Copy + One + Add + Sub + Mul { } impl TypedRect -where T: Copy + Clone + PartialOrd + Add + Sub + Zero { +where + T: Copy + Clone + PartialOrd + Add + Sub + Zero, +{ #[inline] pub fn union(&self, other: &Self) -> Self { if self.size == Zero::zero() { @@ -289,15 +307,17 @@ where T: Copy + Clone + PartialOrd + Add + Sub + Zero return *self; } - let upper_left = TypedPoint2D::new(min(self.min_x(), other.min_x()), - min(self.min_y(), other.min_y())); + let upper_left = TypedPoint2D::new( + min(self.min_x(), other.min_x()), + min(self.min_y(), other.min_y()), + ); let lower_right_x = max(self.max_x(), other.max_x()); let lower_right_y = max(self.max_y(), other.max_y()); TypedRect::new( upper_left, - TypedSize2D::new(lower_right_x - upper_left.x, lower_right_y - upper_left.y) + TypedSize2D::new(lower_right_x - upper_left.x, lower_right_y - upper_left.y), ) } } @@ -305,11 +325,12 @@ where T: Copy + Clone + PartialOrd + Add + Sub + Zero impl TypedRect { #[inline] pub fn scale(&self, x: S, y: S) -> Self - where T: Copy + Clone + Mul + where + T: Copy + Clone + Mul, { TypedRect::new( TypedPoint2D::new(self.origin.x * x, self.origin.y * y), - TypedSize2D::new(self.size.width * x, self.size.height * y) + TypedSize2D::new(self.size.width * x, self.size.height * y), ) } } @@ -317,10 +338,7 @@ impl TypedRect { impl TypedRect { /// Constructor, setting all sides to zero. pub fn zero() -> Self { - TypedRect::new( - TypedPoint2D::origin(), - TypedSize2D::zero(), - ) + TypedRect::new(TypedPoint2D::origin(), TypedSize2D::zero()) } /// Returns true if the size is zero, regardless of the origin's value. @@ -329,16 +347,23 @@ impl TypedRect { } } - pub fn min(x: T, y: T) -> T { - if x <= y { x } else { y } + if x <= y { + x + } else { + y + } } pub fn max(x: T, y: T) -> T { - if x >= y { x } else { y } + if x >= y { + x + } else { + y + } } -impl, U> Mul for TypedRect { +impl, U> Mul for TypedRect { type Output = Self; #[inline] fn mul(self, scale: T) -> Self { @@ -346,7 +371,7 @@ impl, U> Mul for TypedRect { } } -impl, U> Div for TypedRect { +impl, U> Div for TypedRect { type Output = Self; #[inline] fn div(self, scale: T) -> Self { @@ -354,7 +379,7 @@ impl, U> Div for TypedRect { } } -impl, U1, U2> Mul> for TypedRect { +impl, U1, U2> Mul> for TypedRect { type Output = TypedRect; #[inline] fn mul(self, scale: TypedScale) -> TypedRect { @@ -362,7 +387,7 @@ impl, U1, U2> Mul> for TypedRec } } -impl, U1, U2> Div> for TypedRect { +impl, U1, U2> Div> for TypedRect { type Output = TypedRect; #[inline] fn div(self, scale: TypedScale) -> TypedRect { @@ -378,7 +403,10 @@ impl TypedRect { /// Tag a unitless value with units. pub fn from_untyped(r: &Rect) -> TypedRect { - TypedRect::new(TypedPoint2D::from_untyped(&r.origin), TypedSize2D::from_untyped(&r.size)) + TypedRect::new( + TypedPoint2D::from_untyped(&r.origin), + TypedSize2D::from_untyped(&r.size), + ) } } @@ -391,12 +419,12 @@ impl TypedRect { pub fn cast(&self) -> Option> { match (self.origin.cast(), self.size.cast()) { (Some(origin), Some(size)) => Some(TypedRect::new(origin, size)), - _ => None + _ => None, } } } -impl + Sub, U> TypedRect { +impl + Sub, U> TypedRect { /// Return a rectangle with edges rounded to integer coordinates, such that /// the returned rectangle has the same set of pixel centers as the original /// one. @@ -496,16 +524,15 @@ mod tests { #[test] fn test_translate() { let p = Rect::new(Point2D::new(0u32, 0u32), Size2D::new(50u32, 40u32)); - let pp = p.translate(&vec2(10,15)); + let pp = p.translate(&vec2(10, 15)); assert!(pp.size.width == 50); assert!(pp.size.height == 40); assert!(pp.origin.x == 10); assert!(pp.origin.y == 15); - let r = Rect::new(Point2D::new(-10, -5), Size2D::new(50, 40)); - let rr = r.translate(&vec2(0,-10)); + let rr = r.translate(&vec2(0, -10)); assert!(rr.size.width == 50); assert!(rr.size.height == 40); @@ -516,16 +543,15 @@ mod tests { #[test] fn test_translate_by_size() { let p = Rect::new(Point2D::new(0u32, 0u32), Size2D::new(50u32, 40u32)); - let pp = p.translate_by_size(&Size2D::new(10,15)); + let pp = p.translate_by_size(&Size2D::new(10, 15)); assert!(pp.size.width == 50); assert!(pp.size.height == 40); assert!(pp.origin.x == 10); assert!(pp.origin.y == 15); - let r = Rect::new(Point2D::new(-10, -5), Size2D::new(50, 40)); - let rr = r.translate_by_size(&Size2D::new(0,-10)); + let rr = r.translate_by_size(&Size2D::new(0, -10)); assert!(rr.size.width == 50); assert!(rr.size.height == 40); @@ -536,7 +562,7 @@ mod tests { #[test] fn test_union() { let p = Rect::new(Point2D::new(0, 0), Size2D::new(50, 40)); - let q = Rect::new(Point2D::new(20,20), Size2D::new(5, 5)); + let q = Rect::new(Point2D::new(20, 20), Size2D::new(5, 5)); let r = Rect::new(Point2D::new(-15, -30), Size2D::new(200, 15)); let s = Rect::new(Point2D::new(20, -15), Size2D::new(250, 200)); @@ -551,7 +577,6 @@ mod tests { let ps = p.union(&s); assert!(ps.origin == Point2D::new(0, -15)); assert!(ps.size == Size2D::new(270, 200)); - } #[test] @@ -608,10 +633,10 @@ mod tests { let r = Rect::new(Point2D::new(-20.0, 15.0), Size2D::new(100.0, 200.0)); assert!(r.contains_rect(&r)); - assert!(!r.contains_rect(&r.translate(&vec2( 0.1, 0.0)))); - assert!(!r.contains_rect(&r.translate(&vec2(-0.1, 0.0)))); - assert!(!r.contains_rect(&r.translate(&vec2( 0.0, 0.1)))); - assert!(!r.contains_rect(&r.translate(&vec2( 0.0, -0.1)))); + assert!(!r.contains_rect(&r.translate(&vec2(0.1, 0.0)))); + assert!(!r.contains_rect(&r.translate(&vec2(-0.1, 0.0)))); + assert!(!r.contains_rect(&r.translate(&vec2(0.0, 0.1)))); + assert!(!r.contains_rect(&r.translate(&vec2(0.0, -0.1)))); // Empty rectangles are always considered as contained in other rectangles, // even if their origin is not. let p = Point2D::new(1.0, 1.0); diff --git a/src/rotation.rs b/src/rotation.rs index 0c33a43..d961992 100644 --- a/src/rotation.rs +++ b/src/rotation.rs @@ -8,13 +8,13 @@ // except according to those terms. use approxeq::ApproxEq; -use num_traits::{Float, One, Zero, FloatConst}; +use num_traits::{Float, FloatConst, One, Zero}; use std::fmt; -use std::ops::{Add, Neg, Mul, Sub, Div, AddAssign, SubAssign, MulAssign, DivAssign, Rem}; +use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Rem, Sub, SubAssign}; use std::marker::PhantomData; use trig::Trig; use {TypedPoint2D, TypedPoint3D, TypedVector2D, TypedVector3D, Vector3D, point2, point3, vec3}; -use {TypedTransform3D, TypedTransform2D, UnknownUnit}; +use {TypedTransform2D, TypedTransform3D, UnknownUnit}; /// An angle in radians #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Hash)] @@ -35,11 +35,14 @@ impl Angle { } impl Angle - where T: Trig +where + T: Trig, { #[inline] pub fn degrees(deg: T) -> Self { - Angle { radians: T::degrees_to_radians(deg) } + Angle { + radians: T::degrees_to_radians(deg), + } } #[inline] @@ -49,13 +52,8 @@ impl Angle } impl Angle -where T: Rem - + Sub - + Add - + Zero - + FloatConst - + PartialOrd - + Copy +where + T: Rem + Sub + Add + Zero + FloatConst + PartialOrd + Copy, { /// Returns this angle in the [0..2*PI[ range. pub fn positive(&self) -> Self { @@ -74,7 +72,9 @@ where T: Rem } impl Angle -where T: Float { +where + T: Float, +{ /// Returns (sin(self), cos(self)). pub fn sin_cos(self) -> (T, T) { self.radians.sin_cos() @@ -82,24 +82,40 @@ where T: Float { } impl Angle -where T: Zero { - pub fn zero() -> Self { Angle::radians(T::zero()) } +where + T: Zero, +{ + pub fn zero() -> Self { + Angle::radians(T::zero()) + } } impl Angle -where T: FloatConst + Add { - pub fn pi() -> Self { Angle::radians(T::PI()) } +where + T: FloatConst + Add, +{ + pub fn pi() -> Self { + Angle::radians(T::PI()) + } - pub fn two_pi() -> Self { Angle::radians(T::PI() + T::PI()) } + pub fn two_pi() -> Self { + Angle::radians(T::PI() + T::PI()) + } - pub fn frac_pi_2() -> Self { Angle::radians(T::FRAC_PI_2()) } + pub fn frac_pi_2() -> Self { + Angle::radians(T::FRAC_PI_2()) + } - pub fn frac_pi_3() -> Self { Angle::radians(T::FRAC_PI_3()) } + pub fn frac_pi_3() -> Self { + Angle::radians(T::FRAC_PI_3()) + } - pub fn frac_pi_4() -> Self { Angle::radians(T::FRAC_PI_4()) } + pub fn frac_pi_4() -> Self { + Angle::radians(T::FRAC_PI_4()) + } } -impl> Add for Angle { +impl> Add for Angle { type Output = Angle; fn add(self, other: Angle) -> Angle { Angle::radians(self.radians + other.radians) @@ -112,7 +128,7 @@ impl> AddAssign for Angle { } } -impl> Sub> for Angle { +impl> Sub> for Angle { type Output = Angle; fn sub(self, other: Angle) -> ::Output { Angle::radians(self.radians - other.radians) @@ -125,7 +141,7 @@ impl> SubAssign for Angle { } } -impl> Div> for Angle { +impl> Div> for Angle { type Output = T; #[inline] fn div(self, other: Angle) -> T { @@ -133,7 +149,7 @@ impl> Div> for Angle { } } -impl> Div for Angle { +impl> Div for Angle { type Output = Angle; #[inline] fn div(self, factor: T) -> Angle { @@ -147,7 +163,7 @@ impl> DivAssign for Angle { } } -impl> Mul for Angle { +impl> Mul for Angle { type Output = Angle; #[inline] fn mul(self, factor: T) -> Angle { @@ -161,14 +177,13 @@ impl> MulAssign for Angle { } } -impl> Neg for Angle { +impl> Neg for Angle { type Output = Self; fn neg(self) -> Self { Angle::radians(-self.radians) } } - define_matrix! { /// A transform that can represent rotations in 2d, represented as an angle in radians. pub struct TypedRotation2D { @@ -195,12 +210,17 @@ impl TypedRotation2D { /// Creates the identity rotation. #[inline] - pub fn identity() -> Self where T: Zero { + pub fn identity() -> Self + where + T: Zero, + { Self::radians(T::zero()) } } -impl TypedRotation2D where T: Clone +impl TypedRotation2D +where + T: Clone, { /// Returns self.angle as a strongly typed `Angle`. pub fn get_angle(&self) -> Angle { @@ -208,17 +228,19 @@ impl TypedRotation2D where T: Clone } } - impl TypedRotation2D -where T: Copy + Clone + - Add + - Sub + - Mul + - Div + - Neg + - PartialOrd + - Float + - One + Zero +where + T: Copy + + Clone + + Add + + Sub + + Mul + + Div + + Neg + + PartialOrd + + Float + + One + + Zero, { /// Creates a 3d rotation (around the z axis) from this 2d rotation. #[inline] @@ -234,13 +256,19 @@ where T: Copy + Clone + /// Returns a rotation representing the other rotation followed by this rotation. #[inline] - pub fn pre_rotate(&self, other: &TypedRotation2D) -> TypedRotation2D { + pub fn pre_rotate( + &self, + other: &TypedRotation2D, + ) -> TypedRotation2D { TypedRotation2D::radians(self.angle + other.angle) } /// Returns a rotation representing this rotation followed by the other rotation. #[inline] - pub fn post_rotate(&self, other: &TypedRotation2D) -> TypedRotation2D { + pub fn post_rotate( + &self, + other: &TypedRotation2D, + ) -> TypedRotation2D { other.pre_rotate(self) } @@ -250,10 +278,7 @@ where T: Copy + Clone + #[inline] pub fn transform_point(&self, point: &TypedPoint2D) -> TypedPoint2D { let (sin, cos) = Float::sin_cos(self.angle); - point2( - point.x * cos - point.y * sin, - point.y * cos + point.x * sin, - ) + point2(point.x * cos - point.y * sin, point.y * cos + point.x * sin) } /// Returns the given 2d vector transformed by this rotation. @@ -266,14 +291,17 @@ where T: Copy + Clone + } impl TypedRotation2D -where T: Copy + Clone + - Add + - Mul + - Div + - Sub + - Trig + - PartialOrd + - One + Zero +where + T: Copy + + Clone + + Add + + Mul + + Div + + Sub + + Trig + + PartialOrd + + One + + Zero, { /// Returns the matrix representation of this rotation. #[inline] @@ -317,15 +345,25 @@ impl TypedRotation3D { /// The resulting quaternion is not necessarily normalized. See `unit_quaternion`. #[inline] pub fn quaternion(a: T, b: T, c: T, r: T) -> Self { - TypedRotation3D { i: a, j: b, k: c, r, _unit: PhantomData } + TypedRotation3D { + i: a, + j: b, + k: c, + r, + _unit: PhantomData, + } } } - -impl TypedRotation3D where T: Copy { +impl TypedRotation3D +where + T: Copy, +{ /// Returns the vector part (i, j, k) of this quaternion. #[inline] - pub fn vector_part(&self) -> Vector3D { vec3(self.i, self.j, self.k) } + pub fn vector_part(&self) -> Vector3D { + vec3(self.i, self.j, self.k) + } } impl TypedRotation3D @@ -392,9 +430,9 @@ where pub fn euler(roll: Angle, pitch: Angle, yaw: Angle) -> Self { let half = T::one() / (T::one() + T::one()); - let (sy, cy) = Float::sin_cos(half * yaw.get()); - let (sp, cp) = Float::sin_cos(half * pitch.get()); - let (sr, cr) = Float::sin_cos(half * roll.get()); + let (sy, cy) = Float::sin_cos(half * yaw.get()); + let (sp, cp) = Float::sin_cos(half * pitch.get()); + let (sr, cr) = Float::sin_cos(half * roll.get()); Self::quaternion( cy * sr * cp - sy * cr * sp, @@ -418,7 +456,7 @@ where #[inline] pub fn square_norm(&self) -> T { - (self.i * self.i + self.j * self.j + self.k * self.k + self.r *self.r) + (self.i * self.i + self.j * self.j + self.k * self.k + self.r * self.r) } /// Returns a unit quaternion from this one. @@ -576,15 +614,30 @@ where let m33 = one - (ii + jj); TypedTransform3D::row_major( - m11, m12, m13, zero, - m21, m22, m23, zero, - m31, m32, m33, zero, - zero, zero, zero, one, + m11, + m12, + m13, + zero, + m21, + m22, + m23, + zero, + m31, + m32, + m33, + zero, + zero, + zero, + zero, + one, ) } /// Returns a rotation representing the other rotation followed by this rotation. - pub fn pre_rotate(&self, other: &TypedRotation3D) -> TypedRotation3D + pub fn pre_rotate( + &self, + other: &TypedRotation3D, + ) -> TypedRotation3D where T: ApproxEq, { @@ -599,7 +652,10 @@ where /// Returns a rotation representing this rotation followed by the other rotation. #[inline] - pub fn post_rotate(&self, other: &TypedRotation3D) -> TypedRotation3D + pub fn post_rotate( + &self, + other: &TypedRotation3D, + ) -> TypedRotation3D where T: ApproxEq, { @@ -642,19 +698,27 @@ where impl fmt::Debug for TypedRotation3D { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "Quat({:?}*i + {:?}*j + {:?}*k + {:?})", self.i, self.j, self.k, self.r) + write!( + f, + "Quat({:?}*i + {:?}*j + {:?}*k + {:?})", + self.i, self.j, self.k, self.r + ) } } impl fmt::Display for TypedRotation3D { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "Quat({}*i + {}*j + {}*k + {})", self.i, self.j, self.k, self.r) + write!( + f, + "Quat({}*i + {}*j + {}*k + {})", + self.i, self.j, self.k, self.r + ) } } impl ApproxEq for TypedRotation3D where - T: Copy + Neg + ApproxEq + T: Copy + Neg + ApproxEq, { fn approx_epsilon() -> T { T::approx_epsilon() @@ -665,63 +729,83 @@ where } fn approx_eq_eps(&self, other: &Self, eps: &T) -> bool { - ( - self.i.approx_eq_eps(&other.i, eps) - && self.j.approx_eq_eps(&other.j, eps) - && self.k.approx_eq_eps(&other.k, eps) - && self.r.approx_eq_eps(&other.r, eps) - ) || ( - self.i.approx_eq_eps(&-other.i, eps) - && self.j.approx_eq_eps(&-other.j, eps) - && self.k.approx_eq_eps(&-other.k, eps) - && self.r.approx_eq_eps(&-other.r, eps) - ) + (self.i.approx_eq_eps(&other.i, eps) && self.j.approx_eq_eps(&other.j, eps) + && self.k.approx_eq_eps(&other.k, eps) && self.r.approx_eq_eps(&other.r, eps)) + || (self.i.approx_eq_eps(&-other.i, eps) && self.j.approx_eq_eps(&-other.j, eps) + && self.k.approx_eq_eps(&-other.k, eps) + && self.r.approx_eq_eps(&-other.r, eps)) } } #[test] fn simple_rotation_2d() { - use std::f32::consts::{PI, FRAC_PI_2}; + use std::f32::consts::{FRAC_PI_2, PI}; let ri = Rotation2D::identity(); let r90 = Rotation2D::radians(FRAC_PI_2); let rm90 = Rotation2D::radians(-FRAC_PI_2); let r180 = Rotation2D::radians(PI); - assert!(ri.transform_point(&point2(1.0, 2.0)).approx_eq(&point2(1.0, 2.0))); - assert!(r90.transform_point(&point2(1.0, 2.0)).approx_eq(&point2(-2.0, 1.0))); - assert!(rm90.transform_point(&point2(1.0, 2.0)).approx_eq(&point2(2.0, -1.0))); - assert!(r180.transform_point(&point2(1.0, 2.0)).approx_eq(&point2(-1.0, -2.0))); + assert!( + ri.transform_point(&point2(1.0, 2.0)) + .approx_eq(&point2(1.0, 2.0)) + ); + assert!( + r90.transform_point(&point2(1.0, 2.0)) + .approx_eq(&point2(-2.0, 1.0)) + ); + assert!( + rm90.transform_point(&point2(1.0, 2.0)) + .approx_eq(&point2(2.0, -1.0)) + ); + assert!( + r180.transform_point(&point2(1.0, 2.0)) + .approx_eq(&point2(-1.0, -2.0)) + ); assert!( - r90.inverse().inverse().transform_point(&point2(1.0, 2.0)).approx_eq( - &r90.transform_point(&point2(1.0, 2.0)) - ) + r90.inverse() + .inverse() + .transform_point(&point2(1.0, 2.0)) + .approx_eq(&r90.transform_point(&point2(1.0, 2.0))) ); } #[test] fn simple_rotation_3d_in_2d() { - use std::f32::consts::{PI, FRAC_PI_2}; + use std::f32::consts::{FRAC_PI_2, PI}; let ri = Rotation3D::identity(); let r90 = Rotation3D::around_z(Angle::radians(FRAC_PI_2)); let rm90 = Rotation3D::around_z(Angle::radians(-FRAC_PI_2)); let r180 = Rotation3D::around_z(Angle::radians(PI)); - assert!(ri.rotate_point2d(&point2(1.0, 2.0)).approx_eq(&point2(1.0, 2.0))); - assert!(r90.rotate_point2d(&point2(1.0, 2.0)).approx_eq(&point2(-2.0, 1.0))); - assert!(rm90.rotate_point2d(&point2(1.0, 2.0)).approx_eq(&point2(2.0, -1.0))); - assert!(r180.rotate_point2d(&point2(1.0, 2.0)).approx_eq(&point2(-1.0, -2.0))); + assert!( + ri.rotate_point2d(&point2(1.0, 2.0)) + .approx_eq(&point2(1.0, 2.0)) + ); + assert!( + r90.rotate_point2d(&point2(1.0, 2.0)) + .approx_eq(&point2(-2.0, 1.0)) + ); + assert!( + rm90.rotate_point2d(&point2(1.0, 2.0)) + .approx_eq(&point2(2.0, -1.0)) + ); + assert!( + r180.rotate_point2d(&point2(1.0, 2.0)) + .approx_eq(&point2(-1.0, -2.0)) + ); assert!( - r90.inverse().inverse().rotate_point2d(&point2(1.0, 2.0)).approx_eq( - &r90.rotate_point2d(&point2(1.0, 2.0)) - ) + r90.inverse() + .inverse() + .rotate_point2d(&point2(1.0, 2.0)) + .approx_eq(&r90.rotate_point2d(&point2(1.0, 2.0))) ); } #[test] fn pre_post() { - use std::f32::consts::{FRAC_PI_2}; + use std::f32::consts::FRAC_PI_2; let r1 = Rotation3D::around_x(Angle::radians(FRAC_PI_2)); let r2 = Rotation3D::around_y(Angle::radians(FRAC_PI_2)); let r3 = Rotation3D::around_z(Angle::radians(FRAC_PI_2)); @@ -746,7 +830,7 @@ fn pre_post() { #[test] fn to_transform3d() { - use std::f32::consts::{PI, FRAC_PI_2}; + use std::f32::consts::{FRAC_PI_2, PI}; let rotations = [ Rotation3D::identity(), Rotation3D::around_x(Angle::radians(FRAC_PI_2)), @@ -790,33 +874,83 @@ fn slerp() { // quaternion.slerp_evaluate(q1, q2, 0.2) assert!(q1.slerp(&q2, 0.0).approx_eq(&q1)); - assert!(q1.slerp(&q2, 0.2).approx_eq(&Rotation3D::quaternion(0.951056516295154, 0.309016994374947, 0.0, 0.0))); - assert!(q1.slerp(&q2, 0.4).approx_eq(&Rotation3D::quaternion(0.809016994374947, 0.587785252292473, 0.0, 0.0))); - assert!(q1.slerp(&q2, 0.6).approx_eq(&Rotation3D::quaternion(0.587785252292473, 0.809016994374947, 0.0, 0.0))); - assert!(q1.slerp(&q2, 0.8).approx_eq(&Rotation3D::quaternion(0.309016994374947, 0.951056516295154, 0.0, 0.0))); + assert!(q1.slerp(&q2, 0.2).approx_eq(&Rotation3D::quaternion( + 0.951056516295154, + 0.309016994374947, + 0.0, + 0.0 + ))); + assert!(q1.slerp(&q2, 0.4).approx_eq(&Rotation3D::quaternion( + 0.809016994374947, + 0.587785252292473, + 0.0, + 0.0 + ))); + assert!(q1.slerp(&q2, 0.6).approx_eq(&Rotation3D::quaternion( + 0.587785252292473, + 0.809016994374947, + 0.0, + 0.0 + ))); + assert!(q1.slerp(&q2, 0.8).approx_eq(&Rotation3D::quaternion( + 0.309016994374947, + 0.951056516295154, + 0.0, + 0.0 + ))); assert!(q1.slerp(&q2, 1.0).approx_eq(&q2)); assert!(q1.slerp(&q3, 0.0).approx_eq(&q1)); - assert!(q1.slerp(&q3, 0.2).approx_eq(&Rotation3D::quaternion(0.951056516295154, 0.0, -0.309016994374947, 0.0))); - assert!(q1.slerp(&q3, 0.4).approx_eq(&Rotation3D::quaternion(0.809016994374947, 0.0, -0.587785252292473, 0.0))); - assert!(q1.slerp(&q3, 0.6).approx_eq(&Rotation3D::quaternion(0.587785252292473, 0.0, -0.809016994374947, 0.0))); - assert!(q1.slerp(&q3, 0.8).approx_eq(&Rotation3D::quaternion(0.309016994374947, 0.0, -0.951056516295154, 0.0))); + assert!(q1.slerp(&q3, 0.2).approx_eq(&Rotation3D::quaternion( + 0.951056516295154, + 0.0, + -0.309016994374947, + 0.0 + ))); + assert!(q1.slerp(&q3, 0.4).approx_eq(&Rotation3D::quaternion( + 0.809016994374947, + 0.0, + -0.587785252292473, + 0.0 + ))); + assert!(q1.slerp(&q3, 0.6).approx_eq(&Rotation3D::quaternion( + 0.587785252292473, + 0.0, + -0.809016994374947, + 0.0 + ))); + assert!(q1.slerp(&q3, 0.8).approx_eq(&Rotation3D::quaternion( + 0.309016994374947, + 0.0, + -0.951056516295154, + 0.0 + ))); assert!(q1.slerp(&q3, 1.0).approx_eq(&q3)); } #[test] fn around_axis() { - use std::f32::consts::{PI, FRAC_PI_2}; + use std::f32::consts::{FRAC_PI_2, PI}; // Two sort of trivial cases: let r1 = Rotation3D::around_axis(vec3(1.0, 1.0, 0.0), Angle::radians(PI)); let r2 = Rotation3D::around_axis(vec3(1.0, 1.0, 0.0), Angle::radians(FRAC_PI_2)); - assert!(r1.rotate_point3d(&point3(1.0, 2.0, 0.0)).approx_eq(&point3(2.0, 1.0, 0.0))); - assert!(r2.rotate_point3d(&point3(1.0, 0.0, 0.0)).approx_eq(&point3(0.5, 0.5, -0.5.sqrt()))); + assert!( + r1.rotate_point3d(&point3(1.0, 2.0, 0.0)) + .approx_eq(&point3(2.0, 1.0, 0.0)) + ); + assert!( + r2.rotate_point3d(&point3(1.0, 0.0, 0.0)) + .approx_eq(&point3(0.5, 0.5, -0.5.sqrt())) + ); // A more arbitrary test (made up with numpy): let r3 = Rotation3D::around_axis(vec3(0.5, 1.0, 2.0), Angle::radians(2.291288)); - assert!(r3.rotate_point3d(&point3(1.0, 0.0, 0.0)).approx_eq(&point3(-0.58071821, 0.81401868, -0.01182979))); + assert!(r3.rotate_point3d(&point3(1.0, 0.0, 0.0)).approx_eq(&point3( + -0.58071821, + 0.81401868, + -0.01182979 + ))); } #[test] @@ -868,20 +1002,55 @@ fn from_euler() { fn wrap_angles() { use std::f32::consts::{FRAC_PI_2, PI}; assert!(Angle::radians(0.0).positive().radians.approx_eq(&0.0)); - assert!(Angle::radians(FRAC_PI_2).positive().radians.approx_eq(&FRAC_PI_2)); - assert!(Angle::radians(-FRAC_PI_2).positive().radians.approx_eq(&(3.0*FRAC_PI_2))); - assert!(Angle::radians(3.0 * FRAC_PI_2).positive().radians.approx_eq(&(3.0 * FRAC_PI_2))); - assert!(Angle::radians(5.0 * FRAC_PI_2).positive().radians.approx_eq(&FRAC_PI_2)); - assert!(Angle::radians(2.0*PI).positive().radians.approx_eq(&0.0)); - assert!(Angle::radians(-2.0*PI).positive().radians.approx_eq(&0.0)); + assert!( + Angle::radians(FRAC_PI_2) + .positive() + .radians + .approx_eq(&FRAC_PI_2) + ); + assert!( + Angle::radians(-FRAC_PI_2) + .positive() + .radians + .approx_eq(&(3.0 * FRAC_PI_2)) + ); + assert!( + Angle::radians(3.0 * FRAC_PI_2) + .positive() + .radians + .approx_eq(&(3.0 * FRAC_PI_2)) + ); + assert!( + Angle::radians(5.0 * FRAC_PI_2) + .positive() + .radians + .approx_eq(&FRAC_PI_2) + ); + assert!(Angle::radians(2.0 * PI).positive().radians.approx_eq(&0.0)); + assert!(Angle::radians(-2.0 * PI).positive().radians.approx_eq(&0.0)); assert!(Angle::radians(PI).positive().radians.approx_eq(&PI)); assert!(Angle::radians(-PI).positive().radians.approx_eq(&PI)); - assert!(Angle::radians(FRAC_PI_2).signed().radians.approx_eq(&FRAC_PI_2)); - assert!(Angle::radians(3.0 * FRAC_PI_2).signed().radians.approx_eq(&-FRAC_PI_2)); - assert!(Angle::radians(5.0 * FRAC_PI_2).signed().radians.approx_eq(&FRAC_PI_2)); - assert!(Angle::radians(2.0*PI).signed().radians.approx_eq(&0.0)); - assert!(Angle::radians(-2.0*PI).signed().radians.approx_eq(&0.0)); + assert!( + Angle::radians(FRAC_PI_2) + .signed() + .radians + .approx_eq(&FRAC_PI_2) + ); + assert!( + Angle::radians(3.0 * FRAC_PI_2) + .signed() + .radians + .approx_eq(&-FRAC_PI_2) + ); + assert!( + Angle::radians(5.0 * FRAC_PI_2) + .signed() + .radians + .approx_eq(&FRAC_PI_2) + ); + assert!(Angle::radians(2.0 * PI).signed().radians.approx_eq(&0.0)); + assert!(Angle::radians(-2.0 * PI).signed().radians.approx_eq(&0.0)); assert!(Angle::radians(-PI).signed().radians.approx_eq(&PI)); assert!(Angle::radians(PI).signed().radians.approx_eq(&PI)); } diff --git a/src/scale.rs b/src/scale.rs index 7634a23..cc1ea34 100644 --- a/src/scale.rs +++ b/src/scale.rs @@ -13,9 +13,9 @@ use num::One; use num_traits::NumCast; use serde::{Deserialize, Deserializer, Serialize, Serializer}; use std::fmt; -use std::ops::{Add, Mul, Sub, Div, Neg}; +use std::ops::{Add, Div, Mul, Neg, Sub}; use std::marker::PhantomData; -use {TypedRect, TypedSize2D, TypedPoint2D, TypedVector2D}; +use {TypedPoint2D, TypedRect, TypedSize2D, TypedVector2D}; /// A scaling factor between two different units of measurement. /// @@ -39,15 +39,29 @@ use {TypedRect, TypedSize2D, TypedPoint2D, TypedVector2D}; #[repr(C)] pub struct TypedScale(pub T, PhantomData<(Src, Dst)>); -impl<'de, T, Src, Dst> Deserialize<'de> for TypedScale where T: Deserialize<'de> { +impl<'de, T, Src, Dst> Deserialize<'de> for TypedScale +where + T: Deserialize<'de>, +{ fn deserialize(deserializer: D) -> Result, D::Error> - where D: Deserializer<'de> { - Ok(TypedScale(try!(Deserialize::deserialize(deserializer)), PhantomData)) + where + D: Deserializer<'de>, + { + Ok(TypedScale( + try!(Deserialize::deserialize(deserializer)), + PhantomData, + )) } } -impl Serialize for TypedScale where T: Serialize { - fn serialize(&self, serializer: S) -> Result where S: Serializer { +impl Serialize for TypedScale +where + T: Serialize, +{ + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { self.0.serialize(serializer) } } @@ -64,7 +78,7 @@ impl TypedScale { } } -impl, Src, Dst> TypedScale { +impl, Src, Dst> TypedScale { /// The inverse TypedScale (1.0 / self). pub fn inv(&self) -> TypedScale { let one: T = One::one(); @@ -73,8 +87,7 @@ impl, Src, Dst> TypedScale { } // scale0 * scale1 -impl, A, B, C> -Mul> for TypedScale { +impl, A, B, C> Mul> for TypedScale { type Output = TypedScale; #[inline] fn mul(self, other: TypedScale) -> TypedScale { @@ -83,7 +96,7 @@ Mul> for TypedScale { } // scale0 + scale1 -impl, Src, Dst> Add for TypedScale { +impl, Src, Dst> Add for TypedScale { type Output = TypedScale; #[inline] fn add(self, other: TypedScale) -> TypedScale { @@ -92,7 +105,7 @@ impl, Src, Dst> Add for TypedScale { } // scale0 - scale1 -impl, Src, Dst> Sub for TypedScale { +impl, Src, Dst> Sub for TypedScale { type Output = TypedScale; #[inline] fn sub(self, other: TypedScale) -> TypedScale { @@ -108,11 +121,8 @@ impl TypedScale { } impl TypedScale -where T: Copy + Clone + - Mul + - Neg + - PartialEq + - One +where + T: Copy + Clone + Mul + Neg + PartialEq + One, { /// Returns the given point transformed by this scale. #[inline] diff --git a/src/side_offsets.rs b/src/side_offsets.rs index 72ea9f0..259b229 100644 --- a/src/side_offsets.rs +++ b/src/side_offsets.rs @@ -30,8 +30,11 @@ define_matrix! { impl fmt::Debug for TypedSideOffsets2D { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "({:?},{:?},{:?},{:?})", - self.top, self.right, self.bottom, self.left) + write!( + f, + "({:?},{:?},{:?},{:?})", + self.top, self.right, self.bottom, self.left + ) } } @@ -51,24 +54,34 @@ impl TypedSideOffsets2D { } /// Constructor taking a typed Length for each side. - pub fn from_lengths(top: Length, - right: Length, - bottom: Length, - left: Length) -> Self { + pub fn from_lengths( + top: Length, + right: Length, + bottom: Length, + left: Length, + ) -> Self { TypedSideOffsets2D::new(top.0, right.0, bottom.0, left.0) } /// Access self.top as a typed Length instead of a scalar value. - pub fn top_typed(&self) -> Length { Length::new(self.top) } + pub fn top_typed(&self) -> Length { + Length::new(self.top) + } /// Access self.right as a typed Length instead of a scalar value. - pub fn right_typed(&self) -> Length { Length::new(self.right) } + pub fn right_typed(&self) -> Length { + Length::new(self.right) + } /// Access self.bottom as a typed Length instead of a scalar value. - pub fn bottom_typed(&self) -> Length { Length::new(self.bottom) } + pub fn bottom_typed(&self) -> Length { + Length::new(self.bottom) + } /// Access self.left as a typed Length instead of a scalar value. - pub fn left_typed(&self) -> Length { Length::new(self.left) } + pub fn left_typed(&self) -> Length { + Length::new(self.left) + } /// Constructor setting the same value to all sides, taking a scalar value directly. pub fn new_all_same(all: T) -> Self { @@ -81,7 +94,10 @@ impl TypedSideOffsets2D { } } -impl TypedSideOffsets2D where T: Add + Copy { +impl TypedSideOffsets2D +where + T: Add + Copy, +{ pub fn horizontal(&self) -> T { self.left + self.right } @@ -99,7 +115,10 @@ impl TypedSideOffsets2D where T: Add + Copy { } } -impl Add for TypedSideOffsets2D where T : Copy + Add { +impl Add for TypedSideOffsets2D +where + T: Copy + Add, +{ type Output = Self; fn add(self, other: Self) -> Self { TypedSideOffsets2D::new( @@ -114,12 +133,7 @@ impl Add for TypedSideOffsets2D where T : Copy + Add { impl TypedSideOffsets2D { /// Constructor, setting all sides to zero. pub fn zero() -> Self { - TypedSideOffsets2D::new( - Zero::zero(), - Zero::zero(), - Zero::zero(), - Zero::zero(), - ) + TypedSideOffsets2D::new(Zero::zero(), Zero::zero(), Zero::zero(), Zero::zero()) } } @@ -233,7 +247,7 @@ mod tests { mod bench { use test::BenchHarness; use std::num::Zero; - use rand::{XorShiftRng, Rng}; + use rand::{Rng, XorShiftRng}; use super::SideOffsets2DSimdI32; #[cfg(target_arch = "x86")] @@ -264,7 +278,12 @@ mod bench { } } let mut rng = XorShiftRng::new().unwrap(); - bh.iter(|| add(&rng.gen::(), &rng.gen::())) + bh.iter(|| { + add( + &rng.gen::(), + &rng.gen::(), + ) + }) } #[bench] diff --git a/src/size.rs b/src/size.rs index 7211fad..6f8a5d3 100644 --- a/src/size.rs +++ b/src/size.rs @@ -88,14 +88,14 @@ impl TypedSize2D { } } -impl, U> Add for TypedSize2D { +impl, U> Add for TypedSize2D { type Output = Self; fn add(self, other: Self) -> Self { TypedSize2D::new(self.width + other.width, self.height + other.height) } } -impl, U> Sub for TypedSize2D { +impl, U> Sub for TypedSize2D { type Output = Self; fn sub(self, other: Self) -> Self { TypedSize2D::new(self.width - other.width, self.height - other.height) @@ -103,11 +103,15 @@ impl, U> Sub for TypedSize2D { } impl, U> TypedSize2D { - pub fn area(&self) -> T::Output { self.width * self.height } + pub fn area(&self) -> T::Output { + self.width * self.height + } } impl TypedSize2D -where T: Copy + One + Add + Sub + Mul { +where + T: Copy + One + Add + Sub + Mul, +{ /// Linearly interpolate between this size and another size. /// /// `t` is expected to be between zero and one. @@ -130,23 +134,17 @@ impl TypedSize2D { impl TypedSize2D { pub fn zero() -> Self { - TypedSize2D::new( - Zero::zero(), - Zero::zero(), - ) + TypedSize2D::new(Zero::zero(), Zero::zero()) } } impl Zero for TypedSize2D { fn zero() -> Self { - TypedSize2D::new( - Zero::zero(), - Zero::zero(), - ) + TypedSize2D::new(Zero::zero(), Zero::zero()) } } -impl, U> Mul for TypedSize2D { +impl, U> Mul for TypedSize2D { type Output = Self; #[inline] fn mul(self, scale: T) -> Self { @@ -154,7 +152,7 @@ impl, U> Mul for TypedSize2D { } } -impl, U> Div for TypedSize2D { +impl, U> Div for TypedSize2D { type Output = Self; #[inline] fn div(self, scale: T) -> Self { @@ -162,7 +160,7 @@ impl, U> Div for TypedSize2D { } } -impl, U1, U2> Mul> for TypedSize2D { +impl, U1, U2> Mul> for TypedSize2D { type Output = TypedSize2D; #[inline] fn mul(self, scale: TypedScale) -> TypedSize2D { @@ -170,7 +168,7 @@ impl, U1, U2> Mul> for TypedSiz } } -impl, U1, U2> Div> for TypedSize2D { +impl, U1, U2> Div> for TypedSize2D { type Output = TypedSize2D; #[inline] fn div(self, scale: TypedScale) -> TypedSize2D { @@ -181,17 +179,25 @@ impl, U1, U2> Div> for TypedSiz impl TypedSize2D { /// Returns self.width as a Length carrying the unit. #[inline] - pub fn width_typed(&self) -> Length { Length::new(self.width) } + pub fn width_typed(&self) -> Length { + Length::new(self.width) + } /// Returns self.height as a Length carrying the unit. #[inline] - pub fn height_typed(&self) -> Length { Length::new(self.height) } + pub fn height_typed(&self) -> Length { + Length::new(self.height) + } #[inline] - pub fn to_array(&self) -> [T; 2] { [self.width, self.height] } + pub fn to_array(&self) -> [T; 2] { + [self.width, self.height] + } #[inline] - pub fn to_vector(&self) -> TypedVector2D { vec2(self.width, self.height) } + pub fn to_vector(&self) -> TypedVector2D { + vec2(self.width, self.height) + } /// Drop the units, preserving only the numeric value. pub fn to_untyped(&self) -> Size2D { @@ -213,7 +219,7 @@ impl TypedSize2D { pub fn cast(&self) -> Option> { match (NumCast::from(self.width), NumCast::from(self.height)) { (Some(w), Some(h)) => Some(TypedSize2D::new(w, h)), - _ => None + _ => None, } } @@ -258,7 +264,9 @@ impl TypedSize2D { } impl TypedSize2D -where T: Signed { +where + T: Signed, +{ pub fn abs(&self) -> Self { size2(self.width.abs(), self.height.abs()) } diff --git a/src/trig.rs b/src/trig.rs index 26f9f00..26ad5e9 100644 --- a/src/trig.rs +++ b/src/trig.rs @@ -7,7 +7,6 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. - /// Trait for basic trigonometry functions, so they can be used on generic numeric types pub trait Trig { fn sin(self) -> Self; @@ -68,4 +67,3 @@ macro_rules! trig { trig!(f32); trig!(f64); - diff --git a/src/vector.rs b/src/vector.rs index 473001c..c9e8ac0 100644 --- a/src/vector.rs +++ b/src/vector.rs @@ -18,7 +18,7 @@ use Angle; use num::*; use num_traits::{Float, NumCast, Signed}; use std::fmt; -use std::ops::{Add, Neg, Mul, Sub, Div, AddAssign, SubAssign, MulAssign, DivAssign}; +use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign}; use std::marker::PhantomData; define_matrix! { @@ -64,7 +64,11 @@ impl TypedVector2D { /// Constructor taking scalar values directly. #[inline] pub fn new(x: T, y: T) -> Self { - TypedVector2D { x: x, y: y, _unit: PhantomData } + TypedVector2D { + x: x, + y: y, + _unit: PhantomData, + } } } @@ -101,14 +105,17 @@ impl TypedVector2D { size2(self.x, self.y) } - /// Returns self.x as a Length carrying the unit. #[inline] - pub fn x_typed(&self) -> Length { Length::new(self.x) } + pub fn x_typed(&self) -> Length { + Length::new(self.x) + } /// Returns self.y as a Length carrying the unit. #[inline] - pub fn y_typed(&self) -> Length { Length::new(self.y) } + pub fn y_typed(&self) -> Length { + Length::new(self.y) + } /// Drop the units, preserving only the numeric value. #[inline] @@ -129,7 +136,9 @@ impl TypedVector2D { } impl TypedVector2D -where T: Trig + Copy + Sub { +where + T: Trig + Copy + Sub, +{ /// Returns the angle between this vector and the x axis between -PI and PI. pub fn angle_from_x_axis(&self) -> Angle { Angle::radians(Trig::fast_atan2(self.y, self.x)) @@ -137,7 +146,9 @@ where T: Trig + Copy + Sub { } impl TypedVector2D -where T: Copy + Mul + Add + Sub { +where + T: Copy + Mul + Add + Sub, +{ /// Dot product. #[inline] pub fn dot(self, other: Self) -> T { @@ -151,7 +162,10 @@ where T: Copy + Mul + Add + Sub { } #[inline] - pub fn normalize(self) -> Self where T: Float { + pub fn normalize(self) -> Self + where + T: Float, + { self / self.length() } @@ -161,13 +175,18 @@ where T: Copy + Mul + Add + Sub { } #[inline] - pub fn length(&self) -> T where T: Float { + pub fn length(&self) -> T + where + T: Float, + { self.square_length().sqrt() } } impl TypedVector2D -where T: Copy + One + Add + Sub + Mul { +where + T: Copy + One + Add + Sub + Mul, +{ /// Linearly interpolate between this vector and another vector. /// /// `t` is expected to be between zero and one. @@ -178,28 +197,28 @@ where T: Copy + One + Add + Sub + Mul { } } -impl, U> Add for TypedVector2D { +impl, U> Add for TypedVector2D { type Output = Self; fn add(self, other: Self) -> Self { TypedVector2D::new(self.x + other.x, self.y + other.y) } } -impl, U> AddAssign for TypedVector2D { +impl, U> AddAssign for TypedVector2D { #[inline] fn add_assign(&mut self, other: Self) { *self = *self + other } } -impl, U> SubAssign> for TypedVector2D { +impl, U> SubAssign> for TypedVector2D { #[inline] fn sub_assign(&mut self, other: Self) { *self = *self - other } } -impl, U> Sub for TypedVector2D { +impl, U> Sub for TypedVector2D { type Output = Self; #[inline] fn sub(self, other: Self) -> Self { @@ -207,7 +226,7 @@ impl, U> Sub for TypedVector2D { } } -impl , U> Neg for TypedVector2D { +impl, U> Neg for TypedVector2D { type Output = Self; #[inline] fn neg(self) -> Self { @@ -218,7 +237,7 @@ impl , U> Neg for TypedVector2D { impl TypedVector2D { #[inline] pub fn min(self, other: Self) -> Self { - vec2(self.x.min(other.x), self.y.min(other.y)) + vec2(self.x.min(other.x), self.y.min(other.y)) } #[inline] @@ -227,7 +246,7 @@ impl TypedVector2D { } } -impl, U> Mul for TypedVector2D { +impl, U> Mul for TypedVector2D { type Output = Self; #[inline] fn mul(self, scale: T) -> Self { @@ -235,7 +254,7 @@ impl, U> Mul for TypedVector2D { } } -impl, U> Div for TypedVector2D { +impl, U> Div for TypedVector2D { type Output = Self; #[inline] fn div(self, scale: T) -> Self { @@ -243,21 +262,21 @@ impl, U> Div for TypedVector2D { } } -impl, U> MulAssign for TypedVector2D { +impl, U> MulAssign for TypedVector2D { #[inline] fn mul_assign(&mut self, scale: T) { *self = *self * scale } } -impl, U> DivAssign for TypedVector2D { +impl, U> DivAssign for TypedVector2D { #[inline] fn div_assign(&mut self, scale: T) { *self = *self / scale } } -impl, U1, U2> Mul> for TypedVector2D { +impl, U1, U2> Mul> for TypedVector2D { type Output = TypedVector2D; #[inline] fn mul(self, scale: TypedScale) -> TypedVector2D { @@ -265,7 +284,7 @@ impl, U1, U2> Mul> for TypedVec } } -impl, U1, U2> Div> for TypedVector2D { +impl, U1, U2> Div> for TypedVector2D { type Output = TypedVector2D; #[inline] fn div(self, scale: TypedScale) -> TypedVector2D { @@ -319,7 +338,7 @@ impl TypedVector2D { pub fn cast(&self) -> Option> { match (NumCast::from(self.x), NumCast::from(self.y)) { (Some(x), Some(y)) => Some(TypedVector2D::new(x, y)), - _ => None + _ => None, } } @@ -368,7 +387,7 @@ impl TypedVector2D { } } -impl, U> ApproxEq> for TypedVector2D { +impl, U> ApproxEq> for TypedVector2D { #[inline] fn approx_epsilon() -> Self { vec2(T::approx_epsilon(), T::approx_epsilon()) @@ -398,7 +417,9 @@ impl From<[T; 2]> for TypedVector2D { } impl TypedVector2D -where T: Signed { +where + T: Signed, +{ pub fn abs(&self) -> Self { vec2(self.x.abs(), self.y.abs()) } @@ -447,7 +468,12 @@ impl TypedVector3D { /// Constructor taking scalar values directly. #[inline] pub fn new(x: T, y: T, z: T) -> Self { - TypedVector3D { x: x, y: y, z: z, _unit: PhantomData } + TypedVector3D { + x: x, + y: y, + z: z, + _unit: PhantomData, + } } } @@ -486,18 +512,26 @@ impl TypedVector3D { /// Returns self.x as a Length carrying the unit. #[inline] - pub fn x_typed(&self) -> Length { Length::new(self.x) } + pub fn x_typed(&self) -> Length { + Length::new(self.x) + } /// Returns self.y as a Length carrying the unit. #[inline] - pub fn y_typed(&self) -> Length { Length::new(self.y) } + pub fn y_typed(&self) -> Length { + Length::new(self.y) + } /// Returns self.z as a Length carrying the unit. #[inline] - pub fn z_typed(&self) -> Length { Length::new(self.z) } + pub fn z_typed(&self) -> Length { + Length::new(self.z) + } #[inline] - pub fn to_array(&self) -> [T; 3] { [self.x, self.y, self.z] } + pub fn to_array(&self) -> [T; 3] { + [self.x, self.y, self.z] + } /// Drop the units, preserving only the numeric value. #[inline] @@ -518,17 +552,12 @@ impl TypedVector3D { } } -impl + - Add + - Sub + - Copy, U> TypedVector3D { - +impl + Add + Sub + Copy, U> + TypedVector3D { // Dot product. #[inline] pub fn dot(self, other: Self) -> T { - self.x * other.x + - self.y * other.y + - self.z * other.z + self.x * other.x + self.y * other.y + self.z * other.z } // Cross product. @@ -537,12 +566,15 @@ impl + vec3( self.y * other.z - self.z * other.y, self.z * other.x - self.x * other.z, - self.x * other.y - self.y * other.x + self.x * other.y - self.y * other.x, ) } #[inline] - pub fn normalize(self) -> Self where T: Float { + pub fn normalize(self) -> Self + where + T: Float, + { self / self.length() } @@ -552,13 +584,18 @@ impl + } #[inline] - pub fn length(&self) -> T where T: Float { + pub fn length(&self) -> T + where + T: Float, + { self.square_length().sqrt() } } impl TypedVector3D -where T: Copy + One + Add + Sub + Mul { +where + T: Copy + One + Add + Sub + Mul, +{ /// Linearly interpolate between this vector and another vector. /// /// `t` is expected to be between zero and one. @@ -569,7 +606,7 @@ where T: Copy + One + Add + Sub + Mul { } } -impl, U> Add for TypedVector3D { +impl, U> Add for TypedVector3D { type Output = Self; #[inline] fn add(self, other: Self) -> Self { @@ -577,7 +614,7 @@ impl, U> Add for TypedVector3D { } } -impl, U> Sub for TypedVector3D { +impl, U> Sub for TypedVector3D { type Output = Self; #[inline] fn sub(self, other: Self) -> Self { @@ -585,21 +622,21 @@ impl, U> Sub for TypedVector3D { } } -impl, U> AddAssign for TypedVector3D { +impl, U> AddAssign for TypedVector3D { #[inline] fn add_assign(&mut self, other: Self) { *self = *self + other } } -impl, U> SubAssign> for TypedVector3D { +impl, U> SubAssign> for TypedVector3D { #[inline] fn sub_assign(&mut self, other: Self) { *self = *self - other } } -impl , U> Neg for TypedVector3D { +impl, U> Neg for TypedVector3D { type Output = Self; #[inline] fn neg(self) -> Self { @@ -607,7 +644,7 @@ impl , U> Neg for TypedVector3D { } } -impl, U> Mul for TypedVector3D { +impl, U> Mul for TypedVector3D { type Output = Self; #[inline] fn mul(self, scale: T) -> Self { @@ -615,7 +652,7 @@ impl, U> Mul for TypedVector3D { } } -impl, U> Div for TypedVector3D { +impl, U> Div for TypedVector3D { type Output = Self; #[inline] fn div(self, scale: T) -> Self { @@ -623,14 +660,14 @@ impl, U> Div for TypedVector3D { } } -impl, U> MulAssign for TypedVector3D { +impl, U> MulAssign for TypedVector3D { #[inline] fn mul_assign(&mut self, scale: T) { *self = *self * scale } } -impl, U> DivAssign for TypedVector3D { +impl, U> DivAssign for TypedVector3D { #[inline] fn div_assign(&mut self, scale: T) { *self = *self / scale @@ -640,12 +677,20 @@ impl, U> DivAssign for TypedVector3D { impl TypedVector3D { #[inline] pub fn min(self, other: Self) -> Self { - vec3(self.x.min(other.x), self.y.min(other.y), self.z.min(other.z)) + vec3( + self.x.min(other.x), + self.y.min(other.y), + self.z.min(other.z), + ) } #[inline] pub fn max(self, other: Self) -> Self { - vec3(self.x.max(other.x), self.y.max(other.y), self.z.max(other.z)) + vec3( + self.x.max(other.x), + self.y.max(other.y), + self.z.max(other.z), + ) } } @@ -690,11 +735,13 @@ impl TypedVector3D { /// geometrically. Consider using `round()`, `ceil()` or `floor()` before casting. #[inline] pub fn cast(&self) -> Option> { - match (NumCast::from(self.x), - NumCast::from(self.y), - NumCast::from(self.z)) { + match ( + NumCast::from(self.x), + NumCast::from(self.y), + NumCast::from(self.z), + ) { (Some(x), Some(y), Some(z)) => Some(vec3(x, y, z)), - _ => None + _ => None, } } @@ -743,23 +790,24 @@ impl TypedVector3D { } } -impl, U> ApproxEq> for TypedVector3D { +impl, U> ApproxEq> for TypedVector3D { #[inline] fn approx_epsilon() -> Self { - vec3(T::approx_epsilon(), T::approx_epsilon(), T::approx_epsilon()) + vec3( + T::approx_epsilon(), + T::approx_epsilon(), + T::approx_epsilon(), + ) } #[inline] fn approx_eq(&self, other: &Self) -> bool { - self.x.approx_eq(&other.x) - && self.y.approx_eq(&other.y) - && self.z.approx_eq(&other.z) + self.x.approx_eq(&other.x) && self.y.approx_eq(&other.y) && self.z.approx_eq(&other.z) } #[inline] fn approx_eq_eps(&self, other: &Self, eps: &Self) -> bool { - self.x.approx_eq_eps(&other.x, &eps.x) - && self.y.approx_eq_eps(&other.y, &eps.y) + self.x.approx_eq_eps(&other.x, &eps.x) && self.y.approx_eq_eps(&other.y, &eps.y) && self.z.approx_eq_eps(&other.z, &eps.z) } } @@ -777,7 +825,9 @@ impl From<[T; 3]> for TypedVector3D { } impl TypedVector3D -where T: Signed { +where + T: Signed, +{ pub fn abs(&self) -> Self { vec3(self.x.abs(), self.y.abs(), self.z.abs()) } @@ -940,9 +990,11 @@ mod vector3d { let p0: Vec3 = Vec3::zero(); let p1: Vec3 = vec3(0.0, -6.0, 0.0); let p2: Vec3 = vec3(1.0, 2.0, -2.0); - assert!(p0.normalize().x.is_nan() && p0.normalize().y.is_nan() && p0.normalize().z.is_nan()); + assert!( + p0.normalize().x.is_nan() && p0.normalize().y.is_nan() && p0.normalize().z.is_nan() + ); assert_eq!(p1.normalize(), vec3(0.0, -1.0, 0.0)); - assert_eq!(p2.normalize(), vec3(1.0/3.0, 2.0/3.0, -2.0/3.0)); + assert_eq!(p2.normalize(), vec3(1.0 / 3.0, 2.0 / 3.0, -2.0 / 3.0)); } #[test]