Skip to content

Refactor mesh pimpl#88

Open
Luochenghuang wants to merge 3 commits into
NanoComp:masterfrom
Luochenghuang:refactor-mesh-pimpl
Open

Refactor mesh pimpl#88
Luochenghuang wants to merge 3 commits into
NanoComp:masterfrom
Luochenghuang:refactor-mesh-pimpl

Conversation

@Luochenghuang
Copy link
Copy Markdown
Contributor

@Luochenghuang Luochenghuang commented May 30, 2026

Addresses a latent bug in the mesh PR (#74) wake, as well as an old bug (#82), both surfaced by the new GitHub Actions CI:

1. PIMPL refactor for the mesh struct. PR #74 hand-extended utils/ctlgeom-types.h with C-only fields (mesh_bvh_node, face_normals, bvh, is_closed, etc.) that don't have Scheme analogs in geom.scm. Any build with Guile detected regenerated the file from geom.scm and lost the internals, producing unknown type name 'mesh_bvh_node'. This commit applies PIMPL: the public mesh struct in ctlgeom-types.h now contains only Scheme-visible fields (vertices, face_indices as a vector3_list, and void *internal), while all C-only state moves into a new mesh_internal struct in utils/mesh-internal.h, accessed via a static inline mesh_priv(m) cast helper. ctlgeom-types.h goes back to being a regular auto-generated artifact derived from geom.scm. init_mesh allocates the cache and unpacks face_indices; mesh_copy eagerly rebuilds the BVH on copies (eliminating the previous shallow-pointer-share footgun); mesh_destroy frees the cache via mesh_internal_free. About 105 mechanical m->X to mesh_priv(m)->X rewrites in geom.c, 6 in test-mesh.c.

2. Eliminate slist_unique VLA in intersect_line_with_prism. this is an existing dead-store bug: the prism dedup loop wrote deduped values into a stack VLA and then did slist = slist_unique;, which is a no-op since slist is a pass-by-value pointer parameter. The symptom is that a thin spurious "inside" sliver of width ~1e-3*|s| plus a dropped real crossing on edge-grazing rays. test-prism didn't catch it at OMP=1 because the deterministic rand() sequence didn't statistically hit the 1e-3 window often enough, but parallel runs (OMP=8) shuffle the libc rand() order and trip it routinely. Fix: dedup in place with a write head iv and read head nv. This eliminates the VLA, eliminates the dead store, and the behavior is bit-identical to the dedup's original intent (slist[nv-1] still reads the original sorted value because writes go to slist[iv++] with iv <= nv).

Closes #86, closes #82.

Luochenghuang and others added 3 commits May 29, 2026 23:22
…-driven public struct

PR NanoComp#74 (mesh geometry import) hand-extended utils/ctlgeom-types.h with C-only internal fields (mesh_bvh_node, face_normals, face_areas, bvh, bvh_face_ids, is_closed, centroid, lengthscale, num_bvh_nodes, and a flat int* face_indices) because none of those had natural Scheme representations in geom.scm. That worked when Guile wasn't detected at configure time, but any from-git build with Guile installed (including the new GitHub Actions CI) regenerated ctlgeom-types.h from geom.scm, clobbered the hand-extensions, and failed to compile with 'unknown type name mesh_bvh_node'.

This commit applies the PIMPL (pointer-to-implementation) idiom to clean up that situation properly. The public mesh struct in ctlgeom-types.h now contains only Scheme-visible fields — vertices, face_indices (as a vector3_list, each vector3 packing one triangle's three int indices), and a void* internal pointer. All C-only state moves into mesh_internal in a new private header utils/mesh-internal.h, accessed via a static-inline mesh_priv(m) cast helper. ctlgeom-types.h goes back to being a regular auto-generated artifact derived from geom.scm; no Makefile.am workaround needed.

Lifecycle:
  - make_mesh / make_mesh_with_center: populate public fields, then call init_mesh which calloc()s mesh_internal, unpacks face_indices into a flat int[], computes face normals/areas, checks watertight closure, builds the BVH, and stores the pointer in m->internal.
  - reinit_mesh: fast-path returns when m->internal != NULL; otherwise calls init_mesh. Same single-threaded-init contract as before.
  - mesh_destroy (auto-generated): frees vertices.items + face_indices.items, then calls mesh_internal_free (defined in geom.c) to free the cache.
  - mesh_copy (auto-generated): deep-copies the public fields, sets internal=NULL, then calls mesh_init_internal (defined in geom.c) to eagerly rebuild the BVH on the copy. Eliminates the previous shallow-pointer-share footgun where two mesh copies pointed at the same BVH allocation.

geom.c gets ~105 mechanical m->X → mesh_priv(m)->X rewrites for the 10 internal fields (the cast inlines to nothing at runtime). test-mesh.c gets the same rewrite for 6 sites where tests poke directly at m->is_closed.

Both auto-generated files (ctlgeom-types.h and geom-ctl-io.c) now match what gen-ctl-io would emit from the simplified geom.scm, with one hand-tweak in geom-ctl-io.c: mesh_destroy and mesh_copy call into the externally-defined mesh_internal_free / mesh_init_internal so they can manage the opaque cache. This is the minimum manual surface required for any PIMPL design.

Verified: configure + make + make check all clean. test-prism passes (0/10000 point failures, 0/1000 segment failures, 0/65 prism helper failures). test-mesh passes 0 failures across all 19 cases including test_copy_destroy (which exercises mesh_copy + point queries on the copy + destroy of both original and copy).
The duplicate-removal step previously wrote deduped values into a stack VLA slist_unique[num_vertices+2] and then did 'slist = slist_unique;' before returning the new count. That assignment is a dead store: slist is a pass-by-value pointer parameter, so reassigning it only rebinds the local variable. The caller's buffer is never touched and slist_unique is destroyed when the function returns, so the caller reads the first num_unique_elements entries of the sorted-but-undeduped buffer instead of the actual deduped values.

In intersect_line_segment_with_prism this corrupts the inside/outside parity walk: a near-duplicate that the dedup loop intended to drop stays in the buffer (producing a phantom thin 'inside' sliver of width ~1e-3*|s|), and a real far-end crossing is silently dropped because the caller stops at the smaller returned count. The bug triggers when two s-values fall within 1e-3*|s| of each other — edge-grazing rays, rays through a shared vertex, near-tangent rays. test-prism's random-ray distribution didn't statistically hit the window often enough at OMP=1 to flag it, but parallel runs (e.g. OMP=8) shuffle the libc rand() sequence and hit the window often enough that the strict 1e-6 segment-length check fails.

The fix eliminates the VLA entirely. The dedup is done in place with a write head 'iv' and read head 'nv': because writes go to slist[iv++] with iv <= nv, position nv-1 is either untouched or was previously written with its own value as a no-op, so reading slist[nv-1] still yields the original sorted value and the comparison semantics are bit-identical to the original intent. Net effect: removes the dead store, removes a potentially huge stack allocation, and the dedup now actually applies to the caller's buffer.

Verified with --enable-openmp: test-prism passes at both OMP_NUM_THREADS=1 (0/10000 points failed, 0/1000 segments failed, 0/65 prism helper failures) and OMP_NUM_THREADS=8 across multiple trials. test-mesh remains clean at OMP=8 as well.
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.

compilation failre with new mesh feature intersect_line_with_prism: slist dedup never writes back

1 participant