forked from Jekyll1021/MultiPointPushing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.py
278 lines (243 loc) · 8.83 KB
/
helper.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import math
import numpy as np
import matplotlib.pyplot as plt
# Function to know if we have a CCW turn
def CCW(p1, p2, p3):
if (p3[1]-p1[1])*(p2[0]-p1[0]) >= (p2[1]-p1[1])*(p3[0]-p1[0]):
return True
return False
# Main function:
def create_convex_hull(S):
"""takes in an [np array] of points!
and return a convex hull
"""
n = len(S)
P = [None] * n
l = np.where(S[:,0] == np.min(S[:,0]))
pointOnHull = S[l[0][0]]
i = 0
while True:
P[i] = pointOnHull
endpoint = S[0]
for j in range(1,n):
if (endpoint[0] == pointOnHull[0] and endpoint[1] == pointOnHull[1]) or not CCW(S[j],P[i],endpoint):
endpoint = S[j]
i = i + 1
pointOnHull = endpoint
if endpoint[0] == P[0][0] and endpoint[1] == P[0][1]:
break
for i in range(n):
if P[-1] is None:
del P[-1]
return np.array(P)
def euclidean_dist(pos1, pos2):
return math.sqrt((pos1[0] - pos2[0]) ** 2 + (pos1[1] - pos2[1]) ** 2)
def compute_centroid(vertices):
"""
helper function:
input:
vertices: a list of vertices of a polygon
under the assumption that all vertices are ordered either clockwise/counterclockwise
output:
centroid: position of (x, y) tuple of the polygon relative to the local origin of polygon.
"""
c_x = 0
c_y = 0
area = 0
n = len(vertices)
for i in range(n):
curr = vertices[(i - n) % n]
next = vertices[(i + 1 - n) % n]
diff = (curr[0] * next[1] - curr[1] * next[0])
c_x += (curr[0] + next[0]) * diff
c_y += (curr[1] + next[1]) * diff
area += diff
area = area / 2
c_x = c_x / (6 * area)
c_y = c_y / (6 * area)
return c_x, c_y
def compute_area(vertices):
"""
helper function:
input:
vertices: a list of vertices of a polygon
under the assumption that all vertices are ordered either clockwise/counterclockwise
output:
centroid: position of (x, y) tuple of the polygon relative to the local origin of polygon.
"""
c_x = 0
c_y = 0
area = 0
n = len(vertices)
for i in range(n):
curr = vertices[(i - n) % n]
next = vertices[(i + 1 - n) % n]
diff = (curr[0] * next[1] - curr[1] * next[0])
c_x += (curr[0] + next[0]) * diff
c_y += (curr[1] + next[1]) * diff
area += diff
area = area / 2
return abs(area)
def normalize(vector):
"""
helper function:
input:
vector: (x, y) force vector
output:
vector: (x, y) force vector with normalized magnitude 1
"""
mag = math.sqrt(vector[0] ** 2 + vector[1] ** 2)+1e-6
return vector[0] / mag, vector[1] / mag
def side_of_point_on_line(start_pt, end_pt, query_pt):
det = (end_pt[0] - start_pt[0]) * (query_pt[1] - start_pt[1]) - (end_pt[1] - start_pt[1]) * (query_pt[0] - start_pt[0])
if det > 0:
return 1
elif det < 0:
return -1
else:
return 0
def pointToLineDistance(e1, e2, p1):
numerator = np.abs((e2[1] - e1[1])*p1[0] - (e2[0] - e1[0])*p1[1] + e2[0]*e1[1] - e1[0]*e2[1])
normalization = np.sqrt((e2[1] - e1[1])**2 + (e2[0] - e1[0])**2)
return numerator/normalization
def scalarProject(start_pt, end_pt, point):
a = np.array(point) - np.array(start_pt)
unit_b = normalize(np.array(end_pt) - np.array(start_pt))
return a[0]*unit_b[0]+a[1]*unit_b[1]
def projectedPtToStartDistance(e1, e2, p1):
d1 = pointToLineDistance(e1, e2, p1)
d2 = euclidean_dist(e1, p1)
if abs(d1) > abs(d2):
return None
return math.sqrt(d2 ** 2 - d1 ** 2)
def two_line_intersect(e1, e2, e3, e4):
denom = (e1[0]-e2[0])*(e3[1]-e4[1]) - (e1[1]-e2[1])*(e3[0]-e4[0])
f1 = (e1[0]*e2[1] - e1[1]*e2[0])
f2 = (e3[0]*e4[1] - e3[1]*e4[0])
if denom == 0:
return None
pt = ((f1*(e3[0] - e4[0]) - f2 * (e1[0] - e2[0])) / (denom+1e-6), (f1*(e3[1] - e4[1]) - f2 * (e1[1] - e2[1]))/(denom+1e-6))
kap = np.dot(np.array(pt) - np.array(e3), np.array(e4) - np.array(e3))
kab = np.dot(np.array(e4) - np.array(e3), np.array(e4) - np.array(e3))
if kap > kab or kap < 0:
return None
else:
return pt
# return pt
def find_max_contact_range(vertices, e1, e2):
p = np.array(e1) - np.array(e2)
vector = (1, -(p[0] / (p[1] + 1e-6)))
# print(vector)
max_contact_range = 0
start_pt = None
max_dist = 0
for i in range(len(vertices)):
dist = pointToLineDistance(e1, e2, vertices[i])
if dist > max_dist:
max_dist = dist
start_pt = vertices[i]
# print(start_pt)
if not start_pt is None:
end_pt = np.array(start_pt) + np.array(vector)
start_pt = np.array(start_pt) - np.array(vector)
intersect_list = set()
for j in range(len(vertices)):
intersect = two_line_intersect(start_pt, end_pt, vertices[j], vertices[(j + 1) % len(vertices)])
# print(vertices[j], vertices[(j + 1) % len(vertices)])
# print(intersect)
if not intersect is None:
add = True
for pt in intersect_list:
if euclidean_dist(pt, intersect) < 0.01:
add = False
if add:
intersect_list.add(intersect)
# print(intersect_list)
if len(intersect_list) == 2:
# print(intersect_list)
intersect_list = list(intersect_list)
contact_range = euclidean_dist(intersect_list[0], intersect_list[1])
if contact_range > max_contact_range:
max_contact_range = contact_range
# for i in range(len(vertices)):
# perp_end_pt = np.array(vertices[i]) + np.array(vector)
# intersect_list = []
# for j in range(len(vertices)):
# intersect = two_line_intersect(vertices[i], perp_end_pt, vertices[j], vertices[(j + 1) % len(vertices)])
# if not intersect is None:
# intersect_list.append(intersect)
# print(intersect_list)
# if len(intersect_list) == 2:
# # print(intersect_list)
# contact_range = euclidean_dist(intersect_list[0], intersect_list[1])
# if contact_range > max_contact_range:
# max_contact_range = contact_range
return max_contact_range, list(intersect_list)
def find_collision_dist_convex_hull(start_pt, vector, centroid, vertices):
abs_vertices = np.array(vertices) + np.array(centroid)
end_pt = np.array(start_pt) + np.array(vector)
dist = 1e2
for i in range(len(vertices)):
intersect = two_line_intersect(start_pt, end_pt, abs_vertices[i], abs_vertices[(i + 1) % len(abs_vertices)])
if not intersect is None:
if (np.array(intersect) - np.array(start_pt))[0] / (vector[0] + 1e-6) > 0 or (np.array(intersect) - np.array(start_pt))[1] / (vector[1] + 1e-6) > 0:
dist = min(dist, euclidean_dist(intersect, start_pt))
return dist
def find_free_space(start_pt, vector, object_lst):
return min([find_collision_dist_convex_hull(start_pt, vector, obj.original_pos, obj.vertices) for obj in object_lst])
def parametrize_by_bounding_circle(start_pt, vector, centroid, bounding_circle_radius):
"""parametrize as p1 to p2"""
point = (start_pt[0] - centroid[0], start_pt[1] - centroid[1])
a = (vector[0]**2 + vector[1]**2) + 1e-6
b = (2 * point[0] * vector[0] + 2 * point[1] * vector[1])
c = (point[0] ** 2 + point[1] ** 2 - bounding_circle_radius ** 2)
if (b**2 - 4 * a * c) < 0:
print("unable to parametrize by bounding circle: line of force does not touch bounding circle")
return None
else:
t1 = (-b + math.sqrt(b**2 - 4 * a * c))/(2*a)
t2 = (-b - math.sqrt(b**2 - 4 * a * c))/(2*a)
p1 = (point[0] + t2 * vector[0], point[1] + t2 * vector[1])
p2 = (point[0] + t1 * vector[0], point[1] + t1 * vector[1])
return [np.array(normalize([p1[0], p1[1]])) * bounding_circle_radius + np.array(centroid), np.array(normalize([p2[0], p2[1]])) * bounding_circle_radius + np.array(centroid)]
def generatePolygon(min_rad=math.sqrt(2)*2/3, max_rad=math.sqrt(2), num_ver=6) :
angles = sorted([np.random.uniform(0, 2*math.pi) for i in range(num_ver)])
rad = [np.random.uniform(min_rad, max_rad) for i in range(num_ver)]
return [[math.cos(angles[i]) * rad[i], math.sin(angles[i]) * rad[i]] for i in range(num_ver)]
def unitVector2Degree(vector):
vector = normalize(vector)
return math.atan2(vector[1], vector[0])*180/math.pi
def findMaxAwayVector(vector_lst):
sum_degree = sum([unitVector2Degree(vector) for vector in vector_lst])
radius = (sum_degree/len(vector_lst))/180*math.pi
return normalize((math.cos(radius), math.sin(radius)))
def rotatePt(point, vector):
radius = unitVector2Degree(vector)/180*math.pi
x = point[0]*math.cos(radius)-point[1]*math.sin(radius)
y = point[0]*math.sin(radius)+point[1]*math.cos(radius)
return (x, y)
def findLoads(vertices, start_pt, end_pt):
left_points = []
right_points = []
for i in range(len(vertices)):
curr = vertices[i]
next = vertices[(i+1) % len(vertices)]
side_c = side_of_point_on_line(start_pt, end_pt, curr)
side_n = side_of_point_on_line(start_pt, end_pt, next)
# print(curr, next, side_c, side_n)
if side_c <= 0:
left_points.append(curr)
if side_c >= 0:
right_points.append(curr)
if side_c != side_n:
print(curr, next)
intersect = two_line_intersect(start_pt, end_pt, curr, next)
print(intersect)
if not intersect is None:
left_points.append(intersect)
right_points.append(intersect)
# print(left_points)
left = create_convex_hull(np.array(left_points))
right = create_convex_hull(np.array(right_points))
v = compute_centroid(vertices)
return euclidean_dist(compute_centroid(left), v), euclidean_dist(compute_centroid(right), v)