How to convert Euler Angles to Front, Up, Right vectors

后端 未结 1 1149
庸人自扰
庸人自扰 2020-12-17 05:32

I need a function that given Yaw, Pitch, and Roll, can produce the Front (or Looking At), Right, and Up vectors in \"world coordinates\".

In my particular world spac

相关标签:
1条回答
  • Here is a full working example. It isn't very C++-like. You would probably want to use a real matrix class, but it should be ok for demonstration purposes. One thing that isn't clear from your question is the rotation order, but that can easily be changed.

    #include <iostream>
    #include <cmath>
    #include <cstdlib>
    
    typedef float Float;
    typedef Float Axis[3];
    typedef Axis Axes[3];
    
    static void copy(const Axes &from,Axes &to)
    {
      for (size_t i=0; i!=3; ++i) {
        for (size_t j=0; j!=3; ++j) {
          to[i][j] = from[i][j];
        }
      }
    }
    
    static void mul(Axes &mat,Axes &b)
    {
      Axes result;
      for (size_t i=0; i!=3; ++i) {
        for (size_t j=0; j!=3; ++j) {
          Float sum = 0;
          for (size_t k=0; k!=3; ++k) {
            sum += mat[i][k]*b[k][j];
          }
          result[i][j] = sum;
        }
      }
      copy(result,mat);
    }
    
    static void getAxes(Axes &result,Float yaw,Float pitch,Float roll)
    {
      Float x = -pitch;
      Float y = yaw;
      Float z = -roll;
      Axes matX = {
        {1,     0,     0 },
        {0, cos(x),sin(x)},
        {0,-sin(x),cos(x)}
      };
      Axes matY = {
        {cos(y),0,-sin(y)},
        {     0,1,      0},
        {sin(y),0, cos(y)}
      };
      Axes matZ = {
        { cos(z),sin(z),0},
        {-sin(z),cos(z),0},
        {      0,     0,1}
      };
      Axes axes = {
        {1,0,0},
        {0,1,0},
        {0,0,1}
      };
    
      mul(axes,matX);
      mul(axes,matY);
      mul(axes,matZ);
    
      copy(axes,result);
    }
    
    
    static void showAxis(const char *desc,const Axis &axis,Float sign)
    {
      std::cout << "  " << desc << " = (";
      for (size_t i=0; i!=3; ++i) {
        if (i!=0) {
          std::cout << ",";
        }
        std::cout << axis[i]*sign;
      }
      std::cout << ")\n";
    }
    
    static void showAxes(const char *desc,Axes &axes)
    {
      std::cout << desc << ":\n";
      showAxis("front",axes[2],1);
      showAxis("right",axes[0],-1);
      showAxis("up",axes[1],1);
    }
    
    int main(int,char**)
    {
      Axes axes;
      std::cout.setf(std::ios::fixed);
      std::cout.precision(1);
      getAxes(axes,0,0,0);
      showAxes("yaw=0, pitch=0, roll=0",axes);
      getAxes(axes,M_PI/2,0,0);
      showAxes("yaw=90, pitch=0, roll=0",axes);
      getAxes(axes,0,M_PI/2,0);
      showAxes("yaw=0, pitch=90, roll=0",axes);
      getAxes(axes,0,0,M_PI/2);
      showAxes("yaw=0, pitch=0, roll=90",axes);
      return 0;
    }
    
    0 讨论(0)
提交回复
热议问题