From ca5174b41e0684a69c15624a5c175f5e22f6635c Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Sat, 7 Jan 2023 16:18:42 +0100 Subject: [PATCH] vectors: factor out and implement 3D vector MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit • factor out the vectors to separate module • implement ‹Vector3D› Signed-off-by: Matej Focko --- src/lib.rs | 4 +- src/vectors/mod.rs | 5 + src/{vector2d.rs => vectors/vec2d.rs} | 0 src/vectors/vec3d.rs | 136 ++++++++++++++++++++++++++ 4 files changed, 143 insertions(+), 2 deletions(-) create mode 100644 src/vectors/mod.rs rename src/{vector2d.rs => vectors/vec2d.rs} (100%) create mode 100644 src/vectors/vec3d.rs diff --git a/src/lib.rs b/src/lib.rs index 3f38a25..5245217 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,5 +7,5 @@ pub use solution::*; mod testing; pub use testing::*; -mod vector2d; -pub use vector2d::*; +mod vectors; +pub use vectors::*; diff --git a/src/vectors/mod.rs b/src/vectors/mod.rs new file mode 100644 index 0000000..46b4f7e --- /dev/null +++ b/src/vectors/mod.rs @@ -0,0 +1,5 @@ +mod vec2d; +pub use vec2d::*; + +mod vec3d; +pub use vec3d::*; diff --git a/src/vector2d.rs b/src/vectors/vec2d.rs similarity index 100% rename from src/vector2d.rs rename to src/vectors/vec2d.rs diff --git a/src/vectors/vec3d.rs b/src/vectors/vec3d.rs new file mode 100644 index 0000000..72124ad --- /dev/null +++ b/src/vectors/vec3d.rs @@ -0,0 +1,136 @@ +use std::cmp::Eq; +// use std::collections::VecDeque; +use std::fmt::Debug; +use std::hash::Hash; +use std::ops::{Add, /* Index, IndexMut, */ Mul, Sub}; + +#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] +pub struct Vector3D { + x: T, + y: T, + z: T, +} + +impl Vector3D { + pub fn new(x: T, y: T, z: T) -> Vector3D { + Vector3D { x, y, z } + } + + pub fn x(&self) -> &T { + &self.x + } + + pub fn y(&self) -> &T { + &self.y + } + + pub fn z(&self) -> &T { + &self.z + } +} + +// [TODO] Implement indexing +// macro_rules! generate_indices { +// ($container:ty) => { +// impl Index> for $container +// where +// I: Copy + TryInto, +// >::Error: Debug, +// C: Index, +// { +// type Output = C::Output; + +// fn index(&self, index: Vector3D) -> &Self::Output { +// let (x, y): (usize, usize) = +// (index.x.try_into().unwrap(), index.y.try_into().unwrap()); +// &self[y][x] +// } +// } + +// impl IndexMut> for $container +// where +// I: Copy + TryInto, +// >::Error: Debug, +// C: IndexMut, +// { +// fn index_mut(&mut self, index: Vector3D) -> &mut Self::Output { +// let (x, y): (usize, usize) = +// (index.x.try_into().unwrap(), index.y.try_into().unwrap()); +// &mut self[y][x] +// } +// } +// }; +// } + +// generate_indices!(VecDeque); +// generate_indices!([C]); +// generate_indices!(Vec); +// // generate_indices!([C; N], const N: usize); + +// pub fn in_range(v: &[Vec], idx: &Vector3D) -> bool +// where +// I: Copy, +// usize: TryFrom, +// { +// usize::try_from(idx.y) +// .and_then(|y| usize::try_from(idx.x).map(|x| y < v.len() && x < v[y].len())) +// .unwrap_or(false) +// } + +// See: https://github.com/rust-lang/rust/issues/102731 +// impl, T> From> for Vector3D { +// fn from(value: Vector3D) -> Self { +// Self { +// x: U::from(value.x), +// y: U::from(value.y), +// } +// } +// } + +impl, U> Add for Vector3D { + type Output = Vector3D; + + fn add(self, rhs: Self) -> Self::Output { + Vector3D { + x: self.x + rhs.x, + y: self.y + rhs.y, + z: self.z + rhs.z, + } + } +} + +impl, U> Sub for Vector3D { + type Output = Vector3D; + + fn sub(self, rhs: Self) -> Self::Output { + Vector3D { + x: self.x - rhs.x, + y: self.y - rhs.y, + z: self.z - rhs.z, + } + } +} + +impl, U> Mul for Vector3D { + type Output = Vector3D; + + fn mul(self, rhs: Self) -> Self::Output { + Vector3D { + x: self.x * rhs.x, + y: self.y * rhs.y, + z: self.z * rhs.z, + } + } +} + +impl + Copy, U> Mul for Vector3D { + type Output = Vector3D; + + fn mul(self, rhs: T) -> Self::Output { + Vector3D { + x: self.x * rhs, + y: self.y * rhs, + z: self.z * rhs, + } + } +}