lensbench_analysis/analysis/
spot.rs1use super::pupil_gauss;
4use lensbench_core::error::Result;
5use lensbench_core::system::{Cols, FieldSel, RayBuf, System, Trace};
6use lensbench_core::trace::RayCounts;
7use lensbench_core::Waves;
8
9#[derive(Debug, Clone)]
10#[non_exhaustive]
11pub struct Spot {
12 pub x: Vec<f64>,
13 pub y: Vec<f64>,
14 pub wave: Vec<usize>,
15 pub weight: Vec<f64>,
16 pub centroid: (f64, f64),
17 pub chief: (f64, f64),
18 pub rms_centroid: f64,
19 pub rms_chief: f64,
20 pub geo: f64,
21 pub rays: RayCounts,
23}
24
25pub fn spot(s: &System, field: FieldSel, w: Waves, rings: usize) -> Result<Spot> {
29 let w = w.one();
30 spot_arms(s, field, Waves::all_or(w), rings, None)
31}
32
33pub fn spot_arms(
37 s: &System,
38 field: FieldSel,
39 w: Waves,
40 rings: usize,
41 arms: Option<usize>,
42) -> Result<Spot> {
43 spot_pupil(s, field, w, &pupil_gauss(rings, arms))
44}
45
46pub fn spot_pupil(
48 s: &System,
49 field: FieldSel,
50 w: Waves,
51 pupil: &(Vec<f64>, Vec<f64>, Vec<f64>),
52) -> Result<Spot> {
53 let w = w.one();
54 let (px, py, wts) = pupil;
55 let ws: Vec<usize> = match w {
56 None => (0..s.wavelengths.len()).collect(),
57 Some(w) => vec![w],
58 };
59 let mut rs = Vec::with_capacity(ws.len());
60 for &wi in &ws {
61 let mut b = RayBuf::with_cols(Cols::XYZ | Cols::LMN);
62 s.try_trace_into(field, px, py, Some(wi), None, &mut b)?;
63 rs.push((wi, b));
64 }
65 let rc = s.try_trace(
66 field,
67 &[0.0],
68 &[0.0],
69 Some(w.unwrap_or(s.primary)),
70 None,
71 false,
72 )?;
73 let rs: Vec<(usize, &RayBuf)> = rs.iter().map(|(wi, r)| (*wi, r)).collect();
74 let wts = apodized(s, field, px, py, wts)?;
75 Ok(spot_of(s, &rs, &wts, &rc))
76}
77
78pub fn apodized(
81 s: &System,
82 field: FieldSel,
83 px: &[f64],
84 py: &[f64],
85 wts: &[f64],
86) -> Result<Vec<f64>> {
87 apodized_pow(s, field, px, py, wts, 2)
88}
89
90pub fn apodized_amplitude(
94 s: &System,
95 field: FieldSel,
96 px: &[f64],
97 py: &[f64],
98 wts: &[f64],
99) -> Result<Vec<f64>> {
100 apodized_pow(s, field, px, py, wts, 1)
101}
102
103fn apodized_pow(
104 s: &System,
105 field: FieldSel,
106 px: &[f64],
107 py: &[f64],
108 wts: &[f64],
109 pow: i32,
110) -> Result<Vec<f64>> {
111 let a = lensbench_core::pupil::ApodMap::new(s, field)?;
112 if a.is_uniform() {
113 return Ok(wts.to_vec());
114 }
115 Ok(wts
116 .iter()
117 .zip(px.iter().zip(py))
118 .map(|(&w, (&x, &y))| w * a.amp(x, y).powi(pow))
119 .collect())
120}
121
122pub(crate) fn spot_of(s: &System, rs: &[(usize, &RayBuf)], wts: &[f64], rc: &Trace) -> Spot {
125 let (mut x, mut y, mut wt, mut wv) = (Vec::new(), Vec::new(), Vec::new(), Vec::new());
126 let mut rays = RayCounts::default();
127 for &(wi, r) in rs {
128 rays.merge(&r.counts());
129 for (i, &wq) in wts.iter().enumerate() {
130 if r.ok(i) {
131 let (a, b) = s.image_point(r.xyz[i], r.lmn[i]);
132 x.push(a);
133 y.push(b);
134 wt.push(wq * if rs.len() == 1 { 1.0 } else { s.weights[wi] });
136 wv.push(wi);
137 }
138 }
139 }
140 let chief = if rc.traced(0) {
141 s.image_point(rc.rays[0].xyz, rc.rays[0].lmn)
142 } else {
143 (f64::NAN, f64::NAN)
144 };
145 let sw: f64 = wt.iter().sum();
146 if x.is_empty() || !(sw > 0.0) {
147 return Spot {
148 x,
149 y,
150 wave: wv,
151 weight: wt,
152 centroid: (f64::NAN, f64::NAN),
153 chief,
154 rms_centroid: f64::NAN,
155 rms_chief: f64::NAN,
156 geo: f64::NAN,
157 rays,
158 };
159 }
160 let avg = |v: &dyn Fn(usize) -> f64| (0..x.len()).map(|i| v(i) * wt[i]).sum::<f64>() / sw;
161 let cx = avg(&|i| x[i]);
162 let cy = avg(&|i| y[i]);
163 let (hx, hy) = chief;
164 let r2c = |i: usize| (x[i] - cx).powi(2) + (y[i] - cy).powi(2);
165 let rms_c = avg(&r2c).sqrt();
166 let rms_h = avg(&|i| (x[i] - hx).powi(2) + (y[i] - hy).powi(2)).sqrt();
167 let geo = (0..x.len()).map(r2c).fold(0.0, f64::max).sqrt();
168 Spot {
169 centroid: (cx, cy),
170 chief,
171 rms_centroid: rms_c,
172 rms_chief: rms_h,
173 geo,
174 rays,
175 x,
176 y,
177 wave: wv,
178 weight: wt,
179 }
180}