Skip to content

Commit

Permalink
Remove some unnecessary Box and Arc usage
Browse files Browse the repository at this point in the history
  • Loading branch information
banga committed Jan 10, 2024
1 parent c2f5bc6 commit 6b92724
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 39 deletions.
8 changes: 2 additions & 6 deletions src/bin/craytracer.rs
Expand Up @@ -124,11 +124,9 @@ fn render(scene: &Scene, preview_window: &mut Window) -> (Vec<f32>, Vec<u32>) {
let height = scene.film_height;
let tile_width = 64;
let tile_height = 64;
let tiles = generate_tiles(height, width, tile_width, tile_height);
let tiles = &generate_tiles(height, width, tile_width, tile_height);

let tiles = Arc::new(tiles);
let pixels = Arc::new(Mutex::new(vec![0f32; width * height * 3]));
let scene = Arc::new(scene);

let tile_index = Arc::new(AtomicUsize::new(0));
let (sender, receiver) = mpsc::channel();
Expand All @@ -138,9 +136,7 @@ fn render(scene: &Scene, preview_window: &mut Window) -> (Vec<f32>, Vec<u32>) {
thread::scope(|scope| {
for _ in 0..num_threads {
let tile_index = Arc::clone(&tile_index);
let tiles = Arc::clone(&tiles);
let pixels = Arc::clone(&pixels);
let scene = Arc::clone(&scene);
let sender = sender.clone();

scope.spawn(move |_| loop {
Expand Down Expand Up @@ -188,7 +184,7 @@ fn render(scene: &Scene, preview_window: &mut Window) -> (Vec<f32>, Vec<u32>) {
height,
tile_width,
tile_height,
&tiles,
tiles,
Arc::clone(&pixels),
receiver,
);
Expand Down
24 changes: 12 additions & 12 deletions src/bvh.rs
Expand Up @@ -41,7 +41,7 @@ impl Display for PrimitiveInfo {
}

impl BvhNode {
pub fn new(primitives: Vec<Arc<Primitive>>) -> Box<BvhNode> {
pub fn new(primitives: Vec<Arc<Primitive>>) -> BvhNode {
let primitive_infos = primitives
.iter()
.map(|p| PrimitiveInfo {
Expand All @@ -53,15 +53,15 @@ impl BvhNode {
BvhNode::from_primitive_infos(primitive_infos)
}

fn from_primitive_infos(primitive_infos: Vec<PrimitiveInfo>) -> Box<BvhNode> {
fn from_primitive_infos(primitive_infos: Vec<PrimitiveInfo>) -> BvhNode {
assert!(primitive_infos.len() > 0);
let bounds: Bounds = primitive_infos.iter().map(|p| p.bounds).sum();

if primitive_infos.len() <= 4 {
return Box::new(BvhNode::LeafNode {
return BvhNode::LeafNode {
bounds,
primitive_infos,
});
};
}

let split = BvhNode::find_split(&primitive_infos);
Expand All @@ -70,22 +70,22 @@ impl BvhNode {
.partition(|primitive| primitive.bounds.max[split.axis] <= split.location);

if left.len() == 0 {
Box::new(BvhNode::LeafNode {
BvhNode::LeafNode {
bounds,
primitive_infos: right,
})
}
} else if right.len() == 0 {
Box::new(BvhNode::LeafNode {
BvhNode::LeafNode {
bounds,
primitive_infos: left,
})
}
} else {
Box::new(BvhNode::InteriorNode {
BvhNode::InteriorNode {
bounds,
left: BvhNode::from_primitive_infos(left),
right: BvhNode::from_primitive_infos(right),
left: Box::new(BvhNode::from_primitive_infos(left)),
right: Box::new(BvhNode::from_primitive_infos(right)),
split,
})
}
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/scene.rs
Expand Up @@ -11,9 +11,9 @@ pub struct Scene {
pub num_samples: usize,
pub film_width: usize,
pub film_height: usize,
pub camera: Box<Camera>,
pub lights: Vec<Box<Light>>,
bvh: Box<BvhNode>,
pub camera: Camera,
pub lights: Vec<Light>,
bvh: BvhNode,
}

impl Scene {
Expand All @@ -22,8 +22,8 @@ impl Scene {
num_samples: usize,
film_width: usize,
film_height: usize,
camera: Box<Camera>,
lights: Vec<Box<Light>>,
camera: Camera,
lights: Vec<Light>,
primitives: Vec<Arc<Primitive>>,
) -> Self {
Self {
Expand Down
22 changes: 11 additions & 11 deletions src/scene_parser.rs
Expand Up @@ -729,7 +729,7 @@ pub mod scene_parser {
const DEFAULT_NUM_SAMPLES: usize = 4;

/// RawValue -> Camera
impl TryFrom<&RawValue> for Box<Camera> {
impl TryFrom<&RawValue> for Camera {
type Error = ParserError;
fn try_from(value: &RawValue) -> Result<Self, Self::Error> {
let typed_map = match value {
Expand All @@ -749,14 +749,14 @@ pub mod scene_parser {
let film_width: usize = map.get("film_width")?;
let film_height: usize = map.get("film_height")?;

Ok(Box::new(Camera::new_projection_camera(
Ok(Camera::new_projection_camera(
origin,
target,
up,
focal_distance,
film_width,
film_height,
)))
))
}
_ => Err(ParserError::without_location(&format!(
"Unknown camera type: {}",
Expand All @@ -767,7 +767,7 @@ pub mod scene_parser {
}

/// RawValue -> Light
impl TryFrom<&RawValue> for Box<Light> {
impl TryFrom<&RawValue> for Light {
type Error = ParserError;
fn try_from(value: &RawValue) -> Result<Self, Self::Error> {
let typed_map = match value {
Expand All @@ -783,21 +783,21 @@ pub mod scene_parser {
let origin: Point = map.get("origin")?;
let intensity: Color = map.get("intensity")?;

Ok(Box::new(Light::Point { origin, intensity }))
Ok(Light::Point { origin, intensity })
}
"Distant" => {
let direction: Vector = map.get("direction")?;
let intensity: Color = map.get("intensity")?;

Ok(Box::new(Light::Distant {
Ok(Light::Distant {
direction: direction.normalized(),
intensity,
}))
})
}
"Infinite" => {
let intensity: Color = map.get("intensity")?;

Ok(Box::new(Light::Infinite { intensity }))
Ok(Light::Infinite { intensity })
}
_ => Err(ParserError::without_location(&format!(
"Unknown light type: {}",
Expand Down Expand Up @@ -932,9 +932,9 @@ pub mod scene_parser {

let max_depth: usize = scene_map.get_or("max_depth", DEFAULT_MAX_DEPTH)?;
let num_samples: usize = scene_map.get_or("num_samples", DEFAULT_NUM_SAMPLES)?;
let camera: Box<Camera> = scene_map.get("camera")?;
let camera: Camera = scene_map.get("camera")?;

let lights: Vec<Box<Light>> = scene_map.get("lights")?;
let lights: Vec<Light> = scene_map.get("lights")?;

let materials: HashMap<String, Arc<Material>> = scene_map.get("materials")?;
let shapes: HashMap<String, Arc<Shape>> = scene_map.get("shapes")?;
Expand All @@ -946,7 +946,7 @@ pub mod scene_parser {
}

// TODO: move film dimensions to Scene
let (film_width, film_height) = match *camera {
let (film_width, film_height) = match camera {
Camera::Projection {
film_width,
film_height,
Expand Down
2 changes: 1 addition & 1 deletion src/trace.rs
Expand Up @@ -19,7 +19,7 @@ use crate::{
fn estimate_direct(
intersection: &PrimitiveIntersection,
w_o: &Vector,
light: &Box<Light>,
light: &Light,
scene: &Scene,
) -> Color {
let mut Ld = Color::BLACK;
Expand Down
8 changes: 4 additions & 4 deletions tests/test_parser.rs
Expand Up @@ -638,18 +638,18 @@ mod parser {
1,
400,
300,
Box::new(Camera::new_projection_camera(
Camera::new_projection_camera(
Point::O,
Point::new(0, 0, 1),
Vector::Y,
1.0,
400,
300
)),
vec![Box::new(Light::Point {
),
vec![Light::Point {
origin: Point::O,
intensity: Color::WHITE
}),],
},],
vec![
Arc::new(Primitive::new_shape_primitive(
Arc::new(Shape::new_sphere(Point(0.0, 0.0, 2.0), 1.0)),
Expand Down

0 comments on commit 6b92724

Please sign in to comment.