Rise
The Vieneo Province
Loading...
Searching...
No Matches
TelemetrySnapshot.h
Go to the documentation of this file.
1#pragma once
2
3#include <array>
4#include <cstddef>
5#include <cstdint>
6
7namespace Telemetry
8{
9 constexpr std::size_t SnapshotCapacity = 16;
10 constexpr std::size_t EntityCapacity = 64;
11
12 struct Vec3
13 {
14 double x = 0.0;
15 double y = 0.0;
16 double z = 0.0;
17
18 Vec3 operator+(const Vec3& rhs) const;
19 Vec3 operator-(const Vec3& rhs) const;
20 Vec3 operator*(double scalar) const;
21 Vec3 operator/(double scalar) const;
22 Vec3& operator+=(const Vec3& rhs);
23 };
24
26 {
27 double x = 0.0;
28 double y = 0.0;
29 double z = 0.0;
30 double w = 1.0;
31 };
32
33 enum class SequenceMode
34 {
37 };
38
47
49 {
51 // Derivative of position in its encoded reference frame. For a free
52 // vehicle this is inertial velocity minus local barycentric rotation;
53 // for weapons and dock offsets it is normally identical to velocity.
58 // X is pitch, Y is yaw, and Z is roll, in radians/second.
62 double steerDeflection = 0.0;
63 double receiptSeconds = 0.0;
64 double timelineSeconds = 0.0;
66 std::uint16_t sequence = 0;
67 std::uint16_t sampleTimeMs = 0;
68 std::uint16_t sourceRevision = 0;
69 int reference = -127;
70 int parentSlot = -1;
71 int identity = 0;
72 bool finalSample = false;
74 };
75
94
96 {
100 // Sparse streams need one complete source interval in the buffer so the
101 // presentation can interpolate between known keyframes instead of
102 // predicting for nearly the entire interval. This value is the extra
103 // network/jitter margin beyond that source interval.
118 std::uint16_t maximumBootstrapSequence = 32;
119 };
120
122 {
126 double jitterSeconds = 0.0;
130 double predictionErrorKm = 0.0;
132 std::uint32_t sequenceGaps = 0;
133 std::uint32_t coalescedSourceRevisions = 0;
135 std::uint32_t duplicateCount = 0;
136 std::uint32_t outOfOrderCount = 0;
137 std::uint32_t invalidCount = 0;
138 std::uint32_t generation = 0;
139 std::size_t snapshotDepth = 0;
140 std::uint16_t lastAcceptedSequence = 0;
141 std::uint16_t lastSampleTimeMs = 0;
142 std::uint16_t lastSourceRevision = 0;
143 std::uint16_t lastSourceDeltaMs = 0;
144 std::uint16_t lastDeliverySequenceDelta = 0;
145 int reference = -127;
146 int parentSlot = -1;
147 int identity = 0;
148 bool hasSequence = false;
149 bool sourceTimed = false;
150 };
151
152 bool IsFinite(const Vec3& value);
153 bool IsFinite(const Quaternion& value);
154 double Length(const Vec3& value);
155 double QuaternionLength(const Quaternion& value);
156 bool NormalizeQuaternion(Quaternion& value);
157 Quaternion Slerp(const Quaternion& a, const Quaternion& b, double amount);
158 Quaternion IntegrateAngularVelocity(const Quaternion& orientation, const Vec3& angularVelocity, double seconds);
159 bool IsNewerUShort(std::uint16_t incoming, std::uint16_t previous);
160 bool IsNewerModulo255(std::uint8_t incoming, std::uint8_t previous);
161 std::uint16_t SourceTimeDeltaMilliseconds(std::uint16_t incoming, std::uint16_t previous);
162 bool HasExactPayloadLength(std::size_t actualBytes, std::size_t expectedBytes);
163 bool ShouldBufferVehicleReference(int reference);
164 double MonotonicSeconds();
165 Vec3 WeaponFrameEndpoint(const Vec3& presentationPosition, const Vec3& velocity,
166 double elapsedSeconds, bool hasAuthoritativePresentation);
167
169 {
170 public:
172
173 void Configure(SequenceMode sequenceMode, const ChannelConfig& config);
174 void Reset(int identity, int reference, int parentSlot, bool requireLowBootstrapSequence);
175 Acceptance Push(MotionSnapshot snapshot, double receiptSeconds);
176 PresentationState Sample(double nowSeconds, double elapsedSeconds);
177 ChannelMetrics GetMetrics(double nowSeconds) const;
178 bool HasSamples() const;
179 std::size_t Depth() const;
180 const MotionSnapshot& SnapshotAt(std::size_t logicalIndex) const;
181
182 private:
183 std::size_t PhysicalIndex(std::size_t logicalIndex) const;
184 void ClearHistory();
185 void RecordIncoming(double receiptSeconds);
186 void UpdateCadence(double rawIntervalSeconds);
187 void UpdateSourceCadence(double sourceIntervalSeconds);
188 void ResetSourceTiming();
189 double InterpolationDelay() const;
190 double ExtrapolationCap() const;
191 PresentationState Evaluate(double presentationSeconds) const;
192 PresentationState Interpolate(const MotionSnapshot& first, const MotionSnapshot& second, double amount) const;
193 PresentationState Extrapolate(const MotionSnapshot& snapshot, double seconds) const;
194 std::uint16_t ForwardDelta(std::uint16_t incoming) const;
195 void AdvanceCorrection(double elapsedSeconds);
196
197 std::array<MotionSnapshot, SnapshotCapacity> snapshots_ = {};
198 std::size_t head_ = 0;
199 std::size_t count_ = 0;
205 bool hasRawReceipt_ = false;
208 std::uint32_t packetWindowCount_ = 0;
210 bool sourceTimed_ = false;
212 std::uint16_t lastObservedSampleTimeMs_ = 0;
220 };
221
223 {
224 public:
226
227 void ResetVehicle(std::size_t slot, int identity, int reference, int parentSlot, bool identityChanged);
228 void ResetWeapon(std::size_t slot, int identity);
229 void RemoveVehicle(std::size_t slot);
230 void RemoveWeapon(std::size_t slot);
231 Acceptance PushVehicle(std::size_t slot, MotionSnapshot snapshot, double receiptSeconds);
232 Acceptance PushWeapon(std::size_t slot, MotionSnapshot snapshot, double receiptSeconds);
233 PresentationState SampleVehicle(std::size_t slot, double nowSeconds, double elapsedSeconds);
234 PresentationState SampleWeapon(std::size_t slot, double nowSeconds, double elapsedSeconds);
235 ChannelMetrics VehicleMetrics(std::size_t slot, double nowSeconds) const;
236 ChannelMetrics WeaponMetrics(std::size_t slot, double nowSeconds) const;
237 int ClosestWeaponSlot(const Vec3& worldObserverPosition, const Vec3& dockObserverPosition) const;
238 bool HasVehicleSamples(std::size_t slot) const;
239 bool HasWeaponSamples(std::size_t slot) const;
240
241 private:
242 std::array<SnapshotChannel, EntityCapacity> vehicles_;
243 std::array<SnapshotChannel, EntityCapacity> weapons_;
244 };
245
247}
void Configure(SequenceMode sequenceMode, const ChannelConfig &config)
PresentationState Extrapolate(const MotionSnapshot &snapshot, double seconds) const
std::array< MotionSnapshot, SnapshotCapacity > snapshots_
PresentationState lastPresentation_
Acceptance Push(MotionSnapshot snapshot, double receiptSeconds)
PresentationState Evaluate(double presentationSeconds) const
ChannelMetrics GetMetrics(double nowSeconds) const
const MotionSnapshot & SnapshotAt(std::size_t logicalIndex) const
void Reset(int identity, int reference, int parentSlot, bool requireLowBootstrapSequence)
void UpdateSourceCadence(double sourceIntervalSeconds)
PresentationState Sample(double nowSeconds, double elapsedSeconds)
std::size_t PhysicalIndex(std::size_t logicalIndex) const
std::uint16_t lastObservedSourceRevision_
PresentationState Interpolate(const MotionSnapshot &first, const MotionSnapshot &second, double amount) const
void UpdateCadence(double rawIntervalSeconds)
std::uint16_t ForwardDelta(std::uint16_t incoming) const
void RecordIncoming(double receiptSeconds)
void AdvanceCorrection(double elapsedSeconds)
bool HasVehicleSamples(std::size_t slot) const
bool HasWeaponSamples(std::size_t slot) const
void ResetWeapon(std::size_t slot, int identity)
ChannelMetrics VehicleMetrics(std::size_t slot, double nowSeconds) const
void ResetVehicle(std::size_t slot, int identity, int reference, int parentSlot, bool identityChanged)
void RemoveVehicle(std::size_t slot)
Acceptance PushVehicle(std::size_t slot, MotionSnapshot snapshot, double receiptSeconds)
std::array< SnapshotChannel, EntityCapacity > vehicles_
void RemoveWeapon(std::size_t slot)
int ClosestWeaponSlot(const Vec3 &worldObserverPosition, const Vec3 &dockObserverPosition) const
PresentationState SampleVehicle(std::size_t slot, double nowSeconds, double elapsedSeconds)
std::array< SnapshotChannel, EntityCapacity > weapons_
Acceptance PushWeapon(std::size_t slot, MotionSnapshot snapshot, double receiptSeconds)
PresentationState SampleWeapon(std::size_t slot, double nowSeconds, double elapsedSeconds)
ChannelMetrics WeaponMetrics(std::size_t slot, double nowSeconds) const
Quaternion Slerp(const Quaternion &aValue, const Quaternion &bValue, double amount)
double MonotonicSeconds()
Quaternion IntegrateAngularVelocity(const Quaternion &orientation, const Vec3 &angularVelocity, double seconds)
bool HasExactPayloadLength(std::size_t actualBytes, std::size_t expectedBytes)
double QuaternionLength(const Quaternion &value)
double Length(const Vec3 &value)
constexpr std::size_t SnapshotCapacity
constexpr std::size_t EntityCapacity
bool IsNewerModulo255(std::uint8_t incoming, std::uint8_t previous)
std::uint16_t SourceTimeDeltaMilliseconds(std::uint16_t incoming, std::uint16_t previous)
bool IsNewerUShort(std::uint16_t incoming, std::uint16_t previous)
Vec3 WeaponFrameEndpoint(const Vec3 &presentationPosition, const Vec3 &velocity, double elapsedSeconds, bool hasAuthoritativePresentation)
SnapshotSystem gSnapshotSystem
bool ShouldBufferVehicleReference(int reference)
bool NormalizeQuaternion(Quaternion &value)
bool IsFinite(const Vec3 &value)
double maximumSourcePhaseCorrectionPerPacketSeconds
std::uint16_t maximumBootstrapSequence
std::uint16_t lastDeliverySequenceDelta
std::uint32_t duplicateSourceRevisionCount
Vec3 & operator+=(const Vec3 &rhs)
Vec3 operator-(const Vec3 &rhs) const
Vec3 operator+(const Vec3 &rhs) const
Vec3 operator*(double scalar) const
Vec3 operator/(double scalar) const