Create the basic Tuple, Point, and Vector types

This commit is contained in:
Savanni D'Gerinel 2024-06-09 12:24:41 -04:00
parent 15c4ae9bad
commit 01f3e05235
5 changed files with 77 additions and 1 deletions

4
Cargo.lock generated
View File

@ -3331,6 +3331,10 @@ dependencies = [
"rand_core 0.6.4",
]
[[package]]
name = "ray-tracer"
version = "0.1.0"
[[package]]
name = "rayon"
version = "1.8.0"

View File

@ -27,5 +27,5 @@ members = [
"sgf",
"timezone-testing",
"tree",
"visions/server",
"visions/server", "ray-tracer",
]

8
ray-tracer/Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[package]
name = "ray-tracer"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

2
ray-tracer/src/lib.rs Normal file
View File

@ -0,0 +1,2 @@
mod types;

62
ray-tracer/src/types.rs Normal file
View File

@ -0,0 +1,62 @@
const EPSILON: f64 = 0.00001;
fn eq_f64(l: f64, r: f64) -> bool {
(l-r).abs() < EPSILON
}
struct Tuple {
x: f64,
y: f64,
z: f64,
w: f64, // Used for very low-level math. w = 1.0 indicates a point, w = 0.0 indicates a vector.
// Theoretically the type system should make this redundant, so operations on points
// and vectors can always assert the correct value.
}
impl PartialEq for Tuple {
fn eq(&self, r: &Tuple) -> bool {
eq_f64(self.x, r.x) && eq_f64(self.y, r.y) && eq_f64(self.z, r.z) && eq_f64(self.w, r.w)
}
}
struct Point(Tuple);
impl Point {
fn new(x: f64, y: f64, z: f64) -> Self {
Self(Tuple{ x, y, z, w: 1.0 })
}
}
impl Default for Point {
fn default() -> Self {
Self::new(0., 0., 0.)
}
}
struct Vector(Tuple);
impl Vector {
fn new(x: f64, y: f64, z: f64) -> Self {
Self(Tuple{ x, y, z, w: 0.0 })
}
}
impl Default for Vector {
fn default() -> Self {
Self::new(0., 0., 0.)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn eq_f64_compares_values() {
assert!(eq_f64(1.0, 1.0));
assert!(eq_f64(0.9994, 0.9994));
assert!(eq_f64(0.9999994, 0.9999995));
assert!(!eq_f64(0.9995, 0.9994));
}
}