計算機圖形學 變換
變換 Transformation
什麼是變換
變換就是將一個點或向量從一個位置移動到另一個位置的過程。
變換的型別
以二維的形式給出。
縮放
變換公式
x
′
=
s
x
y
′
=
t
y
x'=sx\\y'=ty
x′=sxy′=ty
對應的矩陣
(
s
0
0
t
)
\begin{pmatrix} s & 0\\ 0 & t \end{pmatrix}
(s00t)
反射
變換公式
x
′
=
−
x
y
′
=
y
x'=-x\\ y'=y
x′=−xy′=y
變換矩陣
(
−
1
0
0
1
)
\begin{pmatrix}-1 & 0\\0 &1\end{pmatrix}
切變(shear transformation)
變換公式
x
′
=
x
+
a
y
y
′
=
y
x'=x+ay\\y'=y
x′=x+ayy′=y
變換矩陣
(
1
a
0
1
)
\begin{pmatrix}1 & a\\0 &1\end{pmatrix}
(10a1)
旋轉變換(Rotate)
旋轉變換一般認為是認為繞過原點的旋轉軸逆時針旋轉,假設點 ( x , y ) (x,y) (x,y)繞 Z Z Z軸旋轉 β \beta β得到了 ( x ′ , y ′ ) (x',y') (x′,y′),公式推導如下
對於
(
x
,
y
)
(x,y)
x
=
r
c
o
s
α
y
=
r
sin
α
.
x=rcos\alpha\\y=r\sin\alpha.
x=rcosαy=rsinα.
旋轉了
β
\beta
β後,得到
x
′
=
r
c
o
s
(
α
+
β
)
y
′
=
r
s
i
n
(
α
+
β
)
.
x'=rcos(\alpha + \beta)\\y'=rsin(\alpha + \beta).
x′=rcos(α+β)y′=rsin(α+β).
展開後可得到
x
′
=
r
(
c
o
s
α
c
o
s
β
−
s
i
n
α
s
i
n
β
)
=
x
c
o
s
β
−
y
s
i
n
β
y
′
=
r
(
s
i
n
α
c
o
s
β
+
c
o
s
α
s
i
n
β
)
=
x
s
i
n
β
+
y
c
o
s
β
.
x'=r(cos\alpha cos\beta - sin\alpha sin\beta)=xcos\beta - ysin\beta\\y'=r(sin\alpha cos\beta + cos\alpha sin\beta) = xsin\beta+ycos\beta.
可以得到旋轉矩陣為
R
β
=
(
c
o
s
β
−
s
i
n
β
s
i
n
β
c
o
s
β
)
R_\beta=\begin{pmatrix} cos\beta & -sin\beta \\ sin\beta & cos\beta \end{pmatrix}
Rβ=(cosβsinβ−sinβcosβ)
已知上述矩陣為正交矩陣,因此有
R
−
β
=
R
β
T
R_{-\beta}=R_{\beta}^T
R−β=RβT
線性變換
上面提到的變換都可以統一為如下形式
x
′
=
a
x
+
b
y
y
′
=
c
x
+
d
y
x'=ax+by\\y'=cx+dy\\
x′=ax+byy′=cx+dy
從而寫成矩陣的形式
(
a
b
c
d
)
\begin{pmatrix} a & b\\ c & d\\ \end{pmatrix}
(acbd)
但是,對於平移來說,其公式如下
x
′
=
x
+
t
x
y
′
=
y
+
t
y
.
x'=x+t_x\\y'=y+t_y.
x′=x+txy′=y+ty.
無法統一到矩陣的表示當中,這就導致了齊次座標的引入。
齊次座標
在二維空間中,齊次座標引入了一個與 x , y x,y x,y無關的一個維度來表示二維空間中的點和向量。
- 2D point = ( x , y , 1 ) T (x,y,1)^T (x,y,1)T
- 2D vector = ( x , y , 0 ) T (x,y,0)^T (x,y,0)T
為什麼區別點和向量?向量具有平移不變性(方向不變),做任何平移後不變,即向量的座標表示的是方向。但點平移之後座標需要發生變化。因此規定, ( x , y , w ) T = ( x / w , y / w , 1 ) T (x,y,w)^T=(x/w,y/w,1)^T (x,y,w)T=(x/w,y/w,1)T
由齊次座標可以引出
- v + v = v v + v = v v+v=v
- p − p = v p - p = v p−p=v
- p + v = p ( 點 沿 向 量 的 方 向 移 動 向 量 模 的 大 小 ) p + v = p (點沿向量的方向移動向量模的大小) p+v=p(點沿向量的方向移動向量模的大小)
- p 1 + p 2 = p , p 是 p 1 , p 2 的 中 點 p_1 + p_2 =p,\ p是p_1,p_2的中點 p1+p2=p,p是p1,p2的中點
上述結果可以直接推廣到三維空間中
- 3D point = ( x , y , z , 1 ) T (x,y,z,1)^T (x,y,z,1)T
- 3D vector = ( x , y , z , 0 ) T (x,y,z,0)^T (x,y,z,0)T
規定
(
x
,
y
,
z
,
w
)
T
=
(
x
/
w
,
y
/
w
,
z
/
w
,
1
)
T
(x,y,z,w)^T=(x/w,y/w,z/w,1)^T
(x,y,z,w)T=(x/w,y/w,z/w,1)T
使用了齊次座標後,上面的變換都可以統一到矩陣的表示上來。
-
縮放
( s x 0 0 0 0 s y 0 0 0 0 s z 0 0 0 0 1 ) \begin{pmatrix} s_x & 0 & 0 & 0\\ 0 & s_y & 0 & 0 \\ 0 & 0 & s_z & 0 \\ 0 & 0 & 0 & 1 \end{pmatrix} ⎝⎜⎜⎛sx0000sy0000sz00001⎠⎟⎟⎞ -
旋轉
-
繞 Z Z Z軸旋轉
( c o s α − s i n α 0 0 s i n α c o s α 0 0 0 0 1 0 0 0 0 1 ) \begin{pmatrix} cos\alpha & -sin\alpha & 0 & 0 \\ sin\alpha & cos\alpha & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{pmatrix} ⎝⎜⎜⎛cosαsinα00−sinαcosα0000100001⎠⎟⎟⎞ -
繞 X X X軸旋轉
( 1 0 0 0 0 c o s α − s i n α 0 0 s i n α c o s α 0 0 0 0 1 ) \begin{pmatrix} 1 & 0 & 0 & 0\\ 0 & cos\alpha & -sin\alpha & 0\\ 0 & sin\alpha & cos\alpha & 0\\ 0 & 0 & 0 & 1 \end{pmatrix} ⎝⎜⎜⎛10000cosαsinα00−sinαcosα00001⎠⎟⎟⎞ -
繞 Y Y Y軸旋轉
( c o s α 0 s i n α 0 0 1 0 0 − s i n α 0 c o s α 0 0 0 0 1 ) \begin{pmatrix} cos\alpha & 0 & sin\alpha & 0\\ 0 & 1 & 0 & 0\\ -sin\alpha & 0 & cos\alpha & 0\\ 0 & 0 & 0 & 1 \end{pmatrix} ⎝⎜⎜⎛cosα0−sinα00100sinα0cosα00001⎠⎟⎟⎞
-
-
平移
( 1 0 0 t x 0 1 0 t y 0 0 1 t z 0 0 0 1 ) \begin{pmatrix} 1 & 0 & 0 & t_x\\ 0 & 1 & 0 & t_y\\ 0 & 0 & 1 & t_z\\ 0 & 0 & 0 & 1 \end{pmatrix} ⎝⎜⎜⎛100001000010txtytz1⎠⎟⎟⎞
Rodrigues‘ Rotation Formula
view/camera transformation檢視變換
什麼是view transformation
view rransformation:take a photo
成像過程(MVP):model transfomation,view transformation(find a angle ),projection transformation
設照相機的三個引數,position e e e, gaze at direction g g g, up direction t t t。
檢視變換的最終目的是將照相機變換到原點,向上為 Y Y Y軸,朝 − Z -Z −Z方向看。為了使變換後照相的結果不變,並且注意有若相機和物體做同樣的移動,則“拍照”的結果不變。因此,檢視變換將相機和物體做同樣的變換。為後面的投影變換做準備。
變換過程
變換步驟:
- 將 e e e變換到原點
- 將 g g g旋轉到 − Z -Z −Z軸
- 將 t t t旋轉到 Y Y Y軸
- 將 ( g × t ) (g\times t) (g×t)旋轉到x軸
變換公式
M
v
i
e
w
=
R
v
i
e
w
T
v
i
e
w
O
r
i
g
i
n
v
i
e
w
=
(
x
g
×
t
x
t
x
g
y
g
×
t
y
t
y
g
z
g
×
t
z
t
z
g
0
0
0
)
F
i
n
a
l
v
i
e
w
=
(
1
0
0
0
1
0
0
0
−
1
0
0
0
)
F
i
n
a
l
v
i
e
w
=
M
v
i
e
w
O
r
i
g
i
n
v
i
e
w
M_{view}=R_{view}T_{view}\\ Origin_{view} = \begin{pmatrix} x_{g\times t} & x_{t} & x_{g} \\ y_{g\times t} & y_{t} & y_{g} \\ z_{g\times t} & z_{t} & z_{g} \\ 0 & 0 & 0 \end{pmatrix}\\ Final_{view} = \begin{pmatrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & -1 \\ 0 & 0 & 0 \end{pmatrix}\\ Final_{view}=M_{view}Origin_{view}
Mview=RviewTviewOriginview=⎝⎜⎜⎛xg×tyg×tzg×t0xtytzt0xgygzg0⎠⎟⎟⎞Finalview=⎝⎜⎜⎛1000010000−10⎠⎟⎟⎞Finalview=MviewOriginview
T
v
i
e
w
T_{view}
Tview的目的是首先將
e
e
e移動到原點,表示如下
(
1
0
0
−
x
e
1
0
0
−
x
e
1
0
0
−
x
e
0
0
0
1
)
\begin{pmatrix} 1 & 0 & 0 & -x_{e} \\ 1 & 0 & 0 & -x_{e} \\ 1 & 0 & 0 & -x_{e} \\ 0 & 0 & 0 & 1 \end{pmatrix}
⎝⎜⎜⎛111000000000−xe−xe−xe1⎠⎟⎟⎞
為了求
R
v
i
e
w
R_{view}
Rview,可以從求
R
v
i
e
w
−
1
R_{view}^{-1}
Rview−1入手,因為
R
v
i
e
w
−
1
R_{view}^{-1}
Rview−1容易看出,如下所示
R
v
i
e
w
−
1
=
(
x
g
×
t
x
t
−
x
g
0
y
g
×
t
y
t
−
y
g
0
z
g
×
t
z
t
−
z
g
0
0
0
0
1
)
=
(
x
g
×
t
x
t
x
−
g
0
y
g
×
t
y
t
y
−
g
0
z
g
×
t
z
t
z
−
g
0
0
0
0
1
)
R_{view}^{-1}=\begin{pmatrix} x_{g\times t} & x_t & -x_g & 0\\ y_{g\times t} & y_t & -y_g & 0\\ z_{g\times t} & z_t & -z_g & 0\\ 0 & 0 & 0 & 1 \end{pmatrix} = \begin{pmatrix} x_{g\times t} & x_t & x_{ -g} & 0\\ y_{g\times t} & y_t & y_{-g} & 0\\ z_{g\times t} & z_t & z_{-g} & 0\\ 0 & 0 & 0 & 1 \end{pmatrix}
Rview−1=⎝⎜⎜⎛xg×tyg×tzg×t0xtytzt0−xg−yg−zg00001⎠⎟⎟⎞=⎝⎜⎜⎛xg×tyg×tzg×t0xtytzt0x−gy−gz−g00001⎠⎟⎟⎞
顯然
R
v
i
e
w
−
1
R_{view}^{-1}
Rview−1是正交矩陣,所以有
R
v
i
e
w
T
=
R
v
i
e
w
−
1
R_{view}^T=R_{view}^{-1}
RviewT=Rview−1
R
v
i
e
w
=
(
x
g
×
t
y
g
×
t
z
g
×
t
0
x
t
y
t
z
t
0
x
−
g
y
−
g
z
−
g
0
0
0
0
1
)
R_{view}=\begin{pmatrix} x_{g\times t} & y_{g\times t} & z_{g\times t} & 0\\ x_{t} & y_{t} & z_{t} & 0\\ x_{-g} & y_{-g} & z_{-g} & 0\\ 0 & 0 & 0 & 1 \end{pmatrix}
Rview=⎝⎜⎜⎛xg×txtx−g0yg×tyty−g0zg×tztz−g00001⎠⎟⎟⎞
projection transformation 投影變換
投影變換的目的是將3D物體變換到2D空間,按投影的結果可以分為正交投影和透視投影。
正交投影
擺好照相機的位置後,然後去掉各個點的 Z Z Z座標即可得到各個物體的正交投影結果。一般做法如下
- 定義立方體的空間 [ l , r ] × [ b , t ] × [ f , n ] [l,r]\times[b,t]\times[f,n] [l,r]×[b,t]×[f,n]( n n n更靠近 z z z的正方向,所以有 f < n f < n f<n),然後試圖將其對映到“canonical cube”,以原點為中心,範圍是 [ − 1 , 1 ] 3 [-1,1]^3 [−1,1]3的立方體。
- 變換步驟:先平移其中心到原點,然後縮放
所以變換矩陣為
M
v
i
e
w
=
S
v
i
e
w
T
v
i
e
w
S
v
i
e
w
=
(
2
r
−
l
0
0
0
0
2
t
−
b
0
0
0
0
2
n
−
f
0
0
0
0
1
)
T
v
i
e
w
=
(
1
0
0
−
l
+
r
2
0
1
0
−
b
+
t
2
1
0
1
−
f
+
n
2
0
0
0
1
)
M_{view}=S_{view}T_{view}\\ S_{view}=\begin{pmatrix} \frac{2}{r-l} & 0 & 0 & 0\\ 0 & \frac{2}{t-b} & 0 & 0\\ 0 & 0 & \frac{2}{n-f} & 0 \\ 0 & 0 & 0 & 1 \end{pmatrix}\\ T_{view} = \begin{pmatrix} 1 & 0 & 0 & -\frac{l+r}{2}\\ 0 & 1 & 0 & -\frac{b+t}{2}\\ 1 & 0 & 1 & -\frac{f+n}{2}\\ 0 & 0 & 0 & 1 \end{pmatrix}
Mview=SviewTviewSview=⎝⎜⎜⎛r−l20000t−b20000n−f200001⎠⎟⎟⎞Tview=⎝⎜⎜⎛101001000010−2l+r−2b+t−2f+n1⎠⎟⎟⎞
投影變換
相比於正交變換,投影變換在計算機圖形學中更加常見,也比較符合人的視覺系統。
投影變換可以使得物體達到近大遠小的效果,平行線不會再保持平行,會相交於某一點。
投影變換的一般步驟為如下。投影變換是以視錐體去觀察物體,而正交變換以一個長方體去觀察物體。因此,我們希望先將視錐體變換到一個長方體,然後在進行正交投影即可得到透視投影的結果。如下所示
M
p
e
r
s
=
M
o
r
t
h
M
p
e
r
s
−
>
o
r
t
h
M_{pers}=M_{orth}M_{pers->orth}
Mpers=MorthMpers−>orth
視錐體的點的
X
,
Y
X,Y
X,Y軸座標變換到長方體空間的做法是通過三角形相似。公式如下
y
′
=
n
z
y
x
′
=
n
z
x
z
′
i
s
u
n
k
n
o
w
n
y'=\frac{n}{z}y\\x'=\frac{n}{z}x\\z'\ is\ unknown
y′=znyx′=znxz′isunknown
所以可表示如下
(
x
y
z
1
)
→
(
n
z
x
n
z
y
?
1
)
=
(
n
x
n
y
?
z
z
)
\begin{pmatrix} x\\y\\z\\1 \end{pmatrix} \to \begin{pmatrix} \frac{n}{z}x\\\frac{n}{z}y\\?\\1 \end{pmatrix} = \begin{pmatrix} nx\\ny\\?z\\z \end{pmatrix}
⎝⎜⎜⎛xyz1⎠⎟⎟⎞→⎝⎜⎜⎛znxzny?1⎠⎟⎟⎞=⎝⎜⎜⎛nxny?zz⎠⎟⎟⎞
用矩陣表示為
M
p
e
r
s
−
>
o
r
t
h
=
(
n
0
0
0
0
n
0
0
?
?
?
?
0
0
1
0
)
M_{pers->orth}=\begin{pmatrix} n & 0 & 0 & 0\\ 0 & n & 0 & 0\\ ? & ? & ? & ?\\ 0 & 0 & 1 & 0 \end{pmatrix}
Mpers−>orth=⎝⎜⎜⎛n0?00n?000?100?0⎠⎟⎟⎞
由於近平面和遠平面上的點的
Z
Z
Z軸座標依然為
n
,
f
n,f
n,f所以,矩陣可以簡化為
M
p
e
r
s
−
>
o
r
t
h
=
(
n
0
0
0
0
n
0
0
0
0
A
B
0
0
1
0
)
M_{pers->orth}=\begin{pmatrix} n & 0 & 0 & 0\\ 0 & n & 0 & 0\\ 0 & 0 & A & B\\ 0 & 0 & 1 & 0 \end{pmatrix}
Mpers−>orth=⎝⎜⎜⎛n0000n0000A100B0⎠⎟⎟⎞
同時,有以下兩個條件
(
0
0
n
1
)
→
(
0
0
n
2
n
)
,
(
0
0
f
1
)
→
(
0
0
f
2
f
)
\begin{pmatrix} 0\\ 0 \\ n\\ 1 \end{pmatrix} \to \begin{pmatrix} 0\\ 0 \\ n^2\\ n \end{pmatrix}, \begin{pmatrix} 0\\ 0 \\ f\\ 1 \end{pmatrix} \to \begin{pmatrix} 0\\ 0 \\ f^2\\ f \end{pmatrix}
⎝⎜⎜⎛00n1⎠⎟⎟⎞→⎝⎜⎜⎛00n2n⎠⎟⎟⎞,⎝⎜⎜⎛00f1⎠⎟⎟⎞→⎝⎜⎜⎛00f2f⎠⎟⎟⎞
所以有
A
n
+
B
=
n
2
A
f
+
B
=
f
2
.
An+B=n^2\\ Af+B=f^2.
An+B=n2Af+B=f2.
解得
A
=
n
+
f
,
B
=
−
n
f
A = n+f,B=-nf
A=n+f,B=−nf
所以有
M
p
e
r
s
−
>
o
r
t
h
=
(
n
0
0
0
0
n
0
0
0
0
n
+
f
−
n
f
0
0
1
0
)
M_{pers->orth}=\begin{pmatrix} n & 0 & 0 & 0\\ 0 & n & 0 & 0\\ 0 & 0 & n+f & -nf\\ 0 & 0 & 1 & 0 \end{pmatrix}
Mpers−>orth=⎝⎜⎜⎛n0000n0000n+f100−nf0⎠⎟⎟⎞