diff --git a/CHANGELOG.md b/CHANGELOG.md index c3dceec..3483e03 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,95 @@ This is a workspace of three crates released together: **rsplot** (the plotting library), **rsdm** (a PyDM-style EPICS display layer built on rsplot), and **adl2rsdm** (a MEDM `.adl` → RsDM-Rust-source converter). +## [0.5.3] - 2026-07-10 + +A regression release. A workspace-wide upstream-parity audit (silx / PyDM / +adl2pydm + MEDM C) found that three of the fixes 0.5.2 shipped were wrong, and +turned up ten further divergences from the reference behaviour. All are fixed +here. `VolumeRaycaster` users should upgrade: the volume it rendered in 0.5.2 +carried no thickness cue and lost the hue of faint voxels. + +### Fixed — `rsplot` + +- **Volume opacity depends on the thickness a ray crosses again.** 0.5.2's + "Beer-Lambert" correction divided by the *step count*, an exponent carrying no + world distance, so every ray accumulated the same transmittance whatever chord + it traversed: a uniform volume rendered as a flat silhouette, and at the + default 256 steps the correction was exactly the identity. Each sample is now + corrected from a reference *spacing* (`box_diagonal / 256`, a world distance), + which keeps the step-count invariance 0.5.2 was after while restoring the + density cue. A ray crossing the full box diagonal at the default step count + renders exactly as before. +- **Faint voxels keep their colour.** The premultiplied 3-D texture 0.5.2 + introduced stored the product `rgb · a` in 8 bits, and the shader must divide + the coverage back out to recover the straight colour — a product that at + coverage `a` resolves the quotient only to steps of `1/a`. An authored + `(255, 128, 0)` voxel at `a = 3/255` read back as `(255, 170, 0)`; at + `a = 1/255` no hue survived at all. The texture is now `Rgba16Float`, which + carries the straight colour exactly at every coverage while the filter still + interpolates premultiplied colour (no return of the dark fringe). **This + doubles the volume texture's VRAM.** +- **`VolumeRaycaster::remove` no longer blanks another view.** Two views sharing + a `VolumeId` share one texture, by design; `remove` freed it outright, so the + survivor silently rendered nothing. The texture is now claim-counted and lives + exactly as long as the views holding its id. Dropping a view releases its + claim too, so a `VolumeRaycaster` that is simply dropped no longer pins its + VRAM for the app's lifetime. +- **Alt+Wheel zooms X only and Shift+Wheel zooms Y/Y2 only**, the silx bindings + the axes menu already advertised. Plain wheel and keep-aspect are unchanged. +- **Middle-button drag pans in every interaction mode**, as in silx. The + existing right-button pan is kept. +- **The Reset-Zoom toolbar button disables when neither axis autoscales**, and + its tooltip names the single autoscaling axis when exactly one is on. +- **Programmatic `set_limits` and toolbar Zoom-In/Out repair their limits.** + Inverted, degenerate or float32-overflowing ranges reached the transform + unrepaired; every view-limits commit now runs the same clamp, including the + Y2 and extra-axis paths. +- **The colormap dialog's histogram drops samples equal to the range maximum**, + as `Histogramnd` does with its default half-open last bin. +- **`Scatter` BINNED_STATISTIC drops points on the max edge** instead of + clamping them into the last bin, matching `scipy.stats.binned_statistic_2d` + as silx calls it. A degenerate extent now admits no point rather than + collapsing every point into bin 0. + +### Fixed — `rsdm` + +- **`calc://` re-evaluates when a child connects or disconnects.** PyDM + re-emits on a connection-state change; rsdm only watched values. +- **`calc://` no longer retriggers on a child's alarm or metadata change**, only + on an actual value change, as PyDM's `update` list specifies. +- **A `dialect = medm` expression reading operand `I` or `J` re-evaluates on an + alarm status / severity change.** MEDM's `setDynamicAttrMonitorFlags` adds + `monitorStatusChanged` and `monitorSeverityChanged` exactly when the + expression uses those operands. rsdm never watched severity, so a transition + left the calc showing a stale value, and it had no alarm status to read at all + — operand `I` bound a constant `0.0`. Both are now monitored, and only when + the expression reads them. + +### Added — `rsdm` + +- **`ChannelState::status`** carries the wire alarm status — the record's `STAT` + for `ca://`, the pvAccess `alarm.status` enum for `pva://`. It backs the MEDM + calc operand `I`. + +### Fixed — `adl2rsdm` + +- **Embedded-display and composite-file children take the host screen's + colormap.** MEDM's `parseCompositeFile` parses and discards the child file's + `"color map"` block and resolves the children's `clr`/`bclr` against the + parent display's palette; adl2rsdm used the child's own. Screens on the + default palette are unaffected; those with a custom palette rendered every + embedded widget in the wrong colours. +- **An arc with no `path` spans 90°, not 360°** — MEDM's `createDlArc` default. + +### Changed — `rsplot` + +- The volume 3-D texture is `Rgba16Float` rather than `Rgba8Unorm`, doubling its + VRAM. See the faint-voxel fix above for why an 8-bit texture cannot carry the + data. +- `VolumeRaycaster` now holds a clone of its `RenderState` (a bundle of `Arc`s) + so it can release its texture claim on drop. + ## [0.5.2] - 2026-07-08 A correctness and robustness pass over the `VolumeRaycaster` shipped in 0.5.1, @@ -403,7 +492,11 @@ instead of Qt `.ui` XML. - rsplot is now the root of a three-crate workspace; `rsdm` reaches egui/wgpu through `rsplot::egui` to keep a single egui/wgpu in the tree. -[Unreleased]: https://github.com/physwkim/rsplot/compare/v0.4.2...HEAD +[Unreleased]: https://github.com/physwkim/rsplot/compare/v0.5.3...HEAD +[0.5.3]: https://github.com/physwkim/rsplot/compare/v0.5.2...v0.5.3 +[0.5.2]: https://github.com/physwkim/rsplot/compare/v0.5.1...v0.5.2 +[0.5.1]: https://github.com/physwkim/rsplot/compare/v0.5.0...v0.5.1 +[0.5.0]: https://github.com/physwkim/rsplot/compare/v0.4.2...v0.5.0 [0.4.2]: https://github.com/physwkim/rsplot/compare/v0.4.1...v0.4.2 [0.4.1]: https://github.com/physwkim/rsplot/compare/v0.4.0...v0.4.1 [0.4.0]: https://github.com/physwkim/rsplot/compare/v0.3.0...v0.4.0 diff --git a/Cargo.toml b/Cargo.toml index 2a7c7f8..70659c4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rsplot" -version = "0.5.2" +version = "0.5.3" edition = "2024" rust-version = "1.92" license = "MIT OR Apache-2.0" @@ -24,6 +24,9 @@ resolver = "3" egui = "0.34" egui-wgpu = "0.34" bytemuck = { version = "1", features = ["derive"] } +# f16 encoding for the volume ray-caster's premultiplied 3D texture. Already in +# the tree transitively (via wgpu), so this pins no new major version. +half = "2" colorous = "1" # PNG encoding for save_graph (offscreen readback → file). Already in the tree # transitively via egui's image loading, so this pins no new major version. diff --git a/adl2rsdm/Cargo.toml b/adl2rsdm/Cargo.toml index 7a25b34..a5788b8 100644 --- a/adl2rsdm/Cargo.toml +++ b/adl2rsdm/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "adl2rsdm" -version = "0.5.2" +version = "0.5.3" edition = "2024" rust-version = "1.92" license = "MIT OR Apache-2.0" diff --git a/adl2rsdm/examples/local_panel_screen.rs b/adl2rsdm/examples/local_panel_screen.rs index 8245598..4891969 100644 --- a/adl2rsdm/examples/local_panel_screen.rs +++ b/adl2rsdm/examples/local_panel_screen.rs @@ -176,6 +176,7 @@ impl Screen { place(ui, __frame_origin_16, sx, sy, egui::Order::Middle, egui::Id::new(15u64), 0.0, 18.0, 152.0, 16.0, |ui| { { ui.style_mut().override_font_id = Some(egui::FontId::proportional(10.0 * sy)); + ui.style_mut().visuals.override_text_color = Some(Color32::from_rgb(0, 0, 255)); ui.with_layout(egui::Layout::centered_and_justified(egui::Direction::LeftToRight), |ui| { let spacing = ui.spacing_mut(); spacing.interact_size = egui::Vec2::ZERO; @@ -341,7 +342,7 @@ fn related_display_icon(ui: &egui::Ui, rect: egui::Rect, fg: egui::Color32, bg: let side = (rect.height().min(rect.width()) - 8.0).max(4.0); let icon = egui::Rect::from_center_size(rect.center(), egui::Vec2::splat(side)); let p = |x: f32, y: f32| icon.min + egui::vec2(x, y) * (side / 25.0); - let stroke = egui::Stroke::new(1.0, fg); + let stroke = egui::Stroke::new(1.0_f32, fg); let painter = ui.painter(); painter.line_segment([p(16.0, 9.0), p(22.0, 9.0)], stroke); painter.line_segment([p(22.0, 9.0), p(22.0, 22.0)], stroke); diff --git a/adl2rsdm/src/adl_parser.rs b/adl2rsdm/src/adl_parser.rs index 3664b67..329d55a 100644 --- a/adl2rsdm/src/adl_parser.rs +++ b/adl2rsdm/src/adl_parser.rs @@ -832,6 +832,36 @@ pub fn parse(text: &str) -> MedmScreen { /// `clr`/`bclr` indices resolve to the file's colours during this parse (MEDM /// `executeDlDisplay` → `parseAndExtractExternalColormap`, medmDisplay.c:386-427). pub fn parse_in_dir(text: &str, source_dir: Option<&Path>) -> MedmScreen { + parse_impl(text, source_dir, None) +} + +/// Parse a `.adl` that is being **embedded** into a host display, resolving every +/// `clr`/`bclr` index against `host_colormap` instead of the file's own table. +/// +/// MEDM's `parseCompositeFile` (`medm/medmComposite.c:687-700`) reads the child's +/// `display` block and its `"color map"` block with `parseAndSkip` — both are +/// discarded — and appends the child's elements straight into the **parent** +/// `displayInfo`, whose colormap therefore colours them. The child's `file` +/// block is not skipped: `medmComposite.c:684` copies its version number over, +/// which is what selects the old-format attribute rolling. +/// +/// The host table is the top-level display's, at any nesting depth: a composite +/// file that itself embeds another passes the same `displayInfo` down. +pub fn parse_embedded_in_dir( + text: &str, + source_dir: Option<&Path>, + host_colormap: &[Color], +) -> MedmScreen { + parse_impl(text, source_dir, Some(host_colormap)) +} + +/// `host_colormap` is `Some` when this document is embedded into a host display +/// that owns the colour table — see [`parse_embedded_in_dir`]. +fn parse_impl( + text: &str, + source_dir: Option<&Path>, + host_colormap: Option<&[Color]>, +) -> MedmScreen { let buf: Vec<&str> = text.lines().collect(); let blocks = locate_blocks(&buf); @@ -842,8 +872,15 @@ pub fn parse_in_dir(text: &str, source_dir: Option<&Path>) -> MedmScreen { if let Some(block) = named_block("file", &blocks) { parse_file(&block_content(&buf, block), &mut screen); } - if let Some(block) = named_block("color map", &blocks) { - screen.color_table = parse_color_map(&block_content(&buf, block)); + // An embedded document's own colour table is parsed and thrown away, as + // `parseCompositeFile` does; the host's is installed in its place. + match host_colormap { + Some(table) => screen.color_table = table.to_vec(), + None => { + if let Some(block) = named_block("color map", &blocks) { + screen.color_table = parse_color_map(&block_content(&buf, block)); + } + } } if let Some(block) = named_block("display", &blocks) { let content = block_content(&buf, block); @@ -853,7 +890,7 @@ pub fn parse_in_dir(text: &str, source_dir: Option<&Path>) -> MedmScreen { // dir, file absent, unparsable) fall to the built-in default 65-colour // palette (createDlColormap) and record the file so codegen warns. A BLANK // `cmap` falls straight to the default palette. - if screen.color_table.is_empty() { + if host_colormap.is_none() && screen.color_table.is_empty() { let cmap = locate_assignments(&content) .get("cmap") .map(|s| s.trim().to_string()) diff --git a/adl2rsdm/src/codegen.rs b/adl2rsdm/src/codegen.rs index 9e7f36e..0fff7b2 100644 --- a/adl2rsdm/src/codegen.rs +++ b/adl2rsdm/src/codegen.rs @@ -22,7 +22,7 @@ use std::collections::BTreeMap; use std::fmt::Write as _; use std::path::PathBuf; -use crate::adl_parser::{Color, Geometry, MedmScreen, MedmWidget, parse_in_dir}; +use crate::adl_parser::{Color, Geometry, MedmScreen, MedmWidget, parse_embedded_in_dir}; use crate::symbols::{self, ZLayer}; /// Maximum embedded-display nesting depth inlined at code-gen time, a backstop @@ -191,6 +191,11 @@ struct Builder { /// The convert-time `--macro` table ([`Options::macros`]); cached here so /// `emit_new` can pass it as the root instance's runtime table. macros: Vec<(String, String)>, + /// The **host** display's colour table, the single owner of every `clr`/`bclr` + /// index in this screen and in every file it embeds, at any depth: MEDM's + /// `parseCompositeFile` skips a child's own `"color map"` and parses the + /// child's elements into the parent `displayInfo` (`medmComposite.c:694-707`). + color_table: Vec, /// Converted related-display targets ([`Options::rd_modules`]); cached so /// `emit_related_display` can turn a click into an *open* of the sibling /// module's screen rather than a log line. @@ -276,6 +281,7 @@ pub fn generate(screen: &MedmScreen, options: &Options) -> Generated { macros: options.macros.clone(), rd_modules: options.rd_modules.clone(), child_module: options.child_module, + color_table: screen.color_table.clone(), ..Default::default() }; for widget in &screen.widgets { @@ -1084,8 +1090,14 @@ fn drawing_size_builder(geom: Geometry) -> String { /// `begin`/`path` angles are parsed to degrees (`beginAngle`/`pathAngle`); RsDM's /// arc keeps MEDM's X11 convention (0° at 3 o'clock, CCW positive), so the /// parsed values are used directly (no Qt-style negation). An opaque fill paints -/// a pie wedge; `outline` paints an open stroked arc. Defaults: begin 0°, span -/// 360° when the keys are absent (a degenerate arc still draws a visible sweep). +/// a pie wedge; `outline` paints an open stroked arc. +/// +/// When a key is absent MEDM keeps the value `createDlArc` seeded before +/// parsing: `begin = 0`, `path = 90*64` (`medm/medmArc.c:258-259`, reached via +/// `parseArc`'s `createDlArc(NULL)` at `medmArc.c:277`) — that is 0° and 90°, +/// the angles being stored in 1/64°. PyDM agrees: `adl2pydm` omits `spanAngle` +/// for an absent `path` (`output_handler.py:559`) and `PyDMDrawingArc` seeds +/// `_span_angle = deg_to_qt(90)` (`drawing.py:1271`). fn emit_arc(b: &mut Builder, widget: &MedmWidget, options: &Options, z: ZLayer) { let Some(geom) = widget.geometry else { skip_no_geometry(b, widget); @@ -1093,7 +1105,7 @@ fn emit_arc(b: &mut Builder, widget: &MedmWidget, options: &Options, z: ZLayer) }; let (addr, placeholder) = dynamic_channel(b, widget, options, "shape"); let begin = angle_deg(widget, "beginAngle", 0.0); - let span = angle_deg(widget, "pathAngle", 360.0); + let span = angle_deg(widget, "pathAngle", 90.0); let new_call = format!( "RsdmDrawing::new(&engine, {}, DrawingShape::Arc {{ begin_deg: {}, span_deg: {} }})", medm_str(b, &addr), @@ -2205,7 +2217,13 @@ fn emit_embedded_display(b: &mut Builder, widget: &MedmWidget, options: &Options } }; - let mut target = parse_in_dir(&text, canonical.parent()); + // The child's `clr`/`bclr` indices resolve against the HOST display's + // colormap, never its own: `parseCompositeFile` skips the child's + // `"color map"` block and parses its elements into the parent `displayInfo` + // (`medm/medmComposite.c:694-707`). `b.color_table` is the top-level + // display's, so this holds at every nesting depth, as it does in MEDM. + let host_colormap = b.color_table.clone(); + let mut target = parse_embedded_in_dir(&text, canonical.parent(), &host_colormap); // Resolve the target's channels in the embedded directory (so a nested // embedded display resolves relative to *its* file). Per MEDM // `compositeFileParse`, a non-empty macro string on the `composite file` @@ -3804,7 +3822,7 @@ fn related_display_icon(ui: &egui::Ui, rect: egui::Rect, fg: egui::Color32, bg: let side = (rect.height().min(rect.width()) - 8.0).max(4.0); let icon = egui::Rect::from_center_size(rect.center(), egui::Vec2::splat(side)); let p = |x: f32, y: f32| icon.min + egui::vec2(x, y) * (side / 25.0); - let stroke = egui::Stroke::new(1.0, fg); + let stroke = egui::Stroke::new(1.0_f32, fg); let painter = ui.painter(); painter.line_segment([p(16.0, 9.0), p(22.0, 9.0)], stroke); painter.line_segment([p(22.0, 9.0), p(22.0, 22.0)], stroke); @@ -8384,6 +8402,33 @@ image { generate(&parse(DEFERRED), &Options::default()) } + /// An `arc` block with no `begin`/`path` keeps the values `createDlArc` + /// seeded before parsing: 0° and 90° (`medm/medmArc.c:258-259`). A full + /// 360° sweep is what MEDM draws only when the file says so. + #[test] + fn arc_without_angles_falls_back_to_medms_zero_and_ninety_degrees() { + const NO_ANGLES: &str = r#" +arc { + object { + x=10 + y=10 + width=40 + height=40 + } + "basic attribute" { + clr=3 + } +} +"#; + let g = generate(&parse(NO_ANGLES), &Options::default()); + assert!( + g.source + .contains("DrawingShape::Arc { begin_deg: 0.0, span_deg: 90.0 }"), + "absent angles must not become a 360° sweep:\n{}", + g.source + ); + } + #[test] fn arc_and_polyline_emit_real_drawings_at_the_background_layer() { let g = deferred(); @@ -8644,6 +8689,83 @@ composite { let _ = std::fs::remove_dir_all(&dir); } + /// `parseCompositeFile` skips the child's `"color map"` and parses its + /// elements into the parent `displayInfo` (`medm/medmComposite.c:694-707`), + /// so `clr=1` in the child names the *host* palette's entry 1. + #[test] + fn embedded_display_colours_children_from_the_host_colormap() { + let dir = embed_tmpdir("cmap"); + // Child palette: index 1 = pure red. Host palette: index 1 = pure blue. + std::fs::write( + dir.join("child.adl"), + r#" +"color map" { + colors { + ffffff, + ff0000, + } +} +display { + object { + x=0 + y=0 + width=120 + height=24 + } + clr=1 + bclr=0 +} +text { + object { + x=4 + y=2 + width=110 + height=18 + } + textix="hi" + "basic attribute" { + clr=1 + } +} +"#, + ) + .unwrap(); + let parent = r#" +"color map" { + colors { + ffffff, + 0000ff, + } +} +composite { + object { + x=30 + y=40 + width=120 + height=24 + } + "composite file"="child.adl" +} +"#; + let options = Options { + protocol: String::new(), + source_dir: Some(dir.clone()), + ..Options::default() + }; + let g = generate(&parse(parent), &options); + assert!( + g.source.contains("Color32::from_rgb(0, 0, 255)"), + "embedded child must take the host palette's index 1 (blue):\n{}", + g.source + ); + assert!( + !g.source.contains("Color32::from_rgb(255, 0, 0)"), + "the child's own palette must be discarded, as parseAndSkip does:\n{}", + g.source + ); + let _ = std::fs::remove_dir_all(&dir); + } + #[test] fn embedded_macro_string_replaces_parent_table() { // MEDM compositeFileParse (medmComposite.c:659-668): a NON-EMPTY macro diff --git a/adl2rsdm/tests/fixtures/sample_screen.rs b/adl2rsdm/tests/fixtures/sample_screen.rs index a482781..c7a6e7d 100644 --- a/adl2rsdm/tests/fixtures/sample_screen.rs +++ b/adl2rsdm/tests/fixtures/sample_screen.rs @@ -98,7 +98,7 @@ impl Screen { .with_fill(Color32::from_rgb(255, 0, 0)) .with_size(egui::Vec2::new(60.0, 60.0)) .with_placeholder_channel(); - let w13 = RsdmDrawing::new(&engine, "loc://adl2rsdm_shape_1", DrawingShape::Arc { begin_deg: 0.0, span_deg: 360.0 }) + let w13 = RsdmDrawing::new(&engine, "loc://adl2rsdm_shape_1", DrawingShape::Arc { begin_deg: 0.0, span_deg: 90.0 }) .expect("adl2rsdm: connect loc://adl2rsdm_shape_1 (arc)") .with_fill(Color32::from_rgb(0, 255, 0)) .with_size(egui::Vec2::new(60.0, 60.0)) diff --git a/doc/parity-review-2026-07-03.md b/doc/parity-review-2026-07-03.md index e5767bc..9dc2a51 100644 --- a/doc/parity-review-2026-07-03.md +++ b/doc/parity-review-2026-07-03.md @@ -3171,6 +3171,385 @@ C reference: `medm/utils.c:3444-3459` — `performMacroSubstitutions` emits noth Impact: for `args="P=$(X)"` with `X` undefined in the parent, MEDM's child receives `P=""` (child `$(P)val` → `val`); the generated code's child receives `P="$(X)"` (child channel becomes `$(X)val`) — different child channel names and dedup key. Convert-time `substitute_macros` passthrough (the parse path) is correct and unaffected. +## Round 4 Findings (R4-1..R4-16) + +Round 4, 2026-07-10. Baseline: branch `chore/bump-epics-0.22`, HEAD `4a49b2b` +— **64 commits after the R3 baseline `0a51e4e`**, none of which had been +parity-audited: the 0.5.0–0.5.2 `VolumeRaycaster` hardening pass, the two +`rsdm` data-engine race fixes (`6ba4a4e` ca://, `c020401` calc://), and the +`epics-rs` 0.21 → 0.22.1 dependency bump (`4a49b2b`). All 133 prior findings +(R1-1..R1-40 / R2-1..R2-69 / R3-1..R3-24) are closed. + +Same 5-agent split (A: plot interaction/view, B: items/colormap/fit, +C: plot3d + VolumeRaycaster, D: rsdm↔PyDM, E: adl2rsdm↔adl2pydm+MEDM C), +reference→Rust direction. Agent-local numbers renumbered to the contiguous +R4-1..R4-15 below (A: 1–4, B: 5–6, C: 7–9, D: 10–12, E: 13–15). + +Two lenses were added this round, both carried over from the R3 convergence +verdict: + +1. **New-delta lens.** The `VolumeRaycaster` hardening and the two race fixes + were written to close CI flakes and rendering artifacts — *not* to match an + upstream contract. They were audited against the reference for the first + time here. Three of the 15 findings (R4-7, R4-9, R4-10) are divergences + *introduced* by those changes. +2. **Under-swept-fix lens.** R3 named "fix closed the cited line but not the + sibling call site in the same reference function" as the dominant residual + mechanism (10/24). R4-2 is exactly that shape relative to R1-3. + +Reference paths on this machine (the `~/codes/*` paths in the R1 header are +stale): silx `~/work/silx/src/silx`, PyDM `~/work/pydm/pydm`, adl2pydm +`~/work/adl2pydm/adl2pydm`, MEDM C `~/work/epics-extensions/medm/medm`. + +### Category A — plot interaction, view state, zoom/pan, toolbar (vs silx PlotInteraction/PlotWidget) + +### R4-1: Wheel-zoom ignores the Shift/Alt per-axis modifier override + +Severity: Medium — **FIXED** (`a1b680f`) + +Rust: `src/widget/plot_widget.rs:1053-1088` — the wheel handler builds `enabled` from only `plot.keep_aspect` and `zoom_x_enabled()`/`zoom_y_enabled()`; it never reads `ui.input(|i| i.modifiers)`. Shift+Wheel and Alt+Wheel zoom both axes identically to a plain wheel. + +Reference: `silx/gui/plot/PlotInteraction.py:1908-1918` — when keep-aspect is off, `_onWheel` overrides `enabledAxes` from the keyboard modifiers: `shiftPressed or altPressed` ⇒ `EnabledAxes(xaxis=altPressed, yaxis=shiftPressed, y2axis=shiftPressed)`, so Alt+Wheel zooms X only and Shift+Wheel zooms Y/Y2 only. `tools/menus.py:55-57` documents these bindings in the axes-menu labels ("X axis (Alt+Wheel)", "Y left axis (Shift+Wheel)"). + +Impact: the documented Alt+Wheel (X-only) and Shift+Wheel (Y-only) zoom gestures do not exist in rsplot; both modifier chords zoom both axes. + +Fix (`a1b680f`): the modifier override is now one pure rule, +`interaction::wheel_enabled_axes(keep_aspect, shift, alt, zoom_x, zoom_y)`, +mirroring `_onWheel`'s three-branch cascade exactly — keep-aspect wins, then +`shift || alt` overrides both axes from the modifiers, then the per-axis flags. +Box zoom is untouched: `_getAxesExtent` never reads modifiers either. +`wheel_enabled_axes_follows_silx_modifier_override` covers one case per branch. + +### R4-2: Programmatic `set_limits` / toolbar Zoom-In/Out skip silx's `checkAxisLimits` repair — (R1-3 sibling) + +**FIXED (R4, structural).** `set_limits_internal` is now the single owner of +every view-limits commit and runs `clamp_axis_limits` on X, Y and Y2, so the +`Transform` precondition holds by construction rather than by each caller +remembering to repair. Two bypasses the `rg` sweep found beyond the cited +sites were routed through the owner: `set_graph_y_limits(YAxis::Right)` wrote +`plot.y2` raw, and `YAxis::Extra(n)` wrote `ax.range` raw (the latter repairs +against the extra axis' own `scale`). The two hand-rolled `clamp_axis_limits` +calls in `set_x_log`/`set_y_log` — workarounds for the missing owner — were +removed, and `scale_1d_range`'s doc no longer claims the float32 clip is +"separately tracked". Boundary regression tests in `tests/view_limits_repair.rs` +(inverted / degenerate-zero / degenerate-positive / float32-overflow / y2 / +extra-axis / repeated toolbar zoom-out): 7/7 fail without the fix, pass with. +Closes R1-3's family. + +Severity: Medium + +Rust: `src/widget/high_level.rs:4160-4171` (`set_limits_internal`) forwards straight to `src/render/backend_wgpu.rs:697-700`, which writes `self.plot.limits = (xmin, xmax, ymin, ymax)` raw — no ordering swap, no degenerate expansion, no float32 clamp. The public `set_limits`, `set_graph_x_limits` (`:6797`), `set_graph_y_limits` (`:6814`), and the toolbar `actions::control::apply_zoom` (`src/widget/actions/control.rs:124-147`) all commit through this path. `scale_1d_range` (`control.rs:75-95`) omits the clamp, its doc claiming it is "the separately-tracked float32-safety zoom item", but no downstream site applies it. Contrast `high_level.rs:6921,6972`, where the log-toggle fix manually calls `clamp_axis_limits` before `set_graph_*_limits` precisely because that setter does not. + +Reference: `silx/gui/plot/PlotWidget.py:2723-2730` — `setLimits` runs `getXAxis()._checkLimits(...)` / `getYAxis()._checkLimits(...)` on every call; `items/axis.py:145-154` routes to `_utils/panzoom.py:49-75` `checkAxisLimits`, which clips both bounds to `[FLOAT32_SAFE_MIN/MINPOS, FLOAT32_SAFE_MAX]`, swaps when `vmax < vmin`, and expands when `vmax == vmin`. `applyZoomToPlot` additionally clamps inside `scale1DRange` (`panzoom.py:108-116`), giving toolbar zoom two independent float32 guards. + +Impact: a caller passing inverted (`max < min`) or equal bounds to `set_graph_x_limits`/`set_limits` gets an un-repaired collapsed/inverted view; repeated toolbar Zoom-Out grows the range past the float32-safe window with zero clamping where silx clamps at two layers. The `Transform` precondition (`min < max`, positive for log) can be violated, NaN-ing the render. + +### R4-3: Middle-button drag pan is unported; pan is bound to the right button instead + +Severity: Low — **FIXED** (`b53d26c`) + +Rust: `src/widget/plot_widget.rs:1018-1037` — pan-drag fires only for `PointerButton::Secondary` (right) or, in Pan mode, `Primary`. `PointerButton::Middle` is read only for `mouseClicked` emission (`:1512,1534`); a middle-button drag pans nothing, and the right button carries pan in every mode. + +Reference: `silx/gui/plot/PlotInteraction.py:1190-1192` — `ItemsInteraction` sets `dragButtons=(LEFT_BTN, MIDDLE_BTN)` with `clickButtons=(LEFT_BTN, RIGHT_BTN)`; `beginDrag` routes `MIDDLE_BTN` to `self._pan.beginDrag` (`:1365-1367`). Because `ZoomAndSelect`/`PanAndSelect`/`DrawSelectMode` all inherit this, middle-button drag pans in *every* interaction mode, and the right button is reserved for the click/context event. + +Impact: silx's universal middle-drag pan (pan while in zoom/draw/select mode) does not exist in rsplot; the pan gesture is relocated to the right button, which silx reserves for context/click. + +Fix (`b53d26c`): the pan condition now admits `PointerButton::Middle` in every +interaction mode, matching `ItemsInteraction.beginDrag` routing MIDDLE to +`Pan.beginDrag` (`PlotInteraction.py:1365-1367`). The pre-existing +secondary-button pan is kept — a recorded deliberate divergence +(`doc/parity-roadmap.md:1175`), not a silx binding. +`middle_drag_pans_in_every_interaction_mode` exercises Zoom, Pan, Select, +MaskDraw and RoiCreate. + +### R4-4: Reset-Zoom toolbar button never disables and its tooltip is static + +Severity: Low — **FIXED** (`d8b5fb5`) + +Rust: `src/widget/high_level.rs:6482` — the Home (Reset zoom) button is drawn unconditionally with a fixed `"Reset zoom"` tooltip; there is no gating on the X/Y autoscale flags and no dynamic tooltip. + +Reference: `silx/gui/plot/actions/control.py:82-95` — `ResetZoomAction._autoscaleChanged` calls `setEnabled(xAxis.isAutoScale() or yAxis.isAutoScale())`, greying the action out when both axes have autoscale off, and rewrites the tooltip per combination ("Auto-scale the x-axis of the graph only" / "…y-axis only" / "Auto-scale the graph"), re-evaluated on `sigAutoScaleChanged`. + +Impact: with both axes' autoscale disabled, silx shows a disabled Reset-Zoom button; rsplot shows it enabled with a generic tooltip. The click is a no-op in both, but the affordance state and the informative tooltip diverge. + +### Category B — items, colormap, ticks, fit, stats (vs silx items / silx.math) + +Both findings share one structural misunderstanding: **`silx.math.histogram.Histogramnd` defaults to `last_bin_closed=False`** (upper edge half-open, `[…, xmax[`), but the Rust ports clamp the max sample into the last bin. The pixel-intensity histogram is a **distinct, correct** site — its silx source (`actions/histogram.py:279`) passes `last_bin_closed=True` explicitly, so its inclusive behaviour must be preserved. Fixing this is not a blanket substitution. + +Fix (`d8b5fb5`): `reset_zoom_affordance(x_autoscale, y_autoscale) -> (bool, +&str)` is the whole of `ResetZoomAction._autoscaleChanged` +(`actions/control.py:83-95`): enabled iff either axis autoscales, and the +tooltip names the single autoscaling axis when exactly one is on. Note both-on +and neither-on share the generic "Auto-scale the graph" text — silx's own +`else` branch. `reset_zoom_affordance_matches_silx_enable_and_tooltip` pins all +four combinations. + +### R4-5: Colormap-dialog data histogram counts the range-max sample that silx drops + +Severity: Medium — **FIXED** (`eeca82c`) + +Rust: `src/core/histogram.rs:66-67` — the comment `// Last bin is inclusive of xmax (numpy/Histogramnd convention).` precedes `let idx = ((((t - xmin) / span) * nbins as f64) as usize).min(nbins - 1);`, folding a sample equal to `xmax` into the last bin. The test at `histogram.rs:118-127` asserts the two boundary `5.0` samples are counted (`sum == 4`), encoding the wrong upstream behaviour. + +Reference: `silx/gui/dialog/ColormapDialog.py:1288` calls `Histogramnd(data, n_bins=nbins, histo_range=data_range)` with **no** `last_bin_closed` argument, so it takes the default `False` (`silx/math/histogram.py:163`). Per that module's own doc (`histogram.py:134`), the last bin is `[…, xmax[` — a value exactly equal to `xmax` falls in **no** bin and is not counted. + +Impact: for an image whose maximum value occurs on many pixels (saturated/quantized detector frames), the top histogram bin in the colormap dialog / inline `HistogramColorBar` shows more counts than silx, and total counts exceed silx's. Both the comment and the regression test assert the wrong reference — a textbook principle-4 (test skepticism) case. + +Fix (`eeca82c`): the admission rule moved into one `bin_index(t, xmin, xmax, span, nbins) -> Option`, so the half-open extent `[xmin, xmax)` is uniform. The `span > 0.0` special case disappeared with it: a degenerate extent now admits nothing, which is what `ColormapDialog.computeHistogram` does — it has no degenerate guard (`ColormapDialog.py:1264-1288`) and `Histogramnd` rejects every coordinate at `g_max`. Bin arithmetic multiplies before dividing, as C does. Tests are per boundary of `bin_index`; 5 of 6 fail against the old binning. + +### R4-6: Scatter BINNED_STATISTIC includes the max-edge points that silx drops + +Severity: Medium — **FIXED** (`0fa3d00`) + +Rust: `src/core/scatter_viz.rs:1132-1133` docstring `matching Histogramnd's inclusive last edge`, and `scatter_viz.rs:1184-1190` — `if c >= cols { c = cols - 1; }` / `if r >= rows { r = rows - 1; }` clamp points at `x == x_max` / `y == y_max` into the last column/row. + +Reference: `silx/gui/plot/items/scatter.py:502-504` builds `Histogramnd(points, histo_range=ranges, n_bins=shape, weights=values)` with no `last_bin_closed` (default `False`). With `ranges = min_max(x/y)` (`scatter.py:489`), every point on the maximum X edge and maximum Y edge equals the range max and is dropped from all bins. + +Impact: in `Visualization.BINNED_STATISTIC` the entire rightmost column and topmost row of bins differ — rsplot's `mean`/`count`/`sum` include the max-edge points, silx's exclude them (those bins read `NaN`). For a regular grid this is a systematic full-edge discrepancy, and `BinnedStatistic::pick` returns points silx would not have binned. Corroboration: `BinnedStatistic::pick` already used the strict upper bound through `grid_cell`, so counting and picking disagreed about the max point. + +Fix (`0fa3d00`): both axes route through the now-`pub(crate)` `histogram::bin_index` — silx calls one `Histogramnd` for both sites, so the rule has one home. The `else { 1.0 }` unit-bin fallback is gone; `scale` is `span / n_bins` unconditionally, matching the bin size silx derives from the edge array (`scatter.py:504-507`), and `grid_cell` already rejects a zero scale. A degenerate axis extent therefore yields an empty grid, as in silx. `actions::analysis::pixel_intensity_histogram` was audited and left inclusive: its silx source passes `last_bin_closed=True` (`actions/histogram.py:279`). + +### Category C — plot3d + VolumeRaycaster (vs silx.gui.plot3d) + +**Framing (negative space).** silx's plot3d has **no GPU direct-volume ray-caster**. Its only volume path (`items/volume.py`) is `Isosurface` (marching-cubes geometry) + `CutPlane` (a `sampler2D` textured slice), composited with straight-alpha blending `glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)` (`scene/viewport.py:356-357`) and a straight-alpha cut-plane shader (`scene/cutplane.py:77,89`). rsplot's `VolumeRaycaster` is therefore an **rsplot-original widget with no upstream compositing contract**; its 0.5.0–0.5.2 hardening commits are internally motivated and can only be checked against the physics they invoke and against internal consistency. These three are **reference-independent defects**, not parity gaps. + +The GPU render test `tests/volume_raycaster_render.rs` asserts only two red-pixel counts (`>500` opaque, `<50` transparent) and proves nothing about compositing, correction exponent, or thickness behaviour. + +### R4-7: "Beer–Lambert" opacity correction normalizes by step count, not path length — volume opacity is independent of traversed thickness — (introduced by `4da9ddc`) + +**FIXED (R4, structural).** `c4cf5c3`. The correction exponent is now +`dt / dt_ref` with `dt_ref = length(extent) / REF_STEPS` — a *world distance*, +not a count ratio — so a chord of length `L` accumulates transmittance +`(1 - sa)^(L / dt_ref)`: a function of the traversed thickness alone, still +independent of the sample count. `REF_STEPS` matches the widget's default +`steps`, so a ray crossing the full box diagonal at defaults renders exactly as +before. + +`VolumeRaycaster` has **no silx counterpart** (`plot3d/items/volume.py` ships +only `Isosurface` and `CutPlane`; there is no GPU direct-volume ray-caster), so +this finding — like R4-8 and R4-9 — is *reference-independent*: it was found by +holding the code to the physics its own commit message invokes, not to an +upstream line. `tests/volume_opacity_physics.rs` pins that physics as two +properties: thickness dependence (analytic Beer–Lambert golden for four slab +depths) and step-count invariance. The old exponent renders all four depths at +red `225` — a literal flat silhouette; the thickness test fails against it and +the step-count test is a preservation test that passes against both. + +### R4-8: Premultiplied `Rgba8Unorm` storage cannot survive the shader's un-premultiply (`gain = sa_c / s.a`) — low-coverage voxels lose their hue — (introduced by `1c68900`) + +**RESTATED, then FIXED (R4, structural).** `8be7bb6`. + +The finding as first written was wrong on both its mechanism and its scope, and +verifying it before fixing (principle 4, applied to the review's own claims) is +what surfaced the real defect: + +- *Claimed:* `gain = sa_c / s.a` divides the quantization error by a small + `s.a`, reaching ~12% at `s.a ≈ 4/255`. **False.** For small `sa`, + `sa_c ≈ sa · (dt/dt_ref)`, so `gain ≈ alpha_scale · dt/dt_ref` — bounded near + 1–2, never `1/s.a`. The error is not amplified by the division. +- *Claimed:* at the defaults `gain == 1`, so the base render is unaffected and + the defect is conditional on `set_steps`/`set_alpha_scale`. **False.** The + defect is in the *stored* value and is unconditional. + +The real defect: the texture stores the premultiplied product `rgb · a` in 8 +bits, and `fs_main` must divide the coverage back out to recover the straight +RGB. At coverage `a` an 8-bit product resolves that quotient only to steps of +`1/a`. At `a = 3/255` — an ordinary coverage for a ray-march — an authored +`(255, 128, 0)` voxel stores green `round(128·3/255) = 2` and reads back +`2/3 = 0.667` against the authored `128/255 = 0.502`. At `a = 1/255` the +recovered RGB is only ever 0 or 1. Measured: a slab of those voxels composited +green `145` where its hue calls for `110`. + +Fix: store the same premultiplied colour in `Rgba16Float`. f16 holds every +`c·a/255²` to ~0.05% relative, so the recovered RGB is exact to well under one +8-bit step at every coverage down to `1/255`, while the filter still +interpolates *associated* colour — no return of the dark fringe `1c68900` +closed. Cost: **the volume texture takes twice the VRAM.** The same-VRAM +alternative (straight `Rgba8Unorm`, premultiplied in the shader) reopens that +fringe, so it was rejected: the division is what makes R4-7's correction +possible at all, and the format must carry it. + +Tests: `premultiply_recovers_the_straight_rgb_at_the_lowest_coverages` pins the +encoder at `a = 1..255`, `low_coverage_voxels_composite_their_authored_hue` +pins the rendered composite. Both fail against the 8-bit product. + +### R4-9: `remove()` frees a texture shared by another same-id view, silently blanking the survivor — (introduced by `a6354aa`) + +**FIXED (R4, structural).** `dd1ffbb`. Nothing owned the `VolumeGpu` entry's +lifetime: `set_volume_raycaster` created it via `entry().or_insert_with` and +`remove_volume_raycaster` destroyed it unconditionally, so *any* holder of the +id could free it under the others. The audit for this fix also found the +mirror-image bypass — a `VolumeRaycaster` that was simply dropped released +nothing, pinning its texture for the app's lifetime. + +- **Invariant:** `scenes[id]` exists exactly while at least one live + `VolumeRaycaster` holds a claim on `id`. +- **Owner/Gate:** `VolumeRaycaster`. `new` acquires a claim; `remove` and `Drop` + give it back; `set_volume` takes it again after a `remove`. The entry is + created only by `acquire_volume_raycaster` and destroyed only by the + `release_volume_raycaster` that reaches zero. +- **Bypass audit:** `rg -n 'fn remove_\w+|\.remove\(&'` over `src/render/`, + `src/widget/`. `render/volume_raycaster.rs:369` (entry creation) and `:388` + (entry destruction) are the same defect and are now the owner's two halves. + `scene3d_items.rs:2715`/`:3437` (index-keyed item removal), + `backend_wgpu.rs:689` (per-item visibility map) and the `high_level.rs` + `remove_*` family (handle-keyed item lists) are distinct: none owns a GPU + resource under a shared key. +- **Structural closure:** `set_volume_raycaster` no longer creates the entry + (`entry().or_insert_with` → `get_mut`), so an upload cannot resurrect an id + whose last holder released it; and the widget stores its `RenderState` — a + bundle of `Arc`s — so `Drop` runs the release without the caller remembering. +- **Tests:** by claim-count boundary, not by scenario. `2 → 1` (`remove` while + another view lives: the survivor must still ray-march) and `1 → 0` (drop of + the last view: a fresh view taking the id must start empty). Against the + unconditional remove they fail with `0` red px and `92416` leaked red px. + +### Category D — rsdm channels, data plugins, engine (vs PyDM) + +The `ca://` fix (`6ba4a4e`) **verifies as PyDM-faithful**: PyDM's `send_new_value` gates on `not np.array_equal(value, self._value)` and `clear_cache` on connect makes reconnect re-emit; rsdm mirrors both. The `pva` path is confirmed not in the family (`value_marked(None)` → first Data always emits, no separate connect-time post). The dep bump (`4a49b2b`) is version-only. All three D findings sit in `calc_plugin.rs`, where the audited `c020401` change lives. + +### R4-10: `calc://` publishes an initial value the update-list gate makes PyDM withhold — (introduced by `c020401`) + +**NOT FIXED — deliberate divergence, recorded.** Adopting PyDM's gate exactly +would re-open the CI flake this finding's own commit closed: PyDM's +`callback_value` drops any value callback that predates full connection and +`callback_conn` never re-arms the recompute, so PyDM's own initial calc value +is order-dependent — with `loc://` children (which post their initial value at +connect, `local_plugin.rs:84`) it is a race. That is a reference-side quirk of +the kind `port-translation-lessons` Lesson 1 says a parity audit cannot +adjudicate, not a contract worth importing. rsdm keeps its eager initial +evaluation: the first all-connected tick always evaluates. The R4 trigger +rework preserves this behaviour structurally (a queued pre-connect value event +is held, not collapsed) instead of via the `u64::MAX` sentinel `c020401` used. +Documented at `calc_plugin.rs` `initial_eval_done`. + +Severity: Medium + +Rust: `rsdm/src/data_plugins/calc_plugin.rs:303-330` — the fix leaves `prev_stamps` at `u64::MAX` until the first all-connected tick, so that tick sees every child's stamp as changed; if any update-listed child has a value, `trigger` is set (`:305-313`) and `evaluate` publishes (`:316-329`). The commit frames "connected but valueless until the next child write" as a bug and makes the initial eval hold by construction. + +Reference: `pydm/data_plugins/calc_plugin.py:141-142` — `callback_value` sets `self._calculate` (the only recompute trigger) **only** when `self.connected` AND `name in self.listen_for_update`; `callback_conn` (`:144-157`) never sets it. PyDM's initial evaluation therefore happens only when an update-listed child's *value callback* fires *after* all children are connected. + +Impact: when the last child to connect is not in the `update` list and the update-var's only value callback predates that connection — the exact scenario of the new `calc_publishes_initial_value_when_update_var_connects_before_others` regression test — PyDM does **not** evaluate; the calc channel stays connected with value `None` until the update-var next changes. rsdm now publishes an initial value there. A strip chart on the calc gets a first sample PyDM would not deliver. The CI flake was closed by diverging from PyDM's documented "skip until a listened var updates" gate. + +### R4-11: `calc://` does not re-emit its value on a child connection-state change + +**FIXED (R4, structural).** The poll loop tracks per-child connection state and, +on any change, re-emits the last computed value via `post_value` (or, when no +value has been computed yet, updates the `connected` flag only) — PyDM +`callback_conn` → `_send_update(self.connected, self._value)` with +`receive_new_data`'s `None` skip. Regression: +`child_connection_change_reemits_the_last_value` (fails on the old code). + +Severity: Low + +Rust: `rsdm/src/data_plugins/calc_plugin.rs:282-284` — a child connect/disconnect changes `all_connected`, published with `writer.update(move |s| s.connected = all_connected)` (a connection-only update, no value event). A calc value reaches subscribers only through the separate stamp-triggered `post_value` recompute (`:316-329`), which fires only for an update-listed child whose stamp moved. + +Reference: `pydm/data_plugins/calc_plugin.py:156-157` — `callback_conn` runs `self._send_update(self.connected, self._value)` on **every** child connection-state change, and `receive_new_data` (`:236-240`) re-emits `new_value_signal[type(val)]` whenever that carried value is not `None`. + +Impact: on any child reconnect (or a non-update-var child connecting), PyDM re-emits the calc's last computed value to value subscribers; rsdm emits nothing (only the `connected` flag flips). A strip chart on a calc channel misses the resample PyDM injects on child connection churn. + +### R4-12: `calc://` retriggers on a child's alarm/metadata change, not just its value + +**FIXED (R4, structural).** The trigger primitive was replaced: the poll loop +now drains a per-child `Channel::subscribe_values` queue instead of sampling +`Channel::stamp()`. `StateWriter::update` (alarm, metadata, connection) posts +no value event — an invariant already pinned by +`channel.rs::update_does_not_emit_a_value_event` — so an alarm-only or +DBE_PROPERTY callback can no longer recompute or advance a `prev_res` +accumulator. This is what PyDM gets for free by binding only `value_slot`. +The same change makes the trigger durable (a queue, not a collapsing counter), +which is why R4-10's sentinel could be deleted. Regression: +`alarm_only_child_update_does_not_retrigger_the_calc` (fails on the old code). + +Severity: Medium + +Rust: `rsdm/src/data_plugins/calc_plugin.rs:305-313` — the trigger keys on `ch.stamp()`. But `Channel::stamp()` (`channel.rs:580-582`) returns `state.stamp`, and `StateWriter::update` bumps that stamp for **every** state change (`channel.rs:377-384`), including alarm-only and metadata-only updates. A CA child's DBE_ALARM callback publishes via `writer.update(move |s| apply_alarm(s, &snap))` (`ca_plugin.rs:273`) and its DBE_PROPERTY refetch via `writer.update` (`ca_plugin.rs:288/423`) — both bump the child stamp with no value change. Each then trips `trigger` and runs `post_value` (`calc_plugin.rs:316-329`), which emits unconditionally (no `value != prev_value` guard). + +Reference: `pydm/data_plugins/calc_plugin.py:84-89` — the calc's child `PyDMChannel` binds only `connection_slot` and `value_slot`, not the severity/unit/prec/limit slots. `callback_value` (`:121-142`) is the sole path that sets `self._calculate`, and it fires only on a real value emission (`new_value_signal`). A child's alarm-severity or ctrl-metadata change never reaches the calc's recompute. + +Impact: a calc over CA children recomputes and republishes on every DBE_ALARM (alarm deadband) and DBE_PROPERTY (units/precision/limit) callback of a triggering child, leaking duplicate samples into any strip chart on the calc. For an accumulator expression using `prev_res` (e.g. `prev_res + A`), each alarm-only child callback **double-counts** — a correctness divergence, not a cosmetic one, since PyDM never advances the accumulator on a non-value change. + +### Category E — adl2rsdm parser/codegen/CALC (vs adl2pydm + MEDM C) + +### R4-13: Dynamic-attribute CALC binds only channels A–D; MEDM's reserved metadata operands G–L are unbound + +Severity: Medium — **NOT A DEFECT (withdrawn)**; it did surface a real one, filed below as R4-16 + +Rust: `adl2rsdm/src/codegen.rs:371-376` — `VIS_CHANNEL_KEYS` has exactly four entries (`chan→A`, `chanB→B`, `chanC→C`, `chanD→D`), and `visibility_gate_address` (`:438-459`) emits `calc://…?dialect=medm&expr=&A=…&B=…&C=…&D=…&update=…`. Only A–D are ever bound; a `vis="calc"` expression referencing E–L gets nothing for them. + +Reference: `medm/utils.c:4491-4505` (MEDM C, authoritative). In `calcVisibility`'s `V_CALC` arm MEDM fills a 12-slot `valueArray`: A–D from the four channel records, then `valueArray[4]=0` (E), `[5]=0` (F), `[6]=pr->elementCount` (G), `[7]=pr->hopr` (H), `[8]=pr->status` (I), `[9]=pr->severity` (J), `[10]=pr->precision` (K), `[11]=pr->lopr` (L) — the metadata of the *first* channel (record A). These are the standard EPICS calc-record letters and are documented, usable visibility operands. + +Impact (as filed): a common alarm-driven visibility rule such as `calc="J"` (show when severity ≠ 0) or `calc="I#0"` (status) evaluates with J/I unbound — the rsdm `dialect=medm` engine has no value for those letters, so the gate is permanently false (or fails to compile → hidden). + +**Withdrawn.** The premise is false. `E`–`L` are not channels, so `VIS_CHANNEL_KEYS` is right to carry only `chan`/`chanB`/`chanC`/`chanD`; the *plugin* fills the metadata operands. `eval_medm` (`rsdm/src/data_plugins/calc_plugin.rs`) already binds `G` = element count, `H` = hopr, `J` = severity, `K` = precision, `L` = lopr from the first record, exactly as `utils.c:4498-4505` does, and `medm_calc::compile` accepts a bare `J`. Only `I` (EPICS alarm *status*) is unbound, at `0.0` — a gap already documented in the module docs, because `ChannelState` carries no status code. Principle 4 (test skepticism) applies to review findings too: the cited Rust lines were read without reading the consumer that gives them meaning. + +What the check did surface is a live defect on the trigger side, filed as **R4-16**. + +### R4-14: Composite-file / embedded-display children are coloured by the child file's own colormap; MEDM resolves them against the parent display's colormap + +Severity: Medium — **FIXED** (`fb21fb6`) + +Rust: `adl2rsdm/src/codegen.rs:2208` — `emit_embedded_display` does `let mut target = parse_in_dir(&text, canonical.parent());`, so the child `.adl`'s own inline `"color map"` block is parsed and every child widget's `clr`/`bclr` index is resolved against *that* table (`:2244-2265`). Each embedded file uses its own colormap. + +Reference: `medm/medmComposite.c:694-707` (MEDM C, authoritative). `compositeFileParse` reads the child's `"color map"` token and calls `parseAndSkip(displayInfo)` — the child colormap is deliberately **discarded**. The children are appended into the parent `displayInfo->dlElementList`, so their colour indices resolve against the **parent display's** colormap at draw time. + +Impact: when a host screen uses a non-default colormap (inline `"color map"` or an external `cmap`) and embeds a child carrying a different (or the default) colormap, every embedded widget's `clr`/`bclr` renders with the wrong colours — MEDM indexes the host palette, adl2rsdm indexes the child's. Screens on the default palette are unaffected; facility screens with custom palettes are. + +Fix (`fb21fb6`): the host colormap became the single owner. `Builder` carries +`color_table` (the top-level screen's palette), and `emit_embedded_display` +parses the child through the new `parse_embedded_in_dir(text, dir, +host_colormap)`, which installs the host table and never consults the child's +inline `"color map"` or `cmap` fallback — exactly `parseAndSkip`. Because the +table flows from the `Builder`, nested embeds inherit the top-level palette at +every depth, as MEDM's single `displayInfo` does. Regenerating +`adl2rsdm/examples/local_panel_screen.rs` surfaced the divergence directly: +`embed_child.adl`'s `text update` carries `clr=3`, which its own three-entry +palette lacks (so the old code emitted no colour) and the host palette resolves +to `0000ff`. `embedded_display_colours_children_from_the_host_colormap` fails +without the fix. + +### R4-15: Absent arc `path` defaults to a 360° span; MEDM's default is 90° + +Severity: Low — **FIXED** (`33dee07`) + +Rust: `adl2rsdm/src/codegen.rs:1096` — `let span = angle_deg(widget, "pathAngle", 360.0);`. When the `.adl` arc has no `path` key, the emitted `DrawingShape::Arc` gets `span_deg: 360` (a full circle); `begin` defaults to 0 (correct). + +Reference: `medm/medmArc.c:258-259` (MEDM C, authoritative). `createDlArc` initialises `dlArc->begin = 0; dlArc->path = 90*64;` — the default span for an arc with no `path` key is **90°**. (adl2pydm defaults `pathAngle` to 0 and then omits the span entirely — also wrong, differently.) + +Impact: a hand-authored or truncated `.adl` arc omitting `path` draws a full 360° disc instead of MEDM's 90° wedge. MEDM's own writer always emits `path`, so MEDM-generated files never hit this; the divergence is confined to the absent-key contract. + +### R4-16: MEDM-dialect calc reading operand `J` never re-evaluates on a severity change + +Severity: Medium — **FIXED** (`3f9660e`). Introduced by `976c39c` (this round's R4-11/R4-12 fix); never published. + +Rust: `rsdm/src/data_plugins/calc_plugin.rs` — `run_channel` arms `pending_trigger` only from per-child `ValueSubscription` drains. `Channel::update` posts no value event, so a severity-only change reaches no trigger. Under `dialect=medm` the operands `E`–`L` are record *metadata*, so `eval_medm` would read a fresh severity that nothing asks it to read. + +Reference: `medm/utils.c:4621-4630` — under `V_CALC` `setDynamicAttrMonitorFlags` sets `monitorValueChanged` on the main record, then `monitorStatusChanged` **iff** `calcUsesStatus(attr->calc)` and `monitorSeverityChanged` **iff** `calcUsesSeverity(attr->calc)` (`utils.c:4647-4694`). `medmUpdateChannelCb` (`medmCA.c:1034-1048`) calls `updateValueCb` on a severity change only when that flag is set. MEDM never monitors `hopr`/`precision`/`lopr`, so H/K/L do not retrigger. + +Impact: `vis="calc"` with `calc="J"` (the standard "show while in alarm" gate) latched at the severity observed on the first evaluation. The pre-`976c39c` stamp poll got this right by accident, while wrongly retriggering on H/K/L churn; the value-only rule got it wrong in the other direction. Neither matched MEDM's per-operand monitor set. + +Fix (`3f9660e`, completed by `d3f1e19`): `Evaluator::Medm` gains `first_child` +and `monitors: MedmMonitors`, both decided once at connect as MEDM decides them +once in `setDynamicAttrMonitorFlags`; the loop arms `pending_trigger` on a +*change* of the monitored fields. `calc_uses_operand` ports MEDM's letter scan +verbatim (either case, not preceded by an ASCII letter — the guard that keeps +`sin`/`min`/`nint`/`pi` from reading as operand `I`). + +`3f9660e` closed only the severity half: `ChannelState` carried no EPICS alarm +status, so operand `I` bound a constant `0.0` and `monitorStatusChanged` had +nothing to watch. `d3f1e19` closes the status half at source — both wire types +already carry it (`epics-ca-rs` `snap.alarm.status`, `epics-pva-rs` +`alarm.status`), so `ChannelState::status` lands beside `severity` and is +written at the same three plugin sites. With both operands available the flag +pair collapses into one owner, `MedmMonitors`, whose `sample` reads only the +monitored fields — an unmonitored field stays `None` and cannot arm a +recompute, so the monitor set gates the comparison by construction rather than +by a branch at the read. The PyDM dialect builds no monitors at all: it has no +metadata operands. + +Boundary tests: `I` monitored, `J` monitored but not `I` (a status change must +stay inert — passes against the old code, a preservation test), neither +monitored, and `MedmMonitors::of` against `sin`/`nint`. + ## Cleared During Review Fix round 2026-07-03/04 (one commit per finding; branch merged fast-forward @@ -3663,3 +4042,145 @@ implemented; this whole cluster is now closed):** under-swept fixes (10/24), which a fixes-from-reported-defects `rg`-the-whole-reference-function discipline on the next fix round would close as a class rather than one site at a time. + +- 2026-07-10: **round 4 opened**; same 5 read-only agents (A: plot + interaction/view/toolbar, B: items/colormap/ticks/fit/stats, C: plot3d + + VolumeRaycaster, D: rsdm↔PyDM, E: adl2rsdm↔adl2pydm+MEDM C). Baseline + `4a49b2b` (branch `chore/bump-epics-0.22`), **64 commits after the R3 + baseline `0a51e4e`** — the 0.5.0–0.5.2 VolumeRaycaster hardening, the two + rsdm race fixes, and the epics-rs 0.21→0.22.1 bump, none previously + audited. Two lenses added: a **new-delta lens** (audit the unaudited 64 + commits) and the **under-swept-fix lens** the R3 verdict called for. + Reference paths corrected to `~/work/*` (the `~/codes/*` paths in the R1 + header do not exist on this machine). + +- 2026-07-10: round 4 consolidated — **15 findings** (High 0, Medium 9, + Low 6), renumbered R4-1..R4-15 (A: 1–4, B: 5–6, C: 7–9, D: 10–12, + E: 13–15). + + Thematic clusters: + - **The new deltas introduced three of the fifteen.** R4-7 (step-count, + not path-length, opacity normalization — and at the default `steps=256` + the correction is the identity), R4-9 (per-id VRAM free collides with + the documented shared-id design, silently blanking a live view), R4-10 + (`calc://` now publishes an initial value PyDM withholds). All three + were written to close a CI flake or a rendering artifact, none against + a reference. The lesson is not "the fixes were wrong" but that + *flake-driven and artifact-driven changes bypass the parity contract by + construction* and must be audited as new surface. + - **`VolumeRaycaster` has no upstream contract at all.** silx's plot3d + ships Isosurface + CutPlane with straight-alpha blending and no GPU + ray-caster. R4-7/8/9 are therefore **reference-independent defects**, + not parity gaps — the audit direction cannot help here, and the two + red-pixel-count assertions in `tests/volume_raycaster_render.rs` prove + nothing about compositing. This surface needs physics-level golden + tests (analytic transmittance for a known chord), not more parity + rounds. + - **`Histogramnd` upper-edge convention inverted (rsplot):** R4-5 + R4-6 + both clamp the max sample into the last bin, and *both* carry a comment + asserting the opposite of what silx does; R4-5's regression test + encodes the wrong reference. One invariant — "`Histogramnd`'s upper + edge is exclusive unless `last_bin_closed=True`" — closes both. The + `pixel_intensity_histogram` site is genuinely inclusive (its silx + source passes `last_bin_closed=True`) and must NOT be swept with them. + - **Under-swept fix confirmed once more:** R4-2 is R1-3's sibling. R1-3 + repaired the context-menu reset path's degenerate `(v,v)`; the + programmatic `set_limits`/`set_graph_*_limits` and toolbar zoom paths + still commit raw limits with no swap/expand/float32 clamp, where silx + applies `checkAxisLimits` on *every* `setLimits` and clamps toolbar + zoom at a second layer. One owner for "commit a view-limits change" + would close R1-3 + R4-2 together. + - **`calc://` trigger model is the wrong primitive (rsdm):** R4-10, R4-11 + and R4-12 are three faces of one thing — the poll loop triggers on + `Channel::stamp()`, but `stamp` means "any state change" (value, alarm, + metadata), whereas PyDM's calc triggers on a *value emission* of a + listened child and re-emits on *connection* change. Polling a + monotonic counter cannot express either. A value-event subscription per + child (the primitive `subscribe_values` already provides) would close + all three structurally rather than by three more gates. + - **adl2rsdm: the reference is MEDM, not adl2pydm.** R4-13 (calc letters + G–L unbound) and R4-15 (arc `path` absent-key default) are gaps + adl2pydm *shares*; only reading the MEDM C surfaced them. R4-14 + (embedded child uses its own colormap) is the remaining half of the + `compositeFileParse` feature R3-16/17 opened. + + Classification (per port-translation-lessons): + - **Reference-independent defects** (real regardless of upstream): + R4-7, R4-8, R4-9 (VolumeRaycaster — no upstream exists), R4-12 (calc + accumulator double-counts on alarm-only callbacks), and R4-2's + consequence (raw limits can violate the `Transform` precondition and + NaN the render). + - **Reference-faithful gaps** (adopt upstream posture): R4-2, R4-4, R4-5, + R4-6, R4-10 (port is *more eager* than PyDM), R4-11 (port is *more + conservative* than PyDM). + - **Interop-contract gaps** (the `.adl` format is the contract): R4-13, + R4-14, R4-15. + - **Unimplemented surface** (port, or record a scope decision): R4-1 + (Shift/Alt wheel modifiers), R4-3 (middle-drag pan). + + **Convergence verdict:** R1 40 → R2 69 → R3 24 → R4 15. Count still + falling, severity ceiling still Medium (0 High for the second round + running). But the round is NOT evidence of convergence on the *audited* + surface: 3 of 15 findings are defects the last 64 commits *introduced*, + and one whole surface (`VolumeRaycaster`) turned out to have no reference + to converge against. Per port-translation-lessons this is cycle 4 of an + expected 5–6 before the language-gap categories close structurally. The + two structural closures that would collapse the largest families are + (a) a single owner for view-limits commits (R1-3 + R4-2), and (b) + replacing `calc://`'s stamp-polling trigger with per-child value-event + subscription (R4-10 + R4-11 + R4-12). + + **Process note:** the reference trees were absent at the paths recorded in + the R1 header (`~/codes/*`); they live under `~/work/*` on this machine. + Categories D and E were nearly skipped for want of PyDM/adl2pydm — the + audit was held until the user supplied them rather than proceeding on + assumption. Keep the corrected paths in the R4 header current. + +### Round 4 fix phase — 2026-07-10 + +All 16 R4 findings are closed. 13 fixed at source, 1 withdrawn, 2 recorded as +deliberate divergences. One commit per finding, each preceded by the `rg` anchor +sweep over its structural anchor. + + Fixed: R4-1 `a1b680f`, R4-2 `78b11e4`, R4-3 `b53d26c`, R4-4 `d8b5fb5`, + R4-5 `eeca82c`, R4-6 `0fa3d00`, R4-7 `c4cf5c3`, R4-8 `8be7bb6`, + R4-9 `dd1ffbb`, R4-11 + R4-12 `976c39c`, R4-14 `fb21fb6`, + R4-15 `33dee07`, R4-16 `3f9660e` + Withdrawn: R4-13 (not a defect) + Deliberate divergence, recorded: R4-10, and MEDM's `monitorStatusChanged` + half of R4-16 (rsdm's `ChannelState` carries no EPICS status code, so + operand `I` binds 0.0) + +**Two findings were wrong, and verifying them before fixing is what found the +real defects.** Principle 4 (test skepticism) applies to the review's own +claims, not only to the port's tests: + +- **R4-13** claimed `dialect=medm` never binds operands G–L. `eval_medm` + already binds them from the first record. Withdrawn — but checking the + operand set against `setDynamicAttrMonitorFlags` surfaced R4-16, a real + defect the audit had missed, in the code the previous fix (`976c39c`) had + just written. +- **R4-8** claimed the low-alpha error came from `gain = sa_c / s.a` dividing + quantization error by a small `s.a`, and that the defaults were unaffected. + Both false: `gain ≈ alpha_scale · dt/dt_ref` is bounded near 1–2, and the + defect lives in the 8-bit *storage* of the premultiplied product, where it is + unconditional. The restated defect is more severe than the one reported. + +**`VolumeRaycaster` has no upstream counterpart.** silx `plot3d/items/volume.py` +ships an `Isosurface` and a `CutPlane`; there is no GPU direct-volume +ray-caster anywhere in silx. R4-7, R4-8 and R4-9 are therefore +*reference-independent* findings — they were found by holding the code to the +physics its own commit messages invoke (Beer–Lambert; premultiplied +compositing) and to its own documented contract (same-id views share a +texture), not to an upstream line. Category C's real yield this round was that +its "parity" audit had no parity to audit, and the physics golden that replaced +it (`tests/volume_opacity_physics.rs`) is what the next round should extend. + +**Structural closures landed.** The two the convergence verdict named: +(a) `set_limits_internal` as the single owner of every view-limits commit +(R1-3 + R4-2), and (b) `calc://` replacing stamp-polling with per-child value +subscriptions (R4-11 + R4-12), which R4-16 then extended to the severity +operand. A third emerged during the fix phase: `bin_index` as the single home +of the `Histogramnd` admission rule, shared by the colormap-dialog histogram +and the scatter binned-statistic grid (R4-5 + R4-6). A fourth closed the +`VolumeId` texture lifetime under one claim-counted owner (R4-9). diff --git a/examples/picking.rs b/examples/picking.rs index 8876529..1c17d83 100644 --- a/examples/picking.rs +++ b/examples/picking.rs @@ -62,7 +62,7 @@ impl eframe::App for PickingApp { { let painter = ui.painter(); let c = out.transform.data_to_pixel(pick.x, pick.y); - painter.circle_stroke(c, 9.0, Stroke::new(2.0, Color32::WHITE)); + painter.circle_stroke(c, 9.0, Stroke::new(2.0_f32, Color32::WHITE)); painter.text( c + vec2(12.0, -12.0), Align2::LEFT_BOTTOM, diff --git a/rsdm/Cargo.toml b/rsdm/Cargo.toml index ee046c4..20f6211 100644 --- a/rsdm/Cargo.toml +++ b/rsdm/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rsdm" -version = "0.5.2" +version = "0.5.3" edition = "2024" rust-version = "1.92" license = "MIT OR Apache-2.0" @@ -53,7 +53,7 @@ calc = ["dep:evalexpr", "dep:epics-base-rs"] # `version` (alongside `path`) is required so `cargo publish` records a # crates.io dependency on the published rsplot; the path is used for local # workspace builds. Keep this in lockstep with rsplot's version. -rsplot = { path = "..", version = "0.5.2" } +rsplot = { path = "..", version = "0.5.3" } # Async runtime for the data engine. The connection tasks live on a tokio # runtime the `Engine` owns; widgets read shared state on the GUI thread. `net` # is enabled so `enable_all()` (and the EPICS backends, later) work. @@ -78,20 +78,20 @@ image = { version = "0.25", default-features = false, features = [ "tiff", ] } -# EPICS Channel Access backend (feature `ca`). crates.io 0.21.x — an explicit +# EPICS Channel Access backend (feature `ca`). crates.io 0.22.x — an explicit # deviation from rsplot's no-new-dependency rule, scoped to this crate. -epics-ca-rs = { version = "0.21", optional = true } +epics-ca-rs = { version = "0.22", optional = true } # `Snapshot`/`AlarmInfo`/`DisplayInfo`/`ControlInfo`/`EnumInfo`/`DbrClass`/ # `EpicsValue` returned by the CA client live here and are not re-exported by -# epics-ca-rs, so this is a direct dependency. Unified to the same 0.21.x that +# epics-ca-rs, so this is a direct dependency. Unified to the same 0.22.x that # epics-ca-rs pins, so there is one copy in the tree. -epics-base-rs = { version = "0.21", optional = true } -# pvAccess backend (feature `pva`). crates.io 0.21.x, same explicit deviation +epics-base-rs = { version = "0.22", optional = true } +# pvAccess backend (feature `pva`). crates.io 0.22.x, same explicit deviation # from rsplot's no-new-dependency rule, scoped to this crate. The NT # value/structure types (`PvField`/`PvStructure`/`ScalarValue`/`NTScalar`) are # re-exported by this crate, so no extra epics-base-rs surface is pulled in for # `pva` alone. -epics-pva-rs = { version = "0.21", optional = true } +epics-pva-rs = { version = "0.22", optional = true } # NTNDArray codec decompression (feature `pva`, `pva_codec.rs`): raw-LZ4-block # and blosc/bitshuffle LZ4 sub-streams decode with lz4_flex — the same pure-Rust # block codec rust-hdf5's `lz4` feature uses (rust-hdf5's public filter API diff --git a/rsdm/src/channel.rs b/rsdm/src/channel.rs index 2647021..2d970bc 100644 --- a/rsdm/src/channel.rs +++ b/rsdm/src/channel.rs @@ -173,6 +173,12 @@ pub struct ChannelState { /// Wire alarm severity (see [`ChannelState::effective_severity`] for the /// value to use when styling). pub severity: AlarmSeverity, + /// Wire alarm status: the record's `STAT` field for `ca://` (`DBR_STS_*` + /// `status`), and the pvAccess `alarm.status` enum for `pva://` — two + /// different code spaces, each reported as its channel type sends it. `0` + /// means no alarm in both. Read by the MEDM calc operand `I` + /// (`medm/utils.c` `calcVisibility`), which is a `ca://` concept. + pub status: i16, /// Enumeration strings (CA `DBR_GR/CTRL_ENUM`, pvAccess enum), if any. pub enum_strings: Option>, /// Engineering units (`EGU`). @@ -598,6 +604,22 @@ impl Channel { } } +/// Build a `(Channel, StateWriter)` pair on a standalone connection with a +/// dangling pool weak (the pool prune in `Drop` is a no-op), for tests that +/// drive the value-event path without an [`crate::Engine`]. The write queue, +/// listener channel and cancel token are dropped here; the connection owns what +/// it needs, and no plugin task is listening. +#[cfg(test)] +pub(crate) fn channel_pair(address: &str) -> (Channel, StateWriter) { + let (conn, writer, _writes, _listeners, _cancel) = Connection::new( + crate::address::PvAddress::parse(address), + RepaintHook::default(), + Weak::new(), + address.to_owned(), + ); + (Channel::new(conn), writer) +} + #[cfg(test)] mod tests { use super::*; @@ -676,19 +698,10 @@ mod tests { assert_eq!(s.stamp, 0); } - /// Build a connected `(Channel, StateWriter)` pair with a dangling pool weak - /// (the pool prune in `Drop` is a no-op) for exercising the value-event path. + /// The value-event tests all use one address; the crate-internal helper is + /// parameterized because the calc plugin's tests need distinct children. fn channel_pair() -> (Channel, StateWriter) { - let (conn, writer, _writes, _listeners, _cancel) = Connection::new( - crate::address::PvAddress::parse("loc://value_events"), - RepaintHook::default(), - Weak::new(), - "loc://value_events".to_owned(), - ); - // Keep the write queue / cancel token alive for the test's lifetime by - // leaking them into the Channel's connection (they live on `conn`); the - // returned receivers are only dropped here, which is harmless. - (Channel::new(conn), writer) + super::channel_pair("loc://value_events") } fn drain_values(sub: &ValueSubscription) -> Vec { diff --git a/rsdm/src/data_plugins/calc_plugin.rs b/rsdm/src/data_plugins/calc_plugin.rs index 4e22eba..81f162c 100644 --- a/rsdm/src/data_plugins/calc_plugin.rs +++ b/rsdm/src/data_plugins/calc_plugin.rs @@ -70,8 +70,14 @@ //! - Operands `E`–`L` not bound to an explicit child are record metadata of the //! **first** channel (operand `A`, MEDM `records[0]`; `utils.c:4498-4505`): //! `E`,`F` = 0, `G` = element count, `H` = hopr, `I` = alarm status, -//! `J` = severity, `K` = precision, `L` = lopr. rsdm's `ChannelState` carries -//! no EPICS alarm-status code, so `I` binds `0.0` (documented gap). +//! `J` = severity, `K` = precision, `L` = lopr. +//! - Because `I` and `J` are metadata, they change with no value event, and an +//! expression reading either must re-evaluate on that change. MEDM arms +//! exactly those two monitors and no others: `setDynamicAttrMonitorFlags` +//! (`utils.c:4621-4630`) sets `monitorStatusChanged` iff `calcUsesStatus(calc)` +//! and `monitorSeverityChanged` iff `calcUsesSeverity(calc)`, and never +//! monitors `hopr`/`precision`/`lopr` (H/K/L). [`MedmMonitors`] is that +//! decision, made once at connect. //! - The `expr` query value is **percent-decoded** (`%26` → `&`, `%25` → `%`), //! because the raw query splits on `&`; `adl2rsdm` encodes exactly those two //! bytes. The plain (PyDM) dialect stays raw — PyDM does not decode either. @@ -87,10 +93,20 @@ //! //! **No async wake on child updates.** The snapshot model publishes child values //! through an `Arc>` + an egui repaint, not a tokio waker, -//! so the connection task **polls** each child's update `stamp` on a fixed -//! interval and recomputes when a triggering variable changed. This is the -//! right-sized fit for a handful of children at GUI rates; a notification -//! subsystem in `channel.rs` for this one plugin would not be. +//! so the connection task **drains** each child's value-event queue +//! ([`Channel::subscribe_values`]) on a fixed interval and recomputes when a +//! triggering variable emitted a value. It subscribes to value events rather +//! than sampling `Channel::stamp()` because `stamp` bumps on *any* state change +//! — an alarm-only or metadata-only update would otherwise recompute and +//! republish, double-counting a `prev_res` accumulator. PyDM binds only +//! `connection_slot`/`value_slot` on its calc children (`calc_plugin.py:84-89`), +//! so alarm severity and ctrl metadata never reach its recompute either. The +//! queues additionally make the trigger durable: an event arriving before the +//! last child connects waits in the queue instead of collapsing into a counter. +//! +//! A child connection change re-emits the last computed value, matching PyDM's +//! `callback_conn` → `_send_update(self.connected, self._value)` +//! (`calc_plugin.py:156-157`). //! //! The plugin has no [`Engine`] handle of its own; the engine injects a //! [`ChildConnector`] (capturing a `Weak` to the engine internals) so opening a @@ -112,15 +128,20 @@ use tokio::sync::mpsc; use tokio_util::sync::CancellationToken; use crate::address::PvAddress; -use crate::channel::{Channel, PvValue, StateWriter}; +use crate::channel::{AlarmSeverity, Channel, PvValue, StateWriter, ValueSubscription}; use crate::data_plugins::{ConnectionCtx, DataPlugin}; use crate::engine::EngineError; -/// How often the connection task polls its children for new values. Children -/// publish via a shared state cell + egui repaint (no async waker), so the calc -/// task samples their update stamps at this cadence. +/// How often the connection task drains its children's value events. Children +/// publish via a shared state cell + queued value events (no async waker), so +/// the calc task drains those queues at this cadence. const POLL_INTERVAL: Duration = Duration::from_millis(50); +/// Per-child value-event queue depth. The task only asks "did this child emit a +/// value since the last tick", so overflow costs nothing; the depth exists to +/// bound memory for a child that publishes far faster than [`POLL_INTERVAL`]. +const VALUE_QUEUE_CAP: usize = 16; + /// Opens a child channel by address, returning the live [`Channel`]. The engine /// supplies one that captures a `Weak` to its internals, so the plugin can open /// children through the normal pool/plugin path without keeping the engine @@ -196,6 +217,13 @@ enum Evaluator { Medm { compiled: medm_calc::CompiledExpr, operand_indices: Vec, + /// Index into `children` of the record whose metadata backs operands + /// `E`–`L` (MEDM `records[0]`): operand `A` when a child binds it, else + /// the first listed child. `None` only when there are no children. + first_child: Option, + /// The first record's metadata this expression puts a monitor on, + /// beyond the always-present value monitor. + monitors: MedmMonitors, }, /// MEDM dialect whose expression failed to compile (or a variable is not a /// single `A`–`U` letter): fail-visible — `1.0` was published at task @@ -221,10 +249,19 @@ impl Evaluator { }; operand_indices.push(idx); } + // MEDM's `records[0]`: operand A when bound, else the first + // listed child (MEDM requires channel A for a valid calc — + // `utils.c:4543` — so A is the normal case). + let first_child = operand_indices + .iter() + .position(|&idx| idx == 0) + .or((!config.vars.is_empty()).then_some(0)); match medm_calc::compile(&config.expr) { Ok(compiled) => Self::Medm { compiled, operand_indices, + first_child, + monitors: MedmMonitors::of(&config.expr), }, Err(err) => { log::warn!( @@ -263,11 +300,57 @@ async fn run_channel( // Warn at most once per connection when evaluation errors (fail-visible). let mut warned_eval = false; - // `u64::MAX` can never equal a real stamp, so the first poll after a child - // first publishes registers as a change and triggers the initial eval. - let mut prev_stamps = vec![u64::MAX; children.len()]; - let mut connected = false; + // Trigger on *value events*, not on `Channel::stamp()`. `stamp` bumps on + // every state change — including alarm-only (DBE_ALARM) and metadata-only + // (DBE_PROPERTY) updates — so polling it recomputed and republished on a + // child's alarm churn, double-counting `prev_res` accumulators. PyDM binds + // only `connection_slot`/`value_slot` on its calc children + // (`calc_plugin.py:84-89`), so a child's alarm severity or ctrl metadata + // never reaches the recompute. A per-child value subscription reproduces + // that by construction: `update` (metadata) posts no value event. + // + // The queues also make the trigger durable. `stamp` is a collapsing + // counter, so a child's initial value landing before the last child + // connected had to be preserved with a sentinel; a queue simply holds the + // event until the tick that can act on it, whatever the child-connect vs + // poll-tick interleaving. + let subs: Vec = children + .iter() + .map(|(_, ch)| ch.subscribe_values(VALUE_QUEUE_CAP)) + .collect(); + // Which children may arm a recompute: the `update` list, or all of them. + let triggers: Vec = children + .iter() + .map(|(name, _)| update.as_ref().is_none_or(|u| u.iter().any(|n| n == name))) + .collect(); + + // Under the MEDM dialect the first record's status (`I`) and severity (`J`) + // arrive without a value event, so an expression reading either must be + // re-evaluated on their change. Which of the two is watched was decided once + // at connect, as MEDM decides it once in `setDynamicAttrMonitorFlags`. The + // PyDM dialect has no metadata operands and stays value-only. + let metadata_watch = match &evaluator { + Evaluator::Medm { + first_child: Some(i), + monitors, + .. + } if !monitors.is_empty() => Some((*monitors, &children[*i].1)), + _ => None, + }; + let mut prev_metadata: Option = None; + + let mut child_connected = vec![false; children.len()]; let mut prev_value: Option = None; + // A listened child emitted a value that has not been folded into a + // recompute yet. Survives across ticks, so no edge is lost. + let mut pending_trigger = false; + // Deliberate divergence from PyDM, recorded as R4-10: the first + // all-connected tick always evaluates. PyDM's `callback_value` gate + // (`calc_plugin.py:141-142`) drops any value callback that predates full + // connection and never re-arms from `callback_conn`, so a calc whose + // update-var connects before the others stays connected-but-valueless + // until that var next changes. rsdm publishes the initial derived value. + let mut initial_eval_done = false; let mut ticker = tokio::time::interval(POLL_INTERVAL); ticker.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip); @@ -277,56 +360,73 @@ async fn run_channel( _ = cancel.cancelled() => break, _ = ticker.tick() => { - let all_connected = children.iter().all(|(_, ch)| ch.is_connected()); + // Drain first: an event that arrived before the last child + // connected must still arm the recompute. Only the *presence* + // of a value event matters, so an overflowed queue is harmless. + for (i, sub) in subs.iter().enumerate() { + let mut got_value = false; + sub.drain(|_| got_value = true); + if got_value && triggers[i] { + pending_trigger = true; + } + } - if all_connected != connected { - connected = all_connected; - writer.update(move |s| s.connected = all_connected); + if let Some((monitors, ch)) = metadata_watch { + let now = monitors.sample(ch); + // Arm on a *change*, so the first observation does not + // fabricate a trigger the initial evaluation already covers. + if prev_metadata.is_some_and(|was| was != now) { + pending_trigger = true; + } + prev_metadata = Some(now); } - // Note every child whose value changed; a change to a variable - // in the `update` list (or any variable when no list is given) - // triggers a recompute. - // - // Fold stamp changes into `prev_stamps` ONLY while every child - // is connected — i.e. only while a recompute could actually - // run. A child's initial value often lands a tick or more - // before the last child connects; consuming that change here - // (advancing `prev_stamps` regardless of `all_connected`, the - // old behaviour) dropped its trigger, so the first - // all-connected tick saw no change and the initial value was - // never published — the calc stayed connected but valueless - // until the next child write. Leaving `prev_stamps` at - // `u64::MAX` until the first all-connected tick makes that tick - // observe every child as changed and run the initial eval, with - // no dependence on the child-connect vs poll-tick interleaving. - let mut trigger = false; - if all_connected { - for (i, (name, ch)) in children.iter().enumerate() { - let stamp = ch.stamp(); - if stamp != prev_stamps[i] { - prev_stamps[i] = stamp; - if update.as_ref().is_none_or(|u| u.iter().any(|n| n == name)) { - trigger = true; - } - } + let all_connected = children.iter().all(|(_, ch)| ch.is_connected()); + let mut conn_changed = false; + for (i, (_, ch)) in children.iter().enumerate() { + let now = ch.is_connected(); + if now != child_connected[i] { + child_connected[i] = now; + conn_changed = true; } } - if all_connected && trigger - && let Some(value) = evaluate( + if conn_changed { + // PyDM `callback_conn` runs `_send_update(self.connected, + // self._value)` on *every* child connection change + // (`calc_plugin.py:156-157`), and `receive_new_data` + // (`:236-240`) re-emits the value unless it is None. + match prev_value.clone() { + Some(value) => writer.post_value(move |s| { + s.connected = all_connected; + s.value = Some(value); + }), + None => writer.update(move |s| s.connected = all_connected), + } + } + + if all_connected && !initial_eval_done { + initial_eval_done = true; + pending_trigger = true; + } + + if all_connected && pending_trigger { + // Clear before evaluating: a failing expression must not + // re-arm itself into a hot retry every tick. + pending_trigger = false; + if let Some(value) = evaluate( &id, &evaluator, &children, prev_value.as_ref(), &mut warned_eval, - ) - { - prev_value = Some(value.clone()); - writer.post_value(move |s| { - s.connected = true; - s.value = Some(value); - }); + ) { + prev_value = Some(value.clone()); + writer.post_value(move |s| { + s.connected = true; + s.value = Some(value); + }); + } } } @@ -355,21 +455,25 @@ fn evaluate( Evaluator::Medm { compiled, operand_indices, - } => Some(match eval_medm(compiled, children, operand_indices, prev) { - Ok(result) => PvValue::Float(result), - Err(err) => { - // Fail-visible on evaluation errors too, warning once (MEDM - // hides here — utils.c:4519-4523; see the module docs). - if !*warned_eval { - *warned_eval = true; - log::warn!( - "{id}: MEDM CALC evaluation failed ({err:?}); \ + first_child, + .. + } => Some( + match eval_medm(compiled, children, operand_indices, *first_child, prev) { + Ok(result) => PvValue::Float(result), + Err(err) => { + // Fail-visible on evaluation errors too, warning once (MEDM + // hides here — utils.c:4519-4523; see the module docs). + if !*warned_eval { + *warned_eval = true; + log::warn!( + "{id}: MEDM CALC evaluation failed ({err:?}); \ publishing 1.0 (fail-visible)" - ); + ); + } + PvValue::Float(1.0) } - PvValue::Float(1.0) - } - }), + }, + ), Evaluator::Invalid => None, } } @@ -719,6 +823,7 @@ fn eval_medm( compiled: &medm_calc::CompiledExpr, children: &[(String, Channel)], operand_indices: &[usize], + first_child: Option, prev: Option<&PvValue>, ) -> Result { let mut inputs = medm_calc::NumericInputs::new(); @@ -730,15 +835,7 @@ fn eval_medm( child_bound[idx] = true; } - // The "first channel" whose metadata backs E–L: operand A when a child - // binds it, else the first listed child (MEDM requires channel A for a - // valid calc — utils.c:4543 — so A is the normal case). - let first = operand_indices - .iter() - .position(|&idx| idx == 0) - .or(if children.is_empty() { None } else { Some(0) }) - .map(|i| &children[i].1); - if let Some(ch) = first { + if let Some(ch) = first_child.map(|i| &children[i].1) { ch.read(|s| { if !child_bound[6] { // G: element count (scalar = 1; no value yet = 0, as an MEDM @@ -748,7 +845,10 @@ fn eval_medm( if !child_bound[7] { inputs.vars[7] = s.display_limits.map(|(_, hopr)| hopr).unwrap_or(0.0); } - // I (8): EPICS alarm STATUS — not carried by ChannelState; 0.0. + if !child_bound[8] { + // I: EPICS alarm STATUS (the record's `STAT` field). + inputs.vars[8] = f64::from(s.status); + } if !child_bound[9] { inputs.vars[9] = f64::from(s.severity.as_code()); } @@ -765,6 +865,60 @@ fn eval_medm( medm_calc::eval(compiled, &mut inputs) } +/// Whether `expr` reads the calc operand `letter`, by MEDM's own scan +/// (`calcUsesStatus` / `calcUsesSeverity`, `medm/utils.c:4647-4694`): the letter +/// in either case, at the start of the expression or not preceded by an ASCII +/// letter — the guard that keeps `sin`, `asin`, `min`, `nint`, `pi` from +/// looking like operand `I`. +fn calc_uses_operand(expr: &str, letter: char) -> bool { + let bytes = expr.as_bytes(); + let want = letter.to_ascii_uppercase(); + bytes.iter().enumerate().any(|(i, &b)| { + (b as char).to_ascii_uppercase() == want && (i == 0 || !bytes[i - 1].is_ascii_alphabetic()) + }) +} + +/// Which of the first record's metadata fields MEDM puts a monitor on for this +/// expression, beyond the value monitor it always sets. `setDynamicAttrMonitorFlags` +/// (`medm/utils.c:4621-4630`) adds `monitorStatusChanged` iff the expression uses +/// operand `I` and `monitorSeverityChanged` iff it uses `J`, and nothing else — +/// `hopr`/`precision`/`lopr` (H/K/L) are read but never monitored, so they never +/// retrigger. Decided once, at connect, as MEDM decides it once. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +struct MedmMonitors { + status: bool, + severity: bool, +} + +/// The monitored fields as read on one tick. An unmonitored field is `None`, so +/// it cannot differ between ticks and cannot arm a recompute — the monitor set +/// gates the comparison by construction rather than by a branch at the read. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +struct MedmSample { + status: Option, + severity: Option, +} + +impl MedmMonitors { + fn of(expr: &str) -> Self { + Self { + status: calc_uses_operand(expr, 'I'), + severity: calc_uses_operand(expr, 'J'), + } + } + + fn is_empty(self) -> bool { + !self.status && !self.severity + } + + fn sample(self, ch: &Channel) -> MedmSample { + ch.read(|s| MedmSample { + status: self.status.then_some(s.status), + severity: self.severity.then_some(s.severity), + }) + } +} + /// The calc-operand index for an MEDM-dialect variable name: a single letter /// `A`–`U` in either case (`medm/medmCalc.c:212-236` accepts both cases; /// `A`–`U` is the engine's [`medm_calc::CALC_NARGS`] operand range, a superset @@ -1185,7 +1339,7 @@ mod tests { ); // No NUL: the whole buffer decodes. assert_eq!( - pv_to_evalexpr(&PvValue::Bytes(Arc::from([b'o', b'k'].as_slice()))), + pv_to_evalexpr(&PvValue::Bytes(Arc::from(b"ok".as_slice()))), Some(Value::String("ok".to_owned())) ); } @@ -1411,4 +1565,315 @@ mod tests { assert_eq!(medm_var_index("1"), None); assert_eq!(medm_var_index(""), None); } + + // -- trigger model (R4-10 / R4-11 / R4-12) ------------------------------- + // + // These drive `run_channel` against hand-built children so a child can post + // an alarm/metadata-only update (`StateWriter::update`), which no `loc://` + // child ever does. Cases are one per trigger-model boundary. + + use crate::channel::channel_pair; + + /// Longer than `POLL_INTERVAL` (50 ms), so every case observes at least one + /// full drain/evaluate tick. + const SETTLE: Duration = Duration::from_millis(200); + + struct Harness { + calc: Channel, + cancel: CancellationToken, + // Kept alive so `run_channel`'s `writes.recv()` never returns `None` + // (which would break its loop). + _writes_tx: mpsc::UnboundedSender, + } + + impl Drop for Harness { + fn drop(&mut self) { + self.cancel.cancel(); + } + } + + /// Spawn `run_channel` for `expr` over `children`, restricted to `update`. + fn spawn_calc( + expr: &str, + update: Option>, + children: Vec<(String, Channel)>, + ) -> Harness { + spawn_calc_dialect(expr, Dialect::Evalexpr, update, children) + } + + fn spawn_calc_dialect( + expr: &str, + dialect: Dialect, + update: Option>, + children: Vec<(String, Channel)>, + ) -> Harness { + let config = CalcConfig { + expr: expr.to_owned(), + dialect, + vars: children + .iter() + .map(|(n, _)| (n.clone(), format!("loc://{n}"))) + .collect(), + update, + }; + let (calc, calc_writer) = channel_pair("calc://t"); + let (writes_tx, writes_rx) = mpsc::unbounded_channel(); + let cancel = CancellationToken::new(); + tokio::spawn(run_channel( + "calc://t".to_owned(), + config, + children, + calc_writer, + writes_rx, + cancel.clone(), + )); + Harness { + calc, + cancel, + _writes_tx: writes_tx, + } + } + + fn connect_with(writer: &StateWriter, v: f64) { + writer.post_value(move |s| { + s.connected = true; + s.value = Some(PvValue::Float(v)); + }); + } + + /// One case per boundary of MEDM's `calcUsesStatus`/`calcUsesSeverity` scan + /// (`utils.c:4647-4694`), which is a letter match guarded only by "not + /// preceded by an ASCII letter". + #[test] + fn calc_uses_operand_matches_medms_scan() { + assert!(calc_uses_operand("J", 'J'), "the whole expression"); + assert!(calc_uses_operand("j#0", 'J'), "lower case"); + assert!(calc_uses_operand("A+J", 'J'), "preceded by an operator"); + assert!(calc_uses_operand("A?J:0", 'J'), "preceded by punctuation"); + assert!(calc_uses_operand("2J", 'J'), "preceded by a digit"); + assert!(!calc_uses_operand("A#0", 'J'), "absent"); + assert!(!calc_uses_operand("", 'J'), "empty"); + // The guard exists so function names do not read as operand I. + assert!(!calc_uses_operand("SIN(A)", 'I')); + assert!(!calc_uses_operand("min(A,B)", 'I')); + assert!(!calc_uses_operand("nint(A)", 'I')); + assert!(calc_uses_operand("I#0", 'I'), "operand I on its own"); + } + + /// Boundary: the expression reads operand `J`, so the first record's + /// severity — which arrives with no value event — must re-evaluate it. + /// MEDM sets `monitorSeverityChanged` for exactly this case + /// (`utils.c:4624-4628`). + #[tokio::test] + async fn medm_expr_reading_severity_reevaluates_on_a_severity_change() { + let (a, a_writer) = channel_pair("loc://a"); + let h = spawn_calc_dialect("J", Dialect::Medm, None, vec![("A".into(), a)]); + connect_with(&a_writer, 0.0); + tokio::time::sleep(SETTLE).await; + assert_eq!( + h.calc.read(|s| s.value.clone()), + Some(PvValue::Float(0.0)), + "NO_ALARM" + ); + + // `update` posts no value event; only the severity moves. + a_writer.update(|s| s.severity = AlarmSeverity::Major); + tokio::time::sleep(SETTLE).await; + assert_eq!( + h.calc.read(|s| s.value.clone()), + Some(PvValue::Float(2.0)), + "MAJOR must re-evaluate J" + ); + } + + /// Boundary: the expression reads operand `I` (alarm status), which arrives + /// with no value event. MEDM sets `monitorStatusChanged` for exactly this + /// case (`calcUsesStatus`), so the status change must re-evaluate. + #[tokio::test] + async fn medm_expr_reading_status_reevaluates_on_a_status_change() { + let (a, a_writer) = channel_pair("loc://a"); + let h = spawn_calc_dialect("I", Dialect::Medm, None, vec![("A".into(), a)]); + connect_with(&a_writer, 0.0); + tokio::time::sleep(SETTLE).await; + assert_eq!( + h.calc.read(|s| s.value.clone()), + Some(PvValue::Float(0.0)), + "NO_ALARM status" + ); + + // `update` posts no value event; only the status moves. + a_writer.update(|s| s.status = 7); // HIGH_ALARM + tokio::time::sleep(SETTLE).await; + assert_eq!( + h.calc.read(|s| s.value.clone()), + Some(PvValue::Float(7.0)), + "a status change must re-evaluate I" + ); + } + + /// Boundary: the expression reads neither `I` nor `J`, so status, severity + /// and every other metadata change arm nothing — MEDM leaves both + /// `monitorStatusChanged` and `monitorSeverityChanged` false. This is the + /// same rule that keeps `hopr`/`precision`/`lopr` from ever retriggering. + #[tokio::test] + async fn medm_expr_reading_no_metadata_ignores_every_metadata_change() { + let (a, a_writer) = channel_pair("loc://a"); + let h = spawn_calc_dialect("A", Dialect::Medm, None, vec![("A".into(), a)]); + connect_with(&a_writer, 5.0); + tokio::time::sleep(SETTLE).await; + let stamp = h.calc.stamp(); + assert_eq!(h.calc.read(|s| s.value.clone()), Some(PvValue::Float(5.0))); + + a_writer.update(|s| s.severity = AlarmSeverity::Major); + a_writer.update(|s| s.status = 7); + a_writer.update(|s| s.precision = Some(7)); + tokio::time::sleep(SETTLE).await; + assert_eq!( + h.calc.stamp(), + stamp, + "metadata-only churn must not republish" + ); + } + + /// Boundary: the expression reads `J` but not `I`, so only the severity is + /// monitored. A status change on the same record must arm nothing — the + /// half of MEDM's per-operand monitor set that a single "watch metadata" + /// flag would get wrong. + #[tokio::test] + async fn medm_expr_reading_severity_ignores_a_status_change() { + let (a, a_writer) = channel_pair("loc://a"); + let h = spawn_calc_dialect("J", Dialect::Medm, None, vec![("A".into(), a)]); + connect_with(&a_writer, 0.0); + tokio::time::sleep(SETTLE).await; + let stamp = h.calc.stamp(); + + a_writer.update(|s| s.status = 7); + tokio::time::sleep(SETTLE).await; + assert_eq!( + h.calc.stamp(), + stamp, + "J is monitored, I is not: a status change must not republish" + ); + } + + /// `calcUsesStatus` / `calcUsesSeverity` decide the monitor set from the + /// expression alone. Boundary: each letter present, absent, and hidden + /// inside an identifier. + #[test] + fn medm_monitors_follow_calc_uses_status_and_severity() { + let m = |e: &str| MedmMonitors::of(e); + assert_eq!( + m("A"), + MedmMonitors { + status: false, + severity: false + } + ); + assert_eq!( + m("I"), + MedmMonitors { + status: true, + severity: false + } + ); + assert_eq!( + m("J"), + MedmMonitors { + status: false, + severity: true + } + ); + assert_eq!( + m("I#J"), + MedmMonitors { + status: true, + severity: true + } + ); + // `sin`/`nint` end in a letter-preceded `i`/`n`; neither is operand I. + assert!(m("sin(A)").is_empty()); + assert!(m("nint(A)").is_empty()); + } + + /// Boundary: a child's alarm/metadata-only change bumps `Channel::stamp()` + /// but emits no value event, so it must NOT recompute. PyDM binds only + /// `value_slot` on calc children (`calc_plugin.py:84-89`). The old + /// stamp-polling trigger republished here, double-counting accumulators. + #[tokio::test] + async fn alarm_only_child_update_does_not_retrigger_the_calc() { + let (a, a_writer) = channel_pair("loc://a"); + let harness = spawn_calc("a", None, vec![("a".to_owned(), a)]); + let events = harness.calc.subscribe_values(16); + + connect_with(&a_writer, 1.0); + tokio::time::sleep(SETTLE).await; + let mut n = 0; + events.drain(|_| n += 1); + assert_eq!(n, 1, "one publish for the initial evaluation"); + + // Metadata/alarm only: bumps the child's stamp, emits no value event. + a_writer.update(|s| s.severity = crate::channel::AlarmSeverity::Minor); + tokio::time::sleep(SETTLE).await; + let mut n = 0; + events.drain(|_| n += 1); + assert_eq!(n, 0, "alarm-only child update must not republish the calc"); + } + + /// Boundary: a child connection-state change re-emits the last computed + /// value, matching PyDM `callback_conn` → `_send_update(self.connected, + /// self._value)` (`calc_plugin.py:156-157`). The old code only flipped the + /// `connected` flag with no value event. + #[tokio::test] + async fn child_connection_change_reemits_the_last_value() { + let (a, a_writer) = channel_pair("loc://a"); + let harness = spawn_calc("a", None, vec![("a".to_owned(), a)]); + let events = harness.calc.subscribe_values(16); + + connect_with(&a_writer, 7.0); + tokio::time::sleep(SETTLE).await; + let mut got = Vec::new(); + events.drain(|e| got.push(e.value.as_f64().expect("numeric"))); + assert_eq!(got, vec![7.0], "initial evaluation"); + + // Child drops: connection change, no new value. + a_writer.update(|s| s.connected = false); + tokio::time::sleep(SETTLE).await; + let mut got = Vec::new(); + events.drain(|e| got.push(e.value.as_f64().expect("numeric"))); + assert_eq!(got, vec![7.0], "child disconnect re-emits the last value"); + } + + /// Boundary: a triggering child's value that lands BEFORE the last child + /// connects must still arm the recompute. The queue holds the event; the + /// old collapsing `stamp` counter needed a `u64::MAX` sentinel to survive + /// this interleaving, and lost the edge without it. + #[tokio::test] + async fn value_arriving_before_all_children_connect_still_triggers() { + let (a, a_writer) = channel_pair("loc://a"); + let (b, b_writer) = channel_pair("loc://b"); + let harness = spawn_calc( + "a + b", + Some(vec!["a".to_owned()]), + vec![("a".to_owned(), a), ("b".to_owned(), b)], + ); + let events = harness.calc.subscribe_values(16); + + // The update-var connects and publishes first; `b` is still down. + connect_with(&a_writer, 2.0); + tokio::time::sleep(SETTLE).await; + let mut n = 0; + events.drain(|_| n += 1); + assert_eq!(n, 0, "no publish while a child is disconnected"); + + // `b` is NOT in the update list, so only the queued `a` event (and the + // first all-connected evaluation) can produce the initial value. + connect_with(&b_writer, 3.0); + tokio::time::sleep(SETTLE).await; + let mut got = Vec::new(); + events.drain(|e| got.push(e.value.as_f64().expect("numeric"))); + assert!( + got.contains(&5.0), + "initial derived value a+b = 2+3 = 5.0 was never published (got {got:?})" + ); + } } diff --git a/rsdm/src/data_plugins/epics_plugins/ca_plugin.rs b/rsdm/src/data_plugins/epics_plugins/ca_plugin.rs index 22ecc04..018ec77 100644 --- a/rsdm/src/data_plugins/epics_plugins/ca_plugin.rs +++ b/rsdm/src/data_plugins/epics_plugins/ca_plugin.rs @@ -450,6 +450,7 @@ fn apply_property_metadata( enum_strings: Option>, ) { s.severity = AlarmSeverity::from_epics(snap.alarm.severity); + s.status = snap.alarm.status as i16; if enum_strings.is_some() { s.enum_strings = enum_strings; } @@ -487,6 +488,7 @@ fn apply_value(s: &mut ChannelState, snap: &Snapshot, value: PvValue) { fn apply_alarm(s: &mut ChannelState, snap: &Snapshot) { s.connected = true; s.severity = AlarmSeverity::from_epics(snap.alarm.severity); + s.status = snap.alarm.status as i16; s.timestamp = Some(snap.timestamp.into()); } diff --git a/rsdm/src/data_plugins/epics_plugins/pva_plugin.rs b/rsdm/src/data_plugins/epics_plugins/pva_plugin.rs index 6d9550f..dc560dc 100644 --- a/rsdm/src/data_plugins/epics_plugins/pva_plugin.rs +++ b/rsdm/src/data_plugins/epics_plugins/pva_plugin.rs @@ -676,6 +676,9 @@ fn apply_nt_metadata(s: &mut ChannelState, root: &PvField) { // the signed wire value into its `u16` domain first. s.severity = AlarmSeverity::from_epics(sev.clamp(0, 3) as u16); } + if let Some(stat) = scalar_field(root, "alarm.status").and_then(scalar_i64) { + s.status = stat.clamp(i64::from(i16::MIN), i64::from(i16::MAX)) as i16; + } if let Some(ts) = timestamp_of(root) { s.timestamp = Some(ts); } diff --git a/rsdm/src/widgets/byte.rs b/rsdm/src/widgets/byte.rs index 28243a2..ae9eacf 100644 --- a/rsdm/src/widgets/byte.rs +++ b/rsdm/src/widgets/byte.rs @@ -341,7 +341,7 @@ impl RsdmByteIndicator { /// Paint one bit indicator into `rect` (the shared painter of the flow /// and exact-rect paths). fn paint_bit(&self, painter: &egui::Painter, rect: egui::Rect, color: Color32) { - let outline = egui::Stroke::new(1.0, Color32::from_gray(60)); + let outline = egui::Stroke::new(1.0_f32, Color32::from_gray(60)); if self.circles { let radius = rect.width().min(rect.height()) / 2.0 - 1.0; painter.circle(rect.center(), radius, color, outline); diff --git a/rsdm/src/widgets/image_file.rs b/rsdm/src/widgets/image_file.rs index aa940ba..6ddc0a4 100644 --- a/rsdm/src/widgets/image_file.rs +++ b/rsdm/src/widgets/image_file.rs @@ -108,7 +108,7 @@ impl RsdmImage { painter.rect_stroke( rect, egui::CornerRadius::ZERO, - egui::Stroke::new(1.0, Color32::from_rgb(180, 60, 60)), + egui::Stroke::new(1.0_f32, Color32::from_rgb(180, 60, 60)), egui::StrokeKind::Inside, ); painter.text( diff --git a/rsdm/src/widgets/multi_state.rs b/rsdm/src/widgets/multi_state.rs index 7e9923e..2dce5eb 100644 --- a/rsdm/src/widgets/multi_state.rs +++ b/rsdm/src/widgets/multi_state.rs @@ -131,7 +131,7 @@ impl RsdmMultiStateIndicator { let (rect, _) = ui.allocate_exact_size(size, egui::Sense::hover()); if ui.is_rect_visible(rect) { // PyDM paints a red 1 px border around the state-coloured fill. - let border = egui::Stroke::new(1.0, Color32::RED); + let border = egui::Stroke::new(1.0_f32, Color32::RED); if self.render_as_rectangle { ui.painter().rect( rect, diff --git a/rsdm/src/widgets/scale_indicator.rs b/rsdm/src/widgets/scale_indicator.rs index 950a699..66b28f1 100644 --- a/rsdm/src/widgets/scale_indicator.rs +++ b/rsdm/src/widgets/scale_indicator.rs @@ -262,7 +262,7 @@ impl RsdmScaleIndicator { painter.rect_filled(rect, egui::CornerRadius::ZERO, self.background); let horizontal = self.orientation == Orientation::Horizontal; - let tick_stroke = Stroke::new(1.0, self.tick_color); + let tick_stroke = Stroke::new(1.0_f32, self.tick_color); for tp in division_proportions(self.num_divisions) { let (a, b) = self.axis_line(rect, tp, horizontal); painter.line_segment([a, b], tick_stroke); @@ -281,7 +281,7 @@ impl RsdmScaleIndicator { ); } else { let (a, b) = self.axis_line(rect, self.pos(p), horizontal); - painter.line_segment([a, b], Stroke::new(3.0, bar_color)); + painter.line_segment([a, b], Stroke::new(3.0_f32, bar_color)); } } diff --git a/src/core/fitting.rs b/src/core/fitting.rs index c230e17..44a9101 100644 --- a/src/core/fitting.rs +++ b/src/core/fitting.rs @@ -2651,10 +2651,7 @@ pub fn estimate_multi_gaussian( // (fittheories.py:361-364). let delta: Vec = y.iter().zip(bg.iter()).map(|(&a, &b)| a - b).collect(); let maxv = delta.iter().copied().fold(f64::NEG_INFINITY, f64::max); - match delta.iter().position(|&v| v == maxv) { - Some(p) => vec![p], - None => return None, - } + vec![delta.iter().position(|&v| v == maxv)?] } else { found.iter().map(|p| p.index).collect() }; diff --git a/src/core/histogram.rs b/src/core/histogram.rs index 73c692d..364a771 100644 --- a/src/core/histogram.rs +++ b/src/core/histogram.rs @@ -5,6 +5,29 @@ //! inline `HistogramColorBar`; this is the single home for the binning logic so //! the two never drift. +/// Index of the bin holding `t`, or `None` when `t` is outside the grid. +/// +/// The grid is the `nbins` uniform bins over the half-open extent +/// `[xmin, xmax)`, `span == xmax - xmin`. `Histogramnd` rejects a coordinate +/// below `g_min`, and rejects a coordinate at or above `g_max` unless +/// `last_bin_closed` is set (`math/histogramnd/src/histogramnd_template.c:174-222`). +/// `ColormapDialog.computeHistogram` does not set it (`ColormapDialog.py:1288`), +/// so `t == xmax` is dropped rather than folded into the last bin — and a +/// degenerate extent (`span == 0`) admits no sample at all. +/// +/// The arithmetic multiplies before dividing, as C does, so the bin boundaries +/// fall on the same samples. +/// +/// This is the one home for `Histogramnd`'s admission rule; every caller that +/// mirrors a silx `Histogramnd(...)` without `last_bin_closed` uses it. +pub(crate) fn bin_index(t: f64, xmin: f64, xmax: f64, span: f64, nbins: usize) -> Option { + if !t.is_finite() || t < xmin || t >= xmax { + return None; + } + let idx = ((t - xmin) * nbins as f64 / span) as usize; + (idx < nbins).then_some(idx) +} + /// Compute the data-distribution histogram of a flattened scalar image. /// /// `data` is the flattened scalar image. `range` optionally fixes the histogram @@ -18,6 +41,8 @@ /// when there is no finite data / no valid range (silx returns `(None, None)`). /// The bin count is `clamp(2, min(256, floor(sqrt(N))))` — silx `nbins` (the /// integer-data 256-bin special case does not apply to scalar `f64` images). +/// +/// The histogram extent is half-open, `[xmin, xmax)` — see [`bin_index`]. pub fn compute_histogram( data: &[f64], range: Option<(f64, f64)>, @@ -57,23 +82,10 @@ pub fn compute_histogram( let nbins = ((data.len() as f64).sqrt().floor() as usize).clamp(2, 256); let span = xmax - xmin; let mut counts = vec![0u64; nbins]; - if span > 0.0 { - for &v in data { - let t = xform(v); - if !t.is_finite() || t < xmin || t > xmax { - continue; - } - // Last bin is inclusive of `xmax` (numpy/Histogramnd convention). - let idx = ((((t - xmin) / span) * nbins as f64) as usize).min(nbins - 1); + for &v in data { + if let Some(idx) = bin_index(xform(v), xmin, xmax, span, nbins) { counts[idx] += 1; } - } else { - // Degenerate (all-equal) range: every finite sample lands in bin 0. - for &v in data { - if xform(v).is_finite() { - counts[0] += 1; - } - } } // Edges in the transformed space, mapped back to linear when log. @@ -97,8 +109,9 @@ mod tests { let (counts, edges) = compute_histogram(&data, None, false).expect("histogram"); assert_eq!(counts.len(), 10); assert_eq!(edges.len(), 11); - // Every finite sample is binned exactly once. - assert_eq!(counts.iter().sum::(), 100); + // Every sample below the upper edge is binned exactly once; 99.0 sits + // *on* the open upper edge and is dropped. + assert_eq!(counts.iter().sum::(), 99); // Extent spans the finite data min/max. assert_eq!(edges[0], 0.0); assert_eq!(*edges.last().unwrap(), 99.0); @@ -114,16 +127,42 @@ mod tests { assert_eq!(e1.len(), 3); } + /// One case per boundary of `bin_index`, not one per user story. + /// `nbins = 2` over `[0, 5]` gives bins `[0, 2.5)` and `[2.5, 5)`. + #[test] + fn bin_index_boundaries_match_histogramnd() { + let bin = |t: f64| bin_index(t, 0.0, 5.0, 5.0, 2); + + assert_eq!(bin(-0.001), None, "below the lower edge"); + assert_eq!(bin(0.0), Some(0), "on the lower edge: closed"); + assert_eq!(bin(2.4999), Some(0), "just below the interior edge"); + assert_eq!( + bin(2.5), + Some(1), + "on the interior edge: belongs to the upper bin" + ); + assert_eq!(bin(4.9999), Some(1), "just below the upper edge"); + assert_eq!(bin(5.0), None, "on the upper edge: open, so dropped"); + assert_eq!(bin(5.001), None, "above the upper edge"); + assert_eq!(bin(f64::NAN), None, "non-finite"); + assert_eq!(bin(f64::INFINITY), None, "non-finite"); + + // Degenerate extent: `t >= xmax` for every `t`, so nothing is admitted + // and `span == 0` is never divided by. + assert_eq!(bin_index(7.0, 7.0, 7.0, 0.0, 2), None); + } + #[test] - fn compute_histogram_uses_supplied_range_and_counts_in_range() { - // With an explicit range, out-of-range samples are dropped; the last bin - // is inclusive of the upper edge. + fn compute_histogram_uses_supplied_range_and_drops_the_open_upper_edge() { + // With an explicit range, out-of-range samples are dropped, and so are + // samples sitting exactly on the (open) upper edge. let data = vec![-5.0, 0.0, 2.5, 5.0, 5.0, 10.0]; let (counts, edges) = compute_histogram(&data, Some((0.0, 5.0)), false).expect("histogram"); assert_eq!(edges[0], 0.0); assert_eq!(*edges.last().unwrap(), 5.0); - // -5.0 and 10.0 are outside [0, 5]; 0.0, 2.5, 5.0, 5.0 are inside. - assert_eq!(counts.iter().sum::(), 4); + // Admitted: 0.0, 2.5. Dropped: -5.0 (below), 5.0 twice (open upper + // edge), 10.0 (above). + assert_eq!(counts.iter().sum::(), 2); } #[test] @@ -137,7 +176,8 @@ mod tests { assert!((edges[0] - 1.0).abs() < 1e-9, "{}", edges[0]); assert!((edges[1] - 10f64.powf(1.5)).abs() < 1e-6, "{}", edges[1]); assert!((edges[2] - 1000.0).abs() < 1e-6, "{}", edges[2]); - assert_eq!(counts.iter().sum::(), 4); + // 1000.0 transforms to the open upper edge (log10 = 3) and is dropped. + assert_eq!(counts.iter().sum::(), 3); } #[test] @@ -147,12 +187,14 @@ mod tests { } #[test] - fn compute_histogram_degenerate_range_counts_all_in_first_bin() { - // All-equal data: zero-width extent -> every finite sample into bin 0. + fn compute_histogram_degenerate_range_admits_no_sample() { + // All-equal data: the extent collapses, so every sample sits on the + // open upper edge. `ColormapDialog.computeHistogram` has no guard for + // this (`ColormapDialog.py:1264-1288`) and `Histogramnd` drops them + // all, leaving an empty histogram over a zero-width extent. let data = vec![7.0; 5]; let (counts, edges) = compute_histogram(&data, None, false).expect("histogram"); - assert_eq!(counts[0], 5); - assert_eq!(counts.iter().sum::(), 5); + assert_eq!(counts.iter().sum::(), 0); assert_eq!(edges[0], 7.0); assert_eq!(*edges.last().unwrap(), 7.0); } diff --git a/src/core/scatter_viz.rs b/src/core/scatter_viz.rs index f3da838..d79f61f 100644 --- a/src/core/scatter_viz.rs +++ b/src/core/scatter_viz.rs @@ -32,6 +32,7 @@ use egui::Color32; +use crate::core::histogram::bin_index; use crate::core::triangles::Triangles; /// Major order of points in a regular grid (silx @@ -1128,11 +1129,17 @@ impl BinnedStatistic { /// compute per-bin mean/count/sum (silx `Visualization.BINNED_STATISTIC`, /// `scatter.py::__getHistogramInfo`). /// -/// The grid spans the finite-point bounding box of X and Y. A point is assigned -/// to the bin `floor((coord - min) / binsize)`, with the upper edge clamped into -/// the last bin so the maximum point is included (matching `Histogramnd`'s -/// inclusive last edge). Non-finite points and points with a non-finite value -/// are skipped. +/// The grid spans the finite-point bounding box of X and Y. A point is admitted +/// only when both coordinates fall in the half-open extent `[min, max)` of their +/// axis: `scatter.py:499-501` calls `Histogramnd` without `last_bin_closed`, so +/// a point on the upper edge — including the maximum point of either axis — is +/// dropped, and an axis whose extent is degenerate admits nothing at all. See +/// [`crate::core::histogram::bin_index`]. Points with a non-finite value are +/// skipped. +/// +/// `scale` is the bin size silx derives from the edge array, +/// `span / n_bins` (`scatter.py:504-507`), which is `0.0` for a degenerate +/// extent. /// /// Returns `None` when there are no finite points or `rows`/`cols` is 0. /// @@ -1161,39 +1168,28 @@ pub fn binned_statistic( return None; // no finite points } - // Bin sizes; degenerate (single-valued) ranges get a unit bin to avoid /0. - let sx = { - let span = x_max - x_min; - if span > 0.0 { span / cols as f64 } else { 1.0 } - }; - let sy = { - let span = y_max - y_min; - if span > 0.0 { span / rows as f64 } else { 1.0 } - }; + let x_span = x_max - x_min; + let y_span = y_max - y_min; + let sx = x_span / cols as f64; + let sy = y_span / rows as f64; let mut count = vec![0u64; rows * cols]; let mut sum = vec![0.0f64; rows * cols]; for ((&xi, &yi), &vi) in x.iter().zip(y).zip(values) { - if !xi.is_finite() || !yi.is_finite() || !vi.is_finite() { + if !vi.is_finite() { continue; } - // Column from X, row from Y. - let mut c = ((xi - x_min) / sx).floor() as isize; - let mut r = ((yi - y_min) / sy).floor() as isize; - // Clamp the inclusive upper edge into the last bin. - if c >= cols as isize { - c = cols as isize - 1; - } - if r >= rows as isize { - r = rows as isize - 1; - } - if c < 0 || r < 0 { - continue; // outside the lower edge (only non-finite would do this) - } - let idx = r as usize * cols + c as usize; - count[idx] += 1; - sum[idx] += vi; + // Column from X, row from Y. `bin_index` rejects non-finite coordinates + // and both open edges of the extent. + let (Some(c), Some(r)) = ( + bin_index(xi, x_min, x_max, x_span, cols), + bin_index(yi, y_min, y_max, y_span, rows), + ) else { + continue; + }; + count[r * cols + c] += 1; + sum[r * cols + c] += vi; } let mean: Vec = count @@ -1633,18 +1629,24 @@ mod tests { #[test] fn binned_statistic_2x2_mean_count_sum() { // Points in [0,2]x[0,2], 2x2 bins -> bin size 1x1. - // Bin layout (row=Y, col=X): + // Bin layout (row=Y, col=X), every side half-open: // (r0,c0): x in [0,1), y in [0,1) - // (r0,c1): x in [1,2], y in [0,1) - // (r1,c0): x in [0,1), y in [1,2] - // (r1,c1): x in [1,2], y in [1,2] + // (r0,c1): x in [1,2), y in [0,1) + // (r1,c0): x in [0,1), y in [1,2) + // (r1,c1): x in [1,2), y in [1,2) // Place: two points in (r0,c0); one in (r1,c1); leave (r0,c1),(r1,c0) empty. - // (0,0) and (2,2) pin the data extent to [0,2]x[0,2] so bins are 1x1. - let x = [0.0, 0.5, 2.0]; - let y = [0.0, 0.5, 2.0]; - let v = [10.0, 30.0, 7.0]; + // (0,0) and (2,2) pin the data extent to [0,2]x[0,2] so bins are 1x1; + // (2,2) itself sits on the open upper edge and is dropped. + let x = [0.0, 0.5, 1.5, 2.0]; + let y = [0.0, 0.5, 1.5, 2.0]; + let v = [10.0, 30.0, 7.0, 99.0]; let bs = binned_statistic(&x, &y, &v, 2, 2).expect("binned"); assert_eq!(bs.shape, (2, 2)); + assert_eq!( + bs.count.iter().sum::(), + 3, + "the corner point is dropped" + ); // (r0,c0) = index 0: count 2, sum 40, mean 20. assert_eq!(bs.count[0], 2); @@ -1669,25 +1671,44 @@ mod tests { assert_eq!(bs.scale, (1.0, 1.0)); } + /// One case per boundary of the admission rule, per axis. #[test] - fn binned_statistic_max_point_clamped_into_last_bin() { - // Point exactly at the max edge must land in the last bin, not overflow. - let x = [0.0, 2.0]; - let y = [0.0, 2.0]; - let v = [1.0, 2.0]; + fn binned_statistic_drops_points_on_the_open_upper_edge() { + // Extent [0,2]x[0,2] with 2x2 unit bins. Only a point strictly inside + // both extents is admitted; touching either upper edge drops it, which + // is what `Histogramnd` does without `last_bin_closed`. + let x = [0.0, 2.0, 2.0, 1.9]; + let y = [0.0, 2.0, 1.9, 2.0]; + let v = [1.0, 2.0, 3.0, 4.0]; let bs = binned_statistic(&x, &y, &v, 2, 2).expect("binned"); - // (2,2) is the upper corner -> last bin (r1,c1) = index 3. - assert_eq!(bs.count[3], 1); - assert!((bs.sum[3] - 2.0).abs() < 1e-12); - // (0,0) -> first bin. + // (0,0) is on the closed lower edge -> first bin. assert_eq!(bs.count[0], 1); + assert!((bs.sum[0] - 1.0).abs() < 1e-12); + // The corner (2,2), and each point touching one upper edge, are dropped. + assert_eq!(bs.count[3], 0, "last bin stays empty"); + assert_eq!(bs.count.iter().sum::(), 1); + } + + #[test] + fn binned_statistic_degenerate_extent_admits_no_point() { + // Every X is equal: the X extent collapses, so every point sits on the + // open upper edge of that axis. silx reports a zero bin size here + // (`scatter.py:504-507`) and `Histogramnd` counts nothing. + let x = [3.0, 3.0, 3.0]; + let y = [0.0, 1.0, 2.0]; + let v = [1.0, 2.0, 3.0]; + let bs = binned_statistic(&x, &y, &v, 2, 2).expect("binned"); + assert_eq!(bs.count.iter().sum::(), 0); + assert_eq!(bs.scale.0, 0.0, "degenerate X bin size"); + assert_eq!(bs.scale.1, 1.0); } #[test] fn binned_statistic_select_returns_chosen_grid() { - let x = [0.2, 1.5]; - let y = [0.2, 1.5]; - let v = [10.0, 7.0]; + // (2.0, 2.0) pins the extent and is itself dropped by the open edge. + let x = [0.2, 1.5, 2.0]; + let y = [0.2, 1.5, 2.0]; + let v = [10.0, 7.0, 99.0]; let bs = binned_statistic(&x, &y, &v, 2, 2).expect("binned"); let counts = bs.select(BinnedStatisticFunction::Count); assert_eq!(counts, vec![1.0, 0.0, 0.0, 1.0]); diff --git a/src/render/shaders/volume_raycaster.wgsl b/src/render/shaders/volume_raycaster.wgsl index 87b5e3a..1de4b77 100644 --- a/src/render/shaders/volume_raycaster.wgsl +++ b/src/render/shaders/volume_raycaster.wgsl @@ -2,10 +2,12 @@ // // A full-screen triangle generates one camera ray per pixel from the inverse // view-projection matrix. Each ray is clipped to the volume's axis-aligned box -// and marched in fixed steps. The 3D texture holds PREMULTIPLIED RGBA (the -// uploader premultiplies straight alpha) so the hardware linear filter -// interpolates premultiplied colour — a colour/transparent boundary stays the -// right hue instead of bleeding toward black. Samples accumulate front-to-back +// and marched in fixed steps. The 3D texture holds PREMULTIPLIED RGBA in +// `Rgba16Float` (the uploader premultiplies straight alpha) so the hardware +// linear filter interpolates premultiplied colour — a colour/transparent +// boundary stays the right hue instead of bleeding toward black — while the +// product `rgb·a` keeps enough precision for `fs_main` to divide the straight +// colour back out at low coverage. Samples accumulate front-to-back // into premultiplied colour that blends with `PREMULTIPLIED_ALPHA_BLENDING` // straight into egui's render pass (no offscreen target, no depth). @@ -22,10 +24,15 @@ struct Uniforms { @group(0) @binding(1) var vol_tex: texture_3d; @group(0) @binding(2) var vol_samp: sampler; -// Reference sample count for the opacity correction: at this many steps a -// sample's coverage is used as-is, and other step counts are corrected so the -// accumulated opacity stays the same. Matches the widget's default `steps`, so -// the default view is unchanged. +// The reference sample spacing is `box_diagonal / REF_STEPS`, a WORLD distance: +// a voxel's authored coverage is the opacity it contributes over exactly that +// much traversed thickness. Every sample is corrected from the ray's actual +// spacing `dt` to that reference (see `fs_main`), which makes the accumulated +// opacity depend on the traversed path length and not on the sample count. +// +// REF_STEPS matches the widget's default `steps`, so a ray crossing the full +// box diagonal at the default step count is unchanged from an uncorrected +// march. const REF_STEPS: f32 = 256.0; struct VsOut { @@ -82,6 +89,9 @@ fn fs_main(in: VsOut) -> @location(0) vec4 { let cull_floor = u.params.z; let extent = u.vol_max.xyz - u.vol_min.xyz; let dt = (tfar - tnear) / f32(steps); + // World-space reference spacing (see REF_STEPS). Positive for any non-empty + // box; `intersect_box` already rejected the degenerate ray. + let dt_ref = length(extent) / REF_STEPS; var acc = vec4(0.0, 0.0, 0.0, 0.0); // premultiplied rgb + alpha var t = tnear + dt * 0.5; @@ -90,14 +100,19 @@ fn fs_main(in: VsOut) -> @location(0) vec4 { let uvw = (p - u.vol_min.xyz) / extent; let s = textureSampleLevel(vol_tex, vol_samp, uvw, 0.0); // premultiplied rgb, coverage a if s.a > cull_floor { - // Scale coverage by the user opacity knob, then correct it for the - // step count so accumulated opacity is invariant to `steps` - // (transmittance (1-sa)^steps held to the REF_STEPS baseline). + // Scale coverage by the user opacity knob, then correct it from the + // reference spacing to this ray's actual spacing `dt` + // (Beer–Lambert: transmittance (1-sa)^(dt/dt_ref) per sample, so the + // chord transmittance is (1-sa)^(L/dt_ref) — a function of the + // traversed thickness L alone, not of the sample count). let sa = clamp(s.a * alpha_scale, 0.0, 1.0); - let sa_c = 1.0 - pow(1.0 - sa, REF_STEPS / f32(steps)); + let sa_c = 1.0 - pow(1.0 - sa, dt / dt_ref); // Rescale the premultiplied colour to the corrected coverage // (gain = sa_c/s.a). Compositing stays in premultiplied space, so no - // dark fringe at boundaries. + // dark fringe at boundaries. The division recovers the straight + // colour, which is why the texture is Rgba16Float and not Rgba8Unorm: + // an 8-bit product `rgb·a` only resolves the quotient to steps of + // 1/a, i.e. 3 colour levels at the coverage a=3/255 a march uses. let gain = sa_c / max(s.a, 1e-6); let w = 1.0 - acc.a; acc = vec4(acc.rgb + s.rgb * gain * w, acc.a + sa_c * w); diff --git a/src/render/volume_raycaster.rs b/src/render/volume_raycaster.rs index 93a1fc5..edc4a45 100644 --- a/src/render/volume_raycaster.rs +++ b/src/render/volume_raycaster.rs @@ -15,6 +15,7 @@ use std::collections::HashMap; use std::sync::Mutex; use egui_wgpu::{RenderState, wgpu}; +use half::f16; /// Identifies one raycaster view within the shared resources (caller-assigned, /// like `Scene3dId`). @@ -57,22 +58,34 @@ pub fn volume_bounds(depth: usize, height: usize, width: usize) -> ([f32; 3], [f ([-half[0], -half[1], -half[2]], half) } -/// Premultiply a straight-alpha RGBA8 buffer (`rgb *= a/255`, rounded), so the -/// linear sampler interpolates premultiplied colour: a transparent voxel becomes -/// `(0,0,0,0)` and interpolating it against an opaque colour keeps the hue -/// instead of dragging it toward black. A trailing partial pixel (len not a -/// multiple of 4) is dropped. +/// Premultiply a straight-alpha RGBA8 buffer into `Rgba16Float` texels (`rgb *= +/// a`, both normalised to `[0, 1]`), so the linear sampler interpolates +/// premultiplied colour: a transparent voxel becomes `(0,0,0,0)` and +/// interpolating it against an opaque colour keeps the hue instead of dragging +/// it toward black. A trailing partial pixel (len not a multiple of 4) is +/// dropped. +/// +/// The shader divides the sampled colour back out by the sampled coverage to +/// recover the straight RGB (`gain = sa_c / s.a`), so the *product* `rgb · a` +/// must carry the straight RGB to the input's own 8-bit precision. An 8-bit +/// product cannot: at coverage `a` it quantises the recoverable straight value +/// to steps of `1/a`, which for the low coverages a ray-march is built on +/// (`a = 3/255`) is three colour levels total. `f16` holds every `c·a/255²` to +/// ~0.05% relative, so the recovered RGB is exact to well under one 8-bit step +/// at every coverage down to 1/255. fn premultiply_rgba(rgba: &[u8]) -> Vec { rgba.chunks_exact(4) .flat_map(|px| { - let a = px[3] as u16; - [ - ((px[0] as u16 * a + 127) / 255) as u8, - ((px[1] as u16 * a + 127) / 255) as u8, - ((px[2] as u16 * a + 127) / 255) as u8, - px[3], - ] + let a = f32::from(px[3]) / 255.0; + let texel = [ + f16::from_f32(f32::from(px[0]) / 255.0 * a), + f16::from_f32(f32::from(px[1]) / 255.0 * a), + f16::from_f32(f32::from(px[2]) / 255.0 * a), + f16::from_f32(a), + ]; + texel.map(f16::to_le_bytes) }) + .flatten() .collect() } @@ -182,6 +195,9 @@ struct VolumeGpu { texture: Option, vol_min: [f32; 3], vol_max: [f32; 3], + /// Live claims on this id — see [`acquire_volume_raycaster`]. The entry, and + /// with it the texture, exists exactly while this is non-zero. + claims: u32, } impl VolumeGpu { @@ -190,13 +206,16 @@ impl VolumeGpu { texture: None, vol_min: [-0.5, -0.5, -0.5], vol_max: [0.5, 0.5, 0.5], + claims: 0, } } /// Upload a `(depth, height, width)` RGBA8 volume (row-major, straight /// alpha), (re)building the 3D texture. The straight alpha is premultiplied - /// before upload so the linear sampler interpolates premultiplied colour (no - /// dark fringe at colour/transparent boundaries). + /// into `Rgba16Float` before upload so the linear sampler interpolates + /// premultiplied colour (no dark fringe at colour/transparent boundaries) + /// while the product still carries the straight RGB at low coverage — see + /// [`premultiply_rgba`]. Costs twice the VRAM of an 8-bit texture. fn upload( &mut self, device: &wgpu::Device, @@ -218,7 +237,7 @@ impl VolumeGpu { mip_level_count: 1, sample_count: 1, dimension: wgpu::TextureDimension::D3, - format: wgpu::TextureFormat::Rgba8Unorm, + format: wgpu::TextureFormat::Rgba16Float, usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST, view_formats: &[], }); @@ -232,7 +251,7 @@ impl VolumeGpu { &premul, wgpu::TexelCopyBufferLayout { offset: 0, - bytes_per_row: Some(w * 4), + bytes_per_row: Some(w * 8), // Rgba16Float rows_per_image: Some(h), }, extent, @@ -323,8 +342,46 @@ pub fn install_volume_raycaster(render_state: &RenderState) { renderer.callback_resources.insert(resources); } +/// Take a claim on view `id`'s GPU entry, creating it if this is the first. +/// +/// The entry — and the VRAM of the texture it holds — lives exactly as long as +/// its claims: it is created here and dropped by the +/// [`release_volume_raycaster`] that takes the count back to zero. Nothing else +/// inserts or removes an entry, so a view can never free a texture another view +/// with the same [`VolumeId`] is still rendering. Requires +/// [`install_volume_raycaster`] first. +pub fn acquire_volume_raycaster(render_state: &RenderState, id: VolumeId) { + let mut renderer = render_state.renderer.write(); + let res: &mut VolumeRaycasterResources = renderer + .callback_resources + .get_mut() + .expect("VolumeRaycasterResources not installed — call install_volume_raycaster() first"); + res.scenes.entry(id).or_insert_with(VolumeGpu::new).claims += 1; +} + +/// Drop one claim on view `id`, freeing its 3D texture once the last claim goes. +/// A no-op if `id` holds no claim or the pipeline is not installed. +pub fn release_volume_raycaster(render_state: &RenderState, id: VolumeId) { + let mut renderer = render_state.renderer.write(); + let Some(res) = renderer + .callback_resources + .get_mut::() + else { + return; + }; + let Some(scene) = res.scenes.get_mut(&id) else { + return; + }; + scene.claims = scene.claims.saturating_sub(1); + if scene.claims == 0 { + res.scenes.remove(&id); + } +} + /// Upload view `id`'s volume as a `(depth, height, width)` RGBA8 texture -/// (row-major, straight alpha). Requires [`install_volume_raycaster`] first. +/// (row-major, straight alpha). Requires a claim from +/// [`acquire_volume_raycaster`]; without one the call is a no-op, so an upload +/// can never resurrect an entry whose last holder released it. pub fn set_volume_raycaster( render_state: &RenderState, id: VolumeId, @@ -351,7 +408,10 @@ pub fn set_volume_raycaster( .callback_resources .get_mut() .expect("VolumeRaycasterResources not installed — call install_volume_raycaster() first"); - let scene = res.scenes.entry(id).or_insert_with(VolumeGpu::new); + let Some(scene) = res.scenes.get_mut(&id) else { + debug_assert!(false, "set_volume_raycaster: id {id} holds no claim"); + return; + }; scene.upload( &render_state.device, &render_state.queue, @@ -360,20 +420,6 @@ pub fn set_volume_raycaster( ); } -/// Drop view `id`'s GPU resources (the 3D texture), freeing its VRAM. A no-op if -/// `id` was never uploaded or the pipeline is not installed. Call when a -/// raycaster view goes away so its texture does not linger in -/// `callback_resources` for the app's lifetime. -pub fn remove_volume_raycaster(render_state: &RenderState, id: VolumeId) { - let mut renderer = render_state.renderer.write(); - if let Some(res) = renderer - .callback_resources - .get_mut::() - { - res.scenes.remove(&id); - } -} - /// egui paint callback. `prepare` builds this frame's own bind group (fresh /// uniform buffer for the camera + the view's texture) and stashes it in `ready`; /// `paint` ray-marches with it. Because the bind group is per-callback rather @@ -444,6 +490,15 @@ pub fn paint_volume_raycaster(ui: &mut egui::Ui, rect: egui::Rect, frame: Volume #[cfg(test)] mod tests { use super::premultiply_rgba; + use half::f16; + + /// Decode one `Rgba16Float` texel back to `[f32; 4]`, as the sampler does. + fn texel(out: &[u8], i: usize) -> [f32; 4] { + std::array::from_fn(|c| { + let o = i * 8 + c * 2; + f16::from_le_bytes([out[o], out[o + 1]]).to_f32() + }) + } #[test] fn premultiply_zeroes_transparent_and_keeps_opaque() { @@ -455,15 +510,36 @@ mod tests { 255, 255, 255, 128, // half coverage → ~half ]; let out = premultiply_rgba(&src); - assert_eq!(&out[0..4], &[255, 0, 0, 255]); - assert_eq!(&out[4..8], &[0, 0, 0, 0]); - assert_eq!(out[11], 128); // alpha preserved - assert_eq!(out[8], 128); // 255*128/255 rounded + assert_eq!(texel(&out, 0), [1.0, 0.0, 0.0, 1.0]); + assert_eq!(texel(&out, 1), [0.0; 4]); + let half = texel(&out, 2); + assert!((half[3] - 128.0 / 255.0).abs() < 1e-3, "alpha preserved"); + assert!((half[0] - half[3]).abs() < 1e-3, "white × a == a"); + } + + /// The shader recovers the straight RGB as `s.rgb / s.a`. Boundary: the + /// lowest coverages, where an 8-bit premultiplied product would quantise + /// that quotient to steps of `1/a` — at `a = 3` the recovered channel could + /// only be 0, 1/3, 2/3 or 1, so `200/255 = 0.784` would read as `0.667`. + #[test] + fn premultiply_recovers_the_straight_rgb_at_the_lowest_coverages() { + for a in [1u8, 2, 3, 4, 8, 128, 255] { + for c in [1u8, 37, 128, 200, 254, 255] { + let out = premultiply_rgba(&[c, 0, 0, a]); + let t = texel(&out, 0); + let recovered = t[0] / t[3]; + let want = f32::from(c) / 255.0; + assert!( + (recovered - want).abs() < 0.5 / 255.0, + "a={a} c={c}: recovered {recovered}, want {want}" + ); + } + } } #[test] fn premultiply_drops_trailing_partial_pixel() { let src = [255, 0, 0, 255, 1, 2]; // 6 bytes: one pixel + 2 stragglers - assert_eq!(premultiply_rgba(&src).len(), 4); + assert_eq!(premultiply_rgba(&src).len(), 8); // one Rgba16Float texel } } diff --git a/src/widget/actions/control.rs b/src/widget/actions/control.rs index 9d18e57..a6dae04 100644 --- a/src/widget/actions/control.rs +++ b/src/widget/actions/control.rs @@ -67,9 +67,10 @@ pub const ZOOM_STEP: f64 = 1.1; /// axis guarantees `min`/`max`/`center > 0` by construction): the three inputs /// are mapped through `log10`, scaled linearly there, then mapped back via /// `10^x` — identical to silx's log branch. silx additionally clips the result -/// to the float32 safe range; that clip is the separately-tracked float32-safety -/// zoom item and is intentionally not applied here. Pure and deterministic so the -/// zoom math is unit-testable without a GPU backend. +/// to the float32 safe range; here that clip belongs to the view-limits owner +/// (`set_limits_internal`), which every commit path funnels through, so this +/// function stays pure and deterministic and the zoom math is unit-testable +/// without a GPU backend. /// /// [`Scale::Log10`]: crate::core::transform::Scale::Log10 pub fn scale_1d_range(min: f64, max: f64, center: f64, scale: f64, is_log: bool) -> (f64, f64) { diff --git a/src/widget/chrome.rs b/src/widget/chrome.rs index ed7d76d..74bbfee 100644 --- a/src/widget/chrome.rs +++ b/src/widget/chrome.rs @@ -584,13 +584,13 @@ pub fn draw_axes_with_x_tick_mode( x_time_offset: f64, ) { let area = t.area; - let axis = Stroke::new(1.0, style.axis); - let grid = Stroke::new(1.0, style.grid); + let axis = Stroke::new(1.0_f32, style.axis); + let grid = Stroke::new(1.0_f32, style.grid); // `style.grid` is itself translucent (alpha 28), so a premultiplied read + // rewrap would crush the minor-grid RGB toward black; `with_alpha` keeps the // straight RGB and just halves the alpha. let minor_grid = Stroke::new( - 1.0, + 1.0_f32, crate::core::color::with_alpha(style.grid, style.grid.a() / 2), ); let font = FontId::proportional(11.0); @@ -690,7 +690,7 @@ pub fn draw_axes_with_x_tick_mode( /// (`doc/design.md` §13 A5). pub fn draw_y2_ticks(painter: &Painter, t: &Transform, style: &Style) { let area = t.area; - let axis = Stroke::new(1.0, style.axis); + let axis = Stroke::new(1.0_f32, style.axis); let font = FontId::proportional(11.0); let tick_len = 4.0; @@ -727,7 +727,7 @@ pub fn draw_extra_y_ticks( style: &Style, ) { let area = t.area; - let axis = Stroke::new(1.0, style.axis); + let axis = Stroke::new(1.0_f32, style.axis); let font = FontId::proportional(11.0); let tick_len = 4.0; @@ -1048,7 +1048,7 @@ fn draw_roi_handles( HandleKind::Translate | HandleKind::Center => { // '+' glyph (silx translate handle symbol). let r = 4.0; - let stroke = Stroke::new(1.5, color); + let stroke = Stroke::new(1.5_f32, color); painter.line_segment([pos2(p.x - r, p.y), pos2(p.x + r, p.y)], stroke); painter.line_segment([pos2(p.x, p.y - r), pos2(p.x, p.y + r)], stroke); } @@ -1837,7 +1837,7 @@ pub fn draw_crosshair( x_time_zone: TimeZone, ) { let area = t.area; - let line = Stroke::new(1.0, style.axis); + let line = Stroke::new(1.0_f32, style.axis); painter.vline(pos.x, area.y_range(), line); painter.hline(area.x_range(), pos.y, line); @@ -1917,12 +1917,12 @@ pub fn draw_colorbar(painter: &Painter, rect: Rect, cmap: &Colormap, style: &Sty painter.rect_stroke( rect, egui::CornerRadius::ZERO, - Stroke::new(1.0, style.axis), + Stroke::new(1.0_f32, style.axis), egui::StrokeKind::Inside, ); let font = FontId::proportional(11.0); - let axis = Stroke::new(1.0, style.axis); + let axis = Stroke::new(1.0_f32, style.axis); if cmap.vmax <= cmap.vmin { return; } diff --git a/src/widget/colorbar.rs b/src/widget/colorbar.rs index e428e8d..366d9c4 100644 --- a/src/widget/colorbar.rs +++ b/src/widget/colorbar.rs @@ -106,7 +106,7 @@ impl ColorBarWidget { /// from [`Self::ui`] so callers that own a painter (e.g. chrome) can reuse /// the drawing without re-allocating. pub fn paint(&self, painter: &egui::Painter, rect: Rect, fg: Color32) { - let stroke = Stroke::new(1.0, fg); + let stroke = Stroke::new(1.0_f32, fg); // Reserve the legend strip first; the bar + ticks fill what remains. let (bar_area, legend_area) = self.split_legend(rect); let bar_rect = self.bar_rect(bar_area); @@ -232,7 +232,7 @@ impl ColorBarWidget { let length = self.bar_length(bar_rect) as f64; let nticks = optimal_nb_ticks(length, DEFAULT_TICK_DENSITY); let layout = self.tick_layout(nticks); - let stroke = Stroke::new(1.0, fg); + let stroke = Stroke::new(1.0_f32, fg); let font = FontId::proportional(TICK_FONT_SIZE); for &v in &layout.sub_ticks { diff --git a/src/widget/colormap_dialog.rs b/src/widget/colormap_dialog.rs index 3dd0ca5..f720e13 100644 --- a/src/widget/colormap_dialog.rs +++ b/src/widget/colormap_dialog.rs @@ -567,7 +567,7 @@ impl ColormapDialog { } // vmin / vmax markers where they fall within the data range. - let marker = egui::Stroke::new(1.0, ui.visuals().text_color()); + let marker = egui::Stroke::new(1.0_f32, ui.visuals().text_color()); for v in [vmin, vmax] { if v >= dmin && v <= dmax { let x = x_of(v); diff --git a/src/widget/high_level.rs b/src/widget/high_level.rs index 3afa25c..efc7f8d 100644 --- a/src/widget/high_level.rs +++ b/src/widget/high_level.rs @@ -643,6 +643,23 @@ fn validate_image_len(width: u32, height: u32, actual: usize) -> Result (bool, &'static str) { + let tooltip = match (x_autoscale, y_autoscale) { + (true, false) => "Auto-scale the x-axis of the graph only", + (false, true) => "Auto-scale the y-axis of the graph only", + _ => "Auto-scale the graph", + }; + (x_autoscale || y_autoscale, tooltip) +} + fn toolbar_icon_button( ui: &mut egui::Ui, icon: ToolbarIcon, @@ -688,7 +705,7 @@ fn draw_toolbar_button( } fn draw_toolbar_icon(painter: &egui::Painter, rect: egui::Rect, icon: ToolbarIcon, color: Color32) { - let stroke = egui::Stroke::new(1.6, color); + let stroke = egui::Stroke::new(1.6_f32, color); match icon { ToolbarIcon::Home => draw_home_icon(painter, rect, stroke), ToolbarIcon::Select => draw_select_icon(painter, rect, stroke), @@ -3351,7 +3368,7 @@ fn draw_legend_row(ui: &egui::Ui, p: LegendRowDraw<'_>) { egui::pos2(row_rect.left(), row_rect.bottom()), egui::pos2(row_rect.right(), row_rect.bottom()), ], - egui::Stroke::new(1.0, visuals.widgets.noninteractive.bg_stroke.color), + egui::Stroke::new(1.0_f32, visuals.widgets.noninteractive.bg_stroke.color), ); } @@ -3371,7 +3388,7 @@ fn draw_legend_swatch( let a = egui::pos2(rect.left() + 4.0, y); let b = egui::pos2(rect.right() - 4.0, y); if visual.line_style.draws_line() { - let stroke = egui::Stroke::new(2.0, visual.color); + let stroke = egui::Stroke::new(2.0_f32, visual.color); match visual.line_style.painter_dashes(stroke.width) { None => { painter.line_segment([a, b], stroke); @@ -3408,7 +3425,7 @@ fn draw_legend_swatch( painter.rect_stroke( bar, 0.0, - egui::Stroke::new(1.0, visual.color), + egui::Stroke::new(1.0_f32, visual.color), egui::StrokeKind::Inside, ); } @@ -3448,7 +3465,7 @@ fn draw_legend_swatch( painter.add(egui::Shape::convex_polygon( points, visual.color.linear_multiply(0.45), - egui::Stroke::new(1.0, visual.color), + egui::Stroke::new(1.0_f32, visual.color), )); } PlotItemKind::Shape => { @@ -3456,13 +3473,13 @@ fn draw_legend_swatch( painter.rect_stroke( shape, 0.0, - egui::Stroke::new(1.5, visual.color), + egui::Stroke::new(1.5_f32, visual.color), egui::StrokeKind::Inside, ); } PlotItemKind::Marker => { let center = rect.center(); - let stroke = egui::Stroke::new(1.8, visual.color); + let stroke = egui::Stroke::new(1.8_f32, visual.color); painter.line_segment( [ egui::pos2(center.x - 7.0, center.y), @@ -3495,7 +3512,7 @@ fn draw_legend_symbol( color: Color32, ) { let c = center; - let stroke = egui::Stroke::new(1.5, color); + let stroke = egui::Stroke::new(1.5_f32, color); // Apex-then-arms helper for the open carets. let caret = |apex: egui::Pos2, arm_a: egui::Pos2, arm_b: egui::Pos2| { painter.line_segment([apex, arm_a], stroke); @@ -3653,14 +3670,14 @@ fn draw_legend_eye( crate::core::color::with_alpha(color, 180) }; if visible { - painter.circle_stroke(egui::pos2(cx, cy), r, egui::Stroke::new(1.5, eye_color)); + painter.circle_stroke(egui::pos2(cx, cy), r, egui::Stroke::new(1.5_f32, eye_color)); painter.circle_filled(egui::pos2(cx, cy), r * 0.45, eye_color); } else { let dim = crate::core::color::with_alpha(color, 80); - painter.circle_stroke(egui::pos2(cx, cy), r, egui::Stroke::new(1.5, dim)); + painter.circle_stroke(egui::pos2(cx, cy), r, egui::Stroke::new(1.5_f32, dim)); painter.line_segment( [egui::pos2(cx - r * 1.3, cy), egui::pos2(cx + r * 1.3, cy)], - egui::Stroke::new(1.5, dim), + egui::Stroke::new(1.5_f32, dim), ); } } @@ -4157,6 +4174,14 @@ impl PlotWidget { } } + /// The single owner for every view-limits commit: each axis range passes + /// through [`clamp_axis_limits`] here, so no caller can install an + /// inverted, degenerate, or float32-unsafe range and the `Transform` + /// precondition (`min < max`, positive under log) holds by construction. + /// silx enforces the same invariant by running `Axis._checkLimits` on X, Y + /// and — when present — Y2 inside `PlotWidget.setLimits` + /// (`PlotWidget.py:2723-2730` → `items/axis.py:145-154` → + /// `_utils/panzoom.py:49-75`). fn set_limits_internal( &mut self, xmin: f64, @@ -4165,6 +4190,13 @@ impl PlotWidget { ymax: f64, y2: Option<(f64, f64)>, ) { + let x_log = self.backend.plot().x_scale == Scale::Log10; + let y_log = self.backend.plot().y_scale == Scale::Log10; + let (xmin, xmax) = clamp_axis_limits(xmin, xmax, x_log); + let (ymin, ymax) = clamp_axis_limits(ymin, ymax, y_log); + // silx scales the right axis with the left Y log state (`setLimits` + // takes `getYAxis(axis="right")`, whose `_isLogarithmic` tracks left). + let y2 = y2.map(|(lo, hi)| clamp_axis_limits(lo, hi, y_log)); let before = self.limits_snapshot(); self.backend.set_limits(xmin, xmax, ymin, ymax, y2); self.push_limits_changed_if(before); @@ -6479,7 +6511,16 @@ impl PlotWidget { fn show_toolbar_controls(&mut self, ui: &mut egui::Ui, out: &mut ToolbarResponse) { // Essential controls, always in the row: reset zoom, the box-zoom / // pan interaction modes, Y invert, and keep-aspect. - if toolbar_icon_button(ui, ToolbarIcon::Home, false, "Reset zoom").clicked() { + // silx greys out Reset Zoom when neither axis autoscales, and names the + // single autoscaled axis in the tooltip (`actions/control.py:83-95`). + let plot = self.backend.plot(); + let (enabled, tooltip) = reset_zoom_affordance(plot.x_autoscale(), plot.y_autoscale()); + let clicked = ui + .add_enabled_ui(enabled, |ui| { + toolbar_icon_button(ui, ToolbarIcon::Home, false, tooltip).clicked() + }) + .inner; + if clicked { self.reset_zoom(); out.reset_zoom = true; } @@ -6818,13 +6859,23 @@ impl PlotWidget { self.set_limits_internal(xmin, xmax, ymin, ymax, self.backend.plot().y2); } YAxis::Right => { - let before = self.limits_snapshot(); - self.backend.plot_mut().y2 = Some((ymin, ymax)); - self.push_limits_changed_if(before); + // Route through the owner so y2 gets the same `_checkLimits` + // repair X and Y get; X/Y are re-committed unchanged. + let (xmin, xmax, cur_ymin, cur_ymax) = self.backend.plot().limits; + self.set_limits_internal(xmin, xmax, cur_ymin, cur_ymax, Some((ymin, ymax))); } YAxis::Extra(n) => { + // Extra axes carry their own scale, so they repair against it. + let is_log = self + .backend + .plot() + .extra_axes() + .get(n) + .map(|ax| ax.scale == Scale::Log10) + .unwrap_or(false); + let (lo, hi) = clamp_axis_limits(ymin, ymax, is_log); if let Some(ax) = self.backend.plot_mut().extra_axis_mut(n) { - ax.range = Some((ymin, ymax)); + ax.range = Some((lo, hi)); } } } @@ -6917,8 +6968,8 @@ impl PlotWidget { } } }; - // silx writes via Axis.setLimits → _checkLimits (checkAxisLimits). - let (lo, hi) = clamp_axis_limits(lo, hi, true); + // `set_graph_x_limits` funnels into `set_limits_internal`, which runs + // the `_checkLimits` repair; the axis is already Log10 by this point. self.set_graph_x_limits(lo, hi); } @@ -6968,8 +7019,8 @@ impl PlotWidget { } } }; - // silx writes via Axis.setLimits → _checkLimits (checkAxisLimits). - let (lo, hi) = clamp_axis_limits(lo, hi, true); + // `set_graph_y_limits` funnels into `set_limits_internal`, which runs + // the `_checkLimits` repair; the axis is already Log10 by this point. self.set_graph_y_limits(lo, hi, YAxis::Left); } @@ -11769,7 +11820,7 @@ impl ImageView { roi, (self.width, self.height), self.profile_window.line_width(), - egui::Stroke::new(1.5, PROFILE_OVERLAY_COLOR), + egui::Stroke::new(1.5_f32, PROFILE_OVERLAY_COLOR), ); } @@ -12639,7 +12690,7 @@ impl ScatterView { roi, (0, 0), 1, - egui::Stroke::new(1.5, PROFILE_OVERLAY_COLOR), + egui::Stroke::new(1.5_f32, PROFILE_OVERLAY_COLOR), ); } @@ -14048,7 +14099,7 @@ impl StackView { roi, (self.width, self.height), self.profile_window.line_width(), - egui::Stroke::new(1.5, PROFILE_OVERLAY_COLOR), + egui::Stroke::new(1.5_f32, PROFILE_OVERLAY_COLOR), ); } @@ -15181,6 +15232,30 @@ mod tests { assert_eq!(plot.limits, (10.0, 20.0, 0.0, 1.0)); } + /// One case per combination of the two autoscale flags, matching silx's + /// `_autoscaleChanged` table (`actions/control.py:83-95`). + #[test] + fn reset_zoom_affordance_matches_silx_enable_and_tooltip() { + assert_eq!( + reset_zoom_affordance(true, true), + (true, "Auto-scale the graph") + ); + assert_eq!( + reset_zoom_affordance(true, false), + (true, "Auto-scale the x-axis of the graph only") + ); + assert_eq!( + reset_zoom_affordance(false, true), + (true, "Auto-scale the y-axis of the graph only") + ); + // Disabled, and silx reuses the both-on wording rather than inventing a + // fourth string. + assert_eq!( + reset_zoom_affordance(false, false), + (false, "Auto-scale the graph") + ); + } + #[test] fn widget_reset_with_all_autoscale_off_is_noop() { let mut plot = Plot::new(0); @@ -16384,10 +16459,11 @@ mod tests { #[test] fn scatter_binned_statistic_mode_means_and_nan_empty() { // Item 2: BINNED_STATISTIC converts (x,y,value) to per-bin means; - // two points in bin (0,0) average, an empty bin is NaN. - let x = [0.0, 0.5, 2.0]; - let y = [0.0, 0.5, 2.0]; - let v = [10.0, 30.0, 7.0]; + // two points in bin (0,0) average, an empty bin is NaN. The corner + // point (2,2) pins the extent and is dropped by the open upper edge. + let x = [0.0, 0.5, 1.5, 2.0]; + let y = [0.0, 0.5, 1.5, 2.0]; + let v = [10.0, 30.0, 7.0, 99.0]; let img = scatter_grid_image(ScatterVisualization::BinnedStatistic, &x, &y, &v, (2, 2)) .expect("binned"); assert_eq!(img.shape, (2, 2)); diff --git a/src/widget/histogram_colorbar.rs b/src/widget/histogram_colorbar.rs index c9884e5..4dcf7d0 100644 --- a/src/widget/histogram_colorbar.rs +++ b/src/widget/histogram_colorbar.rs @@ -467,7 +467,7 @@ impl HistogramColorBar { painter.rect_stroke( bar_rect, egui::CornerRadius::ZERO, - Stroke::new(1.0, fg), + Stroke::new(1.0_f32, fg), egui::StrokeKind::Inside, ); @@ -497,7 +497,7 @@ impl HistogramColorBar { let outline = Color32::WHITE; painter.line_segment( [pos2(columns.hist_left, y), pos2(bar_rect.right(), y)], - Stroke::new(1.5, accent), + Stroke::new(1.5_f32, accent), ); let tri = vec![ pos2(bar_rect.left() - 2.0 * HANDLE_TRI, y - HANDLE_TRI), @@ -507,7 +507,7 @@ impl HistogramColorBar { painter.add(Shape::convex_polygon( tri, accent, - Stroke::new(1.0, outline), + Stroke::new(1.0_f32, outline), )); // Value label right of the strip, clamped inside the bar span. RIGHT- diff --git a/src/widget/interaction.rs b/src/widget/interaction.rs index 8b92a8d..307ad21 100644 --- a/src/widget/interaction.rs +++ b/src/widget/interaction.rs @@ -322,6 +322,35 @@ pub fn constrain_zoom_axes( /// how many frames it spans (`egui/src/input_state/wheel_state.rs`). const WHEEL_NOTCH_POINTS: f64 = 40.0; +/// Which axes a wheel zoom moves, as `(x, y)` — `y` also governs y2, exactly as +/// silx's `EnabledAxes(…, yaxis=shiftPressed, y2axis=shiftPressed)` does. +/// +/// silx `_onWheel` (`PlotInteraction.py:1905-1917`): keep-aspect zooms every +/// axis and ignores both the flags and the modifiers. Otherwise Shift or Alt +/// *overrides* the per-axis zoom flags — Alt zooms X, Shift zooms Y/Y2, both +/// together zoom both — and with neither held the flags apply. The axes-menu +/// labels advertise the chords ("X axis (Alt+Wheel)", "Y left axis +/// (Shift+Wheel)", `tools/menus.py:55-57`). +/// +/// `(false, false)` means the wheel does not zoom at all (silx +/// `enabledAxes.isDisabled()`). +#[must_use] +pub fn wheel_enabled_axes( + keep_aspect: bool, + shift: bool, + alt: bool, + zoom_x: bool, + zoom_y: bool, +) -> (bool, bool) { + if keep_aspect { + (true, true) + } else if shift || alt { + (alt, shift) + } else { + (zoom_x, zoom_y) + } +} + /// Convert an egui wheel delta (`smooth_scroll_delta.y`, points) to a zoom /// factor for [`zoom_about`]. Scrolling up (`> 0`) zooms in (`factor < 1`). /// @@ -2231,6 +2260,52 @@ mod tests { )); } + /// One case per branch of silx's `_onWheel` axis selection, not one per + /// user gesture. + #[test] + fn wheel_enabled_axes_follows_silx_modifier_override() { + // Keep-aspect wins over every flag and every modifier. + assert_eq!( + wheel_enabled_axes(true, false, false, false, false), + (true, true) + ); + assert_eq!( + wheel_enabled_axes(true, true, false, false, false), + (true, true) + ); + + // No modifier: the per-axis zoom flags apply verbatim. + assert_eq!( + wheel_enabled_axes(false, false, false, true, true), + (true, true) + ); + assert_eq!( + wheel_enabled_axes(false, false, false, true, false), + (true, false) + ); + assert_eq!( + wheel_enabled_axes(false, false, false, false, false), + (false, false), + "both disabled: the wheel does not zoom" + ); + + // Alt zooms X only, Shift zooms Y (and Y2) only — overriding the flags, + // so a disabled axis still zooms when its chord is held. + assert_eq!( + wheel_enabled_axes(false, false, true, false, true), + (true, false) + ); + assert_eq!( + wheel_enabled_axes(false, true, false, true, false), + (false, true) + ); + // Both chords together zoom both axes. + assert_eq!( + wheel_enabled_axes(false, true, true, false, false), + (true, true) + ); + } + #[test] fn wheel_factor_direction_and_neutral() { assert!(wheel_zoom_factor(100.0) < 1.0); diff --git a/src/widget/mask_tools.rs b/src/widget/mask_tools.rs index 9b38929..a5eea5c 100644 --- a/src/widget/mask_tools.rs +++ b/src/widget/mask_tools.rs @@ -1013,7 +1013,7 @@ impl MaskToolsWidget { )); painter.with_clip_rect(area).add(egui::Shape::dashed_line( &outline, - egui::Stroke::new(1.5, self.color), + egui::Stroke::new(1.5_f32, self.color), 6.0, 4.0, )); diff --git a/src/widget/plot_widget.rs b/src/widget/plot_widget.rs index 1f779ba..b2d41c1 100644 --- a/src/widget/plot_widget.rs +++ b/src/widget/plot_widget.rs @@ -9,9 +9,12 @@ //! //! Mouse mapping follows the active interaction mode: select mode uses primary //! drag for ROI handles, zoom mode uses primary drag for box zoom, pan mode uses -//! primary drag for panning. Secondary drag pans in every mode; a secondary -//! *click* opens a zoom context menu (Zoom Back / Reset Zoom). Wheel zoom -//! remains available. +//! primary drag for panning. Middle drag pans in every mode, as in silx +//! (`ItemsInteraction`'s `dragButtons=(LEFT_BTN, MIDDLE_BTN)`). Secondary drag +//! also pans — a recorded deviation, since silx reserves the right button for +//! click/context — and a secondary *click* opens a zoom context menu (Zoom Back +//! / Reset Zoom). Wheel zoom remains available, with Alt/Shift selecting the +//! X/Y axis. use egui::{PointerButton, Pos2, Rect, Sense, Stroke, Ui}; @@ -735,7 +738,7 @@ fn draw_selection_polygon( // approximation silx renders for the hatch fill). let clipped = painter.with_clip_rect(bb); for (a, b) in interaction::hatch_lines(bb, 6.0) { - clipped.line_segment([a, b], Stroke::new(1.0, style.color)); + clipped.line_segment([a, b], Stroke::new(1.0_f32, style.color)); } } FillMode::None => {} @@ -743,7 +746,7 @@ fn draw_selection_polygon( } // Dashed outline (silx linestyle="--"). - let stroke = Stroke::new(1.5, style.color); + let stroke = Stroke::new(1.5_f32, style.color); let mut outline = pix.to_vec(); if closed && outline.len() >= 2 { outline.push(outline[0]); @@ -1011,14 +1014,28 @@ fn apply_interaction( } } - // Pan: secondary-drag always pans; pan mode also binds primary-drag to pan. - // A marker drag pre-empts the primary-drag pan (secondary-drag pan is - // unaffected, matching silx — only the item-drag branch competes with the - // primary pan). + // Pan: middle-drag always pans, in every interaction mode — silx's + // `ItemsInteraction` sets `dragButtons=(LEFT_BTN, MIDDLE_BTN)` and routes + // MIDDLE to `Pan.beginDrag` (`PlotInteraction.py:1190-1192,1365-1367`), and + // `ZoomAndSelect`/`PanAndSelect`/`DrawSelectMode` all inherit it. Pan mode + // also binds primary-drag to pan. + // + // Secondary-drag pans too. That is a recorded rsplot deviation — silx + // reserves the right button for click/context (`clickButtons=(LEFT_BTN, + // RIGHT_BTN)`) — kept because the ROI gating contract depends on it + // (`doc/parity-roadmap.md:1175`) and the context menu opens on release + // regardless (see the `context_menu` registration below). + // + // A marker drag pre-empts the primary-drag pan only; middle and secondary + // pans are unaffected, matching silx — only the item-drag branch competes + // with the primary pan. let primary_pan = mode == PlotInteractionMode::Pan && !marker_dragging && response.dragged_by(PointerButton::Primary); - if response.dragged_by(PointerButton::Secondary) || primary_pan { + if response.dragged_by(PointerButton::Middle) + || response.dragged_by(PointerButton::Secondary) + || primary_pan + { // Push the pre-pan view once, at the start of the pan gesture, so // zoom-back can restore it (silx pushes on box-zoom; here the limits // history also captures pan gestures — push on drag-start, not every @@ -1055,14 +1072,17 @@ fn apply_interaction( && let Some(p) = response.hover_pos() && area.contains(p) { - // silx `_onWheel` (PlotInteraction.py:1894-1913): all axes zoom - // when keep-aspect is on; otherwise the per-axis zoom flags apply, + // silx `_onWheel` (PlotInteraction.py:1905-1917): keep-aspect zooms + // every axis; otherwise Shift/Alt override the per-axis zoom flags, // and with every axis disabled the wheel does not zoom at all. - let enabled = if plot.keep_aspect { - (true, true) - } else { - (plot.zoom_x_enabled(), plot.zoom_y_enabled()) - }; + let (shift, alt) = ui.input(|i| (i.modifiers.shift, i.modifiers.alt)); + let enabled = interaction::wheel_enabled_axes( + plot.keep_aspect, + shift, + alt, + plot.zoom_x_enabled(), + plot.zoom_y_enabled(), + ); if enabled != (false, false) { let centre = view.pixel_to_data(p); // The y2 zoom centre is the same pixel mapped through the @@ -2090,6 +2110,54 @@ mod tests { assert!(frac_left > 0.0, "downward drag raises the data Y window"); } + /// silx routes MIDDLE_BTN to `Pan.beginDrag` from `ItemsInteraction` + /// (`PlotInteraction.py:1365-1367`), which `ZoomAndSelect`, `PanAndSelect` + /// and `DrawSelectMode` all inherit — so the gesture must pan in *every* + /// mode, including the modes whose primary drag is spoken for. + #[test] + fn middle_drag_pans_in_every_interaction_mode() { + let screen = egui::vec2(200.0, 200.0); + for mode in [ + PlotInteractionMode::Zoom, + PlotInteractionMode::Pan, + PlotInteractionMode::Select, + PlotInteractionMode::MaskDraw, + PlotInteractionMode::RoiCreate(interaction::RoiDrawKind::Rect), + ] { + let ctx = egui::Context::default(); + let mut plot = Plot::new(0); + plot.limits = (0.0, 10.0, 0.0, 10.0); + + let (_r0, area) = run_mode_frame(&ctx, &mut plot, mode, screen_input(screen)); + let start = area.center(); + let end = start + egui::vec2(20.0, 0.0); + + let _ = run_mode_frame( + &ctx, + &mut plot, + mode, + press_button_at(screen, start, egui::PointerButton::Middle), + ); + let _ = run_mode_frame(&ctx, &mut plot, mode, move_to(screen, end)); + let _ = run_mode_frame( + &ctx, + &mut plot, + mode, + release_button_at(screen, end, egui::PointerButton::Middle), + ); + + let (x0, x1, _, _) = plot.limits; + assert!( + (x1 - x0 - 10.0).abs() <= 1e-9, + "{mode:?}: span preserved: {x0} {x1}" + ); + assert!( + x0 < -1e-9, + "{mode:?}: rightward middle drag must move the X window left, got {x0}" + ); + } + } + #[test] fn click_emits_clicked_event_with_correct_data_coords() { let ctx = egui::Context::default(); @@ -2471,11 +2539,15 @@ mod tests { } fn press_at(screen: egui::Vec2, p: Pos2) -> egui::RawInput { + press_button_at(screen, p, egui::PointerButton::Primary) + } + + fn press_button_at(screen: egui::Vec2, p: Pos2, button: egui::PointerButton) -> egui::RawInput { let mut raw = screen_input(screen); raw.events.push(egui::Event::PointerMoved(p)); raw.events.push(egui::Event::PointerButton { pos: p, - button: egui::PointerButton::Primary, + button, pressed: true, modifiers: egui::Modifiers::default(), }); @@ -2489,10 +2561,18 @@ mod tests { } fn release_at(screen: egui::Vec2, p: Pos2) -> egui::RawInput { + release_button_at(screen, p, egui::PointerButton::Primary) + } + + fn release_button_at( + screen: egui::Vec2, + p: Pos2, + button: egui::PointerButton, + ) -> egui::RawInput { let mut raw = screen_input(screen); raw.events.push(egui::Event::PointerButton { pos: p, - button: egui::PointerButton::Primary, + button, pressed: false, modifiers: egui::Modifiers::default(), }); diff --git a/src/widget/print_preview.rs b/src/widget/print_preview.rs index 3fa303c..d269b42 100644 --- a/src/widget/print_preview.rs +++ b/src/widget/print_preview.rs @@ -487,7 +487,7 @@ impl PrintPreview { painter.rect_stroke( scr, egui::CornerRadius::ZERO, - egui::Stroke::new(1.0, Color32::RED), + egui::Stroke::new(1.0_f32, Color32::RED), egui::StrokeKind::Inside, ); let h = Self::handle_rect(it.rect); diff --git a/src/widget/radar_view.rs b/src/widget/radar_view.rs index acd4697..faa4c0d 100644 --- a/src/widget/radar_view.rs +++ b/src/widget/radar_view.rs @@ -438,7 +438,7 @@ impl RadarView { painter.rect_stroke( data_px, 0.0, - Stroke::new(1.0, DATA_STROKE), + Stroke::new(1.0_f32, DATA_STROKE), egui::StrokeKind::Inside, ); @@ -446,7 +446,7 @@ impl RadarView { painter.rect_stroke( view_px, 0.0, - Stroke::new(2.0, VISIBLE_STROKE), + Stroke::new(2.0_f32, VISIBLE_STROKE), egui::StrokeKind::Inside, ); } diff --git a/src/widget/volume_raycaster.rs b/src/widget/volume_raycaster.rs index fb3b999..1d72a76 100644 --- a/src/widget/volume_raycaster.rs +++ b/src/widget/volume_raycaster.rs @@ -19,8 +19,8 @@ use crate::core::scene3d::camera::Camera; use crate::core::scene3d::interaction::{OrbitDrag, PanDrag, window_to_ndc}; use crate::core::scene3d::mat4::{Mat4, Vec3}; use crate::render::volume_raycaster::{ - VolumeFrame, VolumeId, install_volume_raycaster, paint_volume_raycaster, - remove_volume_raycaster, set_volume_raycaster, volume_bounds, + VolumeFrame, VolumeId, acquire_volume_raycaster, install_volume_raycaster, + paint_volume_raycaster, release_volume_raycaster, set_volume_raycaster, volume_bounds, }; /// Upper bound on samples per ray; beyond this a single frame can outrun the @@ -30,6 +30,14 @@ const MAX_STEPS: u32 = 4096; /// Interactive GPU direct-volume-rendering widget. See the module docs. pub struct VolumeRaycaster { id: VolumeId, + /// Kept so [`Drop`] can release this handle's claim on `id` without the + /// caller having to remember to. Cheap: `RenderState` is a bundle of `Arc`s. + render_state: RenderState, + /// Whether this handle currently holds a claim on `id`'s GPU entry. `new` + /// takes one, [`remove`](VolumeRaycaster::remove) and [`Drop`] give it back, + /// and [`set_volume`](VolumeRaycaster::set_volume) takes it again — so the + /// entry outlives exactly the handles that need it. + claimed: bool, camera: Camera, /// World-space axis-aligned box the volume occupies (centred, aspect-kept). bounds: (Vec3, Vec3), @@ -53,9 +61,12 @@ impl VolumeRaycaster { /// The `id` keys the uploaded 3D texture, so distinct volumes need distinct /// ids (two views sharing an id share the last-uploaded texture). Cameras are /// not shared: each paint builds its own uniforms, so same-id views still - /// render with their own viewpoint. + /// render with their own viewpoint. The texture is claimed for as long as any + /// view holding the id is alive, so dropping one such view leaves the others + /// rendering. pub fn new(render_state: &RenderState, id: VolumeId) -> Self { install_volume_raycaster(render_state); + acquire_volume_raycaster(render_state, id); let camera = Camera::new( 30.0, 0.1, @@ -67,6 +78,8 @@ impl VolumeRaycaster { ); Self { id, + render_state: render_state.clone(), + claimed: true, camera, bounds: (Vec3::new(-0.5, -0.5, -0.5), Vec3::new(0.5, 0.5, 0.5)), has_volume: false, @@ -89,6 +102,10 @@ impl VolumeRaycaster { height: usize, width: usize, ) { + if !self.claimed { + acquire_volume_raycaster(render_state, self.id); + self.claimed = true; + } set_volume_raycaster(render_state, self.id, rgba, depth, height, width); let (mn, mx) = volume_bounds(depth, height, width); self.bounds = (Vec3::from_array(mn), Vec3::from_array(mx)); @@ -120,12 +137,16 @@ impl VolumeRaycaster { self.camera.reset_camera(self.bounds); } - /// Free this view's uploaded GPU volume (the 3D texture). Its VRAM is - /// otherwise held for the app's lifetime, since the shared resources keep one - /// entry per id. After this the view paints nothing until the next - /// [`set_volume`](Self::set_volume). + /// Give back this view's claim on the shared 3D texture, freeing its VRAM + /// once no other view holds the same [`VolumeId`]. After this the view paints + /// nothing until the next [`set_volume`](Self::set_volume), which claims the + /// texture again. Dropping the view does the same, so calling this is only + /// needed to release early. pub fn remove(&mut self, render_state: &RenderState) { - remove_volume_raycaster(render_state, self.id); + if self.claimed { + release_volume_raycaster(render_state, self.id); + self.claimed = false; + } self.has_volume = false; } @@ -207,6 +228,18 @@ impl VolumeRaycaster { } } +/// Releasing the claim on drop is what makes the claim count track the live +/// views: without it a forgotten [`remove`](VolumeRaycaster::remove) would pin +/// the id's texture in `callback_resources` for the app's lifetime. +impl Drop for VolumeRaycaster { + fn drop(&mut self) { + if self.claimed { + release_volume_raycaster(&self.render_state, self.id); + self.claimed = false; + } + } +} + /// One `±0.2` zoom step per raw `MouseWheel` event (true = zoom in). Mirrors /// `SceneWidget`'s wheel handling: a fixed step per event, magnitude-independent, /// read from the raw events rather than the smoothed per-frame delta. diff --git a/tests/view_limits_repair.rs b/tests/view_limits_repair.rs new file mode 100644 index 0000000..300e7a2 --- /dev/null +++ b/tests/view_limits_repair.rs @@ -0,0 +1,123 @@ +//! Every view-limits commit passes through one owner (`set_limits_internal`) +//! that runs the silx `checkAxisLimits` repair, so no public entry point can +//! install an inverted, degenerate, or float32-unsafe range. +//! +//! silx enforces this in `PlotWidget.setLimits` (`PlotWidget.py:2723-2730`), +//! which runs `Axis._checkLimits` on X, Y and — when present — Y2 +//! (`items/axis.py:145-154` → `_utils/panzoom.py:49-75`). +//! +//! Cases are one per invariant boundary, not one per user story. +//! +//! Needs a GPU (real or software); mirrors `tests/limits_history_lifecycle.rs`. + +use egui_kittest::wgpu::{create_render_state, default_wgpu_setup}; +use rsplot::{AxisSide, Plot1D, YAxis}; + +const F32_SAFE_MAX: f64 = 1e37; +const F32_SAFE_MIN: f64 = -1e37; + +fn widget() -> Plot1D { + let rs = create_render_state(default_wgpu_setup()); + rsplot::install(&rs); + Plot1D::new(&rs, 0) +} + +/// Boundary: `vmax < vmin` on both axes — silx swaps rather than committing an +/// inverted range (`panzoom.py:57-59`). +#[test] +fn inverted_limits_are_swapped() { + let mut plot = widget(); + plot.set_limits(10.0, 0.0, 20.0, 5.0, None); + assert_eq!(plot.x_limits(), (0.0, 10.0)); + assert_eq!(plot.y_limits(YAxis::Left), Some((5.0, 20.0))); +} + +/// Boundary: `vmax == vmin == 0` — silx expands to `(-0.1, 0.1)` +/// (`panzoom.py:62-63`). Entry point: the X-only setter. +#[test] +fn degenerate_zero_range_expands_to_pm_point_one() { + let mut plot = widget(); + plot.set_graph_x_limits(0.0, 0.0); + assert_eq!(plot.x_limits(), (-0.1, 0.1)); +} + +/// Boundary: `vmax == vmin > 0` — silx expands to `(0.9v, 1.1v)` +/// (`panzoom.py:67-69`). Entry point: the Y-left setter. +#[test] +fn degenerate_positive_range_expands_by_ten_percent() { + let mut plot = widget(); + plot.set_graph_y_limits(5.0, 5.0, YAxis::Left); + let (lo, hi) = plot.y_limits(YAxis::Left).expect("left axis has limits"); + assert!((lo - 4.5).abs() < 1e-12, "lo {lo}"); + assert!((hi - 5.5).abs() < 1e-12, "hi {hi}"); +} + +/// Boundary: bounds outside the float32-safe window are clipped to it +/// (`panzoom.py:54-55`), on both axes, through the all-axis setter. +#[test] +fn out_of_float32_range_limits_are_clipped() { + let mut plot = widget(); + plot.set_limits(-1e40, 1e40, -1e40, 1e40, None); + assert_eq!(plot.x_limits(), (F32_SAFE_MIN, F32_SAFE_MAX)); + assert_eq!( + plot.y_limits(YAxis::Left), + Some((F32_SAFE_MIN, F32_SAFE_MAX)) + ); +} + +/// Regression: the right axis used to be written raw, bypassing the owner. +/// silx runs `_checkLimits` on y2 too (`PlotWidget.py:2729-2730`). +#[test] +fn y2_limits_are_repaired_not_written_raw() { + let mut plot = widget(); + plot.set_graph_y_limits(5.0, 5.0, YAxis::Right); + let (lo, hi) = plot.y_limits(YAxis::Right).expect("right axis has limits"); + assert!((lo - 4.5).abs() < 1e-12, "lo {lo}"); + assert!((hi - 5.5).abs() < 1e-12, "hi {hi}"); + + plot.set_graph_y_limits(20.0, 5.0, YAxis::Right); + assert_eq!( + plot.y_limits(YAxis::Right), + Some((5.0, 20.0)), + "inverted y2" + ); +} + +/// Regression: an extra (stacked) axis used to be written raw. It repairs +/// against its own scale, which is what makes it a separate clamp call. +#[test] +fn extra_axis_limits_are_repaired_not_written_raw() { + let mut plot = widget(); + let ax = plot.add_extra_y_axis(AxisSide::Left); + plot.set_graph_y_limits(5.0, 5.0, YAxis::Extra(ax)); + let (lo, hi) = plot.y_limits(YAxis::Extra(ax)).expect("extra axis range"); + assert!((lo - 4.5).abs() < 1e-12, "lo {lo}"); + assert!((hi - 5.5).abs() < 1e-12, "hi {hi}"); + + plot.set_graph_y_limits(20.0, 5.0, YAxis::Extra(ax)); + assert_eq!(plot.y_limits(YAxis::Extra(ax)), Some((5.0, 20.0))); +} + +/// Boundary: the toolbar zoom path commits through the same owner, so +/// unbounded Zoom-Out saturates at the float32-safe window instead of growing +/// without limit. silx clamps this path twice (`scale1DRange` plus +/// `setLimits`); one owner is enough to hold the invariant. +#[test] +fn repeated_toolbar_zoom_out_stays_in_the_float32_window() { + let mut plot = widget(); + plot.set_limits(0.0, 1.0, 0.0, 1.0, None); + for _ in 0..1000 { + rsplot::actions::control::zoom_out(&mut plot); + } + let (x0, x1) = plot.x_limits(); + let (y0, y1) = plot.y_limits(YAxis::Left).expect("left axis has limits"); + for v in [x0, x1, y0, y1] { + assert!(v.is_finite(), "limit escaped to {v}"); + assert!( + (F32_SAFE_MIN..=F32_SAFE_MAX).contains(&v), + "limit {v} outside" + ); + } + assert!(x0 < x1, "x collapsed: {x0} {x1}"); + assert!(y0 < y1, "y collapsed: {y0} {y1}"); +} diff --git a/tests/volume_opacity_physics.rs b/tests/volume_opacity_physics.rs new file mode 100644 index 0000000..f43cec8 --- /dev/null +++ b/tests/volume_opacity_physics.rs @@ -0,0 +1,197 @@ +//! `VolumeRaycaster` has no silx counterpart — `plot3d/items/volume.py` ships an +//! `Isosurface` and a `CutPlane`, never a GPU direct-volume ray-caster — so its +//! sample model has no upstream contract to diff against. These tests pin the +//! *physics* instead, which is the only contract there is: what a marched ray +//! accumulates, in opacity and in colour. +//! +//! Beer–Lambert: a ray's transmittance through a uniform medium depends on the +//! thickness it traverses and not on how many samples the march happens to take. +//! Written as the two properties that must hold together: +//! +//! 1. **Thickness dependence** — a thinner slab of the same voxels is more +//! transparent. +//! 2. **Step-count invariance** — the same slab renders the same at any `steps`. +//! +//! Holding only (2) is what the step-count-ratio correction did; a uniform volume +//! then read as a flat silhouette. +//! +//! Colour: each sample contributes its *straight* RGB at the corrected coverage, +//! so a uniform slab composites its authored hue no matter how faint its voxels +//! are. +//! +//! The default camera looks straight down `-Z` (`VolumeRaycaster::new`), and +//! `volume_bounds` normalises the longest axis to one unit, so shrinking `depth` +//! shortens the centre ray's chord by exactly that ratio and nothing else. + +use egui_kittest::Harness; +use egui_kittest::wgpu::{WgpuTestRenderer, create_render_state, default_wgpu_setup}; +use rsplot::VolumeRaycaster; +use rsplot::egui; +use std::cell::RefCell; +use std::rc::Rc; +use std::sync::Mutex; + +const WIN: f32 = 320.0; + +/// See `tests/volume_raycaster_render.rs`: concurrent wgpu device create/destroy +/// faults on the Windows CI runner's DX12 software adapter. +static GPU: Mutex<()> = Mutex::new(()); + +/// A `depth × 16 × 16` RGBA8 volume, every voxel `voxel`. +fn slab(depth: usize, voxel: [u8; 4]) -> Vec { + voxel + .iter() + .copied() + .cycle() + .take(depth * 16 * 16 * 4) + .collect() +} + +struct App { + view: VolumeRaycaster, +} + +/// Red channel of the centre pixel after rendering a `depth × 16 × 16` slab of +/// uniform-alpha red voxels at `steps` samples per ray. +fn centre_red(depth: usize, alpha: u8, steps: u32) -> u8 { + centre_rgb(depth, [255, 0, 0, alpha], steps)[0] +} + +/// RGB of the centre pixel after rendering a `depth × 16 × 16` slab of uniform +/// `voxel`s at `steps` samples per ray. +/// +/// The centre pixel's ray crosses the box through its middle, so its chord is +/// the slab's Z extent — `depth / 16` of a unit, by `volume_bounds`. +fn centre_rgb(depth: usize, voxel: [u8; 4], steps: u32) -> [u8; 3] { + let rs = create_render_state(default_wgpu_setup()); + let mut view = VolumeRaycaster::new(&rs, 11); + view.set_volume(&rs, &slab(depth, voxel), depth, 16, 16); + view.set_steps(steps); + let app = Rc::new(RefCell::new(App { view })); + let renderer = WgpuTestRenderer::from_render_state(rs); + let app_ui = app.clone(); + let mut harness = Harness::builder() + .with_size(egui::vec2(WIN, WIN)) + .with_pixels_per_point(1.0) + .renderer(renderer) + .build_ui(move |ui| { + app_ui.borrow_mut().view.show(ui); + }); + harness.step(); + let image = harness.render().expect("headless wgpu render"); + let (iw, ih) = (image.width() as usize, image.height() as usize); + let centre = (ih / 2) * iw + iw / 2; + let raw = image.as_raw(); + [raw[centre * 4], raw[centre * 4 + 1], raw[centre * 4 + 2]] +} + +/// The RGB the centre pixel shows with nothing painted over it: an +/// all-transparent volume composites to the harness background exactly there. +fn background_rgb() -> [u8; 3] { + centre_rgb(16, [255, 0, 0, 0], 256) +} + +/// Red channel of that background. +fn background_red() -> u8 { + background_rgb()[0] +} + +/// Analytic accumulated opacity of the centre ray through a `depth × 16 × 16` +/// slab of uniform-`alpha` voxels. +/// +/// `volume_bounds` normalises the longest axis to one unit, so the box extent is +/// `(1, 1, depth/16)` and the centre ray's chord is `L = depth/16`. Each sample +/// is corrected to the reference spacing `dt_ref = |extent| / 256`, so the chord +/// transmittance telescopes to `(1 - alpha)^(L / dt_ref)` exactly — no +/// discretisation error, whatever the step count. +fn analytic_opacity(depth: usize, alpha: u8) -> f64 { + let ez = depth as f64 / 16.0; + let diag = (1.0 + 1.0 + ez * ez).sqrt(); + let sa = f64::from(alpha) / 255.0; + 1.0 - (1.0 - sa).powf(256.0 * ez / diag) +} + +/// Boundary: same voxels, four thicknesses. The rendered centre pixel must match +/// the Beer–Lambert prediction for its chord, not merely rise with it. +/// +/// The render target is linear and the volume composites with premultiplied +/// alpha over the harness background, so the expected red is +/// `opacity + background · (1 - opacity)` — the volume's own colour is pure red +/// at intensity `opacity`. +/// +/// The step-count-ratio correction made the exponent `REF_STEPS / steps`, which +/// carries no world distance: every chord accumulated `(1 - alpha)^steps`, so all +/// four depths rendered the same red (measured: `[225, 225, 225, 225]`). +#[test] +fn slab_opacity_matches_the_beer_lambert_prediction_for_its_chord() { + let _gpu = GPU.lock().unwrap_or_else(|e| e.into_inner()); + const ALPHA: u8 = 2; // faint enough that even the full cube stays unsaturated + + let bg = f64::from(background_red()) / 255.0; + for depth in [2usize, 4, 8, 16] { + let opacity = analytic_opacity(depth, ALPHA); + let expect = (opacity + bg * (1.0 - opacity)) * 255.0; + let got = f64::from(centre_red(depth, ALPHA, 256)); + assert!( + (got - expect).abs() <= 2.0, + "depth {depth}: chord {:.4}, opacity {opacity:.4} -> expected red {expect:.1}, got {got}", + depth as f64 / 16.0 + ); + } +} + +/// Boundary: one slab, four step counts. The march resolution must not change +/// the accumulated opacity — the property the count-ratio exponent did hold, and +/// which the path-length exponent must keep holding. +/// +/// This is a preservation test: it passes against the old exponent too. +#[test] +fn opacity_is_invariant_to_the_step_count() { + let _gpu = GPU.lock().unwrap_or_else(|e| e.into_inner()); + const ALPHA: u8 = 2; + + let reds: Vec = [64u32, 128, 256, 512] + .iter() + .map(|&s| centre_red(8, ALPHA, s)) + .collect(); + + let lo = *reds.iter().min().expect("four renders"); + let hi = *reds.iter().max().expect("four renders"); + assert!( + hi - lo <= 3, + "step count must not change the accumulated opacity, got {reds:?}" + ); +} + +/// Boundary: the lowest coverage a march is built on. A uniform slab must +/// composite its authored hue — each sample contributes its *straight* RGB at +/// the corrected coverage, so the accumulated colour is +/// `straight_rgb · opacity` over the background, independent of `alpha`. +/// +/// The 3D texture stores the *premultiplied* colour so the linear filter cannot +/// bleed a transparent voxel's hue, and `fs_main` divides the coverage back out +/// to recover the straight RGB. In `Rgba8Unorm` that product resolved the +/// quotient only to steps of `1/alpha`: at `alpha = 3` the stored green of an +/// orange voxel was `round(128·3/255) = 2`, so the shader read `2/3 = 0.667` +/// where the authored green is `128/255 = 0.502` — a 35-level hue shift in the +/// composite, which this test measures directly. +#[test] +fn low_coverage_voxels_composite_their_authored_hue() { + let _gpu = GPU.lock().unwrap_or_else(|e| e.into_inner()); + const VOXEL: [u8; 4] = [255, 128, 0, 3]; // faint orange + + let bg = background_rgb(); + let opacity = analytic_opacity(16, VOXEL[3]); + let got = centre_rgb(16, VOXEL, 256); + + for c in 0..3 { + let straight = f64::from(VOXEL[c]) / 255.0; + let bg_c = f64::from(bg[c]) / 255.0; + let expect = (straight * opacity + bg_c * (1.0 - opacity)) * 255.0; + assert!( + (f64::from(got[c]) - expect).abs() <= 2.0, + "channel {c}: expected {expect:.1}, got {} (all channels {got:?})", + got[c] + ); + } +} diff --git a/tests/volume_raycaster_render.rs b/tests/volume_raycaster_render.rs index 1b73fe5..4c72576 100644 --- a/tests/volume_raycaster_render.rs +++ b/tests/volume_raycaster_render.rs @@ -1,6 +1,8 @@ -//! Headless end-to-end check for `VolumeRaycaster`: an opaque RGBA volume must -//! ray-march to visible colour, and an all-transparent volume must not. Uses the -//! same `egui_kittest` wgpu harness as the scene tests (no window). +//! Headless end-to-end checks for `VolumeRaycaster`: an opaque RGBA volume must +//! ray-march to visible colour, an all-transparent volume must not, and the 3D +//! texture a `VolumeId` names must live exactly as long as the views holding +//! that id. Uses the same `egui_kittest` wgpu harness as the scene tests (no +//! window). use egui_kittest::Harness; use egui_kittest::wgpu::{WgpuTestRenderer, create_render_state, default_wgpu_setup}; @@ -74,6 +76,67 @@ fn red_pixels_for(rgba: [u8; 4]) -> usize { count_red(image.as_raw(), iw, ih) } +/// Render one view (already built and uploaded) and count its red pixels. +fn red_pixels_of(rs: &RenderState, view: VolumeRaycaster) -> usize { + let view = Rc::new(RefCell::new(view)); + let renderer = WgpuTestRenderer::from_render_state(rs.clone()); + let ui_view = view.clone(); + let mut harness = Harness::builder() + .with_size(egui::vec2(WIN, WIN)) + .with_pixels_per_point(1.0) + .renderer(renderer) + .build_ui(move |ui| { + ui_view.borrow_mut().show(ui); + }); + harness.step(); + let image = harness.render().expect("headless wgpu render"); + let (iw, ih) = (image.width() as usize, image.height() as usize); + count_red(image.as_raw(), iw, ih) +} + +/// Boundary: two live claims on one id, one given back. `remove` used to drop the +/// shared entry outright, so the surviving view's ray-march found no texture and +/// painted nothing. +#[test] +fn remove_keeps_the_texture_while_another_same_id_view_lives() { + let _gpu = GPU.lock().unwrap_or_else(|e| e.into_inner()); + let rs = create_render_state(default_wgpu_setup()); + + let mut uploader = VolumeRaycaster::new(&rs, 21); + uploader.set_volume(&rs, &solid(16, [255, 0, 0, 255]), 16, 16, 16); + let survivor = VolumeRaycaster::new(&rs, 21); // second claim on the same volume + uploader.remove(&rs); + + let red = red_pixels_of(&rs, survivor); + assert!( + red > 500, + "one view's remove() must not free a texture another view still renders; \ + only {red} red px" + ); +} + +/// Boundary: the last claim goes. Dropping the view must free the entry, so a +/// fresh view taking the same id starts empty — it used to inherit the dead +/// view's texture, which is the same leak that pinned the VRAM for the app's +/// lifetime. +#[test] +fn dropping_the_last_view_frees_the_texture() { + let _gpu = GPU.lock().unwrap_or_else(|e| e.into_inner()); + let rs = create_render_state(default_wgpu_setup()); + + let mut uploader = VolumeRaycaster::new(&rs, 22); + uploader.set_volume(&rs, &solid(16, [255, 0, 0, 255]), 16, 16, 16); + drop(uploader); + + let reused = VolumeRaycaster::new(&rs, 22); // same id, nothing uploaded + let red = red_pixels_of(&rs, reused); + assert!( + red < 50, + "the last view's drop must free id 22's texture; {red} red px leaked into \ + a fresh view that uploaded nothing" + ); +} + #[test] fn opaque_volume_ray_marches_to_colour() { let _gpu = GPU.lock().unwrap_or_else(|e| e.into_inner());