求多个四元数的平均数

最近要对前几帧数据进行平均数采样。所以又造了一个轮子

 

此方法经过一些单数和双数的基本测试,并且解决了无效四元数的问题,基本可用:

public static Quaternion Average(Quaternion[] quatArray)
{
    var result = new Quaternion();
    var count = quatArray.Length;
    var error = 0;

    while (count > 1)
    {
        if (error >= 10000) break;
        error++;
        var k = 0;
        for (int i = 0; i + 1 < count; i += 2)
        {
            var a = quatArray[i];
            var b = quatArray[i + 1];

            if (Quaternion.Dot(a, a) < Quaternion.kEpsilon)
                a = Quaternion.identity;

            if (Quaternion.Dot(b, b) < Quaternion.kEpsilon)
                b = Quaternion.identity;

            var avgQuat = Quaternion.LerpUnclamped(a, b, 0.5f);

            quatArray[k] = avgQuat;
            k++;
        }

        var lastCount = count;
        count = k;

        if ((lastCount & 1) == 1)
        {
            k++;
            count++;
            quatArray[k] = quatArray[lastCount - 1];
        }
    }

    result = quatArray[0];

    return result;
}

 

你可能感兴趣的:(求多个四元数的平均数)