MCPcopy Create free account
hub / github.com/Notgnoshi/generative / snap_graph_node

Function snap_graph_node

generative/snap.rs:256–300  ·  view source on GitHub ↗

Snap the given node to the closest other node within the given tolerance Returns the NodeIndex that was snapped to, if the given node was snapped.

(
    graph: &mut GeometryGraph<D>,
    node_idx: NodeIndex<usize>,
    index: &mut GraphKdTree,
    tolerance: f64,
)

Source from the content-addressed store, hash-verified

254///
255/// Returns the NodeIndex that was snapped to, if the given node was snapped.
256fn snap_graph_node<D>(
257 graph: &mut GeometryGraph<D>,
258 node_idx: NodeIndex<usize>,
259 index: &mut GraphKdTree,
260 tolerance: f64,
261) -> Option<NodeIndex<usize>>
262where
263 D: EdgeType,
264{
265 let coords = [graph[node_idx].0.x, graph[node_idx].0.y];
266 let nearest_coords = index
267 .within(&coords, tolerance, &squared_euclidean)
268 .unwrap();
269
270 // There's no node close enough to snap to
271 if nearest_coords.is_empty() {
272 return None;
273 }
274
275 // Find the closest node that isn't the query node itself
276 let mut snap_to = None;
277 for (_distance, found_idx) in nearest_coords {
278 if *found_idx != node_idx {
279 snap_to = Some(*found_idx);
280 break;
281 }
282 }
283
284 // We found a node to snap to
285 if let Some(found_idx) = snap_to {
286 // Remove the snapped from node from the index, but we have to be careful to only remove it
287 // if we know the coordinates are actually in the index (duplicate coordinates are filtered
288 // out ahead of time because they would otherwise cause infinite loops here)
289 if graph[found_idx] != graph[node_idx] {
290 // Remove the node we're snapping from
291 index.remove(&coords, &node_idx).unwrap();
292 }
293
294 // Snap the two nodes together, updating the adjacencies
295 snap_graph_nodes(graph, node_idx, found_idx);
296 Some(found_idx)
297 } else {
298 None
299 }
300}
301
302/// Snap `snap_from` to `snap_to`, and update all of `snap_from`s adjacencies
303fn snap_graph_nodes<D>(

Callers 1

snap_graph_closest_pointFunction · 0.85

Calls 1

snap_graph_nodesFunction · 0.85

Tested by

no test coverage detected