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
use core::cmp;
use crate::reception::exchange::mobile;
use crate::reception::exchange::mobile::Mobile;
use crate::reception::exchange::perceived_object::PerceivedObject;
use crate::reception::exchange::reference_position::ReferencePosition;
#[derive(Debug)]
pub struct MobilePerceivedObject {
pub perceived_object: PerceivedObject,
pub reference_position: ReferencePosition,
}
impl MobilePerceivedObject {
pub(crate) fn new(
perceived_object: PerceivedObject,
cpm_position: &ReferencePosition,
cpm_heading: u16,
) -> Self {
let computed_reference_position = compute_position(
perceived_object.distance.x_distance,
perceived_object.distance.y_distance,
cpm_position,
cpm_heading,
);
Self {
perceived_object,
reference_position: computed_reference_position,
}
}
}
impl Mobile for MobilePerceivedObject {
fn mobile_id(&self) -> u32 {
self.perceived_object.object_id as u32
}
fn position(&self) -> &ReferencePosition {
&self.reference_position
}
fn speed(&self) -> Option<u16> {
None
}
fn heading(&self) -> Option<u16> {
None
}
}
impl cmp::PartialEq for MobilePerceivedObject {
fn eq(&self, other: &Self) -> bool {
self.perceived_object == other.perceived_object
&& self.reference_position == other.reference_position
}
}
fn compute_position(
x_distance: i32,
y_distance: i32,
cpm_position: &ReferencePosition,
cpm_heading: u16,
) -> ReferencePosition {
let x_offset_meters = x_distance as f64 / 100.0;
let y_offset_meters = y_distance as f64 / 100.0;
let heading_in_degrees = mobile::heading_in_degrees(cpm_heading);
return cpm_position
.get_destination(x_offset_meters, heading_in_degrees)
.get_destination(y_offset_meters, (heading_in_degrees - 90.0 + 360.0) % 360.0);
}
#[cfg(test)]
mod tests {
use crate::reception::exchange::mobile_perceived_object::{
compute_position, MobilePerceivedObject,
};
use crate::reception::exchange::perceived_object::{Distance, PerceivedObject};
use crate::reception::exchange::reference_position::ReferencePosition;
#[test]
fn it_can_compute_a_position() {
assert_eq!(
compute_position(
50000,
0,
&ReferencePosition {
latitude: 434667520,
longitude: 1205862,
altitude: 220000,
},
1800,
),
ReferencePosition {
latitude: 434622516,
longitude: 1205862,
altitude: 220000,
}
);
assert_eq!(
compute_position(
0,
10000,
&ReferencePosition {
latitude: 434667520,
longitude: 1205862,
altitude: 220000,
},
1800,
),
ReferencePosition {
latitude: 434667520,
longitude: 1218219,
altitude: 220000,
}
);
}
#[test]
fn create_a_new() {
assert_eq!(
MobilePerceivedObject::new(
PerceivedObject {
object_id: 1,
distance: Distance {
x_distance: 50000,
y_distance: 10000,
},
..Default::default()
},
&ReferencePosition {
latitude: 434667520,
longitude: 1205862,
altitude: 220000,
},
1800,
),
MobilePerceivedObject {
perceived_object: PerceivedObject {
object_id: 1,
distance: Distance {
x_distance: 50000,
y_distance: 10000,
},
..Default::default()
},
reference_position: ReferencePosition {
latitude: 434622516,
longitude: 1218218,
altitude: 220000,
},
}
);
}
}