-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_simple_path_tracer.py
109 lines (85 loc) · 3.7 KB
/
test_simple_path_tracer.py
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
import unittest
import math
import simple_path_tracer
from simple_path_tracer import Vector, Color, Ray, Sphere
class SimplePathTracertest(unittest.TestCase):
def test_clamp(self):
self.assertEqual(simple_path_tracer.clamp(0.5), 0.5)
self.assertEqual(simple_path_tracer.clamp(0.0), 0.0)
self.assertEqual(simple_path_tracer.clamp(1), 1.0)
self.assertEqual(simple_path_tracer.clamp(1.1), 1.0)
self.assertEqual(simple_path_tracer.clamp(-1.0), 0.0)
class VectorTest(unittest.TestCase):
def test_add(self):
sum_vec = Vector(1.0, 1.0, 1.0) + Vector(2.0, 2.0, 2.0)
self.assertEqual(sum_vec.x, 3.0)
self.assertEqual(sum_vec.y, 3.0)
self.assertEqual(sum_vec.z, 3.0)
def test_sub(self):
sum_vec = Vector(2.0, 2.0, 2.0) - Vector(1.0, 1.0, 1.0)
self.assertEqual(sum_vec.x, 1.0)
self.assertEqual(sum_vec.y, 1.0)
self.assertEqual(sum_vec.z, 1.0)
def test_mul(self):
sum_vec = Vector(2.0, 2.0, 2.0) * 3.0
self.assertEqual(sum_vec.x, 6.0)
self.assertEqual(sum_vec.y, 6.0)
self.assertEqual(sum_vec.z, 6.0)
def test_div(self):
sum_vec = Vector(2.0, 2.0, 2.0) / 2.0
self.assertEqual(sum_vec.x, 1.0)
self.assertEqual(sum_vec.y, 1.0)
self.assertEqual(sum_vec.z, 1.0)
def test_get_length_squared(self):
vec = Vector(2.0, 2.0, 2.0)
self.assertEqual(vec.get_length_squared(), 12.0)
def test_get_length(self):
vec = Vector(2.0, 2.0, 2.0)
self.assertEqual(vec.get_length(), math.sqrt(12.0))
def test_dot(self):
self.assertEqual(Vector(2.0, 2.0, 2.0).dot(Vector(1.0,1.0,1.0)), 6.0)
def test_cross(self):
result = Vector(0.0, 0.0, 1.0).cross(Vector(0.0,1.0,0.0))
self.assertEqual(result.x, -1.0)
self.assertEqual(result.y, 0.0)
self.assertEqual(result.z, 0.0)
def test_multiply(self):
result = Vector(1.0, 2.0, 3.0).multiply(Vector(2.0, 2.0, 2.0))
self.assertEqual(result.x, 2.0)
self.assertEqual(result.y, 4.0)
self.assertEqual(result.z, 6.0)
def test_normalize(self):
self.assertEqual(Vector(1.0, 1.0, 2.0).normalize().get_length(),1.0)
class ColorTest(unittest.TestCase):
def test_init_default(self):
color = Color()
self.assertEqual(color.x, 0.0)
self.assertEqual(color.y, 0.0)
self.assertEqual(color.z, 0.0)
def test_init(self):
color = Color(1.0, 2.0, 3.0)
self.assertEqual(color.x, 1.0)
self.assertEqual(color.y, 2.0)
self.assertEqual(color.z, 3.0)
class RayTest(unittest.TestCase):
def test_init(self):
origin = Vector(1.0, 1.0, 1.0)
direction = Vector(1.0, 2.0, 3.0)
ray = Ray(origin, direction)
self.assertEqual(ray.origin, origin)
self.assertEqual(ray.direction, direction)
class SphereTest(unittest.TestCase):
def test_intersect(self):
sphere = Sphere(1.0, Vector(0.0, 0.0, 0.0), Color(), Color(1.0, 1.0, 1.0), 1)
ray = Ray(Vector(10.0, 0.0, 0.0), Vector(-1.0, 0.0, 0.0))
self.assertEqual(sphere.intersect(ray), 9.0)
class IntersectTest(unittest.TestCase):
def test_get_intersect_obj(self):
spheres = [
Sphere(1.0, Vector(0.0, 10.0, 0.0), Color(), Color(1.0, 1.0, 1.0), 1),
Sphere(1.0, Vector(0.0, 0.0, 0.0), Color(), Color(1.0, 1.0, 1.0), 1),
]
ray = Ray(Vector(10.0, 0.0, 0.0), Vector(-1.0, 0.0, 0.0))
self.assertEqual(simple_path_tracer.get_intersect_obj(spheres, ray), (1, 9.0))
ray2 = Ray(Vector(10.0, 0.0, 0.0), Vector(1.0, 0.0, 0.0))
self.assertEqual(simple_path_tracer.get_intersect_obj(spheres, ray2), (-1, 1E6))