diff --git a/README.md b/README.md index f86f97b..8cde705 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Lmath-rs -A linear algebra library for Rust, targeted at computer graphics. +A mathematics library for computer graphics. ## Examples diff --git a/src/mat.rs b/src/mat.rs index 2ebbfdb..f459840 100644 --- a/src/mat.rs +++ b/src/mat.rs @@ -14,35 +14,925 @@ // limitations under the License. pub use dim::Dimensional; -pub use self::mat2::{Mat2, ToMat2}; -pub use self::mat3::{Mat3, ToMat3}; -pub use self::mat4::{Mat4, ToMat4}; -pub mod mat2; -pub mod mat3; -pub mod mat4; +use quat::{Quat, ToQuat}; +use vec::{Vec2, Vec3, Vec4}; + +mod num_macros; +mod dim_macros; +mod mat_macros; + +#[deriving(Eq)] +pub struct Mat2 { + x: Vec2, + y: Vec2, +} // GLSL-style type aliases - pub type mat2 = Mat2; pub type dmat2 = Mat2; -pub type mat3 = Mat3; -pub type dmat3 = Mat3; - -pub type mat4 = Mat4; -pub type dmat4 = Mat4; - // Rust-style type aliases - pub type Mat2f = Mat2; pub type Mat2f32 = Mat2; pub type Mat2f64 = Mat2; +impl_dimensional!(Mat2, Vec2, 2) +impl_dimensional_fns!(Mat2, Vec2, 2) +impl_approx!(Mat2) + +impl_mat!(Mat2, Vec2) +impl_mat_copyable!(Mat2, Vec2) +impl_mat_numeric!(Mat2, Vec2) +impl_mat_approx_numeric!(Mat2) +impl_mat_neg!(Mat2) + +pub trait ToMat2 { + pub fn to_mat2(&self) -> Mat2; +} + +impl Mat2 { + #[inline] + pub fn new(c0r0: T, c0r1: T, + c1r0: T, c1r1: T) -> Mat2 { + Mat2::from_cols(Vec2::new(c0r0, c0r1), + Vec2::new(c1r0, c1r1)) + } + + #[inline] + pub fn from_cols(c0: Vec2, + c1: Vec2) -> Mat2 { + Mat2 { x: c0, y: c1 } + } +} + +impl ToMat3 for Mat2 { + #[inline] + pub fn to_mat3(&self) -> Mat3 { + Mat3::new(*self.elem(0, 0), *self.elem(0, 1), zero!(T), + *self.elem(1, 0), *self.elem(1, 1), zero!(T), + zero!(T), zero!(T), one!(T)) + } +} + +impl ToMat4 for Mat2 { + #[inline] + pub fn to_mat4(&self) -> Mat4 { + Mat4::new(*self.elem(0, 0), *self.elem(0, 1), zero!(T), zero!(T), + *self.elem(1, 0), *self.elem(1, 1), zero!(T), zero!(T), + zero!(T), zero!(T), one!(T), zero!(T), + zero!(T), zero!(T), zero!(T), one!(T)) + } +} + +impl Mat2 { + #[inline] + pub fn from_angle(radians: T) -> Mat2 { + let cos_theta = radians.cos(); + let sin_theta = radians.sin(); + + Mat2::new(cos_theta, -sin_theta, + sin_theta, cos_theta) + } +} + +#[cfg(test)] +mod mat2_tests{ + use mat::*; + use vec::*; + + #[test] + fn test_mat2() { + let a = Mat2 { x: Vec2 { x: 1.0, y: 3.0 }, + y: Vec2 { x: 2.0, y: 4.0 } }; + let b = Mat2 { x: Vec2 { x: 2.0, y: 4.0 }, + y: Vec2 { x: 3.0, y: 5.0 } }; + + let v1 = Vec2::new::(1.0, 2.0); + let f1 = 0.5; + + assert_eq!(a, Mat2::new::(1.0, 3.0, + 2.0, 4.0)); + + assert_eq!(a, Mat2::from_cols::(Vec2::new::(1.0, 3.0), + Vec2::new::(2.0, 4.0))); + + assert_eq!(Mat2::from_value::(4.0), + Mat2::new::(4.0, 0.0, + 0.0, 4.0)); + + assert_eq!(*a.col(0), Vec2::new::(1.0, 3.0)); + assert_eq!(*a.col(1), Vec2::new::(2.0, 4.0)); + + assert_eq!(a.row(0), Vec2::new::(1.0, 2.0)); + assert_eq!(a.row(1), Vec2::new::(3.0, 4.0)); + + assert_eq!(*a.col(0), Vec2::new::(1.0, 3.0)); + assert_eq!(*a.col(1), Vec2::new::(2.0, 4.0)); + + assert_eq!(Mat2::identity::(), + Mat2::new::(1.0, 0.0, + 0.0, 1.0)); + + assert_eq!(Mat2::zero::(), + Mat2::new::(0.0, 0.0, + 0.0, 0.0)); + + assert_eq!(a.determinant(), -2.0); + assert_eq!(a.trace(), 5.0); + + assert_eq!(a.neg(), + Mat2::new::(-1.0, -3.0, + -2.0, -4.0)); + assert_eq!(-a, a.neg()); + assert_eq!(a.mul_t(f1), + Mat2::new::(0.5, 1.5, + 1.0, 2.0)); + assert_eq!(a.mul_v(&v1), Vec2::new::(5.0, 11.0)); + assert_eq!(a.add_m(&b), + Mat2::new::(3.0, 7.0, + 5.0, 9.0)); + assert_eq!(a.sub_m(&b), + Mat2::new::(-1.0, -1.0, + -1.0, -1.0)); + assert_eq!(a.mul_m(&b), + Mat2::new::(10.0, 22.0, + 13.0, 29.0)); + assert_eq!(a.dot(&b), 40.0); + + assert_eq!(a.transpose(), + Mat2::new::(1.0, 2.0, + 3.0, 4.0)); + + assert_eq!(a.inverse().unwrap(), + Mat2::new::(-2.0, 1.5, + 1.0, -0.5)); + + assert!(Mat2::new::(0.0, 2.0, + 0.0, 5.0).inverse().is_none()); + + let ident = Mat2::identity::(); + + assert!(ident.is_identity()); + assert!(ident.is_symmetric()); + assert!(ident.is_diagonal()); + assert!(!ident.is_rotated()); + assert!(ident.is_invertible()); + + assert!(!a.is_identity()); + assert!(!a.is_symmetric()); + assert!(!a.is_diagonal()); + assert!(a.is_rotated()); + assert!(a.is_invertible()); + + let c = Mat2::new::(2.0, 1.0, + 1.0, 2.0); + assert!(!c.is_identity()); + assert!(c.is_symmetric()); + assert!(!c.is_diagonal()); + assert!(c.is_rotated()); + assert!(c.is_invertible()); + + assert!(Mat2::from_value::(6.0).is_diagonal()); + + assert_eq!(a.to_mat3(), + Mat3::new::(1.0, 3.0, 0.0, + 2.0, 4.0, 0.0, + 0.0, 0.0, 1.0)); + + assert_eq!(a.to_mat4(), + Mat4::new::(1.0, 3.0, 0.0, 0.0, + 2.0, 4.0, 0.0, 0.0, + 0.0, 0.0, 1.0, 0.0, + 0.0, 0.0, 0.0, 1.0)); + } + + fn test_mat2_mut() { + let a = Mat2 { x: Vec2 { x: 1.0, y: 3.0 }, + y: Vec2 { x: 2.0, y: 4.0 } }; + let b = Mat2 { x: Vec2 { x: 2.0, y: 4.0 }, + y: Vec2 { x: 3.0, y: 5.0 } }; + + let f1 = 0.5; + + let mut mut_a = a; + + mut_a.swap_cols(0, 1); + assert_eq!(mut_a.col(0), a.col(1)); + assert_eq!(mut_a.col(1), a.col(0)); + mut_a = a; + + mut_a.swap_rows(0, 1); + assert_eq!(mut_a.row(0), a.row(1)); + assert_eq!(mut_a.row(1), a.row(0)); + mut_a = a; + + mut_a.to_identity(); + assert!(mut_a.is_identity()); + mut_a = a; + + mut_a.to_zero(); + assert_eq!(mut_a, Mat2::zero::()); + mut_a = a; + + mut_a.mul_self_t(f1); + assert_eq!(mut_a, a.mul_t(f1)); + mut_a = a; + + mut_a.add_self_m(&b); + assert_eq!(mut_a, a.add_m(&b)); + mut_a = a; + + mut_a.sub_self_m(&b); + assert_eq!(mut_a, a.sub_m(&b)); + mut_a = a; + + mut_a.invert_self(); + assert_eq!(mut_a, a.inverse().unwrap()); + mut_a = a; + + mut_a.transpose_self(); + assert_eq!(mut_a, a.transpose()); + // mut_a = a; + } + + #[test] + fn test_mat2_approx_eq() { + assert!(!Mat2::new::(0.000001, 0.000001, + 0.000001, 0.000001).approx_eq(&Mat2::zero::())); + assert!(Mat2::new::(0.0000001, 0.0000001, + 0.0000001, 0.0000001).approx_eq(&Mat2::zero::())); + } +} + +#[deriving(Eq)] +pub struct Mat3 { + x: Vec3, + y: Vec3, + z: Vec3, +} + +// GLSL-style type aliases +pub type mat3 = Mat3; +pub type dmat3 = Mat3; + +// Rust-style type aliases pub type Mat3f = Mat3; pub type Mat3f32 = Mat3; pub type Mat3f64 = Mat3; +impl_dimensional!(Mat3, Vec3, 3) +impl_dimensional_fns!(Mat3, Vec3, 3) +impl_approx!(Mat3) + +impl_mat!(Mat3, Vec3) +impl_mat_copyable!(Mat3, Vec3) +impl_mat_numeric!(Mat3, Vec3) +impl_mat_approx_numeric!(Mat3) +impl_mat_neg!(Mat3) + +pub trait ToMat3 { + pub fn to_mat3(&self) -> Mat3; +} + +impl Mat3 { + #[inline] + pub fn new(c0r0:T, c0r1:T, c0r2:T, + c1r0:T, c1r1:T, c1r2:T, + c2r0:T, c2r1:T, c2r2:T) -> Mat3 { + Mat3::from_cols(Vec3::new(c0r0, c0r1, c0r2), + Vec3::new(c1r0, c1r1, c1r2), + Vec3::new(c2r0, c2r1, c2r2)) + } + + #[inline] + pub fn from_cols(c0: Vec3, + c1: Vec3, + c2: Vec3) -> Mat3 { + Mat3 { x: c0, y: c1, z: c2 } + } +} + +impl ToMat4 for Mat3 { + #[inline] + pub fn to_mat4(&self) -> Mat4 { + Mat4::new(*self.elem(0, 0), *self.elem(0, 1), *self.elem(0, 2), zero!(T), + *self.elem(1, 0), *self.elem(1, 1), *self.elem(1, 2), zero!(T), + *self.elem(2, 0), *self.elem(2, 1), *self.elem(2, 2), zero!(T), + zero!(T), zero!(T), zero!(T), one!(T)) + } +} + +impl Mat3 { + /// Construct a matrix from an angular rotation around the `x` axis + pub fn from_angle_x(radians: T) -> Mat3 { + // http://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations + let cos_theta = radians.cos(); + let sin_theta = radians.sin(); + + Mat3::new(one!(T), zero!(T), zero!(T), + zero!(T), cos_theta, sin_theta, + zero!(T), -sin_theta, cos_theta) + } + + /// Construct a matrix from an angular rotation around the `y` axis + pub fn from_angle_y(radians: T) -> Mat3 { + // http://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations + let cos_theta = radians.cos(); + let sin_theta = radians.sin(); + + Mat3::new(cos_theta, zero!(T), -sin_theta, + zero!(T), one!(T), zero!(T), + sin_theta, zero!(T), cos_theta) + } + + /// Construct a matrix from an angular rotation around the `z` axis + pub fn from_angle_z(radians: T) -> Mat3 { + // http://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations + let cos_theta = radians.cos(); + let sin_theta = radians.sin(); + + Mat3::new(cos_theta, sin_theta, zero!(T), + -sin_theta, cos_theta, zero!(T), + zero!(T), zero!(T), one!(T)) + } + + /// Construct a matrix from Euler angles + /// + /// # Arguments + /// + /// - `theta_x`: the angular rotation around the `x` axis (pitch) + /// - `theta_y`: the angular rotation around the `y` axis (yaw) + /// - `theta_z`: the angular rotation around the `z` axis (roll) + pub fn from_angle_xyz(radians_x: T, radians_y: T, radians_z: T) -> Mat3 { + // http://en.wikipedia.org/wiki/Rotation_matrix#General_rotations + let cx = radians_x.cos(); + let sx = radians_x.sin(); + let cy = radians_y.cos(); + let sy = radians_y.sin(); + let cz = radians_z.cos(); + let sz = radians_z.sin(); + + Mat3::new(cy*cz, cy*sz, -sy, + -cx*sz + sx*sy*cz, cx*cz + sx*sy*sz, sx*cy, + sx*sz + cx*sy*cz, -sx*cz + cx*sy*sz, cx*cy) + } + + /// Construct a matrix from an axis and an angular rotation + pub fn from_angle_axis(radians: T, axis: &Vec3) -> Mat3 { + let c = radians.cos(); + let s = radians.sin(); + let _1_c = one!(T) - c; + + let x = axis.x; + let y = axis.y; + let z = axis.z; + + Mat3::new(_1_c*x*x + c, _1_c*x*y + s*z, _1_c*x*z - s*y, + _1_c*x*y - s*z, _1_c*y*y + c, _1_c*y*z + s*x, + _1_c*x*z + s*y, _1_c*y*z - s*x, _1_c*z*z + c) + } + + #[inline] + pub fn from_axes(x: Vec3, y: Vec3, z: Vec3) -> Mat3 { + Mat3::from_cols(x, y, z) + } + + pub fn look_at(dir: &Vec3, up: &Vec3) -> Mat3 { + let dir_ = dir.normalize(); + let side = dir_.cross(&up.normalize()); + let up_ = side.cross(&dir_).normalize(); + + Mat3::from_axes(up_, side, dir_) + } +} + +impl ToQuat for Mat3 { + /// Convert the matrix to a quaternion + pub fn to_quat(&self) -> Quat { + // Implemented using a mix of ideas from jMonkeyEngine and Ken Shoemake's + // paper on Quaternions: http://www.cs.ucr.edu/~vbz/resources/Quatut.pdf + + let mut s; + let w; let x; let y; let z; + let trace = self.trace(); + + // FIXME: We don't have any numeric conversions in std yet :P + let half = one!(T) / two!(T); + + cond! ( + (trace >= zero!(T)) { + s = (one!(T) + trace).sqrt(); + w = half * s; + s = half / s; + x = (*self.elem(1, 2) - *self.elem(2, 1)) * s; + y = (*self.elem(2, 0) - *self.elem(0, 2)) * s; + z = (*self.elem(0, 1) - *self.elem(1, 0)) * s; + } + ((*self.elem(0, 0) > *self.elem(1, 1)) + && (*self.elem(0, 0) > *self.elem(2, 2))) { + s = (half + (*self.elem(0, 0) - *self.elem(1, 1) - *self.elem(2, 2))).sqrt(); + w = half * s; + s = half / s; + x = (*self.elem(0, 1) - *self.elem(1, 0)) * s; + y = (*self.elem(2, 0) - *self.elem(0, 2)) * s; + z = (*self.elem(1, 2) - *self.elem(2, 1)) * s; + } + (*self.elem(1, 1) > *self.elem(2, 2)) { + s = (half + (*self.elem(1, 1) - *self.elem(0, 0) - *self.elem(2, 2))).sqrt(); + w = half * s; + s = half / s; + x = (*self.elem(0, 1) - *self.elem(1, 0)) * s; + y = (*self.elem(1, 2) - *self.elem(2, 1)) * s; + z = (*self.elem(2, 0) - *self.elem(0, 2)) * s; + } + _ { + s = (half + (*self.elem(2, 2) - *self.elem(0, 0) - *self.elem(1, 1))).sqrt(); + w = half * s; + s = half / s; + x = (*self.elem(2, 0) - *self.elem(0, 2)) * s; + y = (*self.elem(1, 2) - *self.elem(2, 1)) * s; + z = (*self.elem(0, 1) - *self.elem(1, 0)) * s; + } + ) + Quat::new(w, x, y, z) + } +} + +#[cfg(test)] +mod mat3_tests{ + use mat::*; + use vec::*; + + #[test] + fn test_mat3() { + let a = Mat3 { x: Vec3 { x: 1.0, y: 4.0, z: 7.0 }, + y: Vec3 { x: 2.0, y: 5.0, z: 8.0 }, + z: Vec3 { x: 3.0, y: 6.0, z: 9.0 } }; + let b = Mat3 { x: Vec3 { x: 2.0, y: 5.0, z: 8.0 }, + y: Vec3 { x: 3.0, y: 6.0, z: 9.0 }, + z: Vec3 { x: 4.0, y: 7.0, z: 10.0 } }; + + let v1 = Vec3::new::(1.0, 2.0, 3.0); + let f1 = 0.5; + + assert_eq!(a, Mat3::new::(1.0, 4.0, 7.0, + 2.0, 5.0, 8.0, + 3.0, 6.0, 9.0)); + + assert_eq!(a, Mat3::from_cols::(Vec3::new::(1.0, 4.0, 7.0), + Vec3::new::(2.0, 5.0, 8.0), + Vec3::new::(3.0, 6.0, 9.0))); + + assert_eq!(*a.col(0), Vec3::new::(1.0, 4.0, 7.0)); + assert_eq!(*a.col(1), Vec3::new::(2.0, 5.0, 8.0)); + assert_eq!(*a.col(2), Vec3::new::(3.0, 6.0, 9.0)); + + assert_eq!(a.row(0), Vec3::new::(1.0, 2.0, 3.0)); + assert_eq!(a.row(1), Vec3::new::(4.0, 5.0, 6.0)); + assert_eq!(a.row(2), Vec3::new::(7.0, 8.0, 9.0)); + + assert_eq!(*a.col(0), Vec3::new::(1.0, 4.0, 7.0)); + assert_eq!(*a.col(1), Vec3::new::(2.0, 5.0, 8.0)); + assert_eq!(*a.col(2), Vec3::new::(3.0, 6.0, 9.0)); + + assert_eq!(Mat3::identity::(), + Mat3::new::(1.0, 0.0, 0.0, + 0.0, 1.0, 0.0, + 0.0, 0.0, 1.0)); + + assert_eq!(Mat3::zero::(), + Mat3::new::(0.0, 0.0, 0.0, + 0.0, 0.0, 0.0, + 0.0, 0.0, 0.0)); + + assert_eq!(a.determinant(), 0.0); + assert_eq!(a.trace(), 15.0); + + assert_eq!(a.neg(), + Mat3::new::(-1.0, -4.0, -7.0, + -2.0, -5.0, -8.0, + -3.0, -6.0, -9.0)); + assert_eq!(-a, a.neg()); + + assert_eq!(a.mul_t(f1), + Mat3::new::(0.5, 2.0, 3.5, + 1.0, 2.5, 4.0, + 1.5, 3.0, 4.5)); + assert_eq!(a.mul_v(&v1), Vec3::new::(14.0, 32.0, 50.0)); + + assert_eq!(a.add_m(&b), + Mat3::new::(3.0, 9.0, 15.0, + 5.0, 11.0, 17.0, + 7.0, 13.0, 19.0)); + assert_eq!(a.sub_m(&b), + Mat3::new::(-1.0, -1.0, -1.0, + -1.0, -1.0, -1.0, + -1.0, -1.0, -1.0)); + assert_eq!(a.mul_m(&b), + Mat3::new::(36.0, 81.0, 126.0, + 42.0, 96.0, 150.0, + 48.0, 111.0, 174.0)); + assert_eq!(a.dot(&b), 330.0); + + assert_eq!(a.transpose(), + Mat3::new::(1.0, 2.0, 3.0, + 4.0, 5.0, 6.0, + 7.0, 8.0, 9.0)); + + assert!(a.inverse().is_none()); + + assert_eq!(Mat3::new::(2.0, 4.0, 6.0, + 0.0, 2.0, 4.0, + 0.0, 0.0, 1.0).inverse().unwrap(), + Mat3::new::(0.5, -1.0, 1.0, + 0.0, 0.5, -2.0, + 0.0, 0.0, 1.0)); + + let ident = Mat3::identity::(); + + assert_eq!(ident.inverse().unwrap(), ident); + + assert!(ident.is_identity()); + assert!(ident.is_symmetric()); + assert!(ident.is_diagonal()); + assert!(!ident.is_rotated()); + assert!(ident.is_invertible()); + + assert!(!a.is_identity()); + assert!(!a.is_symmetric()); + assert!(!a.is_diagonal()); + assert!(a.is_rotated()); + assert!(!a.is_invertible()); + + let c = Mat3::new::(3.0, 2.0, 1.0, + 2.0, 3.0, 2.0, + 1.0, 2.0, 3.0); + assert!(!c.is_identity()); + assert!(c.is_symmetric()); + assert!(!c.is_diagonal()); + assert!(c.is_rotated()); + assert!(c.is_invertible()); + + assert!(Mat3::from_value::(6.0).is_diagonal()); + + assert_eq!(a.to_mat4(), + Mat4::new::(1.0, 4.0, 7.0, 0.0, + 2.0, 5.0, 8.0, 0.0, + 3.0, 6.0, 9.0, 0.0, + 0.0, 0.0, 0.0, 1.0)); + + // to_Quaternion + } + + fn test_mat3_mut() { + let a = Mat3 { x: Vec3 { x: 1.0, y: 4.0, z: 7.0 }, + y: Vec3 { x: 2.0, y: 5.0, z: 8.0 }, + z: Vec3 { x: 3.0, y: 6.0, z: 9.0 } }; + let b = Mat3 { x: Vec3 { x: 2.0, y: 5.0, z: 8.0 }, + y: Vec3 { x: 3.0, y: 6.0, z: 9.0 }, + z: Vec3 { x: 4.0, y: 7.0, z: 10.0 } }; + let c = Mat3 { x: Vec3 { x: 2.0, y: 4.0, z: 6.0 }, + y: Vec3 { x: 0.0, y: 2.0, z: 4.0 }, + z: Vec3 { x: 0.0, y: 0.0, z: 1.0 } }; + + let f1 = 0.5; + + let mut mut_a = a; + let mut mut_c = c; + + mut_a.swap_cols(0, 2); + assert_eq!(mut_a.col(0), a.col(2)); + assert_eq!(mut_a.col(2), a.col(0)); + mut_a = a; + + mut_a.swap_cols(1, 2); + assert_eq!(mut_a.col(1), a.col(2)); + assert_eq!(mut_a.col(2), a.col(1)); + mut_a = a; + + mut_a.swap_rows(0, 2); + assert_eq!(mut_a.row(0), a.row(2)); + assert_eq!(mut_a.row(2), a.row(0)); + mut_a = a; + + mut_a.swap_rows(1, 2); + assert_eq!(mut_a.row(1), a.row(2)); + assert_eq!(mut_a.row(2), a.row(1)); + mut_a = a; + + mut_a.to_identity(); + assert!(mut_a.is_identity()); + mut_a = a; + + mut_a.to_zero(); + assert_eq!(mut_a, Mat3::zero::()); + mut_a = a; + + mut_a.mul_self_t(f1); + assert_eq!(mut_a, a.mul_t(f1)); + mut_a = a; + + mut_a.add_self_m(&b); + assert_eq!(mut_a, a.add_m(&b)); + mut_a = a; + + mut_a.sub_self_m(&b); + assert_eq!(mut_a, a.sub_m(&b)); + mut_a = a; + + mut_c.invert_self(); + assert_eq!(mut_c, c.inverse().unwrap()); + // mut_c = c; + + mut_a.transpose_self(); + assert_eq!(mut_a, a.transpose()); + // mut_a = a; + } + + #[test] + fn test_mat3_approx_eq() { + assert!(!Mat3::new::(0.000001, 0.000001, 0.000001, + 0.000001, 0.000001, 0.000001, + 0.000001, 0.000001, 0.000001) + .approx_eq(&Mat3::zero::())); + assert!(Mat3::new::(0.0000001, 0.0000001, 0.0000001, + 0.0000001, 0.0000001, 0.0000001, + 0.0000001, 0.0000001, 0.0000001) + .approx_eq(&Mat3::zero::())); + } +} + +#[deriving(Eq)] +pub struct Mat4 { + x: Vec4, + y: Vec4, + z: Vec4, + w: Vec4, +} + +// GLSL-style type aliases +pub type mat4 = Mat4; +pub type dmat4 = Mat4; + +// Rust-style type aliases pub type Mat4f = Mat4; pub type Mat4f32 = Mat4; pub type Mat4f64 = Mat4; + +impl_dimensional!(Mat4, Vec4, 4) +impl_dimensional_fns!(Mat4, Vec4, 4) +impl_approx!(Mat4) + +impl_mat!(Mat4, Vec4) +impl_mat_copyable!(Mat4, Vec4) +impl_mat_numeric!(Mat4, Vec4) +impl_mat_approx_numeric!(Mat4) +impl_mat_neg!(Mat4) + +pub trait ToMat4 { + pub fn to_mat4(&self) -> Mat4; +} + +impl Mat4 { + #[inline] + pub fn new(c0r0: T, c0r1: T, c0r2: T, c0r3: T, + c1r0: T, c1r1: T, c1r2: T, c1r3: T, + c2r0: T, c2r1: T, c2r2: T, c2r3: T, + c3r0: T, c3r1: T, c3r2: T, c3r3: T) -> Mat4 { + Mat4::from_cols(Vec4::new(c0r0, c0r1, c0r2, c0r3), + Vec4::new(c1r0, c1r1, c1r2, c1r3), + Vec4::new(c2r0, c2r1, c2r2, c2r3), + Vec4::new(c3r0, c3r1, c3r2, c3r3)) + } + + #[inline] + pub fn from_cols(c0: Vec4, + c1: Vec4, + c2: Vec4, + c3: Vec4) -> Mat4 { + Mat4 { x: c0, y: c1, z: c2, w: c3 } + } +} + +#[cfg(test)] +mod mat4_tests { + use mat::*; + use vec::*; + + #[test] + fn test_mat4() { + let a = Mat4 { x: Vec4 { x: 1.0, y: 5.0, z: 9.0, w: 13.0 }, + y: Vec4 { x: 2.0, y: 6.0, z: 10.0, w: 14.0 }, + z: Vec4 { x: 3.0, y: 7.0, z: 11.0, w: 15.0 }, + w: Vec4 { x: 4.0, y: 8.0, z: 12.0, w: 16.0 } }; + let b = Mat4 { x: Vec4 { x: 2.0, y: 6.0, z: 10.0, w: 14.0 }, + y: Vec4 { x: 3.0, y: 7.0, z: 11.0, w: 15.0 }, + z: Vec4 { x: 4.0, y: 8.0, z: 12.0, w: 16.0 }, + w: Vec4 { x: 5.0, y: 9.0, z: 13.0, w: 17.0 } }; + let c = Mat4 { x: Vec4 { x: 3.0, y: 2.0, z: 1.0, w: 1.0 }, + y: Vec4 { x: 2.0, y: 3.0, z: 2.0, w: 2.0 }, + z: Vec4 { x: 1.0, y: 2.0, z: 3.0, w: 3.0 }, + w: Vec4 { x: 0.0, y: 1.0, z: 1.0, w: 0.0 } }; + + let v1 = Vec4::new::(1.0, 2.0, 3.0, 4.0); + let f1 = 0.5; + + assert_eq!(a, Mat4::new::(1.0, 5.0, 9.0, 13.0, + 2.0, 6.0, 10.0, 14.0, + 3.0, 7.0, 11.0, 15.0, + 4.0, 8.0, 12.0, 16.0)); + + assert_eq!(a, Mat4::from_cols::(Vec4::new::(1.0, 5.0, 9.0, 13.0), + Vec4::new::(2.0, 6.0, 10.0, 14.0), + Vec4::new::(3.0, 7.0, 11.0, 15.0), + Vec4::new::(4.0, 8.0, 12.0, 16.0))); + + assert_eq!(Mat4::from_value::(4.0), + Mat4::new::(4.0, 0.0, 0.0, 0.0, + 0.0, 4.0, 0.0, 0.0, + 0.0, 0.0, 4.0, 0.0, + 0.0, 0.0, 0.0, 4.0)); + + assert_eq!(*a.col(0), Vec4::new::(1.0, 5.0, 9.0, 13.0)); + assert_eq!(*a.col(1), Vec4::new::(2.0, 6.0, 10.0, 14.0)); + assert_eq!(*a.col(2), Vec4::new::(3.0, 7.0, 11.0, 15.0)); + assert_eq!(*a.col(3), Vec4::new::(4.0, 8.0, 12.0, 16.0)); + + assert_eq!(a.row(0), Vec4::new::( 1.0, 2.0, 3.0, 4.0)); + assert_eq!(a.row(1), Vec4::new::( 5.0, 6.0, 7.0, 8.0)); + assert_eq!(a.row(2), Vec4::new::( 9.0, 10.0, 11.0, 12.0)); + assert_eq!(a.row(3), Vec4::new::(13.0, 14.0, 15.0, 16.0)); + + assert_eq!(*a.col(0), Vec4::new::(1.0, 5.0, 9.0, 13.0)); + assert_eq!(*a.col(1), Vec4::new::(2.0, 6.0, 10.0, 14.0)); + assert_eq!(*a.col(2), Vec4::new::(3.0, 7.0, 11.0, 15.0)); + assert_eq!(*a.col(3), Vec4::new::(4.0, 8.0, 12.0, 16.0)); + + assert_eq!(Mat4::identity::(), + Mat4::new::(1.0, 0.0, 0.0, 0.0, + 0.0, 1.0, 0.0, 0.0, + 0.0, 0.0, 1.0, 0.0, + 0.0, 0.0, 0.0, 1.0)); + + assert_eq!(Mat4::zero::(), + Mat4::new::(0.0, 0.0, 0.0, 0.0, + 0.0, 0.0, 0.0, 0.0, + 0.0, 0.0, 0.0, 0.0, + 0.0, 0.0, 0.0, 0.0)); + + assert_eq!(a.determinant(), 0.0); + assert_eq!(a.trace(), 34.0); + + assert_eq!(a.neg(), + Mat4::new::(-1.0, -5.0, -9.0, -13.0, + -2.0, -6.0, -10.0, -14.0, + -3.0, -7.0, -11.0, -15.0, + -4.0, -8.0, -12.0, -16.0)); + assert_eq!(-a, a.neg()); + assert_eq!(a.mul_t(f1), + Mat4::new::(0.5, 2.5, 4.5, 6.5, + 1.0, 3.0, 5.0, 7.0, + 1.5, 3.5, 5.5, 7.5, + 2.0, 4.0, 6.0, 8.0)); + assert_eq!(a.mul_v(&v1), + Vec4::new::(30.0, 70.0, 110.0, 150.0)); + assert_eq!(a.add_m(&b), + Mat4::new::(3.0, 11.0, 19.0, 27.0, + 5.0, 13.0, 21.0, 29.0, + 7.0, 15.0, 23.0, 31.0, + 9.0, 17.0, 25.0, 33.0)); + assert_eq!(a.sub_m(&b), + Mat4::new::(-1.0, -1.0, -1.0, -1.0, + -1.0, -1.0, -1.0, -1.0, + -1.0, -1.0, -1.0, -1.0, + -1.0, -1.0, -1.0, -1.0)); + assert_eq!(a.mul_m(&b), + Mat4::new::(100.0, 228.0, 356.0, 484.0, + 110.0, 254.0, 398.0, 542.0, + 120.0, 280.0, 440.0, 600.0, + 130.0, 306.0, 482.0, 658.0)); + assert_eq!(a.dot(&b), 1632.0); + assert_eq!(a.transpose(), + Mat4::new::( 1.0, 2.0, 3.0, 4.0, + 5.0, 6.0, 7.0, 8.0, + 9.0, 10.0, 11.0, 12.0, + 13.0, 14.0, 15.0, 16.0)); + + assert_approx_eq!(c.inverse().unwrap(), + Mat4::new::( 5.0, -4.0, 1.0, 0.0, + -4.0, 8.0, -4.0, 0.0, + 4.0, -8.0, 4.0, 8.0, + -3.0, 4.0, 1.0, -8.0).mul_t(0.125)); + + let ident = Mat4::identity::(); + + assert_eq!(ident.inverse().unwrap(), ident); + + assert!(ident.is_identity()); + assert!(ident.is_symmetric()); + assert!(ident.is_diagonal()); + assert!(!ident.is_rotated()); + assert!(ident.is_invertible()); + + assert!(!a.is_identity()); + assert!(!a.is_symmetric()); + assert!(!a.is_diagonal()); + assert!(a.is_rotated()); + assert!(!a.is_invertible()); + + let c = Mat4::new::(4.0, 3.0, 2.0, 1.0, + 3.0, 4.0, 3.0, 2.0, + 2.0, 3.0, 4.0, 3.0, + 1.0, 2.0, 3.0, 4.0); + assert!(!c.is_identity()); + assert!(c.is_symmetric()); + assert!(!c.is_diagonal()); + assert!(c.is_rotated()); + assert!(c.is_invertible()); + + assert!(Mat4::from_value::(6.0).is_diagonal()); + } + + fn test_mat4_mut() { + let a = Mat4 { x: Vec4 { x: 1.0, y: 5.0, z: 9.0, w: 13.0 }, + y: Vec4 { x: 2.0, y: 6.0, z: 10.0, w: 14.0 }, + z: Vec4 { x: 3.0, y: 7.0, z: 11.0, w: 15.0 }, + w: Vec4 { x: 4.0, y: 8.0, z: 12.0, w: 16.0 } }; + let b = Mat4 { x: Vec4 { x: 2.0, y: 6.0, z: 10.0, w: 14.0 }, + y: Vec4 { x: 3.0, y: 7.0, z: 11.0, w: 15.0 }, + z: Vec4 { x: 4.0, y: 8.0, z: 12.0, w: 16.0 }, + w: Vec4 { x: 5.0, y: 9.0, z: 13.0, w: 17.0 } }; + let c = Mat4 { x: Vec4 { x: 3.0, y: 2.0, z: 1.0, w: 1.0 }, + y: Vec4 { x: 2.0, y: 3.0, z: 2.0, w: 2.0 }, + z: Vec4 { x: 1.0, y: 2.0, z: 3.0, w: 3.0 }, + w: Vec4 { x: 0.0, y: 1.0, z: 1.0, w: 0.0 } }; + + let f1 = 0.5; + + let mut mut_a = a; + let mut mut_c = c; + + mut_a.swap_cols(0, 3); + assert_eq!(mut_a.col(0), a.col(3)); + assert_eq!(mut_a.col(3), a.col(0)); + mut_a = a; + + mut_a.swap_cols(1, 2); + assert_eq!(mut_a.col(1), a.col(2)); + assert_eq!(mut_a.col(2), a.col(1)); + mut_a = a; + + mut_a.swap_rows(0, 3); + assert_eq!(mut_a.row(0), a.row(3)); + assert_eq!(mut_a.row(3), a.row(0)); + mut_a = a; + + mut_a.swap_rows(1, 2); + assert_eq!(mut_a.row(1), a.row(2)); + assert_eq!(mut_a.row(2), a.row(1)); + mut_a = a; + + mut_a.to_identity(); + assert!(mut_a.is_identity()); + mut_a = a; + + mut_a.to_zero(); + assert_eq!(mut_a, Mat4::zero::()); + mut_a = a; + + mut_a.mul_self_t(f1); + assert_eq!(mut_a, a.mul_t(f1)); + mut_a = a; + + mut_a.add_self_m(&b); + assert_eq!(mut_a, a.add_m(&b)); + mut_a = a; + + mut_a.sub_self_m(&b); + assert_eq!(mut_a, a.sub_m(&b)); + mut_a = a; + + mut_c.invert_self(); + assert_eq!(mut_c, c.inverse().unwrap()); + // mut_c = c; + + mut_a.transpose_self(); + assert_eq!(mut_a, a.transpose()); + // mut_a = a; + } + + #[test] + fn test_mat4_approx_eq() { + assert!(!Mat4::new::(0.000001, 0.000001, 0.000001, 0.000001, + 0.000001, 0.000001, 0.000001, 0.000001, + 0.000001, 0.000001, 0.000001, 0.000001, + 0.000001, 0.000001, 0.000001, 0.000001) + .approx_eq(&Mat4::zero::())); + assert!(Mat4::new::(0.0000001, 0.0000001, 0.0000001, 0.0000001, + 0.0000001, 0.0000001, 0.0000001, 0.0000001, + 0.0000001, 0.0000001, 0.0000001, 0.0000001, + 0.0000001, 0.0000001, 0.0000001, 0.0000001) + .approx_eq(&Mat4::zero::())); + } +} diff --git a/src/mat2.rs b/src/mat2.rs deleted file mode 100644 index 5f96906..0000000 --- a/src/mat2.rs +++ /dev/null @@ -1,257 +0,0 @@ -// Copyright 2013 The Lmath Developers. For a full listing of the authors, -// refer to the AUTHORS file at the top-level directory of this distribution. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -use dim::Dimensional; -use mat::{Mat3, ToMat3}; -use mat::{Mat4, ToMat4}; -use vec::Vec2; - -mod num_macros; -mod dim_macros; -mod mat_macros; - -#[deriving(Eq)] -pub struct Mat2 { - x: Vec2, - y: Vec2, -} - -impl_dimensional!(Mat2, Vec2, 2) -impl_dimensional_fns!(Mat2, Vec2, 2) -impl_approx!(Mat2) - -impl_mat!(Mat2, Vec2) -impl_mat_copyable!(Mat2, Vec2) -impl_mat_numeric!(Mat2, Vec2) -impl_mat_approx_numeric!(Mat2) -impl_mat_neg!(Mat2) - -pub trait ToMat2 { - pub fn to_mat2(&self) -> Mat2; -} - -impl Mat2 { - #[inline] - pub fn new(c0r0: T, c0r1: T, - c1r0: T, c1r1: T) -> Mat2 { - Mat2::from_cols(Vec2::new(c0r0, c0r1), - Vec2::new(c1r0, c1r1)) - } - - #[inline] - pub fn from_cols(c0: Vec2, - c1: Vec2) -> Mat2 { - Mat2 { x: c0, y: c1 } - } -} - -impl ToMat3 for Mat2 { - #[inline] - pub fn to_mat3(&self) -> Mat3 { - Mat3::new(*self.elem(0, 0), *self.elem(0, 1), zero!(T), - *self.elem(1, 0), *self.elem(1, 1), zero!(T), - zero!(T), zero!(T), one!(T)) - } -} - -impl ToMat4 for Mat2 { - #[inline] - pub fn to_mat4(&self) -> Mat4 { - Mat4::new(*self.elem(0, 0), *self.elem(0, 1), zero!(T), zero!(T), - *self.elem(1, 0), *self.elem(1, 1), zero!(T), zero!(T), - zero!(T), zero!(T), one!(T), zero!(T), - zero!(T), zero!(T), zero!(T), one!(T)) - } -} - -impl Mat2 { - #[inline] - pub fn from_angle(radians: T) -> Mat2 { - let cos_theta = radians.cos(); - let sin_theta = radians.sin(); - - Mat2::new(cos_theta, -sin_theta, - sin_theta, cos_theta) - } -} - -#[cfg(test)] -mod tests{ - use mat::*; - use vec::*; - - #[test] - fn test_mat2() { - let a = Mat2 { x: Vec2 { x: 1.0, y: 3.0 }, - y: Vec2 { x: 2.0, y: 4.0 } }; - let b = Mat2 { x: Vec2 { x: 2.0, y: 4.0 }, - y: Vec2 { x: 3.0, y: 5.0 } }; - - let v1 = Vec2::new::(1.0, 2.0); - let f1 = 0.5; - - assert_eq!(a, Mat2::new::(1.0, 3.0, - 2.0, 4.0)); - - assert_eq!(a, Mat2::from_cols::(Vec2::new::(1.0, 3.0), - Vec2::new::(2.0, 4.0))); - - assert_eq!(Mat2::from_value::(4.0), - Mat2::new::(4.0, 0.0, - 0.0, 4.0)); - - assert_eq!(*a.col(0), Vec2::new::(1.0, 3.0)); - assert_eq!(*a.col(1), Vec2::new::(2.0, 4.0)); - - assert_eq!(a.row(0), Vec2::new::(1.0, 2.0)); - assert_eq!(a.row(1), Vec2::new::(3.0, 4.0)); - - assert_eq!(*a.col(0), Vec2::new::(1.0, 3.0)); - assert_eq!(*a.col(1), Vec2::new::(2.0, 4.0)); - - assert_eq!(Mat2::identity::(), - Mat2::new::(1.0, 0.0, - 0.0, 1.0)); - - assert_eq!(Mat2::zero::(), - Mat2::new::(0.0, 0.0, - 0.0, 0.0)); - - assert_eq!(a.determinant(), -2.0); - assert_eq!(a.trace(), 5.0); - - assert_eq!(a.neg(), - Mat2::new::(-1.0, -3.0, - -2.0, -4.0)); - assert_eq!(-a, a.neg()); - assert_eq!(a.mul_t(f1), - Mat2::new::(0.5, 1.5, - 1.0, 2.0)); - assert_eq!(a.mul_v(&v1), Vec2::new::(5.0, 11.0)); - assert_eq!(a.add_m(&b), - Mat2::new::(3.0, 7.0, - 5.0, 9.0)); - assert_eq!(a.sub_m(&b), - Mat2::new::(-1.0, -1.0, - -1.0, -1.0)); - assert_eq!(a.mul_m(&b), - Mat2::new::(10.0, 22.0, - 13.0, 29.0)); - assert_eq!(a.dot(&b), 40.0); - - assert_eq!(a.transpose(), - Mat2::new::(1.0, 2.0, - 3.0, 4.0)); - - assert_eq!(a.inverse().unwrap(), - Mat2::new::(-2.0, 1.5, - 1.0, -0.5)); - - assert!(Mat2::new::(0.0, 2.0, - 0.0, 5.0).inverse().is_none()); - - let ident = Mat2::identity::(); - - assert!(ident.is_identity()); - assert!(ident.is_symmetric()); - assert!(ident.is_diagonal()); - assert!(!ident.is_rotated()); - assert!(ident.is_invertible()); - - assert!(!a.is_identity()); - assert!(!a.is_symmetric()); - assert!(!a.is_diagonal()); - assert!(a.is_rotated()); - assert!(a.is_invertible()); - - let c = Mat2::new::(2.0, 1.0, - 1.0, 2.0); - assert!(!c.is_identity()); - assert!(c.is_symmetric()); - assert!(!c.is_diagonal()); - assert!(c.is_rotated()); - assert!(c.is_invertible()); - - assert!(Mat2::from_value::(6.0).is_diagonal()); - - assert_eq!(a.to_mat3(), - Mat3::new::(1.0, 3.0, 0.0, - 2.0, 4.0, 0.0, - 0.0, 0.0, 1.0)); - - assert_eq!(a.to_mat4(), - Mat4::new::(1.0, 3.0, 0.0, 0.0, - 2.0, 4.0, 0.0, 0.0, - 0.0, 0.0, 1.0, 0.0, - 0.0, 0.0, 0.0, 1.0)); - } - - fn test_mat2_mut() { - let a = Mat2 { x: Vec2 { x: 1.0, y: 3.0 }, - y: Vec2 { x: 2.0, y: 4.0 } }; - let b = Mat2 { x: Vec2 { x: 2.0, y: 4.0 }, - y: Vec2 { x: 3.0, y: 5.0 } }; - - let f1 = 0.5; - - let mut mut_a = a; - - mut_a.swap_cols(0, 1); - assert_eq!(mut_a.col(0), a.col(1)); - assert_eq!(mut_a.col(1), a.col(0)); - mut_a = a; - - mut_a.swap_rows(0, 1); - assert_eq!(mut_a.row(0), a.row(1)); - assert_eq!(mut_a.row(1), a.row(0)); - mut_a = a; - - mut_a.to_identity(); - assert!(mut_a.is_identity()); - mut_a = a; - - mut_a.to_zero(); - assert_eq!(mut_a, Mat2::zero::()); - mut_a = a; - - mut_a.mul_self_t(f1); - assert_eq!(mut_a, a.mul_t(f1)); - mut_a = a; - - mut_a.add_self_m(&b); - assert_eq!(mut_a, a.add_m(&b)); - mut_a = a; - - mut_a.sub_self_m(&b); - assert_eq!(mut_a, a.sub_m(&b)); - mut_a = a; - - mut_a.invert_self(); - assert_eq!(mut_a, a.inverse().unwrap()); - mut_a = a; - - mut_a.transpose_self(); - assert_eq!(mut_a, a.transpose()); - // mut_a = a; - } - - #[test] - fn test_mat2_approx_eq() { - assert!(!Mat2::new::(0.000001, 0.000001, - 0.000001, 0.000001).approx_eq(&Mat2::zero::())); - assert!(Mat2::new::(0.0000001, 0.0000001, - 0.0000001, 0.0000001).approx_eq(&Mat2::zero::())); - } -} diff --git a/src/mat3.rs b/src/mat3.rs deleted file mode 100644 index 51f5546..0000000 --- a/src/mat3.rs +++ /dev/null @@ -1,412 +0,0 @@ -// Copyright 2013 The Lmath Developers. For a full listing of the authors, -// refer to the AUTHORS file at the top-level directory of this distribution. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -use dim::Dimensional; -use mat::{Mat4, ToMat4}; -use quat::{Quat, ToQuat}; -use vec::Vec3; - -mod num_macros; -mod dim_macros; -mod mat_macros; - -#[deriving(Eq)] -pub struct Mat3 { - x: Vec3, - y: Vec3, - z: Vec3, -} - -impl_dimensional!(Mat3, Vec3, 3) -impl_dimensional_fns!(Mat3, Vec3, 3) -impl_approx!(Mat3) - -impl_mat!(Mat3, Vec3) -impl_mat_copyable!(Mat3, Vec3) -impl_mat_numeric!(Mat3, Vec3) -impl_mat_approx_numeric!(Mat3) -impl_mat_neg!(Mat3) - -pub trait ToMat3 { - pub fn to_mat3(&self) -> Mat3; -} - -impl Mat3 { - #[inline] - pub fn new(c0r0:T, c0r1:T, c0r2:T, - c1r0:T, c1r1:T, c1r2:T, - c2r0:T, c2r1:T, c2r2:T) -> Mat3 { - Mat3::from_cols(Vec3::new(c0r0, c0r1, c0r2), - Vec3::new(c1r0, c1r1, c1r2), - Vec3::new(c2r0, c2r1, c2r2)) - } - - #[inline] - pub fn from_cols(c0: Vec3, - c1: Vec3, - c2: Vec3) -> Mat3 { - Mat3 { x: c0, y: c1, z: c2 } - } -} - -impl ToMat4 for Mat3 { - #[inline] - pub fn to_mat4(&self) -> Mat4 { - Mat4::new(*self.elem(0, 0), *self.elem(0, 1), *self.elem(0, 2), zero!(T), - *self.elem(1, 0), *self.elem(1, 1), *self.elem(1, 2), zero!(T), - *self.elem(2, 0), *self.elem(2, 1), *self.elem(2, 2), zero!(T), - zero!(T), zero!(T), zero!(T), one!(T)) - } -} - -impl Mat3 { - /// Construct a matrix from an angular rotation around the `x` axis - pub fn from_angle_x(radians: T) -> Mat3 { - // http://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations - let cos_theta = radians.cos(); - let sin_theta = radians.sin(); - - Mat3::new(one!(T), zero!(T), zero!(T), - zero!(T), cos_theta, sin_theta, - zero!(T), -sin_theta, cos_theta) - } - - /// Construct a matrix from an angular rotation around the `y` axis - pub fn from_angle_y(radians: T) -> Mat3 { - // http://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations - let cos_theta = radians.cos(); - let sin_theta = radians.sin(); - - Mat3::new(cos_theta, zero!(T), -sin_theta, - zero!(T), one!(T), zero!(T), - sin_theta, zero!(T), cos_theta) - } - - /// Construct a matrix from an angular rotation around the `z` axis - pub fn from_angle_z(radians: T) -> Mat3 { - // http://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations - let cos_theta = radians.cos(); - let sin_theta = radians.sin(); - - Mat3::new(cos_theta, sin_theta, zero!(T), - -sin_theta, cos_theta, zero!(T), - zero!(T), zero!(T), one!(T)) - } - - /// Construct a matrix from Euler angles - /// - /// # Arguments - /// - /// - `theta_x`: the angular rotation around the `x` axis (pitch) - /// - `theta_y`: the angular rotation around the `y` axis (yaw) - /// - `theta_z`: the angular rotation around the `z` axis (roll) - pub fn from_angle_xyz(radians_x: T, radians_y: T, radians_z: T) -> Mat3 { - // http://en.wikipedia.org/wiki/Rotation_matrix#General_rotations - let cx = radians_x.cos(); - let sx = radians_x.sin(); - let cy = radians_y.cos(); - let sy = radians_y.sin(); - let cz = radians_z.cos(); - let sz = radians_z.sin(); - - Mat3::new(cy*cz, cy*sz, -sy, - -cx*sz + sx*sy*cz, cx*cz + sx*sy*sz, sx*cy, - sx*sz + cx*sy*cz, -sx*cz + cx*sy*sz, cx*cy) - } - - /// Construct a matrix from an axis and an angular rotation - pub fn from_angle_axis(radians: T, axis: &Vec3) -> Mat3 { - let c = radians.cos(); - let s = radians.sin(); - let _1_c = one!(T) - c; - - let x = axis.x; - let y = axis.y; - let z = axis.z; - - Mat3::new(_1_c*x*x + c, _1_c*x*y + s*z, _1_c*x*z - s*y, - _1_c*x*y - s*z, _1_c*y*y + c, _1_c*y*z + s*x, - _1_c*x*z + s*y, _1_c*y*z - s*x, _1_c*z*z + c) - } - - #[inline] - pub fn from_axes(x: Vec3, y: Vec3, z: Vec3) -> Mat3 { - Mat3::from_cols(x, y, z) - } - - pub fn look_at(dir: &Vec3, up: &Vec3) -> Mat3 { - let dir_ = dir.normalize(); - let side = dir_.cross(&up.normalize()); - let up_ = side.cross(&dir_).normalize(); - - Mat3::from_axes(up_, side, dir_) - } -} - -impl ToQuat for Mat3 { - /// Convert the matrix to a quaternion - pub fn to_quat(&self) -> Quat { - // Implemented using a mix of ideas from jMonkeyEngine and Ken Shoemake's - // paper on Quaternions: http://www.cs.ucr.edu/~vbz/resources/Quatut.pdf - - let mut s; - let w; let x; let y; let z; - let trace = self.trace(); - - // FIXME: We don't have any numeric conversions in std yet :P - let half = one!(T) / two!(T); - - cond! ( - (trace >= zero!(T)) { - s = (one!(T) + trace).sqrt(); - w = half * s; - s = half / s; - x = (*self.elem(1, 2) - *self.elem(2, 1)) * s; - y = (*self.elem(2, 0) - *self.elem(0, 2)) * s; - z = (*self.elem(0, 1) - *self.elem(1, 0)) * s; - } - ((*self.elem(0, 0) > *self.elem(1, 1)) - && (*self.elem(0, 0) > *self.elem(2, 2))) { - s = (half + (*self.elem(0, 0) - *self.elem(1, 1) - *self.elem(2, 2))).sqrt(); - w = half * s; - s = half / s; - x = (*self.elem(0, 1) - *self.elem(1, 0)) * s; - y = (*self.elem(2, 0) - *self.elem(0, 2)) * s; - z = (*self.elem(1, 2) - *self.elem(2, 1)) * s; - } - (*self.elem(1, 1) > *self.elem(2, 2)) { - s = (half + (*self.elem(1, 1) - *self.elem(0, 0) - *self.elem(2, 2))).sqrt(); - w = half * s; - s = half / s; - x = (*self.elem(0, 1) - *self.elem(1, 0)) * s; - y = (*self.elem(1, 2) - *self.elem(2, 1)) * s; - z = (*self.elem(2, 0) - *self.elem(0, 2)) * s; - } - _ { - s = (half + (*self.elem(2, 2) - *self.elem(0, 0) - *self.elem(1, 1))).sqrt(); - w = half * s; - s = half / s; - x = (*self.elem(2, 0) - *self.elem(0, 2)) * s; - y = (*self.elem(1, 2) - *self.elem(2, 1)) * s; - z = (*self.elem(0, 1) - *self.elem(1, 0)) * s; - } - ) - Quat::new(w, x, y, z) - } -} - -#[cfg(test)] -mod tests{ - use mat::*; - use vec::*; - - #[test] - fn test_mat3() { - let a = Mat3 { x: Vec3 { x: 1.0, y: 4.0, z: 7.0 }, - y: Vec3 { x: 2.0, y: 5.0, z: 8.0 }, - z: Vec3 { x: 3.0, y: 6.0, z: 9.0 } }; - let b = Mat3 { x: Vec3 { x: 2.0, y: 5.0, z: 8.0 }, - y: Vec3 { x: 3.0, y: 6.0, z: 9.0 }, - z: Vec3 { x: 4.0, y: 7.0, z: 10.0 } }; - - let v1 = Vec3::new::(1.0, 2.0, 3.0); - let f1 = 0.5; - - assert_eq!(a, Mat3::new::(1.0, 4.0, 7.0, - 2.0, 5.0, 8.0, - 3.0, 6.0, 9.0)); - - assert_eq!(a, Mat3::from_cols::(Vec3::new::(1.0, 4.0, 7.0), - Vec3::new::(2.0, 5.0, 8.0), - Vec3::new::(3.0, 6.0, 9.0))); - - assert_eq!(*a.col(0), Vec3::new::(1.0, 4.0, 7.0)); - assert_eq!(*a.col(1), Vec3::new::(2.0, 5.0, 8.0)); - assert_eq!(*a.col(2), Vec3::new::(3.0, 6.0, 9.0)); - - assert_eq!(a.row(0), Vec3::new::(1.0, 2.0, 3.0)); - assert_eq!(a.row(1), Vec3::new::(4.0, 5.0, 6.0)); - assert_eq!(a.row(2), Vec3::new::(7.0, 8.0, 9.0)); - - assert_eq!(*a.col(0), Vec3::new::(1.0, 4.0, 7.0)); - assert_eq!(*a.col(1), Vec3::new::(2.0, 5.0, 8.0)); - assert_eq!(*a.col(2), Vec3::new::(3.0, 6.0, 9.0)); - - assert_eq!(Mat3::identity::(), - Mat3::new::(1.0, 0.0, 0.0, - 0.0, 1.0, 0.0, - 0.0, 0.0, 1.0)); - - assert_eq!(Mat3::zero::(), - Mat3::new::(0.0, 0.0, 0.0, - 0.0, 0.0, 0.0, - 0.0, 0.0, 0.0)); - - assert_eq!(a.determinant(), 0.0); - assert_eq!(a.trace(), 15.0); - - assert_eq!(a.neg(), - Mat3::new::(-1.0, -4.0, -7.0, - -2.0, -5.0, -8.0, - -3.0, -6.0, -9.0)); - assert_eq!(-a, a.neg()); - - assert_eq!(a.mul_t(f1), - Mat3::new::(0.5, 2.0, 3.5, - 1.0, 2.5, 4.0, - 1.5, 3.0, 4.5)); - assert_eq!(a.mul_v(&v1), Vec3::new::(14.0, 32.0, 50.0)); - - assert_eq!(a.add_m(&b), - Mat3::new::(3.0, 9.0, 15.0, - 5.0, 11.0, 17.0, - 7.0, 13.0, 19.0)); - assert_eq!(a.sub_m(&b), - Mat3::new::(-1.0, -1.0, -1.0, - -1.0, -1.0, -1.0, - -1.0, -1.0, -1.0)); - assert_eq!(a.mul_m(&b), - Mat3::new::(36.0, 81.0, 126.0, - 42.0, 96.0, 150.0, - 48.0, 111.0, 174.0)); - assert_eq!(a.dot(&b), 330.0); - - assert_eq!(a.transpose(), - Mat3::new::(1.0, 2.0, 3.0, - 4.0, 5.0, 6.0, - 7.0, 8.0, 9.0)); - - assert!(a.inverse().is_none()); - - assert_eq!(Mat3::new::(2.0, 4.0, 6.0, - 0.0, 2.0, 4.0, - 0.0, 0.0, 1.0).inverse().unwrap(), - Mat3::new::(0.5, -1.0, 1.0, - 0.0, 0.5, -2.0, - 0.0, 0.0, 1.0)); - - let ident = Mat3::identity::(); - - assert_eq!(ident.inverse().unwrap(), ident); - - assert!(ident.is_identity()); - assert!(ident.is_symmetric()); - assert!(ident.is_diagonal()); - assert!(!ident.is_rotated()); - assert!(ident.is_invertible()); - - assert!(!a.is_identity()); - assert!(!a.is_symmetric()); - assert!(!a.is_diagonal()); - assert!(a.is_rotated()); - assert!(!a.is_invertible()); - - let c = Mat3::new::(3.0, 2.0, 1.0, - 2.0, 3.0, 2.0, - 1.0, 2.0, 3.0); - assert!(!c.is_identity()); - assert!(c.is_symmetric()); - assert!(!c.is_diagonal()); - assert!(c.is_rotated()); - assert!(c.is_invertible()); - - assert!(Mat3::from_value::(6.0).is_diagonal()); - - assert_eq!(a.to_mat4(), - Mat4::new::(1.0, 4.0, 7.0, 0.0, - 2.0, 5.0, 8.0, 0.0, - 3.0, 6.0, 9.0, 0.0, - 0.0, 0.0, 0.0, 1.0)); - - // to_Quaternion - } - - fn test_mat3_mut() { - let a = Mat3 { x: Vec3 { x: 1.0, y: 4.0, z: 7.0 }, - y: Vec3 { x: 2.0, y: 5.0, z: 8.0 }, - z: Vec3 { x: 3.0, y: 6.0, z: 9.0 } }; - let b = Mat3 { x: Vec3 { x: 2.0, y: 5.0, z: 8.0 }, - y: Vec3 { x: 3.0, y: 6.0, z: 9.0 }, - z: Vec3 { x: 4.0, y: 7.0, z: 10.0 } }; - let c = Mat3 { x: Vec3 { x: 2.0, y: 4.0, z: 6.0 }, - y: Vec3 { x: 0.0, y: 2.0, z: 4.0 }, - z: Vec3 { x: 0.0, y: 0.0, z: 1.0 } }; - - let f1 = 0.5; - - let mut mut_a = a; - let mut mut_c = c; - - mut_a.swap_cols(0, 2); - assert_eq!(mut_a.col(0), a.col(2)); - assert_eq!(mut_a.col(2), a.col(0)); - mut_a = a; - - mut_a.swap_cols(1, 2); - assert_eq!(mut_a.col(1), a.col(2)); - assert_eq!(mut_a.col(2), a.col(1)); - mut_a = a; - - mut_a.swap_rows(0, 2); - assert_eq!(mut_a.row(0), a.row(2)); - assert_eq!(mut_a.row(2), a.row(0)); - mut_a = a; - - mut_a.swap_rows(1, 2); - assert_eq!(mut_a.row(1), a.row(2)); - assert_eq!(mut_a.row(2), a.row(1)); - mut_a = a; - - mut_a.to_identity(); - assert!(mut_a.is_identity()); - mut_a = a; - - mut_a.to_zero(); - assert_eq!(mut_a, Mat3::zero::()); - mut_a = a; - - mut_a.mul_self_t(f1); - assert_eq!(mut_a, a.mul_t(f1)); - mut_a = a; - - mut_a.add_self_m(&b); - assert_eq!(mut_a, a.add_m(&b)); - mut_a = a; - - mut_a.sub_self_m(&b); - assert_eq!(mut_a, a.sub_m(&b)); - mut_a = a; - - mut_c.invert_self(); - assert_eq!(mut_c, c.inverse().unwrap()); - // mut_c = c; - - mut_a.transpose_self(); - assert_eq!(mut_a, a.transpose()); - // mut_a = a; - } - - #[test] - fn test_mat3_approx_eq() { - assert!(!Mat3::new::(0.000001, 0.000001, 0.000001, - 0.000001, 0.000001, 0.000001, - 0.000001, 0.000001, 0.000001) - .approx_eq(&Mat3::zero::())); - assert!(Mat3::new::(0.0000001, 0.0000001, 0.0000001, - 0.0000001, 0.0000001, 0.0000001, - 0.0000001, 0.0000001, 0.0000001) - .approx_eq(&Mat3::zero::())); - } -} diff --git a/src/mat4.rs b/src/mat4.rs deleted file mode 100644 index bee238d..0000000 --- a/src/mat4.rs +++ /dev/null @@ -1,287 +0,0 @@ -// Copyright 2013 The Lmath Developers. For a full listing of the authors, -// refer to the AUTHORS file at the top-level directory of this distribution. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -use dim::Dimensional; -use mat::Mat3; -use vec::Vec4; - -mod num_macros; -mod dim_macros; -mod mat_macros; - -#[deriving(Eq)] -pub struct Mat4 { - x: Vec4, - y: Vec4, - z: Vec4, - w: Vec4, -} - -impl_dimensional!(Mat4, Vec4, 4) -impl_dimensional_fns!(Mat4, Vec4, 4) -impl_approx!(Mat4) - -impl_mat!(Mat4, Vec4) -impl_mat_copyable!(Mat4, Vec4) -impl_mat_numeric!(Mat4, Vec4) -impl_mat_approx_numeric!(Mat4) -impl_mat_neg!(Mat4) - -pub trait ToMat4 { - pub fn to_mat4(&self) -> Mat4; -} - -impl Mat4 { - #[inline] - pub fn new(c0r0: T, c0r1: T, c0r2: T, c0r3: T, - c1r0: T, c1r1: T, c1r2: T, c1r3: T, - c2r0: T, c2r1: T, c2r2: T, c2r3: T, - c3r0: T, c3r1: T, c3r2: T, c3r3: T) -> Mat4 { - Mat4::from_cols(Vec4::new(c0r0, c0r1, c0r2, c0r3), - Vec4::new(c1r0, c1r1, c1r2, c1r3), - Vec4::new(c2r0, c2r1, c2r2, c2r3), - Vec4::new(c3r0, c3r1, c3r2, c3r3)) - } - - #[inline] - pub fn from_cols(c0: Vec4, - c1: Vec4, - c2: Vec4, - c3: Vec4) -> Mat4 { - Mat4 { x: c0, y: c1, z: c2, w: c3 } - } -} - -#[cfg(test)] -mod tests{ - use mat::*; - use vec::*; - - #[test] - fn test_mat4() { - let a = Mat4 { x: Vec4 { x: 1.0, y: 5.0, z: 9.0, w: 13.0 }, - y: Vec4 { x: 2.0, y: 6.0, z: 10.0, w: 14.0 }, - z: Vec4 { x: 3.0, y: 7.0, z: 11.0, w: 15.0 }, - w: Vec4 { x: 4.0, y: 8.0, z: 12.0, w: 16.0 } }; - let b = Mat4 { x: Vec4 { x: 2.0, y: 6.0, z: 10.0, w: 14.0 }, - y: Vec4 { x: 3.0, y: 7.0, z: 11.0, w: 15.0 }, - z: Vec4 { x: 4.0, y: 8.0, z: 12.0, w: 16.0 }, - w: Vec4 { x: 5.0, y: 9.0, z: 13.0, w: 17.0 } }; - let c = Mat4 { x: Vec4 { x: 3.0, y: 2.0, z: 1.0, w: 1.0 }, - y: Vec4 { x: 2.0, y: 3.0, z: 2.0, w: 2.0 }, - z: Vec4 { x: 1.0, y: 2.0, z: 3.0, w: 3.0 }, - w: Vec4 { x: 0.0, y: 1.0, z: 1.0, w: 0.0 } }; - - let v1 = Vec4::new::(1.0, 2.0, 3.0, 4.0); - let f1 = 0.5; - - assert_eq!(a, Mat4::new::(1.0, 5.0, 9.0, 13.0, - 2.0, 6.0, 10.0, 14.0, - 3.0, 7.0, 11.0, 15.0, - 4.0, 8.0, 12.0, 16.0)); - - assert_eq!(a, Mat4::from_cols::(Vec4::new::(1.0, 5.0, 9.0, 13.0), - Vec4::new::(2.0, 6.0, 10.0, 14.0), - Vec4::new::(3.0, 7.0, 11.0, 15.0), - Vec4::new::(4.0, 8.0, 12.0, 16.0))); - - assert_eq!(Mat4::from_value::(4.0), - Mat4::new::(4.0, 0.0, 0.0, 0.0, - 0.0, 4.0, 0.0, 0.0, - 0.0, 0.0, 4.0, 0.0, - 0.0, 0.0, 0.0, 4.0)); - - assert_eq!(*a.col(0), Vec4::new::(1.0, 5.0, 9.0, 13.0)); - assert_eq!(*a.col(1), Vec4::new::(2.0, 6.0, 10.0, 14.0)); - assert_eq!(*a.col(2), Vec4::new::(3.0, 7.0, 11.0, 15.0)); - assert_eq!(*a.col(3), Vec4::new::(4.0, 8.0, 12.0, 16.0)); - - assert_eq!(a.row(0), Vec4::new::( 1.0, 2.0, 3.0, 4.0)); - assert_eq!(a.row(1), Vec4::new::( 5.0, 6.0, 7.0, 8.0)); - assert_eq!(a.row(2), Vec4::new::( 9.0, 10.0, 11.0, 12.0)); - assert_eq!(a.row(3), Vec4::new::(13.0, 14.0, 15.0, 16.0)); - - assert_eq!(*a.col(0), Vec4::new::(1.0, 5.0, 9.0, 13.0)); - assert_eq!(*a.col(1), Vec4::new::(2.0, 6.0, 10.0, 14.0)); - assert_eq!(*a.col(2), Vec4::new::(3.0, 7.0, 11.0, 15.0)); - assert_eq!(*a.col(3), Vec4::new::(4.0, 8.0, 12.0, 16.0)); - - assert_eq!(Mat4::identity::(), - Mat4::new::(1.0, 0.0, 0.0, 0.0, - 0.0, 1.0, 0.0, 0.0, - 0.0, 0.0, 1.0, 0.0, - 0.0, 0.0, 0.0, 1.0)); - - assert_eq!(Mat4::zero::(), - Mat4::new::(0.0, 0.0, 0.0, 0.0, - 0.0, 0.0, 0.0, 0.0, - 0.0, 0.0, 0.0, 0.0, - 0.0, 0.0, 0.0, 0.0)); - - assert_eq!(a.determinant(), 0.0); - assert_eq!(a.trace(), 34.0); - - assert_eq!(a.neg(), - Mat4::new::(-1.0, -5.0, -9.0, -13.0, - -2.0, -6.0, -10.0, -14.0, - -3.0, -7.0, -11.0, -15.0, - -4.0, -8.0, -12.0, -16.0)); - assert_eq!(-a, a.neg()); - assert_eq!(a.mul_t(f1), - Mat4::new::(0.5, 2.5, 4.5, 6.5, - 1.0, 3.0, 5.0, 7.0, - 1.5, 3.5, 5.5, 7.5, - 2.0, 4.0, 6.0, 8.0)); - assert_eq!(a.mul_v(&v1), - Vec4::new::(30.0, 70.0, 110.0, 150.0)); - assert_eq!(a.add_m(&b), - Mat4::new::(3.0, 11.0, 19.0, 27.0, - 5.0, 13.0, 21.0, 29.0, - 7.0, 15.0, 23.0, 31.0, - 9.0, 17.0, 25.0, 33.0)); - assert_eq!(a.sub_m(&b), - Mat4::new::(-1.0, -1.0, -1.0, -1.0, - -1.0, -1.0, -1.0, -1.0, - -1.0, -1.0, -1.0, -1.0, - -1.0, -1.0, -1.0, -1.0)); - assert_eq!(a.mul_m(&b), - Mat4::new::(100.0, 228.0, 356.0, 484.0, - 110.0, 254.0, 398.0, 542.0, - 120.0, 280.0, 440.0, 600.0, - 130.0, 306.0, 482.0, 658.0)); - assert_eq!(a.dot(&b), 1632.0); - assert_eq!(a.transpose(), - Mat4::new::( 1.0, 2.0, 3.0, 4.0, - 5.0, 6.0, 7.0, 8.0, - 9.0, 10.0, 11.0, 12.0, - 13.0, 14.0, 15.0, 16.0)); - - assert_approx_eq!(c.inverse().unwrap(), - Mat4::new::( 5.0, -4.0, 1.0, 0.0, - -4.0, 8.0, -4.0, 0.0, - 4.0, -8.0, 4.0, 8.0, - -3.0, 4.0, 1.0, -8.0).mul_t(0.125)); - - let ident = Mat4::identity::(); - - assert_eq!(ident.inverse().unwrap(), ident); - - assert!(ident.is_identity()); - assert!(ident.is_symmetric()); - assert!(ident.is_diagonal()); - assert!(!ident.is_rotated()); - assert!(ident.is_invertible()); - - assert!(!a.is_identity()); - assert!(!a.is_symmetric()); - assert!(!a.is_diagonal()); - assert!(a.is_rotated()); - assert!(!a.is_invertible()); - - let c = Mat4::new::(4.0, 3.0, 2.0, 1.0, - 3.0, 4.0, 3.0, 2.0, - 2.0, 3.0, 4.0, 3.0, - 1.0, 2.0, 3.0, 4.0); - assert!(!c.is_identity()); - assert!(c.is_symmetric()); - assert!(!c.is_diagonal()); - assert!(c.is_rotated()); - assert!(c.is_invertible()); - - assert!(Mat4::from_value::(6.0).is_diagonal()); - } - - fn test_mat4_mut() { - let a = Mat4 { x: Vec4 { x: 1.0, y: 5.0, z: 9.0, w: 13.0 }, - y: Vec4 { x: 2.0, y: 6.0, z: 10.0, w: 14.0 }, - z: Vec4 { x: 3.0, y: 7.0, z: 11.0, w: 15.0 }, - w: Vec4 { x: 4.0, y: 8.0, z: 12.0, w: 16.0 } }; - let b = Mat4 { x: Vec4 { x: 2.0, y: 6.0, z: 10.0, w: 14.0 }, - y: Vec4 { x: 3.0, y: 7.0, z: 11.0, w: 15.0 }, - z: Vec4 { x: 4.0, y: 8.0, z: 12.0, w: 16.0 }, - w: Vec4 { x: 5.0, y: 9.0, z: 13.0, w: 17.0 } }; - let c = Mat4 { x: Vec4 { x: 3.0, y: 2.0, z: 1.0, w: 1.0 }, - y: Vec4 { x: 2.0, y: 3.0, z: 2.0, w: 2.0 }, - z: Vec4 { x: 1.0, y: 2.0, z: 3.0, w: 3.0 }, - w: Vec4 { x: 0.0, y: 1.0, z: 1.0, w: 0.0 } }; - - let f1 = 0.5; - - let mut mut_a = a; - let mut mut_c = c; - - mut_a.swap_cols(0, 3); - assert_eq!(mut_a.col(0), a.col(3)); - assert_eq!(mut_a.col(3), a.col(0)); - mut_a = a; - - mut_a.swap_cols(1, 2); - assert_eq!(mut_a.col(1), a.col(2)); - assert_eq!(mut_a.col(2), a.col(1)); - mut_a = a; - - mut_a.swap_rows(0, 3); - assert_eq!(mut_a.row(0), a.row(3)); - assert_eq!(mut_a.row(3), a.row(0)); - mut_a = a; - - mut_a.swap_rows(1, 2); - assert_eq!(mut_a.row(1), a.row(2)); - assert_eq!(mut_a.row(2), a.row(1)); - mut_a = a; - - mut_a.to_identity(); - assert!(mut_a.is_identity()); - mut_a = a; - - mut_a.to_zero(); - assert_eq!(mut_a, Mat4::zero::()); - mut_a = a; - - mut_a.mul_self_t(f1); - assert_eq!(mut_a, a.mul_t(f1)); - mut_a = a; - - mut_a.add_self_m(&b); - assert_eq!(mut_a, a.add_m(&b)); - mut_a = a; - - mut_a.sub_self_m(&b); - assert_eq!(mut_a, a.sub_m(&b)); - mut_a = a; - - mut_c.invert_self(); - assert_eq!(mut_c, c.inverse().unwrap()); - // mut_c = c; - - mut_a.transpose_self(); - assert_eq!(mut_a, a.transpose()); - // mut_a = a; - } - - #[test] - fn test_mat4_approx_eq() { - assert!(!Mat4::new::(0.000001, 0.000001, 0.000001, 0.000001, - 0.000001, 0.000001, 0.000001, 0.000001, - 0.000001, 0.000001, 0.000001, 0.000001, - 0.000001, 0.000001, 0.000001, 0.000001) - .approx_eq(&Mat4::zero::())); - assert!(Mat4::new::(0.0000001, 0.0000001, 0.0000001, 0.0000001, - 0.0000001, 0.0000001, 0.0000001, 0.0000001, - 0.0000001, 0.0000001, 0.0000001, 0.0000001, - 0.0000001, 0.0000001, 0.0000001, 0.0000001) - .approx_eq(&Mat4::zero::())); - } -} diff --git a/src/vec.rs b/src/vec.rs index 54f026c..6d93642 100644 --- a/src/vec.rs +++ b/src/vec.rs @@ -14,36 +14,22 @@ // limitations under the License. pub use dim::Dimensional; -pub use self::vec2::Vec2; -pub use self::vec3::Vec3; -pub use self::vec4::Vec4; -pub mod vec2; -pub mod vec3; -pub mod vec4; +mod num_macros; +mod dim_macros; +mod vec_macros; + +#[deriving(Eq)] +pub struct Vec2 { x: T, y: T } // GLSL-style type aliases - pub type vec2 = Vec2; pub type dvec2 = Vec2; pub type bvec2 = Vec2; pub type ivec2 = Vec2; pub type uvec2 = Vec2; -pub type vec3 = Vec3; -pub type dvec3 = Vec3; -pub type bvec3 = Vec3; -pub type ivec3 = Vec3; -pub type uvec3 = Vec3; - -pub type vec4 = Vec4; -pub type dvec4 = Vec4; -pub type bvec4 = Vec4; -pub type ivec4 = Vec4; -pub type uvec4 = Vec4; - // Rust-style type aliases - pub type Vec2f = Vec2; pub type Vec2f32 = Vec2; pub type Vec2f64 = Vec2; @@ -59,6 +45,176 @@ pub type Vec2u32 = Vec2; pub type Vec2u64 = Vec2; pub type Vec2b = Vec2; +impl_dimensional!(Vec2, T, 2) +impl_dimensional_fns!(Vec2, T, 2) +impl_swap!(Vec2) +impl_approx!(Vec2) + +impl_vec!(Vec2 { x, y }) +impl_vec_copyable!(Vec2) +impl_vec_numeric!(Vec2) +impl_vec_neg!(Vec2) +impl_vec_euclidean!(Vec2) +impl_vec_ord!(Vec2) +impl_vec_eq!(Vec2) +impl_vec_bool!(Vec2) +impl_vec_not!(Vec2) + +impl Vec2 { + #[inline] pub fn unit_x() -> Vec2 { Vec2::new(one!(T), zero!(T)) } + #[inline] pub fn unit_y() -> Vec2 { Vec2::new(zero!(T), one!(T)) } + + #[inline] + pub fn perp_dot(&self, other: &Vec2) -> T { + (*self.index(0) * *other.index(1)) - + (*self.index(1) * *other.index(0)) + } +} + +#[cfg(test)] +mod vec2_tests { + use vec::*; + + #[test] + fn test_vec2() { + let a = Vec2 { x: 1.0, y: 2.0 }; + let b = Vec2 { x: 3.0, y: 4.0 }; + let f1 = 1.5; + let f2 = 0.5; + + let mut mut_a = a; + + assert_eq!(Vec2::new::(1.0, 2.0), a); + assert_eq!(Vec2::from_value(1.0), Vec2::new::(1.0, 1.0)); + + assert_eq!(Vec2::zero(), Vec2::new::(0.0, 0.0)); + assert_eq!(Vec2::unit_x(), Vec2::new::(1.0, 0.0)); + assert_eq!(Vec2::unit_y(), Vec2::new::(0.0, 1.0)); + assert_eq!(Vec2::identity(), Vec2::new::(1.0, 1.0)); + + *mut_a.index_mut(0) = 42.0; + *mut_a.index_mut(1) = 43.0; + assert_eq!(mut_a, Vec2::new::(42.0, 43.0)); + mut_a = a; + + mut_a.swap(0, 1); + assert_eq!(*mut_a.index(0), *a.index(1)); + assert_eq!(*mut_a.index(1), *a.index(0)); + mut_a = a; + + assert_eq!(a.x, 1.0); + assert_eq!(a.y, 2.0); + assert_eq!(*a.index(0), 1.0); + assert_eq!(*a.index(1), 2.0); + + assert_eq!(-a, Vec2::new::(-1.0, -2.0)); + assert_eq!(a.neg(), Vec2::new::(-1.0, -2.0)); + + assert_eq!(a.mul_t(f1), Vec2::new::( 1.5, 3.0)); + assert_eq!(a.div_t(f2), Vec2::new::( 2.0, 4.0)); + + assert_eq!(a.add_v(&b), Vec2::new::( 4.0, 6.0)); + assert_eq!(a.sub_v(&b), Vec2::new::( -2.0, -2.0)); + assert_eq!(a.mul_v(&b), Vec2::new::( 3.0, 8.0)); + assert_eq!(a.div_v(&b), Vec2::new::(1.0/3.0, 2.0/4.0)); + + mut_a.neg_self(); + assert_eq!(mut_a, -a); + mut_a = a; + + mut_a.mul_self_t(f1); + assert_eq!(mut_a, a.mul_t(f1)); + mut_a = a; + + mut_a.div_self_t(f2); + assert_eq!(mut_a, a.div_t(f2)); + mut_a = a; + + mut_a.add_self_v(&b); + assert_eq!(mut_a, a.add_v(&b)); + mut_a = a; + + mut_a.sub_self_v(&b); + assert_eq!(mut_a, a.sub_v(&b)); + mut_a = a; + + mut_a.mul_self_v(&b); + assert_eq!(mut_a, a.mul_v(&b)); + mut_a = a; + + mut_a.div_self_v(&b); + assert_eq!(mut_a, a.div_v(&b)); + } + + #[test] + fn test_vec2_approx_eq() { + assert!(!Vec2::new::(0.000001, 0.000001).approx_eq(&Vec2::new::(0.0, 0.0))); + assert!(Vec2::new::(0.0000001, 0.0000001).approx_eq(&Vec2::new::(0.0, 0.0))); + } + + #[test] + fn test_vec2_euclidean() { + let a = Vec2::new::(5.0, 12.0); // (5, 12, 13) Pythagorean triple + let b0 = Vec2::new::(3.0, 4.0); // (3, 4, 5) Pythagorean triple + let b = a.add_v(&b0); + + assert_eq!(a.length(), 13.0); + assert_eq!(a.length2(), 13.0 * 13.0); + + assert_eq!(b0.length(), 5.0); + assert_eq!(b0.length2(), 5.0 * 5.0); + + assert_eq!(a.distance(&b), 5.0); + assert_eq!(a.distance2(&b), 5.0 * 5.0); + + assert!(Vec2::new::(1.0, 0.0).angle(&Vec2::new::(0.0, 1.0)).approx_eq(&Real::frac_pi_2())); + assert!(Vec2::new::(10.0, 0.0).angle(&Vec2::new::(0.0, 5.0)).approx_eq(&Real::frac_pi_2())); + assert!(Vec2::new::(-1.0, 0.0).angle(&Vec2::new::(0.0, 1.0)).approx_eq(&-Real::frac_pi_2::())); + + assert!(Vec2::new::(3.0, 4.0).normalize().approx_eq(&Vec2::new::(3.0/5.0, 4.0/5.0))); + // TODO: test normalize_to, normalize_self, and normalize_self_to + + let c = Vec2::new::(-2.0, -1.0); + let d = Vec2::new::( 1.0, 0.0); + + assert_eq!(c.lerp(&d, 0.75), Vec2::new::(0.250, -0.250)); + + let mut mut_c = c; + mut_c.lerp_self(&d, 0.75); + assert_eq!(mut_c, c.lerp(&d, 0.75)); + } + + #[test] + fn test_vec2_boolean() { + let tf = Vec2::new(true, false); + let ff = Vec2::new(false, false); + let tt = Vec2::new(true, true); + + assert_eq!(tf.any(), true); + assert_eq!(tf.all(), false); + assert_eq!(tf.not(), Vec2::new(false, true)); + + assert_eq!(ff.any(), false); + assert_eq!(ff.all(), false); + assert_eq!(ff.not(), Vec2::new(true, true)); + + assert_eq!(tt.any(), true); + assert_eq!(tt.all(), true); + assert_eq!(tt.not(), Vec2::new(false, false)); + } +} + +#[deriving(Eq)] +pub struct Vec3 { x: T, y: T, z: T } + +// GLSL-style type aliases +pub type vec3 = Vec3; +pub type dvec3 = Vec3; +pub type bvec3 = Vec3; +pub type ivec3 = Vec3; +pub type uvec3 = Vec3; + +// Rust-style type aliases pub type Vec3f = Vec3; pub type Vec3f32 = Vec3; pub type Vec3f64 = Vec3; @@ -74,6 +230,198 @@ pub type Vec3u32 = Vec3; pub type Vec3u64 = Vec3; pub type Vec3b = Vec3; +impl_dimensional!(Vec3, T, 3) +impl_dimensional_fns!(Vec3, T, 3) +impl_swap!(Vec3) +impl_approx!(Vec3) + +impl_vec!(Vec3 { x, y, z }) +impl_vec_copyable!(Vec3) +impl_vec_numeric!(Vec3) +impl_vec_neg!(Vec3) +impl_vec_euclidean!(Vec3) +impl_vec_ord!(Vec3) +impl_vec_eq!(Vec3) +impl_vec_bool!(Vec3) +impl_vec_not!(Vec3) + +impl Vec3 { + #[inline] pub fn unit_x() -> Vec3 { Vec3::new(one!(T), zero!(T), zero!(T)) } + #[inline] pub fn unit_y() -> Vec3 { Vec3::new(zero!(T), one!(T), zero!(T)) } + #[inline] pub fn unit_z() -> Vec3 { Vec3::new(zero!(T), zero!(T), one!(T)) } + + #[inline] + pub fn cross(&self, other: &Vec3) -> Vec3 { + Vec3::new((*self.index(1) * *other.index(2)) - (*self.index(2) * *other.index(1)), + (*self.index(2) * *other.index(0)) - (*self.index(0) * *other.index(2)), + (*self.index(0) * *other.index(1)) - (*self.index(1) * *other.index(0))) + } + + #[inline] + pub fn cross_self(&mut self, other: &Vec3) { + *self = self.cross(other) + } +} + +#[cfg(test)] +mod vec3_tests{ + use vec::*; + + #[test] + fn test_vec3() { + let a = Vec3 { x: 1.0, y: 2.0, z: 3.0 }; + let b = Vec3 { x: 4.0, y: 5.0, z: 6.0 }; + let f1 = 1.5; + let f2 = 0.5; + + let mut mut_a = a; + + assert_eq!(Vec3::new::(1.0, 2.0, 3.0), a); + assert_eq!(Vec3::from_value(1.0), Vec3::new::(1.0, 1.0, 1.0)); + + assert_eq!(Vec3::zero(), Vec3::new::(0.0, 0.0, 0.0)); + assert_eq!(Vec3::unit_x(), Vec3::new::(1.0, 0.0, 0.0)); + assert_eq!(Vec3::unit_y(), Vec3::new::(0.0, 1.0, 0.0)); + assert_eq!(Vec3::unit_z(), Vec3::new::(0.0, 0.0, 1.0)); + assert_eq!(Vec3::identity(), Vec3::new::(1.0, 1.0, 1.0)); + + *mut_a.index_mut(0) = 42.0; + *mut_a.index_mut(1) = 43.0; + *mut_a.index_mut(2) = 44.0; + assert_eq!(mut_a, Vec3::new::(42.0, 43.0, 44.0)); + mut_a = a; + + mut_a.swap(0, 2); + assert_eq!(*mut_a.index(0), *a.index(2)); + assert_eq!(*mut_a.index(2), *a.index(0)); + mut_a = a; + + mut_a.swap(1, 2); + assert_eq!(*mut_a.index(1), *a.index(2)); + assert_eq!(*mut_a.index(2), *a.index(1)); + mut_a = a; + + assert_eq!(a.x, 1.0); + assert_eq!(a.y, 2.0); + assert_eq!(a.z, 3.0); + assert_eq!(*a.index(0), 1.0); + assert_eq!(*a.index(1), 2.0); + assert_eq!(*a.index(2), 3.0); + + assert_eq!(a.cross(&b), Vec3::new::(-3.0, 6.0, -3.0)); + + mut_a.cross_self(&b); + assert_eq!(mut_a, a.cross(&b)); + mut_a = a; + + assert_eq!(-a, Vec3::new::(-1.0, -2.0, -3.0)); + assert_eq!(a.neg(), Vec3::new::(-1.0, -2.0, -3.0)); + + assert_eq!(a.mul_t(f1), Vec3::new::( 1.5, 3.0, 4.5)); + assert_eq!(a.div_t(f2), Vec3::new::( 2.0, 4.0, 6.0)); + + assert_eq!(a.add_v(&b), Vec3::new::( 5.0, 7.0, 9.0)); + assert_eq!(a.sub_v(&b), Vec3::new::( -3.0, -3.0, -3.0)); + assert_eq!(a.mul_v(&b), Vec3::new::( 4.0, 10.0, 18.0)); + assert_eq!(a.div_v(&b), Vec3::new::(1.0/4.0, 2.0/5.0, 3.0/6.0)); + + mut_a.neg_self(); + assert_eq!(mut_a, -a); + mut_a = a; + + mut_a.mul_self_t(f1); + assert_eq!(mut_a, a.mul_t(f1)); + mut_a = a; + + mut_a.div_self_t(f2); + assert_eq!(mut_a, a.div_t(f2)); + mut_a = a; + + mut_a.add_self_v(&b); + assert_eq!(mut_a, a.add_v(&b)); + mut_a = a; + + mut_a.sub_self_v(&b); + assert_eq!(mut_a, a.sub_v(&b)); + mut_a = a; + + mut_a.mul_self_v(&b); + assert_eq!(mut_a, a.mul_v(&b)); + mut_a = a; + + mut_a.div_self_v(&b); + assert_eq!(mut_a, a.div_v(&b)); + } + + #[test] + fn test_vec3_approx_eq() { + assert!(!Vec3::new::(0.000001, 0.000001, 0.000001).approx_eq(&Vec3::new::(0.0, 0.0, 0.0))); + assert!(Vec3::new::(0.0000001, 0.0000001, 0.0000001).approx_eq(&Vec3::new::(0.0, 0.0, 0.0))); + } + + #[test] + fn test_vec3_euclidean() { + let a = Vec3::new::(2.0, 3.0, 6.0); // (2, 3, 6, 7) Pythagorean quadruple + let b0 = Vec3::new::(1.0, 4.0, 8.0); // (1, 4, 8, 9) Pythagorean quadruple + let b = a.add_v(&b0); + + assert_eq!(a.length(), 7.0); + assert_eq!(a.length2(), 7.0 * 7.0); + + assert_eq!(b0.length(), 9.0); + assert_eq!(b0.length2(), 9.0 * 9.0); + + assert_eq!(a.distance(&b), 9.0); + assert_eq!(a.distance2(&b), 9.0 * 9.0); + + assert!(Vec3::new::(1.0, 0.0, 1.0).angle(&Vec3::new::(1.0, 1.0, 0.0)).approx_eq(&Real::frac_pi_3())); + assert!(Vec3::new::(10.0, 0.0, 10.0).angle(&Vec3::new::(5.0, 5.0, 0.0)).approx_eq(&Real::frac_pi_3())); + assert!(Vec3::new::(-1.0, 0.0, -1.0).angle(&Vec3::new::(1.0, -1.0, 0.0)).approx_eq(&(2.0 * Real::frac_pi_3()))); + + assert!(Vec3::new::(2.0, 3.0, 6.0).normalize().approx_eq(&Vec3::new::(2.0/7.0, 3.0/7.0, 6.0/7.0))); + // TODO: test normalize_to, normalize_self, and normalize_self_to + + let c = Vec3::new::(-2.0, -1.0, 1.0); + let d = Vec3::new::( 1.0, 0.0, 0.5); + + assert_eq!(c.lerp(&d, 0.75), Vec3::new::(0.250, -0.250, 0.625)); + + let mut mut_c = c; + mut_c.lerp_self(&d, 0.75); + assert_eq!(mut_c, c.lerp(&d, 0.75)); + } + + #[test] + fn test_vec3_boolean() { + let tft = Vec3::new(true, false, true); + let fff = Vec3::new(false, false, false); + let ttt = Vec3::new(true, true, true); + + assert_eq!(tft.any(), true); + assert_eq!(tft.all(), false); + assert_eq!(tft.not(), Vec3::new(false, true, false)); + + assert_eq!(fff.any(), false); + assert_eq!(fff.all(), false); + assert_eq!(fff.not(), Vec3::new(true, true, true)); + + assert_eq!(ttt.any(), true); + assert_eq!(ttt.all(), true); + assert_eq!(ttt.not(), Vec3::new(false, false, false)); + } +} + +#[deriving(Eq)] +pub struct Vec4 { x: T, y: T, z: T, w: T } + +// GLSL-style type aliases +pub type vec4 = Vec4; +pub type dvec4 = Vec4; +pub type bvec4 = Vec4; +pub type ivec4 = Vec4; +pub type uvec4 = Vec4; + +// Rust-style type aliases pub type Vec4f = Vec4; pub type Vec4f32 = Vec4; pub type Vec4f64 = Vec4; @@ -88,3 +436,173 @@ pub type Vec4u16 = Vec4; pub type Vec4u32 = Vec4; pub type Vec4u64 = Vec4; pub type Vec4b = Vec4; + +impl_dimensional!(Vec4, T, 4) +impl_dimensional_fns!(Vec4, T, 4) +impl_approx!(Vec4) +impl_swap!(Vec4) + +impl_vec!(Vec4 { x, y, z, w }) +impl_vec_copyable!(Vec4) +impl_vec_numeric!(Vec4) +impl_vec_neg!(Vec4) +impl_vec_euclidean!(Vec4) +impl_vec_ord!(Vec4) +impl_vec_eq!(Vec4) +impl_vec_bool!(Vec4) +impl_vec_not!(Vec4) + +impl Vec4 { + #[inline] pub fn unit_x() -> Vec4 { Vec4::new(one!(T), zero!(T), zero!(T), zero!(T)) } + #[inline] pub fn unit_y() -> Vec4 { Vec4::new(zero!(T), one!(T), zero!(T), zero!(T)) } + #[inline] pub fn unit_z() -> Vec4 { Vec4::new(zero!(T), zero!(T), one!(T), zero!(T)) } + #[inline] pub fn unit_w() -> Vec4 { Vec4::new(zero!(T), zero!(T), zero!(T), one!(T)) } +} + +#[cfg(test)] +mod vec4_tests { + use vec::*; + + #[test] + fn test_vec4() { + let a = Vec4 { x: 1.0, y: 2.0, z: 3.0, w: 4.0 }; + let b = Vec4 { x: 5.0, y: 6.0, z: 7.0, w: 8.0 }; + let f1 = 1.5; + let f2 = 0.5; + + let mut mut_a = a; + + assert_eq!(Vec4::new::(1.0, 2.0, 3.0, 4.0), a); + assert_eq!(Vec4::from_value(1.0), Vec4::new::(1.0, 1.0, 1.0, 1.0)); + + *mut_a.index_mut(0) = 42.0; + *mut_a.index_mut(1) = 43.0; + *mut_a.index_mut(2) = 44.0; + *mut_a.index_mut(3) = 45.0; + assert_eq!(mut_a, Vec4::new::(42.0, 43.0, 44.0, 45.0)); + mut_a = a; + + mut_a.swap(0, 3); + assert_eq!(*mut_a.index(0), *a.index(3)); + assert_eq!(*mut_a.index(3), *a.index(0)); + mut_a = a; + + mut_a.swap(1, 2); + assert_eq!(*mut_a.index(1), *a.index(2)); + assert_eq!(*mut_a.index(2), *a.index(1)); + mut_a = a; + + assert_eq!(Vec4::zero(), Vec4::new::(0.0, 0.0, 0.0, 0.0)); + assert_eq!(Vec4::unit_x(), Vec4::new::(1.0, 0.0, 0.0, 0.0)); + assert_eq!(Vec4::unit_y(), Vec4::new::(0.0, 1.0, 0.0, 0.0)); + assert_eq!(Vec4::unit_z(), Vec4::new::(0.0, 0.0, 1.0, 0.0)); + assert_eq!(Vec4::unit_w(), Vec4::new::(0.0, 0.0, 0.0, 1.0)); + assert_eq!(Vec4::identity(), Vec4::new::(1.0, 1.0, 1.0, 1.0)); + + assert_eq!(a.x, 1.0); + assert_eq!(a.y, 2.0); + assert_eq!(a.z, 3.0); + assert_eq!(a.w, 4.0); + assert_eq!(*a.index(0), 1.0); + assert_eq!(*a.index(1), 2.0); + assert_eq!(*a.index(2), 3.0); + assert_eq!(*a.index(3), 4.0); + + assert_eq!(-a, Vec4::new::(-1.0, -2.0, -3.0, -4.0)); + assert_eq!(a.neg(), Vec4::new::(-1.0, -2.0, -3.0, -4.0)); + + assert_eq!(a.mul_t(f1), Vec4::new::( 1.5, 3.0, 4.5, 6.0)); + assert_eq!(a.div_t(f2), Vec4::new::( 2.0, 4.0, 6.0, 8.0)); + + assert_eq!(a.add_v(&b), Vec4::new::( 6.0, 8.0, 10.0, 12.0)); + assert_eq!(a.sub_v(&b), Vec4::new::( -4.0, -4.0, -4.0, -4.0)); + assert_eq!(a.mul_v(&b), Vec4::new::( 5.0, 12.0, 21.0, 32.0)); + assert_eq!(a.div_v(&b), Vec4::new::(1.0/5.0, 2.0/6.0, 3.0/7.0, 4.0/8.0)); + + assert_eq!(a.dot(&b), 70.0); + + mut_a.neg_self(); + assert_eq!(mut_a, -a); + mut_a = a; + + mut_a.mul_self_t(f1); + assert_eq!(mut_a, a.mul_t(f1)); + mut_a = a; + + mut_a.div_self_t(f2); + assert_eq!(mut_a, a.div_t(f2)); + mut_a = a; + + mut_a.add_self_v(&b); + assert_eq!(mut_a, a.add_v(&b)); + mut_a = a; + + mut_a.sub_self_v(&b); + assert_eq!(mut_a, a.sub_v(&b)); + mut_a = a; + + mut_a.mul_self_v(&b); + assert_eq!(mut_a, a.mul_v(&b)); + mut_a = a; + + mut_a.div_self_v(&b); + assert_eq!(mut_a, a.div_v(&b)); + } + + #[test] + fn test_vec4_approx_eq() { + assert!(!Vec4::new::(0.000001, 0.000001, 0.000001, 0.000001).approx_eq(&Vec4::new::(0.0, 0.0, 0.0, 0.0))); + assert!(Vec4::new::(0.0000001, 0.0000001, 0.0000001, 0.0000001).approx_eq(&Vec4::new::(0.0, 0.0, 0.0, 0.0))); + } + + #[test] + fn test_vec4_euclidean() { + let a = Vec4::new::(1.0, 2.0, 4.0, 10.0); // (1, 2, 4, 10, 11) Pythagorean quintuple + let b0 = Vec4::new::(1.0, 2.0, 8.0, 10.0); // (1, 2, 8, 10, 13) Pythagorean quintuple + let b = a.add_v(&b0); + + assert_eq!(a.length(), 11.0); + assert_eq!(a.length2(), 11.0 * 11.0); + + assert_eq!(b0.length(), 13.0); + assert_eq!(b0.length2(), 13.0 * 13.0); + + assert_eq!(a.distance(&b), 13.0); + assert_eq!(a.distance2(&b), 13.0 * 13.0); + + assert!(Vec4::new::(1.0, 0.0, 1.0, 0.0).angle(&Vec4::new::(0.0, 1.0, 0.0, 1.0)).approx_eq(&Real::frac_pi_2())); + assert!(Vec4::new::(10.0, 0.0, 10.0, 0.0).angle(&Vec4::new::(0.0, 5.0, 0.0, 5.0)).approx_eq(&Real::frac_pi_2())); + assert!(Vec4::new::(-1.0, 0.0, -1.0, 0.0).angle(&Vec4::new::(0.0, 1.0, 0.0, 1.0)).approx_eq(&Real::frac_pi_2())); + + assert!(Vec4::new::(1.0, 2.0, 4.0, 10.0).normalize().approx_eq(&Vec4::new::(1.0/11.0, 2.0/11.0, 4.0/11.0, 10.0/11.0))); + // TODO: test normalize_to, normalize_self, and normalize_self_to + + let c = Vec4::new::(-2.0, -1.0, 1.0, 2.0); + let d = Vec4::new::( 1.0, 0.0, 0.5, 1.0); + + assert_eq!(c.lerp(&d, 0.75), Vec4::new::(0.250, -0.250, 0.625, 1.250)); + + let mut mut_c = c; + mut_c.lerp_self(&d, 0.75); + assert_eq!(mut_c, c.lerp(&d, 0.75)); + } + + #[test] + fn test_vec4_boolean() { + let tftf = Vec4::new(true, false, true, false); + let ffff = Vec4::new(false, false, false, false); + let tttt = Vec4::new(true, true, true, true); + + assert_eq!(tftf.any(), true); + assert_eq!(tftf.all(), false); + assert_eq!(tftf.not(), Vec4::new(false, true, false, true)); + + assert_eq!(ffff.any(), false); + assert_eq!(ffff.all(), false); + assert_eq!(ffff.not(), Vec4::new(true, true, true, true)); + + assert_eq!(tttt.any(), true); + assert_eq!(tttt.all(), true); + assert_eq!(tttt.not(), Vec4::new(false, false, false, false)); + } +} diff --git a/src/vec2.rs b/src/vec2.rs deleted file mode 100644 index 41680c9..0000000 --- a/src/vec2.rs +++ /dev/null @@ -1,181 +0,0 @@ -// Copyright 2013 The Lmath Developers. For a full listing of the authors, -// refer to the AUTHORS file at the top-level directory of this distribution. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -use dim::Dimensional; -mod num_macros; -mod dim_macros; -mod vec_macros; - -#[deriving(Eq)] -pub struct Vec2 { x: T, y: T } - -impl_dimensional!(Vec2, T, 2) -impl_dimensional_fns!(Vec2, T, 2) -impl_swap!(Vec2) -impl_approx!(Vec2) - -impl_vec!(Vec2 { x, y }) -impl_vec_copyable!(Vec2) -impl_vec_numeric!(Vec2) -impl_vec_neg!(Vec2) -impl_vec_euclidean!(Vec2) -impl_vec_ord!(Vec2) -impl_vec_eq!(Vec2) -impl_vec_bool!(Vec2) -impl_vec_not!(Vec2) - -impl Vec2 { - #[inline] pub fn unit_x() -> Vec2 { Vec2::new(one!(T), zero!(T)) } - #[inline] pub fn unit_y() -> Vec2 { Vec2::new(zero!(T), one!(T)) } - - #[inline] - pub fn perp_dot(&self, other: &Vec2) -> T { - (*self.index(0) * *other.index(1)) - - (*self.index(1) * *other.index(0)) - } -} - -#[cfg(test)] -mod tests { - use vec::*; - - #[test] - fn test_vec2() { - let a = Vec2 { x: 1.0, y: 2.0 }; - let b = Vec2 { x: 3.0, y: 4.0 }; - let f1 = 1.5; - let f2 = 0.5; - - let mut mut_a = a; - - assert_eq!(Vec2::new::(1.0, 2.0), a); - assert_eq!(Vec2::from_value(1.0), Vec2::new::(1.0, 1.0)); - - assert_eq!(Vec2::zero(), Vec2::new::(0.0, 0.0)); - assert_eq!(Vec2::unit_x(), Vec2::new::(1.0, 0.0)); - assert_eq!(Vec2::unit_y(), Vec2::new::(0.0, 1.0)); - assert_eq!(Vec2::identity(), Vec2::new::(1.0, 1.0)); - - *mut_a.index_mut(0) = 42.0; - *mut_a.index_mut(1) = 43.0; - assert_eq!(mut_a, Vec2::new::(42.0, 43.0)); - mut_a = a; - - mut_a.swap(0, 1); - assert_eq!(*mut_a.index(0), *a.index(1)); - assert_eq!(*mut_a.index(1), *a.index(0)); - mut_a = a; - - assert_eq!(a.x, 1.0); - assert_eq!(a.y, 2.0); - assert_eq!(*a.index(0), 1.0); - assert_eq!(*a.index(1), 2.0); - - assert_eq!(-a, Vec2::new::(-1.0, -2.0)); - assert_eq!(a.neg(), Vec2::new::(-1.0, -2.0)); - - assert_eq!(a.mul_t(f1), Vec2::new::( 1.5, 3.0)); - assert_eq!(a.div_t(f2), Vec2::new::( 2.0, 4.0)); - - assert_eq!(a.add_v(&b), Vec2::new::( 4.0, 6.0)); - assert_eq!(a.sub_v(&b), Vec2::new::( -2.0, -2.0)); - assert_eq!(a.mul_v(&b), Vec2::new::( 3.0, 8.0)); - assert_eq!(a.div_v(&b), Vec2::new::(1.0/3.0, 2.0/4.0)); - - mut_a.neg_self(); - assert_eq!(mut_a, -a); - mut_a = a; - - mut_a.mul_self_t(f1); - assert_eq!(mut_a, a.mul_t(f1)); - mut_a = a; - - mut_a.div_self_t(f2); - assert_eq!(mut_a, a.div_t(f2)); - mut_a = a; - - mut_a.add_self_v(&b); - assert_eq!(mut_a, a.add_v(&b)); - mut_a = a; - - mut_a.sub_self_v(&b); - assert_eq!(mut_a, a.sub_v(&b)); - mut_a = a; - - mut_a.mul_self_v(&b); - assert_eq!(mut_a, a.mul_v(&b)); - mut_a = a; - - mut_a.div_self_v(&b); - assert_eq!(mut_a, a.div_v(&b)); - } - - #[test] - fn test_vec2_approx_eq() { - assert!(!Vec2::new::(0.000001, 0.000001).approx_eq(&Vec2::new::(0.0, 0.0))); - assert!(Vec2::new::(0.0000001, 0.0000001).approx_eq(&Vec2::new::(0.0, 0.0))); - } - - #[test] - fn test_vec2_euclidean() { - let a = Vec2::new::(5.0, 12.0); // (5, 12, 13) Pythagorean triple - let b0 = Vec2::new::(3.0, 4.0); // (3, 4, 5) Pythagorean triple - let b = a.add_v(&b0); - - assert_eq!(a.length(), 13.0); - assert_eq!(a.length2(), 13.0 * 13.0); - - assert_eq!(b0.length(), 5.0); - assert_eq!(b0.length2(), 5.0 * 5.0); - - assert_eq!(a.distance(&b), 5.0); - assert_eq!(a.distance2(&b), 5.0 * 5.0); - - assert!(Vec2::new::(1.0, 0.0).angle(&Vec2::new::(0.0, 1.0)).approx_eq(&Real::frac_pi_2())); - assert!(Vec2::new::(10.0, 0.0).angle(&Vec2::new::(0.0, 5.0)).approx_eq(&Real::frac_pi_2())); - assert!(Vec2::new::(-1.0, 0.0).angle(&Vec2::new::(0.0, 1.0)).approx_eq(&-Real::frac_pi_2::())); - - assert!(Vec2::new::(3.0, 4.0).normalize().approx_eq(&Vec2::new::(3.0/5.0, 4.0/5.0))); - // TODO: test normalize_to, normalize_self, and normalize_self_to - - let c = Vec2::new::(-2.0, -1.0); - let d = Vec2::new::( 1.0, 0.0); - - assert_eq!(c.lerp(&d, 0.75), Vec2::new::(0.250, -0.250)); - - let mut mut_c = c; - mut_c.lerp_self(&d, 0.75); - assert_eq!(mut_c, c.lerp(&d, 0.75)); - } - - #[test] - fn test_vec2_boolean() { - let tf = Vec2::new(true, false); - let ff = Vec2::new(false, false); - let tt = Vec2::new(true, true); - - assert_eq!(tf.any(), true); - assert_eq!(tf.all(), false); - assert_eq!(tf.not(), Vec2::new(false, true)); - - assert_eq!(ff.any(), false); - assert_eq!(ff.all(), false); - assert_eq!(ff.not(), Vec2::new(true, true)); - - assert_eq!(tt.any(), true); - assert_eq!(tt.all(), true); - assert_eq!(tt.not(), Vec2::new(false, false)); - } -} diff --git a/src/vec3.rs b/src/vec3.rs deleted file mode 100644 index 99c28f2..0000000 --- a/src/vec3.rs +++ /dev/null @@ -1,203 +0,0 @@ -// Copyright 2013 The Lmath Developers. For a full listing of the authors, -// refer to the AUTHORS file at the top-level directory of this distribution. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -use dim::Dimensional; -mod num_macros; -mod dim_macros; -mod vec_macros; - -#[deriving(Eq)] -pub struct Vec3 { x: T, y: T, z: T } - -impl_dimensional!(Vec3, T, 3) -impl_dimensional_fns!(Vec3, T, 3) -impl_swap!(Vec3) -impl_approx!(Vec3) - -impl_vec!(Vec3 { x, y, z }) -impl_vec_copyable!(Vec3) -impl_vec_numeric!(Vec3) -impl_vec_neg!(Vec3) -impl_vec_euclidean!(Vec3) -impl_vec_ord!(Vec3) -impl_vec_eq!(Vec3) -impl_vec_bool!(Vec3) -impl_vec_not!(Vec3) - -impl Vec3 { - #[inline] pub fn unit_x() -> Vec3 { Vec3::new(one!(T), zero!(T), zero!(T)) } - #[inline] pub fn unit_y() -> Vec3 { Vec3::new(zero!(T), one!(T), zero!(T)) } - #[inline] pub fn unit_z() -> Vec3 { Vec3::new(zero!(T), zero!(T), one!(T)) } - - #[inline] - pub fn cross(&self, other: &Vec3) -> Vec3 { - Vec3::new((*self.index(1) * *other.index(2)) - (*self.index(2) * *other.index(1)), - (*self.index(2) * *other.index(0)) - (*self.index(0) * *other.index(2)), - (*self.index(0) * *other.index(1)) - (*self.index(1) * *other.index(0))) - } - - #[inline] - pub fn cross_self(&mut self, other: &Vec3) { - *self = self.cross(other) - } -} - -#[cfg(test)] -mod tests{ - use vec::*; - - #[test] - fn test_vec3() { - let a = Vec3 { x: 1.0, y: 2.0, z: 3.0 }; - let b = Vec3 { x: 4.0, y: 5.0, z: 6.0 }; - let f1 = 1.5; - let f2 = 0.5; - - let mut mut_a = a; - - assert_eq!(Vec3::new::(1.0, 2.0, 3.0), a); - assert_eq!(Vec3::from_value(1.0), Vec3::new::(1.0, 1.0, 1.0)); - - assert_eq!(Vec3::zero(), Vec3::new::(0.0, 0.0, 0.0)); - assert_eq!(Vec3::unit_x(), Vec3::new::(1.0, 0.0, 0.0)); - assert_eq!(Vec3::unit_y(), Vec3::new::(0.0, 1.0, 0.0)); - assert_eq!(Vec3::unit_z(), Vec3::new::(0.0, 0.0, 1.0)); - assert_eq!(Vec3::identity(), Vec3::new::(1.0, 1.0, 1.0)); - - *mut_a.index_mut(0) = 42.0; - *mut_a.index_mut(1) = 43.0; - *mut_a.index_mut(2) = 44.0; - assert_eq!(mut_a, Vec3::new::(42.0, 43.0, 44.0)); - mut_a = a; - - mut_a.swap(0, 2); - assert_eq!(*mut_a.index(0), *a.index(2)); - assert_eq!(*mut_a.index(2), *a.index(0)); - mut_a = a; - - mut_a.swap(1, 2); - assert_eq!(*mut_a.index(1), *a.index(2)); - assert_eq!(*mut_a.index(2), *a.index(1)); - mut_a = a; - - assert_eq!(a.x, 1.0); - assert_eq!(a.y, 2.0); - assert_eq!(a.z, 3.0); - assert_eq!(*a.index(0), 1.0); - assert_eq!(*a.index(1), 2.0); - assert_eq!(*a.index(2), 3.0); - - assert_eq!(a.cross(&b), Vec3::new::(-3.0, 6.0, -3.0)); - - mut_a.cross_self(&b); - assert_eq!(mut_a, a.cross(&b)); - mut_a = a; - - assert_eq!(-a, Vec3::new::(-1.0, -2.0, -3.0)); - assert_eq!(a.neg(), Vec3::new::(-1.0, -2.0, -3.0)); - - assert_eq!(a.mul_t(f1), Vec3::new::( 1.5, 3.0, 4.5)); - assert_eq!(a.div_t(f2), Vec3::new::( 2.0, 4.0, 6.0)); - - assert_eq!(a.add_v(&b), Vec3::new::( 5.0, 7.0, 9.0)); - assert_eq!(a.sub_v(&b), Vec3::new::( -3.0, -3.0, -3.0)); - assert_eq!(a.mul_v(&b), Vec3::new::( 4.0, 10.0, 18.0)); - assert_eq!(a.div_v(&b), Vec3::new::(1.0/4.0, 2.0/5.0, 3.0/6.0)); - - mut_a.neg_self(); - assert_eq!(mut_a, -a); - mut_a = a; - - mut_a.mul_self_t(f1); - assert_eq!(mut_a, a.mul_t(f1)); - mut_a = a; - - mut_a.div_self_t(f2); - assert_eq!(mut_a, a.div_t(f2)); - mut_a = a; - - mut_a.add_self_v(&b); - assert_eq!(mut_a, a.add_v(&b)); - mut_a = a; - - mut_a.sub_self_v(&b); - assert_eq!(mut_a, a.sub_v(&b)); - mut_a = a; - - mut_a.mul_self_v(&b); - assert_eq!(mut_a, a.mul_v(&b)); - mut_a = a; - - mut_a.div_self_v(&b); - assert_eq!(mut_a, a.div_v(&b)); - } - - #[test] - fn test_vec3_approx_eq() { - assert!(!Vec3::new::(0.000001, 0.000001, 0.000001).approx_eq(&Vec3::new::(0.0, 0.0, 0.0))); - assert!(Vec3::new::(0.0000001, 0.0000001, 0.0000001).approx_eq(&Vec3::new::(0.0, 0.0, 0.0))); - } - - #[test] - fn test_vec3_euclidean() { - let a = Vec3::new::(2.0, 3.0, 6.0); // (2, 3, 6, 7) Pythagorean quadruple - let b0 = Vec3::new::(1.0, 4.0, 8.0); // (1, 4, 8, 9) Pythagorean quadruple - let b = a.add_v(&b0); - - assert_eq!(a.length(), 7.0); - assert_eq!(a.length2(), 7.0 * 7.0); - - assert_eq!(b0.length(), 9.0); - assert_eq!(b0.length2(), 9.0 * 9.0); - - assert_eq!(a.distance(&b), 9.0); - assert_eq!(a.distance2(&b), 9.0 * 9.0); - - assert!(Vec3::new::(1.0, 0.0, 1.0).angle(&Vec3::new::(1.0, 1.0, 0.0)).approx_eq(&Real::frac_pi_3())); - assert!(Vec3::new::(10.0, 0.0, 10.0).angle(&Vec3::new::(5.0, 5.0, 0.0)).approx_eq(&Real::frac_pi_3())); - assert!(Vec3::new::(-1.0, 0.0, -1.0).angle(&Vec3::new::(1.0, -1.0, 0.0)).approx_eq(&(2.0 * Real::frac_pi_3()))); - - assert!(Vec3::new::(2.0, 3.0, 6.0).normalize().approx_eq(&Vec3::new::(2.0/7.0, 3.0/7.0, 6.0/7.0))); - // TODO: test normalize_to, normalize_self, and normalize_self_to - - let c = Vec3::new::(-2.0, -1.0, 1.0); - let d = Vec3::new::( 1.0, 0.0, 0.5); - - assert_eq!(c.lerp(&d, 0.75), Vec3::new::(0.250, -0.250, 0.625)); - - let mut mut_c = c; - mut_c.lerp_self(&d, 0.75); - assert_eq!(mut_c, c.lerp(&d, 0.75)); - } - - #[test] - fn test_vec3_boolean() { - let tft = Vec3::new(true, false, true); - let fff = Vec3::new(false, false, false); - let ttt = Vec3::new(true, true, true); - - assert_eq!(tft.any(), true); - assert_eq!(tft.all(), false); - assert_eq!(tft.not(), Vec3::new(false, true, false)); - - assert_eq!(fff.any(), false); - assert_eq!(fff.all(), false); - assert_eq!(fff.not(), Vec3::new(true, true, true)); - - assert_eq!(ttt.any(), true); - assert_eq!(ttt.all(), true); - assert_eq!(ttt.not(), Vec3::new(false, false, false)); - } -} diff --git a/src/vec4.rs b/src/vec4.rs deleted file mode 100644 index 52e89da..0000000 --- a/src/vec4.rs +++ /dev/null @@ -1,192 +0,0 @@ -// Copyright 2013 The Lmath Developers. For a full listing of the authors, -// refer to the AUTHORS file at the top-level directory of this distribution. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -use dim::Dimensional; -mod num_macros; -mod dim_macros; -mod vec_macros; - -#[deriving(Eq)] -pub struct Vec4 { x: T, y: T, z: T, w: T } - -impl_dimensional!(Vec4, T, 4) -impl_dimensional_fns!(Vec4, T, 4) -impl_approx!(Vec4) -impl_swap!(Vec4) - -impl_vec!(Vec4 { x, y, z, w }) -impl_vec_copyable!(Vec4) -impl_vec_numeric!(Vec4) -impl_vec_neg!(Vec4) -impl_vec_euclidean!(Vec4) -impl_vec_ord!(Vec4) -impl_vec_eq!(Vec4) -impl_vec_bool!(Vec4) -impl_vec_not!(Vec4) - -impl Vec4 { - #[inline] pub fn unit_x() -> Vec4 { Vec4::new(one!(T), zero!(T), zero!(T), zero!(T)) } - #[inline] pub fn unit_y() -> Vec4 { Vec4::new(zero!(T), one!(T), zero!(T), zero!(T)) } - #[inline] pub fn unit_z() -> Vec4 { Vec4::new(zero!(T), zero!(T), one!(T), zero!(T)) } - #[inline] pub fn unit_w() -> Vec4 { Vec4::new(zero!(T), zero!(T), zero!(T), one!(T)) } -} - -#[cfg(test)] -mod tests { - use vec::*; - - #[test] - fn test_vec4() { - let a = Vec4 { x: 1.0, y: 2.0, z: 3.0, w: 4.0 }; - let b = Vec4 { x: 5.0, y: 6.0, z: 7.0, w: 8.0 }; - let f1 = 1.5; - let f2 = 0.5; - - let mut mut_a = a; - - assert_eq!(Vec4::new::(1.0, 2.0, 3.0, 4.0), a); - assert_eq!(Vec4::from_value(1.0), Vec4::new::(1.0, 1.0, 1.0, 1.0)); - - *mut_a.index_mut(0) = 42.0; - *mut_a.index_mut(1) = 43.0; - *mut_a.index_mut(2) = 44.0; - *mut_a.index_mut(3) = 45.0; - assert_eq!(mut_a, Vec4::new::(42.0, 43.0, 44.0, 45.0)); - mut_a = a; - - mut_a.swap(0, 3); - assert_eq!(*mut_a.index(0), *a.index(3)); - assert_eq!(*mut_a.index(3), *a.index(0)); - mut_a = a; - - mut_a.swap(1, 2); - assert_eq!(*mut_a.index(1), *a.index(2)); - assert_eq!(*mut_a.index(2), *a.index(1)); - mut_a = a; - - assert_eq!(Vec4::zero(), Vec4::new::(0.0, 0.0, 0.0, 0.0)); - assert_eq!(Vec4::unit_x(), Vec4::new::(1.0, 0.0, 0.0, 0.0)); - assert_eq!(Vec4::unit_y(), Vec4::new::(0.0, 1.0, 0.0, 0.0)); - assert_eq!(Vec4::unit_z(), Vec4::new::(0.0, 0.0, 1.0, 0.0)); - assert_eq!(Vec4::unit_w(), Vec4::new::(0.0, 0.0, 0.0, 1.0)); - assert_eq!(Vec4::identity(), Vec4::new::(1.0, 1.0, 1.0, 1.0)); - - assert_eq!(a.x, 1.0); - assert_eq!(a.y, 2.0); - assert_eq!(a.z, 3.0); - assert_eq!(a.w, 4.0); - assert_eq!(*a.index(0), 1.0); - assert_eq!(*a.index(1), 2.0); - assert_eq!(*a.index(2), 3.0); - assert_eq!(*a.index(3), 4.0); - - assert_eq!(-a, Vec4::new::(-1.0, -2.0, -3.0, -4.0)); - assert_eq!(a.neg(), Vec4::new::(-1.0, -2.0, -3.0, -4.0)); - - assert_eq!(a.mul_t(f1), Vec4::new::( 1.5, 3.0, 4.5, 6.0)); - assert_eq!(a.div_t(f2), Vec4::new::( 2.0, 4.0, 6.0, 8.0)); - - assert_eq!(a.add_v(&b), Vec4::new::( 6.0, 8.0, 10.0, 12.0)); - assert_eq!(a.sub_v(&b), Vec4::new::( -4.0, -4.0, -4.0, -4.0)); - assert_eq!(a.mul_v(&b), Vec4::new::( 5.0, 12.0, 21.0, 32.0)); - assert_eq!(a.div_v(&b), Vec4::new::(1.0/5.0, 2.0/6.0, 3.0/7.0, 4.0/8.0)); - - assert_eq!(a.dot(&b), 70.0); - - mut_a.neg_self(); - assert_eq!(mut_a, -a); - mut_a = a; - - mut_a.mul_self_t(f1); - assert_eq!(mut_a, a.mul_t(f1)); - mut_a = a; - - mut_a.div_self_t(f2); - assert_eq!(mut_a, a.div_t(f2)); - mut_a = a; - - mut_a.add_self_v(&b); - assert_eq!(mut_a, a.add_v(&b)); - mut_a = a; - - mut_a.sub_self_v(&b); - assert_eq!(mut_a, a.sub_v(&b)); - mut_a = a; - - mut_a.mul_self_v(&b); - assert_eq!(mut_a, a.mul_v(&b)); - mut_a = a; - - mut_a.div_self_v(&b); - assert_eq!(mut_a, a.div_v(&b)); - } - - #[test] - fn test_vec4_approx_eq() { - assert!(!Vec4::new::(0.000001, 0.000001, 0.000001, 0.000001).approx_eq(&Vec4::new::(0.0, 0.0, 0.0, 0.0))); - assert!(Vec4::new::(0.0000001, 0.0000001, 0.0000001, 0.0000001).approx_eq(&Vec4::new::(0.0, 0.0, 0.0, 0.0))); - } - - #[test] - fn test_vec4_euclidean() { - let a = Vec4::new::(1.0, 2.0, 4.0, 10.0); // (1, 2, 4, 10, 11) Pythagorean quintuple - let b0 = Vec4::new::(1.0, 2.0, 8.0, 10.0); // (1, 2, 8, 10, 13) Pythagorean quintuple - let b = a.add_v(&b0); - - assert_eq!(a.length(), 11.0); - assert_eq!(a.length2(), 11.0 * 11.0); - - assert_eq!(b0.length(), 13.0); - assert_eq!(b0.length2(), 13.0 * 13.0); - - assert_eq!(a.distance(&b), 13.0); - assert_eq!(a.distance2(&b), 13.0 * 13.0); - - assert!(Vec4::new::(1.0, 0.0, 1.0, 0.0).angle(&Vec4::new::(0.0, 1.0, 0.0, 1.0)).approx_eq(&Real::frac_pi_2())); - assert!(Vec4::new::(10.0, 0.0, 10.0, 0.0).angle(&Vec4::new::(0.0, 5.0, 0.0, 5.0)).approx_eq(&Real::frac_pi_2())); - assert!(Vec4::new::(-1.0, 0.0, -1.0, 0.0).angle(&Vec4::new::(0.0, 1.0, 0.0, 1.0)).approx_eq(&Real::frac_pi_2())); - - assert!(Vec4::new::(1.0, 2.0, 4.0, 10.0).normalize().approx_eq(&Vec4::new::(1.0/11.0, 2.0/11.0, 4.0/11.0, 10.0/11.0))); - // TODO: test normalize_to, normalize_self, and normalize_self_to - - let c = Vec4::new::(-2.0, -1.0, 1.0, 2.0); - let d = Vec4::new::( 1.0, 0.0, 0.5, 1.0); - - assert_eq!(c.lerp(&d, 0.75), Vec4::new::(0.250, -0.250, 0.625, 1.250)); - - let mut mut_c = c; - mut_c.lerp_self(&d, 0.75); - assert_eq!(mut_c, c.lerp(&d, 0.75)); - } - - #[test] - fn test_vec4_boolean() { - let tftf = Vec4::new(true, false, true, false); - let ffff = Vec4::new(false, false, false, false); - let tttt = Vec4::new(true, true, true, true); - - assert_eq!(tftf.any(), true); - assert_eq!(tftf.all(), false); - assert_eq!(tftf.not(), Vec4::new(false, true, false, true)); - - assert_eq!(ffff.any(), false); - assert_eq!(ffff.all(), false); - assert_eq!(ffff.not(), Vec4::new(true, true, true, true)); - - assert_eq!(tttt.any(), true); - assert_eq!(tttt.all(), true); - assert_eq!(tttt.not(), Vec4::new(false, false, false, false)); - } -}