当前位置:   article > 正文

3D 视角旋转矩阵 yaw pitch roll (pan, tilt)的数学计算_pan-tilt

pan-tilt
一、 yaw pitch roll  含义


  yaw:  水平偏航转角  (绕Y轴旋转,   Y轴向上)

  pitch: 上下俯仰转角 (绕X轴 旋转,   X轴向右)

  roll:    旋转角(绕Z轴 旋转,   Z轴向前)

                                                                      Direct3D 左手坐标 


图见:  yaw pitch roll 图




二、旋转矩阵、视角矩阵、yaw-pitch-roll的关系



     1.  D3DXMatrixRotationYawPitchRoll 函数, 计算顺序不 是 yaw, pitch, roll .   而是 pitch, yaw。

           注意: 第一个 旋转计算 完毕之后, 第二个旋转的参考轴仍然是全局坐标,  而不是自身参考轴。

  1. D3DXMATRIXA16 mx_xlib;
  2. D3DXMATRIXA16 mx_pitch_first;
  3. D3DXMATRIXA16 mx_yaw_first;
  4. D3DXMATRIXA16 mx_yaw, mx_pitch;
  5. float yaw, pitch, roll =0.0f;
  6. yaw = D3DX_PI/6;
  7. pitch = D3DX_PI/3;
  8. TRACE( "yaw: %f pitch: %f\n", yaw, pitch );
  9. D3DXMatrixRotationYawPitchRoll( &mx_xlib, yaw, pitch, roll );
  10. D3DXMatrixRotationY( &mx_yaw, yaw );
  11. D3DXMatrixRotationX( &mx_pitch, pitch );
  12. mx_pitch_first = mx_pitch * mx_yaw ;
  13. mx_yaw_first = mx_yaw * mx_pitch ;

          上述代码结果: mx_pitch_first== mx_xlib.     但是 mx_yaw_first 与 mx_xlib 不同

          如果 yaw 一定在前面, 则 pitch 使用自身参考轴, 见如下代码:


  1. D3DXMATRIXA16 mx_yp_local;
  2. D3DXMATRIXA16 mx_local_pitch;
  3. D3DXVECTOR3 vAxisX( 1.0, 0, 0 );
  4. D3DXVECTOR3 vLocalX;
  5. D3DXVec3TransformCoord( &vLocalX, &vAxisX, &mx_yaw );
  6. D3DXMatrixRotationAxis( &mx_local_pitch, &vLocalX, pitch );
  7. mx_yp_local = mx_yaw * mx_local_pitch ;

               

          结果相同: mx_yp_local ==  mx_xlib.


     2.  D3DXMatrixRotationYawPitchRoll 生成的旋转矩阵  不等于  摄像的视角矩阵。 

         视角矩阵 是通过 旋转一个标准  z轴单位向量( 0,0,1) , 再通过 两个点 来 确定 视角矩阵。 代码见下:

    

  1. D3DXVECTOR3 vUp  (0,1,0);
  2. D3DXVECTOR3 vZero(0,0,0);
  3. D3DXVECTOR3 vZ(0,0,1.0f);
  4. D3DXVECTOR3 vNewZ;
  5. D3DXMATRIX mRotView;
  6. D3DXVec3TransformCoord( &vNewZ, &vZ, &d_xlib );
  7. D3DXMatrixLookAtLH( &mRotView, &vZero, &vNewZ, & vUp );




     3. 视角矩阵 ==>  计算 yaw, pitch, roll

  1. D3DXMATRIX mInvView;
  2. D3DXMatrixInverse( &mInvView, NULL, &mRotView );
  3. // The axis basis vectors and camera position are stored inside the
  4. // position matrix in the 4 rows of the camera's world matrix.
  5. // To figure out the yaw/pitch of the camera, we just need the Z basis vector
  6. D3DXVECTOR3* pZBasis = (D3DXVECTOR3*) &mInvView._31;
  7. float fCameraYawAngle = atan2f( pZBasis->x, pZBasis->z );
  8. float fLen = sqrtf(pZBasis->z*pZBasis->z + pZBasis->x*pZBasis->x);
  9. float fCameraPitchAngle = -atan2f( pZBasis->y, fLen );


三、云台pan-tilt-zoom

云台 支持 pan, tilt, zoom,focus 四个参数,常用前三个,简称PTZ.  

云台一般只能 水平(pan),上下(tilt)动作。 

不支持旋转。

对于Pan, tilt 构造 旋转矩阵 次序

1. tilt[全局 x 轴] -> Pan [全局轴 y ]

2. Pan [全局轴 y] -> tilt [自身轴 x 轴]

和 yaw pitch roll 概念, 完全相同。


四、补充

         1.  对于旋转矩阵, 它的逆矩阵 和 转置矩阵 是相同。 所以可以用 旋转矩阵 直接 变换 方向矢量。

         2.  视角矩阵 虽然 和 yaw-pitch-roll 不同, 但是其实就是 逆(或转置) 的关系。

         3. 如果一个 先yaw, 后pitch的视角矩阵 (全局轴) ==> 计算   yaw, pitch  , 见下式。

  1. D3DXMATRIX mInvView;
  2. D3DXMatrixInverse( &mInvView, NULL, &mRotView );
  3. // The axis basis vectors and camera position are stored inside the
  4. // position matrix in the 4 rows of the camera's world matrix.
  5. // To figure out the yaw/pitch of the camera, we just need the Z basis vector
  6. D3DXVECTOR3* pZBasis = (D3DXVECTOR3*) &mInvView._31;
  7. float fYawFirst_PitchAngle   = -atan2f( pZBasis->y, pZBasis->z );
  8. float fYawFirst_Len = sqrtf(pZBasis->z*pZBasis->z + pZBasis->y*pZBasis->y);
  9. float fYawFirst_YawAngle = atan2f( pZBasis->x, fYawFirst_Len );





                
本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号