-
Notifications
You must be signed in to change notification settings - Fork 0
/
hittable.py
130 lines (101 loc) · 4.2 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
from vec import *
from ray import *
from aabb import *
class HitRecord:
def __init__(self) -> None:
self.p = None
self.normal = None
self.material = None
self.t: float = 0
self.u: float = 0
self.v: float = 0
self.front_face: bool = False
def set_face_normal(self, r: Ray, outward_normal: V3) -> None:
self.front_face = vec_dot(r.d, outward_normal) < 0
if self.front_face:
self.normal = outward_normal
else:
self.normal = outward_normal.neg()
class Hittable:
def __init__(self) -> None:
pass
def hit(self, r: Ray, t_min: float, t_max: float) -> tuple[bool, HitRecord]:
pass
def bounding_box(self, _time0: float, _time1: float) -> tuple[bool, AABB]:
pass
class Translate(Hittable):
def __init__(self, h: Hittable, displacement: V3) -> None:
self.h = h
self.offset = displacement
def hit(self, r: Ray, t_min: float, t_max: float) -> tuple[bool, HitRecord]:
moved_r = Ray(vec_sub(r.o, self.offset), r.d, r.time)
has_hit, rec = self.h.hit(moved_r, t_min, t_max)
if not has_hit:
return False, None
rec.p = vec_add(rec.p, self.offset)
rec.set_face_normal(moved_r, rec.normal)
return True, rec
def bounding_box(self, _time0: float, _time1: float) -> tuple[bool, AABB]:
b, output_box = self.h.bounding_box(_time0, _time1)
if not b:
return False
return True, AABB(
vec_add(output_box.mini, self.offset),
vec_add(output_box.maxi, self.offset),
)
# BUG Rotation is absolutely in shambles
class RotateY(Hittable):
def __init__(self, h: Hittable, angle: float) -> None:
radians = math.radians(angle)
self.sin_theta = math.sin(radians)
self.cos_theta = math.cos(radians)
self.h = h
self.hasbox, self.bbox = self.h.bounding_box(0, 1)
minvec = V3(math.inf, math.inf, math.inf)
maxvec = V3(-math.inf, -math.inf, -math.inf)
for i in range(2):
for j in range(2):
for k in range(2):
x = i * self.bbox.maxi.x + (1 - i) * self.bbox.mini.x
y = j * self.bbox.maxi.y + (1 - j) * self.bbox.mini.y
z = k * self.bbox.maxi.z + (1 - k) * self.bbox.mini.z
newx = self.cos_theta*x + self.sin_theta*z;
newz = -self.sin_theta*x + self.cos_theta*z;
tester = V3(newx, y, newz)
minvec.x = min(minvec.x, tester.x)
maxvec.x = max(maxvec.x, tester.x)
minvec.y = min(minvec.y, tester.y)
maxvec.y = max(maxvec.y, tester.y)
minvec.z = min(minvec.z, tester.z)
maxvec.z = max(maxvec.z, tester.z)
self.bbox = AABB(minvec, maxvec)
def hit(self, r: Ray, t_min: float, t_max: float) -> tuple[bool, HitRecord]:
origin = V3(
self.cos_theta * r.o.x - self.sin_theta * r.o.z,
r.o.y,
self.sin_theta * r.o.x + self.cos_theta * r.o.z
)
direction = V3(
self.cos_theta * r.d.x - self.sin_theta * r.d.z,
r.d.y,
self.sin_theta * r.d.x + self.cos_theta * r.d.z
)
rotated_r = Ray(origin, direction, r.time)
has_hit, rec = self.h.hit(rotated_r, t_min, t_max)
if not has_hit:
return False, None
p = V3(
self.cos_theta * rec.p.x + self.sin_theta * rec.p.z,
rec.p.y,
-self.sin_theta * rec.p.x + self.cos_theta * rec.p.z
)
normal = V3(
self.cos_theta * rec.normal.x + self.sin_theta * rec.normal.z,
rec.normal.y,
-self.sin_theta * rec.normal.x + self.cos_theta * rec.normal.z
)
rec.p = p
rec.set_face_normal(rotated_r, normal)
return True, rec
def bounding_box(self, _time0: float, _time1: float) -> tuple[bool, AABB]:
return self.has_box, self.bbox