2023-12-15 16:52:44 +00:00
|
|
|
use std::fmt::{Debug, Formatter};
|
2023-12-15 20:48:10 +00:00
|
|
|
use std::ops::{Add, Index, Mul, Sub};
|
2023-11-30 22:53:17 +00:00
|
|
|
|
2023-12-15 20:48:10 +00:00
|
|
|
pub struct Matrix3(Vec<Vector3>);
|
2023-12-15 16:52:44 +00:00
|
|
|
|
2023-12-15 20:48:10 +00:00
|
|
|
impl Debug for Matrix3 {
|
2023-12-15 16:52:44 +00:00
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
2023-12-15 20:48:10 +00:00
|
|
|
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 ")?;
|
|
|
|
}
|
|
|
|
}
|
2023-12-15 16:52:44 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-15 20:48:10 +00:00
|
|
|
impl Matrix3 {
|
2023-12-15 16:52:44 +00:00
|
|
|
pub fn new(i: Vector3, j: Vector3, k: Vector3) -> Self {
|
2023-12-15 20:48:10 +00:00
|
|
|
Self { 0: vec![i, j, k] }
|
2023-12-15 16:52:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn transform(&self, vector: Vector3) -> Vector3 {
|
|
|
|
self.0[0] * vector.x + self.0[1] * vector.y + self.0[2] * vector.z
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
2023-11-30 16:28:37 +00:00
|
|
|
pub struct Vector2 {
|
2023-11-30 22:53:17 +00:00
|
|
|
pub x: f32,
|
|
|
|
pub y: f32,
|
2023-11-30 16:28:37 +00:00
|
|
|
}
|
|
|
|
|
2023-11-30 22:53:17 +00:00
|
|
|
impl Default for Vector2 {
|
2023-11-30 16:28:37 +00:00
|
|
|
fn default() -> Self {
|
2023-11-30 22:53:17 +00:00
|
|
|
Self { x: 0_f32, y: 0_f32 }
|
2023-11-30 16:28:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Vector2 {
|
|
|
|
pub fn new(x: f32, y: f32) -> Self {
|
|
|
|
Self { x, y }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-15 16:52:44 +00:00
|
|
|
#[derive(Debug, Copy, Clone)]
|
2023-11-30 16:28:37 +00:00
|
|
|
pub struct Vector3 {
|
2023-11-30 22:53:17 +00:00
|
|
|
pub x: f32,
|
|
|
|
pub y: f32,
|
|
|
|
pub z: f32,
|
2023-11-30 16:28:37 +00:00
|
|
|
}
|
|
|
|
|
2023-11-30 22:53:17 +00:00
|
|
|
impl Default for Vector3 {
|
2023-11-30 16:28:37 +00:00
|
|
|
fn default() -> Self {
|
2023-11-30 22:53:17 +00:00
|
|
|
Self {
|
|
|
|
x: 0_f32,
|
|
|
|
y: 0_f32,
|
|
|
|
z: 0_f32,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-15 20:48:10 +00:00
|
|
|
impl Index<usize> 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!"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-30 22:53:17 +00:00
|
|
|
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 {
|
2023-12-15 16:52:44 +00:00
|
|
|
type Output = Self;
|
2023-11-30 22:53:17 +00:00
|
|
|
fn sub(self, rhs: Self) -> Self::Output {
|
|
|
|
Self::Output {
|
|
|
|
x: self.x - rhs.x,
|
|
|
|
y: self.y - rhs.y,
|
|
|
|
z: self.z - rhs.z,
|
|
|
|
}
|
2023-11-30 16:28:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-15 16:52:44 +00:00
|
|
|
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<f32> 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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-30 16:28:37 +00:00
|
|
|
impl Vector3 {
|
|
|
|
pub fn new(x: f32, y: f32, z: f32) -> Self {
|
|
|
|
Self { x, y, z }
|
|
|
|
}
|
2023-12-15 20:48:10 +00:00
|
|
|
|
|
|
|
pub fn flatten(&self) -> Vector2 {
|
|
|
|
Vector2::new(self.x, self.y)
|
|
|
|
}
|
2023-11-30 16:28:37 +00:00
|
|
|
}
|
2023-11-30 23:11:20 +00:00
|
|
|
|
2023-12-15 16:52:44 +00:00
|
|
|
#[derive(Debug)]
|
2023-11-30 23:11:20 +00:00
|
|
|
pub struct Vertex {
|
|
|
|
pub position: Vector3,
|
|
|
|
pub uv: Vector2,
|
|
|
|
pub normal: Vector3,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Vertex {
|
|
|
|
pub fn new(position: Vector3, uv: Vector2, normal: Vector3) -> Self {
|
|
|
|
Self {
|
|
|
|
position,
|
|
|
|
uv,
|
|
|
|
normal,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|