1use crate::math::*;
8use crate::shape::sag;
9use crate::surface::{Binary2, Bire, CylFresnel, Reflect, Retarder, SurfType, Surface};
10use crate::usersurf::UserSurf;
11
12#[derive(Debug, Clone, Copy, PartialEq)]
14pub enum JonesSrc<'a> {
15 Matrix(&'a [f64; 8]),
17 Retarder(&'a Retarder),
19}
20
21impl JonesSrc<'_> {
22 pub fn jones8(&self, wl: f64) -> [f64; 8] {
24 match self {
25 JonesSrc::Matrix(m) => **m,
26 JonesSrc::Retarder(r) => r.jones8(wl),
27 }
28 }
29}
30
31#[derive(Debug, Clone, Copy, PartialEq)]
33pub enum FresnelBend<'a> {
34 Radial { c: f64, k: f64, a: &'a [f64] },
36 Cyl(Option<&'a CylFresnel>),
38}
39
40impl FresnelBend<'_> {
41 pub fn normal(&self, x: f64, y: f64) -> V3 {
43 match *self {
44 FresnelBend::Cyl(Some(f)) => {
45 let (_, dz) = sag(f.c, f.k, &f.coef, y * y);
46 [0.0, -2.0 * dz * y, 1.0]
47 }
48 FresnelBend::Cyl(None) => [0.0, 0.0, 1.0],
49 FresnelBend::Radial { c, k, a } => {
50 let (_, dz) = sag(c, k, a, x * x + y * y);
51 [-2.0 * dz * x, -2.0 * dz * y, 1.0]
52 }
53 }
54 }
55
56 pub fn paraxial_curv(&self) -> Option<f64> {
58 match *self {
59 FresnelBend::Radial { c, a, .. } => Some(c + 2.0 * a.first().copied().unwrap_or(0.0)),
60 FresnelBend::Cyl(Some(f)) => Some(f.c + 2.0 * f.coef.first().copied().unwrap_or(0.0)),
61 FresnelBend::Cyl(None) => None,
62 }
63 }
64}
65
66#[derive(Debug, Clone, Copy, PartialEq)]
69pub enum Action<'a> {
70 Refract,
72 Reflect { ghost: Option<&'a Reflect> },
74 Paraxial { f: f64, opd_mode: i32 },
76 Jones(JonesSrc<'a>),
78 Binary2(Option<&'a Binary2>),
80 Fresnel(FresnelBend<'a>),
82 Birefringent(&'a Bire),
84 User(Option<&'a UserSurf>),
86}
87
88impl Surface {
89 pub fn action(&self) -> Action<'_> {
92 match self.typ() {
93 SurfType::Paraxial => Action::Paraxial {
94 f: self.focal(),
95 opd_mode: self.opd_mode(),
96 },
97 SurfType::User => Action::User(self.user()),
98 SurfType::Jones => Action::Jones(match &self.retarder {
99 Some(r) => JonesSrc::Retarder(r),
100 None => JonesSrc::Matrix(match &self.form {
101 crate::surface::Form::Jones { jones } => jones,
102 _ => &crate::surface::JONES_IDENTITY,
103 }),
104 }),
105 SurfType::Binary2 => Action::Binary2(self.bin2()),
106 SurfType::Fresnel => Action::Fresnel(FresnelBend::Radial {
107 c: self.c,
108 k: self.k,
109 a: self.asph(),
110 }),
111 SurfType::CylFresnel => Action::Fresnel(FresnelBend::Cyl(self.cylf())),
112 SurfType::CoordBrk => Action::Refract,
113 _ => match &self.bire {
114 Some(b) if !self.is_mirror() => Action::Birefringent(b),
115 _ if self.is_mirror() => Action::Reflect {
116 ghost: self.reflect.as_ref(),
117 },
118 _ => Action::Refract,
119 },
120 }
121 }
122}
123
124impl Action<'_> {
125 pub fn is_ideal(&self) -> bool {
127 matches!(self, Action::Paraxial { .. } | Action::User(_))
128 }
129}
130
131#[derive(Debug, Clone, Copy, PartialEq, Eq)]
133pub(crate) enum Act {
134 Bend,
136 CoordBrk,
137 Paraxial,
138 User,
139 Jones,
140 Binary2,
141}
142
143impl Act {
144 pub(crate) fn of(d: &Surface, a: &Action) -> Act {
145 if d.is_coord_break() {
146 return Act::CoordBrk;
147 }
148 match a {
149 Action::Paraxial { .. } => Act::Paraxial,
150 Action::User(_) => Act::User,
151 Action::Jones(_) => Act::Jones,
152 Action::Binary2(_) => Act::Binary2,
153 Action::Refract
154 | Action::Reflect { .. }
155 | Action::Fresnel(_)
156 | Action::Birefringent(_) => Act::Bend,
157 }
158 }
159}