3es  0.7
3esquaternion.h
1 //
2 // author: Kazys Stepanas
3 //
4 #ifndef _3ESQUATERNION_H_
5 #define _3ESQUATERNION_H_
6 
7 #include "3es-core.h"
8 
9 #include "3esvector3.h"
10 
11 namespace tes
12 {
14  template <typename T>
15  class Quaternion
16  {
17  public:
18  union
19  {
20  struct
21  {
23  T x, y, z, w;
24  };
26  T q[4];
27  };
28 
30  static const Quaternion<T> identity;
31 
33  inline Quaternion() {}
34 
37  inline Quaternion(bool) { *this = identity; }
38 
41  inline Quaternion(const Quaternion<T> &other) : x(other.x), y(other.y), z(other.z), w(other.w) {}
44  template <typename Q>
45  inline Quaternion(const Quaternion<Q> &other) : x(T(other.x)), y(T(other.y)), z(T(other.z)), w(T(other.w)) {}
51  inline Quaternion(const T &x, const T &y, const T &z, const T &w) : x(x), y(y), z(z), w(w) {}
55  inline Quaternion(const Vector3<T> &v, const T &w) : x(v.x), y(v.y), z(v.z), w(w) {}
59  inline Quaternion(const T *array4) : x(array4[0]), y(array4[1]), z(array4[2]), w(array4[3]) {}
60 
64  Quaternion(const Vector3<T> &from, const Vector3<T> &to);
65 
69  inline T &operator[](int index) { return q[index]; }
71  inline const T &operator[](int index) const { return q[index]; }
72 
76  inline Quaternion<T> &operator=(const Quaternion<T> &other) { x = other.x; y = other.y; z = other.z; w = other.w; return *this; }
77 
81  template <typename Q>
82  inline Quaternion<T> &operator=(const Quaternion<Q> &other) { x = T(other.x); y = T(other.y); z = T(other.z); w = T(other.w); return *this; }
83 
87  bool operator==(const Quaternion<T> &other) const;
91  bool operator!=(const Quaternion<T> &other) const;
92 
100  bool isEqual(const Quaternion<T> &other, const T &epsilon = Vector3<T>::Epsilon);
101 
104  bool isIdentity();
105 
111  void getAxisAngle(Vector3<T> &axis, T &angle) const;
112 
116  Quaternion<T> &setAxisAngle(const Vector3<T> &axis, const T &angle);
117 
121 
124  Quaternion<T> inverse() const;
125 
130 
134  Quaternion<T> conjugated() const;
135 
143  T normalise(const T &epsilon = Vector3<T>::Epsilon);
144 
152  Quaternion<T> normalised(const T &epsilon = Vector3<T>::Epsilon) const;
153 
155  T magnitude() const;
156 
158  T magnitudeSquared() const;
159 
161  T dot(const Quaternion<T> other) const;
162 
165  Vector3<T> transform(const Vector3<T> &v) const;
166 
169  Quaternion<T> &multiply(const T &scalar);
170 
176  static Quaternion<T> slerp(const Quaternion<T> &from, const Quaternion<T> &to, const T &t);
177 
178  Quaternion<T> operator *= (const Quaternion<T> &other);
179  inline Quaternion<T> operator *= (const T &scalar) { return multiply(scalar); }
180  };
181 
182 
187  template <typename T>
189 
194  template <typename T>
195  Vector3<T> operator * (const Quaternion<T> &a, const Vector3<T> &v);
196 
201 
202  template class _3es_coreAPI Quaternion<float>;
203  template class _3es_coreAPI Quaternion<double>;
204 }
205 
206 #include "3esquaternion.inl"
207 
208 #endif // _3ESQUATERNION_H_
T dot(const Quaternion< T > other) const
Calculates the dot product of this and other.
Quaternion< T > & operator=(const Quaternion< T > &other)
Simple assignment operator.
Definition: 3esquaternion.h:76
Quaternion< T > & invert()
Inverts this quaternion, making the counter rotation.
T magnitudeSquared() const
Returns the magnitude squared of this quaternion.
static Quaternion< T > slerp(const Quaternion< T > &from, const Quaternion< T > &to, const T &t)
Performs a spherical linear interpolation of one quaternion to another.
T x
Direct data member access.
Definition: 3esquaternion.h:23
bool isIdentity()
Checks if this quaternion is exactly identity.
Quaternion(const Quaternion< T > &other)
Copy constructor.
Definition: 3esquaternion.h:41
Quaternion()
Default constructor: undefined initialisation behaviour.
Definition: 3esquaternion.h:33
T & operator[](int index)
Index operator.
Definition: 3esquaternion.h:69
static const Quaternion< T > identity
The identity quaternion (0, 0, 0, 1).
Definition: 3esquaternion.h:30
Definition: 3esbounds.h:13
Quaternion(const T *array4)
Initialisation from a array of at least length 4.
Definition: 3esquaternion.h:59
Quaternion< T > & conjugate()
Sets this quaternion to its conjugate.
Quaternion< T > & setAxisAngle(const Vector3< T > &axis, const T &angle)
Sets this quaternion from an axis of rotation and the angle of rotation about that axis (radians)...
Matrix3< T > operator*(const Matrix3< T > &a, const Matrix3< T > &b)
Performs the matrix multiplication AB.
Quaternion(bool)
The identity constructor.
Definition: 3esquaternion.h:37
Quaternion(const Vector3< T > &v, const T &w)
Vector plus scalar initialisation.
Definition: 3esquaternion.h:55
Represents a vector in R3.
Definition: 3esvector3.h:14
Quaternion< T > & operator=(const Quaternion< Q > &other)
Simple assignment operator from a different numeric type.
Definition: 3esquaternion.h:82
Quaternion< T > normalised(const T &epsilon=Vector3< T >::Epsilon) const
Returns a normalised copy of this quaternion.
T normalise(const T &epsilon=Vector3< T >::Epsilon)
Attempts to normalise this quaternion.
Quaternion< T > conjugated() const
Calculates and returns the conjugate of this quaternion.
Quaternion< double > Quaterniond
Defines a double precision quaternion.
Definition: 3esquaternion.h:200
const T & operator[](int index) const
Definition: 3esquaternion.h:71
Vector3< T > transform(const Vector3< T > &v) const
Transforms v by this quaternion rotation.
T q[4]
Array representation of the vector members.
Definition: 3esquaternion.h:26
void getAxisAngle(Vector3< T > &axis, T &angle) const
Converts this quaternion into a axis of rotation and the rotation angle around that axis (radians)...
Quaternion< T > inverse() const
Calculates and returns the inverse of this quaternion.
bool operator==(const Quaternion< T > &other) const
Exact equality operator.
Quaternion< float > Quaternionf
Defines a single precision quaternion.
Definition: 3esquaternion.h:198
bool operator!=(const Quaternion< T > &other) const
Exact inequality operator.
A 4D rotational quaternion class.
Definition: 3esquaternion.h:15
Quaternion(const T &x, const T &y, const T &z, const T &w)
Per coordinate initialisation.
Definition: 3esquaternion.h:51
T magnitude() const
Returns the magnitude of this quaternion.
Quaternion< T > & multiply(const T &scalar)
Multiply all components of this quaternion by a scalar.
bool isEqual(const Quaternion< T > &other, const T &epsilon=Vector3< T >::Epsilon)
Equality test with error.
Quaternion(const Quaternion< Q > &other)
Copy constructor from a different numeric type.
Definition: 3esquaternion.h:45