From 6fd9696253226b336923f72bb0d284bc59c36471 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Crozet?= Date: Sun, 30 Jun 2013 21:19:36 +0000 Subject: [PATCH] Fix bug when trying to create a rotation matrix with a zero angle. --- src/adaptors/rotmat.rs | 51 +++++++++++++++++++++++------------------- src/tests/mat.rs | 17 +++++++++----- 2 files changed, 40 insertions(+), 28 deletions(-) diff --git a/src/adaptors/rotmat.rs b/src/adaptors/rotmat.rs index 6c423338..0c033ddd 100644 --- a/src/adaptors/rotmat.rs +++ b/src/adaptors/rotmat.rs @@ -37,32 +37,37 @@ pub fn rotmat2>(angle: N) -> Rotmat> pub fn rotmat3 (axisangle: Vec3) -> Rotmat> { - let mut axis = axisangle; - let angle = axis.normalize(); - let _1 = One::one::(); - let ux = copy axis.at[0]; - let uy = copy axis.at[1]; - let uz = copy axis.at[2]; - let sqx = ux * ux; - let sqy = uy * uy; - let sqz = uz * uz; - let cos = angle.cos(); - let one_m_cos = _1 - cos; - let sin = angle.sin(); + if axisangle.sqnorm().is_zero() + { One::one() } + else + { + let mut axis = axisangle; + let angle = axis.normalize(); + let _1 = One::one::(); + let ux = copy axis.at[0]; + let uy = copy axis.at[1]; + let uz = copy axis.at[2]; + let sqx = ux * ux; + let sqy = uy * uy; + let sqz = uz * uz; + let cos = angle.cos(); + let one_m_cos = _1 - cos; + let sin = angle.sin(); - Rotmat { - submat: Mat3::new( [ - (sqx + (_1 - sqx) * cos), - (ux * uy * one_m_cos - uz * sin), - (ux * uz * one_m_cos + uy * sin), + Rotmat { + submat: Mat3::new( [ + (sqx + (_1 - sqx) * cos), + (ux * uy * one_m_cos - uz * sin), + (ux * uz * one_m_cos + uy * sin), - (ux * uy * one_m_cos + uz * sin), - (sqy + (_1 - sqy) * cos), - (uy * uz * one_m_cos - ux * sin), + (ux * uy * one_m_cos + uz * sin), + (sqy + (_1 - sqy) * cos), + (uy * uz * one_m_cos - ux * sin), - (ux * uz * one_m_cos - uy * sin), - (uy * uz * one_m_cos + ux * sin), - (sqz + (_1 - sqz) * cos) ] ) + (ux * uz * one_m_cos - uy * sin), + (uy * uz * one_m_cos + ux * sin), + (sqz + (_1 - sqz) * cos) ] ) + } } } diff --git a/src/tests/mat.rs b/src/tests/mat.rs index 37f62e8e..d7ed2ea0 100644 --- a/src/tests/mat.rs +++ b/src/tests/mat.rs @@ -15,7 +15,7 @@ use traits::transpose::Transpose; #[test] use vec::Vec1; #[test] -use mat::{Mat1, Mat2, Mat3}; +use mat::{Mat1, Mat2, Mat3, Mat4, Mat5, Mat6}; #[test] use adaptors::rotmat::Rotmat; @@ -42,10 +42,17 @@ fn test_inv_mat2() fn test_inv_mat3() { test_inv_mat_impl!(Mat3); } -// FIXME: ICE -// #[test] -// fn test_inv_nmat() -// { test_inv_mat_impl!(NMat); } +#[test] +fn test_inv_mat4() +{ test_inv_mat_impl!(Mat4); } + +#[test] +fn test_inv_mat5() +{ test_inv_mat_impl!(Mat5); } + +#[test] +fn test_inv_mat6() +{ test_inv_mat_impl!(Mat6); } #[test] fn test_rotation2()