-
Notifications
You must be signed in to change notification settings - Fork 0
/
hittablelist.py
59 lines (43 loc) · 1.68 KB
/
hittablelist.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
from functools import cmp_to_key
from hittable import *
class HittableList(Hittable):
def __init__(self) -> None:
self.objects = []
def sort(self, cmp: 'function') -> None:
self.objects = sorted(self.objects, key=cmp_to_key(cmp))
def clear(self) -> None:
self.objects.clear()
def add(self, obj: Hittable) -> None:
self.objects.append(obj)
def hit(self, r: Ray, t_min: float, t_max: float) -> tuple[bool, HitRecord]:
rec = HitRecord()
hit_anything = False
closest_so_far = t_max
for obj in self.objects:
has_hit, temp_rec = obj.hit(r, t_min, closest_so_far)
if has_hit:
hit_anything = True
closest_so_far = temp_rec.t
# Set rec to temp rec
rec.p = temp_rec.p
rec.normal = temp_rec.normal
rec.t = temp_rec.t
rec.u = temp_rec.u
rec.v = temp_rec.v
rec.front_face = temp_rec.front_face
rec.material = temp_rec.material
return hit_anything, rec
def bounding_box(self, _time0: float, _time1: float) -> tuple[bool, AABB]:
if len(self.objects) == 0:
return False, None
first_box = True
for obj in self.objects:
b, temp_box = obj.bounding_box(_time0, _time1)
if not b:
return False
if first_box:
output_box = temp_box
else:
output_box = surrounding_box(output_box, temp_box)
first_box = False
return True