eina_quaternion: add forgotten implementation of converting eina_matrix3 to eina_quat...
authorVivek Ellur <vivek.ellur@samsung.com>
Mon, 27 Jul 2015 23:00:34 +0000 (01:00 +0200)
committerCedric BAIL <cedric@osg.samsung.com>
Tue, 28 Jul 2015 01:05:37 +0000 (03:05 +0200)
Summary:
Implemenation of eina_matrix3_quaternion_get function

Signed-off-by: Vivek Ellur <vivek.ellur@samsung.com>
Reviewers: cedric

Subscribers: cedric

Differential Revision: https://phab.enlightenment.org/D2786

This was a function I forgot to finish implement. Thanks Vivek to take care of it.
As it comes with a test case, is self contained and fix a missing bit of code I will
push it at this point in time of our release process. Sorry everyone for that late push.

Signed-off-by: Cedric BAIL <cedric@osg.samsung.com>
src/lib/eina/eina_quaternion.c
src/tests/eina/eina_test_quaternion.c

index 8c751ac..0334d57 100644 (file)
@@ -624,7 +624,53 @@ eina_quaternion_rotation_matrix3_get(Eina_Matrix3 *m,
 }
 
 EAPI void
-eina_matrix3_quaternion_get(Eina_Quaternion *q EINA_UNUSED,
-                            const Eina_Matrix3 *m EINA_UNUSED)
+eina_matrix3_quaternion_get(Eina_Quaternion *q,
+                            const Eina_Matrix3 *m)
 {
+   double tval;
+   double w, x, y, z;
+
+   tval = m->xx + m->yy + m->zz;
+
+   if (tval > 0)
+     {
+
+        double s = 0.5 / sqrtf(tval + 1.0);
+
+        w = (0.25 / s);
+        x = ((m->zy - m->yz) * s);
+        y = ((m->xz - m->zx) * s);
+        z = ((m->yx - m->xy) * s);
+     }
+   else if ((m->xx > m->yy) && (m->xx > m->zz))
+     {
+        double s = 2.0 * sqrtf(1.0 + m->xx - m->yy - m->zz);
+
+        w = ((m->zy - m->yz) / s);
+        x = (0.25 * s);
+        y = ((m->xy + m->yx) / s);
+        z = ((m->xz + m->zx) / s);
+     }
+   else if (m->yy > m->zz)
+     {
+        double s = 2.0 * sqrtf(1.0 + m->yy - m->xx - m->zz);
+
+        w = ((m->xz - m->zx) / s);
+        x = ((m->xy + m->yx) / s);
+        y = (0.25 * s);
+        z = ((m->yz + m->zy) / s);
+     }
+   else
+     {
+        double s = 2.0 * sqrtf(1.0 + m->zz - m->xx - m->yy);
+
+        w = ((m->yx - m->xy) / s);
+        x = ((m->xz + m->zx) / s);
+        y = ((m->yz + m->zy) / s);
+        z = (0.25 * s);
+     }
+   q->w = w;
+   q->x = x;
+   q->y = y;
+   q->z = z;
 }
index 2e47580..1ffbc1a 100644 (file)
@@ -90,6 +90,8 @@ END_TEST
 START_TEST(eina_test_quaternion_matrix)
 {
    Eina_Quaternion q = { 7, 9, 5, 1 };
+   Eina_Quaternion q1 = {7, 9, 5, -1 };
+   Eina_Quaternion tq;
    Eina_Matrix3 m = {
      -211, 136, 52,
      116, -147, 104,
@@ -102,6 +104,9 @@ START_TEST(eina_test_quaternion_matrix)
    eina_quaternion_rotation_matrix3_get(&tm, &q);
    fail_if(!eina_matrix3_cmp(&tm, &m));
 
+   eina_matrix3_quaternion_get(&tq, &m);
+   fail_if(!eina_quaternion_cmp(&tq, &q) && !eina_quaternion_cmp(&tq, &q1));
+
    eina_shutdown();
 }
 END_TEST