Games101----Transformation
1.为什么学习变换?
平移、旋转、缩放、(3D到2D)投影都是变换。
2. 二维变换
- 使用矩阵表示变换;
- 旋转(Rotation)、缩放(Scale)、错切(Shear)
2.1 缩放
假设变换前的点为
(
x
,
y
)
(x,y)
(x,y),变换后的点为
(
x
′
,
y
′
)
(x',y')
(x′,y′),则有:
x
′
=
s
x
y
′
=
s
y
\begin{aligned} x' &=sx \\ y' &=sy \end{aligned}
x′y′=sx=sy
用矩阵的形式可以写成:
[
x
′
y
′
]
=
[
s
0
0
s
]
[
x
y
]
\begin{bmatrix} x' \\ y' \end{bmatrix} =\begin{bmatrix} s & 0 \\ 0 & s \end{bmatrix}\begin{bmatrix} x \\ y \end{bmatrix}
[x′y′]=[s00s][xy].
如果
x
x
x和
y
y
y的缩放比例不一致,则写成:
[
x
′
y
′
]
=
[
s
x
0
0
s
y
]
[
x
y
]
\begin{bmatrix}x' \\ y'\end{bmatrix} = \begin{bmatrix} s_x & 0 \\ 0 & s_y \end{bmatrix}\begin{bmatrix} x \\ y \end{bmatrix}
[x′y′]=[sx00sy][xy].
如果是关于Y轴对称:
x
′
=
−
x
y
′
=
y
\begin{aligned} x' &=-x \\ y' &=y \end{aligned}
x′y′=−x=y
写成矩阵形式为:
[
x
′
y
′
]
=
[
−
1
0
0
1
]
[
x
y
]
\begin{bmatrix}x' \\ y'\end{bmatrix} = \begin{bmatrix} -1 & 0 \\ 0 & 1\end{bmatrix}\begin{bmatrix} x \\ y \end{bmatrix}
[x′y′]=[−1001][xy]
2.2 错切
由上图可知:
- 当 y = 0 y=0 y=0时, x x x的移动为 0 0 0;
- 当 y = 1 y=1 y=1时, x x x的移动为 a a a,
- 竖直方向没有移动。
因此有:
[ x ′ y ′ ] = [ 1 a 0 1 ] [ x y ] \begin{bmatrix}x' \\ y'\end{bmatrix}=\begin{bmatrix}1 & a \\ 0 & 1\end{bmatrix}\begin{bmatrix}x \\ y\end{bmatrix} [x′y′]=[10a1][xy]
2.3 旋转
默认情况下,关于原点
(
0
,
0
)
(0,0)
(0,0)旋转,逆时针方向为正。
2.3.1 推导旋转矩阵
如上图所示:假设正方形边长为1,逆时针旋转了
θ
\theta
θ角度,旋转矩阵为:
R
θ
=
[
a
b
c
d
]
;
R_\theta =\begin{bmatrix}a & b\\c & d\end{bmatrix};
Rθ=[acbd];
矩形的各个点有:
[
x
′
y
′
]
=
R
θ
[
x
y
]
;
\begin{bmatrix}x'\\y'\end{bmatrix}=R_\theta \begin{bmatrix}x\\y\end{bmatrix};
[x′y′]=Rθ[xy];
对旋转前的
(
1
,
0
)
(1,0)
(1,0)点有:
[
cos
θ
sin
θ
]
=
R
θ
[
1
0
]
;
\begin{bmatrix}\cos\theta\\\sin\theta\end{bmatrix}=R_\theta \begin{bmatrix}1\\0\end{bmatrix};
[cosθsinθ]=Rθ[10];
可得:
{
a
=
cos
θ
c
=
sin
θ
;
\begin{dcases}a=\cos\theta \\ c=\sin\theta \end{dcases};
{a=cosθc=sinθ;
对旋转前的
(
0
,
1
)
(0,1)
(0,1)点有:
[
−
sin
θ
cos
θ
]
=
R
θ
[
0
1
]
;
\begin{bmatrix}-\sin\theta\\\cos\theta\end{bmatrix}=R_\theta \begin{bmatrix}0\\1\end{bmatrix};
[−sinθcosθ]=Rθ[01];
可得:
{
b
=
−
sin
θ
d
=
cos
θ
;
\begin{dcases}b=-\sin\theta \\ d=\cos\theta \end{dcases};
{b=−sinθd=cosθ;
从而得出:
R
θ
=
[
cos
θ
−
sin
θ
sin
θ
cos
θ
]
.
R_\theta =\begin{bmatrix}\cos\theta & -\sin\theta\\\sin\theta & \cos\theta\end{bmatrix}.
Rθ=[cosθsinθ−sinθcosθ].
2.4 平移
如上图,有
x
′
=
x
+
t
x
y
′
=
y
+
t
y
;
\begin{aligned} x' &=x+t_x\\y'&=y+t_y\end{aligned};
x′y′=x+tx=y+ty;
可以写成
[
x
′
y
′
]
=
[
x
y
]
+
[
t
x
t
y
]
.
\begin{bmatrix} x' \\ y'\end{bmatrix}=\begin{bmatrix} x \\ y\end{bmatrix}+\begin{bmatrix} t_x \\ t_y\end{bmatrix}.
[x′y′]=[xy]+[txty].
3. 齐次坐标
3.1 为什么要有齐次坐标系
首先要知道什么是线性变换,我的理解是,可以写成下面这种形式的就叫线性变换:
B
=
M
A
,
B=MA,
B=MA,
其中,
A
和
B
A和B
A和B是
m
×
1
m\times1
m×1的列向量,
M
M
M是
m
×
m
m\times m
m×m的矩阵。
由上面二维变换可知,缩放、错切和旋转均属于线性变换,而平移不属于线性变换,我们不希望平移成为一个特例,为了统一形式,就有了齐次坐标。
在原来的基础上再增加一个坐标( w w w),于是:
- 2 D p o i n t : ( x , y , 1 ) T ; 2D \ point: (x,y,1)^T; 2D point:(x,y,1)T;
- 2 D v e c t o r : ( x , y , 0 ) T . 2D \ vector:(x,y,0)^T. 2D vector:(x,y,0)T.
平移就可以表达为线性变换:
[
x
′
y
′
w
′
]
=
[
1
0
t
x
0
1
t
y
0
0
1
]
[
x
y
1
]
=
[
x
+
t
x
y
+
t
y
1
]
\begin{bmatrix}x' \\ y' \\ w'\end{bmatrix}=\begin{bmatrix}1 & 0 & t_x \\ 0 & 1 & t_y\\ 0 & 0 & 1\end{bmatrix}\begin{bmatrix}x \\ y \\ 1\end{bmatrix}=\begin{bmatrix}x+t_x \\ y+t_y \\ 1\end{bmatrix}
⎣⎡x′y′w′⎦⎤=⎣⎡100010txty1⎦⎤⎣⎡xy1⎦⎤=⎣⎡x+txy+ty1⎦⎤
如果是一个向量平移呢?
应用上述变换,有:
[
x
′
y
′
w
′
]
=
[
1
0
t
x
0
1
t
y
0
0
1
]
[
x
y
0
]
=
[
x
y
0
]
\begin{bmatrix}x' \\ y' \\ w'\end{bmatrix}=\begin{bmatrix}1 & 0 & t_x \\ 0 & 1 & t_y\\ 0 & 0 & 1\end{bmatrix}\begin{bmatrix}x \\ y \\ 0\end{bmatrix}=\begin{bmatrix}x \\ y \\ 0\end{bmatrix}
⎣⎡x′y′w′⎦⎤=⎣⎡100010txty1⎦⎤⎣⎡xy0⎦⎤=⎣⎡xy0⎦⎤
由此可知,向量平移后不变,符合直觉。
3.2 点和向量的有效运算
- v e c t o r + v e c t o r = v e c t o r ; vector +vector=vector; vector+vector=vector;
- p o i n t − p o i n t = p o i n t ; point-point=point; point−point=point;
- p o i n t + v e c t o r = p o i n t ; point+vector=point; point+vector=point;
- p o i n t + p o i n t = ? ? ? point+point=??? point+point=???
注意: ( x y w ) \biggl(\begin{matrix}x\\y\\w\end{matrix}\biggr) (xyw)表示2D点 ( x / w y / w 1 ) , 其 中 w ≠ 0. \biggl(\begin{matrix}x/w\\y/w\\1\end{matrix}\biggr),其中w\neq0. (x/wy/w1),其中w=0.
3.3 仿射变换(Affine Transformations)
- 仿射映射 = 线性映射 + 平移;
[ x ′ y ′ ] = [ a b c d ] [ x y ] + [ t x t y ] . \begin{bmatrix}x' \\ y'\end{bmatrix}=\begin{bmatrix}a & b \\ c & d\end{bmatrix}\begin{bmatrix}x \\ y\end{bmatrix}+\begin{bmatrix}t_x \\ t_y\end{bmatrix}. [x′y′]=[acbd][xy]+[txty]. - 使用齐次坐标表示:
[ x ′ y ′ 1 ] = [ a b t x c d t y 0 0 1 ] [ x y 1 ] . \begin{bmatrix}x' \\ y' \\ 1\end{bmatrix}=\begin{bmatrix}a & b & t_x\\ c & d & t_y \\ 0 & 0 & 1\end{bmatrix}\begin{bmatrix}x \\ y \\ 1\end{bmatrix}. ⎣⎡x′y′1⎦⎤=⎣⎡ac0bd0txty1⎦⎤⎣⎡xy1⎦⎤.
3.4 2D变换的齐次坐标表示
- 缩放(Scale)
S ( s x , s y ) = ( s x 0 0 0 s y 0 0 0 1 ) S_{(s_x,s_y)}=\begin{pmatrix}s_x & 0 & 0 \\ 0 & s_y & 0 \\ 0 & 0 & 1\end{pmatrix} S(sx,sy)=⎝⎛sx000sy0001⎠⎞ - 旋转(Rotation)
R ( α ) = ( cos α − sin α 0 sin α cos α 0 0 0 1 ) R(\alpha)=\begin{pmatrix}\cos\alpha & -\sin\alpha & 0 \\ \sin\alpha & \cos\alpha & 0 \\ 0 & 0 & 1\end{pmatrix} R(α)=⎝⎛cosαsinα0−sinαcosα0001⎠⎞ - 平移(Translation)
T ( t x , t y ) = ( 0 0 t x 0 0 t y 0 0 1 ) T(t_x,t_y)=\begin{pmatrix}0 & 0 & t_x \\ 0 & 0 & t_y \\ 0 & 0 & 1\end{pmatrix} T(tx,ty)=⎝⎛000000txty1⎠⎞
3.5 逆变换
无论是矩阵还是几何意义上,
M
−
1
M^{-1}
M−1都是
M
M
M的逆变换。
3.6 组合变换
问题来了,上图从左到右是怎么变换的?先平移后旋转,还是先旋转后平移?
- 如果先平移后旋转
由上图可知,显然是不对的。 - 先旋转后平移
显然,可以得到正确的结果。由此可知,变换的顺序会影响变换的结果(矩阵乘法不满足交换律)。
另外要注意,矩阵应用从右到左。
T ( 1 , 0 ) ∙ R 45 [ x y 1 ] = [ 1 0 1 0 1 0 0 0 1 ] [ cos 45 ° − sin 45 ° 0 sin 45 ° cos 45 ° 0 0 0 1 ] [ x y 1 ] T_{(1,0)}\bull R_{45}\begin{bmatrix}x \\ y \\1 \end{bmatrix}=\begin{bmatrix}1 & 0 & 1 \\ 0 & 1 & 0 \\0 & 0 & 1 \end{bmatrix}\begin{bmatrix}\cos45\degree & -\sin45\degree & 0 \\ \sin45\degree & \cos45\degree & 0 \\0 & 0 &1 \end{bmatrix}\begin{bmatrix}x \\ y \\1 \end{bmatrix} T(1,0)∙R45⎣⎡xy1⎦⎤=⎣⎡100010101⎦⎤⎣⎡cos45°sin45°0−sin45°cos45°0001⎦⎤⎣⎡xy1⎦⎤
假设有一系列的变换矩阵 A 1 , A 2 , A 3 . . . . . . A_1,A_2,A_3...... A1,A2,A3......
- 组合变换通过矩阵乘法实现;
- 提前将
n
n
n个矩阵相乘得到一个矩阵,用该矩阵可以表示最终的变换结果。此法可以提高性能。
3.7 复合变换的分解
问题:如何围绕一个给定的点 c c c旋转?
- 先平移到圆点;
- 旋转;
- 平移回去。
矩阵表示为:
T ( − c ) R ( α ) T ( c ) T_{(-c)}R_{(\alpha)}T_{(c)} T(−c)R(α)T(c)
4. 三维变换
- 再次使用齐次坐标系:
- 3 D p o i n t : ( x , y , z , 1 ) ; 3D \ point:(x,y,z,1); 3D point:(x,y,z,1);
- 3 D v e c t o r : ( x , y , z , w ) . 3D \ vector:(x,y,z,w). 3D vector:(x,y,z,w).
- 通常, ( x , y , z , w ) ( w ≠ 0 ) (x,y,z,w)(w \neq 0) (x,y,z,w)(w=0)表示三维的点: ( x / w , y / w , z / w , 1 ) . (x/w,y/w,z/w,1). (x/w,y/w,z/w,1).
-
3
D
3D
3D变换:
[ x ′ y ′ z ′ 1 ] = [ a b c t x d e f t y g h j t z 0 0 0 1 ] [ x y z 1 ] \begin{bmatrix}x' \\ y' \\ z'\\ 1\end{bmatrix}=\begin{bmatrix}a & b & c & t_x \\ d & e & f & t_y \\ g & h & j & t_z\\0 & 0 & 0 & 1\end{bmatrix}\begin{bmatrix}x \\ y \\ z\\ 1\end{bmatrix} ⎣⎢⎢⎡x′y′z′1⎦⎥⎥⎤=⎣⎢⎢⎡adg0beh0cfj0txtytz1⎦⎥⎥⎤⎣⎢⎢⎡xyz1⎦⎥⎥⎤ - 缩放
S ( s x , s y , s z ) = [ s x 0 0 1 0 s y 0 0 0 0 s z 0 0 0 0 1 ] S_{(s_x,s_y,s_z)}=\begin{bmatrix}s_x & 0 & 0 & 1 \\ 0 & s_y & 0 & 0 \\ 0 & 0 & s_z & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} S(sx,sy,sz)=⎣⎢⎢⎡sx0000sy0000sz01001⎦⎥⎥⎤ - 平移
T ( t x , t y , t z ) = [ 1 0 0 t x 0 1 0 t y 0 0 1 t z 0 0 0 1 ] T_{(t_x,t_y,t_z)}=\begin{bmatrix}1 & 0 & 0 & t_x \\ 0 & 1 & 0 & t_y \\ 0 & 0 & 1 & t_z \\ 0 & 0 & 0 & 1 \end{bmatrix} T(tx,ty,tz)=⎣⎢⎢⎡100001000010txtytz1⎦⎥⎥⎤ - 旋转:分别绕X轴、Y轴和Z轴旋转
R x ( α ) = [ 1 0 0 0 0 cos α − sin α 0 0 sin α cos α 0 0 0 0 1 ] R_x(\alpha)=\begin{bmatrix}1 & 0 & 0 & 0 \\ 0 & \cos\alpha & -\sin\alpha & 0 \\ 0 & \sin\alpha & \cos\alpha & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} Rx(α)=⎣⎢⎢⎡10000cosαsinα00−sinαcosα00001⎦⎥⎥⎤
R y ( α ) = [ cos α 0 sin α 0 − sin α 0 cos α 0 0 0 1 0 0 0 0 1 ] R_y(\alpha)=\begin{bmatrix}\cos\alpha & 0 & \sin\alpha & 0 \\ -\sin\alpha & 0 & \cos\alpha & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} Ry(α)=⎣⎢⎢⎡cosα−sinα000000sinαcosα100001⎦⎥⎥⎤
R z ( α ) = [ cos α − sin α 0 0 sin α cos α 0 0 0 0 1 0 0 0 0 1 ] R_z(\alpha)=\begin{bmatrix}\cos\alpha & -\sin\alpha & 0 & 0 \\ \sin\alpha & \cos\alpha & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} Rz(α)=⎣⎢⎢⎡cosαsinα00−sinαcosα0000100001⎦⎥⎥⎤ - 由
R
x
、
R
y
、
R
z
R_x、R_y、R_z
Rx、Ry、Rz可以组成任意的3D旋转
R x y z ( α , β , γ ) = R x ( α ) R y ( β ) R z ( γ ) R_{xyz}(\alpha,\beta,\gamma)=R_x(\alpha)R_y(\beta)R_z(\gamma) Rxyz(α,β,γ)=Rx(α)Ry(β)Rz(γ) - ( α , β , γ ) (\alpha,\beta,\gamma) (α,β,γ)就是所谓的欧拉角
- 通常用于飞行模拟:
r
o
l
l
、
p
i
t
c
h
、
y
a
w
roll、pitch、yaw
roll、pitch、yaw.
- 罗德里格旋转公式
绕轴 n ⇀ \overrightharpoon{n} n旋转 α \alpha α角度
R ( n ⇀ , α ) = cos ( α ) I + ( 1 − cos ( α ) ) n ⇀ n ⇀ T + sin ( α ) [ 0 − n z n y n z 0 − n x − n y n x 0 ] . R(\overrightharpoon{n},\alpha)=\cos(\alpha)I+(1-\cos(\alpha))\overrightharpoon{n}\overrightharpoon{n}^T+\sin(\alpha)\begin{bmatrix}0 & -n_z & n_y \\ n_z & 0 & -n_x \\ -n_y & n_x & 0\end{bmatrix}. R(n,α)=cos(α)I+(1−cos(α))nnT+sin(α)⎣⎡0nz−ny−nz0nxny−nx0⎦⎤.
问题:如何证明?
参见这里。