0%

03 - 基础矩阵变换

1. 二维线性变换

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

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

1.1 缩放(Scaling)

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

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

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

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

screenShot.png

[xy]=[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}

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

screenShot.png

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

1.2 反射(Reflection)

进行一个镜像的变换

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

沿着 y 轴进行翻转操作

screenShot.png

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

1.3 切变(Shearing)

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

shearx(s)=[1s01]sheary(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}

沿着 x 轴方向拉动 a 距离

screenShot.png

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

NOTE :

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

1.4 旋转

默认规定

  • 绕原点 (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}

可知:

{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}

即:

[xy]=[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}

绕原点逆时针旋转 45°

screenShot.png

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

1.5 位移

screenShot.png

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

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

[xy]=[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}

1.5.1 齐次坐标

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

  • 2Dpoint=(x,y,1)T2D-point = (x,y,1)^T
  • 2Dvector=(x,y,0)T2D-vector = (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} 表示“可视平面”上的点 [x/wy/w1]\begin{bmatrix} x/w \\ y/w \\ 1 \end{bmatrix}w0w \ne 0

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

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

[xyw]=[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]=[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}

1.5.2 仿射变换

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

[xy]=[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}

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

[xy1]=[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}

NOTE

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

1.6 变换的组合

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

R45T(1,0)T(1,0)R45R_{45} \cdot T_{(1,0)} \ne T_{(1,0)} \cdot R_{45}

screenShot.png

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

screenShot.png

[xy1]=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}

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

An(A2(A1(x)))=AnA2A1[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}

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

2. 三维线性变换

2.1 仿射变换

  • 3Dpoint=(x,y,z,1)T3D-point = (x,y,z,1)^T

  • 3Dvector=(x,y,z,0)T3D-vector = (x,y,z,0)^T

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

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

[xyz1]=[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}

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}

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}

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}

Ry(α)=[cosα0sinα00100sinα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}

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}

2.4.1 欧拉角

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

  • 使用三个角:rollroll , pitchpitch , yawyaw

screenShot.png

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

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

R(n,α)=cos(α)I+(1cos(α))nnT+sin(α)(0nznynz0nxnynx0)\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}

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