From 48e8894b741ffa174c69b7f2c69396faf964933c Mon Sep 17 00:00:00 2001 From: Moses Turner <moses@collabora.com> Date: Mon, 19 Sep 2022 21:52:03 -0500 Subject: [PATCH] u/json: Add u_json_get_pose_permissive To parse poses from non-Monado configs with non-standard member names. --- src/xrt/auxiliary/util/u_json.c | 36 +++++++++++++++++++++++++++++++++ src/xrt/auxiliary/util/u_json.h | 10 +++++++++ 2 files changed, 46 insertions(+) diff --git a/src/xrt/auxiliary/util/u_json.c b/src/xrt/auxiliary/util/u_json.c index 98cc1e4b6..d167d7979 100644 --- a/src/xrt/auxiliary/util/u_json.c +++ b/src/xrt/auxiliary/util/u_json.c @@ -278,6 +278,42 @@ u_json_get_pose(const cJSON *json, struct xrt_pose *out_pose) return good; } +bool +u_json_get_pose_permissive(const cJSON *json, struct xrt_pose *out_pose) +{ + struct xrt_pose tmp; + + const char *position_names[] = {"position", "translation", "location", "pos", "loc"}; + const char *orientation_names[] = {"orientation", "rotation", "rot"}; + + bool found_position = false; + + for (uint32_t i = 0; i < ARRAY_SIZE(position_names); i++) { + found_position = u_json_get_vec3(u_json_get(json, position_names[i]), &tmp.position); + if (found_position) { + break; + } + } + if (!found_position) { + return false; + } + + bool found_orientation = false; + + for (uint32_t i = 0; i < ARRAY_SIZE(orientation_names); i++) { + found_orientation = u_json_get_vec3(u_json_get(json, orientation_names[i]), &tmp.position); + if (found_orientation) { + break; + } + } + if (!found_orientation) { + return false; + } + + + return true; +} + size_t u_json_get_float_array(const cJSON *json_array, float *out_array, size_t max_size) diff --git a/src/xrt/auxiliary/util/u_json.h b/src/xrt/auxiliary/util/u_json.h index a2d51e1db..6fb7cfae2 100644 --- a/src/xrt/auxiliary/util/u_json.h +++ b/src/xrt/auxiliary/util/u_json.h @@ -110,6 +110,16 @@ bool u_json_get_pose(const cJSON *json, struct xrt_pose *out_pose); +/*! + * @brief Parse a pose from a JSON object, composed of + * a vec3 named "position", "translation", "location", "pos", or "loc" + * and a quat named "orientation". "rotation", or "rot" + * + * @return true if successful, false if not. + */ +bool +u_json_get_pose_permissive(const cJSON *json, struct xrt_pose *out_pose); + /*! * @brief Parse up to max_size floats from a JSON array. *