Computer Graphics 2d Transform

Posted by Packy on August 3, 2019

基本二维变换

§ Object in model coordinates

§ Transform into world coordinates

§ Represent points on object as vectors

§ Multiply by matrices

§ Demos with applet

§ 2D transformations: rotation, scale, shear 错切

§ Composing transforms

§ 3D rotations

§ Translation: Homogeneous Coordinates (next time) 齐次坐标,用来处理法向变换 和 遮挡

§ Transforming Normals (next time)

(Nonuniform) Scale  Scale(sx, )  0  s  0  O  o  s  O  s  x  z  s  szz  s-  transformation qame_iar

缩放矩阵是一个对角矩阵, 将 x y z 放大为 sx sy sz 倍

shear 错切

Shear  1  —a  Shear —

中间线是没有 shear的。

0 S k

x’ = x + ay y’ = y

Rotations  2D simple, 3D complicated. [Derivation? Examples?]  2D?  • Linear  Commutative  transformation qamejar

Rotations  2D simple, 3D complicated [Derivation? Examples?]  x'  2D?  • Linear  Commutative  cos O —sin O x  sin O coso Y  transformation qame.iar

旋转的顺序在二维空间无影响,在三维情况下会影响。

2D Rotations  P 二 co-o 「  0

公式的推导,主要是 三角函数 展开 coscos- sinsin

cosi = np.cos(151*np.pi/180)

sini = np.sin(151*np.pi/180)

rotation = np.matrix([[cosi,-sini],[sini, cosi]])

i = np.matrix([8.0,7.0])

np.dot(rotation,i.T)

matrix([[-10.390625 ],

​ [ -2.24386099]])

组合变换 Composing Transforms

Often want to combine transforms

§ E.g. first scale by 2, then rotate by 45 degrees

§ Advantage of matrix formulation: All still a matrix

§ Not commutative!! Order matters

多个变换。 矩阵相乘,还是一个矩阵

连接矩阵顺序是不可以交换的,矩阵乘法没有交换性

X2 = SXI  = Rx  = (RS)XI  transformation  qame_lar

先尺度,再旋转 , 是变尺度后旋转,

而旋转之后改变尺度,会出现错切的现象。

操作是不可逆的

Inverting Composite Transforms  Say I want to inven a combination of 3 transforms  • Option 1: Find composite matrix, invert  • Option 2: Invert each transform and swap order  • Obvious from properties of matrices, demo  M  M,M2M3  - M -31M ; 'MCI

如何进行逆变换

方式1: 先将矩阵相乘,然比如M 找M的逆

方式2 单独求解每一个矩阵的逆,但是要改变顺序

记住 R(A+B)=R(A)R(B)

在这里, θ=∑180i=1=181⋅1802=16290

需要将旋转角求和模360 (因为旋转x 度等价于旋转 x+360度)。 因此, 这里的旋转角就是 16290 mod 360=90 度

三维旋转

二维旋转有一个重要性质 R^T * R = I

旋转是正交的 Orthogonal

Rotations in 3D  Rotations about coordinate axes simple  cose —sing O  sin 9 cose 0  cose O sine  —sino O cos0  Always linear, orthogonal  • Rows/cols orthonormal  COS  sin 9  RTR I  —sin 8  cose

二维旋转可以堪称三维中 Z轴不变的旋转

• Rows of matrix are 3 unit vectors of new coord frame  • Can construct rotation matrix from 3 otthonormal vectors  u = x,x.yuY+zuz  yo  y,  yo  y.  z.  z  x

Geometric Interpretation 3D Rotations  xy y, z„zp  • Rows of matrix ark 3 unit vectors of new coord frame  • Can construct rotation matrix from 3 orthonormal vectors  • Effectively, projections of point into new coord frame  • New coord frame UK.AN taken to cartesian components xyz  • Inverse or transpose takes xyz cartesian to LMU

旋转矩阵的三行是新坐标系的三个单位向量

可以根据三个正交向量构建要给旋转矩阵

可以将点投影到新的坐标系

• Not Commutative (unlike in 2D)!!  • Rotate by x, then y is not same as y then x  Order of applying rotations does matter  • Follows from matrix multiplication not commutativ€  • RI * R2 is not the same as R2 RI  • Demo: HWI, order of right or up will matter

三维旋转是不可以交换顺序的

• Rotate by an angle e about arbitrary axis a  • Homework 1: must rotate eye, up direction  • Somewhat mathematical derivation but useful formula  • Problem setup: Rotate vector b by e about a  • Helpful to relate b to X, a to Z, verify does right thing  • For HWI, you probably just need final formula

向量b 围绕 坐标轴 a 旋转

b平行于a的分量是不变的

b垂直于a的分量 等于b - b   平行于a的

formula  Step 1: b has components parallel to a, perpendicular  Parallel component unchanged (rotating about an axis  leaves that axis unchanged after rotation, e.g. rot about z)  9  -C

Axis-An le formula  • Step 2: Define c orthogonal to both a and b  • Analogous to defining Y axis  • Use cross products and matrix formula for that

假设a为Z轴, B 当作x轴

x绕z转,会转到y上,向量c就是Y轴

C = a x b 叉乘

Step 3: With respect to the perpendicular comp of b  • Cos 8 of it remains unchanged  • Sin 6 of it projects onto vector c

Axis-Angle: Putting it together  (b \ a)R0T = (13 cose—aa7 cos9)b + (A • sine  (aar  (b a)qor  cos B + A' sine  Unchanged Component Perpendicular  cosine)  along a  (rotated comp)  (hence unchanged)

3D中绕任意轴的旋转

当然也能绕3D中的任意轴旋转。因为这里不考虑平移,可以假设旋转轴通过原点,这种旋转比绕坐标轴的旋转更复杂也更少见。用单位向量n描述旋转轴,和前面一样用θ描述旋转量。

让我们导出绕轴n旋转角度θ的矩阵,也就是说,我们想得到满足下面条件的矩阵 R(n, θ):

vR(n, θ) = v’

v是向量v绕轴n旋转后的向量。让我们看看能否用vn和θ表示v’。我们的想法是在垂直于n的平面中解决这个问题,那么这就转换为了一个简单的2D问题。为了做到这一点,将v分解为两个分量:**v   v**⊥,分别平行于n和垂直于n,并有v = **v   + v**⊥因为**v   平行于n,所以绕n旋转不会影响它。故只要计算出v**⊥n旋转后的 v就能得到 v’ =**v   ** + v‘。为了计算v‘,我们构造向量**v   ** ,v和临时向量w如图8.12所示:

img

上图展示了以下向量:

(1)**v   ** 是v平行于n的分量,另一种说法就是**v   ** 是vn上的投影,用v.nn计算。
(2)vv垂直于n的分量,因为 v = **v   + v**⊥所以 v = v - **v   **。vv投影到垂直于n的平面上的结果。
(3)w是同时垂直于**v   v**⊥的向量它的长度和v的相同wv同在垂直于n的平面中,wvn旋转90度的结果,由n x v可以得到。

现在,v’垂直于n的分量可以表示为:

img

img

img

来自 https://www.cnblogs.com/jiahenhe2/p/7954707.html