From 2adbde4673e3b122081425ef2920a75079639224 Mon Sep 17 00:00:00 2001 From: Ryan Pavlik Date: Mon, 26 Apr 2021 15:28:12 -0500 Subject: [PATCH] a/util: Add u_device_get_view_pose helper function. I saw many, many exact copies of this function in the code: good to de-duplicate them. --- src/xrt/auxiliary/util/u_device.c | 27 ++++++++++++++++++++++++++- src/xrt/auxiliary/util/u_device.h | 19 ++++++++++++++++++- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/xrt/auxiliary/util/u_device.c b/src/xrt/auxiliary/util/u_device.c index 3abc947f9..55ca8cf77 100644 --- a/src/xrt/auxiliary/util/u_device.c +++ b/src/xrt/auxiliary/util/u_device.c @@ -1,9 +1,10 @@ -// Copyright 2019-2020, Collabora, Ltd. +// Copyright 2019-2021, Collabora, Ltd. // SPDX-License-Identifier: BSL-1.0 /*! * @file * @brief Misc helpers for device drivers. * @author Jakob Bornecrantz + * @author Ryan Pavlik * @ingroup aux_util */ @@ -395,3 +396,27 @@ u_device_setup_tracking_origins(struct xrt_device *head, apply_offset(&right->tracking_origin->offset.position, global_tracking_origin_offset); } } + +void +u_device_get_view_pose(const struct xrt_vec3 *eye_relation, uint32_t view_index, struct xrt_pose *out_pose) +{ + struct xrt_pose pose = XRT_POSE_IDENTITY; + bool adjust = view_index == 0; + + pose.position.x = eye_relation->x / 2.0f; + pose.position.y = eye_relation->y / 2.0f; + pose.position.z = eye_relation->z / 2.0f; + + // Adjust for left/right while also making sure there aren't any -0.f. + if (pose.position.x > 0.0f && adjust) { + pose.position.x = -pose.position.x; + } + if (pose.position.y > 0.0f && adjust) { + pose.position.y = -pose.position.y; + } + if (pose.position.z > 0.0f && adjust) { + pose.position.z = -pose.position.z; + } + + *out_pose = pose; +} diff --git a/src/xrt/auxiliary/util/u_device.h b/src/xrt/auxiliary/util/u_device.h index b29d1b9a6..857ebb705 100644 --- a/src/xrt/auxiliary/util/u_device.h +++ b/src/xrt/auxiliary/util/u_device.h @@ -1,9 +1,10 @@ -// Copyright 2019-2020, Collabora, Ltd. +// Copyright 2019-2021, Collabora, Ltd. // SPDX-License-Identifier: BSL-1.0 /*! * @file * @brief Misc helpers for device drivers. * @author Jakob Bornecrantz + * @author Ryan Pavlik * @ingroup aux_util */ @@ -113,6 +114,22 @@ u_device_setup_tracking_origins(struct xrt_device *head, struct xrt_device *right, struct xrt_vec3 *global_tracking_origin_offset); +/*! + * Helper function for `get_view_pose` in an HMD driver. + * + * Takes in a translation from the left to right eye, and returns a center to left or right eye transform that assumes + * the eye relation is symmetrical around the tracked point ("center eye"). Knowing IPD is a subset of this: If you know + * IPD better than the overall Monado system, copy @p eye_relation and put your known IPD in @p real_eye_relation->x + * + * If you have rotation, apply it after calling this function. + * + * @param eye_relation 3D translation from left eye to right eye. + * @param view_index 0 for left, 1 for right. + * @param out_pose The output pose to populate. Will receive translation, with an identity rotation. + */ +void +u_device_get_view_pose(const struct xrt_vec3 *eye_relation, uint32_t view_index, struct xrt_pose *out_pose); + #ifdef __cplusplus } #endif