Files
dime-online 754ba4f6e1 driver: fix inverted interpolation factor in history::get_at
interpolate(before, after, t) expects t = 0 at the before sample and
t = 1 at the after sample (see hand_joints_list.cpp: radius = a.radius
* (1 - t) + b.radius * t, and monado's m_space_relation_interpolate
which lerps from the predecessor with the elapsed fraction).

The factor was computed as (after - now) / (after - before), which is
1 - t: querying at the before timestamp returned the after sample,
querying at the after timestamp returned the before sample, and
queries in between returned the pose mirrored about the segment
midpoint.

Compute t from the before sample so hand and body skeleton queries
between two tracking samples interpolate along the motion instead of
against it.
2026-09-27 19:17:06 +02:00

130 lines
3.1 KiB
C++

/*
* WiVRn VR streaming
* Copyright (C) 2022-2024 Guillaume Meunier <[email protected]>
* Copyright (C) 2022-2024 Patrick Nicolas <[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 "clock_offset.h"
#include "util/u_time.h"
#include <algorithm>
#include <cstddef>
#include <mutex>
#include <openxr/openxr.h>
namespace wivrn
{
template <typename Derived, typename Data, size_t MaxSamples = 10>
class history
{
struct TimedData : public Data
{
XrTime produced_timestamp;
XrTime at_timestamp_ns;
};
std::array<TimedData, MaxSamples> data{};
std::mutex mutex;
protected:
// return true if object is active (last request is not too old)
void add_sample(XrTime produced_timestamp, XrTime timestamp, const Data & sample, const clock_offset & offset)
{
XrTime produced = offset.from_headset(produced_timestamp);
XrTime t = offset.from_headset(timestamp);
std::lock_guard lock(mutex);
TimedData * target = data.data();
if (offset)
{
for (auto & item: data)
{
// Discard reordered packets
if (item.produced_timestamp > produced)
return;
if (std::abs(item.at_timestamp_ns - t) < 2'000'000)
{
target = &item;
break;
}
if (item.at_timestamp_ns < target->at_timestamp_ns)
target = &item;
}
}
*target = TimedData(sample, produced, t);
}
public:
std::pair<XrTime, Data> get_at(XrTime at_timestamp_ns)
{
std::lock_guard lock(mutex);
TimedData * before = nullptr;
TimedData * after = nullptr;
for (auto & item: data)
{
if (not item.at_timestamp_ns)
continue;
if (item.at_timestamp_ns < at_timestamp_ns)
{
if (not before or before->at_timestamp_ns < item.at_timestamp_ns)
before = &item;
}
else
{
if (not after or after->at_timestamp_ns > item.at_timestamp_ns)
after = &item;
}
}
XrTime produced = 0;
if (after)
produced = std::max(produced, after->produced_timestamp);
if (before)
produced = std::max(produced, before->produced_timestamp);
if (before and after)
{
float t = float(at_timestamp_ns - before->at_timestamp_ns) /
(after->at_timestamp_ns - before->at_timestamp_ns);
return {produced, static_cast<Derived *>(this)->interpolate(*before, *after, t)};
}
if (before)
{
if (at_timestamp_ns > before->at_timestamp_ns + U_TIME_1S_IN_NS)
return {};
return {produced, *before};
}
if (after)
return {produced, *after};
return {};
}
void reset()
{
std::lock_guard lock(mutex);
data.fill({});
}
};
} // namespace wivrn