1mod aber;
6mod basic;
7mod beam;
8mod illum;
9mod image;
10mod mtfs;
11
12pub use aber::{
14 CurvWave, DistKind, FanOut, FanRow, FieldCurvOut, GridOut, GridPoint, LatOut, LatWave, LongOut,
15 LongWave, SeidelOut, SeidelRow, SeidelWaves, ShiftOut,
16};
17pub use basic::{
18 GhostOut, GhostRow, RayCountsOut, SpotOut, SpotRow, WavefrontOut, WavefrontRow, ZKind,
19 ZernikeOut, ZernikeRow,
20};
21pub use beam::{GaussOut, GaussRow, Orientation as GaussOrientation};
22pub use illum::{mean_transmission, FootprintOut, FootprintRow, RelIllumOut, VignettingOut};
23pub use image::{
24 Plane as PsfPlane, PsfAxes, PsfOut, PsfPoint, PsfSampling, Reference as RmsReference, RmsOut,
25 RmsRow, Vs as RmsVs,
26};
27pub use mtfs::{FieldRow, FocusRow, GeoMtfOut, MtfFieldOut, MtfFocusOut, MtfOut};
28
29use lensbench_core::{Error, Result, System};
30use schemars::JsonSchema;
31use serde::{Deserialize, Serialize};
32
33use crate::registry::Analyses;
34
35pub fn register(a: &mut Analyses) {
37 a.add(basic::Spot);
38 a.add(basic::Wavefront);
39 a.add(basic::Zernike);
40 a.add(basic::Ghost);
41 a.add(mtfs::Mtf);
42 a.add(mtfs::MtfFocus);
43 a.add(mtfs::MtfField);
44 a.add(mtfs::GeoMtf);
45 a.add(mtfs::HuygensMtf);
46 a.add(image::Rms);
47 a.add(image::Psf);
48 a.add(aber::RayFan);
49 a.add(aber::OpdFan);
50 a.add(aber::PupilFan);
51 a.add(aber::FieldCurv);
52 a.add(aber::Longitudinal);
53 a.add(aber::LateralColor);
54 a.add(aber::FocalShift);
55 a.add(aber::GridDistortion);
56 a.add(aber::SeidelCoef);
57 a.add(illum::Footprint);
58 a.add(illum::RelIllum);
59 a.add(illum::Vignetting);
60 a.add(beam::Gauss);
61}
62
63pub fn fields(s: &System, field: Option<usize>) -> Result<Vec<usize>> {
65 match field {
66 None => Ok((0..s.fields.len()).collect()),
67 Some(i) if i >= 1 && i <= s.fields.len() => Ok(vec![i - 1]),
68 Some(i) => Err(Error::Invalid(format!(
69 "no field {i} (1 to {})",
70 s.fields.len()
71 ))),
72 }
73}
74
75pub fn field_vignetted(e: &Error) -> bool {
77 use lensbench_core::error::code;
78 matches!(e.code(), code::ALL_VIGNETTED | code::PUPIL_VIGNETTED)
79}
80
81pub(super) fn skip_vignetted<T>(
85 rows: Vec<(usize, Result<T>)>,
86 warn: &mut Vec<String>,
87) -> Result<Vec<T>> {
88 let (mut out, mut first) = (Vec::new(), None);
89 for (i, r) in rows {
90 match r {
91 Ok(x) => out.push(x),
92 Err(e) if field_vignetted(&e) => {
93 warn.push(format!("field {}: {e}", i + 1));
94 first.get_or_insert(e);
95 }
96 Err(e) => return Err(e),
97 }
98 }
99 match first {
100 Some(e) if out.is_empty() => Err(e),
101 _ => Ok(out),
102 }
103}
104
105fn pol_amp(
108 s: &System,
109 i: usize,
110 w: Option<usize>,
111 on: bool,
112) -> Result<Option<crate::pupil_amp::PolAmp>> {
113 if !on {
114 return Ok(None);
115 }
116 let co = lensbench_core::coating::Coatings::for_system(s)?;
117 let ws: Vec<usize> = w.map_or_else(|| (0..s.wavelengths.len()).collect(), |w| vec![w]);
118 let f = lensbench_core::FieldSel::Index(i);
119 crate::pupil_amp::PolAmp::new(
120 s,
121 f,
122 &ws,
123 &co,
124 crate::pupil_amp::POL_GRID,
125 s.input_pol.jones(),
126 )
127 .map(Some)
128}
129
130fn coat_notes(s: &System, on: bool) -> Result<Vec<String>> {
133 if !on {
134 return Ok(Vec::new());
135 }
136 Ok(lensbench_core::coating::Coatings::for_system(s)?.notes)
137}
138
139pub fn wave(s: &System, w: Option<usize>) -> Result<Option<usize>> {
140 lensbench_core::waves::index_opt(w, s.wavelengths.len())
141}
142
143fn spot_unit(s: &System) -> String {
145 s.image_unit().to_string()
146}
147
148fn freq_unit(s: &System) -> String {
150 format!("cycles/{}", s.image_unit())
151}
152
153fn auto_freqs(top: f64, steps: usize) -> Result<Vec<f64>> {
155 if !(top.is_finite() && top > 0.0) {
156 return Err(Error::Invalid(format!("max_freq must be positive ({top})")));
157 }
158 if steps == 0 {
159 return Err(Error::Invalid("freq_steps must be 1 or more".into()));
160 }
161 Ok((0..=steps).map(|k| top * k as f64 / steps as f64).collect())
162}
163
164fn freqs_for(
167 s: &System,
168 field: usize,
169 w: Option<usize>,
170 freqs: &Option<Vec<f64>>,
171 max_freq: Option<f64>,
172 freq_steps: usize,
173 axes: MtfDirs,
174) -> Result<Vec<f64>> {
175 if let Some(f) = freqs {
176 if max_freq.is_some() {
177 return Err(Error::Invalid(
178 "freqs and max_freq can't be used together".into(),
179 ));
180 }
181 return Ok(f.clone());
182 }
183 let top = match max_freq {
184 Some(x) => x,
185 None => {
186 let f = lensbench_core::FieldSel::Index(field);
187 let w = w.unwrap_or(s.primary);
188 0.95 * crate::analysis::mtf_cutoff_axes(s, f, w, axes.core())?.min()
189 }
190 };
191 auto_freqs(top, freq_steps)
192}
193
194#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize, JsonSchema)]
196#[serde(rename_all = "lowercase")]
197pub enum MtfDirs {
198 #[default]
200 Field,
201 Pupil,
203}
204
205impl MtfDirs {
206 pub(super) fn core(self) -> crate::analysis::MtfAxes {
207 match self {
208 MtfDirs::Field => crate::analysis::MtfAxes::Field,
209 MtfDirs::Pupil => crate::analysis::MtfAxes::Pupil,
210 }
211 }
212}
213
214#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize, JsonSchema)]
216#[serde(rename_all = "lowercase")]
217pub enum Axis {
218 #[default]
220 Y,
221 X,
223}
224
225impl Axis {
226 fn core(self) -> crate::sweep::FieldAxis {
227 match self {
228 Axis::X => crate::sweep::FieldAxis::X,
229 Axis::Y => crate::sweep::FieldAxis::Y,
230 }
231 }
232}
233
234#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
236pub struct MtfRow {
237 pub field: usize,
239 #[serde(deserialize_with = "crate::nan::v")]
240 pub freqs: Vec<f64>,
241 #[serde(deserialize_with = "crate::nan::v")]
243 pub tangential: Vec<f64>,
244 #[serde(deserialize_with = "crate::nan::v")]
246 pub sagittal: Vec<f64>,
247 pub cutoff: Cutoff,
249 #[serde(skip_serializing_if = "Option::is_none")]
251 pub waves: Option<Vec<WaveMtf>>,
252}
253
254#[derive(Debug, Clone, Copy, Serialize, Deserialize, JsonSchema)]
256pub struct Cutoff {
257 #[serde(deserialize_with = "crate::nan::f")]
258 pub tangential: f64,
259 #[serde(deserialize_with = "crate::nan::f")]
260 pub sagittal: f64,
261}
262
263#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
265pub struct WaveMtf {
266 pub wave: usize,
268 #[serde(deserialize_with = "crate::nan::v")]
269 pub tangential: Vec<f64>,
270 #[serde(deserialize_with = "crate::nan::v")]
271 pub sagittal: Vec<f64>,
272}
273
274impl MtfRow {
275 fn of(field: usize, m: crate::analysis::Mtf) -> MtfRow {
276 MtfRow {
277 field: field + 1,
278 freqs: m.freqs,
279 tangential: m.tan,
280 sagittal: m.sag,
281 cutoff: Cutoff {
282 tangential: m.cutoff.tan,
283 sagittal: m.cutoff.sag,
284 },
285 waves: None,
286 }
287 }
288}
289
290#[cfg(test)]
291mod tests;