Guest User

Quaternion to Rotation

a guest
Sep 30th, 2015
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.23 KB | None | 0 0
  1. template<class Derived>
  2. inline typename QuaternionBase<Derived>::Matrix3
  3. QuaternionBase<Derived>::toRotationMatrix(void) const
  4. {
  5.   // NOTE if inlined, then gcc 4.2 and 4.4 get rid of the temporary (not gcc 4.3 !!)
  6.   // if not inlined then the cost of the return by value is huge ~ +35%,
  7.   // however, not inlining this function is an order of magnitude slower, so
  8.   // it has to be inlined, and so the return by value is not an issue
  9.   Matrix3 res;
  10.  
  11.   const Scalar tx  = Scalar(2)*this->x();
  12.   const Scalar ty  = Scalar(2)*this->y();
  13.   const Scalar tz  = Scalar(2)*this->z();
  14.   const Scalar twx = tx*this->w();
  15.   const Scalar twy = ty*this->w();
  16.   const Scalar twz = tz*this->w();
  17.   const Scalar txx = tx*this->x();
  18.   const Scalar txy = ty*this->x();
  19.   const Scalar txz = tz*this->x();
  20.   const Scalar tyy = ty*this->y();
  21.   const Scalar tyz = tz*this->y();
  22.   const Scalar tzz = tz*this->z();
  23.  
  24.   res.coeffRef(0,0) = Scalar(1)-(tyy+tzz);
  25.   res.coeffRef(0,1) = txy-twz;
  26.   res.coeffRef(0,2) = txz+twy;
  27.   res.coeffRef(1,0) = txy+twz;
  28.   res.coeffRef(1,1) = Scalar(1)-(txx+tzz);
  29.   res.coeffRef(1,2) = tyz-twx;
  30.   res.coeffRef(2,0) = txz-twy;
  31.   res.coeffRef(2,1) = tyz+twx;
  32.   res.coeffRef(2,2) = Scalar(1)-(txx+tyy);
  33.  
  34.   return res;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment