1use super::*;
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
10pub struct Cols(u16);
11
12impl Cols {
13 pub const XYZ: Cols = Cols(1);
15 pub const LMN: Cols = Cols(1 << 1);
17 pub const OPL: Cols = Cols(1 << 2);
19 pub const PK: Cols = Cols(1 << 3);
21 pub const START: Cols = Cols(1 << 4);
23 pub const OPL_OS: Cols = Cols(1 << 5);
25 pub const LENS_OPD: Cols = Cols(1 << 6);
27 pub const ALL: Cols = Cols((1 << 7) - 1);
29 pub const SPOT: Cols = Cols::XYZ;
31 pub const WAVE: Cols = Cols(1 | 1 << 1 | 1 << 2 | 1 << 6);
33
34 pub const fn empty() -> Cols {
35 Cols(0)
36 }
37 pub const fn has(self, c: Cols) -> bool {
38 self.0 & c.0 == c.0
39 }
40}
41
42impl std::ops::BitOr for Cols {
43 type Output = Cols;
44 fn bitor(self, o: Cols) -> Cols {
45 Cols(self.0 | o.0)
46 }
47}
48
49#[derive(Debug, Clone, Default)]
51pub struct RayBuf {
52 cols: Cols,
54 pub status: Vec<RayStatus>,
55 pub vig: Vec<u32>,
57 pub xyz: Vec<V3>,
58 pub lmn: Vec<V3>,
59 pub opl: Vec<f64>,
60 pub p: Vec<V3>,
61 pub k: Vec<V3>,
62 pub p0: Vec<V3>,
63 pub k0: Vec<V3>,
64 pub opl_os: Vec<f64>,
65 pub lens_opd: Vec<f64>,
66 pub parity: f64,
68 pub last: usize,
69 pub hx_hy: (f64, f64),
70}
71
72impl RayBuf {
73 pub fn new() -> RayBuf {
74 RayBuf::default()
75 }
76 pub fn with_cols(cols: Cols) -> RayBuf {
78 RayBuf {
79 cols,
80 ..Default::default()
81 }
82 }
83 pub fn cols(&self) -> Cols {
85 self.cols
86 }
87 pub fn len(&self) -> usize {
89 self.status.len()
90 }
91 pub fn is_empty(&self) -> bool {
92 self.status.is_empty()
93 }
94 pub fn ok(&self, i: usize) -> bool {
96 self.status[i].is_ok() && self.vig[i] == 0
97 }
98 pub fn counts(&self) -> crate::trace::RayCounts {
100 let mut c = crate::trace::RayCounts::default();
101 for (st, &v) in self.status.iter().zip(&self.vig) {
102 c.add(*st, v as usize);
103 }
104 c
105 }
106}
107
108impl RayBuf {
109 pub fn fill_from(&mut self, t: &Trace) {
111 let c = self.cols;
112 let r = &t.rays;
113 self.status = r.iter().map(|x| x.status).collect();
114 self.vig = r.iter().map(|x| x.vig as u32).collect();
115 let pick = |on: bool, f: &dyn Fn(&RayOut) -> V3| -> Vec<V3> {
116 if on {
117 r.iter().map(f).collect()
118 } else {
119 Vec::new()
120 }
121 };
122 let pickf = |on: bool, f: &dyn Fn(&RayOut) -> f64| -> Vec<f64> {
123 if on {
124 r.iter().map(f).collect()
125 } else {
126 Vec::new()
127 }
128 };
129 self.xyz = pick(c.has(Cols::XYZ), &|x| x.xyz);
130 self.lmn = pick(c.has(Cols::LMN), &|x| x.lmn);
131 self.opl = pickf(c.has(Cols::OPL), &|x| x.opl);
132 self.p = pick(c.has(Cols::PK), &|x| x.p);
133 self.k = pick(c.has(Cols::PK), &|x| x.k);
134 self.lens_opd = pickf(c.has(Cols::LENS_OPD), &|x| x.lens_opd);
135 let st = c.has(Cols::START);
136 self.p0 = if st { t.p0.clone() } else { Vec::new() };
137 self.k0 = if st { t.k0.clone() } else { Vec::new() };
138 self.opl_os = if c.has(Cols::OPL_OS) {
139 t.opl_os.clone()
140 } else {
141 Vec::new()
142 };
143 self.parity = t.parity;
144 self.last = t.last;
145 self.hx_hy = t.hx_hy;
146 }
147}
148
149pub(super) struct Done {
151 pub(super) parity: f64,
152 pub(super) rec: Vec<RecSurf>,
153 pub(super) last: usize,
154 pub(super) hx_hy: (f64, f64),
155}
156
157pub(super) struct ColOut<'a, T: Copy> {
160 p: *mut T,
161 n: usize,
162 _col: std::marker::PhantomData<&'a mut [T]>,
164}
165unsafe impl<T: Copy + Send> Sync for ColOut<'_, T> {}
167unsafe impl<T: Copy + Send> Send for ColOut<'_, T> {}
168
169impl<'a, T: Copy> ColOut<'a, T> {
170 pub(super) fn off() -> Self {
171 ColOut {
172 p: std::ptr::null_mut(),
173 n: 0,
174 _col: std::marker::PhantomData,
175 }
176 }
177
178 fn spare(v: &'a mut Vec<T>, on: bool, n: usize) -> Self {
181 v.clear();
182 if !on {
183 return ColOut::off();
184 }
185 v.reserve(n);
186 ColOut {
187 p: v.spare_capacity_mut()[..n].as_mut_ptr() as *mut T,
188 n,
189 _col: std::marker::PhantomData,
190 }
191 }
192
193 pub(super) fn over(v: &'a mut [T]) -> Self {
195 ColOut {
196 p: v.as_mut_ptr(),
197 n: v.len(),
198 _col: std::marker::PhantomData,
199 }
200 }
201
202 #[inline(always)]
203 pub(super) fn on(&self) -> bool {
204 !self.p.is_null()
205 }
206
207 #[inline(always)]
208 pub(super) fn put(&self, i: usize, v: T) {
209 if self.p.is_null() {
210 return;
211 }
212 assert!(i < self.n);
213 unsafe { self.p.add(i).write(v) }
215 }
216}
217
218pub(super) struct OutPtrs<'a> {
220 ray: ColOut<'a, RayOut>,
221 status: ColOut<'a, RayStatus>,
222 vig: ColOut<'a, u32>,
223 xyz: ColOut<'a, V3>,
224 lmn: ColOut<'a, V3>,
225 opl: ColOut<'a, f64>,
226 p: ColOut<'a, V3>,
227 k: ColOut<'a, V3>,
228 p0: ColOut<'a, V3>,
229 k0: ColOut<'a, V3>,
230 opl_os: ColOut<'a, f64>,
231 lens_opd: ColOut<'a, f64>,
232 os: Option<(V3, V3, f64)>,
234}
235
236impl OutPtrs<'_> {
237 #[inline(always)]
238 pub(super) fn put(&self, i: usize, r: &RayOut, p0: V3, k0: V3) {
239 self.ray.put(i, *r);
240 self.status.put(i, r.status);
241 self.vig.put(i, r.vig as u32);
242 self.xyz.put(i, r.xyz);
243 self.lmn.put(i, r.lmn);
244 self.opl.put(i, r.opl);
245 self.p.put(i, r.p);
246 self.k.put(i, r.k);
247 self.p0.put(i, p0);
248 self.k0.put(i, k0);
249 self.lens_opd.put(i, r.lens_opd);
250 if self.opl_os.on() {
251 let v = match self.os {
252 Some((o, ax, nb)) => r.opl - dot(sub(o, p0), ax) / dot(k0, ax) * nb,
253 None => r.opl,
254 };
255 self.opl_os.put(i, v);
256 }
257 }
258}
259
260pub(super) enum Dst<'a> {
262 Trace {
263 rays: Vec<RayOut>,
264 p0: Vec<V3>,
265 k0: Vec<V3>,
266 opl_os: Vec<f64>,
267 },
268 Buf(&'a mut RayBuf),
269}
270
271impl Dst<'_> {
272 pub(super) fn trace() -> Dst<'static> {
273 Dst::Trace {
274 rays: Vec::new(),
275 p0: Vec::new(),
276 k0: Vec::new(),
277 opl_os: Vec::new(),
278 }
279 }
280
281 pub(super) fn prepare(&mut self, n: usize, os: Option<(V3, V3, f64)>) -> OutPtrs<'_> {
283 match self {
284 Dst::Trace {
285 rays,
286 p0,
287 k0,
288 opl_os,
289 } => OutPtrs {
290 ray: ColOut::spare(rays, true, n),
291 status: ColOut::off(),
292 vig: ColOut::off(),
293 xyz: ColOut::off(),
294 lmn: ColOut::off(),
295 opl: ColOut::off(),
296 p: ColOut::off(),
297 k: ColOut::off(),
298 p0: ColOut::spare(p0, true, n),
299 k0: ColOut::spare(k0, true, n),
300 opl_os: ColOut::spare(opl_os, true, n),
301 lens_opd: ColOut::off(),
302 os,
303 },
304 Dst::Buf(b) => {
305 let c = b.cols;
306 OutPtrs {
307 ray: ColOut::off(),
308 status: ColOut::spare(&mut b.status, true, n),
309 vig: ColOut::spare(&mut b.vig, true, n),
310 xyz: ColOut::spare(&mut b.xyz, c.has(Cols::XYZ), n),
311 lmn: ColOut::spare(&mut b.lmn, c.has(Cols::LMN), n),
312 opl: ColOut::spare(&mut b.opl, c.has(Cols::OPL), n),
313 p: ColOut::spare(&mut b.p, c.has(Cols::PK), n),
314 k: ColOut::spare(&mut b.k, c.has(Cols::PK), n),
315 p0: ColOut::spare(&mut b.p0, c.has(Cols::START), n),
316 k0: ColOut::spare(&mut b.k0, c.has(Cols::START), n),
317 opl_os: ColOut::spare(&mut b.opl_os, c.has(Cols::OPL_OS), n),
318 lens_opd: ColOut::spare(&mut b.lens_opd, c.has(Cols::LENS_OPD), n),
319 os,
320 }
321 }
322 }
323 }
324
325 pub(super) unsafe fn finish(&mut self, n: usize) {
330 unsafe fn set<T>(v: &mut Vec<T>, n: usize) {
331 debug_assert!(v.capacity() >= n);
332 unsafe { v.set_len(n) };
334 }
335 unsafe {
337 match self {
338 Dst::Trace {
339 rays,
340 p0,
341 k0,
342 opl_os,
343 } => {
344 set(rays, n);
345 set(p0, n);
346 set(k0, n);
347 set(opl_os, n);
348 }
349 Dst::Buf(b) => {
350 let c = b.cols;
351 set(&mut b.status, n);
352 set(&mut b.vig, n);
353 let cond = |on: bool| if on { n } else { 0 };
354 set(&mut b.xyz, cond(c.has(Cols::XYZ)));
355 set(&mut b.lmn, cond(c.has(Cols::LMN)));
356 set(&mut b.opl, cond(c.has(Cols::OPL)));
357 set(&mut b.p, cond(c.has(Cols::PK)));
358 set(&mut b.k, cond(c.has(Cols::PK)));
359 set(&mut b.p0, cond(c.has(Cols::START)));
360 set(&mut b.k0, cond(c.has(Cols::START)));
361 set(&mut b.opl_os, cond(c.has(Cols::OPL_OS)));
362 set(&mut b.lens_opd, cond(c.has(Cols::LENS_OPD)));
363 }
364 }
365 }
366 }
367}