Ocean
ExponentialMap.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_MATH_EXPONENTIAL_MAP_H
9 #define META_OCEAN_MATH_EXPONENTIAL_MAP_H
10 
11 #include "ocean/math/Math.h"
12 #include "ocean/math/Quaternion.h"
13 #include "ocean/math/Rotation.h"
15 
16 namespace Ocean
17 {
18 
19 // Forward declaration.
20 template <typename T> class ExponentialMapT;
21 
22 /**
23  * Definition of the ExponentialMap object, depending on the OCEAN_MATH_USE_SINGLE_PRECISION flag either with single or double precision float data type.
24  * @see ExponentialMapT
25  * @ingroup math
26  */
28 
29 /**
30  * Instantiation of the ExponentialMapT template class using a double precision float data type.
31  * @see ExponentialMapT
32  * @ingroup math
33  */
35 
36 /**
37  * Instantiation of the ExponentialMapT template class using a single precision float data type.
38  * @see ExponentialMapT
39  * @ingroup math
40  */
42 
43 /**
44  * Definition of a typename alias for vectors with ExponentialMapT objects.
45  * @see ExponentialMapT
46  * @ingroup math
47  */
48 template <typename T>
49 using ExponentialMapsT = std::vector<ExponentialMapT<T>>;
50 
51 /**
52  * Definition of a vector holding ExponentialMap objects.
53  * @see ExponentialMap
54  * @ingroup math
55  */
56 typedef std::vector<ExponentialMap> ExponentialMaps;
57 
58 /**
59  * This class implements an exponential map defining a rotation by three parameters.<br>
60  * The three parameter define the rotation axis, while the length of the axis vector defines the rotation angle in radian.<br>
61  * @tparam T Floating point data type to be used
62  * @see SphericalExponentialMap, Rotation, Quaternion, Euler.
63  * @ingroup math
64  */
65 template <typename T>
67 {
68  public:
69 
70  /**
71  * Creates a default rotation.
72  */
73  ExponentialMapT() = default;
74 
75  /**
76  * Creates a rotation based on a given 3D axis with axis length defining the rotation angle in radian.
77  * @param rotation 3D vector defining the rotation
78  */
79  explicit inline ExponentialMapT(const VectorT3<T>& rotation);
80 
81  /**
82  * Creates a rotation based on a given 3D axis with axis length defining the rotation angle in radian.
83  * @param wx X component of the rotation axis
84  * @param wy Y component of the rotation axis
85  * @param wz Z component of the rotation axis
86  */
87  inline ExponentialMapT(const T wx, const T wy, const T wz);
88 
89  /**
90  * Creates a rotation based on a given 3D axis with length 1 and a given rotation angle.
91  * @param axis Normalized rotation axis
92  * @param angle The angle of the rotation, in radian
93  */
94  inline ExponentialMapT(const VectorT3<T>& axis, const T angle);
95 
96  /**
97  * Creates an rotation based on a given angle-axis rotation.
98  * @param rotation the rotation to be used, must be valid
99  */
100  explicit inline ExponentialMapT(const RotationT<T>& rotation);
101 
102  /**
103  * Creates a rotation based on a quaternion.
104  * @param rotation The rotation to be used, must be valid
105  */
106  explicit ExponentialMapT(const QuaternionT<T>& rotation);
107 
108  /**
109  * Creates a rotation based on a 3x3 rotation matrix.
110  * @param rotation The rotation matrix to be used, must be valid
111  */
112  explicit ExponentialMapT(const SquareMatrixT3<T>& rotation);
113 
114  /**
115  * Copies an exponential map with different element data type than T.
116  * @param exponentialMap The exponential map to copy
117  * @tparam U The element data type of the exponential map
118  */
119  template <typename U>
120  inline explicit ExponentialMapT(const ExponentialMapT<U>& exponentialMap) noexcept;
121 
122  /**
123  * Returns the (non-normalized) axis of this rotation object.
124  * Beware: This axis has a length equal to the rotation angle in radian.
125  * @return Non normalized rotation axis
126  */
127  inline const VectorT3<T>& axis() const;
128 
129  /**
130  * Returns the angle of this rotation object.
131  * @return Rotation angle in radian
132  */
133  inline T angle() const;
134 
135  /**
136  * Returns this rotation object as quaternion object.
137  * @return Equivalent quaternion rotation
138  */
139  inline QuaternionT<T> quaternion() const;
140 
141  /**
142  * Returns this rotation object as angle-axis object.
143  * @return Equivalent rotation
144  */
145  inline RotationT<T> rotation() const;
146 
147  /**
148  * Returns a pointer to the three rotation values of this object.
149  * @return The three rotation values
150  */
151  inline const T* data() const;
152 
153  /**
154  * Returns the individual rotation values of this map.
155  * @param index The index of the value to return, with range [0, 2]
156  * @return The requested index
157  */
158  inline const T& operator[](const unsigned int index) const;
159 
160  /**
161  * Returns the individual rotation values of this map.
162  * @param index The index of the value to return, with range [0, 2]
163  * @return The requested index
164  */
165  inline T& operator[](const unsigned int index);
166 
167  /**
168  * Adds two exponential map objects.
169  * @param exponentialMap Exponential map to be (component wise) added
170  * @return New exponential map object
171  */
172  inline ExponentialMapT<T> operator+(const ExponentialMapT<T>& exponentialMap) const;
173 
174  /**
175  * Adds two exponential map objects.
176  * @param exponentialMap Exponential map to be (component wise) added
177  * @return Reference to this object
178  */
179  inline ExponentialMapT<T>& operator+=(const ExponentialMapT<T>& exponentialMap);
180 
181  /**
182  * Subtracts two exponential map objects.
183  * @param exponentialMap Exponential map to be (component wise) subtracted
184  * @return New exponential map object
185  */
186  inline ExponentialMapT<T> operator-(const ExponentialMapT<T>& exponentialMap) const;
187 
188  /**
189  * Subtracts two exponential map objects.
190  * @param exponentialMap Exponential map to be (component wise) subtracted
191  * @return Reference to this object
192  */
193  inline ExponentialMapT<T>& operator-=(const ExponentialMapT<T>& exponentialMap);
194 
195  private:
196 
197  /// Axis defining the rotation normal while the length defines the rotation angle.
199 };
200 
201 template <typename T>
203  value_(rotation)
204 {
205  // nothing to do here
206 }
207 
208 template <typename T>
209 inline ExponentialMapT<T>::ExponentialMapT(const T wx, const T wy, const T wz) :
210  value_(wx, wy, wz)
211 {
212  // nothing to do here
213 }
214 
215 template <typename T>
216 inline ExponentialMapT<T>::ExponentialMapT(const VectorT3<T>& axis, const T angle) :
217  value_(axis * angle)
218 {
219  ocean_assert(axis.isUnit());
220 }
221 
222 template <typename T>
224  value_(rotation.axis() * rotation.angle())
225 {
226  ocean_assert(rotation.isValid());
227 }
228 
229 template <typename T>
231  value_(0, 0, 0)
232 {
233  const RotationT<T> angleAxis(rotation);
234  ocean_assert(angleAxis.isValid());
235 
236  const VectorT3<T> axis(angleAxis.axis());
237  ocean_assert(axis.isUnit());
238 
239  value_ = axis * angleAxis.angle();
240 }
241 
242 template <typename T>
244  value_(0, 0, 0)
245 {
246  const RotationT<T> angleAxis(rotation);
247  ocean_assert(angleAxis.isValid());
248 
249  const VectorT3<T> axis(angleAxis.axis());
250  ocean_assert(axis.isUnit());
251 
252  value_ = axis * angleAxis.angle();
253 }
254 
255 template <typename T>
256 template <typename U>
257 inline ExponentialMapT<T>::ExponentialMapT(const ExponentialMapT<U>& expontentialMap) noexcept :
258  value_(expontentialMap.axis())
259 {
260  // nothing to do here
261 }
262 
263 template <typename T>
265 {
266  return value_;
267 }
268 
269 template <typename T>
271 {
272  return value_.length();
273 }
274 
275 template <typename T>
277 {
278  const T angle = value_.length();
279 
280  if (NumericT<T>::isEqualEps(angle))
281  {
282  return QuaternionT<T>();
283  }
284 
285  return QuaternionT<T>(value_ / angle, angle);
286 }
287 
288 template <typename T>
290 {
291  const T angle = value_.length();
292 
293  if (NumericT<T>::isEqualEps(angle))
294  {
295  return RotationT<T>();
296  }
297 
298  return RotationT<T>(value_ / angle, angle);
299 }
300 
301 template <typename T>
302 inline const T* ExponentialMapT<T>::data() const
303 {
304  return value_.data();
305 }
306 
307 template <typename T>
308 inline const T& ExponentialMapT<T>::operator[](const unsigned int index) const
309 {
310  ocean_assert(index <= 2u);
311  return value_[index];
312 }
313 
314 template <typename T>
315 inline T& ExponentialMapT<T>::operator[](const unsigned int index)
316 {
317  ocean_assert(index <= 2u);
318  return value_[index];
319 }
320 
321 template <typename T>
323 {
324  return ExponentialMapT<T>(value_ + exponentialMap.value_);
325 }
326 
327 template <typename T>
329 {
330  value_ += exponentialMap.value_;
331  return *this;
332 }
333 
334 template <typename T>
336 {
337  return ExponentialMapT<T>(value_ - exponentialMap.value_);
338 }
339 
340 template <typename T>
342 {
343  value_ -= exponentialMap.value_;
344  return *this;
345 }
346 
347 }
348 
349 #endif // META_OCEAN_MATH_EXPONENTIAL_MAP_H
This class implements an exponential map defining a rotation by three parameters.
Definition: ExponentialMap.h:67
ExponentialMapT< T > operator+(const ExponentialMapT< T > &exponentialMap) const
Adds two exponential map objects.
Definition: ExponentialMap.h:322
const VectorT3< T > & axis() const
Returns the (non-normalized) axis of this rotation object.
Definition: ExponentialMap.h:264
QuaternionT< T > quaternion() const
Returns this rotation object as quaternion object.
Definition: ExponentialMap.h:276
VectorT3< T > value_
Axis defining the rotation normal while the length defines the rotation angle.
Definition: ExponentialMap.h:198
const T * data() const
Returns a pointer to the three rotation values of this object.
Definition: ExponentialMap.h:302
ExponentialMapT()=default
Creates a default rotation.
RotationT< T > rotation() const
Returns this rotation object as angle-axis object.
Definition: ExponentialMap.h:289
ExponentialMapT< T > operator-(const ExponentialMapT< T > &exponentialMap) const
Subtracts two exponential map objects.
Definition: ExponentialMap.h:335
const T & operator[](const unsigned int index) const
Returns the individual rotation values of this map.
Definition: ExponentialMap.h:308
ExponentialMapT< T > & operator-=(const ExponentialMapT< T > &exponentialMap)
Subtracts two exponential map objects.
Definition: ExponentialMap.h:341
T angle() const
Returns the angle of this rotation object.
Definition: ExponentialMap.h:270
ExponentialMapT< T > & operator+=(const ExponentialMapT< T > &exponentialMap)
Adds two exponential map objects.
Definition: ExponentialMap.h:328
This class provides basic numeric functionalities.
Definition: Numeric.h:57
This class implements a unit quaternion rotation.
Definition: Quaternion.h:100
This class implements a axis-angle rotation using floating point values.
Definition: Rotation.h:79
VectorT3< T > axis() const
Returns the axis of the rotation.
Definition: Rotation.h:740
T angle() const
Returns the angle of the rotation.
Definition: Rotation.h:746
bool isValid() const
Returns whether this rotation has valid parameters.
Definition: Rotation.h:665
This class implements a 3x3 square matrix.
Definition: SquareMatrix3.h:88
This class implements a vector with three elements.
Definition: Vector3.h:97
std::vector< ExponentialMapT< T > > ExponentialMapsT
Definition of a typename alias for vectors with ExponentialMapT objects.
Definition: ExponentialMap.h:49
ExponentialMapT< Scalar > ExponentialMap
Definition of the ExponentialMap object, depending on the OCEAN_MATH_USE_SINGLE_PRECISION flag either...
Definition: ExponentialMap.h:20
ExponentialMapT< float > ExponentialMapF
Instantiation of the ExponentialMapT template class using a single precision float data type.
Definition: ExponentialMap.h:41
std::vector< ExponentialMap > ExponentialMaps
Definition of a vector holding ExponentialMap objects.
Definition: ExponentialMap.h:56
ExponentialMapT< double > ExponentialMapD
Instantiation of the ExponentialMapT template class using a double precision float data type.
Definition: ExponentialMap.h:34
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15