从四元数查看矩阵 [英] View Matrix from Quaternion

查看:197
本文介绍了从四元数查看矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在建立我自己的四元数相机。据我所知,你只需要一个四元数来完全指定相机的方向(纠正我,如果我错了)。那么,我将如何创建视图矩阵?我正在使用C ++作为编程语言。



任何帮助都会感激。

解决方案

首先有一些注意事项。你会看到很多明显冲突的公式在网上和文学上关于这个问题。大多数冲突只是明显的。一些是真正的冲突,但这是因为有人得到了数学错误。问题是没有一个正确的方法来做到这一点。你需要知道你如何使用四元数和矩阵,源如何使用它们,以及如何纠正这些明显的差异。



旋转与转换

您的相机具有与其相关联的参考框架,底层空间也是如此。你的矩阵是否表示摄像机从底层空间到摄像机方向的物理旋转,或者将底层空间中表示的矢量转换为摄像机的帧的矩阵? (或其他;这里有四个选择。)这些选择是相关的;变换矩阵是旋转矩阵的转置。变换和旋转是共轭操作。相同的概念适用于四元数。你使用转换四元数还是旋转四元数?这些又是相关的概念;一个是另一个的共轭。



左对与右四元数

给定单位四元数来转换/旋转矢量 v ,有些使用 qvq * / em> * vq 。哪种形式是正确的?两者都是。两种形式的区别仅在于非共轭四元数是在左边( qvq * )还是在右边( q / sup> vq )。



列与行向量

大多数人使用列向量,但有些使用行向量。这里你遇到一个左对右问题与矩阵。列向量通过Mv 变换/旋转,矩阵在向量的左边;

影响

您可以通过 在阅读文学时要小心。关于从四元数形成矩阵,您需要注意在构造矩阵的非对角元素时的符号变化。



左转换四元数到行向量转换矩阵

我使用左变换四元数和变换矩阵,I表示向量作为行向量。我还表示四元数 q 作为包括实数标量部分 q s 和矢量虚部 q < sub> 。给定这些表示,从四元数生成矩阵的计算是(伪码):

  //计算旋转的余弦角度。 
cost = 2.0 * qs * qs - 1.0;

//构造矩阵的对角线:
// T_ii = cost + 2qv_i ^ 2
for(i = 0; i <3; ++ i)
T [i] [i] = cost + 2.0 * qv [i] * qv [i]
}

//构造非对角线变换矩阵元素:
// T_ij = 2(qv_i qv_j - eps_ijk qs qv_k),其中eps是Levi-Civita符号
for(k = 0; k <3; ++ kk){
i =(k + 1)%3;
j =(i + 1)%3;
T [i] [j] = 2.0 *(qv [i] * qv [j] -qs * qv [k]
T [j] [i] = 2.0 *(qv [i] * qv [j] + qs * qv [k]
}

您可能想要展开这些循环。第一个循环扩展成三个语句,后者,六个。您不需要在后一个循环的展开中计算 i j ;

替代表示

上面的注意事项并不比他们看起来。您需要确保我的演示文稿与您的演示文稿一致。赔率是50-50,它不是。如果不是,只需将分配交换到非对角元素。对 T [j] [i] 使用 T [i] [j] 的计算,反之亦然。如何分辨:




  • s = 1开头。


  • 如果使用正则四元数,则乘以-1(如果使用旋转四元数而不是变换四元数)

  • 如果使用旋转矩阵而不使用转换矩阵,则

  • 乘以-1。



如果使用行向量而不是列向量,则如果为-1,只需将分配交换到 T [i] [j] T [j] [i]



上述计算适用于以下计算:当标量部分不接近于零时。如果我们有无限精度算术,它将无处不在。您可能希望对非常接近零或180度的旋转使用单独的计算。


I'm currently building my own quaternion camera. As far as I know, you only need one quaternion to fully specify the camera's orientation (correct me if I'm wrong). So, how would I go about creating the view matrix? I'm using C++ as a programming language by the way.

Any help would be appreciated.

解决方案

First some caveats. You'll see lots of apparently conflicting formulae on the 'net and in literature on this subject. Most of the conflicts are apparent only. A few are real conflicts, but that's because somebody got the math wrong. The problem is that there is no single right way to do it. You need to know how you are using quaternions and matrices, how the source is using them, and how to rectify those apparent discrepancies.

Rotation versus transformation
Your camera has a reference frame associated with it, as does the underlying space. Does your matrix represent the physical rotation of the camera from the underlying space to the camera's orientation or the matrix that transforms vectors as represented in the underlying space to the frame of the camera? (Or something else; there are four choices here.) These choices are related; the transformation matrix is the transpose of the rotation matrix. Transformation and rotation are conjugate operations. The same concept applies to quaternions. Are you using transformation quaternions or rotation quaternions? These are once again related concepts; one is the conjugate of the other.

Left versus right quaternions
Given a unit quaternion q to transform or rotate a vector v, some use qvq* to transform/rotate the vector, others use q*vq. Which form is correct? Both are. The two forms differ only in whether the unconjugated quaternion is to the left (qvq*) or to the right (q*vq) of the vector to be transformed/rotated.

Column versus row vectors
Most people use column vectors, but some do use row vectors. Here you run into a left versus right issue with matrices. Column vectors transform/rotate via Mv, with the matrix to the left of the vectors; row vectors via vM, with the matrix on the right.

Impact
You have to be careful in reading the literature. With regard to forming a matrix from a quaternion you need to watch out for sign changes in constructing the off-diagonal elements of the matrix. One formulation's addition/subtraction may change to subtraction/addition in another formulation.

Left transformation quaternions to row vector transformation matrices
I use left transformation quaternions and transformation matrices, and I represent vectors as row vectors. I also represent a quaternion q as comprising a real scalar part qs and a vectorial imaginary part qv. Given these representations, the computations to generate a matrix from a quaternion are (pseudocode):

// Compute the cosine of the rotation angle.
cost = 2.0*qs*qs - 1.0;

// Construct the diagonal of the matrix:
// T_ii = cost + 2qv_i^2
for (i = 0; i < 3; ++i) {
   T[i][i] = cost + 2.0*qv[i]*qv[i];
}

// Construct off-diagonal transformation matrix elements:
//   T_ij = 2 (qv_i qv_j - eps_ijk qs qv_k), where eps is the Levi-Civita symbol
for (k = 0; k < 3; ++kk) {
   i = (k+1)%3;
   j = (i+1)%3;
   T[i][j] = 2.0*(qv[i]*qv[j] - qs*qv[k]);
   T[j][i] = 2.0*(qv[i]*qv[j] + qs*qv[k]);
}

You might want to expand those loops out. The first loop expands into three statements, the latter, six. You don't need to compute i and j in the expansion of the latter loop; the expansion of the loop makes them fixed quantities.

Alternate representations
Those caveats above aren't as bad as they seem. You need to make sure my representation is consistent with yours. Odds are 50-50 that it isn't. If it isn't, simply swap the assignments to the off-diagonal elements. Use the computation of T[i][j] for T[j][i], and vice versa. How to tell:

  • Start with s=1.
  • Multiply s by -1 if you use rotation quaternions instead of transformation quaternions.
  • Multiply s by -1 if you use right quaternions instead of left quaternions.
  • Multiply s by -1 if you use rotation matrices instead of transformation matrices.
  • Multiply s by -1 if you use row vectors instead of column vectors.

Use my formulation if the final value of s is 1. If it's -1, simply swap the assignments to T[i][j] and T[j][i]. Or you could change the addition to subtraction, the subtraction to addition.

One last gotcha
The above calculation applies when the scalar part is not close to zero. It would be valid everywhere if we had infinite precision arithmetic. You might want to use a separate calculation for rotations that are very close to zero or 180 degrees.

这篇关于从四元数查看矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆