3d变换基础:平移、旋转、缩放(仿射变换)


是时候整理一波3d变换相关的知识了。模型的变换可以认为是空间中一堆点的变换,三维空间中,(x,y,z)可以认为是点,也可以认为是一个向量,因此,人们引入的第4个维度来标识是点还是向量,这个4维空间就叫仿射空间,具体可以参考CV及CG数学基础:空间,在仿射空间中,(x,y,z,0)标识向量,而(x,y,z,1)表示点。

平移、旋转、缩放

平移

平移没什么好说的,(x,y,z,1)向x,y,z轴分别移动a,b,c单位长度后变成(x+a, y+b, z+c, 1)。写成矩阵相乘的方式即为:

[x+ay+bz+c1]=[100a010b001c0001][xyz1]\left[ \begin{matrix} x+a \\ y+b \\ z+c \\ 1\\ \end{matrix} \right] = \left[ \begin{matrix} 1 & 0 & 0 & a\\ 0 & 1 & 0 & b \\ 0 & 0 & 1 & c \\ 0 & 0 & 0 & 1 \end{matrix} \right] \left[ \begin{matrix} x \\ y \\ z \\ 1\\ \end{matrix} \right]

旋转

对于旋转,任何一个旋转都可以认为是沿着x,y,z轴分别旋转 α\alpha, β\beta, γ\gamma 度数,所以选旋转就先讲沿着某个轴向的旋转。这里以逆着坐标轴正向方向看去的顺时针为旋转的正向,就是你的视线朝向和坐标轴正向是相反的,(⊙o⊙)…我还是画个图吧,下图就是沿着z轴旋转的正向了哈~

3d变换基础:平移、旋转、缩放(仿射变换)

1. 沿x轴旋转

3d变换基础:平移、旋转、缩放(仿射变换)
嗯!这里推一波公式,其实很简单,就是三角函数。
如上图左边,A点沿着x轴旋转一定角度变成A’,为了更容易看,右图是左图的左视图,记旋转的角度为θ\theta, 旋转后得到的A’与旋转中心连线与y轴正方向的夹角为α\alpha(图中的α\alpha是个负值),记A’与旋转中心连线的长度为L(A与旋转中心连线的长度也是L),那么,显而易见,有:

x=xy=Lcos(αθ)z=Lsin(αθ) \begin{aligned} x' =& x\\ y' =& L·cos(\alpha - \theta)\\ z' =& L·sin(\alpha - \theta) \end{aligned}

y=Lcosαz=Lsinα \begin{aligned} y =& -L·cos\alpha\\ z =& L·sin\alpha \end{aligned}
根据三角函数公式可以得到
y=Lcos(αθ)=L(cosαcosθ+sinαsinθ)=ycosθ+zsinθz=Lsin(αθ)=L(sinαcosθcosαsinθ)=zcosθ+ysinθ \begin{aligned} y' =& L·cos(\alpha - \theta) = L·(cos\alpha cos\theta + sin\alpha sin\theta) = -ycos\theta +zsin\theta\\ z' =& L·sin(\alpha - \theta) = L·(sin\alpha cos\theta - cos\alpha sin\theta) = zcos\theta +ysin\theta \end{aligned}
综上,有:
x=xy=ycosθ+zsinθz=zcosθ+ysinθ \begin{aligned} x' =& x\\ y' =& -ycos\theta +zsin\theta\\ z' =& zcos\theta +ysin\theta \end{aligned}
现在就可以写成漂亮的矩阵形式了:
[xyz1]=[10000cosθsinθ00sinθcosθ00001][xyz1]\left[ \begin{matrix} x' \\ y' \\ z' \\ 1\\ \end{matrix} \right] = \left[ \begin{matrix} 1 & 0 & 0 & 0\\ 0 & -cos\theta & sin\theta & 0 \\ 0 & sin\theta & cos\theta & 0 \\ 0 & 0 & 0 & 1 \end{matrix} \right] \left[ \begin{matrix} x \\ y \\ z \\ 1\\ \end{matrix} \right]

2. 沿y轴或者z轴旋转

推了x轴的,其他两个轴向其实原理都是一样的。
对于y轴,可以简单把y轴和x轴对调,也就是公式里的x,y对调,不过这样子的话,z轴的方向会反过来,所以再把z相关的加个符号就好了。
公式如下:
y=yx=xcosθzsinθz=zcosθ+xsinθ \begin{aligned} y' =& y\\ x' =& -xcos\theta -zsin\theta\\ -z' =& -zcos\theta +xsin\theta \end{aligned}
写成漂亮的矩阵形式就是:
[xyz1]=[cosθ0sinθ00100sinθ0cosθ00001][xyz1]\left[ \begin{matrix} x' \\ y' \\ z' \\ 1\\ \end{matrix} \right] = \left[ \begin{matrix} -cos\theta &0 & -sin\theta & 0 \\ 0 & 1 & 0 & 0\\ -sin\theta & 0 & cos\theta & 0 \\ 0 & 0 & 0 & 1 \end{matrix} \right] \left[ \begin{matrix} x \\ y \\ z \\ 1\\ \end{matrix} \right]

对于z轴,x,z互换,y置反,直接上公式:
z=zy=ycosθ+xsinθx=xcosθysinθ \begin{aligned} z' =& z\\ -y' =& ycos\theta +xsin\theta\\ x' =& xcos\theta -ysin\theta \end{aligned}
矩阵形式:
[xyz1]=[cosθsinθ00sinθcosθ0000100001][xyz1]\left[ \begin{matrix} x' \\ y' \\ z' \\ 1\\ \end{matrix} \right] = \left[ \begin{matrix} cos\theta & -sin\theta&0 & 0 \\ -sin\theta & -cos\theta & 0 & 0 \\ 0 & 0 & 1 & 0\\ 0 & 0 & 0 & 1 \end{matrix} \right] \left[ \begin{matrix} x \\ y \\ z \\ 1\\ \end{matrix} \right]

缩放

缩放感觉也没的说,直接上公示,下面公式表示沿着x,y,z轴分别缩放a,b,c倍:
[xyz1]=[a0000b0000c00001][xyz1]\left[ \begin{matrix} x' \\ y' \\ z' \\ 1\\ \end{matrix} \right] = \left[ \begin{matrix} a & 0 & 0 & 0\\ 0 & b & 0 & 0 \\ 0 & 0 & c & 0 \\ 0 & 0 & 0 & 1 \end{matrix} \right] \left[ \begin{matrix} x \\ y \\ z \\ 1\\ \end{matrix} \right]