1 ///////////////////////////////////////////////////////////////////////////////////
2 /// OpenGL Mathematics (glm.g-truc.net)
3 ///
4 /// Copyright (c) 2005 - 2014 G-Truc Creation (www.g-truc.net)
5 /// Permission is hereby granted, free of charge, to any person obtaining a copy
6 /// of this software and associated documentation files (the "Software"), to deal
7 /// in the Software without restriction, including without limitation the rights
8 /// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 /// copies of the Software, and to permit persons to whom the Software is
10 /// furnished to do so, subject to the following conditions:
11 ///
12 /// The above copyright notice and this permission notice shall be included in
13 /// all copies or substantial portions of the Software.
14 ///
15 /// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 /// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 /// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 /// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 /// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 /// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 /// THE SOFTWARE.
22 ///
23 /// @ref gtc_quaternion
24 /// @file glm/gtc/quaternion.hpp
25 /// @date 2009-05-21 / 2012-12-20
26 /// @author Christophe Riccio
27 ///
28 /// @see core (dependence)
29 /// @see gtc_half_float (dependence)
30 /// @see gtc_constants (dependence)
31 ///
32 /// @defgroup gtc_quaternion GLM_GTC_quaternion
33 /// @ingroup gtc
34 ///
35 /// @brief Defines a templated quaternion type and several quaternion operations.
36 ///
37 /// <glm/gtc/quaternion.hpp> need to be included to use these functionalities.
38 ///////////////////////////////////////////////////////////////////////////////////
39 
40 #ifndef GLM_GTC_quaternion
41 #define GLM_GTC_quaternion
42 
43 // Dependency:
44 #include "../mat3x3.hpp"
45 #include "../mat4x4.hpp"
46 #include "../vec3.hpp"
47 #include "../vec4.hpp"
48 #include "../gtc/constants.hpp"
49 
50 #if(defined(GLM_MESSAGES) && !defined(GLM_EXT_INCLUDED))
51 #	pragma message("GLM: GLM_GTC_quaternion extension included")
52 #endif
53 
54 namespace glm{
55 namespace detail
56 {
57 	template <typename T, precision P>
58 	struct tquat
59 	{
60 		enum ctor{null};
61 
62 		typedef tvec4<bool, P> bool_type;
63 
64 	public:
65 		T x, y, z, w;
66 
67 		GLM_FUNC_DECL GLM_CONSTEXPR length_t length() const;
68 
69 		// Constructors
70 		GLM_FUNC_DECL tquat();
71 		template <typename U, precision Q>
72 		GLM_FUNC_DECL explicit tquat(
73 			tquat<U, Q> const & q);
74 		GLM_FUNC_DECL tquat(
75 			T const & s,
76 			tvec3<T, P> const & v);
77 		GLM_FUNC_DECL tquat(
78 			T const & w,
79 			T const & x,
80 			T const & y,
81 			T const & z);
82 
83 		// Convertions
84 
85 		/// Create a quaternion from two normalized axis
86 		///
87 		/// @param u A first normalized axis
88 		/// @param v A second normalized axis
89 		/// @see gtc_quaternion
90 		/// @see http://lolengine.net/blog/2013/09/18/beautiful-maths-quaternion-from-vectors
91 		GLM_FUNC_DECL explicit tquat(
92 			detail::tvec3<T, P> const & u,
93 			detail::tvec3<T, P> const & v);
94 		/// Build a quaternion from euler angles (pitch, yaw, roll), in radians.
95 		GLM_FUNC_DECL explicit tquat(
96 			tvec3<T, P> const & eulerAngles);
97 		GLM_FUNC_DECL explicit tquat(
98 			tmat3x3<T, P> const & m);
99 		GLM_FUNC_DECL explicit tquat(
100 			tmat4x4<T, P> const & m);
101 
102 		// Accesses
103 		GLM_FUNC_DECL T & operator[](length_t i);
104 		GLM_FUNC_DECL T const & operator[](length_t i) const;
105 
106 		// Operators
107 		GLM_FUNC_DECL tquat<T, P> & operator+=(tquat<T, P> const & q);
108 		GLM_FUNC_DECL tquat<T, P> & operator*=(tquat<T, P> const & q);
109 		GLM_FUNC_DECL tquat<T, P> & operator*=(T const & s);
110 		GLM_FUNC_DECL tquat<T, P> & operator/=(T const & s);
111 	};
112 
113 	template <typename T, precision P>
114 	GLM_FUNC_DECL detail::tquat<T, P> operator- (
115 		detail::tquat<T, P> const & q);
116 
117 	template <typename T, precision P>
118 	GLM_FUNC_DECL detail::tquat<T, P> operator+ (
119 		detail::tquat<T, P> const & q,
120 		detail::tquat<T, P> const & p);
121 
122 	template <typename T, precision P>
123 	GLM_FUNC_DECL detail::tquat<T, P> operator* (
124 		detail::tquat<T, P> const & q,
125 		detail::tquat<T, P> const & p);
126 
127 	template <typename T, precision P>
128 	GLM_FUNC_DECL detail::tvec3<T, P> operator* (
129 		detail::tquat<T, P> const & q,
130 		detail::tvec3<T, P> const & v);
131 
132 	template <typename T, precision P>
133 	GLM_FUNC_DECL detail::tvec3<T, P> operator* (
134 		detail::tvec3<T, P> const & v,
135 		detail::tquat<T, P> const & q);
136 
137 	template <typename T, precision P>
138 	GLM_FUNC_DECL detail::tvec4<T, P> operator* (
139 		detail::tquat<T, P> const & q,
140 		detail::tvec4<T, P> const & v);
141 
142 	template <typename T, precision P>
143 	GLM_FUNC_DECL detail::tvec4<T, P> operator* (
144 		detail::tvec4<T, P> const & v,
145 		detail::tquat<T, P> const & q);
146 
147 	template <typename T, precision P>
148 	GLM_FUNC_DECL detail::tquat<T, P> operator* (
149 		detail::tquat<T, P> const & q,
150 		T const & s);
151 
152 	template <typename T, precision P>
153 	GLM_FUNC_DECL detail::tquat<T, P> operator* (
154 		T const & s,
155 		detail::tquat<T, P> const & q);
156 
157 	template <typename T, precision P>
158 	GLM_FUNC_DECL detail::tquat<T, P> operator/ (
159 		detail::tquat<T, P> const & q,
160 		T const & s);
161 
162 } //namespace detail
163 
164 	/// @addtogroup gtc_quaternion
165 	/// @{
166 
167 	/// Returns the length of the quaternion.
168 	///
169 	/// @see gtc_quaternion
170 	template <typename T, precision P>
171 	GLM_FUNC_DECL T length(
172 		detail::tquat<T, P> const & q);
173 
174 	/// Returns the normalized quaternion.
175 	///
176 	/// @see gtc_quaternion
177 	template <typename T, precision P>
178 	GLM_FUNC_DECL detail::tquat<T, P> normalize(
179 		detail::tquat<T, P> const & q);
180 
181 	/// Returns dot product of q1 and q2, i.e., q1[0] * q2[0] + q1[1] * q2[1] + ...
182 	///
183 	/// @see gtc_quaternion
184 	template <typename T, precision P, template <typename, precision> class quatType>
185 	GLM_FUNC_DECL T dot(
186 		quatType<T, P> const & x,
187 		quatType<T, P> const & y);
188 
189 	/// Spherical linear interpolation of two quaternions.
190 	/// The interpolation is oriented and the rotation is performed at constant speed.
191 	/// For short path spherical linear interpolation, use the slerp function.
192 	///
193 	/// @param x A quaternion
194 	/// @param y A quaternion
195 	/// @param a Interpolation factor. The interpolation is defined beyond the range [0, 1].
196 	/// @tparam T Value type used to build the quaternion. Supported: half, float or double.
197 	/// @see gtc_quaternion
198 	/// @see - slerp(detail::tquat<T, P> const & x, detail::tquat<T, P> const & y, T const & a)
199 	template <typename T, precision P>
200 	GLM_FUNC_DECL detail::tquat<T, P> mix(
201 		detail::tquat<T, P> const & x,
202 		detail::tquat<T, P> const & y,
203 		T const & a);
204 
205 	/// Linear interpolation of two quaternions.
206 	/// The interpolation is oriented.
207 	///
208 	/// @param x A quaternion
209 	/// @param y A quaternion
210 	/// @param a Interpolation factor. The interpolation is defined in the range [0, 1].
211 	/// @tparam T Value type used to build the quaternion. Supported: half, float or double.
212 	/// @see gtc_quaternion
213 	template <typename T, precision P>
214 	GLM_FUNC_DECL detail::tquat<T, P> lerp(
215 		detail::tquat<T, P> const & x,
216 		detail::tquat<T, P> const & y,
217 		T const & a);
218 
219 	/// Spherical linear interpolation of two quaternions.
220 	/// The interpolation always take the short path and the rotation is performed at constant speed.
221 	///
222 	/// @param x A quaternion
223 	/// @param y A quaternion
224 	/// @param a Interpolation factor. The interpolation is defined beyond the range [0, 1].
225 	/// @tparam T Value type used to build the quaternion. Supported: half, float or double.
226 	/// @see gtc_quaternion
227 	template <typename T, precision P>
228 	GLM_FUNC_DECL detail::tquat<T, P> slerp(
229 		detail::tquat<T, P> const & x,
230 		detail::tquat<T, P> const & y,
231 		T const & a);
232 
233 	/// Returns the q conjugate.
234 	///
235 	/// @see gtc_quaternion
236 	template <typename T, precision P>
237 	GLM_FUNC_DECL detail::tquat<T, P> conjugate(
238 		detail::tquat<T, P> const & q);
239 
240 	/// Returns the q inverse.
241 	///
242 	/// @see gtc_quaternion
243 	template <typename T, precision P>
244 	GLM_FUNC_DECL detail::tquat<T, P> inverse(
245 		detail::tquat<T, P> const & q);
246 
247 	/// Rotates a quaternion from a vector of 3 components axis and an angle.
248 	///
249 	/// @param q Source orientation
250 	/// @param angle Angle expressed in radians if GLM_FORCE_RADIANS is define or degrees otherwise.
251 	/// @param axis Axis of the rotation
252 	///
253 	/// @see gtc_quaternion
254 	template <typename T, precision P>
255 	GLM_FUNC_DECL detail::tquat<T, P> rotate(
256 		detail::tquat<T, P> const & q,
257 		T const & angle,
258 		detail::tvec3<T, P> const & axis);
259 
260 	/// Returns euler angles, yitch as x, yaw as y, roll as z.
261 	/// The result is expressed in radians if GLM_FORCE_RADIANS is defined or degrees otherwise.
262 	///
263 	/// @see gtc_quaternion
264 	template <typename T, precision P>
265 	GLM_FUNC_DECL detail::tvec3<T, P> eulerAngles(
266 		detail::tquat<T, P> const & x);
267 
268 	/// Returns roll value of euler angles expressed in radians if GLM_FORCE_RADIANS is defined or degrees otherwise.
269 	///
270 	/// @see gtx_quaternion
271 	template <typename T, precision P>
272 	GLM_FUNC_DECL T roll(detail::tquat<T, P> const & x);
273 
274 	/// Returns pitch value of euler angles expressed in radians if GLM_FORCE_RADIANS is defined or degrees otherwise.
275 	///
276 	/// @see gtx_quaternion
277 	template <typename T, precision P>
278 	GLM_FUNC_DECL T pitch(detail::tquat<T, P> const & x);
279 
280 	/// Returns yaw value of euler angles expressed in radians if GLM_FORCE_RADIANS is defined or degrees otherwise.
281 	///
282 	/// @see gtx_quaternion
283 	template <typename T, precision P>
284 	GLM_FUNC_DECL T yaw(detail::tquat<T, P> const & x);
285 
286 	/// Converts a quaternion to a 3 * 3 matrix.
287 	///
288 	/// @see gtc_quaternion
289 	template <typename T, precision P>
290 	GLM_FUNC_DECL detail::tmat3x3<T, P> mat3_cast(
291 		detail::tquat<T, P> const & x);
292 
293 	/// Converts a quaternion to a 4 * 4 matrix.
294 	///
295 	/// @see gtc_quaternion
296 	template <typename T, precision P>
297 	GLM_FUNC_DECL detail::tmat4x4<T, P> mat4_cast(
298 		detail::tquat<T, P> const & x);
299 
300 	/// Converts a 3 * 3 matrix to a quaternion.
301 	///
302 	/// @see gtc_quaternion
303 	template <typename T, precision P>
304 	GLM_FUNC_DECL detail::tquat<T, P> quat_cast(
305 		detail::tmat3x3<T, P> const & x);
306 
307 	/// Converts a 4 * 4 matrix to a quaternion.
308 	///
309 	/// @see gtc_quaternion
310 	template <typename T, precision P>
311 	GLM_FUNC_DECL detail::tquat<T, P> quat_cast(
312 		detail::tmat4x4<T, P> const & x);
313 
314 	/// Returns the quaternion rotation angle.
315 	///
316 	/// @see gtc_quaternion
317 	template <typename T, precision P>
318 	GLM_FUNC_DECL T angle(detail::tquat<T, P> const & x);
319 
320 	/// Returns the q rotation axis.
321 	///
322 	/// @see gtc_quaternion
323 	template <typename T, precision P>
324 	GLM_FUNC_DECL detail::tvec3<T, P> axis(
325 		detail::tquat<T, P> const & x);
326 
327 	/// Build a quaternion from an angle and a normalized axis.
328 	///
329 	/// @param angle Angle expressed in radians if GLM_FORCE_RADIANS is define or degrees otherwise.
330 	/// @param axis Axis of the quaternion, must be normalized.
331 	///
332 	/// @see gtc_quaternion
333 	template <typename T, precision P>
334 	GLM_FUNC_DECL detail::tquat<T, P> angleAxis(
335 		T const & angle,
336 		detail::tvec3<T, P> const & axis);
337 
338 	/// Returns the component-wise comparison result of x < y.
339 	///
340 	/// @tparam quatType Floating-point quaternion types.
341 	///
342 	/// @see gtc_quaternion
343 	template <typename T, precision P>
344 	GLM_FUNC_DECL detail::tvec4<bool, P> lessThan(
345 		detail::tquat<T, P> const & x,
346 		detail::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 detail::tvec4<bool, P> lessThanEqual(
355 		detail::tquat<T, P> const & x,
356 		detail::tquat<T, P> const & y);
357 
358 	/// Returns the component-wise comparison of result x > y.
359 	///
360 	/// @tparam quatType Floating-point quaternion types.
361 	///
362 	/// @see gtc_quaternion
363 	template <typename T, precision P>
364 	GLM_FUNC_DECL detail::tvec4<bool, P> greaterThan(
365 		detail::tquat<T, P> const & x,
366 		detail::tquat<T, P> const & y);
367 
368 	/// Returns the component-wise comparison of result x >= y.
369 	///
370 	/// @tparam quatType Floating-point quaternion types.
371 	///
372 	/// @see gtc_quaternion
373 	template <typename T, precision P>
374 	GLM_FUNC_DECL detail::tvec4<bool, P> greaterThanEqual(
375 		detail::tquat<T, P> const & x,
376 		detail::tquat<T, P> const & y);
377 
378 	/// Returns the component-wise comparison of result x == y.
379 	///
380 	/// @tparam quatType Floating-point quaternion types.
381 	///
382 	/// @see gtc_quaternion
383 	template <typename T, precision P>
384 	GLM_FUNC_DECL detail::tvec4<bool, P> equal(
385 		detail::tquat<T, P> const & x,
386 		detail::tquat<T, P> const & y);
387 
388 	/// Returns the component-wise comparison of result x != y.
389 	///
390 	/// @tparam quatType Floating-point quaternion types.
391 	///
392 	/// @see gtc_quaternion
393 	template <typename T, precision P>
394 	GLM_FUNC_DECL detail::tvec4<bool, P> notEqual(
395 		detail::tquat<T, P> const & x,
396 		detail::tquat<T, P> const & y);
397 
398 	/// @}
399 } //namespace glm
400 
401 #include "quaternion.inl"
402 
403 #endif//GLM_GTC_quaternion
404