-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathShape.py
More file actions
204 lines (156 loc) · 5.89 KB
/
Shape.py
File metadata and controls
204 lines (156 loc) · 5.89 KB
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
import math
from Utility import Point
class Shape:
def get_material(self):
pass
def hit(self, ray):
pass
def shadow_hit(self, ray):
pass
class Sphere(Shape):
def __init__(self, center, radius, material, block_light):
self.center = center
self.radius = radius
self.material = material
self.block_light = block_light
def hit(self, ray):
# (p-c)*(p-c)-r^2=0
# o+td=c
# (d*d)*t^2+[2(o-c)*d]*t+(o-c)*(o-c)-r^2=0
temp = ray.origin - self.center
a = ray.direction * ray.direction
b = 2.0 * temp * ray.direction
c = temp * temp - self.radius * self.radius
discriminant = b * b - 4.0 * a * c
if discriminant < 0.0:
return False
else:
e = math.sqrt(discriminant)
t = (-b - e) / (2.0 * a)
if t > 0.001:
self.t = t
hit_point = ray.origin + (self.t * ray.direction)
self.hit_point_normal = (hit_point - self.center) / self.radius
# v2o = Vector(0.0, 0.0, 0.0) - self.center
# p2o = hit_point + v2o
# self.local_hit_point = p2o + (self.radius * (-1.0 * self.hit_point_normal))
# self.local_hit_point = hit_point + (self.radius * (-1.0 * self.hit_point_normal))
# print v2o, p2o, self.radius, self.hit_point_normal, self.local_hit_point
self.local_hit_point = Point(0.0, 0.0, 0.0) + self.hit_point_normal
return True
e = math.sqrt(discriminant)
t = (-b + e) / (2.0 * a)
if t > 0.001:
self.t = t
hit_point = ray.origin + (self.t * ray.direction)
self.hit_point_normal = (hit_point - self.center) / self.radius
v2o = Vector(0.0, 0.0, 0.0) - self.center
p2o = hit_point + v2o
self.local_hit_point = p2o + (self.radius * (-1.0 * self.hit_point_normal))
# self.local_hit_point = hit_point + (self.radius * (-1.0 * self.hit_point_normal))
self.local_hit_point = Point(0.0, 0.0, 0.0) + self.hit_point_normal
return True
def shadow_hit(self, ray):
if not self.block_light:
return False
temp = ray.origin - self.center
a = ray.direction * ray.direction
b = 2.0 * temp * ray.direction
c = temp * temp - self.radius * self.radius
discriminant = b * b - 4.0 * a * c
if discriminant < 0.0:
return False
else:
e = math.sqrt(discriminant)
t = (-b - e) / (2.0 * a)
if t > 0.001:
self.shadow_t = t
return True
e = math.sqrt(discriminant)
t = (-b + e) / (2.0 * a)
if t > 0.001:
self.shadow_t = t
return True
class Plane:
def __init__(self, center, normal, material, block_light):
self.center = center
self.normal = normal
self.material = material
self.block_light = block_light
def hit(self, ray):
# (p - a) * n =0
# (o + td -a) * n = 0
# t = (a - o) * n / (d * n)
temp = ray.direction * self.normal
if temp != 0.0:
t = (self.center -ray.origin) * self.normal / temp
else:
t = 0.0
if t > 0.001:
self.t = t
hit_point = ray.origin + (self.t * ray.direction)
self.hit_point_normal = self.normal
self.local_hit_point = hit_point
return True
return False
def shadow_hit(self, ray):
if not self.block_light:
return False
t = (self.center - ray.origin) * self.normal / (ray.direction * (self.normal))
if t > 0.001:
self.shadow_t = t
return True
return False
class Rectangle(Shape):
def __init__(self, p0, a, b, material, block_light, sampler = None):
self.p0 = p0
self.a = a
self.b = b
self.material = material
self.sampler = sampler
self.block_light = block_light
normal = a.cross(b)
self.normal = normal.normalize()
area = a.length() * b.length()
self.inv_area = 1.0 / area
def hit(self, ray):
t = (self.p0 - ray.origin) * self.normal / (ray.direction * self.normal)
if t <= 0.001:
return False
hit_point = ray.origin + t * ray.direction
d = hit_point - self.p0
ddota = d * self.a
if ddota < 0.0 or ddota > self.a.length() * self.a.length():
return False
ddotb = d * self.b
if ddotb < 0.0 or ddotb > self.b.length() * self.b.length():
return False
self.t = t
self.hit_point_normal = self.normal
self.local_hit_point = hit_point
return True
def sample(self):
sample = self.sampler.sample_unit_square()
return (self.p0 + sample.x * self.a + sample.y * self.b)
def pdf(self):
return self.inv_area
def shadow_hit(self, ray):
if not self.block_light:
return False
temp = ray.direction * self.normal
if temp != 0.0:
t = (self.p0 - ray.origin) * self.normal / (ray.direction * self.normal)
else:
t = 0.0
if t <= 0.001:
return False
hit_point = ray.origin + t * ray.direction
d = hit_point - self.p0
ddota = d * self.a
if ddota < 0.0 or ddota > self.a.length() * self.a.length():
return False
ddotb = d * self.b
if ddotb < 0.0 or ddotb > self.b.length() * self.b.length():
return False
self.shadow_t = t
return True