geometries.zernike#
Zernike Geometry
The Zernike polynomial geometry represents a surface defined by a Zernike polynomial in two dimensions. The surface is defined as:
- z(x,y) = r^2 / (R * (1 + sqrt(1 - (1 + k) * r^2 / R^2))) +
sum_i [c[i] * Z_i(rho, phi)]
where: - r^2 = x^2 + y^2 - R is the radius of curvature - k is the conic constant - c[i] is the coefficient for the i-th Zernike polynomial - Z_i(…) is the i-th Zernike polynomial in polar coordinates - rho = sqrt(x^2 + y^2) / normalization, phi = atan2(y, x)
Zernike polynomials are a set of orthogonal functions defined over the unit disk, widely used in freeform optical surface design. They efficiently describe wavefront aberrations and complex surface deformations by decomposing them into radial and azimuthal components. Their orthogonality ensures minimal cross-coupling between terms, making them ideal for optimizing optical systems. In freeform optics, they enable precise control of surface shape, improving performance beyond traditional spherical and aspheric designs.
drpaprika, 2025
Classes
|
Represents a Zernike polynomial geometry defined as: |
- class ZernikePolynomialGeometry(coordinate_system: str, radius: float, conic: float = 0.0, tol: float = 1e-10, max_iter: int = 100, coefficients: NDArray | None = None, zernike_type: ZernikeType = 'standard', norm_radius: float | None = None)[source]#
Represents a Zernike polynomial geometry defined as:
- z(x,y) = r^2 / (R * (1 + sqrt(1 - (1 + k) * r^2 / R^2))) +
sum_i [c[i] * Z_i(rho, phi)]
where: - r^2 = x^2 + y^2 - R is the radius of curvature - k is the conic constant - c[i] is the coefficient for the i-th Zernike polynomial - Z_i(…) is the i-th Zernike polynomial in polar coordinates - rho = sqrt(x^2 + y^2) / normalization, phi = atan2(y, x)
The coefficients are defined in a 1D array where coefficients[i] is the coefficient for Z_i.
- Parameters:
coordinate_system (str) – The coordinate system used for the geometry.
radius (float) – The radius of curvature of the geometry.
conic (float, optional) – The conic constant of the geometry. Defaults to 0.0.
tol (float, optional) – The tolerance value used in calculations. Defaults to 1e-10.
max_iter (int, optional) – The maximum number of iterations used in calculations. Defaults to 100.
coefficients (list or be.ndarray, optional) – The coefficients of the Zernike polynomial surface. Defaults to an empty list, indicating no Zernike polynomial coefficients are used.
zernike_type (str, optional) – The type of Zernike polynomial to use. Defaults to “standard”. Options are “standard”, “noll”, or “fringe”.
norm_radius (float, optional) – The normalization radius for the Zernike polynomial coordinates. If None, the radius scales automatically during paraxial updates. Defaults to None.
- property coefficients: NDArray#
Get the coefficients of the Zernike polynomial surface.
- distance(rays)#
Calculates the distance from the ray origin to the surface intersection using a robust Newton-Raphson method. This version uses the base conic intersection as a strong initial guess.
Differentiable mode (torch backend with grad enabled):
The primal Newton-Raphson solve runs inside
torch.no_grad()so that the iterative loop is never recorded in the autograd graph. A one step implicit correction (in DiffOptics style) is then applied:t_implicit = t_detached - F(t_detached) / (dF/dt)_detached
Since F is near zero at convergence, the forward value is unchanged, but the gradients are correct to first order via the implicit function theorem.
Note
This implicit correction is intended for correct first-order gradients. Higher order derivatives (double backward and beyond) are not guaranteed to match the exact unrolled Newton system.
Non differentiable mode (numpy backend, or torch without grad):
Returns the converged t directly.
Assumptions required for the implicit derivative to be exact:
the primal solve converged to the intended physical root;
that root stays on the same branch under small parameter changes;
dF/dtis not zero or numerically singular (no tangent/grazing intersection);|n_z|is above the dtype-aware threshold, so the surface is a numerically valid local height function;sag()and_surface_normal()describe the same surface;only first derivatives are supported.
Rays that fail any of (1)-(4) keep their detached primal forward value and are never evaluated through a grad-attached residual, so a failed or singular root never carries a confident-looking but invalid gradient and never contaminates the gradients of valid rays in the same batch. A grouped
RuntimeWarningreports the rejections by category.- Parameters:
rays (RealRays) – The rays used for calculating distance.
- Returns:
An array of propagation distances ‘t’ from each ray’s current position to its intersection point with the geometry.
- Return type:
be.ndarray
- flip()#
Flip the geometry.
Changes the sign of the radius of curvature. The conic constant remains unchanged.
- classmethod from_dict(data: dict) ZernikePolynomialGeometry[source]#
Create a Zernike polynomial geometry from a dictionary.
- Parameters:
data (dict) – The dictionary representation of the Zernike polynomial geometry.
- Returns:
The Zernike polynomial geometry.
- Return type:
- globalize(rays)#
Convert rays from the local coordinate system to the global coordinate system.
- Parameters:
rays (RealRays) – The rays to convert.
- localize(rays)#
Convert rays from the global coordinate system to the local coordinate system.
- Parameters:
rays (RealRays) – The rays to convert.
- sag(x: NDArray, y: NDArray) NDArray[source]#
Calculate the sag of the Zernike polynomial surface at the given coordinates.
- scale(scale_factor: float)[source]#
Scale the geometry parameters.
- Parameters:
scale_factor (float) – The factor by which to scale the geometry.
- set_radius(value: float) None#
Set the radius of curvature.
- Parameters:
value (float) – The new radius of curvature.
- surface_normal(rays)#
Calculates the surface normal of the geometry at the given rays.
- to_dict() dict[source]#
Convert the Zernike polynomial geometry to a dictionary.
- Returns:
The Zernike polynomial geometry as a dictionary.
- Return type:
- update_normalization(semi_aperture: float) None[source]#
Update the normalization attributes of the geometry based on its defined normalization_mode (‘auto’ or ‘manual’). Base geometry generally does not maintain a normalization radius.
- Parameters:
semi_aperture (float) – The current semi-aperture of the surface.