-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfindDubinsTourCost.m
110 lines (88 loc) · 2.44 KB
/
findDubinsTourCost.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
107
108
109
110
function [c, E] = findDubinsTourCost(V, X, E, pathOptions)
% FINDDUBINSTOURCOST find the cost of the dubins tour through V
%
% Params:
% V A n-by-2 matrix of vertices.
% X An n-by-1 vector of headings.
% E An m-by-3 matrix of edges.
% pathOptions Path options.
%
%============= Input Validation ===============
if nargin < 1
error('No input arguments given!');
elseif nargin > 4
error('Too many arguments given!');
end
if isempty(V)
error('V is empty!');
end
UV = unique(V,'rows');
if ~isequal(size(V),size(UV))
error('V contains duplicate vertices.');
end
[n, ~] = size(V);
if (isempty(X) || min(X) < 0 || max(X) >= 2*pi)
error('x is invalid!');
end
if (length(X) ~= n)
error('Expected size of X to match number of rows in V.');
end
UE = unique(E,'rows');
if ~isequal(size(E),size(UE))
error('E contains duplicate edges.');
end
if (isempty(E))
error('E is empty!');
end
[m, ~] = size(E);
m = m - 1 + strcmp(pathOptions.Circuit, 'on'); % number of edges
%m_expected = n - 1 + strcmp(pathOptions.Circuit, 'on'); % number of edges
%if (m ~= m_expected)
% error('Expected E to have m edges!');
%end
if (E(1,1) ~= 1)
error('Expected tour to start at origin!');
end
if exist('pathOptions','var') && ~isa(pathOptions, 'PathOptions')
error('pathOptions is not a PathOptions object!');
end
if ~exist('pathOptions','var')
pathOptions = PathOptions;
end
%================= Solve ===================
c = 0;
V_visited = zeros(1, n);
if strcmp(pathOptions.Debug,'on')
fprintf('Finding Dubins tour cost with (%d nodes and %d edges).\n',...
n, m);
end
% From the start position, traverse all vertices
position = V(1,:);
heading = X(1);
V_visited(1) = 1;
for i=1:m
e = E(i,:);
src_idx = e(1);
targ_idx = e(2);
u = V(src_idx,:); % should already be at this position & heading
v = V(targ_idx,:);
xv = X(targ_idx);
c_i = findPTPCost(position, heading, v, xv, pathOptions.TurnRadius);
E(i,3) = c_i;
c = c + c_i;
% Update position
V_visited(targ_idx) = 1;
position = v;
heading = xv;
end % for i
% if (~isempty(find(V_visited < 1)))
% if strcmp(pathOptions.Debug,'on')
% disp('Skipped: ');
% find(V_visited < 1)
% end
% error('Path in E was not a complete tour!');
% end
if strcmp(pathOptions.Debug,'on')
fprintf('Found Dubins tour cost: %0.2f\n', c);
end
end % function findDubinsTourCost