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
|
@@ -0,0 +1,483 @@
|
|
|
1
|
+
"""The RayCrafter main window: side view on top, controls and analysis plots below.
|
|
2
|
+
|
|
3
|
+
- Lenses are chosen in the drop-down and added with "Add lens"; each goes to the right of
|
|
4
|
+
the existing lenses.
|
|
5
|
+
- Hover a lens and it turns a darker shade; that is the lens the actions apply to. Drag a
|
|
6
|
+
lens to slide it along the axis; drag it past a neighbour's centre to swap them. Drag the
|
|
7
|
+
green line to move the image plane (not while "Lock focus" is on).
|
|
8
|
+
- Right-click a lens for Flip (F), Delete (Del or Backspace) and Make / Split Plossl.
|
|
9
|
+
|
|
10
|
+
Updates come in two speeds. The side view, spot, through-focus plot and status bar are
|
|
11
|
+
recomputed on every change (coalesced, so a burst of drag events costs one recompute). The
|
|
12
|
+
MTF, ray fan and field curvature are slower, so they are recomputed only once changes have
|
|
13
|
+
stopped for SLOW_PLOT_DELAY_MS, and only for the tab that is showing.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
from __future__ import annotations
|
|
17
|
+
|
|
18
|
+
import time
|
|
19
|
+
from collections.abc import Callable
|
|
20
|
+
|
|
21
|
+
import pyqtgraph as pg
|
|
22
|
+
from PyQt6.QtCore import QPoint, Qt, QTimer
|
|
23
|
+
from PyQt6.QtGui import QKeySequence, QShortcut
|
|
24
|
+
from PyQt6.QtWidgets import (
|
|
25
|
+
QCheckBox,
|
|
26
|
+
QComboBox,
|
|
27
|
+
QFileDialog,
|
|
28
|
+
QFrame,
|
|
29
|
+
QGridLayout,
|
|
30
|
+
QHBoxLayout,
|
|
31
|
+
QLabel,
|
|
32
|
+
QListView,
|
|
33
|
+
QMainWindow,
|
|
34
|
+
QMenu,
|
|
35
|
+
QMessageBox,
|
|
36
|
+
QPushButton,
|
|
37
|
+
QSizePolicy,
|
|
38
|
+
QSlider,
|
|
39
|
+
QVBoxLayout,
|
|
40
|
+
QWidget,
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
from raycrafter import palette
|
|
44
|
+
from raycrafter.core.analysis import FastTrace, airy_radius
|
|
45
|
+
from raycrafter.core.model import OpticalModel
|
|
46
|
+
from raycrafter.gui.analysis_panels import (
|
|
47
|
+
FieldCurvaturePanel,
|
|
48
|
+
MtfPanel,
|
|
49
|
+
RayFanPanel,
|
|
50
|
+
SpotPanel,
|
|
51
|
+
ThroughFocusPanel,
|
|
52
|
+
make_tabs,
|
|
53
|
+
)
|
|
54
|
+
from raycrafter.gui.side_view import IMAGE_MAX_Z, MIN_IMAGE_GAP_MM, SideView
|
|
55
|
+
from raycrafter.gui.style import style_sheet
|
|
56
|
+
from raycrafter.report import write_and_open_report
|
|
57
|
+
|
|
58
|
+
SLOW_PLOT_DELAY_MS = 120 # MTF, ray fan and field curvature wait this long after the last change
|
|
59
|
+
VERGENCE_LIMIT_DIOPTRES = 1.0 # the vergence slider spans +/- this (as for an f = 1000 mm lens)
|
|
60
|
+
DEFAULT_BEAM_DIAMETER_MM = 10 # what "Reset beam" restores
|
|
61
|
+
BEAM_DIAMETER_RANGE_MM = (1, 40)
|
|
62
|
+
BEAM_ANGLE_LIMIT_TENTHS = 100 # the angle slider works in tenths of a degree: +/-10 deg
|
|
63
|
+
CONTROL_PANEL_WIDTH_PX = 300
|
|
64
|
+
HINT_MIN_HEIGHT_PX = 52 # height reserved for the help text at the foot of the control panel
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
class MainWindow(QMainWindow):
|
|
68
|
+
"""Side view, analysis plots and controls in one window.
|
|
69
|
+
|
|
70
|
+
Parameters
|
|
71
|
+
----------
|
|
72
|
+
model : OpticalModel, optional
|
|
73
|
+
The bench to show. A new one (with one default lens) is made if not given.
|
|
74
|
+
"""
|
|
75
|
+
|
|
76
|
+
def __init__(self, model: OpticalModel | None = None) -> None:
|
|
77
|
+
super().__init__()
|
|
78
|
+
self.setWindowTitle("RayCrafter")
|
|
79
|
+
self.resize(1450, 900)
|
|
80
|
+
self.setStyleSheet(style_sheet())
|
|
81
|
+
pg.setConfigOptions(background="w", foreground=palette.TEXT, antialias=True)
|
|
82
|
+
|
|
83
|
+
self.model = model if model is not None else OpticalModel()
|
|
84
|
+
self._build_widgets()
|
|
85
|
+
self._connect_signals()
|
|
86
|
+
|
|
87
|
+
# Updates are coalesced: many drag events trigger one recompute per event-loop pass.
|
|
88
|
+
self.update_timer = QTimer(self)
|
|
89
|
+
self.update_timer.setSingleShot(True)
|
|
90
|
+
self.update_timer.timeout.connect(self.recompute_and_redraw)
|
|
91
|
+
|
|
92
|
+
# The slower plots are redrawn only after changes have stopped for a moment.
|
|
93
|
+
self.slow_timer = QTimer(self)
|
|
94
|
+
self.slow_timer.setSingleShot(True)
|
|
95
|
+
self.slow_timer.timeout.connect(self.update_slow_plots)
|
|
96
|
+
|
|
97
|
+
self.model.autofocus()
|
|
98
|
+
self.populate_catalog_combo()
|
|
99
|
+
self.side_view.rebuild_lens_graphics()
|
|
100
|
+
self.recompute_and_redraw()
|
|
101
|
+
|
|
102
|
+
# ------------------------------------------------------------------ setup
|
|
103
|
+
|
|
104
|
+
def _build_widgets(self) -> None:
|
|
105
|
+
"""Create the side view, the analysis plots and the control panel, and lay them out."""
|
|
106
|
+
self.side_view = SideView(self.model)
|
|
107
|
+
self.spot_panel = SpotPanel()
|
|
108
|
+
self.through_focus_panel = ThroughFocusPanel()
|
|
109
|
+
self.mtf_panel = MtfPanel()
|
|
110
|
+
self.ray_fan_panel = RayFanPanel()
|
|
111
|
+
self.field_curvature_panel = FieldCurvaturePanel()
|
|
112
|
+
|
|
113
|
+
# Three tab widgets of equal width. Field curvature gets a one-tab widget so that all
|
|
114
|
+
# three plots line up.
|
|
115
|
+
self.spot_tabs = make_tabs(
|
|
116
|
+
[("Spot", self.spot_panel), ("Through-focus", self.through_focus_panel)]
|
|
117
|
+
)
|
|
118
|
+
self.mtf_tabs = make_tabs([("MTF", self.mtf_panel), ("Ray fan", self.ray_fan_panel)])
|
|
119
|
+
self.field_tabs = make_tabs([("Field curvature", self.field_curvature_panel)])
|
|
120
|
+
|
|
121
|
+
bottom_row = QHBoxLayout()
|
|
122
|
+
bottom_row.addWidget(self._build_control_panel())
|
|
123
|
+
for tab_widget in (self.spot_tabs, self.mtf_tabs, self.field_tabs):
|
|
124
|
+
bottom_row.addWidget(tab_widget, stretch=1)
|
|
125
|
+
|
|
126
|
+
central = QWidget()
|
|
127
|
+
layout = QVBoxLayout(central)
|
|
128
|
+
# Plot widgets report a large preferred height, which skewed the 2:3 split between the
|
|
129
|
+
# side view and the bottom row; ignoring it lets the stretch factors alone decide.
|
|
130
|
+
for plot_area in (self.side_view, self.spot_tabs, self.mtf_tabs, self.field_tabs):
|
|
131
|
+
plot_area.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Ignored)
|
|
132
|
+
layout.addWidget(self.side_view, stretch=2)
|
|
133
|
+
layout.addLayout(bottom_row, stretch=3)
|
|
134
|
+
self.setCentralWidget(central)
|
|
135
|
+
|
|
136
|
+
self.status_label = QLabel()
|
|
137
|
+
self.statusBar().addWidget(self.status_label)
|
|
138
|
+
|
|
139
|
+
def _build_control_panel(self) -> QFrame:
|
|
140
|
+
"""The framed panel of buttons, sliders and the lens drop-down."""
|
|
141
|
+
state = self.model.state
|
|
142
|
+
self.catalog_combo = QComboBox()
|
|
143
|
+
# A plain QListView popup: the default one is drawn by the native macOS style, which
|
|
144
|
+
# ignores the style sheet's hover and selection colours (white on white).
|
|
145
|
+
self.catalog_combo.setView(QListView())
|
|
146
|
+
self.add_button = QPushButton("Add lens")
|
|
147
|
+
self.autofocus_button = QPushButton("Autofocus")
|
|
148
|
+
self.reset_beam_button = QPushButton("Reset beam")
|
|
149
|
+
self.save_zmx_button = QPushButton("Save ZMX")
|
|
150
|
+
self.report_button = QPushButton("Generate report")
|
|
151
|
+
self.lock_focus_checkbox = QCheckBox("Lock focus") # off by default
|
|
152
|
+
self.lock_focus_checkbox.setToolTip(
|
|
153
|
+
"Keep the image plane at best focus as lenses or the beam change, "
|
|
154
|
+
"whenever that focus is in view"
|
|
155
|
+
)
|
|
156
|
+
|
|
157
|
+
self.diameter_slider, self.diameter_label = self._make_slider(
|
|
158
|
+
*BEAM_DIAMETER_RANGE_MM, value=int(state.beam_diameter)
|
|
159
|
+
)
|
|
160
|
+
self.angle_slider, self.angle_label = self._make_slider(
|
|
161
|
+
-BEAM_ANGLE_LIMIT_TENTHS, BEAM_ANGLE_LIMIT_TENTHS, value=int(state.field_angle * 10)
|
|
162
|
+
)
|
|
163
|
+
vergence_limit_hundredths = round(VERGENCE_LIMIT_DIOPTRES * 100)
|
|
164
|
+
self.vergence_slider, self.vergence_label = self._make_slider(
|
|
165
|
+
-vergence_limit_hundredths,
|
|
166
|
+
vergence_limit_hundredths,
|
|
167
|
+
value=round(state.beam_vergence * 100),
|
|
168
|
+
)
|
|
169
|
+
|
|
170
|
+
# Reminder of where the per-lens actions are, since they are not buttons.
|
|
171
|
+
hint_label = QLabel(
|
|
172
|
+
"Right-click a lens to flip it (F), delete it (Del) or make a Plossl pair. "
|
|
173
|
+
"Drag a lens past its neighbour to swap their order."
|
|
174
|
+
)
|
|
175
|
+
hint_label.setObjectName("hintLabel")
|
|
176
|
+
hint_label.setWordWrap(True)
|
|
177
|
+
# A wrapped label reports a large height-for-width size hint, which swelled the bottom
|
|
178
|
+
# row and squeezed the side view to a sliver. Ignoring its vertical hint keeps the
|
|
179
|
+
# panel's size set by the controls; the label just uses the spare space at the bottom.
|
|
180
|
+
hint_label.setSizePolicy(QSizePolicy.Policy.Preferred, QSizePolicy.Policy.Ignored)
|
|
181
|
+
hint_label.setMinimumHeight(HINT_MIN_HEIGHT_PX)
|
|
182
|
+
hint_label.setAlignment(Qt.AlignmentFlag.AlignBottom | Qt.AlignmentFlag.AlignLeft)
|
|
183
|
+
|
|
184
|
+
controls = QGridLayout()
|
|
185
|
+
controls.setSpacing(8)
|
|
186
|
+
controls.addWidget(self.catalog_combo, 0, 0, 1, 2)
|
|
187
|
+
controls.addWidget(self.add_button, 1, 0)
|
|
188
|
+
controls.addWidget(self.autofocus_button, 1, 1)
|
|
189
|
+
controls.addWidget(self.diameter_label, 2, 0, 1, 2)
|
|
190
|
+
controls.addWidget(self.diameter_slider, 3, 0, 1, 2)
|
|
191
|
+
controls.addWidget(self.angle_label, 4, 0, 1, 2)
|
|
192
|
+
controls.addWidget(self.angle_slider, 5, 0, 1, 2)
|
|
193
|
+
controls.addWidget(self.vergence_label, 6, 0, 1, 2)
|
|
194
|
+
controls.addWidget(self.vergence_slider, 7, 0, 1, 2)
|
|
195
|
+
controls.addWidget(self.reset_beam_button, 8, 0)
|
|
196
|
+
controls.addWidget(self.save_zmx_button, 8, 1)
|
|
197
|
+
controls.addWidget(self.report_button, 9, 0, 1, 2)
|
|
198
|
+
controls.addWidget(self.lock_focus_checkbox, 10, 0, 1, 2)
|
|
199
|
+
controls.setRowStretch(11, 1) # spare space goes above the hint, which sits at the foot
|
|
200
|
+
controls.addWidget(hint_label, 12, 0, 1, 2, Qt.AlignmentFlag.AlignBottom)
|
|
201
|
+
controls.setContentsMargins(14, 14, 14, 14)
|
|
202
|
+
|
|
203
|
+
# A framed card (QFrame#controls in the style sheet), so the controls read as one panel.
|
|
204
|
+
control_panel = QFrame()
|
|
205
|
+
control_panel.setObjectName("controls")
|
|
206
|
+
control_panel.setLayout(controls)
|
|
207
|
+
control_panel.setFixedWidth(CONTROL_PANEL_WIDTH_PX)
|
|
208
|
+
return control_panel
|
|
209
|
+
|
|
210
|
+
@staticmethod
|
|
211
|
+
def _make_slider(minimum: int, maximum: int, value: int) -> tuple[QSlider, QLabel]:
|
|
212
|
+
"""A horizontal slider and the label shown above it."""
|
|
213
|
+
slider = QSlider(Qt.Orientation.Horizontal)
|
|
214
|
+
slider.setRange(minimum, maximum)
|
|
215
|
+
slider.setValue(value)
|
|
216
|
+
label = QLabel()
|
|
217
|
+
label.setObjectName("sliderLabel")
|
|
218
|
+
return slider, label
|
|
219
|
+
|
|
220
|
+
def _connect_signals(self) -> None:
|
|
221
|
+
"""Wire the buttons, sliders, side view and keyboard shortcuts to their handlers."""
|
|
222
|
+
self.add_button.clicked.connect(self.add_selected_catalog_lens)
|
|
223
|
+
self.autofocus_button.clicked.connect(self.run_autofocus)
|
|
224
|
+
self.diameter_slider.valueChanged.connect(self.set_beam_diameter)
|
|
225
|
+
self.angle_slider.valueChanged.connect(self.set_field_angle)
|
|
226
|
+
self.vergence_slider.valueChanged.connect(self.set_beam_vergence)
|
|
227
|
+
self.reset_beam_button.clicked.connect(self.reset_beam)
|
|
228
|
+
self.save_zmx_button.clicked.connect(self.save_zmx)
|
|
229
|
+
self.report_button.clicked.connect(self.generate_report)
|
|
230
|
+
self.lock_focus_checkbox.toggled.connect(self.on_lock_focus_toggled)
|
|
231
|
+
|
|
232
|
+
self.side_view.changed.connect(self.request_update)
|
|
233
|
+
self.side_view.lens_menu_requested.connect(self.show_lens_menu)
|
|
234
|
+
|
|
235
|
+
# F and Delete/Backspace act on the lens under the pointer.
|
|
236
|
+
QShortcut(
|
|
237
|
+
QKeySequence("F"), self, activated=lambda: self.act_on_hovered_lens(self.flip_lens)
|
|
238
|
+
)
|
|
239
|
+
for delete_key in ("Delete", "Backspace"): # Backspace is "delete" on Mac keyboards
|
|
240
|
+
QShortcut(
|
|
241
|
+
QKeySequence(delete_key),
|
|
242
|
+
self,
|
|
243
|
+
activated=lambda: self.act_on_hovered_lens(self.delete_lens),
|
|
244
|
+
)
|
|
245
|
+
|
|
246
|
+
# Only the visible tab of each pair is computed, so switching tab must trigger a redraw.
|
|
247
|
+
self.mtf_tabs.currentChanged.connect(lambda _index: self.update_mtf_tab())
|
|
248
|
+
|
|
249
|
+
# --------------------------------------------------------------- controls
|
|
250
|
+
|
|
251
|
+
def request_update(self) -> None:
|
|
252
|
+
"""Schedule one recompute; repeated requests before it runs collapse into one."""
|
|
253
|
+
if not self.update_timer.isActive():
|
|
254
|
+
self.update_timer.start(0)
|
|
255
|
+
|
|
256
|
+
def populate_catalog_combo(self) -> None:
|
|
257
|
+
"""Fill the drop-down from the model's catalogue (the part number is the item data)."""
|
|
258
|
+
self.catalog_combo.clear()
|
|
259
|
+
for entry in self.model.catalog.values():
|
|
260
|
+
self.catalog_combo.addItem(entry.label, entry.part_number)
|
|
261
|
+
|
|
262
|
+
def add_selected_catalog_lens(self) -> None:
|
|
263
|
+
"""Place the lens chosen in the drop-down to the right of the existing lenses."""
|
|
264
|
+
part_number = self.catalog_combo.currentData()
|
|
265
|
+
if part_number is None:
|
|
266
|
+
return
|
|
267
|
+
self.model.add_lens(part_number)
|
|
268
|
+
self.side_view.rebuild_lens_graphics()
|
|
269
|
+
self.request_update()
|
|
270
|
+
|
|
271
|
+
def act_on_hovered_lens(self, action: Callable[[int], None]) -> None:
|
|
272
|
+
"""Keyboard shortcuts: run ``action(index)`` for the lens under the pointer, if any."""
|
|
273
|
+
if self.side_view.hovered_index is not None:
|
|
274
|
+
action(self.side_view.hovered_index)
|
|
275
|
+
|
|
276
|
+
def delete_lens(self, lens_index: int) -> None:
|
|
277
|
+
"""Remove one lens."""
|
|
278
|
+
self.model.delete_lens(lens_index)
|
|
279
|
+
self.side_view.rebuild_lens_graphics()
|
|
280
|
+
self.request_update()
|
|
281
|
+
|
|
282
|
+
def flip_lens(self, lens_index: int) -> None:
|
|
283
|
+
"""Turn one lens around (not available inside a Plossl pair)."""
|
|
284
|
+
self.model.flip_lens(lens_index)
|
|
285
|
+
self.request_update()
|
|
286
|
+
|
|
287
|
+
def make_plossl(self, lens_index: int) -> None:
|
|
288
|
+
"""Snap a lens and its identical neighbour into a Plossl pair, then refocus."""
|
|
289
|
+
if self.model.plossl_partner(lens_index) is not None:
|
|
290
|
+
self.model.make_plossl(lens_index)
|
|
291
|
+
self.model.autofocus() # the pair has a new focal length, so the old focus is stale
|
|
292
|
+
self.request_update()
|
|
293
|
+
|
|
294
|
+
def split_plossl(self, lens_index: int) -> None:
|
|
295
|
+
"""Release a lens's Plossl pair."""
|
|
296
|
+
self.model.split_plossl(lens_index)
|
|
297
|
+
self.request_update()
|
|
298
|
+
|
|
299
|
+
def show_lens_menu(self, lens_index: int, global_position: QPoint) -> None:
|
|
300
|
+
"""Pop up the actions for one lens at the pointer."""
|
|
301
|
+
self.build_lens_menu(lens_index).exec(global_position)
|
|
302
|
+
|
|
303
|
+
def build_lens_menu(self, lens_index: int) -> QMenu:
|
|
304
|
+
"""The context menu for one lens. Entries that do not apply are greyed out.
|
|
305
|
+
|
|
306
|
+
A tab in an entry's text puts the keyboard shortcut in a right-hand column.
|
|
307
|
+
"""
|
|
308
|
+
in_pair = self.model.state.lenses[lens_index].group_id is not None
|
|
309
|
+
menu = QMenu(self)
|
|
310
|
+
|
|
311
|
+
flip_action = menu.addAction("Flip\tF")
|
|
312
|
+
flip_action.setEnabled(not in_pair)
|
|
313
|
+
flip_action.triggered.connect(lambda: self.flip_lens(lens_index))
|
|
314
|
+
|
|
315
|
+
delete_action = menu.addAction("Delete\tDel")
|
|
316
|
+
delete_action.triggered.connect(lambda: self.delete_lens(lens_index))
|
|
317
|
+
|
|
318
|
+
menu.addSeparator()
|
|
319
|
+
if in_pair:
|
|
320
|
+
split_action = menu.addAction("Split Plossl")
|
|
321
|
+
split_action.triggered.connect(lambda: self.split_plossl(lens_index))
|
|
322
|
+
else:
|
|
323
|
+
plossl_action = menu.addAction("Make Plossl")
|
|
324
|
+
plossl_action.setEnabled(self.model.plossl_partner(lens_index) is not None)
|
|
325
|
+
plossl_action.triggered.connect(lambda: self.make_plossl(lens_index))
|
|
326
|
+
|
|
327
|
+
# While "Make Plossl" is highlighted, outline the two lenses that would be paired.
|
|
328
|
+
def preview_if_plossl(hovered_action) -> None:
|
|
329
|
+
if hovered_action is plossl_action and plossl_action.isEnabled():
|
|
330
|
+
self.side_view.show_plossl_preview(lens_index)
|
|
331
|
+
else:
|
|
332
|
+
self.side_view.hide_plossl_preview()
|
|
333
|
+
|
|
334
|
+
menu.hovered.connect(preview_if_plossl)
|
|
335
|
+
menu.aboutToHide.connect(self.side_view.hide_plossl_preview)
|
|
336
|
+
return menu
|
|
337
|
+
|
|
338
|
+
def run_autofocus(self) -> None:
|
|
339
|
+
"""Move the image plane to the minimum-RMS-spot position."""
|
|
340
|
+
self.model.autofocus()
|
|
341
|
+
self.request_update()
|
|
342
|
+
|
|
343
|
+
def set_beam_diameter(self, value_mm: int) -> None:
|
|
344
|
+
"""Slider callback: beam diameter in mm."""
|
|
345
|
+
self.model.state.beam_diameter = float(value_mm)
|
|
346
|
+
self.request_update()
|
|
347
|
+
|
|
348
|
+
def set_field_angle(self, value_tenths: int) -> None:
|
|
349
|
+
"""Slider callback: beam angle in tenths of a degree."""
|
|
350
|
+
self.model.state.field_angle = value_tenths / 10.0
|
|
351
|
+
self.request_update()
|
|
352
|
+
|
|
353
|
+
def set_beam_vergence(self, value_hundredths: int) -> None:
|
|
354
|
+
"""Slider callback: beam vergence in hundredths of a dioptre (positive = converging)."""
|
|
355
|
+
self.model.state.beam_vergence = value_hundredths / 100.0
|
|
356
|
+
self.request_update()
|
|
357
|
+
|
|
358
|
+
def reset_beam(self) -> None:
|
|
359
|
+
"""Put the beam back to collimated, on axis, at the default diameter."""
|
|
360
|
+
# Moving a slider fires its callback, which updates the model and schedules the redraw.
|
|
361
|
+
self.vergence_slider.setValue(0)
|
|
362
|
+
self.angle_slider.setValue(0)
|
|
363
|
+
self.diameter_slider.setValue(DEFAULT_BEAM_DIAMETER_MM)
|
|
364
|
+
|
|
365
|
+
def save_zmx(self) -> None:
|
|
366
|
+
"""Ask the user where to save, then export the current system as a Zemax .zmx file."""
|
|
367
|
+
file_path, _selected_filter = QFileDialog.getSaveFileName(
|
|
368
|
+
self, "Save Zemax file", "lens_system.zmx", "Zemax files (*.zmx)"
|
|
369
|
+
)
|
|
370
|
+
if not file_path:
|
|
371
|
+
return # dialog cancelled
|
|
372
|
+
if not file_path.lower().endswith(".zmx"):
|
|
373
|
+
file_path += ".zmx"
|
|
374
|
+
try:
|
|
375
|
+
self.model.save_zmx(file_path)
|
|
376
|
+
except Exception as error: # show any export problem instead of crashing the GUI
|
|
377
|
+
QMessageBox.critical(self, "Save failed", f"Could not save {file_path}:\n{error}")
|
|
378
|
+
return
|
|
379
|
+
self.statusBar().showMessage(f"Saved {file_path}", 5000)
|
|
380
|
+
|
|
381
|
+
def generate_report(self) -> None:
|
|
382
|
+
"""Write an HTML report of the current system and open it in the web browser."""
|
|
383
|
+
if not self.model.state.lenses:
|
|
384
|
+
self.statusBar().showMessage("Add a lens before generating a report", 5000)
|
|
385
|
+
return
|
|
386
|
+
try:
|
|
387
|
+
report_path = write_and_open_report(self.model)
|
|
388
|
+
except Exception as error: # show any problem instead of crashing the GUI
|
|
389
|
+
QMessageBox.critical(self, "Report failed", f"Could not generate the report:\n{error}")
|
|
390
|
+
return
|
|
391
|
+
self.statusBar().showMessage(f"Report written to {report_path}", 8000)
|
|
392
|
+
|
|
393
|
+
def on_lock_focus_toggled(self, locked: bool) -> None:
|
|
394
|
+
"""Lock focus on: the image plane follows best focus and cannot be dragged."""
|
|
395
|
+
self.side_view.set_image_line_locked(locked)
|
|
396
|
+
self.request_update()
|
|
397
|
+
|
|
398
|
+
def refocus_if_focus_in_view(self) -> None:
|
|
399
|
+
"""With "Lock focus" on, move the image plane to best focus if that focus is visible.
|
|
400
|
+
|
|
401
|
+
If the best focus falls beyond the right edge of the side view, or the search can
|
|
402
|
+
only end against the back of the last lens (no real focus behind it, e.g. a negative
|
|
403
|
+
lens), the image plane stays where it was rather than jumping somewhere unhelpful.
|
|
404
|
+
"""
|
|
405
|
+
state = self.model.state
|
|
406
|
+
if not state.lenses:
|
|
407
|
+
return
|
|
408
|
+
|
|
409
|
+
previous_image_z = state.image_z
|
|
410
|
+
focus_z = self.model.autofocus()
|
|
411
|
+
focus_is_in_view = state.last_back_z() + MIN_IMAGE_GAP_MM <= focus_z <= IMAGE_MAX_Z
|
|
412
|
+
if not focus_is_in_view:
|
|
413
|
+
state.image_z = previous_image_z
|
|
414
|
+
|
|
415
|
+
# ------------------------------------------------------------------ redraw
|
|
416
|
+
|
|
417
|
+
def recompute_and_redraw(self) -> None:
|
|
418
|
+
"""Re-trace the system; refresh the side view, spot, through-focus and readouts."""
|
|
419
|
+
state = self.model.state
|
|
420
|
+
self.diameter_label.setText(f"Beam diameter: {state.beam_diameter:.0f} mm")
|
|
421
|
+
self.angle_label.setText(f"Beam angle: {state.field_angle:+.1f} deg")
|
|
422
|
+
self.vergence_label.setText(
|
|
423
|
+
f"Beam vergence: {state.beam_vergence:+.2f} D ({self.vergence_description()})"
|
|
424
|
+
)
|
|
425
|
+
|
|
426
|
+
start = time.perf_counter()
|
|
427
|
+
if self.lock_focus_checkbox.isChecked():
|
|
428
|
+
self.refocus_if_focus_in_view()
|
|
429
|
+
fast_trace = self.model.trace_fast()
|
|
430
|
+
through_focus = self.model.compute_through_focus(fast_trace)
|
|
431
|
+
trace_ms = (time.perf_counter() - start) * 1000
|
|
432
|
+
|
|
433
|
+
start = time.perf_counter()
|
|
434
|
+
self.side_view.update_view(fast_trace.paths)
|
|
435
|
+
self.spot_panel.show_result(fast_trace, state.field_angle)
|
|
436
|
+
self.through_focus_panel.show_result(through_focus)
|
|
437
|
+
draw_ms = (time.perf_counter() - start) * 1000
|
|
438
|
+
|
|
439
|
+
self.update_readouts(fast_trace)
|
|
440
|
+
self.side_view.set_timings(trace_ms, draw_ms)
|
|
441
|
+
self.slow_timer.start(SLOW_PLOT_DELAY_MS)
|
|
442
|
+
|
|
443
|
+
def update_readouts(self, fast_trace: FastTrace) -> None:
|
|
444
|
+
"""Status bar: image plane, RMS spot radius, first-order properties and Airy radius."""
|
|
445
|
+
state = self.model.state
|
|
446
|
+
readouts = [
|
|
447
|
+
f"image z={state.image_z:.1f} mm",
|
|
448
|
+
f"RMS={fast_trace.spot.rms_radius * 1000:.2f} um",
|
|
449
|
+
]
|
|
450
|
+
|
|
451
|
+
summary = fast_trace.summary
|
|
452
|
+
if summary is not None:
|
|
453
|
+
readouts += [
|
|
454
|
+
f"EFL={summary.effective_focal_length:.1f} mm",
|
|
455
|
+
f"BFL={summary.back_focal_length:.1f} mm",
|
|
456
|
+
f"f/{summary.f_number:.1f}",
|
|
457
|
+
]
|
|
458
|
+
airy = airy_radius(fast_trace.spot.numerical_aperture)
|
|
459
|
+
if airy is not None:
|
|
460
|
+
readouts.append(f"Airy radius={airy * 1000:.1f} um")
|
|
461
|
+
self.status_label.setText(" ".join(readouts))
|
|
462
|
+
|
|
463
|
+
def vergence_description(self) -> str:
|
|
464
|
+
"""Plain-words description of the beam vergence: collimated, converging or diverging."""
|
|
465
|
+
vergence = self.model.state.beam_vergence
|
|
466
|
+
if vergence == 0.0:
|
|
467
|
+
return "collimated"
|
|
468
|
+
return "converging" if vergence > 0 else "diverging"
|
|
469
|
+
|
|
470
|
+
def update_slow_plots(self) -> None:
|
|
471
|
+
"""Recompute the slower plots (run once changes have settled)."""
|
|
472
|
+
self.update_mtf_tab()
|
|
473
|
+
self.field_curvature_panel.show_result(
|
|
474
|
+
self.model.compute_field_curvature(), self.model.state.field_angle
|
|
475
|
+
)
|
|
476
|
+
|
|
477
|
+
def update_mtf_tab(self) -> None:
|
|
478
|
+
"""Compute whichever of the MTF and ray fan is showing; the other waits until selected."""
|
|
479
|
+
field_angle = self.model.state.field_angle
|
|
480
|
+
if self.mtf_tabs.currentWidget() is self.mtf_panel:
|
|
481
|
+
self.mtf_panel.show_result(self.model.compute_mtf(), field_angle)
|
|
482
|
+
else:
|
|
483
|
+
self.ray_fan_panel.show_result(self.model.compute_ray_fan(), field_angle)
|