侧边栏壁纸
博主头像
LittleAO的学习小站 博主等级

在知识的沙漠寻找绿洲

  • 累计撰写 125 篇文章
  • 累计创建 27 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

计算机图形学之图形几何变换

LittleAO
2024-06-26 / 0 评论 / 0 点赞 / 15 阅读 / 0 字
温馨提示:
本文最后更新于2024-06-26,若内容或图片失效,请留言反馈。 部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

二维几何变换

平移、缩放和旋转是基本的图形变换,这里首先考虑二维操作,然后讨论这些基本的思想怎样扩充到三维场景中。

基本变换

平移变换

将一个点平移距离t_xt_y,就是将原始坐标加上两值,从而获得一个新的坐标(x',y')。可以实现二维未知的平移。用数学表达式表达为:

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

将其转化为矩阵形式,可以表达为:

\begin{bmatrix} x'\\y' \end{bmatrix} =\begin{bmatrix} x\\y \end{bmatrix} + \begin{bmatrix} t_x\\t_y \end{bmatrix}

可以注意到等号右边是一个加法的形式,能不能仅仅用一个矩阵来表示平移的这种操作?这里我们引入齐次坐标的概念。关于齐次坐标我们会在下面进行阐述。齐次坐标的表达形式为:

\begin{bmatrix}x'\\y'\\1\end{bmatrix}= \begin{bmatrix} 1&0&t_x\\0&1&t_y\\0&0&1 \end{bmatrix} \begin{bmatrix} x\\y\\1 \end{bmatrix}

即:

\mathbf{P}'=\mathbf{T}\cdot\mathbf{P}

\mathbf{T}称为平移变换矩阵。

旋转变换

旋转的基准点为O,点P旋转一定角度得到P'。我们将平面直角坐标系转换为极坐标系。更容易计算旋转变换,设角\phiP的原始角度位置与水平线的夹角,\theta为旋转角:

\begin{cases} x'=r\cos(\phi+\theta)=r\cos\phi\cos\theta-r\sin\phi\sin\theta\\ y'=r\sin(\phi+\theta)=r\sin\phi\cos\theta+r\cos\phi\sin\theta\\ \end{cases}

将其转化为齐次坐标矩阵的形式为:

\begin{bmatrix}x'\\y'\\1\end{bmatrix}= \begin{bmatrix} \cos\theta&-\sin\theta&0\\\sin\theta&\cos\theta&0\\0&0&1 \end{bmatrix} \begin{bmatrix} x\\y\\1 \end{bmatrix}
\mathbf{P}'=\mathbf{R}\cdot\mathbf{P}

缩放变换

以坐标原点为不动点,将点的坐标进行缩放,用数学表达式表达为:

\begin{cases} x'=xS_x\\y'=yS_y \end{cases}

用齐次坐标矩阵表达为:

\begin{bmatrix} x'\\y'\\1 \end{bmatrix}= \begin{bmatrix} S_x&0&0\\0&S_y&0\\0&0&1 \end{bmatrix} \begin{bmatrix} x\\y\\1 \end{bmatrix}
\mathbf{P}'=\mathbf{S}\cdot\mathbf{P}

错切变换

![

  • 沿x轴方向错切:y坐标不变,x平移一个与y有关的量:

x'=x+ay

齐次坐标矩阵形式:

\begin{bmatrix} x'\\y'\\1 \end{bmatrix}= \begin{bmatrix} 1&a&0\\0&1&0\\0&0&1 \end{bmatrix} \begin{bmatrix} x\\y\\1 \end{bmatrix}
  • 沿y轴方向错切,x坐标不变,y平移一个与x有关的量。

y'=y+bx

齐次坐标矩阵形式:

\begin{bmatrix} x'\\y'\\1 \end{bmatrix}= \begin{bmatrix} 1&0&0\\b&1&0\\0&0&1 \end{bmatrix} \begin{bmatrix} x\\y\\1 \end{bmatrix}

以上的变换,即平移、旋转、缩放和错切统称为仿射变换,仿射变换的通用矩阵形式为:

\begin{bmatrix} x'\\y'\\1 \end{bmatrix}= \begin{bmatrix} a&b&m\\c&d&n\\0&0&s \end{bmatrix} \begin{bmatrix} x\\y\\1 \end{bmatrix}

\mathbf P'=\mathbf M \mathbf P

其中\mathbf M可以分为如下三个部分:

1. 左上角\begin{bmatrix} a&b\\c&d \end{bmatrix}可以用来描述旋转、缩放和错切。

2. 右上角\begin{bmatrix} m\\n \end{bmatrix}可以用来完成平移变换。

3. 若右下角的[s]不为1,则相当于对整体做了等比例缩放变换。

为什么s能控制整体的缩放?会在下面的齐次坐标说明。

齐次坐标

齐次坐标表示法就是用n+1维向量来表示一个n维向量。n维列向量(\mathbf P_1,\mathbf P_2,...,\mathbf P_n)^T表示为齐次坐标(h\mathbf P_1,h\mathbf P_2,...,h\mathbf P_n,h),其中称h为哑坐标。

齐次坐标具有以下特点:

1.h的值不是唯一的;

2. 普通坐标和齐次坐标的关系为“一对多”。

3. 当h=1产生的齐次坐标称为规格化坐标。

齐次坐标有如下作用:

1. 可以将各种变换用一个阶数统一的矩阵来表示。

2. 当h=0,表示一个方向向量,当h \neq 0,表示一个点。

3. 齐次坐标的变换矩阵保证图形拓扑关系不变。

4. 变换具有统一表示形式的优点,便于变换合成和硬件实现。

逆变换

将变换后的物体还原为原来形状的变换称之为逆变换(\mathbf M^{-1})。以下是常见的逆变换矩阵。

  • 逆平移变换

\mathbf T^{-1}= \begin{bmatrix} 1&0&-t_x\\0&1&-t_y\\0&0&1 \end{bmatrix}
  • 逆旋转变换

\mathbf R^{-1}= \begin{bmatrix} \cos\theta&\sin\theta&0\\-\sin\theta&\cos\theta&0\\0&0&1 \end{bmatrix}
  • 逆缩放变换

\mathbf S^{-1}= \begin{bmatrix} \dfrac 1 {S_x}&0&0\\0&\dfrac 1 {S_y}&0\\0&0&1 \end{bmatrix}

注意到旋转变换中,逆矩阵和转置矩阵相同,该阵为正交阵。

复合变换

复合变换矩阵是由变换矩阵的成绩合并而来。若P要依次经过\mathbf M_1、\mathbf M_2两次变换,其复合变换矩阵可以表示为:

\mathbf P'=\mathbf M_2\mathbf M_1 \mathbf P

下面给出一道实例:基准点旋转。

如图是一个矩形,其几何中心坐标为(x_r,y_r),现要求该矩形要按照几何中心逆时针旋转\theta,求复合变换矩阵。

1. 首先先将基准点移动到坐标原点;

2. 绕坐标原点进行旋转;

3. 将基准点移动到初始位置。

其复合变换矩阵计算方式如下:

\mathbf M=\begin{gathered}\begin{bmatrix}1&0&x_r\\0&1&y_r\\0&0&1\end{bmatrix}\begin{bmatrix}\cos\theta&-\sin\theta&0\\\sin\theta&\cos\theta&0\\0&0&1\end{bmatrix}\begin{bmatrix}1&0&-x_r\\0&1&-y_r\\0&0&1\end{bmatrix} \\=\begin{bmatrix}\cos\theta&-\sin\theta&x_{r}(1-\cos\theta)+y_{r}\sin\theta\\\sin\theta&\cos\theta&y_{r}(1-\cos\theta)-x_{r}\sin\theta\\0&0&1\end{bmatrix} \end{gathered}

坐标系变换

计算机图形应用经常需要在场景处理的各阶段将对象的描述从一个坐标系变换到另一个坐标系,例如将对象描述从世界坐标系变换到观察坐标系。这一问题通常称为坐标变换。

  • 几何变换方法

1. 先将新坐标系的原点O'平移到坐标原点O,可以用下面的矩阵表示:

\mathbf T(-x_0,-y_0)= \begin{bmatrix}1&0&-x_0\\0&1&-y_0\\0&0&1\end{bmatrix}

2. 将新坐标系x'轴旋转到原坐标系x'轴上:

\mathbf R(-\theta)= \begin{bmatrix} \cos\theta&-\sin\theta&0\\ \sin\theta&\cos\theta&0\\ 0&0&1 \end{bmatrix}

3. 将两个变换矩阵合并起来,得到坐标变换矩阵\mathbf M

\mathbf M=\mathbf R(-\theta)\mathbf T(-x_0,-y_0)
  • 基变换方法

该方法属于线性代数的基本内容,由于前置知识较多,这里不做过多阐述,以下是有详细解释的链接:

https://zhuanlan.zhihu.com/p/114084418

三维几何变换

学会了二维几何变换,三维几何变换很容易类比推理得到。三维变换无非是在二维变换的基础上增加了$z$坐标,齐次矩阵变成了4维。

平移变换

这个和二维变换大同小异。变换矩阵如下:

\mathbf T= \begin{bmatrix} 1&0&0&t_x\\ 0&1&0&t_y\\ 0&0&1&t_z\\ 0&0&0&1 \end{bmatrix}

旋转变换

旋转变换分为三种,分别是绕x轴、绕y轴和绕z轴的逆时针旋转变换。结合图示来看:

  • z轴的旋转变换:相当于在xOy平面内绕原点做逆时针旋转。

\mathbf R_z(\theta)= \begin{bmatrix} \cos\theta&-\sin\theta&0&0\\ \sin\theta&\cos\theta&0&0\\ 0&0&1&0\\ 0&0&0&1 \end{bmatrix}
  • x轴的旋转变换,相当于在yOz平面内绕原点做逆时针旋转。

\mathbf R_x(\theta)= \begin{bmatrix} 1&0&0&0\\ 0&\cos\theta&-\sin\theta&0\\ 0&\sin\theta&\cos\theta&0\\ 0&0&0&1 \end{bmatrix}
  • y轴的旋转变换,相当于在xOz平面内绕原点做顺时针旋转。

\mathbf R_y(\theta)= \begin{bmatrix} \cos\theta&0&\sin\theta&0\\ 0&1&0&0\\ -\sin\theta&0&\cos\theta&0\\ 0&0&0&1 \end{bmatrix}

缩放变换

三维缩放变换是二维缩放变换的简单扩充,变换矩阵:

\mathbf S= \begin{bmatrix} s_x&0&0&0\\ 0&s_y&0&0\\ 0&0&s_z&0\\ 0&0&0&1 \end{bmatrix}

错切变换

  • 沿z轴错切:z'=cx+fy+z

\begin{bmatrix}x'\\y'\\z'\\1\end{bmatrix}=\begin{bmatrix}1&0&0&0\\0&1&0&0\\c&f&1&0\\0&0&0&1\end{bmatrix}\begin{bmatrix}x\\y\\z\\1\end{bmatrix}
  • 沿y轴错切:y'=b x+y+hz

\begin{bmatrix}x'\\y'\\z'\\1\end{bmatrix}=\begin{bmatrix}1&0&0&0\\b&1&h&0\\0&0&1&0\\0&0&0&1\end{bmatrix}\begin{bmatrix}x\\y\\z\\1\end{bmatrix}
  • 沿x轴错切:x'=x+dy+gz

\begin{bmatrix}x'\\y'\\z'\\1\end{bmatrix}=\begin{bmatrix}1&d&g&0\\0&1&0&0\\0&0&1&0\\0&0&0&1\end{bmatrix}\begin{bmatrix}x\\y\\z\\1\end{bmatrix}

三维几何变换通式

\mathbf{P'}=\begin{bmatrix}x'\\y'\\z'\\1\end{bmatrix}=\begin{bmatrix}a&b&c&l\\d&e&f&m\\h&i&j&n\\0&0&0&s\end{bmatrix}\begin{bmatrix}x\\y\\z\\1\end{bmatrix}=\mathbf{TP}

在功能上可以分为三部分:

1. 左上角\begin{bmatrix} a&b&c\\ d&e&f\\ h&i&j \end{bmatrix},可以进行缩放、旋转、错切等几何变换。

2. 右上角\begin{bmatrix} l\\ m\\ n \end{bmatrix}可以产生平移变换。

3.[s]影响整体的缩放变换。

坐标系变换和复合变换与二维变换类似,这里不再赘述,其中坐标系变换会在下面的课程中再次出现。

复合变换分析中的两种思考模式

全局固定坐标系模式

第一种容易理解的:将一个复合变换分解成几个基本的变换,在依次将它们作用于图形。这种变换分解再合成的方法比较直观也容易理解。

在使用上述方法时,要注意矩阵相乘的顺序,也就是左乘。

这种方式称为全局固定坐标系模式。

局部活动坐标系模式

这种方法就是将我们要进行变换的物体看作一个坐标系,然后对这个坐标系进行变换操作。需要注意的是,两种方法的思考方式不同,但代码呈现出的结果是相似的

0

评论区