Skip to main content

projective_grid/square/
direction.rs

1use crate::Float;
2use serde::{Deserialize, Serialize};
3
4/// Cardinal direction along a 4-connected grid.
5#[non_exhaustive]
6#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
7pub enum NeighborDirection {
8    Right,
9    Left,
10    Up,
11    Down,
12}
13
14impl NeighborDirection {
15    /// The opposite direction.
16    pub fn opposite(self) -> Self {
17        match self {
18            Self::Right => Self::Left,
19            Self::Left => Self::Right,
20            Self::Up => Self::Down,
21            Self::Down => Self::Up,
22        }
23    }
24
25    /// Grid coordinate delta `(di, dj)` for this direction.
26    ///
27    /// Convention: `i` increases rightward, `j` increases downward.
28    pub fn delta(self) -> (i32, i32) {
29        match self {
30            Self::Right => (1, 0),
31            Self::Left => (-1, 0),
32            Self::Up => (0, -1),
33            Self::Down => (0, 1),
34        }
35    }
36}
37
38/// A validated grid neighbor with direction, distance, and quality score.
39#[derive(Debug)]
40pub struct NodeNeighbor<F: Float = f32> {
41    /// Direction from the source node to this neighbor.
42    pub direction: NeighborDirection,
43    /// Index of the neighbor in the original point array.
44    pub index: usize,
45    /// Euclidean distance in pixels.
46    pub distance: F,
47    /// Quality score (lower is better).
48    pub score: F,
49}