oxihuman_export/
ior_map_export.rs1#![allow(dead_code)]
4
5pub struct IorMap {
6 pub width: u32,
7 pub height: u32,
8 pub data: Vec<f32>,
9}
10
11pub fn new_ior_map(w: u32, h: u32, default_ior: f32) -> IorMap {
12 IorMap {
13 width: w,
14 height: h,
15 data: vec![default_ior; (w * h) as usize],
16 }
17}
18
19pub fn ior_set(m: &mut IorMap, x: u32, y: u32, v: f32) {
20 if x < m.width && y < m.height {
21 m.data[(y * m.width + x) as usize] = v;
22 }
23}
24
25pub fn ior_get(m: &IorMap, x: u32, y: u32) -> f32 {
26 if x < m.width && y < m.height {
27 m.data[(y * m.width + x) as usize]
28 } else {
29 1.0
30 }
31}
32
33pub fn ior_mean(m: &IorMap) -> f32 {
34 if m.data.is_empty() {
35 return 1.0;
36 }
37 m.data.iter().sum::<f32>() / m.data.len() as f32
38}
39
40pub fn ior_is_valid(ior: f32) -> bool {
41 (1.0..=3.0).contains(&ior)
42}
43
44pub fn ior_to_bytes(m: &IorMap) -> Vec<u8> {
45 let mut b = Vec::new();
46 b.extend_from_slice(&m.width.to_le_bytes());
47 b.extend_from_slice(&m.height.to_le_bytes());
48 for &v in &m.data {
49 b.extend_from_slice(&v.to_le_bytes());
50 }
51 b
52}
53
54#[cfg(test)]
55mod tests {
56 use super::*;
57
58 #[test]
59 fn test_new_ior_map() {
60 let m = new_ior_map(4, 4, 1.5);
62 assert!((m.data[0] - 1.5).abs() < 1e-6);
63 }
64
65 #[test]
66 fn test_ior_set_get() {
67 let mut m = new_ior_map(4, 4, 1.0);
69 ior_set(&mut m, 1, 1, 1.8);
70 assert!((ior_get(&m, 1, 1) - 1.8).abs() < 1e-5);
71 }
72
73 #[test]
74 fn test_ior_mean() {
75 let m = new_ior_map(3, 3, 1.5);
77 assert!((ior_mean(&m) - 1.5).abs() < 1e-5);
78 }
79
80 #[test]
81 fn test_ior_is_valid() {
82 assert!(ior_is_valid(1.5));
84 assert!(!ior_is_valid(0.5));
85 assert!(!ior_is_valid(3.5));
86 }
87
88 #[test]
89 fn test_ior_to_bytes() {
90 let m = new_ior_map(2, 2, 1.5);
92 let b = ior_to_bytes(&m);
93 assert!(!b.is_empty());
94 }
95
96 #[test]
97 fn test_ior_get_oob() {
98 let m = new_ior_map(2, 2, 1.5);
100 assert!((ior_get(&m, 99, 99) - 1.0).abs() < 1e-6);
101 }
102}