Skip to content

Commit

Permalink
added Axis
Browse files Browse the repository at this point in the history
  • Loading branch information
scott223 committed Nov 20, 2023
1 parent 3e0e0f2 commit 11a2ce0
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 36 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
![GitHub CI Status](https://img.shields.io/github/actions/workflow/status/scott223/raytracer/rust.yml?style=flat-square&logo=github)
[![dependency status](https://deps.rs/repo/github/scott223/raytracer/status.svg)](https://deps.rs/repo/github/scott223/raytracer)
[![lines count](https://img.shields.io/endpoint?url=https://ghloc.vercel.app/api/scott223/raytracer/badge?filter=.ts$,.tsx$,.obj$,.json$)](https://ghloc.vercel.app/scott223/raytracer/badge?filter=.ts$,.tsx$,.obj$,.json$)


# Raytracer
Simple Raytracer for simple 3D scenes, made in Rust. Objective is to learn Rust, not make a very good raytracer.
Expand Down
Binary file modified renders/render.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 15 additions & 17 deletions src/bvh/aabb.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use crate::elements::Element;
use crate::elements::Hittable;
use crate::linalg::Vec3;
use crate::render::Axis;
use crate::render::Interval;
use crate::render::Ray;

use std::fmt;
use std::ops::Index;

// axis aligned bounding box

/// Axis Aligned Bounding Box struct
#[derive(Debug, Clone, Copy)]
pub struct Aabb {
x: Interval,
Expand Down Expand Up @@ -108,7 +108,7 @@ impl Aabb {
return bbox;
}

// return an AABB that has no side narrower than some delta, padding if necessary
/// Returns an AABB that has no side narrower than some delta, padding if necessary.
pub fn pad(&self) -> Self {
let delta: f64 = 0.0001;

Expand Down Expand Up @@ -152,26 +152,27 @@ impl Aabb {
}

#[inline(always)]
pub fn largest_axis(&self) -> usize {
pub fn largest_axis(&self) -> Axis {
let size = self.max - self.min;

if size.x() > size.y() && size.x() > size.z() {
0
Axis::X
} else if size.y() > size.z() {
1
Axis::Y
} else {
2
Axis::Z
}
}

/// Returns the position of the centriod of the Aabb,
/// Returns the position of the centriod of the Aabb.
#[inline(always)]
pub fn centroid(&self) -> Vec3 {
calculate_centroid(*self)
}

// checks if we have a hit with the aabb, in a given interval
// source: https://docs.rs/bvh/latest/src/bvh/ray.rs.html#168-188
/// Returns if we have a hit with the aabb, in a given interval
///
/// Source: <https://docs.rs/bvh/latest/src/bvh/ray.rs.html#168-188>
pub fn hit(&self, ray: &Ray, _ray_t: &mut Interval) -> bool {
let mut ray_min = (self[ray.sign_x].x() - ray.origin.x()) * ray.inv_direction.x();
Expand Down Expand Up @@ -215,12 +216,8 @@ impl fmt::Display for Aabb {
}
}

/// Make [`AABB`]s indexable. `aabb[0]` gives a reference to the minimum bound.
/// Make [`Aabb`]s indexable. `aabb[0]` gives a reference to the minimum bound.
/// All other indices return a reference to the maximum bound.
///
///
///
impl Index<usize> for Aabb {
type Output = Vec3;

Expand All @@ -237,6 +234,7 @@ impl Index<usize> for Aabb {
mod tests {
use crate::bvh::aabb::Aabb;
use crate::linalg::Vec3;
use crate::render::Axis;
use crate::render::Interval;
use crate::render::Ray;
use assert_approx_eq::assert_approx_eq;
Expand Down Expand Up @@ -349,7 +347,7 @@ mod tests {

let largest_axis = aabb_p.largest_axis();

assert_eq!(largest_axis, 1);
assert_eq!(largest_axis, Axis::Y);

let p: Vec3 = Vec3::new(1.0, 1.0, 1.0);
let q: Vec3 = Vec3::new(2.0, 3.0, 5.0);
Expand All @@ -358,6 +356,6 @@ mod tests {

let largest_axis = aabb_p.largest_axis();

assert_eq!(largest_axis, 2);
assert_eq!(largest_axis, Axis::Z);
}
}
8 changes: 4 additions & 4 deletions src/bvh/bvh_node_sah.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::f64::EPSILON;
use super::aabb::Aabb;
use crate::{
elements::*,
render::{Interval, Ray},
render::{Interval, Ray, Axis},
};

// this is a node for the BHV tree, and it consists of objects with a hittable trait (either another node, or an element)
Expand Down Expand Up @@ -249,13 +249,13 @@ impl BVHNode_SAH {
let mut child_r_indices: Vec<usize> = Vec::with_capacity(indices.len() / 2);

// find the longest axis, to decide how to sort
let sort_axis = aabb_centroids.largest_axis();
let sort_axis_size = aabb_centroids.max[sort_axis] - aabb_centroids.min[sort_axis];
let sort_axis: Axis = aabb_centroids.largest_axis();
let sort_axis_size: f64 = aabb_centroids.max[sort_axis] - aabb_centroids.min[sort_axis];

if sort_axis_size < EPSILON {
// axis is too small, lets just divide into 2

let mid = indices.len() / 2 as usize;
let mid: usize = indices.len() / 2 as usize;

// TODO: write this one more idiomatic...
for (i, index) in indices.iter().enumerate() {
Expand Down
25 changes: 18 additions & 7 deletions src/linalg/mat4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ impl Mat4 {
}
}

/// Computes the transpose matrix
/// Returns the transpose matrix.
#[inline(always)]
pub fn transpose(d_x: f64, d_y: f64, d_z: f64) -> Mat4 {
// TODO: return the transpose matrix!!

Mat4 {
data: [
[1., 0., 0., d_x],
Expand All @@ -36,6 +38,9 @@ impl Mat4 {
}
}

/// Computes the scale matrix (around the origin)
///
/// Source: <https://www.brainvoyager.com/bv/doc/UsersGuide/CoordsAndTransforms/SpatialTransformationMatrices.html>
#[inline(always)]
pub fn scale(s_x: f64, s_y: f64, s_z: f64) -> Mat4 {
Mat4 {
Expand All @@ -49,7 +54,8 @@ impl Mat4 {
}

/// Computes the rotation matrix (around the origin)
/// source: https://www.brainvoyager.com/bv/doc/UsersGuide/CoordsAndTransforms/SpatialTransformationMatrices.html
///
/// Source: <https://www.brainvoyager.com/bv/doc/UsersGuide/CoordsAndTransforms/SpatialTransformationMatrices.html>
#[inline(always)]
pub fn rotate_x(theta_x: f64) -> Mat4 {
Mat4 {
Expand All @@ -63,7 +69,8 @@ impl Mat4 {
}

/// Computes the rotation matrix (around the origin)
/// source: https://www.brainvoyager.com/bv/doc/UsersGuide/CoordsAndTransforms/SpatialTransformationMatrices.html
///
/// Source: <https://www.brainvoyager.com/bv/doc/UsersGuide/CoordsAndTransforms/SpatialTransformationMatrices.html>
#[inline(always)]
pub fn rotate_y(theta_y: f64) -> Mat4 {
Mat4 {
Expand All @@ -77,7 +84,8 @@ impl Mat4 {
}

/// Computes the rotation matrix (around the origin)
/// source: https://www.brainvoyager.com/bv/doc/UsersGuide/CoordsAndTransforms/SpatialTransformationMatrices.html
///
/// source: <https://www.brainvoyager.com/bv/doc/UsersGuide/CoordsAndTransforms/SpatialTransformationMatrices.html>
#[inline(always)]
pub fn rotate_z(theta_z: f64) -> Mat4 {
Mat4 {
Expand All @@ -91,7 +99,8 @@ impl Mat4 {
}

/// Computes the determinant of a 4x4 matrix
/// Source: https://docs.piston.rs/graphics/src/vecmath/lib.rs.html
///
/// Source: <https://docs.piston.rs/graphics/src/vecmath/lib.rs.html>
#[inline(always)]
#[allow(dead_code)]
pub fn determinant(&self) -> f64 {
Expand Down Expand Up @@ -124,15 +133,17 @@ impl Mat4 {
}

/// Computes the inverse determinant of a 4x4 matrix
/// Source: https://docs.piston.rs/graphics/src/vecmath/lib.rs.html
///
/// Source: <https://docs.piston.rs/graphics/src/vecmath/lib.rs.html>
#[inline(always)]
#[allow(dead_code)]
fn inv_determinant(&self) -> f64 {
1.0 / self.determinant()
}

/// Computes the inverse of a 4x4 matrix.
/// Source: https://docs.piston.rs/graphics/src/vecmath/lib.rs.html
///
/// Source: <https://docs.piston.rs/graphics/src/vecmath/lib.rs.html>
#[inline(always)]
#[allow(dead_code)]
pub fn inverse(&self) -> Mat4 {
Expand Down
6 changes: 3 additions & 3 deletions src/linalg/vec3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
pub struct Vec3 {
x: f64,
y: f64,
z: f64,
pub x: f64,
pub y: f64,
pub z: f64,
}

impl Vec3 {
Expand Down
58 changes: 58 additions & 0 deletions src/render/axis.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use std::{fmt::{Display, Formatter, self}, ops::Index};

use crate::linalg::Vec3;


/// An `Axis` in a three-dimensional coordinate system.
/// Used to access `Vec3` structs via index.
///
/// # Examples
/// ```
/// use raytracer::render::Axis;
/// use raytracer::linalg::Vec3;
///
/// let mut vec = Vec3::new(1.0, 0.5, 42.0);
///
/// assert_eq!(vec[Axis::Y], 0.5);
/// ```
///
/// [`Vec3`] is also indexable using `Axis`.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Axis {
/// Index of the X axis.
X = 0,

/// Index of the Y axis.
Y = 1,

/// Index of the Z axis.
Z = 2,
}

/// Display implementation for `Axis`.
impl Display for Axis {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(
f,
"{}",
match *self {
Axis::X => "x",
Axis::Y => "y",
Axis::Z => "z",
}
)
}
}

/// Make `Vector3` indexable by `Axis`.
impl Index<Axis> for Vec3 {
type Output = f64;

fn index(&self, axis: Axis) -> &f64 {
match axis {
Axis::X => &self.x,
Axis::Y => &self.y,
Axis::Z => &self.z,
}
}
}
3 changes: 1 addition & 2 deletions src/render/integrator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ use rand::{rngs::SmallRng, SeedableRng};
use rayon::prelude::*;

use crate::{
bvh::BVHNode,
bvh::BVH_SAH,
elements::{Element, Hittable, JSONElement},
elements::{Element, JSONElement},
materials::{Emmits, Reflects, Refracts, Scatterable},
render::camera::Camera,
render::pdf::{HittablePDF, MixedPDF, PDFTrait, Pdf},
Expand Down
3 changes: 3 additions & 0 deletions src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ pub use interval::Interval;
mod color;
pub use color::Color;
pub use color::Rgb;

mod axis;
pub use axis::Axis;
16 changes: 13 additions & 3 deletions src/render/ray.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
use crate::linalg::Vec3;

#[allow(unused_imports)] // for doc
use crate::bvh::Aabb;

use std::fmt;

#[derive(Debug, Clone, Copy)]
pub struct Ray {
/// Origin of the ray.
pub origin: Vec3,

/// Direction of the ray.
pub direction: Vec3,

/// Inverse direction of the ray.
/// Cached for use in [`Aabb`] intersections.
pub inv_direction: Vec3,

/// Sign of the X direction. 0 means positive, 1 means negative.
/// Cached for use in [`AABB`] intersections.
/// Cached for use in [`Aabb`] intersections.
pub sign_x: usize,

/// Sign of the Y direction. 0 means positive, 1 means negative.
/// Cached for use in [`AABB`] intersections.
/// Cached for use in [`Aabb`] intersections.
pub sign_y: usize,

/// Sign of the Z direction. 0 means positive, 1 means negative.
/// Cached for use in [`AABB`] intersections.
/// Cached for use in [`Aabb`] intersections.
pub sign_z: usize,
}

Expand Down

0 comments on commit 11a2ce0

Please sign in to comment.