sightloom_core/
detection.rs1use crate::{ClassId, CoreError, Rect, TrackId};
4
5#[derive(Clone, Copy, Debug, Default, PartialEq)]
7pub struct Detection {
8 bbox: Rect,
9 score: f32,
10 class_id: Option<ClassId>,
11 track_id: Option<TrackId>,
12}
13
14impl Detection {
15 pub fn new(
23 bbox: Rect,
24 score: f32,
25 class_id: Option<ClassId>,
26 track_id: Option<TrackId>,
27 ) -> Result<Self, CoreError> {
28 if !score.is_finite() {
29 return Err(CoreError::NonFinite);
30 }
31
32 Ok(Self {
33 bbox,
34 score,
35 class_id,
36 track_id,
37 })
38 }
39
40 #[must_use]
42 pub const fn bbox(self) -> Rect {
43 self.bbox
44 }
45
46 #[must_use]
48 pub const fn score(self) -> f32 {
49 self.score
50 }
51
52 #[must_use]
54 pub const fn class_id(self) -> Option<ClassId> {
55 self.class_id
56 }
57
58 #[must_use]
60 pub const fn track_id(self) -> Option<TrackId> {
61 self.track_id
62 }
63}
64
65#[derive(Debug)]
67pub struct DetectionBatch<'a> {
68 storage: &'a mut [Detection],
69 len: usize,
70}
71
72impl<'a> DetectionBatch<'a> {
73 #[must_use]
75 pub fn new(storage: &'a mut [Detection]) -> Self {
76 Self { storage, len: 0 }
77 }
78
79 #[must_use]
81 pub fn from_filled(storage: &'a mut [Detection]) -> Self {
82 let len = storage.len();
83 Self { storage, len }
84 }
85
86 pub fn push(&mut self, detection: Detection) -> Result<(), CoreError> {
93 let slot = self
94 .storage
95 .get_mut(self.len)
96 .ok_or(CoreError::InsufficientCapacity)?;
97 *slot = detection;
98 self.len += 1;
99 Ok(())
100 }
101
102 #[must_use]
104 pub fn as_slice(&self) -> &[Detection] {
105 &self.storage[..self.len]
106 }
107}