Add builder pattern to a few core types.

Add a From<[3;Float]> impl for Color to make things nicer.
This commit is contained in:
2021-07-21 20:42:08 -07:00
parent e041fd1f6a
commit de898f0b0a
6 changed files with 135 additions and 5 deletions

View File

@@ -1,8 +1,12 @@
use derive_builder::Builder;
use crate::tuples::{Color, Tuple};
#[derive(Clone, Debug, PartialEq)]
#[derive(Builder, Clone, Debug, Default, PartialEq)]
#[builder(default)]
pub struct PointLight {
pub position: Tuple,
#[builder(setter(into))]
pub intensity: Color,
}

View File

@@ -1,11 +1,16 @@
use derive_builder::Builder;
use crate::{
lights::PointLight,
tuples::Color,
tuples::{dot, reflect, Tuple},
Float, BLACK, WHITE,
};
#[derive(Debug, PartialEq, Clone)]
#[derive(Builder, Debug, PartialEq, Clone)]
#[builder(default)]
pub struct Material {
#[builder(setter(into))]
pub color: Color,
pub ambient: Float,
pub diffuse: Float,

View File

@@ -1,5 +1,7 @@
use std::sync::{Arc, Mutex};
use derive_builder::Builder;
use crate::{
intersections::Intersections, materials::Material, matrices::Matrix4x4, rays::Ray,
tuples::Tuple,
@@ -20,6 +22,12 @@ pub enum Geometry {
Plane,
}
impl Default for Geometry {
fn default() -> Geometry {
Geometry::Sphere
}
}
impl PartialEq for Geometry {
fn eq(&self, rhs: &Geometry) -> bool {
use Geometry::*;
@@ -35,7 +43,8 @@ impl PartialEq for Geometry {
/// Shape represents visible objects. A signal instance of Shape can generically represent one of
/// many different shapes based on the value of it's geometry field. Users chose the shape by
/// calling the appropriate constructor, i.e. [Shape::sphere].
#[derive(Debug, Clone, PartialEq)]
#[derive(Builder, Debug, Clone, PartialEq)]
#[builder(default)]
pub struct Shape {
transform: Matrix4x4,
inverse_transform: Matrix4x4,
@@ -43,6 +52,17 @@ pub struct Shape {
geometry: Geometry,
}
impl Default for Shape {
fn default() -> Shape {
Shape {
transform: Matrix4x4::identity(),
inverse_transform: Matrix4x4::identity(),
material: Material::default(),
geometry: Geometry::default(),
}
}
}
impl Shape {
/// Create a test shape useful for debugging.
///

View File

@@ -161,17 +161,29 @@ pub fn cross(a: Tuple, b: Tuple) -> Tuple {
)
}
#[derive(Copy, Clone, Debug)]
#[derive(Copy, Clone, Debug, Default)]
pub struct Color {
pub red: Float,
pub green: Float,
pub blue: Float,
}
impl Color {
pub const fn new(red: Float, green: Float, blue: Float) -> Color {
Color { red, green, blue }
}
}
impl From<[Float; 3]> for Color {
fn from(rgb: [Float; 3]) -> Self {
Color {
red: rgb[0],
green: rgb[1],
blue: rgb[2],
}
}
}
impl PartialEq for Color {
fn eq(&self, rhs: &Color) -> bool {
((self.red - rhs.red).abs() < EPSILON)
@@ -179,6 +191,7 @@ impl PartialEq for Color {
&& ((self.blue - rhs.blue).abs() < EPSILON)
}
}
impl Add for Color {
type Output = Self;
fn add(self, other: Self) -> Self {
@@ -222,6 +235,7 @@ impl Mul<Color> for Float {
}
}
}
impl Mul<Color> for Color {
type Output = Color;
fn mul(self, rhs: Color) -> Self::Output {
@@ -243,6 +257,7 @@ impl Neg for Color {
}
}
}
impl Sub for Color {
type Output = Self;
fn sub(self, other: Self) -> Self {