forked from B7722/Sweeping-monks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple+cube.py
More file actions
307 lines (263 loc) · 10.1 KB
/
simple+cube.py
File metadata and controls
307 lines (263 loc) · 10.1 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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import numpy as np
import matplotlib.pyplot as plt
w = 400
h = 300
def normalize(x):
x /= np.linalg.norm(x)
return x
def add_cube(position, radius_X, radius_Y, radius_Z, color):
return dict(type='cube', position=np.array(position), radius_X=np.array(radius_X), radius_Y=np.array(radius_Y),
radius_Z=np.array(radius_Z), color=np.array(color), reflection=.5)
def intersect_cube(O, D, C, RX, RY, RZ):
# Return the distance from O to the intersection of the ray (O, D) with the
# cube (C, RX, RY, RZ), or +inf if there is no intersection.
# O and C are 3D points, D is a normalized vector, RX, RY and RZ are ranged vectors.
min_d = np.inf
X = np.linalg.norm(RX)
Y = np.linalg.norm(RY)
Z = np.linalg.norm(RZ)
denom = np.dot(D,RZ)
d = np.dot(C - O, RZ) / denom
point = O + d*D
vex = point - C
if 0 < np.dot(vex, RX)/X <= X and 0 < np.dot(vex, RY)/Y <= Y:
if d < np.inf:
min_point = O + min_d * D
if(np.linalg.norm(point-O)<np.linalg.norm(min_point-O)):
min_d = d
else:
min_d = d
denom = np.dot(D,RZ)
d = np.dot(C + RZ - O, RZ) / denom
point = O + d*D
vex = point - C
if 0 < np.dot(vex, RX)/X < X and 0 < np.dot(vex, RY)/Y < Y:
if d < np.inf:
min_point = O + min_d * D
if(np.linalg.norm(point-O)<np.linalg.norm(min_point-O)):
min_d = d
else:
min_d = d
denom = np.dot(D, RY)
d = np.dot(C - O, RY) / denom
point = O + d * D
vex = point - C
if 0 < np.dot(vex, RZ) / Z < Z and 0 < np.dot(vex, RX) / X < X:
if d < np.inf:
min_point = O + min_d * D
if(np.linalg.norm(point-O)<np.linalg.norm(min_point-O)):
min_d = d
else:
min_d = d
denom = np.dot(D, RY)
d = np.dot(C + RY - O, RY) / denom
point = O + d * D
vex = point - C
if 0 < np.dot(vex, RZ) / Z < Z and 0 < np.dot(vex, RX) / X < X:
if d < np.inf:
min_point = O + min_d * D
if(np.linalg.norm(point-O)<np.linalg.norm(min_point-O)):
min_d = d
else:
min_d = d
denom = np.dot(D, RX)
d = np.dot(C - O, RX) / denom
point = O + d * D
vex = point - C
if 0 < np.dot(vex, RZ) / Z < Z and 0 < np.dot(vex, RY) / Y < Y:
if d < np.inf:
min_point = O + min_d * D
if(np.linalg.norm(point-O)<np.linalg.norm(min_point-O)):
min_d = d
else:
min_d = d
denom = np.dot(D, RX)
d = np.dot(C + RX - O, RX) / denom
point = O + d * D
vex = point - C
if 0 < np.dot(vex, RZ) / Z < Z and 0 < np.dot(vex, RY) / Y < Y:
if d < np.inf:
min_point = O + min_d * D
if(np.linalg.norm(point-O)<np.linalg.norm(min_point-O)):
min_d = d
else:
min_d = d
return min_d
# fx = np.dot(D, RX)
# fy = np.dot(D, RY)
# fz = np.dot(D, RZ)
# x = np.dot(RX, RX)
# y = np.dot(RY, RY)
# z = np.dot(RZ, RZ)
# if (fx > 0):
# d = np.dot(C - O, RX) / fx
# if ((np.dot(C - O, RY) + (d * fy)) >= 0 and (np.dot(C - O, RY) + (d * fy)) <= y):
# if ((np.dot(C - O, RZ) + (d * fz)) >= 0 and (np.dot(C - O, RZ) + (d * fz)) <= z):
# return d
# elif (fx < 0):
# d = (x - np.dot(C - O, RX)) / fx
# if ((np.dot(C - O, RY) + (d * fy)) >= 0 and (np.dot(C - O, RY) + (d * fy)) <= y):
# if ((np.dot(C - O, RZ) + (d * fz)) >= 0 and (np.dot(C - O, RZ) + (d * fz)) <= z):
# return d
#
# if (fy > 0):
# d = np.dot(C - O, RY) / fy
# if ((np.dot(C - O, RX) + (d * fx)) >= 0 and (np.dot(C - O, RX) + (d * fx)) <= x):
# if ((np.dot(C - O, RZ) + (d * fz)) >= 0 and (np.dot(C - O, RZ) + (d * fz)) <= z):
# return d
# elif (fy < 0):
# d = (y - np.dot(C - O, RY)) / fy
# if ((np.dot(C - O, RX) + (d * fx)) >= 0 and (np.dot(C - O, RX) + (d * fx)) <= x):
# if ((np.dot(C - O, RZ) + (d * fz)) >= 0 and (np.dot(C - O, RZ) + (d * fz)) <= z):
# return d
# if (fz > 0):
# d = np.dot(C - O, RZ) / fz
# if ((np.dot(C - O, RY) + (d * fy)) >= 0 and (np.dot(C - O, RY) + (d * fy)) <= y):
# if ((np.dot(C - O, RX) + (d * fx)) >= 0 and (np.dot(C - O, RX) + (d * fx)) <= x):
# return d
# elif (fz < 0):
# d = (z - np.dot(C - O, RZ)) / fz
# if ((np.dot(C - O, RY) + (d * fy)) >= 0 and (np.dot(C - O, RY) + (d * fy)) <= y):
# if ((np.dot(C - O, RX) + (d * fx)) >= 0 and (np.dot(C - O, RX) + (d * fx)) <= x):
# return d
#
# return np.inf
def intersect_plane(O, D, P, N):
# Return the distance from O to the intersection of the ray (O, D) with the
# plane (P, N), or +inf if there is no intersection.
# O and P are 3D points, D and N (normal) are normalized vectors.
denom = np.dot(D, N)
if np.abs(denom) < 1e-6:
return np.inf
d = np.dot(P - O, N) / denom
if d < 0:
return np.inf
return d
def intersect_sphere(O, D, S, R):
# Return the distance from O to the intersection of the ray (O, D) with the
# sphere (S, R), or +inf if there is no intersection.
# O and S are 3D points, D (direction) is a normalized vector, R is a scalar.
a = np.dot(D, D)
OS = O - S
b = 2 * np.dot(D, OS)
c = np.dot(OS, OS) - R * R
disc = b * b - 4 * a * c
if disc > 0:
distSqrt = np.sqrt(disc)
q = (-b - distSqrt) / 2.0 if b < 0 else (-b + distSqrt) / 2.0
t0 = q / a
t1 = c / q
t0, t1 = min(t0, t1), max(t0, t1)
if t1 >= 0:
return t1 if t0 < 0 else t0
return np.inf
def intersect(O, D, obj):
if obj['type'] == 'plane':
return intersect_plane(O, D, obj['position'], obj['normal'])
elif obj['type'] == 'sphere':
return intersect_sphere(O, D, obj['position'], obj['radius'])
elif obj['type'] == 'cube':
return intersect_cube(O, D, obj['position'], obj['radius_X'], obj['radius_Y'], obj['radius_Z'])
def get_normal(obj, M):
# Find normal.
if obj['type'] == 'sphere':
N = normalize(M - obj['position'])
elif obj['type'] == 'plane':
N = obj['normal']
elif obj['type'] == 'cube':
N = normalize(M - obj['position'])
return N
def get_color(obj, M):
color = obj['color']
if not hasattr(color, '__len__'):
color = color(M)
return color
def trace_ray(rayO, rayD):
# Find first point of intersection with the scene.
t = np.inf
for i, obj in enumerate(scene):
t_obj = intersect(rayO, rayD, obj)
if t_obj < t:
t, obj_idx = t_obj, i
# Return None if the ray does not intersect any object.
if t == np.inf:
return
# Find the object.
obj = scene[obj_idx]
# Find the point of intersection on the object.
M = rayO + rayD * t
# Find properties of the object.
N = get_normal(obj, M)
color = get_color(obj, M)
toL = normalize(L - M)
toO = normalize(O - M)
# Shadow: find if the point is shadowed or not.
l = [intersect(M + N * .0001, toL, obj_sh)
for k, obj_sh in enumerate(scene) if k != obj_idx]
if l and min(l) < np.inf:
return
# Start computing the color.
col_ray = ambient
# Lambert shading (diffuse).
col_ray += obj.get('diffuse_c', diffuse_c) * max(np.dot(N, toL), 0) * color
# Blinn-Phong shading (specular).
col_ray += obj.get('specular_c', specular_c) * max(np.dot(N, normalize(toL + toO)), 0) ** specular_k * color_light
return obj, M, N, col_ray
def add_sphere(position, radius, color):
return dict(type='sphere', position=np.array(position),
radius=np.array(radius), color=np.array(color), reflection=.5)
def add_plane(position, normal):
return dict(type='plane', position=np.array(position),
normal=np.array(normal),
color=lambda M: (color_plane0
if (int(M[0] * 2) % 2) == (int(M[2] * 2) % 2) else color_plane1),
diffuse_c=.75, specular_c=.5, reflection=.25)
# List of objects.
color_plane0 = 1. * np.ones(3)
color_plane1 = 0. * np.ones(3)
scene = [add_cube([.75, -0.5, 1.], [0, 0.6, 0], [0.6, 0, 0], [0, 0, 0.6], [0., 0., 1.]),
add_sphere([-.75, .1, 2.25], .6, [.5, .223, .5]),
add_sphere([-2.75, .1, 3.5], .6, [1., .572, .184]),
add_plane([0., -.5, 0.], [0., 1., 0.]),
]
# Light position and color.
L = np.array([5., 5., -10.])
color_light = np.ones(3)
# Default light and material parameters.
ambient = .05
diffuse_c = 1.
specular_c = 1.
specular_k = 50
depth_max = 5 # Maximum number of light reflections.
col = np.zeros(3) # Current color.
O = np.array([0., 0.35, -1.]) # Camera.
Q = np.array([0., 0., 0.]) # Camera pointing to.
img = np.zeros((h, w, 3))
r = float(w) / h
# Screen coordinates: x0, y0, x1, y1.
S = (-1., -1. / r + .25, 1., 1. / r + .25)
# Loop through all pixels.
for i, x in enumerate(np.linspace(S[0], S[2], w)):
if i % 10 == 0:
print
i / float(w) * 100, "%"
for j, y in enumerate(np.linspace(S[1], S[3], h)):
col[:] = 0
Q[:2] = (x, y)
D = normalize(Q - O)
depth = 0
rayO, rayD = O, D
reflection = 1.
# Loop through initial and secondary rays.
while depth < depth_max:
traced = trace_ray(rayO, rayD)
if not traced:
break
obj, M, N, col_ray = traced
# Reflection: create a new ray.
rayO, rayD = M + N * .0001, normalize(rayD - 2 * np.dot(rayD, N) * N)
depth += 1
col += reflection * col_ray
reflection *= obj.get('reflection', 1.)
img[h - j - 1, i, :] = np.clip(col, 0, 1)
plt.imsave('fig.png', img)