-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlanner.py
57 lines (44 loc) · 1.61 KB
/
Planner.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
import numpy as np
class MyPlanner:
__slots__ = ['boundary', 'blocks']
def __init__(self, boundary, blocks):
self.boundary = boundary
self.blocks = blocks
def plan(self,start,goal):
path = [start]
numofdirs = 26
[dX,dY,dZ] = np.meshgrid([-1,0,1],[-1,0,1],[-1,0,1])
dR = np.vstack((dX.flatten(),dY.flatten(),dZ.flatten()))
dR = np.delete(dR,13,axis=1)
dR = dR / np.sqrt(np.sum(dR**2,axis=0)) / 2.0
for _ in range(2000):
mindisttogoal = 1000000
node = None
for k in range(numofdirs):
next = path[-1] + dR[:,k]
# Check if this direction is valid
if( next[0] < self.boundary[0,0] or next[0] > self.boundary[0,3] or \
next[1] < self.boundary[0,1] or next[1] > self.boundary[0,4] or \
next[2] < self.boundary[0,2] or next[2] > self.boundary[0,5] ):
continue
valid = True
for k in range(self.blocks.shape[0]):
if( next[0] > self.blocks[k,0] and next[0] < self.blocks[k,3] and\
next[1] > self.blocks[k,1] and next[1] < self.blocks[k,4] and\
next[2] > self.blocks[k,2] and next[2] < self.blocks[k,5] ):
valid = False
break
if not valid:
continue
# Update next node
disttogoal = sum((next - goal)**2)
if( disttogoal < mindisttogoal):
mindisttogoal = disttogoal
node = next
if node is None:
break
path.append(node)
# Check if done
if sum((path[-1]-goal)**2) <= 0.1:
break
return np.array(path)