(个人笔记,由于刚开始学习再加上英语不太好,所以有的地理解的可能不太对,望指正)
Chapter 6 Transformation Matrices
6.1 2D Linear Transformation
6.1.1 Scaling
s c a l e ( s x , s y ) = [ s x 0 0 s y ] scale(s_x, s_y) = \begin{bmatrix} s_x & 0 \\ 0 & s_y \end{bmatrix} scale(sx,sy)=[sx00sy]
6.1.2 Shearing
s h e a r X ( s ) = [ 1 s 0 1 ] s h e a r Y ( s ) = [ 1 0 s 1 ] shearX(s) = \begin{bmatrix} 1 & s \\ 0 & 1 \end{bmatrix} \\ shearY(s) = \begin{bmatrix} 1 & 0 \\ s & 1 \end{bmatrix} shearX(s)=[10s1]shearY(s)=[1s01]
shear操作可以看作是只沿一个方向进行旋转
6.1.3 Rotation
r o t a t e ( ϕ ) = [ c o s ϕ − s i n ϕ s i n ϕ c o s ϕ ] rotate(\phi)=\begin{bmatrix} cos\phi & -sin\phi \\ sin\phi & cos\phi \end{bmatrix} rotate(ϕ)=[cosϕsinϕ−sinϕcosϕ]
计算过程:
r = x a 2 + y a 2 x a = r c o s α y a = r s i n α x b = r c o s ( α + ϕ ) = r c o s α c o s ϕ − r s i n α s i n ϕ y b = r s i n ( α + ϕ ) = r s i n α c o s ϕ + r c o s α s i n ϕ r = \sqrt{x_a^2 + y_a^2} \\ x_a = r cos\alpha \\ y_a = r sin \alpha \\ x_b = r cos(\alpha + \phi) = rcos\alpha cos\phi - rsin\alpha sin\phi \\ y_b = r sin(\alpha + \phi) = rsin\alpha cos\phi + rcos\alpha sin\phi r=xa2+ya2xa=rcosαya=rsinαxb=rcos(α+ϕ)=rcosαcosϕ−rsinαsinϕyb=rsin(α+ϕ)=rsinαcosϕ+rcosαsinϕ
如图所示:
旋转矩阵是正交矩阵
6.1.4 Reflection
r e f l e c t Y = [ − 1 0 0 1 ] r e f l e c t X = [ 1 0 0 − 1 ] reflectY = \begin{bmatrix} -1 & 0 \\ 0 & 1 \end{bmatrix} \\ reflectX = \begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix} reflectY=[−1001]reflectX=[100−1]
6.1.5 Composition and Decomposition of Transformations
6.1.6 Decoposition of Transformations
Symmetric Eigenvalue Decomposition
对于对称矩阵来说,可以进行以下分解过程:
A = R S R T A = RSR^{T} A=RSRT
R表示旋转,S表示缩放。
Singular Value Decomposition
对于非对称矩阵来说,可以使用奇异值分解。
A = U S V T A = USV^{T} A=USVT
对于奇异值分解来说UV可能不表示旋转,也可能表示反射,可以通过行列式的值判断:+1表示旋转,-1表示反射。
Paeth Decomposition of Rotations
使用shear操作代替旋转操作
[ c o s ϕ − s i n ϕ s i n ϕ c o s ϕ ] = [ 1 c o s ϕ − 1 s i n ϕ 0 1 ] [ 1 0 s i n ϕ 1 ] [ 1 c o s ϕ − 1 s i n ϕ 0 1 ] \begin{bmatrix} cos\phi & -sin\phi \\ sin\phi & cos\phi \end{bmatrix}=\begin{bmatrix} 1 & \frac{cos\phi-1}{sin\phi} \\ 0 & 1 \end{bmatrix}\begin{bmatrix} 1 & 0 \\ sin\phi & 1 \end{bmatrix}\begin{bmatrix} 1 & \frac{cos\phi-1}{sin\phi} \\ 0 & 1 \end{bmatrix} [cosϕsinϕ−sinϕcosϕ]=[10sinϕcosϕ−11][1sinϕ01][10sinϕcosϕ−11]
该转化对于raster rotation非常有效,因为对于raster operation来说,shear操作是很高效的。
6.2 3D Linear Transformations
缩放
s c a l e ( s x , s y , s z ) = [ s x 0 0 0 s y 0 0 0 s z ] scale(s_x, s_y, s_z) = \begin{bmatrix} s_x & 0 & 0 \\ 0 & s_y & 0 \\ 0 & 0 & s_z \end{bmatrix} scale(sx,sy,sz)=⎣⎡sx000sy000sz⎦⎤
旋转
r o t a t e Z ( ϕ ) = [ c o s ϕ − s i n ϕ 0 s i n ϕ c o s ϕ 0 0 0 1 ] r o t a t e X ( ϕ ) = [ 1 0 c o s ϕ − s i n ϕ 0 s i n ϕ c o s ϕ 0 ] r o t a t e Y ( ϕ ) = [ c o s ϕ 0 s i n ϕ 0 1 0 − s i n ϕ 0 c o s ϕ ] rotateZ(\phi)=\begin{bmatrix} cos\phi & -sin\phi & 0 \\ sin\phi & cos\phi & 0 \\ 0 & 0 & 1 \end{bmatrix} \\ rotateX(\phi)=\begin{bmatrix} 1 & 0 & \\ cos\phi & -sin\phi & 0 \\ sin\phi & cos\phi & 0 \end{bmatrix} \\ rotateY(\phi)=\begin{bmatrix} cos\phi & 0 & sin\phi \\ 0 & 1 & 0 \\ -sin\phi & 0 & cos\phi \end{bmatrix} rotateZ(ϕ)=⎣⎡cosϕsinϕ0−sinϕcosϕ0001⎦⎤rotateX(ϕ)=⎣⎡1cosϕsinϕ0−sinϕcosϕ00⎦⎤rotateY(ϕ)=⎣⎡cosϕ0−sinϕ010sinϕ0cosϕ⎦⎤
shear
s h e a r X ( d y , d z ) = [ 1 d y d z 0 1 0 0 0 1 ] shearX(d_y, d_z) = \begin{bmatrix} 1 & d_y & d_z \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{bmatrix} shearX(dy,dz)=⎣⎡100dy10dz01⎦⎤
6.2.1 Arbitrary 3D Rotation
绕任意向量旋转,可以首先将以该向量建立一个基,从该坐标系转换到标准坐标系,利用绕坐标轴旋转完成之后,再利用旋转矩阵的逆矩阵转回来:
[ x u x v x w y u y v y w z u z v z w ] [ c o s ϕ − s i n ϕ 0 s i n ϕ c o s ϕ 0 0 0 1 ] [ x u y u z u x v y v z v x w y w z w ] \begin{bmatrix} x_u & x_v & x_w \\ y_u & y_v & y_w \\ z_u & z_v & z_w \end{bmatrix}\begin{bmatrix} cos\phi & -sin\phi & 0 \\ sin\phi & cos\phi & 0 \\ 0 & 0 & 1 \end{bmatrix}\begin{bmatrix} x_u & y_u & z_u \\ x_v & y_v & z_v \\ x_w & y_w & z_w \end{bmatrix} ⎣⎡xuyuzuxvyvzvxwywzw⎦⎤⎣⎡cosϕsinϕ0−sinϕcosϕ0001⎦⎤⎣⎡xuxvxwyuyvywzuzvzw⎦⎤
If we have a rotation matrix and we wish to have the rotation in axis-angle form, we can compute the one real eigenvalue (which will be λ = 1), and the corresponding eigenvector is the axis of rotation. This is the one axis that is not changed by the rotation.
6.2.2 Transforming Normal Vectors
法线向量的变换不能直接用顶点转换矩阵M进行转换
设法线为n,切线为t,法线转化矩阵为N。
首先有
n T t = 0 n^{T}t = 0 nTt=0
那么经过转换之后法线和切线也仍然垂直,所以有
n N T t M = 0 ( 6 − 1 ) n^{T}_Nt_M = 0 \ \ \ (6-1) nNTtM=0 (6−1)
推导
n T t = n T I t = n T M − 1 M t = 0 ( n T M − 1 ) t M = 0 n^Tt=n^TIt=n^TM^{-1}Mt=0 \\ (n^TM^{-1})t_M=0\\ nTt=nTIt=nTM−1Mt=0(nTM−1)tM=0
根据6-1,可得
n N T = n T M − 1 n N = ( n T M − 1 ) T = ( M − 1 ) T n n^{T}_N = n^TM^{-1} \\ n_N = (n^TM^{-1})^{T} = (M^{-1})^{T}n nNT=nTM−1nN=(nTM−1)T=(M−1)Tn
所以法线转换矩阵为 ( M − 1 ) T (M^{-1})^{T} (M−1)T
6.3 Translation and Affine Transformations
要移动一个物体,不能仅通过一个同维度的矩阵乘法实现。一种处理思路是使用矩阵表示旋转缩放等操作,一个向量表示移动,但是这种方法不够简洁,可以使用一个矩阵结合这两种操作,对于2维空间来说,可以为
[ m 11 m 12 x t m 21 m 22 y t 0 0 1 ] [ x ′ y ′ 1 ] = [ m 11 m 12 x t m 21 m 22 y t 0 0 1 ] [ x y 1 ] = [ m 11 x + m 12 y + x t m 21 x + m 22 y + y t 1 ] \begin{bmatrix} m_{11} & m_{12} & x_t \\ m_{21} & m_{22} & y_t \\ 0 & 0 & 1 \end{bmatrix} \\ \begin{bmatrix} x' \\ y' \\ 1 \end{bmatrix}= \begin{bmatrix} m_{11} & m_{12} & x_t \\ m_{21} & m_{22} & y_t \\ 0 & 0 & 1 \end{bmatrix}\begin{bmatrix} x \\ y \\ 1 \end{bmatrix}=\begin{bmatrix} m_{11}x + m_{12}y + x_t \\ m_{21}x + m_{22}y + y_t \\ 1 \end{bmatrix} ⎣⎡m11m210m12m220xtyt1⎦⎤⎣⎡x′y′1⎦⎤=⎣⎡m11m210m12m220xtyt1⎦⎤⎣⎡xy1⎦⎤=⎣⎡m11x+m12y+xtm21x+m22y+yt1⎦⎤
这种变换称为仿射变换(affine transformation),这种添加一个额外的维度来实现放射变换的方法称为齐次坐标(homogeneous coordinates)。如果需要表示向量,则可以把第三个维度设为0,那么变换过程就不收到位移的影响了,也就是说第三个维度为1时,表示一个位置;为0时,表示一个向量。
6.4 Inverse of Transformation Matrices
一个转换的逆是相反方向的转换,例如有变换 M = M 1 M 2 . . . M n M=M_1M_2...M_n M=M1M2...Mn,那么它的逆是 M − 1 = M n − 1 . . . M 2 − 1 M 1 − 1 M^{-1}=M_n^{-1}...M_2^{-1}M_1^{-1} M−1=Mn−1...M2−1M1−1。
除了缩放、旋转等表较容易获得逆矩阵的操作,对于一般矩阵来说也可以使用SVD分解来对其进行求逆矩阵的运算:
M = R 1 s c a l e ( σ 1 , σ 2 , σ 3 ) R 2 M − 1 = R 2 T s c a l e ( 1 / σ 1 , 1 / σ 2 , 1 / σ 3 ) R 1 T M = R_1scale(\sigma_1, \sigma_2, \sigma_3)R_2 \\ M^{-1} = R_2^Tscale(1/\sigma_1, 1/\sigma_2, 1/\sigma_3)R_1^T M=R1scale(σ1,σ2,σ3)R2M−1=R2Tscale(1/σ1,1/σ2,1/σ3)R1T
6.5 Coordinate Transformations
一般来说,一个坐标系统由一个顶点和三个正交基组成,在一个坐标系中,原点 p p p和基 { u ⃗ , v ⃗ , w ⃗ } \{\vec{u}, \vec{v}, \vec{w}\} {u,v,w},那么坐标(u, v, w)表示的坐标为
p + u u ⃗ + v v ⃗ + w w ⃗ p + u\vec{u} + v\vec{v} + w\vec{w} p+uu+vv+ww
不同嵌套坐标系之间的转换,以2维空间为例,
p = ( x p , y p ) = o + x p x ⃗ + y p y ⃗ p = ( u p , v p ) = e + u p u ⃗ + v p v ⃗ p = (x_p, y_p) = o + x_p\vec{x} + y_p\vec{y} \\ p = (u_p, v_p) = e + u_p\vec{u} + v_p\vec{v} p=(xp,yp)=o+xpx+ypyp=(up,vp)=e+upu+vpv
那么两个坐标系的转换关系为
[ x p y p 1 ] = [ 1 0 x e 0 1 y e 0 0 1 ] [ x u x v 0 y u y v 0 0 0 1 ] [ u p v p 1 ] = [ x u x v x e y u y v y e 0 0 1 ] [ u p v p 1 ] \begin{bmatrix} x_p \\ y_p \\ 1 \end{bmatrix}=\begin{bmatrix} 1 & 0 & x_e \\ 0 & 1 & y_e \\ 0 & 0 & 1 \end{bmatrix}\begin{bmatrix} x_u & x_v & 0 \\ y_u & y_v & 0 \\ 0 & 0 & 1 \end{bmatrix}\begin{bmatrix} u_p \\ v_p \\ 1 \end{bmatrix}=\begin{bmatrix} x_u & x_v & x_e \\ y_u & y_v & y_e \\ 0 & 0 & 1 \end{bmatrix}\begin{bmatrix} u_p \\ v_p \\ 1 \end{bmatrix} ⎣⎡xpyp1⎦⎤=⎣⎡100010xeye1⎦⎤⎣⎡xuyu0xvyv0001⎦⎤⎣⎡upvp1⎦⎤=⎣⎡xuyu0xvyv0xeye1⎦⎤⎣⎡upvp1⎦⎤
可以简化为
p x y = [ u ⃗ v ⃗ e ⃗ 0 0 1 ] p u v p_{xy} = \begin{bmatrix} \vec{u} & \vec{v} & \vec{e} \\ 0 & 0 & 1 \end{bmatrix}p_{uv} pxy=[u0v0e1]puv
从另一个方向来看
[ u p v p 1 ] = [ x u y u 0 x v y v 0 0 0 1 ] [ 1 0 − x e 0 1 − y e 0 0 1 ] [ x p y p 1 ] \begin{bmatrix} u_p \\ v_p \\ 1 \end{bmatrix}=\begin{bmatrix} x_u & y_u & 0 \\ x_v & y_v & 0 \\ 0 & 0 & 1 \end{bmatrix}\begin{bmatrix} 1 & 0 & -x_e \\ 0 & 1 & -y_e \\ 0 & 0 & 1 \end{bmatrix}\begin{bmatrix} x_p \\ y_p \\ 1 \end{bmatrix} ⎣⎡upvp1⎦⎤=⎣⎡xuxv0yuyv0001⎦⎤⎣⎡100010−xe−ye1⎦⎤⎣⎡xpyp1⎦⎤
可以简化为
p u v = [ u ⃗ v ⃗ e ⃗ 0 0 1 ] − 1 p x y p_{uv} = \begin{bmatrix} \vec{u} & \vec{v} & \vec{e} \\ 0 & 0 & 1 \end{bmatrix}^{-1}p_{xy} puv=[u0v0e1]−1pxy
由于求逆操作的存在,上面的表达式不是很直观,所以也可以表示为
p u v = [ x u v y u v o u v 0 0 1 ] − 1 p x y p_{uv} = \begin{bmatrix} x_{uv} & y_{uv} & o_{uv} \\ 0 & 0 & 1 \end{bmatrix}^{-1}p_{xy} puv=[xuv0yuv0ouv1]−1pxy