pico_detect/geometry/
target.rs

1use imageproc::rect::Rect;
2use nalgebra::Point2;
3
4use crate::traits::Region;
5
6use super::Square;
7
8/// Represents a target region in an image with a center point and size.
9#[derive(Debug, Clone, Copy, PartialEq)]
10pub struct Target {
11    pub(crate) point: Point2<f32>,
12    pub(crate) size: f32,
13}
14
15impl Target {
16    /// Creates a new target with the specified center point and size.
17    #[inline]
18    pub fn new(x: f32, y: f32, s: f32) -> Self {
19        Self {
20            point: Point2::new(x, y),
21            size: s,
22        }
23    }
24
25    /// Returns size of the target.
26    #[inline]
27    pub fn size(&self) -> f32 {
28        self.size
29    }
30
31    /// Returns the `x` coordinate of the target's center point.
32    #[inline]
33    pub fn x(&self) -> f32 {
34        self.point.x
35    }
36
37    /// Returns the `y` coordinate of the target's center point.
38    #[inline]
39    pub fn y(&self) -> f32 {
40        self.point.y
41    }
42
43    /// Returns the center point of the target.
44    #[inline]
45    pub fn point(&self) -> &Point2<f32> {
46        &self.point
47    }
48}
49
50impl Region for Target {
51    #[inline]
52    fn left(&self) -> i32 {
53        (self.point.x - self.size / 2.0) as i32
54    }
55
56    #[inline]
57    fn top(&self) -> i32 {
58        (self.point.y - self.size / 2.0) as i32
59    }
60
61    #[inline]
62    fn width(&self) -> u32 {
63        self.size as u32
64    }
65
66    #[inline]
67    fn height(&self) -> u32 {
68        self.size as u32
69    }
70
71    #[inline]
72    fn is_square(&self) -> bool {
73        true
74    }
75
76    #[inline]
77    fn center(&self) -> Point2<i32> {
78        Point2::new(self.x() as i32, self.y() as i32)
79    }
80}
81
82impl From<Target> for Rect {
83    #[inline]
84    fn from(value: Target) -> Self {
85        Self::at(value.left(), value.top()).of_size(value.width(), value.height())
86    }
87}
88
89impl From<Target> for Square {
90    #[inline]
91    fn from(value: Target) -> Self {
92        Self::new(value.left(), value.top(), value.size() as u32)
93    }
94}
95
96impl From<Square> for Target {
97    #[inline]
98    fn from(value: Square) -> Self {
99        Self {
100            point: value.center().cast(),
101            size: value.size() as f32,
102        }
103    }
104}