From 1cf9f8d4ef71a0054ceefacef37951af2e8ea620 Mon Sep 17 00:00:00 2001 From: Colin Sherratt Date: Sat, 28 Jun 2014 16:47:28 -0400 Subject: [PATCH] - added translate to Matrix - fixed spaces and tabs --- src/matrix.rs | 9 +++++++++ src/test/matrix.rs | 8 ++++++++ 2 files changed, 17 insertions(+) diff --git a/src/matrix.rs b/src/matrix.rs index 8f54056..ee2b240 100644 --- a/src/matrix.rs +++ b/src/matrix.rs @@ -250,6 +250,15 @@ impl Matrix4 { pub fn identity() -> Matrix4 { Matrix4::from_value(one()) } + + /// Create a translation matrix from a Vector3 + #[inline] + pub fn from_translation(v: &Vector3) -> Matrix4 { + Matrix4::new(one(), zero(), zero(), zero(), + zero(), one(), zero(), zero(), + zero(), zero(), one(), zero(), + v.x, v.y, v.z, one()) + } } impl diff --git a/src/test/matrix.rs b/src/test/matrix.rs index 4eedc35..f2294e1 100644 --- a/src/test/matrix.rs +++ b/src/test/matrix.rs @@ -328,6 +328,14 @@ fn test_invert() { assert!(mat_f.invert().unwrap().mul_m(&mat_f).is_identity()); } +#[test] +fn test_from_translation() { + let mat = Matrix4::from_translation(&Vector3::new(1.0f64, 2.0f64, 3.0f64)); + let vertex = Vector4::new(0.0f64, 0.0f64, 0.0f64, 1.0f64); + let res = mat.mul_v(&vertex); + assert_eq!(res, Vector4::new(1., 2., 3., 1.)); +} + #[test] fn test_predicates() { // Matrix2