cherry_rs/specs/
gaps.rs

1use serde::{Deserialize, Serialize};
2
3use crate::core::Float;
4
5/// Specifies a gap in a sequential optical system model.
6#[derive(Serialize, Deserialize, Debug)]
7pub struct GapSpec {
8    pub thickness: Float,
9    pub refractive_index: RefractiveIndexSpec,
10}
11
12/// Specifies the refractive index of the material constituting a gap.
13#[derive(Serialize, Deserialize, Debug, Clone)]
14pub struct RefractiveIndexSpec {
15    pub real: RealSpec,
16    pub imag: Option<ImagSpec>,
17}
18
19/// Creates a real refractive index spec.
20#[macro_export]
21macro_rules! n {
22    ($n:expr) => {
23        RefractiveIndexSpec {
24            real: RealSpec::Constant($n),
25            imag: None,
26        }
27    };
28    () => {};
29}
30
31/// Specifies the real part of a refractive index.
32#[derive(Serialize, Deserialize, Debug, Clone)]
33pub enum RealSpec {
34    Constant(Float),
35    TabulatedN {
36        data: Vec<[Float; 2]>,
37    },
38    Formula1 {
39        wavelength_range: [Float; 2],
40        coefficients: Vec<Float>,
41    },
42    Formula2 {
43        wavelength_range: [Float; 2],
44        coefficients: Vec<Float>,
45    },
46    Formula3 {
47        wavelength_range: [Float; 2],
48        coefficients: Vec<Float>,
49    },
50    Formula4 {
51        wavelength_range: [Float; 2],
52        coefficients: Vec<Float>,
53    },
54    Formula5 {
55        wavelength_range: [Float; 2],
56        coefficients: Vec<Float>,
57    },
58    Formula6 {
59        wavelength_range: [Float; 2],
60        coefficients: Vec<Float>,
61    },
62    Formula7 {
63        wavelength_range: [Float; 2],
64        coefficients: Vec<Float>,
65    },
66    Formula8 {
67        wavelength_range: [Float; 2],
68        coefficients: Vec<Float>,
69    },
70    Formula9 {
71        wavelength_range: [Float; 2],
72        coefficients: Vec<Float>,
73    },
74}
75
76/// Specifies the imaginary part of a refractive index.
77#[derive(Serialize, Deserialize, Debug, Clone)]
78pub enum ImagSpec {
79    Constant(Float),
80    TabulatedK { data: Vec<[Float; 2]> },
81}
82
83impl GapSpec {
84    pub fn from_thickness_and_real_refractive_index(thickness: Float, n: Float) -> Self {
85        let refractive_index = RefractiveIndexSpec {
86            real: RealSpec::Constant(n),
87            imag: None,
88        };
89        Self {
90            thickness,
91            refractive_index,
92        }
93    }
94}
95
96impl RefractiveIndexSpec {
97    pub fn depends_on_wavelength(&self) -> bool {
98        !self.is_constant()
99    }
100
101    pub fn is_constant(&self) -> bool {
102        let is_real_part_const = matches!(&self.real, RealSpec::Constant(_));
103        let is_imag_part_const = matches!(&self.imag, Some(ImagSpec::Constant(_)) | None);
104
105        is_real_part_const && is_imag_part_const
106    }
107
108    pub fn from_real_refractive_index(n: Float) -> Self {
109        Self {
110            real: RealSpec::Constant(n),
111            imag: None,
112        }
113    }
114}