-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcolinear.py
53 lines (43 loc) · 1.46 KB
/
colinear.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
# coding: utf-8
# ## Finding Your Way In The City
#
# In this notebook you'll combine the work of previous exercises to calculate a minimal series of waypoints in order to get from a start location to a goal location.
#
# You'll reuse and modify your algorithms from:
#
# - A*
# - Configuration Space
# - Collinearity and/or Bresenham
# In[21]:
import numpy as np
import matplotlib.pyplot as plt
from grid import create_grid
from planning import a_star
from bresenham import bresenham
def point(p):
return np.array([p[0], p[1], 1.]).reshape(1, -1)
def collinearity_check(p1, p2, p3, epsilon=1e-6):
m = np.concatenate((p1, p2, p3), 0)
det = np.linalg.det(m)
return abs(det) < epsilon
def prune_path(actual_path):
pruned_path = [p for p in actual_path]
# TODO: prune the path!
i = 0
while i < len(pruned_path) - 2:
p1 = point(pruned_path[i])
p2 = point(pruned_path[i+1])
p3 = point(pruned_path[i+2])
#If the 3 points are in a line remove
# the 2nd point.
# The 3rd point now becomes and 2nd point
# and the check is redone with a new third point
# on the next iteration.
if collinearity_check(p1, p2, p3):
# Something subtle here but we can mutate
# `pruned_path` freely because the length
# of the list is check on every iteration.
pruned_path.remove(pruned_path[i+1])
else:
i += 1
return pruned_path