Main MRPT website > C++ reference for MRPT 1.9.9
slerp.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #ifndef mrpt_math_slerp_H
10 #define mrpt_math_slerp_H
11 
12 #include <mrpt/math/CQuaternion.h>
13 #include <mrpt/poses/poses_frwds.h>
14 
15 namespace mrpt
16 {
17 namespace math
18 {
19 /** \addtogroup geometry_grp
20  * @{ */
21 
22 /** @name SLERP (Spherical Linear Interpolation) functions
23  @{ */
24 
25 /** SLERP interpolation between two quaternions
26  * \param[in] q0 The quaternion for t=0
27  * \param[in] q1 The quaternion for t=1
28  * \param[in] t A "time" parameter, in the range [0,1].
29  * \param[out] q The output, interpolated quaternion.
30  * \tparam T The type of the quaternion (e.g. float, double).
31  * \exception std::exception Only in Debug, if t is not in the valid range.
32  * \sa http://en.wikipedia.org/wiki/Slerp
33  */
34 template <typename T>
35 void slerp(
36  const CQuaternion<T>& q0, const CQuaternion<T>& q1, const double t,
38 {
39  ASSERTDEB_(t >= 0 && t <= 1)
40  // See:
41  // http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/slerp/index.htm
42  // Angle between q0-q1:
43  double cosHalfTheta =
44  q0[0] * q1[0] + q0[1] * q1[1] + q0[2] * q1[2] + q0[3] * q1[3];
45  // if qa=qb or qa=-qb then theta = 0 and we can return qa
46  if (std::abs(cosHalfTheta) >= 1.0)
47  {
48  q = q0;
49  return;
50  }
51  bool reverse_q1 = false;
52  if (cosHalfTheta < 0) // Always follow the shortest path
53  {
54  reverse_q1 = true;
55  cosHalfTheta = -cosHalfTheta;
56  }
57  // Calculate temporary values.
58  const double halfTheta = acos(cosHalfTheta);
59  const double sinHalfTheta =
60  std::sqrt(1.0 - mrpt::math::square(cosHalfTheta));
61  // if theta = 180 degrees then result is not fully defined
62  // we could rotate around any axis normal to qa or qb
63  if (std::abs(sinHalfTheta) < 0.001)
64  {
65  if (!reverse_q1)
66  for (int i = 0; i < 4; i++) q[i] = (1 - t) * q0[i] + t * q1[i];
67  else
68  for (int i = 0; i < 4; i++) q[i] = (1 - t) * q0[i] - t * q1[i];
69  return;
70  }
71  const double A = sin((1 - t) * halfTheta) / sinHalfTheta;
72  const double B = sin(t * halfTheta) / sinHalfTheta;
73  if (!reverse_q1)
74  for (int i = 0; i < 4; i++) q[i] = A * q0[i] + B * q1[i];
75  else
76  for (int i = 0; i < 4; i++) q[i] = A * q0[i] - B * q1[i];
77 }
78 
79 /** SLERP interpolation between two 6D poses - like mrpt::math::slerp for
80  * quaternions, but interpolates the [X,Y,Z] coordinates as well.
81  * \param[in] p0 The pose for t=0
82  * \param[in] p1 The pose for t=1
83  * \param[in] t A "time" parameter, in the range [0,1].
84  * \param[out] p The output, interpolated pose.
85  * \exception std::exception Only in Debug, if t is not in the valid range.
86  */
87 void slerp(
88  const mrpt::poses::CPose3D& q0, const mrpt::poses::CPose3D& q1,
89  const double t, mrpt::poses::CPose3D& p);
90 
91 //! \overload
92 void slerp(
94  const double t, mrpt::poses::CPose3DQuat& p);
95 
96 /** \overload Interpolates two SO(3) elements (the rotational part only), given
97  * as mrpt::math::TPose3D
98  * form as yaw,pitch,roll angles. XYZ are ignored.
99  */
100 void slerp_ypr(
101  const mrpt::math::TPose3D& q0, const mrpt::math::TPose3D& q1,
102  const double t, mrpt::math::TPose3D& p);
103 
104 /** @} */
105 
106 /** @} */ // grouping
107 }
108 }
109 #endif
GLdouble GLdouble t
Definition: glext.h:3689
void slerp(const CQuaternion< T > &q0, const CQuaternion< T > &q1, const double t, CQuaternion< T > &q)
SLERP interpolation between two quaternions.
Definition: slerp.h:35
GLdouble GLdouble GLdouble GLdouble q
Definition: glext.h:3721
void slerp_ypr(const mrpt::math::TPose3D &q0, const mrpt::math::TPose3D &q1, const double t, mrpt::math::TPose3D &p)
Definition: slerp.cpp:46
T square(const T x)
Inline function for the square of a number.
Definition: bits.h:55
A class used to store a 3D pose as a translation (x,y,z) and a quaternion (qr,qx,qy,qz).
Definition: CPose3DQuat.h:48
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
#define ASSERTDEB_(f)
Defines an assertion mechanism - only when compiled in debug.
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:88
Lightweight 3D pose (three spatial coordinates, plus three angular coordinates).
A quaternion, which can represent a 3D rotation as pair , with a real part "r" and a 3D vector ...
Definition: CQuaternion.h:46
GLfloat GLfloat p
Definition: glext.h:6305



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019