#include "evo/client/interpolation.hpp" #include #include namespace evo::client { namespace { double ms(TickClock::Clock::duration d) { return std::chrono::duration(d).count(); } // Held-vector motion in micrometres for a fractional number of ticks (presentation only). double motion_um(std::int16_t component, double ticks) { return static_cast(component) * static_cast(network::motion::speed_um_per_tick) / 1000.0 * ticks; } } // namespace void TickClock::reset() noexcept { base_tick_.reset(); newest_ = 0; returned_ = 0; } double TickClock::raw(double base, Clock::time_point now) const { return base + (ms(now - base_time_) / limits_.tick_ms); } void TickClock::observe(std::uint64_t tick, Clock::time_point now) { newest_ = std::max(newest_, tick); seen_ = now; const auto sample = static_cast(tick); if (!base_tick_) { base_tick_ = sample; base_time_ = now; return; } const auto estimate = raw(*base_tick_, now); const auto error = sample - estimate; if (error > limits_.snap_ticks) { base_tick_ = sample; // Far behind (a pause or a reconnect): catch up at once. ++snaps_; } else { base_tick_ = estimate + (error * limits_.gain); // Slew; at() keeps it monotonic. } base_time_ = now; } std::optional TickClock::silence(Clock::time_point now) const { if (!base_tick_) return std::nullopt; return ms(now - seen_) / limits_.tick_ms; } std::optional TickClock::at(Clock::time_point now) { if (!base_tick_) return std::nullopt; const auto bounded = std::min(raw(*base_tick_, now), static_cast(newest_) + limits_.max_ahead); returned_ = std::max(returned_, bounded); return returned_; } void MarkerInterpolator::reset() { snapshots_.clear(); gaps_.clear(); delay_ = target_ = limits_.min_delay; } void MarkerInterpolator::push(std::uint64_t tick, std::span markers) { if (!snapshots_.empty() && tick <= snapshots_.back().tick) { ++stats_.ignored; return; } ++stats_.snapshots; if (!snapshots_.empty()) { gaps_.push_back(tick - snapshots_.back().tick); while (gaps_.size() > limits_.gap_window) gaps_.pop_front(); // Cover the 90th-percentile gap between received snapshots, plus the clock's lead. std::vector sorted(gaps_.begin(), gaps_.end()); std::ranges::sort(sorted); const auto p90 = sorted.at((sorted.size() - 1) * 9 / 10); target_ = std::clamp(static_cast(p90) + 1, limits_.min_delay, limits_.max_delay); } Snapshot s{tick, {markers.begin(), markers.end()}}; std::ranges::sort(s.markers, {}, &RemoteMarker::id); snapshots_.push_back(std::move(s)); while (snapshots_.size() > limits_.max_snapshots) snapshots_.pop_front(); } void MarkerInterpolator::advance(double elapsed_ms) { if (elapsed_ms <= 0) return; const auto seconds = elapsed_ms / 1000; if (delay_ < target_) delay_ = std::min(target_, delay_ + (limits_.raise_per_second * seconds)); else delay_ = std::max(target_, delay_ - (limits_.lower_per_second * seconds)); } std::vector MarkerInterpolator::at(double render_tick) { std::vector out; if (snapshots_.empty()) return out; const auto& newest = snapshots_.back(); const auto show = [&](const Snapshot& s) { for (const auto& m : s.markers) out.push_back({m.id, static_cast(m.state.x), static_cast(m.state.y)}); }; if (render_tick >= static_cast(newest.tick)) { // Beyond the newest snapshot: continue the held vector briefly, then freeze (stale). const auto beyond = render_tick - static_cast(newest.tick); const auto ticks = std::min(beyond, limits_.max_extrapolation); const bool stale = beyond > limits_.max_extrapolation; for (const auto& m : newest.markers) out.push_back({m.id, static_cast(m.state.x) + motion_um(m.state.vector.x, ticks), static_cast(m.state.y) + motion_um(m.state.vector.y, ticks), ticks > 0, stale}); stats_.extrapolated_frames += ticks > 0 ? 1 : 0; stats_.stale_frames += stale ? 1 : 0; return out; } if (render_tick <= static_cast(snapshots_.front().tick)) { show(snapshots_.front()); // Not enough history yet: the oldest as it was. return out; } // a.tick <= render_tick < b.tick. const auto b = std::ranges::find_if( snapshots_, [&](const Snapshot& s) { return static_cast(s.tick) > render_tick; }); const auto& a = *std::prev(b); if (b->tick - a.tick > limits_.max_gap) { ++stats_.gap_snaps; // Too far apart to interpolate: hold the older (stale) until b. show(a); for (auto& m : out) m.stale = true; return out; } const auto t = (render_tick - static_cast(a.tick)) / static_cast(b->tick - a.tick); for (const auto& m : a.markers) { const auto next = std::ranges::lower_bound(b->markers, m.id, {}, &RemoteMarker::id); if (next == b->markers.end() || next->id != m.id) { // Removed by b: shown where it was until b's tick, never moved toward a removal. out.push_back({m.id, static_cast(m.state.x), static_cast(m.state.y)}); continue; } const auto lerp = [t](std::int64_t from, std::int64_t to) { return static_cast(from) + ((static_cast(to) - static_cast(from)) * t); }; out.push_back({m.id, lerp(m.state.x, next->state.x), lerp(m.state.y, next->state.y)}); } return out; // Markers new in b appear from b's tick. } } // namespace evo::client