hotcoco 1.0.0

Perception evaluation in pure Rust — a pycocotools-compatible COCO/LVIS/Open Images engine with diagnostics and dataset tools
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
//! Computational geometry primitives for oriented bounding box (OBB) evaluation.
//!
//! Provides rotated-rectangle mechanics: corner/AABB conversion, polygon area,
//! and Sutherland-Hodgman clipping (exposed to the similarity layer as the
//! crate-internal `obb_intersection_area`).
//! All angles are in radians, counter-clockwise positive.
//!
//! # Mechanics, not formulas
//!
//! The rotated-IoU *formula* lives in [`crate::primitives::sim`]; [`obb_iou`]
//! below is a re-export of it. The re-export is one-way path sugar, never the
//! definition.

pub use crate::primitives::sim::obb_iou;

/// Convert an OBB `[cx, cy, w, h, angle]` to its 4 corner points.
///
/// Returns corners in counter-clockwise order starting from the corner
/// that corresponds to (-w/2, -h/2) before rotation.
pub fn obb_to_corners(obb: &[f64; 5]) -> [(f64, f64); 4] {
    let [cx, cy, w, h, angle] = *obb;
    let cos_a = angle.cos();
    let sin_a = angle.sin();
    let hw = w / 2.0;
    let hh = h / 2.0;

    // Half-extents along each local axis
    let dx_w = hw * cos_a;
    let dy_w = hw * sin_a;
    let dx_h = hh * sin_a;
    let dy_h = hh * cos_a;

    [
        (cx - dx_w + dx_h, cy - dy_w - dy_h), // bottom-left before rotation
        (cx + dx_w + dx_h, cy + dy_w - dy_h), // bottom-right
        (cx + dx_w - dx_h, cy + dy_w + dy_h), // top-right
        (cx - dx_w - dx_h, cy - dy_w + dy_h), // top-left
    ]
}

/// Signed area of a polygon using the shoelace formula.
///
/// Positive for counter-clockwise vertices, negative for clockwise.
fn signed_polygon_area(vertices: &[(f64, f64)]) -> f64 {
    let n = vertices.len();
    if n < 3 {
        return 0.0;
    }
    let mut area = 0.0;
    for i in 0..n {
        let j = (i + 1) % n;
        area += vertices[i].0 * vertices[j].1;
        area -= vertices[j].0 * vertices[i].1;
    }
    area * 0.5
}

/// Absolute area of a polygon, via the shoelace formula.
pub(crate) fn polygon_area(vertices: &[(f64, f64)]) -> f64 {
    signed_polygon_area(vertices).abs()
}

/// Sutherland-Hodgman polygon clipping: clip `subject` against `clip`.
///
/// Both polygons must be convex. The clip polygon's edges define half-planes;
/// the subject polygon is clipped against each in turn. For two rectangles
/// (4 edges each), the result has at most 8 vertices.
fn sutherland_hodgman_clip(subject: &[(f64, f64)], clip: &[(f64, f64)]) -> Vec<(f64, f64)> {
    if subject.is_empty() || clip.is_empty() {
        return Vec::new();
    }

    let mut output = subject.to_vec();

    let clip_len = clip.len();
    for i in 0..clip_len {
        if output.is_empty() {
            return output;
        }

        let edge_start = clip[i];
        let edge_end = clip[(i + 1) % clip_len];

        let input = std::mem::take(&mut output);
        // Reserve capacity for worst case: each edge can add at most 1 vertex
        output.reserve(input.len() + 1);

        let input_len = input.len();
        for j in 0..input_len {
            let current = input[j];
            let previous = input[(j + input_len - 1) % input_len];

            let curr_inside = is_inside(current, edge_start, edge_end);
            let prev_inside = is_inside(previous, edge_start, edge_end);

            if curr_inside {
                if !prev_inside {
                    // Entering: add intersection then current
                    if let Some(pt) = line_intersection(previous, current, edge_start, edge_end) {
                        output.push(pt);
                    }
                }
                output.push(current);
            } else if prev_inside {
                // Leaving: add intersection only
                if let Some(pt) = line_intersection(previous, current, edge_start, edge_end) {
                    output.push(pt);
                }
            }
        }
    }

    output
}

/// Test if a point is on the left side (inside) of a directed edge.
///
/// Uses the cross product of the edge vector and the point vector.
/// Points exactly on the edge are considered inside.
#[inline]
fn is_inside(point: (f64, f64), edge_start: (f64, f64), edge_end: (f64, f64)) -> bool {
    let cross = (edge_end.0 - edge_start.0) * (point.1 - edge_start.1)
        - (edge_end.1 - edge_start.1) * (point.0 - edge_start.0);
    cross >= 0.0
}

/// Compute the intersection point of two line segments.
///
/// Uses the parametric form to find where lines (p1→p2) and (p3→p4) cross.
/// Returns `None` if the lines are parallel (denominator ≈ 0).
#[inline]
fn line_intersection(
    p1: (f64, f64),
    p2: (f64, f64),
    p3: (f64, f64),
    p4: (f64, f64),
) -> Option<(f64, f64)> {
    let d1x = p2.0 - p1.0;
    let d1y = p2.1 - p1.1;
    let d2x = p4.0 - p3.0;
    let d2y = p4.1 - p3.1;

    let denom = d1x * d2y - d1y * d2x;
    if denom.abs() < 1e-12 {
        return None;
    }

    let t = ((p3.0 - p1.0) * d2y - (p3.1 - p1.1) * d2x) / denom;
    Some((p1.0 + t * d1x, p1.1 + t * d1y))
}

/// Compute the axis-aligned bounding box of an OBB.
///
/// Returns `[x, y, w, h]` where `(x, y)` is the top-left corner.
pub fn obb_to_aabb(obb: &[f64; 5]) -> [f64; 4] {
    polygon_bbox(&obb_to_corners(obb))
}

/// Compute the axis-aligned bounding box of a point set as `[x, y, w, h]`,
/// where `(x, y)` is the top-left corner.
///
/// Returns `[0.0, 0.0, 0.0, 0.0]` for an empty slice.
pub(crate) fn polygon_bbox(points: &[(f64, f64)]) -> [f64; 4] {
    if points.is_empty() {
        return [0.0, 0.0, 0.0, 0.0];
    }
    let mut min_x = f64::MAX;
    let mut max_x = f64::MIN;
    let mut min_y = f64::MAX;
    let mut max_y = f64::MIN;
    for &(x, y) in points {
        min_x = min_x.min(x);
        max_x = max_x.max(x);
        min_y = min_y.min(y);
        max_y = max_y.max(y);
    }
    [min_x, min_y, max_x - min_x, max_y - min_y]
}

/// Convert 4 corner points `[x1,y1,x2,y2,x3,y3,x4,y4]` to `[cx, cy, w, h, angle]`.
///
/// Center is the mean of the 4 corners. Width is the distance between corners 0-1,
/// height is the distance between corners 1-2, angle is atan2 of the first edge.
///
/// Errors when `coords` does not hold exactly 8 values (4 corner points) —
/// corner lists reach this function from untrusted converter input.
pub fn corners_to_obb(coords: &[f64]) -> crate::error::Result<[f64; 5]> {
    if coords.len() != 8 {
        return Err(format!(
            "corners_to_obb: expected exactly 8 coordinates (4 corner points), got {}",
            coords.len()
        )
        .into());
    }
    let cx = (coords[0] + coords[2] + coords[4] + coords[6]) / 4.0;
    let cy = (coords[1] + coords[3] + coords[5] + coords[7]) / 4.0;

    let dx01 = coords[2] - coords[0];
    let dy01 = coords[3] - coords[1];
    let w = (dx01 * dx01 + dy01 * dy01).sqrt();

    let dx12 = coords[4] - coords[2];
    let dy12 = coords[5] - coords[3];
    let h = (dx12 * dx12 + dy12 * dy12).sqrt();

    let angle = dy01.atan2(dx01);

    Ok([cx, cy, w, h, angle])
}

/// Intersection area of two rotated rectangles, via Sutherland-Hodgman clipping.
///
/// Rotated-rectangle *mechanics* — the IoU formula built on it lives in
/// [`crate::primitives::sim::obb_iou`].
pub(crate) fn obb_intersection_area(
    corners_a: &[(f64, f64); 4],
    corners_b: &[(f64, f64); 4],
) -> f64 {
    polygon_area(&sutherland_hodgman_clip(corners_a, corners_b))
}

/// Convenience wrapper for computing IoU between two OBB parameter arrays.
#[cfg(test)]
fn obb_iou_single(a: &[f64; 5], b: &[f64; 5], b_is_crowd: bool) -> f64 {
    crate::primitives::sim::obb_iou_pair(
        &obb_to_corners(a),
        a[2] * a[3],
        &obb_to_corners(b),
        b[2] * b[3],
        b_is_crowd,
    )
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
    use super::*;
    use std::f64::consts::{FRAC_PI_2, FRAC_PI_4, PI};

    const EPS: f64 = 1e-9;

    #[test]
    fn test_obb_to_corners_axis_aligned() {
        // 10×6 box centered at (5, 3), no rotation
        let corners = obb_to_corners(&[5.0, 3.0, 10.0, 6.0, 0.0]);
        // Corners should form a rectangle from (0, 0) to (10, 6)
        let mut xs: Vec<f64> = corners.iter().map(|c| c.0).collect();
        let mut ys: Vec<f64> = corners.iter().map(|c| c.1).collect();
        xs.sort_by(|a, b| a.partial_cmp(b).unwrap());
        ys.sort_by(|a, b| a.partial_cmp(b).unwrap());
        assert!((xs[0] - 0.0).abs() < EPS);
        assert!((xs[3] - 10.0).abs() < EPS);
        assert!((ys[0] - 0.0).abs() < EPS);
        assert!((ys[3] - 6.0).abs() < EPS);
    }

    #[test]
    fn test_obb_to_corners_90deg() {
        // 10×4 box rotated 90° → should become 4×10
        let corners = obb_to_corners(&[0.0, 0.0, 10.0, 4.0, FRAC_PI_2]);
        let mut xs: Vec<f64> = corners.iter().map(|c| c.0).collect();
        let mut ys: Vec<f64> = corners.iter().map(|c| c.1).collect();
        xs.sort_by(|a, b| a.partial_cmp(b).unwrap());
        ys.sort_by(|a, b| a.partial_cmp(b).unwrap());
        // After 90° rotation, width and height swap
        assert!((xs[3] - xs[0] - 4.0).abs() < EPS);
        assert!((ys[3] - ys[0] - 10.0).abs() < EPS);
    }

    #[test]
    fn test_polygon_area_square() {
        let square = [(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 1.0)];
        assert!((polygon_area(&square) - 1.0).abs() < EPS);
    }

    #[test]
    fn test_polygon_area_triangle() {
        let tri = [(0.0, 0.0), (4.0, 0.0), (0.0, 3.0)];
        assert!((polygon_area(&tri) - 6.0).abs() < EPS);
    }

    #[test]
    fn test_identical_boxes_iou_1() {
        let a = [10.0, 10.0, 20.0, 30.0, 0.5];
        let iou = obb_iou_single(&a, &a, false);
        assert!(
            (iou - 1.0).abs() < EPS,
            "identical boxes should have IoU=1.0, got {iou}"
        );
    }

    #[test]
    fn test_non_overlapping_iou_0() {
        let a = [0.0, 0.0, 2.0, 2.0, 0.0];
        let b = [100.0, 100.0, 2.0, 2.0, 0.0];
        let iou = obb_iou_single(&a, &b, false);
        assert!(
            iou.abs() < EPS,
            "non-overlapping boxes should have IoU=0.0, got {iou}"
        );
    }

    #[test]
    fn test_axis_aligned_overlap() {
        // Two axis-aligned boxes: [0,0,4,4] and [2,0,4,4] → overlap 2×4 = 8
        // Union = 16 + 16 - 8 = 24, IoU = 8/24 = 1/3
        let a = [2.0, 2.0, 4.0, 4.0, 0.0];
        let b = [4.0, 2.0, 4.0, 4.0, 0.0];
        let iou = obb_iou_single(&a, &b, false);
        assert!(
            (iou - 1.0 / 3.0).abs() < 1e-6,
            "expected IoU ≈ 1/3, got {iou}"
        );
    }

    #[test]
    fn test_90deg_rotation_identical() {
        // A square rotated 90° is the same square
        let a = [5.0, 5.0, 4.0, 4.0, 0.0];
        let b = [5.0, 5.0, 4.0, 4.0, FRAC_PI_2];
        let iou = obb_iou_single(&a, &b, false);
        assert!(
            (iou - 1.0).abs() < 1e-6,
            "square rotated 90° should have IoU=1.0, got {iou}"
        );
    }

    #[test]
    fn test_45deg_rotation_partial_overlap() {
        // Square at origin, one rotated 45° — known overlap geometry
        let a = [0.0, 0.0, 2.0, 2.0, 0.0];
        let b = [0.0, 0.0, 2.0, 2.0, FRAC_PI_4];
        let iou = obb_iou_single(&a, &b, false);
        // Both have area 4. Intersection of two squares rotated 45° at same center:
        // the intersection is a regular octagon. IoU should be between 0 and 1.
        assert!(
            iou > 0.5,
            "45° rotated squares should have significant overlap, got {iou}"
        );
        assert!(
            iou < 1.0,
            "45° rotated squares should not fully overlap, got {iou}"
        );
    }

    #[test]
    fn test_iscrowd() {
        // Crowd mode: IoU = intersection / dt_area
        let a = [0.0, 0.0, 2.0, 2.0, 0.0]; // dt, area = 4
        let b = [0.0, 0.0, 4.0, 4.0, 0.0]; // gt crowd, area = 16
        // a is fully inside b, so intersection = 4, IoU_crowd = 4/4 = 1.0
        let iou = obb_iou_single(&a, &b, true);
        assert!(
            (iou - 1.0).abs() < EPS,
            "small box inside crowd should have IoU=1.0, got {iou}"
        );

        // Normal mode: IoU = 4 / (4 + 16 - 4) = 4/16 = 0.25
        let iou_normal = obb_iou_single(&a, &b, false);
        assert!(
            (iou_normal - 0.25).abs() < 1e-6,
            "expected IoU=0.25, got {iou_normal}"
        );
    }

    #[test]
    fn test_zero_area_box() {
        let a = [0.0, 0.0, 0.0, 2.0, 0.0]; // zero width
        let b = [0.0, 0.0, 2.0, 2.0, 0.0];
        assert!(obb_iou_single(&a, &b, false).abs() < EPS);
    }

    #[test]
    fn test_obb_iou_matrix() {
        let dt = vec![[0.0, 0.0, 2.0, 2.0, 0.0], [100.0, 100.0, 2.0, 2.0, 0.0]];
        let gt = vec![[0.0, 0.0, 2.0, 2.0, 0.0]];
        let iscrowd = vec![false];
        let result = obb_iou(&dt, &gt, &iscrowd);
        assert_eq!(result.len(), 2);
        assert_eq!(result[0].len(), 1);
        assert!((result[0][0] - 1.0).abs() < EPS);
        assert!(result[1][0].abs() < EPS);
    }

    #[test]
    fn test_obb_iou_empty() {
        let dt: Vec<[f64; 5]> = vec![];
        let gt = vec![[0.0, 0.0, 2.0, 2.0, 0.0]];
        let iscrowd = vec![false];
        let result = obb_iou(&dt, &gt, &iscrowd);
        assert!(result.is_empty());
    }

    #[test]
    fn test_180deg_rotation() {
        // 180° rotation of a rectangle should give identical corners (just reordered)
        let a = [5.0, 5.0, 6.0, 4.0, 0.0];
        let b = [5.0, 5.0, 6.0, 4.0, PI];
        let iou = obb_iou_single(&a, &b, false);
        assert!(
            (iou - 1.0).abs() < 1e-6,
            "180° rotation should give IoU=1.0, got {iou}"
        );
    }

    #[test]
    fn test_contained_box() {
        // Small box fully inside large box
        let big = [0.0, 0.0, 10.0, 10.0, 0.3];
        let small = [0.0, 0.0, 2.0, 2.0, 0.3]; // same center, same angle, smaller
        let iou = obb_iou_single(&small, &big, false);
        // intersection = 4, union = 100 + 4 - 4 = 100, IoU = 4/100 = 0.04
        assert!((iou - 0.04).abs() < 1e-6, "expected IoU=0.04, got {iou}");
    }
}