Main MRPT website > C++ reference
MRPT logo
slerp.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | The Mobile Robot Programming Toolkit (MRPT) C++ library |
3  | |
4  | http://www.mrpt.org/ |
5  | |
6  | Copyright (C) 2005-2012 University of Malaga |
7  | |
8  | This software was written by the Machine Perception and Intelligent |
9  | Robotics Lab, University of Malaga (Spain). |
10  | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> |
11  | |
12  | This file is part of the MRPT project. |
13  | |
14  | MRPT is free software: you can redistribute it and/or modify |
15  | it under the terms of the GNU General Public License as published by |
16  | the Free Software Foundation, either version 3 of the License, or |
17  | (at your option) any later version. |
18  | |
19  | MRPT is distributed in the hope that it will be useful, |
20  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
21  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22  | GNU General Public License for more details. |
23  | |
24  | You should have received a copy of the GNU General Public License |
25  | along with MRPT. If not, see <http://www.gnu.org/licenses/>. |
26  | |
27  +---------------------------------------------------------------------------+ */
28 #ifndef mrpt_math_slerp_H
29 #define mrpt_math_slerp_H
30 
31 #include <mrpt/math/CQuaternion.h>
32 
33 namespace mrpt
34 {
35  namespace poses { class CPose3D; class CPose3DQuat; }
36 
37  namespace math
38  {
39  using namespace mrpt::poses;
40 
41  /** \addtogroup geometry_grp
42  * @{ */
43 
44  /** @name SLERP (Spherical Linear Interpolation) functions
45  @{ */
46 
47  /** SLERP interpolation between two quaternions
48  * \param[in] q0 The quaternion for t=0
49  * \param[in] q1 The quaternion for t=1
50  * \param[in] t A "time" parameter, in the range [0,1].
51  * \param[out] q The output, interpolated quaternion.
52  * \tparam T The type of the quaternion (e.g. float, double).
53  * \exception std::exception Only in Debug, if t is not in the valid range.
54  * \sa http://en.wikipedia.org/wiki/Slerp
55  */
56  template <typename T>
57  void slerp(
58  const CQuaternion<T> & q0,
59  const CQuaternion<T> & q1,
60  const double t,
61  CQuaternion<T> & q)
62  {
63  ASSERTDEB_(t>=0 && t<=1)
64  // See: http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/slerp/index.htm
65  // Angle between q0-q1:
66  double cosHalfTheta = q0[0]*q1[0]+q0[1]*q1[1]+q0[2]*q1[2]+q0[3]*q1[3];
67  // if qa=qb or qa=-qb then theta = 0 and we can return qa
68  if (std::abs(cosHalfTheta) >= 1.0)
69  {
70  q = q0;
71  return;
72  }
73  bool reverse_q1 = false;
74  if (cosHalfTheta < 0) // Always follow the shortest path
75  {
76  reverse_q1 = true;
77  cosHalfTheta = -cosHalfTheta;
78  }
79  // Calculate temporary values.
80  const double halfTheta = acos(cosHalfTheta);
81  const double sinHalfTheta = std::sqrt(1.0 - square(cosHalfTheta));
82  // if theta = 180 degrees then result is not fully defined
83  // we could rotate around any axis normal to qa or qb
84  if (std::abs(sinHalfTheta) < 0.001)
85  {
86  if (!reverse_q1)
87  for (int i=0;i<4;i++) q[i] = (1-t)*q0[i] + t*q1[i];
88  else for (int i=0;i<4;i++) q[i] = (1-t)*q0[i] - t*q1[i];
89  return;
90  }
91  const double A = sin((1-t) * halfTheta)/sinHalfTheta;
92  const double B = sin(t*halfTheta)/sinHalfTheta;
93  if (!reverse_q1)
94  for (int i=0;i<4;i++) q[i] = A*q0[i] + B*q1[i];
95  else for (int i=0;i<4;i++) q[i] = A*q0[i] - B*q1[i];
96  }
97 
98  /** SLERP interpolation between two 6D poses - like mrpt::math::slerp for quaternions, but interpolates the [X,Y,Z] coordinates as well.
99  * \param[in] p0 The pose for t=0
100  * \param[in] p1 The pose for t=1
101  * \param[in] t A "time" parameter, in the range [0,1].
102  * \param[out] p The output, interpolated pose.
103  * \exception std::exception Only in Debug, if t is not in the valid range.
104  */
105  void BASE_IMPEXP slerp(
106  const CPose3D & q0,
107  const CPose3D & q1,
108  const double t,
109  CPose3D & p);
110 
111  //! \overload
112  void BASE_IMPEXP slerp(
113  const CPose3DQuat & q0,
114  const CPose3DQuat & q1,
115  const double t,
116  CPose3DQuat & p);
117 
118  /** @} */
119 
120  /** @} */ // grouping
121  }
122 }
123 #endif



Page generated by Doxygen 1.8.3 for MRPT 0.9.6 SVN: at Fri Feb 15 22:05:02 EST 2013