-
Notifications
You must be signed in to change notification settings - Fork 1
/
roadmap_from_decomposition_algorithm.m
106 lines (91 loc) · 3.83 KB
/
roadmap_from_decomposition_algorithm.m
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
function [AdjTable,nodes,start_node,goal_node] = roadmap_from_decomposition_algorithm(T,start,goal)
% input: free workspace, start point, goal point
% output: a roadmap represented as an adjacency table, list of nodes, the
% start node, and goal node
% find and plot midpoints
midpoints = [];
for i = 1:length(T)
rows = find(T{i}(:,1) == (max(T{i}(:,1))));
s = T{i}(rows,:);
for j = 1:length(s)-1
p1 = s(1,:);
p2 = s(j+1,:);
if p1(1,1) == p2(1,1) && p1(1,1) ~= 0 && p1(1,1) ~= 200
midpoints = [midpoints; [p1(1,1), (p1(1,2)+p2(1,2))/2]];
end
end
end
plot(midpoints(:,1), midpoints(:,2), '.', 'Color', 'k', 'MarkerSize', 12)
% find and plot centroids
centroids = [];
for i = 1:length(T)
[c_x, c_y] = centroid(polyshape(T{i}(:,1), T{i}(:,2)));
centroids = [centroids; c_x c_y];
end
plot(centroids(:,1), centroids(:,2), 'd', 'Color', 'k')
% for each trapezoid connect the center to all the midpoints
start_T = [];
goal_T = [];
nodes = [];
for i = 1:length(T)
% plot trapezoids
plot(polyshape(T{i}(:,1),T{i}(:,2)),'FaceColor','w')
edge = [];
nodes = [nodes; centroids(i,:)];
AdjTable{length(nodes)} = [];
% find all midpoints located on current trapezoid
[mid_in, mid_on] = inpolygon(midpoints(:,1), midpoints(:,2), T{i}(:,1), T{i}(:,2));
in_on = [mid_in, mid_on];
T_midpoints = midpoints(find(ismember(in_on, [1, 1], 'rows')),:);
% connections between centers and midpoints
for j = 1:size(T_midpoints,1)
if T_midpoints(j,1) > centroids(i,1)
nodes = [nodes; T_midpoints(j,:)];
AdjTable{length(nodes)} = [];
end
p1 = T_midpoints(j,:);
p2 = centroids(i,:);
n1 = find(ismember(nodes, p1,'rows'));
n2 = find(ismember(nodes, p2,'rows'));
AdjTable{n1}(end+1) = n2;
AdjTable{n2}(end+1) = n1;
plot([p1(1,1), p2(1,1)],[p1(1,2), p2(1,2)], '--', 'Color','k')
end
% connect start and goal points to roadmap
[start_in, start_on] = inpolygon(start(1), start(2), T{i}(:,1), T{i}(:,2));
[goal_in, goal_on] = inpolygon(goal(1), goal(2), T{i}(:,1), T{i}(:,2));
if start_in == 1 && start_on == 0
plot([centroids(i,1) start(1)], [centroids(i,2) start(2)], '--', 'Color', 'r')
start_centroid = centroids(i,:);
start_trap = i;
end
if goal_in == 1 && goal_on == 0
plot([centroids(i,1) goal(1)], [centroids(i,2) goal(2)], '--', 'Color', 'r')
goal_centroid = centroids(i,:);
goal_trap = 1;
end
% if start or goal on vertical segment store corresponding trapezoids
if start_in == 1 && start_on == 1
start_T = [start_T; i];
end
if goal_in == 1 && goal_on == 1
goal_T = [goal_T; i];
end
end
% if start on vertical segment connect to centroid of minimum distance
if size(start_T) > 0
d_start = [pdist([centroids(start_T(1),:); start], 'euclidean');pdist([centroids(start_T(2),:); start], 'euclidean')];
start_trap = start_T(find(d_start == min(d_start)));
start_centroid = centroids(start_trap,:);
plot([centroids(start_trap,1) start(1)], [centroids(start_trap,2) start(2)], '--', 'Color', 'r');
end
% if goal on vertical segment connect to centroid of minimum distance
if size(goal_T) > 0
d_goal = [pdist([centroids(goal_T(1),:); goal], 'euclidean');pdist([centroids(goal_T(2),:); goal], 'euclidean')];
goal_trap = goal_T(find(d_goal == min(d_goal)));
goal_centroid = centroids(goal_trap,:);
plot([centroids(goal_trap,1) goal(1)], [centroids(goal_trap,2) goal(2)], '--', 'Color', 'r');
end
% identify start and goal node
start_node = find(ismember(nodes, start_centroid,'rows'));
goal_node = find(ismember(nodes, goal_centroid,'rows'));