torchdrivesim._iou_utils
#
torch implementation of 2d oriented box intersection
author: Lanxiao Li copied from https://github.com/lilanxiao/Rotated_IoU 2020.8
MIT License
Copyright (c) 2020 Lanxiao Li
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Module Contents#
Functions#



find intersection points of rectangles. 

check if corners of box1 lie in box2 

check if corners of two boxes lie in each other 

find vertices of intersection area 



calculate area of intersection 

Calculates the intersection area of 2D rectangles. 

convert box coordinate to corners 

Convert bounding box coordinates to corners. The returned corners are starting from the rear of the bounding box 

Calculates the differentiable (approx.) IOU between two sets of bounding boxes. 


Attributes#
 torchdrivesim._iou_utils.box_intersection_th(corners1: torch.Tensor, corners2: torch.Tensor) Tuple[torch.Tensor, torch.Tensor] [source]#
find intersection points of rectangles. Convention: if two edges are collinear, there is no intersection point.
 Parameters:
corners1 – B, N, 4, 2
corners2 – B, N, 4, 2
 Returns:
intersections: B, N, 4, 4, 2 mask: B, N, 4, 4; bool
 Return type:
A tuple (intersectons, mask) where
 torchdrivesim._iou_utils.box1_in_box2(corners1: torch.Tensor, corners2: torch.Tensor) torch.Tensor [source]#
check if corners of box1 lie in box2 Convention: if a corner is exactly on the edge of the other box, it’s also a valid point
 Parameters:
corners1 – (B, N, 4, 2)
corners2 – (B, N, 4, 2)
 Returns:
(B, N, 4) Bool
 Return type:
c1_in_2
 torchdrivesim._iou_utils.box_in_box_th(corners1: torch.Tensor, corners2: torch.Tensor) Tuple[torch.Tensor, torch.Tensor] [source]#
check if corners of two boxes lie in each other
 Parameters:
corners1 – (B, N, 4, 2)
corners2 – (B, N, 4, 2)
 Returns:
c1_in_2: (B, N, 4) Bool. ith corner of box1 in box2 c2_in_1: (B, N, 4) Bool. ith corner of box2 in box1
 Return type:
A tuple (c1_in_2, c2_in_1) where
 torchdrivesim._iou_utils.build_vertices(corners1: torch.Tensor, corners2: torch.Tensor, c1_in_2: torch.Tensor, c2_in_1: torch.Tensor, inters: torch.Tensor, mask_inter: torch.Tensor) Tuple[torch.Tensor, torch.Tensor] [source]#
find vertices of intersection area
 Parameters:
corners1 – (B, N, 4, 2)
corners2 – (B, N, 4, 2)
c1_in_2 – Bool, (B, N, 4)
c2_in_1 – Bool, (B, N, 4)
inters – (B, N, 4, 4, 2)
mask_inter – (B, N, 4, 4)
 Returns:
vertices: (B, N, 24, 2) vertices of intersection area. only some elements are valid mask: (B, N, 24) indicates valid elements in vertices
 Return type:
A tuple (vertices, mask) where
 torchdrivesim._iou_utils.sort_indices(vertices: torch.Tensor, mask: torch.Tensor) torch.Tensor [source]#
 Parameters:
vertices – float (B, N, 24, 2)
mask – bool (B, N, 24)
 Returns:
bool (B, N, 9)
Note
why 9? the polygon has maximal 8 vertices. +1 to duplicate the first element. the index should have following structure:
(A, B, C, … , A, X, X, X)
and X indicates the index of arbitary elements in the last 16 (intersections not corners) with value 0 and mask False. (cause they have zero value and zero gradient)
 torchdrivesim._iou_utils.calculate_area(idx_sorted: torch.Tensor, vertices: torch.Tensor) Tuple[torch.Tensor, torch.Tensor] [source]#
calculate area of intersection
 Parameters:
idx_sorted (torch.Tensor) – (B, N, 9)
vertices (torch.Tensor) – (B, N, 24, 2)
 Returns:
 Tuple of (area, selected), where
area: (B, N), area of intersection selected: (B, N, 9, 2), vertices of polygon with zero padding
 torchdrivesim._iou_utils.oriented_box_intersection_2d(corners1: torch.Tensor, corners2: torch.Tensor) Tuple[torch.Tensor, torch.Tensor] [source]#
Calculates the intersection area of 2D rectangles.
 Parameters:
corners1 – (B, N, 4, 2)
corners2 – (B, N, 4, 2)
 Returns:
area: (B, N), area of intersection selected: (B, N, 9, 2), vertices of polygon with zero padding
 Return type:
Tuple of (area, selected)
 torchdrivesim._iou_utils.box2corners_th(box: torch.Tensor) torch.Tensor [source]#
convert box coordinate to corners
 Parameters:
box – (B, N, 5) with x, y, w, h, alpha
 Returns:
(B, N, 4, 2) corners
 torchdrivesim._iou_utils.box2corners_with_rear_factor(box: torch.Tensor, rear_factor: float = 1) torch.Tensor [source]#
Convert bounding box coordinates to corners. The returned corners are starting from the rear of the bounding box up to a rear factor. If the rear factor is 1, then the whole bounding box is returned.
 Parameters:
box – (B, N, 5) with x, y, w, h, alpha
rear_factor – The relative amount of the bounding box will be preserved starting from the rear corners and up to rear_factor * w. If the factor is 1, then the whole bounding box is preserved.
 Returns:
(B, N, 4, 2) corners
 torchdrivesim._iou_utils.iou_differentiable_fast(box1: torch.Tensor, box2: torch.Tensor) torch.Tensor [source]#
Calculates the differentiable (approx.) IOU between two sets of bounding boxes. B is the batch size and N is the number of bounding boxes. A bounding box is described by 5 values in this order: (x, y, w, h, alpha). This method uses the shoelace formula (https://en.wikipedia.org/wiki/Shoelace_formula) to calculate the area of a convex polygon (the overlapping area of two rectangles). It offers fast computation at the expense of some IoU accuracy.
 Parameters:
box1 – (B, N, 5)
box2 – (B, N, 5)
 Returns:
(B, N)
 Return type:
iou