|
| 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 | + |
| 17 | +#include <gtest/gtest.h> |
| 18 | + |
| 19 | +using image_projection_based_fusion::calcIoU; |
| 20 | +using image_projection_based_fusion::calcIoUX; |
| 21 | +using image_projection_based_fusion::calcIoUY; |
| 22 | + |
| 23 | +TEST(GeometryTest, CalcIoU) |
| 24 | +{ |
| 25 | + sensor_msgs::msg::RegionOfInterest roi1, roi2; |
| 26 | + |
| 27 | + // Overlapping ROIs |
| 28 | + roi1.x_offset = 0; |
| 29 | + roi1.y_offset = 0; |
| 30 | + roi1.width = 4; |
| 31 | + roi1.height = 4; |
| 32 | + |
| 33 | + roi2.x_offset = 2; |
| 34 | + roi2.y_offset = 2; |
| 35 | + roi2.width = 4; |
| 36 | + roi2.height = 4; |
| 37 | + |
| 38 | + double iou = calcIoU(roi1, roi2); |
| 39 | + EXPECT_NEAR(iou, 1.0 / 7.0, 1e-6); |
| 40 | + |
| 41 | + // Non-overlapping ROIs |
| 42 | + roi2.x_offset = 5; |
| 43 | + roi2.y_offset = 5; |
| 44 | + |
| 45 | + iou = calcIoU(roi1, roi2); |
| 46 | + EXPECT_EQ(iou, 0.0); |
| 47 | + |
| 48 | + // Zero area ROI |
| 49 | + roi1.width = 0; |
| 50 | + roi1.height = 0; |
| 51 | + |
| 52 | + iou = calcIoU(roi1, roi2); |
| 53 | + EXPECT_EQ(iou, 0.0); |
| 54 | +} |
| 55 | + |
| 56 | +TEST(GeometryTest, CalcIoUX) |
| 57 | +{ |
| 58 | + sensor_msgs::msg::RegionOfInterest roi1, roi2; |
| 59 | + |
| 60 | + // Overlapping ROIs on x-axis |
| 61 | + roi1.x_offset = 0; |
| 62 | + roi1.y_offset = 0; |
| 63 | + roi1.width = 4; |
| 64 | + roi1.height = 4; |
| 65 | + |
| 66 | + roi2.x_offset = 2; |
| 67 | + roi2.y_offset = 0; |
| 68 | + roi2.width = 4; |
| 69 | + roi2.height = 4; |
| 70 | + |
| 71 | + double iou_x = calcIoUX(roi1, roi2); |
| 72 | + EXPECT_NEAR(iou_x, 2.0 / 6.0, 1e-6); |
| 73 | + |
| 74 | + // Non-overlapping ROIs on x-axis |
| 75 | + roi2.x_offset = 5; |
| 76 | + |
| 77 | + iou_x = calcIoUX(roi1, roi2); |
| 78 | + EXPECT_EQ(iou_x, 0.0); |
| 79 | + |
| 80 | + // Zero width ROI |
| 81 | + roi1.width = 0; |
| 82 | + |
| 83 | + iou_x = calcIoUX(roi1, roi2); |
| 84 | + EXPECT_EQ(iou_x, 0.0); |
| 85 | +} |
| 86 | + |
| 87 | +TEST(GeometryTest, CalcIoUY) |
| 88 | +{ |
| 89 | + sensor_msgs::msg::RegionOfInterest roi1, roi2; |
| 90 | + |
| 91 | + // Overlapping ROIs on y-axis |
| 92 | + roi1.x_offset = 0; |
| 93 | + roi1.y_offset = 0; |
| 94 | + roi1.width = 4; |
| 95 | + roi1.height = 4; |
| 96 | + |
| 97 | + roi2.x_offset = 0; |
| 98 | + roi2.y_offset = 2; |
| 99 | + roi2.width = 4; |
| 100 | + roi2.height = 4; |
| 101 | + |
| 102 | + double iou_y = calcIoUY(roi1, roi2); |
| 103 | + EXPECT_NEAR(iou_y, 2.0 / 6.0, 1e-6); |
| 104 | + |
| 105 | + // Non-overlapping ROIs on y-axis |
| 106 | + roi2.y_offset = 5; |
| 107 | + |
| 108 | + iou_y = calcIoUY(roi1, roi2); |
| 109 | + EXPECT_EQ(iou_y, 0.0); |
| 110 | + |
| 111 | + // Zero height ROI |
| 112 | + roi1.height = 0; |
| 113 | + |
| 114 | + iou_y = calcIoUY(roi1, roi2); |
| 115 | + EXPECT_EQ(iou_y, 0.0); |
| 116 | +} |
| 117 | + |
| 118 | +int main(int argc, char ** argv) |
| 119 | +{ |
| 120 | + testing::InitGoogleTest(&argc, argv); |
| 121 | + return RUN_ALL_TESTS(); |
| 122 | +} |
0 commit comments