Skip to main content

eventcv_core/representation/
binary.rs

1use super::{
2    event_index, frame_len, EventFrame, EventFrameData, Representation, RepresentationError,
3    RepresentationKind,
4};
5use crate::EventStream;
6
7#[derive(Clone, Copy, Debug, Default)]
8pub struct Binary;
9
10impl Representation for Binary {
11    type Output = EventFrame;
12
13    fn generate(&self, stream: &EventStream) -> Result<EventFrame, RepresentationError> {
14        let (width, height, length) = frame_len(stream, 1)?;
15        let mut occupancy = vec![0_u8; length];
16        for event in stream.iter() {
17            occupancy[event_index(event, width, height)?] = 1;
18        }
19
20        Ok(EventFrame {
21            data: EventFrameData::U8(occupancy),
22            channels: 1,
23            width,
24            height,
25            kind: RepresentationKind::Binary,
26            channel_names: vec!["event".to_owned()],
27        })
28    }
29}
30
31#[cfg(test)]
32mod tests {
33    use ndarray::array;
34
35    use super::{Binary, Representation};
36    use crate::{representation::EventFrameData, EventStream};
37
38    #[test]
39    fn records_single_channel_occupancy() {
40        let stream = EventStream::from_array2(
41            array![[0, 0, 1, 1], [0, 0, 2, 0], [1, 1, 3, 1]],
42            2,
43            2,
44            0.001,
45        );
46
47        let frame = Binary.generate(&stream).unwrap();
48
49        assert_eq!(frame.shape(), (1, 2, 2));
50        assert_eq!(frame.data(), &EventFrameData::U8(vec![1, 0, 0, 1]));
51    }
52}