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
+5 -76
View File
@@ -21,89 +21,18 @@
#include "wivrn_packets.h"
#include "clock_offset.h"
#include "history.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;
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>
class pose_list : public history<pose_list, xrt_space_relation>
{
xrt::drivers::wivrn::device_id device;
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) :
device(id) {}