From c45357244ab214d84c0daf977548316ef8641e85 Mon Sep 17 00:00:00 2001 From: antifallobst Date: Fri, 15 Dec 2023 17:52:44 +0100 Subject: [PATCH] feat: added linear vector matrix transformations --- lithium-utils/src/lib.rs | 63 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 61 insertions(+), 2 deletions(-) diff --git a/lithium-utils/src/lib.rs b/lithium-utils/src/lib.rs index 6d926f2..7556724 100644 --- a/lithium-utils/src/lib.rs +++ b/lithium-utils/src/lib.rs @@ -1,5 +1,40 @@ -use std::ops::{Add, Sub}; +use std::fmt::{Debug, Formatter}; +use std::ops::{Add, Mul, Sub}; +pub struct Matrix3([Vector3; C]); + +impl Debug for Matrix3<3> { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + writeln!( + f, + "[ {:03.2} {:03.2} {:03.2}", + self.0[0].x, self.0[1].x, self.0[2].x, + )?; + writeln!( + f, + " {:03.2} {:03.2} {:03.2}", + self.0[0].y, self.0[1].y, self.0[2].y, + )?; + writeln!( + f, + " {:03.2} {:03.2} {:03.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 + } +} + +#[derive(Debug)] pub struct Vector2 { pub x: f32, pub y: f32, @@ -17,6 +52,7 @@ impl Vector2 { } } +#[derive(Debug, Copy, Clone)] pub struct Vector3 { pub x: f32, pub y: f32, @@ -45,7 +81,7 @@ impl Add for Vector3 { } impl Sub for Vector3 { - type Output = Vector3; + type Output = Self; fn sub(self, rhs: Self) -> Self::Output { Self::Output { x: self.x - rhs.x, @@ -55,12 +91,35 @@ impl Sub for Vector3 { } } +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 } } } +#[derive(Debug)] pub struct Vertex { pub position: Vector3, pub uv: Vector2,