Skip to content

Commit

Permalink
refactor: reduce repetition in objects.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
Walther committed Sep 11, 2023
1 parent b524c3d commit 41c130c
Showing 1 changed file with 19 additions and 43 deletions.
62 changes: 19 additions & 43 deletions clovers/src/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,7 @@ pub fn object_to_hitable<'scene>(

match obj {
Object::Boxy(x) => {
let material: &Material = match x.material {
MaterialInit::Owned(m) => {
// TODO: do not leak memory
let material: &'scene Material = Box::leak(Box::new(m));
material
}
MaterialInit::Shared(name) => {
&materials.iter().find(|m| m.name == name).unwrap().material
}
};
let material = initialize_material(x.material, materials);
Hitable::Boxy(Boxy::new(x.corner_0, x.corner_1, material))
}
Object::ConstantMedium(x) => {
Expand All @@ -112,17 +103,7 @@ pub fn object_to_hitable<'scene>(
Hitable::FlipFace(FlipFace::new(obj))
}
Object::MovingSphere(x) => {
// TODO: do not leak memory
let material: &Material = match x.material {
MaterialInit::Owned(m) => {
// TODO: do not leak memory
let material: &'scene Material = Box::leak(Box::new(m));
material
}
MaterialInit::Shared(name) => {
&materials.iter().find(|m| m.name == name).unwrap().material
}
};
let material = initialize_material(x.material, materials);
Hitable::MovingSphere(MovingSphere::new(
// TODO: time
x.center_0, x.center_1, 0.0, 1.0, x.radius, material,
Expand Down Expand Up @@ -156,17 +137,7 @@ pub fn object_to_hitable<'scene>(
Hitable::RotateY(RotateY::new(Box::new(obj), x.angle))
}
Object::Sphere(x) => {
// TODO: do not leak memory
let material: &Material = match x.material {
MaterialInit::Owned(m) => {
// TODO: do not leak memory
let material: &'scene Material = Box::leak(Box::new(m));
material
}
MaterialInit::Shared(name) => {
&materials.iter().find(|m| m.name == name).unwrap().material
}
};
let material = initialize_material(x.material, materials);
Hitable::Sphere(Sphere::new(x.center, x.radius, material))
}
#[cfg(feature = "stl")]
Expand All @@ -182,18 +153,23 @@ pub fn object_to_hitable<'scene>(
Hitable::Translate(Translate::new(Box::new(obj), x.offset))
}
Object::Triangle(x) => {
// TODO: do not leak memory
let material: &Material = match x.material {
MaterialInit::Owned(m) => {
// TODO: do not leak memory
let material: &'scene Material = Box::leak(Box::new(m));
material
}
MaterialInit::Shared(name) => {
&materials.iter().find(|m| m.name == name).unwrap().material
}
};
let material = initialize_material(x.material, materials);
Hitable::Triangle(Triangle::new(x.q, x.u, x.v, material))
}
}
}

fn initialize_material<'scene>(
material_init: MaterialInit,
materials: &'scene [SharedMaterial],
) -> &Material {
let material: &Material = match material_init {
MaterialInit::Owned(m) => {
// TODO: do not leak memory
let material: &'scene Material = Box::leak(Box::new(m));
material
}
MaterialInit::Shared(name) => &materials.iter().find(|m| m.name == name).unwrap().material,
};
material
}

0 comments on commit 41c130c

Please sign in to comment.