此文是 齐次坐标与二维图形基本几何变换的矩阵推导 的衍生。理解二维的变换就能轻松推导三维的。
三维图形基本几何变换#
描述从点 (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