Skip to content

Commit 08fa457

Browse files
added test for geometry utils iou calculation
1 parent aa8767b commit 08fa457

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed

perception/image_projection_based_fusion/CMakeLists.txt

+7
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,13 @@ else()
160160
message("Skipping build of some nodes due to missing dependencies")
161161
endif()
162162

163+
if(BUILD_TESTING)
164+
find_package(ament_cmake_gtest REQUIRED)
165+
ament_auto_add_gtest(test_calc_iou_functions
166+
test/test_calc_iou_functions.cpp
167+
)
168+
endif()
169+
163170
ament_auto_package(INSTALL_TO_SHARE
164171
launch
165172
config
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
// Copyright 2024 TIER IV, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "image_projection_based_fusion/utils/geometry.hpp"
16+
#include <gtest/gtest.h>
17+
18+
using namespace image_projection_based_fusion;
19+
20+
TEST(GeometryTest, CalcIoU) {
21+
sensor_msgs::msg::RegionOfInterest roi1, roi2;
22+
23+
// Overlapping ROIs
24+
roi1.x_offset = 0;
25+
roi1.y_offset = 0;
26+
roi1.width = 4;
27+
roi1.height = 4;
28+
29+
roi2.x_offset = 2;
30+
roi2.y_offset = 2;
31+
roi2.width = 4;
32+
roi2.height = 4;
33+
34+
double iou = calcIoU(roi1, roi2);
35+
EXPECT_NEAR(iou, 1.0 / 7.0, 1e-6);
36+
37+
// Non-overlapping ROIs
38+
roi2.x_offset = 5;
39+
roi2.y_offset = 5;
40+
41+
iou = calcIoU(roi1, roi2);
42+
EXPECT_EQ(iou, 0.0);
43+
44+
// Zero area ROI
45+
roi1.width = 0;
46+
roi1.height = 0;
47+
48+
iou = calcIoU(roi1, roi2);
49+
EXPECT_EQ(iou, 0.0);
50+
}
51+
52+
TEST(GeometryTest, CalcIoUX) {
53+
sensor_msgs::msg::RegionOfInterest roi1, roi2;
54+
55+
// Overlapping ROIs on x-axis
56+
roi1.x_offset = 0;
57+
roi1.y_offset = 0;
58+
roi1.width = 4;
59+
roi1.height = 4;
60+
61+
roi2.x_offset = 2;
62+
roi2.y_offset = 0;
63+
roi2.width = 4;
64+
roi2.height = 4;
65+
66+
double ioux = calcIoUX(roi1, roi2);
67+
EXPECT_NEAR(ioux, 2.0 / 6.0, 1e-6);
68+
69+
// Non-overlapping ROIs on x-axis
70+
roi2.x_offset = 5;
71+
72+
ioux = calcIoUX(roi1, roi2);
73+
EXPECT_EQ(ioux, 0.0);
74+
75+
// Zero width ROI
76+
roi1.width = 0;
77+
78+
ioux = calcIoUX(roi1, roi2);
79+
EXPECT_EQ(ioux, 0.0);
80+
}
81+
82+
TEST(GeometryTest, CalcIoUY) {
83+
sensor_msgs::msg::RegionOfInterest roi1, roi2;
84+
85+
// Overlapping ROIs on y-axis
86+
roi1.x_offset = 0;
87+
roi1.y_offset = 0;
88+
roi1.width = 4;
89+
roi1.height = 4;
90+
91+
roi2.x_offset = 0;
92+
roi2.y_offset = 2;
93+
roi2.width = 4;
94+
roi2.height = 4;
95+
96+
double iouy = calcIoUY(roi1, roi2);
97+
EXPECT_NEAR(iouy, 2.0 / 6.0, 1e-6);
98+
99+
// Non-overlapping ROIs on y-axis
100+
roi2.y_offset = 5;
101+
102+
iouy = calcIoUY(roi1, roi2);
103+
EXPECT_EQ(iouy, 0.0);
104+
105+
// Zero height ROI
106+
roi1.height = 0;
107+
108+
iouy = calcIoUY(roi1, roi2);
109+
EXPECT_EQ(iouy, 0.0);
110+
}
111+
112+
int main(int argc, char ** argv)
113+
{
114+
testing::InitGoogleTest(&argc, argv);
115+
return RUN_ALL_TESTS();
116+
}

0 commit comments

Comments
 (0)