1use ndarray::Array2;
29
30pub mod accel;
31pub mod analytics;
32pub mod augment;
33pub mod bias;
34pub mod camera;
35pub mod cluster;
36pub mod cmax;
37#[cfg(feature = "camera")]
38pub mod device;
39pub mod feast;
40pub mod features;
41pub mod filter;
42pub mod flow;
43pub mod image;
44pub mod interp;
45pub mod io;
46pub mod mask;
47#[cfg(feature = "onnx")]
49pub mod model;
50pub mod net;
51#[cfg(feature = "ros2")]
52pub mod ros2;
53
54pub mod representation;
55pub mod simulate;
56pub mod track;
57pub mod transform;
58pub mod video;
59pub mod viz;
60
61const COLUMN_COUNT: usize = 4;
62
63#[derive(Clone, Debug)]
74pub struct EventStream {
75 xs: Vec<u16>,
76 ys: Vec<u16>,
77 ts: Vec<i64>,
78 ps: Vec<bool>,
79 width: usize,
80 height: usize,
81 timestamp_scale_ms: f64,
82}
83
84#[derive(Clone, Copy, Debug, PartialEq, Eq)]
85pub struct Event {
86 pub x: usize,
87 pub y: usize,
88 pub timestamp: u64,
89 pub polarity: bool,
90}
91
92impl EventStream {
93 pub fn len(&self) -> usize {
94 self.xs.len()
95 }
96
97 pub fn is_empty(&self) -> bool {
98 self.xs.is_empty()
99 }
100
101 pub fn sensor_size(&self) -> (usize, usize) {
102 (self.width, self.height)
103 }
104
105 pub fn timestamp_scale_ms(&self) -> f64 {
106 self.timestamp_scale_ms
107 }
108
109 pub fn xs(&self) -> &[u16] {
110 &self.xs
111 }
112
113 pub fn ys(&self) -> &[u16] {
114 &self.ys
115 }
116
117 pub fn ts(&self) -> &[i64] {
118 &self.ts
119 }
120
121 pub fn ps(&self) -> &[bool] {
122 &self.ps
123 }
124
125 pub fn iter(&self) -> impl Iterator<Item = Event> + '_ {
126 let (xs, ys, ts, ps) = (self.xs(), self.ys(), self.ts(), self.ps());
129 (0..self.len()).map(move |index| Event {
130 x: xs[index] as usize,
131 y: ys[index] as usize,
132 timestamp: ts[index] as u64,
133 polarity: ps[index],
134 })
135 }
136
137 pub fn to_array2(&self) -> Array2<u64> {
139 let mut values = Vec::with_capacity(self.len() * COLUMN_COUNT);
140 for index in 0..self.len() {
141 values.push(u64::from(self.xs[index]));
142 values.push(u64::from(self.ys[index]));
143 values.push(self.ts[index] as u64);
144 values.push(u64::from(self.ps[index]));
145 }
146 Array2::from_shape_vec((self.len(), COLUMN_COUNT), values)
147 .expect("columns share a length by construction")
148 }
149
150 #[cfg(test)]
153 pub(crate) fn from_array2(
154 events: Array2<u64>,
155 width: usize,
156 height: usize,
157 timestamp_scale_ms: f64,
158 ) -> Self {
159 Self {
160 xs: events.column(0).iter().map(|&value| value as u16).collect(),
161 ys: events.column(1).iter().map(|&value| value as u16).collect(),
162 ts: events.column(2).iter().map(|&value| value as i64).collect(),
163 ps: events.column(3).iter().map(|&value| value != 0).collect(),
164 width,
165 height,
166 timestamp_scale_ms,
167 }
168 }
169}
170
171#[derive(Clone, Debug)]
174pub struct EventStreamBuilder {
175 xs: Vec<u16>,
176 ys: Vec<u16>,
177 ts: Vec<i64>,
178 ps: Vec<bool>,
179 width: usize,
180 height: usize,
181 timestamp_scale_ms: f64,
182}
183
184impl EventStreamBuilder {
185 pub fn new(width: usize, height: usize, timestamp_scale_ms: f64) -> Self {
186 Self::with_capacity(width, height, timestamp_scale_ms, 0)
187 }
188
189 pub fn with_capacity(
190 width: usize,
191 height: usize,
192 timestamp_scale_ms: f64,
193 capacity: usize,
194 ) -> Self {
195 Self {
196 xs: Vec::with_capacity(capacity),
197 ys: Vec::with_capacity(capacity),
198 ts: Vec::with_capacity(capacity),
199 ps: Vec::with_capacity(capacity),
200 width,
201 height,
202 timestamp_scale_ms,
203 }
204 }
205
206 pub fn push(&mut self, x: u16, y: u16, timestamp: i64, polarity: bool) -> bool {
209 if usize::from(x) >= self.width || usize::from(y) >= self.height {
210 return false;
211 }
212 self.xs.push(x);
213 self.ys.push(y);
214 self.ts.push(timestamp);
215 self.ps.push(polarity);
216 true
217 }
218
219 #[inline]
229 pub(crate) fn push_in_bounds(&mut self, x: u16, y: u16, timestamp: i64, polarity: bool) {
230 self.xs.push(x);
231 self.ys.push(y);
232 self.ts.push(timestamp);
233 self.ps.push(polarity);
234 }
235
236 pub fn extend_from_stream(&mut self, stream: &EventStream) {
245 let fits = stream
246 .xs()
247 .iter()
248 .zip(stream.ys())
249 .all(|(&x, &y)| usize::from(x) < self.width && usize::from(y) < self.height);
250 if fits {
251 self.extend_from_columns(stream.xs(), stream.ys(), stream.ts(), stream.ps());
252 return;
253 }
254 for index in 0..stream.len() {
255 self.push(
256 stream.xs()[index],
257 stream.ys()[index],
258 stream.ts()[index],
259 stream.ps()[index],
260 );
261 }
262 }
263
264 pub(crate) fn extend_from_columns(&mut self, xs: &[u16], ys: &[u16], ts: &[i64], ps: &[bool]) {
269 self.xs.extend_from_slice(xs);
270 self.ys.extend_from_slice(ys);
271 self.ts.extend_from_slice(ts);
272 self.ps.extend_from_slice(ps);
273 }
274
275 pub fn reserve(&mut self, additional: usize) {
277 self.xs.reserve(additional);
278 self.ys.reserve(additional);
279 self.ts.reserve(additional);
280 self.ps.reserve(additional);
281 }
282
283 pub fn len(&self) -> usize {
284 self.xs.len()
285 }
286
287 pub fn is_empty(&self) -> bool {
288 self.xs.is_empty()
289 }
290
291 pub fn build(self) -> EventStream {
292 EventStream {
293 xs: self.xs,
294 ys: self.ys,
295 ts: self.ts,
296 ps: self.ps,
297 width: self.width,
298 height: self.height,
299 timestamp_scale_ms: self.timestamp_scale_ms,
300 }
301 }
302}
303
304#[cfg(test)]
305mod tests {
306 use std::path::PathBuf;
307
308 use crate::io::{load, LoadOptions};
309
310 use super::{EventStream, EventStreamBuilder};
311
312 #[test]
313 fn loads_n_imagenet_events() {
314 let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../data/test/example.npz");
315 let stream = load(path, LoadOptions::default()).unwrap();
316 let events = stream.to_array2();
317
318 assert!(!stream.is_empty());
319 assert_eq!(stream.sensor_size(), (640, 480));
320 assert_eq!(events.dim(), (stream.len(), 4));
321 assert!(events.column(0).iter().all(|&x| x < 640));
322 assert!(events.column(1).iter().all(|&y| y < 480));
323 assert!(events.column(3).iter().all(|&polarity| polarity <= 1));
324 }
325
326 #[test]
327 fn builder_drops_out_of_bounds_events_and_keeps_columns_aligned() {
328 let mut builder = EventStreamBuilder::new(4, 3, 0.001);
329
330 assert!(builder.push(1, 2, 10, true));
331 assert!(!builder.push(4, 0, 20, false)); assert!(!builder.push(0, 3, 30, true)); assert!(builder.push(3, 0, 40, false));
334
335 let stream = builder.build();
336 assert_eq!(stream.len(), 2);
337 assert_eq!(stream.xs(), &[1, 3]);
338 assert_eq!(stream.ys(), &[2, 0]);
339 assert_eq!(stream.ts(), &[10, 40]);
340 assert_eq!(stream.ps(), &[true, false]);
341 assert_eq!(stream.sensor_size(), (4, 3));
342 }
343
344 #[test]
345 fn to_array2_round_trips_columns_in_xytp_order() {
346 let stream =
347 EventStream::from_array2(ndarray::array![[1, 2, 100, 1], [3, 0, 250, 0]], 4, 3, 0.001);
348 let events = stream.to_array2();
349
350 assert_eq!(events.dim(), (2, 4));
351 assert_eq!(events.row(0).to_vec(), vec![1, 2, 100, 1]);
352 assert_eq!(events.row(1).to_vec(), vec![3, 0, 250, 0]);
353 }
354}