diff --git a/server/driver/hand_joints_list.cpp b/server/driver/hand_joints_list.cpp index 632a03a6..0e730788 100644 --- a/server/driver/hand_joints_list.cpp +++ b/server/driver/hand_joints_list.cpp @@ -18,6 +18,7 @@ */ #include "hand_joints_list.h" +#include "math/m_space.h" #include "pose_list.h" #include "xrt_cast.h" @@ -34,16 +35,6 @@ namespace .radius = a.radius * (1-t) + b.radius * t }; } - - xrt_hand_joint_value extrapolate(const xrt_hand_joint_value & a, const xrt_hand_joint_value & b, uint64_t ta, uint64_t tb, uint64_t t) - { - float λ = std::clamp((t - ta) / float(tb - ta), 0.f, 1.f); - - return { - .relation = pose_list::extrapolate(a.relation, b.relation, ta, tb, t), - .radius = a.radius * (1-λ) + b.radius * λ - }; - } } xrt_hand_joint_set hand_joints_list::interpolate(const xrt_hand_joint_set & a, const xrt_hand_joint_set & b, float t) @@ -58,58 +49,64 @@ xrt_hand_joint_set hand_joints_list::interpolate(const xrt_hand_joint_set & a, c xrt_hand_joint_set hand_joints_list::extrapolate(const xrt_hand_joint_set & a, const xrt_hand_joint_set & b, uint64_t ta, uint64_t tb, uint64_t t) { - xrt_hand_joint_set j = a; - for(int i = 0; i < XRT_HAND_JOINT_COUNT; i++) - { - j.values.hand_joint_set_default[i] = ::extrapolate(a.values.hand_joint_set_default[i], b.values.hand_joint_set_default[i], ta, tb, t); - } + xrt_hand_joint_set j = t <= ta ? a : b; + // Only extrapolate the hand pose, individual joints are too noisy + j.hand_pose = pose_list::extrapolate(a.hand_pose, b.hand_pose, ta, tb, t); return j; } +static xrt_space_relation_flags cast_flags(uint8_t in_flags) +{ + std::underlying_type_t flags = 0; + if (in_flags & from_headset::hand_tracking::position_valid) + flags |= XRT_SPACE_RELATION_POSITION_VALID_BIT; + + if (in_flags & from_headset::hand_tracking::orientation_valid) + flags |= XRT_SPACE_RELATION_ORIENTATION_VALID_BIT; + + if (in_flags & from_headset::hand_tracking::linear_velocity_valid) + flags |= XRT_SPACE_RELATION_LINEAR_VELOCITY_VALID_BIT; + + if (in_flags & from_headset::hand_tracking::angular_velocity_valid) + flags |= XRT_SPACE_RELATION_ANGULAR_VELOCITY_VALID_BIT; + + if (in_flags & from_headset::hand_tracking::position_tracked) + flags |= XRT_SPACE_RELATION_POSITION_TRACKED_BIT; + + if (in_flags & from_headset::hand_tracking::orientation_tracked) + flags |= XRT_SPACE_RELATION_ORIENTATION_TRACKED_BIT; + return xrt_space_relation_flags(flags); +} + +static xrt_space_relation to_relation(const from_headset::hand_tracking::pose& pose) +{ + return { + .relation_flags = cast_flags(pose.flags), + .pose = xrt_cast(pose.pose), + .linear_velocity = xrt_cast(pose.linear_velocity), + .angular_velocity = xrt_cast(pose.angular_velocity), + }; +} + static xrt_hand_joint_set convert_joints(const std::array& input_joints) { xrt_hand_joint_set output_joints; output_joints.is_active = true; - output_joints.hand_pose = xrt_space_relation{ - .relation_flags = XRT_SPACE_RELATION_BITMASK_ALL, - .pose = { - .orientation = {0, 0, 0, 1}, - .position = {0, 0, 0} - }, - .linear_velocity = {0, 0, 0}, - .angular_velocity = {0, 0, 0}, - }; // TODO + output_joints.hand_pose = to_relation(input_joints[XRT_HAND_JOINT_WRIST]); + + xrt_relation_chain rel_chain{}; + xrt_space_relation * joint_rel = m_relation_chain_reserve(&rel_chain); + m_relation_chain_push_inverted_relation(&rel_chain, &output_joints.hand_pose); for(int i = 0; i < XR_HAND_JOINT_COUNT_EXT; i++) { xrt_hand_joint_value& res = output_joints.values.hand_joint_set_default[i]; res.radius = input_joints[i].radius; - res.relation.pose = xrt_cast(input_joints[i].pose); - res.relation.angular_velocity = xrt_cast(input_joints[i].angular_velocity); - res.relation.linear_velocity = xrt_cast(input_joints[i].linear_velocity); + *joint_rel = to_relation(input_joints[i]); - int flags = 0; - if (input_joints[i].flags & from_headset::hand_tracking::position_valid) - flags |= XRT_SPACE_RELATION_POSITION_VALID_BIT; - - if (input_joints[i].flags & from_headset::hand_tracking::orientation_valid) - flags |= XRT_SPACE_RELATION_ORIENTATION_VALID_BIT; - - if (input_joints[i].flags & from_headset::hand_tracking::linear_velocity_valid) - flags |= XRT_SPACE_RELATION_LINEAR_VELOCITY_VALID_BIT; - - if (input_joints[i].flags & from_headset::hand_tracking::angular_velocity_valid) - flags |= XRT_SPACE_RELATION_ANGULAR_VELOCITY_VALID_BIT; - - if (input_joints[i].flags & from_headset::hand_tracking::position_tracked) - flags |= XRT_SPACE_RELATION_POSITION_TRACKED_BIT; - - if (input_joints[i].flags & from_headset::hand_tracking::orientation_tracked) - flags |= XRT_SPACE_RELATION_ORIENTATION_TRACKED_BIT; - - res.relation.relation_flags = (xrt_space_relation_flags)flags; + m_relation_chain_resolve(&rel_chain, &res.relation); } return output_joints; } diff --git a/server/driver/history.h b/server/driver/history.h index 875fc9de..8a902d32 100644 --- a/server/driver/history.h +++ b/server/driver/history.h @@ -26,7 +26,7 @@ #include #include "clock_offset.h" -template +template class history { struct TimedData : public Data @@ -75,7 +75,10 @@ public: if (data.front().at_timestamp_ns > at_timestamp_ns) { - return Derived::extrapolate(data[0], data[1], data[0].at_timestamp_ns, data[1].at_timestamp_ns, at_timestamp_ns); + if (extrapolate) + return Derived::extrapolate(data[0], data[1], data[0].at_timestamp_ns, data[1].at_timestamp_ns, at_timestamp_ns); + else + return data.front(); } for (size_t i = 1; i < data.size(); ++i) @@ -88,9 +91,14 @@ public: } } - const auto & d0 = data[data.size() - 2]; - const auto & d1 = data.back(); - - return Derived::extrapolate(d0, d1, d0.at_timestamp_ns, d1.at_timestamp_ns, at_timestamp_ns); + if (extrapolate) + { + const auto & d0 = data[data.size() - 2]; + const auto & d1 = data.back(); + return Derived::extrapolate(d0, d1, d0.at_timestamp_ns, d1.at_timestamp_ns, at_timestamp_ns); + } + else { + return data.back(); + } } }; diff --git a/server/driver/pose_list.cpp b/server/driver/pose_list.cpp index 4c240b1e..aab0707f 100644 --- a/server/driver/pose_list.cpp +++ b/server/driver/pose_list.cpp @@ -18,12 +18,14 @@ */ #include "pose_list.h" +#include "math/m_eigen_interop.hpp" #include "math/m_space.h" #include "math/m_vec3.h" #include "xrt/xrt_defines.h" #include "xrt_cast.h" using namespace xrt::drivers::wivrn; +using namespace xrt::auxiliary::math; xrt_space_relation pose_list::interpolate(const xrt_space_relation & a, const xrt_space_relation & b, float t) { @@ -35,17 +37,26 @@ xrt_space_relation pose_list::interpolate(const xrt_space_relation & a, const xr xrt_space_relation pose_list::extrapolate(const xrt_space_relation & a, const xrt_space_relation & b, uint64_t ta, uint64_t tb, uint64_t t) { - if (t < ta) - return a; + float h = (tb - ta) / 1.e9; - xrt_space_relation res = b; + xrt_space_relation res = t < ta ? a : b; + + xrt_vec3 lin_vel = res.relation_flags & XRT_SPACE_RELATION_LINEAR_VELOCITY_VALID_BIT ? res.linear_velocity : (b.pose.position - a.pose.position ) / h; float dt = (t - tb) / 1.e9; - // if (dt > 0.2) - // dt = 0.2; // saturate dt to 200ms + float dt2_over_2 = dt * dt / 2; res.pose.position = b.pose.position + b.linear_velocity * dt; + if (res.relation_flags & XRT_SPACE_RELATION_ANGULAR_VELOCITY_VALID_BIT) + { + xrt_vec3 dtheta = b.angular_velocity * dt; + xrt_quat dq; + math_quat_exp(&dtheta, &dq); + + map_quat(res.pose.orientation) = map_quat(res.pose.orientation) * map_quat(dq); + } + return res; } diff --git a/server/driver/view_list.cpp b/server/driver/view_list.cpp index a0b5d0bb..4373f9b8 100644 --- a/server/driver/view_list.cpp +++ b/server/driver/view_list.cpp @@ -30,7 +30,7 @@ tracked_views view_list::interpolate(const tracked_views & a, const tracked_view tracked_views view_list::extrapolate(const tracked_views & a, const tracked_views & b, uint64_t ta, uint64_t tb, uint64_t t) { - tracked_views result = a; + tracked_views result = t < ta ? a : b; result.relation = pose_list::extrapolate(a.relation, b.relation, ta, tb, t); return result; } diff --git a/server/driver/view_list.h b/server/driver/view_list.h index d033d8b6..f98a90d2 100644 --- a/server/driver/view_list.h +++ b/server/driver/view_list.h @@ -31,7 +31,7 @@ struct tracked_views }; -class view_list : public history +class view_list : public history { public: static tracked_views interpolate(const tracked_views & a, const tracked_views & b, float t);