CG-02: Transformation

2832 字
14 分钟
CG-02: Transformation

这一块内容可以直接看Games101课程关于Transformation的讲解,更容易理解一些。齐次坐标是精华。二编补充了习题部分。

CG-02 变换#

1. 基础数学#

1.1 三角学#

  • 定义 (Definition): 研究三角形的边长和角度之间的关系

  • 公式 (Formulas):

    a=ccosα,b=ccosβa = c \cdot \cos \alpha, \quad b = c \cdot \cos \beta a=csinβ,b=csinαa = c \cdot \sin \beta, \quad b = c \cdot \sin \alpha b=atanα,a=btanβb = a \cdot \tan \alpha, \quad a = b \cdot \tan \beta

1.2 三角学性质#

  • sin2α+cos2α=1\sin^2 \alpha + \cos^2 \alpha = 1
  • sin(α)=sinα\sin(-\alpha) = -\sin \alpha
  • cos(α)=cosα\cos(-\alpha) = \cos \alpha
  • cosα=sin(π2α)\cos \alpha = \sin \left( \frac{\pi}{2} - \alpha \right)
  • sin(α+β)=sinαcosβ+cosαsinβ\sin(\alpha + \beta) = \sin \alpha \cos \beta + \cos \alpha \sin \beta
  • cos(α+β)=cosαcosβsinαsinβ\cos(\alpha + \beta) = \cos \alpha \cos \beta - \sin \alpha \sin \beta

2. 矩阵 ⭐#

2.1 矩阵定义#

  • 矩阵是一个矩形数组,可以包含数值、表达式或函数。

  • 在计算机图形学中,通常只考虑数值矩阵。

  • 方阵 (Square Matrix): 当矩阵的行数和列数相等时,称为方阵。

2.2 基本矩阵运算#

2.2.1 矩阵加法与减法 (Addition/Subtraction)#

  • 对应元素相加或相减。

2.2.2 标量乘法#

  • 每个矩阵元素乘以标量 λ\lambda

2.2.3 转置 (Transpose)#

  • 行列互换。

2.2.4 矩阵乘法#

  • 左矩阵的列数必须等于右矩阵的行数。

    公式:

    cij=k=1naikbkjc_{ij} = \sum_{k=1}^n a_{ik} \cdot b_{kj}

2.2.5 逆矩阵#

  • 如果方阵的逆矩阵存在,则称该矩阵为非奇异矩阵或可逆矩阵。

    • 矩阵 AA 的逆记为 A1A^{-1},满足 AA1=IA \cdot A^{-1} = I
  • 正交矩阵 (Orthogonal Matrix): 一个矩阵的转置等于其逆矩阵。

    • 满足 QQT=IQ \cdot Q^T = I

3. 二维变换 (2D Transformations)#

3.1 平移 (Translation)#

  • 公式:

    x=x+tx,y=y+tyx' = x + t_x, \quad y' = y + t_y
  • 矩阵形式:

    [xy1]=[10tx01ty001][xy1]\begin{bmatrix} x' \\ y' \\ 1 \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}

    P=T(tx,ty)PP'=T(t_x,t_y)\cdot P

3.2 缩放 (Scaling)#

  • 公式:

    x=xsx,y=ysyx' = x \cdot s_x, \quad y' = y \cdot s_y
  • 矩阵形式:

    [xy1]=[sx000sy0001][xy1]\begin{bmatrix} x' \\ y' \\ 1 \end{bmatrix} = \begin{bmatrix} s_x & 0 & 0 \\ 0 & s_y & 0 \\ 0 & 0 & 1 \end{bmatrix} \cdot \begin{bmatrix} x \\ y \\ 1 \end{bmatrix}

    P=S(sx,sy)PP'=S(s_x,s_y)\cdot P

  • 关于任意参考点的缩放:

    1. 平移参考点到原点。
    2. 关于原点缩放。
    3. 平移回原位置。

    P=T(xf,yf)S(sx,sy)T(xf,yf)PP'=T(x_f,y_f)\cdot S(s_x,s_y) \cdot T(-x_f,-y_f) \cdot P

3.3 旋转 (Rotation)#

  • 关于原点的旋转:

    x=xcosθysinθx' = x \cdot \cos \theta - y \cdot \sin \theta y=xsinθ+ycosθy' = x \cdot \sin \theta + y \cdot \cos \theta
  • 矩阵形式:

    [xy1]=[cosθsinθ0sinθcosθ0001][xy1]\begin{bmatrix} x' \\ y' \\ 1 \end{bmatrix} = \begin{bmatrix} \cos \theta & -\sin \theta & 0 \\ \sin \theta & \cos \theta & 0 \\ 0 & 0 & 1 \end{bmatrix} \cdot \begin{bmatrix} x \\ y \\ 1 \end{bmatrix}

    P=R(θ)PP'=R(\theta)\cdot P

  • 关于任意点的旋转:

    1. 平移旋转点到原点。
    2. 关于原点旋转。
    3. 平移回原位置。

    x=xr+(xxr)cosθ(yyr)sinθx' = x_r + (x - x_r) \cdot \cos \theta - (y - y_r) \cdot \sin \theta

    y=yr+(xxr)sinθ+(yyr)cosθy' = y_r + (x - x_r) \cdot \sin \theta + (y - y_r) \cdot \cos \theta

    P=T(xr,yr)R(θ)T(xr,yr)PP'=T(x_r,y_r)\cdot R(\theta) \cdot T(-x_r,-y_r) \cdot P

3.4 错切 (Shearing)#

  • 水平错切 (Horizontal Shearing):

    x=x+λxy,y=yx' = x + \lambda_x \cdot y, \quad y' = y
  • 垂直错切 (Vertical Shearing):

    x=x,y=y+λyxx' = x, \quad y' = y + \lambda_y \cdot x
  • 矩阵形式:

    水平错切 (Horizontal Shearing): [1λx0010001]\text{水平错切 (Horizontal Shearing): } \begin{bmatrix} 1 & \lambda_x & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{bmatrix} 垂直错切 (Vertical Shearing): [100λy10001]\text{垂直错切 (Vertical Shearing): } \begin{bmatrix} 1 & 0 & 0 \\ \lambda_y & 1 & 0 \\ 0 & 0 & 1 \end{bmatrix}

4. 三维变换 (3D Transformations)#

4.1 平移 (Translation)#

  • 公式:

    x=x+tx,y=y+ty,z=z+tzx' = x + t_x, \quad y' = y + t_y, \quad z' = z + t_z
  • 矩阵形式:

    [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} \cdot \begin{bmatrix} x \\ y \\ z \\ 1 \end{bmatrix}

4.2 缩放 (Scaling)#

  • 公式:

    x=xsx,y=ysy,z=zszx' = x \cdot s_x, \quad y' = y \cdot s_y, \quad z' = z \cdot s_z
  • 矩阵形式:

    [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} \cdot \begin{bmatrix} x \\ y \\ z \\ 1 \end{bmatrix}

4.3 旋转 (Rotation) :whale2:#

  • 关于坐标轴的旋转:

    • xx 轴旋转 (Rotation About xx-Axis):

      [10000cosθsinθ00sinθcosθ00001]\begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & \cos \theta & -\sin \theta & 0 \\ 0 & \sin \theta & \cos \theta & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}

      旋转90度的矩阵为:

      [1000001001000001]\begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 0 & -1 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
    • yy 轴旋转 (Rotation About yy-Axis):

      [cosθ0sinθ00100sinθ0cosθ00001]\begin{bmatrix} \cos \theta & 0 & \sin \theta & 0 \\ 0 & 1 & 0 & 0 \\ -\sin \theta & 0 & \cos \theta & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}

      旋转90度的矩阵为:

      [0010010010000001]\begin{bmatrix} 0 & 0 & 1 & 0 \\ 0 & 1 & 0 & 0 \\ -1 & 0 & 0 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
    • zz 轴旋转 (Rotation About zz-Axis):

      [cosθsinθ00sinθcosθ0000100001]\begin{bmatrix} \cos \theta & -\sin \theta & 0 & 0 \\ \sin \theta & \cos \theta & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}

      旋转90度的矩阵为:

      [0100100000100001]\begin{bmatrix} 0 & -1 & 0 & 0 \\ 1 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
  • 任意轴旋转 (Rotation About an Arbitrary Axis):

    绕任意轴旋转需要使用角轴参数法,并将其转化为矩阵表示。

    1. 旋转轴

      • 旋转轴由一个单位向量 n=(nx,ny,nz)n=(n_x,n_y,n_z) 表示,定义了旋转的方向。

      • 该向量通常是一个归一化向量,即满足:

        n=nx2+ny2+nz2=1∥\textbf{n}∥=n_x^2+n_y^2+n_z^2=1

        n=(x,y,z)x2+y2+z2\textbf{n}=\frac{(x,y,z)}{\sqrt{x^2+y^2+z^2}}

    2. 旋转角度

      • 旋转角度 θθ 表示绕旋转轴旋转的角度,通常以弧度为单位。
    3. 旋转点

      • 被旋转的点 p=(x,y,z)\textbf{p}=(x,y,z) 是旋转操作的输入。
    4. 旋转公式

      n=(x,y,z)x2+y2+z2\textbf{n}=\frac{(x,y,z)}{\sqrt{x^2+y^2+z^2}} r=(np)n\textbf{r} = (\textbf{n}⋅\textbf{p})\textbf{n} s=pr\textbf{s} = \textbf{p}-\textbf{r} v=n×s=n×p\textbf{v} = \textbf{n}×\textbf{s} = \textbf{n}×\textbf{p} q=cosθs+sinθv\textbf{q} = \cos ⁡θ\textbf{s} + \sin ⁡θ\textbf{v} p=r+q\textbf{p}′=\textbf{r}+\textbf{q} =(np)n+cosθ(p(np)n)+sinθ(n×p)=(\textbf{n}⋅\textbf{p})\textbf{n}+\cos ⁡θ(\textbf{p}-(\textbf{n}⋅\textbf{p})\textbf{n})+\sin ⁡θ(\textbf{n}×\textbf{p}) =cosθp+(1cosθ)(np)n+sinθ(n×p)=\cos ⁡θ⋅\textbf{p}+(1−\cos ⁡θ)(\textbf{n}⋅\textbf{p})\textbf{n}+\sin ⁡θ(\textbf{n}×\textbf{p})
      • 第一项: cosθp\cos⁡θ⋅p 表示点 pp在旋转过程中保持的分量。
      • 第二项: (1cosθ)(np)n(1−\cos⁡θ)(n⋅p)n 表示点 pp 在旋转轴方向上的投影分量。
      • 第三项: sinθ(n×p)\sin⁡θ(n×p) 表示点 pp 在旋转平面内的分量。

5. 变换的顺序#

  • 矩阵乘法不满足交换律,变换的顺序会影响结果。

  • 公式:

    ABBAAB \neq BA
  • P=MnMn1M1PP' = M_n \cdot M_{n-1} \cdot \dots \cdot M_1 \cdot P

  • 可以先将多个变换矩阵合并为一个矩阵,再应用到点上,以减少计算成本 。

  • 逆变换:P=MP    P=M1PP'=M \cdot P \iff P=M^{-1} \cdot P'

6. 齐次坐标 (Homogeneous Coordinates)#

  • 定义 (Definition): 在二维中,点 (x,y)(x, y) 的齐次坐标为 (x,y,1)(x, y, 1);在三维中,点 (x,y,z)(x, y, z) 的齐次坐标为 (x,y,z,1)(x, y, z, 1)
  • 优势 (Advantages):
    • 使用齐次坐标可以将多个变换组合成一个矩阵运算 。

Ex#

1.Write Transformation Matrix#

A 3D object is rotated by 90 degrees about an axis passing from (0, 1, 1) to (2, 1, 1). Write out the transformation matrix.

写出绕轴(0,1,1)至(2,1,1)旋转90°的变换矩阵。

Answer#

先计算旋转轴向量为 u=(1,0,0)u=(1,0,0),实际上就是x轴正方向,然后逆时针(默认)旋转90度。

根据矩阵旋转公式,计算每个分量

  • ux=1,uy=0,uz=0u_x=1,u_y=0,u_z=0
  • cosθ=cos(90)=0\cos ⁡θ=\cos ⁡(90∘)=0
  • sinθ=sin(90)=1\sin⁡θ=\sin⁡(90∘)=1

将上述值代入旋转矩阵公式,矩阵 RR 变为:

R=[cosθ+ux2(1cosθ)uxuy(1cosθ)uzsinθuxuz(1cosθ)+uysinθuyux(1cosθ)+uzsinθcosθ+uy2(1cosθ)uyuz(1cosθ)uxsinθuzux(1cosθ)uysinθuzuy(1cosθ)+uxsinθcosθ+uz2(1cosθ)]R = \begin{bmatrix} \cos\theta + u_x^2(1 - \cos\theta) & u_x u_y (1 - \cos\theta) - u_z \sin\theta & u_x u_z (1 - \cos\theta) + u_y \sin\theta \\ u_y u_x (1 - \cos\theta) + u_z \sin\theta & \cos\theta + u_y^2(1 - \cos\theta) & u_y u_z (1 - \cos\theta) - u_x \sin\theta \\ u_z u_x (1 - \cos\theta) - u_y \sin\theta & u_z u_y (1 - \cos\theta) + u_x \sin\theta & \cos\theta + u_z^2(1 - \cos\theta) \end{bmatrix}

得到

R=[100001010]R = \begin{bmatrix} 1 & 0 & 0 \\ 0 & 0 & -1 \\ 0 & 1 & 0 \end{bmatrix}

由于做旋转,是先平移做旋转再平移,所以齐次化的旋转矩阵是:

T=[1000001001000001]T = \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & -1 & 0 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}

总的变换矩阵为:

[1000010100110001][1000001001000001][1000010100110001]\begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 1 \\ 0 & 0 & 1 & 1 \\ 0 & 0 & 0 & 1 \end{bmatrix} \cdot \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 0 & -1 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} \cdot \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & -1 \\ 0 & 0 & 1 & -1 \\ 0 & 0 & 0 & 1 \end{bmatrix}

2. Describe Transformation Matrix#

Describe what transformation the matrix MM performs when applied to a 3D object:

M=[1000002003000001]M = \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 0 & -2 & 0 \\ 0 & 3 & 0 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}

描述这个3D变换矩阵做什么操作。

Answer#

1. 观察矩阵结构#

矩阵 MM 是一个 4×4 的齐次变换矩阵,其常用于表示三维空间的几何变换,齐次变换矩阵的结构通常如下:

[r11r12r13txr21r22r23tyr31r32r33tz0001]\begin{bmatrix} r_{11} & r_{12} & r_{13} & t_x \\ r_{21} & r_{22} & r_{23} & t_y \\ r_{31} & r_{32} & r_{33} & t_z \\ 0 & 0 & 0 & 1 \end{bmatrix}
  • 其中,左上 ( 3×33 \times 3 ) 的矩阵定义了旋转和缩放(线性变换部分)。
  • 最后一列位置放入 (tx,ty,tz)( t_x, t_y, t_z ),用于表示平移。

具体到矩阵 MM

M=[1000002003000001]M = \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 0 & -2 & 0 \\ 0 & 3 & 0 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}
  • 从矩阵第四列 [0,0,0,1]T[ 0, 0, 0, 1 ]^T 可以看出:没有平移分量,仅包含旋转和缩放。 只需要研究左上的 3×33 \times 3 矩阵部分: [100002030]\begin{bmatrix} 1 & 0 & 0 \\ 0 & 0 & -2 \\ 0 & 3 & 0 \end{bmatrix}

2. 如何判断旋转#

旋转矩阵有一些特点和作用:

  • 旋转矩阵表示轴的重定义,它会重新定义 ( x )、( y )、( z ) 三个坐标轴的方向。
  • 而矩阵总体的作用是将物体在局部坐标系中的点(列向量形式)变换到新的坐标系中。

观察矩阵的形式:

  1. 第一列是 [1,0,0]T[1, 0, 0]^T,说明 xx 轴没有变化(保持原来方向)。
  2. 第二列是 [0,0,3]T[0, 0, 3]^T,说明旧 zz 轴变为新的 yy 轴方向。
  3. 第三列是 [0,2,0]T[0, -2, 0]^T,说明旧 yy 轴变为新的 zz 轴方向,但符号被翻转。

这说明,yyzz 发生了互换,且 zz 方向翻转了符号
这是经典的绕 xx 轴旋转 90° 的结果。

对比绕 xx 轴旋转的矩阵:

Rx(90)=[100001010]R_x(90^\circ) = \begin{bmatrix} 1 & 0 & 0 \\ 0 & 0 & -1 \\ 0 & 1 & 0 \end{bmatrix}

基础形式与 MM 对应的矩阵相符,因此可以认定矩阵包 MMxx 轴旋转 90 度 的操作。

3. 如何判断缩放#

旋转矩阵本身是一个正交矩阵,其列向量是单位化的正交向量,然而在矩阵 ( M ) 的左上部分:

[100002030]\begin{bmatrix} 1 & 0 & 0 \\ 0 & 0 & -2 \\ 0 & 3 & 0 \end{bmatrix}

第二列和第三列的长度分别为:

  • 第二列:02+02+32=3\sqrt{0^2 + 0^2 + 3^2} = 3
  • 第三列:02+(2)2+02=2\sqrt{0^2 + (-2)^2 + 0^2} = 2

这说明旋转操作之后同时有缩放操作

  • 在新的 yy 轴方向缩放了3倍。
  • 在新的 zz 轴方向缩放了2倍。
  • xx 轴的缩放因子是1(保持不变)。

因此可以得出,矩阵还包含了非均匀缩放,在 ( x, y, z ) 方向上的缩放因子分别为 ( 1, 2, 3 )。

4. 总结#

结合对旋转和缩放的分析,矩阵 MM 的变换顺序是:

  1. 先绕 xx 轴旋转 90°;
  2. 再在 ( x, y, z ) 方向分别进行非均匀缩放(缩放因子 ( 1, 2, 3 ))。
[1000002003000001]=[1000020000300001][1000001001000001]\begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 0 & -2 & 0 \\ 0 & 3 & 0 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} = \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 2 & 0 & 0 \\ 0 & 0 & 3 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 0 & -1 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}

First rotate 90 degrees around the x-axis

And then perform the non-uniform scaling around the x-, y-, and z-axes with factors 1, 2, and 3, respectively.

文章分享

如果这篇文章对你有帮助,欢迎分享给更多人!

CG-02: Transformation
https://cauchyoooo.github.io/posts/2025/cs5182cg/cg-02/
作者
Cauchy
发布于
2025-02-14
许可协议
CC BY-NC-SA 4.0

评论区

Profile Image of the Author
Cauchy
Thinking...
近期状态
5.20: 人果然还是要好好照顾自己的身体。刚刚更换完blog主题, 文章搬迁ing...
音乐
封面

音乐

暂未播放

0:00 0:00
暂无歌词
分类
标签
站点统计
文章
28
分类
3
标签
8
总字数
96,173
运行时长
0
最后活动
0 天前

文章目录