Skip to main content

Hex2D

Struct Hex2D 

Source
pub struct Hex2D { /* private fields */ }
Expand description

A two-dimensional hexagonal lattice with axial coordinates.

Each cell has coordinate [q, r] where 0 <= q < cols and 0 <= r < rows. The grid uses pointy-top orientation with six neighbours per interior cell. Boundary behavior is Absorb (edge cells have fewer neighbours).

Distance is cube distance: max(|dq|, |dr|, |dq + dr|), which equals the graph geodesic on the hex grid.

Canonical ordering is r-then-q: outer loop over r, inner loop over q.

§Examples

use murk_space::{Hex2D, Space};

let hex = Hex2D::new(5, 5).unwrap();
assert_eq!(hex.rows(), 5);
assert_eq!(hex.cols(), 5);
assert_eq!(hex.cell_count(), 25);
assert_eq!(hex.ndim(), 2);

// Interior cell has 6 neighbours.
let interior: murk_core::Coord = vec![2i32, 2].into();
assert_eq!(hex.neighbours(&interior).len(), 6);

// Corner cell has fewer neighbours (absorb boundary).
let corner: murk_core::Coord = vec![0i32, 0].into();
assert_eq!(hex.neighbours(&corner).len(), 2);

// Cube distance between adjacent cells is 1.
let a: murk_core::Coord = vec![2i32, 1].into();
let b: murk_core::Coord = vec![3i32, 1].into();
assert_eq!(hex.distance(&a, &b), 1.0);

Implementations§

Source§

impl Hex2D

Source

pub const MAX_DIM: u32

Maximum dimension size: coordinates use i32, so each axis must fit.

Source

pub fn new(rows: u32, cols: u32) -> Result<Self, SpaceError>

Create a new hex grid with rows * cols cells.

Returns Err(SpaceError::EmptySpace) if either dimension is 0, or Err(SpaceError::DimensionTooLarge) if either exceeds i32::MAX.

Source

pub fn rows(&self) -> u32

Number of rows.

Source

pub fn cols(&self) -> u32

Number of columns.

Source

pub fn is_empty(&self) -> bool

Always returns false — construction rejects empty grids.

Trait Implementations§

Source§

impl Clone for Hex2D

Source§

fn clone(&self) -> Hex2D

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Hex2D

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Space for Hex2D

Source§

fn ndim(&self) -> usize

Number of spatial dimensions.
Source§

fn cell_count(&self) -> usize

Total number of cells in the space.
Source§

fn neighbours(&self, coord: &Coord) -> SmallVec<[Coord; 8]>

Enumerate the neighbors of a cell. Read more
Source§

fn max_neighbour_degree(&self) -> usize

Maximum neighbor-list length over all cells in this space. Read more
Source§

fn distance(&self, a: &Coord, b: &Coord) -> f64

Graph-geodesic distance between two cells.
Source§

fn compile_region(&self, spec: &RegionSpec) -> Result<RegionPlan, SpaceError>

Compile a region specification into a plan for O(1) lookups.
Source§

fn canonical_ordering(&self) -> Vec<Coord>

All cells in deterministic canonical order. Read more
Source§

fn canonical_rank(&self, coord: &Coord) -> Option<usize>

Position of a coordinate in the canonical ordering. Read more
Source§

fn canonical_rank_slice(&self, coord: &[i32]) -> Option<usize>

Position of a coordinate slice in the canonical ordering. Read more
Source§

fn instance_id(&self) -> SpaceInstanceId

Unique instance identifier for this space object. Read more
Source§

fn topology_eq(&self, other: &dyn Space) -> bool

Returns true if self and other are topologically equivalent: same concrete type and identical behavioral parameters. Read more
Source§

fn iter_region<'a>( &'a self, plan: &'a RegionPlan, ) -> Box<dyn Iterator<Item = Coord> + 'a>

Iterate over the cells in a compiled region. Read more
Source§

fn map_coord_to_tensor_index( &self, coord: &Coord, plan: &RegionPlan, ) -> Option<usize>

Map a coordinate to its flat tensor index within a compiled region. Read more

Auto Trait Implementations§

§

impl Freeze for Hex2D

§

impl RefUnwindSafe for Hex2D

§

impl Send for Hex2D

§

impl Sync for Hex2D

§

impl Unpin for Hex2D

§

impl UnsafeUnpin for Hex2D

§

impl UnwindSafe for Hex2D

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.