Skip to content

Commit

Permalink
starting to add BVH SAH
Browse files Browse the repository at this point in the history
  • Loading branch information
scott223 committed Nov 16, 2023
1 parent 8b7b5f2 commit b0a8fb6
Show file tree
Hide file tree
Showing 10 changed files with 431 additions and 83 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "raytracer"
version = "0.2.0"
edition = "2021"
authors = ["Scott Brugmans <[email protected]>"]
respository = "https://github.com/scott223/raytracer/"
repository = "https://github.com/scott223/raytracer/"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
4 changes: 2 additions & 2 deletions input/config.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"img_width": 600.0,
"img_height": 600.0,
"samples": 8,
"max_depth": 5,
"samples": 12,
"max_depth": 8,
"sky_color": {
"r": 0.3,
"g": 0.5,
Expand Down
46 changes: 9 additions & 37 deletions input/scene.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,8 @@
"camera_defocus_angle": 0.0,
"camera_focus_dist": 800.0
},
"elements": [
{
"JSONObj": {
"filepath": "input/obj/teapot.obj",
"material": {
"Lambertian": {
"albedo": {
"r": 0.43,
"g": 0.53,
"b": 0.43
}
}
},
"transpose" : {
"x": 220.0,
"y": 0.0,
"z": 200.0
},
"rotate" : {
"theta_x": 0.0,
"theta_y": -3.2,
"theta_z": 0.0
},
"scale" : {
"x" : 20.0,
"y" : 20.0,
"z" : 20.0
}
}
},
"elements":
[
{
"JSONBox": {
"a": {
Expand Down Expand Up @@ -419,26 +391,26 @@
{
"JSONQuad": {
"q": {
"x": 343.0,
"x": 383.0,
"y": 554.0,
"z": 332.0
"z": 305.0
},
"u": {
"x": -130.0,
"x": -210.0,
"y": 0.0,
"z": 0.0
},
"v": {
"x": 0.0,
"y": 0.0,
"z": -105.0
"z": -155.0
},
"material": {
"DiffuseLight": {
"albedo": {
"r": 8.0,
"g": 8.0,
"b": 8.0
"r": 5.0,
"g": 5.0,
"b": 5.0
}
}
},
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.
107 changes: 96 additions & 11 deletions src/bvh/aabb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ pub struct Aabb {
x: Interval,
y: Interval,
z: Interval,
min: Vec3,
max: Vec3,
pub min: Vec3,
pub max: Vec3,
pub centroid: Vec3,
}

impl Default for Aabb {
Expand All @@ -34,47 +35,61 @@ impl Default for Aabb {
},
min: Vec3::new(0.0, 0.0, 0.0),
max: Vec3::new(0.0, 0.0, 0.0),
centroid: Vec3::new(0.0, 0.0, 0.0),
}
}
}

impl Aabb {
pub fn new_from_intervals(x: Interval, y: Interval, z: Interval) -> Self {
Aabb {

let mut bbox = Aabb {
x,
y,
z,
min: Vec3::new(x.interval_min, y.interval_min, z.interval_min),
max: Vec3::new(x.interval_max, y.interval_max, z.interval_max),
}
centroid: Vec3::new(0.0, 0.0, 0.0),
};

bbox.centroid = calculate_centroid(bbox);
bbox
}

pub fn new_from_points(p: Vec3, q: Vec3) -> Self {
let x: Interval = Interval::new(q.x().min(p.x()), q.x().max(p.x()));
let y: Interval = Interval::new(q.y().min(p.y()), q.y().max(p.y()));
let z: Interval = Interval::new(q.z().min(p.z()), q.z().max(p.z()));

Aabb {
let mut bbox = Aabb {
x,
y,
z,
min: Vec3::new(x.interval_min, y.interval_min, z.interval_min),
max: Vec3::new(x.interval_max, y.interval_max, z.interval_max),
}
centroid: Vec3::new(0.0, 0.0, 0.0),
};

bbox.centroid = calculate_centroid(bbox);
bbox
}

pub fn new_from_aabbs(a: Aabb, b: Aabb) -> Self {
let x: Interval = Interval::new_from_intervals(a.x, b.x);
let y: Interval = Interval::new_from_intervals(a.y, b.y);
let z: Interval = Interval::new_from_intervals(a.z, b.z);

Aabb {
let mut bbox = Aabb {
x,
y,
z,
min: Vec3::new(x.interval_min, y.interval_min, z.interval_min),
max: Vec3::new(x.interval_max, y.interval_max, z.interval_max),
}
centroid: Vec3::new(0.0, 0.0, 0.0),
};

bbox.centroid = calculate_centroid(bbox);
bbox
}

// return an AABB that has no side narrower than some delta, padding if necessary
Expand All @@ -97,15 +112,20 @@ impl Aabb {
self.z.expand(delta)
};

Aabb {
let mut bbox = Aabb {
x: new_x,
y: new_y,
z: new_z,
min: Vec3::new(new_x.interval_min, new_y.interval_min, new_z.interval_min),
max: Vec3::new(new_x.interval_max, new_y.interval_max, new_z.interval_max),
}
centroid: Vec3::new(0.0, 0.0, 0.0),
};

bbox.centroid = calculate_centroid(bbox);
bbox
}

#[inline(always)]
pub fn axis(&self, n: usize) -> Interval {
match n {
0 => self.x,
Expand All @@ -115,6 +135,25 @@ impl Aabb {
}
}

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

if size.x() > size.y() && size.x() > size.z() {
0
} else if size.y() > size.z() {
1
} else {
2
}
}

/// 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

Expand All @@ -136,13 +175,23 @@ impl Aabb {

ray_min.max(0.0) <= ray_max
}

#[inline(always)]
pub fn grow<T: Hittable>(mut self, b: T) {
self.x = Interval::new_from_intervals(self.x, b.bounding_box().x);
self.y = Interval::new_from_intervals(self.y, b.bounding_box().y);
self.z = Interval::new_from_intervals(self.z, b.bounding_box().z);
}
}

pub fn calculate_centroid(bbox: Aabb) -> Vec3 {
Vec3::new(
bbox.min.x() + (bbox.max.x() - bbox.min.x())/2.,
bbox.min.y() + (bbox.max.y() - bbox.min.y())/2.,
bbox.min.z() + (bbox.max.z() - bbox.min.z())/2.,
)
}

/// Display trait for Aabb
impl fmt::Display for Aabb {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Expand Down Expand Up @@ -260,4 +309,40 @@ mod tests {

assert_eq!(aabb_p.hit(&ray_two, &mut int), false);
}
}

#[test_log::test]
fn test_centroid() {
let p: Vec3 = Vec3::new(1.0, 1.0, 1.0);
let q: Vec3 = Vec3::new(2.0, 2.0, 2.0);

let aabb_p: Aabb = Aabb::new_from_points(p, q);

let centroid = aabb_p.centroid();

assert_eq!(centroid.x(), 1.5);
assert_eq!(centroid.y(), 1.5);
assert_eq!(centroid.z(), 1.5);
}


#[test_log::test]
fn test_longest_axis() {
let p: Vec3 = Vec3::new(1.0, 1.0, 1.0);
let q: Vec3 = Vec3::new(2.0, 3.0, 2.0);

let aabb_p: Aabb = Aabb::new_from_points(p, q);

let largest_axis = aabb_p.largest_axis();

assert_eq!(largest_axis, 1);

let p: Vec3 = Vec3::new(1.0, 1.0, 1.0);
let q: Vec3 = Vec3::new(2.0, 3.0, 5.0);

let aabb_p: Aabb = Aabb::new_from_points(p, q);

let largest_axis = aabb_p.largest_axis();

assert_eq!(largest_axis, 2);
}
}
Loading

0 comments on commit b0a8fb6

Please sign in to comment.