From 25c2449a5eff69ba60b74a1323c2c634d0fc7837 Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Fri, 16 Jan 2026 23:50:31 +0100 Subject: [PATCH] Use polynomial interpolation for poses and views --- server/driver/polynomial_interpolator.h | 177 ++++++++++++++++++++++++ server/driver/pose_list.cpp | 149 +++++++++++++++++--- server/driver/pose_list.h | 17 ++- server/driver/view_list.cpp | 59 ++++---- server/driver/view_list.h | 12 +- server/driver/wivrn_controller.cpp | 24 ++-- server/driver/wivrn_hmd.cpp | 8 +- 7 files changed, 371 insertions(+), 75 deletions(-) create mode 100644 server/driver/polynomial_interpolator.h diff --git a/server/driver/polynomial_interpolator.h b/server/driver/polynomial_interpolator.h new file mode 100644 index 00000000..0a669257 --- /dev/null +++ b/server/driver/polynomial_interpolator.h @@ -0,0 +1,177 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2025 Guillaume Meunier + * Copyright (C) 2025 Patrick Nicolas + * + * 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 3 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, see . + */ + +#pragma once + +// xlib sucks +#ifdef Success +#undef Success +#endif + +#include +#include +#include +#include +#include +#include +#include + +#include "openxr/openxr.h" + +template +class polynomial_interpolator +{ +public: + using value_type = Eigen::Vector; + + struct sample + { + XrTime production_timestamp = std::numeric_limits::lowest(); + XrTime timestamp; + std::optional y; + std::optional dy; + }; + + XrDuration window = 30'000'000; // time span of samples to use for extrapolation + float time_constant = 0.001; // duration (s) to convert velocities into positions + +private: + std::array data; + +public: + polynomial_interpolator() = default; + polynomial_interpolator(XrDuration window, float time_constant) : window(window), time_constant(time_constant) {} + + void reset() + { + data.fill({}); + } + + void add_sample(const sample & s) + { + sample * oldest = nullptr; + for (auto & i: data) + { + // Avoid samples too close to each other + if (std::abs(i.timestamp - s.timestamp) < 2'000'000) + { + if (i.production_timestamp < s.production_timestamp) + i = s; + return; + } + if ((not oldest) or oldest->production_timestamp > i.production_timestamp) + oldest = &i; + } + *oldest = s; + if constexpr (quaternion) + { + if (s.y) + { + sample * closest = nullptr; + for (auto & i: data) + { + if (&i == oldest or not i.y) + continue; + if (not closest) + closest = &i; + else if (std::abs(closest->timestamp - s.timestamp) > std::abs(i.timestamp - s.timestamp)) + closest = &i; + } + if (closest and closest->y->dot(*s.y) < 0) + *oldest->y *= -1; + } + } + } + + sample get_at(XrTime timestamp) + { + Eigen::Matrix A; + Eigen::Matrix b; + + XrTime production_timestamp = std::numeric_limits::lowest(); + + int row = 0; + for (const auto && [i, sample]: std::ranges::enumerate_view(data)) + { + if (not sample.y) + continue; + + int abs_Δt = std::abs(sample.timestamp - timestamp); + + float weight = 1. / (1. + std::pow(abs_Δt / float(window), 3.)); + + production_timestamp = std::max(production_timestamp, sample.production_timestamp); + + float Δt = (sample.timestamp - timestamp) * 1.e-9; + float Δtⁱ = 1; + for (int i = 0; i <= polynomial_order; ++i) + { + A(row, i) = weight * Δtⁱ; + Δtⁱ *= Δt; + } + b.template block<1, N>(row, 0) = *sample.y * weight; + row++; + + if (sample.dy.has_value()) + { + A(row, 0) = 0; + + Δtⁱ = 1; + for (int i = 1; i <= polynomial_order; ++i) + { + A(row, i) = weight * time_constant * i * Δtⁱ; + Δtⁱ *= Δt; + } + b.template block<1, N>(row, 0) = *sample.dy * weight * time_constant; + row++; + } + } + + // Not enough data to extrapolate + if (row < 2) + { + auto closest = std::ranges::min_element(data, std::less{}, [&](const auto & sample) { + if (not sample.y) + return std::numeric_limits::max(); + return std::abs(sample.timestamp - timestamp); + }); + if (closest->y and std::abs(closest->production_timestamp - timestamp) < 1'000'000'000) + return *closest; + return {}; + } + + auto Aprime = A.block(0, 0, row, polynomial_order + 1); + auto bprime = b.block(0, 0, row, N); + + Eigen::Matrix sol; + + if (row <= polynomial_order) + // Underdetermined case + sol = Eigen::ColPivHouseholderQR{Aprime}.solve(bprime).transpose(); + else + sol = (Aprime.transpose() * Aprime).ldlt().solve(Aprime.transpose() * bprime).transpose(); + + return sample{ + .production_timestamp = production_timestamp, + .timestamp = timestamp, + .y = sol.template block(0, 0), + .dy = sol.template block(0, 1), + }; + } +}; diff --git a/server/driver/pose_list.cpp b/server/driver/pose_list.cpp index a74cfb57..9258e473 100644 --- a/server/driver/pose_list.cpp +++ b/server/driver/pose_list.cpp @@ -18,12 +18,17 @@ */ #include "pose_list.h" +#include "driver/clock_offset.h" +#include "driver/polynomial_interpolator.h" #include "math/m_api.h" #include "math/m_eigen_interop.hpp" #include "math/m_space.h" #include "math/m_vec3.h" +#include "os/os_time.h" #include "xrt/xrt_defines.h" -#include "xrt_cast.h" +#include +#include +#include using namespace xrt::auxiliary::math; @@ -96,7 +101,7 @@ bool pose_list::update_tracking(const from_headset::tracking & tracking, const c if (pose.device != device) continue; - return add_sample(tracking.production_timestamp, tracking.timestamp, convert_pose(pose), offset); + return add_sample(tracking.production_timestamp, tracking.timestamp, pose, offset); } return true; } @@ -121,7 +126,7 @@ void pose_list::set_derived(pose_list * source, xrt_pose offset, bool force) } } -std::tuple pose_list::get_pose_at(XrTime at_timestamp_ns) +std::tuple pose_list::get_pose_at(XrTime at_timestamp_ns) { if (auto source = this->source.load()) { @@ -133,24 +138,132 @@ std::tuple pose_list::g return std::tuple_cat(get_at(at_timestamp_ns), std::make_tuple(device)); } -static xrt_space_relation_flags convert_flags(uint8_t flags) +void pose_list::reset() { - static_assert(int(from_headset::tracking::position_valid) == XRT_SPACE_RELATION_POSITION_VALID_BIT); - static_assert(int(from_headset::tracking::orientation_valid) == XRT_SPACE_RELATION_ORIENTATION_VALID_BIT); - static_assert(int(from_headset::tracking::linear_velocity_valid) == XRT_SPACE_RELATION_LINEAR_VELOCITY_VALID_BIT); - static_assert(int(from_headset::tracking::angular_velocity_valid) == XRT_SPACE_RELATION_ANGULAR_VELOCITY_VALID_BIT); - static_assert(int(from_headset::tracking::orientation_tracked) == XRT_SPACE_RELATION_ORIENTATION_TRACKED_BIT); - static_assert(int(from_headset::tracking::position_tracked) == XRT_SPACE_RELATION_POSITION_TRACKED_BIT); - return xrt_space_relation_flags(flags); + std::lock_guard lock(mutex); + positions.reset(); + orientations.reset(); + last_request = os_monotonic_get_ns(); } -xrt_space_relation pose_list::convert_pose(const from_headset::tracking::pose & pose) +bool pose_list::add_sample(XrTime production_timestamp, XrTime timestamp, const from_headset::tracking::pose & pose, const clock_offset & offset) { - return xrt_space_relation{ - .relation_flags = convert_flags(pose.flags), - .pose = xrt_cast(pose.pose), - .linear_velocity = xrt_cast(pose.linear_velocity), - .angular_velocity = xrt_cast(pose.angular_velocity), - }; + production_timestamp = offset.from_headset(production_timestamp); + timestamp = offset.from_headset(timestamp); + + // TODO keep the tracked flag + + polynomial_interpolator<3>::sample position{production_timestamp, timestamp}; + if (pose.flags & from_headset::tracking::position_valid) + position.y.emplace( + pose.pose.position.x, + pose.pose.position.y, + pose.pose.position.z); + if (pose.flags & from_headset::tracking::linear_velocity_valid) + position.dy.emplace( + pose.linear_velocity.x, + pose.linear_velocity.y, + pose.linear_velocity.z); + + polynomial_interpolator<4, true>::sample orientation{production_timestamp, timestamp}; + if (pose.flags & from_headset::tracking::orientation_valid) + { + orientation.y.emplace( + pose.pose.orientation.w, + pose.pose.orientation.x, + pose.pose.orientation.y, + pose.pose.orientation.z); + + if (pose.flags & from_headset::tracking::angular_velocity_valid) + { + Eigen::Quaternionf q{ + pose.pose.orientation.w, + pose.pose.orientation.x, + pose.pose.orientation.y, + pose.pose.orientation.z}; + + Eigen::Quaternionf ω{ + 0, + pose.angular_velocity.x, + pose.angular_velocity.y, + pose.angular_velocity.z}; + + orientation.dy.emplace(0.5 * (ω * q).coeffs()); + } + } + + std::lock_guard lock(mutex); + positions.add_sample(position); + orientations.add_sample(orientation); + return timestamp - last_request < 1'000'000'000; } + +std::pair pose_list::get_at(XrTime at_timestamp_ns) +{ + std::lock_guard lock(mutex); + + last_request = os_monotonic_get_ns(); + + xrt_space_relation ret{}; + auto flags = [&](int f) { + ret.relation_flags = xrt_space_relation_flags(int(ret.relation_flags) | f); + }; + + auto position = positions.get_at(at_timestamp_ns); + auto orientation = orientations.get_at(at_timestamp_ns); + + if (position.y) + { + flags(XRT_SPACE_RELATION_POSITION_VALID_BIT | XRT_SPACE_RELATION_POSITION_TRACKED_BIT); + ret.pose.position = { + position.y->x(), + position.y->y(), + position.y->z()}; + } + + if (position.dy) + { + flags(XRT_SPACE_RELATION_LINEAR_VELOCITY_VALID_BIT); + ret.linear_velocity = { + position.dy->x(), + position.dy->y(), + position.dy->z()}; + } + + if (orientation.y) + { + flags(XRT_SPACE_RELATION_ORIENTATION_VALID_BIT | XRT_SPACE_RELATION_ORIENTATION_TRACKED_BIT); + + ret.pose.orientation = { + .x = (*orientation.y)[1], + .y = (*orientation.y)[2], + .z = (*orientation.y)[3], + .w = (*orientation.y)[0]}; + + if (orientation.dy) + { + Eigen::Quaternionf q{ + (*orientation.y)[0], + (*orientation.y)[1], + (*orientation.y)[2], + (*orientation.y)[3]}; + Eigen::Quaternionf dq{ + (*orientation.dy)[0], + (*orientation.dy)[1], + (*orientation.dy)[2], + (*orientation.dy)[3]}; + Eigen::Quaternionf half_ω = dq * q; + + flags(XRT_SPACE_RELATION_ANGULAR_VELOCITY_VALID_BIT); + ret.angular_velocity = { + 2 * half_ω.x(), + 2 * half_ω.y(), + 2 * half_ω.z(), + }; + } + } + + return {position.production_timestamp, ret}; +} + } // namespace wivrn diff --git a/server/driver/pose_list.h b/server/driver/pose_list.h index b6530ffc..0c20fdf0 100644 --- a/server/driver/pose_list.h +++ b/server/driver/pose_list.h @@ -19,7 +19,7 @@ #pragma once -#include "history.h" +#include "polynomial_interpolator.h" #include "wivrn_packets.h" #include "xrt/xrt_defines.h" @@ -29,11 +29,16 @@ namespace wivrn { struct clock_offset; -class pose_list : public history +class pose_list { std::atomic source = nullptr; xrt_pose offset; std::atomic_bool derive_forced = false; + std::mutex mutex; + XrTime last_request; + + polynomial_interpolator<3> positions; + polynomial_interpolator<4, true> orientations; public: const wivrn::device_id device; @@ -47,8 +52,12 @@ public: 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); + std::tuple get_pose_at(XrTime at_timestamp_ns); - static xrt_space_relation convert_pose(const wivrn::from_headset::tracking::pose &); + void reset(); + std::pair get_at(XrTime at_timestamp_ns); + +private: + bool add_sample(XrTime production_timestamp, XrTime timestamp, const from_headset::tracking::pose & pose, const clock_offset & offset); }; } // namespace wivrn diff --git a/server/driver/view_list.cpp b/server/driver/view_list.cpp index bc1d2d56..a39ee6cc 100644 --- a/server/driver/view_list.cpp +++ b/server/driver/view_list.cpp @@ -18,46 +18,41 @@ */ #include "view_list.h" -#include "pose_list.h" #include "xrt_cast.h" namespace wivrn { -tracked_views view_list::interpolate(const tracked_views & a, const tracked_views & b, float t) -{ - tracked_views result = a; - result.relation = pose_list::interpolate(a.relation, b.relation, t); - return result; -} - -tracked_views view_list::extrapolate(const tracked_views & a, const tracked_views & b, int64_t ta, int64_t tb, int64_t t) -{ - tracked_views result = t < ta ? a : b; - result.relation = pose_list::extrapolate(a.relation, b.relation, ta, tb, t); - return result; -} - bool view_list::update_tracking(const from_headset::tracking & tracking, const clock_offset & offset) { - for (const auto & pose: tracking.device_poses) + if (not std::ranges::contains(tracking.device_poses, device_id::HEAD, &from_headset::tracking::pose::device)) + return true; + + std::lock_guard lock(mutex); + flags = tracking.view_flags; + + for (size_t eye = 0; eye < 2; ++eye) { - if (pose.device != device_id::HEAD) - continue; - - tracked_views view{}; - - view.relation = pose_list::convert_pose(pose); - view.flags = tracking.view_flags; - - for (size_t eye = 0; eye < 2; ++eye) - { - view.poses[eye] = xrt_cast(tracking.views[eye].pose); - view.fovs[eye] = xrt_cast(tracking.views[eye].fov); - } - - return add_sample(tracking.production_timestamp, tracking.timestamp, view, offset); + poses[eye] = xrt_cast(tracking.views[eye].pose); + fovs[eye] = xrt_cast(tracking.views[eye].fov); } - return true; + + return head_poses.update_tracking(tracking, offset); +} + +std::pair view_list::get_at(XrTime at_timestamp_ns) +{ + std::lock_guard lock(mutex); + auto [t, pose] = head_poses.get_at(at_timestamp_ns); + return { + t, + tracked_views{ + .flags = flags, + .relation = pose, + .poses = poses, + .fovs = fovs, + }, + + }; } } // namespace wivrn diff --git a/server/driver/view_list.h b/server/driver/view_list.h index a3d07bfc..09da09f2 100644 --- a/server/driver/view_list.h +++ b/server/driver/view_list.h @@ -35,12 +35,16 @@ struct tracked_views std::array fovs; }; -class view_list : public history +class view_list { -public: - static tracked_views interpolate(const tracked_views & a, const tracked_views & b, float t); - static tracked_views extrapolate(const tracked_views & a, const tracked_views & b, int64_t ta, int64_t tb, int64_t t); + pose_list head_poses{device_id::HEAD}; + std::mutex mutex; + XrViewStateFlags flags; + std::array poses; + std::array fovs; +public: bool update_tracking(const from_headset::tracking & tracking, const clock_offset & offset); + std::pair get_at(XrTime at_timestamp_ns); }; } // namespace wivrn diff --git a/server/driver/wivrn_controller.cpp b/server/driver/wivrn_controller.cpp index 747801b5..274af62d 100644 --- a/server/driver/wivrn_controller.cpp +++ b/server/driver/wivrn_controller.cpp @@ -793,33 +793,33 @@ void wivrn_controller::set_inputs(const from_headset::inputs & inputs, const clo xrt_result_t wivrn_controller::get_tracked_pose(xrt_input_name name, int64_t at_timestamp_ns, xrt_space_relation * res) { - std::chrono::nanoseconds extrapolation_time; + XrTime production_timestamp; device_id device; switch (name) { case XRT_INPUT_TOUCH_AIM_POSE: case XRT_INPUT_HAND_AIM_POSE: - std::tie(extrapolation_time, *res, device) = aim.get_pose_at(at_timestamp_ns); + std::tie(production_timestamp, *res, device) = aim.get_pose_at(at_timestamp_ns); break; case XRT_INPUT_TOUCH_GRIP_POSE: case XRT_INPUT_HAND_GRIP_POSE: - std::tie(extrapolation_time, *res, device) = grip.get_pose_at(at_timestamp_ns); + std::tie(production_timestamp, *res, device) = grip.get_pose_at(at_timestamp_ns); break; case XRT_INPUT_GENERIC_PALM_POSE: - std::tie(extrapolation_time, *res, device) = palm.get_pose_at(at_timestamp_ns); + std::tie(production_timestamp, *res, device) = palm.get_pose_at(at_timestamp_ns); break; case XRT_INPUT_HAND_PINCH_POSE: - std::tie(extrapolation_time, *res, device) = pinch_ext.get_pose_at(at_timestamp_ns); + std::tie(production_timestamp, *res, device) = pinch_ext.get_pose_at(at_timestamp_ns); break; case XRT_INPUT_HAND_POKE_POSE: - std::tie(extrapolation_time, *res, device) = poke_ext.get_pose_at(at_timestamp_ns); + std::tie(production_timestamp, *res, device) = poke_ext.get_pose_at(at_timestamp_ns); break; default: U_LOG_XDEV_UNSUPPORTED_INPUT(this, u_log_get_global_level(), name); return XRT_ERROR_INPUT_UNSUPPORTED; } cnx->set_enabled(device, true); - cnx->add_predict_offset(extrapolation_time); + cnx->add_predict_offset(std::chrono::nanoseconds(at_timestamp_ns - production_timestamp)); if (auto out = tracking_dump()) { auto device = [&] { @@ -837,7 +837,7 @@ xrt_result_t wivrn_controller::get_tracked_pose(xrt_input_name name, int64_t at_ *out->lock() << magic_enum::enum_name(device) << ',' << os_monotonic_get_ns() << ',' << at_timestamp_ns << ',' - << extrapolation_time.count() << ',' + << production_timestamp << ',' << "g," << *res << std::endl; ; @@ -907,17 +907,15 @@ void wivrn_controller::update_tracking(const from_headset::tracking & tracking, *locked << magic_enum::enum_name(pose.device) << ',' << now << ',' << offset.from_headset(tracking.timestamp) << ',' - << tracking.timestamp - tracking.production_timestamp << ',' - << "r," - << pose_list::convert_pose(pose) << std::endl; + << "0,r," + << /*pose_list::convert_pose(pose) <<*/ std::endl; } } } void wivrn_controller::update_hand_tracking(const from_headset::hand_tracking & tracking, const clock_offset & offset) { - if (not joints.update_tracking(tracking, offset)) - cnx->set_enabled(joints.hand_id == 0 ? to_headset::tracking_control::id::left_hand : to_headset::tracking_control::id::right_hand, false); + joints.update_tracking(tracking, offset); } xrt_result_t wivrn_controller::set_output(xrt_output_name name, const xrt_output_value * value) diff --git a/server/driver/wivrn_hmd.cpp b/server/driver/wivrn_hmd.cpp index 5cbfb4a5..7d6d3013 100644 --- a/server/driver/wivrn_hmd.cpp +++ b/server/driver/wivrn_hmd.cpp @@ -129,9 +129,9 @@ xrt_result_t wivrn_hmd::get_tracked_pose(xrt_input_name name, int64_t at_timesta return XRT_ERROR_INPUT_UNSUPPORTED; } - auto [extrapolation_time, view] = views.get_at(at_timestamp_ns); + auto [production_timestamp, view] = views.get_at(at_timestamp_ns); *res = view.relation; - cnx->add_predict_offset(extrapolation_time); + cnx->add_predict_offset(std::chrono::nanoseconds(at_timestamp_ns - production_timestamp)); return XRT_SUCCESS; } @@ -163,8 +163,8 @@ xrt_result_t wivrn_hmd::get_view_poses(const xrt_vec3 * default_eye_relation, xrt_fov * out_fovs, xrt_pose * out_poses) { - auto [extrapolation_time, view] = views.get_at(at_timestamp_ns); - cnx->add_predict_offset(extrapolation_time); + auto [production_timestamp, view] = views.get_at(at_timestamp_ns); + cnx->add_predict_offset(std::chrono::nanoseconds(at_timestamp_ns - production_timestamp)); int flags = view.relation.relation_flags;