RayCrafter 0.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- raycrafter/__init__.py +7 -0
- raycrafter/__main__.py +5 -0
- raycrafter/core/__init__.py +5 -0
- raycrafter/core/analysis.py +420 -0
- raycrafter/core/model.py +306 -0
- raycrafter/core/optic_builder.py +110 -0
- raycrafter/core/plot_geometry.py +93 -0
- raycrafter/core/prescription.py +118 -0
- raycrafter/core/state.py +111 -0
- raycrafter/gui/__init__.py +1 -0
- raycrafter/gui/analysis_panels.py +260 -0
- raycrafter/gui/app.py +24 -0
- raycrafter/gui/main_window.py +483 -0
- raycrafter/gui/side_view.py +468 -0
- raycrafter/gui/style.py +183 -0
- raycrafter/lenscatalog/__init__.py +1 -0
- raycrafter/lenscatalog/catalog.py +103 -0
- raycrafter/lenscatalog/data/thorlabs_achromats.json +544 -0
- raycrafter/lenscatalog/thorlabs.py +85 -0
- raycrafter/palette.py +22 -0
- raycrafter/report/__init__.py +5 -0
- raycrafter/report/assets/report.css +49 -0
- raycrafter/report/assets/report.js +57 -0
- raycrafter/report/explanations.py +73 -0
- raycrafter/report/figures.py +286 -0
- raycrafter/report/html_report.py +209 -0
- raycrafter-0.0.1.dist-info/METADATA +68 -0
- raycrafter-0.0.1.dist-info/RECORD +32 -0
- raycrafter-0.0.1.dist-info/WHEEL +5 -0
- raycrafter-0.0.1.dist-info/entry_points.txt +2 -0
- raycrafter-0.0.1.dist-info/licenses/LICENSE +21 -0
- raycrafter-0.0.1.dist-info/top_level.txt +1 -0
raycrafter/__init__.py
ADDED
raycrafter/__main__.py
ADDED
|
@@ -0,0 +1,420 @@
|
|
|
1
|
+
"""Optical analyses of the bench: spots, through-focus, ray fans, MTF, field curvature, focus.
|
|
2
|
+
|
|
3
|
+
Every function takes an ``OpticalState``, builds the Optiland system from it and returns a
|
|
4
|
+
small result dataclass holding plain numpy arrays, so that the GUI and the HTML report can
|
|
5
|
+
draw the results without touching Optiland. Lengths are in mm unless the name says otherwise.
|
|
6
|
+
|
|
7
|
+
Several analyses avoid re-tracing: after the last surface each ray is a straight line in air,
|
|
8
|
+
so its position at any other plane is position + slope * offset (see ``rms_through_focus``).
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
from dataclasses import dataclass
|
|
14
|
+
|
|
15
|
+
import numpy as np
|
|
16
|
+
from optiland.mtf import FFTMTF
|
|
17
|
+
from optiland.optic import Optic
|
|
18
|
+
from scipy.optimize import minimize_scalar
|
|
19
|
+
|
|
20
|
+
from raycrafter.core.optic_builder import WAVELENGTH_UM, build_optic
|
|
21
|
+
from raycrafter.core.state import AXIS_START_Z, OpticalState
|
|
22
|
+
|
|
23
|
+
SPOT_RINGS = 6 # hexapolar rings for the spot diagram (127 rays)
|
|
24
|
+
SIDE_VIEW_RAYS = 5 # rays across the beam diameter for the side view
|
|
25
|
+
MTF_RAYS = 128
|
|
26
|
+
FIELD_CURVATURE_MAX_ANGLE = 10.0 # degrees
|
|
27
|
+
FIELD_CURVATURE_POINTS = 11
|
|
28
|
+
FIELD_CURVATURE_FAN_RAYS = 21 # rays per fan for the field curvature estimate
|
|
29
|
+
RAY_FAN_RAYS = 41 # rays across the pupil in each ray fan (odd, so one is the chief ray)
|
|
30
|
+
THROUGH_FOCUS_DEPTHS = 4.0 # the through-focus window spans this many depths of focus
|
|
31
|
+
THROUGH_FOCUS_MAX_HALF_RANGE_MM = 100.0 # ... but never more than this from the image plane
|
|
32
|
+
AUTOFOCUS_SEARCH_LIMIT_MM = 600.0 # autofocus looks at most this far behind the image plane
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
# ----------------------------------------------------------------------------- results
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
@dataclass
|
|
39
|
+
class SpotResult:
|
|
40
|
+
"""Spot diagram data at the image plane.
|
|
41
|
+
|
|
42
|
+
``x`` and ``y`` are relative to the spot centroid, for plotting. The ``position_*`` and
|
|
43
|
+
``slope_*`` arrays are the rays' absolute transverse positions and slopes (dx/dz, dy/dz)
|
|
44
|
+
at the image plane. With them the rays can be propagated to other planes, which is how
|
|
45
|
+
autofocus and the through-focus plot work without re-tracing. ``numerical_aperture`` is
|
|
46
|
+
the sine of the half-angle of the converging cone (0 for a parallel beam).
|
|
47
|
+
"""
|
|
48
|
+
|
|
49
|
+
x: np.ndarray
|
|
50
|
+
y: np.ndarray
|
|
51
|
+
rms_radius: float
|
|
52
|
+
centroid_y: float
|
|
53
|
+
position_x: np.ndarray
|
|
54
|
+
position_y: np.ndarray
|
|
55
|
+
slope_x: np.ndarray
|
|
56
|
+
slope_y: np.ndarray
|
|
57
|
+
numerical_aperture: float
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
@dataclass
|
|
61
|
+
class RayPaths:
|
|
62
|
+
"""Ray polylines for drawing: arrays of shape (n_surfaces, n_rays) of absolute z and y."""
|
|
63
|
+
|
|
64
|
+
z: np.ndarray
|
|
65
|
+
y: np.ndarray
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
@dataclass
|
|
69
|
+
class SystemSummary:
|
|
70
|
+
"""First-order (paraxial) properties of the whole lens train."""
|
|
71
|
+
|
|
72
|
+
effective_focal_length: float # mm
|
|
73
|
+
back_focal_length: float # mm, last lens vertex to the back focal point
|
|
74
|
+
f_number: float # effective focal length / beam diameter
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
@dataclass
|
|
78
|
+
class FastTrace:
|
|
79
|
+
"""What the GUI redraws on every change: both spots, the side-view rays and the summary."""
|
|
80
|
+
|
|
81
|
+
spot: SpotResult # at the current beam angle
|
|
82
|
+
on_axis_spot: SpotResult | None # at 0 degrees; None when the beam angle is already 0
|
|
83
|
+
paths: RayPaths
|
|
84
|
+
summary: SystemSummary | None # None when there are no lenses
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
@dataclass
|
|
88
|
+
class ThroughFocusResult:
|
|
89
|
+
"""RMS spot radius against image plane position (straight-line propagation of the rays)."""
|
|
90
|
+
|
|
91
|
+
offsets: np.ndarray # mm from the current image plane (positive = further from the lens)
|
|
92
|
+
rms_current: np.ndarray # mm, at the current beam angle
|
|
93
|
+
rms_on_axis: np.ndarray | None # mm, at 0 degrees; None when the beam angle is already 0
|
|
94
|
+
best_offset: float # mm, where the current-angle RMS is smallest
|
|
95
|
+
airy_radius: float | None # mm; None for a parallel beam
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
@dataclass
|
|
99
|
+
class RayFanResult:
|
|
100
|
+
"""Transverse ray aberration at the image plane for the current beam angle (mm)."""
|
|
101
|
+
|
|
102
|
+
pupil: np.ndarray # normalised pupil coordinate, -1 to +1
|
|
103
|
+
tangential_error: np.ndarray # y offset from the chief ray against pupil y
|
|
104
|
+
sagittal_error: np.ndarray # x offset from the chief ray against pupil x
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
@dataclass
|
|
108
|
+
class MtfResult:
|
|
109
|
+
"""MTF at the current field angle, with the diffraction limit for reference."""
|
|
110
|
+
|
|
111
|
+
frequency: np.ndarray # cycles/mm
|
|
112
|
+
tangential: np.ndarray
|
|
113
|
+
sagittal: np.ndarray
|
|
114
|
+
diffraction_limit: np.ndarray
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
@dataclass
|
|
118
|
+
class FieldCurvatureResult:
|
|
119
|
+
"""Best-focus position of tangential and sagittal fans against field angle.
|
|
120
|
+
|
|
121
|
+
Shifts are in mm relative to the current image plane (positive = behind it).
|
|
122
|
+
"""
|
|
123
|
+
|
|
124
|
+
angles: np.ndarray # degrees
|
|
125
|
+
tangential_shift: np.ndarray
|
|
126
|
+
sagittal_shift: np.ndarray
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
# ------------------------------------------------------------------------- helpers
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def spot_from_rays(rays) -> SpotResult:
|
|
133
|
+
"""Build a SpotResult from Optiland rays that have been traced to the image plane."""
|
|
134
|
+
x, y = np.asarray(rays.x, dtype=float), np.asarray(rays.y, dtype=float)
|
|
135
|
+
direction_x = np.asarray(rays.L, dtype=float)
|
|
136
|
+
direction_y = np.asarray(rays.M, dtype=float)
|
|
137
|
+
direction_z = np.asarray(rays.N, dtype=float)
|
|
138
|
+
|
|
139
|
+
centroid_x, centroid_y = x.mean(), y.mean()
|
|
140
|
+
rms_radius = float(np.sqrt(np.mean((x - centroid_x) ** 2 + (y - centroid_y) ** 2)))
|
|
141
|
+
|
|
142
|
+
# Numerical aperture: the largest angle of any ray from the mean direction of the beam
|
|
143
|
+
# (using the mean direction handles tilted beams). A parallel beam gives zero.
|
|
144
|
+
cone_sine = np.hypot(direction_x - direction_x.mean(), direction_y - direction_y.mean())
|
|
145
|
+
|
|
146
|
+
return SpotResult(
|
|
147
|
+
x=x - centroid_x,
|
|
148
|
+
y=y - centroid_y,
|
|
149
|
+
rms_radius=rms_radius,
|
|
150
|
+
centroid_y=float(centroid_y),
|
|
151
|
+
position_x=x,
|
|
152
|
+
position_y=y,
|
|
153
|
+
slope_x=direction_x / direction_z,
|
|
154
|
+
slope_y=direction_y / direction_z,
|
|
155
|
+
numerical_aperture=float(cone_sine.max()),
|
|
156
|
+
)
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
def rms_through_focus(spot: SpotResult, offsets) -> np.ndarray:
|
|
160
|
+
"""RMS spot radius (mm) if the image plane were moved by each of ``offsets`` (mm).
|
|
161
|
+
|
|
162
|
+
Each ray is a straight line after the last surface, so its position at a trial plane is
|
|
163
|
+
position + slope * offset. This is vectorised over offsets (rows) and rays (columns).
|
|
164
|
+
"""
|
|
165
|
+
offsets = np.asarray(offsets, dtype=float)[:, None]
|
|
166
|
+
xs = spot.position_x[None, :] + spot.slope_x[None, :] * offsets
|
|
167
|
+
ys = spot.position_y[None, :] + spot.slope_y[None, :] * offsets
|
|
168
|
+
spread = (xs - xs.mean(axis=1, keepdims=True)) ** 2 + (ys - ys.mean(axis=1, keepdims=True)) ** 2
|
|
169
|
+
return np.sqrt(spread.mean(axis=1))
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
def airy_radius(numerical_aperture: float) -> float | None:
|
|
173
|
+
"""Radius (mm) of the first dark ring of the Airy pattern, 0.61 lambda / NA.
|
|
174
|
+
|
|
175
|
+
Returns None if the numerical aperture is effectively zero (a parallel beam).
|
|
176
|
+
"""
|
|
177
|
+
if numerical_aperture < 1e-6:
|
|
178
|
+
return None
|
|
179
|
+
return 0.61 * WAVELENGTH_UM * 1e-3 / numerical_aperture
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
def field_y_sign(field_angle: float) -> float:
|
|
183
|
+
"""Optiland field coordinates are normalised to the largest field; the sign picks +/-."""
|
|
184
|
+
return -1.0 if field_angle < 0 else 1.0
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
def _trace_spot(optic: Optic, normalised_field: float) -> SpotResult:
|
|
188
|
+
"""Trace a hexapolar spot at one normalised field."""
|
|
189
|
+
rays = optic.trace(
|
|
190
|
+
Hx=0,
|
|
191
|
+
Hy=normalised_field,
|
|
192
|
+
wavelength=WAVELENGTH_UM,
|
|
193
|
+
num_rays=SPOT_RINGS,
|
|
194
|
+
distribution="hexapolar",
|
|
195
|
+
)
|
|
196
|
+
return spot_from_rays(rays)
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
def _trace_ray_paths(state: OpticalState, optic: Optic, normalised_field: float) -> RayPaths:
|
|
200
|
+
"""Trace a meridional fan for the side view and return absolute z, y at each surface."""
|
|
201
|
+
optic.trace(
|
|
202
|
+
Hx=0,
|
|
203
|
+
Hy=normalised_field,
|
|
204
|
+
wavelength=WAVELENGTH_UM,
|
|
205
|
+
num_rays=SIDE_VIEW_RAYS,
|
|
206
|
+
distribution="line_y",
|
|
207
|
+
)
|
|
208
|
+
# Optiland keeps the per-surface ray positions of the most recent trace on its surfaces.
|
|
209
|
+
z_local = np.asarray(optic.surfaces.z)
|
|
210
|
+
y_local = np.asarray(optic.surfaces.y)
|
|
211
|
+
|
|
212
|
+
# Shift z so that the first real surface (index 1) sits at the first lens (or z = 0).
|
|
213
|
+
first_z = state.lenses[0].z if state.lenses else AXIS_START_Z
|
|
214
|
+
z_absolute = z_local - z_local[1].mean() + first_z
|
|
215
|
+
return RayPaths(z=z_absolute[1:], y=y_local[1:])
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
def _system_summary(state: OpticalState, optic: Optic) -> SystemSummary | None:
|
|
219
|
+
"""Effective and back focal length and f-number from Optiland's paraxial properties.
|
|
220
|
+
|
|
221
|
+
Optiland reports the back focal point relative to the image surface, so the back focal
|
|
222
|
+
length is measured from the last lens vertex using the image plane position.
|
|
223
|
+
"""
|
|
224
|
+
if not state.lenses:
|
|
225
|
+
return None
|
|
226
|
+
paraxial = optic.paraxial
|
|
227
|
+
effective_focal_length = float(paraxial.f2())
|
|
228
|
+
back_focal_point_z = state.image_z + float(paraxial.F2())
|
|
229
|
+
return SystemSummary(
|
|
230
|
+
effective_focal_length=effective_focal_length,
|
|
231
|
+
back_focal_length=float(back_focal_point_z - state.last_back_z()),
|
|
232
|
+
f_number=abs(effective_focal_length) / state.beam_diameter,
|
|
233
|
+
)
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
# ------------------------------------------------------------------------ analyses
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
def trace_fast(state: OpticalState) -> FastTrace:
|
|
240
|
+
"""Trace what the GUI redraws on every change, building the Optiland system only once.
|
|
241
|
+
|
|
242
|
+
The system gets two fields, 0 degrees and the current beam angle (just one if they are
|
|
243
|
+
the same). Optiland's normalised field coordinate Hy = 0 selects the first and Hy = +/-1
|
|
244
|
+
the second. The side-view ray fan is traced last because the per-surface ray positions
|
|
245
|
+
read afterwards belong to the most recent trace.
|
|
246
|
+
"""
|
|
247
|
+
angle = state.field_angle
|
|
248
|
+
has_off_axis_field = angle != 0.0
|
|
249
|
+
optic = build_optic(state, field_angles=[0.0, abs(angle)] if has_off_axis_field else [0.0])
|
|
250
|
+
sign = field_y_sign(angle)
|
|
251
|
+
|
|
252
|
+
on_axis_spot = _trace_spot(optic, 0.0)
|
|
253
|
+
spot = _trace_spot(optic, sign) if has_off_axis_field else on_axis_spot
|
|
254
|
+
|
|
255
|
+
paths = _trace_ray_paths(state, optic, sign if has_off_axis_field else 0.0)
|
|
256
|
+
summary = _system_summary(state, optic)
|
|
257
|
+
return FastTrace(spot, on_axis_spot if has_off_axis_field else None, paths, summary)
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
def trace_spot(state: OpticalState) -> SpotResult:
|
|
261
|
+
"""Trace the spot at the current beam angle only."""
|
|
262
|
+
return _trace_spot(build_optic(state), field_y_sign(state.field_angle))
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
def compute_through_focus(state: OpticalState, fast_trace: FastTrace) -> ThroughFocusResult:
|
|
266
|
+
"""RMS spot radius for image planes either side of the current one.
|
|
267
|
+
|
|
268
|
+
The window is a few depths of focus (lambda / NA^2) wide, spans both the current image
|
|
269
|
+
plane and the best focus so both are always visible, and cannot reach back into the
|
|
270
|
+
last lens.
|
|
271
|
+
"""
|
|
272
|
+
spot = fast_trace.spot
|
|
273
|
+
max_offset = THROUGH_FOCUS_MAX_HALF_RANGE_MM
|
|
274
|
+
min_offset = max(state.last_back_z() - state.image_z + 0.5, -max_offset)
|
|
275
|
+
|
|
276
|
+
# A wide, coarse scan finds the best focus wherever it is; a fine scan then refines it.
|
|
277
|
+
coarse_offsets = np.linspace(min_offset, max_offset, 400)
|
|
278
|
+
coarse_best = coarse_offsets[np.argmin(rms_through_focus(spot, coarse_offsets))]
|
|
279
|
+
coarse_step = coarse_offsets[1] - coarse_offsets[0]
|
|
280
|
+
fine_offsets = np.linspace(coarse_best - coarse_step, coarse_best + coarse_step, 201)
|
|
281
|
+
best_offset = float(fine_offsets[np.argmin(rms_through_focus(spot, fine_offsets))])
|
|
282
|
+
|
|
283
|
+
wavelength_mm = WAVELENGTH_UM * 1e-3
|
|
284
|
+
if spot.numerical_aperture > 1e-4:
|
|
285
|
+
half_range = THROUGH_FOCUS_DEPTHS * wavelength_mm / spot.numerical_aperture**2
|
|
286
|
+
else:
|
|
287
|
+
half_range = max_offset
|
|
288
|
+
half_range = float(np.clip(half_range, 0.5, max_offset))
|
|
289
|
+
|
|
290
|
+
low = max(min(0.0, best_offset) - half_range, min_offset)
|
|
291
|
+
high = min(max(0.0, best_offset) + half_range, max_offset)
|
|
292
|
+
offsets = np.linspace(low, high, 161)
|
|
293
|
+
|
|
294
|
+
on_axis_spot = fast_trace.on_axis_spot
|
|
295
|
+
return ThroughFocusResult(
|
|
296
|
+
offsets=offsets,
|
|
297
|
+
rms_current=rms_through_focus(spot, offsets),
|
|
298
|
+
rms_on_axis=(
|
|
299
|
+
rms_through_focus(on_axis_spot, offsets) if on_axis_spot is not None else None
|
|
300
|
+
),
|
|
301
|
+
best_offset=best_offset,
|
|
302
|
+
airy_radius=airy_radius(spot.numerical_aperture),
|
|
303
|
+
)
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
def compute_ray_fan(state: OpticalState) -> RayFanResult:
|
|
307
|
+
"""Transverse ray aberration at the image plane for the current beam angle.
|
|
308
|
+
|
|
309
|
+
Two fans of rays across the pupil are traced: one along y (tangential) and one along x
|
|
310
|
+
(sagittal). For each ray the error is its image-plane position minus that of the chief
|
|
311
|
+
ray, which is the middle ray of the fan. Optiland orders the fan from -1 to +1 in the
|
|
312
|
+
normalised pupil coordinate.
|
|
313
|
+
"""
|
|
314
|
+
optic = build_optic(state)
|
|
315
|
+
normalised_field = field_y_sign(state.field_angle)
|
|
316
|
+
fans = {}
|
|
317
|
+
for distribution in ("line_y", "line_x"):
|
|
318
|
+
fans[distribution] = optic.trace(
|
|
319
|
+
Hx=0,
|
|
320
|
+
Hy=normalised_field,
|
|
321
|
+
wavelength=WAVELENGTH_UM,
|
|
322
|
+
num_rays=RAY_FAN_RAYS,
|
|
323
|
+
distribution=distribution,
|
|
324
|
+
)
|
|
325
|
+
|
|
326
|
+
chief_ray = RAY_FAN_RAYS // 2
|
|
327
|
+
tangential = np.asarray(fans["line_y"].y, dtype=float)
|
|
328
|
+
sagittal = np.asarray(fans["line_x"].x, dtype=float)
|
|
329
|
+
return RayFanResult(
|
|
330
|
+
pupil=np.linspace(-1.0, 1.0, RAY_FAN_RAYS),
|
|
331
|
+
tangential_error=tangential - tangential[chief_ray],
|
|
332
|
+
sagittal_error=sagittal - sagittal[chief_ray],
|
|
333
|
+
)
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
def compute_mtf(state: OpticalState) -> MtfResult:
|
|
337
|
+
"""Diffraction MTF (FFT method) at the current beam angle, up to the cutoff frequency.
|
|
338
|
+
|
|
339
|
+
Returns tangential and sagittal curves plus the ideal diffraction-limited curve for the
|
|
340
|
+
same cutoff, so the user can see how far the system is from perfect.
|
|
341
|
+
"""
|
|
342
|
+
mtf = FFTMTF(build_optic(state), num_rays=MTF_RAYS)
|
|
343
|
+
cutoff = float(mtf.max_freq)
|
|
344
|
+
frequency = np.asarray(mtf.freq_tang[0], dtype=float)
|
|
345
|
+
keep = frequency <= cutoff
|
|
346
|
+
|
|
347
|
+
# MTF of an aberration-free circular pupil.
|
|
348
|
+
ratio = np.clip(frequency[keep] / cutoff, 0.0, 1.0)
|
|
349
|
+
ideal = (2 / np.pi) * (np.arccos(ratio) - ratio * np.sqrt(1 - ratio**2))
|
|
350
|
+
return MtfResult(
|
|
351
|
+
frequency=frequency[keep],
|
|
352
|
+
tangential=np.asarray(mtf.mtf[0][0], dtype=float)[keep],
|
|
353
|
+
sagittal=np.asarray(mtf.mtf[0][1], dtype=float)[keep],
|
|
354
|
+
diffraction_limit=ideal,
|
|
355
|
+
)
|
|
356
|
+
|
|
357
|
+
|
|
358
|
+
def _best_focus_offset(position: np.ndarray, slope: np.ndarray) -> float:
|
|
359
|
+
"""Offset along z that minimises the variance of position + slope * offset.
|
|
360
|
+
|
|
361
|
+
Setting the derivative of var(position + slope * t) to zero gives the closed form
|
|
362
|
+
t = -cov(position, slope) / var(slope).
|
|
363
|
+
"""
|
|
364
|
+
slope_variance = np.var(slope)
|
|
365
|
+
if slope_variance < 1e-18:
|
|
366
|
+
return 0.0
|
|
367
|
+
return float(-np.cov(position, slope, bias=True)[0, 1] / slope_variance)
|
|
368
|
+
|
|
369
|
+
|
|
370
|
+
def compute_field_curvature(state: OpticalState) -> FieldCurvatureResult:
|
|
371
|
+
"""Best-focus shift of the tangential and sagittal ray fans for field angles 0 to 10 degrees.
|
|
372
|
+
|
|
373
|
+
For each field angle a fan of rays across the beam is traced to the image plane (a line
|
|
374
|
+
in y for tangential, in x for sagittal), and its best focus found in closed form (see
|
|
375
|
+
``_best_focus_offset``). Zero means the image plane is in focus. This is used instead of
|
|
376
|
+
Optiland's ``FieldCurvature`` class (see log.md).
|
|
377
|
+
"""
|
|
378
|
+
angles = np.linspace(0.0, FIELD_CURVATURE_MAX_ANGLE, FIELD_CURVATURE_POINTS)
|
|
379
|
+
optic = build_optic(state, field_angles=list(angles))
|
|
380
|
+
|
|
381
|
+
tangential, sagittal = [], []
|
|
382
|
+
for angle in angles:
|
|
383
|
+
normalised_field = angle / FIELD_CURVATURE_MAX_ANGLE
|
|
384
|
+
trace_settings = dict(
|
|
385
|
+
Hx=0,
|
|
386
|
+
Hy=normalised_field,
|
|
387
|
+
wavelength=WAVELENGTH_UM,
|
|
388
|
+
num_rays=FIELD_CURVATURE_FAN_RAYS,
|
|
389
|
+
)
|
|
390
|
+
fan_y = optic.trace(distribution="line_y", **trace_settings)
|
|
391
|
+
fan_x = optic.trace(distribution="line_x", **trace_settings)
|
|
392
|
+
tangential.append(
|
|
393
|
+
_best_focus_offset(np.asarray(fan_y.y), np.asarray(fan_y.M) / np.asarray(fan_y.N))
|
|
394
|
+
)
|
|
395
|
+
sagittal.append(
|
|
396
|
+
_best_focus_offset(np.asarray(fan_x.x), np.asarray(fan_x.L) / np.asarray(fan_x.N))
|
|
397
|
+
)
|
|
398
|
+
|
|
399
|
+
return FieldCurvatureResult(angles, np.array(tangential), np.array(sagittal))
|
|
400
|
+
|
|
401
|
+
|
|
402
|
+
def find_best_focus_z(state: OpticalState) -> float:
|
|
403
|
+
"""The image plane z that minimises the RMS spot radius at the current beam angle.
|
|
404
|
+
|
|
405
|
+
The rays are traced once to the current image plane, then propagated in straight lines
|
|
406
|
+
(they are in air) to trial planes, so no re-tracing is needed. A coarse scan comes first
|
|
407
|
+
so the minimiser cannot land in a local minimum. The result is never in front of the
|
|
408
|
+
back of the last lens.
|
|
409
|
+
"""
|
|
410
|
+
spot = trace_spot(state)
|
|
411
|
+
min_z = state.last_back_z()
|
|
412
|
+
offsets = np.linspace(min_z - state.image_z, AUTOFOCUS_SEARCH_LIMIT_MM, 400)
|
|
413
|
+
coarse_best_offset = offsets[np.argmin(rms_through_focus(spot, offsets))]
|
|
414
|
+
step = offsets[1] - offsets[0]
|
|
415
|
+
refined = minimize_scalar(
|
|
416
|
+
lambda offset: float(rms_through_focus(spot, [offset])[0]),
|
|
417
|
+
bounds=(coarse_best_offset - step, coarse_best_offset + step),
|
|
418
|
+
method="bounded",
|
|
419
|
+
)
|
|
420
|
+
return max(state.image_z + refined.x, min_z)
|