Skip to content

Commit

Permalink
feat(normal/point/vector): const (E1, E2, E3) versors
Browse files Browse the repository at this point in the history
and derive default for `Point`
  • Loading branch information
andros21 authored and Paolo-Azzini committed Jun 25, 2022
1 parent 52b92a0 commit 5b867ab
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 5 deletions.
21 changes: 21 additions & 0 deletions src/normal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,27 @@ use crate::vector::Vector;
use std::fmt;
use std::ops::Mul;

/// X-axis normal.
pub const E1: Normal = Normal {
x: 1.0,
y: 0.0,
z: 0.0,
};

/// Y-axis normal.
pub const E2: Normal = Normal {
x: 0.0,
y: 1.0,
z: 0.0,
};

/// Z-axis normal.
pub const E3: Normal = Normal {
x: 0.0,
y: 0.0,
z: 1.0,
};

/// 3D Normal struct.
///
/// **Note:** a 3D normal is a 3D vector with norm equal to 1, but
Expand Down
2 changes: 1 addition & 1 deletion src/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::fmt;
use std::ops::{Add, Mul, Sub};

/// 3D Point struct.
#[derive(Clone, Copy, Debug, PartialEq)]
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Point {
/// x component.
pub x: f32,
Expand Down
2 changes: 1 addition & 1 deletion src/ray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl Default for Ray {
/// * `depth = 0`
fn default() -> Self {
Ray {
origin: Point::from((0., 0., 0.)),
origin: Point::default(),
dir: Vector::from((1.0, 0.0, 0.0)),
tmin: 1e-5,
tmax: f32::INFINITY,
Expand Down
6 changes: 3 additions & 3 deletions src/shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ mod test {
);

let ray3 = Ray {
origin: Point::from((0., 0., 0.)),
origin: Point::default(),
dir: Vector::from((1., 0., 0.)),
..Default::default()
};
Expand Down Expand Up @@ -416,7 +416,7 @@ mod test {
let intersection1 = plane.ray_intersection(ray1);
assert!(
matches!(intersection1, Some(intersection) if intersection.is_close(HitRecord {
world_point: Point::from((0., 0., 0.)),
world_point: Point::default(),
normal: Normal::from((0., 0., 1.)),
surface_point: Vector2D { u: 0., v: 0. },
t: 1.,
Expand Down Expand Up @@ -457,7 +457,7 @@ mod test {
let intersection1 = plane.ray_intersection(ray1);
assert!(
matches!(intersection1, Some(intersection) if intersection.is_close(HitRecord {
world_point: Point::from((0., 0., 0.)),
world_point: Point::default(),
normal: Normal::from((1., 0., 0.)),
surface_point: Vector2D { u: 0., v: 0. },
t: 1.,
Expand Down
21 changes: 21 additions & 0 deletions src/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,27 @@ use crate::point::Point;
use std::fmt;
use std::ops::{Add, Mul, Sub};

/// X-axis vector.
pub const E1: Vector = Vector {
x: 1.0,
y: 0.0,
z: 0.0,
};

/// Y-axis vector.
pub const E2: Vector = Vector {
x: 0.0,
y: 1.0,
z: 0.0,
};

/// Z-axis vector.
pub const E3: Vector = Vector {
x: 0.0,
y: 0.0,
z: 1.0,
};

/// 3D Vector struct.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Vector {
Expand Down

0 comments on commit 5b867ab

Please sign in to comment.