Skip to content

Include normalization constant in calculation of Zernike derivative#663

Open
crnh wants to merge 5 commits into
masterfrom
crnh/fix/normalize-zernike-derivative
Open

Include normalization constant in calculation of Zernike derivative#663
crnh wants to merge 5 commits into
masterfrom
crnh/fix/normalize-zernike-derivative

Conversation

@crnh

@crnh crnh commented Jul 6, 2026

Copy link
Copy Markdown
Collaborator

As reported in #662, the normal vector calculation for Zernike coefficients currently calculates the gradient over non-normalized Zernike polynomials, resulting in incorrect normal vectors and thus incorrect ray tracing results. I fixed this by multiplying the gradient terms with the corresponding normalization constants.

When running the sample code from #662 on this implementation, I get this result:

image

Differences between the analytic and finite difference derivatives are now on the order of 1e-8, which is much better than before.

@HarrisonKramer I can also add an additional test for the surface normals, which compares the normals with a finite difference approximation. We could also add this test to all Newton-Raphson geometries to prevent this kind of errors in the future. Do you want such tests as part of this PR? And where should they be placed? In test_geometries.py, test_geometries_extended.py, or somewhere else?

Closes #662

@crnh crnh self-assigned this Jul 6, 2026
@crnh crnh added the bug label Jul 6, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes incorrect surface-normal computation for Zernike polynomial geometries by ensuring derivatives include the same normalization constants used in the sag/polynomial evaluation, improving ray-tracing correctness (as reported in #662).

Changes:

  • Multiply Zernike derivative contributions by the per-term normalization constant when computing _surface_normal.
  • Update expected surface-normal reference data in geometry tests (and add explicit noll coverage in the reference dict).

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
optiland/geometries/zernike.py Applies Zernike normalization constants to derivative terms used in surface-normal computation.
tests/test_geometries.py Updates stored reference normals to match the corrected normalization behavior.

Comment thread optiland/geometries/zernike.py Outdated
Comment on lines +282 to +286
norm_constant = self.zernike._norm_constant(n, m)

# Partial derivatives w.r.t. x and y
dzdx = dzdx + c * (dZdrho * drho_dx + dZdphi * dphi_dx)
dzdy = dzdy + c * (dZdrho * drho_dy + dZdphi * dphi_dy)
dzdx = dzdx + norm_constant * c * (dZdrho * drho_dx + dZdphi * dphi_dx)
dzdy = dzdy + norm_constant * c * (dZdrho * drho_dy + dZdphi * dphi_dy)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes sense because get_derivative() only calculates partial derivatives; _surface_normal is the most sensible place for multiplication with the normalization constant. See also https://www.sciencedirect.com/science/article/pii/S0143816613002285.

Comment thread tests/test_geometries.py
@codecov

codecov Bot commented Jul 6, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@HarrisonKramer

HarrisonKramer commented Jul 11, 2026

Copy link
Copy Markdown
Collaborator

Thanks for submitting this, @crnh.

We could also add this test to all Newton-Raphson geometries to prevent this kind of errors in the future. Do you want such tests as part of this PR?

Yes, this is the right move to assure robustness in surface normal calculations. I am okay with adding these tests to this PR, but it can also be a separate PR if you prefer. I leave that up to you. PR looks good. Feel free to merge when ready (once @drpaprika gets a chance to review as well).

-Kramer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Zernike surfaces calculate normals over non-normalized polynomials

4 participants