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
use std::ops::Add; use num_traits::{NumCast, One, Zero}; use direction::*; use grid_2d::*; use grid::*; use error::*; use metadata::*; use search::*; use path; fn manhatten_distance<Cost>(a: Coord, b: Coord) -> Cost where Cost: NumCast, { let as_i32 = (a.x - b.x).abs() + (a.y - b.y).abs(); NumCast::from(as_i32).expect("Failed to cast to Cost") } fn has_forced_neighbour<G>(grid: &G, coord: Coord, direction: CardinalDirection) -> bool where G: SolidGrid, { if grid.is_solid_or_outside(coord + direction.left135().coord()) { return !grid.is_solid_or_outside(coord + direction.left90().coord()); } if grid.is_solid_or_outside(coord + direction.right135().coord()) { return !grid.is_solid_or_outside(coord + direction.right90().coord()); } false } fn jump_to_jump_point<G>(grid: &G, coord: Coord, direction: CardinalDirection, goal: Coord) -> bool where G: SolidGrid, { let neighbour_coord = coord + direction.coord(); if grid.is_solid_or_outside(neighbour_coord) { return false; } if neighbour_coord == goal { return true; } if has_forced_neighbour(grid, neighbour_coord, direction) { return true; } jump_to_jump_point(grid, neighbour_coord, direction, goal) } fn jump<G, Cost>( grid: &G, coord: Coord, direction: CardinalDirection, goal: Coord, ) -> Option<(Coord, Cost)> where G: SolidGrid, Cost: Add<Cost, Output = Cost> + One, { let neighbour_coord = coord + direction.coord(); if grid.is_solid_or_outside(neighbour_coord) { return None; } if neighbour_coord == goal { return Some((neighbour_coord, One::one())); } if has_forced_neighbour(grid, neighbour_coord, direction) { return Some((neighbour_coord, One::one())); } if jump_to_jump_point(grid, neighbour_coord, direction.left90(), goal) || jump_to_jump_point(grid, neighbour_coord, direction.right90(), goal) { return Some((neighbour_coord, One::one())); } jump(grid, neighbour_coord, direction, goal) .map(|(coord, cost): (Coord, Cost)| (coord, cost + One::one())) } impl<Cost: Copy + Add<Cost> + PartialOrd<Cost> + NumCast + Zero + One> SearchContext<Cost> { fn expand<G>( &mut self, grid: &G, current_coord: Coord, current_cost: Cost, direction: CardinalDirection, goal: Coord, ) where G: SolidGrid, { if let Some((successor_coord, successor_cost)) = jump(grid, current_coord, direction, goal) { self.see_successor( current_cost + successor_cost, successor_coord, direction.direction(), manhatten_distance, goal, ); } } pub fn jump_point_search_cardinal_manhatten_distance_heuristic<G>( &mut self, grid: &G, start: Coord, goal: Coord, path: &mut Vec<Direction>, ) -> Result<SearchMetadata, Error> where G: SolidGrid, { let initial_entry = match self.init(start, goal, grid, path) { Ok(initial_entry) => initial_entry, Err(result) => return result, }; let goal_index = self.node_grid .coord_to_index(goal) .expect("SearchContext too small for grid"); for direction in CardinalDirections { self.expand(grid, start, initial_entry.cost, direction, goal); } let mut num_nodes_visited = 0; while let Some(current_entry) = self.priority_queue.pop() { num_nodes_visited += 1; if current_entry.node_index == goal_index { path::make_path_jump_points(&self.node_grid, goal, self.seq, path); return Ok(SearchMetadata { num_nodes_visited }); } let (current_coord, current_cost, direction) = { let node = &mut self.node_grid[current_entry.node_index]; if node.visited == self.seq { continue; } node.visited = self.seq; let direction = node.from_parent .expect("Open set node without direction") .cardinal() .expect("Expected cardinal directions only"); (node.coord, node.cost, direction) }; self.expand(grid, current_coord, current_cost, direction, goal); self.expand(grid, current_coord, current_cost, direction.left90(), goal); self.expand(grid, current_coord, current_cost, direction.right90(), goal); } Err(Error::NoPath) } }