1 #ifndef __DALI_QUATERNION_H__
2 #define __DALI_QUATERNION_H__
5 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
25 #include <dali/public-api/common/constants.h>
26 #include <dali/public-api/common/dali-common.h>
27 #include <dali/public-api/common/type-traits.h>
28 #include <dali/public-api/math/radian.h>
29 #include <dali/public-api/math/vector4.h>
34 * @addtogroup dali-core-math
38 // Forward declaration
42 * @brief The Quaternion class encapsulates the mathematics of the quaternion.
44 class DALI_IMPORT_API Quaternion
49 * @brief Default Constructor
54 * @brief Construct from a quaternion represented by floats.
56 * @param[in] cosThetaBy2
57 * @param[in] iBySineTheta
58 * @param[in] jBySineTheta
59 * @param[in] kBySineTheta
61 Quaternion( float cosThetaBy2, float iBySineTheta, float jBySineTheta, float kBySineTheta );
64 * @brief Construct from a quaternion represented by a vector.
66 * @param[in] vector - x,y,z fields represent i,j,k coefficients, w represents cos(theta/2)
68 explicit Quaternion( const Vector4& vector );
71 * @brief Constructor from an axis and angle.
73 * @param[in] angle - the angle around the axis
74 * @param[in] axis - the vector of the axis
76 Quaternion( Radian angle, const Vector3& axis );
79 * @brief Construct from Euler angles.
85 Quaternion( Radian pitch, Radian yaw, Radian roll );
88 * @brief Construct from a matrix.
92 explicit Quaternion(const Matrix& matrix);
95 * @brief Construct from 3 orthonormal axes.
97 * @param[in] xAxis The X axis
98 * @param[in] yAxis The Y axis
99 * @param[in] zAxis The Z axis
101 explicit Quaternion( const Vector3& xAxis, const Vector3& yAxis, const Vector3& zAxis );
104 * @brief Construct quaternion which describes minimum rotation to align v0 with v1
105 * @pre v0 and v1 should be normalized
107 * @param[in] v0 First normalized vector
108 * @param[in] v1 Second normalized vector
110 explicit Quaternion( const Vector3& v0, const Vector3& v1 );
113 * @brief Destructor, nonvirtual as this is not a base class.
120 static const Quaternion IDENTITY; ///< (0.0f,0.0f,0.0f,1.0f)
123 * @brief Helper to check if this is an identity quaternion
125 * @return true if this is identity quaternion
127 bool IsIdentity() const;
130 * @brief Convert the quaternion to an axis/angle pair.
133 * @param[out] angle in radians
134 * @return true if converted correctly
136 bool ToAxisAngle( Vector3& axis, Radian& angle ) const;
139 * @brief Return the quaternion as a vector.
141 * @return the vector representation of the quaternion
143 const Vector4& AsVector() const;
146 * @brief SetEuler sets the quaternion from the Euler angles applied in x, y, z order.
152 void SetEuler( Radian pitch, Radian yaw, Radian roll );
155 * @brief returns the Euler angles from a rotation Quaternion.
157 * @return a vector of Euler angles (x == pitch, y == yaw, z == roll)
159 Vector4 EulerAngles() const;
162 * @brief Addition operator.
164 * @param[in] other The quaternion to add
165 * @return A quaternion containing the result of the addition
167 const Quaternion operator+( const Quaternion& other ) const;
170 * @brief Subtraction operator.
172 * @param[in] other The quaternion to subtract
173 * @return A quaternion containing the result of the subtract
175 const Quaternion operator-( const Quaternion& other ) const;
178 * @brief Multiplication operator.
180 * @param[in] other The quaternion to multiply
181 * @return A quaternion containing the result
183 const Quaternion operator*( const Quaternion& other ) const;
186 * @brief Multiplication operator.
188 * @param[in] other The vector to multiply
189 * @return A vector containing the result of the multiplication
191 Vector3 operator*( const Vector3& other ) const;
194 * @brief Division operator.
196 * @param[in] other a quaternion to divide by
197 * @return A quaternion containing the result
199 const Quaternion operator/( const Quaternion& other ) const;
202 * @brief Scale operator.
204 * @param[in] scale A value to scale by
205 * @return A quaternion containing the result
207 const Quaternion operator*( float scale ) const;
210 * @brief Scale operator.
212 * @param[in] scale A value to scale by
213 * @return A quaternion containing the result
215 const Quaternion operator/( float scale ) const;
218 * @brief Unary Negation operator.
220 * @return A quaternion containing the negated result
222 Quaternion operator-() const;
225 * @brief Addition with Assignment operator.
227 * @param[in] other The quaternion to add
230 const Quaternion& operator+=( const Quaternion& other );
233 * @brief Subtraction with Assignment operator.
235 * @param[in] other The quaternion to subtract
238 const Quaternion& operator-=( const Quaternion& other );
241 * @brief Multiplication with Assignment operator.
243 * @param[in] other The quaternion to multiply
246 const Quaternion& operator*=( const Quaternion& other );
249 * @brief Scale with Assignment operator.
251 * @param[in] scale the value to scale by
254 const Quaternion& operator*=( float scale );
257 * @brief Scale with Assignment operator.
259 * @param[in] scale the value to scale by
262 const Quaternion& operator/=( float scale );
265 * @brief Equality operator.
267 * @param[in] rhs The quaterion to compare with.
268 * @return True if the quaternions are equal.
270 bool operator==( const Quaternion& rhs ) const;
273 * @brief Inequality operator.
275 * @param[in] rhs The quaterion to compare with.
276 * @return True if the quaternions are not equal.
278 bool operator!=( const Quaternion& rhs ) const;
281 * @brief Return the length of the quaternion.
283 * @return the length of the quaternion
285 float Length() const;
288 * @brief Return the squared length of the quaternion.
290 * @return the squared length of the quaternion
292 float LengthSquared() const;
295 * @brief Normalize this to unit length.
303 * @return a normalized version of this quaternion
305 Quaternion Normalized() const;
308 * @brief Conjugate this quaternion.
314 * @brief Invert this quaternion.
320 * @brief Performs the logarithm of a Quaternion = v*a where q = (cos(a),v*sin(a)).
322 * @return a quaternion representing the logarithm
324 Quaternion Log() const;
327 * @brief Performs an exponent e^Quaternion = Exp(v*a) = (cos(a),vsin(a)).
329 * @return a quaternion representing the exponent
331 Quaternion Exp() const;
334 * @brief Return the dot product of two quaternions.
336 * @param[in] q1 - the first quaternion
337 * @param[in] q2 - the second quaternion
338 * @return the dot product of the two quaternions
340 static float Dot( const Quaternion &q1, const Quaternion &q2 );
343 * @brief Linear Interpolation (using a straight line between the two quaternions).
345 * @param[in] q1 - the start quaternion
346 * @param[in] q2 - the end quaternion
347 * @param[in] t - a progress value between 0 and 1
348 * @return the interpolated quaternion
350 static Quaternion Lerp( const Quaternion &q1, const Quaternion &q2, float t );
353 * @brief Spherical Linear Interpolation (using the shortest arc of a great circle between
354 * the two quaternions).
356 * @param[in] q1 - the start quaternion
357 * @param[in] q2 - the end quaternion
358 * @param[in] progress - a progress value between 0 and 1
359 * @return the interpolated quaternion
361 static Quaternion Slerp( const Quaternion &q1, const Quaternion &q2, float progress );
364 * @brief This version of Slerp, used by Squad, does not check for theta > 90.
366 * @param[in] q1 - the start quaternion
367 * @param[in] q2 - the end quaternion
368 * @param[in] t - a progress value between 0 and 1
369 * @return the interpolated quaternion
371 static Quaternion SlerpNoInvert( const Quaternion &q1, const Quaternion &q2, float t );
374 * @brief Spherical Cubic Interpolation.
376 * @param[in] start - the start quaternion
377 * @param[in] end - the end quaternion
378 * @param[in] ctrl1 - the control quaternion for q1
379 * @param[in] ctrl2 - the control quaternion for q2
380 * @param[in] t - a progress value between 0 and 1
381 * @return the interpolated quaternion
383 static Quaternion Squad( const Quaternion& start, const Quaternion& end, const Quaternion& ctrl1, const Quaternion& ctrl2, float t );
386 * @brief Returns the shortest angle between two quaternions in Radians.
388 * @param[in] q1 - the first quaternion
389 * @param[in] q2 - the second quaternion
390 * @return the angle between the two quaternions.
392 static float AngleBetween( const Quaternion& q1, const Quaternion& q2 );
395 * @brief Rotate v by this Quaternion (Quaternion must be unit).
397 * @param[in] vector a vector to rotate
398 * @return the rotated vector
400 Vector4 Rotate( const Vector4& vector ) const;
403 * @brief Rotate v by this Quaternion (Quaternion must be unit).
405 * @param[in] vector a vector to rotate
406 * @return the rotated vector
408 Vector3 Rotate( const Vector3& vector ) const;
413 * @brief Set the quaternion from 3 orthonormal axes.
415 * @param[in] xAxis The X axis
416 * @param[in] yAxis The Y axis
417 * @param[in] zAxis The Z axis
419 DALI_INTERNAL void SetFromAxes( const Vector3& xAxis, const Vector3& yAxis, const Vector3& zAxis );
423 Vector4 mVector; ///< w component is s ( = cos(theta/2.0) )
427 * @brief Print a Quaternion.
429 * @param [in] o The output stream operator.
430 * @param [in] quaternion The quaternion to print.
431 * @return The output stream operator.
433 DALI_IMPORT_API std::ostream& operator<< (std::ostream& o, const Quaternion& quaternion);
435 // Allow Quaternion to be treated as a POD type
436 template <> struct TypeTraits< Quaternion > : public BasicTypes< Quaternion > { enum { IS_TRIVIAL_TYPE = true }; };
443 #endif // __DALI_QUATERNION_H__