Monado OpenXR Runtime
u_box_iou.hpp
Go to the documentation of this file.
1// Copyright 2021-2022, 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 Moses Turner <moses@collabora.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 "xrt/xrt_defines.h"
15
16namespace xrt::auxiliary::util::box_iou {
17struct Box
18{
19 float cx;
20 float cy;
21 float w;
22 float h;
23
24 // No uninitialized memory!
25 Box() : cx(0.0f), cy(0.0f), w(0.0f), h(0.0f) {}
26 Box(const float cx, const float cy, const float w, const float h) : cx(cx), cy(cy), w(w), h(h) {}
27 Box(const float cx, const float cy, const float size) : cx(cx), cy(cy), w(size), h(size) {}
28 Box(const xrt_vec2 &center, const float size) : cx(center.x), cy(center.y), w(size), h(size) {}
29};
30
31static float
32overlap(float x1, float w1, float x2, float w2)
33{
34 float l1 = x1 - w1 / 2;
35 float l2 = x2 - w2 / 2;
36 float left = l1 > l2 ? l1 : l2;
37
38 float r1 = x1 + w1 / 2;
39 float r2 = x2 + w2 / 2;
40 float right = r1 < r2 ? r1 : r2;
41
42 return right - left;
43}
44
45static float
46boxIntersection(const Box &a, const Box &b)
47{
48 float w = overlap(a.cx, a.w, b.cx, b.w);
49 float h = overlap(a.cy, a.h, b.cy, b.h);
50
51 if (w < 0 || h < 0)
52 return 0;
53
54 return w * h;
55}
56
57static float
58boxUnion(const Box &a, const Box &b)
59{
60 return a.w * a.h + b.w * b.h - boxIntersection(a, b);
61}
62
63static float
64boxIOU(const Box &a, const Box &b)
65{
66 return boxIntersection(a, b) / boxUnion(a, b);
67}
68} // namespace xrt::auxiliary::util::box_iou
Definition: u_box_iou.hpp:18
A 2 element vector with single floats.
Definition: xrt_defines.h:250
Common defines and enums for XRT.