use eyre::{Result, WrapErr, ensure};
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct BoxPrompt {
cxcywh: [f32; 4],
positive: bool,
}
impl BoxPrompt {
pub fn from_xyxy(xyxy: [f32; 4], width: usize, height: usize, positive: bool) -> Result<Self> {
ensure!(width > 0 && height > 0, "image size {width}x{height} is empty");
ensure!(xyxy.iter().all(|v| v.is_finite()), "box {xyxy:?} has a non-finite coordinate");
let (w, h) = (width as f32, height as f32);
let [x0, y0, x1, y1] = xyxy;
Self::from_cxcywh([(x0 + x1) / 2.0 / w, (y0 + y1) / 2.0 / h, (x1 - x0) / w, (y1 - y0) / h], positive)
.wrap_err_with(|| format!("box {xyxy:?} in a {width}x{height} image"))
}
pub fn from_cxcywh(cxcywh: [f32; 4], positive: bool) -> Result<Self> {
let [cx, cy, w, h] = cxcywh;
ensure!(cxcywh.iter().all(|v| v.is_finite()), "box {cxcywh:?} has a non-finite value");
ensure!(w > 0.0 && h > 0.0, "box {cxcywh:?} has no area");
let corners = [cx - w / 2.0, cy - h / 2.0, cx + w / 2.0, cy + h / 2.0];
ensure!(
corners.iter().all(|c| (-1.0..=2.0).contains(c)),
"box {cxcywh:?} reaches more than one image size past the image"
);
Ok(Self { cxcywh, positive })
}
pub fn cxcywh(&self) -> [f32; 4] {
self.cxcywh
}
pub fn positive(&self) -> bool {
self.positive
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct PointPrompt {
x: f32,
y: f32,
positive: bool,
}
impl PointPrompt {
pub fn new(x: f32, y: f32, positive: bool) -> Result<Self> {
ensure!(x.is_finite() && y.is_finite(), "point ({x}, {y}) has a non-finite coordinate");
Ok(Self { x, y, positive })
}
pub fn x(&self) -> f32 {
self.x
}
pub fn y(&self) -> f32 {
self.y
}
pub fn positive(&self) -> bool {
self.positive
}
}