-
Notifications
You must be signed in to change notification settings - Fork 0
/
RT_object.py
249 lines (197 loc) · 7.9 KB
/
RT_object.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
# object class
import RT_ray as rtr
import RT_utility as rtu
import RT_material as rtm
import RT_texture as rtt
import math
class Object:
def __init__(self) -> None:
pass
def intersect(self, rRay):
pass
def intersect(self, rRay, tmin, tmax):
pass
def intersect(self, rRay, cInterval):
pass
class Sphere(Object):
def __init__(self, vCenter, fRadius, mMat=None) -> None:
super().__init__()
self.center = vCenter
self.radius = fRadius
self.material = mMat
def add_material(self, mMat):
self.material = mMat
def printInfo(self):
self.center.printout()
def intersect(self, rRay):
oc = rRay.getOrigin() - self.center
a = rtu.Vec3.dot_product(rRay.getDirection(), rRay.getDirection())
b = 2.0 * rtu.Vec3.dot_product(oc, rRay.getDirection())
c = rtu.Vec3.dot_product(oc, oc) - self.radius*self.radius
discriminant = b*b - 4*a*c
if discriminant < 0:
return -1.0
else:
return (-b -math.sqrt(discriminant)) / (2.0*a)
def intersect(self, rRay, tmin, tmax):
if tmin < 0:
return None
oc = rRay.getOrigin() - self.center
a = rRay.getDirection().len_squared()
half_b = rtu.Vec3.dot_product(oc, rRay.getDirection())
c = oc.len_squared() - self.radius*self.radius
discriminant = half_b*half_b - a*c
if discriminant < 0:
return None
sqrt_disc = math.sqrt(discriminant)
root = (-half_b - sqrt_disc) / a
if root <= tmin or root >= tmax:
root = (-half_b + sqrt_disc) / a
if root <= tmin or root >= tmax:
return None
hit_t = root
hit_point = rRay.at(root)
hit_normal = (hit_point - self.center) / self.radius
hinfo = rtu.Hitinfo(hit_point, hit_normal, hit_t, self.material)
hinfo.set_face_normal(rRay, hit_normal)
return hinfo
# main intersection calling
def intersect(self, rRay, cInterval):
oc = rRay.getOrigin() - self.center
a = rRay.getDirection().len_squared()
half_b = rtu.Vec3.dot_product(oc, rRay.getDirection())
c = oc.len_squared() - self.radius*self.radius
discriminant = half_b*half_b - a*c
if discriminant < 0:
return None
sqrt_disc = math.sqrt(discriminant)
root = (-half_b - sqrt_disc) / a
if not cInterval.surrounds(root):
root = (-half_b + sqrt_disc) / a
if not cInterval.surrounds(root):
return None
hit_t = root
hit_point = rRay.at(root)
hit_normal = (hit_point - self.center) / self.radius
hinfo = rtu.Hitinfo(hit_point, hit_normal, hit_t, self.material)
hinfo.set_face_normal(rRay, hit_normal)
return hinfo
# Ax + By + Cz = D
class Quad(Object):
def __init__(self, vQ, vU, vV, mMat=None) -> None:
super().__init__()
self.Qpoint = vQ
self.Uvec = vU
self.Vvec = vV
self.material = mMat
self.uxv = rtu.Vec3.cross_product(self.Uvec, self.Vvec)
self.normal = rtu.Vec3.unit_vector(self.uxv)
self.D = rtu.Vec3.dot_product(self.normal, self.Qpoint)
self.Wvec = self.uxv / rtu.Vec3.dot_product(self.uxv, self.uxv)
def add_material(self, mMat):
self.material = mMat
def intersect(self, rRay, cInterval):
# for aabb method
# if not self.is_in_boundary(rRay, cInterval):
# return None
denom = rtu.Vec3.dot_product(self.normal, rRay.getDirection())
# if parallel
if rtu.Interval.near_zero(denom):
return None
# if it is hit.
t = (self.D - rtu.Vec3.dot_product(self.normal, rRay.getOrigin())) / denom
if not cInterval.contains(t):
return None
hit_t = t
hit_point = rRay.at(t)
hit_normal = self.normal
# if not self.is_in_plane(hit_point):
# return None
# determine if the intersection point lies on the quad's plane.
planar_hit = hit_point - self.Qpoint
alpha = rtu.Vec3.dot_product(self.Wvec, rtu.Vec3.cross_product(planar_hit, self.Vvec))
beta = rtu.Vec3.dot_product(self.Wvec, rtu.Vec3.cross_product(self.Uvec, planar_hit))
if self.is_interior(alpha, beta) is None:
return None
hinfo = rtu.Hitinfo(hit_point, hit_normal, hit_t, self.material)
hinfo.set_face_normal(rRay, hit_normal)
return hinfo
def is_interior(self, fa, fb):
delta = 0
if (fa<delta) or (1.0<fa) or (fb<delta) or (1.0<fb):
return None
return rtt.TextureCoord(fa, fb)
# check if the hit ray is in the defined plane.
def is_in_plane(self, vHitPoint):
box0 = self.Qpoint
box1 = self.Qpoint + self.Uvec + self.Vvec
x = rtu.Interval(min(box0.x(), box1.x()), max(box0.x(), box1.x()))
y = rtu.Interval(min(box0.y(), box1.y()), max(box0.y(), box1.y()))
z = rtu.Interval(min(box0.z(), box1.z()), max(box0.z(), box1.z()))
delta = 1e-08
if x.size() < delta:
padding = delta/2.0
x = rtu.Interval(x.min_val-padding, x.max_val+padding)
if y.size() < delta:
padding = delta/2.0
y = rtu.Interval(y.min_val-padding, y.max_val+padding)
if z.size() < delta:
padding = delta/2.0
z = rtu.Interval(z.min_val-padding, z.max_val+padding)
if not x.contains(vHitPoint.x()):
return False
if not y.contains(vHitPoint.y()):
return False
if not z.contains(vHitPoint.z()):
return False
return True
# an aabb intersection method
def is_in_boundary(self, rRay, iRayt):
box0 = self.Qpoint
box1 = self.Qpoint + self.Uvec + self.Vvec
x = rtu.Interval(min(box0.x(), box1.x()), max(box0.x(), box1.x()))
y = rtu.Interval(min(box0.y(), box1.y()), max(box0.y(), box1.y()))
z = rtu.Interval(min(box0.z(), box1.z()), max(box0.z(), box1.z()))
delta = 1e-08
if x.size() < delta:
padding = delta/2.0
x = rtu.Interval(x.min_val-padding, x.max_val+padding)
if y.size() < delta:
padding = delta/2.0
y = rtu.Interval(y.min_val-padding, y.max_val+padding)
if z.size() < delta:
padding = delta/2.0
z = rtu.Interval(z.min_val-padding, z.max_val+padding)
# X axis boundary
ftmin = (x.min_val - rRay.getOrigin().x())/rRay.getDirection().x()
ftmax = (x.max_val - rRay.getOrigin().x())/rRay.getDirection().x()
t0 = min(ftmin, ftmax)
t1 = max(ftmin, ftmax)
iRayt.min_val = max(t0, iRayt.min_val)
iRayt.max_val = min(t1, iRayt.max_val)
if iRayt.max_val <= iRayt.min_val:
return False
# Y axis boundary
ftmin = (y.min_val - rRay.getOrigin().y())/rRay.getDirection().y()
ftmax = (y.max_val - rRay.getOrigin().y())/rRay.getDirection().y()
t0 = min(ftmin, ftmax)
t1 = max(ftmin, ftmax)
iRayt.min_val = max(t0, iRayt.min_val)
iRayt.max_val = min(t1, iRayt.max_val)
if iRayt.max_val <= iRayt.min_val:
return False
# Z axis boundary
ftmin = (z.min_val - rRay.getOrigin().z())/rRay.getDirection().z()
ftmax = (z.max_val - rRay.getOrigin().z())/rRay.getDirection().z()
t0 = min(ftmin, ftmax)
t1 = max(ftmin, ftmax)
iRayt.min_val = max(t0, iRayt.min_val)
iRayt.max_val = min(t1, iRayt.max_val)
if iRayt.max_val <= iRayt.min_val:
return False
return True
class Triangle(Object):
def __init__(self) -> None:
super().__init__()
def intersect(self, rRay, cInterval):
return super().intersect(rRay, cInterval)