Initial commit

This commit is contained in:
Martino Ferrari committed 2026-10-01 15:42:48 +02:00
commit 8059b90de7
29 files changed
+7893

No files matched your search

+414
View File
@@ -0,0 +1,414 @@
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: 2026 Martino Ferrari
#include "plot_host.hpp"
#include <algorithm>
#include <cmath>
namespace udpscope {
namespace {
/// A small qualitative palette, one colour per *signal*. It is indexed by the signal
/// rather than by the trace slot so that a signal plotted on several rows is the same
/// colour in all of them: the signal list's chip is what ties a row of that list to a
/// trace on screen, and a colour that depended on where the signal landed would name a
/// different trace in every row.
constexpr plot::u32 kPalette[] = {
0x4FA3FFu, 0xFF8A4Fu, 0x5FD08Au, 0xE86A9Cu, 0xC9A227u, 0x8F7BFFu, 0x4FD0D0u, 0xFF6B6Bu,
};
constexpr std::size_t kPaletteSize = sizeof(kPalette) / sizeof(kPalette[0]);
/// The chip of a signal that is acquired but on no plot, and of one that is not even
/// acquired. Mid grey: it has to read as "no trace" next to any palette entry.
constexpr plot::u32 kUnplottedColor = 0x6E6E6Eu;
constexpr plot::u32 kCursorAColor = 0xFFD24Fu;
constexpr plot::u32 kCursorBColor = 0x7FE0FFu;
constexpr plot::u32 kRulerColor = 0xB0B0B0u;
constexpr plot::u32 kTriggerColor = 0xFF5B5Bu;
constexpr double kParked = -1.0e12; ///< hides an instrument without removing it
/// The gap between two rows, in device pixels. The rows used to be separate widgets in
/// a box, so there was a widget gap; a figure tiles its viewport, and without a gap two
/// stacked frames read as one tall plot with a line through it.
constexpr plot::f32 kRowGap = 6.0f;
} // namespace
plot::Color RowSet::palette_color(int index) {
const std::size_t i = static_cast<std::size_t>(index < 0 ? 0 : index) % kPaletteSize;
return plot::Color::hex(kPalette[i]);
}
plot::Color RowSet::unplotted_color() { return plot::Color::hex(kUnplottedColor); }
RowSet::RowSet() { rebuild_all(); }
void RowSet::set_row_count(int rows) {
const int want = std::clamp(rows, 1, kMaxRows);
if (want == rows_) return;
rows_ = want;
// A `Figure`'s row count is fixed at construction, so a new count is a new figure.
rebuild_all();
}
void RowSet::install(Row& r, int /*row_index*/, plot::Plot& p) {
r.plot = &p;
r.axis_x = r.plot->add_x_axis(
{"time", {-1.0, 1.0}, true, plot::AxisPlace::Bottom, false, plot::TickFormat::Time});
r.axis_y = r.plot->add_y_axis(
{"value", {-1.0, 1.0}, true, plot::AxisPlace::Left, false, plot::TickFormat::Si});
for (int s = 0; s < kSlotsPerRow; ++s) {
const std::size_t i = static_cast<std::size_t>(s);
r.plot->add_trace(
{&r.slots[i], palette_color(r.color_index[i]), 1.2f, r.axis_x, r.axis_y, r.name[i]});
}
r.cursor_a = r.plot->add_cursor(0.0, plot::Color::hex(kCursorAColor));
r.cursor_b = r.plot->add_cursor(0.0, plot::Color::hex(kCursorBColor));
r.ruler = r.plot->add_h_ruler(0.0, plot::Color::hex(kRulerColor));
r.trigger_marker = r.plot->add_cursor(kParked, plot::Color::hex(kTriggerColor));
// A marker, not an instrument: it must sit exactly on the edge's time, so its
// snap is Free and the controller never rounds it onto a sample.
r.plot->set_cursor_snap(r.trigger_marker, plot::SnapMode::Free);
// No `set_x_follow(Live)`: the link group owns the window *and* the follow state, and
// `Figure::link_x()` refuses a member whose x axis still follows on its own.
}
void RowSet::rebuild_all() {
figure_ = std::make_unique<plot::Figure>(static_cast<plot::u32>(rows_));
figure_->set_row_gap(kRowGap);
x_group_ = figure_->add_x_link();
for (int i = 0; i < kMaxRows; ++i) {
Row& r = row(i);
row_dirty_[static_cast<std::size_t>(i)] = false;
if (i >= rows_) {
r.plot = nullptr; // not a row of this figure: nothing to plot into
continue;
}
install(r, i, figure_->plot(static_cast<plot::u32>(i)));
place_cursors(r);
if (trigger_marker_on_) {
r.plot->set_cursor_x(r.trigger_marker, trigger_marker_x_, plot::CursorPlacement::Free);
}
figure_->link_x(static_cast<plot::u32>(i), x_group_);
}
if (y_gutter_px_ > 0.0) apply_y_gutter();
// A new figure starts with no window at all, so the one the rows were showing has to
// be restated -- as the pin it was, if an event is on screen: assigning a trace or
// changing the row count while a capture is displayed must not throw the view back
// onto the live edge, which in trigger mode nothing would move it off again until the
// next latch.
if (pinned_) {
pin_event_window(pinned_pre_, pinned_post_);
} else {
set_time_window(window_s_);
}
}
bool RowSet::rebuild_if_dirty() {
bool any = false;
for (int i = 0; i < kMaxRows; ++i) {
if (row_dirty_[static_cast<std::size_t>(i)]) any = true;
}
if (!any) return false;
// One row's legend text changed, which `Plot` cannot express, so its traces have to
// be created again -- and a row belongs to a figure, so the figure is rebuilt. That
// costs the plots' CPU state and nothing else: the host keeps its GL context and is
// handed the new figure.
rebuild_all();
return true;
}
void RowSet::assign(int row_index, int slot, std::string signal_name,
std::string display_name, int color_index) {
if (row_index < 0 || row_index >= kMaxRows || slot < 0 || slot >= kSlotsPerRow) return;
Row& r = row(row_index);
const std::size_t s = static_cast<std::size_t>(slot);
r.signal_name[s] = std::move(signal_name);
r.channel[s] = -1; // re-resolved by the next bind()
r.name[s] = std::move(display_name);
r.color_index[s] = color_index;
row_dirty_[static_cast<std::size_t>(row_index)] = true;
}
bool RowSet::slot_bound(int row_index, int slot) const {
if (row_index < 0 || row_index >= kMaxRows || slot < 0 || slot >= kSlotsPerRow) return false;
return !row(row_index).signal_name[static_cast<std::size_t>(slot)].empty();
}
int RowSet::channel_of(int row_index, int slot) const {
if (row_index < 0 || row_index >= kMaxRows || slot < 0 || slot >= kSlotsPerRow) return -1;
return row(row_index).channel[static_cast<std::size_t>(slot)];
}
void RowSet::bind(ScopeModel& model) {
const std::vector<SignalInfo>& signals = model.signals();
const std::vector<ChannelInfo>& channels = model.channels();
for (int i = 0; i < kMaxRows; ++i) {
Row& r = row(i);
for (int s = 0; s < kSlotsPerRow; ++s) {
const std::size_t si = static_cast<std::size_t>(s);
if (r.signal_name[si].empty()) {
r.channel[si] = -1;
r.slots[si].bind(nullptr);
continue;
}
// Resolve the stable identity every frame: the channel list is rebuilt (and
// renumbered) whenever a signal is enabled, disabled or re-described, and a
// stored index would then point at a different signal's channel.
int signal_index = -1;
for (std::size_t k = 0; k < signals.size(); ++k) {
if (signals[k].name == r.signal_name[si]) {
signal_index = static_cast<int>(k);
break;
}
}
int channel = -1;
if (signal_index >= 0) {
for (std::size_t c = 0; c < channels.size(); ++c) {
if (static_cast<int>(channels[c].signal) == signal_index) {
channel = static_cast<int>(c);
break;
}
}
}
r.channel[si] = channel;
if (channel < 0) {
// Its signal is present but not acquired: keep the identity (so enabling it
// brings the trace back) and plot nothing meanwhile.
r.slots[si].bind(nullptr);
continue;
}
// In trigger mode the trace shows the captured event and nothing else: the
// last one latched, or nothing at all before the first. Never the live ring,
// not even while the post window is filling -- a frozen picture that only
// changes on an event is the whole point of trigger mode, and binding the
// live ring mid-capture made the plot scroll for most of every event cycle.
const std::size_t ci = static_cast<std::size_t>(channel);
if (model.trigger_enabled()) {
if (auto snap = model.event()) {
r.slots[si].bind_event(std::move(snap), ci);
} else {
r.slots[si].bind(nullptr); // armed, waiting for the first event: blank
}
} else {
r.slots[si].bind(model.live().shared_channel(ci));
}
}
}
}
bool RowSet::live_edge(double& newest) const {
bool any = false;
for (int i = 0; i < rows_; ++i) {
const Row& r = row(i);
if (!r.plot) continue;
double edge = 0.0;
// Follow-independent: it reads the row's streaming traces, so a row whose own axis
// is `Off` (which every member of a link group is) still reports its edge.
if (!r.plot->x_live_edge(r.axis_x, edge)) continue;
newest = any ? std::max(newest, edge) : edge;
any = true;
}
return any;
}
void RowSet::set_time_window(double seconds) {
window_s_ = std::max(0.05, seconds);
pinned_ = false;
if (!figure_) return;
figure_->set_x_link_follow(x_group_, plot::XFollow::Live);
double newest = 0.0;
if (live_edge(newest)) {
// Under a following group the range states the span and -- through its maximum --
// where the window sits behind the live edge, so a maximum *at* the edge is what
// makes the offset zero and parks the window on the newest sample. Asking for
// `{0, window}` here instead would state an offset of a whole epoch.
figure_->set_x_link_range(x_group_, {newest - window_s_, newest});
} else {
figure_->set_x_link_range(x_group_, {0.0, window_s_});
}
}
void RowSet::set_y_auto(int row_index, bool on) {
Row& r = row(row_index);
if (!r.plot) return;
r.plot->set_y_auto_scale(r.axis_y, on);
if (on) r.plot->auto_fit_y(r.axis_y);
}
void RowSet::set_y_range(int row_index, double lo, double hi) {
Row& r = row(row_index);
if (!r.plot) return;
const double a = std::min(lo, hi);
const double b = std::max(lo, hi);
if (!std::isfinite(a) || !std::isfinite(b) || !(b > a)) return;
r.plot->set_y_range(r.axis_y, {a, b});
}
void RowSet::place_cursors(Row& r) {
if (!r.plot) return;
const bool show = r.cursors_visible;
const double a = show ? r.cursor_a_x : kParked;
const double b = show ? r.cursor_b_x : kParked;
const double h = show ? r.ruler_y : kParked;
r.plot->set_cursor_x(r.cursor_a, a);
r.plot->set_cursor_x(r.cursor_b, b);
r.plot->set_cursor_y(r.ruler, h);
}
void RowSet::clamp_to_data() {
if (!figure_) return;
// The rows share one window, so they share one bound: the union of what they hold.
// Taking each row's own would let a row whose signal was enabled a moment ago clamp
// the window every row is looking at.
plot::Range data{};
bool have = false;
for (int i = 0; i < rows_; ++i) {
Row& r = row(i);
if (!r.plot) continue;
const plot::data::Series* series = nullptr;
for (int s = 0; s < kSlotsPerRow && series == nullptr; ++s) {
if (!r.signal_name[static_cast<std::size_t>(s)].empty()) {
series = r.slots[static_cast<std::size_t>(s)].bound();
}
}
if (series == nullptr || series->size() < 2) continue;
const plot::Range b = series->x_bounds();
if (!std::isfinite(b.min) || !std::isfinite(b.max) || !(b.max > b.min)) continue;
data = have ? plot::Range{std::min(data.min, b.min), std::max(data.max, b.max)} : b;
have = true;
}
if (!have) return;
const double retained = data.max - data.min;
const double span = figure_->x_link_range(x_group_).span();
if (!(span > 0.0) || !std::isfinite(span)) return;
// The *requested* window is intent, and a buffer that has not filled it yet must not
// be allowed to redefine it. This runs every frame and cannot tell "the user zoomed
// out past the data" from "the ring has only just started filling", and the first
// frames after a signal is enabled hold a couple of hundred samples: clamping to
// those shrank a requested 2 s window to ~0.02 s, and nothing here ever grows a span
// back, so the view stayed pinned to the newest 20 ms of a 6.5 s buffer for the rest
// of the run. That is the loudest form of the "live trace is clipped, only the most
// recent part is visible, data is there because the crosshair reads it" defect -- the
// data was never clipped at all, the window was. Clamping against `max(retained,
// window_s_)` leaves a partly filled window empty on its left, which is what a scope
// that just started should look like.
//
// The span only. A pan is the group's `offset` behind its live edge and the group
// clamps it against what its members retain on every layout, so there is nothing
// here to do about `view.min < data.min` -- and doing it anyway would state a maximum
// away from the live edge, i.e. ask the group to scroll back.
const double limit = std::max(retained, window_s_);
if (span > limit) figure_->set_x_link_range(x_group_, {data.max - limit, data.max});
}
void RowSet::pin_event_window(double pre_s, double post_s) {
window_s_ = std::max(1.0e-3, pre_s + post_s);
pinned_ = true;
pinned_pre_ = pre_s;
pinned_post_ = post_s;
if (!figure_) return;
// The captured event sits at [-pre, +post] with the edge at 0, and the bound series is
// the immutable snapshot. The group must stop following first: Live re-derives the
// window from a live edge, and with nothing streaming it would keep placing the window
// from the last stale edge instead of the event.
figure_->set_x_link_follow(x_group_, plot::XFollow::Off);
figure_->set_x_link_range(x_group_, {-pre_s, post_s});
}
void RowSet::fit_all() {
for (int i = 0; i < rows_; ++i) {
Row& r = row(i);
if (r.plot) r.plot->auto_fit_y(r.axis_y);
}
Row& first = row(0);
if (!first.plot || !figure_) return;
// Row 0 fits its own x to the data it holds; that span becomes the shared window, and
// stating it through the group is what puts every row on it.
first.plot->auto_fit_x();
const plot::Range fitted = first.plot->x_range(first.axis_x);
const double span = fitted.span();
if (!std::isfinite(span) || !(span > 0.0)) return;
if (pinned_) {
// Fitting a captured event: the window is the snapshot's own `[-pre, +post]` span,
// and `set_time_window()` would re-arm the follow and re-frame on a live edge that
// trigger mode has nothing bound to.
window_s_ = span;
figure_->set_x_link_range(x_group_, fitted);
} else {
set_time_window(span);
}
}
void RowSet::note_y_gutter(double need_px) {
double want = std::min(need_px + 6.0, 200.0);
// Reserve room for the widest label the formatter can produce, once, with the
// font the labels are actually drawn in (the embedded font is monospaced, so a
// character count is an exact width).
if (!y_gutter_reserved_) {
for (int i = 0; i < rows_; ++i) {
Row& r = row(i);
if (!r.plot) continue;
const plot::render::TextBackend* text = r.plot->text_backend();
if (text == nullptr) continue;
const plot::render::Style& style = r.plot->style();
const double advance = double(text->measure("0123456789")) / 10.0;
const double pad = double(style.device(style.grid_label_pad));
// "-1.23e-45" is the longest label `format_si()`'s fallback can print.
want = std::max(want, 9.0 * advance + 2.0 * pad + 2.0);
y_gutter_reserved_ = true;
break;
}
}
if (want > y_gutter_px_) y_gutter_px_ = want;
}
void RowSet::apply_y_gutter() {
if (!figure_ || !(y_gutter_px_ > 0.0)) return;
figure_->set_min_y_gutter(static_cast<f32>(y_gutter_px_));
}
void RowSet::set_trigger_marker(double x, bool visible) {
trigger_marker_on_ = visible;
trigger_marker_x_ = x;
const double want = visible ? x : kParked;
for (int i = 0; i < rows_; ++i) {
Row& r = row(i);
if (!r.plot) continue;
// Re-assert against what the plot *holds*, not against a cached value: the
// marker is a cursor, so the input layer will happily drag it, and a marker the
// user can move off the trigger instant is not marking anything. Comparing
// with the plot also keeps the steady state write-free (a write invalidates the
// plot's cached measurements).
if (r.plot->cursor(r.trigger_marker).x == want) continue;
r.plot->set_cursor_x(r.trigger_marker, want, plot::CursorPlacement::Free);
}
}
void RowSet::set_cursors_visible(bool on) {
for (int i = 0; i < kMaxRows; ++i) {
Row& r = row(i);
r.cursors_visible = on;
place_cursors(r);
}
}
void RowSet::reset_cursors() {
for (int i = 0; i < kMaxRows; ++i) {
Row& r = row(i);
if (!r.plot) continue;
const plot::Range xr = r.plot->x_range(r.axis_x);
const double span = xr.span() > 0.0 ? xr.span() : window_s_;
r.cursor_a_x = xr.min + 0.25 * span;
r.cursor_b_x = xr.min + 0.75 * span;
r.ruler_y = r.plot->y_range(r.axis_y).min +
0.5 * (r.plot->y_range(r.axis_y).span() > 0.0 ? r.plot->y_range(r.axis_y).span()
: 1.0);
place_cursors(r);
}
}
} // namespace udpscope