diff --git a/client/scenes/stream_tracking.cpp b/client/scenes/stream_tracking.cpp index fa30e2f3..09c3a0fb 100644 --- a/client/scenes/stream_tracking.cpp +++ b/client/scenes/stream_tracking.cpp @@ -207,7 +207,8 @@ static std::optional + * + * 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 + +#include +#include +#include +#include +#include +#include + +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); +} diff --git a/common/wivrn_packets.h b/common/wivrn_packets.h index ef3e4334..18d0f133 100644 --- a/common/wivrn_packets.h +++ b/common/wivrn_packets.h @@ -32,6 +32,7 @@ #include #include +#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 diff --git a/server/driver/hand_joints_list.cpp b/server/driver/hand_joints_list.cpp index a79453df..4789b848 100644 --- a/server/driver/hand_joints_list.cpp +++ b/server/driver/hand_joints_list.cpp @@ -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), }; diff --git a/server/driver/pose_list.cpp b/server/driver/pose_list.cpp index b981627b..a74cfb57 100644 --- a/server/driver/pose_list.cpp +++ b/server/driver/pose_list.cpp @@ -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(&a), const_cast(&b), t, flags, &result); + + if (math_quat_dot(&a.pose.orientation, &b.pose.orientation) > 0) + { + m_space_relation_interpolate(const_cast(&a), const_cast(&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(&a), &b2, t, flags, &result); + } return result; }