Monado OpenXR Runtime
Loading...
Searching...
No Matches
u_box_iou.hpp
Go to the documentation of this file.
1// Copyright 2021-2026, Collabora, Ltd.
2// SPDX-License-Identifier: BSL-1.0
3/*!
4 * @file
5 * @brief Code to deal with bounding boxes for camera-based hand-tracking.
6 * @author Moshi Turner <moshiturner@protonmail.com>
7 * @author Marcus Edel <marcus.edel@collabora.com>
8 * @ingroup aux_util
9 */
10
11#pragma once
12
13#include <math.h>
14#include <cmath>
15#include "xrt/xrt_defines.h"
16
17namespace xrt::auxiliary::util::box_iou {
18
19/// 2D Box defined by center and size (width and height)
20struct Box
21{
22 float cx;
23 float cy;
24 float w;
25 float h;
26
27 // No uninitialized memory!
28 Box() : cx(0.0f), cy(0.0f), w(0.0f), h(0.0f) {}
29 Box(float cx, float cy, float w, float h) : cx(cx), cy(cy), w(w), h(h) {}
30 Box(float cx, float cy, float size) : cx(cx), cy(cy), w(size), h(size) {}
31 Box(const xrt_vec2 &center, const float size) : cx(center.x), cy(center.y), w(size), h(size) {}
32};
33
34static float
35overlap(float x1, float w1, float x2, float w2)
36{
37 // farthest-right left edge
38 float l1 = x1 - w1 / 2;
39 float l2 = x2 - w2 / 2;
40 float left = std::max(l1, l2);
41
42
43 // farthest-left right edge
44 float r1 = x1 + w1 / 2;
45 float r2 = x2 + w2 / 2;
46 float right = std::min(r1, r2);
47
48 return right - left;
49}
50
51static float
52boxIntersection(const Box &a, const Box &b)
53{
54 float w = overlap(a.cx, a.w, b.cx, b.w);
55 float h = overlap(a.cy, a.h, b.cy, b.h);
56
57 if (w < 0 || h < 0)
58 return 0;
59
60 return w * h;
61}
62
63static float
64boxUnion(const Box &a, const Box &b)
65{
66 return a.w * a.h + b.w * b.h - boxIntersection(a, b);
67}
68
69/// Ratio of box intersection area vs box union area (Intersection Over Union)
70static float
71boxIOU(const Box &a, const Box &b)
72{
73 return boxIntersection(a, b) / boxUnion(a, b);
74}
75} // namespace xrt::auxiliary::util::box_iou
2D Box defined by center and size (width and height)
Definition u_box_iou.hpp:21
A 2 element vector with single floats.
Definition xrt_defines.h:279
static float boxIOU(const Box &a, const Box &b)
Ratio of box intersection area vs box union area (Intersection Over Union)
Definition u_box_iou.hpp:71
Common defines and enums for XRT.