quaternionsrotate.htm

来自「电脑图学(Computer Graphics)是资料结构、演算法与数学的应用」· HTM 代码 · 共 303 行 · 第 1/2 页

HTM
303 行
字号
<br>

四元数的加法定义如下:<br>

<div style="margin-left: 40px; font-family: Courier New,Courier,monospace; font-weight: bold;">q<sub>1</sub> + q<sub>2</sub> = (w<sub>1</sub>+w<sub>2</sub>) + (x<sub>1</sub>+x<sub>2</sub>) i + (y<sub>1</sub>+y<sub>2</sub>) j + (z<sub>1</sub>+z<sub>2</sub>) k<br>

</div>

<br>

四元数的乘法定义如下,利用简单的分配律就是了:<br>

<div style="margin-left: 40px;"><span style="font-family: Courier New,Courier,monospace; font-weight: bold;">q<sub>1</sub> * q<sub>2</sub> =</span><br style="font-family: Courier New,Courier,monospace; font-weight: bold;">

<div style="margin-left: 40px;"><span style="font-family: Courier New,Courier,monospace; font-weight: bold;">(w<sub>1</sub>*w<sub>2</sub> - x<sub>1</sub>*x<sub>2</sub> - y<sub>1</sub>*y<sub>2</sub> - z<sub>1</sub>*z<sub>2</sub>) +</span><br style="font-family: Courier New,Courier,monospace; font-weight: bold;">

<span style="font-family: Courier New,Courier,monospace; font-weight: bold;">(w</span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"><sub>1</sub></span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;">*x</span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"></span><sub><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"></span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"></span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;">2</span></sub><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"></span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"> + x</span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"><sub>1</sub></span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;">*w</span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"></span><sub><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"></span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"></span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;">2</span></sub><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"></span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"> + y</span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"><sub>1</sub></span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;">*z</span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"></span><sub><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"></span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"></span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;">2</span></sub><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"></span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"> - z</span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"><sub>1</sub></span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;">*y</span><sub><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"></span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"></span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;">2</span></sub><span style="font-family: Courier New,Courier,monospace; font-weight: bold;">) i +</span><br style="font-family: Courier New,Courier,monospace; font-weight: bold;">

<span style="font-family: Courier New,Courier,monospace; font-weight: bold;">(w</span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"><sub>1</sub></span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;">*y</span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"></span><sub><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"></span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"></span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;">2</span></sub><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"></span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"> - x</span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"><sub>1</sub></span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;">*z</span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"></span><sub><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"></span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"></span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;">2</span></sub><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"></span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"> + y</span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"><sub>1</sub></span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;">*w</span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"></span><sub><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"></span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"></span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;">2</span></sub><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"></span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"> + z</span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"><sub>1</sub></span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;">*x</span><sub><span style="font-family: Courier New,Courier,monospace; font-weight: bold;">2</span></sub><span style="font-family: Courier New,Courier,monospace; font-weight: bold;">) j +</span><br style="font-family: Courier New,Courier,monospace; font-weight: bold;">

<span style="font-family: Courier New,Courier,monospace; font-weight: bold;">(w</span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"><sub>1</sub></span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;">*z</span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"></span><sub><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"></span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"></span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;">2</span></sub><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"></span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"> + x</span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"><sub>1</sub></span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;">*y</span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"></span><sub><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"></span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"></span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;">2</span></sub><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"></span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"> - y</span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"><sub>1</sub></span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;">*x</span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"></span><sub><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"></span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"></span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;">2</span></sub><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"></span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"> + z</span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"><sub>1</sub></span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;">*w</span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"></span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"></span><sub><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"></span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"></span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;">2</span></sub><span style="font-family: Courier New,Courier,monospace; font-weight: bold;"></span><span style="font-family: Courier New,Courier,monospace; font-weight: bold;">) k<br>

</span><br>

</div>

</div>

由于q = w + x i + y j + z k中可以分为纯量w与向量x i + y j + z k,所以为了方便表示,将q表示为(S, V),其中S表示纯量w,V表示向量x i + y j + z k,所以四元数乘法又可以表示为:<br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">q<sub>1</sub> * q<sub>2</sub> = (S</span><span style="font-weight: bold; font-family: Courier New,Courier,monospace;"><sub>1</sub></span><span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> + V</span><span style="font-weight: bold; font-family: Courier New,Courier,monospace;"><sub>1</sub></span><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">)*(S</span><span style="font-weight: bold; font-family: Courier New,Courier,monospace;"><sub>2</sub></span><span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> + V</span><span style="font-weight: bold; font-family: Courier New,Courier,monospace;"><sub>2</sub></span><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">) = S</span><span style="font-weight: bold; font-family: Courier New,Courier,monospace;"><sub>1</sub></span><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">*S</span><span style="font-weight: bold; font-family: Courier New,Courier,monospace;"><sub>2</sub></span><span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> - V</span><span style="font-weight: bold; font-family: Courier New,Courier,monospace;"><sub>1</sub></span><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">.V</span><span style="font-weight: bold; font-family: Courier New,Courier,monospace;"><sub>2</sub></span><span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> + V</span><span style="font-weight: bold; font-family: Courier New,Courier,monospace;"><sub>1</sub></span><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">XV</span><span style="font-weight: bold; font-family: Courier New,Courier,monospace;"><sub>2</sub></span><span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> + S</span><span style="font-weight: bold; font-family: Courier New,Courier,monospace;"><sub>1</sub></span><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">*V</span><span style="font-weight: bold; font-family: Courier New,Courier,monospace;"><sub>2</sub></span><span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> + S</span><span style="font-weight: bold; font-family: Courier New,Courier,monospace;"><sub>2</sub></span><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">*V</span><span style="font-weight: bold; font-family: Courier New,Courier,monospace;"><sub>1</sub></span></div>

<br>

其中V<sub>1</sub>.V<sub>2</sub>表示向量内积,V<sub>1</sub>XV<sub>2</sub>表示向量外积。<br>

<br>

定义四元数q = w + x i + y j +z k 的norm为:<br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">N(q) = |q| = x</span><sup style="font-weight: bold; font-family: Courier New,Courier,monospace;">2</sup><span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> + y</span><sup style="font-weight: bold; font-family: Courier New,Courier,monospace;">2</sup><span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> + z</span><sup style="font-weight: bold; font-family: Courier New,Courier,monospace;">2</sup><span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> + w</span><sup style="font-weight: bold; font-family: Courier New,Courier,monospace;">2</sup><br>

</div>

<br>

满足N(q) = 1的四元数集合,称之为单位四元数(Unit quaternions)。<br>

<br>

定义四元数定义四元数q = w + x i + y j +zk的共轭(Conjugate)为:<br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">q* = 定义四元数q = w - x i - y j -z k = [S - V]</span><br>

</div>

<br>

定义四元数的倒数为:<br>

<div style="margin-left: 40px;"><span style="font-family: Courier New,Courier,monospace;">1/ q = q* / N(q)</span><br>

</div>

<br>

说明了一些数学,您所关心的或许是,四元数与旋转究竟有何关系,假设有一任意旋转轴的向量A(Xa, Ya, Za)与一旋转角度&theta;,如下图所示:<br>

<img style="width: 128px; height: 160px;" alt="" src="images/quaternionsRotate-1.jpg"><br>

<br>

可以将之转换为四元数:<br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">x = s * Xa</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">y = s * Xb</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">z = s * Xc</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">w = cos(&theta;/2)</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">s = sin(&theta;/2)</span><br>

</div>

<br>

所以使用四元数来表示的好处是:我们可以简单的取出旋转轴与旋转角度。<br>

<br>

那么四元数如何表示三维空间的任意轴旋转?假设有一向量P(X, Y, Z)对着一单位四元数q作旋转,则将P视为无纯量的四元数X i + Y j + Z k,则向量的旋转经导证如下:<br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">Rot(P) = q p q*</span><br>

</div>

<br>

四元数具有纯量与向量,为了计算方便,将之以矩阵的方式来表现四元数的乘法,假设将四元数表示如下:<br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">q = [w, x, y, z] = [S, V]</span><br>

</div>

<br>

两个四元数相乘q" = q * q'的矩阵表示法如下所示: <br>

<img style="width: 264px; height: 112px;" alt="" src="images/quaternionsRotate-2.jpg"><br>

若令q = [S, V] = [cos&theta;, u*sin&theta;],其中u为单位向量,而令q'= [S', V']为一四元数,则经过导证,可以得出q * q' * q^(-1)会使得q'绕着u轴旋转2&theta;。<br>

<br>

由四元数的矩阵乘法与四元数的旋转,可以导证出上面的旋转公式可以使用以下的矩阵乘法来达成: <br>

<img style="width: 510px; height: 108px;" alt="" src="images/quaternionsRotate-3.jpg"><br>

<br>

讲了这么多,其实就是要引出上面这个矩阵乘法,也就是说如果您要让向量(x', y', z')(w'为0)对某个单位向量轴u(x, y,
z)旋转角度2&theta;,则w = cos&theta;,代入以上的矩阵乘法,即可得旋转后的(x", y",
z"),如果为了方便,转换矩阵的最下列与最右行会省略不写出来,而如下所示:<br>

<img style="width: 488px; height: 106px;" alt="" src="images/quaternionsRotate-4.jpg"><br>

<br>

关于四元数的其它说明,您可以参考 <a href="http://episte.math.ntu.edu.tw/articles/mm/mm_15_1_12/page2.html">向量外积与四元数</a> 这篇文章。<br>

<br>

关于旋转的转换矩阵导证,在Game Programming Gems第二章有详细的说明。<br>

<br>

关于 <a href="http://www.anticz.com/eularqua.htm">Gimbal lock</a>。<br>

<br>







</body>
</html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?