1use super::*;
4use crate::action::Action;
5
6impl System {
7 pub fn pol_surfaces(&self, w: usize, coat: &Coatings) -> Result<Vec<PolSurf>> {
9 self.check_crystals()?;
10 let (keys, before) = coating::media_keys(self);
11 let wl = self.wavelengths[w];
12 let mut out = Vec::with_capacity(self.surfaces.len());
13 for (j, d) in self.surfaces.iter().enumerate() {
14 let it = self
15 .glasses
16 .get(&before[j])
17 .and_then(|g| coating::internal_t(g, wl));
18 let kind = if d.ignore || j == 0 || d.is_coord_break() {
19 PolKind::None
20 } else {
21 match d.action() {
22 Action::Jones(m) => {
23 let a = m.jones8(wl);
24 PolKind::Jones([
25 C64::new(a[0], a[1]),
26 C64::new(a[2], a[3]),
27 C64::new(a[4], a[5]),
28 C64::new(a[6], a[7]),
29 ])
30 }
31 Action::Paraxial { .. } | Action::User(_) => PolKind::None,
32 _ => match &keys[j] {
33 Some(k) => PolKind::Iface(
34 coat.prepare(
35 self,
36 k,
37 wl,
38 self.index_before(j, w),
39 match &d.reflect {
40 Some(r) if d.is_mirror() => r.n[w],
41 _ => self.n(j)[w],
42 },
43 )
44 .map_err(|e| e.at(self.surf_label(j)))?,
45 ),
46 None => PolKind::None,
47 },
48 }
49 };
50 let mut ps = PolSurf::new(kind, it);
51 if !d.ignore && j > 0 && !d.is_coord_break() {
53 ps.user_jones = match d.action() {
54 Action::User(Some(u)) => crate::trace::UserJones::of_surf(u, wl),
55 _ => d
56 .user_shape()
57 .and_then(|u| crate::trace::UserJones::of_shape(u, wl)),
58 };
59 }
60 if !d.ignore && j > 0 {
61 ps.cry = match &d.bire {
62 Some(Bire::In { mode, axis, ne }) => {
63 let ne = ne.get(w).copied().ok_or_else(|| {
64 Error::Invalid(format!(
65 "{}: no index for the extraordinary material {}",
66 self.surf_label(j),
67 Bire::e_name(&d.mat.name())
68 ))
69 })?;
70 Some(CryKind::In {
71 mode: *mode,
72 axis: *axis,
73 no: self.n(j)[w],
74 ne,
75 })
76 }
77 Some(Bire::Out) => Some(CryKind::Out),
78 None => None,
79 };
80 }
81 out.push(ps);
82 }
83 for j in 1..self.surfaces.len() {
86 let d = &self.surfaces[j];
87 if !matches!(out[j].cry, Some(CryKind::Out)) || d.is_mirror() || d.t != 0.0 {
88 continue;
89 }
90 if !matches!(d.after, After::None) {
91 continue;
92 }
93 let Some(k) = (j + 1..self.surfaces.len()).find(|&k| !self.surfaces[k].ignore) else {
94 continue;
95 };
96 let e = &self.surfaces[k];
97 let same_shape = e.typ() == d.typ()
98 && e.c == d.c
99 && e.k == d.k
100 && e.asph() == d.asph()
101 && e.cx() == d.cx()
102 && d.zern().is_none()
103 && d.grid().is_none()
104 && e.zern().is_none()
105 && e.grid().is_none();
106 if e.is_mirror() || !e.coat.is_empty() || e.before.is_some() || !same_shape {
107 continue;
108 }
109 if let Some(CryKind::In { axis, no, ne, .. }) = out[k].cry {
110 out[j].cement = Some((axis, no, ne));
111 out[k].cemented = true;
112 }
113 }
114 Ok(out)
115 }
116
117 pub fn try_trace_pol(
121 &self,
122 field: FieldSel,
123 px: &[f64],
124 py: &[f64],
125 w: Option<usize>,
126 pol: PolIn,
127 ) -> Result<PolTrace> {
128 let w = w.unwrap_or(self.primary);
129 let (hx, hy) = self.field_sel_norm(field, w)?;
130 let spec = self.object_spec(hx, hy)?;
131 let vig = self.vig_factors(hx, hy)?;
132 self.trace_pol_spec(
133 spec,
134 px,
135 py,
136 w,
137 TraceOpts::new().vig(vig).hxy((hx, hy)),
138 pol,
139 )
140 }
141
142 pub fn trace_pol_spec(
145 &self,
146 spec: ObjSpec,
147 px: &[f64],
148 py: &[f64],
149 w: usize,
150 o: TraceOpts,
151 pol: PolIn,
152 ) -> Result<PolTrace> {
153 let ps = self.pol_surfaces(w, pol.coat)?;
154 let (mut t, e, err) = self.trace_spec_inner(
155 spec,
156 px,
157 py,
158 w,
159 o.vig,
160 o.upto,
161 o.record,
162 o.hxy,
163 Some((pol.pols, &ps, pol.phase)),
164 );
165 if let Some(e) = err {
166 return Err(e);
167 }
168 t.hx_hy = o.hxy.unwrap_or((0.0, 0.0));
169 Ok(PolTrace { trace: t, e })
170 }
171}
172
173#[derive(Debug, Clone, Copy)]
175pub struct PolIn<'a> {
176 pub pols: &'a [[C64; 2]],
177 pub coat: &'a Coatings,
178 pub phase: bool,
180}
181
182impl<'a> PolIn<'a> {
183 pub fn new(pols: &'a [[C64; 2]], coat: &'a Coatings) -> Self {
184 Self {
185 pols,
186 coat,
187 phase: true,
188 }
189 }
190 pub fn no_phase(self) -> Self {
192 self.phase(false)
193 }
194 pub fn phase(mut self, phase: bool) -> Self {
195 self.phase = phase;
196 self
197 }
198}