a ⃗ ⋅ b ⃗ = ∥ a ⃗ ∥ ∥ b ⃗ ∥ cos θ \vec{a} \cdot \vec{b}=\|\vec{a}\|\|\vec{b}\| \cos \theta a ⋅b =∥a ∥∥b ∥cosθ
cos θ = a ⃗ ⋅ b ⃗ ∥ a ⃗ ∥ ∥ b ⃗ ∥ \cos \theta=\frac{\vec{a} \cdot \vec{b}}{\|\vec{a}\|\|\vec{b}\|} cosθ=∥a ∥∥b ∥a ⋅b
当两个向量都是单位向量时, cos θ = a ^ ⋅ b ^ \cos \theta=\hat{a} \cdot \hat{b} cosθ=a^⋅b^
性质:
交换律: a ⃗ ⋅ b ⃗ = b ⃗ ⋅ a ⃗ \vec{a} \cdot \vec{b}=\vec{b} \cdot \vec{a} a ⋅b =b ⋅a
分配律: a ⃗ ⋅ ( b ⃗ + c ⃗ ) = a ⃗ ⋅ b ⃗ + a ⃗ ⋅ c ⃗ \vec{a} \cdot(\vec{b}+\vec{c})=\vec{a} \cdot \vec{b}+\vec{a} \cdot \vec{c} a ⋅(b +c )=a ⋅b +a ⋅c
结合律: ( k a ⃗ ) ⋅ b ⃗ = a ⃗ ⋅ ( k b ⃗ ) = k ( a ⃗ ⋅ b ⃗ ) (k \vec{a}) \cdot \vec{b}=\vec{a} \cdot(k \vec{b})=k(\vec{a} \cdot \vec{b}) (ka )⋅b =a ⋅(kb )=k(a ⋅b )
在笛卡尔坐标系下:
2维: 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{array}{l}x_a \\ y_a\end{array}\right) \cdot\left(\begin{array}{l}x_b \\ y_b\end{array}\right)=x_a x_b+y_a y_b a ⋅b =(xaya)⋅(xbyb)=xaxb+yayb
3维: a ⃗ ⋅ b ⃗ = ( x a y a z a ) ⋅ ( x b y b z b ) = x a x b + y a y b + z a z b \vec{a} \cdot \vec{b}=\left(\begin{array}{c}x_a \\ y_a \\ z_a\end{array}\right) \cdot\left(\begin{array}{c}x_b \\ y_b \\ z_b\end{array}\right)=x_a x_b+y_a y_b+z_a z_b a ⋅b = xayaza ⋅ xbybzb =xaxb+yayb+zazb
图形学中的应用:
找到光照和平面的夹角
找到一个向量在另一个向量上的投影: b ⃗ ⊥ = k a ^ = ∥ b ⃗ ∥ cos θ a ^ \vec{b}_{\perp}=k \hat{a} = \|\vec{b}\| \cos \theta \hat{a} b ⊥=ka^=∥b ∥cosθa^
可以计算方向性
两个方向有多接近
叉乘(cross/vector product)
叉乘后的向量垂直于 a 和b 的向量组成的平面 a × b = − b × a a \times b=-b \times a a×b=−b×a(不满足交换律)
大小: ∥ a × b ∥ = ∥ a ∥ b ∥ sin ϕ \|a \times b\|=\|a\| b \| \sin \phi ∥a×b∥=∥a∥b∥sinϕ
方向:右手定则(从a旋转到b的方向)(openGL API里是左手系)
性质:
x ⃗ × y ⃗ = + z ⃗ \vec{x} \times \vec{y}=+\vec{z} x ×y =+z
y ⃗ × x ⃗ = − z ⃗ \vec{y} \times \vec{x}=-\vec{z} y ×x =−z
y ⃗ × z ⃗ = + x ⃗ \vec{y} \times \vec{z}=+\vec{x} y ×z =+x
z ⃗ × y ⃗ = − x ⃗ \vec{z} \times \vec{y}=-\vec{x} z ×y =−x
z ⃗ × x ⃗ = + y ⃗ \vec{z} \times \vec{x}=+\vec{y} z ×x =+y
x ⃗ × z ⃗ = − y ⃗ \vec{x} \times \vec{z}=-\vec{y} x ×z =−y
a ⃗ × b ⃗ = − b ⃗ × a ⃗ \vec{a} \times \vec{b}=-\vec{b} \times \vec{a} a ×b =−b ×a
a ⃗ × a ⃗ = 0 → \vec{a} \times \vec{a}=\overrightarrow{0} a ×a =0 (长度为0的向量)
分配律: a ⃗ × ( b ⃗ + c ⃗ ) = a ⃗ × b ⃗ + a ⃗ × c ⃗ \vec{a} \times(\vec{b}+\vec{c})=\vec{a} \times \vec{b}+\vec{a} \times \vec{c} a ×(b +c )=a ×b +a ×c
结合律: a ⃗ × ( k b ⃗ ) = k ( a ⃗ × b ⃗ ) \vec{a} \times(k \vec{b})=k(\vec{a} \times \vec{b}) a ×(kb )=k(a ×b )
代数
a ⃗ × 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{array}{c}y_a z_b-y_b z_a \\ z_a x_b-x_a z_b \\ x_a y_b-y_a x_b\end{array}\right) a ×b = yazb−ybzazaxb−xazbxayb−yaxb
可以表示成矩阵的形式: 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^* b=\left(\begin{array}{ccc}0 & -z_a & y_a \\ z_a & 0 & -x_a \\ -y_a & x_a & 0\end{array}\right)\left(\begin{array}{l}x_b \\ y_b \\ z_b\end{array}\right) a ×b =A∗b= 0za−ya−za0xaya−xa0 xbybzb
应用:
判定左右
b 在 a 的左还是右?
a 叉乘 b
得到正值,则 b 在 a 左侧
得到负值,则 b 在 a 的右侧
判定内外(光栅化的基础,对内部的点进行着色)
在内部
点 ABC 逆时针排列
AB 叉乘 AP,正数,P在AB左侧
BC 叉乘 BP,正数,P在BC左侧
CA 叉乘 CP,正数,P在CA左侧
如果不确定点 ABC是顺时针还是逆时针
P 一定都在 AB、BC、CA 的左边或者右边,否则在外部
为0的时候,可以说在里面,也可以说在外面(corner case)
坐标系
任意三个向量,如果满足以下条件,则构成一个直角坐标系
∥ u ⃗ ∥ = ∥ v ⃗ ∥ = ∥ w ⃗ ∥ = 1 \|\vec{u}\|=\|\vec{v}\|=\|\vec{w}\|=1 ∥u ∥=∥v ∥=∥w ∥=1
u ⃗ ⋅ v ⃗ = v ⃗ ⋅ w ⃗ = u ⃗ ⋅ w ⃗ = 0 \vec{u} \cdot \vec{v}=\vec{v} \cdot \vec{w}=\vec{u} \cdot \vec{w}=0 u ⋅v =v ⋅w =u ⋅w =0
w ⃗ = u ⃗ × v ⃗ \vec{w}=\vec{u} \times \vec{v} \quad w =u ×v (right-handed)
任意一个向量,分解到这个直角坐标系中,用投影相加
p ⃗ = ( p ⃗ ⋅ u ⃗ ) u ⃗ + ( p ⃗ ⋅ v ⃗ ) v ⃗ + ( p ⃗ ⋅ w ⃗ ) w ⃗ \vec{p}=(\vec{p} \cdot \vec{u}) \vec{u}+(\vec{p} \cdot \vec{v}) \vec{v}+(\vec{p} \cdot \vec{w}) \vec{w} p =(p ⋅u )u +(p ⋅v )v +(p ⋅w )w
矩阵乘法
矩阵变换:移动、旋转、缩放、错切
性质:
没有交换律
结合律: ( A B ) C = A ( B C ) (A B) C=A(B C) (AB)C=A(BC)
分配律:
A ( B + C ) = A B + A C A(B+C)=A B+A C A(B+C)=AB+AC
( A + B ) C = A C + B C (A+B) C=A C+B C (A+B)C=AC+BC
性质: ( A B ) T = B T A T (A B)^T=B^T A^T (AB)T=BTAT
单位矩阵
对角阵
矩阵互逆:
A A − 1 = A − 1 A = I A A^{-1}=A^{-1} A=I AA−1=A−1A=I
性质: ( A B ) − 1 = B − 1 A − 1 (A B)^{-1}=B^{-1} A^{-1} (AB)−1=B−1A−1
用矩阵的形式表示向量的计算
点乘: a ⃗ ⋅ b ⃗ = a ⃗ T b ⃗ = ( x a y a z a ) ( x b y b z b ) = ( x a x b + y a y b + z a z b ) \vec{a} \cdot \vec{b}=\vec{a}^T \vec{b}=\left(\begin{array}{lll}x_a & y_a & z_a\end{array}\right)\left(\begin{array}{c}x_b \\ y_b \\ z_b\end{array}\right)=\left(x_a x_b+y_a y_b+z_a z_b\right) a ⋅b =a Tb =(xayaza) xbybzb =(xaxb+yayb+zazb)
叉乘: 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^* b=\left(\begin{array}{ccc}0 & -z_a & y_a \\ z_a & 0 & -x_a \\ -y_a & x_a & 0\end{array}\right)\left(\begin{array}{l}x_b \\ y_b \\ z_b\end{array}\right) a ×b =A∗b= 0za−ya−za0xaya−xa0 xbybzb (向量 a ⃗ \vec{a} a 的对偶矩阵 A ∗ A^* A∗)
[ x ′ y ′ ] = [ a b c d ] [ x y ] \left[\begin{array}{l}x^{\prime} \\ y^{\prime}\end{array}\right]=\left[\begin{array}{ll}a & b \\ c & d\end{array}\right]\left[\begin{array}{l}x \\ y\end{array}\right] [x′y′]=[acbd][xy]
用矩阵表示
缩放
[ x ′ y ′ ] = [ s x 0 0 s y ] [ x y ] = [ s x x s y y ] \left[\begin{array}{l}x^{\prime} \\ y^{\prime}\end{array}\right]=\left[\begin{array}{ll}s_x & 0 \\ 0 & s_y\end{array}\right]\left[\begin{array}{l}x \\ y\end{array}\right] = \left[\begin{array}{l}s_x x \\ s_y y\end{array}\right] [x′y′]=[sx00sy][xy]=[sxxsyy]
沿着 y 轴的反射:KaTeX parse error: Unknown column alignment: m at position 191: …[\begin{array}{m̲} -x \\ y \end…
斜切
[ x ′ y ′ ] = [ 1 a 0 1 ] [ x y ] \left[\begin{array}{l}x^{\prime} \\ y^{\prime}\end{array}\right]=\left[\begin{array}{ll}1 & a \\ 0 & 1\end{array}\right]\left[\begin{array}{l}x \\ y\end{array}\right] [x′y′]=[10a1][xy]
旋转
默认是绕着原点逆时针旋转
旋转矩阵: R θ = [ cos θ − sin θ sin θ cos θ ] \mathbf{R}_\theta=\left[\begin{array}{cc}\textcolor{#7785aa}{\cos \theta} & \textcolor{#e4da9a}{-\sin \theta} \\ \textcolor{#7785aa}{\sin \theta} & \textcolor{#e4da9a}{\cos \theta} \end{array}\right] Rθ=[cosθsinθ−sinθcosθ]
齐次坐标
平移变换
平移变换不属于线性变换!!!
Q:如何用一种统一的方法来表示?
A:二维的向量增加一个维度,变成三维的(引入齐次变换)
二维的点: ( x , y , 1 ) T \left(\mathbf{x}, \mathbf{y}, \textcolor{red}{1}\right)^{\mathrm{T}} (x,y,1)T
二维的向量: ( x , y , 0 ) T \left(\mathbf{x}, \mathbf{y}, \textcolor{red}{0}\right)^{\mathrm{T}} (x,y,0)T - 向量具有平移不变性
验证其有效性:
向量 + 向量 = 向量
点 - 点 = 向量
点 + 向量 = 点
点 + 点 = 两个点的中点(扩充的定义) - ( x y w ) \left(\begin{array}{l}x \\ y \\ w \end{array}\right) xyw 相当于 ( x w y w 1 ) \left(\begin{array}{l}\frac{x}{w} \\ \frac{y}{w} \\ 1 \end{array}\right) wxwy1 此处 w ≠ 0 w\neq 0 w=0
( x ′ y ′ ) = ( x y ) + ( t x t y ) \left(\begin{array}{l}x^{\prime} \\ y^{\prime}\end{array}\right)=\left(\begin{array}{l}x \\ y\end{array}\right)+\left(\begin{array}{l}t_x \\ t_y\end{array}\right) (x′y′)=(xy)+(txty)
( x ′ y ′ w ′ ) = ( 1 0 t x 0 1 t y 0 0 1 ) ⋅ ( x y 1 ) = ( x + t x y + t y 1 ) \left(\begin{array}{c}x^{\prime} \\ y^{\prime} \\ w^{\prime}\end{array}\right)=\left(\begin{array}{llc}1 & 0 & t_x \\ 0 & 1 & t_y \\ 0 & 0 & 1\end{array}\right) \cdot\left(\begin{array}{l}x \\ y \\ 1\end{array}\right)=\left(\begin{array}{c}x+t_x \\ y+t_y \\ 1\end{array}\right) x′y′w′ = 100010txty1 ⋅ xy1 = x+txy+ty1
仿射变换
仿射变换 = (先) 线性变换 + (再) 平移变换
在二维仿射变换情况下,最后一行都是 0 0 1
( x ′ y ′ ) = ( a b c d ) ⋅ ( x y ) + ( t x t y ) \left(\begin{array}{l}x^{\prime} \\ y^{\prime}\end{array}\right)=\left(\begin{array}{ll}a & b \\ c & d\end{array}\right) \cdot\left(\begin{array}{l}x \\ y\end{array}\right)+\left(\begin{array}{l}t_x \\ t_y\end{array}\right) (x′y′)=(acbd)⋅(xy)+(txty)
等价于
( x ′ y ′ 1 ) = ( a b t x c d t y 0 0 1 ) ⋅ ( x y 1 ) \left(\begin{array}{c}x^{\prime} \\ y^{\prime} \\ 1 \end{array}\right)=\left(\begin{array}{llc}a & b & t_x \\ c & d & t_y \\ 0 & 0 & 1\end{array}\right) \cdot\left(\begin{array}{l}x \\ y \\ 1\end{array}\right) x′y′1 = ac0bd0txty1 ⋅ xy1
缩放
S ( s x , s y ) = ( s x 0 0 0 s y 0 0 0 1 ) S(s_x, s_y) = \left(\begin{array}{llc} s_x & 0 & 0 \\ 0 & s_y & 0 \\ 0 & 0 & 1\end{array}\right) S(sx,sy)= sx000sy0001
旋转
R ( α ) = ( cos α − sin α 0 sin α cos α 0 0 0 1 ) R(\alpha) = \left(\begin{array}{llc} \cos\alpha & -\sin\alpha & 0 \\ \sin\alpha & \cos\alpha & 0 \\ 0 & 0 & 1\end{array}\right) R(α)= cosαsinα0−sinαcosα0001
平移
T ( t x , t y ) = ( 1 0 t x 0 1 t y 0 0 1 ) T(t_x, t_y) = \left(\begin{array}{llc} 1 & 0 & t_x \\ 0 & 1 & t_y \\ 0 & 0 & 1\end{array}\right) T(tx,ty)= 100010txty1
组合变换
平移、旋转、缩放变换可以组合起来
变换的顺序很重要,不能调换(矩阵的乘法不满足交换律)
e.g. T ( 1 , 0 ) ⋅ R 45 ≠ R 45 ⋅ T ( 1 , 0 ) T_{(1,0)} \cdot R_{45} \neq R_{45} \cdot T_{(1,0)} T(1,0)⋅R45=R45⋅T(1,0)
可以通过矩阵的乘法来实现组合变换,从右到左的操作
A n ( … A 2 ( A 1 ( x ) ) ) = A n ⋯ A 2 ⋅ A 1 ⋅ ( x y 1 ) A_n\left(\ldots A_2\left(A_1(\mathbf{x})\right)\right)=\mathbf{A}_n \cdots \mathbf{A}_2 \cdot \mathbf{A}_1 \cdot\left(\begin{array}{l}x \\ y \\ 1\end{array}\right) An(…A2(A1(x)))=An⋯A2⋅A1⋅ xy1
e.g. 先旋转 45度,再平移 (1, 0)
T ( 1 , 0 ) ⋅ R 45 [ x y 1 ] = [ 1 0 1 0 1 0 0 0 1 ] [ cos 4 5 ∘ − sin 4 5 ∘ 0 sin 4 5 ∘ cos 4 5 ∘ 0 0 0 1 ] [ x y 1 ] = [ cos 4 5 ∘ − sin 4 5 ∘ 1 sin 4 5 ∘ cos 4 5 ∘ 0 0 0 1 ] [ x y 1 ] T_{(1,0)} \cdot R_{45}\left[\begin{array}{c}x \\ y \\ 1\end{array}\right]=\left[\begin{array}{lll}1 & 0 & 1 \\ 0 & 1 & 0 \\ 0 & 0 & 1\end{array}\right]\left[\begin{array}{ccc}\cos 45^{\circ} & -\sin 45^{\circ} & 0 \\ \sin 45^{\circ} & \cos 45^{\circ} & 0 \\ 0 & 0 & 1\end{array}\right]\left[\begin{array}{l}x \\ y \\ 1\end{array}\right] = \left[\begin{array}{ccc}\cos 45^{\circ} & -\sin 45^{\circ} & 1 \\ \sin 45^{\circ} & \cos 45^{\circ} & 0 \\ 0 & 0 & 1\end{array}\right]\left[\begin{array}{l}x \\ y \\ 1\end{array}\right] T(1,0)⋅R45 xy1 = 100010101 cos45∘sin45∘0−sin45∘cos45∘0001 xy1 = cos45∘sin45∘0−sin45∘cos45∘0101 xy1
矩阵有结合律 => 可以用 3x3 的矩阵表示很复杂的变换
矩阵可以分解的好处:
给定一个点,如何绕着它进行旋转???
解决方法:
把点从中心平移到原点位置
旋转
平移回来
矩阵的表示: T ( c ) × R ( α ) × T ( − c ) T(c)\times R(\alpha) \times T(-c) T(c)×R(α)×T(−c)(注意从右到左的变换顺序)
逆变换
变回来,相当于乘以一个矩阵的逆矩阵
旋转
已知旋转 θ \theta θ 角为:
R θ = ( cos θ − sin θ sin θ cos θ ) R_\theta=\left(\begin{array}{cc}\cos \theta & -\sin \theta \\ \sin \theta & \cos \theta\end{array}\right) Rθ=(cosθsinθ−sinθcosθ)
那么,旋转 − θ -\theta −θ 角
R − θ = ( cos θ sin θ − sin θ cos θ ) = R θ T R_{-\theta}=\left(\begin{array}{cc}\cos \theta & \sin \theta \\ -\sin \theta & \cos \theta\end{array}\right)=R_\theta^{\mathrm{T}} R−θ=(cosθ−sinθsinθcosθ)=RθT(数值上相同)
而根据定义,旋转 − θ -\theta −θ 角是旋转 θ \theta θ 角的逆变换:
R − θ = R θ − 1 R_{-\theta}=R_\theta^{\mathrm{-1}} R−θ=Rθ−1
正交矩阵:逆矩阵=转置矩阵
三维变换
用齐次坐标表示:
三维的点: ( x , y , z , 1 ) T \left(\mathbf{x}, \mathbf{y}, \mathbf{z}, \textcolor{red}{1}\right)^{\mathrm{T}} (x,y,z,1)T
三维的向量: ( x , y , z , 0 ) T \left(\mathbf{x}, \mathbf{y}, \mathbf{z},\textcolor{red}{0}\right)^{\mathrm{T}} (x,y,z,0)T - 向量具有平移不变性
( x , y , z , w ) (x, y, z, w) (x,y,z,w) 在 w ≠ 0 w\neq 0 w=0 时表示一个三维空间中的点,即 ( x w , y w , z w ) (\frac{\mathrm{x}}{ \mathrm{w}}, \frac{\mathrm{y}}{\mathrm{w}}, \frac{\mathrm{z}}{\mathrm{w}}) (wx,wy,wz)
用 4 × 4 4\times4 4×4的齐次坐标来表示仿射变换
( x ′ y ′ z ′ 1 ) = ( a b c t x d e f t y g h i t z 0 0 0 1 ) ⋅ ( x y z 1 ) \left(\begin{array}{l}x^{\prime} \\ y^{\prime} \\ z^{\prime} \\ 1\end{array}\right)=\left(\begin{array}{llll}a & b & c & t_x \\ d & e & f & t_y \\ g & h & i & t_z \\ 0 & 0 & 0 & 1\end{array}\right) \cdot\left(\begin{array}{l}x \\ y \\ z \\ 1\end{array}\right) x′y′z′1 = adg0beh0cfi0txtytz1 ⋅ xyz1
先应用线性变换,再加上平移
缩放
S ( s x , s y , s z ) = ( s x 0 0 0 0 s y 0 0 0 0 s z 0 0 0 0 1 ) S(s_x, s_y, s_z) =\left(\begin{array}{llll}s_x & 0 & 0 & 0 \\ 0 & s_y & 0 & 0 \\ 0 & 0 & s_z & 0 \\ 0 & 0 & 0 & 1\end{array}\right) S(sx,sy,sz)= sx0000sy0000sz00001
平移
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)=\left(\begin{array}{llll}1 & 0 & 0 & t_x \\ 0 & 1 & 0 & t_y \\ 0 & 0 & 1 & t_z \\ 0 & 0 & 0 & 1\end{array}\right) T(tx,ty,tz)= 100001000010txtytz1
旋转
齐次坐标
绕着 x 轴, y轴, z轴旋转
绕着 x 轴旋转 R x ( α ) = ( 1 0 0 0 0 cos α − sin α 0 0 sin α cos α 0 0 0 0 1 ) R_x(\alpha)=\left(\begin{array}{llll}1 & 0 & 0 & 0 \\ 0 & \cos\alpha & -\sin\alpha & 0 \\ 0 & \sin\alpha & \cos\alpha & 0 \\ 0 & 0 & 0 & 1\end{array}\right) Rx(α)= 10000cosαsinα00−sinαcosα00001
绕着 y 轴旋转 R y ( α ) = ( cos α 0 sin α 0 0 1 0 0 − sin α 0 cos α 0 0 0 0 1 ) R_y(\alpha)=\left(\begin{array}{llll}\cos\alpha & 0 & \sin\alpha & 0 \\ 0 & 1 & 0 & 0 \\ -\sin\alpha & 0 & \cos\alpha & 0 \\ 0 & 0 & 0 & 1\end{array}\right) Ry(α)= cosα0−sinα00100sinα0cosα00001
绕着 z 轴旋转 R z ( α ) = ( cos α − sin α 0 0 sin α cos α 0 0 0 0 1 0 0 0 0 1 ) R_z(\alpha)=\left(\begin{array}{llll}\cos\alpha & -\sin\alpha & 0 & 0 \\ \sin\alpha & \cos\alpha & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1\end{array}\right) Rz(α)= cosαsinα00−sinαcosα0000100001
欧拉角
三维旋转
R x y z ( α , β , γ ) = R x ( α ) R y ( β ) R z ( γ ) \mathbf{R}_{x y z}(\alpha, \beta, \gamma)=\mathbf{R}_x(\alpha) \mathbf{R}_y(\beta) \mathbf{R}_z(\gamma) Rxyz(α,β,γ)=Rx(α)Ry(β)Rz(γ)
roll
pitch
yaw
Rodirgues 旋转公式
绕着旋转轴 n n n (默认是过原点的轴)旋转 α \alpha α 角都可以变成 绕着 x, y, z 旋转
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 ) ⏟ N \mathbf{R}(\mathbf{n}, \alpha)=\cos (\alpha) \mathbf{I}+(1-\cos (\alpha)) \mathbf{n} \mathbf{n}^T+\sin (\alpha) \underbrace{\left(\begin{array}{ccc}0 & -n_z & n_y \\ n_z & 0 & -n_x \\ -n_y & n_x & 0\end{array}\right)}_{\mathbf{N}} R(n,α)=cos(α)I+(1−cos(α))nnT+sin(α)N 0nz−ny−nz0nxny−nx0
( 0 − n z n y n z 0 − n x − n y n x 0 ) ⏟ N \underbrace{\left(\begin{array}{ccc}0 & -n_z & n_y \\ n_z & 0 & -n_x \\ -n_y & n_x & 0\end{array}\right)}_{\mathbf{N}} N 0nz−ny−nz0nxny−nx0 表示叉乘
四元数
观测变换(viewing)
view/camera transformation 视图变换
从三维变成二维的图片
定义一个相机:
位置 e ⃗ \vec{e} e
看的方向(look-at) g ^ \hat{g} g^
向上的位置 t ^ \hat{t} t^ (假设是垂直于 look-at 的位置)
因为物体和相机是相对的,假设相机的位置固定
相机永远在原点 ( 0 , 0 , 0 ) (0,0,0) (0,0,0),往 − Z -Z −Z 方向看,向上是 Y Y Y
如何进行视图变换?
将相机的位置 e ⃗ \vec{e} e 平移到原点 ( 0 , 0 , 0 ) (0,0,0) (0,0,0)
把 g ^ \hat{g} g^ 转到 − Z -Z −Z 方向
把 t ^ \hat{t} t^ 转到 Y Y Y 方向
把 g ^ × t ^ \hat{g}\times\hat{t} g^×t^ 转到 X X X 方向
projection transformation 投影变换
正交投影 vs. 透视投影
正交投影:不会造成近大远小的视觉差
正交投影:认为相机是一个点
透视投影:认为相机是无限远
Orthographic projection 正交投影
如何把一个 [ l , r ] × [ b , t ] × [ f , n ] [\mathbf{l}, \mathrm{r}] \times[\mathrm{b}, \mathrm{t}] \times[\mathbf{f}, \mathbf{n}] [l,r]×[b,t]×[f,n] ( n > f n>f n>f)的物体变到相机坐标系中?
**相机固定在原点 ( 0 , 0 , 0 ) (0,0,0) (0,0,0),往 − Z -Z −Z 方向看,向上是 Y Y Y
把z轴丢掉,就都在一个平面上啦!
再把 xy 平面的图归一化到 [ − 1 , 1 ] [-1,1] [−1,1]
沿着 − Z -Z −Z 方向看
离人近 Z Z Z 值大
离人远 Z Z Z 值小
正交投影变换矩阵:先平移到原点,再旋转变换(注意后一个式子中最后一列和平移矩阵最后一列的缩放关系)
M ortho = [ 2 r − l 0 0 0 0 2 t − b 0 0 0 0 2 n − f 0 0 0 0 1 ] [ 1 0 0 − r + l 2 0 1 0 − t + b 2 0 0 1 − n + f 2 0 0 0 1 ] = [ 2 r − l 0 0 − r + l r − l 0 2 t − b 0 − t + b t − b 0 0 2 n − f − n + f n − f 0 0 0 1 ] M_{\text {ortho }}=\left[\begin{array}{cccc}\frac{2}{r-l} & 0 & 0 & 0 \\ 0 & \frac{2}{t-b} & 0 & 0 \\ 0 & 0 & \frac{2}{n-f} & 0 \\ 0 & 0 & 0 & 1\end{array}\right]\left[\begin{array}{cccc}1 & 0 & 0 & -\frac{r+l}{2} \\ 0 & 1 & 0 & -\frac{t+b}{2} \\ 0 & 0 & 1 & -\frac{n+f}{2} \\ 0 & 0 & 0 & 1\end{array}\right] = \left[\begin{array}{cccc}\frac{2}{r-l} & 0 & 0 & -\frac{r+l}{r-l} \\ 0 & \frac{2}{t-b} & 0 & -\frac{t+b}{t-b} \\ 0 & 0 & \frac{2}{n-f} & -\frac{n+f}{n-f} \\ 0 & 0 & 0 & 1\end{array}\right] Mortho = r−l20000t−b20000n−f200001 100001000010−2r+l−2t+b−2n+f1 = r−l20000t−b20000n−f20−r−lr+l−t−bt+b−n−fn+f1