Create the basic Tuple, Point, and Vector types
This commit is contained in:
parent
15c4ae9bad
commit
01f3e05235
|
@ -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"
|
||||
|
|
|
@ -27,5 +27,5 @@ members = [
|
|||
"sgf",
|
||||
"timezone-testing",
|
||||
"tree",
|
||||
"visions/server",
|
||||
"visions/server", "ray-tracer",
|
||||
]
|
||||
|
|
|
@ -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]
|
|
@ -0,0 +1,2 @@
|
|||
mod types;
|
||||
|
|
@ -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));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue