[vlc-devel] [PATCH 16/18] viewpoint: add test for euler/viewpoint/euler conversions

Alexandre Janniaux ajanni at videolabs.io
Wed Mar 31 09:25:48 UTC 2021


The conversion to and form viewpoint is trivial with the viewpoint
storing Euler angles but wont be when it will store a quaternion
instead.
---
 test/Makefile.am          |  3 ++
 test/src/misc/viewpoint.c | 93 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 96 insertions(+)
 create mode 100644 test/src/misc/viewpoint.c

diff --git a/test/Makefile.am b/test/Makefile.am
index 693acf0edc..2148dd6505 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -33,6 +33,7 @@ check_PROGRAMS = \
 	test_src_misc_bits \
 	test_src_misc_epg \
 	test_src_misc_keystore \
+	test_src_misc_viewpoint \
 	test_modules_packetizer_helpers \
 	test_modules_packetizer_hxxx \
 	test_modules_packetizer_h264 \
@@ -136,6 +137,8 @@ test_src_misc_epg_SOURCES = src/misc/epg.c
 test_src_misc_epg_LDADD = $(LIBVLCCORE) $(LIBVLC)
 test_src_misc_keystore_SOURCES = src/misc/keystore.c
 test_src_misc_keystore_LDADD = $(LIBVLCCORE) $(LIBVLC)
+test_src_misc_viewpoint_SOURCES = src/misc/viewpoint.c
+test_src_misc_viewpoint_LDADD = $(LIBVLCCORE)
 test_src_interface_dialog_SOURCES = src/interface/dialog.c
 test_src_interface_dialog_LDADD = $(LIBVLCCORE) $(LIBVLC)
 test_src_media_source_LDADD = $(LIBVLCCORE) $(LIBVLC)
diff --git a/test/src/misc/viewpoint.c b/test/src/misc/viewpoint.c
new file mode 100644
index 0000000000..0a759678f6
--- /dev/null
+++ b/test/src/misc/viewpoint.c
@@ -0,0 +1,93 @@
+/*****************************************************************************
+ * viewpoint.c: test for viewpoint
+ *****************************************************************************
+ * Copyright (C) 2019 VLC Authors and VideoLAN
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+#include <vlc_viewpoint.h>
+#include "../../libvlc/test.h"
+
+
+static bool
+compare_angles(float epsilon, const float a1[3], const float a2[3])
+{
+    const float MAX_YAW   = 180.f;
+    const float MAX_PITCH = 360.f;
+    const float MAX_ROLL  = 180.f;
+
+    /* We add MAX_YAW, MAX_PITCH and MAX_ROLL to ensure the value for fmodf
+     * will stay positive. The value will be between 0 and {MAX_ANGLE}. */
+    float dy = fmodf(MAX_YAW   + (a1[0] - a2[0]), MAX_YAW);
+    float dp = fmodf(MAX_PITCH + (a1[1] - a2[1]), MAX_PITCH);
+    float dr = fmodf(MAX_ROLL  + (a1[2] - a2[2]), MAX_ROLL);
+
+    /* Check the two borders of the torus, 0.f and 180.f or 360.f depending
+     * on the range of the compared value. */
+    return (dy < epsilon || MAX_YAW   - dy < epsilon) &&
+           (dp < epsilon || MAX_PITCH - dp < epsilon) &&
+           (dr < epsilon || MAX_ROLL  - dr < epsilon);
+}
+
+/**
+ * Execute conversion back and forth from Euler angles to viewpoint.
+ * Check that the original angles are preserved by the conversion methods.
+ */
+static bool
+reciprocal_euler(float epsilon, float yaw, float pitch, float roll)
+{
+    vlc_viewpoint_t vp;
+    vlc_viewpoint_from_euler(&vp, yaw, pitch, roll);
+
+    float yaw2, pitch2, roll2;
+    vlc_viewpoint_to_euler(&vp, &yaw2, &pitch2, &roll2);
+
+    fprintf(stderr, "original:   yaw=%f, pitch=%f, roll=%f\n", yaw, pitch, roll);
+    fprintf(stderr, "converted:  yaw=%f, pitch=%f, roll=%f\n\n", yaw2, pitch2, roll2);
+
+    return compare_angles(epsilon,
+            (float[]){yaw, pitch, roll},
+            (float[]){yaw2, pitch2, roll2});
+}
+
+static void
+test_conversion_euler_to_euler()
+{
+    const float epsilon = 0.1f;
+    assert(reciprocal_euler(epsilon, 0.f,  0.f,  0.f));
+    assert(reciprocal_euler(epsilon, 45.f, 0.f,  0.f));
+    assert(reciprocal_euler(epsilon, 0.f,  45.f, 0.f));
+    assert(reciprocal_euler(epsilon, 0.f,  0.f,  45.f));
+    assert(reciprocal_euler(epsilon, 45.f, 45.f, 0.f));
+    assert(reciprocal_euler(epsilon, 0.f,  45.f, 45.f));
+    assert(reciprocal_euler(epsilon, 45.f, 45.f, 45.f));
+
+    assert(reciprocal_euler(epsilon, -45.f,  0.f,  0.f));
+    assert(reciprocal_euler(epsilon,  0.f,  -45.f, 0.f));
+    assert(reciprocal_euler(epsilon,  0.f,   0.f,  -45.f));
+    assert(reciprocal_euler(epsilon, -45.f, -45.f,  0.f));
+    assert(reciprocal_euler(epsilon,  0.f,  -45.f, -45.f));
+    assert(reciprocal_euler(epsilon, -45.f, -45.f, -45.f));
+}
+
+int main( void )
+{
+    test_init();
+
+    test_conversion_euler_to_euler();
+
+    return 0;
+}
-- 
2.31.0



More information about the vlc-devel mailing list