pcod-common v1.0.0
Shared preprocessing and postprocessing for point-cloud object detection
Loading...
Searching...
No Matches
bounding_box.cpp
Go to the documentation of this file.
1// Copyright Institute for Automotive Engineering (ika), RWTH Aachen University
2// SPDX-License-Identifier: Apache-2.0
3
5
6#include <algorithm>
7#include <cmath>
8
9namespace pcod_common {
10
12 auto rect1 = rectangle_vertices();
13 auto rect2 = other.rectangle_vertices();
14
15 std::vector<BoundingBoxVertex> intersection = rect1;
16
17 for (std::size_t i = 0; i < rect2.size(); ++i) {
18 if (intersection.size() <= 2) {
19 break;
20 }
21
22 Line line(rect2[i], rect2[(i + 1) % rect2.size()]);
23 std::vector<BoundingBoxVertex> new_intersection;
24 std::vector<float> line_values;
25
26 for (const auto& t : intersection) {
27 line_values.push_back(line(t));
28 }
29
30 for (std::size_t j = 0; j < intersection.size(); ++j) {
31 const BoundingBoxVertex& s = intersection[j];
32 const BoundingBoxVertex& t = intersection[(j + 1) % intersection.size()];
33 float s_value = line_values[j];
34 float t_value = line_values[(j + 1) % line_values.size()];
35
36 if (s_value <= 0) {
37 new_intersection.push_back(s);
38 }
39
40 if (s_value * t_value < 0) {
41 BoundingBoxVertex intersection_point = line.intersection(Line(s, t));
42 new_intersection.push_back(intersection_point);
43 }
44 }
45
46 intersection = new_intersection;
47 }
48
49 if (intersection.size() <= 2) {
50 return 0.0F;
51 }
52
53 float area = 0.0F;
54 for (std::size_t i = 0; i < intersection.size(); ++i) {
55 const BoundingBoxVertex& p = intersection[i];
56 const BoundingBoxVertex& q = intersection[(i + 1) % intersection.size()];
57 area += p.cross(q);
58 }
59
60 return 0.5F * std::abs(area);
61}
62
63bool BoundingBox::overlaps(const BoundingBox& other, float iou_threshold) const {
64 float intersection = intersection_area(other);
65 float union_area = length * width + other.length * other.width - intersection;
66 float iou = intersection / union_area;
67 return iou > iou_threshold;
68}
69
70std::vector<BoundingBoxVertex> BoundingBox::rectangle_vertices() const {
71 float angle = yaw;
72 float dx = length / 2.0F;
73 float dy = width / 2.0F;
74 float dxcos = dx * std::cos(angle);
75 float dxsin = dx * std::sin(angle);
76 float dycos = dy * std::cos(angle);
77 float dysin = dy * std::sin(angle);
78
79 std::vector<BoundingBoxVertex> vertices;
80 vertices.emplace_back(center[0] + (-dxcos - -dysin), center[1] + (-dxsin + -dycos));
81 vertices.emplace_back(center[0] + (dxcos - -dysin), center[1] + (dxsin + -dycos));
82 vertices.emplace_back(center[0] + (dxcos - dysin), center[1] + (dxsin + dycos));
83 vertices.emplace_back(center[0] + (-dxcos - dysin), center[1] + (-dxsin + dycos));
84 return vertices;
85}
86
87} // namespace pcod_common
float cross(const BoundingBoxVertex &v) const
float length
Length along the local X axis.
std::vector< BoundingBoxVertex > rectangle_vertices() const
float intersection_area(const BoundingBox &other) const
std::array< float, 2 > center
XY center in metres.
float width
Width along the local Y axis.
bool overlaps(const BoundingBox &other, float iou_threshold) const
float yaw
Heading in radians.
BoundingBoxVertex intersection(const Line &other) const