-
Notifications
You must be signed in to change notification settings - Fork 1
/
sphere.rs
126 lines (101 loc) · 3.23 KB
/
sphere.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
use math::{Point3, Transform};
use geometry::{Intersectable, Shape, Transformable};
use intersection::Intersection;
use ray::Ray;
use material::Material;
#[derive(Debug)]
pub struct Sphere {
pub origin: Point3,
pub radius: f32,
material: Material,
}
impl Sphere {
pub fn new(origin: Point3, radius: f32, material: Material) -> Sphere {
Sphere {
origin: origin,
radius: radius,
material: material,
}
}
}
impl Shape for Sphere {
fn material(&self) -> &Material {
return &self.material;
}
}
impl Intersectable for Sphere {
fn intersect(&self, ray: Ray, _: bool) -> Option<Intersection> {
let v = ray.origin - self.origin;
let a = ray.direction.dot(&v);
let b = -a;
let c = a.powf(2.0) - v.length().powf(2.0) + self.radius.powf(2.0);
if c < 0.0 {
return None;
}
let t1 = b + c.sqrt();
let t2 = b - c.sqrt();
let mut t: Option<f32> = None;
let mut hit = false;
let mut inside = false;
if t1 > 0.01 {
if t2 < 0.0 {
t = Some(t1);
hit = true;
inside = true;
} else {
t = Some(t2);
hit = true;
}
}
if hit {
assert!(t.is_some());
let point: Point3 = (ray.origin + ray.direction * t.unwrap()).as_point();
let n = (point - self.origin).normalize();
let intersection = Intersection::new(t.unwrap(), self, point, ray, n, inside);
return Some(intersection);
}
None
}
}
impl Transformable for Sphere {
fn transform(&mut self, transform: &Transform) {
self.origin = self.origin * transform.matrix;
}
}
#[cfg(test)]
mod tests {
use super::Sphere;
use math::{Point3, Vector3, EPSILON};
use ray::Ray;
use geometry::Shape;
use material::{Material, MaterialTemplate};
use color::Color;
fn build_test_material() -> Material {
let color = Color::new(0, 0, 0);
MaterialTemplate::new(color, color, color, None, None).build_material(|_ignore| {})
}
#[test]
fn test_intersection_miss() {
let material = build_test_material();
let sphere = Sphere::new(Point3::at_origin(), 1.0, material);
let ray = Ray::new(Point3::new(0.0, 0.0, 2.0),
Vector3::new(0.0, 0.0, 1.0),
None);
let intersection = (&sphere as &Shape).intersect(ray);
assert!(intersection.is_none());
}
#[test]
fn test_intersection() {
let material = build_test_material();
let sphere = Sphere::new(Point3::at_origin(), 1.0, material);
let ray = Ray::new(Point3::new(0.0, 0.0, 2.0),
Vector3::new(0.0, 0.0, -1.0),
None);
let i = (&sphere as &Shape).intersect(ray);
assert!(i.is_some());
let intersection = i.unwrap();
assert_eq_within_bound!(intersection.t, 1.0, EPSILON);
assert_eq_vector3!(intersection.point, Vector3::new(0.0, 0.0, 1.0), EPSILON);
assert_eq_vector3!(intersection.normal, Vector3::new(0.0, 0.0, 1.0), EPSILON);
}
}