def jaccard_index_3d(a: BBox3D, b: BBox3D): """ Compute the Jaccard Index / Intersection over Union (IoU) of a pair of 3D bounding boxes. We compute the IoU using the top-down bird's eye view of the boxes. **Note**: We follow the KITTI format and assume only yaw rotations (along z-axis). Parameters ---------- a : BBox3D 3D bounding box. b : BBox3D 3D bounding box. Returns ------- float The IoU of the 2 bounding boxes. """ # check if the two boxes don't overlap if not polygon_collision(a.p[0:4, 0:2], b.p[0:4, 0:2]): return np.round_(0, decimals=5) intersection_points = polygon_intersection(a.p[0:4, 0:2], b.p[0:4, 0:2]) inter_area = polygon_area(intersection_points) zmax = np.minimum(a.cz, b.cz) zmin = np.maximum(a.cz - a.h, b.cz - b.h) inter_vol = inter_area * np.maximum(0, zmax - zmin) a_vol = a.l * a.w * a.h b_vol = b.l * b.w * b.h union_vol = (a_vol + b_vol - inter_vol) iou = inter_vol / union_vol # set nan and +/- inf to 0 if np.isinf(iou) or np.isnan(iou): iou = 0 return np.round_(iou, decimals=5)
def test_polygon_area(): polygon = np.array([[-3, -2], [-1, 4], [6, 1], [3, 10], [-4, 9]]) assert polygon_area(polygon) == 60