-
Notifications
You must be signed in to change notification settings - Fork 0
/
environment.py
189 lines (162 loc) · 5.64 KB
/
environment.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
from graphics import *
from random import randint
import math
import numpy as np
def draw_room(win):
environment = Rectangle(Point(0, 0), Point(500, 500))
environment.setFill('black')
environment.draw(win)
border = Rectangle(Point(20, 20), Point(480, 480))
border.setFill('white')
border.draw(win)
start1 = Line(Point(35, 35), Point(45, 45))
start1.setFill('green')
start1.setWidth(3)
start1.draw(win)
start2 = Line(Point(35, 45), Point(45, 35))
start2.setFill('green')
start2.setWidth(3)
start2.draw(win)
obstacles = []
obstacles.append(np.array([[35, 35], [35, 45], [45, 45], [45, 35]]))
return obstacles
def draw_table(win, obstacles):
table = Rectangle(Point(380, 420), Point(460, 460))
table.setFill('white')
table.setOutline('red')
table.draw(win)
objects = []
for i in range(5):
y = randint(426, 454)
ob = Circle(Point(390+i*15, y), 5)
ob.draw(win)
objects.append(np.array([390+i*5, y]))
obstacles.append(np.array([[380, 420], [380, 460], [460, 460], [460, 420]]))
return objects, obstacles
def draw_obstacles(win, n, obstacles):
for i in range(n):
a = randint(40, 460)
b = randint(40, 460)
polygonx = []
polygony = []
x1 = randint(a-15, a)
y1 = randint(b-15, b)
x2 = randint(a-15, a)
y2 = randint(b, b+15)
x3 = randint(a, a+15)
y3 = randint(b, b+15)
x4 = randint(a, a+15)
y4 = randint(b-15, b)
l = len(obstacles)
poly1 = np.array([[x1, y1], [x2, y2], [x3, y3], [x4, y4]])
t=0
for j in range(l):
poly2 = obstacles[j]
b=checkPolysIntersecting(poly1, poly2)
if b:
break
else:
t+=1
if t==l:
Obs = Polygon(Point(x1, y1), Point(x2, y2),Point(x3, y3), Point(x4, y4))
Obs.setFill('white')
Obs.setOutline('blue')
Obs.draw(win)
obstacles.append(poly1)
return obstacles
def checkPolysIntersecting(polyA, polyB):
"""
Checks whether convex polygons are intersecting by checking
if no separation axis from A to B and B to A.
Parameters
----------
polyA : np.array(N,2)
Ordered list of 2D vertices defining a convex polygon
polyB : np.array(N,2)
Ordered list of 2D vertices defining a convex polygon
Returns
-------
bool
True - polygons are intersecting
False - polgons are not intersecting
"""
return checkNoSeparation(polyA, polyB) and checkNoSeparation(polyB, polyA)
def checkNoSeparation(polyA, polyB):
"""
Checks whether separation axis is detected from polyA to polyB
(ie checking vertices of A & B against edges of A)
Parameters
----------
polyA : np.array(N,2)
Ordered list of 2D vertices defining a convex polygon
polyB : np.array(N,2)
Ordered list of 2D vertices defining a convex polygon
Returns
-------
bool
True - no separation from A to B
False - separation from A to B
"""
# Calculate orthogonal axis for each edge in A
for i in range(len(polyA)):
vStartIdx = i
# print("vStartIdx: ", vStartIdx)
vEndIdx = (i + 1) % len(polyA)
# print("vEndIdx: ", vEndIdx)
edge = polyA[vEndIdx, :] - polyA[vStartIdx, :]
# print("edge: ", edge)
orthAxis = np.array([-edge[1], edge[0]])
# print("orthAxis: ", orthAxis)
orthAxisNorm = np.linalg.norm(orthAxis)
# print("orthAxisNorm: ", orthAxisNorm)
# Check minimum projection for points in A
minProjA = float('inf')
maxProjA = float('-inf')
for v in polyA:
# print("v: ", v)
proj = np.dot(v, orthAxis)/orthAxisNorm
# print("proj: ", proj)
if proj < minProjA:
minProjA = proj
# print("minProjA: ", minProjA)
if proj > maxProjA:
maxProjA = proj
# print("maxProjA: ", maxProjA)
# Check minimum projection for points in B
minProjB = float('inf')
maxProjB = float('-inf')
for v in polyB:
# print("v: ", v)
proj = np.dot(v, orthAxis)/orthAxisNorm
# print("proj: ", proj)
if proj < minProjB:
minProjB = proj
# print("minProjB: ", minProjB)
if proj > maxProjB:
maxProjB = proj
# print("maxProjB: ", maxProjB)
# Condition for overlap
if (maxProjA > minProjB) and (minProjA < maxProjB):
pass
else:
# If no overlap detected, exit early
# DEBUG
#print("StartIdx, EndIdx, ", vStartIdx, vEndIdx)
#print("Edge, OrthAxis, ", edge, orthAxis)
#print("minProjA: ", minProjA)
#print("maxProjA: ", maxProjA)
#print("minProjB: ", minProjB)
#print("maxProjB: ", maxProjA)
# DEBUG
return False
# If all edges checked without separation detected,
return True
env = GraphWin('Simulation Environment', 500, 500)
env.setCoords(0, 0, 500, 500)
env.setBackground('white')
obstacles = draw_room(env)
[objects, obstacles] = draw_table(env, obstacles)
n_obstacles = randint(120,150)
obstacles = draw_obstacles(env, n_obstacles, obstacles)
env.getMouse()
env.close()