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 */
24
26
27
28using namespace armarx;
29
30namespace armarx
31{
32 std::vector<double>
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
58 OrientationConversion::toQuaternion(const double roll, const double pitch, const double yaw)
59 {
60 QuaternionBasePtr q = QuaternionBasePtr(new FramedOrientation());
61 //Abbreviations for the various angular functions
62 double cy = cos(yaw * 0.5);
63 double sy = sin(yaw * 0.5);
64 double cr = cos(roll * 0.5);
65 double sr = sin(roll * 0.5);
66 double cp = cos(pitch * 0.5);
67 double sp = sin(pitch * 0.5);
68 q->qw = cy * cr * cp + sy * sr * sp;
69 q->qx = cy * sr * cp - sy * cr * sp;
70 q->qy = cy * cr * sp + sy * sr * cp;
71 q->qz = sy * cr * cp - cy * sr * sp;
72 return q;
73 }
74
75 Eigen::Quaternion<double>
77 {
78 return Eigen::Quaternion<double>(q->qw, q->qx, q->qy, q->qz);
79 }
80
81 QuaternionBasePtr
82 OrientationConversion::toArmarX(Eigen::Quaternion<double> q)
83 {
85 temp.qw = q.w();
86 temp.qx = q.x();
87 temp.qy = q.y();
88 temp.qz = q.z();
89 return QuaternionBasePtr(new FramedOrientation(temp));
90 }
91} // namespace armarx
#define M_PI
Definition MathTools.h:17
The FramedOrientation class.
Definition FramedPose.h:216
static std::vector< double > toEulerAngle(QuaternionBasePtr q)
static Eigen::Quaternion< double > toEigen(QuaternionBasePtr q)
static QuaternionBasePtr toArmarX(Eigen::Quaternion< double > q)
static QuaternionBasePtr toQuaternion(const double roll, const double pitch, const double yaw)
#define q
This file offers overloads of toIce() and fromIce() functions for STL container types.