Skip to content

Commit

Permalink
Fixed every bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Red-Rapious committed Sep 10, 2023
1 parent 721d49c commit 1d808f8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 21 deletions.
Binary file modified app/generated_images/22_refactor.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 4 additions & 3 deletions app/src/render.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use lib_ray_tracer::{
bvh::BVHNode,
camera::{self, Camera},
geometry::Sphere,
material::Material,
world::World,
Renderer,
bvh::BVHNode
};
use nalgebra::{Point3, Vector3};
use rand::{thread_rng, Rng};
Expand Down Expand Up @@ -85,8 +85,9 @@ pub fn render() {
world.add(Sphere::stationary(Point3::new(4.0, 1.0, 0.0), 1.0, material3));

let mut world2 = World::empty();
let objects = world.objects();
world2.add(BVHNode::new(objects, 0, objects.len()));
let l = world.objects().len();
let mut objects = world.objects().drain(0..l).map(Some).collect();
world2.add(BVHNode::new(&mut objects, 0, l));

let renderer = Renderer::new(aspect_ratio, image_width, camera);
let img = renderer.render_parallel_image(&world2);
Expand Down
52 changes: 34 additions & 18 deletions src/bvh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,45 @@ pub struct BVHNode {
}

impl BVHNode {
pub fn new(objects: &mut Vec<Box<dyn Hittable + Sync>>, start: usize, end: usize) -> Self {
pub fn new(
objects: &mut Vec<Option<Box<dyn Hittable + Sync>>>,
start: usize,
end: usize,
) -> Self {
let axis = thread_rng().gen_range(0..3);

let object_span = end - start;

let left;
let right;
let left: Box<dyn Hittable + Sync>;
let right: Option<Box<dyn Hittable + Sync>>;

if object_span == 1 {
left = objects.remove(start);
left = std::mem::replace(&mut objects[start], None).unwrap();
right = None;
} else if object_span == 2 {
// If there is exactly two elements, put each node as a leaf.
match BVHNode::box_compare(&objects[start], &objects[start + 1], axis) {
match BVHNode::box_compare(
&objects[start],
&objects[start + 1],
axis,
) {
Ordering::Less | Ordering::Equal => {
left = objects.remove(start);
right = Some(objects.remove(start)); // gives initial `objects[start + 1]`
left = std::mem::replace(&mut objects[start], None).unwrap();
right = std::mem::replace(&mut objects[start + 1], None); // gives initial `objects[start + 1]`
}
Ordering::Greater => {
left = objects.remove(start + 1);
right = Some(objects.remove(start)); // gives initial `objects[start + 1]`
left = std::mem::replace(&mut objects[start + 1], None).unwrap();
right = std::mem::replace(&mut objects[start], None); // gives initial `objects[start + 1]`
}
}
} else {
objects.sort_by(|a, b| BVHNode::box_compare(a, b, axis));
objects.sort_by(|a, b| {
BVHNode::box_compare(&a, &b, axis)
});

let mid = start + object_span / 2;
left = Box::new(BVHNode::new(objects, start, mid));
right = Some(Box::new(BVHNode::new(objects, start, mid)));
right = Some(Box::new(BVHNode::new(objects, mid, end)));
}

let bbox = match &right {
Expand All @@ -54,15 +64,21 @@ impl BVHNode {
}

fn box_compare(
a: &Box<dyn Hittable + Sync>,
b: &Box<dyn Hittable + Sync>,
a: &Option<Box<dyn Hittable + Sync>>,
b: &Option<Box<dyn Hittable + Sync>>,
axis: usize,
) -> Ordering {
a.bounding_box()
.axis(axis)
.min
.partial_cmp(&b.bounding_box().axis(axis).min)
.unwrap()
match (a, b) {
(None, None) => Ordering::Equal,
(Some(_), None) => Ordering::Greater,
(None, Some(_)) => Ordering::Less,
(Some(a), Some(b)) =>
a.bounding_box()
.axis(axis)
.min
.partial_cmp(&b.bounding_box().axis(axis).min)
.unwrap()
}
}
}

Expand Down

0 comments on commit 1d808f8

Please sign in to comment.