Skip to main content

lensbench_optim/
dls_seidel.rs

1//! ザイデル収差係数の項([`TermKind::Seidel`])、実光線の歪曲の項([`TermKind::Distortion`])、
2//! 縦収差・軸上色収差の項([`TermKind::Longitudinal`])と倍率色収差の項([`TermKind::LateralColor`])
3
4use super::*;
5use lensbench_core::seidel::{
6    chief_height, distortion_against, distortion_max, distortion_point, distortion_ref,
7    ftheta_distortion, longitudinal, seidel, DistRef,
8};
9
10/// ザイデル収差係数(波面収差の係数 [波]、[`lensbench_core::seidel::Seidel::waves`])
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12pub enum SeidelQ {
13    /// 球面収差 W040 = S_I/8(SPHA)
14    Spha,
15    /// コマ W131 = S_II/2(COMA)
16    Coma,
17    /// 非点収差 W222 = S_III/2(ASTI)
18    Asti,
19    /// 像面湾曲 W220 = S_IV/4(FCUR)
20    Fcur,
21    /// 歪曲 W311 = S_V/2(DIST の面の寄与)
22    Dist,
23}
24
25impl SeidelQ {
26    pub const ALL: [SeidelQ; 5] = [
27        SeidelQ::Spha,
28        SeidelQ::Coma,
29        SeidelQ::Asti,
30        SeidelQ::Fcur,
31        SeidelQ::Dist,
32    ];
33    pub fn name(self) -> &'static str {
34        match self {
35            SeidelQ::Spha => "spha",
36            SeidelQ::Coma => "coma",
37            SeidelQ::Asti => "asti",
38            SeidelQ::Fcur => "fcur",
39            SeidelQ::Dist => "dist",
40        }
41    }
42    pub fn parse(s: &str) -> Result<SeidelQ> {
43        let t = s.trim().to_ascii_lowercase();
44        match SeidelQ::ALL.iter().find(|q| q.name() == t) {
45            Some(q) => Ok(*q),
46            None => invalid!(
47                "Seidel quantity {s} ({})",
48                SeidelQ::ALL.map(|q| q.name()).join(", ")
49            ),
50        }
51    }
52    fn index(self) -> usize {
53        SeidelQ::ALL.iter().position(|q| *q == self).unwrap_or(0)
54    }
55}
56
57pub(super) fn check(i: usize, k: &TermKind, s: &System) -> Result<()> {
58    let (wave, target) = match k {
59        TermKind::Seidel {
60            surf, wave, target, ..
61        } => {
62            if *surf > s.image() {
63                invalid!("term {i}: surface {surf} (0 to {})", s.image());
64            }
65            (wave, target)
66        }
67        TermKind::Distortion { wave, target, .. } => (wave, target),
68        TermKind::Longitudinal {
69            wave,
70            wave2,
71            zone,
72            target,
73        } => {
74            if !(0.0..=1.0).contains(zone) {
75                invalid!("term {i}: zone {zone} (0 to 1)");
76            }
77            if let Some(w) = wave2 {
78                if *w >= s.wavelengths.len() {
79                    invalid!("term {i}: wavelength {w} (of {})", s.wavelengths.len());
80                }
81            }
82            (wave, target)
83        }
84        TermKind::LateralColor {
85            wave,
86            wave2,
87            target,
88        } => {
89            if *wave2 >= s.wavelengths.len() {
90                invalid!("term {i}: wavelength {wave2} (of {})", s.wavelengths.len());
91            }
92            (wave, target)
93        }
94        _ => unreachable!("not a Seidel term"),
95    };
96    if !target.is_finite() {
97        invalid!("term {i}: target {target}");
98    }
99    if let Some(w) = wave {
100        if *w >= s.wavelengths.len() {
101            invalid!("term {i}: wavelength {w} (of {})", s.wavelengths.len());
102        }
103    }
104    value(s, k).map(|_| ())
105}
106
107fn value(s: &System, k: &TermKind) -> Result<f64> {
108    match k {
109        TermKind::Seidel { surf, q, wave, .. } => {
110            let w = wave.unwrap_or(s.primary);
111            Ok(seidel(s, w)?.waves(*surf)[q.index()])
112        }
113        TermKind::Distortion {
114            wave,
115            absolute,
116            field,
117            reference,
118            mode,
119            abcd,
120            component,
121            ..
122        } => {
123            let w = wave.unwrap_or(s.primary);
124            let norm = |f: &Option<FieldSel>, d: (f64, f64)| -> Result<(f64, f64)> {
125                match f {
126                    Some(f) => s.field_sel_norm(*f, w),
127                    None => Ok(d),
128                }
129            };
130            let h = norm(field, (0.0, 1.0))?;
131            // 軸上の視野の基準は近軸主光線の倍率(数値の微分より正確)
132            let r = match reference {
133                Some(r) => Some(norm(&Some(*r), (0.0, 0.0))?).filter(|&h| h != (0.0, 0.0)),
134                None => None,
135            };
136            let base = || -> Result<DistRef> {
137                match abcd {
138                    Some([a, b, c, d]) => {
139                        let mut x = distortion_point(s, w, r)?;
140                        x.j = [[*a, *b], [*c, *d]];
141                        Ok(x)
142                    }
143                    None => distortion_ref(s, w, r),
144                }
145            };
146            let (d, pct) = match mode {
147                DistMode::At => distortion_against(s, w, h, &base()?, *component)?,
148                DistMode::Max => {
149                    let m = distortion_max(s, w, h, *absolute)?;
150                    (m, m)
151                }
152                DistMode::FTheta => ftheta_distortion(s, w)?,
153                DistMode::Coef(k) => {
154                    let j = distortion_ref(s, w, r)?.j;
155                    let v = j[k / 2][k % 2];
156                    (v, v)
157                }
158            };
159            Ok(if *absolute { d } else { pct })
160        }
161        TermKind::Longitudinal {
162            wave, wave2, zone, ..
163        } => {
164            let w = wave.unwrap_or(s.primary);
165            let a = longitudinal(s, w, *zone)?;
166            Ok(match wave2 {
167                Some(w2) => longitudinal(s, *w2, *zone)? - a,
168                None => a,
169            })
170        }
171        TermKind::LateralColor { wave, wave2, .. } => {
172            let w = wave.unwrap_or(s.primary);
173            let v = chief_height(s, *wave2) - chief_height(s, w);
174            if !v.is_finite() {
175                invalid!("the lateral color is not finite");
176            }
177            Ok(v)
178        }
179        _ => unreachable!("not a Seidel term"),
180    }
181}
182
183pub(super) fn term(s: &System, k: &TermKind) -> Result<TermOut> {
184    let t = k.target().unwrap_or(0.0);
185    Ok((vec![value(s, k)? - t], None))
186}