From 0d3500ceb5e8cee141f73ff6b618931e2ba1cdcc Mon Sep 17 00:00:00 2001 From: bijan2005 Date: Sat, 20 Mar 2021 17:48:03 -0400 Subject: [PATCH] Refactor some method code --- .gitignore | 2 +- src/main.rs | 9 +++--- src/object.rs | 10 +++---- src/object/plane.rs | 4 +-- src/object/{pointlight.rs => point_light.rs} | 12 +++----- src/object/sphere.rs | 9 +++--- src/object/triangle.rs | 29 ++++++++------------ src/render.rs | 6 ++-- src/types.rs | 7 ++--- 9 files changed, 38 insertions(+), 50 deletions(-) rename src/object/{pointlight.rs => point_light.rs} (78%) diff --git a/.gitignore b/.gitignore index 17326b1..b9a4f97 100644 --- a/.gitignore +++ b/.gitignore @@ -11,4 +11,4 @@ Cargo.lock **/*.rs.bk # Debug output of program -*.ppm \ No newline at end of file +*.ppm diff --git a/src/main.rs b/src/main.rs index b8a341f..70fb5c0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,15 +36,16 @@ fn render(camera: &Camera, scene: &Scene, filename: &str) -> std::io::Result<()> fn main() -> std::io::Result<()> { - let camera = Camera::new(Point3::new(0.0,0.0,2.5), Vector3::new(0.0,0.0,-1.0), 1.0, 16.0 / 9.0, 2.0, 720); + let camera = Camera::new(Point3::new(0.0,0.0,3.0), Vector3::new(0.0,0.0,-1.0), 1.0, 16.0 / 9.0, 2.0, 720); let scene = Scene { objects: vec![ - Object::new(Sphere::new(0.0, 0.0, 0.0, 1.0, |a, b| Texture::new(0.0, a, b, 1.0))) + Object::new(Sphere::new_solid(1.0, 0.2, 1.7, 0.2, Texture::new(1.0, 1.0, 1.0, 1.0))), + Object::new(Sphere::new_solid(0.0, -1.0, 0.0, 1.0, Texture::new(1.0, 1.0, 1.0, 1.0))) ], lights: vec![ - Box::new(PointLight::new(Point3::new(1.0, 0.7, 1.5), Color::white(), 3.0)), - Box::new(PointLight::new(Point3::new(-1.0, -0.3, 0.4), Color::new(1.0, 0.0, 0.0), 4.0)) + Box::new(PointLight::new(Point3::new(1.5, 1.0, 2.5), Color::white(), 3.0)), + Box::new(PointLight::new(Point3::new(1.0, -0.4, 1.5), Color::white(), 2.0)) ], background: Color::gray(0.5) }; diff --git a/src/object.rs b/src/object.rs index 114ee0a..d7d4d8b 100644 --- a/src/object.rs +++ b/src/object.rs @@ -3,7 +3,7 @@ mod sphere; pub use sphere::*; mod plane; pub use plane::*; mod triangle; pub use triangle::*; mod bound; pub use bound::*; -mod pointlight; pub use pointlight::*; +mod point_light; pub use point_light::*; use crate::types::*; @@ -21,7 +21,7 @@ pub trait Surface { // Takes in a point (assumed to be on the object's surface) // and returns the texture information on that point. - fn gettexture(&self, point: Point3f) -> Texture; + fn get_texture(&self, point: Point3f) -> Texture; // Creates a bounding sphere around the object. fn bound(&self) -> Bound; @@ -38,7 +38,7 @@ impl Object { let bound = surface.bound(); Object { surface: Box::new(surface), - bound: bound + bound } } @@ -49,7 +49,7 @@ impl Object { } else { None } } pub fn normal(&self, point: Point3f) -> Unit3f { self.surface.normal(point) } - pub fn gettexture(&self, point: Point3f) -> Texture { self.surface.gettexture(point) } + pub fn get_texture(&self, point: Point3f) -> Texture { self.surface.get_texture(point) } } pub trait Light { @@ -57,7 +57,7 @@ pub trait Light { fn check_shadow(&self, point: Point3f, objects: &Vec) -> bool; // Compute color on a point. - fn getcolor(&self, point: Point3f) -> Color; + fn get_color(&self, point: Point3f) -> Color; // Compute intensity on a point. fn intensity(&self, point: Point3f) -> f32; diff --git a/src/object/plane.rs b/src/object/plane.rs index 881e5d6..981fb5f 100644 --- a/src/object/plane.rs +++ b/src/object/plane.rs @@ -24,7 +24,7 @@ impl Plane { where F: Fn(f32, f32) -> Texture { Plane { - center: center, + center, normal: Unit::new_normalize(x_axis.cross(&y_axis)), x_axis: x_axis, y_axis: y_axis, @@ -77,7 +77,7 @@ impl Surface for Plane { fn normal(&self, _point: Point3f) -> Unit3f { self.normal } - fn gettexture(&self, point: Point3f) -> Texture { + fn get_texture(&self, point: Point3f) -> Texture { let rel_pos = point - self.center; let proj_point3 = rel_pos - (*self.normal * self.normal.dot(&rel_pos)); diff --git a/src/object/pointlight.rs b/src/object/point_light.rs similarity index 78% rename from src/object/pointlight.rs rename to src/object/point_light.rs index 2c964dd..96c0667 100644 --- a/src/object/pointlight.rs +++ b/src/object/point_light.rs @@ -14,11 +14,7 @@ pub struct PointLight { #[allow(dead_code)] impl PointLight { pub fn new(pos: Point3f, color: Color, intensity: f32) -> PointLight { - PointLight { - pos: pos, - color: color, - intensity: intensity - } + PointLight { pos, color, intensity } } } @@ -26,11 +22,11 @@ impl Light for PointLight { fn check_shadow(&self, point: Point3f, objects: &Vec) -> bool { let max_d = distance(&self.pos, &point); objects.iter() - .filter_map(|obj| obj.intersect(Ray::from_points(self.pos, point))) + .filter_map(|&obj| obj.intersect(Ray::from_points(self.pos, point))) .all(|d| d - max_d > -1e-3 ) } - fn getcolor(&self, _point: Point3f) -> Color { self.color } + fn get_color(&self, _point: Point3f) -> Color { self.color } fn intensity(&self, _point: Point3f) -> f32 { self.intensity } @@ -44,7 +40,7 @@ mod tests { use super::*; #[test] - fn pointlight_checkshadow() { + fn point_light_check_shadow() { let light = PointLight::new(Point3::new(0.0, 1.0, 0.0), Color::white(), 1.0); let block = Object::new(Sphere::new_solid(0.0, 0.5, 0.0, 0.1, Texture::new(0.0, 0.0, 0.0, 0.0))); diff --git a/src/object/sphere.rs b/src/object/sphere.rs index bc774ee..232637f 100644 --- a/src/object/sphere.rs +++ b/src/object/sphere.rs @@ -23,8 +23,7 @@ impl Sphere { where F: Fn(f32, f32) -> Texture { Sphere { - center: Point3::new(x, y, z), - radius: radius, + center: Point3::new(x, y, z), radius, texture: Box::new(texture) } } @@ -66,10 +65,10 @@ impl Surface for Sphere { Unit::new_normalize(point - self.center) } - fn gettexture(&self, point: Point3f) -> Texture { + fn get_texture(&self, point: Point3f) -> Texture { let normal = self.normal(point); - // In this particular case, the normal is simular to a point on a unit sphere + // In this particular case, the normal is similar to a point on a unit sphere // centred around the origin. We can thus use the normal coordinates to compute // the spherical coordinates of the point. let x = 0.5 + normal.z.atan2(normal.x) / (2.0 * PI); @@ -78,5 +77,5 @@ impl Surface for Sphere { (*self.texture)(x, y) } - fn bound(&self) -> Bound { Bound::bypass() } + fn bound(&self) -> Bound { Bound { center: self.center, radius: self.radius, bypass: false } } } diff --git a/src/object/triangle.rs b/src/object/triangle.rs index 1b2a361..c66f755 100644 --- a/src/object/triangle.rs +++ b/src/object/triangle.rs @@ -22,7 +22,7 @@ pub struct Triangle { pub struct TriangleMesh { pub vertices: Vec, - pub tris: Vec + pub triangles: Vec } fn tri_area(a: &Point3f, b: &Point3f, c: &Point3f) -> f32 { @@ -78,7 +78,7 @@ impl Triangle { self.intersect_(vertices, ray).map(|(t, u, v)| distance(&ray.origin, &self.from_bary(vertices, t, u, v))) } - fn gettexture(&self, vertices: &Vec, point: Point3f) -> Texture { + fn get_texture(&self, vertices: &Vec, point: Point3f) -> Texture { let (t, u, v) = self.to_bary(vertices, point); (*self.texture)(t, u, v) } @@ -89,32 +89,27 @@ impl TriangleMesh { pub fn new(vertices: Vec, tris: Vec<(usize, usize, usize, Box Texture>)>) -> Self { let triangles = tris.into_iter() .map(|(v1, v2, v3, f)| Triangle { - v1: v1, - v2: v2, - v3: v3, + v1, v2, v3, normal: Unit::new_normalize((&vertices[v2] - &vertices[v1]).cross(&(&vertices[v3] - &vertices[v1]))), area: tri_area(&vertices[v1], &vertices[v2], &vertices[v3]), texture: f }).collect(); TriangleMesh { - vertices: vertices, - tris: triangles + vertices, triangles } } pub fn new_solid(vertices: Vec, tris: Vec<(usize, usize, usize)>, texture: Texture) -> Self { let triangles = tris.into_iter() .map(|(v1, v2, v3)| Triangle { - v1: v1, - v2: v2, - v3: v3, + v1, v2, v3, normal: Unit::new_normalize((&vertices[v2] - &vertices[v1]).cross(&(&vertices[v3] - &vertices[v1]))), area: tri_area(&vertices[v1], &vertices[v2], &vertices[v3]), texture: Box::new(move |_, _, _| texture) }).collect(); TriangleMesh { - vertices: vertices, - tris: triangles + vertices, + triangles } } @@ -127,7 +122,7 @@ impl TriangleMesh { fn closest_tri(&self, point: Point3f) -> &Triangle { - self.tris.iter() + self.triangles.iter() .map(move |tri| { let rel_pos = point - tri.vertex1(&self.vertices); @@ -150,7 +145,7 @@ impl TriangleMesh { impl Surface for TriangleMesh { fn intersect(&self, ray: Ray) -> Option { - self.tris.iter() + self.triangles.iter() .filter_map(|tri| tri.intersect(&self.vertices, ray)) .min_by(|a, b| a.partial_cmp(&b).unwrap_or(Ordering::Equal)) } @@ -159,8 +154,8 @@ impl Surface for TriangleMesh { self.closest_tri(point).normal } - fn gettexture(&self, point: Point3f) -> Texture { - self.closest_tri(point).gettexture(&self.vertices, point) + fn get_texture(&self, point: Point3f) -> Texture { + self.closest_tri(point).get_texture(&self.vertices, point) } // Uses Welzl's algorithm to solve the bounding sphere problem @@ -238,6 +233,6 @@ impl Surface for TriangleMesh { let (center, radius) = smallest_sphere(points, Vec::new()); - Bound { center: center, radius: radius + 1e-3, bypass: false } + Bound { center, radius: radius + 1e-3, bypass: false } } } diff --git a/src/render.rs b/src/render.rs index a947715..729e420 100644 --- a/src/render.rs +++ b/src/render.rs @@ -11,9 +11,9 @@ use crate::object::*; fn trace(ray: Ray, objects: &Vec) -> Option<(&Object, f32)> { objects.iter() - .filter_map(|obj| obj.intersect(ray) + .filter_map(|&obj| obj.intersect(ray) .map(|x| (obj, x))) - .min_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(Ordering::Equal)) + .min_by(|&a, &b| a.1.partial_cmp(&b.1).unwrap_or(Ordering::Equal)) } fn light_point(objects: &Vec, obj: &Object, point: Point3f, light: &dyn Light) -> Color { @@ -34,7 +34,7 @@ pub fn cast_ray(ray: Ray, scene: &Scene) -> Color { let surface_color = obj.gettexture(point).color; scene.lights.iter() - .map(|light| light_point(&scene.objects, obj, point, &**light)) + .map(|&light| light_point(&scene.objects, obj, point, &*light)) .fold(Color::black(), |acc, c| acc + c) * surface_color } else { scene.background } diff --git a/src/types.rs b/src/types.rs index 2ab6885..6328873 100644 --- a/src/types.rs +++ b/src/types.rs @@ -17,10 +17,7 @@ pub struct Ray { impl Ray { pub fn from_parts(origin: Point3f, direction: Unit3f) -> Self { - Ray { - origin: origin, - direction: direction - } + Ray { origin, direction } } pub fn new(origin: Point3f, direction: Vector3f) -> Self { Ray::from_parts(origin, Unit::new_normalize(direction)) } pub fn from_points(origin: Point3f, points_to: Point3f) -> Self { Ray::new(origin, points_to - origin) } @@ -109,7 +106,7 @@ impl Texture { pub fn new(red: f32, green: f32, blue: f32, albedo: f32) -> Self { Texture { color: Color::new(red, green, blue), - albedo: albedo + albedo } } }