pinocchio  2.4.4
A fast and flexible implementation of Rigid Body Dynamics algorithms and their analytical derivatives
rpy.hpp
1 //
2 // Copyright (c) 2016-2020 CNRS INRIA
3 //
4 
5 #ifndef __pinocchio_math_rpy_hpp__
6 #define __pinocchio_math_rpy_hpp__
7 
8 #include "pinocchio/math/fwd.hpp"
9 #include "pinocchio/math/comparison-operators.hpp"
10 
11 #include <Eigen/Geometry>
12 
13 namespace pinocchio
14 {
15  namespace rpy
16  {
24  template<typename Scalar>
25  Eigen::Matrix<Scalar,3,3> rpyToMatrix(const Scalar r,
26  const Scalar p,
27  const Scalar y)
28  {
29  typedef Eigen::AngleAxis<Scalar> AngleAxis;
30  typedef Eigen::Matrix<Scalar,3,1> Vector3s;
31  return (AngleAxis(y, Vector3s::UnitZ())
32  * AngleAxis(p, Vector3s::UnitY())
33  * AngleAxis(r, Vector3s::UnitX())
34  ).toRotationMatrix();
35  }
36 
44  template<typename Vector3Like>
45  Eigen::Matrix<typename Vector3Like::Scalar,3,3,PINOCCHIO_EIGEN_PLAIN_TYPE(Vector3Like)::Options>
46  rpyToMatrix(const Eigen::MatrixBase<Vector3Like> & rpy)
47  {
48  PINOCCHIO_ASSERT_MATRIX_SPECIFIC_SIZE(Vector3Like, rpy, 3, 1);
49  return rpyToMatrix(rpy[0], rpy[1], rpy[2]);
50  }
51 
65  template<typename Matrix3Like>
66  Eigen::Matrix<typename Matrix3Like::Scalar,3,1,PINOCCHIO_EIGEN_PLAIN_TYPE(Matrix3Like)::Options>
67  matrixToRpy(const Eigen::MatrixBase<Matrix3Like> & R)
68  {
69  PINOCCHIO_ASSERT_MATRIX_SPECIFIC_SIZE(Matrix3Like, R, 3, 3);
70  assert(R.isUnitary() && "R is not a unitary matrix");
71 
72  typedef typename Matrix3Like::Scalar Scalar;
73  typedef Eigen::Matrix<Scalar,3,1,PINOCCHIO_EIGEN_PLAIN_TYPE(Matrix3Like)::Options> ReturnType;
74  static const Scalar pi = PI<Scalar>();
75 
76  ReturnType res = R.eulerAngles(2,1,0).reverse();
77 
78  if(res[1] < -pi/2)
79  res[1] += 2*pi;
80 
81  if(res[1] > pi/2)
82  {
83  res[1] = pi - res[1];
84  if(res[0] < Scalar(0))
85  res[0] += pi;
86  else
87  res[0] -= pi;
88  // res[2] > 0 according to Eigen's eulerAngles doc, no need to check its sign
89  res[2] -= pi;
90  }
91 
92  return res;
93  }
94  } // namespace rpy
95 }
96 #endif //#ifndef __pinocchio_math_rpy_hpp__
Eigen::Matrix< typename Matrix3Like::Scalar, 3, 1, Matrix3Like ::Options > matrixToRpy(const Eigen::MatrixBase< Matrix3Like > &R)
Convert from Transformation Matrix to Roll, Pitch, Yaw.
Definition: rpy.hpp:67
Eigen::Matrix< Scalar, 3, 3 > rpyToMatrix(const Scalar r, const Scalar p, const Scalar y)
Convert from Roll, Pitch, Yaw to rotation Matrix.
Definition: rpy.hpp:25
Main pinocchio namespace.
Definition: treeview.dox:24