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/core/model.py
ADDED
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
"""The lens bench model: the one object that front ends (GUI, report, scripts) talk to.
|
|
2
|
+
|
|
3
|
+
``OpticalModel`` owns the bench state (``raycrafter.core.state``) and the list of lenses
|
|
4
|
+
that can be placed. It provides the editing operations: add, delete, move and swap lenses,
|
|
5
|
+
and make or split Plossl pairs. It also gives access to the analyses
|
|
6
|
+
(``raycrafter.core.analysis``) and to Zemax export, applied to its current state. It has
|
|
7
|
+
no Qt code.
|
|
8
|
+
|
|
9
|
+
Example
|
|
10
|
+
-------
|
|
11
|
+
>>> from raycrafter.core.model import OpticalModel
|
|
12
|
+
>>> model = OpticalModel() # starts with one ACT508-200-A on the axis
|
|
13
|
+
>>> model.autofocus() # doctest: +SKIP
|
|
14
|
+
>>> model.trace_fast().spot.rms_radius # doctest: +SKIP
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
from __future__ import annotations
|
|
18
|
+
|
|
19
|
+
import numpy as np
|
|
20
|
+
|
|
21
|
+
from raycrafter.core import analysis, optic_builder
|
|
22
|
+
from raycrafter.core.prescription import prescription_from_zmx_text, surface_sag
|
|
23
|
+
from raycrafter.core.state import AXIS_START_Z, OpticalState, PlacedLens
|
|
24
|
+
from raycrafter.lenscatalog import thorlabs
|
|
25
|
+
from raycrafter.lenscatalog.catalog import CatalogEntry
|
|
26
|
+
|
|
27
|
+
DEFAULT_LENS_PART = "ACT508-200-A" # the lens placed when the model is created
|
|
28
|
+
INITIAL_LENS_Z = 100.0 # mm, front vertex of the first lens
|
|
29
|
+
LENS_GAP_MM = 10.0 # air gap left between a new lens and its left neighbour
|
|
30
|
+
IMAGE_CLEARANCE_MM = 50.0 # a new lens pushes the image plane at least this far behind it
|
|
31
|
+
FIRST_LENS_MIN_Z = AXIS_START_Z + 20.0 # lenses cannot be dragged closer to the axis start
|
|
32
|
+
PLOSSL_GAP_MM = 1.0 # closest distance between the two facing surfaces of a Plossl pair
|
|
33
|
+
PLOSSL_SEARCH_WIDTHS = 2.0 # a Plossl partner must be within this many lens thicknesses
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class OpticalModel:
|
|
37
|
+
"""A row of lenses on an optical axis, a beam and an image plane.
|
|
38
|
+
|
|
39
|
+
Parameters
|
|
40
|
+
----------
|
|
41
|
+
catalog : dict, optional
|
|
42
|
+
Part number -> CatalogEntry for the lenses that can be placed. Defaults to the
|
|
43
|
+
packaged Thorlabs achromats.
|
|
44
|
+
add_default_lens : bool
|
|
45
|
+
If True (the default), start with one DEFAULT_LENS_PART on the axis.
|
|
46
|
+
|
|
47
|
+
Attributes
|
|
48
|
+
----------
|
|
49
|
+
state : OpticalState
|
|
50
|
+
The lenses, beam and image plane. Front ends may set the beam properties and
|
|
51
|
+
``image_z`` directly; lens positions should be changed through the methods here.
|
|
52
|
+
catalog : dict
|
|
53
|
+
Part number -> CatalogEntry.
|
|
54
|
+
"""
|
|
55
|
+
|
|
56
|
+
def __init__(
|
|
57
|
+
self, catalog: dict[str, CatalogEntry] | None = None, add_default_lens: bool = True
|
|
58
|
+
) -> None:
|
|
59
|
+
self.state = OpticalState()
|
|
60
|
+
self.catalog = catalog if catalog is not None else thorlabs.load_achromats()
|
|
61
|
+
if add_default_lens and DEFAULT_LENS_PART in self.catalog:
|
|
62
|
+
self.add_lens(DEFAULT_LENS_PART)
|
|
63
|
+
|
|
64
|
+
# --------------------------------------------------------------- adding and removing
|
|
65
|
+
|
|
66
|
+
def add_lens(self, part_number: str) -> PlacedLens:
|
|
67
|
+
"""Place a catalogue lens just to the right of the rightmost lens.
|
|
68
|
+
|
|
69
|
+
The first lens goes at INITIAL_LENS_Z. The image plane is pushed back if the new
|
|
70
|
+
lens would otherwise end up beyond it.
|
|
71
|
+
|
|
72
|
+
Returns
|
|
73
|
+
-------
|
|
74
|
+
PlacedLens
|
|
75
|
+
The new lens.
|
|
76
|
+
"""
|
|
77
|
+
entry = self.catalog[part_number]
|
|
78
|
+
lenses = self.state.lenses
|
|
79
|
+
front_z = lenses[-1].back_z + LENS_GAP_MM if lenses else INITIAL_LENS_Z
|
|
80
|
+
|
|
81
|
+
new_lens = PlacedLens(
|
|
82
|
+
part_number=part_number,
|
|
83
|
+
surfaces=prescription_from_zmx_text(entry.zmx_text),
|
|
84
|
+
semi_aperture=entry.semi_aperture,
|
|
85
|
+
z=front_z,
|
|
86
|
+
is_negative=entry.focal_length < 0,
|
|
87
|
+
)
|
|
88
|
+
lenses.append(new_lens)
|
|
89
|
+
self.state.image_z = max(self.state.image_z, new_lens.back_z + IMAGE_CLEARANCE_MM)
|
|
90
|
+
return new_lens
|
|
91
|
+
|
|
92
|
+
def delete_lens(self, lens_index: int) -> None:
|
|
93
|
+
"""Remove one lens. Its Plossl partner, if any, becomes a normal lens."""
|
|
94
|
+
self.split_plossl(lens_index)
|
|
95
|
+
del self.state.lenses[lens_index]
|
|
96
|
+
|
|
97
|
+
def flip_lens(self, lens_index: int) -> None:
|
|
98
|
+
"""Turn one lens around. Lenses in a Plossl pair cannot be flipped on their own."""
|
|
99
|
+
lens = self.state.lenses[lens_index]
|
|
100
|
+
if lens.group_id is None:
|
|
101
|
+
lens.flipped = not lens.flipped
|
|
102
|
+
|
|
103
|
+
# ---------------------------------------------------------------- moving
|
|
104
|
+
|
|
105
|
+
def group_indices(self, lens_index: int) -> list[int]:
|
|
106
|
+
"""Indices of the lenses that move together with this one (just itself if ungrouped)."""
|
|
107
|
+
lenses = self.state.lenses
|
|
108
|
+
group_id = lenses[lens_index].group_id
|
|
109
|
+
if group_id is None:
|
|
110
|
+
return [lens_index]
|
|
111
|
+
return [index for index, lens in enumerate(lenses) if lens.group_id == group_id]
|
|
112
|
+
|
|
113
|
+
def _block_centre_z(self, members: list[int]) -> float:
|
|
114
|
+
"""z of the middle of a block of adjacent lenses, from its first front to last back."""
|
|
115
|
+
lenses = self.state.lenses
|
|
116
|
+
return 0.5 * (lenses[members[0]].z + lenses[members[-1]].back_z)
|
|
117
|
+
|
|
118
|
+
def move_lens(self, lens_index: int, requested_front_z: float) -> list[int] | None:
|
|
119
|
+
"""Move a lens (and its Plossl partner) so this lens's front vertex is near the request.
|
|
120
|
+
|
|
121
|
+
If the request carries the group's centre past the centre of the neighbouring lens
|
|
122
|
+
(or Plossl pair), the two swap places (see ``swap_neighbouring_blocks``). Otherwise
|
|
123
|
+
the group stays between its neighbours (nothing overlaps), the first lens stays to
|
|
124
|
+
the right of the start of the axis, and the last lens stays before the image plane;
|
|
125
|
+
if the allowed range is empty the lens does not move.
|
|
126
|
+
|
|
127
|
+
Returns
|
|
128
|
+
-------
|
|
129
|
+
list of int or None
|
|
130
|
+
After a swap, the new order as a list of old indices, so the caller can reorder
|
|
131
|
+
anything it keeps per lens. None when the order is unchanged.
|
|
132
|
+
"""
|
|
133
|
+
lenses = self.state.lenses
|
|
134
|
+
members = self.group_indices(lens_index)
|
|
135
|
+
first, last = members[0], members[-1]
|
|
136
|
+
|
|
137
|
+
shift_wanted = requested_front_z - lenses[lens_index].z
|
|
138
|
+
wanted_centre_z = self._block_centre_z(members) + shift_wanted
|
|
139
|
+
|
|
140
|
+
# Dragged past the centre of the next block to the right, or the previous one?
|
|
141
|
+
if last < len(lenses) - 1:
|
|
142
|
+
right_members = self.group_indices(last + 1)
|
|
143
|
+
if wanted_centre_z > self._block_centre_z(right_members):
|
|
144
|
+
return self.swap_neighbouring_blocks(members, right_members)
|
|
145
|
+
if first > 0:
|
|
146
|
+
left_members = self.group_indices(first - 1)
|
|
147
|
+
if wanted_centre_z < self._block_centre_z(left_members):
|
|
148
|
+
return self.swap_neighbouring_blocks(left_members, members)
|
|
149
|
+
|
|
150
|
+
group_width = lenses[last].back_z - lenses[first].z
|
|
151
|
+
lowest_front_z = lenses[first - 1].back_z if first > 0 else FIRST_LENS_MIN_Z
|
|
152
|
+
if last < len(lenses) - 1:
|
|
153
|
+
next_front_z = lenses[last + 1].z
|
|
154
|
+
else:
|
|
155
|
+
next_front_z = self.state.image_z - 1.0
|
|
156
|
+
highest_front_z = next_front_z - group_width
|
|
157
|
+
|
|
158
|
+
shift_min = lowest_front_z - lenses[first].z
|
|
159
|
+
shift_max = highest_front_z - lenses[first].z
|
|
160
|
+
if shift_max < shift_min:
|
|
161
|
+
return None
|
|
162
|
+
shift = float(np.clip(shift_wanted, shift_min, shift_max))
|
|
163
|
+
for index in members:
|
|
164
|
+
lenses[index].z += shift
|
|
165
|
+
return None
|
|
166
|
+
|
|
167
|
+
def swap_neighbouring_blocks(
|
|
168
|
+
self, left_members: list[int], right_members: list[int]
|
|
169
|
+
) -> list[int]:
|
|
170
|
+
"""Exchange two adjacent lens groups, so the left one ends up on the right.
|
|
171
|
+
|
|
172
|
+
Each block keeps its internal spacing. The air gap between them is kept, and the two
|
|
173
|
+
together occupy exactly the same stretch of axis as before, so nothing downstream
|
|
174
|
+
(later lenses, the image plane) has to move.
|
|
175
|
+
|
|
176
|
+
Returns
|
|
177
|
+
-------
|
|
178
|
+
list of int
|
|
179
|
+
The new order as a list of old lens indices.
|
|
180
|
+
"""
|
|
181
|
+
lenses = self.state.lenses
|
|
182
|
+
left_block = [lenses[index] for index in left_members]
|
|
183
|
+
right_block = [lenses[index] for index in right_members]
|
|
184
|
+
|
|
185
|
+
left_start = left_block[0].z
|
|
186
|
+
right_start = right_block[0].z
|
|
187
|
+
gap = right_start - left_block[-1].back_z
|
|
188
|
+
right_width = right_block[-1].back_z - right_start
|
|
189
|
+
|
|
190
|
+
# The old right block now starts where the old left one did; the old left one follows.
|
|
191
|
+
for lens in right_block:
|
|
192
|
+
lens.z += left_start - right_start
|
|
193
|
+
for lens in left_block:
|
|
194
|
+
lens.z += right_width + gap
|
|
195
|
+
|
|
196
|
+
low, high = left_members[0], right_members[-1]
|
|
197
|
+
lenses[low : high + 1] = right_block + left_block
|
|
198
|
+
return list(range(low)) + right_members + left_members + list(range(high + 1, len(lenses)))
|
|
199
|
+
|
|
200
|
+
# ---------------------------------------------------------------- Plossl pairs
|
|
201
|
+
|
|
202
|
+
def plossl_partner(self, lens_index: int) -> int | None:
|
|
203
|
+
"""Index of a lens this one could form a Plossl pair with, or None.
|
|
204
|
+
|
|
205
|
+
The partner must be an immediate neighbour, be the same part, and not already be
|
|
206
|
+
grouped; the gap between the two must be at most PLOSSL_SEARCH_WIDTHS lens
|
|
207
|
+
thicknesses. Only positive lenses can form a Plossl pair.
|
|
208
|
+
"""
|
|
209
|
+
lenses = self.state.lenses
|
|
210
|
+
lens = lenses[lens_index]
|
|
211
|
+
if lens.group_id is not None or lens.is_negative:
|
|
212
|
+
return None
|
|
213
|
+
for other_index in (lens_index - 1, lens_index + 1):
|
|
214
|
+
if not 0 <= other_index < len(lenses):
|
|
215
|
+
continue
|
|
216
|
+
other = lenses[other_index]
|
|
217
|
+
left, right = sorted((lens, other), key=lambda item: item.z)
|
|
218
|
+
gap = right.z - left.back_z
|
|
219
|
+
if (
|
|
220
|
+
other.part_number == lens.part_number
|
|
221
|
+
and other.group_id is None
|
|
222
|
+
and gap <= PLOSSL_SEARCH_WIDTHS * lens.thickness
|
|
223
|
+
):
|
|
224
|
+
return other_index
|
|
225
|
+
return None
|
|
226
|
+
|
|
227
|
+
def make_plossl(self, lens_index: int) -> None:
|
|
228
|
+
"""Snap the lens and its partner into a Plossl pair and group them.
|
|
229
|
+
|
|
230
|
+
A Plossl pair is two identical doublets facing each other symmetrically, with their
|
|
231
|
+
positive (crown) elements on the inside. The left lens is flipped if needed so its
|
|
232
|
+
positive element is at its back, and the right lens so its positive element is at
|
|
233
|
+
its front. The right lens is then moved so the closest points of the two facing
|
|
234
|
+
surfaces are PLOSSL_GAP_MM apart, and everything downstream (later lenses and the
|
|
235
|
+
image plane) shifts with it. Does nothing if the lens has no partner.
|
|
236
|
+
"""
|
|
237
|
+
partner_index = self.plossl_partner(lens_index)
|
|
238
|
+
if partner_index is None:
|
|
239
|
+
return
|
|
240
|
+
left_index, right_index = sorted((lens_index, partner_index))
|
|
241
|
+
lenses = self.state.lenses
|
|
242
|
+
left, right = lenses[left_index], lenses[right_index]
|
|
243
|
+
|
|
244
|
+
left.flipped = left.positive_element_is_first() # moves the positive element to the back
|
|
245
|
+
right.flipped = not right.positive_element_is_first() # keeps or puts it at the front
|
|
246
|
+
|
|
247
|
+
original_right_z = right.z
|
|
248
|
+
right.z = self._plossl_front_z(left, right)
|
|
249
|
+
shift = right.z - original_right_z
|
|
250
|
+
for downstream in lenses[right_index + 1 :]:
|
|
251
|
+
downstream.z += shift
|
|
252
|
+
self.state.image_z = max(self.state.image_z + shift, self.state.last_back_z() + 1.0)
|
|
253
|
+
|
|
254
|
+
left.group_id = right.group_id = self.state.next_group_id
|
|
255
|
+
self.state.next_group_id += 1
|
|
256
|
+
|
|
257
|
+
@staticmethod
|
|
258
|
+
def _plossl_front_z(left: PlacedLens, right: PlacedLens) -> float:
|
|
259
|
+
"""Front-vertex z for ``right`` so the facing surfaces are PLOSSL_GAP_MM apart at closest.
|
|
260
|
+
|
|
261
|
+
The facing surfaces are the back of ``left`` and the front of ``right`` in their
|
|
262
|
+
current orientations. The gap is measured along z at each height across the smaller
|
|
263
|
+
clear aperture, and the smallest value (at the vertices for two convex surfaces,
|
|
264
|
+
nearer the rim if either is concave) is set to the required gap.
|
|
265
|
+
"""
|
|
266
|
+
heights = np.linspace(0.0, min(left.semi_aperture, right.semi_aperture), 200)
|
|
267
|
+
back_sag = surface_sag(left.surfaces_in_use()[-1].radius, heights)
|
|
268
|
+
front_sag = surface_sag(right.surfaces_in_use()[0].radius, heights)
|
|
269
|
+
gap_if_vertices_touched = front_sag - back_sag
|
|
270
|
+
return left.back_z + PLOSSL_GAP_MM - float(gap_if_vertices_touched.min())
|
|
271
|
+
|
|
272
|
+
def split_plossl(self, lens_index: int) -> None:
|
|
273
|
+
"""Dissolve the lens's group so its lenses move independently again."""
|
|
274
|
+
for index in self.group_indices(lens_index):
|
|
275
|
+
self.state.lenses[index].group_id = None
|
|
276
|
+
|
|
277
|
+
# ---------------------------------------------------------------- optics
|
|
278
|
+
|
|
279
|
+
def autofocus(self) -> float:
|
|
280
|
+
"""Move the image plane to the z with the smallest RMS spot radius, and return it."""
|
|
281
|
+
self.state.image_z = analysis.find_best_focus_z(self.state)
|
|
282
|
+
return self.state.image_z
|
|
283
|
+
|
|
284
|
+
def trace_fast(self) -> analysis.FastTrace:
|
|
285
|
+
"""Spots, side-view rays and first-order summary (see ``analysis.trace_fast``)."""
|
|
286
|
+
return analysis.trace_fast(self.state)
|
|
287
|
+
|
|
288
|
+
def compute_through_focus(self, fast_trace: analysis.FastTrace) -> analysis.ThroughFocusResult:
|
|
289
|
+
"""RMS radius against image plane position (see ``analysis.compute_through_focus``)."""
|
|
290
|
+
return analysis.compute_through_focus(self.state, fast_trace)
|
|
291
|
+
|
|
292
|
+
def compute_ray_fan(self) -> analysis.RayFanResult:
|
|
293
|
+
"""Transverse ray aberration (see ``analysis.compute_ray_fan``)."""
|
|
294
|
+
return analysis.compute_ray_fan(self.state)
|
|
295
|
+
|
|
296
|
+
def compute_mtf(self) -> analysis.MtfResult:
|
|
297
|
+
"""FFT MTF at the current beam angle (see ``analysis.compute_mtf``)."""
|
|
298
|
+
return analysis.compute_mtf(self.state)
|
|
299
|
+
|
|
300
|
+
def compute_field_curvature(self) -> analysis.FieldCurvatureResult:
|
|
301
|
+
"""Best focus against field angle (see ``analysis.compute_field_curvature``)."""
|
|
302
|
+
return analysis.compute_field_curvature(self.state)
|
|
303
|
+
|
|
304
|
+
def save_zmx(self, file_path: str) -> None:
|
|
305
|
+
"""Write the current system to a Zemax .zmx file."""
|
|
306
|
+
optic_builder.save_zmx(self.state, file_path)
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"""Build an Optiland ``Optic`` from the bench state, and export it as a Zemax file.
|
|
2
|
+
|
|
3
|
+
A fresh Optiland system is built from ``OpticalState`` for every update, rather than
|
|
4
|
+
editing a long-lived one (see log.md, "rebuild the Optiland system on every change").
|
|
5
|
+
|
|
6
|
+
A collimated beam is modelled as an object at infinity with an entrance pupil diameter
|
|
7
|
+
equal to the beam diameter. A converging or diverging beam moves the object to a finite
|
|
8
|
+
(possibly virtual) distance instead.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
import warnings
|
|
14
|
+
|
|
15
|
+
import numpy as np
|
|
16
|
+
from optiland.fileio import save_zemax_file
|
|
17
|
+
from optiland.optic import Optic
|
|
18
|
+
|
|
19
|
+
from raycrafter.core.state import AXIS_START_Z, OpticalState
|
|
20
|
+
|
|
21
|
+
WAVELENGTH_UM = 0.555 # the single wavelength used throughout (green)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def object_distance_mm(beam_vergence: float) -> float:
|
|
25
|
+
"""Distance (mm) from the object to the first lens surface that gives the beam its vergence.
|
|
26
|
+
|
|
27
|
+
A collimated beam (vergence 0) comes from infinity. A diverging beam (negative vergence)
|
|
28
|
+
comes from a real point source 1000/|V| mm in front of the first surface. A converging
|
|
29
|
+
beam (positive vergence) is aimed at a point 1000/V mm behind the first surface, which is
|
|
30
|
+
a virtual object, i.e. a negative object distance.
|
|
31
|
+
|
|
32
|
+
Parameters
|
|
33
|
+
----------
|
|
34
|
+
beam_vergence : float
|
|
35
|
+
Vergence in dioptres (1/m).
|
|
36
|
+
"""
|
|
37
|
+
if beam_vergence == 0.0:
|
|
38
|
+
return np.inf
|
|
39
|
+
return -1000.0 / beam_vergence
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def build_optic(state: OpticalState, field_angles: list[float] | None = None) -> Optic:
|
|
43
|
+
"""Create a fresh Optiland system from the bench state.
|
|
44
|
+
|
|
45
|
+
Lenses are chained left to right. The last surface of each lens is followed by air up
|
|
46
|
+
to the next lens (or the image plane). The first surface is the aperture stop. With no
|
|
47
|
+
lenses a flat air surface is the stop, so the beam still reaches the image.
|
|
48
|
+
|
|
49
|
+
Parameters
|
|
50
|
+
----------
|
|
51
|
+
state : OpticalState
|
|
52
|
+
The lenses, beam and image plane.
|
|
53
|
+
field_angles : list of float, optional
|
|
54
|
+
Field angles in degrees. By default there is one field, at the current beam angle.
|
|
55
|
+
|
|
56
|
+
Returns
|
|
57
|
+
-------
|
|
58
|
+
optiland.optic.Optic
|
|
59
|
+
The system, ready to trace.
|
|
60
|
+
"""
|
|
61
|
+
optic = Optic()
|
|
62
|
+
optic.surfaces.add(index=0, radius=np.inf, thickness=object_distance_mm(state.beam_vergence))
|
|
63
|
+
|
|
64
|
+
if state.lenses:
|
|
65
|
+
surface_index = 1
|
|
66
|
+
for lens_number, lens in enumerate(state.lenses):
|
|
67
|
+
is_last_lens = lens_number == len(state.lenses) - 1
|
|
68
|
+
next_front_z = state.image_z if is_last_lens else state.lenses[lens_number + 1].z
|
|
69
|
+
gap_after_lens = max(next_front_z - lens.back_z, 0.0)
|
|
70
|
+
|
|
71
|
+
surfaces = lens.surfaces_in_use()
|
|
72
|
+
for surface_number, surface in enumerate(surfaces):
|
|
73
|
+
is_last_surface = surface_number == len(surfaces) - 1
|
|
74
|
+
surface_settings: dict[str, object] = dict(
|
|
75
|
+
index=surface_index,
|
|
76
|
+
radius=surface.radius,
|
|
77
|
+
thickness=gap_after_lens if is_last_surface else surface.thickness_after,
|
|
78
|
+
is_stop=(surface_index == 1),
|
|
79
|
+
)
|
|
80
|
+
# Optiland uses air when no material is given ("material=None" is rejected).
|
|
81
|
+
if surface.material_after != "air" and not is_last_surface:
|
|
82
|
+
surface_settings["material"] = surface.material_after
|
|
83
|
+
optic.surfaces.add(**surface_settings)
|
|
84
|
+
surface_index += 1
|
|
85
|
+
image_index = surface_index
|
|
86
|
+
else:
|
|
87
|
+
optic.surfaces.add(
|
|
88
|
+
index=1,
|
|
89
|
+
radius=np.inf,
|
|
90
|
+
thickness=max(state.image_z - AXIS_START_Z, 0.0),
|
|
91
|
+
is_stop=True,
|
|
92
|
+
)
|
|
93
|
+
image_index = 2
|
|
94
|
+
|
|
95
|
+
optic.surfaces.add(index=image_index)
|
|
96
|
+
optic.set_aperture("EPD", state.beam_diameter)
|
|
97
|
+
optic.fields.set_type("angle")
|
|
98
|
+
for angle in field_angles if field_angles is not None else [abs(state.field_angle)]:
|
|
99
|
+
optic.fields.add(y=angle)
|
|
100
|
+
optic.wavelengths.add(WAVELENGTH_UM, is_primary=True)
|
|
101
|
+
return optic
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def save_zmx(state: OpticalState, file_path: str) -> None:
|
|
105
|
+
"""Write the lens arrangement (lenses, gaps and image plane) to a Zemax .zmx file."""
|
|
106
|
+
with warnings.catch_warnings():
|
|
107
|
+
# Glasses missing from Zemax's catalogue are written as model glass; that warning is
|
|
108
|
+
# expected.
|
|
109
|
+
warnings.simplefilter("ignore")
|
|
110
|
+
save_zemax_file(build_optic(state), file_path)
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"""Geometry for drawing the bench, shared by the GUI and the HTML report.
|
|
2
|
+
|
|
3
|
+
These functions turn model data into plain arrays to draw (lens outlines, rays, plot
|
|
4
|
+
ranges). They know nothing about Qt or matplotlib, so both front ends draw the same
|
|
5
|
+
picture.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import numpy as np
|
|
11
|
+
|
|
12
|
+
from raycrafter.core.analysis import RayPaths, SpotResult, airy_radius
|
|
13
|
+
from raycrafter.core.prescription import surface_sag
|
|
14
|
+
from raycrafter.core.state import OpticalState, PlacedLens
|
|
15
|
+
|
|
16
|
+
AXIS_Z_RANGE = (0.0, 450.0) # mm of optical axis shown in the side view
|
|
17
|
+
AXIS_Y_RANGE = (-32.0, 32.0) # mm either side of the axis shown in the side view
|
|
18
|
+
MIN_SPOT_HALF_WIDTH_UM = 5.0 # the spot plot never zooms in beyond +/-5 um
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def lens_surface_arcs(lens: PlacedLens, point_count: int = 60) -> tuple[np.ndarray, list]:
|
|
22
|
+
"""The curve of every surface of a lens, in its current position and orientation.
|
|
23
|
+
|
|
24
|
+
Returns
|
|
25
|
+
-------
|
|
26
|
+
edge_y : numpy.ndarray
|
|
27
|
+
Heights from -semi_aperture to +semi_aperture (mm).
|
|
28
|
+
surface_arcs_z : list of numpy.ndarray
|
|
29
|
+
For each surface, front to back, the z of the surface at each height in ``edge_y``.
|
|
30
|
+
The first and last arcs bound the glass body; the ones between are cemented faces.
|
|
31
|
+
"""
|
|
32
|
+
edge_y = np.linspace(-lens.semi_aperture, lens.semi_aperture, point_count)
|
|
33
|
+
surface_arcs_z = []
|
|
34
|
+
vertex_z = lens.z
|
|
35
|
+
for surface in lens.surfaces_in_use():
|
|
36
|
+
surface_arcs_z.append(vertex_z + surface_sag(surface.radius, edge_y))
|
|
37
|
+
vertex_z += surface.thickness_after
|
|
38
|
+
return edge_y, surface_arcs_z
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def rays_from_axis_start(state: OpticalState, paths: RayPaths) -> list[tuple]:
|
|
42
|
+
"""Each traced side-view ray, extended back to the start of the axis.
|
|
43
|
+
|
|
44
|
+
Optiland's ray paths begin at the first lens. To draw the incoming beam, each ray is
|
|
45
|
+
extended back to the left edge of the side view along its incoming direction. That slope
|
|
46
|
+
is the beam tilt plus the vergence: a ray at height h is bent by -V * h (V in 1/mm), so
|
|
47
|
+
converging beams (V > 0) are wider before the lens than at it, diverging ones narrower.
|
|
48
|
+
|
|
49
|
+
Returns
|
|
50
|
+
-------
|
|
51
|
+
list of (z, y) tuples of numpy arrays, one per ray.
|
|
52
|
+
"""
|
|
53
|
+
tilt_slope = np.tan(np.radians(state.field_angle))
|
|
54
|
+
vergence_per_mm = state.beam_vergence / 1000.0
|
|
55
|
+
start_z = AXIS_Z_RANGE[0]
|
|
56
|
+
|
|
57
|
+
rays = []
|
|
58
|
+
for ray_index in range(paths.z.shape[1]):
|
|
59
|
+
height_at_first_surface = paths.y[0, ray_index]
|
|
60
|
+
incoming_slope = tilt_slope - vergence_per_mm * height_at_first_surface
|
|
61
|
+
start_y = height_at_first_surface - incoming_slope * (paths.z[0, ray_index] - start_z)
|
|
62
|
+
ray_z = np.concatenate([[start_z], paths.z[:, ray_index]])
|
|
63
|
+
ray_y = np.concatenate([[start_y], paths.y[:, ray_index]])
|
|
64
|
+
rays.append((ray_z, ray_y))
|
|
65
|
+
return rays
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def spot_view_half_width_um(spot: SpotResult, on_axis_spot: SpotResult | None) -> float:
|
|
69
|
+
"""Half-width (um) of the square spot plot.
|
|
70
|
+
|
|
71
|
+
The view reaches 20% beyond the spots and is never smaller than MIN_SPOT_HALF_WIDTH_UM.
|
|
72
|
+
It also widens to show the Airy circle, unless that is far bigger than the spots: then
|
|
73
|
+
the circle is cropped rather than shrinking the spots to a dot.
|
|
74
|
+
"""
|
|
75
|
+
extent_um = np.abs(np.concatenate([spot.x, spot.y])).max() * 1000.0
|
|
76
|
+
if on_axis_spot is not None:
|
|
77
|
+
on_axis_extent_um = np.abs(np.concatenate([on_axis_spot.x, on_axis_spot.y])).max() * 1000
|
|
78
|
+
extent_um = max(extent_um, on_axis_extent_um)
|
|
79
|
+
|
|
80
|
+
half_width = max(extent_um * 1.2, MIN_SPOT_HALF_WIDTH_UM)
|
|
81
|
+
airy = airy_radius(spot.numerical_aperture)
|
|
82
|
+
if airy is not None:
|
|
83
|
+
half_width = max(half_width, min(airy * 1000.0 * 1.2, half_width * 3.0))
|
|
84
|
+
return float(half_width)
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def ray_fan_limit_um(largest_error_mm: float) -> float:
|
|
88
|
+
"""Half-height (um) of the zero-centred ray fan plot.
|
|
89
|
+
|
|
90
|
+
The range is fixed about zero so that a perfect system shows as a flat line rather than
|
|
91
|
+
a zoomed-in plot of numerical noise.
|
|
92
|
+
"""
|
|
93
|
+
return max(largest_error_mm * 1000.0 * 1.15, 1.0)
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
"""Lens prescriptions: the surfaces of a single catalogue lens.
|
|
2
|
+
|
|
3
|
+
A prescription is a list of ``LensSurface`` objects, front to back, each holding the radius
|
|
4
|
+
of curvature, the distance to the next surface and the material that fills that gap. This
|
|
5
|
+
module reads prescriptions from Zemax ZMX text (through Optiland), flips them, and computes
|
|
6
|
+
simple geometry such as surface sag and lens thickness. It has no Qt code.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
import tempfile
|
|
12
|
+
import warnings
|
|
13
|
+
from dataclasses import dataclass
|
|
14
|
+
from pathlib import Path
|
|
15
|
+
|
|
16
|
+
import numpy as np
|
|
17
|
+
from optiland.fileio import load_zemax_file
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@dataclass
|
|
21
|
+
class LensSurface:
|
|
22
|
+
"""One lens surface: its radius of curvature and the material that follows it.
|
|
23
|
+
|
|
24
|
+
Attributes
|
|
25
|
+
----------
|
|
26
|
+
radius : float
|
|
27
|
+
Radius of curvature in mm; ``inf`` for a flat surface.
|
|
28
|
+
thickness_after : float
|
|
29
|
+
Distance in mm from this surface's vertex to the next one.
|
|
30
|
+
material_after : str
|
|
31
|
+
Glass name of the material after this surface, or ``"air"``.
|
|
32
|
+
"""
|
|
33
|
+
|
|
34
|
+
radius: float
|
|
35
|
+
thickness_after: float
|
|
36
|
+
material_after: str
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def prescription_from_zmx_text(zmx_text: str) -> list[LensSurface]:
|
|
40
|
+
"""Parse ZMX text with Optiland and return the lens surfaces.
|
|
41
|
+
|
|
42
|
+
The text is written to a temporary UTF-8 file because Optiland's reader needs a path.
|
|
43
|
+
The object and image surfaces are dropped (the bench places its own image plane), and so
|
|
44
|
+
is any leading flat air surface (the air-spaced doublets have one). A surface is followed
|
|
45
|
+
by glass if Optiland gives its following material a name; otherwise it is air.
|
|
46
|
+
|
|
47
|
+
Parameters
|
|
48
|
+
----------
|
|
49
|
+
zmx_text : str
|
|
50
|
+
Contents of a Zemax .zmx file for one lens.
|
|
51
|
+
|
|
52
|
+
Returns
|
|
53
|
+
-------
|
|
54
|
+
list of LensSurface
|
|
55
|
+
The lens surfaces, front to back, in the catalogue orientation.
|
|
56
|
+
"""
|
|
57
|
+
with tempfile.TemporaryDirectory() as temp_dir:
|
|
58
|
+
temp_path = Path(temp_dir) / "lens.zmx"
|
|
59
|
+
temp_path.write_text(zmx_text, encoding="utf-8")
|
|
60
|
+
with warnings.catch_warnings():
|
|
61
|
+
# Optiland warns about catalogue details it cannot use; none matter for a single lens.
|
|
62
|
+
warnings.simplefilter("ignore")
|
|
63
|
+
loaded_optic = load_zemax_file(str(temp_path))
|
|
64
|
+
surfaces = loaded_optic.surfaces.surfaces[1:-1]
|
|
65
|
+
|
|
66
|
+
lens_surfaces = []
|
|
67
|
+
for surface in surfaces:
|
|
68
|
+
material_name = getattr(surface.material_post, "name", None)
|
|
69
|
+
lens_surfaces.append(
|
|
70
|
+
LensSurface(
|
|
71
|
+
radius=float(surface.geometry.radius),
|
|
72
|
+
thickness_after=float(surface.thickness),
|
|
73
|
+
material_after=material_name if isinstance(material_name, str) else "air",
|
|
74
|
+
)
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
while (
|
|
78
|
+
lens_surfaces
|
|
79
|
+
and np.isinf(lens_surfaces[0].radius)
|
|
80
|
+
and lens_surfaces[0].material_after == "air"
|
|
81
|
+
):
|
|
82
|
+
lens_surfaces.pop(0)
|
|
83
|
+
return lens_surfaces
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def surface_sag(radius: float, height: np.ndarray) -> np.ndarray:
|
|
87
|
+
"""Sag (z displacement from the vertex, mm) of a spherical surface at the given heights.
|
|
88
|
+
|
|
89
|
+
A flat surface (infinite radius) has no sag. Heights beyond the sphere are clipped.
|
|
90
|
+
"""
|
|
91
|
+
if np.isinf(radius):
|
|
92
|
+
return np.zeros_like(height, dtype=float)
|
|
93
|
+
return radius - np.sign(radius) * np.sqrt(np.maximum(radius**2 - height**2, 0.0))
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def flipped_prescription(lens_surfaces: list[LensSurface]) -> list[LensSurface]:
|
|
97
|
+
"""Return the lens turned around: surface order reversed and radii negated.
|
|
98
|
+
|
|
99
|
+
The glass that followed surface k now precedes it, so each surface takes the thickness
|
|
100
|
+
and material that used to sit *before* it in the original order.
|
|
101
|
+
"""
|
|
102
|
+
surface_count = len(lens_surfaces)
|
|
103
|
+
flipped = []
|
|
104
|
+
for new_index in range(surface_count):
|
|
105
|
+
old_index = surface_count - 1 - new_index
|
|
106
|
+
previous_old_index = old_index - 1
|
|
107
|
+
if previous_old_index >= 0:
|
|
108
|
+
thickness = lens_surfaces[previous_old_index].thickness_after
|
|
109
|
+
material = lens_surfaces[previous_old_index].material_after
|
|
110
|
+
else:
|
|
111
|
+
thickness, material = 0.0, "air"
|
|
112
|
+
flipped.append(LensSurface(-lens_surfaces[old_index].radius, thickness, material))
|
|
113
|
+
return flipped
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def lens_thickness(lens_surfaces: list[LensSurface]) -> float:
|
|
117
|
+
"""Distance (mm) from the front vertex to the back vertex: the sum of the glass thicknesses."""
|
|
118
|
+
return sum(surface.thickness_after for surface in lens_surfaces[:-1])
|