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/dali-common.h>
26 #include <dali/public-api/common/constants.h>
27 #include <dali/public-api/math/radian.h>
28 #include <dali/public-api/math/vector4.h>
33 // Forward declaration
37 * @brief The Quaternion class encapsulates the mathematics of the quaternion.
39 class DALI_IMPORT_API Quaternion
44 * @brief Default Constructor
49 * @brief Construct from a quaternion represented by floats.
51 * @param[in] cosThetaBy2
52 * @param[in] iBySineTheta
53 * @param[in] jBySineTheta
54 * @param[in] kBySineTheta
56 Quaternion( float cosThetaBy2, float iBySineTheta, float jBySineTheta, float kBySineTheta );
59 * @brief Construct from a quaternion represented by a vector.
61 * @param[in] vector - x,y,z fields represent i,j,k coefficients, w represents cos(theta/2)
63 explicit Quaternion( const Vector4& vector );
66 * @brief Constructor from an axis and angle.
68 * @param[in] angle - the angle around the axis
69 * @param[in] axis - the vector of the axis
71 Quaternion( Radian angle, const Vector3& axis );
74 * @brief Construct from Euler angles.
80 Quaternion( Radian pitch, Radian yaw, Radian roll );
83 * @brief Construct from a matrix.
87 explicit Quaternion(const Matrix& matrix);
90 * @brief Construct from 3 orthonormal axes.
92 * @param[in] xAxis The X axis
93 * @param[in] yAxis The Y axis
94 * @param[in] zAxis The Z axis
96 explicit Quaternion( const Vector3& xAxis, const Vector3& yAxis, const Vector3& zAxis );
99 * @brief Construct quaternion which describes minimum rotation to align v0 with v1
100 * @pre v0 and v1 should be normalized
102 * @param[in] v0 First normalized vector
103 * @param[in] v1 Second normalized vector
105 explicit Quaternion( const Vector3& v0, const Vector3& v1 );
108 * @brief Destructor, nonvirtual as this is not a base class.
115 static const Quaternion IDENTITY; ///< (0.0f,0.0f,0.0f,1.0f)
118 * @brief Helper to check if this is an identity quaternion
120 * @return true if this is identity quaternion
122 bool IsIdentity() const;
125 * @brief Convert the quaternion to an axis/angle pair.
128 * @param[out] angle in radians
129 * @return true if converted correctly
131 bool ToAxisAngle( Vector3& axis, Radian& angle ) const;
134 * @brief Return the quaternion as a vector.
136 * @return the vector representation of the quaternion
138 const Vector4& AsVector() const;
141 * @brief SetEuler sets the quaternion from the Euler angles applied in x, y, z order.
147 void SetEuler( Radian pitch, Radian yaw, Radian roll );
150 * @brief returns the Euler angles from a rotation Quaternion.
152 * @return a vector of Euler angles (x == pitch, y == yaw, z == roll)
154 Vector4 EulerAngles() const;
157 * @brief Addition operator.
159 * @param[in] other The quaternion to add
160 * @return A quaternion containing the result of the addition
162 const Quaternion operator+( const Quaternion& other ) const;
165 * @brief Subtraction operator.
167 * @param[in] other The quaternion to subtract
168 * @return A quaternion containing the result of the subtract
170 const Quaternion operator-( const Quaternion& other ) const;
173 * @brief Multiplication operator.
175 * @param[in] other The quaternion to multiply
176 * @return A quaternion containing the result
178 const Quaternion operator*( const Quaternion& other ) const;
181 * @brief Multiplication operator.
183 * @param[in] other The vector to multiply
184 * @return A vector containing the result of the multiplication
186 Vector3 operator*( const Vector3& other ) const;
189 * @brief Division operator.
191 * @param[in] other a quaternion to divide by
192 * @return A quaternion containing the result
194 const Quaternion operator/( const Quaternion& other ) const;
197 * @brief Scale operator.
199 * @param[in] scale A value to scale by
200 * @return A quaternion containing the result
202 const Quaternion operator*( float scale ) const;
205 * @brief Scale operator.
207 * @param[in] scale A value to scale by
208 * @return A quaternion containing the result
210 const Quaternion operator/( float scale ) const;
213 * @brief Unary Negation operator.
215 * @return A quaternion containing the negated result
217 Quaternion operator-() const;
220 * @brief Addition with Assignment operator.
222 * @param[in] other The quaternion to add
225 const Quaternion& operator+=( const Quaternion& other );
228 * @brief Subtraction with Assignment operator.
230 * @param[in] other The quaternion to subtract
233 const Quaternion& operator-=( const Quaternion& other );
236 * @brief Multiplication with Assignment operator.
238 * @param[in] other The quaternion to multiply
241 const Quaternion& operator*=( const Quaternion& other );
244 * @brief Scale with Assignment operator.
246 * @param[in] scale the value to scale by
249 const Quaternion& operator*=( float scale );
252 * @brief Scale with Assignment operator.
254 * @param[in] scale the value to scale by
257 const Quaternion& operator/=( float scale );
260 * @brief Equality operator.
262 * @param[in] rhs The quaterion to compare with.
263 * @return True if the quaternions are equal.
265 bool operator==( const Quaternion& rhs ) const;
268 * @brief Inequality operator.
270 * @param[in] rhs The quaterion to compare with.
271 * @return True if the quaternions are not equal.
273 bool operator!=( const Quaternion& rhs ) const;
276 * @brief Return the length of the quaternion.
278 * @return the length of the quaternion
280 float Length() const;
283 * @brief Return the squared length of the quaternion.
285 * @return the squared length of the quaternion
287 float LengthSquared() const;
290 * @brief Normalize this to unit length.
298 * @return a normalized version of this quaternion
300 Quaternion Normalized() const;
303 * @brief Conjugate this quaternion.
309 * @brief Invert this quaternion.
315 * @brief Performs the logarithm of a Quaternion = v*a where q = (cos(a),v*sin(a)).
317 * @return a quaternion representing the logarithm
319 Quaternion Log() const;
322 * @brief Performs an exponent e^Quaternion = Exp(v*a) = (cos(a),vsin(a)).
324 * @return a quaternion representing the exponent
326 Quaternion Exp() const;
329 * @brief Return the dot product of two quaternions.
331 * @param[in] q1 - the first quaternion
332 * @param[in] q2 - the second quaternion
333 * @return the dot product of the two quaternions
335 static float Dot( const Quaternion &q1, const Quaternion &q2 );
338 * @brief Linear Interpolation (using a straight line between the two quaternions).
340 * @param[in] q1 - the start quaternion
341 * @param[in] q2 - the end quaternion
342 * @param[in] t - a progress value between 0 and 1
343 * @return the interpolated quaternion
345 static Quaternion Lerp( const Quaternion &q1, const Quaternion &q2, float t );
348 * @brief Spherical Linear Interpolation (using the shortest arc of a great circle between
349 * the two quaternions).
351 * @param[in] q1 - the start quaternion
352 * @param[in] q2 - the end quaternion
353 * @param[in] progress - a progress value between 0 and 1
354 * @return the interpolated quaternion
356 static Quaternion Slerp( const Quaternion &q1, const Quaternion &q2, float progress );
359 * @brief This version of Slerp, used by Squad, does not check for theta > 90.
361 * @param[in] q1 - the start quaternion
362 * @param[in] q2 - the end quaternion
363 * @param[in] t - a progress value between 0 and 1
364 * @return the interpolated quaternion
366 static Quaternion SlerpNoInvert( const Quaternion &q1, const Quaternion &q2, float t );
369 * @brief Spherical Cubic Interpolation.
371 * @param[in] start - the start quaternion
372 * @param[in] end - the end quaternion
373 * @param[in] ctrl1 - the control quaternion for q1
374 * @param[in] ctrl2 - the control quaternion for q2
375 * @param[in] t - a progress value between 0 and 1
376 * @return the interpolated quaternion
378 static Quaternion Squad( const Quaternion& start, const Quaternion& end, const Quaternion& ctrl1, const Quaternion& ctrl2, float t );
381 * @brief Returns the shortest angle between two quaternions in Radians.
383 * @param[in] q1 - the first quaternion
384 * @param[in] q2 - the second quaternion
385 * @return the angle between the two quaternions.
387 static float AngleBetween( const Quaternion& q1, const Quaternion& q2 );
390 * @brief Rotate v by this Quaternion (Quaternion must be unit).
392 * @param[in] vector a vector to rotate
393 * @return the rotated vector
395 Vector4 Rotate( const Vector4& vector ) const;
398 * @brief Rotate v by this Quaternion (Quaternion must be unit).
400 * @param[in] vector a vector to rotate
401 * @return the rotated vector
403 Vector3 Rotate( const Vector3& vector ) const;
408 * @brief Set the quaternion from 3 orthonormal axes.
410 * @param[in] xAxis The X axis
411 * @param[in] yAxis The Y axis
412 * @param[in] zAxis The Z axis
414 DALI_INTERNAL void SetFromAxes( const Vector3& xAxis, const Vector3& yAxis, const Vector3& zAxis );
418 Vector4 mVector; ///< w component is s ( = cos(theta/2.0) )
422 * @brief Print a Quaternion.
424 * @param [in] o The output stream operator.
425 * @param [in] quaternion The quaternion to print.
426 * @return The output stream operator.
428 DALI_IMPORT_API std::ostream& operator<< (std::ostream& o, const Quaternion& quaternion);
432 #endif // __DALI_QUATERNION_H__