geometries.polynomial#

Polynomial XY Geometry

The Polynomial XY geometry represents a surface defined by a polynomial in two dimensions. The surface is defined as:

z = r^2 / (R * (1 + sqrt(1 - (1 + k) * r^2 / R^2))) + sum(Cij * x^i * y^j)

where - r^2 = x^2 + y^2 - R is the radius of curvature - k is the conic constant - Cij are the polynomial coefficients

The coefficients are defined in a 2D array where coefficients[i][j] is the coefficient for x^i * y^j.

Historically, XY-polynomials were the first type of polynomials used for low-order freeform surfaces (see https://doi.org/10.1364/AO.38.003572). These polynomials remain common surface descriptors of freeform surfaces; however their lack of orthogonality renders them less desirable than their orthogonal counterparts for highly corrected imaging systems.

Kramer Harrison, 2024

Classes

PolynomialGeometry(coordinate_system, radius)

Represents a polynomial geometry defined as:

class PolynomialGeometry(coordinate_system, radius, conic=0.0, tol=1e-10, max_iter=100, coefficients=None)[source]#

Represents a polynomial geometry defined as:

z = r^2 / (R * (1 + sqrt(1 - (1 + k) * r^2 / R^2))) + sum(Cij * x^i * y^j)

where - r^2 = x^2 + y^2 - R is the radius of curvature - k is the conic constant - Cij are the polynomial coefficients

The coefficients are defined in a 2D array where coefficients[i][j] is the coefficient for x^i * y^j.

Historically, XY-polynomials were the first type of polynomials used for low-order freeform surfaces (see https://doi.org/10.1364/AO.38.003572). These polynomials remain common surface descriptors of freeform surfaces; however their lack of orthogonality renders them less desirable than their orthogonal counterparts for highly corrected imaging systems.

Parameters:
  • coordinate_system (CoordinateSystem) – The coordinate system of the geometry.

  • radius (float) – The radius of curvature of the base sphere.

  • conic (float, optional) – The conic constant of the base sphere. Defaults to 0.0.

  • tol (float, optional) – Tolerance for Newton-Raphson iteration. Defaults to 1e-10.

  • max_iter (int, optional) – Maximum iterations for Newton-Raphson. Defaults to 100.

  • coefficients (list[list[float]] or be.ndarray, optional) – A 2D array or list of lists representing the polynomial coefficients Cij. coefficients[i][j] is the coefficient for x^i * y^j. Defaults to an empty list (no polynomial contribution).

Variables:

c (be.ndarray) – 2D array of polynomial coefficients.

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:

  1. the primal solve converged to the intended physical root;

  2. that root stays on the same branch under small parameter changes;

  3. dF/dt is not zero or numerically singular (no tangent/grazing intersection);

  4. |n_z| is above the dtype-aware threshold, so the surface is a numerically valid local height function;

  5. sag() and _surface_normal() describe the same surface;

  6. 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 RuntimeWarning reports 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)[source]#

Creates a PolynomialGeometry from a dictionary.

Parameters:

data (dict) – The dictionary containing the geometry data.

Returns:

An instance of PolynomialGeometry.

Return type:

PolynomialGeometry

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=0, y=0)[source]#

Calculates the sag of the polynomial surface at the given coordinates.

Parameters:
  • x (float or be.ndarray, optional) – The x-coordinate(s). Defaults to 0.

  • y (float or be.ndarray, optional) – The y-coordinate(s). Defaults to 0.

Returns:

The sag value(s) at the given coordinates.

Return type:

be.ndarray or float

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.

Parameters:

rays (RealRays) – The rays, positioned at the surface, for which to calculate the surface normal.

Returns:

The surface normal components (nx, ny, nz).

Return type:

tuple[be.ndarray, be.ndarray, be.ndarray]

to_dict()[source]#

Converts the geometry to a dictionary.

Returns:

The dictionary representation of the geometry.

Return type:

dict

update_normalization(semi_aperture: float) None#

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.