仿射变换
仿射变换 = 线性变换 + 平移
线性变换
缩放
x ′ = s x x y ′ = s y y x' = s_xx \space\space\space\space\space\space\space\space\space\space\space y' = s_yy x′=sxx y′=syy
[ x ′ y ′ ] = [ s x 0 0 s y ] [ x y ] \left[ \begin{matrix} x' \\ y' \end{matrix} \right]= \left[ \begin{matrix} s_x & 0 \\ 0 & s_y \end{matrix} \right] \left[ \begin{matrix} x \\ y \end{matrix} \right] [x′y′]=[sx00sy][xy]
错切变换
[
x
′
y
′
]
=
[
1
a
0
1
]
[
x
y
]
\left[ \begin{matrix} x' \\ y' \end{matrix} \right]= \left[ \begin{matrix} 1 & a \\ 0 & 1 \end{matrix} \right] \left[ \begin{matrix} x\\y \end{matrix} \right]
[x′y′]=[10a1][xy]
旋转
默认为绕原点逆时针旋转
有如下性质:
R − θ = [ c o s θ s i n θ − s i n θ c o s θ ] = R θ T = R θ − 1 R_{-\theta}=\left[\begin{matrix} cos\theta & sin\theta\\-sin\theta & cos\theta \end{matrix}\right]=R_\theta^{\space T}=R_\theta^{\space-1} R−θ=[cosθ−sinθsinθcosθ]=Rθ T=Rθ −1
像这种其逆矩阵等于转置矩阵的矩阵,在数学上定义为正交矩阵。
平移
[ x ′ y ′ ] = [ x y ] + [ t x t y ] \left[ \begin{matrix} x' \\ y' \end{matrix} \right]= \left[ \begin{matrix} x\\y \end{matrix} \right]+ \left[ \begin{matrix} t_x\\t_y \end{matrix} \right] [x′y′]=[xy]+[txty]
齐次坐标
为了将两种变换统一表示,引入齐次坐标,增加一个维度w
故平移后:
点与向量的表示
2D point: (x, y, 1)T
2D vector: (x, y, 0)T 具有平移不变性
且满足:
vector ± vector = vector
point - point = vector
vector + point = point
point + point : 结果为两点的中点,因为在齐次坐标中:
[
x
y
w
]
\left[\begin{matrix}x\\y\\w\end{matrix}\right]
⎣⎡xyw⎦⎤ is a point
[
x
/
w
y
/
w
1
]
(
w
≠
0
)
\left[\begin{matrix}x/w\\y/w\\1\end{matrix}\right](w\not=0)
⎣⎡x/wy/w1⎦⎤(w=0)
逆变换
矩阵A经过M变换得到矩阵B,矩阵B则经过M-1 变换回到A
证明:
MA = B = MM-1 B
故 A = M-1 B
由于矩阵的乘法是不满足交换律的,故变换的顺序十分重要
如旋转45°后向x方向平移1个单位:(从右至左结合)
复杂的操作可分解成多次简单变换
如围绕点 c 旋转 α
变换矩阵为:
T
(
c
)
⋅
R
(
α
)
⋅
T
(
−
c
)
T(c)·R(α)·T(-c)
T(c)⋅R(α)⋅T(−c)
3D变换
use 4x4 matrix for affine transformations
旋转
由z到y旋转,
y
×
z
=
x
y\times z = x
y×z=x
由z到x旋转,因为
z
×
x
=
y
z\times x = y
z×x=y,和另外两种情况旋转方向不一致,所以写法上有差异。
由x到y旋转,
x
×
y
=
z
x \times y = z
x×y=z
绕轴n旋转α度 (n经过原点)
Viewing(观测) 变换
视角变换
一般将摄像机放在原点,y轴作为上方,看向-z。
通过变换矩阵
M
v
i
e
w
M_{view}
Mview将其变换到目标状态,需要以下步骤:
- 将 e 移至原点
- 旋转 g 至 -Z
- 旋转 t 至 Y
- 旋转 (g x t) 至 X
M v i e w = R v i e w T v i e w M_{view}=R_{view}T_{view} Mview=RviewTview
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 = [ 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}=\left[ \begin{matrix} x_{\hat{g}\times\hat{t}} & x_t & x_g & 0\\ y_{\hat{g}\times\hat{t}} & y_t & y_g & 0\\ z_{\hat{g}\times\hat{t}} & z_t & z_g & 0\\ 0 &0 &0&1 \end{matrix}\right] Rview−1=⎣⎢⎢⎡xg^×t^yg^×t^zg^×t^0xtytzt0xgygzg00001⎦⎥⎥⎤
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}=\left[ \begin{matrix} x_{\hat{g}\times\hat{t}} &y_{\hat{g}\times\hat{t}} &z_{\hat{g}\times\hat{t}}&0\\ x_t & y_t & z_t &0\\ x_g & y_g & z_g &0\\ 0&0&0&1 \end{matrix}\right] Rview=⎣⎢⎢⎡xg^×t^xtxg0yg^×t^ytyg0zg^×t^ztzg00001⎦⎥⎥⎤
投影
正交投影
简单理解为:
- 摄像机看向 -Z,上方为Y轴
- 丢弃模型的 Z 轴
- 缩放到 [ − 1 , 1 ] 2 [-1,1]^2 [−1,1]2 的矩形中
更正式的做法:
将任意立方体转化为标准立方体
透视投影
将视锥体压成一个立方体,再进行正交投影
根据相似三角形原理,可求得:
y
′
=
n
z
y
,
x
′
=
n
z
x
y'=\frac{n}{z}y, \space x'=\frac{n}{z}x
y′=zny, x′=znx
所以:
M
p
e
r
s
p
−
>
o
r
t
h
o
[
x
y
z
1
]
=
[
n
x
n
y
u
n
k
n
o
w
n
z
]
M_{persp->ortho}\left[ \begin{matrix}x\\y\\z\\1\end{matrix}\right]=\left[\begin{matrix}nx\\ny\\unknown\\z\end{matrix}\right]
Mpersp−>ortho⎣⎢⎢⎡xyz1⎦⎥⎥⎤=⎣⎢⎢⎡nxnyunknownz⎦⎥⎥⎤
M p e r s p − > o r t h o = [ n 0 0 0 0 n 0 0 ? ? ? ? 0 0 1 0 ] M_{persp->ortho}= \left[\begin{matrix} n&0&0&0\\ 0&n&0&0\\ ?&?&?&?\\ 0&0&1&0 \end{matrix}\right] Mpersp−>ortho=⎣⎢⎢⎡n0?00n?000?100?0⎦⎥⎥⎤
近平面上所有的点位置不会发生改变,带入z=n
[ x y n 1 ] = > [ n x n y n 2 n ] \left[\begin{matrix} x\\y\\n\\1\end{matrix}\right] =>\left[\begin{matrix} nx\\ny\\n^2\\n \end{matrix}\right] ⎣⎢⎢⎡xyn1⎦⎥⎥⎤=>⎣⎢⎢⎡nxnyn2n⎦⎥⎥⎤
可求出第三列为 [ 0 0 A B ] \left[\begin{matrix}0&0 &A &B\end{matrix}\right] [00AB]
且 A n + B = n 2 A n+B =n^2 An+B=n2
代入远平面中点,同理可得: A f + B = f 2 Af+B=f^2 Af+B=f2
求解可得: A = n + f , B = − n f A=n+f,\space B=-nf A=n+f, B=−nf
至此,求得:
M
p
e
r
s
p
−
>
o
r
t
h
o
=
[
n
0
0
0
0
n
0
0
0
0
n
+
f
−
n
f
0
0
1
0
]
M_{persp->ortho}= \left[\begin{matrix} n&0&0&0\\ 0&n&0&0\\ 0&0&n+f&-nf\\ 0&0&1&0 \end{matrix}\right]
Mpersp−>ortho=⎣⎢⎢⎡n0000n0000n+f100−nf0⎦⎥⎥⎤