lensbench_core/glass/
builtin.rs1use super::Glass;
7use std::collections::HashMap;
8use std::path::PathBuf;
9use std::sync::{Arc, OnceLock};
10
11pub type Glasses = Arc<HashMap<String, Glass>>;
13
14pub const FILES: [(&str, &str); 2] = [
16 ("SCHOTT.AGF", include_str!("../../assets/glass/SCHOTT.AGF")),
17 ("OHARA.AGF", include_str!("../../assets/glass/OHARA.AGF")),
18];
19
20pub fn catalogs() -> impl Iterator<Item = &'static str> {
22 FILES.iter().map(|(f, _)| f.trim_end_matches(".AGF"))
23}
24
25pub const SHA256: [&str; 2] = [
28 "sha256:a4d4e7e0bc2802044118e1a08befb4f8d7cdb2385c4d20dcc2660ad6a73ecf20",
29 "sha256:f699cbfe20731c294c36e25053a98f360e17874d80d89168d8658f89137fb600",
30];
31
32fn index(name: &str) -> Option<usize> {
34 FILES
35 .iter()
36 .position(|(f, _)| f.trim_end_matches(".AGF").eq_ignore_ascii_case(name))
37}
38
39pub fn sha256(name: &str) -> Option<&'static str> {
41 index(name).map(|i| SHA256[i])
42}
43
44pub fn catalog(name: &str) -> Option<Glasses> {
47 static C: [OnceLock<Glasses>; FILES.len()] = [const { OnceLock::new() }; FILES.len()];
48 let i = index(name)?;
49 Some(
50 C[i].get_or_init(|| {
51 let (f, t) = FILES[i];
52 let mut g = super::parse_agf(t, f.trim_end_matches(".AGF")).unwrap_or_default();
54 for x in g.values_mut() {
56 x.status = None;
57 }
58 Arc::new(g)
59 })
60 .clone(),
61 )
62}
63
64pub fn used_catalogs(names: &[String], dirs: &[PathBuf]) -> Vec<String> {
66 names
67 .iter()
68 .filter(|n| catalog(n).is_some() && super::find_agf(n, dirs).is_none())
69 .cloned()
70 .collect()
71}
72
73pub fn used_glasses(materials: &[&str], names: &[String], dirs: &[PathBuf]) -> Vec<String> {
76 let from_builtin = used_catalogs(names, dirs);
77 if from_builtin.is_empty() {
78 return Vec::new();
79 }
80 let (user, _) = super::load_catalogs_shared(
82 &names
83 .iter()
84 .filter(|n| !from_builtin.contains(n))
85 .cloned()
86 .collect::<Vec<_>>(),
87 dirs,
88 );
89 let mut out: Vec<String> = Vec::new();
90 for m in materials {
91 let u = m.to_uppercase();
92 let hit = !user.contains_key(&u)
93 && from_builtin
94 .iter()
95 .any(|c| catalog(c).is_some_and(|g| g.contains_key(&u)));
96 if hit && !out.iter().any(|x| x.eq_ignore_ascii_case(m)) {
97 out.push(m.to_string());
98 }
99 }
100 out
101}
102
103pub fn note(materials: &[&str], names: &[String], dirs: &[PathBuf]) -> Option<String> {
105 let used = used_glasses(materials, names, dirs);
106 (!used.is_empty()).then(|| {
107 format!(
108 "glass {} came from the built-in catalog (index only; no internal transmittance or thermal expansion). Pass the maker catalogs with --glass-dir (CLI), glass_dirs (serve, Python) or LENSBENCH_GLASSCAT",
109 used.join(", ")
110 )
111 })
112}
113
114pub fn note_for(s: &crate::Prescription, names: &[String], dirs: &[PathBuf]) -> Option<String> {
116 let mats: Vec<String> = s.surfaces.iter().map(|x| x.mat.name()).collect();
117 let mats: Vec<&str> = mats.iter().map(String::as_str).collect();
118 note(&mats, names, dirs)
119}
120
121#[cfg(test)]
122mod tests {
123 use super::*;
124
125 #[test]
127 fn gux01_builtin_catalogs_parse_and_cover_common_glasses() {
128 for (f, t) in FILES {
129 let g = super::super::parse_agf(t, f.trim_end_matches(".AGF")).unwrap();
130 assert!(g.len() > 100, "{f}: {}", g.len());
131 }
132 let s = catalog("schott").unwrap();
133 let o = catalog("OHARA").unwrap();
134 for n in ["N-BK7", "F2", "N-SK16", "N-LAK9"] {
135 assert!(s.contains_key(n), "{n}");
136 }
137 assert!(o.contains_key("S-FSL5"));
138 assert!(catalog("HOYA").is_none());
139 let n = s["N-BK7"].index(0.5875618, 20.0, 1.0, false);
140 assert!((n - 1.5168).abs() < 2e-5, "{n}");
141 }
142
143 #[test]
145 fn cli31_fallback_and_note() {
146 let none = [PathBuf::from("/lensbench-no-such-glass-dir")];
147 let names = vec!["SCHOTT".to_string(), "OHARA".to_string()];
148 let (g, missing) = super::super::load_catalogs_shared(&names, &none);
149 assert!(missing.is_empty(), "{missing:?}");
150 assert!(g.contains_key("N-BK7") && g.contains_key("S-FSL5"));
151 let (_, missing) = super::super::load_catalogs_shared(&["HOYA".into()], &none);
152 assert_eq!(missing, ["HOYA"]);
153 assert_eq!(used_catalogs(&names, &none), names);
154 assert_eq!(
155 used_glasses(&["N-BK7", "AIR", "n-bk7", ""], &names, &none),
156 ["N-BK7"]
157 );
158 let t = note(&["S-FSL5"], &names, &none).unwrap();
159 assert!(t.contains("S-FSL5") && t.contains("--glass-dir"), "{t}");
160 assert!(note(&["AIR"], &names, &none).is_none());
161 let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../tests/glass");
163 if dir.join("SCHOTT.AGF").is_file() || dir.join("schott.agf").is_file() {
164 assert!(used_glasses(&["N-BK7"], &names, &[dir]).is_empty());
165 }
166 }
167}