Skip to content

Fix click picks: elements from the pick buffer, pick pass after the scene - #9

Merged
xarthurx merged 5 commits into
mainfrom
fix/click-picks
Nov 27, 2026
Merged

xarthurx merged 5 commits into
mainfrom
fix/click-picks

Conversation

@xarthurx

Copy link
Copy Markdown
Owner

Summary

Fixes the three pick issues that PR #7 recorded under Known Issues. Planned for 0.6.3. Five commits: the first two hold the fixes, and the last three hold the fixes from three Codex review rounds.

1. A click takes the element from the pick buffer (0603cad)

  • Before: a click on a surface mesh or a volume mesh selected element 0. The click code intersected the click ray with all mesh triangles on the CPU and added that hit with element 0. It gave the face from the pick buffer the same distance, so on the tie the CPU hit won. The click code also ignored the volume mesh cells, the volume grid gridcubes and the camera view wireframes of the pick buffer.
  • Now: App::click_hit_at takes the structure and the element from the pick buffer for every structure type, as C++ Polyscope does.
    • A gridcube selects the volume grid with the cell of a cell quantity or the node of a node quantity (shown as "Vertex"). A camera view reports the edge of its wireframe. Both differ from C++, see section 4.
    • The CPU triangle test is removed, so a click on a large mesh no longer tests each triangle.
  • New public API: RenderEngine::lookup_pick_owner and PickOwnerHit, which give the quantity index of a pick range.

2. Pick uniforms with the render uniforms, pick pass last (b99f50c)

  • Before: the pick pass wrote the full pick uniforms and read the transforms under its own lock, after the frame preparation had written the render uniforms. If another thread moved a structure between the two, the pick buffer of that frame used the new position while the window showed the old one. The pick pass also ran before the scene passes, which make the curve network tube geometry, so for one frame a tube was picked at its old place or with its old radius.
  • Now:
    • The frame preparation creates the pick resources and writes the pick uniforms (model, radius and cube size) in the same lock and with the same values as the render uniforms. Gridcubes do this in init_volume_grid_quantities. A gizmo drag in the UI writes both together.
    • The pick pass runs last in the window frame. It gives the pick ranges and writes only the first pick index of each owner, with the new write_pick_global_start (a 4-byte write at the offset of global_start).
    • A gridcube quantity drops its pick resources when its render data is built again, as a volume mesh does.

3. First review fixes (306b869)

  • VolumeMesh::set_transform keeps its pick resources, so a gizmo drag does not take the volume mesh out of the pick pass. New render data drops them.
  • Gridcube pick uniforms follow the render uniforms in every visualization mode, so gridcubes that come back after an isosurface are picked in place.
  • The pick pass records the element type of each gridcube range, and a click uses that record. A quantity removed after the frame does not change the reported type.
  • The click uses the center of the pixel under the cursor for the pick, the pick depth and the slice plane test. It compares a slice plane with the pick depth of the element for every type, because the scene uses the same depth. It reads the pick depth only when a slice plane is under the cursor; before, each click copied the full depth buffer.

4. Second review fixes (d5fb584)

  • The tube pick shader of a curve network writes the depth that the window shows: the drawn tube surface where the pick ray hits it, and the curve line elsewhere. Before, it wrote the front of the click area, which is wider than the drawn line or tube, so a curve just behind a mesh covered the mesh in the pick buffer. A curve in line mode keeps the click area that it had before.
  • A click and a double-click use a copy of the camera of the frame in the pick buffer. Before, they used the live camera, so a scroll or a drag before the next frame could make a slice plane behind a mesh win.
  • The delayed selection checks the element index against the current element count, so a point cloud with fewer points does not show a point that it no longer has.
  • Architecture Differences states where the picks differ from C++: C++ gives a camera view one pick index, and it finds the node or the cell of a volume grid on the CPU from the pick position.

5. Third review fixes (0d04f78)

  • The pick pass sets the depth source of the tube pick with the render mode of its frame (new CurveNetwork::write_tube_pick_render_mode). Before, the frame preparation set it, so after a switch from lines to tubes in the UI, the pick pass of that frame took the line depth where the window showed the tube.
  • The nearest point of a curve segment to the pick ray comes from the parts of the segment ends across the ray. It stays accurate for a segment almost parallel to the ray, and a parallel segment takes its nearer end. Before, the edge order decided the depth of an end-on line in an orthographic view.
  • A slice plane wins a click on a face that is nearer by less than the precision of the pick depth. The scene draws the slice planes first and the structures with a "less" depth test, so the window shows the plane there.

Known issues (documented in docs/feature-status.md, not changed here)

  • Picks ignore slice planes. The pick shaders of points, curve networks, camera views and surface meshes have no slice plane culling, so a click can select an element that a slice plane hides. 0.6.2 has the same gap for points, curves and meshes. A fix needs slice plane bind groups in four pick pipelines and a flag for volume meshes, which cull whole cells instead. I recommend a separate PR.
  • Changes from another thread during a frame. The scene passes and the pick pass read visibility and render data under separate locks. If another thread hides, removes or updates a structure between them, the pick buffer of that frame and the window can differ. 0.6.2 has the same limitation.
  • The click area past a tube end. The click area of a tube reaches half a tube radius past each end, with the depth of a longer tube. Outside the node sphere at the end, a click can select the tube where the window shows a surface just behind it.
  • A click right after a window resize. A resize makes new, empty pick buffers. A click before the next frame finds no element, so it clears the selection.

Tests

  • New tests in crates/polyscope/src/tests/pick.rs. The GPU tests skip when no GPU adapter is available, as the other GPU tests do.
    • test_click_reports_mesh_faces_and_cells, test_click_reports_volume_grid_cells_and_nodes, test_click_selects_a_camera_view.
    • test_pick_pass_uses_the_transform_of_the_scene: another thread moves a point and a tetrahedron after the frame preparation. The scene image and the pick buffer of that frame both show the old position; the next frame shows the new position in both.
    • test_gridcubes_after_an_isosurface_are_picked_in_place and test_gridcube_click_keeps_the_element_type_of_the_frame.
    • test_click_compares_slice_planes_with_the_pick_depth: a slice plane behind and in front of a square, with clicks at pixel corners.
    • test_line_behind_a_mesh_does_not_cover_it: a line 0.02 behind a square, with a click area of radius 0.05.
    • test_click_uses_the_camera_of_the_pick_buffer: a scroll after the frame, with a slice plane 0.1 behind a square.
    • test_click_selection_checks_the_element (CPU): a point cloud loses points during the double-click time.
    • test_tube_switch_in_a_frame_is_picked_as_a_tube: the UI switches a line 0.02 behind a square to a tube of radius 0.05 after the frame preparation.
    • test_end_on_line_is_picked_at_its_nearer_end: two lines along an orthographic view direction, in both edge orders, with a square between their ends.
    • test_slice_plane_wins_a_tie_with_a_face: 25 clicks on a square in the plane of a slice plane.
    • test_curve_pick_radius_follows_the_radius now expects the new tube radius one frame after the change.
  • Each new test failed when its fix was undone: the 0.6.2 click algorithm (a click on face 1 reported element 0; gridcubes and camera views gave no hit), the old full-uniform write in the pick pass, the pick pass before the scene passes, set_transform dropping the volume mesh pick resources, gridcube pick uniforms only in gridcube mode, the element type from the current quantities, the curve pick depth from the front of the click area, the old line-mode pick uniforms, the live camera in the click, the selection without the element check, the tube depth source from the frame preparation, the old nearest-point formula, and the strict distance comparison with the slice plane.
  • Local run on macOS (Metal), Rust 1.100.0, on 0d04f78: cargo test --workspace --no-fail-fast gives 346 passed, 0 failed, 2 ignored, with no GPU test skipped. cargo fmt --all -- --check, cargo clippy -- -D warnings and RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --document-private-items pass. cargo clippy --workspace --all-targets reports no finding on a line that this branch changes.

🤖 Generated with Claude Code

A click on a surface mesh or a volume mesh selected element 0. The click
code intersected the click ray with all mesh triangles on the CPU and
added that hit with element 0. It gave the face from the pick buffer the
same distance, so on the tie the CPU hit won. The click code also
ignored the volume mesh cells, the volume grid gridcubes and the camera
view wireframes of the pick buffer.

As in C++ Polyscope, `App::click_hit_at` now takes the structure and the
element from the pick buffer for every structure type. The distance of
the element along the click ray comes from the pick depth buffer; points
and curve network edges keep their exact ray test. A slice plane under
the cursor wins if it is nearer. The CPU triangle test is removed.

A gridcube reports the volume grid, with the cell of a cell quantity or
the node of a node quantity. The new `RenderEngine::lookup_pick_owner`
gives the quantity index of a pick range for this. A camera view
reports the edge of its wireframe. `ClickHit::Structure` carries the
element type.
The pick pass wrote the full pick uniforms and read the transforms under
its own lock, after the frame preparation had written the render
uniforms. If another thread moved a structure between the two, the pick
buffer of that frame used the new position while the window showed the
old one. The pick pass also ran before the scene passes, which make the
curve network tube geometry, so for one frame a tube was picked at its
old place or with its old radius.

Now the frame preparation creates the pick resources and writes the pick
uniforms (model, radius and cube size) in the same lock and with the
same values as the render uniforms. Volume grid gridcubes do this in
`init_volume_grid_quantities`. A gizmo drag in the UI writes the pick
uniforms together with the render uniforms. The pick pass runs last in
the window frame. It gives the pick ranges and writes only the first
pick index of each owner with the new `write_pick_global_start`.

A gridcube quantity drops its pick resources when its render data is
built again, as a volume mesh does.
The Codex review of this branch found these defects:

- `VolumeMesh::set_transform` dropped the pick resources. A gizmo drag
  after the frame preparation then left the volume mesh out of the pick
  pass of that frame, although the scene drew it. New render data drops
  the pick resources since 9c07272, so `set_transform` keeps them.
- A node quantity wrote its gridcube pick uniforms only in gridcube
  mode, but its render uniforms in every mode. When the UI showed the
  gridcubes again after an isosurface, the pick pass of that frame used
  an old transform. The pick uniforms now follow the render uniforms in
  every mode.
- A click took the type of a gridcube (node or cell) from the current
  quantities. If a quantity was removed after the frame, the type could
  be wrong. The pick pass now records the type of each gridcube range,
  and the click uses this record.
- The click compared a slice plane at the cursor position with the pick
  depth at the pixel center. The ray tests of points and curve network
  edges used the current transform, not the one of the displayed frame.
  The click now uses the pixel center for the pick, the pick depth and
  the plane test, and the pick depth for every element type, as the
  scene does. The CPU ray tests are removed.
- Each click read the full pick depth buffer. The click now reads it
  only when a slice plane is under the cursor.

Feature Status lists one more known issue: the pick shaders ignore
slice planes, so a click can select an element that a slice plane hides.
The second Codex review of this branch found these defects:

- The tube pick shader of a curve network wrote the depth of the front
  of its click area, which is wider than the drawn line or tube. A curve
  just behind a surface mesh then covered the mesh in the pick buffer,
  and a click on the mesh selected the curve. The shader now writes the
  depth that the window shows: the tube surface where the pick ray hits
  the drawn tube, and the curve line elsewhere. A curve in line mode
  writes 0 as its tube radius and puts its tube radius into the minimum
  pick radius, so its click area does not change.
- A click unprojected the pick depth of the last frame with the live
  camera, and it cast the slice plane ray with the live camera. A scroll
  or a drag before the next frame could then make a slice plane behind a
  mesh win. The frame preparation now keeps a copy of the camera whose
  uniforms it writes, and the pick pass makes it the view of the pick
  buffer. A click and a double-click use this view.
- The delayed selection did not check the element index. A point cloud
  with fewer points after the frame could show a point that it no longer
  has. The selection now checks the index against the current element
  count.

Architecture Differences now states where the picks of camera views and
volume grids differ from C++ Polyscope: C++ gives a camera view one pick
index, and it finds the node or the cell of a volume grid on the CPU.
The third Codex review of this branch found these defects:

- The frame preparation wrote the tube pick radius for the render mode
  that it saw. The scene passes read the render mode when they draw. So
  after a switch from lines to tubes in the UI, the pick pass of that
  frame took the depth of the line where the window showed the tube.
  The frame preparation now writes the tube radius in both modes. The
  pick pass writes 0 for a curve in line mode, with the render mode of
  its frame.
- The nearest point of a curve segment to the pick ray lost its
  precision for a segment almost parallel to the ray. For a segment
  parallel to an orthographic ray, it always took the first end, so the
  edge order decided whether the line covered a surface. The shader now
  computes the point from the parts of the segment ends across the ray,
  and a parallel segment takes its nearer end.
- A face in the plane of a slice plane could win a click over the plane,
  because of the rounding of the pick depth. The scene draws the slice
  planes first and the structures with a "less" depth test, so the
  window shows the plane. The plane now wins if the face is nearer by
  less than the precision of the pick depth.

Feature Status lists the two other findings of the review as known
issues: the click area past a tube end, and a click before the first
frame after a window resize.
@xarthurx
xarthurx merged commit f245b90 into main Nov 27, 2026
5 checks passed
@xarthurx
xarthurx deleted the fix/click-picks branch November 27, 2026 13:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant