Ravelin
Quat.h
1 /****************************************************************************
2  * Copyright 2013 Evan Drumwright
3  * This library is distributed under the terms of the Apache V2.0
4  * License (obtainable from http://www.apache.org/licenses/LICENSE-2.0).
5  ****************************************************************************/
6 
7 #ifndef QUAT
8 #error This class is not to be included by the user directly. Use Quatd.h or Quatf.h instead.
9 #endif
10 
11 class ORIGIN3;
12 class VECTORN;
13 class MATRIX3;
14 class AANGLE;
15 
17 
21 class QUAT
22 {
23  public:
24  QUAT();
25  QUAT(REAL x, REAL y, REAL z, REAL w);
26  QUAT(const QUAT& q) { operator=(q); }
27  QUAT(const VECTORN& v) { operator=(v); }
28  QUAT(const AANGLE& v) { operator=(v); }
29  QUAT(const MATRIX3& v) { operator=(v); }
30  bool unit() const;
31  static bool rel_equal(const QUAT& q1, const QUAT& q2, REAL tol = -1.0);
32  static QUAT zero();
33  void conjugate();
34  static QUAT conjugate(const QUAT& q);
35  static QUAT identity() { return QUAT((REAL) 0.0, (REAL) 0.0, (REAL) 0.0, (REAL) 1.0); }
36  void set_identity() { w = (REAL) 1.0; x = y = z = (REAL) 0.0; }
37  void slerp(const QUAT& q, REAL alpha);
38  void lerp(const QUAT& q, REAL alpha);
39  static QUAT slerp(const QUAT& q1, const QUAT& q2, REAL alpha);
40  static QUAT lerp(const QUAT& q1, const QUAT& q2, REAL alpha);
41  static QUAT rpy(REAL roll, REAL pitch, REAL yaw);
42  static QUAT rpy(const ORIGIN3& RPY) { return rpy(RPY[0],RPY[1],RPY[2]); }
43  static REAL calc_angle(const QUAT& q1, const QUAT& q2);
44  void to_rpy(REAL& roll, REAL& pitch, REAL& yaw) const;
45  void to_rpy(ORIGIN3& RPY) const { this->to_rpy(RPY[0],RPY[1],RPY[2]);}
46  QUAT& invert();
47  QUAT inverse() const { QUAT q = *this; q.invert(); return q; }
48  static QUAT invert(const QUAT& q);
49  REAL norm_sq() const;
50  REAL norm() const { return magnitude(); }
51  REAL norm_inf() const;
52  void normalize();
53  static QUAT normalize(const QUAT& q);
54  QUAT& operator=(const VECTORN& v);
55  QUAT& operator=(const AANGLE& a);
56  QUAT& operator=(const MATRIX3& m);
57  QUAT& operator=(const QUAT& q);
58  void calc_generalized_force(const VECTOR3& t, REAL gt[4]) const;
59  unsigned size() const { return 4; }
60  REAL& operator[](unsigned i);
61  const REAL& operator[](unsigned i) const;
62  QUAT operator-(const QUAT& q) const;
63  QUAT& operator-=(const QUAT& q);
64  QUAT operator+(const QUAT& q) const;
65  QUAT& operator+=(const QUAT& q);
66  QUAT operator*(const QUAT& q) const;
67  QUAT operator/(const QUAT& q) const;
68  ORIGIN3 operator*(const ORIGIN3& v) const;
69  QUAT operator*(REAL scalar) const;
70  QUAT operator/(REAL scalar) const { return operator*(1.0/scalar); }
71  QUAT& operator*=(const QUAT& q);
72  QUAT& operator*=(REAL scalar);
73  QUAT& operator/=(REAL scalar) { return operator*=(1.0/scalar); }
74  REAL magnitude() const;
75  static QUAT deriv(const QUAT& q, const VECTOR3& w);
76  static QUAT dderiv(const QUAT& q, const VECTOR3& omega, const VECTOR3& alpha);
77  static VECTOR3 to_omega(const QUAT& q, const QUAT& qd);
78 // static VECTORN to_vect(const QUAT& q);
79  VECTOR3 G_mult(REAL qx, REAL qy, REAL qz, REAL qw) const;
80  QUAT G_transpose_mult(const VECTOR3& v) const;
81  QUAT L_transpose_mult(const VECTOR3& v) const;
82 // MATRIXN& determine_G(MATRIXN& G) const;
83 // MATRIXN& determine_L(MATRIXN& L) const;
84  VECTOR3 L_mult(REAL qx, REAL qy, REAL qz, REAL qw) const;
85 
87  REAL x;
88 
90  REAL y;
91 
93  REAL z;
94 
96  REAL w;
97 
98  private:
99  static REAL sgn(REAL x) { return (x >= (REAL) 0.0) ? (REAL) 1.0 : (REAL) -1.0; }
100 
102 
107  static REAL safe_sqrt(REAL x) { return (x < (REAL) 0.0) ? (REAL) 0.0 : std::sqrt(x); }
108 };
109 
110 QUAT operator*(REAL scalar, const QUAT& q);
111 std::ostream& operator<<(std::ostream& out, const QUAT& q);
112 
VECTOR3 G_mult(REAL qx, REAL qy, REAL qz, REAL qw) const
Multiplies the matrix 'G' by a quaternion vector.
Definition: Quat.cpp:169
QUAT & operator*=(const QUAT &q)
Multiplies this by q and stores the result in this
Definition: Quat.cpp:580
QUAT & invert()
Computes the inverse orientation represented by this quaternion in place.
Definition: Quat.cpp:451
QUAT & operator-=(const QUAT &q)
Subtracts one quaternion from another.
Definition: Quat.cpp:504
QUAT()
Default constructor.
Definition: Quat.cpp:11
bool unit() const
Determines whether this is a unit quaternion.
Definition: Quat.cpp:491
Quaternion class used for orientation.
Definition: Quat.h:21
QUAT operator/(const QUAT &q) const
Multiplies inv(q) by this and returns the result.
Definition: Quat.cpp:524
QUAT & operator=(const VECTORN &v)
Sets the quaternion based on the given parameters.
Definition: Quat.cpp:646
REAL norm_sq() const
Calculates the squared magnitude of a quaternion.
Definition: Quat.cpp:594
static QUAT rpy(REAL roll, REAL pitch, REAL yaw)
Sets up the quaternion from roll-pitch-yaw.
Definition: Quat.cpp:369
static QUAT deriv(const QUAT &q, const VECTOR3 &w)
Computes the derivative of a quaternion given current orientation and angular velocity.
Definition: Quat.cpp:242
Class for representation of orientation by an angle around an axis.
Definition: AAngle.h:15
REAL norm_inf() const
Returns the l-infinity norm of the quaternion components.
Definition: Quat.cpp:587
REAL w
Fourth quaternion component.
Definition: Quat.h:96
REAL x
First quaternion component.
Definition: Quat.h:87
static QUAT dderiv(const QUAT &q, const VECTOR3 &omega, const VECTOR3 &alpha)
Calculates the second derivative of a quaternion.
Definition: Quat.cpp:269
REAL y
Second quaternion component.
Definition: Quat.h:90
void to_rpy(REAL &roll, REAL &pitch, REAL &yaw) const
Converts quaternion to roll/pitch/yaw.
Definition: Quat.cpp:393
QUAT operator-(const QUAT &q) const
Subtracts a quaternion from this
Definition: Quat.cpp:498
static QUAT zero()
Constructs a zero quaternion.
Definition: Quat.cpp:33
VECTOR3 L_mult(REAL qx, REAL qy, REAL qz, REAL qw) const
Multiplies the 3x4 matrix 'L' times a four dimensional array.
Definition: Quat.cpp:123
void conjugate()
Negates the value of each of the x, y, and z coordinates in place.
Definition: Quat.cpp:97
A generic N-dimensional floating point vector.
Definition: VectorN.h:16
static bool rel_equal(const QUAT &q1, const QUAT &q2, REAL tol=-1.0)
Determines whether two poses in 3D are relatively equivalent.
Definition: Quat.cpp:336
REAL magnitude() const
Calculates the magnitude of a quaternion.
Definition: Quat.cpp:600
QUAT L_transpose_mult(const VECTOR3 &v) const
Multiplies the transpose of the matrix 'L' by a vector.
Definition: Quat.cpp:217
QUAT & operator+=(const QUAT &q)
Adds this quaon to another and stores the result in this
Definition: Quat.cpp:517
A three-dimensional floating point vector used for representing points and vectors in 3D with associa...
Definition: Vector3.h:15
A three-dimensional floating point vector used for representing points and vectors in 3D and without ...
Definition: Origin3.h:16
static REAL calc_angle(const QUAT &q1, const QUAT &q2)
Calculates the angle between two orientations.
Definition: Quat.cpp:348
static VECTOR3 to_omega(const QUAT &q, const QUAT &qd)
Computes the angular velocity of a body given the current quaternion orientation and the quaternion v...
Definition: Quat.cpp:255
void lerp(const QUAT &q, REAL alpha)
Performs linear interpolation between this and q.
Definition: Quat.cpp:297
REAL z
Third quaterion component.
Definition: Quat.h:93
REAL & operator[](unsigned i)
Gets the i'th component of the quaternion.
Definition: Quat.cpp:47
A 3x3 matrix that may be used for orientation, inertia tensors, etc.
Definition: Matrix3.h:20
QUAT G_transpose_mult(const VECTOR3 &v) const
Multiplies the transpose of the matrix 'G' by a vector.
Definition: Quat.cpp:190
QUAT operator*(const QUAT &q) const
Multiplies this by q and returns the result.
Definition: Quat.cpp:530
void normalize()
Normalizes this quaternion in place.
Definition: Quat.cpp:464
QUAT operator+(const QUAT &q) const
Adds this quaternion to another and returns the result in a new quaternion.
Definition: Quat.cpp:511
void slerp(const QUAT &q, REAL alpha)
Performs spherical linear interpolation between this and q.
Definition: Quat.cpp:284