1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
//!
//! A module containing Indexing utilities for 2D and 3D grids with static dimensions
//! The main purpose of this module is to provide a way to index into a 1D array as if it were a 2D or 3D array
//! And not using x, y, z coordinates, but a single index that is transformed.
//! This is useful for implementing cellular automata and other grid-based algorithms
//! The main advantage of this approach is that it is faster than using a 2D or 3D array
//! And it is more flexible than using a 1D array and manually calculating the index
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct StaticIndex2<const X: usize, const Y: usize> {
pub i: usize,
}
impl<const X: usize, const Y: usize> StaticIndex2<X, Y> {
#[inline(always)]
pub fn new(i: usize) -> Self {
Self { i }
}
/// Returns a new StaticIndex2 from a ratio of the total number of indices
#[inline(always)]
pub fn from_ratio(f: f64) -> Self {
Self::new((f * ((X * Y) as f64)) as usize)
}
/// Returns the x coordinate of the index transformed into the L * H 2D grid
#[inline(always)]
pub fn x(&self) -> usize {
self.i % X
}
/// Returns the y coordinate of the index transformed into the L * H 2D grid
#[inline(always)]
pub fn y(&self) -> usize {
self.i / X
}
#[inline(always)]
pub fn is_x_max(&self) -> bool {
self.x() == X - 1
}
#[inline(always)]
pub fn is_x_min(&self) -> bool {
self.x() == 0
}
#[inline(always)]
pub fn is_y_max(&self) -> bool {
self.i >= (Y - 1) * X
}
#[inline(always)]
pub fn is_y_min(&self) -> bool {
self.i < X
}
/// Warning: this can overflow the index if is already at the maximum
fn add_x(&mut self) {
self.i += 1;
}
/// Warning: this can underflow the index if is already at the minimum
fn sub_x(&mut self) {
self.i -= 1;
}
/// Warning: this can overflow the index if is already at the maximum
fn add_y(&mut self) {
self.i += X;
}
/// Warning: this can underflow the index if is already at the minimum
fn sub_y(&mut self) {
self.i -= X;
}
pub fn add_x_periodic(&mut self) {
if self.is_x_max() {
self.i += 1 - X
} else {
self.add_x()
}
}
pub fn sub_x_periodic(&mut self) {
if self.is_x_min() {
self.i += X - 1
} else {
self.sub_x()
}
}
pub fn add_y_periodic(&mut self) {
if self.is_y_max() {
self.i = self.x() // wraps to y = 0
} else {
self.add_y()
}
}
pub fn sub_y_periodic(&mut self) {
if self.is_y_min() {
self.i += (Y - 1) * X // wraps to y = H - 1
} else {
self.sub_y()
}
}
pub fn add_x_constrained(&mut self) {
if !self.is_x_max() {
self.add_x()
}
}
pub fn sub_x_constrained(&mut self) {
if !self.is_x_min() {
self.sub_x()
}
}
pub fn add_y_constrained(&mut self) {
if !self.is_y_max() {
self.add_y()
}
}
pub fn sub_y_constrained(&mut self) {
if !self.is_y_min() {
self.sub_y()
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct StaticIndex3<const X: usize, const Y: usize, const Z: usize> {
pub i: usize,
}
impl<const X: usize, const Y: usize, const Z: usize> StaticIndex3<X, Y, Z> {
#[inline(always)]
pub fn new(i: usize) -> Self {
Self { i }
}
#[inline(always)]
pub fn from_ratio(f: f64) -> Self {
Self::new((f * ((X * Y * Z) as f64)) as usize)
}
#[inline(always)]
pub fn x(&self) -> usize {
self.i % X
}
#[inline(always)]
pub fn y(&self) -> usize {
(self.i / X) % Y
}
#[inline(always)]
pub fn z(&self) -> usize {
self.i / (X * Y * Z)
}
#[inline(always)]
pub fn is_x_max(&self) -> bool {
self.x() == X - 1
}
#[inline(always)]
pub fn is_x_min(&self) -> bool {
self.x() == 0
}
#[inline(always)]
pub fn is_y_max(&self) -> bool {
self.y() == Y - 1
}
#[inline(always)]
pub fn is_y_min(&self) -> bool {
self.y() == 0
}
#[inline(always)]
pub fn is_z_max(&self) -> bool {
self.z() == Z - 1
}
#[inline(always)]
pub fn is_z_min(&self) -> bool {
self.z() == 0
}
/// Warning: this can overflow the index if is already at the maximum
pub fn add_x(&mut self) {
self.i += 1;
}
/// Warning: this can underflow the index if is already at the minimum
pub fn sub_x(&mut self) {
self.i -= 1;
}
/// Warning: this can overflow the index if is already at the maximum
pub fn add_y(&mut self) {
self.i += X;
}
/// Warning: this can underflow the index if is already at the minimum
pub fn sub_y(&mut self) {
self.i -= X;
}
/// Warning: this can overflow the index if is already at the maximum
pub fn add_z(&mut self) {
self.i += X * Y;
}
/// Warning: this can underflow the index if is already at the minimum
pub fn sub_z(&mut self) {
self.i -= X * Y;
}
pub fn add_x_periodic(&mut self) {
if self.is_x_max() {
self.i += 1 - X
} else {
self.add_x()
}
}
pub fn sub_x_periodic(&mut self) {
if self.is_x_min() {
self.i += X - 1
} else {
self.sub_x()
}
}
pub fn add_y_periodic(&mut self) {
if self.is_y_max() {
self.i = self.x() + self.z() * X * Y
} else {
self.add_y()
}
}
pub fn sub_y_periodic(&mut self) {
if self.is_y_min() {
self.i += (Y - 1) * X // wraps to y = H - 1
} else {
self.sub_y()
}
}
pub fn add_z_periodic(&mut self) {
if self.is_z_max() {
self.i = self.x() + self.y() * X
} else {
self.add_z()
}
}
pub fn sub_z_periodic(&mut self) {
if self.is_z_min() {
self.i += (Z - 1) * X * Y
} else {
self.sub_z()
}
}
pub fn add_x_constrained(&mut self) {
if !self.is_x_max() {
self.add_x()
}
}
pub fn sub_x_constrained(&mut self) {
if !self.is_x_min() {
self.sub_x()
}
}
pub fn add_y_constrained(&mut self) {
if !self.is_y_max() {
self.add_y()
}
}
pub fn sub_y_constrained(&mut self) {
if !self.is_y_min() {
self.sub_y()
}
}
pub fn add_z_constrained(&mut self) {
if !self.is_z_max() {
self.add_z()
}
}
pub fn sub_z_constrained(&mut self) {
if !self.is_z_min() {
self.sub_z()
}
}
}