warp-lang 1.0.2__py3-none-manylinux2014_x86_64.whl → 1.2.0__py3-none-manylinux2014_x86_64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of warp-lang might be problematic. Click here for more details.

Files changed (356) hide show
  1. warp/__init__.py +108 -97
  2. warp/__init__.pyi +1 -1
  3. warp/bin/warp-clang.so +0 -0
  4. warp/bin/warp.so +0 -0
  5. warp/build.py +88 -113
  6. warp/build_dll.py +383 -375
  7. warp/builtins.py +3693 -3354
  8. warp/codegen.py +2925 -2792
  9. warp/config.py +40 -36
  10. warp/constants.py +49 -45
  11. warp/context.py +5409 -5102
  12. warp/dlpack.py +442 -442
  13. warp/examples/__init__.py +16 -16
  14. warp/examples/assets/bear.usd +0 -0
  15. warp/examples/assets/bunny.usd +0 -0
  16. warp/examples/assets/cartpole.urdf +110 -110
  17. warp/examples/assets/crazyflie.usd +0 -0
  18. warp/examples/assets/cube.usd +0 -0
  19. warp/examples/assets/nv_ant.xml +92 -92
  20. warp/examples/assets/nv_humanoid.xml +183 -183
  21. warp/examples/assets/quadruped.urdf +267 -267
  22. warp/examples/assets/rocks.nvdb +0 -0
  23. warp/examples/assets/rocks.usd +0 -0
  24. warp/examples/assets/sphere.usd +0 -0
  25. warp/examples/benchmarks/benchmark_api.py +381 -383
  26. warp/examples/benchmarks/benchmark_cloth.py +278 -277
  27. warp/examples/benchmarks/benchmark_cloth_cupy.py +88 -88
  28. warp/examples/benchmarks/benchmark_cloth_jax.py +97 -100
  29. warp/examples/benchmarks/benchmark_cloth_numba.py +146 -142
  30. warp/examples/benchmarks/benchmark_cloth_numpy.py +77 -77
  31. warp/examples/benchmarks/benchmark_cloth_pytorch.py +86 -86
  32. warp/examples/benchmarks/benchmark_cloth_taichi.py +112 -112
  33. warp/examples/benchmarks/benchmark_cloth_warp.py +145 -146
  34. warp/examples/benchmarks/benchmark_launches.py +293 -295
  35. warp/examples/browse.py +29 -29
  36. warp/examples/core/example_dem.py +232 -219
  37. warp/examples/core/example_fluid.py +291 -267
  38. warp/examples/core/example_graph_capture.py +142 -126
  39. warp/examples/core/example_marching_cubes.py +186 -174
  40. warp/examples/core/example_mesh.py +172 -155
  41. warp/examples/core/example_mesh_intersect.py +203 -193
  42. warp/examples/core/example_nvdb.py +174 -170
  43. warp/examples/core/example_raycast.py +103 -90
  44. warp/examples/core/example_raymarch.py +197 -178
  45. warp/examples/core/example_render_opengl.py +183 -141
  46. warp/examples/core/example_sph.py +403 -387
  47. warp/examples/core/example_torch.py +219 -181
  48. warp/examples/core/example_wave.py +261 -248
  49. warp/examples/fem/bsr_utils.py +378 -380
  50. warp/examples/fem/example_apic_fluid.py +432 -389
  51. warp/examples/fem/example_burgers.py +262 -0
  52. warp/examples/fem/example_convection_diffusion.py +180 -168
  53. warp/examples/fem/example_convection_diffusion_dg.py +217 -209
  54. warp/examples/fem/example_deformed_geometry.py +175 -159
  55. warp/examples/fem/example_diffusion.py +199 -173
  56. warp/examples/fem/example_diffusion_3d.py +178 -152
  57. warp/examples/fem/example_diffusion_mgpu.py +219 -214
  58. warp/examples/fem/example_mixed_elasticity.py +242 -222
  59. warp/examples/fem/example_navier_stokes.py +257 -243
  60. warp/examples/fem/example_stokes.py +218 -192
  61. warp/examples/fem/example_stokes_transfer.py +263 -249
  62. warp/examples/fem/mesh_utils.py +133 -109
  63. warp/examples/fem/plot_utils.py +292 -287
  64. warp/examples/optim/example_bounce.py +258 -246
  65. warp/examples/optim/example_cloth_throw.py +220 -209
  66. warp/examples/optim/example_diffray.py +564 -536
  67. warp/examples/optim/example_drone.py +862 -835
  68. warp/examples/optim/example_inverse_kinematics.py +174 -168
  69. warp/examples/optim/example_inverse_kinematics_torch.py +183 -169
  70. warp/examples/optim/example_spring_cage.py +237 -231
  71. warp/examples/optim/example_trajectory.py +221 -199
  72. warp/examples/optim/example_walker.py +304 -293
  73. warp/examples/sim/example_cartpole.py +137 -129
  74. warp/examples/sim/example_cloth.py +194 -186
  75. warp/examples/sim/example_granular.py +122 -111
  76. warp/examples/sim/example_granular_collision_sdf.py +195 -186
  77. warp/examples/sim/example_jacobian_ik.py +234 -214
  78. warp/examples/sim/example_particle_chain.py +116 -105
  79. warp/examples/sim/example_quadruped.py +191 -180
  80. warp/examples/sim/example_rigid_chain.py +195 -187
  81. warp/examples/sim/example_rigid_contact.py +187 -177
  82. warp/examples/sim/example_rigid_force.py +125 -125
  83. warp/examples/sim/example_rigid_gyroscopic.py +107 -95
  84. warp/examples/sim/example_rigid_soft_contact.py +132 -122
  85. warp/examples/sim/example_soft_body.py +188 -177
  86. warp/fabric.py +337 -335
  87. warp/fem/__init__.py +61 -27
  88. warp/fem/cache.py +403 -388
  89. warp/fem/dirichlet.py +178 -179
  90. warp/fem/domain.py +262 -263
  91. warp/fem/field/__init__.py +100 -101
  92. warp/fem/field/field.py +148 -149
  93. warp/fem/field/nodal_field.py +298 -299
  94. warp/fem/field/restriction.py +22 -21
  95. warp/fem/field/test.py +180 -181
  96. warp/fem/field/trial.py +183 -183
  97. warp/fem/geometry/__init__.py +16 -19
  98. warp/fem/geometry/closest_point.py +69 -70
  99. warp/fem/geometry/deformed_geometry.py +270 -271
  100. warp/fem/geometry/element.py +748 -744
  101. warp/fem/geometry/geometry.py +184 -186
  102. warp/fem/geometry/grid_2d.py +380 -373
  103. warp/fem/geometry/grid_3d.py +437 -435
  104. warp/fem/geometry/hexmesh.py +953 -953
  105. warp/fem/geometry/nanogrid.py +455 -0
  106. warp/fem/geometry/partition.py +374 -376
  107. warp/fem/geometry/quadmesh_2d.py +532 -532
  108. warp/fem/geometry/tetmesh.py +840 -840
  109. warp/fem/geometry/trimesh_2d.py +577 -577
  110. warp/fem/integrate.py +1684 -1615
  111. warp/fem/operator.py +190 -191
  112. warp/fem/polynomial.py +214 -213
  113. warp/fem/quadrature/__init__.py +2 -2
  114. warp/fem/quadrature/pic_quadrature.py +243 -245
  115. warp/fem/quadrature/quadrature.py +295 -294
  116. warp/fem/space/__init__.py +179 -292
  117. warp/fem/space/basis_space.py +522 -489
  118. warp/fem/space/collocated_function_space.py +100 -105
  119. warp/fem/space/dof_mapper.py +236 -236
  120. warp/fem/space/function_space.py +148 -145
  121. warp/fem/space/grid_2d_function_space.py +148 -267
  122. warp/fem/space/grid_3d_function_space.py +167 -306
  123. warp/fem/space/hexmesh_function_space.py +253 -352
  124. warp/fem/space/nanogrid_function_space.py +202 -0
  125. warp/fem/space/partition.py +350 -350
  126. warp/fem/space/quadmesh_2d_function_space.py +261 -369
  127. warp/fem/space/restriction.py +161 -160
  128. warp/fem/space/shape/__init__.py +90 -15
  129. warp/fem/space/shape/cube_shape_function.py +728 -738
  130. warp/fem/space/shape/shape_function.py +102 -103
  131. warp/fem/space/shape/square_shape_function.py +611 -611
  132. warp/fem/space/shape/tet_shape_function.py +565 -567
  133. warp/fem/space/shape/triangle_shape_function.py +429 -429
  134. warp/fem/space/tetmesh_function_space.py +224 -292
  135. warp/fem/space/topology.py +297 -295
  136. warp/fem/space/trimesh_2d_function_space.py +153 -221
  137. warp/fem/types.py +77 -77
  138. warp/fem/utils.py +495 -495
  139. warp/jax.py +166 -141
  140. warp/jax_experimental.py +341 -339
  141. warp/native/array.h +1081 -1025
  142. warp/native/builtin.h +1603 -1560
  143. warp/native/bvh.cpp +402 -398
  144. warp/native/bvh.cu +533 -525
  145. warp/native/bvh.h +430 -429
  146. warp/native/clang/clang.cpp +496 -464
  147. warp/native/crt.cpp +42 -32
  148. warp/native/crt.h +352 -335
  149. warp/native/cuda_crt.h +1049 -1049
  150. warp/native/cuda_util.cpp +549 -540
  151. warp/native/cuda_util.h +288 -203
  152. warp/native/cutlass_gemm.cpp +34 -34
  153. warp/native/cutlass_gemm.cu +372 -372
  154. warp/native/error.cpp +66 -66
  155. warp/native/error.h +27 -27
  156. warp/native/exports.h +187 -0
  157. warp/native/fabric.h +228 -228
  158. warp/native/hashgrid.cpp +301 -278
  159. warp/native/hashgrid.cu +78 -77
  160. warp/native/hashgrid.h +227 -227
  161. warp/native/initializer_array.h +32 -32
  162. warp/native/intersect.h +1204 -1204
  163. warp/native/intersect_adj.h +365 -365
  164. warp/native/intersect_tri.h +322 -322
  165. warp/native/marching.cpp +2 -2
  166. warp/native/marching.cu +497 -497
  167. warp/native/marching.h +2 -2
  168. warp/native/mat.h +1545 -1498
  169. warp/native/matnn.h +333 -333
  170. warp/native/mesh.cpp +203 -203
  171. warp/native/mesh.cu +292 -293
  172. warp/native/mesh.h +1887 -1887
  173. warp/native/nanovdb/GridHandle.h +366 -0
  174. warp/native/nanovdb/HostBuffer.h +590 -0
  175. warp/native/nanovdb/NanoVDB.h +6624 -4782
  176. warp/native/nanovdb/PNanoVDB.h +3390 -2553
  177. warp/native/noise.h +850 -850
  178. warp/native/quat.h +1112 -1085
  179. warp/native/rand.h +303 -299
  180. warp/native/range.h +108 -108
  181. warp/native/reduce.cpp +156 -156
  182. warp/native/reduce.cu +348 -348
  183. warp/native/runlength_encode.cpp +61 -61
  184. warp/native/runlength_encode.cu +46 -46
  185. warp/native/scan.cpp +30 -30
  186. warp/native/scan.cu +36 -36
  187. warp/native/scan.h +7 -7
  188. warp/native/solid_angle.h +442 -442
  189. warp/native/sort.cpp +94 -94
  190. warp/native/sort.cu +97 -97
  191. warp/native/sort.h +14 -14
  192. warp/native/sparse.cpp +337 -337
  193. warp/native/sparse.cu +544 -544
  194. warp/native/spatial.h +630 -630
  195. warp/native/svd.h +562 -562
  196. warp/native/temp_buffer.h +30 -30
  197. warp/native/vec.h +1177 -1133
  198. warp/native/volume.cpp +529 -297
  199. warp/native/volume.cu +58 -32
  200. warp/native/volume.h +960 -538
  201. warp/native/volume_builder.cu +446 -425
  202. warp/native/volume_builder.h +34 -19
  203. warp/native/volume_impl.h +61 -0
  204. warp/native/warp.cpp +1057 -1052
  205. warp/native/warp.cu +2949 -2828
  206. warp/native/warp.h +321 -305
  207. warp/optim/__init__.py +9 -9
  208. warp/optim/adam.py +120 -120
  209. warp/optim/linear.py +1104 -939
  210. warp/optim/sgd.py +104 -92
  211. warp/render/__init__.py +10 -10
  212. warp/render/render_opengl.py +3356 -3204
  213. warp/render/render_usd.py +768 -749
  214. warp/render/utils.py +152 -150
  215. warp/sim/__init__.py +52 -59
  216. warp/sim/articulation.py +685 -685
  217. warp/sim/collide.py +1594 -1590
  218. warp/sim/import_mjcf.py +489 -481
  219. warp/sim/import_snu.py +220 -221
  220. warp/sim/import_urdf.py +536 -516
  221. warp/sim/import_usd.py +887 -881
  222. warp/sim/inertia.py +316 -317
  223. warp/sim/integrator.py +234 -233
  224. warp/sim/integrator_euler.py +1956 -1956
  225. warp/sim/integrator_featherstone.py +1917 -1991
  226. warp/sim/integrator_xpbd.py +3288 -3312
  227. warp/sim/model.py +4473 -4314
  228. warp/sim/particles.py +113 -112
  229. warp/sim/render.py +417 -403
  230. warp/sim/utils.py +413 -410
  231. warp/sparse.py +1289 -1227
  232. warp/stubs.py +2192 -2469
  233. warp/tape.py +1162 -225
  234. warp/tests/__init__.py +1 -1
  235. warp/tests/__main__.py +4 -4
  236. warp/tests/assets/test_index_grid.nvdb +0 -0
  237. warp/tests/assets/torus.usda +105 -105
  238. warp/tests/aux_test_class_kernel.py +26 -26
  239. warp/tests/aux_test_compile_consts_dummy.py +10 -10
  240. warp/tests/aux_test_conditional_unequal_types_kernels.py +21 -21
  241. warp/tests/aux_test_dependent.py +20 -22
  242. warp/tests/aux_test_grad_customs.py +21 -23
  243. warp/tests/aux_test_reference.py +9 -11
  244. warp/tests/aux_test_reference_reference.py +8 -10
  245. warp/tests/aux_test_square.py +15 -17
  246. warp/tests/aux_test_unresolved_func.py +14 -14
  247. warp/tests/aux_test_unresolved_symbol.py +14 -14
  248. warp/tests/disabled_kinematics.py +237 -239
  249. warp/tests/run_coverage_serial.py +31 -31
  250. warp/tests/test_adam.py +155 -157
  251. warp/tests/test_arithmetic.py +1088 -1124
  252. warp/tests/test_array.py +2415 -2326
  253. warp/tests/test_array_reduce.py +148 -150
  254. warp/tests/test_async.py +666 -656
  255. warp/tests/test_atomic.py +139 -141
  256. warp/tests/test_bool.py +212 -149
  257. warp/tests/test_builtins_resolution.py +1290 -1292
  258. warp/tests/test_bvh.py +162 -171
  259. warp/tests/test_closest_point_edge_edge.py +227 -228
  260. warp/tests/test_codegen.py +562 -553
  261. warp/tests/test_compile_consts.py +217 -101
  262. warp/tests/test_conditional.py +244 -246
  263. warp/tests/test_copy.py +230 -215
  264. warp/tests/test_ctypes.py +630 -632
  265. warp/tests/test_dense.py +65 -67
  266. warp/tests/test_devices.py +89 -98
  267. warp/tests/test_dlpack.py +528 -529
  268. warp/tests/test_examples.py +403 -378
  269. warp/tests/test_fabricarray.py +952 -955
  270. warp/tests/test_fast_math.py +60 -54
  271. warp/tests/test_fem.py +1298 -1278
  272. warp/tests/test_fp16.py +128 -130
  273. warp/tests/test_func.py +336 -337
  274. warp/tests/test_generics.py +596 -571
  275. warp/tests/test_grad.py +885 -640
  276. warp/tests/test_grad_customs.py +331 -336
  277. warp/tests/test_hash_grid.py +208 -164
  278. warp/tests/test_import.py +37 -39
  279. warp/tests/test_indexedarray.py +1132 -1134
  280. warp/tests/test_intersect.py +65 -67
  281. warp/tests/test_jax.py +305 -307
  282. warp/tests/test_large.py +169 -164
  283. warp/tests/test_launch.py +352 -354
  284. warp/tests/test_lerp.py +217 -261
  285. warp/tests/test_linear_solvers.py +189 -171
  286. warp/tests/test_lvalue.py +419 -493
  287. warp/tests/test_marching_cubes.py +63 -65
  288. warp/tests/test_mat.py +1799 -1827
  289. warp/tests/test_mat_lite.py +113 -115
  290. warp/tests/test_mat_scalar_ops.py +2905 -2889
  291. warp/tests/test_math.py +124 -193
  292. warp/tests/test_matmul.py +498 -499
  293. warp/tests/test_matmul_lite.py +408 -410
  294. warp/tests/test_mempool.py +186 -190
  295. warp/tests/test_mesh.py +281 -324
  296. warp/tests/test_mesh_query_aabb.py +226 -241
  297. warp/tests/test_mesh_query_point.py +690 -702
  298. warp/tests/test_mesh_query_ray.py +290 -303
  299. warp/tests/test_mlp.py +274 -276
  300. warp/tests/test_model.py +108 -110
  301. warp/tests/test_module_hashing.py +111 -0
  302. warp/tests/test_modules_lite.py +36 -39
  303. warp/tests/test_multigpu.py +161 -163
  304. warp/tests/test_noise.py +244 -248
  305. warp/tests/test_operators.py +248 -250
  306. warp/tests/test_options.py +121 -125
  307. warp/tests/test_peer.py +131 -137
  308. warp/tests/test_pinned.py +76 -78
  309. warp/tests/test_print.py +52 -54
  310. warp/tests/test_quat.py +2084 -2086
  311. warp/tests/test_rand.py +324 -288
  312. warp/tests/test_reload.py +207 -217
  313. warp/tests/test_rounding.py +177 -179
  314. warp/tests/test_runlength_encode.py +188 -190
  315. warp/tests/test_sim_grad.py +241 -0
  316. warp/tests/test_sim_kinematics.py +89 -97
  317. warp/tests/test_smoothstep.py +166 -168
  318. warp/tests/test_snippet.py +303 -266
  319. warp/tests/test_sparse.py +466 -460
  320. warp/tests/test_spatial.py +2146 -2148
  321. warp/tests/test_special_values.py +362 -0
  322. warp/tests/test_streams.py +484 -473
  323. warp/tests/test_struct.py +708 -675
  324. warp/tests/test_tape.py +171 -148
  325. warp/tests/test_torch.py +741 -743
  326. warp/tests/test_transient_module.py +85 -87
  327. warp/tests/test_types.py +554 -659
  328. warp/tests/test_utils.py +488 -499
  329. warp/tests/test_vec.py +1262 -1268
  330. warp/tests/test_vec_lite.py +71 -73
  331. warp/tests/test_vec_scalar_ops.py +2097 -2099
  332. warp/tests/test_verify_fp.py +92 -94
  333. warp/tests/test_volume.py +961 -736
  334. warp/tests/test_volume_write.py +338 -265
  335. warp/tests/unittest_serial.py +38 -37
  336. warp/tests/unittest_suites.py +367 -359
  337. warp/tests/unittest_utils.py +434 -578
  338. warp/tests/unused_test_misc.py +69 -71
  339. warp/tests/walkthrough_debug.py +85 -85
  340. warp/thirdparty/appdirs.py +598 -598
  341. warp/thirdparty/dlpack.py +143 -143
  342. warp/thirdparty/unittest_parallel.py +563 -561
  343. warp/torch.py +321 -295
  344. warp/types.py +4941 -4450
  345. warp/utils.py +1008 -821
  346. {warp_lang-1.0.2.dist-info → warp_lang-1.2.0.dist-info}/LICENSE.md +126 -126
  347. {warp_lang-1.0.2.dist-info → warp_lang-1.2.0.dist-info}/METADATA +365 -400
  348. warp_lang-1.2.0.dist-info/RECORD +359 -0
  349. warp/examples/assets/cube.usda +0 -42
  350. warp/examples/assets/sphere.usda +0 -56
  351. warp/examples/assets/torus.usda +0 -105
  352. warp/examples/fem/example_convection_diffusion_dg0.py +0 -194
  353. warp/native/nanovdb/PNanoVDBWrite.h +0 -295
  354. warp_lang-1.0.2.dist-info/RECORD +0 -352
  355. {warp_lang-1.0.2.dist-info → warp_lang-1.2.0.dist-info}/WHEEL +0 -0
  356. {warp_lang-1.0.2.dist-info → warp_lang-1.2.0.dist-info}/top_level.txt +0 -0
warp/tests/test_volume.py CHANGED
@@ -1,736 +1,961 @@
1
- # Copyright (c) 2022 NVIDIA CORPORATION. All rights reserved.
2
- # NVIDIA CORPORATION and its licensors retain all intellectual property
3
- # and proprietary rights in and to this software, related documentation
4
- # and any modifications thereto. Any use, reproduction, disclosure or
5
- # distribution of this software and related documentation without an express
6
- # license agreement from NVIDIA CORPORATION is strictly prohibited.
7
-
8
- import unittest
9
-
10
- import numpy as np
11
-
12
- import warp as wp
13
- from warp.tests.unittest_utils import *
14
-
15
- wp.init()
16
-
17
-
18
- # float volume tests
19
- @wp.kernel
20
- def test_volume_lookup_f(volume: wp.uint64, points: wp.array(dtype=wp.vec3)):
21
- tid = wp.tid()
22
-
23
- p = points[tid]
24
- expected = p[0] * p[1] * p[2]
25
- if abs(p[0]) > 10.0 or abs(p[1]) > 10.0 or abs(p[2]) > 10.0:
26
- expected = 10.0
27
-
28
- i = int(p[0])
29
- j = int(p[1])
30
- k = int(p[2])
31
-
32
- expect_eq(wp.volume_lookup_f(volume, i, j, k), expected)
33
-
34
-
35
- @wp.kernel
36
- def test_volume_sample_closest_f(volume: wp.uint64, points: wp.array(dtype=wp.vec3)):
37
- tid = wp.tid()
38
-
39
- p = points[tid]
40
- i = round(p[0])
41
- j = round(p[1])
42
- k = round(p[2])
43
- expected = i * j * k
44
- if abs(i) > 10.0 or abs(j) > 10.0 or abs(k) > 10.0:
45
- expected = 10.0
46
-
47
- expect_eq(wp.volume_sample_f(volume, p, wp.Volume.CLOSEST), expected)
48
-
49
- q = wp.volume_index_to_world(volume, p)
50
- q_inv = wp.volume_world_to_index(volume, q)
51
- expect_eq(p, q_inv)
52
-
53
-
54
- @wp.kernel
55
- def test_volume_sample_linear_f(volume: wp.uint64, points: wp.array(dtype=wp.vec3)):
56
- tid = wp.tid()
57
-
58
- p = points[tid]
59
-
60
- expected = p[0] * p[1] * p[2]
61
- if abs(p[0]) > 10.0 or abs(p[1]) > 10.0 or abs(p[2]) > 10.0:
62
- return # not testing against background values
63
-
64
- expect_near(wp.volume_sample_f(volume, p, wp.Volume.LINEAR), expected, 2.0e-4)
65
-
66
-
67
- @wp.kernel
68
- def test_volume_sample_grad_linear_f(volume: wp.uint64, points: wp.array(dtype=wp.vec3)):
69
- tid = wp.tid()
70
-
71
- p = points[tid]
72
-
73
- expected_val = p[0] * p[1] * p[2]
74
- expected_gx = p[1] * p[2]
75
- expected_gy = p[0] * p[2]
76
- expected_gz = p[0] * p[1]
77
-
78
- if abs(p[0]) > 10.0 or abs(p[1]) > 10.0 or abs(p[2]) > 10.0:
79
- return # not testing against background values
80
-
81
- grad = wp.vec3(0.0, 0.0, 0.0)
82
- val = wp.volume_sample_grad_f(volume, p, wp.Volume.LINEAR, grad)
83
-
84
- expect_near(val, expected_val, 2.0e-4)
85
- expect_near(grad[0], expected_gx, 2.0e-4)
86
- expect_near(grad[1], expected_gy, 2.0e-4)
87
- expect_near(grad[2], expected_gz, 2.0e-4)
88
-
89
-
90
- @wp.kernel
91
- def test_volume_sample_local_f_linear_values(
92
- volume: wp.uint64, points: wp.array(dtype=wp.vec3), values: wp.array(dtype=wp.float32)
93
- ):
94
- tid = wp.tid()
95
- p = points[tid]
96
- values[tid] = wp.volume_sample_f(volume, p, wp.Volume.LINEAR)
97
-
98
-
99
- @wp.kernel
100
- def test_volume_sample_grad_local_f_linear_values(
101
- volume: wp.uint64, points: wp.array(dtype=wp.vec3), values: wp.array(dtype=wp.float32), case_num: int
102
- ):
103
- tid = wp.tid()
104
- p = points[tid]
105
-
106
- grad = wp.vec3(0.0, 0.0, 0.0)
107
- val = wp.volume_sample_grad_f(volume, p, wp.Volume.LINEAR, grad)
108
-
109
- if case_num == 1:
110
- val = grad[0]
111
- elif case_num == 2:
112
- val = grad[1]
113
- elif case_num == 3:
114
- val = grad[2]
115
- values[tid] = val
116
-
117
-
118
- @wp.kernel
119
- def test_volume_sample_world_f_linear_values(
120
- volume: wp.uint64, points: wp.array(dtype=wp.vec3), values: wp.array(dtype=wp.float32)
121
- ):
122
- tid = wp.tid()
123
- q = points[tid]
124
- p = wp.volume_world_to_index(volume, q)
125
- values[tid] = wp.volume_sample_f(volume, p, wp.Volume.LINEAR)
126
-
127
-
128
- @wp.kernel
129
- def test_volume_sample_grad_world_f_linear_values(
130
- volume: wp.uint64, points: wp.array(dtype=wp.vec3), values: wp.array(dtype=wp.float32), case_num: int
131
- ):
132
- tid = wp.tid()
133
- q = points[tid]
134
- p = wp.volume_world_to_index(volume, q)
135
-
136
- grad = wp.vec3(0.0, 0.0, 0.0)
137
- val = wp.volume_sample_grad_f(volume, p, wp.Volume.LINEAR, grad)
138
-
139
- if case_num == 1:
140
- val = grad[0]
141
- elif case_num == 2:
142
- val = grad[1]
143
- elif case_num == 3:
144
- val = grad[2]
145
-
146
- values[tid] = val
147
-
148
-
149
- # vec3f volume tests
150
- @wp.kernel
151
- def test_volume_lookup_v(volume: wp.uint64, points: wp.array(dtype=wp.vec3)):
152
- tid = wp.tid()
153
-
154
- p = points[tid]
155
- expected = wp.vec3(
156
- p[0] + 2.0 * p[1] + 3.0 * p[2], 4.0 * p[0] + 5.0 * p[1] + 6.0 * p[2], 7.0 * p[0] + 8.0 * p[1] + 9.0 * p[2]
157
- )
158
- if abs(p[0]) > 10.0 or abs(p[1]) > 10.0 or abs(p[2]) > 10.0:
159
- expected = wp.vec3(10.8, -4.13, 10.26)
160
-
161
- i = int(p[0])
162
- j = int(p[1])
163
- k = int(p[2])
164
-
165
- expect_eq(wp.volume_lookup_v(volume, i, j, k), expected)
166
-
167
-
168
- @wp.kernel
169
- def test_volume_sample_closest_v(volume: wp.uint64, points: wp.array(dtype=wp.vec3)):
170
- tid = wp.tid()
171
-
172
- p = points[tid]
173
- i = round(p[0])
174
- j = round(p[1])
175
- k = round(p[2])
176
- expected = wp.vec3(i + 2.0 * j + 3.0 * k, 4.0 * i + 5.0 * j + 6.0 * k, 7.0 * i + 8.0 * j + 9.0 * k)
177
- if abs(i) > 10.0 or abs(j) > 10.0 or abs(k) > 10.0:
178
- expected = wp.vec3(10.8, -4.13, 10.26)
179
-
180
- expect_eq(wp.volume_sample_v(volume, p, wp.Volume.CLOSEST), expected)
181
-
182
- q = wp.volume_index_to_world(volume, p)
183
- q_inv = wp.volume_world_to_index(volume, q)
184
- expect_eq(p, q_inv)
185
-
186
-
187
- @wp.kernel
188
- def test_volume_sample_linear_v(volume: wp.uint64, points: wp.array(dtype=wp.vec3)):
189
- tid = wp.tid()
190
-
191
- p = points[tid]
192
-
193
- expected = wp.vec3(
194
- p[0] + 2.0 * p[1] + 3.0 * p[2], 4.0 * p[0] + 5.0 * p[1] + 6.0 * p[2], 7.0 * p[0] + 8.0 * p[1] + 9.0 * p[2]
195
- )
196
- if abs(p[0]) > 10.0 or abs(p[1]) > 10.0 or abs(p[2]) > 10.0:
197
- return # not testing against background values
198
-
199
- expect_near(wp.volume_sample_v(volume, p, wp.Volume.LINEAR), expected, 2.0e-4)
200
-
201
-
202
- @wp.kernel
203
- def test_volume_sample_local_v_linear_values(
204
- volume: wp.uint64, points: wp.array(dtype=wp.vec3), values: wp.array(dtype=wp.float32)
205
- ):
206
- tid = wp.tid()
207
- p = points[tid]
208
- ones = wp.vec3(1.0, 1.0, 1.0)
209
- values[tid] = wp.dot(wp.volume_sample_v(volume, p, wp.Volume.LINEAR), ones)
210
-
211
-
212
- @wp.kernel
213
- def test_volume_sample_world_v_linear_values(
214
- volume: wp.uint64, points: wp.array(dtype=wp.vec3), values: wp.array(dtype=wp.float32)
215
- ):
216
- tid = wp.tid()
217
- q = points[tid]
218
- p = wp.volume_world_to_index(volume, q)
219
- ones = wp.vec3(1.0, 1.0, 1.0)
220
- values[tid] = wp.dot(wp.volume_sample_v(volume, p, wp.Volume.LINEAR), ones)
221
-
222
-
223
- # int32 volume tests
224
- @wp.kernel
225
- def test_volume_lookup_i(volume: wp.uint64, points: wp.array(dtype=wp.vec3)):
226
- tid = wp.tid()
227
-
228
- p = points[tid]
229
- i = int(p[0])
230
- j = int(p[1])
231
- k = int(p[2])
232
- expected = i * j * k
233
- if abs(i) > 10 or abs(j) > 10 or abs(k) > 10:
234
- expected = 10
235
-
236
- expect_eq(wp.volume_lookup_i(volume, i, j, k), expected)
237
-
238
-
239
- @wp.kernel
240
- def test_volume_sample_i(volume: wp.uint64, points: wp.array(dtype=wp.vec3)):
241
- tid = wp.tid()
242
-
243
- p = points[tid]
244
- i = round(p[0])
245
- j = round(p[1])
246
- k = round(p[2])
247
- expected = int(i * j * k)
248
- if abs(i) > 10.0 or abs(j) > 10.0 or abs(k) > 10.0:
249
- expected = 10
250
-
251
- expect_eq(wp.volume_sample_i(volume, p), expected)
252
-
253
- q = wp.volume_index_to_world(volume, p)
254
- q_inv = wp.volume_world_to_index(volume, q)
255
- expect_eq(p, q_inv)
256
-
257
-
258
- # Index/world transformation tests
259
- @wp.kernel
260
- def test_volume_index_to_world(
261
- volume: wp.uint64,
262
- points: wp.array(dtype=wp.vec3),
263
- values: wp.array(dtype=wp.float32),
264
- grad_values: wp.array(dtype=wp.vec3),
265
- ):
266
- tid = wp.tid()
267
- p = points[tid]
268
- ones = wp.vec3(1.0, 1.0, 1.0)
269
- values[tid] = wp.dot(wp.volume_index_to_world(volume, p), ones)
270
- grad_values[tid] = wp.volume_index_to_world_dir(volume, ones)
271
-
272
-
273
- @wp.kernel
274
- def test_volume_world_to_index(
275
- volume: wp.uint64,
276
- points: wp.array(dtype=wp.vec3),
277
- values: wp.array(dtype=wp.float32),
278
- grad_values: wp.array(dtype=wp.vec3),
279
- ):
280
- tid = wp.tid()
281
- p = points[tid]
282
- ones = wp.vec3(1.0, 1.0, 1.0)
283
- values[tid] = wp.dot(wp.volume_world_to_index(volume, p), ones)
284
- grad_values[tid] = wp.volume_world_to_index_dir(volume, ones)
285
-
286
-
287
- # Volume write tests
288
- @wp.kernel
289
- def test_volume_store_f(volume: wp.uint64, points: wp.array(dtype=wp.vec3), values: wp.array(dtype=wp.float32)):
290
- tid = wp.tid()
291
-
292
- p = points[tid]
293
- i = int(p[0])
294
- j = int(p[1])
295
- k = int(p[2])
296
-
297
- # NB: Writing outside the allocated domain overwrites the background value of the Volume
298
- if abs(i) <= 11 and abs(j) <= 11 and abs(k) <= 11:
299
- wp.volume_store_f(volume, i, j, k, float(i + 100 * j + 10000 * k))
300
- values[tid] = wp.volume_lookup_f(volume, i, j, k)
301
-
302
-
303
- @wp.kernel
304
- def test_volume_store_v(volume: wp.uint64, points: wp.array(dtype=wp.vec3), values: wp.array(dtype=wp.vec3)):
305
- tid = wp.tid()
306
-
307
- p = points[tid]
308
- i = int(p[0])
309
- j = int(p[1])
310
- k = int(p[2])
311
-
312
- # NB: Writing outside the allocated domain overwrites the background value of the Volume
313
- if abs(i) <= 11 and abs(j) <= 11 and abs(k) <= 11:
314
- wp.volume_store_v(volume, i, j, k, p)
315
- values[tid] = wp.volume_lookup_v(volume, i, j, k)
316
-
317
-
318
- @wp.kernel
319
- def test_volume_store_i(volume: wp.uint64, points: wp.array(dtype=wp.vec3), values: wp.array(dtype=wp.int32)):
320
- tid = wp.tid()
321
-
322
- p = points[tid]
323
- i = int(p[0])
324
- j = int(p[1])
325
- k = int(p[2])
326
-
327
- # NB: Writing outside the allocated domain overwrites the background value of the Volume
328
- if abs(i) <= 11 and abs(j) <= 11 and abs(k) <= 11:
329
- wp.volume_store_i(volume, i, j, k, i + 100 * j + 10000 * k)
330
- values[tid] = wp.volume_lookup_i(volume, i, j, k)
331
-
332
-
333
- devices = get_test_devices()
334
- rng = np.random.default_rng(101215)
335
-
336
- # Note about the test grids:
337
- # test_grid and test_int32_grid
338
- # active region: [-10,10]^3
339
- # values: v[i,j,k] = i * j * k
340
- # voxel size: 0.25
341
- #
342
- # test_vec_grid
343
- # active region: [-10,10]^3
344
- # values: v[i,j,k] = (i + 2*j + 3*k, 4*i + 5*j + 6*k, 7*i + 8*j + 9*k)
345
- # voxel size: 0.25
346
- #
347
- # torus
348
- # index to world transformation:
349
- # [0.1, 0, 0, 0]
350
- # [0, 0, 0.1, 0]
351
- # [0, 0.1, 0, 0]
352
- # [1, 2, 3, 1]
353
- # (-90 degrees rotation along X)
354
- # voxel size: 0.1
355
- volume_paths = {
356
- "float": os.path.abspath(os.path.join(os.path.dirname(__file__), "assets/test_grid.nvdb")),
357
- "int32": os.path.abspath(os.path.join(os.path.dirname(__file__), "assets/test_int32_grid.nvdb")),
358
- "vec3f": os.path.abspath(os.path.join(os.path.dirname(__file__), "assets/test_vec_grid.nvdb")),
359
- "torus": os.path.abspath(os.path.join(os.path.dirname(__file__), "assets/torus.nvdb")),
360
- "float_write": os.path.abspath(os.path.join(os.path.dirname(__file__), "assets/test_grid.nvdb")),
361
- }
362
-
363
- test_volume_tiles = (
364
- np.array([[i, j, k] for i in range(-2, 2) for j in range(-2, 2) for k in range(-2, 2)], dtype=np.int32) * 8
365
- )
366
-
367
- volumes = {}
368
- for value_type, path in volume_paths.items():
369
- volumes[value_type] = {}
370
- volume_data = open(path, "rb").read()
371
- for device in devices:
372
- try:
373
- volume = wp.Volume.load_from_nvdb(volume_data, device)
374
- except RuntimeError as e:
375
- raise RuntimeError(f'Failed to load volume from "{path}" to {device} memory:\n{e}')
376
-
377
- volumes[value_type][device.alias] = volume
378
-
379
- axis = np.linspace(-1, 1, 3)
380
- point_grid = np.array([[x, y, z] for x in axis for y in axis for z in axis], dtype=np.float32)
381
-
382
-
383
- def test_volume_sample_linear_f_gradient(test, device):
384
- points = rng.uniform(-10.0, 10.0, size=(100, 3))
385
- values = wp.array(np.zeros(1), dtype=wp.float32, device=device, requires_grad=True)
386
- for test_case in points:
387
- uvws = wp.array(test_case, dtype=wp.vec3, device=device, requires_grad=True)
388
- xyzs = wp.array(test_case * 0.25, dtype=wp.vec3, device=device, requires_grad=True)
389
-
390
- tape = wp.Tape()
391
- with tape:
392
- wp.launch(
393
- test_volume_sample_local_f_linear_values,
394
- dim=1,
395
- inputs=[volumes["float"][device.alias].id, uvws, values],
396
- device=device,
397
- )
398
- tape.backward(values)
399
-
400
- x, y, z = test_case
401
- grad_expected = np.array([y * z, x * z, x * y])
402
- grad_computed = tape.gradients[uvws].numpy()[0]
403
- np.testing.assert_allclose(grad_computed, grad_expected, rtol=1e-4)
404
-
405
- tape = wp.Tape()
406
- with tape:
407
- wp.launch(
408
- test_volume_sample_world_f_linear_values,
409
- dim=1,
410
- inputs=[volumes["float"][device.alias].id, xyzs, values],
411
- device=device,
412
- )
413
- tape.backward(values)
414
-
415
- x, y, z = test_case
416
- grad_expected = np.array([y * z, x * z, x * y]) / 0.25
417
- grad_computed = tape.gradients[xyzs].numpy()[0]
418
- np.testing.assert_allclose(grad_computed, grad_expected, rtol=1e-4)
419
-
420
-
421
- def test_volume_sample_grad_linear_f_gradient(test, device):
422
- points = rng.uniform(-10.0, 10.0, size=(100, 3))
423
- values = wp.array(np.zeros(1), dtype=wp.float32, device=device, requires_grad=True)
424
- for test_case in points:
425
- uvws = wp.array(test_case, dtype=wp.vec3, device=device, requires_grad=True)
426
- xyzs = wp.array(test_case * 0.25, dtype=wp.vec3, device=device, requires_grad=True)
427
-
428
- for case_num in range(4):
429
- tape = wp.Tape()
430
- with tape:
431
- wp.launch(
432
- test_volume_sample_grad_local_f_linear_values,
433
- dim=1,
434
- inputs=[volumes["float"][device.alias].id, uvws, values, case_num],
435
- device=device,
436
- )
437
- tape.backward(values)
438
-
439
- x, y, z = test_case
440
- grad_computed = tape.gradients[uvws].numpy()[0]
441
- if case_num == 0:
442
- grad_expected = np.array([y * z, x * z, x * y])
443
- elif case_num == 1:
444
- grad_expected = np.array([0.0, z, y])
445
- elif case_num == 2:
446
- grad_expected = np.array([z, 0.0, x])
447
- elif case_num == 3:
448
- grad_expected = np.array([y, x, 0.0])
449
-
450
- np.testing.assert_allclose(grad_computed, grad_expected, rtol=1e-4)
451
- tape.zero()
452
-
453
- for case_num in range(4):
454
- tape = wp.Tape()
455
- with tape:
456
- wp.launch(
457
- test_volume_sample_grad_world_f_linear_values,
458
- dim=1,
459
- inputs=[volumes["float"][device.alias].id, xyzs, values, case_num],
460
- device=device,
461
- )
462
- tape.backward(values)
463
-
464
- x, y, z = test_case
465
- grad_computed = tape.gradients[xyzs].numpy()[0]
466
- if case_num == 0:
467
- grad_expected = np.array([y * z, x * z, x * y]) / 0.25
468
- elif case_num == 1:
469
- grad_expected = np.array([0.0, z, y]) / 0.25
470
- elif case_num == 2:
471
- grad_expected = np.array([z, 0.0, x]) / 0.25
472
- elif case_num == 3:
473
- grad_expected = np.array([y, x, 0.0]) / 0.25
474
-
475
- np.testing.assert_allclose(grad_computed, grad_expected, rtol=1e-4)
476
- tape.zero()
477
-
478
-
479
- def test_volume_sample_linear_v_gradient(test, device):
480
- points = rng.uniform(-10.0, 10.0, size=(100, 3))
481
- values = wp.zeros(1, dtype=wp.float32, device=device, requires_grad=True)
482
- for test_case in points:
483
- uvws = wp.array(test_case, dtype=wp.vec3, device=device, requires_grad=True)
484
- xyzs = wp.array(test_case * 0.25, dtype=wp.vec3, device=device, requires_grad=True)
485
-
486
- tape = wp.Tape()
487
- with tape:
488
- wp.launch(
489
- test_volume_sample_local_v_linear_values,
490
- dim=1,
491
- inputs=[volumes["vec3f"][device.alias].id, uvws, values],
492
- device=device,
493
- )
494
- tape.backward(values)
495
-
496
- grad_expected = np.array([6.0, 15.0, 24.0])
497
- grad_computed = tape.gradients[uvws].numpy()[0]
498
- np.testing.assert_allclose(grad_computed, grad_expected, rtol=1e-4)
499
-
500
- tape = wp.Tape()
501
- with tape:
502
- wp.launch(
503
- test_volume_sample_world_v_linear_values,
504
- dim=1,
505
- inputs=[volumes["vec3f"][device.alias].id, xyzs, values],
506
- device=device,
507
- )
508
- tape.backward(values)
509
-
510
- grad_expected = np.array([6.0, 15.0, 24.0]) / 0.25
511
- grad_computed = tape.gradients[xyzs].numpy()[0]
512
- np.testing.assert_allclose(grad_computed, grad_expected, rtol=1e-4)
513
-
514
-
515
- def test_volume_transform_gradient(test, device):
516
- values = wp.zeros(1, dtype=wp.float32, device=device, requires_grad=True)
517
- grad_values = wp.zeros(1, dtype=wp.vec3, device=device)
518
- test_points = rng.uniform(-10.0, 10.0, size=(10, 3))
519
- for test_case in test_points:
520
- points = wp.array(test_case, dtype=wp.vec3, device=device, requires_grad=True)
521
- tape = wp.Tape()
522
- with tape:
523
- wp.launch(
524
- test_volume_index_to_world,
525
- dim=1,
526
- inputs=[volumes["torus"][device.alias].id, points, values, grad_values],
527
- device=device,
528
- )
529
- tape.backward(values)
530
-
531
- grad_computed = tape.gradients[points].numpy()
532
- grad_expected = grad_values.numpy()
533
- np.testing.assert_allclose(grad_computed, grad_expected, rtol=1e-4)
534
-
535
- grad_computed = tape.gradients[points].numpy()
536
- grad_expected = grad_values.numpy()
537
- np.testing.assert_allclose(grad_computed, grad_expected, rtol=1e-4)
538
-
539
-
540
- def test_volume_store(test, device):
541
- values_ref = np.array([x + 100 * y + 10000 * z for x, y, z in point_grid])
542
- points = wp.array(point_grid, dtype=wp.vec3, device=device)
543
- values = wp.empty(len(point_grid), dtype=wp.float32, device=device)
544
- wp.launch(
545
- test_volume_store_f,
546
- dim=len(point_grid),
547
- inputs=[volumes["float_write"][device.alias].id, points, values],
548
- device=device,
549
- )
550
-
551
- values_res = values.numpy()
552
- np.testing.assert_equal(values_res, values_ref)
553
-
554
-
555
- def test_volume_allocation_f(test, device):
556
- bg_value = -123.0
557
- points_np = np.append(point_grid, [[8096, 8096, 8096]], axis=0)
558
- values_ref = np.append(np.array([x + 100 * y + 10000 * z for x, y, z in point_grid]), bg_value)
559
-
560
- volume = wp.Volume.allocate(min=[-11, -11, -11], max=[11, 11, 11], voxel_size=0.1, bg_value=bg_value, device=device)
561
- points = wp.array(points_np, dtype=wp.vec3, device=device)
562
- values = wp.empty(len(points_np), dtype=wp.float32, device=device)
563
- wp.launch(test_volume_store_f, dim=len(points_np), inputs=[volume.id, points, values], device=device)
564
-
565
- values_res = values.numpy()
566
- np.testing.assert_equal(values_res, values_ref)
567
-
568
-
569
- def test_volume_allocation_v(test, device):
570
- bg_value = (-1, 2.0, -3)
571
- points_np = np.append(point_grid, [[8096, 8096, 8096]], axis=0)
572
- values_ref = np.append(point_grid, [bg_value], axis=0)
573
-
574
- volume = wp.Volume.allocate(min=[-11, -11, -11], max=[11, 11, 11], voxel_size=0.1, bg_value=bg_value, device=device)
575
- points = wp.array(points_np, dtype=wp.vec3, device=device)
576
- values = wp.empty(len(points_np), dtype=wp.vec3, device=device)
577
- wp.launch(test_volume_store_v, dim=len(points_np), inputs=[volume.id, points, values], device=device)
578
-
579
- values_res = values.numpy()
580
- np.testing.assert_equal(values_res, values_ref)
581
-
582
-
583
- def test_volume_allocation_i(test, device):
584
- bg_value = -123
585
- points_np = np.append(point_grid, [[8096, 8096, 8096]], axis=0)
586
- values_ref = np.append(np.array([x + 100 * y + 10000 * z for x, y, z in point_grid], dtype=np.int32), bg_value)
587
-
588
- volume = wp.Volume.allocate(min=[-11, -11, -11], max=[11, 11, 11], voxel_size=0.1, bg_value=bg_value, device=device)
589
- points = wp.array(points_np, dtype=wp.vec3, device=device)
590
- values = wp.empty(len(points_np), dtype=wp.int32, device=device)
591
- wp.launch(test_volume_store_i, dim=len(points_np), inputs=[volume.id, points, values], device=device)
592
-
593
- values_res = values.numpy()
594
- np.testing.assert_equal(values_res, values_ref)
595
-
596
-
597
- def test_volume_introspection(test, device):
598
- for volume_names in ("float", "vec3f"):
599
- with test.subTest(volume_names=volume_names):
600
- volume = volumes[volume_names][device.alias]
601
- tiles_actual = volume.get_tiles().numpy()
602
- tiles_sorted = tiles_actual[np.lexsort(tiles_actual.T[::-1])]
603
- voxel_size = np.array(volume.get_voxel_size())
604
-
605
- np.testing.assert_equal(test_volume_tiles, tiles_sorted)
606
- np.testing.assert_equal([0.25] * 3, voxel_size)
607
-
608
-
609
- def test_volume_from_numpy(test, device):
610
- # Volume.allocate_from_tiles() is only available with CUDA
611
- mins = np.array([-3.0, -3.0, -3.0])
612
- voxel_size = 0.2
613
- maxs = np.array([3.0, 3.0, 3.0])
614
- nums = np.ceil((maxs - mins) / (voxel_size)).astype(dtype=int)
615
- center = np.array([0.0, 0.0, 0.0])
616
- rad = 2.5
617
- sphere_sdf_np = np.zeros(tuple(nums))
618
- for x in range(nums[0]):
619
- for y in range(nums[1]):
620
- for z in range(nums[2]):
621
- pos = mins + voxel_size * np.array([x, y, z])
622
- dis = np.linalg.norm(pos - center)
623
- sphere_sdf_np[x, y, z] = dis - rad
624
- sphere_vdb = wp.Volume.load_from_numpy(sphere_sdf_np, mins, voxel_size, rad + 3.0 * voxel_size, device=device)
625
-
626
- test.assertNotEqual(sphere_vdb.id, 0)
627
-
628
- sphere_vdb_array = sphere_vdb.array()
629
- test.assertEqual(sphere_vdb_array.dtype, wp.uint8)
630
- test.assertIsNone(sphere_vdb_array.deleter)
631
-
632
-
633
- class TestVolume(unittest.TestCase):
634
- pass
635
-
636
-
637
- add_function_test(
638
- TestVolume, "test_volume_sample_linear_f_gradient", test_volume_sample_linear_f_gradient, devices=devices
639
- )
640
- add_function_test(
641
- TestVolume, "test_volume_sample_grad_linear_f_gradient", test_volume_sample_grad_linear_f_gradient, devices=devices
642
- )
643
- add_function_test(
644
- TestVolume, "test_volume_sample_linear_v_gradient", test_volume_sample_linear_v_gradient, devices=devices
645
- )
646
- add_function_test(TestVolume, "test_volume_transform_gradient", test_volume_transform_gradient, devices=devices)
647
- add_function_test(TestVolume, "test_volume_store", test_volume_store, devices=devices)
648
- add_function_test(
649
- TestVolume, "test_volume_allocation_f", test_volume_allocation_f, devices=get_unique_cuda_test_devices()
650
- )
651
- add_function_test(
652
- TestVolume, "test_volume_allocation_v", test_volume_allocation_v, devices=get_unique_cuda_test_devices()
653
- )
654
- add_function_test(
655
- TestVolume, "test_volume_allocation_i", test_volume_allocation_i, devices=get_unique_cuda_test_devices()
656
- )
657
- add_function_test(TestVolume, "test_volume_introspection", test_volume_introspection, devices=devices)
658
- add_function_test(TestVolume, "test_volume_from_numpy", test_volume_from_numpy, devices=get_unique_cuda_test_devices())
659
-
660
- points = {}
661
- points_jittered = {}
662
- for device in devices:
663
- points_jittered_np = point_grid + rng.uniform(-0.5, 0.5, size=point_grid.shape)
664
- points[device.alias] = wp.array(point_grid, dtype=wp.vec3, device=device)
665
- points_jittered[device.alias] = wp.array(points_jittered_np, dtype=wp.vec3, device=device)
666
-
667
- add_kernel_test(
668
- TestVolume,
669
- test_volume_lookup_f,
670
- dim=len(point_grid),
671
- inputs=[volumes["float"][device.alias].id, points[device.alias]],
672
- devices=[device],
673
- )
674
- add_kernel_test(
675
- TestVolume,
676
- test_volume_sample_closest_f,
677
- dim=len(point_grid),
678
- inputs=[volumes["float"][device.alias].id, points_jittered[device.alias]],
679
- devices=[device.alias],
680
- )
681
- add_kernel_test(
682
- TestVolume,
683
- test_volume_sample_linear_f,
684
- dim=len(point_grid),
685
- inputs=[volumes["float"][device.alias].id, points_jittered[device.alias]],
686
- devices=[device.alias],
687
- )
688
- add_kernel_test(
689
- TestVolume,
690
- test_volume_sample_grad_linear_f,
691
- dim=len(point_grid),
692
- inputs=[volumes["float"][device.alias].id, points_jittered[device.alias]],
693
- devices=[device.alias],
694
- )
695
-
696
- add_kernel_test(
697
- TestVolume,
698
- test_volume_lookup_v,
699
- dim=len(point_grid),
700
- inputs=[volumes["vec3f"][device.alias].id, points[device.alias]],
701
- devices=[device.alias],
702
- )
703
- add_kernel_test(
704
- TestVolume,
705
- test_volume_sample_closest_v,
706
- dim=len(point_grid),
707
- inputs=[volumes["vec3f"][device.alias].id, points_jittered[device.alias]],
708
- devices=[device.alias],
709
- )
710
- add_kernel_test(
711
- TestVolume,
712
- test_volume_sample_linear_v,
713
- dim=len(point_grid),
714
- inputs=[volumes["vec3f"][device.alias].id, points_jittered[device.alias]],
715
- devices=[device.alias],
716
- )
717
-
718
- add_kernel_test(
719
- TestVolume,
720
- test_volume_lookup_i,
721
- dim=len(point_grid),
722
- inputs=[volumes["int32"][device.alias].id, points[device.alias]],
723
- devices=[device.alias],
724
- )
725
- add_kernel_test(
726
- TestVolume,
727
- test_volume_sample_i,
728
- dim=len(point_grid),
729
- inputs=[volumes["int32"][device.alias].id, points_jittered[device.alias]],
730
- devices=[device.alias],
731
- )
732
-
733
-
734
- if __name__ == "__main__":
735
- wp.build.clear_kernel_cache()
736
- unittest.main(verbosity=2)
1
+ # Copyright (c) 2022 NVIDIA CORPORATION. All rights reserved.
2
+ # NVIDIA CORPORATION and its licensors retain all intellectual property
3
+ # and proprietary rights in and to this software, related documentation
4
+ # and any modifications thereto. Any use, reproduction, disclosure or
5
+ # distribution of this software and related documentation without an express
6
+ # license agreement from NVIDIA CORPORATION is strictly prohibited.
7
+
8
+ import unittest
9
+ from typing import Any
10
+
11
+ import numpy as np
12
+
13
+ import warp as wp
14
+ from warp.tests.unittest_utils import *
15
+
16
+
17
+ # float volume tests
18
+ @wp.kernel
19
+ def test_volume_lookup_f(volume: wp.uint64, points: wp.array(dtype=wp.vec3)):
20
+ tid = wp.tid()
21
+
22
+ p = points[tid]
23
+ expected = p[0] * p[1] * p[2]
24
+ if abs(p[0]) > 10.0 or abs(p[1]) > 10.0 or abs(p[2]) > 10.0:
25
+ expected = 10.0
26
+
27
+ i = int(p[0])
28
+ j = int(p[1])
29
+ k = int(p[2])
30
+
31
+ expect_eq(wp.volume_lookup_f(volume, i, j, k), expected)
32
+ expect_eq(wp.volume_lookup(volume, i, j, k, dtype=wp.float32), expected)
33
+
34
+
35
+ @wp.kernel
36
+ def test_volume_sample_closest_f(volume: wp.uint64, points: wp.array(dtype=wp.vec3)):
37
+ tid = wp.tid()
38
+
39
+ p = points[tid]
40
+ i = round(p[0])
41
+ j = round(p[1])
42
+ k = round(p[2])
43
+ expected = i * j * k
44
+ if abs(i) > 10.0 or abs(j) > 10.0 or abs(k) > 10.0:
45
+ expected = 10.0
46
+
47
+ expect_eq(wp.volume_sample_f(volume, p, wp.Volume.CLOSEST), expected)
48
+ expect_eq(wp.volume_sample(volume, p, wp.Volume.CLOSEST, dtype=wp.float32), expected)
49
+
50
+ q = wp.volume_index_to_world(volume, p)
51
+ q_inv = wp.volume_world_to_index(volume, q)
52
+ expect_eq(p, q_inv)
53
+
54
+
55
+ @wp.kernel
56
+ def test_volume_sample_linear_f(volume: wp.uint64, points: wp.array(dtype=wp.vec3)):
57
+ tid = wp.tid()
58
+
59
+ p = points[tid]
60
+
61
+ expected = p[0] * p[1] * p[2]
62
+ if abs(p[0]) > 10.0 or abs(p[1]) > 10.0 or abs(p[2]) > 10.0:
63
+ return # not testing against background values
64
+
65
+ expect_near(wp.volume_sample_f(volume, p, wp.Volume.LINEAR), expected, 2.0e-4)
66
+ expect_near(wp.volume_sample(volume, p, wp.Volume.LINEAR, dtype=wp.float32), expected, 2.0e-4)
67
+
68
+
69
+ @wp.kernel
70
+ def test_volume_sample_grad_linear_f(volume: wp.uint64, points: wp.array(dtype=wp.vec3)):
71
+ tid = wp.tid()
72
+
73
+ p = points[tid]
74
+
75
+ expected_val = p[0] * p[1] * p[2]
76
+ expected_gx = p[1] * p[2]
77
+ expected_gy = p[0] * p[2]
78
+ expected_gz = p[0] * p[1]
79
+
80
+ if abs(p[0]) > 10.0 or abs(p[1]) > 10.0 or abs(p[2]) > 10.0:
81
+ return # not testing against background values
82
+
83
+ grad = wp.vec3(0.0, 0.0, 0.0)
84
+ val = wp.volume_sample_grad_f(volume, p, wp.Volume.LINEAR, grad)
85
+
86
+ expect_near(val, expected_val, 2.0e-4)
87
+ expect_near(grad[0], expected_gx, 2.0e-4)
88
+ expect_near(grad[1], expected_gy, 2.0e-4)
89
+ expect_near(grad[2], expected_gz, 2.0e-4)
90
+
91
+ val = wp.volume_sample_grad(volume, p, wp.Volume.LINEAR, grad, dtype=wp.float32)
92
+
93
+ expect_near(val, expected_val, 2.0e-4)
94
+ expect_near(grad[0], expected_gx, 2.0e-4)
95
+ expect_near(grad[1], expected_gy, 2.0e-4)
96
+ expect_near(grad[2], expected_gz, 2.0e-4)
97
+
98
+
99
+ @wp.kernel
100
+ def test_volume_sample_local_f_linear_values(
101
+ volume: wp.uint64, points: wp.array(dtype=wp.vec3), values: wp.array(dtype=wp.float32)
102
+ ):
103
+ tid = wp.tid()
104
+ p = points[tid]
105
+ values[tid] = wp.volume_sample_f(volume, p, wp.Volume.LINEAR)
106
+
107
+
108
+ @wp.kernel
109
+ def test_volume_sample_grad_local_f_linear_values(
110
+ volume: wp.uint64, points: wp.array(dtype=wp.vec3), values: wp.array(dtype=wp.float32), case_num: int
111
+ ):
112
+ tid = wp.tid()
113
+ p = points[tid]
114
+
115
+ grad = wp.vec3(0.0, 0.0, 0.0)
116
+ val = wp.volume_sample_grad_f(volume, p, wp.Volume.LINEAR, grad)
117
+ if case_num == 0:
118
+ values[tid] = val
119
+ elif case_num == 1:
120
+ values[tid] = grad[0]
121
+ elif case_num == 2:
122
+ values[tid] = grad[1]
123
+ elif case_num == 3:
124
+ values[tid] = grad[2]
125
+
126
+
127
+ @wp.kernel
128
+ def test_volume_sample_world_f_linear_values(
129
+ volume: wp.uint64, points: wp.array(dtype=wp.vec3), values: wp.array(dtype=wp.float32)
130
+ ):
131
+ tid = wp.tid()
132
+ q = points[tid]
133
+ p = wp.volume_world_to_index(volume, q)
134
+ values[tid] = wp.volume_sample_f(volume, p, wp.Volume.LINEAR)
135
+
136
+
137
+ @wp.kernel
138
+ def test_volume_sample_grad_world_f_linear_values(
139
+ volume: wp.uint64, points: wp.array(dtype=wp.vec3), values: wp.array(dtype=wp.float32), case_num: int
140
+ ):
141
+ tid = wp.tid()
142
+ q = points[tid]
143
+ p = wp.volume_world_to_index(volume, q)
144
+
145
+ grad = wp.vec3(0.0, 0.0, 0.0)
146
+ val = wp.volume_sample_grad_f(volume, p, wp.Volume.LINEAR, grad)
147
+ if case_num == 0:
148
+ values[tid] = val
149
+ elif case_num == 1:
150
+ values[tid] = grad[0]
151
+ elif case_num == 2:
152
+ values[tid] = grad[1]
153
+ elif case_num == 3:
154
+ values[tid] = grad[2]
155
+
156
+
157
+ # vec3f volume tests
158
+ @wp.kernel
159
+ def test_volume_lookup_v(volume: wp.uint64, points: wp.array(dtype=wp.vec3)):
160
+ tid = wp.tid()
161
+
162
+ p = points[tid]
163
+ expected = wp.vec3(
164
+ p[0] + 2.0 * p[1] + 3.0 * p[2], 4.0 * p[0] + 5.0 * p[1] + 6.0 * p[2], 7.0 * p[0] + 8.0 * p[1] + 9.0 * p[2]
165
+ )
166
+ if abs(p[0]) > 10.0 or abs(p[1]) > 10.0 or abs(p[2]) > 10.0:
167
+ expected = wp.vec3(10.8, -4.13, 10.26)
168
+
169
+ i = int(p[0])
170
+ j = int(p[1])
171
+ k = int(p[2])
172
+
173
+ expect_eq(wp.volume_lookup_v(volume, i, j, k), expected)
174
+ expect_eq(wp.volume_lookup(volume, i, j, k, dtype=wp.vec3), expected)
175
+
176
+
177
+ @wp.kernel
178
+ def test_volume_sample_closest_v(volume: wp.uint64, points: wp.array(dtype=wp.vec3)):
179
+ tid = wp.tid()
180
+
181
+ p = points[tid]
182
+ i = round(p[0])
183
+ j = round(p[1])
184
+ k = round(p[2])
185
+ expected = wp.vec3(i + 2.0 * j + 3.0 * k, 4.0 * i + 5.0 * j + 6.0 * k, 7.0 * i + 8.0 * j + 9.0 * k)
186
+ if abs(i) > 10.0 or abs(j) > 10.0 or abs(k) > 10.0:
187
+ expected = wp.vec3(10.8, -4.13, 10.26)
188
+
189
+ expect_eq(wp.volume_sample_v(volume, p, wp.Volume.CLOSEST), expected)
190
+ expect_eq(wp.volume_sample(volume, p, wp.Volume.CLOSEST, dtype=wp.vec3), expected)
191
+
192
+ q = wp.volume_index_to_world(volume, p)
193
+ q_inv = wp.volume_world_to_index(volume, q)
194
+ expect_eq(p, q_inv)
195
+
196
+
197
+ @wp.kernel
198
+ def test_volume_sample_linear_v(volume: wp.uint64, points: wp.array(dtype=wp.vec3)):
199
+ tid = wp.tid()
200
+
201
+ p = points[tid]
202
+
203
+ expected = wp.vec3(
204
+ p[0] + 2.0 * p[1] + 3.0 * p[2], 4.0 * p[0] + 5.0 * p[1] + 6.0 * p[2], 7.0 * p[0] + 8.0 * p[1] + 9.0 * p[2]
205
+ )
206
+ if abs(p[0]) > 10.0 or abs(p[1]) > 10.0 or abs(p[2]) > 10.0:
207
+ return # not testing against background values
208
+
209
+ expect_near(wp.volume_sample_v(volume, p, wp.Volume.LINEAR), expected, 2.0e-4)
210
+ expect_near(wp.volume_sample(volume, p, wp.Volume.LINEAR, dtype=wp.vec3), expected, 2.0e-4)
211
+
212
+
213
+ @wp.kernel
214
+ def test_volume_sample_grad_linear_v(volume: wp.uint64, points: wp.array(dtype=wp.vec3)):
215
+ tid = wp.tid()
216
+
217
+ p = points[tid]
218
+
219
+ if abs(p[0]) > 10.0 or abs(p[1]) > 10.0 or abs(p[2]) > 10.0:
220
+ return # not testing against background values
221
+
222
+ expected_val = wp.vec3(
223
+ p[0] + 2.0 * p[1] + 3.0 * p[2], 4.0 * p[0] + 5.0 * p[1] + 6.0 * p[2], 7.0 * p[0] + 8.0 * p[1] + 9.0 * p[2]
224
+ )
225
+ expected_grad = wp.mat33(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0)
226
+
227
+ grad = wp.mat33(0.0)
228
+ val = wp.volume_sample_grad(volume, p, wp.Volume.LINEAR, grad, dtype=wp.vec3)
229
+
230
+ expect_near(val, expected_val, 2.0e-4)
231
+ expect_near(grad[0], expected_grad[0], 2.0e-4)
232
+ expect_near(grad[1], expected_grad[1], 2.0e-4)
233
+ expect_near(grad[2], expected_grad[2], 2.0e-4)
234
+
235
+
236
+ @wp.kernel
237
+ def test_volume_sample_local_v_linear_values(
238
+ volume: wp.uint64, points: wp.array(dtype=wp.vec3), values: wp.array(dtype=wp.float32)
239
+ ):
240
+ tid = wp.tid()
241
+ p = points[tid]
242
+ ones = wp.vec3(1.0, 1.0, 1.0)
243
+ values[tid] = wp.dot(wp.volume_sample_v(volume, p, wp.Volume.LINEAR), ones)
244
+
245
+
246
+ @wp.kernel
247
+ def test_volume_sample_world_v_linear_values(
248
+ volume: wp.uint64, points: wp.array(dtype=wp.vec3), values: wp.array(dtype=wp.float32)
249
+ ):
250
+ tid = wp.tid()
251
+ q = points[tid]
252
+ p = wp.volume_world_to_index(volume, q)
253
+ ones = wp.vec3(1.0, 1.0, 1.0)
254
+ values[tid] = wp.dot(wp.volume_sample_v(volume, p, wp.Volume.LINEAR), ones)
255
+
256
+
257
+ # int32 volume tests
258
+ @wp.kernel
259
+ def test_volume_lookup_i(volume: wp.uint64, points: wp.array(dtype=wp.vec3)):
260
+ tid = wp.tid()
261
+
262
+ p = points[tid]
263
+ i = int(p[0])
264
+ j = int(p[1])
265
+ k = int(p[2])
266
+ expected = i * j * k
267
+ if abs(i) > 10 or abs(j) > 10 or abs(k) > 10:
268
+ expected = 10
269
+
270
+ expect_eq(wp.volume_lookup_i(volume, i, j, k), expected)
271
+ expect_eq(wp.volume_lookup(volume, i, j, k, dtype=wp.int32), expected)
272
+
273
+
274
+ @wp.kernel
275
+ def test_volume_sample_i(volume: wp.uint64, points: wp.array(dtype=wp.vec3)):
276
+ tid = wp.tid()
277
+
278
+ p = points[tid]
279
+ i = round(p[0])
280
+ j = round(p[1])
281
+ k = round(p[2])
282
+ expected = int(i * j * k)
283
+ if abs(i) > 10.0 or abs(j) > 10.0 or abs(k) > 10.0:
284
+ expected = 10
285
+
286
+ expect_eq(wp.volume_sample_i(volume, p), expected)
287
+ expect_eq(wp.volume_sample(volume, p, wp.Volume.CLOSEST, dtype=wp.int32), expected)
288
+
289
+ q = wp.volume_index_to_world(volume, p)
290
+ q_inv = wp.volume_world_to_index(volume, q)
291
+ expect_eq(p, q_inv)
292
+
293
+
294
+ # Index/world transformation tests
295
+ @wp.kernel
296
+ def test_volume_index_to_world(
297
+ volume: wp.uint64,
298
+ points: wp.array(dtype=wp.vec3),
299
+ values: wp.array(dtype=wp.float32),
300
+ grad_values: wp.array(dtype=wp.vec3),
301
+ ):
302
+ tid = wp.tid()
303
+ p = points[tid]
304
+ ones = wp.vec3(1.0, 1.0, 1.0)
305
+ values[tid] = wp.dot(wp.volume_index_to_world(volume, p), ones)
306
+ grad_values[tid] = wp.volume_index_to_world_dir(volume, ones)
307
+
308
+
309
+ @wp.kernel
310
+ def test_volume_world_to_index(
311
+ volume: wp.uint64,
312
+ points: wp.array(dtype=wp.vec3),
313
+ values: wp.array(dtype=wp.float32),
314
+ grad_values: wp.array(dtype=wp.vec3),
315
+ ):
316
+ tid = wp.tid()
317
+ p = points[tid]
318
+ ones = wp.vec3(1.0, 1.0, 1.0)
319
+ values[tid] = wp.dot(wp.volume_world_to_index(volume, p), ones)
320
+ grad_values[tid] = wp.volume_world_to_index_dir(volume, ones)
321
+
322
+
323
+ # Volume write tests
324
+ @wp.kernel
325
+ def test_volume_store_f(volume: wp.uint64, points: wp.array(dtype=wp.vec3), values: wp.array(dtype=wp.float32)):
326
+ tid = wp.tid()
327
+
328
+ p = points[tid]
329
+ i = int(p[0])
330
+ j = int(p[1])
331
+ k = int(p[2])
332
+
333
+ wp.volume_store(volume, i, j, k, float(i + 100 * j + 10000 * k))
334
+ values[tid] = wp.volume_lookup_f(volume, i, j, k)
335
+
336
+
337
+ @wp.kernel
338
+ def test_volume_store_v(volume: wp.uint64, points: wp.array(dtype=wp.vec3), values: wp.array(dtype=wp.vec3)):
339
+ tid = wp.tid()
340
+
341
+ p = points[tid]
342
+ i = int(p[0])
343
+ j = int(p[1])
344
+ k = int(p[2])
345
+
346
+ wp.volume_store(volume, i, j, k, p)
347
+ values[tid] = wp.volume_lookup_v(volume, i, j, k)
348
+
349
+
350
+ @wp.kernel
351
+ def test_volume_store_i(volume: wp.uint64, points: wp.array(dtype=wp.vec3), values: wp.array(dtype=wp.int32)):
352
+ tid = wp.tid()
353
+
354
+ p = points[tid]
355
+ i = int(p[0])
356
+ j = int(p[1])
357
+ k = int(p[2])
358
+
359
+ wp.volume_store(volume, i, j, k, i + 100 * j + 10000 * k)
360
+ values[tid] = wp.volume_lookup_i(volume, i, j, k)
361
+
362
+
363
+ devices = get_test_devices()
364
+ rng = np.random.default_rng(101215)
365
+
366
+ # Note about the test grids:
367
+ # test_grid and test_int32_grid
368
+ # active region: [-10,10]^3
369
+ # values: v[i,j,k] = i * j * k
370
+ # voxel size: 0.25
371
+ #
372
+ # test_vec_grid
373
+ # active region: [-10,10]^3
374
+ # values: v[i,j,k] = (i + 2*j + 3*k, 4*i + 5*j + 6*k, 7*i + 8*j + 9*k)
375
+ # voxel size: 0.25
376
+ #
377
+ # torus
378
+ # index to world transformation:
379
+ # [0.1, 0, 0, 0]
380
+ # [0, 0, 0.1, 0]
381
+ # [0, 0.1, 0, 0]
382
+ # [1, 2, 3, 1]
383
+ # (-90 degrees rotation along X)
384
+ # voxel size: 0.1
385
+ volume_paths = {
386
+ "float": os.path.abspath(os.path.join(os.path.dirname(__file__), "assets/test_grid.nvdb")),
387
+ "int32": os.path.abspath(os.path.join(os.path.dirname(__file__), "assets/test_int32_grid.nvdb")),
388
+ "vec3f": os.path.abspath(os.path.join(os.path.dirname(__file__), "assets/test_vec_grid.nvdb")),
389
+ "index": os.path.abspath(os.path.join(os.path.dirname(__file__), "assets/test_index_grid.nvdb")),
390
+ "torus": os.path.abspath(os.path.join(os.path.dirname(__file__), "assets/torus.nvdb")),
391
+ "float_write": os.path.abspath(os.path.join(os.path.dirname(__file__), "assets/test_grid.nvdb")),
392
+ }
393
+
394
+ test_volume_tiles = (
395
+ np.array([[i, j, k] for i in range(-2, 2) for j in range(-2, 2) for k in range(-2, 2)], dtype=np.int32) * 8
396
+ )
397
+
398
+ volumes = {}
399
+ for value_type, path in volume_paths.items():
400
+ volumes[value_type] = {}
401
+ volume_data = open(path, "rb").read()
402
+ for device in devices:
403
+ try:
404
+ volume = wp.Volume.load_from_nvdb(volume_data, device)
405
+ except RuntimeError as e:
406
+ raise RuntimeError(f'Failed to load volume from "{path}" to {device} memory:\n{e}') from e
407
+
408
+ volumes[value_type][device.alias] = volume
409
+
410
+ axis = np.linspace(-1, 1, 3)
411
+ point_grid = np.array([[x, y, z] for x in axis for y in axis for z in axis], dtype=np.float32)
412
+
413
+
414
+ def test_volume_sample_linear_f_gradient(test, device):
415
+ points = rng.uniform(-10.0, 10.0, size=(100, 3))
416
+ values = wp.array(np.zeros(1), dtype=wp.float32, device=device, requires_grad=True)
417
+ for test_case in points:
418
+ uvws = wp.array(test_case, dtype=wp.vec3, device=device, requires_grad=True)
419
+ xyzs = wp.array(test_case * 0.25, dtype=wp.vec3, device=device, requires_grad=True)
420
+
421
+ tape = wp.Tape()
422
+ with tape:
423
+ wp.launch(
424
+ test_volume_sample_local_f_linear_values,
425
+ dim=1,
426
+ inputs=[volumes["float"][device.alias].id, uvws, values],
427
+ device=device,
428
+ )
429
+ tape.backward(values)
430
+
431
+ x, y, z = test_case
432
+ grad_expected = np.array([y * z, x * z, x * y])
433
+ grad_computed = tape.gradients[uvws].numpy()[0]
434
+ np.testing.assert_allclose(grad_computed, grad_expected, rtol=1e-4)
435
+
436
+ tape = wp.Tape()
437
+ with tape:
438
+ wp.launch(
439
+ test_volume_sample_world_f_linear_values,
440
+ dim=1,
441
+ inputs=[volumes["float"][device.alias].id, xyzs, values],
442
+ device=device,
443
+ )
444
+ tape.backward(values)
445
+
446
+ x, y, z = test_case
447
+ grad_expected = np.array([y * z, x * z, x * y]) / 0.25
448
+ grad_computed = tape.gradients[xyzs].numpy()[0]
449
+ np.testing.assert_allclose(grad_computed, grad_expected, rtol=1e-4)
450
+
451
+
452
+ def test_volume_sample_grad_linear_f_gradient(test, device):
453
+ points = rng.uniform(-10.0, 10.0, size=(100, 3))
454
+ values = wp.array(np.zeros(1), dtype=wp.float32, device=device, requires_grad=True)
455
+ for test_case in points:
456
+ uvws = wp.array(test_case, dtype=wp.vec3, device=device, requires_grad=True)
457
+ xyzs = wp.array(test_case * 0.25, dtype=wp.vec3, device=device, requires_grad=True)
458
+
459
+ for case_num in range(4):
460
+ tape = wp.Tape()
461
+ with tape:
462
+ wp.launch(
463
+ test_volume_sample_grad_local_f_linear_values,
464
+ dim=1,
465
+ inputs=[volumes["float"][device.alias].id, uvws, values, case_num],
466
+ device=device,
467
+ )
468
+ tape.backward(values)
469
+
470
+ x, y, z = test_case
471
+ grad_computed = tape.gradients[uvws].numpy()[0]
472
+ if case_num == 0:
473
+ grad_expected = np.array([y * z, x * z, x * y])
474
+ elif case_num == 1:
475
+ grad_expected = np.array([0.0, z, y])
476
+ elif case_num == 2:
477
+ grad_expected = np.array([z, 0.0, x])
478
+ elif case_num == 3:
479
+ grad_expected = np.array([y, x, 0.0])
480
+
481
+ np.testing.assert_allclose(grad_computed, grad_expected, rtol=1e-4)
482
+ tape.zero()
483
+
484
+ for case_num in range(4):
485
+ tape = wp.Tape()
486
+ with tape:
487
+ wp.launch(
488
+ test_volume_sample_grad_world_f_linear_values,
489
+ dim=1,
490
+ inputs=[volumes["float"][device.alias].id, xyzs, values, case_num],
491
+ device=device,
492
+ )
493
+ tape.backward(values)
494
+
495
+ x, y, z = test_case
496
+ grad_computed = tape.gradients[xyzs].numpy()[0]
497
+ if case_num == 0:
498
+ grad_expected = np.array([y * z, x * z, x * y]) / 0.25
499
+ elif case_num == 1:
500
+ grad_expected = np.array([0.0, z, y]) / 0.25
501
+ elif case_num == 2:
502
+ grad_expected = np.array([z, 0.0, x]) / 0.25
503
+ elif case_num == 3:
504
+ grad_expected = np.array([y, x, 0.0]) / 0.25
505
+
506
+ np.testing.assert_allclose(grad_computed, grad_expected, rtol=1e-4)
507
+ tape.zero()
508
+
509
+
510
+ def test_volume_sample_linear_v_gradient(test, device):
511
+ points = rng.uniform(-10.0, 10.0, size=(100, 3))
512
+ values = wp.zeros(1, dtype=wp.float32, device=device, requires_grad=True)
513
+ for test_case in points:
514
+ uvws = wp.array(test_case, dtype=wp.vec3, device=device, requires_grad=True)
515
+ xyzs = wp.array(test_case * 0.25, dtype=wp.vec3, device=device, requires_grad=True)
516
+
517
+ tape = wp.Tape()
518
+ with tape:
519
+ wp.launch(
520
+ test_volume_sample_local_v_linear_values,
521
+ dim=1,
522
+ inputs=[volumes["vec3f"][device.alias].id, uvws, values],
523
+ device=device,
524
+ )
525
+ tape.backward(values)
526
+
527
+ grad_expected = np.array([12.0, 15.0, 18.0])
528
+ grad_computed = tape.gradients[uvws].numpy()[0]
529
+ np.testing.assert_allclose(grad_computed, grad_expected, rtol=1e-4)
530
+
531
+ tape = wp.Tape()
532
+ with tape:
533
+ wp.launch(
534
+ test_volume_sample_world_v_linear_values,
535
+ dim=1,
536
+ inputs=[volumes["vec3f"][device.alias].id, xyzs, values],
537
+ device=device,
538
+ )
539
+ tape.backward(values)
540
+
541
+ grad_expected = np.array([12.0, 15.0, 18.0]) / 0.25
542
+ grad_computed = tape.gradients[xyzs].numpy()[0]
543
+ np.testing.assert_allclose(grad_computed, grad_expected, rtol=1e-4)
544
+
545
+
546
+ def test_volume_transform_gradient(test, device):
547
+ values = wp.zeros(1, dtype=wp.float32, device=device, requires_grad=True)
548
+ grad_values = wp.zeros(1, dtype=wp.vec3, device=device)
549
+ test_points = rng.uniform(-10.0, 10.0, size=(10, 3))
550
+ for test_case in test_points:
551
+ points = wp.array(test_case, dtype=wp.vec3, device=device, requires_grad=True)
552
+ tape = wp.Tape()
553
+ with tape:
554
+ wp.launch(
555
+ test_volume_index_to_world,
556
+ dim=1,
557
+ inputs=[volumes["torus"][device.alias].id, points, values, grad_values],
558
+ device=device,
559
+ )
560
+ tape.backward(values)
561
+
562
+ grad_computed = tape.gradients[points].numpy()
563
+ grad_expected = grad_values.numpy()
564
+ np.testing.assert_allclose(grad_computed, grad_expected, rtol=1e-4)
565
+
566
+ grad_computed = tape.gradients[points].numpy()
567
+ grad_expected = grad_values.numpy()
568
+ np.testing.assert_allclose(grad_computed, grad_expected, rtol=1e-4)
569
+
570
+
571
+ def test_volume_store(test, device):
572
+ values_ref = np.array([x + 100 * y + 10000 * z for x, y, z in point_grid])
573
+ points = wp.array(point_grid, dtype=wp.vec3, device=device)
574
+ values = wp.empty(len(point_grid), dtype=wp.float32, device=device)
575
+ wp.launch(
576
+ test_volume_store_f,
577
+ dim=len(point_grid),
578
+ inputs=[volumes["float_write"][device.alias].id, points, values],
579
+ device=device,
580
+ )
581
+
582
+ values_res = values.numpy()
583
+ np.testing.assert_equal(values_res, values_ref)
584
+
585
+
586
+ def test_volume_allocation_f(test, device):
587
+ bg_value = -123.0
588
+ points_np = np.append(point_grid, [[8096, 8096, 8096]], axis=0)
589
+ values_ref = np.append(np.array([x + 100 * y + 10000 * z for x, y, z in point_grid]), bg_value)
590
+
591
+ volume = wp.Volume.allocate(min=[-11, -11, -11], max=[11, 11, 11], voxel_size=0.1, bg_value=bg_value, device=device)
592
+ points = wp.array(points_np, dtype=wp.vec3, device=device)
593
+ values = wp.empty(len(points_np), dtype=wp.float32, device=device)
594
+ wp.launch(test_volume_store_f, dim=len(points_np), inputs=[volume.id, points, values], device=device)
595
+
596
+ values_res = values.numpy()
597
+ np.testing.assert_equal(values_res, values_ref)
598
+
599
+
600
+ def test_volume_allocation_v(test, device):
601
+ bg_value = (-1, 2.0, -3)
602
+ points_np = np.append(point_grid, [[8096, 8096, 8096]], axis=0)
603
+ values_ref = np.append(point_grid, [bg_value], axis=0)
604
+
605
+ volume = wp.Volume.allocate(min=[-11, -11, -11], max=[11, 11, 11], voxel_size=0.1, bg_value=bg_value, device=device)
606
+ points = wp.array(points_np, dtype=wp.vec3, device=device)
607
+ values = wp.empty(len(points_np), dtype=wp.vec3, device=device)
608
+ wp.launch(test_volume_store_v, dim=len(points_np), inputs=[volume.id, points, values], device=device)
609
+
610
+ values_res = values.numpy()
611
+ np.testing.assert_equal(values_res, values_ref)
612
+
613
+
614
+ def test_volume_allocation_i(test, device):
615
+ bg_value = -123
616
+ points_np = np.append(point_grid, [[8096, 8096, 8096]], axis=0)
617
+ values_ref = np.append(np.array([x + 100 * y + 10000 * z for x, y, z in point_grid], dtype=np.int32), bg_value)
618
+
619
+ volume = wp.Volume.allocate(min=[-11, -11, -11], max=[11, 11, 11], voxel_size=0.1, bg_value=bg_value, device=device)
620
+ points = wp.array(points_np, dtype=wp.vec3, device=device)
621
+ values = wp.empty(len(points_np), dtype=wp.int32, device=device)
622
+ wp.launch(test_volume_store_i, dim=len(points_np), inputs=[volume.id, points, values], device=device)
623
+
624
+ values_res = values.numpy()
625
+ np.testing.assert_equal(values_res, values_ref)
626
+
627
+
628
+ def test_volume_introspection(test, device):
629
+ for volume_names in ("float", "vec3f"):
630
+ with test.subTest(volume_names=volume_names):
631
+ volume = volumes[volume_names][device.alias]
632
+ tiles_actual = volume.get_tiles().numpy()
633
+ tiles_sorted = tiles_actual[np.lexsort(tiles_actual.T[::-1])]
634
+ voxel_size = np.array(volume.get_voxel_size())
635
+
636
+ np.testing.assert_equal(test_volume_tiles, tiles_sorted)
637
+ np.testing.assert_equal([0.25] * 3, voxel_size)
638
+
639
+ voxel_count = volume.get_voxel_count()
640
+ voxels_actual = volume.get_voxels().numpy()
641
+ assert voxel_count == voxels_actual.shape[0]
642
+
643
+ # Voxel coordinates should be unique
644
+ voxels_unique = np.unique(voxels_actual, axis=0)
645
+ assert voxel_count == voxels_unique.shape[0]
646
+
647
+ # Get back tiles from voxels, should match get_tiles()
648
+ voxel_tiles = 8 * (voxels_unique // 8)
649
+ voxel_tiles_sorted = voxel_tiles[np.lexsort(voxel_tiles.T[::-1])]
650
+ voxel_tiles_unique = np.unique(voxel_tiles_sorted, axis=0)
651
+
652
+ np.testing.assert_equal(voxel_tiles_unique, tiles_sorted)
653
+
654
+
655
+ def test_volume_multiple_grids(test, device):
656
+ volume = volumes["index"][device.alias]
657
+
658
+ volume_2 = volume.load_next_grid()
659
+
660
+ test.assertIsNotNone(volume_2)
661
+
662
+ test.assertNotEqual(volume.id, volume_2.id)
663
+ test.assertNotEqual(volume.get_voxel_count(), volume_2.get_voxel_count())
664
+
665
+ test.assertEqual(volume.get_grid_info().grid_count, volume_2.get_grid_info().grid_count)
666
+ test.assertEqual(volume.get_grid_info().grid_index + 1, volume_2.get_grid_info().grid_index)
667
+
668
+ volume_3 = volume_2.load_next_grid()
669
+ test.assertIsNone(volume_3)
670
+
671
+
672
+ def test_volume_feature_array(test, device):
673
+ volume = volumes["index"][device.alias]
674
+
675
+ test.assertEqual(volume.get_feature_array_count(), 1)
676
+
677
+ array = volume.feature_array(0, dtype=wp.uint64)
678
+ test.assertEqual(array.device, device)
679
+ test.assertEqual(array.dtype, wp.uint64)
680
+
681
+ # fVDB convention, data starts with array ndim + shape
682
+ np.testing.assert_equal(array.numpy()[0:4], [3, volume.get_voxel_count(), 2, 3])
683
+
684
+
685
+ @wp.kernel
686
+ def fill_leaf_values_kernel(volume: wp.uint64, ijk: wp.array2d(dtype=wp.int32), values: wp.array(dtype=Any)):
687
+ tid = wp.tid()
688
+
689
+ i = ijk[tid, 0]
690
+ j = ijk[tid, 1]
691
+ k = ijk[tid, 2]
692
+
693
+ expect_eq(tid, wp.volume_lookup_index(volume, i, j, k))
694
+
695
+ values[tid] = wp.volume_lookup(volume, i, j, k, dtype=values.dtype)
696
+
697
+
698
+ @wp.kernel
699
+ def test_volume_sample_index_kernel(
700
+ volume: wp.uint64,
701
+ points: wp.array(dtype=wp.vec3),
702
+ values: wp.array(dtype=Any),
703
+ background: wp.array(dtype=Any),
704
+ sampled_values: wp.array(dtype=Any),
705
+ ):
706
+ tid = wp.tid()
707
+ p = points[tid]
708
+
709
+ ref = wp.volume_sample(volume, p, wp.Volume.LINEAR, dtype=values.dtype)
710
+ sampled_values[tid] = wp.volume_sample_index(volume, p, wp.Volume.LINEAR, values, background[0])
711
+ expect_eq(sampled_values[tid], ref)
712
+
713
+
714
+ @wp.kernel
715
+ def test_volume_sample_grad_index_kernel(
716
+ volume: wp.uint64,
717
+ points: wp.array(dtype=wp.vec3),
718
+ values: wp.array(dtype=Any),
719
+ background: wp.array(dtype=Any),
720
+ sampled_values: wp.array(dtype=Any),
721
+ sampled_grads: wp.array(dtype=Any),
722
+ ):
723
+ tid = wp.tid()
724
+ p = points[tid]
725
+
726
+ ref_grad = sampled_grads.dtype()
727
+ ref = wp.volume_sample_grad(volume, p, wp.Volume.LINEAR, ref_grad, dtype=values.dtype)
728
+
729
+ grad = type(ref_grad)()
730
+ sampled_values[tid] = wp.volume_sample_grad_index(volume, p, wp.Volume.LINEAR, values, background[0], grad)
731
+ expect_eq(sampled_values[tid], ref)
732
+
733
+ expect_eq(grad[0], ref_grad[0])
734
+ expect_eq(grad[1], ref_grad[1])
735
+ expect_eq(grad[2], ref_grad[2])
736
+ sampled_grads[tid] = grad
737
+
738
+
739
+ def test_volume_sample_index(test, device):
740
+ points = rng.uniform(-10.0, 10.0, size=(100, 3))
741
+ points[0:10, 0] += 100.0 # ensure some points are over unallocated voxels
742
+ uvws = wp.array(points, dtype=wp.vec3, device=device)
743
+
744
+ bg_values = {
745
+ "float": 10.0,
746
+ "vec3f": wp.vec3(10.8, -4.13, 10.26),
747
+ }
748
+ grad_types = {
749
+ "float": wp.vec3,
750
+ "vec3f": wp.mat33,
751
+ }
752
+
753
+ for volume_names in ("float", "vec3f"):
754
+ with test.subTest(volume_names=volume_names):
755
+ volume = volumes[volume_names][device.alias]
756
+
757
+ ijk = volume.get_voxels()
758
+
759
+ values = wp.empty(shape=volume.get_voxel_count(), dtype=volume.dtype, device=device, requires_grad=True)
760
+
761
+ vid = wp.uint64(volume.id)
762
+ wp.launch(fill_leaf_values_kernel, dim=values.shape, inputs=[vid, ijk, values], device=device)
763
+
764
+ sampled_values = wp.empty(shape=points.shape[0], dtype=volume.dtype, device=device, requires_grad=True)
765
+ background = wp.array([bg_values[volume_names]], dtype=volume.dtype, device=device, requires_grad=True)
766
+
767
+ tape = wp.Tape()
768
+ with tape:
769
+ wp.launch(
770
+ test_volume_sample_index_kernel,
771
+ dim=points.shape[0],
772
+ inputs=[vid, uvws, values, background, sampled_values],
773
+ device=device,
774
+ )
775
+
776
+ sampled_values.grad.fill_(1.0)
777
+ tape.backward()
778
+
779
+ # test adjoint w.r.t voxel and background value arrays
780
+ # we should have sum(sampled_values) = sum(adj_values * values) + (adj_background * background)
781
+ sum_sampled_values = np.sum(sampled_values.numpy(), axis=0)
782
+ sum_values_adj = np.sum(values.numpy() * values.grad.numpy(), axis=0)
783
+ sum_background_adj = background.numpy()[0] * background.grad.numpy()[0]
784
+
785
+ np.testing.assert_allclose(sum_sampled_values, sum_values_adj + sum_background_adj, rtol=1.0e-3)
786
+
787
+ tape.reset()
788
+
789
+ sampled_grads = wp.empty(
790
+ shape=points.shape[0], dtype=grad_types[volume_names], device=device, requires_grad=True
791
+ )
792
+
793
+ with tape:
794
+ wp.launch(
795
+ test_volume_sample_grad_index_kernel,
796
+ dim=points.shape[0],
797
+ inputs=[vid, uvws, values, background, sampled_values, sampled_grads],
798
+ device=device,
799
+ )
800
+
801
+ sampled_values.grad.fill_(1.0)
802
+ tape.backward()
803
+
804
+ # we should have sum(sampled_values) = sum(adj_values * values) + (adj_background * background)
805
+ sum_sampled_values = np.sum(sampled_values.numpy(), axis=0)
806
+ sum_values_adj = np.sum(values.numpy() * values.grad.numpy(), axis=0)
807
+ sum_background_adj = background.numpy()[0] * background.grad.numpy()[0]
808
+ np.testing.assert_allclose(sum_sampled_values, sum_values_adj + sum_background_adj, rtol=1.0e-3)
809
+
810
+ tape.zero()
811
+ sampled_values.grad.fill_(0.0)
812
+ sampled_grads.grad.fill_(1.0)
813
+ tape.backward()
814
+
815
+ # we should have sum(sampled_grad, axes=(0, -1)) = sum(adj_values * values) + (adj_background * background)
816
+ sum_sampled_grads = np.sum(np.sum(sampled_grads.numpy(), axis=0), axis=-1)
817
+ sum_values_adj = np.sum(values.numpy() * values.grad.numpy(), axis=0)
818
+ sum_background_adj = background.numpy()[0] * background.grad.numpy()[0]
819
+ np.testing.assert_allclose(sum_sampled_grads, sum_values_adj + sum_background_adj, rtol=1.0e-3)
820
+
821
+
822
+ def test_volume_from_numpy(test, device):
823
+ # Volume.allocate_from_tiles() is only available with CUDA
824
+ mins = np.array([-3.0, -3.0, -3.0])
825
+ voxel_size = 0.2
826
+ maxs = np.array([3.0, 3.0, 3.0])
827
+ nums = np.ceil((maxs - mins) / (voxel_size)).astype(dtype=int)
828
+ center = np.array([0.0, 0.0, 0.0])
829
+ rad = 2.5
830
+ sphere_sdf_np = np.zeros(tuple(nums))
831
+ for x in range(nums[0]):
832
+ for y in range(nums[1]):
833
+ for z in range(nums[2]):
834
+ pos = mins + voxel_size * np.array([x, y, z])
835
+ dis = np.linalg.norm(pos - center)
836
+ sphere_sdf_np[x, y, z] = dis - rad
837
+ sphere_vdb = wp.Volume.load_from_numpy(sphere_sdf_np, mins, voxel_size, rad + 3.0 * voxel_size, device=device)
838
+
839
+ test.assertNotEqual(sphere_vdb.id, 0)
840
+
841
+ sphere_vdb_array = sphere_vdb.array()
842
+ test.assertEqual(sphere_vdb_array.dtype, wp.uint8)
843
+ test.assertIsNone(sphere_vdb_array.deleter)
844
+
845
+
846
+ class TestVolume(unittest.TestCase):
847
+ pass
848
+
849
+
850
+ add_function_test(
851
+ TestVolume, "test_volume_sample_linear_f_gradient", test_volume_sample_linear_f_gradient, devices=devices
852
+ )
853
+ add_function_test(
854
+ TestVolume, "test_volume_sample_grad_linear_f_gradient", test_volume_sample_grad_linear_f_gradient, devices=devices
855
+ )
856
+ add_function_test(
857
+ TestVolume, "test_volume_sample_linear_v_gradient", test_volume_sample_linear_v_gradient, devices=devices
858
+ )
859
+ add_function_test(TestVolume, "test_volume_transform_gradient", test_volume_transform_gradient, devices=devices)
860
+ add_function_test(TestVolume, "test_volume_store", test_volume_store, devices=devices)
861
+ add_function_test(
862
+ TestVolume, "test_volume_allocation_f", test_volume_allocation_f, devices=get_selected_cuda_test_devices()
863
+ )
864
+ add_function_test(
865
+ TestVolume, "test_volume_allocation_v", test_volume_allocation_v, devices=get_selected_cuda_test_devices()
866
+ )
867
+ add_function_test(
868
+ TestVolume, "test_volume_allocation_i", test_volume_allocation_i, devices=get_selected_cuda_test_devices()
869
+ )
870
+ add_function_test(TestVolume, "test_volume_introspection", test_volume_introspection, devices=devices)
871
+ add_function_test(
872
+ TestVolume, "test_volume_from_numpy", test_volume_from_numpy, devices=get_selected_cuda_test_devices()
873
+ )
874
+ add_function_test(TestVolume, "test_volume_multiple_grids", test_volume_multiple_grids, devices=devices)
875
+ add_function_test(TestVolume, "test_volume_feature_array", test_volume_feature_array, devices=devices)
876
+ add_function_test(TestVolume, "test_volume_sample_index", test_volume_sample_index, devices=devices)
877
+
878
+ points = {}
879
+ points_jittered = {}
880
+ for device in devices:
881
+ points_jittered_np = point_grid + rng.uniform(-0.5, 0.5, size=point_grid.shape)
882
+ points[device.alias] = wp.array(point_grid, dtype=wp.vec3, device=device)
883
+ points_jittered[device.alias] = wp.array(points_jittered_np, dtype=wp.vec3, device=device)
884
+
885
+ add_kernel_test(
886
+ TestVolume,
887
+ test_volume_lookup_f,
888
+ dim=len(point_grid),
889
+ inputs=[volumes["float"][device.alias].id, points[device.alias]],
890
+ devices=[device],
891
+ )
892
+ add_kernel_test(
893
+ TestVolume,
894
+ test_volume_sample_closest_f,
895
+ dim=len(point_grid),
896
+ inputs=[volumes["float"][device.alias].id, points_jittered[device.alias]],
897
+ devices=[device.alias],
898
+ )
899
+ add_kernel_test(
900
+ TestVolume,
901
+ test_volume_sample_linear_f,
902
+ dim=len(point_grid),
903
+ inputs=[volumes["float"][device.alias].id, points_jittered[device.alias]],
904
+ devices=[device.alias],
905
+ )
906
+ add_kernel_test(
907
+ TestVolume,
908
+ test_volume_sample_grad_linear_f,
909
+ dim=len(point_grid),
910
+ inputs=[volumes["float"][device.alias].id, points_jittered[device.alias]],
911
+ devices=[device.alias],
912
+ )
913
+
914
+ add_kernel_test(
915
+ TestVolume,
916
+ test_volume_lookup_v,
917
+ dim=len(point_grid),
918
+ inputs=[volumes["vec3f"][device.alias].id, points[device.alias]],
919
+ devices=[device.alias],
920
+ )
921
+ add_kernel_test(
922
+ TestVolume,
923
+ test_volume_sample_closest_v,
924
+ dim=len(point_grid),
925
+ inputs=[volumes["vec3f"][device.alias].id, points_jittered[device.alias]],
926
+ devices=[device.alias],
927
+ )
928
+ add_kernel_test(
929
+ TestVolume,
930
+ test_volume_sample_linear_v,
931
+ dim=len(point_grid),
932
+ inputs=[volumes["vec3f"][device.alias].id, points_jittered[device.alias]],
933
+ devices=[device.alias],
934
+ )
935
+ add_kernel_test(
936
+ TestVolume,
937
+ test_volume_sample_grad_linear_v,
938
+ dim=len(point_grid),
939
+ inputs=[volumes["vec3f"][device.alias].id, points_jittered[device.alias]],
940
+ devices=[device.alias],
941
+ )
942
+
943
+ add_kernel_test(
944
+ TestVolume,
945
+ test_volume_lookup_i,
946
+ dim=len(point_grid),
947
+ inputs=[volumes["int32"][device.alias].id, points[device.alias]],
948
+ devices=[device.alias],
949
+ )
950
+ add_kernel_test(
951
+ TestVolume,
952
+ test_volume_sample_i,
953
+ dim=len(point_grid),
954
+ inputs=[volumes["int32"][device.alias].id, points_jittered[device.alias]],
955
+ devices=[device.alias],
956
+ )
957
+
958
+
959
+ if __name__ == "__main__":
960
+ wp.build.clear_kernel_cache()
961
+ unittest.main(verbosity=2)