-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
64 lines (48 loc) · 1.57 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import math
import numpy as np
import matplotlib.path
def vec_from_line_to_point(
line_point0: np.ndarray,
line_point1: np.ndarray,
out_point: np.ndarray
):
vec01 = line_point1 - line_point0
vec0x = out_point - line_point0
vec01_norm2 = vec01[0]**2 + vec01[1]**2
if vec01_norm2 < 1e-6:
vec01_norm2 = 1e-6
proj_vec = np.dot(vec01, vec0x) * vec01 / vec01_norm2
vec = vec0x - proj_vec
dist = np.linalg.norm(vec)
cross_point = line_point0 + proj_vec
return vec, dist, cross_point
def dist_from_line_to_point(
line_point0: np.ndarray,
line_point1: np.ndarray,
out_point: np.ndarray
):
vec01 = line_point1 - line_point0
vec10 = line_point0 - line_point1
vec0x = out_point - line_point0
vec1x = out_point - line_point1
if np.dot(vec01, vec0x) < 0:
return np.linalg.norm(vec0x)
if np.dot(vec10, vec1x) < 0:
return np.linalg.norm(vec1x)
return np.linalg.norm(np.cross(vec0x, vec1x)) / np.linalg.norm(vec01)
def judge_point_in_polygon(polygon_points, point, tolerance=0):
polygon = matplotlib.path.Path(polygon_points)
return polygon.contains_point(point, radius=tolerance)
def get_nearest_vertex(polygon_points, point):
dists = np.hypot(polygon_points[:, 0] - point[0], polygon_points[:, 1] - point[1])
idx = dists.argmin()
return idx, dists[idx]
def norm_angle(a):
while a <= -math.pi:
a += math.pi * 2
while a > math.pi:
a -= math.pi * 2
return a
class Struct(object):
def __init__(self, **kwargs):
self.__dict__.update(**kwargs)