Nazara Engine  0.4
A fast, complete, cross-platform API designed for game development
Quaternion.hpp
1 // Copyright (C) 2017 Rémi Bèges - Jérôme Leclercq
2 // This file is part of the "Nazara Engine - Mathematics module"
3 // For conditions of distribution and use, see copyright notice in Config.hpp
4 
5 #pragma once
6 
7 #ifndef NAZARA_QUATERNION_HPP
8 #define NAZARA_QUATERNION_HPP
9 
10 #include <Nazara/Core/String.hpp>
11 
12 namespace Nz
13 {
14  struct SerializationContext;
15 
16  template<typename T> class EulerAngles;
17  template<typename T> class Vector3;
18 
19  template<typename T> class Quaternion
20  {
21  public:
22  Quaternion() = default;
23  Quaternion(T W, T X, T Y, T Z);
24  Quaternion(const EulerAngles<T>& angles);
25  Quaternion(T angle, const Vector3<T>& axis);
26  Quaternion(const T quat[4]);
27  //Quaternion(const Matrix3<T>& mat);
28  template<typename U> explicit Quaternion(const Quaternion<U>& quat);
29  Quaternion(const Quaternion& quat) = default;
30  ~Quaternion() = default;
31 
32  Quaternion& ComputeW();
33  Quaternion& Conjugate();
34 
35  T DotProduct(const Quaternion& vec) const;
36 
37  Quaternion GetConjugate() const;
38  Quaternion GetInverse() const;
39  Quaternion GetNormal(T* length = nullptr) const;
40 
41  Quaternion& Inverse();
42 
43  Quaternion& MakeIdentity();
44  Quaternion& MakeRotationBetween(const Vector3<T>& from, const Vector3<T>& to);
45  Quaternion& MakeZero();
46 
47  T Magnitude() const;
48 
49  Quaternion& Normalize(T* length = nullptr);
50 
51  Quaternion& Set(T W, T X, T Y, T Z);
52  Quaternion& Set(const EulerAngles<T>& angles);
53  Quaternion& Set(T angle, const Vector3<T>& normalizedAxis);
54  Quaternion& Set(const T quat[4]);
55  //Quaternion& Set(const Matrix3<T>& mat);
56  Quaternion& Set(const Quaternion& quat);
57  template<typename U> Quaternion& Set(const Quaternion<U>& quat);
58 
59  T SquaredMagnitude() const;
60 
61  EulerAngles<T> ToEulerAngles() const;
62  //Matrix3<T> ToRotationMatrix() const;
63  String ToString() const;
64 
65  Quaternion& operator=(const Quaternion& quat) = default;
66 
67  Quaternion operator+(const Quaternion& quat) const;
68  Quaternion operator*(const Quaternion& quat) const;
69  Vector3<T> operator*(const Vector3<T>& vec) const;
70  Quaternion operator*(T scale) const;
71  Quaternion operator/(const Quaternion& quat) const;
72 
73  Quaternion& operator+=(const Quaternion& quat);
74  Quaternion& operator*=(const Quaternion& quat);
75  Quaternion& operator*=(T scale);
76  Quaternion& operator/=(const Quaternion& quat);
77 
78  bool operator==(const Quaternion& quat) const;
79  bool operator!=(const Quaternion& quat) const;
80 
81  static Quaternion Identity();
82  static Quaternion Lerp(const Quaternion& from, const Quaternion& to, T interpolation);
83  static Quaternion Normalize(const Quaternion& quat, T* length = nullptr);
84  static Quaternion RotationBetween(const Vector3<T>& from, const Vector3<T>& to);
85  static Quaternion Slerp(const Quaternion& from, const Quaternion& to, T interpolation);
86  static Quaternion Zero();
87 
88  T w, x, y, z;
89  };
90 
91  typedef Quaternion<double> Quaterniond;
92  typedef Quaternion<float> Quaternionf;
93 
94  template<typename T> bool Serialize(SerializationContext& context, const Quaternion<T>& quat);
95  template<typename T> bool Unserialize(SerializationContext& context, Quaternion<T>* quat);
96 }
97 
98 template<typename T> std::ostream& operator<<(std::ostream& out, const Nz::Quaternion<T>& quat);
99 
100 #include <Nazara/Math/Quaternion.inl>
101 
102 #endif // NAZARA_QUATERNION_HPP
TODO: Inherit SoundEmitter from Node.
Definition: Algorithm.hpp:12
Quaternion GetInverse() const
Gets the inverse of this quaternion.
Definition: Quaternion.inl:180
T SquaredMagnitude() const
Calculates the squared magnitude (length) of the quaternion.
Definition: Quaternion.inl:466
T DotProduct(const Quaternion &vec) const
Calculates the dot (scalar) product with two quaternions.
Definition: Quaternion.inl:147
bool Unserialize(SerializationContext &context, bool *value)
Unserializes a boolean.
Definition: Algorithm.inl:279
Quaternion & ComputeW()
Computes the w component of the quaternion to make it unit.
Definition: Quaternion.inl:108
Quaternion operator/(const Quaternion &quat) const
Divides the quaternion with other quaternion.
Definition: Quaternion.inl:589
Quaternion & Inverse()
Inverts this quaternion.
Definition: Quaternion.inl:218
EulerAngles< T > ToEulerAngles() const
Converts this quaternion to Euler angles representation.
Definition: Quaternion.inl:479
Quaternion & MakeZero()
Makes the quaternion (0, 0, 0, 0)
Definition: Quaternion.inl:293
Quaternion & Normalize(T *length=nullptr)
Normalizes the current quaternion.
Definition: Quaternion.inl:323
Quaternion operator*(const Quaternion &quat) const
Multiplies of the quaternion with other quaternion.
Definition: Quaternion.inl:535
bool operator==(const Quaternion &quat) const
Compares the quaternion to other one.
Definition: Quaternion.inl:654
Quaternion & Conjugate()
Returns the rotational conjugate of this quaternion.
Definition: Quaternion.inl:130
Quaternion & MakeIdentity()
Makes the quaternion (1, 0, 0, 0)
Definition: Quaternion.inl:242
Quaternion & operator*=(const Quaternion &quat)
Multiplies of the quaternion with other quaternion.
Definition: Quaternion.inl:615
Quaternion GetConjugate() const
Gets the rotational conjugate of this quaternion.
Definition: Quaternion.inl:162
Quaternion & operator+=(const Quaternion &quat)
Adds the components of the quaternion with other quaternion.
Definition: Quaternion.inl:602
static Quaternion Lerp(const Quaternion &from, const Quaternion &to, T interpolation)
Interpolates the quaternion to other one with a factor of interpolation.
Definition: Quaternion.inl:706
Quaternion & MakeRotationBetween(const Vector3< T > &from, const Vector3< T > &to)
Makes this quaternion to the rotation required to rotate direction Vector3 from to direction Vector3 ...
Definition: Quaternion.inl:258
bool operator!=(const Quaternion &quat) const
Compares the quaternion to other one.
Definition: Quaternion.inl:670
Quaternion & Set(T W, T X, T Y, T Z)
Sets the components of the quaternion.
Definition: Quaternion.inl:352
bool Serialize(SerializationContext &context, bool value)
Serializes a boolean.
Definition: Algorithm.inl:214
static Quaternion Slerp(const Quaternion &from, const Quaternion &to, T interpolation)
Interpolates spherically the quaternion to other one with a factor of interpolation.
Definition: Quaternion.inl:775
static Quaternion Zero()
Shorthand for the quaternion (0, 0, 0, 0)
Definition: Quaternion.inl:828
static Quaternion RotationBetween(const Vector3< T > &from, const Vector3< T > &to)
Gets the rotation required to rotate direction Vector3 from to direction Vector3 to.
Definition: Quaternion.inl:752
Quaternion GetNormal(T *length=nullptr) const
Gets the normalization of this quaternion.
Definition: Quaternion.inl:200
Quaternion & operator/=(const Quaternion &quat)
Divides the quaternion with other quaternion.
Definition: Quaternion.inl:641
Quaternion operator+(const Quaternion &quat) const
Adds the components of the quaternion with other quaternion.
Definition: Quaternion.inl:516
T Magnitude() const
Calculates the magnitude (length) of the quaternion.
Definition: Quaternion.inl:306
String ToString() const
Gives a string representation.
Definition: Quaternion.inl:501
static Quaternion Identity()
Shorthand for the quaternion (1, 0, 0, 0)
Definition: Quaternion.inl:683