Repository navigation
Union_mesh mcdisplay slow with large meshes - optimize merge_lines_to_draw and add option for drawing coarse meshes - #2893
Conversation
…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]>
Union mcdisplay: fix merge_lines_to_draw ownership, leaks and quadratic cost
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]>
Union_mesh: draw BVH boxes in mcdisplay for large meshes
|
@willend and @mads-bertelsen sorry for the PR follow up so soon. I know how this is pushing you guys, but no rush regarding this PR, I just made it to flag the issue as well as a possible solution. |
|
@Lomholy no worries, it’s very good! Will have a look as soon as possible. Doubt the preceding mesh solution was better in this regard, so it’s still progress |
|
|
||
| struct rotation_struct rotation; // Not used, is the x y and z rotation angles. | ||
| int visualization_on; // If visualization_on is true, the volume will be drawn in mcdisplay, otherwise not | ||
| int mesh_display_layer; // mesh only: BVH layer whose boxes mcdisplay draws, 0 to draw the triangle edges |
There was a problem hiding this comment.
This is specific to mesh and should be in the mesh storage structure, now that is the mesh library struct, but it will need to be a struct of this value and the library structure
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]>
Union_mesh: keep the mcdisplay BVH layer in a mesh_storage struct
|
I do know that Union_master / functions called from there draws the analysed geometry as single line-segments, but I am convinced speed of visualisation / transfer to the .py utils would likely become much quicker the moment we sent polygon's directly. (And yes I know it is not a trivial rewrite to do, yet I think it is worth to have on the wish-list for some time in the future...) |
|
@Lomholy and @mads-bertelsen I couldn't help letting Claude have a go at a polygon-based Union_master display:
|
|
@mads-bertelsen as we discussed today I prototyped a bit more on the visualisation-options for meshes / 'surface'-primitives in mcdisplay-webgl (and -classic). New work incoming in via #2915. I aim to rebase the related Union-surface PR once the line-toggle PR tests are run to green and merged. |
mads-bertelsen
left a comment
There was a problem hiding this comment.
The underlying problem seems to be that reporting all these line segments through mcdisplay for huge systems is not a good idea in the first place, and @willend 's experiments with passing polyhedra instead seems like a viable option, that can easily plot the dragon.stl.
That said the PR here is valuable for several reasons, it adds the capability to see the bounding boxes which is a good fallback in viewers where the polyhedra is not supported and it fixes some bugs in code that will still be used.
The Union_mesh component needs a quick clang-format pass, then we can merge this and build upon it with a polyhedra solution.
|
@mads-bertelsen Alright, sounds good! I have just performed the clangformatting so I suppose this can be merged whenever the tests go through 😄 |
p_mesh was renamed to p_mesh_storage (with the mesh inside) in 12058c4, merged to main via mccode-dev#2893; the main merge into this branch left the polyhedron code pointing at the old member. Co-Authored-By: Claude Opus 5.5 <[email protected]>




Free-form text area
Please describe what your PR is adding in terms of features or bugfixes:
Following the merge of #2866 I wanted to do a quick simulation with a big mesh to showcase how cool this new addition is. I ran into the issue that I could not use mcdisplay to visualize my geometry.
This PR fixes that by doing two things for the Union_mesh visualization. First it optimizes the allocation procedure in the merge_lines_to_draw and an int array leak in r_has_highest_priority functions, because those were causing issues with memory.
This fix however is not enough to draw a Stanford Dragon (871414 triangles) on my M4 max laptop.
So the second fix is to use the Bounding volume hierarchy to allow the user to see a coarser approximation of their shape. The parameter bvh_display_layers has been added to the Union_mesh component for this reason. When set to an integer, the integer decides what level of the Bounding volume hierarchy will be displayed. Higher bvh_display_layers, more detail.
A screenshot from the mcdisplay with the stanford dragon and a bvh_display_layers = 14:

Declaration of use of AI-tools
The LLM found the malloc in the union merge_lines_to_draw and the int array leak, and implemented the bvh layer visualizer handle
Development OS / boundary conditions
Please describe what OS you developed and tested your additions on, and if any special dependencies are required:
MacOS Golden Bridge.
PR Checklist for contributing to McStas/McXtrace
For a coherent and useful contribution to McStas/McXtrace, please fill in relevant parts of the checklist:
My contribution includes patches to an existing component file
mcdocutility and rendered a reasonable documentation page for the component (please attach as screenshot in comments!)mctestutility to test one or more instruments making use of the component (please attachmcviewtestreport as screenshot in comments)mccode-clangformattool to apply the standard McCode component indentation schememcrun --c-lint"linter" and followed advice to remove most / all warnings that are raisedMy work touches / adds to the runtime lib code (.c,.h etc in multiple locations