Why is D3DXQuaternionToAxisAngle being called in the following code?

93 Views Asked by At

I am attempting to convert some code over to glm/opengl that was originally using direct3d, and have run into a block that does not make sense according to what I found in the documentation on microsoft's website. The block in question is detailed in comments below:

Gx::Quaternion Gx::Quaternion::rotationBetween(const Gx::Vec3 &a, const Gx::Vec3 &b)
{
    Quaternion q;

    Vec3 v0 = a.normalized();
    Vec3 v1 = b.normalized();

    float d = v0.dot(v1);

    if(d >= 1.0f)
    {
        return Quaternion{ 0, 0, 0, 0 };
    }

    if(d < (1e-6f - 1.0f))
    {
        Vec3 axis = Vec3(1, 0, 0).cross(a);
        if(axis.dot(axis) == 0)
        {
            axis = Vec3(0, 1, 0).cross(a);
        }

        axis = axis.normalized();

        float ang = static_cast<float>(M_PI);
        D3DXQuaternionToAxisAngle(&q, &axis, &ang);
        
        // This block does not appear to be doing anything as
        // according to microsofts documentation on D3DXQuaternionToAxisAngle,
        // the function "Computes a quaternion's axis and angle of rotation" and
        // does not modify the quaternion value passed as it's passed as const.
        
        // Therefore I am confused as to why this block exists as it does not
        // affect the returned quaternion, and the variables axis and ang are 
        // scoped to this block and not taken into account anywhere else in this
        // function.
        
    }
    else
    {
        float s = std::sqrt((1 + d) * 2);
        float invs = 1 / s;

        Vec3 c = v0.cross(v1);

        q.x = c.x * invs;
        q.y = c.y * invs;
        q.z = c.z * invs;
        q.w = s * 0.5f;

        D3DXQuaternionNormalize(&q, &q);
    }

    return q;
}

Link to microsofts api documentation

Am I correct in my conclusion that this if block is superfluous? Or am I possibly missing something?

1

There are 1 best solutions below

0
Chuck Walbourn On BEST ANSWER

As you note, the code in the the first if case is broken. They may have meant to use D3DXQuaternionRotationAxis which has the same signature.

As a reminder, these are 'D3DXMath' functions which were in the now deprecated D3DX9/D3DX10 utility libraries. The modern solution is DirectXMath. There's a list of D3DXMath equivalents in DirectXMath here.