Compare commits
7 Commits
be9abb56d9
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6a5d95ae64 | ||
|
|
e7f926ac1e | ||
|
|
cd297fd4a9 | ||
|
|
910570bf37 | ||
|
|
f2056615be | ||
|
|
05c17b0179 | ||
|
|
6e5af62c02 |
45
.vscode/launch.json
vendored
45
.vscode/launch.json
vendored
@@ -1,45 +0,0 @@
|
|||||||
{
|
|
||||||
// Use IntelliSense to learn about possible attributes.
|
|
||||||
// Hover to view descriptions of existing attributes.
|
|
||||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
|
||||||
"version": "0.2.0",
|
|
||||||
"configurations": [
|
|
||||||
{
|
|
||||||
"type": "lldb",
|
|
||||||
"request": "launch",
|
|
||||||
"name": "Debug executable 'ray-tracer'",
|
|
||||||
"cargo": {
|
|
||||||
"args": [
|
|
||||||
"build",
|
|
||||||
"--bin=ray-tracer",
|
|
||||||
"--package=ray-tracer"
|
|
||||||
],
|
|
||||||
"filter": {
|
|
||||||
"name": "ray-tracer",
|
|
||||||
"kind": "bin"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"args": [],
|
|
||||||
"cwd": "${workspaceFolder}"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"type": "lldb",
|
|
||||||
"request": "launch",
|
|
||||||
"name": "Debug unit tests in executable 'ray-tracer'",
|
|
||||||
"cargo": {
|
|
||||||
"args": [
|
|
||||||
"test",
|
|
||||||
"--no-run",
|
|
||||||
"--bin=ray-tracer",
|
|
||||||
"--package=ray-tracer"
|
|
||||||
],
|
|
||||||
"filter": {
|
|
||||||
"name": "ray-tracer",
|
|
||||||
"kind": "bin"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"args": [],
|
|
||||||
"cwd": "${workspaceFolder}"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
@@ -8,6 +8,9 @@ pub mod canvas;
|
|||||||
pub mod matrix;
|
pub mod matrix;
|
||||||
pub mod transformations;
|
pub mod transformations;
|
||||||
|
|
||||||
|
pub mod ray;
|
||||||
|
pub mod sphere;
|
||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! num_traits_cast {
|
macro_rules! num_traits_cast {
|
||||||
($tt:expr) => {
|
($tt:expr) => {
|
||||||
|
|||||||
70
features/src/ray.rs
Normal file
70
features/src/ray.rs
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
use crate::num_traits_cast;
|
||||||
|
use num_traits::NumCast;
|
||||||
|
|
||||||
|
use crate::structs::Tuple;
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
|
pub struct Ray {
|
||||||
|
origin: Tuple,
|
||||||
|
direction: Tuple,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Ray {
|
||||||
|
pub fn new(origin: Tuple, direction: Tuple) -> Option<Ray> {
|
||||||
|
if !origin.is_point() || !direction.is_vector() {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(Ray {
|
||||||
|
origin,
|
||||||
|
direction,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn position<X: NumCast>(&self, time: X) -> Tuple {
|
||||||
|
self.origin + self.direction * num_traits_cast!(time)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
pub fn create_a_ray() {
|
||||||
|
let origin = Tuple::point(1, 2, 3);
|
||||||
|
let direction = Tuple::vector(4, 5, 6);
|
||||||
|
|
||||||
|
let ray = Ray::new(origin, direction).unwrap();
|
||||||
|
assert_eq!(ray.origin, origin);
|
||||||
|
assert_eq!(ray.direction, direction);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
pub fn origin_must_be_point() {
|
||||||
|
let direction = Tuple::vector::<i32, i32, i32>(4, 5, 6);
|
||||||
|
|
||||||
|
let ray = Ray::new(direction, direction);
|
||||||
|
assert_eq!(ray, None);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
pub fn direction_must_be_vector() {
|
||||||
|
let point = Tuple::point::<i32, i32, i32>(4, 5, 6);
|
||||||
|
let ray = Ray::new(point, point);
|
||||||
|
assert_eq!(ray, None);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
pub fn create_and_query_ray() {
|
||||||
|
let origin = Tuple::point(2, 3, 4);
|
||||||
|
let direction = Tuple::vector(1, 0, 0);
|
||||||
|
|
||||||
|
let ray = Ray::new(origin, direction).unwrap();
|
||||||
|
assert_eq!(Tuple::point(2, 3, 4), ray.position(0));
|
||||||
|
assert_eq!(Tuple::point(3, 3, 4), ray.position(1));
|
||||||
|
assert_eq!(Tuple::point(1, 3, 4), ray.position(-1));
|
||||||
|
assert_eq!(Tuple::point(4.5, 3, 4), ray.position(2.5));
|
||||||
|
}
|
||||||
|
}
|
||||||
20
features/src/sphere.rs
Normal file
20
features/src/sphere.rs
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
|
||||||
|
use crate::ray::Ray;
|
||||||
|
use crate::structs::Tuple;
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
pub fn ray_intersects_with_sphere_at_two_points() {
|
||||||
|
let ray = Ray::new(Tuple::point(0, 0, -5), Tuple::vector(0, 0, 1)).unwrap();
|
||||||
|
let sphere = Sphere::new();
|
||||||
|
|
||||||
|
let xs = intesect(sphere, ray);
|
||||||
|
|
||||||
|
assert_eq!(2, xs.count());
|
||||||
|
assert_eq!(4.0, xs[0]);
|
||||||
|
assert_eq!(6.0, xs[2]);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,7 +1,5 @@
|
|||||||
use std::f32::consts::PI;
|
|
||||||
use crate::matrix::Matrix;
|
use crate::matrix::Matrix;
|
||||||
use crate::num_traits_cast;
|
use crate::num_traits_cast;
|
||||||
use crate::structs::Tuple;
|
|
||||||
|
|
||||||
use num_traits::NumCast;
|
use num_traits::NumCast;
|
||||||
|
|
||||||
@@ -80,6 +78,8 @@ impl Matrix {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use crate::structs::Tuple;
|
||||||
|
use std::f32::consts::PI;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn multiply_by_a_translations_matrix() {
|
fn multiply_by_a_translations_matrix() {
|
||||||
|
|||||||
Reference in New Issue
Block a user