Skip to content

Commit

Permalink
Make shapes Copy and Clone when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
ekzhang committed Feb 26, 2021
1 parent a704aae commit 9e5acf4
Show file tree
Hide file tree
Showing 7 changed files with 8 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/kdtree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ impl BoundingBox {
/// [fogleman/pt](https://github.com/fogleman/pt/blob/master/pt/tree.go).
/// Parts of the construction algorithm were also taken from PBRT, which helped
/// optimize the code by a few orders of magnitude.
#[derive(Clone)]
pub struct KdTree<T> {
root: Box<KdNode>,
objects: Vec<T>,
Expand Down Expand Up @@ -222,6 +223,7 @@ impl<T: Bounded> KdTree<T> {
}
}

#[derive(Clone)]
enum KdNode {
SplitX(f64, Box<KdNode>, Box<KdNode>),
SplitY(f64, Box<KdNode>, Box<KdNode>),
Expand Down
3 changes: 1 addition & 2 deletions src/shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ impl Ray {
}

/// Record of when a hit occurs, and the corresponding normal
///
/// TODO: Look into adding more information, such as (u, v) texels
pub struct HitRecord {
/// The time at which the hit occurs (see `Ray`)
pub time: f64,
Expand All @@ -99,6 +97,7 @@ impl HitRecord {
}

/// A shape that has been composed with a transformation
#[derive(Copy, Clone)]
pub struct Transformed<T> {
shape: T,
transform: glm::DMat4,
Expand Down
1 change: 1 addition & 0 deletions src/shape/cube.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use super::{HitRecord, Ray, Shape};
use crate::kdtree::{Bounded, BoundingBox};

/// A unit cube centered at the origin
#[derive(Copy, Clone)]
pub struct Cube;

impl Bounded for Cube {
Expand Down
1 change: 1 addition & 0 deletions src/shape/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use super::{HitRecord, Ray, Shape};
use crate::kdtree::{Bounded, BoundingBox, KdTree};

/// A triangle with three vertices and three normals
#[derive(Copy, Clone)]
pub struct Triangle {
/// The first vertex
pub v1: glm::DVec3,
Expand Down
1 change: 1 addition & 0 deletions src/shape/monomial_surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::kdtree::{Bounded, BoundingBox};
/// Points satisfy the relation y = height * sqrt(x^2 + z^2)^exp, x^2 + z^2 <= 1.
///
/// Normals and other things probably can't be generalized, so they work only for exp=4 for now
#[derive(Copy, Clone)]
pub struct MonomialSurface {
/// The height of the surface
pub height: f64,
Expand Down
1 change: 1 addition & 0 deletions src/shape/plane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use rand::rngs::StdRng;
use super::{HitRecord, Ray, Shape};

/// A plane represented by the linear equation x • normal = value
#[derive(Copy, Clone)]
pub struct Plane {
/// The normal vector
pub normal: glm::DVec3,
Expand Down
1 change: 1 addition & 0 deletions src/shape/sphere.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use super::{HitRecord, Ray, Shape};
use crate::kdtree::{Bounded, BoundingBox};

/// A unit sphere centered at the origin
#[derive(Copy, Clone)]
pub struct Sphere;

#[allow(clippy::many_single_char_names)]
Expand Down

0 comments on commit 9e5acf4

Please sign in to comment.