OrientationConversion.cpp
Go to the documentation of this file.
1 
2 /*
3  * This file is part of ArmarX.
4  *
5  * ArmarX is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * ArmarX is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  *
17  * @package ArmarXGuiPlugins::RobotTrajectoryDesigner::Interpolation
18  * @author Timo Birr
19  * @date 2018
20  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
21  * GNU General Public License
22  */
23 #include "OrientationConversion.h"
24 
26 
27 
28 using namespace armarx;
29 
30 
31 namespace armarx
32 {
33  std::vector<double> OrientationConversion::toEulerAngle(QuaternionBasePtr q)
34  {
35  // roll (x-axis rotation)
36  double sinr = +2.0 * (q->qw * q->qx + q->qy * q->qz);
37  double cosr = +1.0 - 2.0 * (q->qx * q->qx + q->qy * q->qy);
38  double roll = atan2(sinr, cosr);
39  // pitch (y-axis rotation)
40  double sinp = +2.0 * (q->qw * q->qy - q->qz * q->qx);
41  double pitch;
42  if (fabs(sinp) >= 1)
43  {
44  pitch = copysign(M_PI / 2, sinp); // use 90 degrees if out of range
45  }
46  else
47  {
48  pitch = asin(sinp);
49  }
50  // yaw (z-axis rotation)
51  double siny = +2.0 * (q->qw * q->qz + q->qx * q->qy);
52  double cosy = +1.0 - 2.0 * (q->qy * q->qy + q->qz * q->qz);
53  double yaw = atan2(siny, cosy);
54  return {roll, pitch, yaw};
55  }
56 
57  QuaternionBasePtr OrientationConversion::toQuaternion(const double roll, const double pitch, const double yaw)
58  {
59  QuaternionBasePtr q = QuaternionBasePtr(new FramedOrientation());
60  //Abbreviations for the various angular functions
61  double cy = cos(yaw * 0.5);
62  double sy = sin(yaw * 0.5);
63  double cr = cos(roll * 0.5);
64  double sr = sin(roll * 0.5);
65  double cp = cos(pitch * 0.5);
66  double sp = sin(pitch * 0.5);
67  q->qw = cy * cr * cp + sy * sr * sp;
68  q->qx = cy * sr * cp - sy * cr * sp;
69  q->qy = cy * cr * sp + sy * sr * cp;
70  q->qz = sy * cr * cp - cy * sr * sp;
71  return q;
72  }
73 
74  Eigen::Quaternion<double> OrientationConversion::toEigen(QuaternionBasePtr q)
75  {
76  return Eigen::Quaternion<double>(q->qw, q->qx, q->qy, q->qz);
77  }
78 
79  QuaternionBasePtr OrientationConversion::toArmarX(Eigen::Quaternion<double> q)
80  {
82  temp.qw = q.w();
83  temp.qx = q.x();
84  temp.qy = q.y();
85  temp.qz = q.z();
86  return QuaternionBasePtr(new FramedOrientation(temp));
87  }
88 }
armarx::OrientationConversion::toEulerAngle
static std::vector< double > toEulerAngle(QuaternionBasePtr q)
Definition: OrientationConversion.cpp:33
OrientationConversion.h
M_PI
#define M_PI
Definition: MathTools.h:17
FramedPose.h
armarx::OrientationConversion::toQuaternion
static QuaternionBasePtr toQuaternion(const double roll, const double pitch, const double yaw)
Definition: OrientationConversion.cpp:57
armarx::FramedOrientation
The FramedOrientation class.
Definition: FramedPose.h:199
armarx::OrientationConversion::toArmarX
static QuaternionBasePtr toArmarX(Eigen::Quaternion< double > q)
Definition: OrientationConversion.cpp:79
q
#define q
armarx::VariantType::FramedOrientation
const VariantTypeId FramedOrientation
Definition: FramedPose.h:40
armarx::OrientationConversion::toEigen
static Eigen::Quaternion< double > toEigen(QuaternionBasePtr q)
Definition: OrientationConversion.cpp:74
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28