[dali_1.0.15] Merge branch 'tizen'
[platform/core/uifw/dali-core.git] / dali / public-api / math / quaternion.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/public-api/math/quaternion.h>
20
21 // INTERNAL INCLUDES
22 #include <dali/public-api/common/constants.h>
23 #include <dali/public-api/math/degree.h>
24 #include <dali/public-api/math/matrix.h>
25 #include <dali/public-api/math/radian.h>
26 #include <dali/public-api/math/math-utils.h>
27 #include <dali/internal/render/common/performance-monitor.h>
28
29 namespace Dali
30 {
31 using Internal::PerformanceMonitor;
32
33 const Quaternion Quaternion::IDENTITY;
34
35
36 /**
37  * Default Constructor
38  */
39 Quaternion::Quaternion()
40   : mVector(0.0f, 0.0f, 0.0f, 1.0f)
41 {
42 }
43
44 Quaternion::Quaternion(float cosThetaBy2, float iBySineTheta, float jBySineTheta, float kBySineTheta) :
45   mVector(iBySineTheta, jBySineTheta, kBySineTheta, cosThetaBy2)
46 {
47 }
48
49 Quaternion::Quaternion(const Vector4& vector)
50 {
51   mVector = vector;
52 }
53
54 Quaternion::Quaternion(float angle, const Vector3 &axis)
55 {
56   MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY,4);
57
58   Vector3 tmpAxis = axis;
59   tmpAxis.Normalize();
60   const float halfAngle = angle * 0.5f;
61   const float sinThetaByTwo = sinf(halfAngle);
62   const float cosThetaByTwo = cosf(halfAngle);
63   mVector.x = tmpAxis.x * sinThetaByTwo;
64   mVector.y = tmpAxis.y * sinThetaByTwo;
65   mVector.z = tmpAxis.z * sinThetaByTwo;
66   mVector.w = cosThetaByTwo;
67 }
68
69 Quaternion::Quaternion(float theta, const Vector4 &axis)
70 {
71   MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY,4);
72
73   Vector4 tmpAxis = axis;
74   tmpAxis.Normalize();
75   const float halfTheta = theta * 0.5f;
76   const float sinThetaByTwo = sinf(halfTheta);
77   const float cosThetaByTwo = cosf(halfTheta);
78   mVector.x = tmpAxis.x * sinThetaByTwo;
79   mVector.y = tmpAxis.y * sinThetaByTwo;
80   mVector.z = tmpAxis.z * sinThetaByTwo;
81   mVector.w = cosThetaByTwo;
82 }
83
84 Quaternion::Quaternion(float x, float y, float z)
85 {
86   SetEuler(x,y,z);
87 }
88
89 Quaternion::Quaternion(const Matrix& matrix)
90 {
91   Vector3 xAxis( matrix.GetXAxis() );
92   Vector3 yAxis( matrix.GetYAxis() );
93   Vector3 zAxis( matrix.GetZAxis() );
94
95   SetFromAxes( xAxis, yAxis, zAxis );
96 }
97
98 Quaternion::Quaternion( const Vector3& xAxis, const Vector3& yAxis, const Vector3& zAxis )
99 {
100   SetFromAxes( xAxis, yAxis, zAxis );
101 }
102
103
104 Quaternion Quaternion::FromAxisAngle(const Vector4 &axis, float angle)
105 {
106   return Quaternion(angle, axis);
107 }
108
109 Quaternion::~Quaternion()
110 {
111 }
112
113 bool Quaternion::ToAxisAngle(Vector3 &axis, float &angle) const
114 {
115   angle = acosf(mVector.w);
116   bool converted = false;
117   // pre-compute to save time
118   const float sine = sinf( angle );
119
120   // If sine(angle) is zero, conversion is not possible
121
122   if ( ! EqualsZero( sine ) )
123   {
124     MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY,3);
125
126     float sinf_theta_inv = 1.0f / sine;
127
128     axis.x = mVector.x*sinf_theta_inv;
129     axis.y = mVector.y*sinf_theta_inv;
130     axis.z = mVector.z*sinf_theta_inv;
131     angle*=2.0f;
132     converted = true;
133   }
134   return converted;
135 }
136
137 bool Quaternion::ToAxisAngle(Vector4 &axis, float &angle) const
138 {
139   Vector3 axis3;
140   bool converted = ToAxisAngle(axis3, angle);
141   if(converted)
142   {
143     axis.x = axis3.x;
144     axis.y = axis3.y;
145     axis.z = axis3.z;
146     axis.w = 0;
147   }
148   return converted;
149 }
150
151 const Vector4& Quaternion::AsVector() const
152 {
153   return mVector;
154 }
155
156 void Quaternion::SetEuler(float x, float y, float z)
157 {
158   MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY,19);
159
160   const float halfX = 0.5f * x;
161   const float halfY = 0.5f * y;
162   const float halfZ = 0.5f * z;
163
164   float cosX2 = cosf(halfX);
165   float cosY2 = cosf(halfY);
166   float cosZ2 = cosf(halfZ);
167
168   float sinX2 = sinf(halfX);
169   float sinY2 = sinf(halfY);
170   float sinZ2 = sinf(halfZ);
171
172   mVector.w = cosZ2 * cosY2 * cosX2 + sinZ2 * sinY2 * sinX2;
173   mVector.x = cosZ2 * cosY2 * sinX2 - sinZ2 * sinY2 * cosX2;
174   mVector.y = cosZ2 * sinY2 * cosX2 + sinZ2 * cosY2 * sinX2;
175   mVector.z = sinZ2 * cosY2 * cosX2 - cosZ2 * sinY2 * sinX2;
176 }
177
178 Vector4 Quaternion::EulerAngles() const
179 {
180   MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY,13);
181
182   float sqw = mVector.w*mVector.w;
183   float sqx = mVector.x*mVector.x;
184   float sqy = mVector.y*mVector.y;
185   float sqz = mVector.z*mVector.z;
186
187   Vector4 euler;
188   euler.x = atan2f(2.0f * (mVector.y*mVector.z + mVector.x*mVector.w), -sqx - sqy + sqz + sqw);
189   euler.y = asinf(-2.0f * (mVector.x*mVector.z - mVector.y*mVector.w));
190   euler.z = atan2f(2.0f * (mVector.x*mVector.y + mVector.z*mVector.w), sqx - sqy - sqz + sqw);
191   return euler;
192 }
193
194 const Quaternion Quaternion::operator +(const Quaternion &other) const
195 {
196   return Quaternion(mVector + other.mVector);
197 }
198
199 const Quaternion Quaternion::operator -(const Quaternion &other) const
200 {
201   return Quaternion(mVector - other.mVector);
202 }
203
204 const Quaternion Quaternion::operator *(const Quaternion &other) const
205 {
206   MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY,12);
207
208   return Quaternion(mVector.w * other.mVector.w - mVector.Dot(other.mVector),
209                     mVector.y * other.mVector.z - mVector.z * other.mVector.y + mVector.w * other.mVector.x + mVector.x * other.mVector.w,
210                     mVector.z * other.mVector.x - mVector.x * other.mVector.z + mVector.w * other.mVector.y + mVector.y * other.mVector.w,
211                     mVector.x * other.mVector.y - mVector.y * other.mVector.x + mVector.w * other.mVector.z + mVector.z * other.mVector.w);
212 }
213
214 Vector3 Quaternion::operator *(const Vector3& v) const
215 {
216   // nVidia SDK implementation
217   Vector3 uv, uuv;
218   Vector3 qvec(mVector.x, mVector.y, mVector.z);
219   uv = qvec.Cross(v);
220   uuv = qvec.Cross(uv);
221   uv *= (2.0f * mVector.w);
222   uuv *= 2.0f;
223
224   return v + uv + uuv;
225 }
226
227 const Quaternion Quaternion::operator /(const Quaternion &q) const
228 {
229   Quaternion p(q);
230   p.Invert();
231   return *this * p;
232 }
233
234 const Quaternion Quaternion::operator *(float scale) const
235 {
236   return Quaternion(mVector*scale);
237 }
238
239 const Quaternion Quaternion::operator /(float scale) const
240 {
241   return Quaternion(mVector/scale);
242 }
243
244 Quaternion Quaternion::operator -() const
245 {
246   return Quaternion(-mVector.w, -mVector.x, -mVector.y, -mVector.z);
247 }
248
249 const Quaternion& Quaternion::operator +=(const Quaternion &q)
250 {
251   mVector += q.mVector; return *this;
252 }
253
254 const Quaternion& Quaternion::operator -=(const Quaternion &q)
255 {
256   mVector -= q.mVector; return *this;
257 }
258
259 const Quaternion& Quaternion::operator *=(const Quaternion &q)
260 {
261   MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY,12);
262
263   float x = mVector.x, y = mVector.y, z = mVector.z, w = mVector.w;
264
265   mVector.w = mVector.w * q.mVector.w - mVector.Dot(q.mVector);
266   mVector.x = y*q.mVector.z - z*q.mVector.y + w*q.mVector.x + x*q.mVector.w;
267   mVector.y = z*q.mVector.x - x*q.mVector.z + w*q.mVector.y + y*q.mVector.w;
268   mVector.z = x*q.mVector.y - y*q.mVector.x + w*q.mVector.z + z*q.mVector.w;
269   return *this;
270 }
271
272 const Quaternion& Quaternion::operator *= (float scale)
273 {
274   mVector*=scale; return *this;
275 }
276
277 const Quaternion& Quaternion::operator /= (float scale)
278 {
279   mVector/=scale; return *this;
280 }
281
282 bool Quaternion::operator== (const Quaternion& rhs) const
283 {
284   return ( ( fabsf(mVector.x - rhs.mVector.x) < Math::MACHINE_EPSILON_1 &&
285              fabsf(mVector.y - rhs.mVector.y) < Math::MACHINE_EPSILON_1 &&
286              fabsf(mVector.z - rhs.mVector.z) < Math::MACHINE_EPSILON_1 &&
287              fabsf(mVector.w - rhs.mVector.w) < Math::MACHINE_EPSILON_1 ) ||
288            // Or equal to negation of rhs
289            ( fabsf(mVector.x + rhs.mVector.x) < Math::MACHINE_EPSILON_1 &&
290              fabsf(mVector.y + rhs.mVector.y) < Math::MACHINE_EPSILON_1 &&
291              fabsf(mVector.z + rhs.mVector.z) < Math::MACHINE_EPSILON_1 &&
292              fabsf(mVector.w + rhs.mVector.w) < Math::MACHINE_EPSILON_1 )
293          );
294 }
295
296 bool Quaternion::operator!= (const Quaternion& rhs) const
297 {
298   return !operator==(rhs);
299 }
300
301 float Quaternion::Length() const
302 {
303   return (float)sqrt(mVector.w * mVector.w + mVector.Dot(mVector));
304 }
305
306 float Quaternion::LengthSquared() const
307 {
308   return (float)(mVector.w * mVector.w + mVector.Dot(mVector));
309 }
310
311 void Quaternion::Normalize()
312 {
313   *this/=Length();
314 }
315
316 Quaternion Quaternion::Normalized() const
317 {
318   return  *this/Length();
319 }
320
321 void Quaternion::Conjugate()
322 {
323   mVector.x = -mVector.x;
324   mVector.y = -mVector.y;
325   mVector.z = -mVector.z;
326 }
327
328 void Quaternion::Invert()
329 {
330   Conjugate();
331   *this/=LengthSquared();
332 }
333
334 Quaternion Quaternion::Log() const
335 {
336   float a = acosf(mVector.w);
337   float sina = sinf(a);
338   Quaternion ret;
339
340   ret.mVector.w = 0;
341   if (fabsf(sina) >= Math::MACHINE_EPSILON_1)
342   {
343     MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY,4);
344
345     float angleBySinAngle = a * (1.0f / sina);
346     ret.mVector.x = mVector.x * angleBySinAngle;
347     ret.mVector.y = mVector.y * angleBySinAngle;
348     ret.mVector.z = mVector.z * angleBySinAngle;
349   }
350   else
351   {
352     ret.mVector.x= ret.mVector.y= ret.mVector.z= 0;
353   }
354   return ret;
355 }
356
357 Quaternion Quaternion::Exp() const
358 {
359   DALI_ASSERT_ALWAYS( EqualsZero( mVector.w ) && "Cannot perform Exponent" );
360
361   float a = mVector.Length();
362   float sina = sinf(a);
363   Quaternion ret;
364
365   ret.mVector.w = cosf(a);
366
367   if (a >= Math::MACHINE_EPSILON_1)
368   {
369     MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY,4);
370
371     float sinAOverA = sina * (1.0f / a);
372     ret.mVector.x = mVector.x * sinAOverA;
373     ret.mVector.y = mVector.y * sinAOverA;
374     ret.mVector.z = mVector.z * sinAOverA;
375   }
376   else
377   {
378     ret.mVector.x = ret.mVector.y = ret.mVector.z = 0.0f;
379   }
380   return ret;
381 }
382
383 float Quaternion::Dot(const Quaternion &q1, const Quaternion &q2)
384 {
385   return q1.mVector.Dot4(q2.mVector);
386 }
387
388 Quaternion Quaternion::Lerp(const Quaternion &q1, const Quaternion &q2, float t)
389 {
390   return (q1*(1.0f-t) + q2*t).Normalized();
391 }
392
393 Quaternion Quaternion::Slerp(const Quaternion &q1, const Quaternion &q2, float progress)
394 {
395   Quaternion q3;
396   float cosTheta = Quaternion::Dot(q1, q2);
397
398   /**
399    * If cos(theta) < 0, q1 and q2 are more than 90 degrees apart,
400    * so invert one to reduce spinning.
401    */
402   if (cosTheta < 0.0f)
403   {
404     cosTheta = -cosTheta;
405     q3 = -q2;
406   }
407   else
408   {
409     q3 = q2;
410   }
411
412   if (fabsf(cosTheta) < 0.95f)
413   {
414     MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY,5);
415
416     // Normal SLERP
417     float sine = sqrtf(1.0f - cosTheta*cosTheta);
418     float angle = atan2f(sine, cosTheta);
419     float invSine = 1.0f / sine;
420     float coeff0 = sinf((1.0f - progress) * angle) * invSine;
421     float coeff1 = sinf(progress * angle) * invSine;
422
423     return q1*coeff0 + q3*coeff1;
424   }
425   else
426   {
427     // If the angle is small, use linear interpolation
428     Quaternion result = q1*(1.0f - progress) + q3*progress;
429
430     return result.Normalized();
431   }
432 }
433
434 Quaternion Quaternion::SlerpNoInvert(const Quaternion &q1, const Quaternion &q2, float t)
435 {
436   float cosTheta = Quaternion::Dot(q1, q2);
437
438   if (cosTheta > -0.95f && cosTheta < 0.95f)
439   {
440     MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY,2);
441
442     float theta = acosf(cosTheta);
443     return (q1*sinf(theta*(1.0f-t)) + q2*sinf(theta*t))/sinf(theta);
444   }
445   else
446   {
447     return Lerp(q1, q2, t);
448   }
449 }
450
451 Quaternion Quaternion::Squad(
452   const Quaternion &q1, // start
453   const Quaternion &q2, // end
454   const Quaternion &a,  // ctrl pt for q1
455   const Quaternion &b,  // ctrl pt for q2
456   float t)
457 {
458   MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY,2);
459
460   Quaternion c = SlerpNoInvert(q1, q2, t);
461   Quaternion d = SlerpNoInvert(a, b, t);
462   return SlerpNoInvert(c, d, 2*t*(1-t));
463 }
464
465 float Quaternion::AngleBetween(const Quaternion &q1, const Quaternion &q2)
466 {
467   Quaternion from(q1);
468   Quaternion to(q2);
469
470   from.Normalize();
471   to.Normalize();
472
473   //Formula for angle θ between two quaternion is:
474   //θ = cos^−1 (2⟨q1,q2⟩^2 − 1), Where (q1,q2) is inner product of the quaternions.
475   float X = from.mVector.Dot4(to.mVector);
476   float theta = acos( (2 * X * X) - 1);
477
478   return theta;
479 }
480
481 Vector4 Quaternion::Rotate(const Vector4 &v) const
482 {
483   Quaternion V(0.0f, v.x, v.y, v.z);
484   Quaternion conjugate(*this);
485   conjugate.Conjugate();
486   return (*this * V * conjugate).mVector;
487 }
488
489 Vector3 Quaternion::Rotate(const Vector3 &v) const
490 {
491   Quaternion V(0.0f, v.x, v.y, v.z);
492   Quaternion conjugate(*this);
493   conjugate.Conjugate();
494   return Vector3((*this * V * conjugate).mVector);
495 }
496
497 void Quaternion::SetFromAxes( const Vector3& xAxis, const Vector3& yAxis, const Vector3& zAxis )
498 {
499   MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY,4);
500
501   float t = xAxis.x + yAxis.y + zAxis.z;
502   if ( t > 0.0f )                                      // w is largest
503   {
504     float root = sqrtf( t + 1.0f );
505     float one_over_4w = 0.5f / root;
506     mVector.x = ( yAxis.z - zAxis.y ) * one_over_4w;
507     mVector.y = ( zAxis.x - xAxis.z ) * one_over_4w;
508     mVector.z = ( xAxis.y - yAxis.x ) * one_over_4w;
509     mVector.w = root * 0.5f;
510   }
511   else if( zAxis.z > xAxis.x && zAxis.z > yAxis.y )    // z is largest
512   {
513     float root = sqrtf( zAxis.z - xAxis.x - yAxis.y + 1.0f );
514     float one_over_4w = 0.5f / root;
515     mVector.x = ( xAxis.z + zAxis.x ) * one_over_4w;
516     mVector.y = ( yAxis.z + zAxis.y ) * one_over_4w;
517     mVector.z = root * 0.5f;
518     mVector.w = ( xAxis.y - yAxis.x ) * one_over_4w;
519   }
520   else if( yAxis.y > xAxis.x )                         // y is largest
521   {
522     float root = sqrtf(yAxis.y - zAxis.z - xAxis.x + 1.0f );
523     float one_over_4w = 0.5f / root;
524
525     mVector.x = ( xAxis.y + yAxis.x ) * one_over_4w;
526     mVector.y = root * 0.5f;
527     mVector.z = ( zAxis.y + yAxis.z ) * one_over_4w;
528     mVector.w = ( zAxis.x - xAxis.z ) * one_over_4w;
529   }
530   else                                                 // x is largest
531   {
532     float root = sqrtf( xAxis.x - yAxis.y - zAxis.z + 1.0f );
533     float one_over_4w = 0.5f / root;
534     mVector.x = root * 0.5f;
535     mVector.y = ( yAxis.x + xAxis.y ) * one_over_4w;
536     mVector.z = ( zAxis.x + xAxis.z ) * one_over_4w;
537     mVector.w = ( yAxis.z - zAxis.y ) * one_over_4w;
538   }
539
540   Normalize();
541 }
542
543 std::ostream& operator<< (std::ostream& o, const Quaternion& quaternion)
544 {
545   Vector3 axis;
546   float angleRadians;
547
548   quaternion.ToAxisAngle( axis, angleRadians );
549   Degree degrees = Radian(angleRadians);
550
551   return o << "[ Axis: [" << axis.x << ", " << axis.y << ", " << axis.z << "], Angle: " << degrees << " degrees ]";
552 }
553
554 } // namespace Dali
555