lensbench_core/coating/format/
dat.rs1use super::super::*;
4
5#[derive(Debug, Clone, PartialEq)]
6pub struct Layer {
7 pub mat: String,
8 pub d: f64,
9 pub absolute: bool,
11}
12
13pub type Table = Vec<(f64, Vec<[f64; 9]>)>;
15
16#[derive(Debug, Clone, Default)]
18pub struct CoatingFile {
19 pub mate: HashMap<String, Vec<[f64; 3]>>,
21 pub coat: HashMap<String, Vec<Layer>>,
22 pub table: HashMap<String, Table>,
23}
24
25pub(in crate::coating) fn num(s: &str, name: &str) -> Result<f64> {
26 s.parse::<f64>()
27 .map_err(|_| Error::Invalid(format!("coating file: {name} number {s}")))
28}
29
30impl CoatingFile {
31 pub fn load(path: &Path) -> Result<CoatingFile> {
32 let txt =
33 glass::read_text(path).map_err(|e| Error::Io(format!("{}: {e}", path.display())))?;
34 Self::parse(&txt)
35 }
36
37 pub fn parse(txt: &str) -> Result<CoatingFile> {
38 enum Cur {
39 None,
40 Mate(String),
41 Coat(String),
42 Table(String, Option<f64>),
43 }
44 let mut f = CoatingFile::default();
45 let mut tables: HashMap<String, Table> = HashMap::new();
46 let mut cur = Cur::None;
47 for line in txt.lines() {
48 let t: Vec<&str> = line.split_whitespace().collect();
49 if t.is_empty() || t[0].starts_with('!') {
50 continue;
51 }
52 match t[0] {
53 "IDEAL" | "IDEAL2" | "ENCRYPTED" | "TAPR" => cur = Cur::None,
55 "MATE" | "COAT" | "TABLE" if t.len() >= 2 => {
56 let name = t[1].to_string();
57 cur = match t[0] {
58 "MATE" => {
59 f.mate.insert(name.clone(), Vec::new());
60 Cur::Mate(name)
61 }
62 "COAT" => {
63 f.coat.insert(name.clone(), Vec::new());
64 Cur::Coat(name)
65 }
66 _ => {
67 tables.insert(name.clone(), Vec::new());
68 Cur::Table(name, None)
69 }
70 };
71 }
72 _ => {
73 match &mut cur {
74 Cur::None => {}
75 Cur::Mate(name) => {
76 if t.len() < 2 {
77 invalid!("coating file: material {name} line {line}");
78 }
79 let k = if t.len() > 2 { num(t[2], name)? } else { 0.0 };
80 let row = [num(t[0], name)?, num(t[1], name)?, k];
81 f.mate
82 .get_mut(name.as_str())
83 .expect("table created above")
84 .push(row);
85 }
86 Cur::Coat(name) => {
87 if t.len() < 2 {
88 invalid!("coating file: coating {name} line {line}");
89 }
90 let flag = if t.len() > 2 {
91 num(t[2], name)? as i64
92 } else {
93 0
94 };
95 let l = Layer {
96 mat: t[0].to_string(),
97 d: num(t[1], name)?,
98 absolute: flag == 1,
99 };
100 f.coat
101 .get_mut(name.as_str())
102 .expect("table created above")
103 .push(l);
104 }
105 Cur::Table(name, ang) => {
106 match t[0] {
107 "ANGL" if t.len() >= 2 => {
108 let a = num(t[1], name)?;
109 let v =
110 tables.get_mut(name.as_str()).expect("table created above");
111 v.retain(|(b, _)| *b != a);
112 v.push((a, Vec::new()));
113 *ang = Some(a);
114 }
115 "WAVE" => {
116 let Some(a) = *ang else {
117 invalid!("coating file: WAVE before the first ANGL of table {name}");
118 };
119 if t.len() < 6 {
121 invalid!("coating file: table {name} line {line}");
122 }
123 let mut row = [0.0; 9];
124 for i in 0..(t.len() - 1).min(9) {
125 row[i] = num(t[i + 1], name)?;
126 }
127 let v =
128 tables.get_mut(name.as_str()).expect("table created above");
129 v.iter_mut()
130 .find(|(b, _)| *b == a)
131 .expect("row created above")
132 .1
133 .push(row);
134 }
135 _ => {}
136 }
137 }
138 }
139 }
140 }
141 }
142 for (k, mut v) in tables {
143 v.sort_by(|a, b| a.0.total_cmp(&b.0));
144 f.table.insert(k, v);
145 }
146 Ok(f)
147 }
148}