diff --git a/common/object_recognition_utils/include/object_recognition_utils/matching.hpp b/common/object_recognition_utils/include/object_recognition_utils/matching.hpp index 5e8b6a3d49890..f2033dba0290e 100644 --- a/common/object_recognition_utils/include/object_recognition_utils/matching.hpp +++ b/common/object_recognition_utils/include/object_recognition_utils/matching.hpp @@ -30,8 +30,18 @@ namespace object_recognition_utils { using tier4_autoware_utils::Polygon2d; +inline bool validatePolygons(const Polygon2d & source_polygon, const Polygon2d & target_polygon) +{ + static constexpr double min_area = 1e-9; + if (boost::geometry::area(source_polygon) < min_area) return false; + if (boost::geometry::area(target_polygon) < min_area) return false; + return true; +} + inline double getConvexShapeArea(const Polygon2d & source_polygon, const Polygon2d & target_polygon) { + if (!validatePolygons(source_polygon, target_polygon)) return 0.0; + boost::geometry::model::multi_polygon union_polygons; boost::geometry::union_(source_polygon, target_polygon, union_polygons); @@ -50,6 +60,8 @@ inline double getSumArea(const std::vector & polygons) inline double getIntersectionArea( const Polygon2d & source_polygon, const Polygon2d & target_polygon) { + if (!validatePolygons(source_polygon, target_polygon)) return 0.0; + std::vector intersection_polygons; boost::geometry::intersection(source_polygon, target_polygon, intersection_polygons); return getSumArea(intersection_polygons); @@ -57,6 +69,8 @@ inline double getIntersectionArea( inline double getUnionArea(const Polygon2d & source_polygon, const Polygon2d & target_polygon) { + if (!validatePolygons(source_polygon, target_polygon)) return 0.0; + std::vector union_polygons; boost::geometry::union_(source_polygon, target_polygon, union_polygons); return getSumArea(union_polygons); @@ -84,8 +98,11 @@ double get2dGeneralizedIoU(const T1 & source_object, const T2 & target_object) const auto target_polygon = tier4_autoware_utils::toPolygon2d(target_object); const double intersection_area = getIntersectionArea(source_polygon, target_polygon); + if (intersection_area == 0.0) return 0.0; const double union_area = getUnionArea(source_polygon, target_polygon); + if (union_area == 0.0) return 0.0; const double convex_shape_area = getConvexShapeArea(source_polygon, target_polygon); + if (convex_shape_area == 0.0) return 0.0; const double iou = union_area < 0.01 ? 0.0 : std::min(1.0, intersection_area / union_area); return iou - (convex_shape_area - union_area) / convex_shape_area;