Skip to content

Commit

Permalink
Fix #522: float-guard the identity matrix test
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ftessier committed Jan 13, 2020
1 parent d4b9afb commit e270d56
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions HEN_HOUSE/egs++/egs_transformations.h
Original file line number Diff line number Diff line change
Expand Up @@ -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$ */
Expand Down

0 comments on commit e270d56

Please sign in to comment.