-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathtest_dda.py
493 lines (412 loc) · 13.5 KB
/
test_dda.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
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
import drjit as dr
from drjit.dda import dda, integrate, integrate_ref
from typing import Tuple, List
from dataclasses import dataclass
import pytest
import sys
@dataclass
class Voxel:
t: float
idx: Tuple[int, ...]
p0: Tuple[float, ...]
p1: Tuple[float, ...]
def dda_bruteforce(
ray_o: Tuple[float, ...],
ray_d: Tuple[float, ...],
ray_max: float = float("inf"),
grid_res: Tuple[int, ...] = (1, 1, 1),
grid_min: Tuple[float, ...] = (0, 0, 0),
grid_max: Tuple[float, ...] = (1, 1, 1),
) -> List[Voxel]:
"""
Brute-force DDA routine that enumerates all grid cells and computes
intersections with each one. Used in dda_check()
"""
import itertools
n = len(grid_res)
grid_res = tuple(reversed(grid_res))
grid_scale = tuple(grid_res[i] / (grid_max[i] - grid_min[i]) for i in range(n))
ray_o = tuple((ray_o[i] - grid_min[i]) * grid_scale[i] for i in range(n))
ray_d = tuple(ray_d[i] * grid_scale[i] for i in range(n))
result: list[Voxel] = []
for idx in itertools.product(*tuple(range(res) for res in grid_res)):
t_min, t_max = float("-inf"), float("inf")
valid = False
for i in range(n):
if ray_d[i] == 0:
if ray_o[i] < idx[i] or ray_o[i] > (idx[i] + 1):
valid = False
break
continue
t_min_i = (idx[i] - ray_o[i]) / ray_d[i]
t_max_i = (idx[i] + 1 - ray_o[i]) / ray_d[i]
t_min = max(t_min, min(t_min_i, t_max_i))
t_max = min(t_max, max(t_min_i, t_max_i))
valid = True
t_min = max(t_min, 0)
t_max = min(t_max, ray_max)
if t_min < t_max and valid:
result.append(
Voxel(
t_min,
tuple(reversed(idx)),
tuple(ray_o[i] + ray_d[i] * t_min - idx[i] for i in range(n)),
tuple(ray_o[i] + ray_d[i] * t_max - idx[i] for i in range(n)),
)
)
result.sort(key=lambda x: x.t)
return result
def dda_check(
ray_o: Tuple[float, ...],
ray_d: Tuple[float, ...],
ray_max: float = float("inf"),
grid_res: Tuple[int, ...] = (1, 1, 1),
grid_min: Tuple[float, ...] = (0, 0, 0),
grid_max: Tuple[float, ...] = (1, 1, 1),
) -> None:
"""
Compare the proper and brute force versions of DDA against each other.
"""
from drjit.scalar import Array3f, Array3u
ref = dda_bruteforce(
grid_res=grid_res,
grid_min=grid_min,
grid_max=grid_max,
ray_o=ray_o,
ray_d=ray_d,
ray_max=ray_max,
)
def dda_cb(state, idx, p0, p1, active):
if active:
state.append(Voxel(0, tuple(idx), tuple(p0), tuple(p1)))
return state, True
out = dda(
func=dda_cb,
state=[],
ray_o=Array3f(ray_o),
ray_d=Array3f(ray_d),
ray_max=ray_max,
grid_min=Array3f(grid_min),
grid_max=Array3f(grid_max),
grid_res=Array3u(grid_res),
active=True
)
assert len(ref) == len(out)
for i in range(len(ref)):
# print(f"ref[{i}]={ref[i]}")
# print(f"out[{i}]={out[i]}")
assert ref[i].idx == out[i].idx
assert ref[i].p0 == pytest.approx(out[i].p0, rel=1e-5, abs=1e-5)
assert ref[i].p1 == pytest.approx(out[i].p1, rel=1e-5, abs=1e-5)
@pytest.mark.parametrize("s", (-1, 1))
def test01_single_voxel_inside(s):
"""A ray starting and ending inside a single cell"""
dda_check(ray_o=(0.5, 0.5, 0.5), ray_d=(0.1 * s, 0.2 * s, 0.3 * s), ray_max=1.0)
@pytest.mark.parametrize("s", (-1, 1))
def test02_single_voxel_outside(s):
"""A ray starting and ending outside a single cell"""
dda_check(ray_o=(0, 0, -s * 1.1), ray_d=(0.1 * s, 0.2 * s, 0.3 * s))
@pytest.mark.parametrize("s", (-1, 1))
def test03_several_outside_1(s):
"""A ray starting outside of the bounding box traversing 6 cells."""
dda_check(
ray_o=(0, 0, -s * 1.1),
ray_d=(0.1 * s, 0.2 * s, 0.3 * s),
grid_res=(3, 4, 5),
grid_min=(-1, -1, -1),
grid_max=(1, 1, 1),
)
@pytest.mark.parametrize("s", (-1, 1))
@pytest.mark.parametrize("z", (0, 0.001))
def test04_several_outside_2(s, z):
"""
Variation of test03 with different scales, optionally
with a zero-valued direction component
"""
dda_check(
ray_o=(0, 0, -s * 1.1),
ray_d=(z, 0.2 * s, 0.3 * s),
grid_res=(9, 8, 1),
grid_min=(-1, -1, -1),
grid_max=(1, 1, 1),
)
@pytest.mark.parametrize("o", ((1e-10, 3.4, 4.5), (1e-10, 3, 4)))
@pytest.mark.parametrize("s", (-1, 1))
@pytest.mark.parametrize("maxt", (float('inf'), 3.5))
def test05_several_inout_2(s, o, maxt):
"""
Ray starting within the grid, optionally with a starting position
that is exactly on a grid cell boundary
"""
dda_check(
ray_o=o,
ray_d=(0, 0.25 * s, 0.3 * s),
grid_res=(9, 8, 1),
grid_min=(0, 0, 0),
grid_max=(1, 8, 9),
ray_max=maxt
)
def test06_invalid():
"""Invalid ray with a zero-valued ray direction"""
dda_check(
ray_o=(.5, .5, .5),
ray_d=(0, 0, 0),
grid_res=(3, 3, 3)
)
@pytest.mark.parametrize("s", (-1, 1))
def test07_diagonal(s):
"""Test a ray that goes through corners"""
dda_check(
ray_o=(-s, -s, -s),
ray_d=(s, s, s),
grid_res=(10, 10, 10),
grid_min=(-1, -1, -1),
grid_max=(1, 1, 1)
)
def check(t, rng, vol, n_samples=4, grid_min=None, grid_max=None, mode=None):
"""
Helper function to check the correctness of the analytic integration routines
"""
if grid_min is None:
grid_min = t(-1)
if grid_max is None:
grid_max = t(1)
ndim = vol.ndim
tv = dr.value_t(t)
p0 = t([tv(rng.next_float32()) for _ in range(ndim)])
p1 = t([tv(rng.next_float32()) for _ in range(ndim)])
grid_scale = grid_max - grid_min
p0 = p0 *grid_scale + grid_min
p1 = p1 *grid_scale + grid_min
val_ref = integrate_ref(
ray_o=p0,
ray_d=p1 - p0,
ray_max=tv(1),
grid_min=grid_min,
grid_max=grid_max,
vol=vol,
n_samples=n_samples
)
val_dda = integrate(
ray_o=p0,
ray_d=p1 - p0,
ray_max=tv(1),
grid_min=grid_min,
grid_max=grid_max,
vol=vol,
mode=mode
)
# print("----")
# print("Result: (ref/dda)")
# print(val_ref)
# print(val_dda)
assert dr.allclose(val_ref, val_dda)
configs = ('float, shape=(2, *), -complex, -float16', 'float, shape=(3, *), -float16')
configs_ad = ('float, shape=(2, *), diff, -complex, -float16', 'float, diff, shape=(3, *), -float16')
@pytest.test_arrays(*configs)
def test08_integrate_constant(t):
m = sys.modules[t.__module__]
ndim = dr.size_v(t)
vol = dr.tensor_t(t)(dr.value_t(t)(1 for _ in range(2**ndim)),
shape=(2,)*ndim)
rng = m.PCG32(16)
check(t, rng, vol)
@pytest.test_arrays(*configs)
def test09_integrate_constant_nonuniform(t):
m = sys.modules[t.__module__]
ndim = dr.size_v(t)
shape=(3, 2, 7)
grid_max=[2, 4, 3]
data = dr.value_t(t)([1]*dr.prod(shape[:ndim]))
vol = dr.tensor_t(t)(data, shape=shape[:ndim])
rng = m.PCG32(16)
check(t, rng, vol, mode='evaluated', grid_min=t(0), grid_max=t(grid_max[:ndim]))
@pytest.test_arrays(*configs)
def test10_integrate_gradient(t):
m = sys.modules[t.__module__]
ndim = dr.size_v(t)
tt = dr.tensor_t(t)
vol = dr.tensor_t(t)((0, 1)*(2**(ndim-1)), shape=(2,)*ndim)
rng = m.PCG32(16)
check(t, rng, vol)
@pytest.test_arrays(*configs)
def test11_integrate_random(t):
m = sys.modules[t.__module__]
ndim = dr.size_v(t)
res = 3
data = m.PCG32(res**ndim).next_float32()*2-1
vol = dr.tensor_t(t)(data, shape=(res,)*ndim)
rng = m.PCG32(16)
check(t, rng, vol, n_samples=512, grid_min=t(0),
grid_max=t(range(1, ndim+1)))
@pytest.test_arrays(*configs)
def test12_integrate_random_nonuniform(t):
m = sys.modules[t.__module__]
ndim = dr.size_v(t)
shape=(3, 2, 5)[:ndim]
data = m.PCG32(dr.prod(shape)).next_float32()*2-1
vol = dr.tensor_t(t)(data, shape=shape)
rng = m.PCG32(16)
check(t, rng, vol, n_samples=2048, grid_min=t(0),
grid_max=t(range(1, ndim+1)))
@pytest.test_arrays(*configs)
def test13_integrate_random(t):
m = sys.modules[t.__module__]
ndim = dr.size_v(t)
res = 3
data = m.PCG32(res**ndim).next_float32()*2-1
vol = dr.tensor_t(t)(data, shape=(res,)*ndim)
rng = m.PCG32(16)
check(t, rng, vol, n_samples=512, grid_min=t(0),
grid_max=t(range(1, ndim+1)))
def check_grad(t, rng, vol, diff, grad_val, n_samples=4, grid_min=None, grid_max=None, mode=None, rtol=None):
"""
Helper function to check the correctness of the analytic integration routines
"""
if grid_min is None:
grid_min = t(-1)
if grid_max is None:
grid_max = t(1)
ndim = vol.ndim
tv = dr.value_t(t)
p0 = t([tv(rng.next_float32()) for _ in range(ndim)])
p1 = t([tv(rng.next_float32()) for _ in range(ndim)])
grid_scale = grid_max - grid_min
p0 = p0 *grid_scale + grid_min
p1 = p1 *grid_scale + grid_min
if True:
dr.enable_grad(vol)
val_ref = integrate_ref(
ray_o=p0,
ray_d=p1 - p0,
ray_max=tv(1),
grid_min=grid_min,
grid_max=grid_max,
vol=vol,
n_samples=n_samples
)
if diff == 'fwd':
vol.grad = grad_val
grad_ref = dr.forward_to(val_ref)
dr.disable_grad(vol)
else:
val_ref.grad = grad_val
grad_ref = dr.backward_to(vol)
dr.disable_grad(vol)
if True:
dr.enable_grad(vol)
val_dda = integrate(
ray_o=p0,
ray_d=p1 - p0,
ray_max=tv(1),
grid_min=grid_min,
grid_max=grid_max,
vol=vol,
mode=mode
)
assert dr.allclose(val_ref, val_dda, rtol=rtol)
if diff == 'fwd':
vol.grad = grad_val
grad_dda = dr.forward_to(val_ref)
else:
val_dda.grad = grad_val
grad_dda = dr.backward_to(vol)
# print("----")
# print("Result: (ref/dda)")
# print(val_ref)
# print(val_dda)
# print("Grad: (ref/dda)")
# print(grad_ref)
# print(grad_dda)
assert dr.allclose(grad_ref, grad_dda, rtol=rtol)
@pytest.test_arrays(*configs_ad)
def test14_fwd_ad_constant(t):
m = sys.modules[t.__module__]
ndim = dr.size_v(t)
res = 3
data = m.PCG32(res**ndim).next_float32()*2-1
dr.eval(data)
vol = dr.tensor_t(t)(data, shape=(res,)*ndim)
rng = m.PCG32(16)
check_grad(t, rng, vol, n_samples=512, diff='fwd', grad_val=1)
@pytest.test_arrays(*configs_ad)
def test15_fwd_ad_random(t):
m = sys.modules[t.__module__]
ndim = dr.size_v(t)
res = 3
vol_rng = m.PCG32(res**ndim)
data = vol_rng.next_float32()*2-1
grad_val = vol_rng.next_float32()*2-1
dr.eval(data)
vol = dr.tensor_t(t)(data, shape=(res,)*ndim)
rng = m.PCG32(16)
check_grad(t, rng, vol, n_samples=512, diff='fwd', grad_val=grad_val)
@pytest.test_arrays(*configs_ad)
def test16_rev_ad_constant(t):
m = sys.modules[t.__module__]
ndim = dr.size_v(t)
res = 3
data = m.PCG32(res**ndim).next_float32()*2-1
dr.eval(data)
vol = dr.tensor_t(t)(data, shape=(res,)*ndim)
rng = m.PCG32(16)
check_grad(t, rng, vol, n_samples=1024, diff='rev', grad_val=1)
@pytest.test_arrays(*configs_ad)
def test17_rev_ad_random(t):
m = sys.modules[t.__module__]
ndim = dr.size_v(t)
res = 3
vol_rng = m.PCG32(res**ndim)
data = vol_rng.next_float32()*2-1
dr.eval(data)
vol = dr.tensor_t(t)(data, shape=(res,)*ndim)
rng = m.PCG32(16)
grad_val = rng.next_float32()*2-1
check_grad(t, rng, vol, n_samples=1024, diff='rev', rtol=1e-4, grad_val=grad_val)
@pytest.mark.parametrize("corner", range(8))
@pytest.mark.parametrize("direction", range(6))
def test18_edges(corner, direction):
"""Test a ray that goes along the edges of the grid"""
ray_o = [corner&4 != 0, corner&2 != 0, corner&1 != 0]
ray_o = [2 * v - 1 for v in ray_o]
ray_d = [0, 0, 0]
ray_d[direction//2] = 2 * (direction&1) - 1
dda_check(
ray_o = ray_o,
ray_d = ray_d,
grid_res = (3, 3, 3),
grid_min = (-1, -1, -1),
grid_max = (1, 1, 1)
)
@pytest.mark.parametrize("face", range(6))
@pytest.mark.parametrize("direction", range(2))
def test19_negative_zero_direction(face, direction):
"""Test a ray with a negative zero direction"""
ray_o = [-0.0, -0.0, -0.0]
ray_o[face//2] = 2 * (face&1) - 1
ray_d = [-0.0, -0.0, -0.0]
ray_d[face//2] = 2 * direction - 1
dda_check(
ray_o = ray_o,
ray_d = ray_d,
grid_res = (3, 3, 3),
grid_min = (-1, -1, -1),
grid_max = (1, 1, 1)
)
@pytest.mark.parametrize("face", range(6))
@pytest.mark.parametrize("direction", range(4))
def test20_zero_direction_no_hit(face, direction):
"""Test a ray with a zero direction that does not hit the grid"""
eps = 1e-6
ray_o = [0, 0, 0]
ray_o[face//2] = 2 * (face&1) - 1 + (2 * (face&1) - 1) * eps
ray_d = [0, 0, 0]
ray_d[(face//2 + 1) % 3] = 2 * (direction & 2 != 0) - 1
ray_d[(face//2 + 2) % 3] = 2 * (direction & 1 != 0) - 1
dda_check(
ray_o = ray_o,
ray_d = ray_d,
grid_res = (3, 3, 3),
grid_min = (-1, -1, -1),
grid_max = (1, 1, 1)
)