aboutsummaryrefslogtreecommitdiff
path: root/samples/ObjectDetection/src/BoundingBox.cpp
blob: c52b0fe58a14b626d356be37a83d579043acbaf6 (plain)
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
//
// Copyright © 2020 Arm Ltd and Contributors. All rights reserved.
// SPDX-License-Identifier: MIT
//

#include "BoundingBox.hpp"
#include <algorithm>
namespace od
{

BoundingBox::BoundingBox() :
        BoundingBox(0, 0, 0u, 0u) {}

BoundingBox::BoundingBox(
        int x,
        int y,
        unsigned int width,
        unsigned int height) :
        m_X(x),
        m_Y(y),
        m_Width(width),
        m_Height(height) {}

BoundingBox::BoundingBox(const BoundingBox& other) :
        m_X(other.m_X),
        m_Y(other.m_Y),
        m_Width(other.m_Width),
        m_Height(other.m_Height) {}

int BoundingBox::GetX() const {
    return m_X;
}

int BoundingBox::GetY() const {
    return m_Y;
}

unsigned int BoundingBox::GetWidth() const {
    return m_Width;
}

unsigned int BoundingBox::GetHeight() const {
    return m_Height;
}

void BoundingBox::SetX(int x) {
    m_X = x;
}

void BoundingBox::SetY(int y) {
    m_Y = y;
}

void BoundingBox::SetWidth(unsigned int width) {
    m_Width = width;
}

void BoundingBox::SetHeight(unsigned int height) {
    m_Height = height;
}

BoundingBox& BoundingBox::operator=(const BoundingBox& other) {
    m_X = other.m_X;
    m_Y = other.m_Y;

    m_Width = other.m_Width;
    m_Height = other.m_Height;

    return *this;
}

/* Helper function to get a "valid" bounding box */
void GetValidBoundingBox(const BoundingBox& boxIn, BoundingBox& boxOut,
                         const BoundingBox& boxLimits) {
    boxOut.SetX(std::max(boxIn.GetX(), boxLimits.GetX()));
    boxOut.SetY(std::max(boxIn.GetY(), boxLimits.GetY()));

    /* If we have changed x and/or y, we compensate by reducing the height and/or width */
    int boxOutWidth = static_cast<int>(boxIn.GetWidth()) -
                      std::max(0, (boxOut.GetX() - boxIn.GetX()));
    int boxOutHeight = static_cast<int>(boxIn.GetHeight()) -
                       std::max(0, (boxOut.GetY() - boxIn.GetY()));

    /* This suggests that there was no overlap on x or/and y axis */
    if (boxOutHeight <= 0 || boxOutWidth <= 0)
    {
        boxOut = BoundingBox{0, 0, 0, 0};
        return;
    }

    const int limitBoxRightX = boxLimits.GetX() + static_cast<int>(boxLimits.GetWidth());
    const int limitBoxRightY = boxLimits.GetY() + static_cast<int>(boxLimits.GetHeight());
    const int boxRightX = boxOut.GetX() + boxOutWidth;
    const int boxRightY = boxOut.GetY() + boxOutHeight;

    if (boxRightX > limitBoxRightX)
    {
        boxOutWidth -= (boxRightX - limitBoxRightX);
    }

    if (boxRightY > limitBoxRightY)
    {
        boxOutHeight -= (boxRightY - limitBoxRightY);
    }

    /* This suggests value has rolled over because of very high numbers, not handled for now */
    if (boxOutHeight <= 0 || boxOutWidth <= 0)
    {
        boxOut = BoundingBox{0, 0, 0, 0};
        return;
    }

    boxOut.SetHeight(static_cast<unsigned int>(boxOutHeight));
    boxOut.SetWidth(static_cast<unsigned int>(boxOutWidth));
}
}// namespace od