Skip to content

feat(visualization): warn when lens surfaces physically overlap (#731) - #734

Merged
HarrisonKramer merged 3 commits into
optiland:masterfrom
alexsmolya:feat/warn-lens-surface-overlap-731
Aug 16, 2026
Merged

HarrisonKramer merged 3 commits into
optiland:masterfrom
alexsmolya:feat/warn-lens-surface-overlap-731

Conversation

@alexsmolya

Copy link
Copy Markdown
Contributor

Resolves #731

Summary

This pull request implements the surface overlap warning in the lens visualization subsystem (Lens2D and Lens3D), addressing the existing TODO marker in optiland/visualization/system/lens.py.

Technical Details

  • Geometry-Aware Overlap Evaluation: Rather than a naive center thickness or scalar radius comparison, the check evaluates the actual surface sag profiles across the full semi-diameter clear aperture.
  • Coordinate Invariance: Coordinates on surface 2 are transformed to global coordinates and then localized into surface 1's coordinate system, accounting for non-zero center thickness, 3D translations, and rotations.
  • Physical Criterion: Checks if the physical axial separation $\Delta z = z_2 - z_1$ along the local optical axis is $\le 0$ anywhere across the sampled aperture extent ($r \in [0, \max(e_1, e_2)]$).
  • Warning: Emits a standard UserWarning("Lens surfaces overlap.") with stacklevel=2 when physical overlap or zero thickness is detected.
  • API Freeze Compliance: No changes to public API signatures or behaviors for valid prescriptions.

Tests & Validation

  • Added tests/visualization/system/test_lens_overlap.py with 10 focused regression tests covering:
    • Overlapping and valid biconvex singlets
    • Overlapping and valid steep meniscus lenses
    • Plano-concave and convex-plano configurations
    • Zero-thickness elements
    • Aspheric lenses (EvenAsphere)
    • 3D visualization (OpticViewer3D / Lens3D)
    • Empty / single-surface boundary cases
  • Scoped test suite pytest tests/visualization/ tests/test_optic.py passes (133/133 tests).
  • ruff check optiland/ and ruff format --check optiland/ pass cleanly.

…land#731)

Detect when neighboring surfaces of a lens element physically penetrate
or cross each other given their radii, thicknesses, and semi-diameters,
and raise a UserWarning in the lens visualization initialization path.

- Evaluates surface sag profiles across the full semi-diameter aperture.
- Transforms coordinates between neighboring surface coordinate systems
  to determine physical local axial separation across the aperture.
- Supports spherical, planar, and aspheric/arbitrary geometry classes.
- Adds regression and boundary case tests in test_lens_overlap.py.
@codecov

codecov Bot commented Aug 16, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@HarrisonKramer

Copy link
Copy Markdown
Collaborator

Thanks for closing out the TODO here, nice work.

A few notes:

  1. Scope: this only checks consecutive surfaces within a single lens element (each Lens2D is one glass group per system.py::_identify_components), matching what Warn when lens surfaces overlap in the visualization system #731 asked for. It won't catch overlap across an air gap between two separate elements (e.g. a doublet colliding with the next singlet). Not asking to expand scope here, just flagging it as a gap we should track: either a follow-up PR (a system-level check would probably be the cleaner home for it) or folded into this one if you'd rather.
  2. _surfaces_overlap only samples along the X/Y cross-sections (pts in the sampling loop). For non-rotationally-symmetric surfaces (Zernike, biconic, freeform) that can miss a real overlap that only shows up off-axis. Worth a docstring note on this limitation at minimum.
  3. Minor: _check_surface_overlap/_surfaces_overlap are missing type hints (repo convention is mandatory hints on new signatures).
  4. Minor: the float(be.to_numpy(x).item()) if hasattr(be.to_numpy(x), "item") else float(x) pattern calls to_numpy twice, and the else branch looks unreachable since to_numpy always returns an ndarray. Could simplify to a small _to_float helper.

I'm okay merging this as-is, but let me know if you'd like to make any changes first.

Best,
Kramer

@alexsmolya

Copy link
Copy Markdown
Contributor Author

Thanks for the review, Kramer!

I addressed the feedback in the latest commit:

  • added the missing type hints;
  • simplified scalar conversion with a _to_float helper;
  • documented the X/Y cross-section sampling limitation for non-rotationally-symmetric surfaces;
  • added focused regression tests for the overlap helpers and edge cases.

I kept the scope limited to #731, so cross-element/air-gap overlap detection remains a potential follow-up.

Locally, 137 tests pass along with ruff and mypy.

Thanks again!

@HarrisonKramer
HarrisonKramer merged commit eb15fcf into optiland:master Aug 16, 2026
8 of 9 checks passed
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.

Warn when lens surfaces overlap in the visualization system

2 participants