此文是 齊次座標與二維圖形基本幾何變換的矩陣推導 的衍生。理解二維的變換就能輕鬆推導三維的。
三維圖形基本幾何變換#
描述從點 (x, y, z) 到 (x + dx, y+ dy, z + dz)
引入齊次座標,可表述為 (x, y, z, 1) 變形推導為 (x + dx, y+ dy, z + dz, 1)
已知:
Copy ( a 11 a 12 a 13 a 14 a 21 a 22 a 23 a 24 a 31 a 32 a 33 a 34 a 41 a 42 a 43 a 44 ) ( x y z 1 ) = ( x + d x y + d y z + d z 1 ) \begin{pmatrix}
a_{11} & a_{12} & a_{13} & a_{14} \\
a_{21} & a_{22} & a_{23} & a_{24} \\
a_{31} & a_{32} & a_{33} & a_{34} \\
a_{41} & a_{42} & a_{43} & a_{44} \\
\end{pmatrix}
\begin{pmatrix}
x \\
y \\
z \\
1
\end{pmatrix}
=
\begin{pmatrix}
x + d_x \\
y+ d_y \\
z + d_z \\
1
\end{pmatrix} a 11 a 21 a 31 a 41 a 12 a 22 a 32 a 42 a 13 a 23 a 33 a 43 a 14 a 24 a 34 a 44 x y z 1 = x + d x y + d y z + d z 1
得
Copy { a 11 x + a 12 y + a 13 z + a 14 = x + d x a 21 x + a 22 y + a 23 z + a 24 = y + d y a 31 x + a 32 y + a 33 z + a 34 = z + d z a 41 x + a 42 y + a 43 z + a 44 = 1 \begin{cases}
a_{11}x + a_{12}y + a_{13}z + a_{14} = x + d_x \\
a_{21}x + a_{22}y + a_{23}z + a_{24} = y+ d_y \\
a_{31}x + a_{32}y + a_{33}z + a_{34} = z + d_z \\
a_{41}x + a_{42}y + a_{43}z + a_{44} = 1 \\
\end{cases} ⎩ ⎨ ⎧ a 11 x + a 12 y + a 13 z + a 14 = x + d x a 21 x + a 22 y + a 23 z + a 24 = y + d y a 31 x + a 32 y + a 33 z + a 34 = z + d z a 41 x + a 42 y + a 43 z + a 44 = 1
解得
Copy ( 1 0 0 d x 0 1 0 d y 0 0 1 d z 0 0 0 1 ) \begin{pmatrix}
1 & 0 & 0 & dx \\
0 & 1 & 0 & dy \\
0 & 0 & 1 & dz \\
0 & 0 & 0 & 1
\end{pmatrix} 1 0 0 0 0 1 0 0 0 0 1 0 d x d y d z 1
描述從點 (x, y, z) 到 (sx*x, sy*y, sz*z),sx sy sz 為常量。
引入齊次座標,可表述為 (x, y, z, 1) 變形推導為 (sx*x, sy*y, sz*z, 1)
已知:
Copy ( a 11 a 12 a 13 a 14 a 21 a 22 a 23 a 24 a 31 a 32 a 33 a 34 a 41 a 42 a 43 a 44 ) ( x y z 1 ) = ( s x ∗ x s y ∗ y s z ∗ z 1 ) \begin{pmatrix}
a_{11} & a_{12} & a_{13} & a_{14} \\
a_{21} & a_{22} & a_{23} & a_{24} \\
a_{31} & a_{32} & a_{33} & a_{34} \\
a_{41} & a_{42} & a_{43} & a_{44} \\
\end{pmatrix}
\begin{pmatrix}
x \\
y \\
z \\
1
\end{pmatrix}
=
\begin{pmatrix}
s_x * x \\
s_y * y \\
s_z * z \\
1
\end{pmatrix} a 11 a 21 a 31 a 41 a 12 a 22 a 32 a 42 a 13 a 23 a 33 a 43 a 14 a 24 a 34 a 44 x y z 1 = s x ∗ x s y ∗ y s z ∗ z 1
得
Copy { a 11 x + a 12 y + a 13 z + a 14 = s x ∗ x a 21 x + a 22 y + a 23 z + a 24 = s y ∗ y a 31 x + a 32 y + a 33 z + a 34 = s z ∗ z a 41 x + a 42 y + a 43 z + a 44 = 1 \begin{cases}
a_{11}x + a_{12}y + a_{13}z + a_{14} = s_x * x \\
a_{21}x + a_{22}y + a_{23}z + a_{24} = s_y * y \\
a_{31}x + a_{32}y + a_{33}z + a_{34} = s_z * z \\
a_{41}x + a_{42}y + a_{43}z + a_{44} = 1 \\
\end{cases} ⎩ ⎨ ⎧ a 11 x + a 12 y + a 13 z + a 14 = s x ∗ x a 21 x + a 22 y + a 23 z + a 24 = s y ∗ y a 31 x + a 32 y + a 33 z + a 34 = s z ∗ z a 41 x + a 42 y + a 43 z + a 44 = 1
解得
Copy ( 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} s x 0 0 0 0 s y 0 0 0 0 s z 0 0 0 0 1
對於三維的旋轉可以分解為三個矩陣(繞 x 軸旋轉、繞 y 軸旋轉、繞 z 軸旋轉)的乘積
繞 z 軸旋轉 —— Rz (β)#
還記得我們對二維旋轉的推導嗎?我們在 x, y 軸構成的平面上進行旋轉時其實也可以視為是在繞著 z 軸的旋轉。那麼其三維的旋轉其實就很好理解了,無非就是增加一個固定不變的維度罷了。
即:
Copy ( a 11 a 12 a 13 a 14 a 21 a 22 a 23 a 24 a 31 a 32 a 33 a 34 a 41 a 42 a 43 a 44 ) ( x y z 1 ) = ( x c o s β − y s i n β y c o s β + x s i n β z 1 ) \begin{pmatrix}
a_{11} & a_{12} & a_{13} & a_{14} \\
a_{21} & a_{22} & a_{23} & a_{24} \\
a_{31} & a_{32} & a_{33} & a_{34} \\
a_{41} & a_{42} & a_{43} & a_{44} \\
\end{pmatrix}
\begin{pmatrix}
x \\
y \\
z \\
1
\end{pmatrix}
=
\begin{pmatrix}
xcosβ - ysinβ \\
ycosβ + xsinβ \\
z \\
1
\end{pmatrix} a 11 a 21 a 31 a 41 a 12 a 22 a 32 a 42 a 13 a 23 a 33 a 43 a 14 a 24 a 34 a 44 x y z 1 = x cos β − ys in β ycos β + x s in β z 1
得:
Copy { a 11 x + a 12 y + a 13 z + a 14 = x c o s β − y s i n β a 21 x + a 22 y + a 23 z + a 24 = y c o s β + x s i n β a 31 x + a 32 y + a 33 z + a 34 = z a 41 x + a 42 y + a 43 z + a 44 = 1 \begin{cases}
a_{11}x + a_{12}y + a_{13}z + a_{14} = xcosβ - ysinβ \\
a_{21}x + a_{22}y + a_{23}z + a_{24} = ycosβ + xsinβ \\
a_{31}x + a_{32}y + a_{33}z + a_{34} = z \\
a_{41}x + a_{42}y + a_{43}z + a_{44} = 1 \\
\end{cases} ⎩ ⎨ ⎧ a 11 x + a 12 y + a 13 z + a 14 = x cos β − ys in β a 21 x + a 22 y + a 23 z + a 24 = ycos β + x s in β a 31 x + a 32 y + a 33 z + a 34 = z a 41 x + a 42 y + a 43 z + a 44 = 1
解:
Copy R 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 ) R_{z}β =
\begin{pmatrix}
cosβ & -sinβ & 0 & 0 \\
sinβ & cosβ & 0 & 0 \\
0 & 0 & 1 & 0 \\
0 & 0 & 0 & 1
\end{pmatrix} R z β = cos β s in β 0 0 − s in β cos β 0 0 0 0 1 0 0 0 0 1
繞 x 軸旋轉 —— Rx (α)#
同理可得(固定 x)
Copy R 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 ) R_{x}α =
\begin{pmatrix}
1 & 0 & 0 & 0 \\
0 & cosα & -sinα & 0 \\
0 & sinα & cosα & 0 \\
0 & 0 & 0 & 1
\end{pmatrix} R x α = 1 0 0 0 0 cos α s in α 0 0 − s in α cos α 0 0 0 0 1
繞 y 軸旋轉 —— Ry (γ)#
同理可得(固定 y)
Copy R 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 ) R_{y}γ =
\begin{pmatrix}
cosγ & 0 & sinγ & 0 \\
0 & 1 & 0 & 0 \\
-sinγ & 0 & cosγ & 0 \\
0 & 0 & 0 & 1
\end{pmatrix} R y γ = cos γ 0 − s inγ 0 0 1 0 0 s inγ 0 cos γ 0 0 0 0 1
於是我們得到了三維的旋轉矩陣
Copy R = R x α R y γ R z β R = R_{x}αR_{y}γR_{z}β R = R x α R y γ R z β
但是看到這一定會產生這樣的疑問 —— 我該如何將繞某個軸的 θ 角分解為 α β γ 呢?這個疑問先留著,我好再水一篇文章。
只不過目前旋轉的軸都被限制在過原點這一前提下,如果旋轉軸並不過原點的話,得先把軸平移到過原點,隨後再旋轉,旋轉結束後再平移到原來的位置。任何複雜的變換都可以分解為數個簡單變換的合成。
沿著 X 軸切變#
即描述從 (x, y, z) 到 (x + my + n z, y, z), m,n 均為正切函數
Copy ( a 11 a 12 a 13 a 14 a 21 a 22 a 23 a 24 a 31 a 32 a 33 a 34 a 41 a 42 a 43 a 44 ) ( x y z 1 ) = ( x + m ∗ y + n ∗ z y z 1 ) \begin{pmatrix}
a_{11} & a_{12} & a_{13} & a_{14} \\
a_{21} & a_{22} & a_{23} & a_{24} \\
a_{31} & a_{32} & a_{33} & a_{34} \\
a_{41} & a_{42} & a_{43} & a_{44} \\
\end{pmatrix}
\begin{pmatrix}
x \\
y \\
z \\
1
\end{pmatrix}
=
\begin{pmatrix}
x + m*y + n*z \\
y \\
z \\
1
\end{pmatrix} a 11 a 21 a 31 a 41 a 12 a 22 a 32 a 42 a 13 a 23 a 33 a 43 a 14 a 24 a 34 a 44 x y z 1 = x + m ∗ y + n ∗ z y z 1
解得
Copy ( 1 m n 0 0 1 0 0 0 0 1 0 0 0 0 1 ) \begin{pmatrix}
1 & m & n & 0 \\
0 & 1 & 0 & 0 \\
0 & 0 & 1 & 0 \\
0 & 0 & 0 & 1 \\
\end{pmatrix} 1 0 0 0 m 1 0 0 n 0 1 0 0 0 0 1
沿著 Y 軸切變#
描述點 (x, y, z) 到點 (x, y + mx + n z, z)
求解就跳過了,相信聰明的大家一定看出來了行列項代表的意思了
Copy ( 1 0 0 0 m 1 n 0 0 0 1 0 0 0 0 1 ) \begin{pmatrix}
1 & 0 & 0 & 0 \\
m & 1 & n & 0 \\
0 & 0 & 1 & 0 \\
0 & 0 & 0 & 1 \\
\end{pmatrix} 1 m 0 0 0 1 0 0 0 n 1 0 0 0 0 1
沿著 Z 軸切變#
描述點 (x, y, z) 到點 (x, y, z + mx + n y)
Copy ( 1 0 0 0 0 1 0 0 m n 1 0 0 0 0 1 ) \begin{pmatrix}
1 & 0 & 0 & 0 \\
0 & 1 & 0 & 0 \\
m & n & 1 & 0 \\
0 & 0 & 0 & 1 \\
\end{pmatrix} 1 0 m 0 0 1 n 0 0 0 1 0 0 0 0 1