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

本文介绍了二维和三维空间中的线性变换原理,包括缩放、反射、切变、旋转和位移等基本变换,并详细解释了如何使用矩阵来表示这些变换。特别地,文章深入探讨了齐次坐标系下的仿射变换及其组合应用。

1. 二维线性变换

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

[a11a12a21a22][xy]=[a11x+a12ya21x+a22y]=[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)

缩放变换是一种沿着 坐标轴作用 的变换

scale(sx,sy)=[sx00sy] scale(s_x,s_y) = \begin{bmatrix} s_x & 0 \\ 0 & s_y \end{bmatrix} scale(sx,sy)=[sx00sy]

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

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

screenShot.png

[x′y′]=[0.5000.5][xy] \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.5001][xy] \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′=−xy′=y \begin{aligned} &x' = -x \\ &y' = y \end{aligned} x=xy=y

沿着 y 轴进行翻转操作

screenShot.png

[x′y′]=[−1001][xy] \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)

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

shear−x(s)=[1s01]shear−y(s)=[10s1] \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′]=[1a01][xy] \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+ayx' = x + ayx=x+ay
    • y = 0 的部分不变化
    • y = 1 的部分右移 a 距离
       
  • 竖直方向:y′=yy' = yy=y
    • 不变化

1.4 旋转

默认规定

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

screenShot.png

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

可知:

{x′=xcos⁡ϕ−ysin⁡ϕy′=ycos⁡ϕ+xsin⁡ϕ \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⁡ϕ][xy] \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

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

1.5 位移

screenShot.png

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

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

[x′y′]=[abcd][xy]+[txty] \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 齐次坐标

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

  • 2D−point=(x,y,1)T2D-point = (x,y,1)^T2Dpoint=(x,y,1)T
  • 2D−vector=(x,y,0)T2D-vector = (x,y,0)^T2Dvector=(x,y,0)T

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

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

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

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

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

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

[x′y′w′]=[10tx01ty001]⋅[xy1]=[x+txy+ty1] \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′]=[10tx01ty001]⋅[xy0]=[xy0] \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′]=[abcd]⋅[xy]+[txty] \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]=[abtxcdty001]⋅[xy1] \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 变换的组合

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

R45⋅T(1,0)≠T(1,0)⋅R45 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]=R45⋅T(1,0)⋅[xy1] \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)⋅R45⋅[xy1] \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

组合应用若干变换 A1,A2,A3,…A_1,A_2,A_3,\dotsA1,A2,A3,

An(…A2(A1(x)))=An…A2⋅A1⋅[xy1] 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 仿射变换

  • 3D−point=(x,y,z,1)T3D-point = (x,y,z,1)^T3Dpoint=(x,y,z,1)T

  • 3D−vector=(x,y,z,0)T3D-vector = (x,y,z,0)^T3Dvector=(x,y,z,0)T

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

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

[x′y′z′1]=[abctxdeftyghitz0001]⋅[xyz1] \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 缩放变换

Scale(sx,sy,sz)=[sx0000sy0000sz00001] 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(tx,ty,tz)=[100tx010ty001tz0001] 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

Rx(α)=[10000cos⁡α−sin⁡α00sin⁡αcos⁡α00001] 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

Ry(α)=[cos⁡α0sin⁡α00100−sin⁡α0cos⁡α00001] 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

Rz(α)=[cos⁡α−sin⁡α00sin⁡αcos⁡α0000100001] 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 欧拉角

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

  • 使用三个角:rollrollroll , pitchpitchpitch , yawyawyaw

screenShot.png

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

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

R(n,α)=cos⁡(α)I+(1−cos⁡(α))nnT+sin⁡(α)(0−nznynz0−nx−nynx0) \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

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

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值