commit d1c207e1c9cc8653afc01b989428e8d4370900df
parent e0bc8ec773b3216517fd54ddbb9b364766cf44b0
Author: vaplv <vaplv@free.fr>
Date: Sat, 14 Feb 2015 15:55:18 +0100
Test the quaternion functions
Diffstat:
2 files changed, 80 insertions(+), 1 deletion(-)
diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt
@@ -121,6 +121,7 @@ if(NOT NO_TEST)
endmacro(new_test)
new_test(test_atomic)
+ new_test(test_binary_heap rsys)
new_test(test_dynamic_array rsys)
new_test(test_float2 m)
new_test(test_float3 m)
@@ -134,8 +135,8 @@ if(NOT NO_TEST)
new_test(test_list rsys)
new_test(test_logger rsys)
new_test(test_mem_allocator rsys)
- new_test(test_binary_heap rsys)
new_test(test_math m)
+ new_test(test_quaternion rsys)
new_test(test_ref)
new_test(test_signal rsys)
new_test(test_str rsys)
diff --git a/src/test_quaternion.c b/src/test_quaternion.c
@@ -0,0 +1,78 @@
+/* Copyright (C) 2013-2015 Vincent Forest (vaplv@free.fr)
+ *
+ * The RSys library is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * The RSys library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with the RSys library. If not, see <http://www.gnu.org/licenses/>. */
+
+#include "quaternion.h"
+
+int
+main(int argc, char** argv)
+{
+ float v3[3], m33[9];
+ float q0[4], q1[4], q2[4];
+ (void)argc, (void)argv;
+
+ CHECK(quat_identity(q0), q0);
+ CHECK(q0[0], 0.f);
+ CHECK(q0[1], 0.f);
+ CHECK(q0[2], 0.f);
+ CHECK(q0[3], 1.f);
+
+ f3(v3, 2.f, 5.f, 1.f);
+ CHECK(quat_set_axis_angle(q0, v3, (float)PI*0.3f), q0);
+ CHECK(eq_eps(q0[0], 0.907981f, 1.e-6f), 1);
+ CHECK(eq_eps(q0[1], 2.269953f, 1.e-6f), 1);
+ CHECK(eq_eps(q0[2], 0.453991f, 1.e-6f), 1);
+ CHECK(eq_eps(q0[3], 0.891007f, 1.e-6f), 1);
+
+ f4(q0, 1.f, 2.f, 3.f, 4.f);
+ f4(q1, 5.f, 6.f, 7.f, 8.f);
+ CHECK(quat_mul(q2, q0, q1), q2);
+ CHECK(q2[0], 24.f);
+ CHECK(q2[1], 48.f);
+ CHECK(q2[2], 48.f);
+ CHECK(q2[3], -6.f);
+
+ CHECK(quat_conj(q2, q0), q2);
+ CHECK(q2[0], -1.f);
+ CHECK(q2[1], -2.f);
+ CHECK(q2[2], -3.f);
+ CHECK(q2[3], 4.f);
+
+ f4_normalize(q0, f4(q0, 1.f, 2.f, 5.f, 0.5f));
+ f3_set(q1, q0);
+ q1[3] = quat_calca(q1);
+ CHECK(eq_eps(q1[3], q0[3], 1.e-6f), 1);
+
+ f4(q0, 1.f, 2.F, 3.f, 5.f);
+ f4(q1, 2.f, 6.f, 7.f, 6.f);
+ CHECK(quat_slerp(q2, q0, q1, 0.3f), q2);
+ CHECK(eq_eps(q2[0], 1.3f, 1.e-6f), 1);
+ CHECK(eq_eps(q2[1], 3.2f, 1.e-6f), 1);
+ CHECK(eq_eps(q2[2], 4.2f, 1.e-6f), 1);
+ CHECK(eq_eps(q2[3], 5.3f, 1.e-6f), 1);
+
+ f4(q0, 2.f, 5.f, 17.f, 9.f);
+ CHECK(quat_to_f33(m33, q0), m33);
+ CHECK(f3_eq_eps(m33 + 0, f3(v3, -627.f, 326.f, -22.f), 1.e-6f), 1);
+ CHECK(f3_eq_eps(m33 + 3, f3(v3, -286.f, -585.f, 206.f), 1.e-6f), 1);
+ CHECK(f3_eq_eps(m33 + 6, f3(v3, 158.f, 134.f, -57.f), 1.e-6f), 1);
+
+ f4_normalize(q0, q0);
+ CHECK(quat_to_f33(m33, q0), m33);
+ CHECK(f3_eq_eps(m33 + 0, f3(v3,-0.573935f, 0.817043f,-0.055138f), 1.e-6f), 1);
+ CHECK(f3_eq_eps(m33 + 3, f3(v3,-0.716792f,-0.468672f, 0.516291f), 1.e-6f), 1);
+ CHECK(f3_eq_eps(m33 + 6, f3(v3, 0.395990f, 0.335840f, 0.854637f), 1.e-6f), 1);
+
+ return 0;
+}