gol_core/util/
grid_util.rs

1use serde::{Deserialize, Serialize};
2
3#[derive(Clone, Debug, Serialize, Deserialize)]
4pub struct Shape1D {
5    width: usize,
6}
7
8impl Shape1D {
9    pub fn new(width: usize) -> Self {
10        Self { width }
11    }
12
13    pub fn width(&self) -> usize {
14        self.width
15    }
16
17    pub fn volume(&self) -> usize {
18        self.width()
19    }
20
21    pub fn x_idx_min(&self) -> i64 {
22        dim_idx_min(self.width())
23    }
24
25    pub fn x_idx_max(&self) -> i64 {
26        dim_idx_max(self.width())
27    }
28}
29
30#[derive(Clone, Debug, Serialize, Deserialize)]
31pub struct Shape2D {
32    width: usize,
33    height: usize,
34}
35
36impl Shape2D {
37    pub fn new(width: usize, height: usize) -> Self {
38        Self { width, height }
39    }
40
41    pub fn width(&self) -> usize {
42        self.width
43    }
44
45    pub fn height(&self) -> usize {
46        self.height
47    }
48
49    pub fn volume(&self) -> usize {
50        self.width() * self.height()
51    }
52
53    pub fn x_idx_min(&self) -> i64 {
54        dim_idx_min(self.width())
55    }
56
57    pub fn x_idx_max(&self) -> i64 {
58        dim_idx_max(self.width())
59    }
60
61    pub fn y_idx_min(&self) -> i64 {
62        dim_idx_min(self.height())
63    }
64
65    pub fn y_idx_max(&self) -> i64 {
66        dim_idx_max(self.height())
67    }
68}
69
70#[derive(Clone, Debug, Serialize, Deserialize)]
71pub struct Shape3D {
72    width: usize,
73    height: usize,
74    depth: usize,
75}
76
77impl Shape3D {
78    pub fn new(width: usize, height: usize, depth: usize) -> Self {
79        Self {
80            width,
81            height,
82            depth,
83        }
84    }
85
86    pub fn width(&self) -> usize {
87        self.width
88    }
89
90    pub fn height(&self) -> usize {
91        self.height
92    }
93
94    pub fn depth(&self) -> usize {
95        self.depth
96    }
97
98    pub fn volume(&self) -> usize {
99        self.width() * self.height() * self.depth()
100    }
101
102    pub fn x_idx_min(&self) -> i64 {
103        dim_idx_min(self.width())
104    }
105
106    pub fn x_idx_max(&self) -> i64 {
107        dim_idx_max(self.width())
108    }
109
110    pub fn y_idx_min(&self) -> i64 {
111        dim_idx_min(self.height())
112    }
113
114    pub fn y_idx_max(&self) -> i64 {
115        dim_idx_max(self.height())
116    }
117
118    pub fn z_idx_min(&self) -> i64 {
119        dim_idx_min(self.depth())
120    }
121
122    pub fn z_idx_max(&self) -> i64 {
123        dim_idx_max(self.depth())
124    }
125}
126
127#[inline(always)]
128fn dim_idx_min(len: usize) -> i64 {
129    -(len as i64) / 2
130}
131
132#[inline(always)]
133fn dim_idx_max(len: usize) -> i64 {
134    len as i64 + dim_idx_min(len) - 1
135}