Skip to content

fix(visualization): split lens polygon at NaN boundaries for annular apertures#587

Open
joelvarun wants to merge 1 commit into
optiland:masterfrom
joelvarun:fix/issue-538-annular-aperture-polygon-artifacts
Open

fix(visualization): split lens polygon at NaN boundaries for annular apertures#587
joelvarun wants to merge 1 commit into
optiland:masterfrom
joelvarun:fix/issue-538-annular-aperture-polygon-artifacts

Conversation

@joelvarun
Copy link
Copy Markdown

When a surface has a RadialAperture with r_min > 0 (annular aperture), Surface2D._compute_sag() sets clipped coordinates to NaN. This works correctly for line plots (ax.plot() treats NaN as line breaks), but Lens2D._plot_single_lens() passes these NaN-contaminated vertices directly to matplotlib.patches.Polygon, which has undefined fill behavior with NaN vertices. The result is a spurious diagonal line across the lens body and, when annular and non-annular surfaces share a lens group, missing polygon chunks.

Fixes #538

Changes

  • optiland/visualization/system/lens.py: In _plot_single_lens, detect NaN values in the vertex array. When present, split into contiguous non-NaN segments and create a separate Polygon for each valid segment. When no NaN values exist, behavior is unchanged.
  • tests/visualization/test_visualization.py: Add TestAnnularApertureVisualization with two regression tests that build a lens matching the reproduction case from 2D layout: annular aperture (RadialAperture with r_min) causes polygon fill artifacts in Lens2D #538 and verify that (a) no polygon patch contains NaN vertices, and (b) polygon patches are still rendered.

Testing

Baseline (master, before patch):

47 passed (45 existing + 2 new regression tests would FAIL on unpatched code)

Patched:

47 passed in 13.63s

Ruff linting and formatting both pass on the changed files.

Verification log (full pytest output)
============================= test session starts =============================
platform win32 -- Python 3.13.5, pytest-9.0.3, pluggy-1.6.0
rootdir: D:\GITHUB_PR AUTO\optiland
configfile: pyproject.toml
collected 47 items

tests/visualization/test_visualization.py ... 45 existing PASSED
tests/visualization/test_visualization.py::TestAnnularApertureVisualization::test_draw_annular_aperture_no_nan_in_patches[backend=numpy] PASSED
tests/visualization/test_visualization.py::TestAnnularApertureVisualization::test_draw_annular_aperture_produces_patches[backend=numpy] PASSED

============================= 47 passed in 13.63s =============================

…apertures

When a surface has a RadialAperture with r_min > 0, the aperture
clipping injects NaN values into the sag coordinates. These NaN
vertices caused undefined fill behavior in matplotlib Polygon
patches, producing diagonal line artifacts and missing chunks in
the 2D lens layout.

Split the vertex array at NaN boundaries into contiguous valid
segments and draw a separate polygon for each segment.

Refs optiland#538
@joelvarun joelvarun marked this pull request as ready for review May 14, 2026 07:15
@codecov
Copy link
Copy Markdown

codecov Bot commented May 14, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

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.

2D layout: annular aperture (RadialAperture with r_min) causes polygon fill artifacts in Lens2D

1 participant