1 /// @ref gtc_quaternion
2 /// @file glm/gtc/quaternion.hpp
3 ///
4 /// @see core (dependence)
5 /// @see gtc_half_float (dependence)
6 /// @see gtc_constants (dependence)
7 ///
8 /// @defgroup gtc_quaternion GLM_GTC_quaternion
9 /// @ingroup gtc
10 ///
11 /// @brief Defines a templated quaternion type and several quaternion operations.
12 ///
13 /// <glm/gtc/quaternion.hpp> need to be included to use these functionalities.
14 
15 #pragma once
16 
17 // Dependency:
18 #include "../mat3x3.hpp"
19 #include "../mat4x4.hpp"
20 #include "../vec3.hpp"
21 #include "../vec4.hpp"
22 #include "../gtc/constants.hpp"
23 
24 #if GLM_MESSAGES == GLM_MESSAGES_ENABLED && !defined(GLM_EXT_INCLUDED)
25 #	pragma message("GLM: GLM_GTC_quaternion extension included")
26 #endif
27 
28 namespace glm
29 {
30 	/// @addtogroup gtc_quaternion
31 	/// @{
32 
33 	template <typename T, precision P = defaultp>
34 	struct tquat
35 	{
36 		// -- Implementation detail --
37 
38 		typedef tquat<T, P> type;
39 		typedef T value_type;
40 
41 		// -- Data --
42 
43 #		if GLM_HAS_ALIGNED_TYPE
44 #			if GLM_COMPILER & GLM_COMPILER_GCC
45 #				pragma GCC diagnostic push
46 #				pragma GCC diagnostic ignored "-Wpedantic"
47 #			endif
48 #			if GLM_COMPILER & GLM_COMPILER_CLANG
49 #				pragma clang diagnostic push
50 #				pragma clang diagnostic ignored "-Wgnu-anonymous-struct"
51 #				pragma clang diagnostic ignored "-Wnested-anon-types"
52 #			endif
53 
54 			union
55 			{
56 				struct { T x, y, z, w;};
57 				typename detail::storage<T, sizeof(T) * 4, detail::is_aligned<P>::value>::type data;
58 			};
59 
60 #			if GLM_COMPILER & GLM_COMPILER_CLANG
61 #				pragma clang diagnostic pop
62 #			endif
63 #			if GLM_COMPILER & GLM_COMPILER_GCC
64 #				pragma GCC diagnostic pop
65 #			endif
66 #		else
67 			T x, y, z, w;
68 #		endif
69 
70 		// -- Component accesses --
71 
72 		typedef length_t length_type;
73 		/// Return the count of components of a quaternion
lengthglm::tquat74 		GLM_FUNC_DECL static length_type length(){return 4;}
75 
76 		GLM_FUNC_DECL T & operator[](length_type i);
77 		GLM_FUNC_DECL T const & operator[](length_type i) const;
78 
79 		// -- Implicit basic constructors --
80 
81 		GLM_FUNC_DECL GLM_CONSTEXPR tquat() GLM_DEFAULT_CTOR;
82 		GLM_FUNC_DECL GLM_CONSTEXPR tquat(tquat<T, P> const & q) GLM_DEFAULT;
83 		template <precision Q>
84 		GLM_FUNC_DECL GLM_CONSTEXPR tquat(tquat<T, Q> const & q);
85 
86 		// -- Explicit basic constructors --
87 
88 		GLM_FUNC_DECL GLM_CONSTEXPR_CTOR explicit tquat(ctor);
89 		GLM_FUNC_DECL GLM_CONSTEXPR tquat(T const & s, tvec3<T, P> const & v);
90 		GLM_FUNC_DECL GLM_CONSTEXPR tquat(T const & w, T const & x, T const & y, T const & z);
91 
92 		// -- Conversion constructors --
93 
94 		template <typename U, precision Q>
95 		GLM_FUNC_DECL GLM_CONSTEXPR GLM_EXPLICIT tquat(tquat<U, Q> const & q);
96 
97 		/// Explicit conversion operators
98 #		if GLM_HAS_EXPLICIT_CONVERSION_OPERATORS
99 			GLM_FUNC_DECL explicit operator tmat3x3<T, P>();
100 			GLM_FUNC_DECL explicit operator tmat4x4<T, P>();
101 #		endif
102 
103 		/// Create a quaternion from two normalized axis
104 		///
105 		/// @param u A first normalized axis
106 		/// @param v A second normalized axis
107 		/// @see gtc_quaternion
108 		/// @see http://lolengine.net/blog/2013/09/18/beautiful-maths-quaternion-from-vectors
109 		GLM_FUNC_DECL tquat(tvec3<T, P> const & u, tvec3<T, P> const & v);
110 
111 		/// Build a quaternion from euler angles (pitch, yaw, roll), in radians.
112 		GLM_FUNC_DECL GLM_EXPLICIT tquat(tvec3<T, P> const & eulerAngles);
113 		GLM_FUNC_DECL GLM_EXPLICIT tquat(tmat3x3<T, P> const & m);
114 		GLM_FUNC_DECL GLM_EXPLICIT tquat(tmat4x4<T, P> const & m);
115 
116 		// -- Unary arithmetic operators --
117 
118 		GLM_FUNC_DECL tquat<T, P> & operator=(tquat<T, P> const & m) GLM_DEFAULT;
119 
120 		template <typename U>
121 		GLM_FUNC_DECL tquat<T, P> & operator=(tquat<U, P> const & m);
122 		template <typename U>
123 		GLM_FUNC_DECL tquat<T, P> & operator+=(tquat<U, P> const & q);
124 		template <typename U>
125 		GLM_FUNC_DECL tquat<T, P> & operator-=(tquat<U, P> const & q);
126 		template <typename U>
127 		GLM_FUNC_DECL tquat<T, P> & operator*=(tquat<U, P> const & q);
128 		template <typename U>
129 		GLM_FUNC_DECL tquat<T, P> & operator*=(U s);
130 		template <typename U>
131 		GLM_FUNC_DECL tquat<T, P> & operator/=(U s);
132 	};
133 
134 	// -- Unary bit operators --
135 
136 	template <typename T, precision P>
137 	GLM_FUNC_DECL tquat<T, P> operator+(tquat<T, P> const & q);
138 
139 	template <typename T, precision P>
140 	GLM_FUNC_DECL tquat<T, P> operator-(tquat<T, P> const & q);
141 
142 	// -- Binary operators --
143 
144 	template <typename T, precision P>
145 	GLM_FUNC_DECL tquat<T, P> operator+(tquat<T, P> const & q, tquat<T, P> const & p);
146 
147 	template <typename T, precision P>
148 	GLM_FUNC_DECL tquat<T, P> operator*(tquat<T, P> const & q, tquat<T, P> const & p);
149 
150 	template <typename T, precision P>
151 	GLM_FUNC_DECL tvec3<T, P> operator*(tquat<T, P> const & q, tvec3<T, P> const & v);
152 
153 	template <typename T, precision P>
154 	GLM_FUNC_DECL tvec3<T, P> operator*(tvec3<T, P> const & v, tquat<T, P> const & q);
155 
156 	template <typename T, precision P>
157 	GLM_FUNC_DECL tvec4<T, P> operator*(tquat<T, P> const & q, tvec4<T, P> const & v);
158 
159 	template <typename T, precision P>
160 	GLM_FUNC_DECL tvec4<T, P> operator*(tvec4<T, P> const & v, tquat<T, P> const & q);
161 
162 	template <typename T, precision P>
163 	GLM_FUNC_DECL tquat<T, P> operator*(tquat<T, P> const & q, T const & s);
164 
165 	template <typename T, precision P>
166 	GLM_FUNC_DECL tquat<T, P> operator*(T const & s, tquat<T, P> const & q);
167 
168 	template <typename T, precision P>
169 	GLM_FUNC_DECL tquat<T, P> operator/(tquat<T, P> const & q, T const & s);
170 
171 	// -- Boolean operators --
172 
173 	template <typename T, precision P>
174 	GLM_FUNC_DECL bool operator==(tquat<T, P> const & q1, tquat<T, P> const & q2);
175 
176 	template <typename T, precision P>
177 	GLM_FUNC_DECL bool operator!=(tquat<T, P> const & q1, tquat<T, P> const & q2);
178 
179 	/// Returns the length of the quaternion.
180 	///
181 	/// @see gtc_quaternion
182 	template <typename T, precision P>
183 	GLM_FUNC_DECL T length(tquat<T, P> const & q);
184 
185 	/// Returns the normalized quaternion.
186 	///
187 	/// @see gtc_quaternion
188 	template <typename T, precision P>
189 	GLM_FUNC_DECL tquat<T, P> normalize(tquat<T, P> const & q);
190 
191 	/// Returns dot product of q1 and q2, i.e., q1[0] * q2[0] + q1[1] * q2[1] + ...
192 	///
193 	/// @see gtc_quaternion
194 	template <typename T, precision P, template <typename, precision> class quatType>
195 	GLM_FUNC_DECL T dot(quatType<T, P> const & x, quatType<T, P> const & y);
196 
197 	/// Spherical linear interpolation of two quaternions.
198 	/// The interpolation is oriented and the rotation is performed at constant speed.
199 	/// For short path spherical linear interpolation, use the slerp function.
200 	///
201 	/// @param x A quaternion
202 	/// @param y A quaternion
203 	/// @param a Interpolation factor. The interpolation is defined beyond the range [0, 1].
204 	/// @tparam T Value type used to build the quaternion. Supported: half, float or double.
205 	/// @see gtc_quaternion
206 	/// @see - slerp(tquat<T, P> const & x, tquat<T, P> const & y, T const & a)
207 	template <typename T, precision P>
208 	GLM_FUNC_DECL tquat<T, P> mix(tquat<T, P> const & x, tquat<T, P> const & y, T a);
209 
210 	/// Linear interpolation of two quaternions.
211 	/// The interpolation is oriented.
212 	///
213 	/// @param x A quaternion
214 	/// @param y A quaternion
215 	/// @param a Interpolation factor. The interpolation is defined in the range [0, 1].
216 	/// @tparam T Value type used to build the quaternion. Supported: half, float or double.
217 	/// @see gtc_quaternion
218 	template <typename T, precision P>
219 	GLM_FUNC_DECL tquat<T, P> lerp(tquat<T, P> const & x, tquat<T, P> const & y, T a);
220 
221 	/// Spherical linear interpolation of two quaternions.
222 	/// The interpolation always take the short path and the rotation is performed at constant speed.
223 	///
224 	/// @param x A quaternion
225 	/// @param y A quaternion
226 	/// @param a Interpolation factor. The interpolation is defined beyond the range [0, 1].
227 	/// @tparam T Value type used to build the quaternion. Supported: half, float or double.
228 	/// @see gtc_quaternion
229 	template <typename T, precision P>
230 	GLM_FUNC_DECL tquat<T, P> slerp(tquat<T, P> const & x, tquat<T, P> const & y, T a);
231 
232 	/// Returns the q conjugate.
233 	///
234 	/// @see gtc_quaternion
235 	template <typename T, precision P>
236 	GLM_FUNC_DECL tquat<T, P> conjugate(tquat<T, P> const & q);
237 
238 	/// Returns the q inverse.
239 	///
240 	/// @see gtc_quaternion
241 	template <typename T, precision P>
242 	GLM_FUNC_DECL tquat<T, P> inverse(tquat<T, P> const & q);
243 
244 	/// Rotates a quaternion from a vector of 3 components axis and an angle.
245 	///
246 	/// @param q Source orientation
247 	/// @param angle Angle expressed in radians.
248 	/// @param axis Axis of the rotation
249 	///
250 	/// @see gtc_quaternion
251 	template <typename T, precision P>
252 	GLM_FUNC_DECL tquat<T, P> rotate(tquat<T, P> const & q, T const & angle, tvec3<T, P> const & axis);
253 
254 	/// Returns euler angles, pitch as x, yaw as y, roll as z.
255 	/// The result is expressed in radians if GLM_FORCE_RADIANS is defined or degrees otherwise.
256 	///
257 	/// @see gtc_quaternion
258 	template <typename T, precision P>
259 	GLM_FUNC_DECL tvec3<T, P> eulerAngles(tquat<T, P> const & x);
260 
261 	/// Returns roll value of euler angles expressed in radians.
262 	///
263 	/// @see gtx_quaternion
264 	template <typename T, precision P>
265 	GLM_FUNC_DECL T roll(tquat<T, P> const & x);
266 
267 	/// Returns pitch value of euler angles expressed in radians.
268 	///
269 	/// @see gtx_quaternion
270 	template <typename T, precision P>
271 	GLM_FUNC_DECL T pitch(tquat<T, P> const & x);
272 
273 	/// Returns yaw value of euler angles expressed in radians.
274 	///
275 	/// @see gtx_quaternion
276 	template <typename T, precision P>
277 	GLM_FUNC_DECL T yaw(tquat<T, P> const & x);
278 
279 	/// Converts a quaternion to a 3 * 3 matrix.
280 	///
281 	/// @see gtc_quaternion
282 	template <typename T, precision P>
283 	GLM_FUNC_DECL tmat3x3<T, P> mat3_cast(tquat<T, P> const & x);
284 
285 	/// Converts a quaternion to a 4 * 4 matrix.
286 	///
287 	/// @see gtc_quaternion
288 	template <typename T, precision P>
289 	GLM_FUNC_DECL tmat4x4<T, P> mat4_cast(tquat<T, P> const & x);
290 
291 	/// Converts a 3 * 3 matrix to a quaternion.
292 	///
293 	/// @see gtc_quaternion
294 	template <typename T, precision P>
295 	GLM_FUNC_DECL tquat<T, P> quat_cast(tmat3x3<T, P> const & x);
296 
297 	/// Converts a 4 * 4 matrix to a quaternion.
298 	///
299 	/// @see gtc_quaternion
300 	template <typename T, precision P>
301 	GLM_FUNC_DECL tquat<T, P> quat_cast(tmat4x4<T, P> const & x);
302 
303 	/// Returns the quaternion rotation angle.
304 	///
305 	/// @see gtc_quaternion
306 	template <typename T, precision P>
307 	GLM_FUNC_DECL T angle(tquat<T, P> const & x);
308 
309 	/// Returns the q rotation axis.
310 	///
311 	/// @see gtc_quaternion
312 	template <typename T, precision P>
313 	GLM_FUNC_DECL tvec3<T, P> axis(tquat<T, P> const & x);
314 
315 	/// Build a quaternion from an angle and a normalized axis.
316 	///
317 	/// @param angle Angle expressed in radians.
318 	/// @param axis Axis of the quaternion, must be normalized.
319 	///
320 	/// @see gtc_quaternion
321 	template <typename T, precision P>
322 	GLM_FUNC_DECL tquat<T, P> angleAxis(T const & angle, tvec3<T, P> const & axis);
323 
324 	/// Returns the component-wise comparison result of x < y.
325 	///
326 	/// @tparam quatType Floating-point quaternion types.
327 	///
328 	/// @see gtc_quaternion
329 	template <typename T, precision P>
330 	GLM_FUNC_DECL tvec4<bool, P> lessThan(tquat<T, P> const & x, tquat<T, P> const & y);
331 
332 	/// Returns the component-wise comparison of result x <= y.
333 	///
334 	/// @tparam quatType Floating-point quaternion types.
335 	///
336 	/// @see gtc_quaternion
337 	template <typename T, precision P>
338 	GLM_FUNC_DECL tvec4<bool, P> lessThanEqual(tquat<T, P> const & x, tquat<T, P> const & y);
339 
340 	/// Returns the component-wise comparison of result x > y.
341 	///
342 	/// @tparam quatType Floating-point quaternion types.
343 	///
344 	/// @see gtc_quaternion
345 	template <typename T, precision P>
346 	GLM_FUNC_DECL tvec4<bool, P> greaterThan(tquat<T, P> const & x, tquat<T, P> const & y);
347 
348 	/// Returns the component-wise comparison of result x >= y.
349 	///
350 	/// @tparam quatType Floating-point quaternion types.
351 	///
352 	/// @see gtc_quaternion
353 	template <typename T, precision P>
354 	GLM_FUNC_DECL tvec4<bool, P> greaterThanEqual(tquat<T, P> const & x, tquat<T, P> const & y);
355 
356 	/// Returns the component-wise comparison of result x == y.
357 	///
358 	/// @tparam quatType Floating-point quaternion types.
359 	///
360 	/// @see gtc_quaternion
361 	template <typename T, precision P>
362 	GLM_FUNC_DECL tvec4<bool, P> equal(tquat<T, P> const & x, tquat<T, P> const & y);
363 
364 	/// Returns the component-wise comparison of result x != y.
365 	///
366 	/// @tparam quatType Floating-point quaternion types.
367 	///
368 	/// @see gtc_quaternion
369 	template <typename T, precision P>
370 	GLM_FUNC_DECL tvec4<bool, P> notEqual(tquat<T, P> const & x, tquat<T, P> const & y);
371 
372 	/// Returns true if x holds a NaN (not a number)
373 	/// representation in the underlying implementation's set of
374 	/// floating point representations. Returns false otherwise,
375 	/// including for implementations with no NaN
376 	/// representations.
377 	///
378 	/// /!\ When using compiler fast math, this function may fail.
379 	///
380 	/// @tparam genType Floating-point scalar or vector types.
381 	template <typename T, precision P>
382 	GLM_FUNC_DECL tvec4<bool, P> isnan(tquat<T, P> const & x);
383 
384 	/// Returns true if x holds a positive infinity or negative
385 	/// infinity representation in the underlying implementation's
386 	/// set of floating point representations. Returns false
387 	/// otherwise, including for implementations with no infinity
388 	/// representations.
389 	///
390 	/// @tparam genType Floating-point scalar or vector types.
391 	template <typename T, precision P>
392 	GLM_FUNC_DECL tvec4<bool, P> isinf(tquat<T, P> const & x);
393 
394 	/// @}
395 } //namespace glm
396 
397 #include "quaternion.inl"
398