四元数的运算:
基本的:
p=[1 2 3 4] q=[5 6 7 8]
p+q=[6 8 10 12]
2p=[2 4 6 8]
2个四元数的积:
p=[m,u] q=[n,v] pq=[mn-vu,nu+mv+(v×u)]
m,n是标量,u,v是向量
共轭四元数:
p=[n,v] ~p=[n,-v]
旋转1个四元数( 或向量):
p'=q(p)(~q)
旋转向量的话:用向量取代p的向量部分,p的标量部分取零。
四元数到旋转矩阵的变换:
| w2+x2-y2-y2 2xy-2wz 2xy+2yn |
| 2xy+2wz w2-x2+y2-y2 2yz-2wx |
| 2xz-2wy 2yz-2wx w2-x2-y2+z2 |
旋转矩阵到四元数的变换:
tr=m11+m22+m33
if(tr>0)
{
temp=1/2squrt(tr+1);
qw=0.25/temp qx=(m23-m32)temp qy=(m31-m13)temp qz=(m12-m21)temp
}else
{
m11,m22,m33中
if(m11 is greatest){
temp=1/2squrt(1+m11-m22+m33)
qw=0.25/temp qx=(m21+m12)temp qy=(m13+m31)temp qz=(m32-m23)temp}
if(m22 is greatest){
temp=1/squrt(1+m22-m11-m33)
qw=(m21+m12temp qx=0.25/temp qy=(m32+m23)temp qz=(m13-m31)temp}
if(m33 is greatest){
temp=1/squrt(1+m33-m11-m22)
qw=(m13+m31)temp qx=(m32+m23)temp qy=0.25/temp qz=(m21-m12)temp}
}
Euler Angles and Quaternions:
q=[cos(angle/2),sing(angle/2)axis]
axis为一向量,是旋转所绕之轴
sa=squrt(1-qw2) angle=2arccos(qw)
axisx=qx/sa axisy=qy/sa axisz=qz/sa
还有四元数的线性和球面插值晚上再打。