--- /tmp/tmplrsu9G-meld/Eigen/src/Geometry/Quaternion.h +++ /home/strasdat/src/ScaViSLAM/EXTERNAL/eigen3.1/Eigen/src/Geometry/Quaternion.h @@ -545,8 +545,8 @@ return derived(); } -/** Convert the quaternion to a 3x3 rotation matrix. The quaternion is required to - * be normalized, otherwise the result is undefined. +/** Convert the quaternion to a 3x3 rotation matrix. + * The quaternion is not required to be normalized. */ template inline typename QuaternionBase::Matrix3 @@ -558,9 +558,17 @@ // it has to be inlined, and so the return by value is not an issue Matrix3 res; - const Scalar tx = Scalar(2)*this->x(); - const Scalar ty = Scalar(2)*this->y(); - const Scalar tz = Scalar(2)*this->z(); + Scalar n2 = this->squaredNorm(); + if (n2 == 0) + { + res.setIdentity(); + return res; + } + + const Scalar two_by_squarednorm = 2./n2; + const Scalar tx = two_by_squarednorm*this->x(); + const Scalar ty = two_by_squarednorm*this->y(); + const Scalar tz = two_by_squarednorm*this->z(); const Scalar twx = tx*this->w(); const Scalar twy = ty*this->w(); const Scalar twz = tz*this->w();