Skip to content

Union: polyhedron display (#2898) combined with Union_mesh BVH display (#2893) - #2899

Closed
willend wants to merge 9 commits into
mccode-dev:mainfrom
willend:2026-10-union-polyhedron-bvh
Closed

willend wants to merge 9 commits into
mccode-dev:mainfrom
willend:2026-10-union-polyhedron-bvh

Conversation

@willend

@willend willend commented Oct 24, 2026

Copy link
Copy Markdown
Contributor

Combines #2898 (Union volumes as polyhedra in mcdisplay) with #2893 (Union_mesh BVH display for large meshes, by @Lomholy), plus one commit that makes the two work together. If this is merged, #2898 and #2893 can be closed.

Contents

Tested (macOS arm64, clang, conda env mccode-dev)

Instrument --trace=2 --trace=1
Tests_union/Geometry_test 6 polyhedra; output byte-identical to #2898 alone lines; byte-identical
Geometry_test, Union_mesh bvh_display_layers=3 5 polyhedra + torus as BVH box lines lines incl. BVH boxes
Tests_union/Many_meshes 2 polyhedra; byte-identical 2512 lines
Tests_union/Test_mesh_boxes 3 polyhedra, 36 triangles 688 lines
Union_demos/Demonstration 38 polyhedra, 17433 triangles (as before) 8005 lines
Union_sample_environments/SE_usage_example 19 polyhedra, 8832 triangles (as before) 5342 lines
  • Geometry_test built with -fsanitize=address,undefined runs clean under --trace=2.
  • Every merge_lines_to_draw caller in McStas and McXtrace is consistent with its new ownership (lines_new is freed and emptied), so nothing is freed twice.

Not tested: Linux, Windows/MSVC, a GPU build, the real mcdisplay frontends (webgl, matplotlib, plopp, cad), and the Stanford-dragon-sized meshes that #2893 targets. Union_master_GPU is untouched; it already does not compile against the current union-lib.

🤖 Generated with Claude Code

willend and others added 8 commits October 24, 2026 16:59
With -DUNION_POLYHEDRON, Union_master draws each non-mask volume as one
mcdis_polyhedron instead of priority-clipped lines. Surfaces of box,
cylinder, cone, sphere (and Union_mesh in McStas) are triangulated and
refined adaptively where r_has_highest_priority disagrees, so parts
covered by higher-priority volumes are dropped. Masks stay wireframe.
Default (line) display is unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
--trace=1 always draws lines. Under --trace=2, display_mode=0 (default) and 2
draw polyhedra, display_mode=1 keeps the line drawing.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ic cost

merge_lines_to_draw now appends into a geometrically grown buffer, frees
lines_new->lines and leaves lines_new empty. The old version aliased
lines_new->lines into an empty master (use after free in
draw_circle_with_highest_priority), leaked every merged list and copied the
whole master twice per merge, which is quadratic for large meshes.
draw_line_with_highest_priority now always frees intersection_list.elements,
and frees int_dummy (McStas) / temp_intersection (McXtrace).

The McXtrace version also no longer exits when the first merged list of an
empty master has no lines.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
New parameter bvh_display_layers (default 12). When the mesh BVH has more
layers than this, mcdisplay draws the bounding boxes of that BVH layer (and
of shallower leaves) instead of every triangle edge; 0 keeps the triangle
edges. For the 871k-triangle Stanford dragon (19 layers) this is 24,891
lines instead of 1,307,274, and the mcdisplay-webgl instrument.json shrinks
from 276 MB to 5.2 MB. Display only, simulation unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The mesh geometry parameters are now a struct mesh_storage, holding the
geometry-mesh-lib gmesh_struct and the BVH display layer, like the
box/cylinder/sphere/cone storage structs. geometry_parameters.p_mesh becomes
p_mesh_storage, and mesh_display_layer is removed from geometry_struct.
No change in behaviour.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
- Mesh triangles are now read from p_mesh_storage->mesh (renamed from p_mesh).
- union_mcdisplay_polyhedron returns 0 for a mesh with display_layer > 0, and
  Union_master then draws that volume as lines, so the coarse BVH boxes are kept
  under --trace=2 instead of triangulating the full mesh.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Lomholy

Lomholy commented Oct 25, 2026

Copy link
Copy Markdown
Collaborator

@willend I have a suspicion that you may be right regarding the polygon emission solving the python tooling bottleneck I was finding, so your polyhedra solution may very well fix the large mesh display issue.

I am however wondering if these two solutions should not stay as separate PR's to be merged at each their own pace?

They are in my opinion quite different PR's and might benefit from separate review processes.

@willend

willend commented Oct 26, 2026

Copy link
Copy Markdown
Contributor Author

I think they absolutely should stay separate. I only added multiple PR's to double-check for potential technical blockers. :-)

@willend
willend marked this pull request as draft October 26, 2026 07:16
@willend

willend commented Oct 26, 2026

Copy link
Copy Markdown
Contributor Author

Putting this one in draft

@willend

willend commented Oct 28, 2026

Copy link
Copy Markdown
Contributor Author

Closing this one, BVH is already in and other PR's are incoming on the theme:

#2898
#2927
#2928

@willend willend closed this Oct 28, 2026
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.

2 participants