From 0bdd1133e483c7e584060a4b7d4b9331da950d9a Mon Sep 17 00:00:00 2001 From: Lubosz Sarnecki Date: Fri, 26 Jun 2020 13:03:19 +0200 Subject: [PATCH] u/json: Add u_json_get_vec3_array. As seen in Vive driver. --- src/xrt/auxiliary/util/u_json.c | 37 +++++++++++++++++++++++++++++++++ src/xrt/auxiliary/util/u_json.h | 8 +++++++ 2 files changed, 45 insertions(+) diff --git a/src/xrt/auxiliary/util/u_json.c b/src/xrt/auxiliary/util/u_json.c index 44162066a..26eaa9e1f 100644 --- a/src/xrt/auxiliary/util/u_json.c +++ b/src/xrt/auxiliary/util/u_json.c @@ -146,6 +146,43 @@ u_json_get_vec3(const cJSON *json, struct xrt_vec3 *out_vec3) return true; } +bool +u_json_get_vec3_array(const cJSON *json, struct xrt_vec3 *out_vec3) +{ + assert(out_vec3 != NULL); + + if (!json) { + return false; + } + if (!cJSON_IsArray(json)) { + return false; + } + + float array[3]; + + if (cJSON_GetArraySize(json) != 3) { + return false; + } + + const cJSON *item = NULL; + size_t i = 0; + cJSON_ArrayForEach(item, json) + { + assert(cJSON_IsNumber(item)); + array[i] = (float)item->valuedouble; + ++i; + if (i == 3) { + break; + } + } + + out_vec3->x = array[0]; + out_vec3->y = array[1]; + out_vec3->z = array[2]; + + return true; +} + bool u_json_get_quat(const cJSON *json, struct xrt_quat *out_quat) { diff --git a/src/xrt/auxiliary/util/u_json.h b/src/xrt/auxiliary/util/u_json.h index 94c2a1740..e402e3e37 100644 --- a/src/xrt/auxiliary/util/u_json.h +++ b/src/xrt/auxiliary/util/u_json.h @@ -77,6 +77,14 @@ u_json_get_double(const cJSON *json, double *out_double); bool u_json_get_vec3(const cJSON *json, struct xrt_vec3 *out_vec3); +/*! + * @brief Parse a vec3 from a JSON array. + * + * @return true if successful, false if not. + */ +bool +u_json_get_vec3_array(const cJSON *json, struct xrt_vec3 *out_vec3); + /*! * @brief Parse a quaternion from a JSON object. *