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
use crate::geometry::point::PointTrait;
use itertools::Itertools;
use std::ops::{Add, AddAssign, Mul, Sub, SubAssign};
/// A point in 4-dimensional space
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Vector4 {
pub x: i32,
pub y: i32,
pub z: i32,
pub w: i32,
}
impl Vector4 {
pub fn new(x: i32, y: i32, z: i32, w: i32) -> Vector4 {
Vector4 { x, y, z, w }
}
/// Return the manhattan distance of this vector from the origin
pub fn abs_sum(self) -> i32 {
self.x.abs() + self.y.abs() + self.z.abs() + self.w.abs()
}
/// Return this point with all dimensions decremented by 1
pub fn decr(self) -> Vector4 {
Vector4::new(self.x - 1, self.y - 1, self.z - 1, self.w - 1)
}
/// Return this point with all dimensions incremented by 1
pub fn incr(self) -> Vector4 {
Vector4::new(self.x + 1, self.y + 1, self.z + 1, self.w + 1)
}
/// Return all points that lie within the minimum and maximum bounds, inclusive
pub fn inclusive_range(min: Vector4, max: Vector4) -> impl Iterator<Item = Vector4> {
(min.x..=max.x)
.cartesian_product(min.y..=max.y)
.cartesian_product(min.z..=max.z)
.cartesian_product(min.w..=max.w)
.map(|(((x, y), z), w)| Vector4::new(x, y, z, w))
}
/// Iterate over points in 3d space adjacent to this point
///
/// This includes diagonals, and excludes the center. It always returns 26 items.
pub fn adjacent(self) -> impl Iterator<Item = Vector4> {
Vector4::inclusive_range(self.decr(), self.incr()).filter(move |&v| v != self)
}
/// Return the boundary minimum between `self` and `other`.
///
/// The standard `.min` function computes a total ordering between two vectors, but it doesn't
/// help for computing an inclusive range. For example, it is true that
///
/// ```rust
/// # use aoclib::geometry::vector4::Vector4;
/// let a = Vector4::new(-1, -1, -1, 0);
/// let b = Vector4::new(0, -3, -1, 0);
/// assert!(a < b);
/// ```
///
/// The boundary minimum, on the other hand, computes the minimal bounded point which
/// contains both `self` and `other`:
///
/// ```rust
/// # use aoclib::geometry::vector4::Vector4;
/// let a = Vector4::new(-1, -1, -1, 0);
/// let b = Vector4::new(0, -3, -1, 0);
/// assert_eq!(a.boundary_min(b), Vector4::new(-1, -3, -1, 0));
/// ```
pub fn boundary_min(self, other: Vector4) -> Vector4 {
Vector4::new(
self.x.min(other.x),
self.y.min(other.y),
self.z.min(other.z),
self.w.min(other.w),
)
}
/// Return the boundary maximum between `self` and `other`.
///
/// The standard `.max` function computes a total ordering between two vectors, but it doesn't
/// help for computing an inclusive range. For example, it is true that
///
/// ```rust
/// # use aoclib::geometry::vector4::Vector4;
/// let a = Vector4::new(1, 1, 1, 0);
/// let b = Vector4::new(0, 3, 1, 0);
/// assert!(a > b);
/// ```
///
/// The boundary minimum, on the other hand, computes the minimal bounded point which
/// contains both `self` and `other`:
///
/// ```rust
/// # use aoclib::geometry::vector4::Vector4;
/// let a = Vector4::new(1, 1, 1, 0);
/// let b = Vector4::new(0, 3, 1, 0);
/// assert_eq!(a.boundary_max(b), Vector4::new(1, 3, 1, 0));
/// ```
pub fn boundary_max(self, other: Vector4) -> Vector4 {
Vector4::new(
self.x.max(other.x),
self.y.max(other.y),
self.z.max(other.z),
self.w.max(other.w),
)
}
/// Return the volume of the space defined between this point and the origin.
pub fn volume<T>(self) -> T
where
T: From<i32> + Mul<Output = T>,
{
let x: T = self.x.abs().into();
let y: T = self.y.abs().into();
let z: T = self.z.abs().into();
let w: T = self.w.abs().into();
x * y * z * w
}
}
impl AddAssign for Vector4 {
fn add_assign(&mut self, other: Self) {
self.x += other.x;
self.y += other.y;
self.z += other.z;
self.w += other.w;
}
}
impl Add for Vector4 {
type Output = Vector4;
fn add(mut self, other: Self) -> Self {
self += other;
self
}
}
impl SubAssign for Vector4 {
fn sub_assign(&mut self, other: Vector4) {
self.x -= other.x;
self.y -= other.y;
self.z -= other.z;
self.w -= other.w;
}
}
impl Sub for Vector4 {
type Output = Vector4;
fn sub(mut self, rhs: Vector4) -> Self::Output {
self -= rhs;
self
}
}
impl PointTrait for Vector4 {
type N = i32;
fn manhattan(self) -> Self::N {
<Self>::abs_sum(self)
}
fn decr(self) -> Self {
<Self>::decr(self)
}
fn incr(self) -> Self {
<Self>::incr(self)
}
fn inclusive_range(min: Self, max: Self) -> Box<dyn Iterator<Item = Self>> {
Box::new(<Self>::inclusive_range(min, max))
}
fn boundary_min(self, other: Self) -> Self {
<Self>::boundary_min(self, other)
}
fn boundary_max(self, other: Self) -> Self {
<Self>::boundary_max(self, other)
}
fn volume<T>(self) -> T
where
T: From<Self::N> + Mul<Output = T>,
{
<Self>::volume(self)
}
}