四元数乘积不同于从矩阵乘积中提取的四元数

Quaternion product is different from the quaternion extracted from the matrix product

昨天我一直在尝试解决以下问题:在给定全局旋转四元数和骨骼状态的情况下计算骨骼的局部旋转四元数。

我计算出全局四元数等于父骨骼全局乘以骨骼的局部四元数:

global = globalParent * local

经过一些简单的操作后,我得到了以下信息:

local = (globalParent)-1 * global

我对该等式做了一些测试,令我惊讶的是,它有时会得出正确答案,有时我会得到正确答案乘以 -1。不是局部四元数的共轭,而是整个四元数乘以-1。

所以我回到原来的等式(global = globalParent * local)并进行了测试。同样的事情发生了,有时是正确答案,有时是正确答案乘以-1。

我发现这真的很奇怪,并进一步制作矩阵乘积(globalParent * local)并提取结果的四元数。在这种情况下,我总是得到正确的答案。

最后,我的问题很简单。我在操作四元数时的思维过程错误在哪里?

我用来检查我说的东西的代码如下:

{
    var p = new THREE.Vector3();
    var s = new THREE.Vector3();
    var bone = this.get_model_bone(label);

    var gm = bone.matrixWorld;
    var g = new THREE.Quaternion();
    gm.decompose(p, g, s);
    console.log(label + " - GLOBAL: (" + g.x + ", " + g.y + ", " + g.z + ", " + g.w + ")");

    var m = bone.matrix;
    var q = new THREE.Quaternion();
    m.decompose(p, q, s);
    console.log(label + " - LOCAL: (" + q.x + ", " + q.y + ", " + q.z + ", " + q.w + ")");

    if(bone.parent !== null)
    {
        var gpm = bone.parent.matrixWorld;
        var gp = new THREE.Quaternion();
        gpm.decompose(p, gp, s);
        console.log(label + " - PARENT GLOBAL: (" + gp.x + ", " + gp.y + ", " + gp.z + ", " + gp.w + ")");

        var productMatrix = new THREE.Matrix4().multiplyMatrices(gpm, m);
        var qprod = new THREE.Quaternion();
        productMatrix.decompose(p, qprod, s);
        console.log(label + " - MATRIX PROD: (" + qprod.x + ", " + qprod.y + ", " + qprod.z + ", " + qprod.w + ")");

        var gpq = new THREE.Quaternion().multiplyQuaternions(gp, q);
        console.log(label + " - QUAT PROD: (" + gpq.x + ", " + gpq.y + ", " + gpq.z + ", " + gpq.w + ")");
    }
}

在我的日志中,有时 "MATRIX PROD" 与 "QUAT PROD" 不同。我正在使用的模型可以在以下位置找到:

https://raw.githubusercontent.com/poli-libras/virtual-jonah2/master/resources/model/human.js

I did some tests on that equation and, to my surprise, it sometimes yields the correct answer and sometimes I get the correct answer multiplied by -1.

四元数和四元数乘以-1表示完全相同的变换(旋转)。