-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathchecker.py
146 lines (124 loc) · 4.04 KB
/
checker.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
import numpy as np
'''
This function returns the equation of the line defined by the two points given
'''
def getEqn(x, y, start, end):
x1 = start[0]
y1 = start[1]
x2 = end[0]
y2 = end[1]
# If the line is not perpendicular to x-axis, calculate slope
if x1 != x2:
k = (y1-y2)/float(x1-x2)
# Otherwise, we cannot calculate the slope
else:
return x == x1
return y == k*(x-x2) + y2
'''
This function checks if Xnew is within the goal region
'''
def goalChecker(Xnew, goal):
if(Xnew[0] >= goal[0] and Xnew[0] <= goal[1] and Xnew[1] >= goal[2] and Xnew[1] <= goal[3]):
return True
else:
return False
'''
This functions checks if a line intersects with a single obsLine
Return: True if intersects
False if not
'''
def obsCheck(a,b,c,d):
a1 = b[1]-a[1]
b1 = a[0]-b[0]
c1 = a1*a[0]+b1*a[1]
a2 = d[1]-c[1]
b2 = c[0]-d[0]
c2 = a2*c[0]+b2*c[1]
determinant = a1*b2 - a2*b1
if determinant == 0:
return False
else:
x = (b2*c1 - b1*c2)/determinant
y = (a1*c2 - a2*c1)/determinant
if x - min(a[0], b[0]) < -0.01 or x - max(a[0], b[0]) > 0.01:
return False
if x - min(c[0], d[0]) < -0.01 or x - max(c[0], d[0]) > 0.01:
return False
if y - min(a[1], b[1]) < -0.01 or y - max(a[1], b[1]) > 0.01:
return False
if y - min(c[1], d[1]) < -0.01 or y - max(c[1], d[1]) > 0.01:
return False
return True
'''
This function checks whether the line defined by p1 and p2 intersects with any of the obstacle line
Return: True if intersects
False if not
'''
def collisionChecker(p1, p2, obs):
flag = False
# Iterates to see if rrtLine intersects with any obstacles
for ob in obs:
if obsCheck(p1,p2,ob[0],ob[1])==True:
flag = True
return flag
'''
This function identifies the safe corners of the box Bk.
By safe I mean the corner is directly connectable from its center (xg, yg).
NOTE However, this way might not be the correct way to do it,
since directly connectable does not mean approachable by the dynamics.
Inputs: Bk - the box to deal with
obs - the list of obstacles
Output: a,b,c,d - booleans represents the safety of corners left-top, right-top, left-bottom, right-bottom
'''
def boxChecker(Bk, obs, winsize):
xg = Bk.xg
yg = Bk.yg
r = Bk.epsilon
# Four corner points coordinate
lt = Bk.lt
rt = Bk.rt
lb = Bk.lb
rb = Bk.rb
print 'lt, rt, lb, rb are ', lt, rt, lb, rb
print 'xg, yg', (xg, yg)
a,b,c,d = False, False, False, False
for ob in obs:
if obsCheck(lt, (xg,yg), ob[0], ob[1]):
a = True
if obsCheck(rt, (xg,yg), ob[0], ob[1]):
b = True
if obsCheck(lb, (xg,yg), ob[0], ob[1]):
c = True
if obsCheck(rb, (xg,yg), ob[0], ob[1]):
d = True
return a,b,c,d
'''
This function checks whether the given point p
can be conncected to the goal directly
'''
def connectChecker(p, goal, obs):
a = ((goal[1]+goal[0])/2, (goal[3]+goal[2])/2)
return not collisionChecker(p, a, obs)
def regionChecker(region, X):
xg = X[0]
yg = X[1]
theta = X[2]
for r in region:
box = r[0]
theta_range = r[1]
if xg <= box.right and xg >= box.left and yg <= box.bottom and yg >= box.top:
if theta <= theta_range[1]+np.pi/9 and theta >= theta_range[0]-np.pi/9:
return True
return False
if __name__ == '__main__':
# print randomChecker((1,0), ((16,-1), (16,3)))
obs = []
# obs.append(((50, 0), (50, 250)))
obs.append(((100, 250), (100, 500)))
# obs.append(((150, 0), (150, 250)))
obs.append(((200, 250), (200, 500)))
# obs.append(((250, 0), (250, 250)))
obs.append(((300, 250), (300, 500)))
# obs.append(((350, 0), (350, 250)))
obs.append(((400, 250), (400, 500)))
# obs.append(((450, 0), (450, 250)))