Quaternion.h
Go to the documentation of this file.
1 //Copyright (c) 2013-2023, The MercuryDPM Developers Team. All rights reserved.
2 //For the list of developers, see <http://www.MercuryDPM.org/Team>.
3 //
4 //Redistribution and use in source and binary forms, with or without
5 //modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name MercuryDPM nor the
12 // names of its contributors may be used to endorse or promote products
13 // derived from this software without specific prior written permission.
14 //
15 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 //ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 //WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 //DISCLAIMED. IN NO EVENT SHALL THE MERCURYDPM DEVELOPERS TEAM BE LIABLE FOR ANY
19 //DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 //(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 //LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 //ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 //(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 //SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
31 #ifndef MECURYDPM_QUATERNION_H
32 #define MECURYDPM_QUATERNION_H
33 
34 #include <cmath>
35 #include <sstream>
36 #include <iostream>
37 #include <cstdlib>
38 #include "Math/Vector.h"
39 #include "Math/MatrixSymmetric.h"
40 
41 #include "GeneralDefine.h"
42 #include "SmallVector.h"
43 #include "SmallMatrix.h"
44 
63 {
64 public:
65 
82 
86  Quaternion();
87 
92 
97  Quaternion(Vec3D normal)
98  {
100  }
101 
105  void setUnity();
106 
113  bool isUnity() const
114  {
115  return q0 == 1.0 && q1 == 0.0 && q2 == 0.0 && q3 == 0.0;
116  };
117 
121  Quaternion operator+(const Quaternion& a) const;
122 
126  Quaternion operator-(const Quaternion& a) const;
127 
131  Quaternion operator*(Mdouble a) const;
132 
136  Quaternion operator/(Mdouble a) const;
137 
141  Quaternion& operator+=(const Quaternion& a);
142 
146  Quaternion& operator-=(const Quaternion& a);
147 
152 
157 
161  void normalise();
162 
166  void setLength(Mdouble length);
167 
171  static Mdouble getDistance(const Quaternion& a, const Quaternion& b);
172 
176  static Mdouble getDistanceSquared(const Quaternion& a, const Quaternion& b);
177 
181  static Mdouble getLength(const Quaternion& a);
182 
186  static Mdouble getLengthSquared(const Quaternion& a);
187 
191  Mdouble getLength() const;
192 
196  Mdouble getLengthSquared() const;
197 
201  Mdouble getComponent(int index) const;
202 
205 
211 
212  void updateAngularDisplacement(Vec3D angularVelocityDt);
213 
217  Vec3D applyCInverse(Quaternion q) const;
218 
222  void setComponent(int index, double val);
223 
227  bool isEqualTo(const Quaternion& other, double tol) const;
228 
232  static Quaternion getUnitQuaternion(const Quaternion& a);
233 
237  friend std::ostream& operator<<(std::ostream& os, const Quaternion& a);
238 
242  friend std::istream& operator>>(std::istream& is, Quaternion& a);
243 
247  friend Quaternion operator+(Mdouble a, const Quaternion& b);
248 
252  friend Quaternion operator-(Mdouble a, const Quaternion& b);
253 
257  friend Quaternion operator-(const Quaternion& a);
258 
262  friend Quaternion operator*(Mdouble a, const Quaternion& b);
263 
268  Vec3D getEuler() const;
269 
270 
275  void setEuler(const Vec3D& e);
276 
281  Mdouble getAngleZ() const;
282 
287  void setAngleZ(Mdouble psi);
288 
294 
295  void rotateTensor(SmallMatrix<3, 3> I) const;
296 
301  Vec3D getAxis() const;
302 
307  void setOrientationViaNormal(Vec3D normal);
308 
313  void getRotationMatrix(SmallMatrix<3, 3>& A) const; //note: A is output parameter
314 
318  void rotate(Vec3D& position) const;
319 
323  Vec3D rotate(const Vec3D& position) const;
324 
328  void rotate(SmallVector<3>& position) const;
329 
333  void rotateBack(Vec3D& position) const;
334 
338  Vec3D rotateBack(const Vec3D& position) const;
339 
343  Mdouble getDistance(Vec3D p, Vec3D p0) const;
344 };
345 
346 #endif
double Mdouble
Definition: GeneralDefine.h:34
@ A
Definition: StatisticsVector.h:42
Implementation of a 3D symmetric matrix.
Definition: MatrixSymmetric.h:37
This class contains the 4 components of a quaternion and the standard operators and functions needed ...
Definition: Quaternion.h:63
Quaternion operator/(Mdouble a) const
Divides by a scalar.
Definition: Quaternion.cc:96
void setEuler(const Vec3D &e)
Convert Euler angles to a quaternion. See Wikipedia for details.
Definition: Quaternion.cc:473
void setLength(Mdouble length)
Make this Quaternion a certain length |q|=length.
Definition: Quaternion.cc:176
Quaternion & operator*=(Mdouble a)
Multiplies *this by a scalar.
Definition: Quaternion.cc:134
Mdouble getLength() const
Calculates the length of this Quaternion: .
Definition: Quaternion.cc:299
void setUnity()
Sets quaternion value to (1,0,0,0)
Definition: Quaternion.cc:53
Quaternion & operator/=(Mdouble a)
Divides by a scalar.
Definition: Quaternion.cc:148
Mdouble getAngleZ() const
Converts a quaternion to the rotation angle in the XY plane (for Mercury2D). See Wikipedia for detail...
Definition: Quaternion.cc:487
friend std::ostream & operator<<(std::ostream &os, const Quaternion &a)
Adds elements to an output stream.
Definition: Quaternion.cc:340
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:290
void rotate(Vec3D &position) const
Definition: Quaternion.cc:563
Quaternion & operator+=(const Quaternion &a)
Adds another quaternion.
Definition: Quaternion.cc:106
Quaternion operator+(const Quaternion &a) const
Adds another quaternion and returns the result.
Definition: Quaternion.cc:66
Vec3D getEuler() const
Convert a quaternion to Euler angles. See Wikipedia for details.
Definition: Quaternion.cc:452
static Quaternion getUnitQuaternion(const Quaternion &a)
Returns a unit Quaternion based on a.
Definition: Quaternion.cc:324
static Mdouble getDistanceSquared(const Quaternion &a, const Quaternion &b)
Calculates the squared distance between two Quaternion: .
Definition: Quaternion.cc:201
Quaternion operator-(const Quaternion &a) const
Subtracts another quaternion and returns the result.
Definition: Quaternion.cc:76
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:501
Quaternion & operator-=(const Quaternion &a)
Subtracts another quaternion.
Definition: Quaternion.cc:120
Mdouble getComponent(int index) const
Returns the requested component of this Quaternion.
Definition: Quaternion.cc:232
Quaternion(Vec3D normal)
Definition: Quaternion.h:97
Mdouble q1
the first component of the quaternion q = (q0,q1,q2,q3)
Definition: Quaternion.h:73
Quaternion angularVelocityBodyFixedFrameToAngularDisplacement(Vec3D v) const
Definition: Quaternion.cc:411
void setAngleZ(Mdouble psi)
Converts the rotation angle in the XY plane into a quaternion (for Mercury2D). See Wikipedia for deta...
Definition: Quaternion.cc:492
Mdouble q3
the third component of the quaternion q = (q0,q1,q2,q3)
Definition: Quaternion.h:81
Mdouble q2
the second component of the quaternion q = (q0,q1,q2,q3)
Definition: Quaternion.h:77
void rotateTensor(SmallMatrix< 3, 3 > I) const
Definition: Quaternion.cc:690
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:662
void normalise()
Makes this Quaternion unit length |q|=1.
Definition: Quaternion.cc:161
Quaternion()
Constructor; sets quaternion value to (1,0,0,0)
Definition: Quaternion.cc:30
Quaternion operator*(Mdouble a) const
Multiplies by a scalar.
Definition: Quaternion.cc:86
void rotateBack(Vec3D &position) const
Definition: Quaternion.cc:610
void updateAngularDisplacement(Vec3D angularVelocityDt)
Definition: Quaternion.cc:431
void setOrientationViaNormal(Vec3D normal)
Definition: Quaternion.cc:536
Vec3D applyCInverse(Quaternion q) const
Converts quaternion rate of change into an angular momentum omega.
Definition: Quaternion.cc:441
friend std::istream & operator>>(std::istream &is, Quaternion &a)
Adds elements to an input stream.
Definition: Quaternion.cc:353
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:422
void setComponent(int index, double val)
Sets the requested component of this Quaternion to the requested value.
Definition: Quaternion.cc:258
static Mdouble getDistance(const Quaternion &a, const Quaternion &b)
Calculates the distance between two Quaternion: .
Definition: Quaternion.cc:188
Mdouble q0
the zeroth component of the quaternion q = (q0,q1,q2,q3)
Definition: Quaternion.h:69
void getRotationMatrix(SmallMatrix< 3, 3 > &A) const
Definition: Quaternion.cc:508
Mdouble getLengthSquared() const
Calculates the squared length of this Quaternion: .
Definition: Quaternion.cc:222
bool isUnity() const
Checks if the quaternion value is (1,0,0,0)
Definition: Quaternion.h:113
Data type for small dense matrix.
Definition: SmallMatrix.h:68
Definition: SmallVector.h:62
Definition: Vector.h:51