-
Notifications
You must be signed in to change notification settings - Fork 0
/
RT_utility.py
211 lines (158 loc) · 5.48 KB
/
RT_utility.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
# Vec3
import math
import numpy as np
from PIL import Image as im
import sys
global infinity_number
global pi
infinity_number = sys.float_info.max
pi = 3.1415926535897932385
def random_double(min=0.0, max=1.0):
return np.random.uniform(min, max)
def linear_to_gamma(val, gammaVal):
return math.pow(val, 1.0/gammaVal)
class Vec3:
def __init__(self, e0=0.0, e1=0.0, e2=0.0) -> None:
self.e = [e0, e1, e2]
pass
def x(self):
return self.e[0]
def y(self):
return self.e[1]
def z(self):
return self.e[2]
def len_squared(self):
return self.e[0]*self.e[0] + self.e[1]*self.e[1] + self.e[2]*self.e[2]
def len(self):
return math.sqrt(self.len_squared())
def __truediv__(self, val):
return Vec3(self.e[0]/val, self.e[1]/val, self.e[2]/val)
def __add__(self, vec):
return Vec3(self.e[0] + vec.x(), self.e[1] + vec.y(), self.e[2] + vec.z())
def __sub__(self, vec):
return Vec3(self.e[0] - vec.x(), self.e[1] - vec.y(), self.e[2] - vec.z())
def __mul__(self, val):
return Vec3(self.e[0]*val, self.e[1]*val, self.e[2]*val)
def __neg__(self):
return Vec3(-self.e[0], -self.e[1], -self.e[2])
def printout(self):
print('{},{},{}'.format(self.e[0], self.e[1], self.e[2]))
def near_zero(self):
tol = 1e-8
return (math.fabs(self.e[0]) < tol) and (math.fabs(self.e[1]) < tol) and (math.fabs(self.e[2]) < tol)
@staticmethod
def unit_vector(v):
return v / v.len()
@staticmethod
def cross_product(u, v):
return Vec3(u.y()*v.z() - u.z()*v.y(),
u.z()*v.x() - u.x()*v.z(),
u.x()*v.y() - u.y()*v.x())
@staticmethod
def dot_product(u, v):
return u.x()*v.x() + u.y()*v.y() + u.z()*v.z()
@staticmethod
def random_vec3(minval=0.0, maxval=1.0):
return Vec3(random_double(minval, maxval), random_double(minval, maxval), random_double(minval, maxval))
@staticmethod
def random_vec3_in_unit_disk():
while True:
p = Vec3(random_double(-1,1), random_double(-1,1), 0)
if p.len_squared() < 1:
return p
@staticmethod
def random_vec3_in_unit_sphere():
while True:
p = Vec3.random_vec3(-1, 1)
if p.len_squared() < 1:
return p
@staticmethod
def random_vec3_unit():
return Vec3.unit_vector(Vec3.random_vec3_in_unit_sphere())
@staticmethod
def random_vec3_on_hemisphere(vNormal):
in_unit_sphere = Vec3.random_vec3_unit()
if Vec3.dot_product(in_unit_sphere, vNormal) > 0.0:
return in_unit_sphere
else:
return -in_unit_sphere
class Color(Vec3):
def __init__(self, e0=0, e1=0, e2=0) -> None:
super().__init__(e0, e1, e2)
def r(self):
return self.e[0]
def g(self):
return self.e[1]
def b(self):
return self.e[2]
def write_to_256(self):
return Color(int(self.e[0]*255), int(self.e[1]*255), int(self.e[2]*255))
def __truediv__(self, val):
return Color(self.e[0]/val, self.e[1]/val, self.e[2]/val)
def __add__(self, vec):
return Color(self.e[0] + vec.r(), self.e[1] + vec.g(), self.e[2] + vec.b())
def __sub__(self, vec):
return Color(self.e[0] - vec.r(), self.e[1] - vec.g(), self.e[2] - vec.b())
def __mul__(self, val):
if isinstance(val, Color):
return Color(self.e[0]*val.r(), self.e[1]*val.g(), self.e[2]*val.b())
return Color(self.e[0]*val, self.e[1]*val, self.e[2]*val)
def __neg__(self):
return Color(-self.e[0], -self.e[1], -self.e[2])
class Hitinfo:
def __init__(self, vP, vNormal, fT, mMat=None, tcTex=None) -> None:
self.point = vP
self.normal = vNormal
self.t = fT
self.front_face = True
self.mat = mMat
self.texture_uv = tcTex
pass
def set_face_normal(self, vRay, outwardNormal):
self.front_face = Vec3.dot_product(vRay.getDirection(), outwardNormal) < 0
if self.front_face:
self.normal = outwardNormal
else:
self.normal = -outwardNormal
pass
def getT(self):
return self.t
def getNormal(self):
return self.normal
def getP(self):
return self.point
def getMaterial(self):
return self.mat
def getTextureUV(self):
return self.texture_uv
class Scatterinfo:
def __init__(self, vRay, fAttenuation) -> None:
self.scattered_ray = vRay
self.attenuation_color = fAttenuation
class Interval:
def __init__(self, minval, maxval) -> None:
self.min_val = minval
self.max_val = maxval
pass
def contains(self, x):
return self.min_val <= x and x <= self.max_val
def surrounds(self, x):
return self.min_val < x and x < self.max_val
def clamp(self, x):
if x < self.min_val:
return self.min_val
if x > self.max_val:
return self.max_val
return x
def size(self):
return self.max_val - self.min_val
@staticmethod
def near_zero(x, fTol=1e-8):
tol = fTol
return math.fabs(x) < tol
@staticmethod
def Empty():
return Interval(+infinity_number, -infinity_number)
@staticmethod
def Universe():
return Interval(-infinity_number, +infinity_number)