Compress quaternions in hand tracking packets

This commit is contained in:
Guillaume Meunier committed 2026-01-16 18:01:05 +01:00
1 parent 3702cc05b8
commit 317cd6bb91
5 files changed
+138 -4

No files matched your search

+2 -1
View File
@@ -207,7 +207,8 @@ static std::optional<std::array<from_headset::hand_tracking::pose, XR_HAND_JOINT
for (int i = 0; i < XR_HAND_JOINT_COUNT_EXT; i++)
{
poses[i] = {
.pose = (*joints)[i].first.pose,
.position = (*joints)[i].first.pose.position,
.orientation = pack((*joints)[i].first.pose.orientation),
.linear_velocity = (*joints)[i].second.linearVelocity,
.angular_velocity = (*joints)[i].second.angularVelocity,
.radius = uint16_t((*joints)[i].first.radius * 10'000),
+105
View File
@@ -0,0 +1,105 @@
/*
* WiVRn VR streaming
* Copyright (C) 2026 Guillaume Meunier <[email protected]>
*
* 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 <algorithm>
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <openxr/openxr.h>
struct packed_quaternion
{
uint32_t value;
static packed_quaternion from_quaternion(const XrQuaternionf & q)
{
assert(std::abs(q.w * q.w + q.x * q.x + q.y * q.y + q.z * q.z - 1) < 0.01);
float abs_w = std::abs(q.w);
float abs_x = std::abs(q.x);
float abs_y = std::abs(q.y);
float abs_z = std::abs(q.z);
const float scale_factor = 511 * std::sqrt(2);
if (abs_x > std::max({abs_y, abs_z, abs_w}))
{
uint32_t q1 = q.y * std::copysign(scale_factor, q.x) + 512.5;
uint32_t q2 = q.z * std::copysign(scale_factor, q.x) + 512.5;
uint32_t q3 = q.w * std::copysign(scale_factor, q.x) + 512.5;
return {(0 << 30) | (q1 << 20) | (q2 << 10) | q3};
}
else if (abs_y > std::max({abs_x, abs_z, abs_w}))
{
uint32_t q1 = q.x * std::copysign(scale_factor, q.y) + 512.5;
uint32_t q2 = q.z * std::copysign(scale_factor, q.y) + 512.5;
uint32_t q3 = q.w * std::copysign(scale_factor, q.y) + 512.5;
return {(1 << 30) | (q1 << 20) | (q2 << 10) | q3};
}
else if (abs_z > std::max({abs_x, abs_y, abs_w}))
{
uint32_t q1 = q.x * std::copysign(scale_factor, q.z) + 512.5;
uint32_t q2 = q.y * std::copysign(scale_factor, q.z) + 512.5;
uint32_t q3 = q.w * std::copysign(scale_factor, q.z) + 512.5;
return {(2 << 30) | (q1 << 20) | (q2 << 10) | q3};
}
else
{
uint32_t q1 = q.x * std::copysign(scale_factor, q.w) + 512.5;
uint32_t q2 = q.y * std::copysign(scale_factor, q.w) + 512.5;
uint32_t q3 = q.z * std::copysign(scale_factor, q.w) + 512.5;
return {(3 << 30) | (q1 << 20) | (q2 << 10) | q3};
}
}
operator XrQuaternionf() const
{
const float scale_factor = 1. / (511 * std::sqrt(2));
float q1 = (int((value >> 20) & 0x3ff) - 512) * scale_factor;
float q2 = (int((value >> 10) & 0x3ff) - 512) * scale_factor;
float q3 = (int((value >> 0) & 0x3ff) - 512) * scale_factor;
float q0 = std::sqrt(1 - q1 * q1 - q2 * q2 - q3 * q3);
switch (value >> 30)
{
case 0:
return {q0, q1, q2, q3};
case 1:
return {q1, q0, q2, q3};
case 2:
return {q1, q2, q0, q3};
case 3:
return {q1, q2, q3, q0};
}
__builtin_unreachable();
}
};
inline packed_quaternion pack(const XrQuaternionf & q)
{
return packed_quaternion::from_quaternion(q);
}
+3 -1
View File
@@ -32,6 +32,7 @@
#include <vulkan/vulkan_core.h>
#include <openxr/openxr.h>
#include "packed_quaternion.h"
#include "smp.h"
#include "wivrn_serialization_types.h"
@@ -372,7 +373,8 @@ struct hand_tracking
};
struct pose
{
XrPosef pose;
XrVector3f position;
packed_quaternion orientation;
XrVector3f linear_velocity;
XrVector3f angular_velocity;
// In order to avoid packet fragmentation
+4 -1
View File
@@ -80,7 +80,10 @@ static xrt_space_relation to_relation(const from_headset::hand_tracking::pose &
{
return {
.relation_flags = cast_flags(pose.flags),
.pose = xrt_cast(pose.pose),
.pose = xrt_cast(XrPosef{
.orientation = pose.orientation,
.position = pose.position,
}),
.linear_velocity = xrt_cast(pose.linear_velocity),
.angular_velocity = xrt_cast(pose.angular_velocity),
};
+24 -1
View File
@@ -34,7 +34,30 @@ xrt_space_relation pose_list::interpolate(const xrt_space_relation & a, const xr
{
xrt_space_relation result;
xrt_space_relation_flags flags = xrt_space_relation_flags(a.relation_flags & b.relation_flags);
m_space_relation_interpolate(const_cast<xrt_space_relation *>(&a), const_cast<xrt_space_relation *>(&b), t, flags, &result);
if (math_quat_dot(&a.pose.orientation, &b.pose.orientation) > 0)
{
m_space_relation_interpolate(const_cast<xrt_space_relation *>(&a), const_cast<xrt_space_relation *>(&b), t, flags, &result);
}
else
{
xrt_space_relation b2{
.relation_flags = b.relation_flags,
.pose = {
.orientation = {
.x = -b.pose.orientation.x,
.y = -b.pose.orientation.y,
.z = -b.pose.orientation.z,
.w = -b.pose.orientation.w,
},
.position = b.pose.position,
},
.linear_velocity = b.linear_velocity,
.angular_velocity = b.angular_velocity,
};
m_space_relation_interpolate(const_cast<xrt_space_relation *>(&a), &b2, t, flags, &result);
}
return result;
}