1pub fn ashrae(aoi: f64, b0: f64) -> f64 {
12 let aoi_rad = aoi.to_radians();
13 if aoi_rad >= std::f64::consts::PI / 2.0 {
14 return 0.0;
15 }
16 let cos_aoi = aoi_rad.cos();
17 if cos_aoi <= 0.0 {
18 return 0.0;
19 }
20
21 let iam = 1.0 - b0 * (1.0 / cos_aoi - 1.0);
22 iam.clamp(0.0, 1.0)
23}
24
25pub fn martin_ruiz(aoi: f64, a_r: f64) -> f64 {
34 if aoi >= 90.0 {
35 return 0.0;
36 }
37 let aoi_rad = aoi.to_radians();
38 let numerator = 1.0 - (-aoi_rad.cos() / a_r).exp();
40 let denominator = 1.0 - (-1.0 / a_r).exp();
41 if denominator == 0.0 {
42 return 1.0;
43 }
44 (numerator / denominator).clamp(0.0, 1.0)
45}
46
47pub fn physical(aoi: f64, n: f64, k: f64, l: f64) -> f64 {
61 if aoi.is_nan() {
62 return f64::NAN;
63 }
64 if aoi.abs() >= 90.0 {
65 return 0.0;
66 }
67
68 let aoi_rad = aoi.to_radians();
69 let cos_aoi = aoi_rad.cos().max(0.0);
70 let sin_aoi = (1.0 - cos_aoi * cos_aoi).sqrt();
71
72 let sin_refr = sin_aoi / n;
74 let cos_refr = (1.0 - sin_refr * sin_refr).sqrt();
75
76 let n1_cos1 = cos_aoi; let n2_cos1 = n * cos_aoi;
79 let n1_cos2 = cos_refr; let n2_cos2 = n * cos_refr;
81
82 let rho_s = ((n1_cos1 - n2_cos2) / (n1_cos1 + n2_cos2)).powi(2);
83 let rho_p = ((n1_cos2 - n2_cos1) / (n1_cos2 + n2_cos1)).powi(2);
84 let rho_0 = ((1.0 - n) / (1.0 + n)).powi(2);
85
86 let tau_s = 1.0 - rho_s;
88 let tau_p = 1.0 - rho_p;
89 let tau_0 = 1.0 - rho_0;
90
91 let tau_s = tau_s * (-k * l / cos_refr).exp();
93 let tau_p = tau_p * (-k * l / cos_refr).exp();
94 let tau_0 = tau_0 * (-k * l).exp();
95
96 (tau_s + tau_p) / (2.0 * tau_0)
98}
99
100pub fn schlick(aoi: f64) -> f64 {
110 if aoi.abs() >= 90.0 {
111 return 0.0;
112 }
113 let cos_aoi = aoi.to_radians().cos();
114 1.0 - (1.0 - cos_aoi).powi(5)
115}
116
117pub fn schlick_diffuse(surface_tilt: f64) -> (f64, f64) {
128 let cos_b = surface_tilt.to_radians().cos();
129 let sin_b = surface_tilt.to_radians().sin();
130 let beta_rad = surface_tilt.to_radians();
131 let pi = std::f64::consts::PI;
132
133 let cuk = (2.0 / (pi * (1.0 + cos_b)))
135 * ((30.0 / 7.0) * pi - (160.0 / 21.0) * beta_rad - (10.0 / 3.0) * pi * cos_b
136 + (160.0 / 21.0) * cos_b * sin_b
137 - (5.0 / 3.0) * pi * cos_b * sin_b.powi(2)
138 + (20.0 / 7.0) * cos_b * sin_b.powi(3)
139 - (5.0 / 16.0) * pi * cos_b * sin_b.powi(4)
140 + (16.0 / 105.0) * cos_b * sin_b.powi(5));
141
142 let cug = if surface_tilt < 1e-6 {
144 0.0
145 } else {
146 40.0 / (21.0 * (1.0 - cos_b)) - (1.0 + cos_b) / (1.0 - cos_b) * cuk
147 };
148
149 (cuk, cug)
150}
151
152pub fn martin_ruiz_diffuse(surface_tilt: f64, a_r: f64) -> (f64, f64) {
163 let pi = std::f64::consts::PI;
164
165 let tilt = if surface_tilt == 0.0 {
167 1e-6
168 } else if surface_tilt == 180.0 {
169 180.0 - 1e-6
170 } else {
171 surface_tilt
172 };
173
174 let c1 = 4.0 / 3.0 / pi; let c2 = 0.5 * a_r - 0.154;
176
177 let beta = tilt.to_radians();
178 let cos_beta = beta.cos();
179 let sin_beta = if tilt < 90.0 {
180 beta.sin()
181 } else {
182 (pi - beta).sin()
183 };
184
185 let trig_sky = sin_beta + (pi - beta - sin_beta) / (1.0 + cos_beta);
186 let trig_gnd = sin_beta + (beta - sin_beta) / (1.0 - cos_beta);
187
188 let iam_sky = 1.0 - (-(c1 + c2 * trig_sky) * trig_sky / a_r).exp();
189 let iam_gnd = 1.0 - (-(c1 + c2 * trig_gnd) * trig_gnd / a_r).exp();
190
191 (iam_sky, iam_gnd)
192}
193
194#[allow(clippy::too_many_arguments)]
199pub fn sapm(aoi: f64, b0: f64, b1: f64, b2: f64, b3: f64, b4: f64, b5: f64) -> f64 {
200 if !(0.0..=90.0).contains(&aoi) { return 0.0; }
201
202 let iam = b0
203 + b1 * aoi
204 + b2 * aoi.powi(2)
205 + b3 * aoi.powi(3)
206 + b4 * aoi.powi(4)
207 + b5 * aoi.powi(5);
208
209 iam.clamp(0.0, 1.0)
210}