| 195 | } |
| 196 | |
| 197 | pub fn snap_graph<D>(graph: GeometryGraph<D>, strategy: SnappingStrategy) -> GeometryGraph<D> |
| 198 | where |
| 199 | D: EdgeType, |
| 200 | { |
| 201 | // You can't look up a node in a graph by its weight (the coordinate) |
| 202 | // So we need an auxiliary GraphKdTree index for us to look up NodeIndices from their |
| 203 | // coordinates. |
| 204 | let mut index = GraphKdTree::new(2); |
| 205 | for node_idx in graph.node_indices() { |
| 206 | let node = graph[node_idx]; |
| 207 | let coords = [node.0.x, node.0.y]; |
| 208 | |
| 209 | // Don't add duplicate vertices to the index |
| 210 | let closest = index.nearest(&coords, 1, &squared_euclidean).unwrap(); |
| 211 | if let Some(closest) = closest.first() { |
| 212 | let (distance, _) = closest; |
| 213 | if *distance == 0.0 { |
| 214 | continue; |
| 215 | } |
| 216 | } |
| 217 | index.add(coords, node_idx).unwrap(); |
| 218 | } |
| 219 | |
| 220 | match strategy { |
| 221 | SnappingStrategy::ClosestPoint(tolerance) => { |
| 222 | snap_graph_closest_point(graph, &mut index, tolerance) |
| 223 | } |
| 224 | SnappingStrategy::RegularGrid(tolerance) => snap_graph_grid(graph, tolerance), |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | fn snap_graph_closest_point<D>( |
| 229 | mut graph: GeometryGraph<D>, |