Skip to content

Fix slanted-prism top vertices at collinear base vertices - #102

Open
Andeloth wants to merge 1 commit into
NanoComp:masterfrom
Andeloth:slanted-prism-collinear-vertex
Open

Andeloth wants to merge 1 commit into
NanoComp:masterfrom
Andeloth:slanted-prism-collinear-vertex

Conversation

@Andeloth

@Andeloth Andeloth commented Nov 13, 2026 •

Copy link
Copy Markdown

Fixes #100.

Problem

For sidewall_angle != 0, init_prism placed each top vertex at the intersection of the two adjacent offset edges, using the two-point line–line formula. That formula divides by the cross product of the two edges:

  • Vertex collinear with its neighbors: 0/0, so the top vertex is NaN. point_in_prism then turns the prism inside out for every z > 0, and normal_to_prism aborts with degenerate plane in project_point_into_plane, so Meep's init_sim exits when subpixel averaging is on.
  • Vertex collinear only to within rounding, e.g. a vertex partway along an edge of a rotated polygon, as layout tools produce: the formula cancels catastrophically and misplaces the top vertex without any error. For a 4 × 2 rectangle rotated by 17° with an extra vertex 30% of the way along one edge, the top vertex lands 8.5 units from where it belongs. Across 18 rotations and translations of that shape the error ranged from 0.05 to 8.5. Exactly collinear vertices are rarely exactly representable, so in practice this is the more common failure.

Fix

Compute each top vertex directly as the base vertex plus the miter vector

m = (u1 + u2) / (1 + u1·u2 / w²)

where u1 and u2 are the offsets of the two edges meeting at the vertex (|u1| = |u2| = w). Since m·u1 = m·u2 = w², m lies on both offset edges, so in exact arithmetic it is the same point as before. There is no division by the cross product, though. The denominator 1 + cos of the angle between the edge normals vanishes only where the polygon doubles back on itself, where the old formula failed as well. The issue suggested a guard on the old formula; this approach needs no tolerance and also handles vertices that are collinear only to within rounding.

Test

test_slanted_prism_collinear_vertices() in utils/test-prism.c covers:

  • a 4 × 2 rectangle with an extra vertex exactly on an edge, 1e-11 off an edge, listed first (to exercise the wrap-around), and two extra vertices, each at ±30°. Each is checked against the analytic top polygon, point inclusion just inside and outside every wall, chord lengths, and the wall normals;
  • the plain rectangle as a control;
  • 18 rotated and translated rectangles whose extra vertex is collinear only to within rounding, checked against the analytic top polygon.

26 checks fail before this change (8 NaN top vertices, 18 misplaced ones), and none fail after. make check passes with OMP_NUM_THREADS = 1 and 8.

Meep check (Meep 1.34.0; conda-forge libctl 4.7.1 vs this branch, loaded with LD_PRELOAD), using the reproducer from #100: a 4 × 2 rectangle with an extra vertex at the midpoint of its x = +2 edge, 30° sidewalls, probed at z = 0.25, where the expected cross section is x ∈ [−1.856, 1.856]:

libctl 4.7.1 this PR
is_point_in_object inside along x (−3.0, −1.853) (−1.855, 1.856)
rendered ε along x, no subpixel averaging (−2.0, −1.86) (−1.86, 1.86)
init_sim with subpixel averaging aborts: degenerate plane in project_point_into_plane completes

With this PR the prism with the extra vertex gives the same results as the plain rectangle.

This is independent of #103, the fix for #101. Both add a test to run_unit_tests(), so whichever is merged second will have a trivial conflict there; with both applied all tests pass.

🤖 Generated with Claude Code

init_prism placed each top vertex of a slanted prism at the intersection
of the two adjacent offset edges, using the two-point line-line formula.
That divides by the cross product of the edges: at a base vertex that is
collinear with its neighbors it is 0/0 and the top vertex is NaN, which
turns point_in_prism inside out for z > 0 and makes normal_to_prism abort
with "degenerate plane in project_point_into_plane". When the vertex is
collinear only to within rounding (e.g. a vertex partway along an edge of
a rotated polygon) the formula cancels catastrophically instead and puts
the top vertex up to several units away, silently.

Compute the top vertex directly as the base vertex plus the miter vector
m = (u1 + u2) / (1 + u1.u2 / w^2), where u1, u2 are the two edges'
offsets (|u1| = |u2| = w). It satisfies m.u1 = w^2 and m.u2 = w^2, i.e.
lies on both offset edges, and its denominator vanishes only where the
polygon doubles back on itself.

Add test_slanted_prism_collinear_vertices to test-prism: rectangles with
extra vertices on their edges (exact, 1e-11 off, listed first, two of
them) at +/-30 degrees, checked against the analytic top polygon, cross
section and wall normals, plus rotated and translated rectangles whose
extra vertex is collinear only to within rounding. 26 checks fail before
this change, none after.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

Slanted prism (sidewall_angle != 0) with a collinear vertex: wrong geometry, or an abort with subpixel averaging

1 participant