1use crate::math::*;
4use crate::system::{FieldSel, FieldType, System, ENPP_MAX};
5
6pub(crate) fn working_fnum(s: &System) -> f64 {
8 let Ok(r) = s.try_trace(
9 FieldSel::Norm(0.0, 0.0),
10 &[0.0, 1.0, -1.0, 0.0, 0.0],
11 &[0.0, 0.0, 0.0, 1.0, -1.0],
12 Some(s.primary),
13 None,
14 false,
15 ) else {
16 return f64::NAN;
17 };
18 let k0 = r.rays[0].lmn;
19 let m: f64 = r.rays[1..]
20 .iter()
21 .map(|x| {
22 let c = dot(x.lmn, k0).clamp(-1.0, 1.0);
23 1.0 - c * c
24 })
25 .sum::<f64>()
26 / 4.0;
27 1.0 / (2.0 * s.image_index(s.primary) * m.sqrt())
28}
29
30#[derive(Debug, Clone)]
31pub struct SystemData {
32 pub effl: f64,
33 pub epdi: f64,
34 pub enpp: f64,
35 pub expd: f64,
36 pub expp: f64,
37 pub wfno: f64,
38 pub pwfn: f64,
39 pub isfn: f64,
40 pub pimh: f64,
41 pub pmag: f64,
42 pub totr: f64,
43 pub obsn: f64,
44 pub isna: f64,
48}
49
50pub fn system_data(s: &System) -> SystemData {
51 let p = s.paraxial();
52 let hmax = s.fields.iter().map(|f| f.x.hypot(f.y)).fold(0.0, f64::max);
53 let pimh = match s.field_type {
54 FieldType::Angle => {
55 let t = crate::quantity::Angle::from_deg(hmax).tan();
56 if s.infinite() {
57 p.efl.abs() * t
58 } else {
59 (p.pmag * (s.surfaces[0].t + p.enpp) * t).abs()
60 }
61 }
62 FieldType::ObjH => p.pmag.abs() * hmax,
63 FieldType::Solve(crate::system::FieldCond::Position { surf }) if surf == s.image() => hmax,
64 FieldType::Solve(_) => f64::NAN,
66 _ => hmax,
67 };
68 let mut z = vec![0.0];
69 for d in &s.surfaces[1..s.image()] {
70 if !d.ignore {
71 z.push(z.last().expect("starts with one entry") + d.t);
72 }
73 }
74 let totr =
75 z.iter().cloned().fold(f64::MIN, f64::max) - z.iter().cloned().fold(f64::MAX, f64::min);
76 let n0 = s.n(0)[s.primary];
77 let obsn = if s.infinite() {
78 n0 * p.epd / 2.0 / ENPP_MAX
79 } else {
80 match p.u_obj {
81 None => 0.0,
82 Some(u) => n0 * u.abs().atan().sin(),
83 }
84 };
85 let umi = p.marginal.1[s.image() - 1];
86 let isna = s.image_index(s.primary) * umi.abs().atan().sin();
87 SystemData {
88 effl: p.efl,
89 epdi: p.epd,
90 enpp: p.enpp,
91 expd: s.exit_pupil_diameter(),
92 expp: if s.cusp_pupil() {
94 s.exit_pupil()
95 } else {
96 p.expp
97 },
98 wfno: working_fnum(s),
99 pwfn: p.wfno,
100 isfn: p.isfn,
101 pimh,
102 pmag: p.pmag,
103 totr,
104 obsn,
105 isna,
106 }
107}
108
109#[derive(Debug, Clone, Copy, PartialEq)]
116pub struct Cardinal {
117 pub n_object: f64,
119 pub n_image: f64,
120 pub efl: f64,
122 pub f_object: f64,
123 pub f_image: f64,
124 pub focal_object: f64,
126 pub principal_object: f64,
127 pub anti_principal_object: f64,
128 pub nodal_object: f64,
129 pub anti_nodal_object: f64,
130 pub focal_image: f64,
132 pub principal_image: f64,
133 pub anti_principal_image: f64,
134 pub nodal_image: f64,
135 pub anti_nodal_image: f64,
136 pub bfl: f64,
138 pub ffl: f64,
140}
141
142pub fn cardinal(s: &System, w: usize) -> Cardinal {
143 let img = s.image();
144 let last = (1..img).rev().find(|&j| !s.surfaces[j].ignore).unwrap_or(0);
146 let mut c = cardinal_span(s, 1, last, w);
147 let t_img = if last >= 1 { s.surfaces[last].t } else { 0.0 };
149 for v in [
150 &mut c.focal_image,
151 &mut c.principal_image,
152 &mut c.anti_principal_image,
153 &mut c.nodal_image,
154 &mut c.anti_nodal_image,
155 ] {
156 *v -= t_img;
157 }
158 c
159}
160
161pub fn cardinal_span(s: &System, from: usize, to: usize, w: usize) -> Cardinal {
165 let sf = &s.surfaces;
166 let from = from.max(1);
167 let n0 = s.index_before(from, w);
168 let n1 = s.index_before(to + 1, w);
169 let (ya, ua) = s.ptrace_from(from, 1.0, 0.0, w, Some(to));
170 let (_, ub) = s.ptrace_from(from, 0.0, 1.0, w, Some(to));
171 let (m11, m21) = (ya[to], n1 * ua[to]);
173 let m22 = n1 * ub[to] / n0;
174 let phi = -m21;
175 let sgn = sf
177 .get(from..=to)
178 .unwrap_or(&[])
179 .iter()
180 .filter(|d| !d.ignore && d.is_mirror())
181 .fold(1.0, |a, _| -a);
182 let inf = f64::INFINITY;
183 if phi == 0.0 || !phi.is_finite() {
184 return Cardinal {
185 n_object: n0,
186 n_image: n1,
187 efl: inf,
188 f_object: -inf,
189 f_image: inf,
190 focal_object: inf,
191 principal_object: inf,
192 anti_principal_object: inf,
193 nodal_object: inf,
194 anti_nodal_object: inf,
195 focal_image: inf,
196 principal_image: inf,
197 anti_principal_image: inf,
198 nodal_image: inf,
199 anti_nodal_image: inf,
200 bfl: inf,
201 ffl: inf,
202 };
203 }
204 let efl = 1.0 / phi;
205 let n1s = sgn * n1;
207 let (fo, fi) = (-n0 * efl, n1s * efl);
208 let bfl = sgn * m11 * n1 * efl;
210 let fz = bfl;
211 let pz = fz - fi;
212 let ffl = -m22 * n0 * efl;
214 let fz0 = ffl;
215 let pz0 = fz0 - fo;
216 let dn = (n1s - n0) * efl;
218 let (nz0, nz) = (pz0 + dn, pz + dn);
219 Cardinal {
220 n_object: n0,
221 n_image: n1s,
222 efl,
223 f_object: fo,
224 f_image: fi,
225 focal_object: fz0,
226 principal_object: pz0,
227 anti_principal_object: 2.0 * fz0 - pz0,
228 nodal_object: nz0,
229 anti_nodal_object: 2.0 * fz0 - nz0,
230 focal_image: fz,
231 principal_image: pz,
232 anti_principal_image: 2.0 * fz - pz,
233 nodal_image: nz,
234 anti_nodal_image: 2.0 * fz - nz,
235 bfl,
236 ffl,
237 }
238}