From f3768834c52d37f1cc13d4293e3322180ff5d6d7 Mon Sep 17 00:00:00 2001 From: Vivek Ellur Date: Tue, 28 Jul 2015 01:00:34 +0200 Subject: [PATCH] eina_quaternion: add forgotten implementation of converting eina_matrix3 to eina_quaternion Summary: Implemenation of eina_matrix3_quaternion_get function Signed-off-by: Vivek Ellur 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 --- src/lib/eina/eina_quaternion.c | 50 +++++++++++++++++++++++++++++++++-- src/tests/eina/eina_test_quaternion.c | 5 ++++ 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/src/lib/eina/eina_quaternion.c b/src/lib/eina/eina_quaternion.c index 8c751ac..0334d57 100644 --- a/src/lib/eina/eina_quaternion.c +++ b/src/lib/eina/eina_quaternion.c @@ -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; } diff --git a/src/tests/eina/eina_test_quaternion.c b/src/tests/eina/eina_test_quaternion.c index 2e47580..1ffbc1a 100644 --- a/src/tests/eina/eina_test_quaternion.c +++ b/src/tests/eina/eina_test_quaternion.c @@ -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 -- 2.7.4