【GAMES101学习笔记】03 - 基础矩阵变换

1. 二维线性变换

对向量 [ x y ] \begin{bmatrix} x \\ y \end{bmatrix} [xy] 进行线性变换

[ a 11 a 12 a 21 a 22 ] [ x y ] = [ a 11 x + a 12 y a 21 x + a 22 y ] = [ x ′ y ′ ] \begin{bmatrix} a_{11} &a_{12} \\ a_{21} &a_{22} \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix} = \begin{bmatrix} a_{11}x + a_{12}y \\ a_{21}x + a_{22}y \end{bmatrix} = \begin{bmatrix} x' \\ y' \end{bmatrix} [a11a21a12a22][xy]=[a11x+a12ya21x+a22y]=[xy]

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]

除了 [ 0 0 ] \begin{bmatrix} 0 \\ 0 \end{bmatrix} [00] 保持不变,其他所有点变为 [ s x x s y y ] \begin{bmatrix} s_x x \\ s_y y \end{bmatrix} [sxxsyy]

第一种情况:x 方向和 y 方向均缩小0.5倍

screenShot.png

[ x ′ y ′ ] = [ 0.5 0 0 0.5 ] [ x y ] \begin{bmatrix} x' \\ y' \end{bmatrix} = \begin{bmatrix} 0.5 & 0 \\ 0 & 0.5 \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix} [xy]=[0.5000.5][xy]

第二种情况:x 方向缩小 0.5 倍, y 方向不变

screenShot.png

[ x ′ y ′ ] = [ 0.5 0 0 1 ] [ x y ] \begin{bmatrix} x' \\ y' \end{bmatrix} = \begin{bmatrix} 0.5 & 0 \\ 0 & 1 \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix} [xy]=[0.5001][xy]

1.2 反射(Reflection)

进行一个镜像的变换

x ′ = − x y ′ = y \begin{aligned} &x' = -x \\ &y' = y \end{aligned} x=xy=y

沿着 y 轴进行翻转操作

screenShot.png

[ x ′ y ′ ] = [ − 1 0 0 1 ] [ x y ] \begin{bmatrix} x' \\ y' \end{bmatrix} = \begin{bmatrix} -1 & 0 \\ 0 & 1 \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix} [xy]=[1001][xy]

1.3 切变(Shearing)

把物体的其中一边固定,然后拉动对应的另外一边

s h e a r − x ( s ) = [ 1 s 0 1 ] s h e a r − y ( s ) = [ 1 0 s 1 ] \begin{aligned} shear-x(s) = \begin{bmatrix} 1 & s \\ 0 & 1 \end{bmatrix} \\[3ex] shear-y(s) = \begin{bmatrix} 1 & 0 \\ s & 1 \end{bmatrix} \end{aligned} shearx(s)=[10s1]sheary(s)=[1s01]

沿着 x 轴方向拉动 a 距离

screenShot.png

[ x ′ y ′ ] = [ 1 a 0 1 ] [ x y ] \begin{bmatrix} x' \\ y' \end{bmatrix} = \begin{bmatrix} 1 & a \\ 0 & 1 \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix} [xy]=[10a1][xy]

NOTE :

  • 水平方向: x ′ = x + a y x' = x + ay x=x+ay
    • y = 0 的部分不变化
    • y = 1 的部分右移 a 距离
       
  • 竖直方向: y ′ = y y' = y y=y
    • 不变化

1.4 旋转

默认规定

  • 绕原点 ( 0 , 0 ) (0,0) (0,0) 进行旋转;
  • 旋转方向为逆时针。

screenShot.png

记为;
r o t a t e ( ϕ ) = [ cos ⁡ ϕ − sin ⁡ ϕ sin ⁡ ϕ cos ⁡ ϕ ] rotate(\phi) = \begin{bmatrix} \cos\phi & -\sin\phi \\ \sin\phi & \cos\phi \end{bmatrix} rotate(ϕ)=[cosϕsinϕsinϕcosϕ]

可知:

{ x ′ = x cos ⁡ ϕ − y sin ⁡ ϕ y ′ = y cos ⁡ ϕ + x sin ⁡ ϕ \begin{cases} x' = x\cos\phi - y\sin\phi \\[1.5ex] y' = y\cos\phi + x\sin\phi \end{cases} x=xcosϕysinϕy=ycosϕ+xsinϕ

即:

[ x ′ y ′ ] = [ cos ⁡ ϕ − sin ⁡ ϕ sin ⁡ ϕ cos ⁡ ϕ ] [ x y ] \begin{bmatrix} x' \\ y' \end{bmatrix} = \begin{bmatrix} \begin{array}{c:c} \cos\phi & -\sin\phi \\ \sin\phi & \cos\phi \end{array} \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix} [xy]=[cosϕsinϕsinϕcosϕ][xy]

绕原点逆时针旋转 45°

screenShot.png

[ 2 2 − 2 2 2 2 2 2 ] \begin{bmatrix} \cfrac{\sqrt{2}}{2} & -\cfrac{\sqrt{2}}{2} \\[3ex] \cfrac{\sqrt{2}}{2} & \cfrac{\sqrt{2}}{2} \end{bmatrix} 22 22 22 22

1.5 位移

screenShot.png

{ x ′ = x + t x y ′ = y + t y \begin{cases} x' = x + t_x \\ y' = y + t_y \end{cases} {x=x+txy=y+ty

但是当存在位移变换时,便无法仅靠一个二维矩阵的变换来进行表示:

[ x ′ y ′ ] = [ a b c d ] [ x y ] + [ t x t y ] \begin{bmatrix} x' \\ y' \end{bmatrix} = \begin{bmatrix} a & b \\ c & d \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix} + \begin{bmatrix} t_x \\ t_y \end{bmatrix} [xy]=[acbd][xy]+[txty]

1.5.1 齐次坐标

增加一个维度,提供更多的信息,将二维向量区分为“点”和“位移向量”:

  • 2 D − p o i n t = ( x , y , 1 ) T 2D-point = (x,y,1)^T 2Dpoint=(x,y,1)T
  • 2 D − v e c t o r = ( x , y , 0 ) T 2D-vector = (x,y,0)^T 2Dvector=(x,y,0)T

使用的 1 和 0 是有实际意义的:

  • vector + vector = vector(三角形法则:向量的叠加)
  • point - point = vector(生成从第二个点指向第一个点的向量)
  • point + vector = point(点进行位移)
  • point + point = 这2个点的中点

可以通过三维空间中的一个点集去表示二维空间中的一个点

  • 这个点集即是“视点”出发,穿过二维空间中“可视平面”上的该点的射线
  • 射线点集 [ x y w ] \begin{bmatrix} x \\ y \\ w \end{bmatrix} xyw 表示“可视平面”上的点 [ x / w y / w 1 ] \begin{bmatrix} x/w \\ y/w \\ 1 \end{bmatrix} x/wy/w1 w ≠ 0 w \ne 0 w=0

此时可以使用矩阵变换去表示一个位移变换

  • 当“点”进行位移时,会发生相应的变化:

[ x ′ y ′ w ′ ] = [ 1 0 t x 0 1 t y 0 0 1 ] ⋅ [ x y 1 ] = [ x + t x y + t y 1 ] \begin{bmatrix} x' \\ y' \\ w' \end{bmatrix} = \begin{bmatrix} 1 & 0 & t_x \\ 0 & 1 & t_y \\ 0 & 0 & 1 \end{bmatrix} \cdot \begin{bmatrix} x \\ y \\ 1 \end{bmatrix} = \begin{bmatrix} x + t_x \\ y + t_y \\ 1 \end{bmatrix} xyw=100010txty1xy1=x+txy+ty1

  • 当“位移向量”进行位移时,不会发生变化:

[ x ′ y ′ w ′ ] = [ 1 0 t x 0 1 t y 0 0 1 ] ⋅ [ x y 0 ] = [ x y 0 ] \begin{bmatrix} x' \\ y' \\ w' \end{bmatrix} = \begin{bmatrix} 1 & 0 & t_x \\ 0 & 1 & t_y \\ 0 & 0 & 1 \end{bmatrix} \cdot \begin{bmatrix} x \\ y \\ 0 \end{bmatrix} = \begin{bmatrix} x \\ y \\ 0 \end{bmatrix} xyw=100010txty1xy0=xy0

1.5.2 仿射变换

“线性变换” + “位移变换”

[ x ′ y ′ ] = [ a b c d ] ⋅ [ x y ] + [ t x t y ] \begin{bmatrix} x' \\ y' \end{bmatrix} = \begin{bmatrix} a & b \\ c & d \end{bmatrix} \cdot \begin{bmatrix} x \\ y \end{bmatrix} + \begin{bmatrix} t_x \\ t_y \end{bmatrix} [xy]=[acbd][xy]+[txty]

使用齐次坐标表示仿射变换:

[ x ′ y ′ 1 ] = [ a b t x c d t y 0 0 1 ] ⋅ [ x y 1 ] \begin{bmatrix} x' \\ y' \\ 1 \end{bmatrix} = \begin{bmatrix} a & b & t_x \\ c & d & t_y \\ 0 & 0 & 1 \end{bmatrix} \cdot \begin{bmatrix} x \\ y \\ 1 \end{bmatrix} xy1=ac0bd0txty1xy1

NOTE

  • 仅在仿射变换下最后一排是 ( 0 , 0 , 1 ) (0,0,1) (0,0,1)

1.6 变换的组合

  • 一个复杂变换可以拆分成若干个基础变换
  • 变换顺序需要注意 —— 矩阵的乘法不满足交换律

R 45 ⋅ T ( 1 , 0 ) ≠ T ( 1 , 0 ) ⋅ R 45 R_{45} \cdot T_{(1,0)} \ne T_{(1,0)} \cdot R_{45} R45T(1,0)=T(1,0)R45

screenShot.png

[ x ′ y ′ 1 ] = R 45 ⋅ T ( 1 , 0 ) ⋅ [ x y 1 ] \begin{bmatrix} x' \\ y' \\ 1 \end{bmatrix} = R_{45} \cdot T_{(1,0)} \cdot \begin{bmatrix} x \\ y \\ 1 \end{bmatrix} xy1=R45T(1,0)xy1

screenShot.png

[ x ′ y ′ 1 ] = T ( 1 , 0 ) ⋅ R 45 ⋅ [ x y 1 ] \begin{bmatrix} x' \\ y' \\ 1 \end{bmatrix} = T_{(1,0)} \cdot R_{45} \cdot \begin{bmatrix} x \\ y \\ 1 \end{bmatrix} xy1=T(1,0)R45xy1

组合应用若干变换 A 1 , A 2 , A 3 , … A_1,A_2,A_3,\dots A1,A2,A3,

A n ( … A 2 ( A 1 ( x ) ) ) = A n … A 2 ⋅ A 1 ⋅ [ x y 1 ] A_n(\dots A_2(A_1(x))) = A_n \dots A_2 \cdot A_1 \cdot \begin{bmatrix} x \\ y \\ 1 \end{bmatrix} An(A2(A1(x)))=AnA2A1xy1

NOTE
矩阵有结合律,可以用一个矩阵表示这一系列的复合变换

2. 三维线性变换

2.1 仿射变换

  • 3 D − p o i n t = ( x , y , z , 1 ) T 3D-point = (x,y,z,1)^T 3Dpoint=(x,y,z,1)T

  • 3 D − v e c t o r = ( x , y , z , 0 ) T 3D-vector = (x,y,z,0)^T 3Dvector=(x,y,z,0)T

  • 射线点集 [ x y z w ] \begin{bmatrix} x \\ y \\ z \\ w \end{bmatrix} xyzw 表示“可视三维平面”上的点 [ x / w y / w z / w 1 ] \begin{bmatrix} x/w \\ y/w \\ z/w \\ 1 \end{bmatrix} x/wy/wz/w1 w ≠ 0 w \neq 0 w=0

使用四维矩阵表示三维空间中的仿射变换

[ x ′ y ′ z ′ 1 ] = [ a b c t x d e f t y g h i t z 0 0 0 1 ] ⋅ [ x y z 1 ] \begin{bmatrix} x' \\ y' \\ z' \\ 1 \end{bmatrix} = \begin{bmatrix} a & b & c & t_x \\ d & e & f & t_y \\ g & h & i & t_z \\ 0 & 0 & 0 & 1 \end{bmatrix} \cdot \begin{bmatrix} x \\ y \\ z \\ 1 \end{bmatrix} xyz1=adg0beh0cfi0txtytz1xyz1

2.2 缩放变换

S c a l e ( s x , s y , s z ) = [ s x 0 0 0 0 s y 0 0 0 0 s z 0 0 0 0 1 ] Scale(s_x,s_y,s_z) = \begin{bmatrix} s_x & 0 & 0 & 0 \\ 0 & s_y & 0 & 0 \\ 0 & 0 & s_z & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} Scale(sx,sy,sz)=sx0000sy0000sz00001

2.3 位移变换

T ( t x , t y , t z ) = [ 1 0 0 t x 0 1 0 t y 0 0 1 t z 0 0 0 1 ] T(t_x,t_y,t_z) = \begin{bmatrix} 1 & 0 & 0 & t_x \\ 0 & 1 & 0 & t_y \\ 0 & 0 & 1 & t_z \\ 0 & 0 & 0 & 1 \end{bmatrix} T(tx,ty,tz)=100001000010txtytz1

2.4 旋转变换

screenShot.png

R x ( α ) = [ 1 0 0 0 0 cos ⁡ α − sin ⁡ α 0 0 sin ⁡ α cos ⁡ α 0 0 0 0 1 ] R_x(\alpha) = \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & \cos{\alpha} & - \sin{\alpha} & 0 \\ 0 & \sin{\alpha} & \cos{\alpha} & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} Rx(α)=10000cosαsinα00sinαcosα00001

R y ( α ) = [ cos ⁡ α 0 sin ⁡ α 0 0 1 0 0 − sin ⁡ α 0 cos ⁡ α 0 0 0 0 1 ] R_y(\alpha) = \begin{bmatrix} \cos{\alpha} & 0 & \sin{\alpha} & 0 \\ 0 & 1 & 0 & 0 \\ - \sin{\alpha} & 0 & \cos{\alpha} & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} Ry(α)=cosα0sinα00100sinα0cosα00001

R z ( α ) = [ cos ⁡ α − sin ⁡ α 0 0 sin ⁡ α cos ⁡ α 0 0 0 0 1 0 0 0 0 1 ] R_z(\alpha) = \begin{bmatrix} \cos{\alpha} & - \sin{\alpha} & 0 & 0 \\ \sin{\alpha} & \cos{\alpha} & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} Rz(α)=cosαsinα00sinαcosα0000100001

2.4.1 欧拉角

R x y z ( α , β , γ ) = R x ( α ) R y ( β ) R z ( γ ) R_{xyz}(\alpha,\beta,\gamma) = R_{x}(\alpha)R_{y}(\beta)R_{z}(\gamma) Rxyz(α,β,γ)=Rx(α)Ry(β)Rz(γ)

  • 使用三个角: r o l l roll roll , p i t c h pitch pitch , y a w yaw yaw

screenShot.png

2.4.2 罗德里格斯(Rodrigues)旋转方程

绕着某个过原点的轴 n \bold{n} n 旋转 α \alpha α 角度 :

R ( n , α ) = cos ⁡ ( α ) I + ( 1 − cos ⁡ ( α ) ) n n T + sin ⁡ ( α ) ( 0 − n z n y n z 0 − n x − n y n x 0 ) \bold{R}(\bold{n},\alpha) = \cos(\alpha)\bold{I} + (1 - \cos(\alpha))\bold{n}\bold{n}^T + \sin(\alpha) \begin{pmatrix} 0 & -n_z & n_y \\ n_z & 0 & -n_x \\ -n_y & n_x & 0 \end{pmatrix} R(n,α)=cos(α)I+(1cos(α))nnT+sin(α)0nznynz0nxnynx0

式中的矩阵形式类似叉乘矩阵

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值