【现代图形学基础 Games101】1. 2D Transformation

Transformation

[ x ′ y ′ ] = [ a b c d ] ⏞ T r a n s f o r   M a t r i x ⋅ [ x y ] T r a n s f o r m a t i o n = M a t r i x ⏟ M a t r i c e s   o f   t h e   s a m e   d i m e n s i o n . ⋅ O r i g i n . \begin{array}{l} {\Huge \begin{bmatrix} x^{'} \\ y^{'} \end{bmatrix} } &{\Huge=}& \overbrace{ \Huge \begin{bmatrix} a & b \\ c & d \end{bmatrix} }^{Transfor\ Matrix} & {\Huge\cdot} & {\Huge \begin{bmatrix} x \\ y \end{bmatrix} }\\ \\ \\ Transformation &{\Huge=}& \underbrace{\Large Matrix}_{Matrices\ of\ the\ same\ dimension.} & {\Huge\cdot} & Origin. \\ \end{array} xyTransformation==acbd Transfor MatrixMatrices of the same dimension. MatrixxyOrigin.

1. Scale

desc

将一个平面点集进行平面缩放,记在 x, y 方向缩放倍数为 s x   s y s_x \ s_y sx sy

Scale Matrix:

[ x ′ y ′ ] = [ s x 0 0 s y ] [ x y ] \begin{bmatrix} x^{'} \\ y^{'} \end{bmatrix} = {\Large \begin{bmatrix} s_x & 0 \\ 0 & s_y \end{bmatrix} } \begin{bmatrix} x \\ y \end{bmatrix} [xy]=sx00sy[xy]

2. Reflection

desc

将一个平面点集绕 y y y 轴进行水平翻转。

Reflection Matrix:
[ x ′ y ′ ] = [ − 1 0 0 1 ] [ x y ] \begin{bmatrix} x^{'} \\ y^{'} \end{bmatrix} = { \Large \begin{bmatrix} -1 & 0 \\ 0 & 1 \end{bmatrix} } \begin{bmatrix} x \\ y \end{bmatrix} [xy]=1001[xy]

3. Shear

desc

将一个平面点集进行水平方向的平面切变,记图形在坐标系中的高度为 h h h,切变平移最大距离为 a a a

在水平方向上的切变形变随 y y y 线性递增, y y y 达到 h h h 时形变距离最大,为 a a a

Shear Matrix:

[ x ′ y ′ ] = [ 1 a / h 0 1 ] [ x y ] \begin{bmatrix} x^{'} \\ y^{'} \end{bmatrix} = {\Large \begin{bmatrix} 1 & a/h \\ 0 & 1 \end{bmatrix} } \begin{bmatrix} x \\ y \end{bmatrix} [xy]=10a/h1[xy]

4. Rotate

desc

将一个平面点集绕原点进行旋转,记旋转角度 θ \theta θ​(认为逆时针旋转为正向)。

在点集中任意选一点,记偏转角 α \alpha α,长度 r r r,其参数方程:
{ x = r cos ⁡ α y = r sin ⁡ α \begin{cases} x = r\cos{\alpha} \\ y = r\sin{\alpha} \end{cases} {x=rcosαy=rsinα
旋转后得到:
{ x = r cos ⁡ ( α + θ )    = r cos ⁡ α cos ⁡ θ − r sin ⁡ α sin ⁡ θ y = r sin ⁡ ( α + θ )    = r sin ⁡ α cos ⁡ θ + r cos ⁡ α sin ⁡ θ \begin{cases} x = r\cos{(\alpha + \theta)} \\ \ \ = r\cos\alpha\cos\theta - r\sin\alpha\sin\theta \\ \\ y = r\sin{(\alpha + \theta)} \\ \ \ = r\sin\alpha\cos\theta + r\cos\alpha\sin\theta \end{cases} x=rcos(α+θ)  =rcosαcosθrsinαsinθy=rsin(α+θ)  =rsinαcosθ+rcosαsinθ
即:
{ x = x α cos ⁡ θ − y α sin ⁡ θ y = y α cos ⁡ θ + x α sin ⁡ θ \begin{cases} x = x\alpha\cos\theta - y\alpha\sin\theta \\ y = y\alpha\cos\theta + x\alpha\sin\theta \end{cases} {x=xαcosθyαsinθy=yαcosθ+xαsinθ

Rotate Matrix:
[ x ′ y ′ ] = [ cos ⁡ θ − sin ⁡ θ sin ⁡ θ cos ⁡ θ ] [ x y ] \begin{bmatrix} x^{'} \\ y^{'} \end{bmatrix} = { \Large \begin{bmatrix} \cos{\theta} & -\sin{\theta} \\ \sin{\theta} & \cos{\theta} \end{bmatrix} } \begin{bmatrix} x \\ y \end{bmatrix} [xy]=cosθsinθsinθcosθ[xy]

5. Translation

将一个平面点集进行平移,记在 x   y x\ y x y 方形上平移的距离 t x   t y t_x\ t_y tx ty

它可以写成:
[ x ′ y ′ ] = [ 1 0 0 1 ] [ x y ] + [ t x t y ] \begin{bmatrix} x^{'} \\ y^{'} \end{bmatrix} = { \Large \begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix} } \begin{bmatrix} x \\ y \end{bmatrix} + {\Large \begin{bmatrix} t_x \\ t_y \end{bmatrix} } [xy]=1001[xy]+txty
这是一个非线性变换(它的子空间本来是左零空间,现在是平移了的左零空间),如果想要通过线性变换来表示它,可以通过升维的方式解决。

下面我们介绍齐次坐标,以及将 二维平面上的非线性变换 转换为 高维空间的线性变换 的方法。

从直观的几何角度看,线性变换有以下三个特点:

  • 变换前是直线的,变换后依然是直线。
  • 直线比例保持不变。
  • 变换前是原点的,变换后依然是原点。

Homogeneous Coordinates

引入一个概念 —— Homogeneous Corrdinates(齐次坐标)。

给定欧氏平面上的一点 ( x ,   y ) (x,\ y) (x, y),对任意非零实数 ω \omega ω,三元组 ( x ⋅ ω ,   y ⋅ ω ,   ω ) (x\cdot \omega,\ y\cdot \omega,\ \omega) (xω, yω, ω) 即称之为该点的齐次坐标(或投影坐标)。

例如 ( 1 ,   2 ) (1,\ 2) (1, 2) 的两个齐次坐标为 [ ( 1 ,   2 ,   1 ) ,   ( 2 ,   4 ,   2 ) ] [(1,\ 2,\ 1),\ (2,\ 4,\ 2)] [(1, 2, 1), (2, 4, 2)]

推广一下,一个齐次坐标 ( x ,   y ,   ω ) (x,\ y,\ \omega) (x, y, ω) 在二维平面中对应的点为 ( x / ω ,   y / ω ,   1 )   ω ≠ 0 (x/\omega,\ y/\omega,\ 1)\ \omega \ne 0 (x/ω, y/ω, 1) ω=0

继续刚才的平移变换,依然记在 x   y x\ y x y 方形上平移的距离 t x   t y t_x\ t_y tx ty

我们取 ω = 1 \omega = 1 ω=1 时该点集 ( x ,   y ) (x,\ y) (x, y) 的齐次坐标 ( x ,   y ,   1 ) (x,\ y,\ 1) (x, y, 1),下面我们将对该其次坐标进行线性变化。

从空间抽象上理解,这个操作其实是将切变这种线性变换在第三个维度 ω \omega ω 上进行,但投影在二维平面的变换看起来像是平移变换,从而避开了直接在二维平面进行平移的非线性变换。
[ x ′ y ′ ω ′ ] = [ 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^{'} \\ \omega^{'} \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} xyω=100010txty1xy1=x+txy+ty1
这样就通过线性变换得到了一个平移变换的结果。

还有一种我自己琢磨出的一种平移变换方法,不需要引入齐次坐标,但仅 x, y != 0 时有效:

[ x ′ y ′ ] = [ 1 + t x / x 0 0 1 + t y / y ] [ x y ] = [ x + t x y + t y ]      x , y ≠ 0 \begin{bmatrix}x^{'} \\ y^{'}\end{bmatrix} = \begin{bmatrix}1 + t_x/x & 0 \\ 0 & 1 + t_y/y\end{bmatrix}\begin{bmatrix}x \\ y\end{bmatrix} = \begin{bmatrix} x + t_x \\ y + t_y\end{bmatrix}\ \ \ \ x,y \ne 0 [xy]=[1+tx/x001+ty/y][xy]=[x+txy+ty]    x,y=0

此外,他还存在一个缺陷,即对点集的每个点的平移矩阵都不相同,因为参数被放在平移矩阵中了。

我们当然期望我们将三维向量作为二维向量操作时不会引起其他影响,但由于引入了新维度,确实带来了一些问题:

  • 向量的平移不变性被破坏

    在二维平面中,
    v ⃗ ( 3 ,   7 ) = a l ⃗ ( 1 ,   2 ) + b ⃗ ( 2 ,   5 ) v ⃗ ( 3 ,   7 ) = c ⃗ ( 7 ,   4 ) − d ⃗ ( 4 ,   − 3 ) \begin{array}{l} \vec{v}(3,\ 7) & = & \vec{al}(1,\ 2) + \vec{b}(2,\ 5) \\ \vec{v}(3,\ 7) & = & \vec{c}(7,\ 4) - \vec{d}(4,\ -3) \end{array} v (3, 7)v (3, 7)==al (1, 2)+b (2, 5)c (7, 4)d (4, 3)
    但若使用齐次坐标,

    v 1 ⃗ ( 3 ,   7 ,   2 ) = a l ⃗ ( 1 ,   2 ,   1 ) + b ⃗ ( 2 ,   5 ,   1 ) v 2 ⃗ ( 3 ,   7 ,   0 ) = c ⃗ ( 7 ,   4 ,   1 ) − d ⃗ ( 4 ,   − 3 ,   1 ) v 1 ⃗ ≠ v 2 ⃗ \begin{array}{l} \vec{v_1}(3,\ 7,\ 2) & = & \vec{al}(1,\ 2,\ 1) + \vec{b}(2,\ 5,\ 1) \\ \vec{v_2}(3,\ 7,\ 0) & = & \vec{c}(7,\ 4,\ 1) - \vec{d}(4,\ -3,\ 1) \end{array} \\ \vec{v_1} \ne \vec{v_2} v1 (3, 7, 2)v2 (3, 7, 0)==al (1, 2, 1)+b (2, 5, 1)c (7, 4, 1)d (4, 3, 1)v1 =v2
    新向量延伸到了第三个维度的另两个平面 ω = 0 ,   2 \omega = 0,\ 2 ω=0, 2 中,如果方法得当,但在二维中本该保持平移不变性的向量却不相等了。

所以,在高维进行线性变换后,我们取:
{ ( x ,   y ,   1 ) i f   p o i n t , ( x ,   y ,   0 ) i f   v e c t o r . \begin{cases} (x,\ y,\ 1) & if\ point, \\ \\ (x,\ y,\ 0) & if\ vector. \end{cases} (x, y, 1)(x, y, 0)if point,if vector.
此时向量在二维平面维持了平移不变性。

Affine

平移变换是仿射变换的一个特例,如果我们在平移时加上在二维线性变换的参数,就成了仿射变换。
[ x ′ y ′ ] = [ a b c d ] [ x y ] + [ t x t y ] = > [ x ′ y ′ 1 ] = [ a b t x c d t y 0 0 1 ] [ x y 1 ] \begin{array}{l} & \begin{bmatrix} x^{'} \\ y^{'} \end{bmatrix} = {\Large \begin{bmatrix} \textcolor{orange}{a} & \textcolor{orange}{b}\\ \textcolor{orange}{c} & \textcolor{orange}{d} \end{bmatrix} } \begin{bmatrix} x \\ y \end{bmatrix} + {\Large \begin{bmatrix} \textcolor{#0aa}{t_x} \\ \textcolor{#0aa}{t_y} \end{bmatrix} }\\ \\ => & \begin{bmatrix} x^{'} \\ y^{'} \\ 1 \end{bmatrix} = {\Large \begin{bmatrix} \textcolor{orange}{a} & \textcolor{orange}{b} & \textcolor{#0aa}{t_x} \\ \textcolor{orange}{c} & \textcolor{orange}{d} & \textcolor{#0aa}{t_y} \\ 0 & 0 & 1 \end{bmatrix} } \begin{bmatrix} x \\ y \\ 1 \end{bmatrix} \end{array} =>[xy]=acbd[xy]+txtyxy1=ac0bd0txty1xy1
现在我们使用高维的线性变换统一了所有低维变换,现在我们来看一下之前介绍的低维线性变换该如何表示:(其中切变进行了简单推广)
1.   S c a l e S ( s x ,   s y ) = [ s x 0 0 0 s y 0 0 0 1 ] 2.   R e f l e c t i o n R = [ − 1 0 0 0 1 0 0 0 1 ] 3.   S h e a r S h ( h x ,   a y x ,   h y ,   a x y ) = [ 1 a y x / h y 0 a x y / h x 1 0 0 0 1 ] 4.   R o t a t e R ( θ ) = [ cos ⁡ θ − sin ⁡ θ 0 sin ⁡ θ cos ⁡ θ 0 0 0 1 ] 5.   T r a n s l a t i o n T ( t x ,   t y ) = [ 1 0 t x 0 1 t y 0 0 1 ] \begin{array}{lrcl} \large{1.\ Scale} \\ & \bf{S}(s_x,\ s_y) &=& {\Large \begin{bmatrix} s_x & 0 & 0 \\ 0 & s_y & 0 \\ 0 & 0 & 1 \end{bmatrix} } \\ \large{2.\ Reflection} \\ & \bf{R} &=& {\Large \begin{bmatrix} -1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{bmatrix} } \\ \large{3.\ Shear} \\ & \bf{Sh}(h_{x},\ a_{yx},\ h_{y},\ a_{xy}) &=& {\Large \begin{bmatrix} 1 & a_{yx}/h_{y} & 0 \\ a_{xy}/h_{x} & 1 & 0 \\ 0 & 0 & 1 \end{bmatrix} } \\ \large{4.\ Rotate} \\ & \bf{R}(\theta) &=& {\Large \begin{bmatrix} \cos\theta & -\sin\theta & 0 \\ \sin\theta & \cos\theta & 0 \\ 0 & 0 & 1 \end{bmatrix} } \\ \large{5.\ Translation} \\ & \bf{T}(t_x,\ t_y) &=& {\Large \begin{bmatrix} 1 & 0 & t_x \\ 0 & 1 & t_y \\ 0 & 0 & 1 \end{bmatrix} } \end{array} 1. Scale2. Reflection3. Shear4. Rotate5. TranslationS(sx, sy)RSh(hx, ayx, hy, axy)R(θ)T(tx, ty)=====sx000sy00011000100011axy/hx0ayx/hy10001cosθsinθ0sinθcosθ0001100010txty1

Order

对图形做变换,实际上是对图形中每个向量的矩阵做 左乘的初等变换,由于矩阵乘法的不可交换,我们必须注意顺序。

对于仿射变换,在高维的线性变换在低维包含两个信息,

  • 第一个是左上方的低维线性变换矩阵。
  • 第二个是右上方的平移矩阵。

由于矩阵乘法没有交换律,我们有必要搞清楚这种高维变换在低维的变换顺序,这个顺序就是先进行低维变换,再平移(或者称高维切变),这个结论在上一节的第一个公式中有所体现。

既然是初等变换,那么结果矩阵一定可逆。

例如变换矩阵 M = R π / 2 ⋅ T 1 , 0 M = R_{\pi/2} \cdot T_{1,0} M=Rπ/2T1,0​​,对向量 p a ⃗ = [ x y 1 ] T \vec{pa}=\begin{bmatrix}x & y & 1\end{bmatrix}^{\bf{T}} pa =[xy1]T​ 的变换 M ⋅ p ⃗ M \cdot \vec{p} Mp ​ 有逆变换 M − 1 ⋅ p ⃗ M^{-1} \cdot \vec{p} M1p ​ 其中 M − 1 = T 1 , 0 − 1 ⋅ R π / 2 − 1 M^{-1} = T_{1,0}^{-1} \cdot R_{\pi/2}^{-1} M1=T1,01Rπ/21​​.

Decomposing Complex Transforms

例如给定某点的旋转变换,我们可以先将该点作为中心,平移到坐标系原点处,然后进行旋转变换,然后再平移回去。
T x , y ⋅ R θ ⋅ T − x , − y ⋅ p ⃗ T_{x,y}\cdot R_{\theta}\cdot T_{-x,-y}\cdot\vec{p} Tx,yRθTx,yp

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

高厉害

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值