1 为什么学习变换 Why study transformation
1.1 模型变换 Modeling
- 场景展示——描述摄像机的位置移动
- 机器人跳舞——物体的旋转
- 皮克斯开场动画台灯压扁字母I——物体的缩放
1.2 视图变换 Viewing
- 光栅化成像中涉及大量的视图变换
2 二维变换 2D Transformations
3B1B线代的本质(可视化理解线性变换):https://www.bilibili.com/video/BV1ns41167b9?spm_id_from=333.999.0.0
2.1 缩放变换矩阵 Scale Matrix
- 不锁定比例的 Non-Uniform
( x ′ y ′ ) = ( s x 0 0 s y ) ( x y ) \begin{pmatrix} { } { x ^ { \prime } } \\ { y ^ { \prime } } \end{pmatrix} = \begin{pmatrix} { } { s _ { x } } & { 0 } \\ { 0 } & { s _ { y } } \end{pmatrix} \begin{pmatrix} { } { x } \\ { y } \end{pmatrix} (x′y′)=(sx00sy)(xy)
2.2 对称变换矩阵 Reflection Matrix
- 水平翻转 Horizontal reflection:
( x ′ y ′ ) = ( − 1 0 0 1 ) ( x y ) \begin{pmatrix} { } { x ^ { \prime } } \\ { y ^ { \prime } } \end{pmatrix} = \begin{pmatrix} { } { - 1 } & { 0 } \\ { 0 } & { 1 } \end{pmatrix} \begin{pmatrix} { } { x } \\ { y } \end{pmatrix} (x′y′)=(−1001)(xy)
2.3 切变变换矩阵 Shear Matrix
- 水平方向位移在y=0处是0,在y=1处是a,竖直方向位移总是0
( x ′ y ′ ) = ( 1 a 0 1 ) ( x y ) \begin{pmatrix} { } { x ^ { \prime } } \\ { y ^ { \prime } } \end{pmatrix} = \begin{pmatrix} { } { 1 } & { a } \\ { 0 } & { 1 } \end{pmatrix} \begin{pmatrix} { } { x } \\ { y } \end{pmatrix} (x′y′)=(10a1)(xy)
2.4 旋转变换矩阵(默认绕原点、逆时针方向) Rotation Matrix
- 旋转的角度为θ
R θ = ( cos θ − sin θ sin θ cos θ ) R _ { \theta } = \begin{pmatrix} { } { \cos \theta } & { -\sin \theta } \\ { \sin \theta } & { \cos \theta } \end{pmatrix} Rθ=(cosθsinθ−sinθcosθ)
2.5 线性变换 Linear Transforms
- 使用相同维度的矩阵相乘,形式为坐标与矩阵相乘得到新的坐标
( x ′ y ′ ) = ( a b c d ) ( x y ) \begin{pmatrix} { } { x ^ { \prime } } \\ { y ^ { \prime } } \end{pmatrix} = \begin{pmatrix} { } { a } & { b } \\ { c } & { d } \end{pmatrix} \begin{pmatrix} { } { x } \\ { y } \end{pmatrix} (x′y′)=(acbd)(xy)
3 齐次坐标 Homogeneous Coordinates
3.1 为什么学习齐次坐标 Why homogeneous coordinates
- 平移变换不是线性变换,无法直接用矩阵形式表示
( x ′ y ′ ) = ( a b c d ) ( x y ) + ( t x t y ) \begin{pmatrix} { } { x ^ { \prime } } \\ { y ^ { \prime } } \end{pmatrix} = \begin{pmatrix} { } { a } & { b } \\ { c } & { d } \end{pmatrix} \begin{pmatrix} { } { x } \\ { y } \end{pmatrix} + \begin{pmatrix} { } { t _ { x } } \\ { t y } \end{pmatrix} (x′y′)=(acbd)(xy)+(txty)
- 我们不想把平移当作一个特殊的变换 But we don’t want translation to be a special case
- 是否有一种统一的方式能够表示所有变换,它的代价怎么样? Is there a unified way to represent all transformations? (and what’s the cost?)
3.2 通过齐次坐标解决问题 Solution: Homogenous Coordinates
3.2.1 添加第三个坐标 Add a third coordinate (w-coordinate)
2 D p o i n t = ( x , y , 1 ) T , 2 D v e c t o r = ( x , y , 0 ) T \rm{2D\,point}=(x,y,1)^T,\quad\rm{2D\,vector}=(x,y,0)^T 2Dpoint=(x,y,1)T,2Dvector=(x,y,0)T
3.2.2 平移的矩阵表示 Matrix representation of translations
( 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{pmatrix} { } { x ^ { \prime } } \\ { y ^ { \prime } } \\ { w ^ { \prime } } \end{pmatrix} = \begin{pmatrix} { } { 1 } & { 0 } & { t_x } \\ { 0 } & { 1 } & { t_y } \\ { 0 } & { 0 } & { 1 } \end{pmatrix}\cdot\begin{pmatrix} { } { x } \\ { y } \\ { 1 } \end{pmatrix} = \begin{pmatrix} { } { x + t_x } \\ { y + t_y } \\ { 1 } \end{pmatrix} ⎝⎛x′y′w′⎠⎞=⎝⎛100010txty1⎠⎞⋅⎝⎛xy1⎠⎞=⎝⎛x+txy+ty1⎠⎞
3.2.3 如果平移的是一个向量? What if you translate a vector?
- 向量具有平移不变性,经过平移变换后还是原向量
( x ′ y ′ w ′ ) = ( 1 0 t x 0 1 t y 0 0 1 ) ⋅ ( x y 0 ) = ( x y 0 ) \begin{pmatrix} { } { x ^ { \prime } } \\ { y ^ { \prime } } \\ { w ^ { \prime } } \end{pmatrix} = \begin{pmatrix} { } { 1 } & { 0 } & { t_x } \\ { 0 } & { 1 } & { t_y } \\ { 0 } & { 0 } & { 1 } \end{pmatrix}\cdot\begin{pmatrix} { } { x } \\ { y } \\ { 0 } \end{pmatrix} = \begin{pmatrix} { } { x } \\ { y } \\ { 0 } \end{pmatrix} ⎝⎛x′y′w′⎠⎞=⎝⎛100010txty1⎠⎞⋅⎝⎛xy0⎠⎞=⎝⎛xy0⎠⎞
3.2.4 齐次坐标的妙处
- 如果w-coordinate的结果是1或0则为有效操作,这样既能让平移变换形式统一,也保留了向量和点加减运算的可操作性
- 使用齐次坐标时,(x, y, w)表示平面内一点(x/w, y/w, 1), w≠0
- vector + vector = vector 0+0=0
- point – point = vector 1-1=0
- point + vector = point 1+0=1
- point + point = ??
point+point得到的结果为两个点连线的中点(将w-coordinate化为1)
3.2.5 仿射变换 Affine Transformations
- 仿射变换 = 线性变换 + 平移变换 Affine map = linear map + translation
( x ′ y ′ ) = ( a b c d ) ⋅ ( x y ) + ( t x t y ) \begin{pmatrix} { } { x ^ { \prime } } \\ { y ^ { \prime } } \end{pmatrix} = \begin{pmatrix} { } { a } & { b } \\ { c } & { d } \end{pmatrix} \cdot \begin{pmatrix} { } { x } \\ { y } \end{pmatrix} + \begin{pmatrix} { } { t_x } \\ { t_y } \end{pmatrix} (x′y′)=(acbd)⋅(xy)+(txty) - 使用齐次坐标 Using homogenous coordinates:
A f f i n e T r a n s f o r m a t i o n s : ( x ′ y ′ 1 ) = ( a b t x c d t y 0 0 1 ) ⋅ ( x y 1 ) \rm{Affine\;Transformations:}\begin{pmatrix} { } { x ^ { \prime } } \\ { y ^ { \prime } } \\ { 1 } \end{pmatrix} = \begin{pmatrix} { } { a } & { b } & { t _ { x } } \\ { c } & { d } & { t _ { y } } \\ { 0 } & { 0 } & { 1 } \end{pmatrix} \cdot \begin{pmatrix} { } { x } \\ { y }\\{1} \end{pmatrix} AffineTransformations:⎝⎛x′y′1⎠⎞=⎝⎛ac0bd0txty1⎠⎞⋅⎝⎛xy1⎠⎞
3.2.6 使用齐次坐标的2D变换 2D Transformations
S c a l e : S ( s x , s y ) = ( s x 0 0 0 s y 0 0 0 1 ) \rm{Scale:}S (s _ { x , } s _ { y } ) = \begin{pmatrix} {} { s _ { x } } & { 0 } & { 0 } \\ { 0 } & { s _ { y } } & { 0 } \\ { 0 } & { 0 } & { 1 } \end{pmatrix} Scale:S(sx,sy)=⎝⎛s