【Computer Graphics】旋转变换:矩阵形式

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/LeeXxs/article/details/83107273

内容

  1. 2D中绕原点旋转
  2. 2D中绕任意点旋转
  3. 3D中绕坐标轴旋转
  4. 3D中绕任意轴旋转

1. 2D中绕原点旋转

在二维坐标系中,物体只能绕点旋转;且逆时针旋转经常(不是必须)被认为是正方向。

设物体在二维坐标系中,绕原点旋转θ度角,求旋转矩阵:

如下图,原坐标系基向量 p,q 绕原点旋转,得到新的基向量 p',q'。     

知道了旋转后基向量的值,就可以如下所示用基向量造旋转矩阵

R(\theta ) = \begin{bmatrix} p'&q' \end{bmatrix} = \begin{bmatrix} cos\theta & -sin\theta \\ sin\theta & cos\theta \end{bmatrix}

或者是:

R(\theta ) =\begin{bmatrix} p'\\q' \end{bmatrix} = \begin{bmatrix} cos\theta & sin\theta \\ -sin\theta & cos\theta \end{bmatrix}

旋转后的坐标为:

\small x = x_0 \cdot cos\theta - y_0 \cdot sin\theta

\small y = x_0 \cdot sin\theta + y_0 \cdot cos\theta


2. 2D中绕任意点旋转

已知物体绕原点的旋转矩阵,那么绕任意一点的旋转,可以分解为平移,旋转,反平移这三个过程,可以分别求变换矩阵。

设点P = (x,y)P_0 = (x_0,y_0),求P点绕P_0点旋转\theta度角的旋转矩阵:

  • 平移:将P点坐标平移(-x_0,-y_0),平移矩阵如下:
    \begin{bmatrix} 1& 0 & -x_0\\ 0 & 1 & -y_0\\ 0 & 0 & 1 \end{bmatrix}

  • 旋转:将平移后的P点绕原点旋转\theta度,旋转矩阵如下:
    \begin{bmatrix} cos\theta & -sin\theta \\ sin\theta & cos\theta \end{bmatrix}

  • 反平移:将旋转后的坐标平移回去,平移矩阵如下:
    \begin{bmatrix} 1& 0 & x_0\\ 0 & 1 & y_0\\ 0 & 0 & 1 \end{bmatrix}

综上,点P绕点P_0旋转\theta度得到的坐标点为:d

P'=\begin{bmatrix} x'\\y'\\1 \end{bmatrix} =\begin{bmatrix} 1& 0 & x_0\\ 0 & 1 & y_0\\ 0 & 0 & 1 \end{bmatrix}\cdot \begin{bmatrix} cos\theta & -sin\theta & 0 \\ sin\theta & cos\theta & 0\\ 0 & 0 &1 \end{bmatrix}\cdot \begin{bmatrix} 1& 0 & -x_0\\ 0 & 1 & -y_0\\ 0 & 0 & 1 \end{bmatrix} \cdot \begin{bmatrix} x\\ y \\ 1 \end{bmatrix}

=\begin{bmatrix} cos\theta & -sin\theta & -x_0\cdot cos\theta+y_0\cdot sin\theta+x_0 \\ sin\theta & cos\theta & -y_0\cdot cos\theta-x_0\cdot sin\theta+y_0\\ 0 & 0 &1 \end{bmatrix}\cdot \begin{bmatrix} x\\ y\\1 \end{bmatrix}

因此,P'点的坐标为:

\left\{\begin{matrix} x' = (x-x_0)cos\theta -(y-y_0)sin\theta+x_0 \\ y'= (x-x_0)sin\theta+ (y-y_0)cos\theta +y_0 \end{matrix}\right.

3. 3D中绕坐标轴旋转

在3D场景中,绕轴旋转而不是绕点旋转(轴是指旋转所绕的直线,不一定是笛卡尔坐标系的x,y,z轴)。这里暂不考虑平移,所以只讨论旋转轴穿过原点的情况。

和2D场景中相似,在讨论旋转之前,首先要明确旋转的正方向。这里需要区分左手坐标系右手坐标系,比如左手坐标系中,定义旋转正方向的法则称为左手法则:伸出左手,大拇指指向旋转轴的正方向,其余四指弯曲的方向就是旋转的正方向。右手坐标系中也有类似的右手法则,不过使用右手代替左手。

  • 先从绕x轴旋转开始,如图:

求出旋转后的基向量,可以得到矩阵:

R_x(\theta ) = \begin{bmatrix} p'\\q' \\ r' \end{bmatrix} = \begin{bmatrix} 1 & 0 & 0\\ 0& cos\theta & sin\theta \\ 0& -sin\theta & cos\theta \end{bmatrix}

  • 绕y轴与之类似,如图:

可得绕y轴旋转的矩阵:

R_y(\theta ) = \begin{bmatrix} p'\\q' \\ r' \end{bmatrix} = \begin{bmatrix} cos\theta & 0 & -sin\theta \\ 0&1& 0\\ sin\theta & 0& cos\theta \end{bmatrix}

  • 最后是绕z轴旋转,如图:

可得绕y轴旋转的矩阵:

R_z(\theta ) = \begin{bmatrix} p'\\q' \\ r' \end{bmatrix} = \begin{bmatrix} cos\theta & sin\theta &0\\ -sin\theta &cos\theta & 0\\ 0& 0&1\end{bmatrix}

4. 3D中绕任意轴旋转

在不考虑平移的情况下,假设旋转轴通过原点,设单位向量n为旋转轴θ为旋转量。我们想要求出绕轴n旋转角度θ的矩阵。也就是说,我们想得到满足下面条件的矩阵R(n,θ),其中v'是向量v绕轴n旋转后的向量:

vR(n,\theta ) = v'

如何用vn和θ表示v',我们的想法是在垂直于n的平面中解决这个问题,那么这就转化为了一个简单的2D问题。为了做到这一点,将v分解为两个分向量: v_\parallelv_\perp,分别平行和垂直于n,并且有v = v_\parallel + v_\perp。因为v_\parallel平行于n ,所以绕n旋转不会影响到它。固只要计算出v_\perpn旋转后的v_\perp',就能得到v' = v_\parallel +v'_\perp。如下图所示,为了计算v' = v_\parallel +v'_\perp,我们构造了向量v_\parallelv_\perp和临时向量w

  • v_\parallelv平行于n的分量。或者说是v_\parallelvn上的投影,即:
    v_\parallel =(v\cdot n) n 
  • v_\perpv垂直于n的分量,也是v投影到垂直于n的平面上的结果:
    v_\perp = v - v_\parallel
  • w是同时垂直于v_\parallelv_\perp的向量。它的长度和v_\perp的相同。w和v_\perp同在垂直于n的平面中,w是v_\perp绕n旋转90°的结果,即:
    w = n \times v_\perp = n\times (n-v_\parallel ) = n \times v - n\times v_\parallel = n \times v - 0 = n \times v

如上图所示,v_\perp'可以表示为:

v_\perp' = v_\perp cos\theta + wsin\theta
                =(v-(v\cdot n)n )cos\theta+(n\times v)sin\theta

代入v'的表达式,有:

v' = v_\perp ' + v_\parallel
              = (v-(v\cdot n)n)cos\theta +(n\times v)sin\theta +(v\cdot n)n

现在,已经得到了v'和v,n,θ的关系式了,可以用它来计算变换后的基向量并构造矩阵。
第一个基向量为:

p =\begin{bmatrix} 1 & 0 & 0 \end{bmatrix}

p' = (p-(p\cdot n)n)cos\theta +(n\times p)sin\theta +(p\cdot n)n
               =\left ( \begin{bmatrix}1\\0 \\0 \end{bmatrix} - \left ( \begin{bmatrix}1\\ 0\\0 \end{bmatrix} \cdot \begin{bmatrix}n_x\\n_y \\n_z \end{bmatrix} \right ) \begin{bmatrix}n_x\\n_y \\n_z \end{bmatrix} \right )cos\theta - \left ( \begin{bmatrix} n_x\\n_y \\n_z \end{bmatrix} \times \begin{bmatrix} 1\\ 0\\ 0\end{bmatrix} \right )sin\theta + \left ( \begin{bmatrix} 1\\0 \\0 \end{bmatrix} \cdot \begin{bmatrix} n_x\\n_y \\n_z \end{bmatrix} \right ) \begin{bmatrix} n_x\\n_y \\n_z \end{bmatrix}

    = \left ( \begin{bmatrix} 1\\0 \\0 \end{bmatrix} - n_x\begin{bmatrix} n_x\\n_y \\n_z \end{bmatrix} \right )cos\theta + \begin{bmatrix} 0\\ n_z\\-n_y \end{bmatrix}sin\theta + n_x\begin{bmatrix} n_x\\ n_y\\n_z \end{bmatrix}

    = \begin{bmatrix} 1-n_x^2\\-n_xn_y \\-n_x n_z \end{bmatrix} cos\theta + \begin{bmatrix} 0\\ n_z\\ -n_y\end{bmatrix} sin\theta +n_x \begin{bmatrix} n_x^2\\ n_xn_y\\ n_xn_z\end{bmatrix}
               = \begin{bmatrix} cos\theta -n_x^2cos\theta \\ -n_xn_ycos\theta \\-n_xn_zcos\theta \end{bmatrix} + \begin{bmatrix} 0\\n_zsin\theta \\-n_ysin\theta \end{bmatrix} + \begin{bmatrix} n_x^2\\n_xn_y \\ n_xn_z\end{bmatrix}

               = \begin{bmatrix} cos\theta - n_x^2cos\theta +n_x^2 \\-n_xn_ycos\theta +n_zsin\theta +n_xn_y \\ -n_xn_zcos\theta -n_ysin\theta +n_xn_z \end{bmatrix}
               =\begin{bmatrix} n_x^2(1- cos\theta )+cos\theta \\ n_xn_y(1- cos\theta )+n_zsin\theta \\ n_xn_z(1- cos\theta )-n_ysin\theta \end{bmatrix}

另外两个基向量的推导与之类似,有:

q=\begin{bmatrix} 0 & 1 & 0 \end{bmatrix}
           q'=\begin{bmatrix} n_xn_y(1- cos\theta )-n_zsin\theta \\ n_y^2(1- cos\theta )+cos\theta \\ n_yn_z(1- cos\theta )+n_xsin\theta \end{bmatrix}

r=\begin{bmatrix} 0 & 0 & 1 \end{bmatrix}
           r'=\begin{bmatrix} n_xn_z(1- cos\theta )+n_ysin\theta \\ n_yn_z(1- cos\theta )-n_xsin\theta \\ n_z^2(1- cos\theta )+cos\theta \end{bmatrix}

用这些基向量构造矩阵,可得公式为:

R(n,\theta ) = \begin{bmatrix}p'\\q' \\ r'\end{bmatrix} = \begin{bmatrix} n_x^2(1- cos\theta )+cos\theta & n_xn_y(1- cos\theta )+n_zsin\theta &n_xn_z(1- cos\theta )-n_ysin\theta \\ n_xn_y(1- cos\theta )-n_zsin\theta & n_y^2(1- cos\theta )+cos\theta & n_yn_z(1- cos\theta )+n_xsin\theta \\ n_xn_z(1- cos\theta )+n_ysin\theta & n_yn_z(1- cos\theta )-n_xsin\theta &n_z^2(1- cos\theta )+cos\theta \end{bmatrix}

猜你喜欢

转载自blog.csdn.net/LeeXxs/article/details/83107273