add basic geometry stuff

This commit is contained in:
wires 2025-04-22 12:13:25 -04:00
parent 845e1c9a5a
commit 48abb19464
Signed by: wires
SSH key fingerprint: SHA256:9GtP+M3O2IivPDlw1UY872UPUuJH2gI0yG6ExBxaaiM
3 changed files with 176 additions and 10 deletions

20
Cargo.lock generated
View file

@ -96,9 +96,9 @@ dependencies = [
[[package]]
name = "pyo3"
version = "0.24.1"
version = "0.24.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "17da310086b068fbdcefbba30aeb3721d5bb9af8db4987d6735b2183ca567229"
checksum = "e5203598f366b11a02b13aa20cab591229ff0a89fd121a308a5df751d5fc9219"
dependencies = [
"cfg-if",
"indoc",
@ -114,9 +114,9 @@ dependencies = [
[[package]]
name = "pyo3-build-config"
version = "0.24.1"
version = "0.24.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e27165889bd793000a098bb966adc4300c312497ea25cf7a690a9f0ac5aa5fc1"
checksum = "99636d423fa2ca130fa5acde3059308006d46f98caac629418e53f7ebb1e9999"
dependencies = [
"once_cell",
"target-lexicon",
@ -124,9 +124,9 @@ dependencies = [
[[package]]
name = "pyo3-ffi"
version = "0.24.1"
version = "0.24.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05280526e1dbf6b420062f3ef228b78c0c54ba94e157f5cb724a609d0f2faabc"
checksum = "78f9cf92ba9c409279bc3305b5409d90db2d2c22392d443a87df3a1adad59e33"
dependencies = [
"libc",
"pyo3-build-config",
@ -134,9 +134,9 @@ dependencies = [
[[package]]
name = "pyo3-macros"
version = "0.24.1"
version = "0.24.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c3ce5686aa4d3f63359a5100c62a127c9f15e8398e5fdeb5deef1fed5cd5f44"
checksum = "0b999cb1a6ce21f9a6b147dcf1be9ffedf02e0043aec74dc390f3007047cecd9"
dependencies = [
"proc-macro2",
"pyo3-macros-backend",
@ -146,9 +146,9 @@ dependencies = [
[[package]]
name = "pyo3-macros-backend"
version = "0.24.1"
version = "0.24.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f4cf6faa0cbfb0ed08e89beb8103ae9724eb4750e3a78084ba4017cbe94f3855"
checksum = "822ece1c7e1012745607d5cf0bcb2874769f0f7cb34c4cde03b9358eb9ef911a"
dependencies = [
"heck",
"proc-macro2",

165
core/src/geometry.rs Normal file
View file

@ -0,0 +1,165 @@
use std::ops::{Add, Div, Mul, Sub};
#[repr(transparent)]
#[derive(Debug, Clone, Copy)]
pub struct Vector(glam::Vec3A);
impl From<Vector> for glam::Vec3 {
fn from(value: Vector) -> Self {
value.0.into()
}
}
impl From<(f32, f32, f32)> for Vector {
fn from((x, y, z): (f32, f32, f32)) -> Self {
Self::new(x, y, z)
}
}
impl Vector {
pub const ZERO: Self = Self(glam::Vec3A::ZERO);
pub const I: Self = Self(glam::Vec3A::X);
pub const J: Self = Self(glam::Vec3A::Y);
pub const K: Self = Self(glam::Vec3A::Z);
pub fn new(x: f32, y: f32, z: f32) -> Self {
Self(glam::vec3a(x, y, z))
}
pub fn length(self) -> f32 {
self.0.length()
}
pub fn dot(self, other: Vector) -> f32 {
self.0.dot(other.0)
}
pub fn cross(self, other: Vector) -> Vector {
Self(self.0.cross(other.0))
}
}
impl Mul<f32> for Vector {
type Output = Self;
fn mul(self, rhs: f32) -> Self::Output {
Self(self.0 * rhs)
}
}
impl Mul<Vector> for f32 {
type Output = Vector;
fn mul(self, rhs: Vector) -> Self::Output {
Vector(self * rhs.0)
}
}
impl Div<f32> for Vector {
type Output = Self;
fn div(self, rhs: f32) -> Self::Output {
Self(self.0 / rhs)
}
}
impl Add for Vector {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self(self.0 + rhs.0)
}
}
#[repr(transparent)]
#[derive(Debug, Clone, Copy)]
pub struct Point(glam::Vec3A);
impl From<Point> for glam::Vec3 {
fn from(value: Point) -> Self {
value.0.into()
}
}
impl From<(f32, f32, f32)> for Point {
fn from((x, y, z): (f32, f32, f32)) -> Self {
Self::new(x, y, z)
}
}
impl Point {
pub const ORIGIN: Self = Self(glam::Vec3A::ZERO);
pub fn new(x: f32, y: f32, z: f32) -> Self {
Self(glam::vec3a(x, y, z))
}
}
impl Add<Vector> for Point {
type Output = Point;
fn add(self, rhs: Vector) -> Self::Output {
Self(self.0 + rhs.0)
}
}
impl Sub for Point {
type Output = Vector;
fn sub(self, rhs: Self) -> Self::Output {
Vector(self.0 - rhs.0)
}
}
#[derive(Debug, Copy, Clone)]
#[repr(transparent)]
pub struct Transform(glam::Affine3A);
impl Transform {
pub fn scale(x: f32, y: f32, z: f32) -> Self {
Self(glam::Affine3A::from_scale(glam::vec3(x, y, z)))
}
pub fn inverse(self) -> Self {
Self(self.0.inverse())
}
pub fn look_at(eye: Point, center: Point, up: Vector) -> Self {
Self(glam::Affine3A::look_at_lh(
eye.into(),
center.into(),
up.into(),
))
}
}
impl Mul for Transform {
type Output = Transform;
fn mul(self, rhs: Self) -> Self::Output {
Self(self.0 * rhs.0)
}
}
impl Mul<Vector> for Transform {
type Output = Vector;
fn mul(self, rhs: Vector) -> Self::Output {
Vector(self.0.transform_vector3a(rhs.0))
}
}
pub struct Ray {
origin: Point,
direction: Vector,
}
impl Ray {
pub fn new(origin: Point, direction: Vector) -> Self {
Self { origin, direction }
}
pub fn at(&self, t: f32) -> Point {
self.origin + t * self.direction
}
}

View file

@ -2,6 +2,7 @@
pub mod impl_bytemuck;
pub mod color;
mod geometry;
use color::Rgba;