Compuer Animation(Postgraduate Course): Lecture 3:Representation of transformation and rotation

Welcome file

Lecture 3: Representation of transformation and rotation

Motion Specification:

  • Low level techniques (techniques that aid the animator in precisely specifying motion)

  • High level techniques (techniques used to describe general motion behavior)

  • Translate, scale and rotation are all below to low level.

  • Transformation can be used to change the position of objects; Set animations for objects and light source.

Technical Background:

  • Spaces and transformations:

  • Coordinate Space: left-handed, right-handed, local coordinates system, global coordinates system.

  • Viewing pipeline: Homogeneous coordinate, Transformation matrix, Matrix Concatenation

  • Orientation representation:

  • Rotation matrix

  • Fixed angle

  • Euler angle

  • Angle and Axis

  • Quaternion

I.Representation of Transformation

1.1.3-D Transformations

  1. Translate, scale, or rotate a point pp to pp':

p=p+Tp'=p+T p=Spp'=Sp pP=RppP'=Rp

2.How to treat these transformations in a unified way?

p=Mpp'=Mp

All in the homogeneous coordinate

  1. MM can be used for animation, viewing, or modeling.

1.2.Homogeneous Coordinate

1.In graphics, we use homogeneous coordinate for transformation

2.4×44\times 4 matrix can represent translation, scaling, and rotation and other transformations:

(xw,yw,zw)[x,y,z,w]\left(\frac{x}{w}, \frac{y}{w}, \frac{z}{w}\right)\Leftarrow [x, y, z, w]

3.Typically, when transforming a point in 3D space, we set w=1w=1

(x,y,z)[x,y,z,1](x, y, z)\Leftarrow [x, y, z, 1]

1.3.Translation

[xyz1]=[100tx010ty001tz0001][xyz1]\begin{bmatrix}x'\\ y'\\ z'\\ 1\\\end{bmatrix}=\begin{bmatrix}1&0&0&t_x\\ 0&1&0&t_y\\ 0&0&1&t_z\\ 0&0&0&1\\ \end{bmatrix} \begin{bmatrix}x\\ y\\ z\\ 1\\ \end{bmatrix}

1.4.Scaling

[xyz1]=[sx0000sy0000sz00001][xyz1]\begin{bmatrix}x'\\ y'\\ z'\\ 1\end{bmatrix}= \begin{bmatrix}s_x&0&0&0\\ 0&s_y&0&0\\ 0&0&s_z&0\\ 0&0&0&1\\\end{bmatrix}\begin{bmatrix}x\\ y\\ z\\ 1\\ \end{bmatrix}

1.5.Rotation

  • X axis:

Rx(θ)[xyz1]=[10000cos(θ)sin(θ)00sin(θ)cos(θ)00001][xyz1]R_x(\theta)\quad \begin{bmatrix}x'\\ y'\\ z'\\ 1\\\end{bmatrix}=\begin{bmatrix}1&0&0&0\\ 0&\cos(\theta)&-\sin(\theta)&0\\ 0&\sin(\theta)&\cos(\theta)&0\\ 0&0&0&1\\ \end{bmatrix}\begin{bmatrix}x\\ y\\ z\\ 1\\ \end{bmatrix}

  • Y axis:

Ry(θ)[xyz1]=[cos(θ)0sin(θ)00100sin(θ)0cos(θ)00001][xyz1]R_y(\theta)\quad \begin{bmatrix}x'\\y'\\z'\\1\\\end{bmatrix}=\begin{bmatrix}\cos(\theta)&0&\sin(\theta)&0\\0&1&0&0\\-\sin(\theta)&0&\cos(\theta)&0\\0&0&0&1\\\end{bmatrix}\begin{bmatrix}x\\y\\z\\1\\\end{bmatrix}

  • Z axis:

Rz(θ)[xyz1]=[cos(θ)sin(θ)00sin(θ)cos(θ)0000100001][xyz1]R_z(\theta)\quad \begin{bmatrix}x'\\y'\\z'\\1\\\end{bmatrix}=\begin{bmatrix}\cos(\theta)&-\sin(\theta)&0&0\\\sin(\theta)&\cos(\theta)&0&0\\0&0&1&0\\0&0&0&1\\\end{bmatrix}\begin{bmatrix}x\\y\\z\\1\\\end{bmatrix}

Properties:

  1. Trace is not connected with the rotation axis, we all have:tr(R)=1+2cos(θ)tr(R)=1 + 2\cos(\theta)

  2. All rotation matrices are orthogonal, multiplication of several rotation matrices are stll orthogonal:R1=RTR^{-1}=R^T

1.6.Transformations Concatenation

Transformations can be treated as a series of matrix multiplications:

P=M1M2M3...MnPP'=M_1M_2M_3...M_nP

M=M1M2M3...MnM=M_1M_2M_3...M_n

P=MP\Rightarrow P'=MP

PT=(M1M2M3...MnP)TP'^T=(M_1M_2M_3...M_nP)^T

MT=MnTMn1T...M1TM^T=M_n^TM_{n-1}^T...M_1^T

PT=PTMTP'^T=P^TM^T

1.7.Rigid body transformation

1.Only the position(translate transformation ) and direction(rotation transformation) change, it remains the geometry unchanged, that transformation is called rigid body transformation.

2.Feature: Keep the length and angle

X=T(t)R=(r00r01r02txr10r11r12tyr20r21r22tz0001)X=T(t)R=\begin{pmatrix}r_{00}&r_{01}&r_{02}&t_x\\r_{10}&r_{11}&r_{12}&t_y\\r_{20}&r_{21}&r_{22}&t_z\\0&0&0&1\\\end{pmatrix}

Calculate the inverse matrix:

X1=(T(t)R)1=R1T(t)1=RTT(t)X^{-1}=(T(t)R)^{-1}=R^{-1}T(t)^{-1}=R^TT(-t)

The calculation of the inverse matrix:

1.If a matrix is composed of one or several simple transformation, and the parameters are known, the inverse matrix can be multipled in order through the inverse parameters:

A example is :M=T(t)R(θ)M1R(θ)T(t)M=T(t)R(\theta)\Rightarrow M^{-1}R(-\theta)T(-t)

2.If the matrix is orthogonal, then M1=MTM^{-1}=M^T

3.If nothing is known: Accompanying matrix method, Cramer’s method, LU decomposition method, Gauss elimination method

  1. Cramer’s method and Accompanying method have less “if”, we should consider at first.

II. 3D Rotation representations

1.Rotation Matrix

  • Rows/columns of matrix must be orthogonal: Unit length and orthogonal

  • Numerical errors cause a nonorthogonal matrix when a series of rotations apply

  • How to interpolate between matrices?

  • Interpolating the components of two matrices doesn’t maintain the orthonomality.

  • The generated matrix is not a rotation matrix.

See the Problem:

[010100001][010100001]\begin{bmatrix}0&-1&0\\1&0&0\\0&0&1\\\end{bmatrix}\to\begin{bmatrix}0&1&0\\-1&0&0\\0&0&1\\\end{bmatrix}

The halfway matrix you get by linearly interpolating each entry is:

[000000001]\begin{bmatrix}0&0&0\\0&0&0\\0&0&1\\\end{bmatrix}

It is obviously that it is not a rotation matrix any more!

2.Fixed Angle

  • Ordered triple of rotations about fixed axes

  • Any triple can be used that doesn’t repeat an axis:

immediately, e.g., x-y-z is fine, so is x-y-x. But x-x-z is not.

Gimbal:

A gimbal is a mechanical device allowing the rotation of an object in multiple dimensions.

Gimbal Lock:

Gimbal lock occurs when two of the rotation aces align; e.g., x and y axes in the figure: Lost a degree of freedom; cannot rotate about x-axis.

For an orientation (0,90,0)(0, 90, 0),

  • A slight change in the first value (+/ϵ,90,0)(+/-\epsilon, 90, 0)

  • A slight change in the third value (0,90,+/ϵ)(0, 90, +/-\epsilon)

  • 90-degree y-axis rotation essentially makes x-axis align with z-axis -> gimbal lock: From (0,90,0)(0, 90, 0), the object can no longer be rotated about x-axis by a small change since orientation actually performed is (90,90+ϵ,90)(90, 90+\epsilon, 90)

Interpolation Problem in Fixed Angle:

  • The rotation from (0,90,0)(0, 90, 0) to (90,45,90)(90, 45, 90) is a 45-degree x-axis rotation: Impossible because the first 90-degree y-axis rotation

  • Directly interpolating between (0,90,0)(0, 90, 0) and (90,45,90)(90, 45, 90) produces a halfway orientation (45,67.5,45)(45, 67.5, 45): Desired halfway orientation is (90,22.5,90)(90, 22.5, 90)

Conclusion for Fixed Angle Representation:

  • Compact

  • Fairly intuitive

  • Easy to work

  • But not the most desirable representation to use because gimal lock problem.

3.Euler Angle

  • Euler Amgle is an efficient way to make the camera or object rotate toward an given direction.

  • Ordered triple of rotations about local axes.

  • As with fixed angles, any triple can be used that doesn’t immediately repeat an axis, e.g., x-y-z is fine, so is x-y-x. But x-x-z is not.

  • Euler angle ordering is equivalent to reverse ordering in fixed angles: The Euler anghle representation has exactly the same advantages and disadvantages as those of the fixed angle representation.

Calculation:

  • Euler angle ordering is equivalent to reverse ordering in fixed angles.

  • For a Euler angle representation in x-y-z ordering:

  • Y-axis rotation: around the y-axis of the local, rotated coordinate system:Ry(β)Rx(α)=Rx(α)Ry(β)Rx(α)Rx(α)=Rx(α)Ry(β)R_y'(\beta)R_x(\alpha)=R_x(\alpha)R_y(\beta)R_x(-\alpha)R_x(\alpha)=R_x(\alpha)R_y(\beta)

  • Z-axis rotation:around the twice-rotated frame

Rz(γ)Ry(β)Rx(α)=Rx(α)Ry(β)Rz(γ)Ry(β)Rx(α)Rx(α)Ry(β)=Rx(α)Ry(β)Rz(γ)\begin{aligned}&R_z''(\gamma)R_y'(\beta)R_x(\alpha)\\&=R_x(\alpha)R_y(\beta)R_z(\gamma)R_y(-\beta)R_x(-\alpha)R_x(\alpha)R_y(\beta)\\&=R_x(\alpha)R_y(\beta)R_z(\gamma)\end{aligned}

Gimbal lock in Euler Angle:

When p=π2p=\frac{\pi}{2}, the matrix only rely on the triangle (r+h)(r+h):

E(h,π2,r)=[cosrcoshsinrsinh0cosrsinh+sinrcoshsinrcosh+cosrsinh0sinrsinhcosrcosh010]=[cos(r+h)0sin(r+h)sin(r+h)0cos(r+h)010]\begin{aligned}E\left(h, \frac{\pi}{2}, r\right)&=\begin{bmatrix}\cos r\cos h-\sin r\sin h&0&\cos r \sin h + \sin r\cos h\\\sin r\cos h+\cos r\sin h&0&\sin r\sin h-\cos r\cos h\\0&1&0\\\end{bmatrix}\\&=\begin{bmatrix}\cos (r + h)& 0& \sin (r+h)\\\sin(r+h)&0&-\cos(r+h)\\0&1&0\\\end{bmatrix}\end{aligned}

4.Axis Angle

  • Euler’s rotation theorem:

  • One orientation can be derived from another by a single rotation about an axis

  • So ,we use an axis and a single angle to represent an orientation(with respect to the object’s initial orientation)

  • Interpolation can be implemented by interpolating axes of rotation and angles separately; but the transfomation concatenation cannot be done easily.

5.Quaternion

  • Similar to axis-angle representations: 4-tuple of real numbers:

q=(s,x,y,z)q=(s,x, y, z) or [s,v][s, \vec{v}], ss is a scalar; v\vec{v} is a vector

  • The quaternion for rotating an angle about an axis(an axis-angle rotation):

q=Rot(θ,(ax,ay,az))=[cos(θ2),sin(θ2)(ax,ay,az)]\begin{aligned}q&=\text{Rot}\left(\theta, (a_x,a_y,a_z)\right)\\&=\left[\cos\left(\frac{\theta}{2}\right), \sin\left(\frac{\theta}{2}\right)(a_x, a_y,a_z)\right]\end{aligned}

If aa is unit length, then qq will be also:

q=q02+q12+q22+q32=cos2θ2+ax2+sin2θ2+ay2sin2θ2+az2sin2θ2=cos2θ2+sin2θ2(ax2+ay2+az2)=cos2θ2+sin2θ2a2=cos2θ2+sin2θ2=1\begin{aligned}|q|&=\sqrt{q_0^2+q_1^2+q_2^2+q_3^2}\\&=\sqrt{\cos^2\frac{\theta}{2}+a_x^2+\sin^2\frac{\theta}{2}+a_y^2\sin^2\frac{\theta}{2}+a_z^2\sin^2\frac{\theta}{2}}\\&=\sqrt{\cos^2\frac{\theta}{2}+\sin^2\frac{\theta}{2}(a_x^2+a_y^2+a_z^2)}\\&=\sqrt{\cos^2\frac{\theta}{2}+\sin^2\frac{\theta}{2}|\vec{a}|^2}=\sqrt{\cos^2\frac{\theta}{2}+\sin^2\frac{\theta}{2}}\\&=1\end{aligned}

  • Rotating some angle around an axis is the same as rotating the negative angle around the negated axis

q=Rotθ,(x,y,z)=[cos(θ2),sin(θ2)((x,y,z))]=[cos(θ2),sin(θ2)(x,y,z)]=Rotθ,(x,y,z)=q\begin{aligned}-q&=\text{Rot}_{-\theta, -(x, y,z)}\\&=\left[\cos\left(-\frac{\theta}{2}\right),\sin \left(-\frac{\theta}{2}\right)(-(x, y, z))\right]\\&=\left[\cos\left(\frac{\theta}{2}\right),\sin\left(\frac{\theta}{2}\right)(x,y,z)\right]\\&=\text{Rot}_{\theta,(x,y,z)}\\&=q\end{aligned}

Quaternion Math

Definition for Addition:

[s1,v1]+[s2,v2]=[s1+s2,v1+v2][s_1,\vec{v_1}]+[s_2,\vec{v_2}]=[s_1+s_2,\vec{v_1}+\vec{v_2}]

Definition for Multiplication:

[s1,v1][s2,v2]=[s1s2v1v2,s1v2+s2v1+v1×v2][s_1,\vec{v_1}][s_2,\vec{v_2}]=[s_1s_2-\vec{v_1}·\vec{v_2},s_1\vec{v_2}+s_2\vec{v_1}+\vec{v_1}\times \vec{v_2}]

Multiplication is associative but not commutative:

q1(q2q3)=(q1q2)q3q_1(q_2q_3)=(q_1q_2)q_3

q1q2q2q1q_1q_2\neq q_2q_1

Multiplicative identity:[1, 0, 0, 0]

q[1,0,0,0]=qq[1, 0, 0, 0]=q

Inverse:

q1=[s,v]q2q^{-1}=\frac{[s,-v]}{\|q\|^2}

qq1=[1,0,0,0]qq^{-1}=[1,0,0,0]

Normalization for unit quaternion:

q^=qq\hat{q}=\frac{q}{\|q\|}

q=s2+x2+y2+z2\|q\|=\sqrt{s^2+x^2+y^2+z^2}

As for Quaternion:

q=sinθuq+cosθ=eθuq\vec{q}=\sin \theta \vec{u_q}+\cos \theta = e^{\theta \vec{u_q}}

Logarithm:

log(q)=log(eθuq)=θuq\log (\vec{q})=\log (e^{\theta \vec{u_q}})=\theta \vec{u_q}

Exponent:

qt=(sinθuq,cosθ)t=eθtuq=sin(θt)uq+cos(θt)\vec{q}^t=(\sin \theta \vec{u_q},\cos \theta)^t=e^{\theta t \vec{u_q}}=\sin (\theta t)\vec{u_q}+\cos(\theta t)

The transformation from rotation matrix to quaternion: for unit quaternion q=(qw,qx,qy,qz)q=(q_w,q_x,q_y,q_z),we have:

Mq=(12(qy2+qz2)2(qxqyqwqz)2(qxqz+qwqy)02(qxqy+qwqz)12(qx2+qz2)2(qyqzqwqx)02(qxqzqwqy)2(qyqz+qwqx)12(qx2+qy2)00001)M^q=\begin{pmatrix}1-2(q_y^2+q_z^2)&2(q_xq_y-q_wq_z)&2(q_xq_z+q_wq_y)&0\\ 2(q_xq_y+q_wq_z)&1-2(q_x^2+q_z^2)&2(q_yq_z-q_wq_x)&0\\2(q_xq_z-q_wq_y)&2(q_yq_z+q_wq_x)&1-2(q_x^2+q_y^2)&0\\0&0&0&1\\\end{pmatrix}

Noticed that:

tr(Mq)=42s(qx2+qy2+qz2)=4(1qx2+qy2+qz2qx2+qy2+qz2+qw2)=4qw2qx2+qy2+qz2+qw2=4qw2n(q)\begin{aligned}tr(M^q)&=4-2s(q_x^2+q_y^2+q_z^2)\\&=4\left(1-\frac{q_x^2+q_y^2+q_z^2}{q_x^2+q_y^2+q_z^2+q_w^2}\right)\\&=\frac{4q_w^2}{q_x^2+q_y^2+q_z^2+q_w^2}\\&=\frac{4q_w^2}{n(\vec{q})}\end{aligned}

Then the unit quaternion is:

{qw=12tr(Mq)qx=m21qm12q4qwqy=m02qm20q4qwqz=m10qm01q4qw\begin{cases}q_w=\frac{1}{2}\sqrt{tr(M^q)}\\q_x=\frac{m_{21}^q-m_{12}^q}{4q_w}\\q_y=\frac{m_{02}^q-m_{20}^q}{4q_w}\\q_z=\frac{m_{10}^q-m_{01}^q}{4q_w}\end{cases}

Rotating Vectors Using Quaternion

  • A point in space v\vec{v}, is represented as [0,v][0, \vec{v}]

  • To rotate a vector v\vec{v} using quaternion qq

  • Represent the vector as v=[0,v]v=[0, \vec{v}]

  • Represent the rotation as a quaternion qq

  • Using quaternion multiplication v=Rotq(v)=qvq1\vec{v}'=\text{Rot}_q(\vec{v})=q\vec{v}q^{-1}

  • Note that the result v\vec{v}' always has zero scalar value

  • Rotating a vector v\vec{v} by first quaternion pp followed by a quaternion qq is like rotation using qpqp:

Rotq(Rotp(v))=Rotq(pvp1)=qpvp1q1=(qp)v(qp)1=Rotqp(v)\begin{aligned}\text{Rot}_q(\text{Rot}_p(\vec{v}))&=\text{Rot}_q(p\vec{v}p^{-1})\\&=qp\vec{v}p^{-1}q^{-1}\\&=(qp)\vec{v}(qp)^{-1}\\&=\text{Rot}_{qp}(\vec{v})\end{aligned}

  • To rotate a vector v\vec{v} bu quaternion qq followed by its inverse quaternion q1q^{-1}:

Rotq1(Rotq(v))=v\begin{aligned}\text{Rot}_{q^{-1}}(\text{Rot}_q(\vec{v}))=\vec{v}\end{aligned}

Quaternion Interpolation:

  • A quaternion is a point on a 4D unit sphere

  • Interpolating quaternion means moving between two points on the 4D unit sphere:

  • A unit quaternion at each step-another point in the 4D unit sphere

  • Move with constant angular velocity along the greatest circle between the two points on the 4D unit sphere

  • Spherical Linear Interpolation(Slerp):

  • Want equal increment along are connecting two quaternion on the spherical surface:

  • Spherical linear interpolation(slerp) slerp(q1,q2,u)=sin((1u)θ)sinθq1+sin(uθ)sinθq2slerp(q_1,q_2,u)=\frac{\sin ((1-u)\theta)}{\sin \theta}q_1+\frac{\sin (u\theta)}{\sin \theta}q_2

where uu goes from 0 to 1 and θ=cos1(q1q2)\theta=\cos^{-1}(q_1·q_2)

  • Normalize to regain unit quaternion

Adavantages of Quaternion:

  • Good and smooth interpolation

  • No gimbal lock

  • Can be composed much more efficiently (requiring 8 multiplications and 4 divides)

  • But :1.Impossible to visualize 2.Unintuitive

  • Good for internal representation of rotation

Quaternion Class in Cocos2d-x# Lecture 3: Representation of transformation and rotation

Motion Specification:

  • Low level techniques (techniques that aid the animator in precisely specifying motion)

  • High level techniques (techniques used to describe general motion behavior)

  • Translate, scale and rotation are all below to low level.

  • Transformation can be used to change the position of objects; Set animations for objects and light source.

Technical Background:

  • Spaces and transformations:

  • Coordinate Space: left-handed, right-handed, local coordinates system, global coordinates system.

  • Viewing pipeline: Homogeneous coordinate, Transformation matrix, Matrix Concatenation

  • Orientation representation:

  • Rotation matrix

  • Fixed angle

  • Euler angle

  • Angle and Axis

  • Quaternion

I.Representation of Transformation

1.1.3-D Transformations

  1. Translate, scale, or rotate a point pp to pp':

p=p+Tp'=p+T p=Spp'=Sp pP=RppP'=Rp

2.How to treat these transformations in a unified way?

p=Mpp'=Mp

All in the homogeneous coordinate

  1. MM can be used for animation, viewing, or modeling.

1.2.Homogeneous Coordinate

1.In graphics, we use homogeneous coordinate for transformation

2.4×44\times 4 matrix can represent translation, scaling, and rotation and other transformations:

(xw,yw,zw)[x,y,z,w]\left(\frac{x}{w}, \frac{y}{w}, \frac{z}{w}\right)\Leftarrow [x, y, z, w]

3.Typically, when transforming a point in 3D space, we set w=1w=1

(x,y,z)[x,y,z,1](x, y, z)\Leftarrow [x, y, z, 1]

1.3.Translation

[xyz1]=[100tx010ty001tz0001][xyz1]\begin{bmatrix}x'\\ y'\\ z'\\ 1\\\end{bmatrix}=\begin{bmatrix}1&0&0&t_x\\ 0&1&0&t_y\\ 0&0&1&t_z\\ 0&0&0&1\\ \end{bmatrix} \begin{bmatrix}x\\ y\\ z\\ 1\\ \end{bmatrix}

1.4.Scaling

[xyz1]=[sx0000sy0000sz00001][xyz1]\begin{bmatrix}x'\\ y'\\ z'\\ 1\end{bmatrix}= \begin{bmatrix}s_x&0&0&0\\ 0&s_y&0&0\\ 0&0&s_z&0\\ 0&0&0&1\\\end{bmatrix}\begin{bmatrix}x\\ y\\ z\\ 1\\ \end{bmatrix}

1.5.Rotation

  • X axis:

Rx(θ)[xyz1]=[10000cos(θ)sin(θ)00sin(θ)cos(θ)00001][xyz1]R_x(\theta)\quad \begin{bmatrix}x'\\ y'\\ z'\\ 1\\\end{bmatrix}=\begin{bmatrix}1&0&0&0\\ 0&\cos(\theta)&-\sin(\theta)&0\\ 0&\sin(\theta)&\cos(\theta)&0\\ 0&0&0&1\\ \end{bmatrix}\begin{bmatrix}x\\ y\\ z\\ 1\\ \end{bmatrix}

  • Y axis:

Ry(θ)[xyz1]=[cos(θ)0sin(θ)00100sin(θ)0cos(θ)00001][xyz1]R_y(\theta)\quad \begin{bmatrix}x'\\y'\\z'\\1\\\end{bmatrix}=\begin{bmatrix}\cos(\theta)&0&\sin(\theta)&0\\0&1&0&0\\-\sin(\theta)&0&\cos(\theta)&0\\0&0&0&1\\\end{bmatrix}\begin{bmatrix}x\\y\\z\\1\\\end{bmatrix}

  • Z axis:

Rz(θ)[xyz1]=[cos(θ)sin(θ)00sin(θ)cos(θ)0000100001][xyz1]R_z(\theta)\quad \begin{bmatrix}x'\\y'\\z'\\1\\\end{bmatrix}=\begin{bmatrix}\cos(\theta)&-\sin(\theta)&0&0\\\sin(\theta)&\cos(\theta)&0&0\\0&0&1&0\\0&0&0&1\\\end{bmatrix}\begin{bmatrix}x\\y\\z\\1\\\end{bmatrix}

Properties:

  1. Trace is not connected with the rotation axis, we all have:tr(R)=1+2cos(θ)tr(R)=1 + 2\cos(\theta)

  2. All rotation matrices are orthogonal, multiplication of several rotation matrices are stll orthogonal:R1=RTR^{-1}=R^T

1.6.Transformations Concatenation

Transformations can be treated as a series of matrix multiplications:

P=M1M2M3...MnPP'=M_1M_2M_3...M_nP

M=M1M2M3...MnM=M_1M_2M_3...M_n

P=MP\Rightarrow P'=MP

PT=(M1M2M3...MnP)TP'^T=(M_1M_2M_3...M_nP)^T

MT=MnTMn1T...M1TM^T=M_n^TM_{n-1}^T...M_1^T

PT=PTMTP'^T=P^TM^T

1.7.Rigid body transformation

1.Only the position(translate transformation ) and direction(rotation transformation) change, it remains the geometry unchanged, that transformation is called rigid body transformation.

2.Feature: Keep the length and angle

X=T(t)R=(r00r01r02txr10r11r12tyr20r21r22tz0001)X=T(t)R=\begin{pmatrix}r_{00}&r_{01}&r_{02}&t_x\\r_{10}&r_{11}&r_{12}&t_y\\r_{20}&r_{21}&r_{22}&t_z\\0&0&0&1\\\end{pmatrix}

Calculate the inverse matrix:

X1=(T(t)R)1=R1T(t)1=RTT(t)X^{-1}=(T(t)R)^{-1}=R^{-1}T(t)^{-1}=R^TT(-t)

The calculation of the inverse matrix:

1.If a matrix is composed of one or several simple transformation, and the parameters are known, the inverse matrix can be multipled in order through the inverse parameters:

A example is :M=T(t)R(θ)M1R(θ)T(t)M=T(t)R(\theta)\Rightarrow M^{-1}R(-\theta)T(-t)

2.If the matrix is orthogonal, then M1=MTM^{-1}=M^T

3.If nothing is known: Accompanying matrix method, Cramer’s method, LU decomposition method, Gauss elimination method

  1. Cramer’s method and Accompanying method have less “if”, we should consider at first.

II. 3D Rotation representations

1.Rotation Matrix

  • Rows/columns of matrix must be orthogonal: Unit length and orthogonal

  • Numerical errors cause a nonorthogonal matrix when a series of rotations apply

  • How to interpolate between matrices?

  • Interpolating the components of two matrices doesn’t maintain the orthonomality.

  • The generated matrix is not a rotation matrix.

See the Problem:

[010100001][010100001]\begin{bmatrix}0&-1&0\\1&0&0\\0&0&1\\\end{bmatrix}\to\begin{bmatrix}0&1&0\\-1&0&0\\0&0&1\\\end{bmatrix}

The halfway matrix you get by linearly interpolating each entry is:

[000000001]\begin{bmatrix}0&0&0\\0&0&0\\0&0&1\\\end{bmatrix}

It is obviously that it is not a rotation matrix any more!

2.Fixed Angle

  • Ordered triple of rotations about fixed axes

  • Any triple can be used that doesn’t repeat an axis:

immediately, e.g., x-y-z is fine, so is x-y-x. But x-x-z is not.

Gimbal:

A gimbal is a mechanical device allowing the rotation of an object in multiple dimensions.

Gimbal Lock:

Gimbal lock occurs when two of the rotation aces align; e.g., x and y axes in the figure: Lost a degree of freedom; cannot rotate about x-axis.

For an orientation (0,90,0)(0, 90, 0),

  • A slight change in the first value (+/ϵ,90,0)(+/-\epsilon, 90, 0)

  • A slight change in the third value (0,90,+/ϵ)(0, 90, +/-\epsilon)

  • 90-degree y-axis rotation essentially makes x-axis align with z-axis -> gimbal lock: From (0,90,0)(0, 90, 0), the object can no longer be rotated about x-axis by a small change since orientation actually performed is (90,90+ϵ,90)(90, 90+\epsilon, 90)

Interpolation Problem in Fixed Angle:

  • The rotation from (0,90,0)(0, 90, 0) to (90,45,90)(90, 45, 90) is a 45-degree x-axis rotation: Impossible because the first 90-degree y-axis rotation

  • Directly interpolating between (0,90,0)(0, 90, 0) and (90,45,90)(90, 45, 90) produces a halfway orientation (45,67.5,45)(45, 67.5, 45): Desired halfway orientation is (90,22.5,90)(90, 22.5, 90)

Conclusion for Fixed Angle Representation:

  • Compact

  • Fairly intuitive

  • Easy to work

  • But not the most desirable representation to use because gimal lock problem.

3.Euler Angle

  • Euler Amgle is an efficient way to make the camera or object rotate toward an given direction.

  • Ordered triple of rotations about local axes.

  • As with fixed angles, any triple can be used that doesn’t immediately repeat an axis, e.g., x-y-z is fine, so is x-y-x. But x-x-z is not.

  • Euler angle ordering is equivalent to reverse ordering in fixed angles: The Euler anghle representation has exactly the same advantages and disadvantages as those of the fixed angle representation.

Calculation:

  • Euler angle ordering is equivalent to reverse ordering in fixed angles.

  • For a Euler angle representation in x-y-z ordering:

  • Y-axis rotation: around the y-axis of the local, rotated coordinate system:Ry(β)Rx(α)=Rx(α)Ry(β)Rx(α)Rx(α)=Rx(α)Ry(β)R_y'(\beta)R_x(\alpha)=R_x(\alpha)R_y(\beta)R_x(-\alpha)R_x(\alpha)=R_x(\alpha)R_y(\beta)

  • Z-axis rotation:around the twice-rotated frame

Rz(γ)Ry(β)Rx(α)=Rx(α)Ry(β)Rz(γ)Ry(β)Rx(α)Rx(α)Ry(β)=Rx(α)Ry(β)Rz(γ)\begin{aligned}&R_z''(\gamma)R_y'(\beta)R_x(\alpha)\\&=R_x(\alpha)R_y(\beta)R_z(\gamma)R_y(-\beta)R_x(-\alpha)R_x(\alpha)R_y(\beta)\\&=R_x(\alpha)R_y(\beta)R_z(\gamma)\end{aligned}

Gimbal lock in Euler Angle:

When p=π2p=\frac{\pi}{2}, the matrix only rely on the triangle (r+h)(r+h):

E(h,π2,r)=[cosrcoshsinrsinh0cosrsinh+sinrcoshsinrcosh+cosrsinh0sinrsinhcosrcosh010]=[cos(r+h)0sin(r+h)sin(r+h)0cos(r+h)010]\begin{aligned}E\left(h, \frac{\pi}{2}, r\right)&=\begin{bmatrix}\cos r\cos h-\sin r\sin h&0&\cos r \sin h + \sin r\cos h\\\sin r\cos h+\cos r\sin h&0&\sin r\sin h-\cos r\cos h\\0&1&0\\\end{bmatrix}\\&=\begin{bmatrix}\cos (r + h)& 0& \sin (r+h)\\\sin(r+h)&0&-\cos(r+h)\\0&1&0\\\end{bmatrix}\end{aligned}

4.Axis Angle

  • Euler’s rotation theorem:

  • One orientation can be derived from another by a single rotation about an axis

  • So ,we use an axis and a single angle to represent an orientation(with respect to the object’s initial orientation)

  • Interpolation can be implemented by interpolating axes of rotation and angles separately; but the transfomation concatenation cannot be done easily.

5.Quaternion

  • Similar to axis-angle representations: 4-tuple of real numbers:

q=(s,x,y,z)q=(s,x, y, z) or [s,v][s, \vec{v}], ss is a scalar; v\vec{v} is a vector

  • The quaternion for rotating an angle about an axis(an axis-angle rotation):

q=Rot(θ,(ax,ay,az))=[cos(θ2),sin(θ2)(ax,ay,az)]\begin{aligned}q&=\text{Rot}\left(\theta, (a_x,a_y,a_z)\right)\\&=\left[\cos\left(\frac{\theta}{2}\right), \sin\left(\frac{\theta}{2}\right)(a_x, a_y,a_z)\right]\end{aligned}

If aa is unit length, then qq will be also:

q=q02+q12+q22+q32=cos2θ2+ax2+sin2θ2+ay2sin2θ2+az2sin2θ2=cos2θ2+sin2θ2(ax2+ay2+az2)=cos2θ2+sin2θ2a2=cos2θ2+sin2θ2=1\begin{aligned}|q|&=\sqrt{q_0^2+q_1^2+q_2^2+q_3^2}\\&=\sqrt{\cos^2\frac{\theta}{2}+a_x^2+\sin^2\frac{\theta}{2}+a_y^2\sin^2\frac{\theta}{2}+a_z^2\sin^2\frac{\theta}{2}}\\&=\sqrt{\cos^2\frac{\theta}{2}+\sin^2\frac{\theta}{2}(a_x^2+a_y^2+a_z^2)}\\&=\sqrt{\cos^2\frac{\theta}{2}+\sin^2\frac{\theta}{2}|\vec{a}|^2}=\sqrt{\cos^2\frac{\theta}{2}+\sin^2\frac{\theta}{2}}\\&=1\end{aligned}

  • Rotating some angle around an axis is the same as rotating the negative angle around the negated axis

q=Rotθ,(x,y,z)=[cos(θ2),sin(θ2)((x,y,z))]=[cos(θ2),sin(θ2)(x,y,z)]=Rotθ,(x,y,z)=q\begin{aligned}-q&=\text{Rot}_{-\theta, -(x, y,z)}\\&=\left[\cos\left(-\frac{\theta}{2}\right),\sin \left(-\frac{\theta}{2}\right)(-(x, y, z))\right]\\&=\left[\cos\left(\frac{\theta}{2}\right),\sin\left(\frac{\theta}{2}\right)(x,y,z)\right]\\&=\text{Rot}_{\theta,(x,y,z)}\\&=q\end{aligned}

Quaternion Math

Definition for Addition:

[s1,v1]+[s2,v2]=[s1+s2,v1+v2][s_1,\vec{v_1}]+[s_2,\vec{v_2}]=[s_1+s_2,\vec{v_1}+\vec{v_2}]

Definition for Multiplication:

[s1,v1][s2,v2]=[s1s2v1v2,s1v2+s2v1+v1×v2][s_1,\vec{v_1}][s_2,\vec{v_2}]=[s_1s_2-\vec{v_1}·\vec{v_2},s_1\vec{v_2}+s_2\vec{v_1}+\vec{v_1}\times \vec{v_2}]

Multiplication is associative but not commutative:

q1(q2q3)=(q1q2)q3q_1(q_2q_3)=(q_1q_2)q_3

q1q2q2q1q_1q_2\neq q_2q_1

Multiplicative identity:[1, 0, 0, 0]

q[1,0,0,0]=qq[1, 0, 0, 0]=q

Inverse:

q1=[s,v]q2q^{-1}=\frac{[s,-v]}{\|q\|^2}

qq1=[1,0,0,0]qq^{-1}=[1,0,0,0]

Normalization for unit quaternion:

q^=qq\hat{q}=\frac{q}{\|q\|}

q=s2+x2+y2+z2\|q\|=\sqrt{s^2+x^2+y^2+z^2}

As for Quaternion:

q=sinθuq+cosθ=eθuq\vec{q}=\sin \theta \vec{u_q}+\cos \theta = e^{\theta \vec{u_q}}

Logarithm:

log(q)=log(eθuq)=θuq\log (\vec{q})=\log (e^{\theta \vec{u_q}})=\theta \vec{u_q}

Exponent:

qt=(sinθuq,cosθ)t=eθtuq=sin(θt)uq+cos(θt)\vec{q}^t=(\sin \theta \vec{u_q},\cos \theta)^t=e^{\theta t \vec{u_q}}=\sin (\theta t)\vec{u_q}+\cos(\theta t)

The transformation from rotation matrix to quaternion: for unit quaternion q=(qw,qx,qy,qz)q=(q_w,q_x,q_y,q_z),we have:

Mq=(12(qy2+qz2)2(qxqyqwqz)2(qxqz+qwqy)02(qxqy+qwqz)12(qx2+qz2)2(qyqzqwqx)02(qxqzqwqy)2(qyqz+qwqx)12(qx2+qy2)00001)M^q=\begin{pmatrix}1-2(q_y^2+q_z^2)&2(q_xq_y-q_wq_z)&2(q_xq_z+q_wq_y)&0\\ 2(q_xq_y+q_wq_z)&1-2(q_x^2+q_z^2)&2(q_yq_z-q_wq_x)&0\\2(q_xq_z-q_wq_y)&2(q_yq_z+q_wq_x)&1-2(q_x^2+q_y^2)&0\\0&0&0&1\\\end{pmatrix}

Noticed that:

tr(Mq)=42s(qx2+qy2+qz2)=4(1qx2+qy2+qz2qx2+qy2+qz2+qw2)=4qw2qx2+qy2+qz2+qw2=4qw2n(q)\begin{aligned}tr(M^q)&=4-2s(q_x^2+q_y^2+q_z^2)\\&=4\left(1-\frac{q_x^2+q_y^2+q_z^2}{q_x^2+q_y^2+q_z^2+q_w^2}\right)\\&=\frac{4q_w^2}{q_x^2+q_y^2+q_z^2+q_w^2}\\&=\frac{4q_w^2}{n(\vec{q})}\end{aligned}

Then the unit quaternion is:

{qw=12tr(Mq)qx=m21qm12q4qwqy=m02qm20q4qwqz=m10qm01q4qw\begin{cases}q_w=\frac{1}{2}\sqrt{tr(M^q)}\\q_x=\frac{m_{21}^q-m_{12}^q}{4q_w}\\q_y=\frac{m_{02}^q-m_{20}^q}{4q_w}\\q_z=\frac{m_{10}^q-m_{01}^q}{4q_w}\end{cases}

Rotating Vectors Using Quaternion

  • A point in space v\vec{v}, is represented as [0,v][0, \vec{v}]

  • To rotate a vector v\vec{v} using quaternion qq

  • Represent the vector as v=[0,v]v=[0, \vec{v}]

  • Represent the rotation as a quaternion qq

  • Using quaternion multiplication v=Rotq(v)=qvq1\vec{v}'=\text{Rot}_q(\vec{v})=q\vec{v}q^{-1}

  • Note that the result v\vec{v}' always has zero scalar value

  • Rotating a vector v\vec{v} by first quaternion pp followed by a quaternion qq is like rotation using qpqp:

Rotq(Rotp(v))=Rotq(pvp1)=qpvp1q1=(qp)v(qp)1=Rotqp(v)\begin{aligned}\text{Rot}_q(\text{Rot}_p(\vec{v}))&=\text{Rot}_q(p\vec{v}p^{-1})\\&=qp\vec{v}p^{-1}q^{-1}\\&=(qp)\vec{v}(qp)^{-1}\\&=\text{Rot}_{qp}(\vec{v})\end{aligned}

  • To rotate a vector v\vec{v} bu quaternion qq followed by its inverse quaternion q1q^{-1}:

Rotq1(Rotq(v))=v\begin{aligned}\text{Rot}_{q^{-1}}(\text{Rot}_q(\vec{v}))=\vec{v}\end{aligned}

Quaternion Interpolation:

  • A quaternion is a point on a 4D unit sphere

  • Interpolating quaternion means moving between two points on the 4D unit sphere:

  • A unit quaternion at each step-another point in the 4D unit sphere

  • Move with constant angular velocity along the greatest circle between the two points on the 4D unit sphere

  • Spherical Linear Interpolation(Slerp):

  • Want equal increment along are connecting two quaternion on the spherical surface:

  • Spherical linear interpolation(slerp) slerp(q1,q2,u)=sin((1u)θ)sinθq1+sin(uθ)sinθq2slerp(q_1,q_2,u)=\frac{\sin ((1-u)\theta)}{\sin \theta}q_1+\frac{\sin (u\theta)}{\sin \theta}q_2

where uu goes from 0 to 1 and θ=cos1(q1q2)\theta=\cos^{-1}(q_1·q_2)

  • Normalize to regain unit quaternion

Adavantages of Quaternion:

  • Good and smooth interpolation

  • No gimbal lock

  • Can be composed much more efficiently (requiring 8 multiplications and 4 divides)

  • But :1.Impossible to visualize 2.Unintuitive

  • Good for internal representation of rotation

Quaternion Class in Cocos2d-x

此博客中的热门博文

Numberical Analysis --- Interpolation & Polynomial Approximation

Compuer Animation(Postgraduate Course): Lecture 4:Keyframe interpolation and velocity control