坐标系转换

今天一项工作要处理坐标变换,简单研究了下。

局部坐标系到全局坐标系

原理如下:
坐标系转换_第1张图片
图中,世界坐标系是 ( X , Y , Z ) (X, Y, Z) (X,Y,Z),局部坐标系是 ( X ′ , Y ′ , Z ′ ) (X', Y', Z') (X,Y,Z),局部坐标系原点 O ′ O' O
要转换的点是 P P P
O P = x ∗ i + y ∗ j + z ∗ k O O ′ = a ∗ i + b ∗ j + c ∗ k OP =x*i+y*j+z*k \\ OO'=a*i+b*j+c*k OP=xi+yj+zkOO=ai+bj+ck
坐标轴对应关系
{ i ′ = u 11 ∗ i + u 12 ∗ j + u 13 ∗ k j ′ = u 21 ∗ i + u 22 ∗ j + u 22 ∗ k k ′ = u 31 ∗ i + u 32 ∗ j + u 33 ∗ k \left\{\begin{matrix} i'=u_{11}*i+u_{12}*j+u_{13}*k \\ j'=u_{21}*i+u_{22}*j+u_{22}*k \\ k'=u_{31}*i+u_{32}*j+u_{33}*k \end{matrix}\right. i=u11i+u12j+u13kj=u21i+u22j+u22kk=u31i+u32j+u33k
联立公式
O ′ P = x ′ ∗ i ′ + y ′ ∗ j ′ + z ′ ∗ k ′ = x ′ ∗ ( u 11 ∗ i + u 12 ∗ j + u 13 ∗ k ) + y ′ ∗ ( u 21 ∗ i + u 22 ∗ j + u 22 ∗ k ) + z ′ ∗ ( u 31 ∗ i + u 32 ∗ j + u 33 ∗ k ) = ( u 11 ∗ x ′ + u 21 ∗ y ′ + u 31 ∗ z ′ ) ∗ i + ( u 12 ∗ x ′ + u 22 ∗ y ′ + u 32 ∗ z ′ ) ∗ j + ( u 13 ∗ x ′ + u 23 ∗ y ′ + u 33 ∗ z ′ ) ∗ k O'P=x'*i'+y'*j'+z'*k'\\ = \quad x'*(u_{11}*i+u_{12}*j+u_{13}*k) \\ \quad +y'*(u_{21}*i+u_{22}*j+u_{22}*k) \\ \quad +z'*(u_{31}*i+u_{32}*j+u_{33}*k) \\ = \quad (u_{11}*x'+u_{21}*y'+u_{31}*z')*i \\ \quad + (u_{12}*x'+u_{22}*y'+u_{32}*z')*j \\ \quad + (u_{13}*x'+u_{23}*y'+u_{33}*z')*k OP=xi+yj+zk=x(u11i+u12j+u13k)+y(u21i+u22j+u22k)+z(u31i+u32j+u33k)=(u11x+u21y+u31z)i+(u12x+u22y+u32z)j+(u13x+u23y+u33z)k
O P = O O ′ + O ′ P OP=OO'+O'P OP=OO+OP ,于是
{ x = a + u 11 ∗ x ′ + u 21 ∗ y ′ + u 31 ∗ z ′ y = b + u 12 ∗ x ′ + u 22 ∗ y ′ + u 32 ∗ z ′ z = c + u 13 ∗ x ′ + u 23 ∗ y ′ + u 33 ∗ z ′ \left\{\begin{matrix} x=a+u_{11}*x'+u_{21}*y'+u_{31}*z'\\ y=b+u_{12}*x'+u_{22}*y'+u_{32}*z'\\ z=c+u_{13}*x'+u_{23}*y'+u_{33}*z' \end{matrix}\right. x=a+u11x+u21y+u31zy=b+u12x+u22y+u32zz=c+u13x+u23y+u33z
得转换关系
[ x y z 1 ] = [ x ′ y ′ z ′ 1 ] ∗ W \begin{bmatrix} x & y & z & 1 \end{bmatrix} = \begin{bmatrix} x' & y' & z' & 1 \end{bmatrix} * W [xyz1]=[xyz1]W
最终获得矩阵
W = [ u 11 u 12 u 13 0 u 21 u 22 u 23 0 u 31 u 32 u 33 0 a b c 1 ] W= \begin{bmatrix} u_{11} & u_{12} & u_{13} & 0\\ u_{21} & u_{22} & u_{23} & 0\\ u_{31} & u_{32} & u_{33} & 0\\ a & b & c & 1 \end{bmatrix} W=u11u21u31au12u22u32bu13u23u33c0001

全局坐标系到局部坐标系

因为
[ x y z 1 ] = [ x ′ y ′ z ′ 1 ] ∗ W \begin{bmatrix} x & y & z & 1 \end{bmatrix} = \begin{bmatrix} x' & y' & z' & 1 \end{bmatrix} * W [xyz1]=[xyz1]W
那么
[ x y z 1 ] ∗ W − 1 = [ x ′ y ′ z ′ 1 ] \begin{bmatrix} x & y & z & 1 \end{bmatrix} * W^{-1} = \begin{bmatrix} x' & y' & z' & 1 \end{bmatrix} [xyz1]W1=[xyz1]
观察到
W = [ u 11 u 12 u 13 0 u 21 u 22 u 23 0 u 31 u 32 u 33 0 a b c 1 ] = [ u 11 u 12 u 13 0 u 21 u 22 u 23 0 u 31 u 32 u 33 0 0 0 0 1 ] ⋅ [ 1 0 0 0 0 1 0 0 0 0 1 0 a b c 1 ] = R ⋅ T W= \begin{bmatrix} u_{11} & u_{12} & u_{13} & 0\\ u_{21} & u_{22} & u_{23} & 0\\ u_{31} & u_{32} & u_{33} & 0\\ a & b & c & 1 \end{bmatrix} = \begin{bmatrix} u_{11} & u_{12} & u_{13} & 0\\ u_{21} & u_{22} & u_{23} & 0\\ u_{31} & u_{32} & u_{33} & 0\\ 0 & 0 & 0 & 1 \end{bmatrix} \cdot \begin{bmatrix} 1 & 0 & 0 & 0\\ 0 & 1 & 0 & 0\\ 0 & 0 & 1 & 0\\ a & b & c & 1 \end{bmatrix} =R \cdot T W=u11u21u31au12u22u32bu13u23u33c0001=u11u21u310u12u22u320u13u23u3300001100a010b001c0001=RT
其中, R R R是旋转矩阵, T T T是平移矩阵
那么
W − 1 = ( R ⋅ T ) − 1 = T − 1 ∗ R − 1 W^{-1}= (R\cdot T)^{-1} = T^{-1}*R^{-1} W1=(RT)1=T1R1
其中
T − 1 = [ 1 0 0 0 0 1 0 0 0 0 1 0 − a − b − c 1 ] T^{-1} = \begin{bmatrix} 1 & 0 & 0 & 0\\ 0 & 1 & 0 & 0\\ 0 & 0 & 1 & 0\\ -a & -b & -c & 1 \end{bmatrix} T1=100a010b001c0001
如果 R R R是正定矩阵,即 R ⋅ R − 1 = E R \cdot R^{-1} = E RR1=E。那么
R − 1 = R T = [ u 11 u 21 u 31 0 u 12 u 22 u 32 0 u 13 u 23 u 33 0 0 0 0 1 ] R^{-1}=R^{T}= \begin{bmatrix} u_{11} & u_{21} & u_{31} & 0\\ u_{12} & u_{22} & u_{32} & 0\\ u_{13} & u_{23} & u_{33} & 0\\ 0 & 0 & 0 & 1 \end{bmatrix} R1=RT=u11u12u130u21u22u230u31u32u3300001
直接可计算 W − 1 W^{-1} W1

但如果 R R R不是正定方阵,即局部坐标系的坐标轴不是右手系相互垂直的,那么还是直接求逆得 W − 1 W^{-1} W1

你可能感兴趣的:(opengl,坐标转换)