圖形幾何變換
圖形變換是計算機圖形學中的一個重要內容。通過對簡單圖形進行多種變換和組合,可以形成一個復雜圖形,這些操作也用於將世界坐標系中的場景描述轉換為輸出設備上的觀察顯示中。
應用於對象幾何描述並改變它的位置、方向或大小等幾何信息的操作稱為幾何變換(Geometric Transformation)。這種變換一般維持圖形的拓撲關系(構成規則)不變,只改變圖形的幾何關系(大小、形狀及相對位置),主要包括平移、放縮、旋轉及投影等操作。
平移變換

\[P=\begin{bmatrix} x \\ y \end{bmatrix} {P}'=\begin{bmatrix} {x}' \\ {y}' \end{bmatrix} T=\begin{bmatrix} t_{x} \\ t_{y} \end{bmatrix}\]
\[{P}'=P+T \]
通過齊次坐標變換矩陣表示
\[{P}'=\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}= T(t_{x},t_{y})\cdot P \]
繞坐標原點的旋轉變換

旋轉角定向:逆時針為正,順時針為負
\[cos(\alpha + \theta )= cos(\alpha)cos(\theta) - sin(\alpha)sin(\theta) \]
\[sin(\alpha + \theta )= sin(\alpha)cos(\theta) + cos(\alpha)sin(\theta) \]
\[OA=\begin{bmatrix} rcos(\alpha) \\ rsin(\alpha) \end{bmatrix} OB=\begin{bmatrix} rcos(\alpha + \theta ) \\ rsin(\alpha + \theta ) \end{bmatrix} T=\begin{bmatrix} cos\theta & -sin\theta \\ sin\theta & cos\theta \end{bmatrix}\]
\[OB=OA \cdot T \]
通過齊次坐標變換矩陣表示
\[{P}'=\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}= R(\theta) \cdot P \]
以坐標原點為基准點的縮放變換

\[{x}'=x \cdot s_{x} \]
\[{y}'=y \cdot s_{y} \]
\[{P}'=\begin{bmatrix}{x}'\\ {y}' \end{bmatrix} =\begin{bmatrix}s_{x} & 0\\ 0 & s_{y} \end{bmatrix}\cdot\begin{bmatrix}x\\ y \end{bmatrix}= s \cdot P\]
通過齊次坐標變換矩陣表示
\[{x}'=x \cdot s_{x} \]
\[{y}'=y \cdot s_{y} \]
\[{P}'=\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}= S(S_{x},s_{y}) \cdot P \]
反射變換
產生對象鏡像的變換稱為反射(reflection),變換通過將對象繞反射軸旋轉180°來生成反射鏡像

1 相對於x軸的反射
\[ \begin{bmatrix} {x}' \\ {y}' \end{bmatrix} =\begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix} \cdot \begin{bmatrix} x \\ y \end{bmatrix} \]
2 相對於y軸的反射
\[ \begin{bmatrix} {x}' \\ {y}' \end{bmatrix}= \begin{bmatrix} -1 & 0 \\ 0 & 1 \end{bmatrix} \cdot \begin{bmatrix} x \\ y \end{bmatrix} \]
3 相對於坐標原點的反射
\[ \begin{bmatrix} {x}' \\ {y}' \end{bmatrix}= \begin{bmatrix} -1 & 0 \\ 0 & -1 \end{bmatrix} \cdot \begin{bmatrix} x \\ y \end{bmatrix} \]
通過齊次坐標變換矩陣表示
\[{P}'=\begin{bmatrix}{x}'\\ {y}' \\ 1 \end{bmatrix} =\begin{bmatrix} a & b & 0\\ c & d & 0 \\ 0 & 0& 1 \end{bmatrix} \cdot \begin{bmatrix}x\\ y \\ 1 \end{bmatrix} =R(a,b,c,d) \cdot P \]
4 相對於任意點的反射
\[{P}'=\begin{bmatrix}{x}'\\ {y}' \\ 1 \end{bmatrix} =\begin{bmatrix} -1 & 0 & 2u\\ 0 & -1 & 2v \\ 0 & 0& 1 \end{bmatrix} \cdot \begin{bmatrix}x\\ y \\ 1 \end{bmatrix} =T \cdot P \]
5 關於對角線 y= x 的反射
\[ \begin{bmatrix} {x}' \\ {y}' \end{bmatrix}= \begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix} \cdot \begin{bmatrix} x \\ y \end{bmatrix} \]
6 關於對角線 y= -x 的反射
\[ \begin{bmatrix} {x}' \\ {y}' \end{bmatrix}= \begin{bmatrix} 0 & -1 \\ -1 & 0 \end{bmatrix} \cdot \begin{bmatrix} x \\ y \end{bmatrix} \]
7 相對於任意直線 y=mx+b 的反射
\[p'= \begin{bmatrix} x'\\ y' \\ 1 \end{bmatrix} = \frac{1}{1+m^{2}} \begin{bmatrix} 1-m^{2} & 2m & -2mb\\ 2m & m^{2}-1 & 2b\\ 0 & 0 & 1+m^{2} \end{bmatrix} \cdot \begin{bmatrix} x\\ y \\ 1 \end{bmatrix} =T\cdot P \]
推導
錯切變換
錯切(shear)是一種使對象形狀發生變化的變換,經過錯切的對象好像是由相互滑動的內部夾層組成

相對於x軸的x方向錯切由下列變換產生
\[ \begin{bmatrix} {x}' \\ {y}' \end{bmatrix} =\begin{bmatrix} 1 & sh_{x} \\ 0 & 1 \end{bmatrix} \cdot \begin{bmatrix} x \\ y \end{bmatrix} \]
該變換對應的坐標轉換為
\[{x}'=x+sh_{x} \cdot y \]
\[{y}'=y \]
通過齊次坐標變換矩陣表示
相對於線\(y=y_{ref}\)的x方向錯切
\[ \begin{bmatrix}{x}'\\ {y}' \\ 1 \end{bmatrix} =\begin{bmatrix} 1 & sh_{x} & -sh_{x} \cdot y_{ref} \\ 0 & 1 & 0 \\ 0 & 0& 1 \end{bmatrix} \cdot \begin{bmatrix}x\\ y \\ 1 \end{bmatrix} \]
相對於線\(x=x_{ref}\)的y方向錯切
\[ \begin{bmatrix}{x}'\\ {y}' \\ 1 \end{bmatrix} =\begin{bmatrix} 1 & 0 & 0 \\ sh_{y} & 1 & -sh_{y} \cdot x_{ref}\\ 0 & 0& 1 \end{bmatrix} \cdot \begin{bmatrix}x\\ y \\ 1 \end{bmatrix} \]
逆變換
通過齊次坐標變換矩陣表示
平移逆變換
\[T^{-1} =\begin{bmatrix} 1 & 0 & -t_{x} \\ 0 & 1 & -t_{y} \\ 0 & 0& 1 \end{bmatrix} \]
旋轉逆變換
\[R^{-1} =\begin{bmatrix} cos \theta & -sin \theta & 0 \\ sin \theta & cos \theta & 0 \\ 0 & 0& 1 \end{bmatrix} \]
縮放逆變換
\[S^{-1} =\begin{bmatrix} \frac{1}{s_{x}} & 0 & 0 \\ 0 & \frac{1}{s_{y}} & 0 \\ 0 & 0& 1 \end{bmatrix} \]
二維復合變換
利用矩陣表達式,可以通過計算單個變換的矩陣乘積,將任意的變換序列組成復合變換矩陣。形成變換矩陣的乘積經常稱為矩陣的合並或復合。由於坐標點位置已經用齊次列矩陣表示,故必須將變換矩陣序列依次先乘該列矩陣。由於場景中許多位置用相同的順序變換,因此先將所有變換矩陣相乘形成一個復合矩陣將是高效率的方法。
因此,如果對點位置P進行兩次變換,變換后的位置將用下式計算
\[{P}'=M_{1} \cdot M_{2} \cdot P = M \cdot P \]
\(該坐標位置可采用矩陣M來變換,而不是單獨地先用M_{1}然后再用M_{2}來變換\)
二維基本復合變換
1 復合二維平移
\[\begin{bmatrix} 1 & 0 & t_{2x} \\ 0 & 1 & t_{2y} \\ 0 & 0& 1 \end{bmatrix} \cdot \begin{bmatrix} 1 & 0 & t_{1x} \\ 0 & 1 & t_{1y} \\ 0 & 0& 1 \end{bmatrix} = \begin{bmatrix} 1 & 0 & t_{1x}+t_{2x} \\ 0 & 1 & t_{1y}+t_{2y} \\ 0 & 0& 1 \end{bmatrix} \]
\[T(t_{2x},t_{2y}) \cdot T(t_{1x},t_{1y}) = T (t_{1x}+t_{2x},t_{1y}+t_{2y} ) \]
2 復合二維旋轉
\[\begin{bmatrix} cos \theta_{1} & -sin \theta_{1} & 0 \\ sin \theta_{1} & cos \theta_{1} & 0 \\ 0 & 0& 1 \end{bmatrix} \cdot \begin{bmatrix} cos \theta_{2} & -sin \theta_{2} & 0 \\ sin \theta_{2} & cos \theta_{2} & 0 \\ 0 & 0& 1 \end{bmatrix} = \begin{bmatrix} cos (\theta_{1}+\theta_{2}) & -sin (\theta_{1}+\theta_{2}) & 0 \\ sin (\theta_{1}+\theta_{2}) & cos (\theta_{1}+\theta_{2}) & 0 \\ 0 & 0& 1 \end{bmatrix} \]
\[R( \theta_{1}) \cdot R(\theta_{2}) = R(\theta_{1} + \theta_{2}) \]
3 復合二維縮放
\[\begin{bmatrix} s_{2x} & 0 & 0 \\ 0 & s_{2y} & 1 \\ 0 & 0& 1 \end{bmatrix} \cdot \begin{bmatrix} s_{1x} & 0 & 0 \\ 0 & s_{1y} & 1 \\ 0 & 0& 1 \end{bmatrix} = \begin{bmatrix} s_{2x} \cdot s_{1x} & 0 & 0 \\ 0 & s_{2y} \cdot s_{1y} & 1 \\ 0 & 0& 1 \end{bmatrix} \]
\[S(s_{2x},s_{2y}) \cdot S(s_{1x},s_{1y}) = S (s_{1x} \cdot s_{2x},s_{1y} \cdot s_{2y} ) \]
二維通用復合變換
基准點旋轉
平移對象使基准點位置移動到坐標原點
繞坐標原點旋轉
平移對象使基准點回到其原始位置
\[T(x_{r},y_{r}) \cdot R(\theta) \cdot T(-x_{r},-y_{r}) = R(x_{r},y_{r},\theta) \]
基准點縮放
平移對象使固定點與坐標原點重合
對坐標原點為基准點進行縮放
反向平移將對象返回到原始位置
\[T(x_{r},y_{r}) \cdot S(s_{x},s_{y}) \cdot T(-x_{r},-y_{r}) = R(x_{r},y_{r},s_{x},s_{y}) \]
定向縮放
將對象所希望的縮放方向旋轉到與坐標軸一致,進行縮放后再逆向旋轉至原來方位。
\[R^{-1}(\theta) \cdot S(s_{x},s_{y}) \cdot R(\theta) \]
二維坐標系間的變換
計算機圖形應用經常需要在場景處理的各階段將對象的描述從一個坐標系變換到另一個坐標系。觀察子程序將對象描述從世界坐標系變換到設備坐標系。對於建模和設計應用,每個對象在各自的局部笛卡爾坐標系中設計。這些局部坐標描述必須接着變換到整個場景坐標系的相應位置和方向。
同樣,有時場景在利用對象對稱性的非笛卡爾參考系描述。在這些系統中的對象描述必須轉換到笛卡爾世界坐標系中進行處理。
三維幾何基本變換
三維基本幾何變換都是相對於坐標原點和坐標軸進行的幾何變換
假設三維形體變換前一點為p(x,y,z),變換后為p'(x',y',z')。
平移變換

通過齊次坐標變換矩陣表示
\[{P}'=\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} \]
繞坐標原點的旋轉變換

通過齊次坐標變換矩陣表示
- 繞x軸旋轉
\[T_{RX} =\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軸旋轉
\[T_{RY} =\begin{bmatrix} cos \theta & 0 & sin \theta & 0 \\ 0 & 1 & 0 & 0\\ -sin \theta & 0 & cos \theta & 0 \\ 0 & 0 & 0& 1 \end{bmatrix} \]
- 繞z軸旋轉
\[T_{RZ} =\begin{bmatrix} cos \theta & -sin \theta & 0 & 0 \\ sin \theta & cos \theta & 0 & 0\\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0& 1 \end{bmatrix} \]
縮放變換

通過齊次坐標變換矩陣表示
\[T_{S} =\begin{bmatrix} S_{x} & 0 & 0 & 0 \\ 0 & S_{y} & 0 & 0\\ 0 & 0 & S_{z} & 0 \\ 0 & 0 & 0& 1 \end{bmatrix} \]
三維復合變換
三維復合變換是指圖形作一次以上的基本變換,變換結果是基本變換矩陣相乘
圖形幾何變換的模式
變換合成的方法大大提高了對圖形對象依次做多次變換時的效率,同時變換合成也提供了一種構造復雜變換的方法。需要對圖形對象進行的復雜變換也可以分解成為多個簡單的基本變換,再依次將其作用於圖形
固定坐標系模式(相對於原始坐標系)
相對於同一個固定坐標系,每一次變換均可看成相對於原始坐標系執行
先調用的變換先執行,后調用的變換后執行
矩陣合並時,先調用的矩陣放在右邊,后調用的矩陣放在左邊,也稱為固定坐標系模式
活動坐標系模式(堆棧結構調用)
又稱空間模式,連續執行幾個變換時,變換矩陣的合並方式與固定坐標系模式相反。
先調用的變換乘在連乘式左邊,而后調用的變換乘在連乘式右邊。
體現在程序執行方式上,即為先調用的變換后執行,后調用的變換先執行。
該模式非常適合堆棧結構實現,也是OpenGL圖形庫所采用的方式