-
Notifications
You must be signed in to change notification settings - Fork 8
/
dijkstra.rs
205 lines (174 loc) · 6.5 KB
/
dijkstra.rs
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
// Dijkstra path planning
// author: Salah Eddine Ghamri (s.ghamri)
use gnuplot::*;
use std::thread::sleep;
use std::time::Duration;
use itertools::iproduct;
use std::collections::BTreeMap;
use ordered_float::NotNan;
use std::cmp::Reverse;
use std::collections::BinaryHeap;
use na::DMatrix;
extern crate nalgebra as na;
use rust_robotics::grid_map;
// The neighbors function returns adjacent cell coordinates for a given cell in a grid
// while excluding out-of-bound and the cell itself.
fn calculate_neighbors(nrows: usize, ncols: usize) -> impl Fn(usize, usize) -> Vec<(usize, usize)> {
move |i, j| {
let delta = [-1, 0, 1];
let neighbors: Vec<_> = iproduct!(delta, delta)
.filter_map(|(di, dj)| {
let ni = i as i32 + di;
let nj = j as i32 + dj;
if ni >= 0
&& ni < nrows as i32
&& nj >= 0
&& nj < ncols as i32
&& (ni, nj) != (i as i32, j as i32)
{
Some((ni as usize, nj as usize))
} else {
None
}
})
.collect();
neighbors
}
}
fn has_collision(obstacle_map: &grid_map::Map, ni: usize, nj: usize) -> bool {
let (height, width) = obstacle_map.shape();
let get_neighbors = calculate_neighbors(height, width);
let neighbors = get_neighbors(ni, nj);
if neighbors.iter().all(|&(i, j)| obstacle_map[(i, j)] != 1) && obstacle_map[(ni, nj)] != 1 {
return false;
}
true
}
fn dijkstra_planner(obstacle_map: &grid_map::Map, start: &(usize, usize), goal: &(usize, usize)) {
let (height, width) = obstacle_map.shape();
let get_neighbors = calculate_neighbors(height, width);
let mut dist = BTreeMap::new();
let mut prev: BTreeMap<(usize, usize), Option<(usize, usize)>> = BTreeMap::new();
let mut pq = BinaryHeap::new();
let (mut ci, mut cj) = start;
pq.push((Reverse(NotNan::new(0.0).unwrap()), (ci, cj)));
dist.insert((ci, cj), 0.0 as f64);
let mut fg = Figure::new();
let mut map_x = vec![];
let mut map_y = vec![];
let mut neighbors_x = vec![];
let mut neighbors_y = vec![];
let mut visited_x = vec![];
let mut visited_y = vec![];
let mut path_x = vec![];
let mut path_y = vec![];
for i in 0..obstacle_map.nrows() {
for j in 0..obstacle_map.ncols() {
if obstacle_map[(i, j)] == 1 {
map_x.push(j as i32);
map_y.push(-(i as i32));
}
}
}
while let Some((distance, current)) = pq.pop() {
if current != *goal {
(ci, cj) = current;
visited_x.push(cj as i32);
visited_y.push(-(ci as i32));
let neighbors = get_neighbors(ci, cj);
neighbors_x.clear();
neighbors_y.clear();
for (ni, nj) in neighbors {
// NOTE: in practice add a collision check with robots radius here
if !has_collision(obstacle_map, ni, nj) {
neighbors_x.push(nj as i32);
neighbors_y.push(-(ni as i32));
let delta_x = (ci as f64 - ni as f64) as f64;
let delta_y = (cj as f64 - nj as f64) as f64;
let edge_distance = (delta_x * delta_x + delta_y * delta_y).sqrt();
let alt = distance.0.into_inner() + edge_distance;
if *dist.get(&(ni, nj)).unwrap_or(&f64::MAX) > alt {
dist.insert((ni, nj), alt);
prev.insert((ni, nj), Some((ci, cj)));
pq.push((Reverse(NotNan::new(alt).unwrap()), (ni, nj)));
}
}
}
} else {
let mut path = vec![current];
while let Some(previous) = prev.get(path.last().unwrap()).unwrap_or(&None) {
path.push(*previous);
if *path.last().unwrap() == *start {
path.reverse();
path_x.clear();
path_y.clear();
for point in path {
path_x.push(point.1 as i32);
path_y.push(-(point.0 as i32));
}
break;
}
}
}
fg.clear_axes();
fg.axes2d()
.set_x_range(Fix(0.0), Fix(width as f64))
.set_y_range(Fix(-(height as f64)), Fix(1.0))
.points(
map_x.iter(),
map_y.iter(),
&[PointSymbol('S'), Color("black"), PointSize(7.5)],
)
.points(
Some(start.1 as i32),
Some(-(start.0 as i32)),
&[PointSymbol('O'), Color("red"), PointSize(3.0)],
)
.points(
Some(goal.1),
Some(-(goal.0 as i32)),
&[PointSymbol('O'), Color("red"), PointSize(3.0)],
)
.points(
&neighbors_x,
&neighbors_y,
&[PointSymbol('S'), Color("gray"), PointSize(2.0)],
)
.points(
&visited_x,
&visited_y,
&[PointSymbol('S'), Color("green"), PointSize(2.5)],
)
.lines(&path_x, &path_y, &[Color("black"), LineWidth(3.0)]);
let crate_dir = option_env!("CARGO_MANIFEST_DIR").unwrap();
fg.show_and_keep_running().unwrap();
sleep(Duration::from_millis(10));
if path_x.len() > 1 {
let _ = fg.save_to_svg(
format!("{}/img/dijkstra_planner.svg", crate_dir).as_str(),
800,
600,
);
break;
}
}
}
fn main() {
#[rustfmt::skip]
let original_matrix = DMatrix::from_row_slice(10, 14, &[
0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0, 0, 0 ,0 ,0 ,0, 0, 0,
0, 0, 0, 0, 1, 0, 0, 0, 0 ,0 ,0 ,0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 1 ,0 ,0 ,0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 1 ,0 ,0 ,0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 1 ,0 ,0 ,0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 1 ,0 ,0 ,0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0
]);
let obstacle_map = grid_map::Map::new(original_matrix, 3).unwrap();
let start = (28, 5);
let goal = (28, 28);
dijkstra_planner(&obstacle_map, &start, &goal);
}