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
use crate::Float;
use serde::{Deserialize, Serialize};
/// Cardinal direction along a 4-connected grid.
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum NeighborDirection {
Right,
Left,
Up,
Down,
}
impl NeighborDirection {
/// The opposite direction.
pub fn opposite(self) -> Self {
match self {
Self::Right => Self::Left,
Self::Left => Self::Right,
Self::Up => Self::Down,
Self::Down => Self::Up,
}
}
/// Grid coordinate delta `(di, dj)` for this direction.
///
/// Convention: `i` increases rightward, `j` increases downward.
pub fn delta(self) -> (i32, i32) {
match self {
Self::Right => (1, 0),
Self::Left => (-1, 0),
Self::Up => (0, -1),
Self::Down => (0, 1),
}
}
}
/// A validated grid neighbor with direction, distance, and quality score.
#[derive(Debug)]
pub struct NodeNeighbor<F: Float = f32> {
/// Direction from the source node to this neighbor.
pub direction: NeighborDirection,
/// Index of the neighbor in the original point array.
pub index: usize,
/// Euclidean distance in pixels.
pub distance: F,
/// Quality score (lower is better).
pub score: F,
}