MIRA
Lerp.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Contact: info@mira-project.org
9  *
10  * Commercial Usage:
11  * Licensees holding valid commercial licenses may use this file in
12  * accordance with the commercial license agreement provided with the
13  * software or, alternatively, in accordance with the terms contained in
14  * a written agreement between you and MLAB or NICR.
15  *
16  * GNU General Public License Usage:
17  * Alternatively, this file may be used under the terms of the GNU
18  * General Public License version 3.0 as published by the Free Software
19  * Foundation and appearing in the file LICENSE.GPL3 included in the
20  * packaging of this file. Please review the following information to
21  * ensure the GNU General Public License version 3.0 requirements will be
22  * met: http://www.gnu.org/copyleft/gpl.html.
23  * Alternatively you may (at your option) use any later version of the GNU
24  * General Public License if such license has been publicly approved by
25  * MLAB and NICR (or its successors, if any).
26  *
27  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
28  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
29  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
30  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
33  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
34  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
35  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
36  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
37  */
38 
49 #ifndef _MIRA_LERP_H_
50 #define _MIRA_LERP_H_
51 
52 #include <platform/Platform.h>
53 
54 #include <Eigen/Eigen>
55 #include <Eigen/Geometry>
56 
57 #include <math/Angle.h>
58 
59 namespace mira {
60 
62 
75 template<typename T, typename S>
76 inline T lerp(const T& a, const T& b, S alpha)
77 {
78  return (1-alpha) * a + alpha * b;
79 }
80 
81 // specialization for other types follow:
82 
83 template <typename T, typename UnitTag, typename Derived, typename S>
84 inline Derived lerpAngle(const AngleBase<T,UnitTag,Derived>& a,
86  S alpha)
87 {
88  T diff = b.smallestDifferenceValue(a);
89  return Derived(a.value() + diff*alpha);
90 }
91 
92 // we have to write the specializations for each angle type explicitly.
93 // Renaming the above lerpAngle method to lerp() will not work, since the
94 // compilers would then choose the generic lerp() method as best matching
95 // method.
96 
97 template<typename T, typename S>
98 inline Degree<T> lerp(const Degree<T>& a, const Degree<T>& b, S alpha) {
99  return lerpAngle(a,b,alpha);
100 }
101 
102 template<typename T, typename S>
104  const SignedDegree<T>& b, S alpha) {
105  return lerpAngle(a,b,alpha);
106 }
107 
108 template<typename T, typename S>
109 inline Radian<T> lerp(const Radian<T>& a, const Radian<T>& b, S alpha) {
110  return lerpAngle(a,b,alpha);
111 }
112 
113 template<typename T, typename S>
115  const SignedRadian<T>& b, S alpha) {
116  return lerpAngle(a,b,alpha);
117 }
118 
119 template<typename T, typename S>
120 inline Angle<T> lerp(const Angle<T>& a, const Angle<T>& b, S alpha) {
121  return lerpAngle(a,b,alpha);
122 }
123 
124 template<typename T, typename S>
126  const SignedAngle<T>& b, S alpha) {
127  return lerpAngle(a,b,alpha);
128 }
129 
130 
131 template<typename T, typename S>
133  const Eigen::Rotation2D<T>& b,
134  S alpha)
135 {
136  T result = lerp(SignedAngle<T>(a.angle()),
137  SignedAngle<T>(b.angle()),
138  alpha).value();
139  return Eigen::Rotation2D<T>(result);
140 }
141 
142 template<typename T, typename S>
144  const Eigen::Quaternion<T>& b,
145  S alpha)
146 {
147  return a.slerp(alpha, b);
148 }
149 
151 
152 } // namespace
153 
154 #endif
Quaternion slerp(Scalar t, const Quaternion &other) const
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
Signed angle that is represented using degrees.
Definition: Angle.h:571
T lerp(const T &a, const T &b, S alpha)
Linear interpolation of different types like scalars, angles and rotations, vectors, etc.
Definition: Lerp.h:76
Derived lerpAngle(const AngleBase< T, UnitTag, Derived > &a, const AngleBase< T, UnitTag, Derived > &b, S alpha)
Definition: Lerp.h:84
const T & value() const
Returns the raw angle value given in the native unit of the angle class.
Definition: Angle.h:252
Unsigned angle that is represented using degrees.
Definition: Angle.h:558
Implementations of angles (values in periodic interval of width 2*pi) with arbitrary base type...
Signed angle that is represented using radians.
Definition: Angle.h:651
Unsigned angle that is represented using radians.
Definition: Angle.h:638
Signed angle that is represented using radians.
Definition: Angle.h:733
T smallestDifferenceValue(const Derived &other) const
Returns the signed difference angle between this angle and the specified other angle that has the sma...
Definition: Angle.h:406
Unsigned angle that is represented using radians.
Definition: Angle.h:717
Scalar & angle()
Base class template for derived Angle implementations.
Definition: Angle.h:183
Platform dependent defines and macros.