Skip to content

Commit ea65361

Browse files
committed
[web] implement finding point in grid, search time down to a few ms
1 parent dfc0976 commit ea65361

File tree

3 files changed

+47
-15
lines changed

3 files changed

+47
-15
lines changed

pre/src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,9 @@ fn main() {
203203
latitude,
204204
longitude,
205205
};
206-
let lat_grid = (latitude * GRID_MULTIPLICATOR as f32) as i32;
207-
let lng_grid = (longitude * GRID_MULTIPLICATOR as f32) as i32;
208-
let current_grid = grid.get_mut(&(lat_grid as usize, lng_grid as usize));
206+
let lat_grid = (latitude * GRID_MULTIPLICATOR as f32) as usize;
207+
let lng_grid = (longitude * GRID_MULTIPLICATOR as f32) as usize;
208+
let current_grid = grid.get_mut(&(lat_grid, lng_grid));
209209
match current_grid {
210210
Some(id_list) => {
211211
id_list.push(*our_id);

web/src/graph.rs

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,20 @@ impl Graph {
4848

4949
/// returns closes point of given long & lat
5050
pub fn get_point_id(&self, lat: f32, long: f32) -> usize {
51-
let lat_grid = (lat * GRID_MULTIPLICATOR as f32) as i32;
52-
let lng_grid = (long * GRID_MULTIPLICATOR as f32) as i32;
5351
let mut min_distance: f32 = std::f32::MAX;
5452
let mut min_distance_id: usize = 0;
55-
56-
for i in 0..self.nodes.len() {
57-
let distance =
58-
calc_distance(lat, long, self.nodes[i].latitude, self.nodes[i].longitude);
59-
if distance < min_distance {
60-
min_distance = distance;
61-
min_distance_id = i;
53+
let adjacent_nodes = self.get_adjacent_node_ids(lat, long);
54+
for node_id in adjacent_nodes {
55+
match self.nodes.get(node_id) {
56+
Some(node) => {
57+
let distance =
58+
calc_distance(lat, long, node.latitude, node.longitude);
59+
if distance < min_distance {
60+
min_distance = distance;
61+
min_distance_id = node_id;
62+
}
63+
},
64+
None => continue
6265
}
6366
}
6467
return min_distance_id;
@@ -81,8 +84,37 @@ impl Graph {
8184
}
8285
}
8386

84-
fn get_adjacent_grids(current_grid: (usize, usize), grid: &HashMap<(usize, usize), Vec<usize>>) {
85-
87+
/// returns node_ids in adjacent grid cells
88+
/// goes from most inner cell to cells with distance 1 to n until a node is found
89+
fn get_adjacent_node_ids(&self, lat: f32, lng: f32) -> Vec<usize> {
90+
let lat_grid = (lat * GRID_MULTIPLICATOR as f32) as usize;
91+
let lng_grid = (lng * GRID_MULTIPLICATOR as f32) as usize;
92+
let mut node_ids = Vec::<usize>::new();
93+
match self.grid.get(&(lat_grid, lng_grid)) {
94+
Some(adjacent_node_ids) => node_ids.extend(adjacent_node_ids),
95+
None => ()
96+
}
97+
let mut in_dist: usize = 1;
98+
loop {
99+
for x in lat_grid-in_dist..lat_grid+in_dist+1 {
100+
for y in lng_grid-in_dist..lng_grid+in_dist+1 {
101+
if (x < lat_grid+in_dist || x > lat_grid-in_dist) && (y < lng_grid-in_dist || y > lng_grid+in_dist) {
102+
// both coordinates are bigger or smaller than the outer bounds => in inner square => already investigated
103+
continue;
104+
}
105+
match self.grid.get(&(x,y)) {
106+
Some(adjacent_node_ids) => node_ids.extend(adjacent_node_ids),
107+
None => continue
108+
}
109+
}
110+
}
111+
if node_ids.len() > 0 {
112+
return node_ids
113+
} else {
114+
// search in next level
115+
in_dist += 1;
116+
}
117+
}
86118
}
87119

88120
/// executes dijkstra

web/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ fn main() {
121121
// read file
122122
let mut f = BufReader::new(File::open(filename).unwrap());
123123
let input: Input = deserialize_from(&mut f).unwrap();
124-
let d = Graph::new(input.nodes, input.ways, input.offset);
124+
let d = Graph::new(input.nodes, input.ways, input.offset, input.grid);
125125

126126
let graph = web::Data::new(d);
127127

0 commit comments

Comments
 (0)