-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutilib.py
226 lines (197 loc) · 6.26 KB
/
utilib.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import math
# From: https://stackoverflow.com/a/1937202
# Answer by Andreas Brinck
# <editor-fold>
def expand_line(line, t):
"""
Returns an array of four coordinate sets expanding the input line into a rectangle with thickness t
"""
x0 = line[0][0]
y0 = line[0][1]
x1 = line[1][0]
y1 = line[1][1]
t = int(t)
dx = x1 - x0 # delta x
dy = y1 - y0 # delta y
line_length = math.sqrt(dx * dx + dy * dy)
dx /= line_length
dy /= line_length
# Ok, (dx, dy) is now a unit vector pointing in the direction of the line
# A perpendicular vector is given by (-dy, dx)
px = 0.5 * t * (-dy) # perpendicular vector with length thickness * 0.5
py = 0.5 * t * dx
x2 = x0 + px
y2 = y0 + py
x3 = x1 + px
y3 = y1 + py
x4 = x1 - px
y4 = y1 - py
x5 = x0 - px
y5 = y0 - py
return ((x2, y2), (x3, y3), (x4, y4), (x5, y5))
def expand_line2(line, thickness):
length = distance(line[0], line[1])
line_calc = ((line[0][0] - line[1][0]), (line[0][1] - line[1][1]))
line_calc = (-line_calc[1], line_calc[0])
line_calc = (line_calc[0] / length, line_calc[1] / length)
line_calc = (line_calc[0] * (thickness/2), line_calc[1] * (thickness/2))
nl1 = ((line[0][0] + line_calc[0], line[0][1] + line_calc[1]), (line[1][0] + line_calc[0], line[1][1] + line_calc[1]))
nl2 = ((line[0][0] - line_calc[0], line[0][1] - line_calc[1]), (line[1][0] - line_calc[0], line[1][1] - line_calc[1]))
return (nl1[0], nl1[1], nl2[0], nl2[1])
# check if r lies on (p,q)
def on_segment(p, q, r):
if r[0] <= max(p[0], q[0]) and r[0] >= min(p[0], q[0]) and r[1] <= max(p[1], q[1]) and r[1] >= min(p[1], q[1]):
return True
return False
# return 0/1/-1 for colinear/clockwise/counterclockwise
def orientation(p, q, r):
val = ((q[1] - p[1]) * (r[0] - q[0])) - ((q[0] - p[0]) * (r[1] - q[1]))
if val == 0 : return 0
return 1 if val > 0 else -1
#check if seg1 and seg2 intersect
def intersects(seg1, seg2):
p1, q1 = seg1
p2, q2 = seg2
o1 = orientation(p1, q1, p2) #find all orientations
o2 = orientation(p1, q1, q2)
o3 = orientation(p2, q2, p1)
o4 = orientation(p2, q2, q1)
if o1 != o2 and o3 != o4: #check general case
return True
if o1 == 0 and on_segment(p1, q1, p2) : return True #check special cases
if o2 == 0 and on_segment(p1, q1, q2) : return True
if o3 == 0 and on_segment(p2, q2, p1) : return True
if o4 == 0 and on_segment(p2, q2, q1) : return True
return False
# </editor-fold>
def get_intersect_point(line1, line2):
from shapely.geometry import LineString, Point
line1 = LineString(line1)
line2 = LineString(line2)
int_pt = line1.intersection(line2)
try:
return int_pt.x, int_pt.y
except:
return None
def points_close(point1, point2, sensitivity = 1):
# vprint("points_close()", 'debug')
# vprint("point1 => " + str(point1), 'debug')
# vprint("point2 => " + str(point2), 'debug')
if point1 is None or point2 is None:
return False
x0 = point1[0]
y0 = point1[1]
x1 = point2[0]
y1 = point2[1]
if x0 > x1 - sensitivity and x0 < x1 + sensitivity:
if y0 > y1 - sensitivity and y0 < y1 + sensitivity:
return True
return False
def true_intersect_check(line_a, line_b, intersect, sensitivity = 1):
if line_a is None or line_b is None or intersect is None:
return False
if points_close(line_a[0], intersect, sensitivity):
return False
if points_close(line_a[1], intersect, sensitivity):
return False
if points_close(line_b[0], intersect, sensitivity):
return False
if points_close(line_b[1], intersect, sensitivity):
return False
return True
def distance(p1, p2):
return math.sqrt((p1[0]-p2[0])**2 + (p1[1]-p2[1])**2)
def tuple_to_point_list(tup, stringify = False):
points = []
for t in tup:
if type(t) is tuple:
points = points + tuple_to_point_list(t, stringify)
else:
if stringify:
points.append(str(t))
else:
points.append(t)
return points
def point_to_bounding_box(point, size = 5):
return ((point[0] - size, point[1] - size), (point[0] + size, point[1] + size))
def polygon_to_line_set(polygon):
points = []
first_point = None
prev_point = None
for point in polygon:
if first_point is None:
first_point = point
if prev_point is not None:
points.append((prev_point, point))
prev_point = point
points.append((first_point, prev_point))
return points
try:
from PIL import ImageDraw
def draw_polygon(draw: ImageDraw, polygon, color = (255, 255, 255), line_width = 2):
for line in polygon_to_line_set(polygon):
draw.line(tuple_to_point_list(line), fill=color, width=line_width)
except Exception:
def draw_polygons():
return None
pass
def check_point_is_inside_polygon(point, polygon):
line = (point, (point[0] + 10000, point[1]))
intersect_count = 0
for p_line in polygon_to_line_set(polygon):
inter = get_intersect_point(line, p_line)
# print(inter)
if inter is not None:
intersect_count += 1
return not intersect_count % 2 == 0
def get_polygon_intersect_points(line, polygon):
intersections = []
for p_line in polygon_to_line_set(polygon):
inter = get_intersect_point(line, p_line)
if inter is not None:
intersections.append(inter)
return intersections
timers = {}
def timer_start(label):
import time
timers[label] = time.time()
def timer_print(label):
import time
if timers.keys().__contains__(label):
current = time.time()
minutes = 0
seconds = current - timers[label]
string = "?"
if seconds >= 60:
seconds = round(seconds)
minutes = math.floor(seconds / 60)
seconds = seconds - (minutes * 60)
else:
seconds = round(seconds, 2)
if minutes >= 1 and seconds >= 1:
string = str(minutes) + " minutes and " + str(seconds) + " seconds"
elif minutes >= 1 and seconds < 1:
string = str(minutes) + " minutes"
else:
string = str(seconds) + " seconds"
print("'" + label + "' took " + string)
log_level_ranks = {
'debug': 0,
'info': 1,
'warn': 2,
'error': 3,
'trace': 4,
'fatal': 5,
}
log_level = 'info'
log_file = False
def vprint(msg, level = 'info', log_file_override = None):
import datetime
global log_level, log_file, log_level_ranks
if type(msg) != str:
msg = str(msg)
if level == 'init' or log_level_ranks[level] >= log_level_ranks[log_level]:
print("[" + level.upper() + "] " + msg)
if (log_file_override is not None and log_file_override) or log_file:
with open("output.log", "a") as f:
f.write(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " [" + level.upper() + "] " + msg + "\n")