Skip to main content

lensbench_core/
action.rs

1//! 面の働き([`Action`]): 光線の方向・光路長・偏光を面でどう変えるか。
2//!
3//! [`Surface::action`] は今の平たい [`Surface`] の値から働きを借りて作る見方で、形([`crate::shape`])と組で
4//! 面の種類を表す。座標ブレークは面でないので [`Surface::is_coord_break`] で先に分ける。
5//! 面の後ろの屈折率分布(GRIN)は面の働きでなく、面の後ろの媒質に属する([`Surface::grin`])
6
7use crate::math::*;
8use crate::shape::sag;
9use crate::surface::{Binary2, Bire, CylFresnel, Reflect, Retarder, SurfType, Surface};
10use crate::usersurf::UserSurf;
11
12/// Jones の面の行列の元
13#[derive(Debug, Clone, Copy, PartialEq)]
14pub enum JonesSrc<'a> {
15    /// 波長によらない行列 [Ar, Ai, Br, Bi, Cr, Ci, Dr, Di]
16    Matrix(&'a [f64; 8]),
17    /// 波長ごとに作る偏光子・波長板
18    Retarder(&'a Retarder),
19}
20
21impl JonesSrc<'_> {
22    /// 波長 `wl` [µm] の行列 [Ar, Ai, Br, Bi, Cr, Ci, Dr, Di]
23    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/// Fresnel の面の屈折を決める面(交点は形 [`crate::shape::Shape`] で求め、屈折はこの面の傾きで)
32#[derive(Debug, Clone, Copy, PartialEq)]
33pub enum FresnelBend<'a> {
34    /// Fresnel(FRESNELS): 偶数次非球面(c・k・r², r⁴, … の係数 a)
35    Radial { c: f64, k: f64, a: &'a [f64] },
36    /// Cylinder Fresnel(CFRESNEL): y だけの面(無ければ曲げない)
37    Cyl(Option<&'a CylFresnel>),
38}
39
40impl FresnelBend<'_> {
41    /// 局所座標 (x, y) での屈折の面の法線(規格化前)
42    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    /// 近軸の曲率(YZ 面)。Cylinder Fresnel の屈折の面が無ければ None(基板の形で)
57    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/// 面の働き([`Surface::action`])。鏡([`Surface::mirror`])の Binary 2・Fresnel は Binary2・Fresnel のまま
67/// (反射してから位相・傾きを加える)
68#[derive(Debug, Clone, Copy, PartialEq)]
69pub enum Action<'a> {
70    /// 屈折(前後の媒質の屈折率で)
71    Refract,
72    /// 反射(鏡)。`ghost` は屈折面での反射(ゴーストの経路の解析の写しだけ)の、光が入らない側の媒質
73    Reflect { ghost: Option<&'a Reflect> },
74    /// 理想レンズ(焦点距離 f、OPD の求め方 opd_mode)
75    Paraxial { f: f64, opd_mode: i32 },
76    /// Jones 行列(方向は変えず、電場に行列をかける)
77    Jones(JonesSrc<'a>),
78    /// 屈折・反射の後に位相 Φ で回折する(Binary 2)
79    Binary2(Option<&'a Binary2>),
80    /// 屈折を別の面の傾きで決める(Fresnel・Cylinder Fresnel)
81    Fresnel(FresnelBend<'a>),
82    /// 複屈折材の板の入口・出口(屈折は常光で、偏光の位相を板の中の道のりで)
83    Birefringent(&'a Bire),
84    /// 利用者定義の面(登録のモデル)
85    User(Option<&'a UserSurf>),
86}
87
88impl Surface {
89    /// 面の働き(今の値から作る見方)。座標ブレークは面でないので [`Surface::is_coord_break`] で先に分ける
90    /// (ここでは [`Action::Refract`] を返すが、屈折率は前の値を引き継ぐ)
91    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    /// 形を持たず、方向を直接変える面(理想レンズ・利用者定義の面)
126    pub fn is_ideal(&self) -> bool {
127        matches!(self, Action::Paraxial { .. } | Action::User(_))
128    }
129}
130
131/// 追跡の内側で面ごとに分ける働き([`crate::shape::SurfKind`])
132#[derive(Debug, Clone, Copy, PartialEq, Eq)]
133pub(crate) enum Act {
134    /// 屈折・反射(Fresnel・複屈折材の板も)
135    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}