Skip to main content

sightloom_core/
owned.rs

1//! Allocation-backed detection batch conveniences.
2
3use alloc::{vec, vec::Vec};
4
5use crate::{CoreError, Detection, NmsConfig, nms_in_place};
6
7/// A heap-backed batch of validated detections.
8#[derive(Clone, Debug, Default, PartialEq)]
9pub struct OwnedDetectionBatch {
10    detections: Vec<Detection>,
11}
12
13impl OwnedDetectionBatch {
14    /// Creates an empty owned detection batch.
15    #[must_use]
16    pub const fn new() -> Self {
17        Self {
18            detections: Vec::new(),
19        }
20    }
21
22    /// Appends a detection, growing the backing allocation when necessary.
23    pub fn push(&mut self, detection: Detection) {
24        self.detections.push(detection);
25    }
26
27    /// Returns the detections currently in the batch.
28    #[must_use]
29    pub fn as_slice(&self) -> &[Detection] {
30        &self.detections
31    }
32
33    /// Applies non-maximum suppression and removes suppressed detections.
34    ///
35    /// # Errors
36    ///
37    /// Propagates errors from [`nms_in_place`] without modifying this batch.
38    pub fn nms(&mut self, config: NmsConfig) -> Result<usize, CoreError> {
39        let len = self.detections.len();
40        let mut order = vec![0; len];
41        let mut suppressed = vec![false; len];
42        let kept = nms_in_place(
43            self.detections.as_mut_slice(),
44            order.as_mut_slice(),
45            suppressed.as_mut_slice(),
46            config,
47        )?;
48        self.detections.truncate(kept);
49        Ok(kept)
50    }
51}