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 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
//! Helper types for cartesian coordinate system topology
use num_traits::{CheckedAdd, CheckedSub, One};
use std::ops::{Add, Sub};
/// Direction on a number line/coordinate axis or identifiers for the end points of a line
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub enum Direction {
Negative = 0,
Positive = 1,
}
/// Abbreviated type alias for cartesian coordinate axes in 3D
pub type Axis = CartesianAxis3d;
/// The cartesian coordinate axes in 3D
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub enum CartesianAxis3d {
/// The x-axis
X = 0,
/// The y-axis
Y = 1,
/// The z-axis
Z = 2,
}
/// Identifies a direction along a specific cartesian axis
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub struct DirectedAxis {
pub axis: Axis,
pub direction: Direction,
}
/// Collection that stores one value per unique [`DirectedAxis`], can be used e.g. to store neighbors in a cartesian grid
#[derive(Copy, Clone, PartialEq, Eq, Default, Debug)]
pub struct DirectedAxisArray<T> {
data: [T; 6],
}
impl Direction {
/// Returns a reference to an array containing all possible directions
/// ```
/// use crate::splashsurf_lib::topology::Direction;
/// assert!(Direction::all_possible().iter().any(|d| d.is_positive()));
/// assert!(Direction::all_possible().iter().any(|d| d.is_negative()));
/// assert_eq!(Direction::all_possible().iter().count(), 2);
/// ```
pub const fn all_possible() -> &'static [Direction; 2] {
&ALL_DIRECTIONS
}
/// Constructs a new positive or negative direction depending on the flag
#[inline(always)]
/// ```
/// use crate::splashsurf_lib::topology::Direction;
/// assert_eq!(Direction::new_positive(true), Direction::Positive);
/// assert_eq!(Direction::new_positive(false), Direction::Negative);
/// ```
pub const fn new_positive(is_positive: bool) -> Self {
if is_positive {
Direction::Positive
} else {
Direction::Negative
}
}
/// Returns the opposite direction
pub const fn opposite(&self) -> Self {
match self {
Direction::Positive => Direction::Negative,
Direction::Negative => Direction::Positive,
}
}
/// Adds or subtracts the given step from the value depending on the direction
/// ```
/// use crate::splashsurf_lib::topology::Direction;
/// assert_eq!(Direction::Positive.apply_step(27, 3), 30);
/// assert_eq!(Direction::Negative.apply_step(27, 3), 24);
/// ```
#[inline(always)]
pub fn apply_step<N: Add<Output = N> + Sub<Output = N>>(&self, n: N, step: N) -> N {
if self.is_positive() {
n + step
} else {
n - step
}
}
/// Same as `apply_step` but uses `checked_add` and `checked_sub`, i.e. returns `None` on overflow
/// ```
/// use crate::splashsurf_lib::topology::Direction;
/// assert_eq!(Direction::Negative.checked_apply_step(0 as i32, 10), Some(-10));
/// assert_eq!(Direction::Negative.checked_apply_step(0 as u32, 10), None);
/// ```
#[inline(always)]
pub fn checked_apply_step<N: CheckedAdd<Output = N> + CheckedSub<Output = N>>(
&self,
n: N,
step: N,
) -> Option<N> {
if self.is_positive() {
n.checked_add(&step)
} else {
n.checked_sub(&step)
}
}
/// Adds or subtracts a 3D step from the array depending on the direction, returns `None` on overflow
/// ```
/// use crate::splashsurf_lib::topology::Direction;
/// assert_eq!(Direction::Negative.checked_apply_step_ijk(&[1, 2, 3], &[0, 1, 2]), Some([1, 1, 1]));
/// ```
#[inline(always)]
pub fn checked_apply_step_ijk<N: CheckedAdd<Output = N> + CheckedSub<Output = N>>(
&self,
start: &[N; 3],
step: &[N; 3],
) -> Option<[N; 3]> {
Some(if self.is_positive() {
[
start[0].checked_add(&step[0])?,
start[1].checked_add(&step[1])?,
start[2].checked_add(&step[2])?,
]
} else {
[
start[0].checked_sub(&step[0])?,
start[1].checked_sub(&step[1])?,
start[2].checked_sub(&step[2])?,
]
})
}
/// Returns whether the direction is positive
/// ```
/// use crate::splashsurf_lib::topology::Direction;
/// assert_eq!(Direction::Positive.is_positive(), true);
/// assert_eq!(Direction::Negative.is_positive(), false);
/// ```
#[inline(always)]
pub const fn is_positive(&self) -> bool {
match self {
Direction::Positive => true,
Direction::Negative => false,
}
}
/// Returns whether the direction is negative
/// ```
/// use crate::splashsurf_lib::topology::Direction;
/// assert_eq!(Direction::Positive.is_negative(), false);
/// assert_eq!(Direction::Negative.is_negative(), true);
/// ```
#[inline(always)]
pub const fn is_negative(&self) -> bool {
!self.is_positive()
}
}
const ALL_DIRECTIONS: [Direction; 2] = [Direction::Negative, Direction::Positive];
impl CartesianAxis3d {
/// Returns a reference to an array containing all 3D cartesian axes
/// ```
/// use crate::splashsurf_lib::topology::Axis;
/// assert_eq!(Axis::all_possible()[0], Axis::X);
/// assert_eq!(Axis::all_possible()[2], Axis::Z);
/// assert_eq!(Axis::all_possible().len(), 3);
/// ```
#[inline(always)]
pub const fn all_possible() -> &'static [Axis; 3] {
&ALL_AXES
}
/// Converts the cartesian axis into the corresponding 3D dimension index (X=0, Y=1, Z=2)
/// ```
/// use crate::splashsurf_lib::topology::Axis;
/// assert_eq!(Axis::X.dim(), 0);
/// assert_eq!(Axis::Y.dim(), 1);
/// assert_eq!(Axis::Z.dim(), 2);
/// ```
#[inline(always)]
pub const fn dim(self) -> usize {
self as usize
}
/// Returns the other two axes that are orthogonal to the current axis
/// ```
/// use crate::splashsurf_lib::topology::Axis;
/// assert_eq!(Axis::X.orthogonal_axes(), [Axis::Y, Axis::Z]);
/// ```
#[inline(always)]
pub const fn orthogonal_axes(&self) -> [Self; 2] {
ORTHOGONAL_AXES[self.dim()]
}
/// Combines this coordinate axis with a direction into a DirectedAxis
/// ```
/// use crate::splashsurf_lib::topology::{Axis, DirectedAxis, Direction};
/// assert_eq!(Axis::X.with_direction(Direction::Positive), DirectedAxis::new(Axis::X, Direction::Positive));
/// ```
#[inline(always)]
pub const fn with_direction(self, direction: Direction) -> DirectedAxis {
DirectedAxis::new(self, direction)
}
}
const ALL_AXES: [Axis; 3] = [Axis::X, Axis::Y, Axis::Z];
const AXES_ORTHOGONAL_TO_X: [Axis; 2] = [Axis::Y, Axis::Z];
const AXES_ORTHOGONAL_TO_Y: [Axis; 2] = [Axis::Z, Axis::X];
const AXES_ORTHOGONAL_TO_Z: [Axis; 2] = [Axis::X, Axis::Y];
const ORTHOGONAL_AXES: [[Axis; 2]; 3] = [
AXES_ORTHOGONAL_TO_X,
AXES_ORTHOGONAL_TO_Y,
AXES_ORTHOGONAL_TO_Z,
];
#[test]
fn test_orthogonal_axes() {
assert_eq!(
CartesianAxis3d::X.orthogonal_axes(),
[CartesianAxis3d::Y, CartesianAxis3d::Z]
);
assert_eq!(
CartesianAxis3d::Y.orthogonal_axes(),
[CartesianAxis3d::Z, CartesianAxis3d::X]
);
assert_eq!(
CartesianAxis3d::Z.orthogonal_axes(),
[CartesianAxis3d::X, CartesianAxis3d::Y]
);
}
impl DirectedAxis {
/// Returns a reference to an array of all possible directed axes in 3D
#[inline(always)]
pub const fn all_possible() -> &'static [DirectedAxis; 6] {
&ALL_DIRECTED_AXES
}
/// Constructs a new directed axis
/// ```
/// use crate::splashsurf_lib::topology::{Axis, DirectedAxis, Direction};
/// assert_eq!(DirectedAxis::new(Axis::X, Direction::Positive),
/// Axis::X.with_direction(Direction::Positive));
/// ```
#[inline(always)]
pub const fn new(axis: Axis, direction: Direction) -> Self {
Self { axis, direction }
}
/// Returns a directed axis with the opposite direction
/// ```
/// use crate::splashsurf_lib::topology::{Axis, DirectedAxis, Direction};
/// assert_eq!(DirectedAxis::new(Axis::X, Direction::Positive)
/// .opposite(), DirectedAxis::new(Axis::X, Direction::Negative));
/// assert_eq!(DirectedAxis::new(Axis::Z, Direction::Negative)
/// .opposite(), DirectedAxis::new(Axis::Z, Direction::Positive));
/// ```
#[inline(always)]
pub const fn opposite(&self) -> Self {
Self::new(self.axis, self.direction.opposite())
}
/// Converts the directed axis into a unique index in the range `(0..=5)`
#[inline(always)]
pub const fn to_usize(&self) -> usize {
self.axis as usize + (self.direction as usize * 3)
}
/// Converts an index in the range `(0..=5)` to the corresponding directed axis, panics if the index is out of range
#[inline(always)]
pub const fn from_usize(n: usize) -> Self {
Self::all_possible()[n]
}
/// Applies an increment of `1` in the direction of this directed axis to the given index array, returns `None` on overflow
/// ```
/// use crate::splashsurf_lib::topology::{Axis, DirectedAxis, Direction};
/// assert_eq!(DirectedAxis::new(Axis::X, Direction::Positive)
/// .apply_single_step(&[1,2,3]), Some([2,2,3]));
/// ```
#[inline(always)]
pub fn apply_single_step<N: Clone + CheckedAdd<Output = N> + CheckedSub<Output = N> + One>(
&self,
index: &[N; 3],
) -> Option<[N; 3]> {
self.checked_apply_step(index, N::one())
}
/// Applies the given step in the direction of this directed axis to the given index array, returns `None` on overflow
/// ```
/// use crate::splashsurf_lib::topology::{Axis, DirectedAxis, Direction};
/// assert_eq!(DirectedAxis::new(Axis::X, Direction::Positive)
/// .checked_apply_step(&[1,2,3], 6), Some([7,2,3]));
/// assert_eq!(DirectedAxis::new(Axis::Z, Direction::Negative)
/// .checked_apply_step(&[1,2,3], 10), Some([1,2,-7]));
/// ```
#[inline(always)]
pub fn checked_apply_step<N: Clone + CheckedAdd<Output = N> + CheckedSub<Output = N>>(
&self,
index: &[N; 3],
step: N,
) -> Option<[N; 3]> {
let mut index = index.clone();
index[self.axis.dim()] = self
.direction
.checked_apply_step(index[self.axis.dim()].clone(), step)?;
Some(index)
}
/// Applies the corresponding component of the step in the direction of this directed axis to the given index array, returns `None` on overflow
/// ```
/// use crate::splashsurf_lib::topology::{Axis, DirectedAxis, Direction};
/// assert_eq!(DirectedAxis::new(Axis::X, Direction::Positive)
/// .checked_apply_step_ijk(&[1,2,3], &[6,8,10]), Some([7,2,3]));
/// assert_eq!(DirectedAxis::new(Axis::Z, Direction::Negative)
/// .checked_apply_step_ijk(&[1,2,3], &[6,8,10]), Some([1,2,-7]));
/// ```
#[inline(always)]
pub fn checked_apply_step_ijk<N: Clone + CheckedAdd<Output = N> + CheckedSub<Output = N>>(
&self,
index: &[N; 3],
step: &[N; 3],
) -> Option<[N; 3]> {
let mut index = index.clone();
index[self.axis.dim()] = self.direction.checked_apply_step(
index[self.axis.dim()].clone(),
step[self.axis.dim()].clone(),
)?;
Some(index)
}
}
const ALL_DIRECTED_AXES: [DirectedAxis; 6] = [
DirectedAxis::new(Axis::X, Direction::Negative),
DirectedAxis::new(Axis::Y, Direction::Negative),
DirectedAxis::new(Axis::Z, Direction::Negative),
DirectedAxis::new(Axis::X, Direction::Positive),
DirectedAxis::new(Axis::Y, Direction::Positive),
DirectedAxis::new(Axis::Z, Direction::Positive),
];
#[test]
fn test_directed_axis_usize_conversion() {
for i in 0..6 {
assert_eq!(DirectedAxis::from_usize(i).to_usize(), i);
}
}
#[test]
fn test_directed_axis_all_possible_consistency() {
let all_directed_axes = DirectedAxis::all_possible();
for (i, ax) in all_directed_axes.iter().enumerate() {
assert_eq!(ax.to_usize(), i);
assert_eq!(*ax, DirectedAxis::from_usize(i));
}
}
impl<T> DirectedAxisArray<T> {
/// Constructs a new array and fills it with values produced by the given closure
pub fn new_with<F: FnMut(&DirectedAxis) -> T>(f: F) -> Self {
let mut f = f;
Self {
data: [
f(&DirectedAxis::all_possible()[0]),
f(&DirectedAxis::all_possible()[1]),
f(&DirectedAxis::all_possible()[2]),
f(&DirectedAxis::all_possible()[3]),
f(&DirectedAxis::all_possible()[4]),
f(&DirectedAxis::all_possible()[5]),
],
}
}
/// Returns a reference to the value stored for the given axis
pub fn get(&self, axis: &DirectedAxis) -> &T {
&self.data[axis.to_usize()]
}
/// Returns a mutable reference to the value stored for the given axis
pub fn get_mut(&mut self, axis: &DirectedAxis) -> &mut T {
&mut self.data[axis.to_usize()]
}
/// Returns an iterator of all unique directed axes and references to the corresponding stored value
pub fn iter(&self) -> impl Iterator<Item = (&DirectedAxis, &T)> {
DirectedAxis::all_possible().iter().zip(self.data.iter())
}
/// Returns an iterator of all unique directed axes and mutable references to the corresponding stored value
pub fn iter_mut(&mut self) -> impl Iterator<Item = (&DirectedAxis, &mut T)> {
DirectedAxis::all_possible()
.iter()
.zip(self.data.iter_mut())
}
/// Returns an iterator over references of all stored values
pub fn values(&self) -> impl Iterator<Item = &T> {
self.data.iter()
}
}