Skip to content

Commit

Permalink
Merge pull request #18 from soumyasen1809/tasks
Browse files Browse the repository at this point in the history
Add unit tests and read scenes from JSON file
  • Loading branch information
soumyasen1809 authored Aug 18, 2024
2 parents f0f133e + 8f89c7c commit 786bb29
Show file tree
Hide file tree
Showing 19 changed files with 445 additions and 152 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/.[Vv]scode
*.ppm
*.png
backup_code.rs
88 changes: 86 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 10 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,25 @@ authors = ["Sen"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
# Your dependencies here
lib ={ path = "lib"} # needed for the tests to work
rand="0.3.14"
rayon = "1.10.0"

[dev-dependencies]
# Dependencies needed only for testing
# mockall = "0.13.0"

[[bin]]
name = "bin"
path = "src/main.rs"

[[test]]
name = "test"
path = "tests/common_config.rs"

# Check https://rust-classes.com/chapter_4_3
[workspace]
members = [
"lib",
"lib" # DONOT include the tests directory here and DONOT create a Cargo.toml file in the tests directory
]
resolver = "2"

Expand Down
33 changes: 31 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,34 @@
# RustRayTracer
# RustyRayTracer

<!-- See: https://github.com/KPMGE/KPMGE/blob/main/README.md -->
<table align="center">
<tr>
<td align="center" width="96">
<a href="#rust">
<img src="https://cdn.jsdelivr.net/gh/devicons/devicon@latest/icons/rust/rust-original.svg" width="48" height="48"
alt="Rust" />
</a>
<br>Rust
<td align="center" width="96">
<a href="#git">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/3/3f/Git_icon.svg/1200px-Git_icon.svg.png"
width="48" height="48" alt="Git" />
</a>
<br>Git
</td>
<td align="center" width="96">
<img src="https://user-images.githubusercontent.com/25181517/192108374-8da61ba1-99ec-41d7-80b8-fb2f7c0a4948.png"
width="48" height="48" alt="GitHub" />
<br>Github
</td>
<td align="center" width="96">
<a href="#" target="_blank"> <img
src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/linux/linux-original.svg" alt="webpack" width="40"
height="40" /> </a>
<br>Linux
</td>
</tr>
</table>

## About
This is a simple ray-tracer implementation in Rust of [Peter Shirley's "Ray Tracing In One Weekend"](https://raytracing.github.io/books/RayTracingInOneWeekend.html) book. This is the first of the series:
Expand Down Expand Up @@ -29,4 +59,3 @@ Every commit in the code implements a particular chapter. In this way it's easy

## License
This project is licensed under the GNU GENERAL PUBLIC license. See the LICENSE file for more details.

3 changes: 2 additions & 1 deletion lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ authors = ["Sen"]

[dependencies]
rand="0.3.14"
rayon = "1.10.0"
rayon = "1.10.0"
serde_json = "1.0.125"
14 changes: 6 additions & 8 deletions lib/src/utilities/camera.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use rayon::prelude::*;

use rand::Rng;
use rayon::prelude::*;

use super::{
color::Color,
Expand Down Expand Up @@ -58,7 +57,7 @@ impl Camera {

// Render and write to file
let file_path = "image_test.ppm";
let mut file = File::create(&file_path).unwrap();
let mut file = File::create(file_path).unwrap();
writeln!(file, "P3\n{} {}\n255", self.image_width, self.image_height).unwrap();

let pixel_color_vec: Vec<Color> = (0..self.image_height)
Expand All @@ -75,17 +74,16 @@ impl Camera {
Self::ray_color(ray_sent, self.max_depth, &world[..])
})
.sum(); // need to implement sum trait for Color
return pixel_color; // Return the Color from the map closure
pixel_color // Return the Color from the map closure
})
.collect::<Vec<Color>>() // Collect the inner Vec<Color>
})
.collect(); // Collect the outer Vec<Color>

for pixel in pixel_color_vec.iter() {
let write_res = (*pixel * self.pixel_samples_scale).write_color(&mut file);
match write_res {
Err(e) => println!("Error in writing result to file: {}", e),
_ => (), // The () is just the unit value, so nothing will happen
if let Err(e) = write_res {
println!("Error in writing result to file: {}", e)
}
}

Expand Down Expand Up @@ -137,7 +135,7 @@ impl Camera {
return Color::default();
}

if let Some(hit) = world.hit(ray, Interval::new(0.001, std::f64::INFINITY)) {
if let Some(hit) = world.hit(ray, Interval::new(0.001, f64::INFINITY)) {
if let Some(scatter) = hit.material.scatter(ray, &hit) {
let Scatter {
scattered_ray,
Expand Down
2 changes: 1 addition & 1 deletion lib/src/utilities/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl Color {
if linear_component > 0.0 {
return linear_component.sqrt();
}
return 0.0;
0.0
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/src/utilities/hit_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ impl<'a> HitRecord<'a> {
material,
}
}
/// Sets the hit record normal vector.
/// The parameter `outward_normal` is assumed to have unit length
pub fn set_face_normal(
ray: Ray,
outward_normal: Vector3,
point: Point3,
material: &'a dyn Material,
parameter: f64,
) -> Self {
// Sets the hit record normal vector.
// NOTE: the parameter `outward_normal` is assumed to have unit length
let is_face_front = ray.get_direction().dot_prod(outward_normal) < 0.0;
let normal = match is_face_front {
true => outward_normal,
Expand Down
8 changes: 2 additions & 6 deletions lib/src/utilities/interval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ pub struct Interval {

impl Interval {
// https://stackoverflow.com/questions/26549480/how-do-i-declare-a-static-field-in-a-struct-in-rust
// const EMPTY: Interval = Interval::new(std::f64::INFINITY, std::f64::NEG_INFINITY);
// const UNIVERSE: Interval = Interval::new(std::f64::NEG_INFINITY, std::f64::INFINITY);

pub const fn new(min: f64, max: f64) -> Self {
// Needs to add it as const fn, since EMPTY const needs a const function
Self { min, max }
}

Expand All @@ -29,8 +25,8 @@ impl Interval {
impl Default for Interval {
fn default() -> Self {
Self {
min: std::f64::INFINITY,
max: std::f64::NEG_INFINITY,
min: f64::INFINITY,
max: f64::NEG_INFINITY,
}
}
}
1 change: 1 addition & 0 deletions lib/src/utilities/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ pub mod interval;
pub mod material;
pub mod point;
pub mod ray;
pub mod scenes;
pub mod vector3;
4 changes: 1 addition & 3 deletions lib/src/utilities/point.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use super::vector3::Vector3;
use std::ops::{Add, Neg, Sub};

/// The Point3 Class
///
use super::vector3::Vector3;

#[derive(Clone, Copy)]
pub struct Point3 {
Expand Down
14 changes: 1 addition & 13 deletions lib/src/utilities/ray.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use super::{point::Point3, vector3::Vector3};

/// The Ray Class
///
#[derive(Clone, Copy)]
#[derive(Clone, Copy, Default)]
pub struct Ray {
origin: Point3,
direction: Vector3,
Expand All @@ -26,12 +23,3 @@ impl Ray {
self.direction
}
}

impl Default for Ray {
fn default() -> Self {
Self {
origin: Point3::default(),
direction: Vector3::default(),
}
}
}
Loading

0 comments on commit 786bb29

Please sign in to comment.