Rise
The Vieneo Province
Loading...
Searching...
No Matches
FreeTrackInput.h
Go to the documentation of this file.
1#pragma once
2
3#include <cmath>
4#include <cstdint>
5
6// The shared-memory ABI is the same for 32-bit and 64-bit trackers/clients.
8{
9 std::uint32_t dataID;
10 std::int32_t camWidth, camHeight;
11 float yaw, pitch, roll, x, y, z;
13 float x1, y1, x2, y2, x3, y3, x4, y4;
14};
15static_assert(sizeof(FreeTrackData) == 92, "FreeTrack shared memory is architecture independent");
16
17// Device-independent stream state, shared by the DLL adapter and regression tests.
19{
20 unsigned selection = 0;
21 bool haveCounter = false;
22 bool havePose = false;
23 std::uint32_t lastCounter = 0;
24 std::uint64_t lastFrameTime = 0, rateStart = 0;
25 unsigned frames = 0;
26
27public:
28 float yaw = 0, pitch = 0, roll = 0, x = 0, y = 0, z = 0;
29 float rate = 0;
30
31 // A UI liveness timeout, not a tracker/physics timing constant.
32 bool Tracking(std::uint64_t now) const { return havePose && now - lastFrameTime < 1000; }
33 void Reset() { *this = FreeTrackInput{}; }
34
35 template<class Reader>
36 bool Poll(bool lateral, bool vertical, std::uint64_t now, Reader read)
37 {
38 const unsigned requested = (lateral ? 1u : 0u) | (vertical ? 2u : 0u);
39 if (requested != selection)
40 {
41 Reset();
42 selection = requested;
43 }
44 if (!requested)
45 return false; // In particular, do not call into the DLL.
46
47 if (!Tracking(now))
48 {
49 havePose = false;
50 roll = x = y = z = rate = 0;
51 frames = 0;
52 }
53
54 FreeTrackData sample{};
55 if (!read(&sample))
56 return false;
57 if (!haveCounter)
58 {
59 // Establish a baseline on enable/rebind. A retained pose from a
60 // stopped producer must not jump the camera or replay on re-enable.
61 haveCounter = true;
62 lastCounter = sample.dataID;
63 return false;
64 }
65 if (sample.dataID == lastCounter)
66 return false;
67 lastCounter = sample.dataID; // Equality remains valid across wraparound.
68 if (!std::isfinite(sample.yaw) || !std::isfinite(sample.pitch) ||
69 !std::isfinite(sample.roll) || !std::isfinite(sample.x) ||
70 !std::isfinite(sample.y) || !std::isfinite(sample.z))
71 return false;
72
73 if (!havePose)
74 rateStart = now;
75 else
76 ++frames;
77 havePose = true;
78 lastFrameTime = now;
79 if (now - rateStart >= 1000)
80 {
81 rate = static_cast<float>(frames) * 1000.0f / static_cast<float>(now - rateStart);
82 rateStart = now;
83 frames = 0;
84 }
85 yaw = -sample.yaw;
86 pitch = -sample.pitch;
87 if (lateral && vertical)
88 {
89 roll = sample.roll;
90 // Preserve Rise's existing tracker translation scale (to km).
91 x = -sample.x * 0.0000004f;
92 y = -sample.z * 0.0000004f;
93 z = sample.y * 0.0000004f;
94 }
95 return true;
96 }
97
98 void Apply(bool lateral, bool vertical, bool invertLateral, bool invertVertical,
99 float& targetLateral, float& targetVertical) const
100 {
101 if (lateral) targetLateral = invertLateral ? -yaw : yaw;
102 if (vertical) targetVertical = invertVertical ? -pitch : pitch;
103 }
104};
void Apply(bool lateral, bool vertical, bool invertLateral, bool invertVertical, float &targetLateral, float &targetVertical) const
std::uint32_t lastCounter
bool Poll(bool lateral, bool vertical, std::uint64_t now, Reader read)
bool Tracking(std::uint64_t now) const
std::uint64_t lastFrameTime
std::uint64_t rateStart
std::int32_t camWidth
std::uint32_t dataID
std::int32_t camHeight