Skip to content

Commit

Permalink
Refactoring: move files into lib
Browse files Browse the repository at this point in the history
  • Loading branch information
fremag committed Feb 1, 2024
1 parent 40f0574 commit 6929ac3
Show file tree
Hide file tree
Showing 77 changed files with 308 additions and 325 deletions.
10 changes: 9 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
workspace = { members = [ "ray-tracer-lib"] }

[package]
name = "ray_tracer_rust"
version = "0.1.0"
Expand All @@ -6,5 +8,5 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
sorted-vec = "0.8.3"
rand = { version = "0.8.5", features = [] }
rand = { version = "0.8.5", features = [] }
ray-tracer-lib = { version = "0.1.0", path = "ray-tracer-lib" }
4 changes: 2 additions & 2 deletions src/camera.rs → ray-tracer-lib/src/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl Camera {
self.inverse_transform = self.transform.inverse();
}

pub(crate) fn ray_for_pixel(&self, px: usize, py: usize) -> Ray {
pub fn ray_for_pixel(&self, px: usize, py: usize) -> Ray {
// the offset from the edge of the canvas to the pixel's center
let xoffset = (px as Float + 0.5) * self.pixel_size;
let yoffset = (py as Float + 0.5) * self.pixel_size;
Expand All @@ -63,7 +63,7 @@ impl Camera {
ray(origin, direction)
}

pub(crate) fn render(&self, world: &World, file_path: &str) -> Canvas {
pub fn render(&self, world: &World, file_path: &str) -> Canvas {
let mut image = Canvas::new(self.h_size, self.v_size);
for y in 0..self.v_size {
let pct= (y+1) as f32 / self.v_size as f32 * 100.0;
Expand Down
2 changes: 1 addition & 1 deletion src/canvas.rs → ray-tracer-lib/src/canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl Canvas {
line.push(' ' );
}

pub(crate) fn save(&self, path: &str) -> Result<(), Error> {
pub fn save(&self, path: &str) -> Result<(), Error> {
let mut output = File::create(path)?;
write!(output, "{}", self.ppm_header())?;
write!(output, "{}", self.pixel_data())?;
Expand Down
6 changes: 3 additions & 3 deletions src/colors.rs → ray-tracer-lib/src/colors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use std::ops::{Add, Sub, Mul};

#[derive(Debug, Clone, Copy)]
pub struct Color {
pub(crate) r : Float,
pub(crate) g : Float,
pub(crate) b : Float
pub r : Float,
pub g : Float,
pub b : Float
}

impl Color {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ impl<'a> Intersections<'a> {
}
None
}
pub fn count(&self) -> usize {
self.intersections.len()
}
}

impl<'a> Index<usize> for Intersections<'a> {
Expand Down
1 change: 0 additions & 1 deletion src/core/math.rs → ray-tracer-lib/src/core/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ pub type Float = f64;
pub const EPSILON: Float = 0.0001;
pub const INFINITY: Float = f64::INFINITY;
pub const PI: Float = std::f64::consts::PI;
#[cfg(test)]
pub const SQRT2: Float = std::f64::consts::SQRT_2 as Float;

pub fn equals(a: Float, b: Float) -> bool {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ pub fn rotation_z(r : Float) -> Matrix<4> {
0.0, 0.0, 0.0, 1.0 )
}

#[cfg(test)]
pub fn shearing(xy : Float , xz: Float, yx : Float, yz : Float , zx: Float, zy : Float) -> Matrix<4> {
Matrix::new4(1.0, xy, xz, 0.0,
yx, 1.0, yz, 0.0,
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions src/material.rs → ray-tracer-lib/src/material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ pub struct Material {
pub reflective: Float,
pub transparency: Float,
pub refractive_index: Float,
pub(crate) pattern: Pattern,
pub pattern: Pattern,
}

impl Material {
pub fn new() -> Material {
Material { color: Color { r: 1.0, g: 1.0, b: 1.0 }, ambient: 0.1, diffuse: 0.9, specular: 0.9, shininess: 200.0, reflective: 0.0, transparency: 0.0, refractive_index: 1.0, pattern: Pattern::new() }
}

pub(crate) fn lighting(&self, object: &Object, light: &Light, point: Tuple, eyev: Tuple, normalv: Tuple, in_shadow : bool) -> Color {
pub fn lighting(&self, object: &Object, light: &Light, point: Tuple, eyev: Tuple, normalv: Tuple, in_shadow : bool) -> Color {
let color =self.pattern.pattern_at_object(object, point);

// combine the surface color with the light's color/intensity
Expand Down
17 changes: 12 additions & 5 deletions src/object.rs → ray-tracer-lib/src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,24 @@ pub struct Object {
pub enum ObjectType { ObjectShape(Shape), ObjectGroup(Group)}

impl Object {
pub(crate) fn normal_to_world(&self, normal: &Tuple) -> Tuple {
pub fn group(&self) -> Option<&Group> {
match &self.object_type {
ObjectGroup(group) => Some(&group),
_ => None,
}
}

pub fn normal_to_world(&self, normal: &Tuple) -> Tuple {
let mut n = &self.transformation_inverse_transpose * normal;
n.w = 0.0;
n.normalize()
}

pub(crate) fn world_to_object(&self, point: &Tuple) -> Tuple {
pub fn world_to_object(&self, point: &Tuple) -> Tuple {
&(self.transformation_inverse) * point
}

pub(crate) fn normal_at(&self, world_point: Tuple) -> Tuple {
pub fn normal_at(&self, world_point: Tuple) -> Tuple {
let local_point = self.world_to_object(&world_point);
let local_normal = match &self.object_type {
ObjectShape(shape) => shape.normal_at(local_point),
Expand All @@ -49,15 +56,15 @@ impl Object {
n
}

pub(crate) fn intersect(&self, ray: &Ray) -> Intersections {
pub fn intersect(&self, ray: &Ray) -> Intersections {
let ray2 = ray.transform(&self.transformation_inverse);
return match &self.object_type {
ObjectShape(shape) => intersections(shape.intersect(&ray2).iter().map(|t| Intersection::new(*t, &self )).collect()),
ObjectGroup(group) => group.intersect(&ray2),
};
}

pub(crate) fn bounds(&self) -> Bounds {
pub fn bounds(&self) -> Bounds {
return match &self.object_type {
ObjectShape(shape) => shape.bounds().transform(&self.transformation),
ObjectGroup(group) => group.bounds(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub struct CheckerPattern {
impl CheckerPattern {
pub fn new(a: Color, b: Color) -> Self { Self {a, b} }

pub(crate) fn pattern_at(&self, point: &Tuple) -> Color {
pub fn pattern_at(&self, point: &Tuple) -> Color {
let sum_floor_coord = point.x.floor() as i32 + point.y.floor() as i32 + point.z.floor() as i32;
if sum_floor_coord % 2 == 0 {
return self.a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub struct GradientPattern {
impl GradientPattern {
pub fn new(a: Color, b: Color) -> Self { Self {a, b} }

pub(crate) fn pattern_at(&self, point: &Tuple) -> Color {
pub fn pattern_at(&self, point: &Tuple) -> Color {
let fraction = point.x - point.x.floor();
Color::new(
self.a.r + (self.b.r - self.a.r) * fraction,
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::core::tuple::Tuple;

#[derive(Debug, Copy, Clone)]
pub struct Pattern {
pub(crate) pattern : Patterns,
pub pattern : Patterns,
pub inverse_transform : Matrix<4>,
}

Expand All @@ -24,27 +24,27 @@ impl Pattern {
new_pattern
}

pub(crate) fn stripe(color_a: Color, color_b: Color) -> Pattern {
pub fn stripe(color_a: Color, color_b: Color) -> Pattern {
Self::from(Patterns::Stripe(StripePattern::new(color_a, color_b)))
}

pub(crate) fn gradient(color_a: Color, color_b: Color) -> Pattern {
pub fn gradient(color_a: Color, color_b: Color) -> Pattern {
Self::from(Patterns::Gradient(GradientPattern::new(color_a, color_b)))
}

pub(crate) fn ring(color_a: Color, color_b: Color) -> Pattern {
pub fn ring(color_a: Color, color_b: Color) -> Pattern {
Self::from(Patterns::Ring(RingPattern::new(color_a, color_b)))
}

pub(crate) fn checker(color_a: Color, color_b: Color) -> Pattern {
pub fn checker(color_a: Color, color_b: Color) -> Pattern {
Self::from(Patterns::Checker(CheckerPattern::new(color_a, color_b)))
}

pub(crate) fn test() -> Pattern {
pub fn test() -> Pattern {
Self::from(Patterns::Test)
}

pub(crate) fn pattern_at_object(&self, object: &Object, world_point: Tuple) -> Color {
pub fn pattern_at_object(&self, object: &Object, world_point: Tuple) -> Color {
let object_point = object.transformation_inverse() * &world_point;
let pattern_point = &self.inverse_transform * &object_point;
match self.pattern {
Expand All @@ -57,7 +57,7 @@ impl Pattern {
}
}

pub(crate) fn set_pattern_transform(&mut self, transform: &Matrix<4>) {
pub fn set_pattern_transform(&mut self, transform: &Matrix<4>) {
self.inverse_transform = transform.inverse();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub struct RingPattern {
impl RingPattern {
pub fn new(a: Color, b: Color) -> Self { Self {a, b} }

pub(crate) fn pattern_at(&self, point: &Tuple) -> Color {
pub fn pattern_at(&self, point: &Tuple) -> Color {
let dist = (point.x * point.x + point.z * point.z).sqrt().floor() as i32;
if dist % 2 == 0 {
return self.a
Expand Down
File renamed without changes.
14 changes: 9 additions & 5 deletions src/shapes/cone.rs → ray-tracer-lib/src/shapes/cone.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::mem;
use crate::core::bounds::Bounds;
use crate::core::math::{EPSILON, equals, Float};
use crate::core::math::{EPSILON, equals, Float, INFINITY};
use crate::core::ray::Ray;
use crate::core::tuple::{point, Tuple, vector};

Expand All @@ -21,7 +21,11 @@ impl Eq for Cone {
}

impl Cone {
pub(crate) fn intersect(&self, ray: &Ray) -> Vec<Float> {
pub fn new() -> Self {
Cone { min: -INFINITY, max: INFINITY, closed: false }
}

pub fn intersect(&self, ray: &Ray) -> Vec<Float> {
let mut xs = vec![];

let a = ray.direction.x * ray.direction.x - ray.direction.y * ray.direction.y + ray.direction.z * ray.direction.z;
Expand Down Expand Up @@ -64,11 +68,11 @@ impl Cone {
xs
}

pub(crate) fn from(min : Float, max : Float, closed : bool) -> Self {
pub fn from(min : Float, max : Float, closed : bool) -> Self {
Cone {min, max, closed}
}

pub(crate) fn normal_at(&self, point: &Tuple) -> Tuple {
pub fn normal_at(&self, point: &Tuple) -> Tuple {
// compute the square of the distance from the y axis
let dist = point.x * point.x + point.z * point.z;
if dist < 1.0 && point.y >= self.max - EPSILON {
Expand Down Expand Up @@ -115,7 +119,7 @@ impl Cone {
}
}

pub(crate) fn bounds(&self) -> Bounds {
pub fn bounds(&self) -> Bounds {
Bounds::from( point(self.min, self.min, self.min), point(self.max, self.max, self.max))
}
}
6 changes: 3 additions & 3 deletions src/shapes/cube.rs → ray-tracer-lib/src/shapes/cube.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::core::tuple::{point, Tuple, vector};
pub struct Cube {}

impl Cube {
pub(crate) fn normal_at(&self, point: Tuple) -> Tuple {
pub fn normal_at(&self, point: Tuple) -> Tuple {
let max_c = math::max(point.x.abs(), point.y.abs(), point.z.abs());
if max_c == point.x.abs() {
return vector(point.x, 0.0, 0.0);
Expand All @@ -22,11 +22,11 @@ impl Cube {
Cube {}
}

pub(crate) fn intersect(&self, ray: &Ray) -> Vec<Float> {
pub fn intersect(&self, ray: &Ray) -> Vec<Float> {
self.bounds().intersect(&ray)
}

pub(crate) fn bounds(&self) -> Bounds {
pub fn bounds(&self) -> Bounds {
Bounds::from( point(-1.0, -1.0, -1.0), point(1.0, 1.0, 1.0))
}
}
14 changes: 9 additions & 5 deletions src/shapes/cylinder.rs → ray-tracer-lib/src/shapes/cylinder.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::mem;
use crate::core::bounds::Bounds;
use crate::core::math::{EPSILON, equals, Float};
use crate::core::math::{EPSILON, equals, Float, INFINITY};
use crate::core::ray::Ray;
use crate::core::tuple::{point, Tuple, vector};

Expand All @@ -21,7 +21,11 @@ impl Eq for Cylinder {
}

impl Cylinder {
pub(crate) fn intersect(&self, ray: &Ray) -> Vec<Float> {
pub fn new() -> Self {
Cylinder {min: -INFINITY, max: INFINITY, closed: false}
}

pub fn intersect(&self, ray: &Ray) -> Vec<Float> {
let mut xs = vec![];

let a = ray.direction.x * ray.direction.x + ray.direction.z * ray.direction.z;
Expand Down Expand Up @@ -60,11 +64,11 @@ impl Cylinder {
xs
}

pub(crate) fn from(min : Float, max : Float, closed : bool) -> Self {
pub fn from(min : Float, max : Float, closed : bool) -> Self {
Cylinder {min, max, closed}
}

pub(crate) fn normal_at(&self, point: &Tuple) -> Tuple {
pub fn normal_at(&self, point: &Tuple) -> Tuple {
// compute the square of the distance from the y axis
let dist = point.x * point.x + point.z * point.z;
if dist < 1.0 && point.y >= self.max - EPSILON {
Expand Down Expand Up @@ -107,7 +111,7 @@ impl Cylinder {
}
}

pub(crate) fn bounds(&self) -> Bounds {
pub fn bounds(&self) -> Bounds {
Bounds::from( point(-1.0, self.min, -1.0), point(1.0, self.max, 1.0))
}
}
Loading

0 comments on commit 6929ac3

Please sign in to comment.