Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
Quaternion.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2018 DIVIDE-Studio
3 Copyright (c) 2009 Ionut Cava
4
5 This file is part of DIVIDE Framework.
6
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software
9 and associated documentation files (the "Software"), to deal in the Software
10 without restriction,
11 including without limitation the rights to use, copy, modify, merge, publish,
12 distribute, sublicense,
13 and/or sell copies of the Software, and to permit persons to whom the
14 Software is furnished to do so,
15 subject to the following conditions:
16
17 The above copyright notice and this permission notice shall be included in
18 all copies or substantial portions of the Software.
19
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED,
22 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
23 PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
25 DAMAGES OR OTHER LIABILITY,
26 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
27 IN CONNECTION WITH THE SOFTWARE
28 OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30 */
31
32#pragma once
33#ifndef DVD_QUATERNION_H_
34#define DVD_QUATERNION_H_
35
36/*
37http://gpwiki.org/index.php/OpenGL:Tutorials:Using_Quaternions_to_represent_rotation
38Quaternion class based on code from " OpenGL:Tutorials:Using Quaternions to
39represent rotation "
40*/
41
42namespace Divide {
43
44template <typename T>
46 static_assert(ValidMathType<T>, "Invalid base type!");
47
48 public:
49 Quaternion() noexcept;
50 Quaternion(T x, T y, T z, T w) noexcept;
51 explicit Quaternion(const vec4<T>& values) noexcept;
52
53 template<typename U = T> requires std::is_same_v<U, F32>
54 explicit Quaternion(__m128 reg) noexcept : _elements(reg) {}
55
56 explicit Quaternion(const mat3<T>& rotationMatrix) noexcept;
57 Quaternion(const vec3<T>& axis, Angle::DEGREES<T> angle) noexcept;
58 Quaternion(const vec3<T>& forward, const vec3<T>& up = WORLD_Y_AXIS) noexcept;
60 Quaternion(const Quaternion& q) noexcept;
61
62 Quaternion& operator=(const Quaternion& q) noexcept;
63
64 [[nodiscard]] T dot(const Quaternion& rq) const noexcept;
65 [[nodiscard]] T magnitude() const;
66 [[nodiscard]] T magnituteSQ() const;
67
68 [[nodiscard]] bool compare(const Quaternion& rq, Angle::DEGREES<T> tolerance = 1e-3f) const;
69
70 void set(const vec4<T>& values) noexcept;
71 void set(T x, T y, T z, T w) noexcept;
72 void set(const Quaternion& q) noexcept;
73
75 //do anything
77 void normalize() noexcept;
78 [[nodiscard]] Quaternion inverse() const;
79
81 //quaternion-rotation to a vector
83 //quaternion is unit-length
84 [[nodiscard]] Quaternion getConjugate() const;
85
88 Quaternion operator*(const Quaternion& rq) const noexcept;
89
91 Quaternion& operator*=(const Quaternion& rq) noexcept;
92
94 Quaternion operator/(const Quaternion& rq) const;
95 Quaternion& operator/=(const Quaternion& rq);
96
98 vec3<T> operator*(const vec3<T>& vec) const noexcept;
99
100 bool operator==(const Quaternion& rq) const;
101 bool operator!=(const Quaternion& rq) const;
102
103 Quaternion& operator+=(const Quaternion& rq);
104
105 Quaternion& operator-=(const Quaternion& rq);
106
107 Quaternion& operator*=(T scalar);
108
109 Quaternion& operator/=(T scalar);
110
111 Quaternion operator+(const Quaternion& rq) const;
112
113 Quaternion operator-(const Quaternion& rq) const;
114
115 Quaternion operator*(T scalar) const;
116
117 Quaternion operator/(T scalar) const;
118
119 void slerp(const Quaternion& q, F32 t) noexcept;
120
121 void slerp(const Quaternion& q0, const Quaternion& q1, F32 t) noexcept;
122
124 void fromAxisAngle(const vec3<T>& v, Angle::DEGREES<T> angle) noexcept;
125
126 void fromEuler(const vec3<Angle::DEGREES<T>>& v) noexcept;
127
129 void fromEuler(Angle::DEGREES<T> pitch, Angle::DEGREES<T> yaw, Angle::DEGREES<T> roll) noexcept;
130
131 void lookRotation(vec3<T> forward, vec3<T> up);
132
133 // a la Ogre3D
134 void fromMatrix(const mat3<T>& rotationMatrix) noexcept;
135
136 void fromMatrix(const mat4<T>& viewMatrix) noexcept;
137
139 void getMatrix(mat3<T>& outMatrix) const noexcept;
140
142 void getAxisAngle(vec3<T>& axis, Angle::DEGREES<T>& angle) const;
143
144 vec3<Angle::RADIANS<T>> getEuler() const noexcept;
145
146
148 void fromAxes(const vec3<T>* axis);
149 void fromAxes(const vec3<T>& xAxis, const vec3<T>& yAxis, const vec3<T>& zAxis);
150 void toAxes(vec3<T>* axis) const;
151 void toAxes(vec3<T>& xAxis, vec3<T>& yAxis, vec3<T>& zAxis) const;
152 [[nodiscard]] vec3<T> xAxis() const noexcept;
153 [[nodiscard]] vec3<T> yAxis() const noexcept;
154 [[nodiscard]] vec3<T> zAxis() const noexcept;
155
156 [[nodiscard]] T X() const noexcept;
157 [[nodiscard]] T Y() const noexcept;
158 [[nodiscard]] T Z() const noexcept;
159 [[nodiscard]] T W() const noexcept;
160
161 [[nodiscard]] vec3<T> XYZ() const noexcept;
162
163 template<typename U>
164 void X(U x) noexcept;
165 template<typename U>
166 void Y(U y) noexcept;
167 template<typename U>
168 void Z(U z) noexcept;
169 template<typename U>
170 void W(U w) noexcept;
171
172 void identity() noexcept;
173
174 [[nodiscard]] const vec4<T>& asVec4() const noexcept;
175
176 private:
178};
179
182template <typename T>
183Quaternion<T> RotationFromVToU(const vec3<T>& v, const vec3<T>& u, const vec3<T>& fallbackAxis = VECTOR3_ZERO) noexcept;
184
185template <typename T>
186Quaternion<T> Slerp(const Quaternion<T>& q0, const Quaternion<T>& q1, F32 t) noexcept;
187
188template <typename T>
189mat3<T> GetMatrix(const Quaternion<T>& q) noexcept;
190
191template <typename T>
192vec3<Angle::RADIANS<T>> GetEuler(const Quaternion<T>& q);
193
194template <typename T>
195vec3<T> operator*(vec3<T> const & v, Quaternion<T> const & q);
196
197template <typename T>
198vec3<T> Rotate(vec3<T> const & v, Quaternion<T> const & q) noexcept;
199
200template <typename T>
201vec3<T> DirectionFromAxis(const Quaternion<T>& q, const vec3<T>& AXIS) noexcept;
202
203template <typename T>
204vec3<T> DirectionFromEuler(vec3<Angle::DEGREES<T>> const & euler, const vec3<T>& FORWARD_DIRECTION);
205} // namespace Divide
206
207#endif //DVD_QUATERNION_H_
208
209#include "Quaternion.inl"
T magnituteSQ() const
Definition: Quaternion.inl:136
bool compare(const Quaternion &rq, Angle::DEGREES< T > tolerance=1e-3f) const
Definition: Quaternion.inl:141
void fromMatrix(const mat3< T > &rotationMatrix) noexcept
Definition: Quaternion.inl:402
T dot(const Quaternion &rq) const noexcept
Definition: Quaternion.inl:126
T X() const noexcept
Definition: Quaternion.inl:608
Quaternion getConjugate() const
We need to get the inverse of a quaternion to properly apply a.
Definition: Quaternion.inl:174
T Y() const noexcept
Definition: Quaternion.inl:613
vec3< T > xAxis() const noexcept
Definition: Quaternion.inl:548
void fromAxisAngle(const vec3< T > &v, Angle::DEGREES< T > angle) noexcept
Convert from Axis Angle.
Definition: Quaternion.inl:310
void set(const vec4< T > &values) noexcept
Definition: Quaternion.inl:149
void fromAxes(const vec3< T > *axis)
X/Y/Z Axis get/set a la Ogre: OgreQuaternion.cpp.
Definition: Quaternion.inl:511
void getAxisAngle(vec3< T > &axis, Angle::DEGREES< T > &angle) const
Convert to Axis/Angles.
Definition: Quaternion.inl:472
void getMatrix(mat3< T > &outMatrix) const noexcept
Convert to Matrix.
Definition: Quaternion.inl:442
Quaternion(__m128 reg) noexcept
Definition: Quaternion.h:54
void toAxes(vec3< T > *axis) const
Definition: Quaternion.inl:534
const vec4< T > & asVec4() const noexcept
Definition: Quaternion.inl:662
void lookRotation(vec3< T > forward, vec3< T > up)
Definition: Quaternion.inl:341
T Z() const noexcept
Definition: Quaternion.inl:618
void identity() noexcept
Definition: Quaternion.inl:657
void normalize() noexcept
normalizing a quaternion works similar to a vector. This method will not
Definition: Quaternion.inl:164
void slerp(const Quaternion &q, F32 t) noexcept
Definition: Quaternion.inl:281
vec3< T > XYZ() const noexcept
Definition: Quaternion.inl:628
vec4< T > _elements
Definition: Quaternion.h:177
vec3< T > yAxis() const noexcept
Definition: Quaternion.inl:568
vec3< T > zAxis() const noexcept
Definition: Quaternion.inl:588
Quaternion() noexcept
Definition: Quaternion.inl:72
Quaternion inverse() const
Definition: Quaternion.inl:169
vec3< Angle::RADIANS< T > > getEuler() const noexcept
Definition: Quaternion.inl:478
void fromEuler(const vec3< Angle::DEGREES< T > > &v) noexcept
Definition: Quaternion.inl:316
Quaternion & operator=(const Quaternion &q) noexcept
Definition: Quaternion.inl:120
T W() const noexcept
Definition: Quaternion.inl:623
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
Quaternion< T > Slerp(const Quaternion< T > &q0, const Quaternion< T > &q1, F32 t) noexcept
Definition: Quaternion.inl:712
Quaternion< T > RotationFromVToU(const vec3< T > &v, const vec3< T > &u, const vec3< T > &fallbackAxis=VECTOR3_ZERO) noexcept
get the shortest arc quaternion to rotate vector 'v' to the target vector 'u'(from Ogre3D!...
Definition: Quaternion.inl:669
vec3< Angle::RADIANS< T > > GetEuler(const Quaternion< T > &q)
Definition: Quaternion.inl:726
vec3< T > DirectionFromAxis(const Quaternion< T > &q, const vec3< T > &AXIS) noexcept
Definition: Quaternion.inl:743
vec3< T > DirectionFromEuler(vec3< Angle::DEGREES< T > > const &euler, const vec3< T > &FORWARD_DIRECTION)
Definition: Quaternion.inl:748
mat3< T > GetMatrix(const Quaternion< T > &q) noexcept
Definition: Quaternion.inl:719
static const vec3< F32 > VECTOR3_ZERO
Definition: MathVectors.h:1434
static const vec3< F32 > WORLD_Y_AXIS
Definition: MathVectors.h:1440
vec3< T > Rotate(vec3< T > const &v, Quaternion< T > const &q) noexcept
Definition: Quaternion.inl:736