-
Notifications
You must be signed in to change notification settings - Fork 0
/
hittable.py
280 lines (203 loc) · 8.34 KB
/
hittable.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import math
from abc import ABC, abstractmethod
import types
import aabb
import util
import vector
class HitRecord:
def __init__(self, position: vector.Vec3, normal: vector.Vec3, t: float, material_value):
self.position = position
self.normal = normal
self.t = t
self.front_face = True
self.material = material_value
def set_face_normal(self, ray: vector.Ray, outward_normal: vector.Vec3):
self.front_face = vector.dot(ray.direction, outward_normal) < 0
if self.front_face:
self.normal = outward_normal
else:
self.normal = -outward_normal
class Hittable(ABC):
@abstractmethod
def hit(self, ray: vector.Ray, t_min: float, t_max: float):
pass
@abstractmethod
def bounding_box(self, time0: float, time1: float):
pass
class HittableList(Hittable):
def __init__(self):
self.objects = []
def bounding_box(self, time0: float, time1: float):
if len(self.objects) == 0:
return False,None
first_box = True
temp_box = aabb.AABB()
for obj in self.objects:
ret, temp_box = obj.bounding_box(time0, time1)
if not ret:
return False, None
if first_box:
output_box = temp_box
else:
ret, output_box = aabb.surrounding_box(output_box, temp_box)
first_box = False
return True, output_box
def add(self, o):
self.objects.append(o)
def hit(self, ray: vector.Ray, t_min: float, t_max: float):
rec = HitRecord(None, None, None, None)
hit_anything = False
closest_so_far = t_max
for o in self.objects:
didhit, temp_rec = o.hit(ray, t_min, closest_so_far)
if didhit:
hit_anything = True
closest_so_far = temp_rec.t
rec = temp_rec
return hit_anything, rec
def box_x_compare(a: Hittable):
return box_compare(a,0)
def box_y_compare(a: Hittable):
return box_compare(a,1)
def box_z_compare(a: Hittable):
return box_compare(a,2)
def box_compare(a: Hittable, axis: int):
a_true, box_a = a.bounding_box(0,0)
if not a_true:
print("ERROR, no box in bvh_node __init__")
if axis == 0:
return box_a.min.x
if axis == 1:
return box_a.min.y
if axis == 2:
return box_a.min.z
class bvh_node(Hittable):
def __init__(self, hittable_list, time0: float, time1: float):
objects = hittable_list
axis = util.random_int(0,2)
if axis == 0:
comparator = box_x_compare
if axis == 1:
comparator = box_y_compare
if axis == 2:
comparator = box_z_compare
start = 0
end = len(objects)
object_span = end - start
if object_span == 1:
self.left = self.right = objects[start]
elif object_span == 2:
if comparator(objects[start]) < comparator(objects[start+1]):
self.left = objects[start]
self.right = objects[start+1]
else:
self.left = objects[start+1]
self.right = objects[start]
else:
objs = objects[start: end]
objs.sort(key=comparator)
mid = start + object_span//2
self.left = bvh_node(objs[start: mid], time0, time1)
self.right = bvh_node(objs[mid: end], time0, time1)
pass
def bounding_box(self, time0: float, time1: float):
return True, self.bounding_box
def hit(self, ray: vector.Ray, t_min: float, t_max: float):
hit_left, left_hit_record = self.left.hit(ray, t_min, t_max)
hit_right, right_hit_record = self.right.hit(ray, t_min, t_max)
# both report a hit, return the one that is closer
if hit_left and hit_right:
if left_hit_record.t < right_hit_record.t:
return hit_left, left_hit_record
else:
return hit_right, right_hit_record
if hit_left:
return hit_left, left_hit_record
if hit_right:
return hit_right, right_hit_record
rec = HitRecord(None, None, None, None)
return False, rec
class Sphere(Hittable):
def __init__(self, center: vector.Vec3, radius: float, thematerial):
self.center = center
self.radius = radius
self.m = thematerial
def bounding_box(self, time0: float, time1: float):
output_box = aabb.AABB(self.center - vector.Vec3(self.radius, self.radius, self.radius),
self.center + vector.Vec3(self.radius, self.radius, self.radius))
return True, output_box
def hit(self, ray: vector.Ray, t_min: float, t_max: float):
oc = ray.origin - self.center
a = ray.direction.length_squared()
half_b = vector.dot(oc, ray.direction)
c = oc.length_squared() - self.radius*self.radius
discriminant = half_b*half_b - a*c
if discriminant > 0:
root = math.sqrt(discriminant)
temp = (-half_b - root)/a
if t_min < temp < t_max:
hit_rec = HitRecord(None, None, None, None)
hit_rec.t = temp
hit_rec.position = ray.at(hit_rec.t)
hit_rec.material = self.m
outward_normal = (hit_rec.position - self.center).divide(self.radius)
hit_rec.set_face_normal(ray, outward_normal)
return True, hit_rec
temp = (-half_b + root) / a
if t_min < temp < t_max:
hit_rec = HitRecord(None, None, None, None)
hit_rec.t = temp
hit_rec.position = ray.at(hit_rec.t)
hit_rec.material = self.m
outward_normal = (hit_rec.position - self.center).divide(self.radius)
hit_rec.set_face_normal(ray, outward_normal)
return True, hit_rec
return False, None
class MovingSphere(Hittable):
def __init__(self, center0: vector.Vec3, center1: vector.Vec3, time0: float, time1: float, radius: float, thematerial):
self.center0 = center0
self.center1 = center1
self.radius = radius
self.m = thematerial
self.time0 = time0
self.time1 = time1
def bounding_box(self, time0: float, time1: float):
center0 = self.center(time0)
center1 = self.center(time1)
output_box0 = aabb.AABB(center0 - vector.Vec3(self.radius, self.radius, self.radius),
center0 + vector.Vec3(self.radius, self.radius, self.radius))
output_box1 = aabb.AABB(center1 - vector.Vec3(self.radius, self.radius, self.radius),
center1 + vector.Vec3(self.radius, self.radius, self.radius))
output_box = aabb.surrounding_box(output_box0, output_box1)
return True, output_box
def center(self, time: float):
diff = (self.center1 - self.center0)
diff = diff.times( ((time - self.time0) / (self.time1 - self.time0)) )
return self.center0 + diff
def hit(self, ray: vector.Ray, t_min: float, t_max: float):
oc = ray.origin - self.center(ray.time)
a = ray.direction.length_squared()
half_b = vector.dot(oc, ray.direction)
c = oc.length_squared() - self.radius*self.radius
discriminant = half_b*half_b - a*c
if discriminant > 0:
root = math.sqrt(discriminant)
temp = (-half_b - root)/a
if t_min < temp < t_max:
hit_rec = HitRecord(None, None, None, None)
hit_rec.t = temp
hit_rec.position = ray.at(hit_rec.t)
hit_rec.material = self.m
outward_normal = (hit_rec.position - self.center(ray.time)).divide(self.radius)
hit_rec.set_face_normal(ray, outward_normal)
return True, hit_rec
temp = (-half_b + root) / a
if t_min < temp < t_max:
hit_rec = HitRecord(None, None, None, None)
hit_rec.t = temp
hit_rec.position = ray.at(hit_rec.t)
hit_rec.material = self.m
outward_normal = (hit_rec.position - self.center(ray.time)).divide(self.radius)
hit_rec.set_face_normal(ray, outward_normal)
return True, hit_rec
return False, None