From a6c64f297dc6c50290a9710b0c9e8fce49e67550 Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Thu, 30 Jan 2025 16:09:19 +0100 Subject: [PATCH] Implement derived poses Client can describe a pose to be derived from an other one, for instance AIM can be derived from GRIP by applying an offset and rotation. This reduces the amount of data that needs to be polled and transmitted over the network. --- client/scenes/stream.cpp | 3 ++ client/scenes/stream.h | 2 + client/scenes/stream_tracking.cpp | 49 ++++++++++++++++++++++ common/wivrn_packets.h | 8 ++++ server/driver/pose_list.cpp | 35 ++++++++++++++-- server/driver/pose_list.h | 16 ++++++-- server/driver/wivrn_controller.cpp | 66 +++++++++++++++++++----------- server/driver/wivrn_controller.h | 1 + server/driver/wivrn_session.cpp | 6 +++ server/driver/wivrn_session.h | 1 + 10 files changed, 157 insertions(+), 30 deletions(-) diff --git a/client/scenes/stream.cpp b/client/scenes/stream.cpp index da1b9643..ea6c44ed 100644 --- a/client/scenes/stream.cpp +++ b/client/scenes/stream.cpp @@ -1180,6 +1180,9 @@ void scenes::stream::on_xr_event(const xr::event & event) .view_index = uint8_t(event.visibility_mask_changed.viewIndex), }); break; + case XR_TYPE_EVENT_DATA_INTERACTION_PROFILE_CHANGED: + on_interaction_profile_changed(event.interaction_profile_changed); + break; default: break; } diff --git a/client/scenes/stream.h b/client/scenes/stream.h index de4ce922..e36a9e36 100644 --- a/client/scenes/stream.h +++ b/client/scenes/stream.h @@ -179,6 +179,8 @@ private: void tracking(); void read_actions(); + void on_interaction_profile_changed(const XrEventDataInteractionProfileChanged &); + void setup(const to_headset::video_stream_description &); void setup_reprojection_swapchain(); void exit(); diff --git a/client/scenes/stream_tracking.cpp b/client/scenes/stream_tracking.cpp index d44a6fe0..d0bf8d58 100644 --- a/client/scenes/stream_tracking.cpp +++ b/client/scenes/stream_tracking.cpp @@ -198,6 +198,8 @@ void scenes::stream::tracking() const bool hand_tracking = config.check_feature(feature::hand_tracking); const bool face_tracking = config.check_feature(feature::face_tracking); + on_interaction_profile_changed({}); + while (not exiting) { try @@ -368,3 +370,50 @@ void scenes::stream::operator()(to_headset::tracking_control && packet) tracking_control = packet; tracking_control.min_offset = std::min(tracking_control.min_offset, tracking_control.max_offset); } + +void scenes::stream::on_interaction_profile_changed(const XrEventDataInteractionProfileChanged & event) +{ + auto now = instance.now(); + for (auto [target, space]: { + std::tuple{device_id::LEFT_AIM, application::space(xr::spaces::aim_left)}, + {device_id::LEFT_PALM, application::space(xr::spaces::palm_left)}, + {device_id::RIGHT_AIM, application::space(xr::spaces::aim_right)}, + {device_id::RIGHT_PALM, application::space(xr::spaces::palm_right)}, + }) + { + device_id source; + XrSpace source_space = XR_NULL_HANDLE; + switch (target) + { + case device_id::LEFT_AIM: + case device_id::LEFT_PALM: + source = device_id::LEFT_GRIP; + source_space = application::space(xr::spaces::grip_left); + break; + case device_id::RIGHT_AIM: + case device_id::RIGHT_PALM: + source = device_id::RIGHT_GRIP; + source_space = application::space(xr::spaces::grip_right); + break; + default: + continue; + } + auto pose = locate_space(target, space, source_space, now); + + if (pose.flags & from_headset::tracking::position_valid and pose.flags & from_headset::tracking::orientation_valid) + { + network_session->send_control(from_headset::derived_pose{ + .source = source, + .target = target, + .relation = pose.pose, + }); + } + else + { + network_session->send_control(from_headset::derived_pose{ + .source = target, + .target = target, + }); + } + } +} diff --git a/common/wivrn_packets.h b/common/wivrn_packets.h index 9a127b88..c52a9b90 100644 --- a/common/wivrn_packets.h +++ b/common/wivrn_packets.h @@ -215,6 +215,13 @@ struct trackings std::vector items; }; +struct derived_pose +{ + device_id source; + device_id target; + XrPosef relation; +}; + struct hand_tracking { enum flags : uint8_t @@ -308,6 +315,7 @@ using packets = std::variant< handshake, tracking, trackings, + derived_pose, hand_tracking, inputs, timesync_response, diff --git a/server/driver/pose_list.cpp b/server/driver/pose_list.cpp index 4c146d3e..78dcedb5 100644 --- a/server/driver/pose_list.cpp +++ b/server/driver/pose_list.cpp @@ -65,18 +65,47 @@ xrt_space_relation pose_list::extrapolate(const xrt_space_relation & a, const xr bool pose_list::update_tracking(const from_headset::tracking & tracking, const clock_offset & offset) { + std::shared_lock lock(derived_mutex); + if (source) + return false; + for (const auto & pose: tracking.device_poses) { if (pose.device != device) continue; - xrt_space_relation new_pose = convert_pose(pose); - math_quat_rotate(&new_pose.pose.orientation, &rotation_offset, &new_pose.pose.orientation); - return add_sample(tracking.production_timestamp, tracking.timestamp, new_pose, offset); + return add_sample(tracking.production_timestamp, tracking.timestamp, convert_pose(pose), offset); } return true; } +void pose_list::set_derived(pose_list * source, xrt_pose offset, bool force) +{ + std::lock_guard lock(derived_mutex); + if (derive_forced and not force) + return; + // TODO: check for loops? + if (source == this) + this->source = nullptr; + else + this->source = source; + this->offset = offset; + this->derive_forced = force; +} + +std::tuple pose_list::get_pose_at(XrTime at_timestamp_ns) +{ + std::shared_lock lock(derived_mutex); + if (source) + { + auto res = source->get_pose_at(at_timestamp_ns); + math_pose_transform(&std::get<1>(res).pose, &offset, &std::get<1>(res).pose); + return res; + } + + return std::tuple_cat(get_at(at_timestamp_ns), std::make_tuple(device)); +} + xrt_space_relation pose_list::convert_pose(const from_headset::tracking::pose & pose) { xrt_space_relation res{}; diff --git a/server/driver/pose_list.h b/server/driver/pose_list.h index 710f3ef3..d1e7b3a8 100644 --- a/server/driver/pose_list.h +++ b/server/driver/pose_list.h @@ -23,24 +23,32 @@ #include "wivrn_packets.h" #include "xrt/xrt_defines.h" +#include + namespace wivrn { struct clock_offset; class pose_list : public history { + std::shared_mutex derived_mutex; + pose_list * source = nullptr; + xrt_pose offset; + bool derive_forced = false; + public: const wivrn::device_id device; - const xrt_quat rotation_offset; static xrt_space_relation interpolate(const xrt_space_relation & a, const xrt_space_relation & b, float t); static xrt_space_relation extrapolate(const xrt_space_relation & a, const xrt_space_relation & b, int64_t ta, int64_t tb, int64_t t); - pose_list(wivrn::device_id id, xrt_quat rotation_quat = XRT_QUAT_IDENTITY) : - device(id), - rotation_offset(rotation_quat) {} + pose_list(wivrn::device_id id) : + device(id) {} bool update_tracking(const wivrn::from_headset::tracking &, const clock_offset & offset); + void set_derived(pose_list * source, xrt_pose offset, bool force = false); + + std::tuple get_pose_at(XrTime at_timestamp_ns); static xrt_space_relation convert_pose(const wivrn::from_headset::tracking::pose &); }; diff --git a/server/driver/wivrn_controller.cpp b/server/driver/wivrn_controller.cpp index 7f507bc7..ca2942ae 100644 --- a/server/driver/wivrn_controller.cpp +++ b/server/driver/wivrn_controller.cpp @@ -19,6 +19,7 @@ #include "wivrn_controller.h" #include "configuration.h" +#include "driver/xrt_cast.h" #include "math/m_api.h" #include "wivrn_session.h" @@ -219,28 +220,13 @@ static std::ofstream & tracking_dump() return res; } -static auto make_palm(int hand_id, bool palm_pose) -{ - if (auto grip_surface = configuration::read_user_configuration().grip_surface) - { - std::array angles = grip_surface.value(); - float deg_2_rad = std::numbers::pi / 180.0; - xrt_vec3 rotation_angles{angles[0] * deg_2_rad, angles[1] * deg_2_rad, angles[2] * deg_2_rad}; - xrt_quat rotation_quat = XRT_QUAT_IDENTITY; - math_quat_from_euler_angles(&rotation_angles, &rotation_quat); - - return pose_list(hand_id == 0 ? device_id::LEFT_GRIP : device_id::RIGHT_GRIP, rotation_quat); - } - return pose_list(hand_id == 0 ? device_id::LEFT_PALM : device_id::RIGHT_PALM); -} - wivrn_controller::wivrn_controller(int hand_id, xrt_device * hmd, wivrn::wivrn_session * cnx) : xrt_device{}, grip(hand_id == 0 ? device_id::LEFT_GRIP : device_id::RIGHT_GRIP), aim(hand_id == 0 ? device_id::LEFT_AIM : device_id::RIGHT_AIM), - palm(make_palm(hand_id, cnx->get_info().palm_pose)), + palm(hand_id == 0 ? device_id::LEFT_PALM : device_id::RIGHT_PALM), joints(hand_id), cnx(cnx) { @@ -300,7 +286,20 @@ wivrn_controller::wivrn_controller(int hand_id, inputs[WIVRN_CONTROLLER_HAND_TRACKER].active = hand_tracking_supported; inputs[WIVRN_CONTROLLER_PALM_POSE].name = XRT_INPUT_GENERIC_PALM_POSE; - inputs[WIVRN_CONTROLLER_PALM_POSE].active = cnx->get_info().palm_pose or palm.device == grip.device; + inputs[WIVRN_CONTROLLER_PALM_POSE].active = cnx->get_info().palm_pose; + + if (auto grip_surface = configuration::read_user_configuration().grip_surface) + { + std::array angles = grip_surface.value(); + float deg_2_rad = std::numbers::pi / 180.0; + xrt_vec3 rotation_angles{angles[0] * deg_2_rad, angles[1] * deg_2_rad, angles[2] * deg_2_rad}; + xrt_pose offset = XRT_POSE_IDENTITY; + math_quat_from_euler_angles(&rotation_angles, &offset.orientation); + + palm.set_derived(&grip, offset, true); + cnx->set_enabled(palm.device, false); + inputs[WIVRN_CONTROLLER_PALM_POSE].active = true; + } output_count = 1; outputs = &haptic_output; @@ -401,19 +400,20 @@ xrt_space_relation wivrn_controller::get_tracked_pose(xrt_input_name name, int64 { std::chrono::nanoseconds extrapolation_time; xrt_space_relation res; + device_id device; switch (name) { case XRT_INPUT_TOUCH_AIM_POSE: - std::tie(extrapolation_time, res) = aim.get_at(at_timestamp_ns); - cnx->set_enabled(aim.device, true); + std::tie(extrapolation_time, res, device) = aim.get_pose_at(at_timestamp_ns); + cnx->set_enabled(device, true); break; case XRT_INPUT_TOUCH_GRIP_POSE: - std::tie(extrapolation_time, res) = grip.get_at(at_timestamp_ns); - cnx->set_enabled(grip.device, true); + std::tie(extrapolation_time, res, device) = grip.get_pose_at(at_timestamp_ns); + cnx->set_enabled(device, true); break; case XRT_INPUT_GENERIC_PALM_POSE: - std::tie(extrapolation_time, res) = palm.get_at(at_timestamp_ns); - cnx->set_enabled(palm.device, true); + std::tie(extrapolation_time, res, device) = palm.get_pose_at(at_timestamp_ns); + cnx->set_enabled(device, true); break; default: U_LOG_W("Unknown input name requested"); @@ -462,6 +462,26 @@ std::pair wivrn_controller::get_hand_tracking(xrt_i } } +void wivrn_controller::set_derived_pose(const from_headset::derived_pose & derived) +{ + auto list = [this](device_id id) -> pose_list * { + for (auto item: {&grip, &aim, &palm}) + { + if (item->device == id) + return item; + } + return nullptr; + }; + auto source = list(derived.source); + auto target = list(derived.target); + if (source and target) + { + target->set_derived(source, xrt_cast(derived.relation)); + if (source != target) + cnx->set_enabled(derived.target, false); + } +} + void wivrn_controller::update_tracking(const from_headset::tracking & tracking, const clock_offset & offset) { if (not aim.update_tracking(tracking, offset)) diff --git a/server/driver/wivrn_controller.h b/server/driver/wivrn_controller.h index 3d9c8980..a98c7db5 100644 --- a/server/driver/wivrn_controller.h +++ b/server/driver/wivrn_controller.h @@ -62,6 +62,7 @@ public: void set_inputs(const from_headset::inputs &, const clock_offset &); + void set_derived_pose(const from_headset::derived_pose &); void update_tracking(const from_headset::tracking &, const clock_offset &); void update_hand_tracking(const from_headset::hand_tracking &, const clock_offset &); diff --git a/server/driver/wivrn_session.cpp b/server/driver/wivrn_session.cpp index 6f090ed3..ee9235bf 100644 --- a/server/driver/wivrn_session.cpp +++ b/server/driver/wivrn_session.cpp @@ -326,6 +326,12 @@ void wivrn_session::operator()(const from_headset::tracking & tracking) fb_face2_tracker->update_tracking(tracking, offset); } +void wivrn_session::operator()(from_headset::derived_pose && derived) +{ + left_hand.set_derived_pose(derived); + right_hand.set_derived_pose(derived); +} + void wivrn_session::operator()(from_headset::hand_tracking && hand_tracking) { auto offset = offset_est.get_offset(); diff --git a/server/driver/wivrn_session.h b/server/driver/wivrn_session.h index 0d0293df..bee5869d 100644 --- a/server/driver/wivrn_session.h +++ b/server/driver/wivrn_session.h @@ -153,6 +153,7 @@ public: void operator()(from_headset::handshake &&) {} void operator()(from_headset::trackings &&); void operator()(const from_headset::tracking &); + void operator()(from_headset::derived_pose &&); void operator()(from_headset::hand_tracking &&); void operator()(from_headset::inputs &&); void operator()(from_headset::timesync_response &&);