distance.py 948 Bytes
Newer Older
yeshenglong1's avatar
yeshenglong1 committed
1
from numpy.typing import NDArray
zhe chen's avatar
zhe chen committed
2
3
from scipy.spatial import distance

yeshenglong1's avatar
yeshenglong1 committed
4
5

def chamfer_distance(line1: NDArray, line2: NDArray) -> float:
zhe chen's avatar
zhe chen committed
6
    ''' Calculate chamfer distance between two lines. Make sure the
yeshenglong1's avatar
yeshenglong1 committed
7
8
9
10
11
    lines are interpolated.

    Args:
        line1 (array): coordinates of line1
        line2 (array): coordinates of line2
zhe chen's avatar
zhe chen committed
12

yeshenglong1's avatar
yeshenglong1 committed
13
14
15
    Returns:
        distance (float): chamfer distance
    '''
zhe chen's avatar
zhe chen committed
16

yeshenglong1's avatar
yeshenglong1 committed
17
18
19
20
21
22
    dist_matrix = distance.cdist(line1, line2, 'euclidean')
    dist12 = dist_matrix.min(-1).sum() / len(line1)
    dist21 = dist_matrix.min(-2).sum() / len(line2)

    return (dist12 + dist21) / 2

zhe chen's avatar
zhe chen committed
23

yeshenglong1's avatar
yeshenglong1 committed
24
def frechet_distance(line1: NDArray, line2: NDArray) -> float:
zhe chen's avatar
zhe chen committed
25
    ''' Calculate frechet distance between two lines. Make sure the
yeshenglong1's avatar
yeshenglong1 committed
26
27
28
29
30
    lines are interpolated.

    Args:
        line1 (array): coordinates of line1
        line2 (array): coordinates of line2
zhe chen's avatar
zhe chen committed
31

yeshenglong1's avatar
yeshenglong1 committed
32
33
34
35
    Returns:
        distance (float): frechet distance
    '''

zhe chen's avatar
zhe chen committed
36
    raise NotImplementedError