Skip to main content

sightloom_core/
overlap.rs

1//! Rectangle overlap metrics used by matching and suppression algorithms.
2
3use crate::Rect;
4
5/// Returns the area shared by two rectangles.
6#[must_use]
7pub fn intersection_area(a: Rect, b: Rect) -> f32 {
8    a.intersection(b).area()
9}
10
11/// Returns intersection over union (`IoU`) for two rectangles.
12///
13/// The result is zero when the union has zero area.
14#[must_use]
15pub fn iou(a: Rect, b: Rect) -> f32 {
16    let intersection = intersection_area(a, b);
17    let union = a.area() + b.area() - intersection;
18
19    if union > 0.0 {
20        intersection / union
21    } else {
22        0.0
23    }
24}
25
26/// Returns intersection over the smaller area (`IoS`) for two rectangles.
27///
28/// The result is zero when either rectangle has zero area.
29#[must_use]
30pub fn ios(a: Rect, b: Rect) -> f32 {
31    let smaller = a.area().min(b.area());
32
33    if smaller > 0.0 {
34        intersection_area(a, b) / smaller
35    } else {
36        0.0
37    }
38}