-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpathlengthdistribution.py
More file actions
434 lines (325 loc) · 10.7 KB
/
pathlengthdistribution.py
File metadata and controls
434 lines (325 loc) · 10.7 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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
import os
import numpy as np
from numpy import sqrt, sin, arcsin, cos, arccos, exp, pi, linspace, ceil
from plyfile import PlyData, PlyElement
def intersectBBox(ox, oy, oz, dx, dy, dz, sizex, sizey, sizez):
# Intersection code below is adapted from Suffern (2007) Listing 19.1
x0 = -0.5*sizex
x1 = 0.5*sizex
y0 = -0.5 * sizey
y1 = 0.5 * sizey
z0 = -1e-6
z1 = sizez
if dx == 0:
a = 1e6
else:
a = 1.0 / dx
if a >= 0:
tx_min = (x0 - ox) * a
tx_max = (x1 - ox) * a
else:
tx_min = (x1 - ox) * a
tx_max = (x0 - ox) * a
if dy == 0:
b = 1e6
else:
b = 1.0 / dy
if b >= 0:
ty_min = (y0 - oy) * b
ty_max = (y1 - oy) * b
else:
ty_min = (y1 - oy) * b
ty_max = (y0 - oy) * b
if dz == 0:
c = 1e6
else:
c = 1.0 / dz
if c >= 0:
tz_min = (z0 - oz) * c
tz_max = (z1 - oz) * c
else:
tz_min = (z1 - oz) * c
tz_max = (z0 - oz) * c
# find largest entering t value
if tx_min > ty_min:
t0 = tx_min
else:
t0 = ty_min
if tz_min > t0:
t0 = tz_min
# find smallest exiting t value
if tx_max < ty_max:
t1 = tx_max
else:
t1 = ty_max
if tz_max < t1:
t1 = tz_max
if t0 < t1 and t1 > 1e-6:
if t0 > 1e-6:
dr = t1-t0
else:
dr = t1
else:
dr = 0
xe = ox + t1 * dx
ye = oy + t1 * dy
ze = oz + t1 * dz
if dr == 0:
raise Exception('Shouldnt be here')
return dr, xe, ye, ze
def intersectEllipsoid(ox, oy, oz, dx, dy, dz, sizex, sizey, sizez):
tempx = ox/sizex
tempy = oy/sizey
tempz = (oz - 0.5)/sizez
dx = dx/sizex
dy = dy/sizey
dz = dz/sizez
a = dx*dx + dy*dy + dz*dz
b = 2.0 * (tempx*dx+tempy*dy+tempz*dz)
c = (tempx*tempx+tempy*tempy+tempz*tempz) - 0.5*0.5
disc = b * b - 4.0 * a * c
if disc < 0.0:
return 0
else:
e = sqrt(disc)
denom = 2.0 * a
t_small = (-b - e) / denom # smaller root
t_big = (-b + e) / denom # larger root
if t_small > 1e-6 or t_big > 1e-6:
dr = abs(t_big-t_small)
return dr
return 0
def intersectCylinder(ox, oy, oz, dx, dy, dz, sizex, sizey, sizez):
tempx = ox / sizex
tempy = oy / sizey
tempz = oz / sizez
dx = dx / sizex
dy = dy / sizey
dz = dz / sizez
#19.5.3 of Suffern
a = dx * dx + dy * dy
b = 2.0 * (tempx * dx + tempy * dy)
c = (tempx * tempx + tempy * tempy) - 0.5 * 0.5
disc = b*b - 4 * a * c
if disc >= 0 and a != 0:
t0 = (-b + sqrt(disc)) / (2 * a)
t1 = (-b - sqrt(disc)) / (2 * a)
else:
t0 = -1
t1 = -1
T = np.array([-1., -1., -1., -1.])
# check if hit side surface of cylinder
z0 = tempz + t0 * dz
if t0 > 1e-6 and 1 >= z0 >= 0:
T[0] = t0
z1 = tempz + t1 * dz
if t1 > 1e-6 and 1 >= z1 >= 0:
T[1] = t1
# check if it hits top of cylinder
t_top = (1 - tempz) / dz
hx = tempx + t_top * dx
hy = tempy + t_top * dy
if t_top > 0 and hx*hx + hy*hy <= 0.25: # hits top
T[2] = t_top
# check if it hits bottom of cylinder
t_bot = (0 - tempz) / dz
hx = tempx + t_bot * dx
hy = tempy + t_bot * dy
if t_bot > 0 and hx*hx + hy*hy <= 0.25: # hits bottom
T[3] = t_bot
t_int = T[T >= 0.0]
if np.size(t_int) == 0:
return 0
Tin = np.min(t_int)
Tout = np.max(T)
if 0 < Tin < Tout and Tout > 0:
dr = Tout-Tin
else:
dr = 0
return dr
def importPLY(file):
plydata = PlyData.read(file)
return plydata
def intersectTriangle(ox, oy, oz, dx, dy, dz, vertices):
kEpsilon = 1e-6
a = vertices[0][0] - vertices[1][0]
b = vertices[0][0] - vertices[2][0]
c = dx
d = vertices[0][0] - ox
e = vertices[0][1] - vertices[1][1]
f = vertices[0][1] - vertices[2][1]
g = dy
h = vertices[0][1] - oy
i = vertices[0][2] - vertices[1][2]
j = vertices[0][2] - vertices[2][2]
k = dz
l = vertices[0][2] - oz
m = f * k - g * j
n = h * k - g * l
p = f * l - h * j
q = g * i - e * k
s = e * j - f * i
if (a * m + b * q + c * s) == 0:
inv_denom = 1e8
else:
inv_denom = 1.0 / (a * m + b * q + c * s)
e1 = d * m - b * n - c * p
beta = e1 * inv_denom
if beta < 0.0:
return 0
r = e * l - h * i
e2 = a * n + d * q + c * r
gamma = e2 * inv_denom
if gamma < 0.0:
return 0
if beta + gamma > 1.0:
return 0
e3 = a * p - b * r + d * s
t = e3 * inv_denom
if t < kEpsilon:
return 0
# xe = ox + t * dx
# ye = oy + t * dy
# ze = oz + t * dz
return t
def intersectPolymesh(ox, oy, oz, dx, dy, dz, sizex, sizey, sizez, plydata):
ox = ox / sizex
oy = oy / sizey
oz = oz / sizez
dx = dx / sizex
dy = dy / sizey
dz = dz / sizez
vertices = plydata.elements[0].data
faces = plydata.elements[1].data
Nvertices = len(vertices)
Nfaces = len(faces)
face_verts = np.empty((3, 3))
T = [0, 0]
for face in range(0, Nfaces):
f = faces[face][0]
Nv = len(f)
if Nv != 3:
raise Exception('ERROR: only triangular elements are supported in PLY file geometry.')
for v in range(0, 3):
face_verts[0, v] = vertices[f[0]][v]
face_verts[1, v] = vertices[f[1]][v]
face_verts[2, v] = vertices[f[2]][v]
# THIS CODE IS FOR POLYGONS WITH MORE THAN 3 VERTICES. IT DOESN'T SEEM TO WORK
# for tri in range(0, Nv-2):
# for v in range(0, 3):
# face_verts[0, v] = vertices[f[0]][v]
# face_verts[1, v] = vertices[f[1+tri]][v]
# face_verts[2, v] = vertices[f[2+tri]][v]
# print(face_verts[0, :])
# ax.plot(face_verts[:, 0], face_verts[:, 1], face_verts[:, 2], '-')
# if tri > 0:
# ax.plot_trisurf(face_verts[:, 0], face_verts[:, 1], face_verts[:, 2])
# for i in range(0, 3):
# ax.plot(face_verts[i][0], face_verts[i][1], face_verts[i][2], '.')
# plt.show()
t = intersectTriangle(ox, oy, oz, dx, dy, dz, face_verts)
if t > 0:
if T[0] > 0:
T[1] = t
else:
T[0] = t
if T[0] > 0 and T[1] > 0:
return abs(T[0]-T[1])
else:
return 0
def pathlengths(shape, scale_x, scale_y, scale_z, ray_zenith, ray_azimuth, nrays, plyfile='', outputfile=''):
kEpsilon = 1e-5
N = int(ceil(sqrt(nrays)))
# Ray direction Cartesian unit vector
dx = sin(ray_zenith) * cos(ray_azimuth)
dy = sin(ray_zenith) * sin(ray_azimuth)
dz = cos(ray_zenith)
path_length = np.zeros(N*N)
plydata=[]
if shape == 'polymesh':
if len(plyfile) == 0:
raise Exception('Path to PLY file must be provided for polymesh intersection.')
# check that plyfile exists
elif not os.path.exists(plyfile):
raise Exception('PLY file does not exist.')
plydata = PlyData.read(plyfile)
if shape == 'polymesh':
vertices = plydata.elements[0].data
Nvertices = len(vertices)
bx_min = 1e6
bx_max = -1e6
by_min = 1e6
by_max = -1e6
z_min = 1e6
z_max = -1e6
for vert in range(0, Nvertices):
vx = vertices[vert][0] * scale_x
vy = vertices[vert][1] * scale_y
vz = vertices[vert][2] * scale_z
if vx < bx_min:
bx_min = vx
if vx > bx_max:
bx_max = vx
if vy < by_min:
by_min = vy
if vy > by_max:
by_max = vy
if vz < z_min:
z_min = vz
if vz > z_max:
z_max = vz
bbox_sizex = 2*max(abs(bx_max), abs(bx_min)) * (1.0 + kEpsilon)
bbox_sizey = 2*max(abs(by_max), abs(by_min)) * (1.0 + kEpsilon)
z_min = z_min
z_max = z_max * (1.0 + kEpsilon)
else:
bbox_sizex = scale_x * (1.0 + kEpsilon)
bbox_sizey = scale_y * (1.0 + kEpsilon)
z_min = 0
z_max = scale_z * (1.0 + kEpsilon)
sx = bbox_sizex/N
sy = bbox_sizey/N
# loop over all rays, which originate at the bottom of the box
for j in range(0, N):
for i in range(0, N):
# ray origin point
ox = -0.5*bbox_sizex + (i+0.5)*sx
oy = -0.5*bbox_sizey + (j+0.5)*sy
oz = z_min-kEpsilon
ze = 0
dr = 0
while ze <= z_max:
# Intersect shape
if shape == 'prism':
dr, _, _, _, = intersectBBox(ox, oy, oz, dx, dy, dz, scale_x, scale_y, scale_z)
elif shape == 'ellipsoid':
dr = intersectEllipsoid(ox, oy, oz, dx, dy, dz, scale_x, scale_y, scale_z)
elif shape == 'cylinder':
dr = intersectCylinder(ox, oy, oz, dx, dy, dz, scale_x, scale_y, scale_z)
elif shape == 'polymesh':
dr = intersectPolymesh(ox, oy, oz, dx, dy, dz, scale_x, scale_y, scale_z, plydata)
else:
raise Exception('Invalid shape argument.')
# Intersect bounding box walls
_, xe, ye, ze = intersectBBox(ox, oy, oz, dx, dy, dz, bbox_sizex, bbox_sizey, 1e6)
if ze <= z_max: # intersection below object height -> record path length and periodically cycle ray
path_length = np.append(path_length, dr)
ox = xe
oy = ye
oz = ze
if abs(ox-0.5*bbox_sizex) < kEpsilon: # hit +x wall
ox = ox - bbox_sizex + kEpsilon
elif abs(ox+0.5*bbox_sizex) < kEpsilon: # hit -x wall
ox = ox + bbox_sizex - kEpsilon
if abs(oy-0.5*bbox_sizey) < kEpsilon: # hit +y wall
oy = oy - bbox_sizey + kEpsilon
elif abs(oy + 0.5 * bbox_sizey) < kEpsilon: # hit -y wall
oy = oy + bbox_sizey - kEpsilon
path_length[i+j*N] = dr
if( outputfile != '' ):
np.savetxt(outputfile, path_length, delimiter=',')
return path_length[path_length > kEpsilon]
def pathlengthdistribution(shape, scale_x, scale_y, scale_z, ray_zenith, ray_azimuth, nrays, plyfile='', bins=10, normalize=True):
path_lengths = pathlengths(shape, scale_x, scale_y, scale_z, ray_zenith, ray_azimuth, nrays, plyfile)
hist, bin_edges = np.histogram(path_lengths, bins=bins, density=normalize)
return hist, bin_edges