From 00000050246d8cf499a784f1113bd2a6997462d6 Mon Sep 17 00:00:00 2001 From: Michael Zhang Date: Tue, 31 Jan 2023 01:15:22 -0600 Subject: [PATCH] Ray intersection --- assignment-1/.gitignore | 1 + assignment-1/Cargo.lock | 330 +++++++++++++++++++++++++++++++++ assignment-1/Cargo.toml | 10 + assignment-1/src/input_file.rs | 9 + assignment-1/src/main.rs | 39 ++++ assignment-1/src/ray.rs | 70 +++++++ assignment-1/src/scene_data.rs | 74 ++++++++ 7 files changed, 533 insertions(+) create mode 100644 assignment-1/.gitignore create mode 100644 assignment-1/Cargo.lock create mode 100644 assignment-1/Cargo.toml create mode 100644 assignment-1/src/input_file.rs create mode 100644 assignment-1/src/main.rs create mode 100644 assignment-1/src/ray.rs create mode 100644 assignment-1/src/scene_data.rs diff --git a/assignment-1/.gitignore b/assignment-1/.gitignore new file mode 100644 index 000000000..ea8c4bf --- /dev/null +++ b/assignment-1/.gitignore @@ -0,0 +1 @@ +/target diff --git a/assignment-1/Cargo.lock b/assignment-1/Cargo.lock new file mode 100644 index 000000000..b05f05b --- /dev/null +++ b/assignment-1/Cargo.lock @@ -0,0 +1,330 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "anyhow" +version = "1.0.68" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2cb2f989d18dd141ab8ae82f64d1a8cdd37e0840f73a406896cf5e99502fab61" + +[[package]] +name = "assignment-1" +version = "0.1.0" +dependencies = [ + "anyhow", + "clap", +] + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "cc" +version = "1.0.78" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a20104e2335ce8a659d6dd92a51a767a0c062599c73b343fd152cb401e828c3d" + +[[package]] +name = "clap" +version = "4.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f13b9c79b5d1dd500d20ef541215a6423c75829ef43117e1b4d17fd8af0b5d76" +dependencies = [ + "bitflags", + "clap_derive", + "clap_lex", + "is-terminal", + "once_cell", + "strsim", + "termcolor", +] + +[[package]] +name = "clap_derive" +version = "4.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "684a277d672e91966334af371f1a7b5833f9aa00b07c84e92fbce95e00208ce8" +dependencies = [ + "heck", + "proc-macro-error", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "783fe232adfca04f90f56201b26d79682d4cd2625e0bc7290b95123afe558ade" +dependencies = [ + "os_str_bytes", +] + +[[package]] +name = "errno" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f639046355ee4f37944e44f60642c6f3a7efa3cf6b78c78a0d989a8ce6c396a1" +dependencies = [ + "errno-dragonfly", + "libc", + "winapi", +] + +[[package]] +name = "errno-dragonfly" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf" +dependencies = [ + "cc", + "libc", +] + +[[package]] +name = "heck" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9" + +[[package]] +name = "hermit-abi" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee512640fe35acbfb4bb779db6f0d80704c2cacfa2e39b601ef3e3f47d1ae4c7" +dependencies = [ + "libc", +] + +[[package]] +name = "io-lifetimes" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7d6c6f8c91b4b9ed43484ad1a938e393caf35960fce7f82a040497207bd8e9e" +dependencies = [ + "libc", + "windows-sys", +] + +[[package]] +name = "is-terminal" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28dfb6c8100ccc63462345b67d1bbc3679177c75ee4bf59bf29c8b1d110b8189" +dependencies = [ + "hermit-abi", + "io-lifetimes", + "rustix", + "windows-sys", +] + +[[package]] +name = "libc" +version = "0.2.139" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79" + +[[package]] +name = "linux-raw-sys" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f051f77a7c8e6957c0696eac88f26b0117e54f52d3fc682ab19397a8812846a4" + +[[package]] +name = "once_cell" +version = "1.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f61fba1741ea2b3d6a1e3178721804bb716a68a6aeba1149b5d52e3d464ea66" + +[[package]] +name = "os_str_bytes" +version = "6.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b7820b9daea5457c9f21c69448905d723fbd21136ccf521748f23fd49e723ee" + +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "syn", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] + +[[package]] +name = "proc-macro2" +version = "1.0.50" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ef7d57beacfaf2d8aee5937dab7b7f28de3cb8b1828479bb5de2a7106f2bae2" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8856d8364d252a14d474036ea1358d63c9e6965c8e5c1885c18f73d70bff9c7b" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rustix" +version = "0.36.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4fdebc4b395b7fbb9ab11e462e20ed9051e7b16e42d24042c776eca0ac81b03" +dependencies = [ + "bitflags", + "errno", + "io-lifetimes", + "libc", + "linux-raw-sys", + "windows-sys", +] + +[[package]] +name = "strsim" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" + +[[package]] +name = "syn" +version = "1.0.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f4064b5b16e03ae50984a5a8ed5d4f8803e6bc1fd170a3cda91a1be4b18e3f5" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "termcolor" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be55cf8942feac5c765c2c993422806843c9a9a45d4d5c407ad6dd2ea95eb9b6" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "unicode-ident" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "84a22b9f218b40614adcb3f4ff08b703773ad44fa9423e4e0d346d5db86e4ebc" + +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi", +] + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows-sys" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a3e1820f08b8513f676f7ab6c1f99ff312fb97b553d30ff4dd86f9f15728aa7" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8c9864e83243fdec7fc9c5444389dcbbfd258f745e7853198f365e3c4968a608" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c8b1b673ffc16c47a9ff48570a9d85e25d265735c503681332589af6253c6c7" + +[[package]] +name = "windows_i686_gnu" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de3887528ad530ba7bdbb1faa8275ec7a1155a45ffa57c37993960277145d640" + +[[package]] +name = "windows_i686_msvc" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf4d1122317eddd6ff351aa852118a2418ad4214e6613a50e0191f7004372605" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1040f221285e17ebccbc2591ffdc2d44ee1f9186324dd3e84e99ac68d699c45" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "628bfdf232daa22b0d64fdb62b09fcc36bb01f05a3939e20ab73aaf9470d0463" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "447660ad36a13288b1db4d4248e857b510e8c3a225c822ba4fb748c0aafecffd" diff --git a/assignment-1/Cargo.toml b/assignment-1/Cargo.toml new file mode 100644 index 000000000..1b68709 --- /dev/null +++ b/assignment-1/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "assignment-1" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +anyhow = "1.0.68" +clap = { version = "4.1.4", features = ["derive"] } diff --git a/assignment-1/src/input_file.rs b/assignment-1/src/input_file.rs new file mode 100644 index 000000000..4a4d8e8 --- /dev/null +++ b/assignment-1/src/input_file.rs @@ -0,0 +1,9 @@ +use std::path::Path; + +use anyhow::Result; + +use crate::scene_data::Scene; + +pub fn parse_input_file(path: impl AsRef) -> Result { + todo!() +} diff --git a/assignment-1/src/main.rs b/assignment-1/src/main.rs new file mode 100644 index 000000000..5a722b9 --- /dev/null +++ b/assignment-1/src/main.rs @@ -0,0 +1,39 @@ +mod input_file; +mod ray; +mod scene_data; + +use std::path::PathBuf; + +use anyhow::Result; +use clap::Parser; + +use crate::input_file::parse_input_file; + +/// Noise image generator. +#[derive(Parser)] +#[clap(author, version, about, long_about = None)] +struct Opt { + /// Path to the input file to use. + /// + /// The input file should follow this format: + /// + /// imsize [width] [height] + /// + /// Where `imsize' is a keyword, and `width' and `height' are integer values + /// denoting the desired size of the image to be generated. + #[clap()] + input_path: PathBuf, +} + +fn main() -> Result<()> { + let opt = Opt::parse(); + + let scene = parse_input_file(&opt.input_path)?; + + // Loop through every single pixel of the output file + for (px, py) in (0..scene.image_width).zip(0..scene.image_height) { + + } + + Ok(()) +} diff --git a/assignment-1/src/ray.rs b/assignment-1/src/ray.rs new file mode 100644 index 000000000..c963fbf --- /dev/null +++ b/assignment-1/src/ray.rs @@ -0,0 +1,70 @@ +use crate::scene_data::{Sphere, Vec3}; + +/// A normalized Ray +pub struct Ray { + origin: Vec3, + direction: Vec3, +} + +impl Ray { + pub fn eval(&self, time: f64) -> Vec3 { + self.origin + self.direction * time + } +} + +/// Given a ray and a sphere, returns the first point at which this ray intersects the sphere. +/// +/// If there is no intersection point, returns None. +pub fn ray_intersection_time(ray: &Ray, sphere: &Sphere) -> Option { + let a = + ray.direction.x.powi(2) + ray.direction.y.powi(2) + ray.direction.z.powi(2); + let b = 2.0 + * (ray.direction.x * (ray.origin.x - sphere.center.x) + + ray.direction.y * (ray.origin.y - sphere.center.y) + + ray.direction.z * (ray.origin.z - sphere.center.z)); + let c = (ray.origin.x - sphere.center.x).powi(2) + + (ray.origin.y - sphere.center.y).powi(2) + + (ray.origin.z - sphere.center.z).powi(2) + - sphere.radius.powi(2); + let discriminant = b * b - 4.0 * a * c; + + match discriminant { + // Discriminant < 0, means the equation has no solutions. + d if d < 0.0 => return None, + + // Discriminant == 0 + d if d == 0.0 => { + return Some(-b / (2.0 * a)); + } + + d if d > 0.0 => { + let solution_1 = (-b + discriminant.sqrt()) / (2.0 * a); + let solution_2 = (-b - discriminant.sqrt()) / (2.0 * a); + + return Some(solution_1.min(solution_2)); + } + _ => unreachable!("Invalid determinant value: {discriminant}"), + } +} + +#[cfg(test)] +mod tests { + use crate::scene_data::{Sphere, Vec3}; + + use super::{ray_intersection_time, Ray}; + + #[test] + fn practice_problem_slide_154() { + let ray = Ray { + origin: Vec3::new(0.0, 0.0, 0.0), + direction: Vec3::new(0.0, 0.0, -1.0), + }; + let sphere = Sphere { + center: Vec3::new(0.0, 0.0, -10.0), + radius: 4.0, + }; + + let t = ray_intersection_time(&ray, &sphere).unwrap(); + assert_eq!(ray.eval(t), Vec3::new(0.0, 0.0, -6.0)); + } +} diff --git a/assignment-1/src/scene_data.rs b/assignment-1/src/scene_data.rs new file mode 100644 index 000000000..99ae346 --- /dev/null +++ b/assignment-1/src/scene_data.rs @@ -0,0 +1,74 @@ +use std::ops::{Add, Mul}; + +#[derive(Copy, Clone, Debug, PartialEq, Eq)] +pub struct Vec3 { + pub x: T, + pub y: T, + pub z: T, +} + +impl Vec3 { + pub fn new(x: T, y: T, z: T) -> Self { + Vec3 { x, y, z } + } +} + +/// Vector addition +impl Add> for Vec3 +where + T: Add, +{ + type Output = Vec3; + + fn add(self, rhs: Vec3) -> Self::Output { + Vec3::new(self.x + rhs.x, self.y + rhs.y, self.z + rhs.z) + } +} + +/// Scalar multiplication +impl Mul for Vec3 +where + T: Mul, + U: Copy, +{ + type Output = Vec3; + + fn mul(self, rhs: U) -> Self::Output { + Vec3::new(self.x * rhs, self.y * rhs, self.z * rhs) + } +} + +pub struct Sphere { + pub center: Vec3, + pub radius: f64, +} + +pub enum Object { + Sphere(Sphere), + Cylinder { + center: Vec3, + direction: Vec3, + radius: f64, + length: f64, + }, +} + +pub struct Scene { + pub eye_pos: Vec3, + pub view_dir: Vec3, + pub up_dir: Vec3, + + /// Horizontal field of view (in degrees) + pub hfov: f64, + + pub image_width: usize, + pub image_height: usize, + + /// Background color + pub bkg_color: Vec3, + + /// Material color + pub mtl_color: Vec3, + + pub objects: Vec, +}