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:
parent
e041fd1f6a
commit
de898f0b0a
87
rtchallenge/Cargo.lock
generated
87
rtchallenge/Cargo.lock
generated
@ -130,7 +130,7 @@ dependencies = [
|
|||||||
"ansi_term",
|
"ansi_term",
|
||||||
"atty",
|
"atty",
|
||||||
"bitflags",
|
"bitflags",
|
||||||
"strsim",
|
"strsim 0.8.0",
|
||||||
"textwrap",
|
"textwrap",
|
||||||
"unicode-width",
|
"unicode-width",
|
||||||
"vec_map",
|
"vec_map",
|
||||||
@ -259,6 +259,41 @@ dependencies = [
|
|||||||
"memchr",
|
"memchr",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "darling"
|
||||||
|
version = "0.12.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "5f2c43f534ea4b0b049015d00269734195e6d3f0f6635cb692251aca6f9f8b3c"
|
||||||
|
dependencies = [
|
||||||
|
"darling_core",
|
||||||
|
"darling_macro",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "darling_core"
|
||||||
|
version = "0.12.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "8e91455b86830a1c21799d94524df0845183fa55bafd9aa137b01c7d1065fa36"
|
||||||
|
dependencies = [
|
||||||
|
"fnv",
|
||||||
|
"ident_case",
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"strsim 0.10.0",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "darling_macro"
|
||||||
|
version = "0.12.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "29b5acf0dea37a7f66f7b25d2c5e93fd46f8f6968b1a5d7a3e02e97768afc95a"
|
||||||
|
dependencies = [
|
||||||
|
"darling_core",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "deflate"
|
name = "deflate"
|
||||||
version = "0.8.6"
|
version = "0.8.6"
|
||||||
@ -269,6 +304,37 @@ dependencies = [
|
|||||||
"byteorder",
|
"byteorder",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "derive_builder"
|
||||||
|
version = "0.10.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d13202debe11181040ae9063d739fa32cfcaaebe2275fe387703460ae2365b30"
|
||||||
|
dependencies = [
|
||||||
|
"derive_builder_macro",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "derive_builder_core"
|
||||||
|
version = "0.10.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "66e616858f6187ed828df7c64a6d71720d83767a7f19740b2d1b6fe6327b36e5"
|
||||||
|
dependencies = [
|
||||||
|
"darling",
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "derive_builder_macro"
|
||||||
|
version = "0.10.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "58a94ace95092c5acb1e97a7e846b310cfbd499652f72297da7493f618a98d73"
|
||||||
|
dependencies = [
|
||||||
|
"derive_builder_core",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "either"
|
name = "either"
|
||||||
version = "1.6.1"
|
version = "1.6.1"
|
||||||
@ -308,6 +374,12 @@ dependencies = [
|
|||||||
"backtrace",
|
"backtrace",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "fnv"
|
||||||
|
version = "1.0.7"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "getrandom"
|
name = "getrandom"
|
||||||
version = "0.2.3"
|
version = "0.2.3"
|
||||||
@ -349,6 +421,12 @@ dependencies = [
|
|||||||
"libc",
|
"libc",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ident_case"
|
||||||
|
version = "1.0.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "itertools"
|
name = "itertools"
|
||||||
version = "0.9.0"
|
version = "0.9.0"
|
||||||
@ -662,6 +740,7 @@ dependencies = [
|
|||||||
"anyhow",
|
"anyhow",
|
||||||
"core_affinity",
|
"core_affinity",
|
||||||
"criterion",
|
"criterion",
|
||||||
|
"derive_builder",
|
||||||
"enum-utils",
|
"enum-utils",
|
||||||
"num_cpus",
|
"num_cpus",
|
||||||
"png",
|
"png",
|
||||||
@ -773,6 +852,12 @@ version = "0.8.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
|
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "strsim"
|
||||||
|
version = "0.10.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "structopt"
|
name = "structopt"
|
||||||
version = "0.3.22"
|
version = "0.3.22"
|
||||||
|
|||||||
@ -15,6 +15,7 @@ float-as-double = []
|
|||||||
anyhow = "1.0.41"
|
anyhow = "1.0.41"
|
||||||
core_affinity = "0.5.10"
|
core_affinity = "0.5.10"
|
||||||
criterion = "0.3.4"
|
criterion = "0.3.4"
|
||||||
|
derive_builder = "0.10.2"
|
||||||
enum-utils = "0.1.2"
|
enum-utils = "0.1.2"
|
||||||
num_cpus = "1.13.0"
|
num_cpus = "1.13.0"
|
||||||
png = "0.16.8"
|
png = "0.16.8"
|
||||||
|
|||||||
@ -1,8 +1,12 @@
|
|||||||
|
use derive_builder::Builder;
|
||||||
|
|
||||||
use crate::tuples::{Color, Tuple};
|
use crate::tuples::{Color, Tuple};
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Builder, Clone, Debug, Default, PartialEq)]
|
||||||
|
#[builder(default)]
|
||||||
pub struct PointLight {
|
pub struct PointLight {
|
||||||
pub position: Tuple,
|
pub position: Tuple,
|
||||||
|
#[builder(setter(into))]
|
||||||
pub intensity: Color,
|
pub intensity: Color,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,11 +1,16 @@
|
|||||||
|
use derive_builder::Builder;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
lights::PointLight,
|
lights::PointLight,
|
||||||
tuples::Color,
|
tuples::Color,
|
||||||
tuples::{dot, reflect, Tuple},
|
tuples::{dot, reflect, Tuple},
|
||||||
Float, BLACK, WHITE,
|
Float, BLACK, WHITE,
|
||||||
};
|
};
|
||||||
#[derive(Debug, PartialEq, Clone)]
|
|
||||||
|
#[derive(Builder, Debug, PartialEq, Clone)]
|
||||||
|
#[builder(default)]
|
||||||
pub struct Material {
|
pub struct Material {
|
||||||
|
#[builder(setter(into))]
|
||||||
pub color: Color,
|
pub color: Color,
|
||||||
pub ambient: Float,
|
pub ambient: Float,
|
||||||
pub diffuse: Float,
|
pub diffuse: Float,
|
||||||
|
|||||||
@ -1,5 +1,7 @@
|
|||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
|
use derive_builder::Builder;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
intersections::Intersections, materials::Material, matrices::Matrix4x4, rays::Ray,
|
intersections::Intersections, materials::Material, matrices::Matrix4x4, rays::Ray,
|
||||||
tuples::Tuple,
|
tuples::Tuple,
|
||||||
@ -20,6 +22,12 @@ pub enum Geometry {
|
|||||||
Plane,
|
Plane,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Default for Geometry {
|
||||||
|
fn default() -> Geometry {
|
||||||
|
Geometry::Sphere
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl PartialEq for Geometry {
|
impl PartialEq for Geometry {
|
||||||
fn eq(&self, rhs: &Geometry) -> bool {
|
fn eq(&self, rhs: &Geometry) -> bool {
|
||||||
use Geometry::*;
|
use Geometry::*;
|
||||||
@ -35,7 +43,8 @@ impl PartialEq for Geometry {
|
|||||||
/// Shape represents visible objects. A signal instance of Shape can generically represent one of
|
/// 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
|
/// 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].
|
/// calling the appropriate constructor, i.e. [Shape::sphere].
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Builder, Debug, Clone, PartialEq)]
|
||||||
|
#[builder(default)]
|
||||||
pub struct Shape {
|
pub struct Shape {
|
||||||
transform: Matrix4x4,
|
transform: Matrix4x4,
|
||||||
inverse_transform: Matrix4x4,
|
inverse_transform: Matrix4x4,
|
||||||
@ -43,6 +52,17 @@ pub struct Shape {
|
|||||||
geometry: Geometry,
|
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 {
|
impl Shape {
|
||||||
/// Create a test shape useful for debugging.
|
/// Create a test shape useful for debugging.
|
||||||
///
|
///
|
||||||
|
|||||||
@ -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 struct Color {
|
||||||
pub red: Float,
|
pub red: Float,
|
||||||
pub green: Float,
|
pub green: Float,
|
||||||
pub blue: Float,
|
pub blue: Float,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Color {
|
impl Color {
|
||||||
pub const fn new(red: Float, green: Float, blue: Float) -> Color {
|
pub const fn new(red: Float, green: Float, blue: Float) -> Color {
|
||||||
Color { red, green, blue }
|
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 {
|
impl PartialEq for Color {
|
||||||
fn eq(&self, rhs: &Color) -> bool {
|
fn eq(&self, rhs: &Color) -> bool {
|
||||||
((self.red - rhs.red).abs() < EPSILON)
|
((self.red - rhs.red).abs() < EPSILON)
|
||||||
@ -179,6 +191,7 @@ impl PartialEq for Color {
|
|||||||
&& ((self.blue - rhs.blue).abs() < EPSILON)
|
&& ((self.blue - rhs.blue).abs() < EPSILON)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Add for Color {
|
impl Add for Color {
|
||||||
type Output = Self;
|
type Output = Self;
|
||||||
fn add(self, other: Self) -> Self {
|
fn add(self, other: Self) -> Self {
|
||||||
@ -222,6 +235,7 @@ impl Mul<Color> for Float {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Mul<Color> for Color {
|
impl Mul<Color> for Color {
|
||||||
type Output = Color;
|
type Output = Color;
|
||||||
fn mul(self, rhs: Color) -> Self::Output {
|
fn mul(self, rhs: Color) -> Self::Output {
|
||||||
@ -243,6 +257,7 @@ impl Neg for Color {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Sub for Color {
|
impl Sub for Color {
|
||||||
type Output = Self;
|
type Output = Self;
|
||||||
fn sub(self, other: Self) -> Self {
|
fn sub(self, other: Self) -> Self {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user