1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
use crate::util::geom::{degrees_to_radians, radians_to_degrees};
use property::Property;
use std::{default::Default};
use super::units::IntensityUnits;
#[derive(Debug, Clone)]
pub enum PlaneOrientation {
Vertical,
Horizontal,
}
impl Default for PlaneOrientation {
fn default() -> Self {
PlaneOrientation::Vertical
}
}
#[derive(Debug, Clone)]
pub enum PlaneWidth {
Symmetric(f64),
Asymmetric{ lower: f64, upper: f64}
}
impl PlaneWidth {
pub fn new() -> Self {
PlaneWidth::default()
}
pub fn total(&self) -> f64 {
match *self {
PlaneWidth::Symmetric(width) => width,
PlaneWidth::Asymmetric { ref lower, ref upper } => lower + upper
}
}
pub fn lower(&self) -> f64 {
match *self {
PlaneWidth::Symmetric(width) => width / 2.0,
PlaneWidth::Asymmetric{ lower, upper: _ } => lower,
}
}
pub fn upper(&self) -> f64 {
match *self {
PlaneWidth::Symmetric(width) => width / 2.0,
PlaneWidth::Asymmetric{ lower: _, upper } => upper,
}
}
}
impl Default for PlaneWidth {
fn default() -> Self {
PlaneWidth::Symmetric( 2.0 * std::f64::consts::PI )
}
}
#[derive(Debug, Clone, Default, Property)]
#[property(get(public), set(public))]
pub struct Plane {
angle: f64,
width: PlaneWidth,
orientation: PlaneOrientation,
angles: Vec<f64>,
intensities: Vec<f64>,
units: IntensityUnits,
}
impl Plane {
pub fn new() -> Self {
Self {
..Default::default()
}
}
pub fn set_angle_degrees(&mut self, ang_deg: f64) {
self.set_angle(degrees_to_radians(ang_deg));
}
pub fn set_angles_degrees(&mut self, ang_deg: &Vec<f64>) {
self.set_angles(
ang_deg
.iter()
.map(|ang| degrees_to_radians(*ang))
.collect::<Vec<f64>>(),
);
}
pub fn n_samples(&self) -> usize {
self.angles.iter().count()
}
pub fn angle_deg(&self) -> f64 {
radians_to_degrees(self.angle)
}
pub fn angles_deg(&self) -> Vec<f64> {
self.angles
.iter()
.map(|angle_radians| radians_to_degrees(*angle_radians))
.collect()
}
pub fn delta_angle(&self, i: usize) -> f64 {
match i {
0 => self.angles[1] - self.angles[0],
x if x >= self.angles.iter().count() - 1 => self.angles[i] - self.angles[i - 1],
_ => {
0.5 * ((self.angles[i] - self.angles[i - 1])
+ (self.angles[i + 1] - self.angles[i]))
}
}
}
pub fn integrate_intensity(&self) -> f64 {
self.width.total() * self.intensities
.iter()
.enumerate()
.map(|(i, int)| int * f64::sin(self.angles[i]) * self.delta_angle(i))
.sum::<f64>()
}
}
#[cfg(test)]
mod tests {
use super::{Plane, PlaneWidth};
use approx::assert_abs_diff_eq;
#[test]
fn test_integrate_plane() {
let mut plane = Plane::new();
plane.set_angle(0.0);
plane.set_width(PlaneWidth::Symmetric(1.0));
plane.set_angles_degrees(
&(0..181)
.into_iter()
.map(|ang_i| ang_i as f64)
.collect::<Vec<f64>>(),
);
plane.set_intensities(plane.angles().iter().map(|_| 1.0).collect::<Vec<f64>>());
assert_abs_diff_eq!(plane.integrate_intensity(), 2.0, epsilon = 2.0E-4);
}
}