1use super::raybuf::ColOut;
4use super::*;
5
6#[derive(Debug, Clone, Copy)]
8pub enum Launch<'a> {
9 Pupil {
13 spec: ObjSpec,
14 signed: bool,
15 px: &'a [f64],
16 py: &'a [f64],
17 vig: Option<[f64; 5]>,
18 hxy: Option<(f64, f64)>,
19 },
20 Stop {
23 spec: ObjSpec,
24 x: &'a [f64],
25 y: &'a [f64],
26 },
27 Rays { p: &'a [V3], k: &'a [V3] },
29}
30
31#[derive(Debug, Clone, Copy, PartialEq)]
33pub enum FieldSel {
34 Index(usize),
35 Norm(f64, f64),
36}
37impl From<usize> for FieldSel {
38 fn from(i: usize) -> Self {
39 FieldSel::Index(i)
40 }
41}
42impl From<(f64, f64)> for FieldSel {
43 fn from(h: (f64, f64)) -> Self {
44 FieldSel::Norm(h.0, h.1)
45 }
46}
47
48#[derive(Debug, Clone)]
50pub struct Trace {
51 pub rays: Vec<RayOut>,
52 pub parity: f64,
54 pub p0: Vec<V3>,
56 pub k0: Vec<V3>,
57 pub opl_os: Vec<f64>,
59 pub rec: Vec<RecSurf>,
61 pub last: usize,
62 pub hx_hy: (f64, f64),
63}
64
65#[derive(Debug, Clone)]
67pub struct PolTrace {
68 pub trace: Trace,
69 pub e: PolField,
71}
72
73#[derive(Debug, Clone, Default, PartialEq)]
75pub struct PolField {
76 np: usize,
78 n: usize,
80 data: Vec<CV3>,
81}
82
83impl PolField {
84 pub fn from_flat(np: usize, data: Vec<CV3>) -> Self {
86 let n = data.len().checked_div(np).unwrap_or(0);
87 assert_eq!(
88 n * np,
89 data.len(),
90 "the field length must be a multiple of the polarizations"
91 );
92 PolField { np, n, data }
93 }
94 pub fn len(&self) -> usize {
96 self.n
97 }
98 pub fn is_empty(&self) -> bool {
99 self.n == 0
100 }
101 pub fn pols(&self) -> usize {
103 self.np
104 }
105 pub fn iter(&self) -> PolFieldIter<'_> {
107 PolFieldIter { f: self, i: 0 }
108 }
109 pub fn as_flat(&self) -> &[CV3] {
111 &self.data
112 }
113 pub fn into_flat(self) -> Vec<CV3> {
114 self.data
115 }
116}
117
118#[derive(Debug, Clone)]
120pub struct PolFieldIter<'a> {
121 f: &'a PolField,
122 i: usize,
123}
124
125impl<'a> Iterator for PolFieldIter<'a> {
126 type Item = &'a [CV3];
127 fn next(&mut self) -> Option<&'a [CV3]> {
128 let f = self.f;
129 (self.i < f.n).then(|| {
130 self.i += 1;
131 &f.data[(self.i - 1) * f.np..self.i * f.np]
132 })
133 }
134 fn size_hint(&self) -> (usize, Option<usize>) {
135 let r = self.f.n - self.i;
136 (r, Some(r))
137 }
138}
139
140impl ExactSizeIterator for PolFieldIter<'_> {}
141
142impl<'a> IntoIterator for &'a PolField {
143 type Item = &'a [CV3];
144 type IntoIter = PolFieldIter<'a>;
145 fn into_iter(self) -> PolFieldIter<'a> {
146 self.iter()
147 }
148}
149
150impl std::ops::Index<usize> for PolField {
151 type Output = [CV3];
152 fn index(&self, i: usize) -> &[CV3] {
153 assert!(i < self.n, "ray {i} out of {}", self.n);
154 &self.data[i * self.np..(i + 1) * self.np]
155 }
156}
157
158impl Trace {
159 pub fn ok(&self, i: usize) -> bool {
160 self.rays[i].status.is_ok() && self.rays[i].vig == 0
161 }
162 pub fn counts(&self) -> crate::trace::RayCounts {
164 let mut c = crate::trace::RayCounts::default();
165 for r in &self.rays {
166 c.add(r.status, r.vig);
167 }
168 c
169 }
170 pub fn traced(&self, i: usize) -> bool {
173 self.rays[i].status.is_ok()
174 }
175 pub fn rec_at(&self, j: usize) -> Option<&RecSurf> {
177 self.rec
178 .binary_search_by_key(&j, |r| r.surf)
179 .ok()
180 .map(|k| &self.rec[k])
181 }
182}
183
184#[derive(Debug, Clone, Copy, Default)]
186pub struct TraceOpts {
187 pub vig: Option<[f64; 5]>,
189 pub upto: Option<usize>,
191 pub record: bool,
193 pub hxy: Option<(f64, f64)>,
195}
196
197impl TraceOpts {
198 pub fn new() -> Self {
199 Self::default()
200 }
201 pub fn vig(self, v: [f64; 5]) -> Self {
202 self.vig_opt(Some(v))
203 }
204 pub fn vig_opt(mut self, v: Option<[f64; 5]>) -> Self {
205 self.vig = v;
206 self
207 }
208 pub fn upto(self, j: usize) -> Self {
209 self.upto_opt(Some(j))
210 }
211 pub fn upto_opt(mut self, j: Option<usize>) -> Self {
212 self.upto = j;
213 self
214 }
215 pub fn record(self) -> Self {
216 self.record_if(true)
217 }
218 pub fn record_if(mut self, b: bool) -> Self {
219 self.record = b;
220 self
221 }
222 pub fn hxy(self, h: (f64, f64)) -> Self {
223 self.hxy_opt(Some(h))
224 }
225 pub fn hxy_opt(mut self, h: Option<(f64, f64)>) -> Self {
226 self.hxy = h;
227 self
228 }
229}
230
231#[derive(Debug, Clone, Copy)]
233pub enum RecSel<'a> {
234 Off,
236 All,
238 Surfs(&'a [usize]),
240}
241
242impl From<bool> for RecSel<'_> {
243 fn from(b: bool) -> Self {
244 if b {
245 RecSel::All
246 } else {
247 RecSel::Off
248 }
249 }
250}
251
252pub(super) struct SoaPtrs<'a> {
254 slot: Vec<usize>,
256 xyz: Vec<ColOut<'a, V3>>,
257 lmn: Vec<ColOut<'a, V3>>,
258 opl: Vec<ColOut<'a, f64>>,
259 p: Vec<ColOut<'a, V3>>,
260 pub(super) n: usize,
261}
262
263impl<'a> SoaPtrs<'a> {
264 pub(super) fn new(r: &'a mut [RecSurf], slot: Vec<usize>, n: usize) -> SoaPtrs<'a> {
265 let (mut xyz, mut lmn, mut opl, mut p) = (vec![], vec![], vec![], vec![]);
266 for x in r {
267 xyz.push(ColOut::over(&mut x.xyz));
268 lmn.push(ColOut::over(&mut x.lmn));
269 opl.push(ColOut::over(&mut x.opl));
270 p.push(ColOut::over(&mut x.p));
271 }
272 SoaPtrs {
273 slot,
274 xyz,
275 lmn,
276 opl,
277 p,
278 n,
279 }
280 }
281}
282
283pub(super) struct SoaRow<'a> {
284 pub(super) out: &'a SoaPtrs<'a>,
285 pub(super) i: usize,
286}
287
288impl trace::RecSink for SoaRow<'_> {
289 #[inline(always)]
290 fn wants(&self, k: usize) -> bool {
291 self.out.slot.get(k).is_some_and(|&s| s != usize::MAX)
292 }
293 #[inline(always)]
294 fn put(&mut self, k: usize, e: RecEntry) {
295 let o = self.out;
296 let k = match o.slot.get(k) {
297 Some(&s) if s != usize::MAX => s,
298 _ => return,
299 };
300 o.xyz[k].put(self.i, e.xyz);
302 o.lmn[k].put(self.i, e.lmn);
303 o.opl[k].put(self.i, e.opl);
304 o.p[k].put(self.i, e.p);
305 }
306}
307
308pub(super) struct SoaLanes<'a> {
310 pub(super) out: &'a SoaPtrs<'a>,
311 pub(super) base: usize,
312}
313
314impl batch::LaneSink for SoaLanes<'_> {
315 #[inline(always)]
316 fn wants(&self, k: usize) -> bool {
317 self.out.slot.get(k).is_some_and(|&s| s != usize::MAX)
318 }
319 #[inline(always)]
320 fn put(&mut self, lane: usize, k: usize, e: RecEntry) {
321 let i = self.base + lane;
322 if i < self.out.n {
323 trace::RecSink::put(&mut SoaRow { out: self.out, i }, k, e);
324 }
325 }
326}
327
328#[derive(Debug, Clone)]
330pub struct RecSurf {
331 pub surf: usize,
332 pub parity: f64,
334 pub xyz: Vec<V3>,
336 pub lmn: Vec<V3>,
338 pub opl: Vec<f64>,
340 pub p: Vec<V3>,
342}
343
344impl RecSurf {
345 pub fn entry(&self, i: usize) -> RecEntry {
346 RecEntry {
347 surf: self.surf,
348 xyz: self.xyz[i],
349 lmn: self.lmn[i],
350 opl: self.opl[i],
351 p: self.p[i],
352 }
353 }
354}