devela/num/geom/metric/
region.rs

1// devela::num::geom::metric::distance
2//
3//! Defines [`Region`], [`RegionStrided`].
4//
5
6use crate::{Extent, Position, Stride};
7
8/// A [`Position`]ed [`Extent`].
9#[must_use]
10#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
11pub struct Region<T, const D: usize> {
12    ///
13    pub pos: Position<T, D>,
14    ///
15    pub size: Extent<T, D>,
16}
17
18/// A 2-dimensional [`Region`].
19pub type Region2d<T> = Region<T, 2>;
20/// A 3-dimensional [`Region`].
21pub type Region3d<T> = Region<T, 3>;
22
23/// A [`Stride`]d [`Region`] defining structured traversal.
24///
25/// `RegionStrided` extends `Region` by adding a stride, allowing
26/// structured access to subregions or non-contiguous patterns.
27///
28/// - Used in **grids, datasets, and memory layouts**.
29/// - Supports **efficient structured stepping** (e.g. row-major iteration).
30#[must_use]
31#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
32pub struct RegionStrided<T, const D: usize> {
33    /// The positioned extent.
34    pub region: Region<T, D>,
35    /// The step size per dimension.
36    pub stride: Stride<T, D>,
37}
38
39/* impls */
40
41impl<T, const D: usize> Region<T, D> {
42    /// Returns a new `Region` from a `pos`ition and a `size`.
43    pub const fn new(pos: Position<T, D>, size: Extent<T, D>) -> Self {
44        Self { pos, size }
45    }
46
47    ///
48    #[must_use] #[rustfmt::skip]
49    pub fn position(&self) -> Position<T, D> where T: Clone { self.pos.clone() }
50    ///
51    #[rustfmt::skip]
52    pub fn extent(&self) -> Extent<T, D> where T: Clone { self.size.clone() }
53}
54impl<T, const D: usize> From<(Position<T, D>, Extent<T, D>)> for Region<T, D> {
55    fn from(from: (Position<T, D>, Extent<T, D>)) -> Self {
56        Self::new(from.0, from.1)
57    }
58}
59impl<T, const D: usize> From<(Extent<T, D>, Position<T, D>)> for Region<T, D> {
60    fn from(from: (Extent<T, D>, Position<T, D>)) -> Self {
61        Self::new(from.1, from.0)
62    }
63}