From e270d56d1b6c8346f4387021e85b980851f9d877 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Tessier?= Date: Fri, 22 Nov 2019 11:34:50 -0500 Subject: [PATCH] Fix #522: float-guard the identity matrix test Provide floating-point error margins in testing whether or not a matrix is the identity matrix. This is done on a component by component basis. Previously, the matrix values were compared directly to constants '0' and '1', so the test could fail on account of floating-point errors. --- HEN_HOUSE/egs++/egs_transformations.h | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/HEN_HOUSE/egs++/egs_transformations.h b/HEN_HOUSE/egs++/egs_transformations.h index 5c1113a44..e38e2e3c5 100644 --- a/HEN_HOUSE/egs++/egs_transformations.h +++ b/HEN_HOUSE/egs++/egs_transformations.h @@ -218,11 +218,20 @@ class EGS_EXPORT EGS_RotationMatrix { /*! \brief Returns \c true, if this object is approximately the unit matrix, \a false otherwise */ bool isI() const { - // compute square distance to unit matrix - EGS_Float d2 = (rxx-1)*(rxx-1) + rxy*rxy + rxz*rxz + - ryx*ryx + (ryy-1)*(ryy-1) + ryz*ryz + - rzx*rzx + rzy*rzy + (rzz-1)*(rzz-1); - return (d2 < epsilon*epsilon); + + // compare with unit matrix components + if (fabs(rxy) < epsilon && + fabs(rxz) < epsilon && + fabs(ryx) < epsilon && + fabs(ryz) < epsilon && + fabs(rzx) < epsilon && + fabs(rzy) < epsilon && + fabs(rxx-1) < epsilon && + fabs(ryy-1) < epsilon && + fabs(rzz-1) < epsilon ) { + return true; + } + return false; }; /*! \brief Returns the rotated a vector \f$ R \cdot \vec{v}\f$ */