-
Notifications
You must be signed in to change notification settings - Fork 0
/
interface_octave.m
495 lines (427 loc) · 18 KB
/
interface_octave.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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
clc; clear;
pkg load image
% Clear all figures
FigList = findall(groot, 'Type', 'figure');
for iFig = 1:numel(FigList)
try
clf(FigList(iFig));
catch
% Nothing to do
end
end
system('./vbm');
%% Parser - parse settings
% Open the file for reading
fid = fopen('config/settings.config', 'r');
% Define the format string for textscan
formatSpec = '%s';
% Read the settings using textscan
settingsCell = textscan(fid, formatSpec, 'Delimiter', '=', 'CommentStyle', '#');
% Close the file
fclose(fid);
% Convert the cell array to a struct
settings = struct();
for i = 1:2:length(settingsCell{1})
key = settingsCell{1}(i);
value = settingsCell{1}(i+1);
% Check if the value is a number
if ~isnan(str2double(value{1}))
% Convert the value to a number
value = str2double(value{1});
end
% Save the key-value pair in the settings struct
if strcmp(key{1}, 'imagePath') || strcmp(key{1}, 'initialFrontline')
% Don't convert imagePath and initialFrontline to numbers
settings.(key{1}) = value{1};
else
settings.(key{1}) = value;
end
if strcmp(key{1}, 'initialFrontline')
settings.(key{1}) = eval(strrep(strrep(value{1}, '{', '['), '}', ']'));
end
end
% Original start and end points (works for point to point path planning,
% not intended to be part of the VBM. VBM gives paths to all grid points,
% which can be read as shown in an example below).
sp_o = [settings.initialFrontline(1), settings.initialFrontline(2)] + 1;
ep_o = [settings.target_x, settings.target_y] + 1;
%%
if settings.saveResults
%% Processed/used visibility field / occupancy grid
if settings.saveVisibilityField
filename_visibilityField = "output/visibilityField.txt";
T_visibilityField = importdata(filename_visibilityField, ' ');
visibilityField = T_visibilityField;
[nx, ny] = size(visibilityField);
figure(1)
set(gcf, 'Name', 'Visibility field (occupancy grid)')
mesh(visibilityField,'FaceLighting','phong','FaceColor','interp',...
'AmbientStrength',1.0, 'EdgeColor', 'interp');
colormap(gray)
view(0,90)
axis equal
axis([1 ny 1 nx])
hold on
grid off
set(gca, 'xtick', [-1e6 1e6]);
set(gca, 'ytick', [-1e6 1e6]);
set(gca,'LooseInset',get(gca,'TightInset'));
end
%% VBM
if settings.visibilityBasedSolver
filename_visibilityBased = "output/visibilityBased.txt";
T_visibilityBased = importdata(filename_visibilityBased, ' ');
visibilityBased = T_visibilityBased;
visibilityBased(isinf(visibilityBased)) = 0;
if settings.saveCameFrom
filename_visibilityBased_cameFrom = "output/visibilityBased_cameFrom.txt";
T_visibilityBased_cameFrom = importdata(filename_visibilityBased_cameFrom, ' ');
visibilityBased_cameFrom = T_visibilityBased_cameFrom + 1;
end
% VBM results
figure(2);
set(gcf, 'Name', 'Visibility-based solver')
clf
m = mesh(visibilityBased,'FaceLighting','phong','FaceColor','interp',...
'AmbientStrength',1.0, 'EdgeColor', 'interp');
colormap(jet)
minimum = min(min(visibilityBased));
maximum = max(max(visibilityBased));
visibilityBased(visibilityBased == 0) = maximum;
[nx, ny] = size(visibilityBased);
caxis([minimum maximum])
view(0,90)
axis equal
axis([1 ny 1 nx])
hold on
contour3(visibilityBased+1, linspace(minimum, maximum, 40), 'LineWidth', 3);
grid off
set(gca, 'xtick', [-1e6 1e6]);
set(gca, 'ytick', [-1e6 1e6]);
set(gca, 'ztick', [-1e6 1e6]);
set(gca,'LooseInset',get(gca,'TightInset'));
for i = 1:2:length(settings.initialFrontline)
piv = [settings.initialFrontline(i), settings.initialFrontline(i+1)] + 1;
plot3(piv(1), piv(2), visibilityBased(piv(2), piv(1))+50,'o',...
'MarkerFaceColor','green', 'MarkerEdgeColor','black',...
'MarkerSize', 24, 'LineWidth', 1)
end
% Plot light sources/pivots
if settings.saveLightSources
filename_visibilityBased_lightSources = "output/visibilityBased_lightSources.txt";
T_visibilityBased_lightSources = importdata(filename_visibilityBased_lightSources, ' ');
visibilityBased_lightSources = T_visibilityBased_lightSources;
visibilityBased_lightSources = visibilityBased_lightSources + 1;
plotlightSources = false;
if plotlightSources
for i = 1:size(visibilityBased_lightSources, 1)
pt_ = visibilityBased_lightSources(i,:);
plot3(pt_(1), pt_(2), maximum+10,'o',...
'MarkerFaceColor','white', 'MarkerEdgeColor','black',...
'MarkerSize', 10, 'LineWidth', 2)
end
end
end
% Enable if you want to plot a path read from the C matrix
% pt need not be ep_o, can be any other feasible point in
% the grid
if settings.saveCameFrom && settings.saveLightSources
if true
pt = ep_o;
if (ep_o(1) <= nx && ep_o(2) <= ny)
points = [pt];
while true
if pt(1) == sp_o(1) && pt(2) == sp_o(2)
break
end
pt = visibilityBased_lightSources(visibilityBased_cameFrom(pt(2), pt(1)), :);
points(end+1, :) = pt;
line([points(end,1),points(end-1,1)],...
[points(end,2),points(end-1,2)], ...
[visibilityBased(points(end,2), points(end,1))+1e2 ...
visibilityBased(points(end-1,2),points(end-1,1))+1e2],...
'LineWidth', 4, "color", "magenta")
end
end
end
end
% CameFrom matrix C
if (settings.saveCameFrom && settings.saveVisibilityField)
figure(3)
% Define the grid size and number of colors
gridSize = size(visibilityField, 1);
% gridSize = [nx, ny];
numColors = size(visibilityBased_lightSources, 1);
% Define the range of values for each color
colorRanges = linspace(1, numColors+1, numColors+1);
% Create a random grid with integer values between 1 and gridSize^2
% grid = randi(gridSize^2, gridSize, gridSize);
% grid = visibilityBased_cameFrom;
% Create a random obstacle matrix
obstacleMatrix = imcomplement(visibilityField);
% obstacleMatrix = randi([0,1], gridSize, gridSize);
% Create the colormap
% colors = hsv(randperm(numColors);
colorIndices = randperm(numColors);
colors = jet(numColors);
colors = colors(colorIndices, :);
% Display the grid with obstacles
imagesc(visibilityBased_cameFrom);
% mesh(visibilityBased_cameFrom,'FaceLighting','phong','FaceColor','interp',...
% 'AmbientStrength',1.0, 'EdgeColor', 'interp','FaceAlpha','1.0');
view(0,90)
% set(gca, 'YDir', 'reverse');
% mesh(visibilityBased_cameFrom);
colormap(colors);
% Create a black mask for the obstacle positions
mask = repmat(obstacleMatrix, [1, 1, 3]);
mask(:, :, 1) = mask(:, :, 1) * 0;
mask(:, :, 2) = mask(:, :, 2) * 0;
mask(:, :, 3) = mask(:, :, 3) * 0;
% Overlay the black mask on top of the grid
hold on;
h = image(mask);
set(h, 'AlphaData', obstacleMatrix);
grid off
set(gca, 'xtick', [-1e6 1e6]);
set(gca, 'ytick', [-1e6 1e6]);
set(gca, 'ztick', [-1e6 1e6]);
set(gca,'LooseInset',get(gca,'TightInset'));
set(gca, 'YDir','normal')
view(0,90)
axis equal
axis([1 ny 1 nx])
end
end
%% Euclidean distance function settings (ESDF)
if (settings.distanceFunction && settings.saveDistanceFunction)
filename_distanceFunction = "output/distanceFunction.txt";
T_distanceFunction = importdata(filename_distanceFunction, ' ');
distanceFunction = T_distanceFunction;
figure(4)
set(gcf, 'Name', 'Computed distance function')
clf
mesh(distanceFunction,'FaceLighting','phong','FaceColor','interp',...
'AmbientStrength',1.0, 'EdgeColor', 'interp');
colormap(jet)
view(0,90)
axis equal
axis([1 ny 1 nx])
hold on
minimum = min(min(distanceFunction));
maximum = max(max(distanceFunction));
contour3(distanceFunction, linspace(minimum, maximum, 15), 'LineWidth', 3);
shading flat
grid off
set(gca, 'xtick', [-1e6 1e6]);
set(gca, 'ytick', [-1e6 1e6]);
set(gca,'LooseInset',get(gca,'TightInset'));
end
%% Astar settings
if settings.astar
if settings.savePath
filename_astar_path = "output/astar_path.txt";
T_astar_path = importdata(filename_astar_path, ' ');
astar_path = T_astar_path + 1;
end
if settings.saveFScore
filename_astar_fScore = "output/astar_fScore.txt";
T_astar_fScore = importdata(filename_astar_fScore, ' ');
astar_fScore = T_astar_fScore;
figure(5)
set(gcf, 'Name', 'AStar FScore')
% mesh(astar_fScore,'FaceLighting','phong','FaceColor','interp','AmbientStrength',1.0, 'interp');
mesh(astar_fScore);
colormap(jet)
view(0,90)
axis equal
axis([1 ny 1 nx])
hold on
astar_fScore(isinf(astar_fScore)) = 0;
minimum = min(min(astar_fScore));
maximum = max(max(astar_fScore));
contour3(astar_fScore+1, linspace(minimum, maximum, 40), 'LineWidth', 3);
grid off
set(gca, 'xtick', [-1e6 1e6]);
set(gca, 'ytick', [-1e6 1e6]);
set(gca,'LooseInset',get(gca,'TightInset'));
plot3(ep_o(1), ep_o(2), astar_fScore(ep_o(2), ep_o(1))+1e2,'o',...
'MarkerFaceColor','red', 'MarkerEdgeColor','black',...
'MarkerSize', 24, 'LineWidth', 3)
plot3(sp_o(1), sp_o(2), astar_fScore(sp_o(2), sp_o(1))+1e2,'o',...
'MarkerFaceColor','green', 'MarkerEdgeColor','black',...
'MarkerSize', 24, 'LineWidth', 3)
end
if settings.saveGScore
filename_astar_gScore = "output/astar_gScore.txt";
T_astar_gScore = importdata(filename_astar_gScore, ' ');
astar_gScore = T_astar_gScore ;
d_astar = 0;
elevation = zeros(size(astar_path, 1), 1);
pt_old = astar_path(1,:);
for i = 1:size(astar_path,1)
pt = astar_path(i,:);
d_astar = d_astar + norm(pt-pt_old);
elevation(i) = astar_fScore(pt(end,2), pt(end,1))+10;
pt_old = pt;
end
figure(6)
set(gcf, 'Name', 'AStar gScore')
clf
mesh(astar_gScore,'FaceLighting','phong','FaceColor','interp',...
'AmbientStrength',1.0, 'EdgeColor', 'interp');
colormap(jet)
view(0,90)
axis equal
axis([1 ny 1 nx])
hold on
astar_gScore(isinf(astar_gScore)) = 0;
minimum = min(min(astar_gScore));
maximum = max(max(astar_gScore));
contour3(astar_gScore+2, linspace(minimum, maximum, 40), 'LineWidth', 3);
grid off
set(gca, 'xtick', [-1e6 1e6]);
set(gca, 'ytick', [-1e6 1e6]);
set(gca,'LooseInset',get(gca,'TightInset'));
plot3(astar_path(:,1), astar_path(:,2), elevation,...
'Color', 'magenta', 'LineWidth', 3);
plot3(ep_o(1), ep_o(2), astar_gScore(ep_o(2), ep_o(1))+1e3,'o',...
'MarkerFaceColor','red', 'MarkerEdgeColor','black',...
'MarkerSize', 24, 'LineWidth', 1)
plot3(sp_o(1), sp_o(2), astar_gScore(sp_o(2), sp_o(1))+1e3,'o',...
'MarkerFaceColor','green', 'MarkerEdgeColor','black',...
'MarkerSize', 24, 'LineWidth', 3)
end
end
%% Vstar settings
if settings.vstar
if settings.saveFScore
filename_vstar_fScore = "output/vstar_fScore.txt";
T_vstar_fScore = importdata(filename_vstar_fScore, ' ');
vstar_fScore = T_vstar_fScore ;
figure(7)
set(gcf, 'Name', 'VStar FScore')
clf
% mesh(vstar_fScore,'FaceLighting','phong','FaceColor','interp',...
% 'AmbientStrength',1.0, 'interp');
mesh(vstar_fScore);
colormap(jet)
view(0,90)
axis equal
axis([1 ny 1 nx])
hold on
minimum = min(min(vstar_fScore));
vstar_fScore(isinf(vstar_fScore)) = 0;
maximum = max(max(vstar_fScore));
contour3(vstar_fScore+1, linspace(minimum, maximum, 5), 'LineWidth', 3);
grid off
set(gca, 'xtick', [-1e6 1e6]);
set(gca, 'ytick', [-1e6 1e6]);
set(gca,'LooseInset',get(gca,'TightInset'));
plot3(ep_o(1), ep_o(2), vstar_fScore(ep_o(2), ep_o(1))+1e2,'o',...
'MarkerFaceColor','red', 'MarkerEdgeColor','black',...
'MarkerSize', 24, 'LineWidth', 3)
plot3(sp_o(1), sp_o(2), vstar_fScore(sp_o(2), sp_o(1))+1e2,'o',...
'MarkerFaceColor','green', 'MarkerEdgeColor','black',...
'MarkerSize', 24, 'LineWidth', 3)
end
if settings.saveGScore
filename_vstar_gScore = "output/vstar_gScore.txt";
T_vstar_gScore = importdata(filename_vstar_gScore, ' ');
vstar_gScore = T_vstar_gScore ;
vstar_gScore(isinf(vstar_gScore)) = 0;
figure(8)
set(gcf, 'Name', 'VStar GScore')
% minimum = min(min(vstar_gScore));
% maximum = max(max(vstar_gScore));
%
% vstar_gScore(vstar_gScore==0) = inf;
% mesh(vstar_gScore,'FaceLighting','phong','FaceColor','interp',...
% 'AmbientStrength',1.0, 'interp');
mesh(vstar_gScore);
colormap(jet)
view(0,90)
axis equal
axis([1 ny 1 nx])
hold on
contour3(vstar_gScore+4, linspace(minimum, maximum, 40), 'LineWidth', 3);
grid off
set(gca, 'xtick', [-1e6 1e6]);
set(gca, 'ytick', [-1e6 1e6]);
set(gca,'LooseInset',get(gca,'TightInset'));
plot3(ep_o(1), ep_o(2), vstar_gScore(ep_o(2), ep_o(1))+1e2,'o',...
'MarkerFaceColor','red', 'MarkerEdgeColor','black',...
'MarkerSize', 24, 'LineWidth', 3)
plot3(sp_o(1), sp_o(2), vstar_gScore(sp_o(2), sp_o(1))+1e2,'o',...
'MarkerFaceColor','green', 'MarkerEdgeColor','black',...
'MarkerSize', 24, 'LineWidth', 3)
if settings.savePath
filename_vstar_path = "output/vstar_path.txt";
T_vstar_path = importdata(filename_vstar_path, ' ');
vstar_path = T_vstar_path + 1;
d_vstar = 0;
elevation = zeros(size(vstar_path,1), 1);
pt_old = vstar_path(1,:);
for i = 1:size(vstar_path,1)
pt = vstar_path(i,:);
d_vstar = d_vstar + norm(pt-pt_old);
elevation(i) = vstar_gScore(pt(end,2), pt(end,1))+10;
pt_old = pt;
end
plot3(vstar_path(:,1), vstar_path(:,2), elevation,...
'Color', 'magenta', 'LineWidth', 3);
end
end
waitfor(gcf)
%% Misc
if settings.saveLightSources
filename_vstar_lightSources = "output/vstar_lightSources.txt";
T_vstar_lightSources = importdata(filename_vstar_lightSources, ' ');
vstar_lightSources = T_vstar_lightSources + 1;
end
end
end
%% MSFM
% if settings.msfm && settings.saveResults && settings.saveVisibilityField
% addpath(genpath("fast_marching"))
% % Load a maze image
% I1 = visibilityField;
% [nx, ny] = size(I1);
% % Convert the image to a speed map
% SpeedImage = I1;
% % I1; % imcomplement(I1)*2;
% % Set the source to end of the maze
% SourcePoint = [sp_o(2); sp_o(1)];
% % SourcePoint = [582; 618];
% % Calculate the distance map (distance to source)
% tic
% % DistanceMap = msfm2d(SpeedImage, SourcePoint, true, true);
% DistanceMap = fastmarching(SpeedImage, SourcePoint, true, true);
% toc
% max_lim = 1000;
% DistanceMap(DistanceMap>max_lim) = 0;
% % Show the distance map
% % figure, imshow(DistanceMap,[0 3400])
% figure(9)
% clf
% set(gcf, 'Name', 'MSFM solution')
% mesh(DistanceMap,'FaceLighting','phong','FaceColor','interp',...
% 'AmbientStrength',1.0, 'interp');
% colormap(jet)
% % caxis([0 800])
% view(0,90)
% axis equal
% axis([1 ny 1 nx])
% hold on
% DistanceMap(isinf(DistanceMap)) = 0;
% minimum = min(min(DistanceMap));
% maximum = max_lim; % 800; % max(max(DistanceMap));
% contour3(DistanceMap, linspace(minimum, maximum, 60), 'LineWidth', 2);
% grid off
% set(gca, 'xtick', [-1e6 1e6]);
% set(gca, 'ytick', [-1e6 1e6]);
% set(gca,'LooseInset',get(gca,'TightInset'));
% end
%% Expfig - sample usage
% addpath(genpath('expfig'))
% export_fig test_image -r250 -transparent -png