计算机图形学完整笔记(六):三维图形变换

Posted by Lucius on August 19, 2020

第六章 三维图形变换

6.1 三维图形几何变换

6.1.1 几何变换概述

三维基本几何变换皆是相对于坐标原点和坐标轴进行的几何变换。与二维变换类似,引入齐次坐标表示,即三维空间中某点的变换可以表示成点的齐次坐标与四阶的三维变换矩阵相乘。

$$ p' = \left[ \begin{matrix} x^* & y^* & z^* & 1\end{matrix}\right] =\left[ \begin{matrix} x & y & z & 1 \end{matrix}\right]· \left[ \begin{matrix} a&b&c&p\\d&e&f&q\\g&h&i&r\\l&m&n&s \end{matrix} \right] $$

6.1.2 平移变换
$$ \left[ \begin{matrix} x' & y' & z' & 1\end{matrix}\right]=\left[ \begin{matrix} x & y & z & 1\end{matrix}\right]·T_t=\left[ \begin{matrix} x & y & z & 1\end{matrix}\right] · \left[ \begin{matrix} 1&0&0&0\\0&1&0&0\\0&0&1&0\\T_x&T_y&T_z&1 \end{matrix} \right] = \left[ \begin{matrix} x+T_x & y+T_y & z+T_z & 1\end{matrix}\right] $$
6.1.3 比例变换
  • 局部比例变换:
$$ \left[ \begin{matrix} x' & y' & z' & 1\end{matrix}\right]=\left[ \begin{matrix} x & y & z & 1\end{matrix}\right]·T_s=\left[ \begin{matrix} x & y & z & 1\end{matrix}\right] ·\left[ \begin{matrix} a&0&0&0\\0&e&0&0\\0&0&i&0\\0&0&0&1 \end{matrix} \right] = \left[ \begin{matrix} ax&ey&iz & 1\end{matrix}\right] $$
  • 整体比例变换:
$$ \left[ \begin{matrix} x' & y' & z' & 1\end{matrix}\right]=\left[ \begin{matrix} x & y & z & 1\end{matrix}\right]·T_s=\left[ \begin{matrix} x & y & z & 1\end{matrix}\right] ·\left[ \begin{matrix} 1&0&0&0\\0&1&0&0\\0&0&1&0\\0&0&0&s \end{matrix} \right] = \left[ \begin{matrix} x&y&z & s\end{matrix}\right] $$
6.1.4 旋转变换

三维立体的旋转变换是指给定的三维立体绕三维空间某个指定的坐标轴旋转 $ \theta$ 角度。旋转后,立体的空间位置将发生变化,但形状不变。

  • 右手定则:右手大拇指指向旋转轴的正向,其余四指指向旋转角的正向

  • 绕 z 轴旋转:
$$ \left[ \begin{matrix} x & y&z & 1 \end{matrix}\right]· \left[ \begin{matrix} cos\theta & sin\theta &0&0\\-sin\theta&cos\theta & 0&0\\0&0&1&0\\0&0&0&1 \end{matrix} \right] =\left[ \begin{matrix} x ·cos\theta-y ·sin\theta & x ·sin\theta + y ·cos\theta &z&1\end{matrix}\right] $$
  • 绕 x 轴旋转:
$$ \left[ \begin{matrix} x & y&z & 1 \end{matrix}\right]· \left[ \begin{matrix} 1&0&0&0\\ 0&cos\theta & sin\theta &0\\0&-sin\theta&cos\theta & 0\\0&0&0&1 \end{matrix} \right] =\left[ \begin{matrix} x & y·cos\theta-z ·sin\theta & y ·sin\theta + z ·cos\theta &1\end{matrix}\right] $$
  • 绕 y 轴旋转:
$$ \left[ \begin{matrix} x & y&z & 1 \end{matrix}\right]· \left[ \begin{matrix} cos\theta &0& -sin\theta &0\\0&1&0&0\\ sin\theta&0&cos\theta &0\\0&0&0&1 \end{matrix} \right] =\left[ \begin{matrix} x·sin\theta+x·cos\theta & y & z·cos\theta-x·sin\theta & 1 \end{matrix}\right] $$
  • 绕任意轴旋转

    • 先将轴移动到坐标原点,再将轴旋转到坐标轴上

    • 进行几何变换

    • 将轴反旋转,再反平移到原来位置

6.1.5 对称变换
  • 关于坐标平面的对称

    • 关于 $x0y$ 平面对称:
    $$ T = \left[ \begin{matrix} 1&0&0&0\\0&1&0&0\\0&0&-1&0\\0&0&0&1 \end{matrix} \right],[x'\ y'\ z'\ 1]=[x\ y \ -z\ \ 1] $$
    • 关于 $y0z$ 平面对称:
    $$ T = \left[ \begin{matrix} -1&0&0&0\\0&1&0&0\\0&0&1&0\\0&0&0&1 \end{matrix} \right], [x'\ y'\ z'\ 1]=[-x\ y \ z\ 1] $$
    • 关于 $z0x$ 平面对称:
    $$ T = \left[ \begin{matrix} 1&0&0&0\\0&-1&0&0\\0&0&1&0\\0&0&0&1 \end{matrix} \right], [x'\ y'\ z'\ 1]=[x\ -y \ \ z \ 1] $$
  • 关于坐标轴的对称

    • 关于 $x$ 轴对称:
    $$ T = \left[ \begin{matrix} 1&0&0&0\\0&-1&0&0\\0&0&-1&0\\0&0&0&1 \end{matrix} \right],[x'\ y'\ z'\ 1]=[x\ \ -y \ -z\ \ 1] $$
    • 关于 $x$ 轴对称:
    $$ T = \left[ \begin{matrix} -1&0&0&0\\0&1&0&0\\0&0&-1&0\\0&0&0&1 \end{matrix} \right],[x'\ y'\ z'\ 1]=[-x\ \ y \ -z\ \ 1] $$
    • 关于 $z$ 轴对称:
    $$ T = \left[ \begin{matrix} -1&0&0&0\\0&-1&0&0\\0&0&1&0\\0&0&0&1 \end{matrix} \right],[x'\ y'\ z'\ 1]=[-x\ -y \ \ z\ \ 1] $$

6.2 投影变换分类

投影变换:解决三维输出到二维

  • 投影法分类(区别在于投影中心到投影面之间的距离是有/无限的):

  • 透视投影法(中心投影法),比如建筑透视

    • 生成真实感视图但不保持相关比例
  • 平行投影法

    • 正投影法,比如工程样图

    • 斜投影法

    • 保持物体的有关比例不变,物体的各个面的精确视图由平行投影而得,没有给出三维物体外表的真实性表示


6.3 平行投影 (三视图、轴测图)

6.3.1 平行投影概述
  • 特点

    • 物体各个面的精确视图又平行投影而得

    • 没有给出三维物体外表的真实性但保持比例

6.3.2 三视图

  • 主视图变换矩阵

    • 投影在 $x0z$ 面,直接将 $y$ 置 $0$
    $$ T = \left[ \begin{matrix} 1&0&0&0\\0&0&0&0\\0&0&1&0\\0&0&0&1 \end{matrix} \right] $$
  • 俯视图变换矩阵
    $$ T = \left[ \begin{matrix} 1&0&0&0\\0&0&-1&0\\0&0&0&0\\0&0&-z_0&1 \end{matrix} \right] $$
    • 下述为推导过程

      • 投影在 $x0y$ 面,直接置 $z=0$,
      $$ T = \left[ \begin{matrix} 1&0&0&0\\0&1&0&0\\0&0&0&0\\0&0&0&1 \end{matrix} \right] $$
      • 为了使俯视图与主视图都画在一个平面内,就要使 $H$ 面绕 $x$ 轴顺时针转90°,进入 $x0z$ 面
      $$ T = \left[ \begin{matrix} 1&0&0&0\\0&cos(-90^ \circ)&sin(-90^ \circ)&0\\0&-sin(-90^ \circ)&cos(-90^ \circ)&0\\0&0&0&1 \end{matrix} \right] =\left[ \begin{matrix} 1&0&0&0\\0&0&-1&0\\0&1&0&0\\0&0&0&1 \end{matrix} \right] $$
      • 为了使主视图和俯视图有一定的间距,还要使 $H$ 面沿 $z$ 方向平移一段距离 $-z_0$ ,
      $$ T = \left[ \begin{matrix} 1&0&0&0\\0&1&0&0\\0&0&1&0\\0&0&-z_0 &1 \end{matrix} \right] $$
  • 侧视图变换矩阵

    $$ T = \left[ \begin{matrix} 0&0&0&0\\-1&0&0&0\\0&0&1&0\\-x_0&0&0&1 \end{matrix} \right] $$
    • 下述为推导过程

      • 投影在 $y0z$面,直接置 $x=0$,
      $$ T = \left[ \begin{matrix} 0&0&0&0\\0&1&0&0\\0&0&1&0\\0&0&0&1 \end{matrix} \right] $$
      • 为了使侧视图与主视图也在一个平面内,就要使 $W$ 面绕 $z$ 轴正转90°,进入 $x0z$ 面
      $$ T = \left[ \begin{matrix} cos(-90^ \circ)&sin(-90^ \circ)&0&0\\-sin(-90^ \circ)&cos(-90^ \circ)&0&0\\0&0&1&0\\0&0&0&1 \end{matrix} \right] =\left[ \begin{matrix} 0&1&0&0\\-1&0&0&0\\0&0&1&0\\0&0&0&1 \end{matrix} \right] $$
      • 为使主视图和侧视图有一定的间距,还要使 $W$ 面沿负 $x$ 方向平移一段距离 $-x_0$
      $$ T = \left[ \begin{matrix} 1&0&0&0\\0&1&0&0\\0&0&1&0\\-x_0&0&0 &1 \end{matrix} \right] $$
  • 主视图: $\left[ \begin{matrix} x’ & y’&z’ & 1 \end{matrix}\right] = \left[ \begin{matrix} x & 0&z & 1 \end{matrix}\right]$

  • 俯视图: $\left[ \begin{matrix} x’ & y’&z’ & 1 \end{matrix}\right] = \left[ \begin{matrix} x & 0&-(y+z_0 ) & 1 \end{matrix}\right]$

  • 侧视图: $\left[ \begin{matrix} x’ & y’&z’ & 1 \end{matrix}\right] = \left[ \begin{matrix} -(y+x_0) & 0&z & 1 \end{matrix}\right]$

  • 三个视图中的 $y’$ 均为 $0$,表明三个视图均落在 $x0z$ 面上
6.3.3 正轴侧
  • 等轴侧:当投影面与三个坐标轴之间的夹角都相等

  • 正二测:当投影面与两个坐标轴之间的夹角相等

  • 正三测:当投影面与三个坐标轴之间的夹角都不相等

  • 正轴侧变换矩阵:

    • 以 $V$ 面为轴测投影面 (三视图中的 $x0z$ 面),先将物体绕 $Z$ 轴转 $γ$ 角,接着绕 $X$ 轴转 $-α$ 角,最后向 $V$ 面投影

    $$ T_正 = T_Z ·T_X·T_V = \left[ \begin{matrix} cos\gamma & sin\gamma & 0&0\\-sin\gamma & cos\gamma & 0& 0 \\ 0&0&1&0\\0&0&0&1 \end{matrix} \right] · \left[ \begin{matrix} 1&0&0&0\\0&cos\alpha&-sin\alpha&0\\0&sin\alpha&cos\alpha&0\\0&0&0&1 \end{matrix} \right] · \left[ \begin{matrix} 1&0&0&0\\0&0&0&0\\0&0&1&0\\0&0&0&1 \end{matrix} \right] \\ = \left[ \begin{matrix} cos\gamma & 0 & -sin\gamma sin\alpha &0\\ -sin\gamma & 0&-cos\gamma sin\alpha & 0 \\ 0&0&cos\alpha&0\\0&0&0&1 \end{matrix} \right] $$
    • 即 $\left[ \begin{matrix} x’ & y’&z’ & 1 \end{matrix}\right] = \left[ \begin{matrix} xcos\gamma-ysin\gamma & 0& -xsin\gamma sin\alpha-ycos\gamma sin\alpha + zcos\alpha & 1 \end{matrix}\right]$
  • 正等轴测图:取 $\gamma = 45^\circ , \alpha = -35.26^\circ$

$$ T_{\text{正等轴测}} = \left[ \begin{matrix} 0.7071&0&-0.4082&0\\-0.7071&0&-0.4082&0\\0&0&0.8165&0\\0&0&0&1 \end{matrix} \right] $$
  • 正二测图:取 $\gamma = 20.7^\circ , \alpha = 19.47^\circ$
$$ T_{\text{正二测}} = \left[ \begin{matrix} 0.9354&0&-0.1178&0\\-0.7071&0&-0.3118&0\\0&0&0.9428&0\\0&0&0&1 \end{matrix} \right] $$

6.4 透视投影

6.4.1 透视投影概述
  • 特点

    • 物体投影视图由计算投影线与观察平面之交点而得

    • 生成真实感视图但不保持比例

  • 三维变换矩阵,其中 p、q、r 能产生透视变换的效果。
$$ T_{3D} = \left[ \begin{matrix} a&b&c&p\\d&e&f&q\\g&h&i&r\\l&m&n&s \end{matrix} \right] $$

6.4.2 一点透视
  • 假设 $q \not=0,p=r=0$

$$ \left[ \begin{matrix} x&y&z&1 \end{matrix} \right]· \left[ \begin{matrix} 1&0&0&0\\0&1&0&q\\0&0&1&0\\0&0&0&1 \end{matrix} \right] =\left[ \begin{matrix} x&y&z&qy+1 \end{matrix} \right] $$
  • 齐次化后:
$$ \left[ \begin{matrix} x'&y'&z'&1 \end{matrix} \right]=\left[ \begin{matrix} \frac{x}{qy+1}&\frac{y}{qy+1}&\frac{z}{qy+1}&1 \end{matrix} \right] $$
  • 当 $y=0$ 时,处于 y=0 平面内的点变换后无变化
$$ \left[ \begin{matrix} x'&y'&z'&1 \end{matrix} \right]=\left[ \begin{matrix}x&0&z&1 \end{matrix} \right] $$
  • 当 $y\rightarrow \infty$时,所有点都集中到y轴的1/q处,这点叫灭点
$$ \left[ \begin{matrix} x'&y'&z'&1 \end{matrix} \right]=\left[ \begin{matrix}0&\frac{1}{q}&0&1 \end{matrix} \right]$$

  • 只形成一个灭点的透视变换即为一点透视。

  • 同样的分别假设 $p$、$r$ 其中一个不为 $0$ ,另外两个为 $0$ ,都会产生一个灭点 $(\frac{1}{p}, 0, 0)$、$(0, 0, \frac{1}{r})$。

6.4.3 多点透视
  • 根据一点透视的原理予以推广,如果 $p$、$q$、$r$ 三个元素中有两个为非零元素时,将会生成两个灭点,因此得到两点透视;相应的三点透视概念也可得知。

    • 如当 $p\not=0,r\not=0$,
    $$ \left[ \begin{matrix} x&y&z&1 \end{matrix} \right]· \left[ \begin{matrix} 1&0&0&p\\0&1&0&0\\0&0&1&r\\0&0&0&1 \end{matrix} \right] =\left[ \begin{matrix} x&y&z&px+rz+1 \end{matrix} \right] $$
    • 齐次化后:
    $$ \left[ \begin{matrix} x'&y'&z'&1 \end{matrix} \right]=\left[ \begin{matrix} \frac{x}{px+rz+1}&\frac{y}{px+rz+1}&\frac{z}{px+rz+1}&1 \end{matrix} \right] $$
    • 分别看到两个灭点,$(\frac{1}{p}, 0, 0)$、$(0, 0, \frac{1}{r})$

6.4.4 生成透视投影图的方法

  • 设投影中心:c(0, 0, -d),现在推空间一点 $p(x, y, z)$ 的透视投影点 $p’(x’, y’, z’)$

  • $ \triangle ABC \sim \triangle A’OC \Rightarrow \frac{x’}{x} =\frac{y’}{y} = \frac{d}{d+z} \Rightarrow x’= \frac{x}{1+z/d},y’ = \frac{y}{1+z/d},z’ = 0$

  • 矩阵形式是:

$$ \left[ \begin{matrix} x&y&z&1 \end{matrix} \right]· \left[ \begin{matrix} 1&0&0&0\\0&1&0&0\\0&0&1&\frac{1}{d}\\0&0&0&1 \end{matrix} \right] =\left[ \begin{matrix} x&y&z&\frac{z}{d}+1 \end{matrix} \right] $$
  • 透视坐标与 $z$ 值成反比。即 $z$ 值越大,透视坐标值越小

  • $d$ 的取值不同,可以对形成的透视图有放大和缩小的功能。当值较大时,形成的透视图变大;反之缩小。

  • 再乘以向投影面投影的变换矩阵,就得到点在画面上的投影

$$ \left[ \begin{matrix} x&y&z&1 \end{matrix} \right]· \left[ \begin{matrix} 1&0&0&0\\0&1&0&0\\0&0&1&\frac{1}{d}\\0&0&0&1 \end{matrix} \right] · \left[ \begin{matrix} 1&0&0&0\\0&1&0&0\\0&0&0&0\\0&0&0&1 \end{matrix} \right] $$
  • 若投影中心在无穷远处,则 $\frac{1}{d} \rightarrow 0$,上式变为平行投影。
6.4.5 一点透视投影实例

  • 设 $l=-0.8$, $m=-1.6$, $n=-2$, 视距 $d=-2.5$

6.4.6 二点透视投影实例:

  • 将物体平移到适当位置 $l、m、n$

  • 将物体绕 $y$ 轴旋转 $θ$ 角

  • 进行透视变换

  • 最后向 $x0y$ 面做正投影,即得二点透视图


资料来源