-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSetter_visible.py
268 lines (220 loc) · 7.24 KB
/
Setter_visible.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
# ======================================================================================================================
# author: Xincong YANG
# date: 12 Oct. 2017
# email: [email protected]
# name: Setter_visible
# ======================================================================================================================
import numpy as np
from shapely.geometry import Polygon, Point
# precision of visibility region
EPSILON = 0.01
def visible(sensor, obj_polygon):
"""
generate sensor visible region in obj_polygon
:param sensor:
:param obj_polygon:
:return: visible region
"""
ext_ring = np.array(obj_polygon.exterior.coords)
int_rings = [np.array(int_ring.coords) for int_ring in obj_polygon.interiors]
config = sensor.get_config
x0, y0 = config['x'], config['y']
ray_start = [x0, y0]
min_angle, max_angle = sensor.get_FOV
min_rho, max_rho = sensor.get_FOD
if config['isOmini'] or min_rho > 0:
ray_ends = []
else:
ray_ends = [ray_start]
angle = min_angle
while True:
if angle > max_angle:
break
x, y = x0 + max_rho * np.cos(angle), y0 + max_rho * np.sin(angle)
# ray line segment
ray_end = [x, y]
ray = [ray_start, ray_end]
length = max_rho
if config['isThrough']:
# if sensor can pass through objects
pass
else:
# if sensor can not pass through objects
# intersect with ext_ring
for i in range(len(ext_ring) - 1):
start = ext_ring[i]
end = ext_ring[i + 1]
line = [start, end]
intersect = ray_casting(ray, line)
if distance(ray_start, intersect) <= length:
ray_end = intersect
length = distance(ray_start, intersect)
# intersect with int_rings
for int_ring in int_rings:
for i in range(len(int_ring) - 1):
start = int_ring[i]
end = int_ring[i + 1]
line = [start, end]
intersect = ray_casting(ray, line)
if distance(ray_start, intersect) <= length:
ray_end = intersect
length = distance(ray_start, intersect)
ray_ends.append(ray_end)
angle += EPSILON
visible_region = Polygon(ray_ends)
if min_rho > 0:
try:
invisible_region = Point((x0, y0)).buffer(distance=min_rho)
visible_region = visible_region.symmetric_difference(invisible_region)
except:
visible_region = None
return visible_region
def distance(start, end):
"""
generate the distance between point start and end
:param start: [x1, y1]
:param end: [x2, y2]
:return:
"""
x1, y1 = start
x2, y2 = end
return np.sqrt((y2 - y1) ** 2 + (x2 - x1) ** 2)
def ray_casting(ray, line):
"""
:param ray: [p0, p]
:param line: [p1, p2]
:return:
"""
p0, p = ray
p1, p2 = line
if isIntersect(ray, line):
return line_intersect(ray, line)
else:
return p
def isIntersect(line1, line2):
"""
determine whether two lines intersect
:param line1: [p11, p12]
:param line2: [p21, p22]
:return:
"""
p11, p12 = line1
p21, p22 = line2
return isCCW(p11, p12, p21) != isCCW(p11, p12, p22) and isCCW(p21, p22, p11) != isCCW(p21, p22, p12)
def isCCW(point1, point2, point3):
"""
determine whether three points are in counter clock wise - k component of cross product (1->2 and 1->3) should be +
:param point1: [x1, y1]
:param point2: [x2, y2]
:param point3: [x3, y3]
:return:
"""
x1, y1 = point1
x2, y2 = point2
x3, y3 = point3
return (x2 - x1) * (y3 - y1) > (y2 - y1) * (x3 - x1)
def isCollinear(point1, point2, point3):
"""
determine whether three points are collinear - slopes equal
:param point1: [x1, y1]
:param point2: [x2, y2]
:param point3: [x3, y3]
:return:
"""
x1, y1 = point1
x2, y2 = point2
x3, y3 = point3
return (y3 - y1) * (x2 - x1) == (y2 - y1) * (x3 - x1)
def line_intersect(line1, line2):
"""
generate the intersect of two line segments
:param line1: [p11, p12]
:param line2: [p21, p22]
:return:
"""
p11, p12 = line1
p21, p22 = line2
x1, y1 = p11
x2, y2 = p12
x3, y3 = p21
x4, y4 = p22
Dx11 = x1 * y2 - x2 * y1
Dx12 = x1 - x2
Dx21 = x3 * y4 - x4 * y3
Dx22 = x3 - x4
Dx = Dx11 * Dx22 - Dx12 * Dx21
Dy11 = x1 * y2 - x2 * y1
Dy12 = y1 - y2
Dy21 = x3 * y4 - x4 * y3
Dy22 = y3 - y4
Dy = Dy11 * Dy22 - Dy12 * Dy21
D11 = x1 - x2
D12 = y1 - y2
D21 = x3 - x4
D22 = y3 - y4
D = D11 * D22 - D12 * D21
if D != 0:
Px = Dx / D
Py = Dy / D
return (Px, Py)
else:
return None
def clip(obj_polygon, max_dist, min_dist):
"""
clip the obj_polygon edges into points
:param obj_polygon:
:param max_dist:
:param min_dist:
:return:
"""
ext_coords = np.array(obj_polygon.exterior.coords)
int_coords_list = [np.array(int_ring.coords) for int_ring in obj_polygon.interiors]
points = []
ext_n = len(ext_coords)
for i in range(ext_n - 1):
start = ext_coords[i]
end = ext_coords[i + 1]
dist = distance(start, end)
if dist > max_dist:
n = int(dist / max_dist)
xs = np.linspace(start[0], end[0], num=n, endpoint=False)
ys = np.linspace(start[1], end[1], num=n, endpoint=False)
for x, y in zip(xs, ys):
points.append((x, y))
elif dist > min_dist:
points.append(start)
for int_coords in int_coords_list:
int_n = len(int_coords)
for i in range(int_n - 1):
start = int_coords[i]
end = int_coords[i + 1]
dist = distance(start, end)
if dist > max_dist:
n = int(dist / max_dist)
xs = np.linspace(start[0], end[0], num=n, endpoint=False)
ys = np.linspace(start[1], end[1], num=n, endpoint=False)
for x, y in zip(xs, ys):
points.append((x, y))
elif dist > min_dist:
points.append(start)
return np.array(points)
def discrete(obj_polygon, grid_size):
"""
discrete the layout into points
:param obj_polygon:
:param grid_size:
:return:
"""
ext_coords = np.array(obj_polygon.exterior.coords)
max_x, max_y = ext_coords.max(axis=0)
min_x, min_y = ext_coords.min(axis=0)
grid_num_x = int(np.ceil((max_x - min_x) / grid_size))
grid_num_y = int(np.ceil((max_y - min_y) / grid_size))
xs = np.linspace(min_x, max_x, num=grid_num_x, endpoint=True)
ys = np.linspace(min_y, max_y, num=grid_num_y, endpoint=True)
points = []
for x in xs:
for y in ys:
if obj_polygon.contains(Point([x, y])):
points.append([x, y])
return np.array(points)