Merge pull request #1 from DaseinPhaos/patchendline0

Add newline at end of file
This commit is contained in:
Luxko 2017-03-12 07:47:40 -05:00 committed by GitHub
commit 6bb3830a2e
5 changed files with 242 additions and 90 deletions

View file

@ -30,6 +30,8 @@ use point::Point3;
use rotation::{Rotation, Rotation3, Basis3}; use rotation::{Rotation, Rotation3, Basis3};
use vector::Vector3; use vector::Vector3;
#[cfg(feature = "use_simd")]
use simd::f32x4 as Simdf32x4;
/// A [quaternion](https://en.wikipedia.org/wiki/Quaternion) in scalar/vector /// A [quaternion](https://en.wikipedia.org/wiki/Quaternion) in scalar/vector
/// form. /// form.
@ -46,6 +48,30 @@ pub struct Quaternion<S> {
pub v: Vector3<S>, pub v: Vector3<S>,
} }
#[cfg(feature = "use_simd")]
impl From<Simdf32x4> for Quaternion<f32> {
#[inline]
fn from(f: Simdf32x4) -> Self {
unsafe {
let mut ret: Self = mem::uninitialized();
{
let ret_mut: &mut [f32; 4] = ret.as_mut();
f.store(ret_mut.as_mut(), 0 as usize);
}
ret
}
}
}
#[cfg(feature = "use_simd")]
impl Into<Simdf32x4> for Quaternion<f32> {
#[inline]
fn into(self) -> Simdf32x4 {
let self_ref: &[f32; 4] = self.as_ref();
Simdf32x4::load(self_ref.as_ref(), 0 as usize)
}
}
impl<S: BaseFloat> Quaternion<S> { impl<S: BaseFloat> Quaternion<S> {
/// Construct a new quaternion from one scalar component and three /// Construct a new quaternion from one scalar component and three
/// imaginary components /// imaginary components
@ -73,7 +99,7 @@ impl<S: BaseFloat> Quaternion<S> {
let mag_avg = (src.magnitude2() * dst.magnitude2()).sqrt(); let mag_avg = (src.magnitude2() * dst.magnitude2()).sqrt();
let dot = src.dot(dst); let dot = src.dot(dst);
if ulps_eq!(dot, &mag_avg) { if ulps_eq!(dot, &mag_avg) {
Quaternion::one() Quaternion::<S>::one()
} else if ulps_eq!(dot, &-mag_avg) { } else if ulps_eq!(dot, &-mag_avg) {
let axis = fallback.unwrap_or_else(|| { let axis = fallback.unwrap_or_else(|| {
let mut v = Vector3::unit_x().cross(src); let mut v = Vector3::unit_x().cross(src);
@ -151,7 +177,7 @@ impl<S: BaseFloat> Zero for Quaternion<S> {
#[inline] #[inline]
fn is_zero(&self) -> bool { fn is_zero(&self) -> bool {
ulps_eq!(self, &Quaternion::zero()) ulps_eq!(self, &Quaternion::<S>::zero())
} }
} }
@ -175,6 +201,7 @@ impl<S: BaseFloat> MetricSpace for Quaternion<S> {
} }
} }
#[cfg(not(feature = "use_simd"))]
impl<S: BaseFloat> InnerSpace for Quaternion<S> { impl<S: BaseFloat> InnerSpace for Quaternion<S> {
#[inline] #[inline]
fn dot(self, other: Quaternion<S>) -> S { fn dot(self, other: Quaternion<S>) -> S {
@ -182,6 +209,25 @@ impl<S: BaseFloat> InnerSpace for Quaternion<S> {
} }
} }
#[cfg(feature = "use_simd")]
impl<S: BaseFloat> InnerSpace for Quaternion<S> {
#[inline]
default fn dot(self, other: Quaternion<S>) -> S {
self.s * other.s + self.v.dot(other.v)
}
}
#[cfg(feature = "use_simd")]
impl InnerSpace for Quaternion<f32> {
#[inline]
fn dot(self, other: Quaternion<f32>) -> f32 {
let lhs: Simdf32x4 = self.into();
let rhs: Simdf32x4 = other.into();
let r = lhs * rhs;
r.extract(0) + r.extract(1) + r.extract(2) + r.extract(3)
}
}
impl<A> From<Euler<A>> for Quaternion<<A as Angle>::Unitless> where impl<A> From<Euler<A>> for Quaternion<<A as Angle>::Unitless> where
A: Angle + Into<Rad<<A as Angle>::Unitless>>, A: Angle + Into<Rad<<A as Angle>::Unitless>>,
{ {
@ -203,35 +249,119 @@ impl<A> From<Euler<A>> for Quaternion<<A as Angle>::Unitless> where
} }
} }
#[cfg(not(feature = "use_simd"))]
impl_operator!(<S: BaseFloat> Neg for Quaternion<S> { impl_operator!(<S: BaseFloat> Neg for Quaternion<S> {
fn neg(quat) -> Quaternion<S> { fn neg(quat) -> Quaternion<S> {
Quaternion::from_sv(-quat.s, -quat.v) Quaternion::from_sv(-quat.s, -quat.v)
} }
}); });
#[cfg(feature = "use_simd")]
impl_operator_default!(<S: BaseFloat> Neg for Quaternion<S> {
fn neg(quat) -> Quaternion<S> {
Quaternion::from_sv(-quat.s, -quat.v)
}
});
#[cfg(feature = "use_simd")]
impl_operator_simd!{
[Simdf32x4]; Neg for Quaternion<f32> {
fn neg(lhs) -> Quaternion<f32> {
(-lhs).into()
}
}
}
#[cfg(not(feature = "use_simd"))]
impl_operator!(<S: BaseFloat> Mul<S> for Quaternion<S> { impl_operator!(<S: BaseFloat> Mul<S> for Quaternion<S> {
fn mul(lhs, rhs) -> Quaternion<S> { fn mul(lhs, rhs) -> Quaternion<S> {
Quaternion::from_sv(lhs.s * rhs, lhs.v * rhs) Quaternion::from_sv(lhs.s * rhs, lhs.v * rhs)
} }
}); });
#[cfg(feature = "use_simd")]
impl_operator_default!(<S: BaseFloat> Mul<S> for Quaternion<S> {
fn mul(lhs, rhs) -> Quaternion<S> {
Quaternion::from_sv(lhs.s * rhs, lhs.v * rhs)
}
});
#[cfg(feature = "use_simd")]
impl_operator_simd!{@rs
[Simdf32x4]; Mul<f32> for Quaternion<f32> {
fn mul(lhs, rhs) -> Quaternion<f32> {
(lhs * rhs).into()
}
}
}
#[cfg(not(feature = "use_simd"))]
impl_assignment_operator!(<S: BaseFloat> MulAssign<S> for Quaternion<S> { impl_assignment_operator!(<S: BaseFloat> MulAssign<S> for Quaternion<S> {
fn mul_assign(&mut self, scalar) { self.s *= scalar; self.v *= scalar; } fn mul_assign(&mut self, scalar) { self.s *= scalar; self.v *= scalar; }
}); });
#[cfg(feature = "use_simd")]
impl_assignment_operator_default!(<S: BaseFloat> MulAssign<S> for Quaternion<S> {
fn mul_assign(&mut self, scalar) { self.s *= scalar; self.v *= scalar; }
});
#[cfg(feature = "use_simd")]
impl MulAssign<f32> for Quaternion<f32> {
fn mul_assign(&mut self, other: f32) {
let s: Simdf32x4 = (*self).into();
let other = Simdf32x4::splat(other);
*self = (s * other).into();
}
}
#[cfg(not(feature = "use_simd"))]
impl_operator!(<S: BaseFloat> Div<S> for Quaternion<S> { impl_operator!(<S: BaseFloat> Div<S> for Quaternion<S> {
fn div(lhs, rhs) -> Quaternion<S> { fn div(lhs, rhs) -> Quaternion<S> {
Quaternion::from_sv(lhs.s / rhs, lhs.v / rhs) Quaternion::from_sv(lhs.s / rhs, lhs.v / rhs)
} }
}); });
#[cfg(feature = "use_simd")]
impl_operator_default!(<S: BaseFloat> Div<S> for Quaternion<S> {
fn div(lhs, rhs) -> Quaternion<S> {
Quaternion::from_sv(lhs.s / rhs, lhs.v / rhs)
}
});
#[cfg(feature = "use_simd")]
impl_operator_simd!{@rs
[Simdf32x4]; Div<f32> for Quaternion<f32> {
fn div(lhs, rhs) -> Quaternion<f32> {
(lhs / rhs).into()
}
}
}
#[cfg(not(feature = "use_simd"))]
impl_assignment_operator!(<S: BaseFloat> DivAssign<S> for Quaternion<S> { impl_assignment_operator!(<S: BaseFloat> DivAssign<S> for Quaternion<S> {
fn div_assign(&mut self, scalar) { self.s /= scalar; self.v /= scalar; } fn div_assign(&mut self, scalar) { self.s /= scalar; self.v /= scalar; }
}); });
#[cfg(feature = "use_simd")]
impl_assignment_operator_default!(<S: BaseFloat> DivAssign<S> for Quaternion<S> {
fn div_assign(&mut self, scalar) { self.s /= scalar; self.v /= scalar; }
});
#[cfg(feature = "use_simd")]
impl DivAssign<f32> for Quaternion<f32> {
fn div_assign(&mut self, other: f32) {
let s: Simdf32x4 = (*self).into();
let other = Simdf32x4::splat(other);
*self = (s / other).into();
}
}
impl_operator!(<S: BaseFloat> Rem<S> for Quaternion<S> { impl_operator!(<S: BaseFloat> Rem<S> for Quaternion<S> {
fn rem(lhs, rhs) -> Quaternion<S> { fn rem(lhs, rhs) -> Quaternion<S> {
Quaternion::from_sv(lhs.s % rhs, lhs.v % rhs) Quaternion::from_sv(lhs.s % rhs, lhs.v % rhs)
} }
}); });
impl_assignment_operator!(<S: BaseFloat> RemAssign<S> for Quaternion<S> { impl_assignment_operator!(<S: BaseFloat> RemAssign<S> for Quaternion<S> {
fn rem_assign(&mut self, scalar) { self.s %= scalar; self.v %= scalar; } fn rem_assign(&mut self, scalar) { self.s %= scalar; self.v %= scalar; }
}); });
@ -245,24 +375,93 @@ impl_operator!(<S: BaseFloat> Mul<Vector3<S> > for Quaternion<S> {
}} }}
}); });
#[cfg(not(feature = "use_simd"))]
impl_operator!(<S: BaseFloat> Add<Quaternion<S> > for Quaternion<S> { impl_operator!(<S: BaseFloat> Add<Quaternion<S> > for Quaternion<S> {
fn add(lhs, rhs) -> Quaternion<S> { fn add(lhs, rhs) -> Quaternion<S> {
Quaternion::from_sv(lhs.s + rhs.s, lhs.v + rhs.v) Quaternion::from_sv(lhs.s + rhs.s, lhs.v + rhs.v)
} }
}); });
#[cfg(feature = "use_simd")]
impl_operator_default!(<S: BaseFloat> Add<Quaternion<S> > for Quaternion<S> {
fn add(lhs, rhs) -> Quaternion<S> {
Quaternion::from_sv(lhs.s + rhs.s, lhs.v + rhs.v)
}
});
#[cfg(feature = "use_simd")]
impl_operator_simd!{
[Simdf32x4]; Add<Quaternion<f32>> for Quaternion<f32> {
fn add(lhs, rhs) -> Quaternion<f32> {
(lhs + rhs).into()
}
}
}
#[cfg(not(feature = "use_simd"))]
impl_assignment_operator!(<S: BaseFloat> AddAssign<Quaternion<S> > for Quaternion<S> { impl_assignment_operator!(<S: BaseFloat> AddAssign<Quaternion<S> > for Quaternion<S> {
fn add_assign(&mut self, other) { self.s += other.s; self.v += other.v; } fn add_assign(&mut self, other) { self.s += other.s; self.v += other.v; }
}); });
#[cfg(feature = "use_simd")]
impl_assignment_operator_default!(<S: BaseFloat> AddAssign<Quaternion<S> > for Quaternion<S> {
fn add_assign(&mut self, other) { self.s += other.s; self.v += other.v; }
});
#[cfg(feature = "use_simd")]
impl AddAssign for Quaternion<f32> {
#[inline]
fn add_assign(&mut self, rhs: Self) {
let s: Simdf32x4 = (*self).into();
let rhs: Simdf32x4 = rhs.into();
*self = (s + rhs).into();
}
}
#[cfg(not(feature = "use_simd"))]
impl_operator!(<S: BaseFloat> Sub<Quaternion<S> > for Quaternion<S> { impl_operator!(<S: BaseFloat> Sub<Quaternion<S> > for Quaternion<S> {
fn sub(lhs, rhs) -> Quaternion<S> { fn sub(lhs, rhs) -> Quaternion<S> {
Quaternion::from_sv(lhs.s - rhs.s, lhs.v - rhs.v) Quaternion::from_sv(lhs.s - rhs.s, lhs.v - rhs.v)
} }
}); });
#[cfg(feature = "use_simd")]
impl_operator_default!(<S: BaseFloat> Sub<Quaternion<S> > for Quaternion<S> {
fn sub(lhs, rhs) -> Quaternion<S> {
Quaternion::from_sv(lhs.s - rhs.s, lhs.v - rhs.v)
}
});
#[cfg(feature = "use_simd")]
impl_operator_simd!{
[Simdf32x4]; Sub<Quaternion<f32>> for Quaternion<f32> {
fn sub(lhs, rhs) -> Quaternion<f32> {
(lhs - rhs).into()
}
}
}
#[cfg(not(feature = "use_simd"))]
impl_assignment_operator!(<S: BaseFloat> SubAssign<Quaternion<S> > for Quaternion<S> { impl_assignment_operator!(<S: BaseFloat> SubAssign<Quaternion<S> > for Quaternion<S> {
fn sub_assign(&mut self, other) { self.s -= other.s; self.v -= other.v; } fn sub_assign(&mut self, other) { self.s -= other.s; self.v -= other.v; }
}); });
#[cfg(feature = "use_simd")]
impl_assignment_operator_default!(<S: BaseFloat> SubAssign<Quaternion<S> > for Quaternion<S> {
fn sub_assign(&mut self, other) { self.s -= other.s; self.v -= other.v; }
});
#[cfg(feature = "use_simd")]
impl SubAssign for Quaternion<f32> {
#[inline]
fn sub_assign(&mut self, rhs: Self) {
let s: Simdf32x4 = (*self).into();
let rhs: Simdf32x4 = rhs.into();
*self = (s - rhs).into();
}
}
#[cfg(not(feature = "use_simd"))]
impl_operator!(<S: BaseFloat> Mul<Quaternion<S> > for Quaternion<S> { impl_operator!(<S: BaseFloat> Mul<Quaternion<S> > for Quaternion<S> {
fn mul(lhs, rhs) -> Quaternion<S> { fn mul(lhs, rhs) -> Quaternion<S> {
Quaternion::new(lhs.s * rhs.s - lhs.v.x * rhs.v.x - lhs.v.y * rhs.v.y - lhs.v.z * rhs.v.z, Quaternion::new(lhs.s * rhs.s - lhs.v.x * rhs.v.x - lhs.v.y * rhs.v.y - lhs.v.z * rhs.v.z,
@ -272,6 +471,37 @@ impl_operator!(<S: BaseFloat> Mul<Quaternion<S> > for Quaternion<S> {
} }
}); });
#[cfg(feature = "use_simd")]
impl_operator_default!(<S: BaseFloat> Mul<Quaternion<S> > for Quaternion<S> {
fn mul(lhs, rhs) -> Quaternion<S> {
Quaternion::new(lhs.s * rhs.s - lhs.v.x * rhs.v.x - lhs.v.y * rhs.v.y - lhs.v.z * rhs.v.z,
lhs.s * rhs.v.x + lhs.v.x * rhs.s + lhs.v.y * rhs.v.z - lhs.v.z * rhs.v.y,
lhs.s * rhs.v.y + lhs.v.y * rhs.s + lhs.v.z * rhs.v.x - lhs.v.x * rhs.v.z,
lhs.s * rhs.v.z + lhs.v.z * rhs.s + lhs.v.x * rhs.v.y - lhs.v.y * rhs.v.x)
}
});
#[cfg(feature = "use_simd")]
impl_operator_simd!{
[Simdf32x4]; Mul<Quaternion<f32>> for Quaternion<f32> {
fn mul(lhs, rhs) -> Quaternion<f32> {
{
let p0 = Simdf32x4::splat(lhs.extract(0)) * rhs;
let p1 = Simdf32x4::splat(lhs.extract(1)) * Simdf32x4::new(
-rhs.extract(1), rhs.extract(0), -rhs.extract(3), rhs.extract(2)
);
let p2 = Simdf32x4::splat(lhs.extract(2)) * Simdf32x4::new(
-rhs.extract(2), rhs.extract(3), rhs.extract(0), -rhs.extract(1)
);
let p3 = Simdf32x4::splat(lhs.extract(3)) * Simdf32x4::new(
-rhs.extract(3), -rhs.extract(2), rhs.extract(1), rhs.extract(0)
);
(p0 + p1 + p2 + p3).into()
}
}
}
}
macro_rules! impl_scalar_mul { macro_rules! impl_scalar_mul {
($S:ident) => { ($S:ident) => {
impl_operator!(Mul<Quaternion<$S>> for $S { impl_operator!(Mul<Quaternion<$S>> for $S {

View file

@ -802,6 +802,14 @@ impl Into<Simdf32x4> for Vector4<f32> {
} }
} }
#[cfg(feature = "use_simd")]
impl_operator_simd!{
[Simdf32x4]; Add<Vector4<f32>> for Vector4<f32> {
fn add(lhs, rhs) -> Vector4<f32> {
(lhs + rhs).into()
}
}
}
#[cfg(feature = "use_simd")] #[cfg(feature = "use_simd")]
impl_operator_simd!{ impl_operator_simd!{
@ -1050,7 +1058,6 @@ impl Into<Simdu32x4> for Vector4<u32> {
} }
} }
#[cfg(feature = "use_simd")] #[cfg(feature = "use_simd")]
impl_operator_simd!{ impl_operator_simd!{
[Simdu32x4]; Add<Vector4<u32>> for Vector4<u32> { [Simdu32x4]; Add<Vector4<u32>> for Vector4<u32> {
@ -1060,15 +1067,6 @@ impl_operator_simd!{
} }
} }
#[cfg(feature = "use_simd")]
impl_operator_simd!{
[Simdf32x4]; Add<Vector4<f32>> for Vector4<f32> {
fn add(lhs, rhs) -> Vector4<f32> {
(lhs + rhs).into()
}
}
}
#[cfg(feature = "use_simd")] #[cfg(feature = "use_simd")]
impl_operator_simd!{ impl_operator_simd!{
[Simdu32x4]; Sub<Vector4<u32>> for Vector4<u32> { [Simdu32x4]; Sub<Vector4<u32>> for Vector4<u32> {

View file

@ -23,15 +23,11 @@ use std::f32;
#[test] #[test]
fn test_constructor() { fn test_constructor() {
assert_eq!(vec2(1f32, 2f32), Vector2::new(1f32, 2f32));
assert_eq!(vec3(1f32, 2f32, 3f32), Vector3::new(1f32, 2f32, 3f32));
assert_eq!(vec4(1f32, 2f32, 3f32, 4f32), Vector4::new(1f32, 2f32, 3f32, 4f32)); assert_eq!(vec4(1f32, 2f32, 3f32, 4f32), Vector4::new(1f32, 2f32, 3f32, 4f32));
} }
#[test] #[test]
fn test_from_value() { fn test_from_value() {
assert_eq!(Vector2::from_value(102f32), Vector2::new(102f32, 102f32));
assert_eq!(Vector3::from_value(22f32), Vector3::new(22f32, 22f32, 22f32));
assert_eq!(Vector4::from_value(76.5f32), Vector4::new(76.5f32, 76.5f32, 76.5f32, 76.5f32)); assert_eq!(Vector4::from_value(76.5f32), Vector4::new(76.5f32, 76.5f32, 76.5f32, 76.5f32));
} }
@ -95,94 +91,58 @@ fn test_add() {
#[test] #[test]
fn test_sub() { fn test_sub() {
impl_test_sub!(Vector4 { x, y, z, w }, 2.0f32, vec4(2.0f32, 4.0f32, 6.0f32, 8.0f32)); impl_test_sub!(Vector4 { x, y, z, w }, 2.0f32, vec4(2.0f32, 4.0f32, 6.0f32, 8.0f32));
impl_test_sub!(Vector3 { x, y, z }, 2.0f32, vec3(2.0f32, 4.0f32, 6.0f32));
impl_test_sub!(Vector2 { x, y }, 2.0f32, vec2(2.0f32, 4.0f32));
} }
#[test] #[test]
fn test_mul() { fn test_mul() {
impl_test_mul!(Vector4 { x, y, z, w }, 2.0f32, vec4(2.0f32, 4.0f32, 6.0f32, 8.0f32)); impl_test_mul!(Vector4 { x, y, z, w }, 2.0f32, vec4(2.0f32, 4.0f32, 6.0f32, 8.0f32));
impl_test_mul!(Vector3 { x, y, z }, 2.0f32, vec3(2.0f32, 4.0f32, 6.0f32));
impl_test_mul!(Vector2 { x, y }, 2.0f32, vec2(2.0f32, 4.0f32));
} }
#[test] #[test]
fn test_div() { fn test_div() {
impl_test_div!(Vector4 { x, y, z, w }, 2.0f32, vec4(2.0f32, 4.0f32, 6.0f32, 8.0f32)); impl_test_div!(Vector4 { x, y, z, w }, 2.0f32, vec4(2.0f32, 4.0f32, 6.0f32, 8.0f32));
impl_test_div!(Vector3 { x, y, z }, 2.0f32, vec3(2.0f32, 4.0f32, 6.0f32));
impl_test_div!(Vector2 { x, y }, 2.0f32, vec2(2.0f32, 4.0f32));
} }
#[test] #[test]
fn test_rem() { fn test_rem() {
impl_test_rem!(Vector4 { x, y, z, w }, 2.0f32, vec4(2.0f32, 4.0f32, 6.0f32, 8.0f32)); impl_test_rem!(Vector4 { x, y, z, w }, 2.0f32, vec4(2.0f32, 4.0f32, 6.0f32, 8.0f32));
impl_test_rem!(Vector3 { x, y, z }, 2.0f32, vec3(2.0f32, 4.0f32, 6.0f32));
impl_test_rem!(Vector2 { x, y }, 2.0f32, vec2(2.0f32, 4.0f32));
} }
#[test] #[test]
fn test_dot() { fn test_dot() {
assert_eq!(Vector2::new(1.0f32, 2.0f32).dot(Vector2::new(3.0f32, 4.0f32)), 11.0f32);
assert_eq!(Vector3::new(1.0f32, 2.0f32, 3.0f32).dot(Vector3::new(4.0f32, 5.0f32, 6.0f32)), 32.0f32);
assert_eq!(Vector4::new(1.0f32, 2.0f32, 3.0f32, 4.0f32).dot(Vector4::new(5.0f32, 6.0f32, 7.0f32, 8.0f32)), 70.0f32); assert_eq!(Vector4::new(1.0f32, 2.0f32, 3.0f32, 4.0f32).dot(Vector4::new(5.0f32, 6.0f32, 7.0f32, 8.0f32)), 70.0f32);
} }
#[test] #[test]
fn test_sum() { fn test_sum() {
assert_eq!(Vector2::new(1f32, 2f32).sum(), 3f32);
assert_eq!(Vector3::new(1f32, 2f32, 3f32).sum(), 6f32);
assert_eq!(Vector4::new(1f32, 2f32, 3f32, 4f32).sum(), 10f32); assert_eq!(Vector4::new(1f32, 2f32, 3f32, 4f32).sum(), 10f32);
assert_eq!(Vector2::new(3.0f32, 4.0f32).sum(), 7.0f32);
assert_eq!(Vector3::new(4.0f32, 5.0f32, 6.0f32).sum(), 15.0f32);
assert_eq!(Vector4::new(5.0f32, 6.0f32, 7.0f32, 8.0f32).sum(), 26.0f32); assert_eq!(Vector4::new(5.0f32, 6.0f32, 7.0f32, 8.0f32).sum(), 26.0f32);
} }
#[test] #[test]
fn test_product() { fn test_product() {
assert_eq!(Vector2::new(1f32, 2f32).product(), 2f32);
assert_eq!(Vector3::new(1f32, 2f32, 3f32).product(), 6f32);
assert_eq!(Vector4::new(1f32, 2f32, 3f32, 4f32).product(), 24f32); assert_eq!(Vector4::new(1f32, 2f32, 3f32, 4f32).product(), 24f32);
assert_eq!(Vector2::new(3.0f32, 4.0f32).product(), 12.0f32);
assert_eq!(Vector3::new(4.0f32, 5.0f32, 6.0f32).product(), 120.0f32);
assert_eq!(Vector4::new(5.0f32, 6.0f32, 7.0f32, 8.0f32).product(), 1680.0f32); assert_eq!(Vector4::new(5.0f32, 6.0f32, 7.0f32, 8.0f32).product(), 1680.0f32);
} }
#[test] #[test]
fn test_min() { fn test_min() {
assert_eq!(Vector2::new(1f32, 2f32).min(), 1f32);
assert_eq!(Vector3::new(1f32, 2f32, 3f32).min(), 1f32);
assert_eq!(Vector4::new(1f32, 2f32, 3f32, 4f32).min(), 1f32); assert_eq!(Vector4::new(1f32, 2f32, 3f32, 4f32).min(), 1f32);
assert_eq!(Vector2::new(3.0f32, 4.0f32).min(), 3.0f32);
assert_eq!(Vector3::new(4.0f32, 5.0f32, 6.0f32).min(), 4.0f32);
assert_eq!(Vector4::new(5.0f32, 6.0f32, 7.0f32, 8.0f32).min(), 5.0f32); assert_eq!(Vector4::new(5.0f32, 6.0f32, 7.0f32, 8.0f32).min(), 5.0f32);
} }
#[test] #[test]
fn test_max() { fn test_max() {
assert_eq!(Vector2::new(1f32, 2f32).max(), 2f32);
assert_eq!(Vector3::new(1f32, 2f32, 3f32).max(), 3f32);
assert_eq!(Vector4::new(1f32, 2f32, 3f32, 4f32).max(), 4f32); assert_eq!(Vector4::new(1f32, 2f32, 3f32, 4f32).max(), 4f32);
assert_eq!(Vector2::new(3.0f32, 4.0f32).max(), 4.0f32);
assert_eq!(Vector3::new(4.0f32, 5.0f32, 6.0f32).max(), 6.0f32);
assert_eq!(Vector4::new(5.0f32, 6.0f32, 7.0f32, 8.0f32).max(), 8.0f32); assert_eq!(Vector4::new(5.0f32, 6.0f32, 7.0f32, 8.0f32).max(), 8.0f32);
} }
#[test]
fn test_cross() {
let a = Vector3::new(1f32, 2f32, 3f32);
let b = Vector3::new(4f32, 5f32, 6f32);
let r = Vector3::new(-3f32, 6f32, -3f32);
assert_eq!(a.cross(b), r);
}
#[test] #[test]
fn test_is_perpendicular() { fn test_is_perpendicular() {
assert!(Vector2::new(1.0f32, 0.0f32).is_perpendicular(Vector2::new(0.0f32, 1.0f32)));
assert!(Vector3::new(0.0f32, 1.0f32, 0.0f32).is_perpendicular(Vector3::new(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))); assert!(Vector4::new(1.0f32, 0.0f32, 0.0f32, 0.0f32).is_perpendicular(Vector4::new(0.0f32, 0.0f32, 0.0f32, 1.0f32)));
} }
@ -190,30 +150,6 @@ fn test_is_perpendicular() {
mod test_magnitude { mod test_magnitude {
use cgmath::*; use cgmath::*;
#[test]
fn test_vector2(){
let (a, a_res) = (Vector2::new(3.0f32, 4.0f32), 5.0f32); // (3, 4, 5) Pythagorean triple
let (b, b_res) = (Vector2::new(5.0f32, 12.0f32), 13.0f32); // (5, 12, 13) Pythagorean triple
assert_eq!(a.magnitude2(), a_res * a_res);
assert_eq!(b.magnitude2(), b_res * b_res);
assert_eq!(a.magnitude(), a_res);
assert_eq!(b.magnitude(), b_res);
}
#[test]
fn test_vector3(){
let (a, a_res) = (Vector3::new(2.0f32, 3.0f32, 6.0f32), 7.0f32); // (2, 3, 6, 7) Pythagorean quadruple
let (b, b_res) = (Vector3::new(1.0f32, 4.0f32, 8.0f32), 9.0f32); // (1, 4, 8, 9) Pythagorean quadruple
assert_eq!(a.magnitude2(), a_res * a_res);
assert_eq!(b.magnitude2(), b_res * b_res);
assert_eq!(a.magnitude(), a_res);
assert_eq!(b.magnitude(), b_res);
}
#[test] #[test]
fn test_vector4(){ fn test_vector4(){
let (a, a_res) = (Vector4::new(1.0f32, 2.0f32, 4.0f32, 10.0f32), 11.0f32); // (1, 2, 4, 10, 11) Pythagorean quintuple let (a, a_res) = (Vector4::new(1.0f32, 2.0f32, 4.0f32, 10.0f32), 11.0f32); // (1, 2, 4, 10, 11) Pythagorean quintuple
@ -238,14 +174,6 @@ mod test_magnitude {
#[test] #[test]
fn test_angle() { fn test_angle() {
assert_ulps_eq!(Vector2::new(1.0f32, 0.0f32).angle(Vector2::new(0.0f32, 1.0f32)), &Rad(f32::consts::FRAC_PI_2));
assert_ulps_eq!(Vector2::new(10.0f32, 0.0f32).angle(Vector2::new(0.0f32, 5.0f32)), &Rad(f32::consts::FRAC_PI_2));
assert_ulps_eq!(Vector2::new(-1.0f32, 0.0f32).angle(Vector2::new(0.0f32, 1.0f32)), &-Rad(f32::consts::FRAC_PI_2));
assert_ulps_eq!(Vector3::new(1.0f32, 0.0f32, 1.0f32).angle(Vector3::new(1.0f32, 1.0f32, 0.0f32)), &Rad(f32::consts::FRAC_PI_3));
assert_ulps_eq!(Vector3::new(10.0f32, 0.0f32, 10.0f32).angle(Vector3::new(5.0f32, 5.0f32, 0.0f32)), &Rad(f32::consts::FRAC_PI_3));
assert_ulps_eq!(Vector3::new(-1.0f32, 0.0f32, -1.0f32).angle(Vector3::new(1.0f32, -1.0f32, 0.0f32)), &Rad(2.0f32 * f32::consts::FRAC_PI_3));
assert_ulps_eq!(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(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)), &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)), &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)), &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)), &Rad(f32::consts::FRAC_PI_2));
@ -254,14 +182,10 @@ fn test_angle() {
#[test] #[test]
fn test_normalize() { fn test_normalize() {
// TODO: test normalize_to, normalize_sel.0f32, and normalize_self_to // TODO: test normalize_to, normalize_sel.0f32, and normalize_self_to
assert_ulps_eq!(Vector2::new(3.0f32, 4.0f32).normalize(), &Vector2::new(3.0f32/5.0f32, 4.0f32/5.0f32));
assert_ulps_eq!(Vector3::new(2.0f32, 3.0f32, 6.0f32).normalize(), &Vector3::new(2.0f32/7.0f32, 3.0f32/7.0f32, 6.0f32/7.0f32));
assert_ulps_eq!(Vector4::new(1.0f32, 2.0f32, 4.0f32, 10.0f32).normalize(), &Vector4::new(1.0f32/11.0f32, 2.0f32/11.0f32, 4.0f32/11.0f32, 10.0f32/11.0f32)); assert_ulps_eq!(Vector4::new(1.0f32, 2.0f32, 4.0f32, 10.0f32).normalize(), &Vector4::new(1.0f32/11.0f32, 2.0f32/11.0f32, 4.0f32/11.0f32, 10.0f32/11.0f32));
} }
#[test] #[test]
fn test_cast() { fn test_cast() {
assert_ulps_eq!(Vector2::new(0.9f32, 1.5).cast(), Vector2::new(0.9f32, 1.5));
assert_ulps_eq!(Vector3::new(1.0f32, 2.4, -3.13).cast(), Vector3::new(1.0f32, 2.4, -3.13));
assert_ulps_eq!(Vector4::new(13.5f32, -4.6, -8.3, 2.41).cast(), Vector4::new(13.5f32, -4.6, -8.3, 2.41)); assert_ulps_eq!(Vector4::new(13.5f32, -4.6, -8.3, 2.41).cast(), Vector4::new(13.5f32, -4.6, -8.3, 2.41));
} }