Changeset 1319


Ignore:
Timestamp:
Apr 30, 2012, 3:40:57 PM (9 years ago)
Author:
sam
Message:

math: fix a bug in the rotation matrix to quaternion conversion, found
with the unit tests.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/math/vector.cpp

    r1315 r1319  
    419419}
    420420
    421 template<> quat::Quat(mat4 const &m)
     421static void MatrixToQuat(quat &that, mat3 const &m)
    422422{
    423423    /* See http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/christian.htm for a version with no branches */
     
    425425    if (t > 0)
    426426    {
    427         w = 0.5f * sqrtf(1.0f + t);
    428         float s = 0.25f / w;
    429         x = s * (m[2][1] - m[1][2]);
    430         y = s * (m[0][2] - m[2][0]);
    431         z = s * (m[1][0] - m[0][1]);
     427        that.w = 0.5f * sqrtf(1.0f + t);
     428        float s = 0.25f / that.w;
     429        that.x = s * (m[1][2] - m[2][1]);
     430        that.y = s * (m[2][0] - m[0][2]);
     431        that.z = s * (m[0][1] - m[1][0]);
    432432    }
    433433    else if (m[0][0] > m[1][1] && m[0][0] > m[2][2])
    434434    {
    435         x = 0.5f * sqrtf(1.0f + m[0][0] - m[1][1] - m[2][2]);
    436         float s = 0.25f / x;
    437         y = s * (m[1][0] + m[0][1]);
    438         z = s * (m[0][2] + m[2][0]);
    439         w = s * (m[2][1] - m[1][2]);
     435        that.x = 0.5f * sqrtf(1.0f + m[0][0] - m[1][1] - m[2][2]);
     436        float s = 0.25f / that.x;
     437        that.y = s * (m[0][1] + m[1][0]);
     438        that.z = s * (m[2][0] + m[0][2]);
     439        that.w = s * (m[1][2] - m[2][1]);
    440440    }
    441441    else if (m[1][1] > m[2][2])
    442442    {
    443         y = 0.5f * sqrtf(1.0f - m[0][0] + m[1][1] - m[2][2]);
    444         float s = 0.25f / y;
    445         x = s * (m[1][0] + m[0][1]);
    446         z = s * (m[2][1] + m[1][2]);
    447         w = s * (m[0][2] - m[2][0]);
     443        that.y = 0.5f * sqrtf(1.0f - m[0][0] + m[1][1] - m[2][2]);
     444        float s = 0.25f / that.y;
     445        that.x = s * (m[0][1] + m[1][0]);
     446        that.z = s * (m[1][2] + m[2][1]);
     447        that.w = s * (m[2][0] - m[0][2]);
    448448    }
    449449    else
    450450    {
    451         z = 0.5f * sqrtf(1.0f - m[0][0] - m[1][1] + m[2][2]);
    452         float s = 0.25f / z;
    453         x = s * (m[0][2] + m[2][0]);
    454         y = s * (m[2][1] + m[1][2]);
    455         w = s * (m[1][0] - m[0][1]);
     451        that.z = 0.5f * sqrtf(1.0f - m[0][0] - m[1][1] + m[2][2]);
     452        float s = 0.25f / that.z;
     453        that.x = s * (m[2][0] + m[0][2]);
     454        that.y = s * (m[1][2] + m[2][1]);
     455        that.w = s * (m[0][1] - m[1][0]);
    456456    }
     457}
     458
     459template<> quat::Quat(mat3 const &m)
     460{
     461    MatrixToQuat(*this, m);
     462}
     463
     464template<> quat::Quat(mat4 const &m)
     465{
     466    MatrixToQuat(*this, mat3(m));
    457467}
    458468
Note: See TracChangeset for help on using the changeset viewer.