-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquaternion.h
220 lines (200 loc) · 7.22 KB
/
quaternion.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
The MIT License (MIT)
Copyright (c) 2013, 2014 Jacob McGladdery
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* @file quaternion.h
* @brief Quaternion mathematics structure.
*/
#ifndef QUATERNION_H
#define QUATERNION_H
/**
* @brief Quaternion.
* @details A quaternion is a four-dimensional vector space over the real
* numbers. They are a number system which extends the complex
* numbers.
*/
struct Quaternion
{
Quaternion();
Quaternion(const Quaternion &q);
Quaternion(const float w, const float x, const float y, const float z);
Quaternion conjugate() const;
void convertToAxisAngle(float &wx, float &wy, float &wz, float &angle) const;
void convertToEulerAngles(float &roll, float &pitch, float &yaw) const;
float dot(const Quaternion &q) const;
Quaternion inverse() const;
float norm() const;
void normalize();
Quaternion normalized() const;
Quaternion &operator=(const Quaternion &q);
Quaternion &operator+=(const Quaternion &q);
Quaternion &operator-=(const Quaternion &q);
Quaternion &operator*=(const float factor);
Quaternion &operator*=(const Quaternion &q);
Quaternion &operator/=(const float divisor);
friend inline bool operator==(const Quaternion &q1, const Quaternion &q2);
friend inline bool operator!=(const Quaternion &q1, const Quaternion &q2);
friend inline const Quaternion operator+(Quaternion q1, const Quaternion &q2);
friend inline const Quaternion operator-(const Quaternion &q);
friend inline const Quaternion operator-(Quaternion q1, const Quaternion &q2);
friend inline const Quaternion operator*(float factor, Quaternion q);
friend inline const Quaternion operator*(Quaternion q, float factor);
friend inline const Quaternion operator*(Quaternion q1, const Quaternion &q2);
friend inline const Quaternion operator/(Quaternion q, float divisor);
float w; /**< The real scalar component */
float x; /**< The imaginary vector X axis component */
float y; /**< The imaginary vector Y axis component */
float z; /**< The imaginary vector Z axis component */
};
/**
* @brief Equality operator.
* @details Tests for equality between two quaternions by comparing each of
* their corresponding components.
* @note A quaternion will represent the same rotation after it has been
* negated. However, the way the equality operator is implemented
* will cause a comparison between a quaternion and its negated self
* to always return false. This is because this function only tests
* if each component is exactly equal.
*
* @param[in] q1 The left side quaternion operand.
* @param[in] q2 The right side quaternion operand.
* @retval true If the two quaternions are identical.
* @retval false Otherwise.
* @return The result of the equality test.
*/
inline bool operator==(const Quaternion &q1, const Quaternion &q2)
{
return (q1.w == q2.w) && (q1.x == q2.x) && (q1.y == q2.y) && (q1.z == q2.z);
}
/**
* @brief Inequality operator.
* @details Tests for inequality between two quaternions by comparing each of
* their corresponding components.
* @see operator==()
*
* @param[in] q1 The left side quaternion operand.
* @param[in] q2 The right side quaternion operand.
* @retval true If the two quaternions are not identical.
* @retval false Otherwise.
* @return The result of the inequality test.
*/
inline bool operator!=(const Quaternion &q1, const Quaternion &q2)
{
return !operator==(q1, q2);
}
/**
* @brief Addition operator.
* @details Performs addition between two quaternions.
* @see Quaternion::operator+=()
*
* @param[in] q1 The left side quaternion operand.
* @param[in] q2 The right side quaternion operand.
* @return The result of addition.
*/
inline const Quaternion operator+(Quaternion q1, const Quaternion &q2)
{
q1 += q2;
return q1;
}
/**
* @brief Negation operator.
* @details Negates the quaternion by negating each of its components.
* @f[
* -q = \begin{bmatrix}
* -q_0 & -q_1 & -q_2 & -q_3
* \end{bmatrix}
* @f]
*
* @param[in] q The quaternion to negate.
* @return The negated quaternion.
*/
inline const Quaternion operator-(const Quaternion &q)
{
return Quaternion(-q.w, -q.x, -q.y, -q.z);
}
/**
* @brief Subtraction operator.
* @details Performs subtraction between two quaternions.
* @see Quaternion::operator-=()
*
* @param[in] q1 The left side quaternion.
* @param[in] q2 The right side quaternion.
* @return The result of subtraction.
*/
inline const Quaternion operator-(Quaternion q1, const Quaternion &q2)
{
q1 -= q2;
return q1;
}
/**
* @brief Scalar multiplication operator.
* @details Performs scalar multiplication between a quaternion and a scalar.
* @see Quaternion::operator*=(float)
*
* @param[in] factor The left side scalar.
* @param[in] q The right side quaternion.
* @return The result of multiplication.
*/
inline const Quaternion operator*(float factor, Quaternion q)
{
q *= factor;
return q;
}
/**
* @brief Scalar multiplication operator.
* @details Performs scalar multiplication between a quaternion and a scalar.
* @see Quaternion::operator*=(float)
*
* @param[in] q The left side quaternion.
* @param[in] factor The right side scalar.
* @return The result of multiplication.
*/
inline const Quaternion operator*(Quaternion q, float factor)
{
q *= factor;
return q;
}
/**
* @brief Cross product multiplication operator.
* @details Performs cross product multiplication between two quaternions.
* @see Quaternion::operator*=()
*
* @param[in] q1 The left side quaternion.
* @param[in] q2 The right side quaternion.
* @return The result of multiplication.
*/
inline const Quaternion operator*(Quaternion q1, const Quaternion &q2)
{
q1 *= q2;
return q1;
}
/**
* @brief Scalar division operator.
* @details Performs scalar division between a quaternion and a scalar.
* @see Quaternion::operator/=(float)
*
* @param[in] q The left side quaternion.
* @param[in] divisor The right side divisor.
* @return The result of division.
*/
inline const Quaternion operator/(Quaternion q, float divisor)
{
q /= divisor;
return q;
}
#endif // QUATERNION_H