From 0f32424c1b61129d944194a3f89e5ea74ca1e084 Mon Sep 17 00:00:00 2001 From: antifallobst Date: Sun, 17 Dec 2023 16:08:41 +0100 Subject: [PATCH] refactor(utils): splitted into submodules --- lithium-utils/src/lib.rs | 135 ++---------------------------------- lithium-utils/src/matrix.rs | 35 ++++++++++ lithium-utils/src/vector.rs | 98 ++++++++++++++++++++++++++ 3 files changed, 137 insertions(+), 131 deletions(-) create mode 100644 lithium-utils/src/matrix.rs create mode 100644 lithium-utils/src/vector.rs diff --git a/lithium-utils/src/lib.rs b/lithium-utils/src/lib.rs index dc027ca..81016cd 100644 --- a/lithium-utils/src/lib.rs +++ b/lithium-utils/src/lib.rs @@ -1,135 +1,8 @@ -use std::fmt::{Debug, Formatter}; -use std::ops::{Add, Index, Mul, Sub}; +mod vector; +mod matrix; -pub struct Matrix3(Vec); - -impl Debug for Matrix3 { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - write!(f, "[")?; - for coord in 0..3 { - for vec in self.0.iter() { - write!(f, " {:06.2}", vec[coord])?; - } - if coord == 2 { - write!(f, " ]\n")?; - } else { - write!(f, "\n ")?; - } - } - Ok(()) - } -} - -impl Matrix3 { - pub fn new(i: Vector3, j: Vector3, k: Vector3) -> Self { - Self { 0: vec![i, j, k] } - } - - pub fn transform(&self, vector: Vector3) -> Vector3 { - self.0[0] * vector.x + self.0[1] * vector.y + self.0[2] * vector.z - } -} - -#[derive(Debug)] -pub struct Vector2 { - pub x: f32, - pub y: f32, -} - -impl Default for Vector2 { - fn default() -> Self { - Self { x: 0_f32, y: 0_f32 } - } -} - -impl Vector2 { - pub fn new(x: f32, y: f32) -> Self { - Self { x, y } - } -} - -#[derive(Debug, Copy, Clone)] -pub struct Vector3 { - pub x: f32, - pub y: f32, - pub z: f32, -} - -impl Default for Vector3 { - fn default() -> Self { - Self { - x: 0_f32, - y: 0_f32, - z: 0_f32, - } - } -} - -impl Index for Vector3 { - type Output = f32; - fn index(&self, index: usize) -> &Self::Output { - match index { - 0 => &self.x, - 1 => &self.y, - 2 => &self.z, - _ => panic!("Index {index} is out of bounds in a Vector3!"), - } - } -} - -impl Add for Vector3 { - type Output = Self; - fn add(self, rhs: Self) -> Self::Output { - Self::Output { - x: self.x + rhs.x, - y: self.y + rhs.y, - z: self.z + rhs.z, - } - } -} - -impl Sub for Vector3 { - type Output = Self; - fn sub(self, rhs: Self) -> Self::Output { - Self::Output { - x: self.x - rhs.x, - y: self.y - rhs.y, - z: self.z - rhs.z, - } - } -} - -impl Mul for Vector3 { - type Output = Self; - fn mul(self, rhs: Self) -> Self::Output { - Self::Output { - x: self.x * rhs.x, - y: self.y * rhs.y, - z: self.z * rhs.z, - } - } -} - -impl Mul for Vector3 { - type Output = Self; - fn mul(self, rhs: f32) -> Self::Output { - Self::Output { - x: self.x * rhs, - y: self.y * rhs, - z: self.z * rhs, - } - } -} - -impl Vector3 { - pub fn new(x: f32, y: f32, z: f32) -> Self { - Self { x, y, z } - } - - pub fn flatten(&self) -> Vector2 { - Vector2::new(self.x, self.y) - } -} +pub use vector::*; +pub use matrix::*; #[derive(Debug)] pub struct Vertex { diff --git a/lithium-utils/src/matrix.rs b/lithium-utils/src/matrix.rs new file mode 100644 index 0000000..882fbd2 --- /dev/null +++ b/lithium-utils/src/matrix.rs @@ -0,0 +1,35 @@ +use crate::vector::Vector3; +use std::fmt::{Debug, Formatter}; + +pub struct Matrix3([Vector3; COLS]); + +impl Debug for Matrix3<3> { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + writeln!( + f, + "[ {:06.2} {:06.2} {:06.2}", + self.0[0].x, self.0[1].x, self.0[2].x + )?; + writeln!( + f, + " {:06.2} {:06.2} {:06.2}", + self.0[0].y, self.0[1].y, self.0[2].y + )?; + writeln!( + f, + " {:06.2} {:06.2} {:06.2} ]", + self.0[0].z, self.0[1].z, self.0[2].z + )?; + Ok(()) + } +} + +impl Matrix3<3> { + pub fn new(i: Vector3, j: Vector3, k: Vector3) -> Self { + Self { 0: [i, j, k] } + } + + pub fn transform(&self, vector: Vector3) -> Vector3 { + self.0[0] * vector.x + self.0[1] * vector.y + self.0[2] * vector.z + } +} diff --git a/lithium-utils/src/vector.rs b/lithium-utils/src/vector.rs new file mode 100644 index 0000000..40e844d --- /dev/null +++ b/lithium-utils/src/vector.rs @@ -0,0 +1,98 @@ +use std::ops::{Add, Index, Mul, Sub}; + +#[derive(Debug)] +pub struct Vector2 { + pub x: f32, + pub y: f32, +} + +impl Default for Vector2 { + fn default() -> Self { + Self { x: 0_f32, y: 0_f32 } + } +} + +impl Vector2 { + pub fn new(x: f32, y: f32) -> Self { + Self { x, y } + } +} + +#[derive(Debug, Copy, Clone)] +pub struct Vector3 { + pub x: f32, + pub y: f32, + pub z: f32, +} + +impl Default for Vector3 { + fn default() -> Self { + Self { + x: 0_f32, + y: 0_f32, + z: 0_f32, + } + } +} + +impl Index for Vector3 { + type Output = f32; + fn index(&self, index: usize) -> &Self::Output { + match index { + 0 => &self.x, + 1 => &self.y, + 2 => &self.z, + _ => panic!("Index {index} is out of bounds in a Vector3!"), + } + } +} + +impl Add for Vector3 { + type Output = Self; + fn add(self, rhs: Self) -> Self::Output { + Self::Output { + x: self.x + rhs.x, + y: self.y + rhs.y, + z: self.z + rhs.z, + } + } +} + +impl Sub for Vector3 { + type Output = Self; + fn sub(self, rhs: Self) -> Self::Output { + Self::Output { + x: self.x - rhs.x, + y: self.y - rhs.y, + z: self.z - rhs.z, + } + } +} + +impl Mul for Vector3 { + type Output = Self; + fn mul(self, rhs: Self) -> Self::Output { + Self::Output { + x: self.x * rhs.x, + y: self.y * rhs.y, + z: self.z * rhs.z, + } + } +} + +impl Mul for Vector3 { + type Output = Self; + fn mul(self, rhs: f32) -> Self::Output { + Self::Output { + x: self.x * rhs, + y: self.y * rhs, + z: self.z * rhs, + } + } +} + +impl Vector3 { + pub fn new(x: f32, y: f32, z: f32) -> Self { + Self { x, y, z } + } +}