在四元数旋转上应用镜像效果的有效方法?

run*_*ion 6 math 3d quaternions

四元数表示旋转 - 它们不包括有关缩放或镜像的信息.但是仍然可以反映旋转的效果.

考虑在xy平面上进行镜像(我们也可以将其称为沿z轴的镜像).围绕在xy平面上镜像的x轴的旋转将被否定.同样围绕y轴旋转.但是,围绕z轴的旋转将保持不变.

另一个例子:围绕xy平面镜像的轴(1,1,1)旋转90º会使(-1,1,-1)周围旋转-90º.为了帮助直觉,如果您可以想象轴的描绘和指示旋转的圆形箭头,则镜像该可视化指示新旋转应该是什么.

我找到了一种计算旋转镜像的方法,如下所示:

  • 获取四元数的角度轴表示.
  • 对于轴x,y和z中的每一个.
    • 如果沿该轴缩放为负(镜像):
      • 取消角度和轴.
  • 从修改的角度和轴获取更新的四元数.

这仅支持沿主轴x,y和z的镜像,因为这就是我所需要的.它适用于任意旋转.

然而,从四元数到角轴以及从角轴到四元数的转换是昂贵的.我想知道是否有一种方法可以直接在四元数上进行转换,但是我对四元数数学的理解还不足以让我自己去任何地方.

(由于计算效率方法的重要性,发布在StackOverflow而不是数学相关论坛.)

BCo*_*nic 11

我花了很长时间才弄清楚这个问题的明确答案,所以我将它张贴在这里作为记录.

介绍

正如在其他答案中所指出的,镜像效果不能表示为旋转.然而,给定从坐标系C1到坐标系C2的旋转R1to2,我们可能有兴趣在将相同的镜像效果应用于C1和C2时有效地计算等效旋转(例如,我面临转换输入四元数的问题,在左手坐标系中给出,表示相同旋转但在右手坐标系中的四元数).

就旋转矩阵而言,可以认为如下:

R_mirroredC1_to_mirroredC2 = M_mirrorC2 * R_C1_to_C2 * M_mirrorC1
Run Code Online (Sandbox Code Playgroud)

在这里,无论是R_C1_to_C2R_mirroredC1_to_mirroredC2代表有效轮换,所以用四元打交道时,你是如何高效地计算q_mirroredC1_to_mirroredC2q_C1_to_C2

以下假定q_C1_to_C2=[w,x,y,z]:

  • 如果C1和C2沿X轴(即M_mirrorC1=M_mirrorC2=diag_3x3(-1,1,1))镜像q_mirroredC1_to_mirroredC2=[w,x,-y,-z]
  • 如果C1和C2沿Y轴(即M_mirrorC1=M_mirrorC2=diag_3x3(1,-1,1))镜像q_mirroredC1_to_mirroredC2=[w,-x,y,-z]
  • 如果C1和C2沿Z轴(即M_mirrorC1=M_mirrorC2=diag_3x3(1,1,-1))镜像q_mirroredC1_to_mirroredC2=[w,-x,-y,z]

在考虑C1和C2的不同镜像轴时,我们有以下内容:

  • 如果C1沿X轴镜像,C2沿Y轴镜像(即M_mirrorC1=diag_3x3(-1,1,1)&M_mirrorC2=diag_3x3(1,-1,1))q_mirroredC1_to_mirroredC2=[z,y,x,w]
  • 如果C1沿X轴镜像,C2沿Z轴镜像(即M_mirrorC1=diag_3x3(-1,1,1)&M_mirrorC2=diag_3x3(1,1,-1))q_mirroredC1_to_mirroredC2=[-y,z,-w,x]

  • 如果C1沿Y轴镜像,C2沿X轴镜像(即M_mirrorC1=diag_3x3(1,-1,1)&M_mirrorC2=diag_3x3(-1,1,1))q_mirroredC1_to_mirroredC2=[z,-y,-x,w]

  • 如果C1沿Y轴镜像,C2沿Z轴镜像(即M_mirrorC1=diag_3x3(1,-1,1)&M_mirrorC2=diag_3x3(1,1,-1))q_mirroredC1_to_mirroredC2=[x,w,z,y]

  • 如果C1沿Z轴镜像,C2沿X轴镜像(即M_mirrorC1=diag_3x3(1,1,-1)&M_mirrorC2=diag_3x3(-1,1,1))q_mirroredC1_to_mirroredC2=[y,z,w,x]

  • 如果C1沿Z轴镜像,C2沿Y轴镜像(即M_mirrorC1=diag_3x3(1,1,-1)&M_mirrorC2=diag_3x3(1,-1,1))q_mirroredC1_to_mirroredC2=[x,w,-z,-y]

测试程序

这是一个基于OpenCV的小型c ++程序来测试所有这些:

#include <opencv2/opencv.hpp>
#define CST_PI 3.1415926535897932384626433832795

// Random rotation matrix uniformly sampled from SO3 (see "Fast random rotation matrices" by J.Arvo)
cv::Matx<double,3,3> get_random_rotmat()
{
    double theta1 = 2*CST_PI*cv::randu<double>();
    double theta2 = 2*CST_PI*cv::randu<double>();
    double x3 = cv::randu<double>();
    cv::Matx<double,3,3> R(std::cos(theta1),std::sin(theta1),0,-std::sin(theta1),std::cos(theta1),0,0,0,1);
    cv::Matx<double,3,1> v(std::cos(theta2)*std::sqrt(x3),std::sin(theta2)*std::sqrt(x3),std::sqrt(1-x3));
    return -1*(cv::Matx<double,3,3>::eye()-2*v*v.t())*R;
}

cv::Matx<double,4,1> rotmat2quatwxyz(const cv::Matx<double,3,3> &R)
{
    // Implementation from Ceres 1.10
    const double trace = R(0,0) + R(1,1) + R(2,2);
    cv::Matx<double,4,1> quat_wxyz;
    if (trace >= 0.0) {
        double t = sqrt(trace + 1.0);
        quat_wxyz(0) = 0.5 * t;
        t = 0.5 / t;
        quat_wxyz(1) = (R(2,1) - R(1,2)) * t;
        quat_wxyz(2) = (R(0,2) - R(2,0)) * t;
        quat_wxyz(3) = (R(1,0) - R(0,1)) * t;
    } else {
        int i = 0;
        if (R(1, 1) > R(0, 0))
            i = 1;
        if (R(2, 2) > R(i, i))
            i = 2;

        const int j = (i + 1) % 3;
        const int k = (j + 1) % 3;
        double t = sqrt(R(i, i) - R(j, j) - R(k, k) + 1.0);
        quat_wxyz(i + 1) = 0.5 * t;
        t = 0.5 / t;
        quat_wxyz(0)     = (R(k,j) - R(j,k)) * t;
        quat_wxyz(j + 1) = (R(j,i) + R(i,j)) * t;
        quat_wxyz(k + 1) = (R(k,i) + R(i,k)) * t;
    }
    // Check that the w element is positive
    if(quat_wxyz(0)<0)
        quat_wxyz *= -1;    // quat and -quat represent the same rotation, but to make quaternion comparison easier, we always use the one with positive w
    return quat_wxyz;
}

cv::Matx<double,4,1> apply_quaternion_trick(const unsigned int item_permuts[4], const int sign_flips[4], const cv::Matx<double,4,1>& quat_wxyz)
{
    // Flip the sign of the x and z components
    cv::Matx<double,4,1> quat_flipped(sign_flips[0]*quat_wxyz(item_permuts[0]),sign_flips[1]*quat_wxyz(item_permuts[1]),sign_flips[2]*quat_wxyz(item_permuts[2]),sign_flips[3]*quat_wxyz(item_permuts[3]));
    // Check that the w element is positive
    if(quat_flipped(0)<0)
        quat_flipped *= -1; // quat and -quat represent the same rotation, but to make quaternion comparison easier, we always use the one with positive w
    return quat_flipped;
}

void detect_quaternion_trick(const cv::Matx<double,4,1> &quat_regular, const cv::Matx<double,4,1> &quat_flipped, unsigned int item_permuts[4], int sign_flips[4])
{
    if(abs(quat_regular(0))==abs(quat_flipped(0))) {
        item_permuts[0]=0;
        sign_flips[0] = (quat_regular(0)/quat_flipped(0)>0 ? 1 : -1);
    }
    else if(abs(quat_regular(0))==abs(quat_flipped(1))) {
        item_permuts[1]=0;
        sign_flips[1] = (quat_regular(0)/quat_flipped(1)>0 ? 1 : -1);
    }
    else if(abs(quat_regular(0))==abs(quat_flipped(2))) {
        item_permuts[2]=0;
        sign_flips[2] = (quat_regular(0)/quat_flipped(2)>0 ? 1 : -1);
    }
    else if(abs(quat_regular(0))==abs(quat_flipped(3))) {
        item_permuts[3]=0;
        sign_flips[3] = (quat_regular(0)/quat_flipped(3)>0 ? 1 : -1);
    }
    if(abs(quat_regular(1))==abs(quat_flipped(0))) {
        item_permuts[0]=1;
        sign_flips[0] = (quat_regular(1)/quat_flipped(0)>0 ? 1 : -1);
    }
    else if(abs(quat_regular(1))==abs(quat_flipped(1))) {
        item_permuts[1]=1;
        sign_flips[1] = (quat_regular(1)/quat_flipped(1)>0 ? 1 : -1);
    }
    else if(abs(quat_regular(1))==abs(quat_flipped(2))) {
        item_permuts[2]=1;
        sign_flips[2] = (quat_regular(1)/quat_flipped(2)>0 ? 1 : -1);
    }
    else if(abs(quat_regular(1))==abs(quat_flipped(3))) {
        item_permuts[3]=1;
        sign_flips[3] = (quat_regular(1)/quat_flipped(3)>0 ? 1 : -1);
    }
    if(abs(quat_regular(2))==abs(quat_flipped(0))) {
        item_permuts[0]=2;
        sign_flips[0] = (quat_regular(2)/quat_flipped(0)>0 ? 1 : -1);
    }
    else if(abs(quat_regular(2))==abs(quat_flipped(1))) {
        item_permuts[1]=2;
        sign_flips[1] = (quat_regular(2)/quat_flipped(1)>0 ? 1 : -1);
    }
    else if(abs(quat_regular(2))==abs(quat_flipped(2))) {
        item_permuts[2]=2;
        sign_flips[2] = (quat_regular(2)/quat_flipped(2)>0 ? 1 : -1);
    }
    else if(abs(quat_regular(2))==abs(quat_flipped(3))) {
        item_permuts[3]=2;
        sign_flips[3] = (quat_regular(2)/quat_flipped(3)>0 ? 1 : -1);
    }
    if(abs(quat_regular(3))==abs(quat_flipped(0))) {
        item_permuts[0]=3;
        sign_flips[0] = (quat_regular(3)/quat_flipped(0)>0 ? 1 : -1);
    }
    else if(abs(quat_regular(3))==abs(quat_flipped(1))) {
        item_permuts[1]=3;
        sign_flips[1] = (quat_regular(3)/quat_flipped(1)>0 ? 1 : -1);
    }
    else if(abs(quat_regular(3))==abs(quat_flipped(2))) {
        item_permuts[2]=3;
        sign_flips[2] = (quat_regular(3)/quat_flipped(2)>0 ? 1 : -1);
    }
    else if(abs(quat_regular(3))==abs(quat_flipped(3))) {
        item_permuts[3]=3;
        sign_flips[3] = (quat_regular(3)/quat_flipped(3)>0 ? 1 : -1);
    }
}

int main(int argc, char **argv)
{
    cv::Matx<double,3,3> M_xflip(-1,0,0,0,1,0,0,0,1);
    cv::Matx<double,3,3> M_yflip(1,0,0,0,-1,0,0,0,1);
    cv::Matx<double,3,3> M_zflip(1,0,0,0,1,0,0,0,-1);

    // Let the user choose the configuration
    char im,om;
    std::cout << "Enter the axis (x,y,z) along which input ref is flipped:" << std::endl;
    std::cin >> im;
    std::cout << "Enter the axis (x,y,z) along which output ref is flipped:" << std::endl;
    std::cin >> om;
    cv::Matx<double,3,3> M_iflip,M_oflip;
    if(im=='x') M_iflip=M_xflip;
    else if(im=='y') M_iflip=M_yflip;
    else if(im=='z') M_iflip=M_zflip;
    if(om=='x') M_oflip=M_xflip;
    else if(om=='y') M_oflip=M_yflip;
    else if(om=='z') M_oflip=M_zflip;

    // Generate random quaternions until we find one where no two elements are equal
    cv::Matx<double,3,3> R;
    cv::Matx<double,4,1> quat_regular,quat_flipped;
    do {
        R = get_random_rotmat();
        quat_regular = rotmat2quatwxyz(R);
    } while(quat_regular(0)==quat_regular(1) || quat_regular(0)==quat_regular(2) || quat_regular(0)==quat_regular(3) ||
            quat_regular(1)==quat_regular(2) || quat_regular(1)==quat_regular(3) ||
            quat_regular(2)==quat_regular(3));

    // Determine and display the appropriate quaternion trick
    quat_flipped = rotmat2quatwxyz(M_oflip*R*M_iflip);
    unsigned int item_permuts[4]={0,1,2,3};
    int sign_flips[4]={1,1,1,1};
    detect_quaternion_trick(quat_regular,quat_flipped,item_permuts,sign_flips);
    char str_quat[4]={'w','x','y','z'};
    std::cout << std::endl << "When iref is flipped along the " << im << "-axis and oref along the " << om << "-axis:" << std::endl;
    std::cout << "resulting_quat=[" << (sign_flips[0]>0?"":"-") << str_quat[item_permuts[0]] << ","
                                    << (sign_flips[1]>0?"":"-") << str_quat[item_permuts[1]] << ","
                                    << (sign_flips[2]>0?"":"-") << str_quat[item_permuts[2]] << ","
                                    << (sign_flips[3]>0?"":"-") << str_quat[item_permuts[3]] << "], where initial_quat=[w,x,y,z]" << std::endl;

    // Test this trick on several random rotation matrices
    unsigned int n_errors = 0, n_tests = 10000;
    std::cout << std::endl << "Performing " << n_tests << " tests on random rotation matrices:" << std::endl;
    for(unsigned int i=0; i<n_tests; ++i) {

        // Get a random rotation matrix and the corresponding quaternion
        cv::Matx<double,3,3> R = get_random_rotmat();
        cv::Matx<double,4,1> quat_regular = rotmat2quatwxyz(R);
        // Get the quaternion corresponding to the flipped coordinate frames, via the sign trick and via computation on rotation matrices
        cv::Matx<double,4,1> quat_tricked = apply_quaternion_trick(item_permuts,sign_flips,quat_regular);
        cv::Matx<double,4,1> quat_flipped = rotmat2quatwxyz(M_oflip*R*M_iflip);
        // Check that both results are identical
        if(cv::norm(quat_tricked-quat_flipped,cv::NORM_INF)>1e-6) {
            std::cout << "Error (idx=" << i << ")!"
                      << "\n   quat_regular=" << quat_regular.t()
                      << "\n   quat_tricked=" << quat_tricked.t()
                      << "\n   quat_flipped=" << quat_flipped.t() << std::endl;
            ++n_errors;
        }
    }
    std::cout << n_errors << " errors on " << n_tests << " tests." << std::endl;
    system("pause");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)


Shi*_*hah 6

有一点更容易和面向程序员的方式来思考这个问题。假设您要在坐标系中反转 z 轴(即将 z 轴翻转为 -z)。现在将四元数视为滚动、俯仰和偏航方面的方向向量。当您翻转 z 轴时,请注意横滚和俯仰的符号相反,但偏航的符号保持不变。

现在,您可以使用以下将欧拉角转换为四元数的代码来找到对四元数的净影响(我将此代码放入维基百科):

static Quaterniond toQuaternion(double pitch, double roll, double yaw)
{
    Quaterniond q;
    double t0 = std::cos(yaw * 0.5f);
    double t1 = std::sin(yaw * 0.5f);
    double t2 = std::cos(roll * 0.5f);
    double t3 = std::sin(roll * 0.5f);
    double t4 = std::cos(pitch * 0.5f);
    double t5 = std::sin(pitch * 0.5f);

    q.w() = t0 * t2 * t4 + t1 * t3 * t5;
    q.x() = t0 * t3 * t4 - t1 * t2 * t5;
    q.y() = t0 * t2 * t5 + t1 * t3 * t4;
    q.z() = t1 * t2 * t4 - t0 * t3 * t5;
    return q;
}
Run Code Online (Sandbox Code Playgroud)

使用基本的三角学,sin(-x) = -sin(x)cos(-x) = cos(x). 将此应用到上面的代码中,您可以看到 t3 和 t5 的符号将翻转。这将导致 x 和 y 的符号翻转。

所以当你反转 z 轴时,

Q'(w, x, y, z) = Q(w, -x, -y, z)
Run Code Online (Sandbox Code Playgroud)

同样,您可以找出轴反转的任何其他组合并找到对四元数的影响。

PS:如果有人想知道为什么有人会需要这个......我需要在上面转换来自控制无人机的 MavLink/Pixhawk 系统的四元数。源系统使用 NED 坐标系,但通常的 3D 环境(如 Unreal)使用 NEU 坐标系,这需要将 z 轴转换为 -z 才能正确使用四元数。


run*_*ion 5

我做了一些进一步的分析,看起来四元数 (w, x, y, z) 的效果可以像这样反映它的效果:

  • 通过翻转四元数的 y 和 z 元素沿 x 轴旋转的镜像效果。
  • 通过翻转四元数的 x 和 z 元素沿 y 轴旋转的镜像效果。
  • 通过翻转四元数的 x 和 y 元素沿 z 轴旋转的镜像效果。

永远不需要触及四元数的 w 元素。

不幸的是,我仍然不能很好地理解四元数,无法解释为什么会这样,但是我从轴角格式转换和转换的实现中推导出来,在实现这个解决方案后,它和我原来的一样好用在我执行的所有测试中。

  • 你不会碰巧遇到y轴的任何问题吧?我可以像你说的那样沿 x 轴和 z 轴镜像,但是由于某种原因,当我尝试否定 `x` 和 `z` 时,它不是沿 y 轴镜像,而是相当于沿 x 轴镜像,然后是 z 轴。 (2认同)