Compare commits
2 Commits
b2ec53525d
...
8a0f44144d
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8a0f44144d | ||
|
|
71fc73bf1a |
45
.vscode/launch.json
vendored
Normal file
45
.vscode/launch.json
vendored
Normal file
@@ -0,0 +1,45 @@
|
||||
{
|
||||
// Use IntelliSense to learn about possible attributes.
|
||||
// Hover to view descriptions of existing attributes.
|
||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"type": "lldb",
|
||||
"request": "launch",
|
||||
"name": "Debug executable 'ray-tracer'",
|
||||
"cargo": {
|
||||
"args": [
|
||||
"build",
|
||||
"--bin=ray-tracer",
|
||||
"--package=ray-tracer"
|
||||
],
|
||||
"filter": {
|
||||
"name": "ray-tracer",
|
||||
"kind": "bin"
|
||||
}
|
||||
},
|
||||
"args": [],
|
||||
"cwd": "${workspaceFolder}"
|
||||
},
|
||||
{
|
||||
"type": "lldb",
|
||||
"request": "launch",
|
||||
"name": "Debug unit tests in executable 'ray-tracer'",
|
||||
"cargo": {
|
||||
"args": [
|
||||
"test",
|
||||
"--no-run",
|
||||
"--bin=ray-tracer",
|
||||
"--package=ray-tracer"
|
||||
],
|
||||
"filter": {
|
||||
"name": "ray-tracer",
|
||||
"kind": "bin"
|
||||
}
|
||||
},
|
||||
"args": [],
|
||||
"cwd": "${workspaceFolder}"
|
||||
}
|
||||
]
|
||||
}
|
||||
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -22,6 +22,7 @@ name = "features"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"approx",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
||||
1
features/Cargo.lock
generated
1
features/Cargo.lock
generated
@@ -22,6 +22,7 @@ name = "features"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"approx",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
||||
@@ -7,3 +7,4 @@ edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
approx = "0.5"
|
||||
num-traits = "0.2"
|
||||
@@ -1,5 +1,6 @@
|
||||
#[macro_use]
|
||||
extern crate approx;
|
||||
extern crate num_traits;
|
||||
|
||||
pub mod structs;
|
||||
pub mod color;
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
use std::fmt;
|
||||
use std::ops;
|
||||
|
||||
use num_traits::NumCast;
|
||||
use num_traits::cast;
|
||||
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
pub struct Tuple {
|
||||
x: f32,
|
||||
@@ -9,13 +12,24 @@ pub struct Tuple {
|
||||
w: f32,
|
||||
}
|
||||
|
||||
macro_rules! num_traits_cast {
|
||||
($tt:ident) => {
|
||||
cast($tt).unwrap()
|
||||
};
|
||||
}
|
||||
|
||||
impl Tuple {
|
||||
pub fn new(x: f32, y: f32, z: f32, w: f32) -> Tuple {
|
||||
pub fn new<X, Y, Z, W>(x: X, y: Y, z: Z, w: W) -> Tuple
|
||||
where X: NumCast,
|
||||
Y: NumCast,
|
||||
Z: NumCast,
|
||||
W: NumCast,
|
||||
{
|
||||
Tuple {
|
||||
x,
|
||||
y,
|
||||
z,
|
||||
w,
|
||||
x: num_traits_cast!(x),
|
||||
y: num_traits_cast!(y),
|
||||
z: num_traits_cast!(z),
|
||||
w: num_traits_cast!(w),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,20 +42,28 @@ impl Tuple {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn point(x: f32, y: f32, z: f32) -> Tuple {
|
||||
pub fn point<T, U, V>(x: T, y: U, z: V) -> Tuple
|
||||
where T: NumCast,
|
||||
U: NumCast,
|
||||
V: NumCast,
|
||||
|
||||
{
|
||||
Tuple {
|
||||
x,
|
||||
y,
|
||||
z,
|
||||
x: num_traits_cast!(x),
|
||||
y: num_traits_cast!(y),
|
||||
z: num_traits_cast!(z),
|
||||
w: 1.0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn vector(x: f32, y: f32, z: f32) -> Tuple {
|
||||
pub fn vector<X, Y, Z>(x: X, y: Y, z: Z) -> Tuple
|
||||
where X: NumCast,
|
||||
Y: NumCast,
|
||||
Z: NumCast {
|
||||
Tuple {
|
||||
x,
|
||||
y,
|
||||
z,
|
||||
x: num_traits_cast!(x),
|
||||
y: num_traits_cast!(y),
|
||||
z: num_traits_cast!(z),
|
||||
w: 0.0,
|
||||
}
|
||||
}
|
||||
@@ -424,4 +446,39 @@ mod tests {
|
||||
let b_cross_a = Tuple::vector(1.0, -2.0, 1.0);
|
||||
assert_eq!(b_cross_a, b.cross(&a));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn works_with_i32() {
|
||||
let a = Tuple::new(1, 2, 3, 0);
|
||||
assert_eq!(1.0, a.x());
|
||||
assert_eq!(2.0, a.y());
|
||||
assert_eq!(3.0, a.z());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn works_with_mixed_types() {
|
||||
let a = Tuple::new(1.1, 2.2, 3, 0);
|
||||
assert_eq!(1.1, a.x());
|
||||
assert_eq!(2.2, a.y());
|
||||
assert_eq!(3.0, a.z());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn point_with_mixed_types() {
|
||||
let a = Tuple::point(1.0, 2.2, 3);
|
||||
assert_eq!(1.0, a.x());
|
||||
assert_eq!(2.2, a.y());
|
||||
assert_eq!(3.0, a.z());
|
||||
assert_eq!(1.0, a.w());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn vector_with_mixed_types() {
|
||||
let a = Tuple::vector(1.0, 2.2, 3);
|
||||
assert_eq!(1.0, a.x());
|
||||
assert_eq!(2.2, a.y());
|
||||
assert_eq!(3.0, a.z());
|
||||
assert_eq!(0.0, a.w());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -175,7 +175,7 @@ fn clock() {
|
||||
let mut canvas = Canvas::new(1024, 1024);
|
||||
let color = Color::new(1.0, 0.0, 0.0);
|
||||
let middle = 1024.0 / 2.0;
|
||||
canvas.write_pixel(middle as usize, middle as usize, Color::new(0.0, 1.0, 0.0));
|
||||
|
||||
let middle_point = &Tuple::point_zero() * &Matrix::translation(middle, middle, 0.0);
|
||||
draw_cross(&mut canvas, &middle_point, Color::new(0.0, 1.0, 0.0));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user