Skip to content

Commit 06eefab

Browse files
committed
[web] optimize get_point_id
1 parent cc4681d commit 06eefab

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

web/src/graph.rs

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -93,27 +93,35 @@ impl Graph {
9393
/// returns node_ids in adjacent grid cells
9494
/// goes from most inner cell to cells with distance 1 to n until a node is found
9595
fn get_adjacent_node_ids(&self, lat: f32, lng: f32) -> Vec<usize> {
96-
let lat_grid = (lat * GRID_MULTIPLICATOR as f32) as usize;
97-
let lng_grid = (lng * GRID_MULTIPLICATOR as f32) as usize;
96+
let lat_grid = (lat * GRID_MULTIPLICATOR as f32) as i32;
97+
let lng_grid = (lng * GRID_MULTIPLICATOR as f32) as i32;
9898
let mut node_ids = Vec::<usize>::new();
99-
match self.grid.get(&(lat_grid, lng_grid)) {
99+
match self.grid.get(&(lat_grid as usize, lng_grid as usize)) {
100100
Some(adjacent_node_ids) => node_ids.extend(adjacent_node_ids),
101101
None => (),
102102
}
103-
let mut in_dist: usize = 1;
103+
let mut in_dist: i32 = 1;
104104
loop {
105-
for x in lat_grid - in_dist..lat_grid + in_dist + 1 {
106-
for y in lng_grid - in_dist..lng_grid + in_dist + 1 {
107-
if (x < lat_grid + in_dist || x > lat_grid - in_dist)
108-
&& (y < lng_grid - in_dist || y > lng_grid + in_dist)
109-
{
110-
// both coordinates are bigger or smaller than the outer bounds => in inner square => already investigated
111-
continue;
112-
}
113-
match self.grid.get(&(x, y)) {
114-
Some(adjacent_node_ids) => node_ids.extend(adjacent_node_ids),
115-
None => continue,
116-
}
105+
for i in -in_dist..in_dist {
106+
// top row left to right (increasing x, fix y)
107+
match self.grid.get(&((lat_grid+i) as usize, (lng_grid+in_dist) as usize)) {
108+
Some(adjacent_node_ids) => node_ids.extend(adjacent_node_ids),
109+
None => continue,
110+
}
111+
// right column top to bottom (fix x, decreasing y)
112+
match self.grid.get(&((lat_grid+in_dist) as usize, (lng_grid-i) as usize)) {
113+
Some(adjacent_node_ids) => node_ids.extend(adjacent_node_ids),
114+
None => continue,
115+
}
116+
// bottom row right to left (decreasing x, fix y)
117+
match self.grid.get(&((lat_grid-i) as usize, (lng_grid-in_dist) as usize)) {
118+
Some(adjacent_node_ids) => node_ids.extend(adjacent_node_ids),
119+
None => continue,
120+
}
121+
// left column bottom to top (fix x, increasing y)
122+
match self.grid.get(&((lat_grid-in_dist) as usize, (lng_grid+i) as usize)) {
123+
Some(adjacent_node_ids) => node_ids.extend(adjacent_node_ids),
124+
None => continue,
117125
}
118126
}
119127
if node_ids.len() > 0 {

web/src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,10 @@ fn query(request: web::Json<Query>, dijkstra: web::Data<Graph>) -> web::Json<Res
7171
// println!("travel_type: {}, by_distance: {}", travel_type, by_distance);
7272

7373
// search for clicked points
74+
let timing_find = Instant::now();
7475
let start_id: usize = dijkstra.get_point_id(start.latitude, start.longitude, travel_type);
7576
let end_id: usize = dijkstra.get_point_id(end.latitude, end.longitude, travel_type);
77+
println!("### duration for get_point_id(): {:?}", timing_find.elapsed());
7678

7779
let timing = Instant::now();
7880
let tmp = dijkstra.find_path(start_id, end_id, travel_type, by_distance);

0 commit comments

Comments
 (0)