Skip to content

Commit

Permalink
fix(shape): minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Paolo-Azzini authored and andros21 committed May 4, 2022
1 parent 1c60cc3 commit 4231115
Showing 1 changed file with 40 additions and 36 deletions.
76 changes: 40 additions & 36 deletions src/shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ use crate::transformation::Transformation;
use crate::vector::Vector;
use std::f32::consts::PI;

trait RayIntersection {
pub trait RayIntersection {
fn ray_intersection(&self, ray: Ray) -> Option<HitRecord>;
}

#[derive(Clone, Copy, Debug, Default, PartialEq)]
struct HitRecord {
world_point: Point,
normal: Normal,
surface_point: Vector2D,
t: f32,
ray: Ray,
pub struct HitRecord {
pub world_point: Point,
pub normal: Normal,
pub surface_point: Vector2D,
pub t: f32,
pub ray: Ray,
}

impl IsClose for HitRecord {
Expand All @@ -34,12 +34,18 @@ pub struct Sphere {
transformation: Transformation,
}

impl Sphere {
pub fn new(transformation: Transformation) -> Self {
Sphere { transformation }
}
}

fn sphere_normal(point: Point, ray_dir: Vector) -> Normal {
let result = Normal::from((point.x, point.y, point.z));
if Vector::from(point).dot(ray_dir) < 0.0 {
result
} else {
result * -1.
result.neg()
}
}

Expand Down Expand Up @@ -91,6 +97,28 @@ pub struct Plane {
transformation: Transformation,
}

impl Plane {
pub fn new(transformation: Transformation) -> Self {
Plane { transformation }
}
}

fn plane_normal(ray_dir: Vector) -> Normal {
let normal = Normal::from((0., 0., 1.));
if Vector::from(normal).dot(ray_dir) < 0.0 {
normal
} else {
normal.neg()
}
}

fn plane_point_to_uv(point: Point) -> Vector2D {
Vector2D {
u: point.x - point.x.floor(),
v: point.y - point.y.floor(),
}
}

impl RayIntersection for Plane {
fn ray_intersection(&self, ray: Ray) -> Option<HitRecord> {
let inv_ray = self.transformation.inverse() * ray;
Expand All @@ -112,22 +140,6 @@ impl RayIntersection for Plane {
}
}

fn plane_normal(ray_dir: Vector) -> Normal {
let normal = Normal::from((0., 0., 1.));
if Vector::from(normal).dot(ray_dir) < 0.0 {
normal
} else {
normal * -1.
}
}

fn plane_point_to_uv(point: Point) -> Vector2D {
Vector2D {
u: point.x - point.x.floor(),
v: point.y - point.y.floor(),
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down Expand Up @@ -195,9 +207,7 @@ mod test {

#[test]
fn test_transform_sphere() {
let sphere = Sphere {
transformation: translation(Vector::from((10., 0., 0.))),
};
let sphere = Sphere::new(translation(Vector::from((10., 0., 0.))));

let ray1 = Ray {
origin: Point::from((10., 0., 2.)),
Expand Down Expand Up @@ -246,9 +256,7 @@ mod test {

#[test]
fn test_sphere_normal() {
let sphere1 = Sphere {
transformation: scaling(Vector::from((2., 1., 1.))),
};
let sphere1 = Sphere::new(scaling(Vector::from((2., 1., 1.))));
let ray1 = Ray {
origin: Point::from((1., 1., 0.)),
dir: Vector::from((-1., -1., 0.)),
Expand All @@ -263,9 +271,7 @@ mod test {
.unwrap()
.is_close(Normal::from((1., 4., 0.)).normalize().unwrap()));

let sphere2 = Sphere {
transformation: scaling(Vector::from((-1., -1., -1.))),
};
let sphere2 = Sphere::new(scaling(Vector::from((-1., -1., -1.))));
let ray2 = Ray {
origin: Point::from((0., 2., 0.)),
dir: Vector::from((0., -1., 0.)),
Expand Down Expand Up @@ -394,9 +400,7 @@ mod test {

#[test]
fn test_transform_plane() {
let plane = Plane {
transformation: rotation_y(PI / 2.),
};
let plane = Plane::new(rotation_y(PI / 2.));
let ray1 = Ray {
origin: Point::from((1., 0., 0.)),
dir: Vector::from((-1., 0., 0.)),
Expand Down

0 comments on commit 4231115

Please sign in to comment.