计算机图形学学习笔记(向量、矩阵、变换)
文章目录
前言
向量(Vector)运算、矩阵(Matrix)运算、和变换矩阵 (Transformation Marices) 在计算机图形学中非常重要,一切物体的缩放,旋转,位移,都可以通过变换矩阵作用得到。同时在投影 (projection) 变换的时候也有很多应用,本文将会介绍一些向量运算、矩阵运算和一些简单的变换矩阵
一、向量运算
1.向量点乘
几何:
a
⃗
⋅
b
⃗
\vec{a}\cdot\vec{b}
a⋅b =
∣
∣
a
⃗
∣
∣
||\vec{a}||
∣∣a∣∣ *
∣
∣
b
⃗
∣
∣
||\vec{b}||
∣∣b∣∣ *
cos
θ
\cos\theta
cosθ
代数:
a
⃗
⋅
b
⃗
=
(
x
a
y
a
)
⋅
(
x
b
y
b
)
=
x
a
x
b
+
y
a
y
b
\vec{a}\cdot\vec{b} = \left(\begin{matrix}x_a\\y_a\\\end{matrix}\right)\cdot\left(\begin{matrix}x_b\\y_b\\\end{matrix}\right)=x_ax_b+y_ay_b
a⋅b=(xaya)⋅(xbyb)=xaxb+yayb
∣
∣
a
⃗
∣
∣
||\vec{a}||
∣∣a∣∣ :
a
⃗
\vec{a}
a向量的模
cos
θ
\cos\theta
cosθ :
a
⃗
\vec{a}
a
b
⃗
\vec{b}
b向量的夹角
向量点乘符合交换、结合、分配律
2.向量叉乘
几何:
a
⃗
×
b
⃗
\vec{a}\times\vec{b}
a×b =
−
(
b
⃗
×
a
⃗
)
-(\vec{b}\times\vec{a})
−(b×a)
∣
∣
a
⃗
×
b
⃗
∣
∣
||\vec{a}\times\vec{b}||
∣∣a×b∣∣ =
∣
∣
a
⃗
∣
∣
||\vec{a}||
∣∣a∣∣ *
∣
∣
b
⃗
∣
∣
||\vec{b}||
∣∣b∣∣ *
sin
θ
\sin\theta
sinθ
叉乘获得垂直于
a
⃗
\vec{a}
a与
b
⃗
\vec{b}
b的向量方向(右手螺旋定则,四指旋转方向为:如果
a
⃗
×
b
⃗
\vec{a}\times\vec{b}
a×b 就从
a
⃗
\vec{a}
a旋转到
b
⃗
\vec{b}
b大拇指方向为叉乘向量的方向)
注意:GAMES101教学使用右手系,所以使用右手螺旋定则,Unity和UE引擎都使用左手系所以应变为左手定则
代数:
a
⃗
×
b
⃗
=
(
x
a
y
a
z
a
)
×
(
x
b
y
b
z
b
)
=
(
y
a
z
b
−
y
b
z
a
z
a
x
b
−
x
a
z
b
x
a
y
b
−
y
a
x
b
)
\vec{a}\times\vec{b} = \left(\begin{matrix}x_a\\y_a\\z_a\end{matrix}\right)\times \left(\begin{matrix}x_b\\y_b\\z_b\end{matrix}\right) = \left(\begin{matrix}y_az_b-y_bz_a\\z_ax_b-x_az_b\\x_ay_b-y_ax_b\end{matrix}\right)
a×b=⎝⎛xayaza⎠⎞×⎝⎛xbybzb⎠⎞=⎝⎛yazb−ybzazaxb−xazbxayb−yaxb⎠⎞
叉乘符合结合、分配律,但是不符合交换律(方向会反)
向量叉乘的应用:判断左右、判断内外
a ⃗ × b ⃗ \vec{a}\times\vec{b} a×b得正: a ⃗ \vec{a} a在 b ⃗ \vec{b} b右侧
A B ⃗ × A P ⃗ \vec{AB}\times\vec{AP} AB×AP与 B C ⃗ × B P ⃗ \vec{BC}\times\vec{BP} BC×BP与 C A ⃗ × C P ⃗ \vec{CA}\times\vec{CP} CA×CP方向相同则P在三角形ABC内
二、矩阵运算
1.矩阵相乘
一个M行N列的矩阵与一个N行P列的矩阵相乘获得一个M行P列矩阵
A(M
×
\times
× N)
×
\times
× B(N
×
\times
× P) = C(M
×
\times
× P)
C矩阵i行j列的元素是A矩阵第i行的行向量与B矩阵第j列的列向量的点乘
性质:没有交换律,有结合、分配律
一些公式:
(
A
B
)
T
=
B
T
A
T
(AB)^T=B^TA^T
(AB)T=BTAT
(
A
B
)
−
1
=
B
−
1
A
−
1
(AB)^{-1}=B^{-1}A^{-1}
(AB)−1=B−1A−1
向量叉乘用矩阵乘法形式表达(知道即可):
a
⃗
×
b
⃗
=
A
×
b
⃗
=
(
0
−
z
a
y
a
z
a
0
−
x
a
−
y
a
x
a
0
)
(
x
b
y
b
z
b
)
\vec{a}\times\vec{b}=A\times\vec{b}= \left(\begin{matrix}0&-z_a&y_a\\z_a&0&-x_a\\-y_a&x_a&0\end{matrix}\right)\left(\begin{matrix}x_b\\y_b\\z_b\end{matrix}\right)
a×b=A×b=⎝⎛0za−ya−za0xaya−xa0⎠⎞⎝⎛xbybzb⎠⎞
三、变换(Transform)
1.2D变换
3.1.1缩放(Scale)
(
x
y
)
×
(
s
0
0
s
)
=
(
x
1
y
1
)
\left(\begin{matrix}x\\y\\\end{matrix}\right)\times\left(\begin{matrix}s&0\\0&s\\\end{matrix}\right)=\left(\begin{matrix}x_1\\y_1\\\end{matrix}\right)
(xy)×(s00s)=(x1y1)
(
x
y
)
×
(
s
x
0
0
s
y
)
=
(
x
1
y
1
)
\left(\begin{matrix}x\\y\\\end{matrix}\right)\times\left(\begin{matrix}s_x&0\\0&s_y\\\end{matrix}\right)=\left(\begin{matrix}x_1\\y_1\\\end{matrix}\right)
(xy)×(sx00sy)=(x1y1)
水平翻转:
(
x
y
)
×
(
−
1
0
0
−
1
)
=
(
x
1
y
1
)
\left(\begin{matrix}x\\y\\\end{matrix}\right)\times\left(\begin{matrix}-1&0\\0&-1\\\end{matrix}\right)=\left(\begin{matrix}x_1\\y_1\\\end{matrix}\right)
(xy)×(−100−1)=(x1y1)
切变:
(
x
y
)
×
(
−
1
a
0
−
1
)
=
(
x
1
y
1
)
\left(\begin{matrix}x\\y\\\end{matrix}\right)\times\left(\begin{matrix}-1&a\\0&-1\\\end{matrix}\right)=\left(\begin{matrix}x_1\\y_1\\\end{matrix}\right)
(xy)×(−10a−1)=(x1y1)
理解:A点移动了a,B点移动了a/2,所以
x
1
=
x
+
a
y
x_1=x+ay
x1=x+ay
3.1.2旋转(Rotate)
(
x
y
)
×
(
cos
θ
−
sin
θ
sin
θ
cos
θ
)
=
(
x
1
y
1
)
\left(\begin{matrix}x\\y\\\end{matrix}\right)\times\left(\begin{matrix}\cos\theta&-\sin\theta\\\sin\theta&\cos\theta\\\end{matrix}\right)=\left(\begin{matrix}x_1\\y_1\\\end{matrix}\right)
(xy)×(cosθsinθ−sinθcosθ)=(x1y1)
R
−
θ
=
(
cos
θ
sin
θ
−
sin
θ
cos
θ
)
=
R
θ
T
R_{-\theta}=\left(\begin{matrix}\cos\theta&\sin\theta\\-\sin\theta&\cos\theta\\\end{matrix}\right)=R_{\theta}^T
R−θ=(cosθ−sinθsinθcosθ)=RθT
R
−
θ
=
R
θ
−
1
(
定
义
可
得
)
R_{-\theta}=R_{\theta}^{-1}(定义可得)
R−θ=Rθ−1(定义可得)
3.1.3线性变换(Linear Transforms)
线性变换=矩阵
(
x
y
)
×
(
a
b
c
d
)
=
(
x
1
y
1
)
\left(\begin{matrix}x\\y\\\end{matrix}\right)\times\left(\begin{matrix}a&b\\c&d\\\end{matrix}\right)=\left(\begin{matrix}x_1\\y_1\\\end{matrix}\right)
(xy)×(acbd)=(x1y1)
x
1
=
a
x
+
b
y
x_1 =ax+by
x1=ax+by
y
1
=
c
x
+
d
y
y_1 =cx+dy
y1=cx+dy
以上介绍的变换都是线性变换
3.1.4齐次坐标(Homogeneous coordinates)
平移:
(
x
y
)
+
(
t
x
t
y
)
=
(
x
1
y
1
)
\left(\begin{matrix}x\\y\\\end{matrix}\right)+\left(\begin{matrix}t_x\\t_y\\\end{matrix}\right)=\left(\begin{matrix}x_1\\y_1\\\end{matrix}\right)
(xy)+(txty)=(x1y1)
x
1
=
x
+
t
x
x_1 =x+t_x
x1=x+tx
y
1
=
y
+
t
y
y_1 =y+t_y
y1=y+ty
平移不能用传统的变换矩阵表示,所以平移不是线性变换
平移——>矩阵表示——>引入齐次坐标,增加一个新的维度(w-coordinate)即
点:
(
x
y
)
−
>
(
x
y
1
)
\left(\begin{matrix}x\\y\\\end{matrix}\right)->\left(\begin{matrix}x\\y\\1\end{matrix}\right)
(xy)−>⎝⎛xy1⎠⎞
向量:为什么向量是(x,y,0)?因为向量平移之后还是原向量
(
x
y
)
−
>
(
x
y
0
)
\left(\begin{matrix}x\\y\\\end{matrix}\right)->\left(\begin{matrix}x\\y\\0\end{matrix}\right)
(xy)−>⎝⎛xy0⎠⎞
表示平移:
(
x
1
y
1
w
1
)
=
(
1
0
t
x
0
1
t
y
0
0
1
)
(
x
y
1
)
=
(
x
+
t
x
y
+
t
y
1
)
\left(\begin{matrix}x_1\\y_1\\w_1\end{matrix}\right)=\left(\begin{matrix}1&0&t_x\\0&1&t_y\\0&0&1\end{matrix}\right)\left(\begin{matrix}x\\y\\1\end{matrix}\right)=\left(\begin{matrix}x+t_x\\y+t_y\\1\end{matrix}\right)
⎝⎛x1y1w1⎠⎞=⎝⎛100010txty1⎠⎞⎝⎛xy1⎠⎞=⎝⎛x+txy+ty1⎠⎞
简而言之,齐次坐标就是用N+1维来代表N维坐标。我们可以在一个2D笛卡尔坐标末尾加上一个额外的变量w来形成2D齐次坐标,因此,一个点(X,Y)在齐次坐标里面变成了(x,y,w),并且一定有:
X = x/w
Y = y/w
也就是说我们把齐次坐标转化为笛卡尔坐标的方法是前面n-1个坐标分量分别除以最后一个分量即可。
在转换的过程中,会发现(1, 2, 3), (2, 4, 6) 和(4, 8, 12)对应同一个笛卡尔坐标点 (1/3, 2/3),任何标量的乘积,例如(1a, 2a, 3a) 对应 笛卡尔空间里面的(1/3, 2/3) 。因此,这些点是“齐次的”,因为他们代表了笛卡尔坐标系里面的同一个点。换句话说,齐次坐标有规模不变性。
3.1.5仿射变换(Affine transform)
仿射变换 = 先线性变换 + 后平移
缩放、旋转、平移变换都属于仿射变换
使用齐次坐标:
(
x
1
y
1
w
1
)
=
(
a
b
t
x
c
d
t
y
0
0
1
)
(
x
y
1
)
\left(\begin{matrix}x_1\\y_1\\w_1\end{matrix}\right)=\left(\begin{matrix}a&b&t_x\\c&d&t_y\\0&0&1\end{matrix}\right)\left(\begin{matrix}x\\y\\1\end{matrix}\right)
⎝⎛x1y1w1⎠⎞=⎝⎛ac0bd0txty1⎠⎞⎝⎛xy1⎠⎞
3.1.6逆变换(Inverse transform)
3.1.7组合变换
为什么变换结果不同?
因为矩阵乘法不符合交换律
T
(
1
,
0
)
R
45
(
x
y
1
)
≠
R
45
T
(
1
,
0
)
(
x
y
1
)
T_{(1,0)}R_{45}\left(\begin{matrix}x\\y\\1\end{matrix}\right)\neq R_{45}T_{(1,0)}\left(\begin{matrix}x\\y\\1\end{matrix}\right)
T(1,0)R45⎝⎛xy1⎠⎞=R45T(1,0)⎝⎛xy1⎠⎞