diff --git a/assignment-1b/.gitignore b/assignment-1b/.gitignore index 44d187f..699e25f 100644 --- a/assignment-1b/.gitignore +++ b/assignment-1b/.gitignore @@ -1,5 +1,6 @@ /target -/assignment-1 +/assignment-1b +/raytracer1b /examples/*.png *.ppm *.zip diff --git a/assignment-1b/Cargo.toml b/assignment-1b/Cargo.toml index 63a1cf3..2d30ba5 100644 --- a/assignment-1b/Cargo.toml +++ b/assignment-1b/Cargo.toml @@ -4,6 +4,10 @@ authors = ["Michael Zhang "] version = "0.1.0" edition = "2021" +[[bin]] +name = "raytracer1b" +path = "src/main.rs" + [dependencies] anyhow = "1.0.68" clap = { version = "4.1.4", features = ["derive"] } diff --git a/assignment-1b/Makefile b/assignment-1b/Makefile index d7d255e..de80502 100644 --- a/assignment-1b/Makefile +++ b/assignment-1b/Makefile @@ -8,7 +8,7 @@ PANDOC := pandoc CONVERT := convert HANDIN := hw1b.michael.zhang.zip -BINARY := ./assignment-1b +BINARY := ./raytracer1b WRITEUP := writeup.pdf SOURCES := $(shell find -name "*.rs") @@ -27,7 +27,7 @@ $(BINARY): $(SOURCES) -w /usr/src/myapp \ rust \ cargo build --release - mv target/release/assignment-1b $@ + mv target/release/raytracer1b $@ $(HANDIN): $(BINARY) $(WRITEUP) Makefile Cargo.toml Cargo.lock README.md $(EXAMPLES_PNG) $(EXAMPLES_PPM) $(ZIP) -r $@ src examples $^ diff --git a/assignment-1b/assignment-1b b/assignment-1b/assignment-1b deleted file mode 100755 index bc2f626..000000000 Binary files a/assignment-1b/assignment-1b and /dev/null differ diff --git a/assignment-1b/examples/Hw1bSample1.txt b/assignment-1b/examples/Hw1bSample1.txt new file mode 100755 index 000000000..dc42708 --- /dev/null +++ b/assignment-1b/examples/Hw1bSample1.txt @@ -0,0 +1,14 @@ +imsize 512 512 +eye 0 0 0 +viewdir 0 0.1 -1 +hfov 90 +updir 0 1 0 +bkgcolor 0.1 0.1 0.1 +light -1 -1 -1 0 0.9 0.5 0.05 +mtlcolor 0 1 0 1 1 1 0.6 0.2 0.2 10 +sphere 0 1.5 -4 1 +mtlcolor 0 1 0 1 1 1 0.1 0.8 0.2 10 +sphere -1.275 -0.75 -4 1 +mtlcolor 0 1 0 1 1 1 0.1 0.2 0.8 10 +sphere 1.275 -0.75 -4 1 + diff --git a/assignment-1b/src/main.rs b/assignment-1b/src/main.rs index 6235353..65c0cea 100644 --- a/assignment-1b/src/main.rs +++ b/assignment-1b/src/main.rs @@ -15,6 +15,7 @@ use std::path::PathBuf; use anyhow::Result; use clap::Parser; use rayon::prelude::{IntoParallelIterator, ParallelIterator}; +use scene::data::ObjectKind; use crate::image::Image; use crate::input_file::parse_input_file; @@ -82,8 +83,12 @@ fn main() -> Result<()> { // Generate a parallel iterator for pixels // The iterator preserves order and uses row-major order let pixels_iter = (0..scene.image_height) - .into_par_iter() - .flat_map(|y| (0..scene.image_width).into_par_iter().map(move |x| (x, y))); + // .into_par_iter() + .flat_map(|y| { + (0..scene.image_width) + // .into_par_iter() + .map(move |x| (x, y)) + }); // Loop through every single pixel of the output file let pixels = pixels_iter diff --git a/assignment-1b/src/scene/cylinder.rs b/assignment-1b/src/scene/cylinder.rs index 07459ac..9a6ad16 100644 --- a/assignment-1b/src/scene/cylinder.rs +++ b/assignment-1b/src/scene/cylinder.rs @@ -132,8 +132,7 @@ impl ObjectKind for Cylinder { let c = rotated_cylinder_center; if r.z == 0.0 { - // No solutions here - vec![] + Vec::new() // No solutions here } else { vec![ (-o.z + c.z + self.length / 2.0) / r.z, @@ -177,3 +176,29 @@ impl ObjectKind for Cylinder { Ok(solutions.min_by_key(|ctx| ctx.time)) } } + +#[cfg(test)] +mod tests { + use nalgebra::Vector3; + + use crate::{ray::Ray, scene::data::ObjectKind}; + + use super::Cylinder; + + #[test] + fn test_cylinder() { + let cylinder = Cylinder { + center: Vector3::new(0.0, 0.0, 0.0), + direction: Vector3::new(0.0, 1.0, 0.0), + radius: 3.0, + length: 4.0, + }; + + let eye = Vector3::new(0.0, 3.0, 3.0); + let end = Vector3::new(0.0, 2.0, 2.0); + let ray = Ray::from_endpoints(eye, end); + + let res = cylinder.intersects_ray_at(&ray); + panic!("Result: {res:?}"); + } +} diff --git a/assignment-1b/src/scene/data.rs b/assignment-1b/src/scene/data.rs index 8e67cf3..f053285 100644 --- a/assignment-1b/src/scene/data.rs +++ b/assignment-1b/src/scene/data.rs @@ -84,6 +84,7 @@ pub struct Scene { pub objects: Vec, } +/// Information about an intersection #[derive(Derivative)] #[derivative(Debug, PartialEq, PartialOrd, Ord)] pub struct IntersectionContext { diff --git a/assignment-1b/src/utils.rs b/assignment-1b/src/utils.rs index 10b1680..1c798f7 100644 --- a/assignment-1b/src/utils.rs +++ b/assignment-1b/src/utils.rs @@ -25,7 +25,10 @@ where } /// Calculate the rotation matrix between the 2 given vectors -/// Based on the method here: https://math.stackexchange.com/a/897677 +/// +/// Based on the method given [here][1]. +/// +/// [1]: https://math.stackexchange.com/a/897677 pub fn compute_rotation_matrix( a: Vector3, b: Vector3,