use serde::{Deserialize, Serialize};
use std::ops::{Add, Sub, Mul, Div, Neg};
pub type Result<T> = std::result::Result<T, Vec3dError>;
#[derive(Debug, thiserror::Error)]
pub enum Vec3dError {
#[error("Division by zero")]
DivisionByZero,
#[error("Invalid vector: {0}")]
InvalidVector(String),
#[error("Invalid matrix: {0}")]
InvalidMatrix(String),
#[error("Geometry error: {0}")]
GeometryError(String),
}
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct Vec2 {
pub x: f32,
pub y: f32,
}
impl Vec2 {
pub const ZERO: Self = Self { x: 0.0, y: 0.0 };
pub const ONE: Self = Self { x: 1.0, y: 1.0 };
pub const X: Self = Self { x: 1.0, y: 0.0 };
pub const Y: Self = Self { x: 0.0, y: 1.0 };
#[inline]
pub const fn new(x: f32, y: f32) -> Self {
Self { x, y }
}
#[inline]
pub fn dot(&self, other: &Self) -> f32 {
self.x * other.x + self.y * other.y
}
#[inline]
pub fn length_squared(&self) -> f32 {
self.dot(self)
}
#[inline]
pub fn length(&self) -> f32 {
self.length_squared().sqrt()
}
#[inline]
pub fn normalize(&self) -> Result<Self> {
let len = self.length();
if len < f32::EPSILON {
return Err(Vec3dError::InvalidVector("Cannot normalize zero vector".into()));
}
Ok(Self {
x: self.x / len,
y: self.y / len,
})
}
#[inline]
pub fn distance(&self, other: &Self) -> f32 {
(*self - *other).length()
}
#[inline]
pub fn lerp(&self, other: &Self, t: f32) -> Self {
*self + (*other - *self) * t
}
}
impl Add for Vec2 {
type Output = Self;
#[inline]
fn add(self, rhs: Self) -> Self {
Self { x: self.x + rhs.x, y: self.y + rhs.y }
}
}
impl Sub for Vec2 {
type Output = Self;
#[inline]
fn sub(self, rhs: Self) -> Self {
Self { x: self.x - rhs.x, y: self.y - rhs.y }
}
}
impl Mul<f32> for Vec2 {
type Output = Self;
#[inline]
fn mul(self, scalar: f32) -> Self {
Self { x: self.x * scalar, y: self.y * scalar }
}
}
impl Div<f32> for Vec2 {
type Output = Self;
#[inline]
fn div(self, scalar: f32) -> Self {
Self { x: self.x / scalar, y: self.y / scalar }
}
}
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct Vec3 {
pub x: f32,
pub y: f32,
pub z: f32,
}
impl Vec3 {
pub const ZERO: Self = Self { x: 0.0, y: 0.0, z: 0.0 };
pub const ONE: Self = Self { x: 1.0, y: 1.0, z: 1.0 };
pub const X: Self = Self { x: 1.0, y: 0.0, z: 0.0 };
pub const Y: Self = Self { x: 0.0, y: 1.0, z: 0.0 };
pub const Z: Self = Self { x: 0.0, y: 0.0, z: 1.0 };
#[inline]
pub const fn new(x: f32, y: f32, z: f32) -> Self {
Self { x, y, z }
}
#[inline]
pub fn from_slice(slice: &[f32]) -> Result<Self> {
if slice.len() < 3 {
return Err(Vec3dError::InvalidVector(format!("Expected 3 elements, got {}", slice.len())));
}
Ok(Self { x: slice[0], y: slice[1], z: slice[2] })
}
#[inline]
pub fn to_array(&self) -> [f32; 3] {
[self.x, self.y, self.z]
}
#[inline]
pub fn dot(&self, other: &Self) -> f32 {
self.x * other.x + self.y * other.y + self.z * other.z
}
#[inline]
pub fn cross(&self, other: &Self) -> Self {
Self {
x: self.y * other.z - self.z * other.y,
y: self.z * other.x - self.x * other.z,
z: self.x * other.y - self.y * other.x,
}
}
#[inline]
pub fn length_squared(&self) -> f32 {
self.dot(self)
}
#[inline]
pub fn length(&self) -> f32 {
self.length_squared().sqrt()
}
#[inline]
pub fn normalize(&self) -> Result<Self> {
let len = self.length();
if len < f32::EPSILON {
return Err(Vec3dError::InvalidVector("Cannot normalize zero vector".into()));
}
Ok(*self / len)
}
#[inline]
pub fn distance(&self, other: &Self) -> f32 {
(*self - *other).length()
}
#[inline]
pub fn distance_squared(&self, other: &Self) -> f32 {
(*self - *other).length_squared()
}
#[inline]
pub fn lerp(&self, other: &Self, t: f32) -> Self {
*self + (*other - *self) * t
}
#[inline]
pub fn project_onto(&self, other: &Self) -> Result<Self> {
let other_len_sq = other.length_squared();
if other_len_sq < f32::EPSILON {
return Err(Vec3dError::InvalidVector("Cannot project onto zero vector".into()));
}
Ok(*other * (self.dot(other) / other_len_sq))
}
#[inline]
pub fn reflect(&self, normal: &Self) -> Self {
*self - *normal * (2.0 * self.dot(normal))
}
#[inline]
pub fn angle_to(&self, other: &Self) -> Result<f32> {
let len_product = self.length() * other.length();
if len_product < f32::EPSILON {
return Err(Vec3dError::InvalidVector("Cannot compute angle with zero vector".into()));
}
Ok((self.dot(other) / len_product).clamp(-1.0, 1.0).acos())
}
}
impl Add for Vec3 {
type Output = Self;
#[inline]
fn add(self, rhs: Self) -> Self {
Self { x: self.x + rhs.x, y: self.y + rhs.y, z: self.z + rhs.z }
}
}
impl Sub for Vec3 {
type Output = Self;
#[inline]
fn sub(self, rhs: Self) -> Self {
Self { x: self.x - rhs.x, y: self.y - rhs.y, z: self.z - rhs.z }
}
}
impl Mul<f32> for Vec3 {
type Output = Self;
#[inline]
fn mul(self, scalar: f32) -> Self {
Self { x: self.x * scalar, y: self.y * scalar, z: self.z * scalar }
}
}
impl Div<f32> for Vec3 {
type Output = Self;
#[inline]
fn div(self, scalar: f32) -> Self {
Self { x: self.x / scalar, y: self.y / scalar, z: self.z / scalar }
}
}
impl Neg for Vec3 {
type Output = Self;
#[inline]
fn neg(self) -> Self {
Self { x: -self.x, y: -self.y, z: -self.z }
}
}
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct Vec4 {
pub x: f32,
pub y: f32,
pub z: f32,
pub w: f32,
}
impl Vec4 {
pub const ZERO: Self = Self { x: 0.0, y: 0.0, z: 0.0, w: 0.0 };
#[inline]
pub const fn new(x: f32, y: f32, z: f32, w: f32) -> Self {
Self { x, y, z, w }
}
#[inline]
pub fn from_vec3(v: Vec3, w: f32) -> Self {
Self { x: v.x, y: v.y, z: v.z, w }
}
#[inline]
pub fn to_vec3(&self) -> Vec3 {
Vec3 { x: self.x, y: self.y, z: self.z }
}
#[inline]
pub fn to_array(&self) -> [f32; 4] {
[self.x, self.y, self.z, self.w]
}
}
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct Mat4 {
pub m: [[f32; 4]; 4],
}
impl Mat4 {
pub const IDENTITY: Self = Self {
m: [
[1.0, 0.0, 0.0, 0.0],
[0.0, 1.0, 0.0, 0.0],
[0.0, 0.0, 1.0, 0.0],
[0.0, 0.0, 0.0, 1.0],
],
};
pub const ZERO: Self = Self {
m: [
[0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0],
],
};
#[inline]
pub fn from_cols(c0: Vec4, c1: Vec4, c2: Vec4, c3: Vec4) -> Self {
Self {
m: [
c0.to_array(),
c1.to_array(),
c2.to_array(),
c3.to_array(),
],
}
}
#[inline]
pub fn to_flat_array(&self) -> [f32; 16] {
let mut result = [0.0; 16];
for col in 0..4 {
for row in 0..4 {
result[col * 4 + row] = self.m[col][row];
}
}
result
}
#[inline]
pub fn translation(translation: Vec3) -> Self {
Self {
m: [
[1.0, 0.0, 0.0, 0.0],
[0.0, 1.0, 0.0, 0.0],
[0.0, 0.0, 1.0, 0.0],
[translation.x, translation.y, translation.z, 1.0],
],
}
}
#[inline]
pub fn scale(scale: Vec3) -> Self {
Self {
m: [
[scale.x, 0.0, 0.0, 0.0],
[0.0, scale.y, 0.0, 0.0],
[0.0, 0.0, scale.z, 0.0],
[0.0, 0.0, 0.0, 1.0],
],
}
}
pub fn rotation_x(angle_rad: f32) -> Self {
let (sin, cos) = angle_rad.sin_cos();
Self {
m: [
[1.0, 0.0, 0.0, 0.0],
[0.0, cos, sin, 0.0],
[0.0, -sin, cos, 0.0],
[0.0, 0.0, 0.0, 1.0],
],
}
}
pub fn rotation_y(angle_rad: f32) -> Self {
let (sin, cos) = angle_rad.sin_cos();
Self {
m: [
[cos, 0.0, -sin, 0.0],
[0.0, 1.0, 0.0, 0.0],
[sin, 0.0, cos, 0.0],
[0.0, 0.0, 0.0, 1.0],
],
}
}
pub fn rotation_z(angle_rad: f32) -> Self {
let (sin, cos) = angle_rad.sin_cos();
Self {
m: [
[cos, sin, 0.0, 0.0],
[-sin, cos, 0.0, 0.0],
[0.0, 0.0, 1.0, 0.0],
[0.0, 0.0, 0.0, 1.0],
],
}
}
#[inline]
pub fn transform_point(&self, point: Vec3) -> Vec3 {
let v = Vec4::from_vec3(point, 1.0);
let x = self.m[0][0] * v.x + self.m[1][0] * v.y + self.m[2][0] * v.z + self.m[3][0] * v.w;
let y = self.m[0][1] * v.x + self.m[1][1] * v.y + self.m[2][1] * v.z + self.m[3][1] * v.w;
let z = self.m[0][2] * v.x + self.m[1][2] * v.y + self.m[2][2] * v.z + self.m[3][2] * v.w;
let w = self.m[0][3] * v.x + self.m[1][3] * v.y + self.m[2][3] * v.z + self.m[3][3] * v.w;
if w.abs() > f32::EPSILON {
Vec3::new(x / w, y / w, z / w)
} else {
Vec3::new(x, y, z)
}
}
pub fn mul_mat4(&self, other: &Self) -> Self {
let mut result = Self::ZERO;
for col in 0..4 {
for row in 0..4 {
result.m[col][row] =
self.m[0][row] * other.m[col][0] +
self.m[1][row] * other.m[col][1] +
self.m[2][row] * other.m[col][2] +
self.m[3][row] * other.m[col][3];
}
}
result
}
pub fn inverse(&self) -> Result<Self> {
let m = &self.m;
let det =
m[0][0] * (m[1][1] * m[2][2] * m[3][3] + m[1][2] * m[2][3] * m[3][1] + m[1][3] * m[2][1] * m[3][2]
- m[1][3] * m[2][2] * m[3][1] - m[1][2] * m[2][1] * m[3][3] - m[1][1] * m[2][3] * m[3][2])
- m[0][1] * (m[1][0] * m[2][2] * m[3][3] + m[1][2] * m[2][3] * m[3][0] + m[1][3] * m[2][0] * m[3][2]
- m[1][3] * m[2][2] * m[3][0] - m[1][2] * m[2][0] * m[3][3] - m[1][0] * m[2][3] * m[3][2])
+ m[0][2] * (m[1][0] * m[2][1] * m[3][3] + m[1][1] * m[2][3] * m[3][0] + m[1][3] * m[2][0] * m[3][1]
- m[1][3] * m[2][1] * m[3][0] - m[1][1] * m[2][0] * m[3][3] - m[1][0] * m[2][3] * m[3][1])
- m[0][3] * (m[1][0] * m[2][1] * m[3][2] + m[1][1] * m[2][2] * m[3][0] + m[1][2] * m[2][0] * m[3][1]
- m[1][2] * m[2][1] * m[3][0] - m[1][1] * m[2][0] * m[3][2] - m[1][0] * m[2][2] * m[3][1]);
if det.abs() < f32::EPSILON {
return Err(Vec3dError::InvalidMatrix("Matrix is not invertible (determinant = 0)".into()));
}
self.inverse_affine()
}
pub fn inverse_affine(&self) -> Result<Self> {
let m = &self.m;
let det =
m[0][0] * (m[1][1] * m[2][2] - m[1][2] * m[2][1]) -
m[1][0] * (m[0][1] * m[2][2] - m[0][2] * m[2][1]) +
m[2][0] * (m[0][1] * m[1][2] - m[0][2] * m[1][1]);
if det.abs() < f32::EPSILON {
return Err(Vec3dError::InvalidMatrix("Matrix is not invertible".into()));
}
let inv_det = 1.0 / det;
let mut inv = Self::IDENTITY;
inv.m[0][0] = (m[1][1] * m[2][2] - m[1][2] * m[2][1]) * inv_det;
inv.m[1][0] = (m[1][2] * m[2][0] - m[1][0] * m[2][2]) * inv_det;
inv.m[2][0] = (m[1][0] * m[2][1] - m[1][1] * m[2][0]) * inv_det;
inv.m[0][1] = (m[0][2] * m[2][1] - m[0][1] * m[2][2]) * inv_det;
inv.m[1][1] = (m[0][0] * m[2][2] - m[0][2] * m[2][0]) * inv_det;
inv.m[2][1] = (m[0][1] * m[2][0] - m[0][0] * m[2][1]) * inv_det;
inv.m[0][2] = (m[0][1] * m[1][2] - m[0][2] * m[1][1]) * inv_det;
inv.m[1][2] = (m[0][2] * m[1][0] - m[0][0] * m[1][2]) * inv_det;
inv.m[2][2] = (m[0][0] * m[1][1] - m[0][1] * m[1][0]) * inv_det;
let tx = m[3][0];
let ty = m[3][1];
let tz = m[3][2];
inv.m[3][0] = -(inv.m[0][0] * tx + inv.m[1][0] * ty + inv.m[2][0] * tz);
inv.m[3][1] = -(inv.m[0][1] * tx + inv.m[1][1] * ty + inv.m[2][1] * tz);
inv.m[3][2] = -(inv.m[0][2] * tx + inv.m[1][2] * ty + inv.m[2][2] * tz);
Ok(inv)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct Quat {
pub x: f32,
pub y: f32,
pub z: f32,
pub w: f32,
}
impl Quat {
pub const IDENTITY: Self = Self { x: 0.0, y: 0.0, z: 0.0, w: 1.0 };
#[inline]
pub const fn new(x: f32, y: f32, z: f32, w: f32) -> Self {
Self { x, y, z, w }
}
pub fn from_axis_angle(axis: Vec3, angle_rad: f32) -> Result<Self> {
let axis = axis.normalize()?;
let half_angle = angle_rad * 0.5;
let (sin, cos) = half_angle.sin_cos();
Ok(Self {
x: axis.x * sin,
y: axis.y * sin,
z: axis.z * sin,
w: cos,
})
}
pub fn to_mat4(&self) -> Mat4 {
let x2 = self.x * self.x;
let y2 = self.y * self.y;
let z2 = self.z * self.z;
let xy = self.x * self.y;
let xz = self.x * self.z;
let yz = self.y * self.z;
let wx = self.w * self.x;
let wy = self.w * self.y;
let wz = self.w * self.z;
Mat4 {
m: [
[1.0 - 2.0 * (y2 + z2), 2.0 * (xy + wz), 2.0 * (xz - wy), 0.0],
[2.0 * (xy - wz), 1.0 - 2.0 * (x2 + z2), 2.0 * (yz + wx), 0.0],
[2.0 * (xz + wy), 2.0 * (yz - wx), 1.0 - 2.0 * (x2 + y2), 0.0],
[0.0, 0.0, 0.0, 1.0],
],
}
}
#[inline]
pub fn normalize(&self) -> Result<Self> {
let len = (self.x * self.x + self.y * self.y + self.z * self.z + self.w * self.w).sqrt();
if len < f32::EPSILON {
return Err(Vec3dError::InvalidVector("Cannot normalize zero quaternion".into()));
}
Ok(Self {
x: self.x / len,
y: self.y / len,
z: self.z / len,
w: self.w / len,
})
}
}
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct Aabb {
pub min: Vec3,
pub max: Vec3,
}
impl Aabb {
pub const EMPTY: Self = Self {
min: Vec3::new(f32::INFINITY, f32::INFINITY, f32::INFINITY),
max: Vec3::new(f32::NEG_INFINITY, f32::NEG_INFINITY, f32::NEG_INFINITY),
};
#[inline]
pub fn new(min: Vec3, max: Vec3) -> Self {
Self { min, max }
}
#[inline]
pub fn from_points(points: &[Vec3]) -> Self {
let mut aabb = Self::EMPTY;
for &p in points {
aabb.expand_point(p);
}
aabb
}
#[inline]
pub fn expand_point(&mut self, point: Vec3) {
self.min.x = self.min.x.min(point.x);
self.min.y = self.min.y.min(point.y);
self.min.z = self.min.z.min(point.z);
self.max.x = self.max.x.max(point.x);
self.max.y = self.max.y.max(point.y);
self.max.z = self.max.z.max(point.z);
}
#[inline]
pub fn center(&self) -> Vec3 {
(self.min + self.max) * 0.5
}
#[inline]
pub fn size(&self) -> Vec3 {
self.max - self.min
}
#[inline]
pub fn volume(&self) -> f32 {
let size = self.size();
size.x * size.y * size.z
}
#[inline]
pub fn contains_point(&self, point: Vec3) -> bool {
point.x >= self.min.x && point.x <= self.max.x &&
point.y >= self.min.y && point.y <= self.max.y &&
point.z >= self.min.z && point.z <= self.max.z
}
#[inline]
pub fn intersects(&self, other: &Self) -> bool {
self.min.x <= other.max.x && self.max.x >= other.min.x &&
self.min.y <= other.max.y && self.max.y >= other.min.y &&
self.min.z <= other.max.z && self.max.z >= other.min.z
}
pub fn merge(&self, other: &Self) -> Self {
Self {
min: Vec3::new(
self.min.x.min(other.min.x),
self.min.y.min(other.min.y),
self.min.z.min(other.min.z),
),
max: Vec3::new(
self.max.x.max(other.max.x),
self.max.y.max(other.max.y),
self.max.z.max(other.max.z),
),
}
}
pub fn transform(&self, matrix: &Mat4) -> Self {
let corners = [
Vec3::new(self.min.x, self.min.y, self.min.z),
Vec3::new(self.max.x, self.min.y, self.min.z),
Vec3::new(self.min.x, self.max.y, self.min.z),
Vec3::new(self.max.x, self.max.y, self.min.z),
Vec3::new(self.min.x, self.min.y, self.max.z),
Vec3::new(self.max.x, self.min.y, self.max.z),
Vec3::new(self.min.x, self.max.y, self.max.z),
Vec3::new(self.max.x, self.max.y, self.max.z),
];
let transformed: Vec<Vec3> = corners.iter()
.map(|&c| matrix.transform_point(c))
.collect();
Self::from_points(&transformed)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct Ray {
pub origin: Vec3,
pub direction: Vec3,
}
impl Ray {
pub fn new(origin: Vec3, direction: Vec3) -> Result<Self> {
let direction = direction.normalize()?;
Ok(Self { origin, direction })
}
#[inline]
pub fn at(&self, t: f32) -> Vec3 {
self.origin + self.direction * t
}
pub fn intersect_aabb(&self, aabb: &Aabb) -> Option<(f32, f32)> {
let mut tmin = f32::NEG_INFINITY;
let mut tmax = f32::INFINITY;
for i in 0..3 {
let origin = match i {
0 => self.origin.x,
1 => self.origin.y,
_ => self.origin.z,
};
let direction = match i {
0 => self.direction.x,
1 => self.direction.y,
_ => self.direction.z,
};
let min = match i {
0 => aabb.min.x,
1 => aabb.min.y,
_ => aabb.min.z,
};
let max = match i {
0 => aabb.max.x,
1 => aabb.max.y,
_ => aabb.max.z,
};
if direction.abs() < f32::EPSILON {
if origin < min || origin > max {
return None;
}
} else {
let inv_d = 1.0 / direction;
let mut t0 = (min - origin) * inv_d;
let mut t1 = (max - origin) * inv_d;
if t0 > t1 {
std::mem::swap(&mut t0, &mut t1);
}
tmin = tmin.max(t0);
tmax = tmax.min(t1);
if tmin > tmax {
return None;
}
}
}
Some((tmin, tmax))
}
}
#[cfg(test)]
mod tests {
use super::*;
use approx::assert_relative_eq;
#[test]
fn test_vec3_operations() {
let v1 = Vec3::new(1.0, 2.0, 3.0);
let v2 = Vec3::new(4.0, 5.0, 6.0);
assert_eq!(v1 + v2, Vec3::new(5.0, 7.0, 9.0));
assert_eq!(v1 - v2, Vec3::new(-3.0, -3.0, -3.0));
assert_relative_eq!(v1.dot(&v2), 32.0);
let cross = v1.cross(&v2);
assert_eq!(cross, Vec3::new(-3.0, 6.0, -3.0));
}
#[test]
fn test_mat4_transformations() {
let translation = Mat4::translation(Vec3::new(10.0, 20.0, 30.0));
let point = Vec3::new(1.0, 2.0, 3.0);
let transformed = translation.transform_point(point);
assert_relative_eq!(transformed.x, 11.0);
assert_relative_eq!(transformed.y, 22.0);
assert_relative_eq!(transformed.z, 33.0);
}
#[test]
fn test_aabb() {
let points = vec![
Vec3::new(0.0, 0.0, 0.0),
Vec3::new(10.0, 20.0, 30.0),
Vec3::new(-5.0, 15.0, 25.0),
];
let aabb = Aabb::from_points(&points);
assert_eq!(aabb.min, Vec3::new(-5.0, 0.0, 0.0));
assert_eq!(aabb.max, Vec3::new(10.0, 20.0, 30.0));
let center = aabb.center();
assert_relative_eq!(center.x, 2.5);
assert_relative_eq!(center.y, 10.0);
assert_relative_eq!(center.z, 15.0);
}
#[test]
fn test_ray_aabb_intersection() {
let aabb = Aabb::new(Vec3::ZERO, Vec3::ONE);
let ray = Ray::new(Vec3::new(-1.0, 0.5, 0.5), Vec3::X).unwrap();
let intersection = ray.intersect_aabb(&aabb);
assert!(intersection.is_some());
let (tmin, tmax) = intersection.unwrap();
assert_relative_eq!(tmin, 1.0);
assert_relative_eq!(tmax, 2.0);
}
}