Skip to main content

projective_grid/lattice/
square.rs

1//! Square-lattice family: the only family with a complete detection pipeline
2//! today.
3//!
4//! [`Square`] implements [`Lattice`] with a Cartesian `model_point`, the four
5//! cardinal neighbour offsets, and the dihedral D4 symmetry group acting on
6//! `(i, j)` coordinates.
7
8use nalgebra::{Point2, Vector2};
9
10use super::{CellTopology, Coord, GridTransform, Lattice, LatticeKind};
11
12/// Zero-sized marker for the orthogonal square lattice.
13#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
14#[non_exhaustive]
15pub struct Square;
16
17impl Square {
18    /// Construct the square-lattice marker.
19    pub const fn new() -> Self {
20        Self
21    }
22}
23
24impl Lattice for Square {
25    const KIND: LatticeKind = LatticeKind::Square;
26
27    fn model_point(self, coord: Coord) -> Point2<f32> {
28        Point2::new(coord.u as f32, coord.v as f32)
29    }
30
31    fn neighbour_offsets(self) -> &'static [Coord] {
32        &SQUARE_CARDINAL_OFFSETS
33    }
34
35    fn symmetry_transforms(self) -> &'static [GridTransform] {
36        &D4_TRANSFORMS
37    }
38
39    fn axis_family_count(self) -> usize {
40        2
41    }
42
43    fn model_axis_directions(self) -> &'static [Vector2<f32>] {
44        &SQUARE_AXIS_DIRECTIONS
45    }
46
47    fn cell_topology(self) -> CellTopology {
48        CellTopology::TrianglePairToQuad
49    }
50}
51
52/// Unit model-plane directions of the two square axis families: `+u` and `+v`.
53static SQUARE_AXIS_DIRECTIONS: [Vector2<f32>; 2] = [Vector2::new(1.0, 0.0), Vector2::new(0.0, 1.0)];
54
55/// Four cardinal neighbour offsets on a square grid.
56pub const SQUARE_CARDINAL_OFFSETS: [Coord; 4] = [
57    Coord::new(1, 0),
58    Coord::new(0, 1),
59    Coord::new(-1, 0),
60    Coord::new(0, -1),
61];
62
63/// Dihedral group D4 acting on square lattice coordinates.
64pub const D4_TRANSFORMS: [GridTransform; 8] = [
65    GridTransform::new(LatticeKind::Square, [[1, 0], [0, 1]], [0, 0]),
66    GridTransform::new(LatticeKind::Square, [[0, -1], [1, 0]], [0, 0]),
67    GridTransform::new(LatticeKind::Square, [[-1, 0], [0, -1]], [0, 0]),
68    GridTransform::new(LatticeKind::Square, [[0, 1], [-1, 0]], [0, 0]),
69    GridTransform::new(LatticeKind::Square, [[-1, 0], [0, 1]], [0, 0]),
70    GridTransform::new(LatticeKind::Square, [[1, 0], [0, -1]], [0, 0]),
71    GridTransform::new(LatticeKind::Square, [[0, 1], [1, 0]], [0, 0]),
72    GridTransform::new(LatticeKind::Square, [[0, -1], [-1, 0]], [0, 0]),
73];