Create xdevs from body skeleton

This commit is contained in:
Sapphire
2026-07-10 13:22:14 -05:00
parent 10cefe65b9
commit 727445520e
5 changed files with 108 additions and 43 deletions
+66 -20
View File
@@ -20,6 +20,7 @@
#include "wivrn_body_tracker.h"
#include "driver/pose_list.h"
#include "driver/wivrn_generic_tracker.h"
#include "util/u_logging.h"
#include "utils/method.h"
#include "utils/overloaded.h"
@@ -175,23 +176,29 @@ void body_joints_list::update_tracking(const wivrn::from_headset::meta_body & tr
.is_active = not std::holds_alternative<std::monostate>(tracking.joints),
},
};
if (s.base_body_joint_set_meta.is_active)
{
std::visit(utils::overloaded{
[](std::monostate) {
__builtin_unreachable();
},
[&](auto & joints) {
s.body_joint_set_fb.joint_locations[XRT_BODY_JOINT_ROOT_FB].relation = to_relation(joints.root);
for (size_t joint = XRT_BODY_JOINT_HIPS_FB; joint < std::size(joints.joints) + 1; joint++)
std::visit(utils::overloaded{
[&](std::monostate) {
for (auto & [_, t]: parent.virtual_trackers)
{
t.update_tracking(tracking.production_timestamp, tracking.timestamp, {}, offset);
}
},
[&](auto & joints) {
s.body_joint_set_fb.joint_locations[XRT_BODY_JOINT_ROOT_FB].relation = to_relation(joints.root);
for (size_t joint = XRT_BODY_JOINT_HIPS_FB; joint < std::size(joints.joints) + 1; joint++)
{
const auto pose = to_relation(joints.root, joints.joints[joint - 1]);
static_assert(offsetof(xrt_full_body_joint_set_meta, joint_locations) == offsetof(xrt_body_joint_set_fb, joint_locations));
static_assert(sizeof(xrt_full_body_joint_set_meta::joint_locations[0]) == sizeof(xrt_body_joint_set_fb::joint_locations[0]));
s.full_body_joint_set_meta.joint_locations[joint].relation = pose;
if (auto it = parent.virtual_trackers.find(joint); it != parent.virtual_trackers.cend())
{
static_assert(offsetof(xrt_full_body_joint_set_meta, joint_locations) == offsetof(xrt_body_joint_set_fb, joint_locations));
static_assert(sizeof(xrt_full_body_joint_set_meta::joint_locations[0]) == sizeof(xrt_body_joint_set_fb::joint_locations[0]));
s.full_body_joint_set_meta.joint_locations[joint].relation = to_relation(joints.root, joints.joints[joint - 1]);
it->second.update_tracking(tracking.production_timestamp, tracking.timestamp, pose, offset);
}
}},
tracking.joints);
}
}
}},
tracking.joints);
add_sample(tracking.production_timestamp, tracking.timestamp, s, offset);
}
@@ -208,13 +215,19 @@ void body_joints_list::update_tracking(const wivrn::from_headset::bd_body & trac
};
for (size_t joint = XRT_BODY_JOINT_PELVIS_BD; joint < XRT_BODY_JOINT_COUNT_BD; joint++)
{
s.body_joint_set_bd.joint_locations[joint].relation = to_relation(tracking.joints[joint]);
const auto pose = to_relation(tracking.joints[joint]);
s.body_joint_set_bd.joint_locations[joint].relation = pose;
if (auto it = parent.virtual_trackers.find(joint); it != parent.virtual_trackers.cend())
{
it->second.update_tracking(tracking.production_timestamp, tracking.timestamp, pose, offset);
}
}
add_sample(tracking.production_timestamp, tracking.timestamp, s, offset);
}
wivrn_body_tracker::wivrn_body_tracker(xrt_device * hmd, wivrn::wivrn_session & cnx) :
wivrn_body_tracker::wivrn_body_tracker(xrt_device * hmd, wivrn::wivrn_session & cnx, std::function<void(xrt_device &)> device_add_callback) :
xrt_device{
.name = XRT_DEVICE_FB_BODY_TRACKING, // FIXME: replace with something more appropriate, when there is one
.device_type = XRT_DEVICE_TYPE_BODY_TRACKER,
@@ -227,9 +240,11 @@ wivrn_body_tracker::wivrn_body_tracker(xrt_device * hmd, wivrn::wivrn_session &
.get_body_joints = method_pointer<&wivrn_body_tracker::get_body_joints>,
.destroy = [](xrt_device *) {},
},
joints_list(cnx.get_info().body_tracking),
joints_list(cnx.get_info().body_tracking, *this),
cnx(cnx)
{
// joint id, pretty name
std::map<uint32_t, std::string> virtual_joints{};
switch (cnx.get_info().body_tracking)
{
case from_headset::body_type::meta:
@@ -243,6 +258,15 @@ wivrn_body_tracker::wivrn_body_tracker(xrt_device * hmd, wivrn::wivrn_session &
.active = true,
.name = XRT_INPUT_META_FULL_BODY_TRACKING,
});
virtual_joints.emplace(XRT_FULL_BODY_JOINT_CHEST_META, "Chest");
virtual_joints.emplace(XRT_FULL_BODY_JOINT_LEFT_ARM_LOWER_META, "Left Elbow");
virtual_joints.emplace(XRT_FULL_BODY_JOINT_RIGHT_ARM_LOWER_META, "Right Elbow");
virtual_joints.emplace(XRT_FULL_BODY_JOINT_HIPS_META, "Hip");
virtual_joints.emplace(XRT_FULL_BODY_JOINT_LEFT_LOWER_LEG_META, "Left Knee");
virtual_joints.emplace(XRT_FULL_BODY_JOINT_RIGHT_LOWER_LEG_META, "Right Knee");
virtual_joints.emplace(XRT_FULL_BODY_JOINT_LEFT_FOOT_TRANSVERSE_META, "Left Foot");
virtual_joints.emplace(XRT_FULL_BODY_JOINT_RIGHT_FOOT_TRANSVERSE_META, "Right Foot");
break;
case from_headset::body_type::fb:
strcpy(str, "WiVRn FB body tracker");
@@ -251,21 +275,43 @@ wivrn_body_tracker::wivrn_body_tracker(xrt_device * hmd, wivrn::wivrn_session &
.active = true,
.name = XRT_INPUT_FB_BODY_TRACKING,
});
virtual_joints.emplace(XRT_BODY_JOINT_CHEST_FB, "Chest");
virtual_joints.emplace(XRT_BODY_JOINT_LEFT_ARM_LOWER_FB, "Left Elbow");
virtual_joints.emplace(XRT_BODY_JOINT_RIGHT_ARM_LOWER_FB, "Right Elbow");
virtual_joints.emplace(XRT_BODY_JOINT_HIPS_FB, "Hip");
break;
case from_headset::body_type::bd:
strcpy(str, "WiVRn BD body Tracker");
strcpy(str, "WiVRn BD body tracker");
strcpy(serial, "WiVRn BD body tracker");
inputs_array.push_back({
.active = true,
.name = XRT_INPUT_BD_BODY_TRACKING,
});
virtual_joints.emplace(XRT_BODY_JOINT_SPINE3_BD, "Chest");
virtual_joints.emplace(XRT_BODY_JOINT_LEFT_ELBOW_BD, "Left Elbow");
virtual_joints.emplace(XRT_BODY_JOINT_RIGHT_ELBOW_BD, "Right Elbow");
virtual_joints.emplace(XRT_BODY_JOINT_PELVIS_BD, "Hip");
virtual_joints.emplace(XRT_BODY_JOINT_LEFT_KNEE_BD, "Left Knee");
virtual_joints.emplace(XRT_BODY_JOINT_RIGHT_KNEE_BD, "Right Knee");
virtual_joints.emplace(XRT_BODY_JOINT_LEFT_FOOT_BD, "Left Foot");
virtual_joints.emplace(XRT_BODY_JOINT_RIGHT_FOOT_BD, "Right Foot");
break;
default:
assert(false);
__builtin_unreachable();
}
inputs = inputs_array.data();
input_count = inputs_array.size();
for (auto & [joint, name]: virtual_joints)
{
auto [it, success] = virtual_trackers.try_emplace(joint, name, hmd, cnx);
assert(success);
device_add_callback(it->second);
}
}
xrt_result_t wivrn_body_tracker::update_inputs()
@@ -319,7 +365,7 @@ void wivrn_body_tracker::update_skeleton(const from_headset::meta_body_skeleton
{
assert(std::ranges::contains(inputs_array, XRT_INPUT_FB_BODY_TRACKING, &xrt_input::name));
auto s = this->meta_skeleton.lock();
auto s = meta_skeleton.lock();
std::visit(utils::overloaded{
[&s](auto & skeleton) {
for (size_t i = 0; i < skeleton.joints.size(); i++)
+11 -2
View File
@@ -20,6 +20,7 @@
#pragma once
#include "driver/wivrn_generic_tracker.h"
#include "utils/thread_safe.h"
#include "xrt/xrt_defines.h"
#include "xrt/xrt_device.h"
@@ -29,18 +30,21 @@
#include "wivrn_packets.h"
#include <atomic>
#include <map>
namespace wivrn
{
class wivrn_session;
class wivrn_body_tracker;
struct clock_offset;
class body_joints_list : public history<body_joints_list, xrt_body_joint_set>
{
wivrn::from_headset::body_type type;
wivrn_body_tracker & parent;
public:
body_joints_list(wivrn::from_headset::body_type type) : type(type) {}
body_joints_list(wivrn::from_headset::body_type type, wivrn_body_tracker & parent) : type(type), parent(parent) {}
xrt_body_joint_set interpolate(const xrt_body_joint_set & a, const xrt_body_joint_set & b, float t);
xrt_body_joint_set extrapolate(const xrt_body_joint_set & a, const xrt_body_joint_set & b, int64_t ta, int64_t tb, int64_t t);
@@ -50,6 +54,8 @@ public:
class wivrn_body_tracker : public xrt_device
{
friend body_joints_list;
body_joints_list joints_list;
std::vector<xrt_input> inputs_array;
@@ -58,9 +64,12 @@ class wivrn_body_tracker : public xrt_device
thread_safe<xrt_body_skeleton> meta_skeleton;
std::atomic_uint32_t meta_skeleton_generation;
protected:
std::map<uint32_t, wivrn_generic_tracker> virtual_trackers;
public:
using base_t = xrt_device;
wivrn_body_tracker(xrt_device * hmd, wivrn_session & cnx);
wivrn_body_tracker(xrt_device * hmd, wivrn_session & cnx, std::function<void(xrt_device &)> device_add_callback);
xrt_result_t update_inputs();
xrt_result_t get_body_skeleton(xrt_input_name body_tracking_type, xrt_body_skeleton * out_value);
+13 -14
View File
@@ -22,7 +22,6 @@
#include "util/u_logging.h"
#include "utils/method.h"
#include "wivrn_session.h"
#include "xrt_cast.h"
#include "math/m_api.h"
#include "math/m_eigen_interop.hpp"
@@ -70,18 +69,12 @@ xrt_space_relation tracker_pose_list::extrapolate(const xrt_space_relation & a,
return res;
}
void tracker_pose_list::update_tracking(XrTime produced_timestamp, XrTime timestamp, const from_headset::htc_body::pose & pose, const clock_offset & offset)
void tracker_pose_list::update_tracking(XrTime produced_timestamp, XrTime timestamp, const xrt_space_relation & pose, const clock_offset & offset)
{
xrt_space_relation rel{
.relation_flags = from_pose_flags(pose.flags),
.pose = xrt_cast(pose.pose),
.linear_velocity = xrt_cast(pose.linear_velocity),
.angular_velocity = xrt_cast(pose.angular_velocity),
};
add_sample(produced_timestamp, timestamp, rel, offset);
add_sample(produced_timestamp, timestamp, pose, offset);
}
wivrn_generic_tracker::wivrn_generic_tracker(int index, xrt_device * hmd, wivrn_session & cnx) :
wivrn_generic_tracker::wivrn_generic_tracker(std::string name, xrt_device * hmd, wivrn_session & cnx) :
xrt_device{
.name = XRT_DEVICE_VIVE_TRACKER,
.device_type = XRT_DEVICE_TYPE_GENERIC_TRACKER,
@@ -97,10 +90,16 @@ wivrn_generic_tracker::wivrn_generic_tracker(int index, xrt_device * hmd, wivrn_
},
cnx(cnx)
{
auto unique_name = std::format("WiVRn Generic Tracker #{}", index + 1);
auto unique_name = std::format("WiVRn generic tracker ({})", name);
strlcpy(str, unique_name.c_str(), std::size(str));
auto unique_serial = std::format("wivrn-{}", index + 1);
strlcpy(serial, unique_serial.c_str(), std::size(serial));
{
std::string serial{};
serial.reserve(name.size());
std::ranges::transform(name, std::back_inserter(serial), [](auto c) { return tolower(c); });
std::ranges::replace(serial, ' ', '-');
auto unique_serial = std::format("wivrn-{}", serial);
strlcpy(this->serial, unique_serial.c_str(), std::size(this->serial));
}
pose_input.name = XRT_INPUT_GENERIC_TRACKER_POSE;
pose_input.active = true;
@@ -130,7 +129,7 @@ xrt_result_t wivrn_generic_tracker::get_tracked_pose(xrt_input_name name, int64_
return XRT_ERROR_INPUT_UNSUPPORTED;
}
void wivrn_generic_tracker::update_tracking(XrTime produced_timestamp, XrTime timestamp, const from_headset::htc_body::pose & pose, const clock_offset & offset)
void wivrn_generic_tracker::update_tracking(XrTime produced_timestamp, XrTime timestamp, const xrt_space_relation & pose, const clock_offset & offset)
{
poses.update_tracking(produced_timestamp, timestamp, pose, offset);
}
+3 -4
View File
@@ -21,7 +21,6 @@
#include "xrt/xrt_device.h"
#include "history.h"
#include "wivrn_packets.h"
namespace wivrn
{
@@ -36,7 +35,7 @@ public:
tracker_pose_list() = default;
void update_tracking(XrTime produced_timestamp, XrTime timestamp, const wivrn::from_headset::htc_body::pose & pose, const clock_offset & offset);
void update_tracking(XrTime produced_timestamp, XrTime timestamp, const xrt_space_relation & pose, const clock_offset & offset);
};
class wivrn_generic_tracker : public xrt_device
@@ -48,11 +47,11 @@ class wivrn_generic_tracker : public xrt_device
public:
using base_t = xrt_device;
wivrn_generic_tracker(int index, xrt_device * hmd, wivrn_session & cnx);
wivrn_generic_tracker(std::string name, xrt_device * hmd, wivrn_session & cnx);
xrt_result_t update_inputs();
xrt_result_t get_tracked_pose(xrt_input_name name, int64_t at_timestamp_ns, xrt_space_relation * out_relation);
void update_tracking(XrTime produced_timestamp, XrTime timestamp, const wivrn::from_headset::htc_body::pose & pose, const clock_offset & offset);
void update_tracking(XrTime produced_timestamp, XrTime timestamp, const xrt_space_relation & pose, const clock_offset & offset);
};
} // namespace wivrn
+15 -3
View File
@@ -24,6 +24,7 @@
#include "application.h"
#include "configuration.h"
#include "driver/app_pacer.h"
#include "driver/xrt_cast.h"
#include "server/ipc_server.h"
#include "utils/load_icon.h"
#include "utils/method.h"
@@ -198,7 +199,9 @@ wivrn::wivrn_session::wivrn_session(std::unique_ptr<wivrn_connection> connection
auto body = get_info().body_tracking;
if (body != from_headset::body_type::none && body != from_headset::body_type::htc)
{
static_xdevs[static_xdev_count++] = static_roles.body = &body_tracker.emplace(&hmd, *this);
static_xdevs[static_xdev_count++] = static_roles.body = &body_tracker.emplace(&hmd, *this, [this](xrt_device & xdev) {
static_xdevs[static_xdev_count++] = &xdev;
});
}
if (body == from_headset::body_type::htc)
{
@@ -224,7 +227,7 @@ wivrn::wivrn_session::wivrn_session(std::unique_ptr<wivrn_connection> connection
for (int i = 0; i < num_generic_trackers; ++i)
{
static_xdevs[static_xdev_count++] = &generic_trackers.emplace_back(i, &hmd, *this);
static_xdevs[static_xdev_count++] = &generic_trackers.emplace_back(std::to_string(i), &hmd, *this);
}
}
}
@@ -707,7 +710,16 @@ void wivrn_session::operator()(from_headset::htc_body && body_tracking)
auto offset = offset_est.get_offset();
for (auto [tracker, pose]: std::ranges::zip_view(generic_trackers, body_tracking.poses))
tracker.update_tracking(body_tracking.production_timestamp, body_tracking.timestamp, pose, offset);
tracker.update_tracking(
body_tracking.production_timestamp,
body_tracking.timestamp,
xrt_space_relation{
.relation_flags = from_pose_flags(pose.flags),
.pose = xrt_cast(pose.pose),
.linear_velocity = xrt_cast(pose.linear_velocity),
.angular_velocity = xrt_cast(pose.angular_velocity),
},
offset);
}
void wivrn_session::operator()(from_headset::inputs && inputs)
{