Kernel/Math/Quaternion.h
Go to the documentation of this file.
1 // This file is part of the MercuryDPM project (https://www.mercurydpm.org).
2 // Copyright (c), The MercuryDPM Developers Team. All rights reserved.
3 // License: BSD 3-Clause License; see the LICENSE file in the root directory.
4 
10 #ifndef MECURYDPM_QUATERNION_H
11 #define MECURYDPM_QUATERNION_H
12 
13 #include <cmath>
14 #include <sstream>
15 #include <iostream>
16 #include <cstdlib>
17 #include "Math/Vector.h"
18 #include "Math/MatrixSymmetric.h"
19 
20 #include "GeneralDefine.h"
21 #include "SmallVector.h"
22 #include "SmallMatrix.h"
23 
42 {
43 public:
44 
61 
65  Quaternion();
66 
71 
77  {
79  }
80 
84  void setUnity();
85 
92  bool isUnity() const
93  {
94  return q0 == 1.0 && q1 == 0.0 && q2 == 0.0 && q3 == 0.0;
95  };
96 
100  Quaternion operator+(const Quaternion& a) const;
101 
105  Quaternion operator-(const Quaternion& a) const;
106 
110  Quaternion operator*(Mdouble a) const;
111 
115  Quaternion operator/(Mdouble a) const;
116 
121 
126 
131 
136 
140  void normalise();
141 
145  void setLength(Mdouble length);
146 
150  static Mdouble getDistance(const Quaternion& a, const Quaternion& b);
151 
155  static Mdouble getDistanceSquared(const Quaternion& a, const Quaternion& b);
156 
160  static Mdouble getLength(const Quaternion& a);
161 
165  static Mdouble getLengthSquared(const Quaternion& a);
166 
170  Mdouble getLength() const;
171 
175  Mdouble getLengthSquared() const;
176 
180  Mdouble getComponent(int index) const;
181 
184 
190 
191  void updateAngularDisplacement(Vec3D angularVelocityDt);
192 
197 
201  void setComponent(int index, double val);
202 
206  bool isEqualTo(const Quaternion& other, double tol) const;
207 
211  static Quaternion getUnitQuaternion(const Quaternion& a);
212 
216  friend std::ostream& operator<<(std::ostream& os, const Quaternion& a);
217 
221  friend std::istream& operator>>(std::istream& is, Quaternion& a);
222 
226  friend Quaternion operator+(Mdouble a, const Quaternion& b);
227 
231  friend Quaternion operator-(Mdouble a, const Quaternion& b);
232 
236  friend Quaternion operator-(const Quaternion& a);
237 
241  friend Quaternion operator*(Mdouble a, const Quaternion& b);
242 
247  Vec3D getEuler() const;
248 
249 
254  void setEuler(const Vec3D& e);
255 
260  Mdouble getAngleZ() const;
261 
266  void setAngleZ(Mdouble psi);
267 
273 
274  void rotateTensor(SmallMatrix<3, 3> I) const;
275 
280  Vec3D getAxis() const;
281 
287 
292  void getRotationMatrix(SmallMatrix<3, 3>& A) const; //note: A is output parameter
293 
297  void rotate(Vec3D& position) const;
298 
302  Vec3D rotate(const Vec3D& position) const;
303 
307  void rotate(SmallVector<3>& position) const;
308 
312  void rotateBack(Vec3D& position) const;
313 
317  Vec3D rotateBack(const Vec3D& position) const;
318 
322  Mdouble getDistance(Vec3D p, Vec3D p0) const;
323 
324  // Quaternion multiplication
326  double w = q.q0 * r.q0 - q.q1 * r.q1 - q.q2 * r.q2 - q.q3 * r.q3;
327  double x = q.q0 * r.q1 + q.q1 * r.q0 + q.q2 * r.q3 - q.q3 * r.q2;
328  double y = q.q0 * r.q2 - q.q1 * r.q3 + q.q2 * r.q0 + q.q3 * r.q1;
329  double z = q.q0 * r.q3 + q.q1 * r.q2 - q.q2 * r.q1 + q.q3 * r.q0;
330  return {w, x, y, z};
331  }
332 
338  static Quaternion fromAxisAndAngle(const Vec3D &axis, const Mdouble &angle)
339  {
340  double axisMagnitude = axis.getLength();
341  logger.assert_debug(axisMagnitude > 0, "Quaternion::fromAxisAndangle: Axis Magnitude is %. Cannot compute a quaternion.", axisMagnitude);
342 
343  Mdouble halfAngle = angle / 2.0;
344  Mdouble sinHalfAngle = std::sin(halfAngle);
345  Mdouble q0 = std::cos(halfAngle);
346  Mdouble q1 = axis.X * sinHalfAngle / axisMagnitude;
347  Mdouble q2 = axis.Y * sinHalfAngle / axisMagnitude;
348  Mdouble q3 = axis.Z * sinHalfAngle / axisMagnitude;
349  return Quaternion(q0, q1, q2, q3);
350  };
351 };
352 
353 #endif
AnnoyingScalar cos(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:136
AnnoyingScalar sin(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:137
Array< int, Dynamic, 1 > v
Definition: Array_initializer_list_vector_cxx11.cpp:1
Array< double, 1, 3 > e(1./3., 0.5, 2.)
double Mdouble
Definition: GeneralDefine.h:13
Logger< MERCURYDPM_LOGLEVEL > logger("MercuryKernel")
Definition of different loggers with certain modules. A user can define its own custom logger here.
Vector3f p0
Definition: MatrixBase_all.cpp:2
RowVector3d w
Definition: Matrix_resize_int.cpp:3
float * p
Definition: Tutorial_Map_using.cpp:9
Scalar * b
Definition: benchVecAdd.cpp:17
Matrix< SCALARA, Dynamic, Dynamic, opt_A > A
Definition: bench_gemm.cpp:47
Implementation of a 3D symmetric matrix.
Definition: MatrixSymmetric.h:16
This class contains the 4 components of a quaternion and the standard operators and functions needed ...
Definition: Kernel/Math/Quaternion.h:42
Quaternion operator/(Mdouble a) const
Divides by a scalar.
Definition: Quaternion.cc:77
void setEuler(const Vec3D &e)
Convert Euler angles to a quaternion. See Wikipedia for details.
Definition: Quaternion.cc:453
void setLength(Mdouble length)
Make this Quaternion a certain length |q|=length.
Definition: Quaternion.cc:157
Quaternion & operator*=(Mdouble a)
Multiplies *this by a scalar.
Definition: Quaternion.cc:115
Mdouble getLength() const
Calculates the length of this Quaternion: .
Definition: Quaternion.cc:280
void setUnity()
Sets quaternion value to (1,0,0,0)
Definition: Quaternion.cc:34
Quaternion & operator/=(Mdouble a)
Divides by a scalar.
Definition: Quaternion.cc:129
Mdouble getAngleZ() const
Converts a quaternion to the rotation angle in the XY plane (for Mercury2D). See Wikipedia for detail...
Definition: Quaternion.cc:467
friend std::ostream & operator<<(std::ostream &os, const Quaternion &a)
Adds elements to an output stream.
Definition: Quaternion.cc:321
bool isEqualTo(const Quaternion &other, double tol) const
Checks if the length this Quaternion is equal the length of other with a certain tolerance.
Definition: Quaternion.cc:271
void rotate(Vec3D &position) const
Definition: Quaternion.cc:550
Quaternion & operator+=(const Quaternion &a)
Adds another quaternion.
Definition: Quaternion.cc:87
Quaternion operator+(const Quaternion &a) const
Adds another quaternion and returns the result.
Definition: Quaternion.cc:47
Vec3D getEuler() const
Convert a quaternion to Euler angles. See Wikipedia for details.
Definition: Quaternion.cc:433
static Quaternion getUnitQuaternion(const Quaternion &a)
Returns a unit Quaternion based on a.
Definition: Quaternion.cc:305
static Mdouble getDistanceSquared(const Quaternion &a, const Quaternion &b)
Calculates the squared distance between two Quaternion: .
Definition: Quaternion.cc:182
Quaternion operator-(const Quaternion &a) const
Subtracts another quaternion and returns the result.
Definition: Quaternion.cc:57
static Quaternion multiplyQuaternions(const Quaternion &q, const Quaternion &r)
Definition: Kernel/Math/Quaternion.h:325
Vec3D getAxis() const
Converts the quaternions into a normal vector by rotating the vector x=(1,0,0); see See Wiki for deta...
Definition: Quaternion.cc:481
Quaternion & operator-=(const Quaternion &a)
Subtracts another quaternion.
Definition: Quaternion.cc:101
Mdouble getComponent(int index) const
Returns the requested component of this Quaternion.
Definition: Quaternion.cc:213
Quaternion(Vec3D normal)
Definition: Kernel/Math/Quaternion.h:76
Mdouble q1
the first component of the quaternion q = (q0,q1,q2,q3)
Definition: Kernel/Math/Quaternion.h:52
static Quaternion fromAxisAndAngle(const Vec3D &axis, const Mdouble &angle)
Creates a quaternion from a given rotation axis and an angle.
Definition: Kernel/Math/Quaternion.h:338
Quaternion angularVelocityBodyFixedFrameToAngularDisplacement(Vec3D v) const
Definition: Quaternion.cc:392
void setAngleZ(Mdouble psi)
Converts the rotation angle in the XY plane into a quaternion (for Mercury2D). See Wikipedia for deta...
Definition: Quaternion.cc:472
Mdouble q3
the third component of the quaternion q = (q0,q1,q2,q3)
Definition: Kernel/Math/Quaternion.h:60
Mdouble q2
the second component of the quaternion q = (q0,q1,q2,q3)
Definition: Kernel/Math/Quaternion.h:56
void rotateTensor(SmallMatrix< 3, 3 > I) const
Definition: Quaternion.cc:685
MatrixSymmetric3D rotateInverseInertiaTensor(const MatrixSymmetric3D &invI) const
Converts the inverse inertia tensor from the reference frame to the lab frame; see See QuaternionsWou...
Definition: Quaternion.cc:657
void normalise()
Makes this Quaternion unit length |q|=1.
Definition: Quaternion.cc:142
Quaternion()
Constructor; sets quaternion value to (1,0,0,0)
Definition: Quaternion.cc:11
Quaternion operator*(Mdouble a) const
Multiplies by a scalar.
Definition: Quaternion.cc:67
void rotateBack(Vec3D &position) const
Definition: Quaternion.cc:597
void updateAngularDisplacement(Vec3D angularVelocityDt)
Definition: Quaternion.cc:412
void setOrientationViaNormal(Vec3D normal)
Definition: Quaternion.cc:523
Vec3D applyCInverse(Quaternion q) const
Converts quaternion rate of change into an angular momentum omega.
Definition: Quaternion.cc:422
friend std::istream & operator>>(std::istream &is, Quaternion &a)
Adds elements to an input stream.
Definition: Quaternion.cc:334
Quaternion angularDisplacementTimeDerivative(Vec3D v) const
Converts an angular momentum v=omega into a quaternion rate of change, q(t+dt)-q(t)/dt.
Definition: Quaternion.cc:403
void setComponent(int index, double val)
Sets the requested component of this Quaternion to the requested value.
Definition: Quaternion.cc:239
static Mdouble getDistance(const Quaternion &a, const Quaternion &b)
Calculates the distance between two Quaternion: .
Definition: Quaternion.cc:169
Mdouble q0
the zeroth component of the quaternion q = (q0,q1,q2,q3)
Definition: Kernel/Math/Quaternion.h:48
void getRotationMatrix(SmallMatrix< 3, 3 > &A) const
Definition: Quaternion.cc:495
Mdouble getLengthSquared() const
Calculates the squared length of this Quaternion: .
Definition: Quaternion.cc:203
bool isUnity() const
Checks if the quaternion value is (1,0,0,0)
Definition: Kernel/Math/Quaternion.h:92
Data type for small dense matrix.
Definition: SmallMatrix.h:48
Definition: SmallVector.h:42
Definition: Kernel/Math/Vector.h:30
Mdouble Y
Definition: Kernel/Math/Vector.h:45
Mdouble Z
Definition: Kernel/Math/Vector.h:45
Mdouble X
the vector components
Definition: Kernel/Math/Vector.h:45
static Mdouble getLength(const Vec3D &a)
Calculates the length of a Vec3D: .
Definition: Vector.cc:350
Scalar * y
Definition: level1_cplx_impl.h:128
const Scalar * a
Definition: level2_cplx_impl.h:32
#define I
Definition: main.h:127
EIGEN_DEVICE_FUNC const Scalar & q
Definition: SpecialFunctionsImpl.h:2019
double angle(const double &t)
Angular position as a function of time t.
Definition: jeffery_orbit.cc:98
r
Definition: UniformPSDSelfTest.py:20
void normal(const Vector< double > &x, Vector< double > &normal)
Definition: free_surface_rotation.cc:65
val
Definition: calibrate.py:119
list x
Definition: plotDoE.py:28