简体   繁体   English

单元测试欧拉到四元数实现失败

[英]Unit testing euler-to-quaternion implementation fails

I'm currently trying to implement a quaternion, in which I need euler-to-quaternion conversion.我目前正在尝试实现一个四元数,其中我需要欧拉到四元数的转换。 My current implementation looks like this, which I've nicked from here我当前的实现看起来像这样,我从这里切入

void Quaternion::FromEuler(double x, double y, double z)
{
    z *= Math::DegToRad;
    y *= Math::DegToRad;
    x *= Math::DegToRad;

    double xCos = Math::Cos(x / 2);
    double xSin = Math::Sin(x / 2);
    double yCos = Math::Cos(y / 2);
    double ySin = Math::Sin(y / 2);
    double zCos = Math::Cos(z / 2);
    double zSin = Math::Sin(z / 2);

    W = zCos * yCos * xCos + zSin * ySin * xSin;
    X = zCos * yCos * xSin - zSin * ySin * xCos;
    Y = zSin * yCos * xSin + zCos * ySin * xCos;
    Z = zSin * yCos * xCos - zCos * ySin * xSin;
}

I'm testing the implementation using the following unit test我正在使用以下单元测试来测试实现

TEST(Quaternions, FromEuler)
{
    Quaternion quaternion(45, 90, 180);


    ExpectNear(quaternion, 0.6532815, -0.2705981, 0.6532815, 0.270598);
}

Which fails in the following way.以下方式失败。

  Expected | Actual     
X  0.6533   -0.6533
Y -0.2706    0.2706
Z  0.6533    0.6533
W  0.2706    0.2706

The expected value have been acquired from a variety of websites, which yields the same values, but with different signs, similar to how my current output differs from the expected output.期望值是从各种网站获取的,它们产生相同的值,但符号不同,类似于我当前的输出与期望输出的不同。

I've also tried several different implementation, yielding the same type of failure.我还尝试了几种不同的实现,产生了相同类型的失败。

Is this due to rotations having several representations , in which case the failure of my unit test is actually a false negative?这是由于轮换有几种表示,在这种情况下,我的单元测试失败实际上是假阴性吗? If so, how do I implement a proper unit test?如果是这样,我如何实施适当的单元测试?

By simple math it can be deduced that your expection on the results is wrong: z_deg == 180, that is z_rad == PI, will give a zCos value of 0.0: cos of PI/2 is 0.0.通过简单的数学计算可以推断出您对结果的期望是错误的:z_deg == 180,即 z_rad == PI,将zCos值为 0.0:PI/2 的 cos 为 0.0。 Knowing this, you can simplify the computations of W , X , Y , Z , ignoring all products with zCos .知道了这一点,您可以简化WXYZ的计算,忽略所有与zCos

Since also x/2 and y/2 are in the first quadrant, all values for xCos , xSin , yCos , ySin and zSin are positive.由于 x/2 和 y/2 也在第一象限中,因此xCosxSinyCosySinzSin所有值都是正的。 Therefore, the result for X must be negative (0.0 minus some positive product).因此, X的结果必须为负(0.0 减去某个正乘积)。 And, the result for Y must be positive: some positive product plus 0.0.而且, Y的结果必须是正的:一些正乘积加上 0.0。

This just follows from your code and the arguments given.这只是从您的代码和给出的参数中得出的。 The conclusion is, that either the expectations are wrong, or you have found a bug in your algorithm.结论是,要么期望是错误的,要么您在算法中发现了错误。 At least (as was to be expected) the actual results are plausible :-)至少(正如预期的那样)实际结果是合理的:-)

So, check your code and the expectations, maybe you have mixed implementation and expectations from different sources?所以,检查你的代码和期望,也许你有来自不同来源的混合实现和期望? Or, if they came from the same source and you have not made any copying mistakes, the original author made some calculation error...或者,如果它们来自同一来源并且您没有犯任何复制错误,则原作者犯了一些计算错误......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM