Add hand tracking

This commit is contained in:
Guillaume Meunier
2024-02-25 16:09:32 +01:00
parent 91f6dc08f0
commit a2271d486a
23 changed files with 615 additions and 93 deletions
+15 -1
View File
@@ -725,6 +725,7 @@ void application::initialize()
std::vector<std::string> opt_extensions; std::vector<std::string> opt_extensions;
opt_extensions.push_back(XR_KHR_COMPOSITION_LAYER_COLOR_SCALE_BIAS_EXTENSION_NAME); opt_extensions.push_back(XR_KHR_COMPOSITION_LAYER_COLOR_SCALE_BIAS_EXTENSION_NAME);
opt_extensions.push_back(XR_FB_DISPLAY_REFRESH_RATE_EXTENSION_NAME); opt_extensions.push_back(XR_FB_DISPLAY_REFRESH_RATE_EXTENSION_NAME);
opt_extensions.push_back(XR_EXT_HAND_TRACKING_EXTENSION_NAME);
for(const auto& i: interaction_profiles) for(const auto& i: interaction_profiles)
{ {
@@ -770,7 +771,6 @@ void application::initialize()
spdlog::info(" Orientation tracking: {}", (bool)properties.trackingProperties.orientationTracking); spdlog::info(" Orientation tracking: {}", (bool)properties.trackingProperties.orientationTracking);
spdlog::info(" Position tracking: {}", (bool)properties.trackingProperties.positionTracking); spdlog::info(" Position tracking: {}", (bool)properties.trackingProperties.positionTracking);
// Log view configurations and blend modes // Log view configurations and blend modes
log_views(); log_views();
@@ -789,6 +789,20 @@ void application::initialize()
world_space = xr_session.create_reference_space(XR_REFERENCE_SPACE_TYPE_STAGE); world_space = xr_session.create_reference_space(XR_REFERENCE_SPACE_TYPE_STAGE);
// world_space = xr_session.create_reference_space(XR_REFERENCE_SPACE_TYPE_LOCAL); // world_space = xr_session.create_reference_space(XR_REFERENCE_SPACE_TYPE_LOCAL);
if (utils::contains(xr_extensions, XR_EXT_HAND_TRACKING_EXTENSION_NAME))
{
XrSystemHandTrackingPropertiesEXT hand_tracking_properties = xr_system_id.hand_tracking_properties();
spdlog::info("Hand tracking support: {}", (bool)hand_tracking_properties.supportsHandTracking);
hand_tracking_supported = hand_tracking_properties.supportsHandTracking;
if (hand_tracking_supported)
{
left_hand = xr_session.create_hand_tracker(XR_HAND_LEFT_EXT);
right_hand = xr_session.create_hand_tracker(XR_HAND_RIGHT_EXT);
}
}
vk::CommandPoolCreateInfo cmdpool_create_info; vk::CommandPoolCreateInfo cmdpool_create_info;
cmdpool_create_info.queueFamilyIndex = vk_queue_family_index; cmdpool_create_info.queueFamilyIndex = vk_queue_family_index;
cmdpool_create_info.flags = vk::CommandPoolCreateFlagBits::eResetCommandBuffer; cmdpool_create_info.flags = vk::CommandPoolCreateFlagBits::eResetCommandBuffer;
+20
View File
@@ -19,6 +19,7 @@
#pragma once #pragma once
#include "xr/hand_tracker.h"
#ifdef __ANDROID__ #ifdef __ANDROID__
#include <android_native_app_glue.h> #include <android_native_app_glue.h>
#endif #endif
@@ -118,6 +119,10 @@ class application : public singleton<application>
xr::space right_grip_space; xr::space right_grip_space;
xr::space right_aim_space; xr::space right_aim_space;
bool hand_tracking_supported = false;
xr::hand_tracker left_hand;
xr::hand_tracker right_hand;
bool session_running = false; bool session_running = false;
bool session_focused = false; bool session_focused = false;
bool session_visible = false; bool session_visible = false;
@@ -379,4 +384,19 @@ public:
{ {
return instance().last_scene_gpu_time; return instance().last_scene_gpu_time;
} }
static bool get_hand_tracking_supported()
{
return instance().hand_tracking_supported;
}
static xr::hand_tracker& get_left_hand()
{
return instance().left_hand;
}
static xr::hand_tracker& get_right_hand()
{
return instance().right_hand;
}
}; };
+3
View File
@@ -35,6 +35,7 @@
#include <vulkan/vulkan_core.h> #include <vulkan/vulkan_core.h>
#include "audio/audio.h" #include "audio/audio.h"
#include "hardware.h" #include "hardware.h"
#include "utils/contains.h"
using namespace xrt::drivers::wivrn; using namespace xrt::drivers::wivrn;
@@ -108,6 +109,8 @@ std::shared_ptr<scenes::stream> scenes::stream::create(std::unique_ptr<wivrn_ses
if (info.available_refresh_rates.empty()) if (info.available_refresh_rates.empty())
spdlog::warn("Unable to detect refresh rates"); spdlog::warn("Unable to detect refresh rates");
info.hand_tracking = application::get_hand_tracking_supported();
audio::get_audio_description(info); audio::get_audio_description(info);
self->network_session->send_control(info); self->network_session->send_control(info);
+47
View File
@@ -63,6 +63,42 @@ static from_headset::tracking::pose locate_space(device_id device, XrSpace space
return res; return res;
} }
static std::array<from_headset::hand_tracking::pose, XR_HAND_JOINT_COUNT_EXT> locate_hands(xr::hand_tracker& hand, XrSpace space, XrTime time)
{
std::array<xr::hand_tracker::joint, XR_HAND_JOINT_COUNT_EXT> joints = hand.locate(space, time);
std::array<from_headset::hand_tracking::pose, XR_HAND_JOINT_COUNT_EXT> poses;
for(int i = 0; i < XR_HAND_JOINT_COUNT_EXT; i++)
{
poses[i] = {
.pose = joints[i].first.pose,
.linear_velocity = joints[i].second.linearVelocity,
.angular_velocity = joints[i].second.angularVelocity,
.radius = joints[i].first.radius,
};
if (joints[i].first.locationFlags & XR_SPACE_LOCATION_ORIENTATION_VALID_BIT)
poses[i].flags |= from_headset::hand_tracking::orientation_valid;
if (joints[i].first.locationFlags & XR_SPACE_LOCATION_POSITION_VALID_BIT)
poses[i].flags |= from_headset::hand_tracking::position_valid;
if (joints[i].second.velocityFlags & XR_SPACE_VELOCITY_LINEAR_VALID_BIT)
poses[i].flags |= from_headset::hand_tracking::linear_velocity_valid;
if (joints[i].second.velocityFlags & XR_SPACE_VELOCITY_ANGULAR_VALID_BIT)
poses[i].flags |= from_headset::hand_tracking::angular_velocity_valid;
if (joints[i].first.locationFlags & XR_SPACE_LOCATION_ORIENTATION_TRACKED_BIT)
poses[i].flags |= from_headset::hand_tracking::orientation_tracked;
if (joints[i].first.locationFlags & XR_SPACE_LOCATION_POSITION_TRACKED_BIT)
poses[i].flags |= from_headset::hand_tracking::position_tracked;
}
return poses;
}
void scenes::stream::tracking() void scenes::stream::tracking()
{ {
std::vector<std::pair<device_id, XrSpace>> spaces = { std::vector<std::pair<device_id, XrSpace>> spaces = {
@@ -91,7 +127,10 @@ void scenes::stream::tracking()
for (XrTime Δt = 0; Δt < extrapolation_horizon; Δt += tracking_period) for (XrTime Δt = 0; Δt < extrapolation_horizon; Δt += tracking_period)
{ {
from_headset::tracking packet{}; from_headset::tracking packet{};
from_headset::hand_tracking hands{};
packet.timestamp = t0 + Δt; packet.timestamp = t0 + Δt;
hands.timestamp = t0 + Δt;
try try
{ {
@@ -112,6 +151,14 @@ void scenes::stream::tracking()
} }
network_session->send_stream(packet); network_session->send_stream(packet);
if (application::get_hand_tracking_supported())
{
hands.left = locate_hands(application::get_left_hand(), world_space, hands.timestamp);
hands.right = locate_hands(application::get_right_hand(), world_space, hands.timestamp);
network_session->send_stream(hands);
}
} }
catch (const std::system_error & e) catch (const std::system_error & e)
{ {
+61
View File
@@ -0,0 +1,61 @@
/*
* WiVRn VR streaming
* Copyright (C) 2024 Guillaume Meunier <guillaume.meunier@centraliens.net>
* Copyright (C) 2024 Patrick Nicolas <patricknicolas@laposte.net>
*
* 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 <https://www.gnu.org/licenses/>.
*/
#include "hand_tracker.h"
std::array<xr::hand_tracker::joint, XR_HAND_JOINT_COUNT_EXT> xr::hand_tracker::locate(XrSpace space, XrTime time)
{
if (!xrLocateHandJointsEXT)
xrLocateHandJointsEXT = inst->get_proc<PFN_xrLocateHandJointsEXT>("xrLocateHandJointsEXT");
XrHandJointsLocateInfoEXT info{
.type = XR_TYPE_HAND_JOINTS_LOCATE_INFO_EXT,
.next = nullptr,
.baseSpace = space,
.time = time,
};
std::array<XrHandJointLocationEXT, XR_HAND_JOINT_COUNT_EXT> joints_pos;
std::array<XrHandJointVelocityEXT, XR_HAND_JOINT_COUNT_EXT> joints_vel;
XrHandJointVelocitiesEXT velocities{
.type = XR_TYPE_HAND_JOINT_VELOCITIES_EXT,
.next = nullptr,
.jointCount = joints_vel.size(),
.jointVelocities = joints_vel.data(),
};
XrHandJointLocationsEXT locations{
.type = XR_TYPE_HAND_JOINT_LOCATIONS_EXT,
.next = &velocities,
.jointCount = joints_pos.size(),
.jointLocations = joints_pos.data(),
};
CHECK_XR(xrLocateHandJointsEXT(id, &info, &locations));
std::array<xr::hand_tracker::joint, XR_HAND_JOINT_COUNT_EXT> joints;
for(int i = 0; i < XR_HAND_JOINT_COUNT_EXT; i++)
{
joints[i] = { joints_pos[i], joints_vel[i] };
}
return joints;
}
+58
View File
@@ -0,0 +1,58 @@
/*
* WiVRn VR streaming
* Copyright (C) 2024 Guillaume Meunier <guillaume.meunier@centraliens.net>
* Copyright (C) 2024 Patrick Nicolas <patricknicolas@laposte.net>
*
* 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 <https://www.gnu.org/licenses/>.
*/
#pragma once
#include "utils/handle.h"
#include <openxr/openxr.h>
#include <array>
#include <utility>
#include "instance.h"
namespace xr
{
class hand_tracker : public utils::handle<XrHandTrackerEXT>
{
instance * inst;
PFN_xrLocateHandJointsEXT xrLocateHandJointsEXT{};
public:
hand_tracker() = default;
hand_tracker(instance * i, XrHandTrackerEXT h)
{
inst = i;
id = h;
}
hand_tracker(hand_tracker &&) = default;
hand_tracker & operator=(hand_tracker &&) = default;
~hand_tracker()
{
auto xrDestroyHandTrackerEXT = inst->get_proc<PFN_xrDestroyHandTrackerEXT>("xrDestroyHandTrackerEXT");
if (id != XR_NULL_HANDLE && xrDestroyHandTrackerEXT)
xrDestroyHandTrackerEXT(id);
}
using joint = std::pair<XrHandJointLocationEXT, XrHandJointVelocityEXT>;
std::array<joint, XR_HAND_JOINT_COUNT_EXT> locate(XrSpace space, XrTime time);
};
} // namespace xr
+20
View File
@@ -76,6 +76,26 @@ xr::space xr::session::create_action_space(XrAction action, const XrPosef & pose
return s; return s;
} }
xr::hand_tracker xr::session::create_hand_tracker(XrHandEXT hand, XrHandJointSetEXT hand_joint_set)
{
XrHandTrackerCreateInfoEXT create_info
{
.type = XR_TYPE_HAND_TRACKER_CREATE_INFO_EXT,
.next = nullptr,
.hand = hand,
.handJointSet = hand_joint_set,
};
XrHandTrackerEXT ht;
auto xrCreateHandTrackerEXT = inst->get_proc<PFN_xrCreateHandTrackerEXT>("xrCreateHandTrackerEXT");
assert(xrCreateHandTrackerEXT);
CHECK_XR(xrCreateHandTrackerEXT(id, &create_info, &ht));
return {inst, ht};
}
std::vector<vk::Format> xr::session::get_swapchain_formats() const std::vector<vk::Format> xr::session::get_swapchain_formats() const
{ {
std::vector<vk::Format> formats; std::vector<vk::Format> formats;
+2
View File
@@ -27,6 +27,7 @@
#include <openxr/openxr.h> #include <openxr/openxr.h>
#include "space.h" #include "space.h"
#include "hand_tracker.h"
namespace xr namespace xr
{ {
@@ -48,6 +49,7 @@ public:
std::vector<XrReferenceSpaceType> get_reference_spaces() const; std::vector<XrReferenceSpaceType> get_reference_spaces() const;
space create_reference_space(XrReferenceSpaceType ref, const XrPosef & pose = {{0, 0, 0, 1}, {0, 0, 0}}); space create_reference_space(XrReferenceSpaceType ref, const XrPosef & pose = {{0, 0, 0, 1}, {0, 0, 0}});
space create_action_space(XrAction action, const XrPosef & pose = {{0, 0, 0, 1}, {0, 0, 0}}); space create_action_space(XrAction action, const XrPosef & pose = {{0, 0, 0, 1}, {0, 0, 0}});
hand_tracker create_hand_tracker(XrHandEXT hand, XrHandJointSetEXT hand_joint_set = XR_HAND_JOINT_SET_DEFAULT_EXT);
std::vector<vk::Format> get_swapchain_formats() const; std::vector<vk::Format> get_swapchain_formats() const;
+19
View File
@@ -63,6 +63,25 @@ XrSystemProperties xr::system::properties() const
return prop; return prop;
} }
XrSystemHandTrackingPropertiesEXT xr::system::hand_tracking_properties() const
{
if (!id)
throw std::invalid_argument("this");
XrSystemHandTrackingPropertiesEXT hand_tracking_prop
{
.type = XR_TYPE_SYSTEM_HAND_TRACKING_PROPERTIES_EXT,
};
XrSystemProperties prop{
.type = XR_TYPE_SYSTEM_PROPERTIES,
.next = &hand_tracking_prop,
};
CHECK_XR(xrGetSystemProperties(*inst, id, &prop));
return hand_tracking_prop;
}
vk::raii::PhysicalDevice xr::system::physical_device(vk::raii::Instance& vulkan) const vk::raii::PhysicalDevice xr::system::physical_device(vk::raii::Instance& vulkan) const
{ {
auto xrGetVulkanGraphicsDevice2KHR = auto xrGetVulkanGraphicsDevice2KHR =
+2
View File
@@ -54,6 +54,8 @@ public:
} }
XrSystemProperties properties() const; XrSystemProperties properties() const;
XrSystemHandTrackingPropertiesEXT hand_tracking_properties() const;
XrGraphicsRequirementsVulkan2KHR graphics_requirements() const; XrGraphicsRequirementsVulkan2KHR graphics_requirements() const;
vk::raii::PhysicalDevice physical_device(vk::raii::Instance& vulkan) const; vk::raii::PhysicalDevice physical_device(vk::raii::Instance& vulkan) const;
vk::raii::Device create_device(vk::raii::PhysicalDevice& pdev, vk::DeviceCreateInfo & create_info) const; vk::raii::Device create_device(vk::raii::PhysicalDevice& pdev, vk::DeviceCreateInfo & create_info) const;
+28 -1
View File
@@ -105,6 +105,7 @@ struct headset_info_packet
std::optional<audio_description> speaker; std::optional<audio_description> speaker;
std::optional<audio_description> microphone; std::optional<audio_description> microphone;
std::array<XrFovf, 2> fov; std::array<XrFovf, 2> fov;
bool hand_tracking;
}; };
struct handshake struct handshake
@@ -146,6 +147,32 @@ struct tracking
std::vector<pose> device_poses; std::vector<pose> device_poses;
}; };
struct hand_tracking
{
enum flags : uint8_t
{
orientation_valid = 1 << 0,
position_valid = 1 << 1,
linear_velocity_valid = 1 << 2,
angular_velocity_valid = 1 << 3,
orientation_tracked = 1 << 4,
position_tracked = 1 << 5
};
struct pose
{
XrPosef pose;
XrVector3f linear_velocity;
XrVector3f angular_velocity;
float radius;
uint8_t flags;
};
uint64_t timestamp;
std::array<pose, XR_HAND_JOINT_COUNT_EXT> left;
std::array<pose, XR_HAND_JOINT_COUNT_EXT> right;
};
struct inputs struct inputs
{ {
struct input_value struct input_value
@@ -183,7 +210,7 @@ struct feedback
uint8_t received_data_packets; uint8_t received_data_packets;
}; };
using packets = std::variant<headset_info_packet, feedback, audio_data, handshake, tracking, inputs, timesync_response>; using packets = std::variant<headset_info_packet, feedback, audio_data, handshake, tracking, hand_tracking, inputs, timesync_response>;
} // namespace from_headset } // namespace from_headset
namespace to_headset namespace to_headset
+1
View File
@@ -78,6 +78,7 @@ add_executable(wivrn-server
driver/wivrn_controller.cpp driver/wivrn_controller.cpp
driver/pose_list.cpp driver/pose_list.cpp
driver/view_list.cpp driver/view_list.cpp
driver/hand_joints_list.cpp
driver/wivrn_session.cpp driver/wivrn_session.cpp
driver/wivrn_connection.cpp driver/wivrn_connection.cpp
driver/xrt_cast.cpp driver/xrt_cast.cpp
+123
View File
@@ -0,0 +1,123 @@
/*
* WiVRn VR streaming
* Copyright (C) 2024 Guillaume Meunier <guillaume.meunier@centraliens.net>
* Copyright (C) 2024 Patrick Nicolas <patricknicolas@laposte.net>
*
* 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 <https://www.gnu.org/licenses/>.
*/
#include "hand_joints_list.h"
#include "pose_list.h"
#include "xrt_cast.h"
using namespace xrt::drivers::wivrn;
static_assert(XRT_HAND_JOINT_COUNT == XR_HAND_JOINT_COUNT_EXT);
namespace
{
xrt_hand_joint_value interpolate(const xrt_hand_joint_value & a, const xrt_hand_joint_value & b, float t)
{
return {
.relation = pose_list::interpolate(a.relation, b.relation, t),
.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)
{
xrt_hand_joint_set j = a;
for(int i = 0; i < XRT_HAND_JOINT_COUNT; i++)
{
j.values.hand_joint_set_default[i] = ::interpolate(a.values.hand_joint_set_default[i], b.values.hand_joint_set_default[i], t);
}
return j;
}
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);
}
return j;
}
static xrt_hand_joint_set convert_joints(const std::array<from_headset::hand_tracking::pose, XR_HAND_JOINT_COUNT_EXT>& 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
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);
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;
}
return output_joints;
}
void hand_joints_list::update_tracking(const from_headset::hand_tracking & tracking, const clock_offset & offset)
{
if (hand_id == 0)
add_sample(tracking.timestamp, convert_joints(tracking.left), offset);
else
add_sample(tracking.timestamp, convert_joints(tracking.right), offset);
}
+37
View File
@@ -0,0 +1,37 @@
/*
* WiVRn VR streaming
* Copyright (C) 2024 Guillaume Meunier <guillaume.meunier@centraliens.net>
* Copyright (C) 2024 Patrick Nicolas <patricknicolas@laposte.net>
*
* 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 <https://www.gnu.org/licenses/>.
*/
#pragma once
#include "history.h"
#include "xrt/xrt_defines.h"
class hand_joints_list : public history<hand_joints_list, xrt_hand_joint_set>
{
int hand_id;
public:
static xrt_hand_joint_set interpolate(const xrt_hand_joint_set & a, const xrt_hand_joint_set & b, float t);
static xrt_hand_joint_set extrapolate(const xrt_hand_joint_set & a, const xrt_hand_joint_set & b, uint64_t ta, uint64_t tb, uint64_t t);
hand_joints_list(int hand_id) : hand_id(hand_id) {}
void update_tracking(const xrt::drivers::wivrn::from_headset::hand_tracking & tracking, const clock_offset & offset);
};
+96
View File
@@ -0,0 +1,96 @@
/*
* WiVRn VR streaming
* Copyright (C) 2022-2024 Guillaume Meunier <guillaume.meunier@centraliens.net>
* Copyright (C) 2022-2024 Patrick Nicolas <patricknicolas@laposte.net>
*
* 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 <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <cstddef>
#include <cstdint>
#include <mutex>
#include <vector>
#include <algorithm>
#include "clock_offset.h"
template <typename Derived, typename Data, size_t MaxSamples = 10>
class history
{
struct TimedData : public Data
{
TimedData(const Data & d, uint64_t t) :
Data(d), at_timestamp_ns(t) {}
uint64_t at_timestamp_ns;
};
std::mutex mutex;
std::vector<TimedData> data;
protected:
void add_sample(uint64_t timestamp, const Data & sample, const clock_offset & offset)
{
std::lock_guard lock(mutex);
uint64_t t = offset.from_headset(timestamp);
auto it = std::lower_bound(data.begin(), data.end(), t, [](TimedData & sample, uint64_t t) { return sample.at_timestamp_ns < t; });
if (it == data.end())
data.emplace_back(sample, t);
else if (it->at_timestamp_ns == t)
*it = TimedData(sample, t);
else
data.emplace(it, sample, t);
if (data.size() > MaxSamples)
data.erase(data.begin());
}
public:
Data get_at(uint64_t at_timestamp_ns)
{
std::lock_guard lock(mutex);
if (data.empty())
{
return {};
}
if (data.size() == 1)
{
return data[0];
}
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);
}
for (size_t i = 1; i < data.size(); ++i)
{
if (data[i].at_timestamp_ns > at_timestamp_ns)
{
float t = float(data[i].at_timestamp_ns - at_timestamp_ns) /
(data[i].at_timestamp_ns - data[i - 1].at_timestamp_ns);
return Derived::interpolate(data[i - 1], data[i], t);
}
}
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);
}
};
+2 -2
View File
@@ -27,7 +27,7 @@
using namespace xrt::auxiliary::math; using namespace xrt::auxiliary::math;
using namespace xrt::drivers::wivrn; using namespace xrt::drivers::wivrn;
xrt_space_relation interpolate(const xrt_space_relation & a, const xrt_space_relation & b, float t) xrt_space_relation pose_list::interpolate(const xrt_space_relation & a, const xrt_space_relation & b, float t)
{ {
xrt_space_relation result; xrt_space_relation result;
xrt_space_relation_flags flags = xrt_space_relation_flags(a.relation_flags & b.relation_flags); xrt_space_relation_flags flags = xrt_space_relation_flags(a.relation_flags & b.relation_flags);
@@ -35,7 +35,7 @@ xrt_space_relation interpolate(const xrt_space_relation & a, const xrt_space_rel
return result; return result;
} }
xrt_space_relation extrapolate(const xrt_space_relation & a, const xrt_space_relation & b, uint64_t ta, uint64_t tb, uint64_t t) 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) if (t < ta)
return a; return a;
+5 -76
View File
@@ -21,89 +21,18 @@
#include "wivrn_packets.h" #include "wivrn_packets.h"
#include "clock_offset.h" #include "clock_offset.h"
#include "history.h"
#include "xrt/xrt_defines.h" #include "xrt/xrt_defines.h"
#include <algorithm>
#include <mutex>
#include <vector>
template <typename Data, size_t MaxSamples = 10>
class history
{
struct TimedData : public Data
{
TimedData(const Data & d, uint64_t t) :
Data(d), at_timestamp_ns(t) {}
uint64_t at_timestamp_ns;
};
std::mutex mutex; class pose_list : public history<pose_list, xrt_space_relation>
std::vector<TimedData> data;
protected:
void add_sample(uint64_t timestamp, const Data & sample, const clock_offset & offset)
{
std::lock_guard lock(mutex);
uint64_t t = offset.from_headset(timestamp);
auto it = std::lower_bound(data.begin(), data.end(), t, [](TimedData & sample, uint64_t t) { return sample.at_timestamp_ns < t; });
if (it == data.end())
data.emplace_back(sample, t);
else if (it->at_timestamp_ns == t)
*it = TimedData(sample, t);
else
data.emplace(it, sample, t);
if (data.size() > MaxSamples)
data.erase(data.begin());
}
public:
Data get_at(uint64_t at_timestamp_ns)
{
std::lock_guard lock(mutex);
if (data.empty())
{
return {};
}
if (data.size() == 1)
{
return data[0];
}
if (data.front().at_timestamp_ns > at_timestamp_ns)
{
return extrapolate(data[0], data[1], data[0].at_timestamp_ns, data[1].at_timestamp_ns, at_timestamp_ns);
}
for (size_t i = 1; i < data.size(); ++i)
{
if (data[i].at_timestamp_ns > at_timestamp_ns)
{
float t = float(data[i].at_timestamp_ns - at_timestamp_ns) /
(data[i].at_timestamp_ns - data[i - 1].at_timestamp_ns);
return interpolate(data[i - 1], data[i], t);
}
}
const auto & d0 = data[data.size() - 2];
const auto & d1 = data.back();
return extrapolate(d0, d1, d0.at_timestamp_ns, d1.at_timestamp_ns, at_timestamp_ns);
}
};
xrt_space_relation interpolate(const xrt_space_relation & a, const xrt_space_relation & b, float t);
xrt_space_relation extrapolate(const xrt_space_relation & a, const xrt_space_relation & b, uint64_t ta, uint64_t tb, uint64_t t);
class pose_list : public history<xrt_space_relation>
{ {
xrt::drivers::wivrn::device_id device; xrt::drivers::wivrn::device_id device;
public: public:
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, uint64_t ta, uint64_t tb, uint64_t t);
pose_list(xrt::drivers::wivrn::device_id id) : pose_list(xrt::drivers::wivrn::device_id id) :
device(id) {} device(id) {}
+4 -4
View File
@@ -21,17 +21,17 @@
#include "pose_list.h" #include "pose_list.h"
#include "xrt_cast.h" #include "xrt_cast.h"
tracked_views interpolate(const tracked_views & a, const tracked_views & b, float t) tracked_views view_list::interpolate(const tracked_views & a, const tracked_views & b, float t)
{ {
tracked_views result = a; tracked_views result = a;
result.relation = interpolate(a.relation, b.relation, t); result.relation = pose_list::interpolate(a.relation, b.relation, t);
return result; return result;
} }
tracked_views extrapolate(const tracked_views & a, const tracked_views & b, uint64_t ta, uint64_t tb, uint64_t t) 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 = a;
result.relation = extrapolate(a.relation, b.relation, ta, tb, t); result.relation = pose_list::extrapolate(a.relation, b.relation, ta, tb, t);
return result; return result;
} }
+4 -4
View File
@@ -30,12 +30,12 @@ struct tracked_views
std::array<xrt_fov, 2> fovs; std::array<xrt_fov, 2> fovs;
}; };
tracked_views interpolate(const tracked_views & a, const tracked_views & b, float t);
tracked_views extrapolate(const tracked_views & a, const tracked_views & b, uint64_t ta, uint64_t tb, uint64_t t); class view_list : public history<view_list, tracked_views>
class view_list : public history<tracked_views>
{ {
public: 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, uint64_t ta, uint64_t tb, uint64_t t);
void update_tracking(const from_headset::tracking & tracking, const clock_offset & offset); void update_tracking(const from_headset::tracking & tracking, const clock_offset & offset);
}; };
+46 -3
View File
@@ -34,6 +34,8 @@ enum wivrn_controller_input_index
{ {
WIVRN_CONTROLLER_AIM_POSE, WIVRN_CONTROLLER_AIM_POSE,
WIVRN_CONTROLLER_GRIP_POSE, WIVRN_CONTROLLER_GRIP_POSE,
WIVRN_CONTROLLER_HAND_TRACKER,
WIVRN_CONTROLLER_MENU_CLICK, // /user/hand/left/input/menu/click WIVRN_CONTROLLER_MENU_CLICK, // /user/hand/left/input/menu/click
WIVRN_CONTROLLER_A_CLICK, // /user/hand/right/input/a/click WIVRN_CONTROLLER_A_CLICK, // /user/hand/right/input/a/click
WIVRN_CONTROLLER_A_TOUCH, // /user/hand/right/input/a/touch WIVRN_CONTROLLER_A_TOUCH, // /user/hand/right/input/a/touch
@@ -136,6 +138,12 @@ static void wivrn_controller_get_tracked_pose(xrt_device * xdev,
uint64_t at_timestamp_ns, uint64_t at_timestamp_ns,
xrt_space_relation * out_relation); xrt_space_relation * out_relation);
static void wivrn_controller_get_hand_tracking(xrt_device * xdev,
xrt_input_name name,
uint64_t desired_timestamp_ns,
xrt_hand_joint_set * out_value,
uint64_t *out_timestamp_ns);
static void wivrn_controller_set_output(struct xrt_device * xdev, enum xrt_output_name name, const union xrt_output_value * value); static void wivrn_controller_set_output(struct xrt_device * xdev, enum xrt_output_name name, const union xrt_output_value * value);
static void wivrn_controller_update_inputs(xrt_device * xdev); static void wivrn_controller_update_inputs(xrt_device * xdev);
@@ -143,19 +151,23 @@ static void wivrn_controller_update_inputs(xrt_device * xdev);
wivrn_controller::wivrn_controller(int hand_id, wivrn_controller::wivrn_controller(int hand_id,
xrt_device * hmd, xrt_device * hmd,
std::shared_ptr<xrt::drivers::wivrn::wivrn_session> cnx) : std::shared_ptr<xrt::drivers::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), cnx(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),
joints(hand_id),
cnx(cnx)
{ {
xrt_device * base = this; xrt_device * base = this;
base->destroy = wivrn_controller_destroy; base->destroy = wivrn_controller_destroy;
base->get_tracked_pose = wivrn_controller_get_tracked_pose; base->get_tracked_pose = wivrn_controller_get_tracked_pose;
base->get_hand_tracking = NULL; // TODO base->get_hand_tracking = wivrn_controller_get_hand_tracking;
base->set_output = wivrn_controller_set_output; base->set_output = wivrn_controller_set_output;
base->update_inputs = wivrn_controller_update_inputs; base->update_inputs = wivrn_controller_update_inputs;
base->name = XRT_DEVICE_TOUCH_CONTROLLER; base->name = XRT_DEVICE_TOUCH_CONTROLLER;
base->orientation_tracking_supported = true; base->orientation_tracking_supported = true;
base->hand_tracking_supported = false; base->hand_tracking_supported = true;
base->position_tracking_supported = true; base->position_tracking_supported = true;
base->tracking_origin = hmd->tracking_origin; base->tracking_origin = hmd->tracking_origin;
@@ -197,6 +209,9 @@ wivrn_controller::wivrn_controller(int hand_id,
SET_INPUT(THUMBSTICK_TOUCH); SET_INPUT(THUMBSTICK_TOUCH);
SET_INPUT(THUMBREST_TOUCH); SET_INPUT(THUMBREST_TOUCH);
inputs[WIVRN_CONTROLLER_HAND_TRACKER].name = hand_id == 0 ? XRT_INPUT_GENERIC_HAND_TRACKING_LEFT : XRT_INPUT_GENERIC_HAND_TRACKING_RIGHT;
inputs[WIVRN_CONTROLLER_HAND_TRACKER].active = true;
output_count = 1; output_count = 1;
outputs = &haptic_output; outputs = &haptic_output;
haptic_output.name = XRT_OUTPUT_NAME_TOUCH_HAPTIC; haptic_output.name = XRT_OUTPUT_NAME_TOUCH_HAPTIC;
@@ -308,12 +323,31 @@ xrt_space_relation wivrn_controller::get_tracked_pose(xrt_input_name name, uint6
} }
} }
std::pair<xrt_hand_joint_set, uint64_t> wivrn_controller::get_hand_tracking(xrt_input_name name, uint64_t desired_timestamp_ns)
{
switch(name)
{
case XRT_INPUT_GENERIC_HAND_TRACKING_LEFT:
case XRT_INPUT_GENERIC_HAND_TRACKING_RIGHT:
return { joints.get_at(desired_timestamp_ns), desired_timestamp_ns };
default:
U_LOG_W("Unknown input name requested");
return {};
}
}
void wivrn_controller::update_tracking(const from_headset::tracking & tracking, const clock_offset & offset) void wivrn_controller::update_tracking(const from_headset::tracking & tracking, const clock_offset & offset)
{ {
aim.update_tracking(tracking, offset); aim.update_tracking(tracking, offset);
grip.update_tracking(tracking, offset); grip.update_tracking(tracking, offset);
} }
void wivrn_controller::update_hand_tracking(const from_headset::hand_tracking & tracking, const clock_offset & offset)
{
joints.update_tracking(tracking, offset);
}
void wivrn_controller::set_output(xrt_output_name name, const xrt_output_value * value) void wivrn_controller::set_output(xrt_output_name name, const xrt_output_value * value)
{ {
auto id = device_type == XRT_DEVICE_TYPE_LEFT_HAND_CONTROLLER ? device_id::LEFT_CONTROLLER_HAPTIC auto id = device_type == XRT_DEVICE_TYPE_LEFT_HAND_CONTROLLER ? device_id::LEFT_CONTROLLER_HAPTIC
@@ -357,6 +391,15 @@ static void wivrn_controller_get_tracked_pose(xrt_device * xdev,
*out_relation = static_cast<wivrn_controller *>(xdev)->get_tracked_pose(name, at_timestamp_ns); *out_relation = static_cast<wivrn_controller *>(xdev)->get_tracked_pose(name, at_timestamp_ns);
} }
static void wivrn_controller_get_hand_tracking(xrt_device * xdev,
xrt_input_name name,
uint64_t desired_timestamp_ns,
xrt_hand_joint_set * out_value,
uint64_t *out_timestamp_ns)
{
std::tie(*out_value, *out_timestamp_ns) = static_cast<wivrn_controller *>(xdev)->get_hand_tracking(name, desired_timestamp_ns);
}
static void wivrn_controller_set_output(struct xrt_device * xdev, enum xrt_output_name name, const union xrt_output_value * value) static void wivrn_controller_set_output(struct xrt_device * xdev, enum xrt_output_name name, const union xrt_output_value * value)
{ {
static_cast<wivrn_controller *>(xdev)->set_output(name, value); static_cast<wivrn_controller *>(xdev)->set_output(name, value);
+5 -2
View File
@@ -22,6 +22,7 @@
#include "xrt/xrt_device.h" #include "xrt/xrt_device.h"
#include "pose_list.h" #include "pose_list.h"
#include "hand_joints_list.h"
#include "wivrn_session.h" #include "wivrn_session.h"
#include <memory> #include <memory>
@@ -34,6 +35,7 @@ class wivrn_controller : public xrt_device
pose_list grip; pose_list grip;
pose_list aim; pose_list aim;
hand_joints_list joints;
std::vector<xrt_input> inputs_staging; std::vector<xrt_input> inputs_staging;
std::vector<xrt_input> inputs_array; std::vector<xrt_input> inputs_array;
@@ -51,14 +53,15 @@ public:
void update_inputs(); void update_inputs();
xrt_space_relation get_tracked_pose(xrt_input_name name, uint64_t at_timestamp_ns); xrt_space_relation get_tracked_pose(xrt_input_name name, uint64_t at_timestamp_ns);
std::pair<xrt_hand_joint_set, uint64_t> get_hand_tracking(xrt_input_name name, uint64_t desired_timestamp_ns);
void set_output(xrt_output_name name, const xrt_output_value * value); void set_output(xrt_output_name name, const xrt_output_value * value);
void set_inputs(const from_headset::inputs &, const clock_offset &); void set_inputs(const from_headset::inputs &, const clock_offset &);
void update_tracking(const from_headset::tracking &, const clock_offset &); void update_tracking(const from_headset::tracking &, const clock_offset &);
void update_hand_tracking(const from_headset::hand_tracking &, const clock_offset &);
private: private:
void void set_inputs(device_id input_id, float value, uint64_t last_change_time);
set_inputs(device_id input_id, float value, uint64_t last_change_time);
}; };
+16
View File
@@ -120,9 +120,15 @@ xrt_result_t xrt::drivers::wivrn::wivrn_session::create_session(xrt::drivers::wi
if (self->hmd) if (self->hmd)
usysds->base.base.static_roles.head = devices->xdevs[n++] = self->hmd.get(); usysds->base.base.static_roles.head = devices->xdevs[n++] = self->hmd.get();
if (self->left_hand) if (self->left_hand)
{
devices->xdevs[n++] = self->left_hand.get(); devices->xdevs[n++] = self->left_hand.get();
usysds->base.base.static_roles.hand_tracking.left = self->left_hand.get();
}
if (self->right_hand) if (self->right_hand)
{
devices->xdevs[n++] = self->right_hand.get(); devices->xdevs[n++] = self->right_hand.get();
usysds->base.base.static_roles.hand_tracking.right = self->right_hand.get();
}
devices->xdev_count = n; devices->xdev_count = n;
u_system_devices_static_finalize(usysds, self->left_hand.get(), self->right_hand.get()); u_system_devices_static_finalize(usysds, self->left_hand.get(), self->right_hand.get());
@@ -179,6 +185,16 @@ void wivrn_session::operator()(from_headset::tracking && tracking)
right_hand->update_tracking(tracking, offset); right_hand->update_tracking(tracking, offset);
} }
void wivrn_session::operator()(from_headset::hand_tracking && hand_tracking)
{
auto offset = offset_est.get_offset();
if (not offset)
return;
left_hand->update_hand_tracking(hand_tracking, offset);
right_hand->update_hand_tracking(hand_tracking, offset);
}
void wivrn_session::operator()(from_headset::inputs && inputs) void wivrn_session::operator()(from_headset::inputs && inputs)
{ {
auto offset = get_offset(); auto offset = get_offset();
+1
View File
@@ -77,6 +77,7 @@ public:
void operator()(from_headset::handshake&&) {} void operator()(from_headset::handshake&&) {}
void operator()(from_headset::headset_info_packet &&); void operator()(from_headset::headset_info_packet &&);
void operator()(from_headset::tracking &&); void operator()(from_headset::tracking &&);
void operator()(from_headset::hand_tracking &&);
void operator()(from_headset::inputs &&); void operator()(from_headset::inputs &&);
void operator()(from_headset::timesync_response &&); void operator()(from_headset::timesync_response &&);
void operator()(from_headset::feedback &&); void operator()(from_headset::feedback &&);