From 844092205e5e8c12d489b50f89d2e8aa95de495a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Crozet?= Date: Sun, 17 Aug 2014 22:41:55 +0200 Subject: [PATCH] Do not compute the matrix inverse if the determinant is near-zero. --- src/structs/spec/mat.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/structs/spec/mat.rs b/src/structs/spec/mat.rs index d35e0d75..5ff57d42 100644 --- a/src/structs/spec/mat.rs +++ b/src/structs/spec/mat.rs @@ -1,11 +1,11 @@ use std::num::{Zero, One}; use structs::vec::{Vec2, Vec3, Vec2MulRhs, Vec3MulRhs}; use structs::mat::{Mat1, Mat2, Mat3, Mat3MulRhs, Mat2MulRhs}; -use traits::operations::{Inv, Det}; +use traits::operations::{Inv, Det, ApproxEq}; use traits::structure::{Row, Col}; // some specializations: -impl Inv for Mat1 { +impl + Clone> Inv for Mat1 { #[inline] fn inv_cpy(m: &Mat1) -> Option> { let mut res = m.clone(); @@ -20,7 +20,7 @@ impl Inv for Mat1 { #[inline] fn inv(&mut self) -> bool { - if self.m11.is_zero() { + if ApproxEq::approx_eq(&self.m11, &Zero::zero()) { false } else { @@ -32,7 +32,7 @@ impl Inv for Mat1 { } } -impl Inv for Mat2 { +impl + Clone> Inv for Mat2 { #[inline] fn inv_cpy(m: &Mat2) -> Option> { let mut res = m.clone(); @@ -49,7 +49,7 @@ impl Inv for Mat2 { fn inv(&mut self) -> bool { let det = Det::det(self); - if det.is_zero() { + if ApproxEq::approx_eq(&det, &Zero::zero()) { false } else { @@ -62,7 +62,7 @@ impl Inv for Mat2 { } } -impl Inv for Mat3 { +impl + Clone> Inv for Mat3 { #[inline] fn inv_cpy(m: &Mat3) -> Option> { let mut res = m.clone(); @@ -83,7 +83,7 @@ impl Inv for Mat3 { let det = self.m11 * minor_m12_m23 - self.m12 * minor_m11_m23 + self.m13 * minor_m11_m22; - if det.is_zero() { + if ApproxEq::approx_eq(&det, &Zero::zero()) { false } else {