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

 Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 7b5ddf9de Fri May 29 14:02:56 2020 +0200 at vie may 29 14:15:09 CEST 2020