1pub use crate::form::{Form, JONES_IDENTITY};
8pub use crate::material::Material;
9
10#[derive(
11 Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize, schemars::JsonSchema,
12)]
13pub enum SurfType {
14 Standard,
15 EvenAsph,
16 CoordBrk,
17 Jones,
18 Paraxial,
19 Toroidal,
22 ZernikeStd,
24 ZernikeFringe,
26 GridSag,
28 Fresnel,
30 CylFresnel,
32 Binary2,
34 Gradium,
36 Gradient9,
38 OddAsph,
40 TiltSurf,
42 Irregular,
45 User,
47 UserShape,
50}
51
52#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
54pub struct Binary2 {
55 pub order: f64,
57 pub norm: f64,
59 pub coef: Vec<f64>,
60}
61
62impl Binary2 {
63 pub fn phase(&self, x: f64, y: f64) -> (f64, f64, f64) {
65 let inv = 1.0 / (self.norm * self.norm);
66 let p2 = (x * x + y * y) * inv;
67 let (mut f, mut df, mut pw) = (0.0, 0.0, 1.0);
69 for (i, &a) in self.coef.iter().enumerate() {
70 let e = (i + 1) as f64;
71 df += a * e * pw;
72 pw *= p2;
73 f += a * pw;
74 }
75 let g = self.order * df * 2.0 * inv;
76 (self.order * f, g * x, g * y)
77 }
78}
79
80#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
82pub struct CylFresnel {
83 pub c: f64,
84 pub k: f64,
85 pub coef: Vec<f64>,
86}
87
88#[derive(
90 Debug, Clone, Copy, PartialEq, serde::Serialize, serde::Deserialize, schemars::JsonSchema,
91)]
92pub struct Tilt {
93 pub d: [f64; 5],
94 pub order: u8,
95}
96
97#[derive(
98 Debug, Clone, Copy, PartialEq, serde::Serialize, serde::Deserialize, schemars::JsonSchema,
99)]
100pub enum After {
101 None,
102 Reverse,
104 Tilt(Tilt),
105}
106
107#[derive(
108 Debug, Clone, Copy, PartialEq, serde::Serialize, serde::Deserialize, schemars::JsonSchema,
109)]
110pub enum Aperture {
111 None,
112 Circular {
114 min: f64,
115 max: f64,
116 dx: f64,
117 dy: f64,
118 },
119 Rect {
121 hx: f64,
122 hy: f64,
123 dx: f64,
124 dy: f64,
125 },
126 Floating {
128 semi: f64,
129 },
130 Ellipse {
132 hx: f64,
133 hy: f64,
134 dx: f64,
135 dy: f64,
136 },
137 Obsc {
139 min: f64,
140 max: f64,
141 dx: f64,
142 dy: f64,
143 },
144 Spider {
147 width: f64,
148 arms: u32,
149 dx: f64,
150 dy: f64,
151 },
152}
153
154#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
156pub struct Surface {
157 #[serde(flatten)]
159 pub form: Form,
160 pub ignore: bool,
161 pub t: f64,
163 pub comment: String,
164 pub mat: Material,
166 pub coat: String,
167 pub stop: bool,
168 pub semi: f64,
170 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
172 pub semi_auto: bool,
173 pub c: f64,
174 pub k: f64,
175 pub before: Option<Tilt>,
176 pub after: After,
177 pub ap: Aperture,
178 pub dn: f64,
180 pub disp: f64,
182 #[serde(default, skip_serializing_if = "is_zero")]
185 pub dnd: f64,
186 #[serde(default, skip_serializing_if = "is_zero")]
188 pub dvd: f64,
189 pub reflect: Option<Reflect>,
192 #[serde(default, skip_serializing_if = "Option::is_none")]
195 pub retarder: Option<Retarder>,
196 #[serde(default, skip_serializing_if = "Option::is_none")]
198 pub bire: Option<Bire>,
199 #[serde(default, skip_serializing_if = "is_zero")]
202 pub tce: f64,
203 #[serde(default, skip_serializing_if = "Option::is_none")]
207 pub mech_semi: Option<f64>,
208 #[serde(default, skip_serializing_if = "Option::is_none")]
211 pub coord_return: Option<crate::coord_return::CoordReturn>,
212 #[serde(default, skip_serializing_if = "is_zero")]
215 pub chip_zone: f64,
216}
217
218fn is_zero(x: &f64) -> bool {
219 *x == 0.0
220}
221
222#[derive(serde::Serialize, serde::Deserialize, schemars::JsonSchema, Debug, Clone, PartialEq)]
228pub struct Retarder {
229 pub axis_deg: f64,
230 pub tx: f64,
231 pub ty: f64,
232 pub retardance: f64,
234 pub order: u32,
236 pub wl0: f64,
238 #[serde(default, skip_serializing_if = "Vec::is_empty")]
240 pub plates: Vec<Plate>,
241}
242
243#[derive(serde::Serialize, serde::Deserialize, schemars::JsonSchema, Debug, Clone, PartialEq)]
248pub struct Plate {
249 pub material: String,
250 pub thickness: f64,
252 pub axis_deg: f64,
253 #[serde(default)]
255 pub o: Option<crate::glass::Glass>,
256 #[serde(default)]
257 pub e: Option<crate::glass::Glass>,
258}
259
260impl Plate {
261 pub fn dn(&self, wl: f64) -> f64 {
263 match (&self.o, &self.e) {
264 (Some(o), Some(e)) => e.n_cat(wl) - o.n_cat(wl),
265 _ => 0.0,
266 }
267 }
268}
269
270fn axis_matrix(
272 a: num_complex::Complex64,
273 b: num_complex::Complex64,
274 axis_deg: f64,
275) -> [num_complex::Complex64; 4] {
276 let (s, c) = axis_deg.to_radians().sin_cos();
277 [
279 a * c * c + b * s * s,
280 (a - b) * c * s,
281 (a - b) * c * s,
282 a * s * s + b * c * c,
283 ]
284}
285
286fn mul2(
287 m: &[num_complex::Complex64; 4],
288 n: &[num_complex::Complex64; 4],
289) -> [num_complex::Complex64; 4] {
290 [
291 m[0] * n[0] + m[1] * n[2],
292 m[0] * n[1] + m[1] * n[3],
293 m[2] * n[0] + m[3] * n[2],
294 m[2] * n[1] + m[3] * n[3],
295 ]
296}
297
298impl Retarder {
299 pub fn resolve(
302 &mut self,
303 glasses: &std::collections::HashMap<String, crate::glass::Glass>,
304 ) -> crate::Result<()> {
305 let n = self.plates.len();
306 for p in &mut self.plates {
307 let ename = format!("{}-E", p.material);
308 let (Some(o), Some(e)) = (glasses.get(&p.material), glasses.get(&ename)) else {
309 return Err(format!(
310 "birefringent material {}: the ordinary glass {} or extraordinary glass {ename} is not in the catalogs",
311 p.material, p.material
312 ).into());
313 };
314 p.o = Some(o.clone());
315 p.e = Some(e.clone());
316 let dn = p.dn(self.wl0);
317 if !(dn.is_finite() && dn != 0.0) {
318 return Err(format!(
319 "ne − no of birefringent material {} at the design wavelength {} µm is {dn}",
320 p.material, self.wl0
321 )
322 .into());
323 }
324 if p.thickness == 0.0 {
325 if n != 1 {
326 return Err(
327 "write a thickness for each plate of a waveplate with 2 or more plates"
328 .into(),
329 );
330 }
331 p.thickness = (self.order as f64 + self.retardance) * self.wl0 * 1e-3 / dn.abs();
333 }
334 if !(p.thickness > 0.0 && p.thickness.is_finite()) {
335 return Err(format!(
336 "birefringent material {} plate thickness {}",
337 p.material, p.thickness
338 )
339 .into());
340 }
341 }
342 Ok(())
343 }
344
345 pub fn plate_retardance(&self, wl: f64) -> Vec<f64> {
347 self.plates
348 .iter()
349 .map(|p| p.dn(self.wl0).signum() * p.dn(wl) * p.thickness * 1e3 / wl)
350 .collect()
351 }
352
353 pub fn jones_at(&self, wl: f64) -> [num_complex::Complex64; 4] {
355 use num_complex::Complex64 as C;
356 if !self.plates.is_empty() {
357 let tau = 2.0 * std::f64::consts::PI;
358 let mut m = axis_matrix(C::new(self.tx, 0.0), C::new(self.ty, 0.0), self.axis_deg);
359 for (p, r) in self.plates.iter().zip(self.plate_retardance(wl)) {
360 let q = axis_matrix(
361 C::from_polar(1.0, tau * r / 2.0),
362 C::from_polar(1.0, -tau * r / 2.0),
363 self.axis_deg + p.axis_deg,
364 );
365 m = mul2(&q, &m);
366 }
367 return m;
368 }
369 let phi = if self.retardance == 0.0 && self.order == 0 {
370 0.0
371 } else {
372 2.0 * std::f64::consts::PI * (self.order as f64 + self.retardance) * self.wl0 / wl
373 };
374 axis_matrix(
375 C::from_polar(self.tx, phi / 2.0),
376 C::from_polar(self.ty, -phi / 2.0),
377 self.axis_deg,
378 )
379 }
380 pub fn jones8(&self, wl: f64) -> [f64; 8] {
382 let m = self.jones_at(wl);
383 [
384 m[0].re, m[0].im, m[1].re, m[1].im, m[2].re, m[2].im, m[3].re, m[3].im,
385 ]
386 }
387}
388
389#[derive(serde::Serialize, serde::Deserialize, schemars::JsonSchema, Debug, Clone, PartialEq)]
398pub enum Bire {
399 In {
400 mode: u8,
403 axis: [f64; 3],
405 #[serde(default)]
407 ne: Vec<f64>,
408 },
409 Out,
410}
411
412impl Bire {
413 pub fn e_name(mat: &str) -> String {
415 format!("{}-E", mat.trim())
416 }
417}
418
419#[derive(serde::Serialize, serde::Deserialize, schemars::JsonSchema, Debug, Clone, PartialEq)]
421pub struct Reflect {
422 pub mat: String,
424 pub n: Vec<f64>,
426}
427
428impl Default for Surface {
429 fn default() -> Self {
430 Surface {
431 form: Form::Standard,
432 ignore: false,
433 t: 0.0,
434 comment: String::new(),
435 mat: Material::Air,
436 coat: String::new(),
437 stop: false,
438 semi: 0.0,
439 semi_auto: false,
440 c: 0.0,
441 k: 0.0,
442 before: None,
443 after: After::None,
444 ap: Aperture::None,
445 dn: 0.0,
446 disp: 0.0,
447 dnd: 0.0,
448 dvd: 0.0,
449 reflect: None,
450 retarder: None,
451 bire: None,
452 tce: 0.0,
453 mech_semi: None,
454 coord_return: None,
455 chip_zone: 0.0,
456 }
457 }
458}
459
460pub fn medium_edges(surfaces: &[Surface]) -> Vec<bool> {
465 let same = |a: &Material, b: &Material| match (a, b) {
466 (Material::Glass(x), Material::Glass(y)) => x.eq_ignore_ascii_case(y),
467 _ => a == b,
468 };
469 let mut before = Material::Air;
470 let mut out = Vec::with_capacity(surfaces.len());
471 for (k, d) in surfaces.iter().enumerate() {
472 let after = match &d.mat {
473 Material::Mirror | Material::Inherit => before.clone(),
474 m => m.clone(),
475 };
476 out.push(
477 k > 0
478 && match &d.mat {
479 Material::Mirror => !before.is_air(),
480 Material::Inherit => false,
481 _ => !same(&before, &after),
482 },
483 );
484 before = after;
485 }
486 out
487}
488
489impl Surface {
490 pub fn is_mirror(&self) -> bool {
492 self.mat.is_mirror()
493 }
494
495 pub fn has_asph(&self) -> bool {
496 self.asph().iter().any(|&a| a != 0.0)
497 }
498}