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
use crate::consts::{NUM_TRANS_IN_UNIT, NUM_TRANS_X, NUM_TRANS_Y, TRANS_SIZE};
use crate::{Matrix4x4, Quaternion, UnitQuaternion, Vector3, Vector4};
struct Device {
device_id: usize,
global_trans_positions: Vec<Vector3>,
x_direction: Vector3,
y_direction: Vector3,
z_direction: Vector3,
}
impl Device {
pub fn new(device_id: usize, position: Vector3, rotation: UnitQuaternion) -> Device {
let rot_mat: Matrix4x4 = From::from(rotation);
let trans_mat = rot_mat.append_translation(&position);
let x_direction = Self::get_direction(Vector3::x(), rotation);
let y_direction = Self::get_direction(Vector3::y(), rotation);
let z_direction = Self::get_direction(Vector3::z(), rotation);
let mut global_trans_positions = Vec::with_capacity(NUM_TRANS_IN_UNIT);
for y in 0..NUM_TRANS_Y {
for x in 0..NUM_TRANS_X {
if !crate::is_missing_transducer(x, y) {
let local_pos =
Vector4::new(x as f64 * TRANS_SIZE, y as f64 * TRANS_SIZE, 0., 1.);
global_trans_positions.push(crate::convert_to_vec3(trans_mat * local_pos));
}
}
}
Device {
device_id,
global_trans_positions,
x_direction,
y_direction,
z_direction,
}
}
fn get_direction(dir: Vector3, rotation: UnitQuaternion) -> Vector3 {
let dir: UnitQuaternion = UnitQuaternion::from_quaternion(Quaternion::from_imag(dir));
(rotation * dir * rotation.conjugate()).imag().normalize()
}
}
#[derive(Default)]
pub struct Geometry {
devices: Vec<Device>,
}
impl Geometry {
pub fn add_device(&mut self, position: Vector3, euler_angles: Vector3) -> usize {
let q = UnitQuaternion::from_axis_angle(&Vector3::z_axis(), euler_angles.x)
* UnitQuaternion::from_axis_angle(&Vector3::y_axis(), euler_angles.y)
* UnitQuaternion::from_axis_angle(&Vector3::z_axis(), euler_angles.z);
self.add_device_quaternion(position, q)
}
pub fn add_device_quaternion(&mut self, position: Vector3, rotation: UnitQuaternion) -> usize {
let device_id = self.devices.len();
self.devices
.push(Device::new(device_id, position, rotation));
device_id
}
pub fn del_device(&mut self, device_id: usize) {
let mut index = 0;
for (i, dev) in self.devices.iter().enumerate() {
if dev.device_id == device_id {
index = i;
break;
}
}
self.devices.remove(index);
}
pub fn num_devices(&self) -> usize {
self.devices.len()
}
pub fn position(&self, transducer_id: usize) -> Vector3 {
let local_trans_id = transducer_id % NUM_TRANS_IN_UNIT;
let device = self.device(transducer_id);
device.global_trans_positions[local_trans_id]
}
pub fn local_position(&self, device_id: usize, global_position: Vector3) -> Vector3 {
let device = &self.devices[device_id];
let local_origin = device.global_trans_positions[0];
let x_dir = device.x_direction;
let y_dir = device.y_direction;
let z_dir = device.z_direction;
let rv = global_position - local_origin;
Vector3::new(rv.dot(&x_dir), rv.dot(&y_dir), rv.dot(&z_dir))
}
pub fn direction(&self, transducer_id: usize) -> Vector3 {
let device = self.device(transducer_id);
device.z_direction
}
fn device(&self, transducer_id: usize) -> &Device {
let eid = transducer_id / NUM_TRANS_IN_UNIT;
&self.devices[eid]
}
}