warp-lang 1.0.2__py3-none-manylinux2014_aarch64.whl → 1.1.0__py3-none-manylinux2014_aarch64.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 (346) 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 +115 -113
  6. warp/build_dll.py +383 -375
  7. warp/builtins.py +3425 -3354
  8. warp/codegen.py +2878 -2792
  9. warp/config.py +40 -36
  10. warp/constants.py +45 -45
  11. warp/context.py +5194 -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 +383 -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 +146 -146
  34. warp/examples/benchmarks/benchmark_launches.py +295 -295
  35. warp/examples/browse.py +29 -29
  36. warp/examples/core/example_dem.py +234 -219
  37. warp/examples/core/example_fluid.py +293 -267
  38. warp/examples/core/example_graph_capture.py +144 -126
  39. warp/examples/core/example_marching_cubes.py +188 -174
  40. warp/examples/core/example_mesh.py +174 -155
  41. warp/examples/core/example_mesh_intersect.py +205 -193
  42. warp/examples/core/example_nvdb.py +176 -170
  43. warp/examples/core/example_raycast.py +105 -90
  44. warp/examples/core/example_raymarch.py +199 -178
  45. warp/examples/core/example_render_opengl.py +185 -141
  46. warp/examples/core/example_sph.py +405 -387
  47. warp/examples/core/example_torch.py +222 -181
  48. warp/examples/core/example_wave.py +263 -248
  49. warp/examples/fem/bsr_utils.py +378 -380
  50. warp/examples/fem/example_apic_fluid.py +407 -389
  51. warp/examples/fem/example_convection_diffusion.py +182 -168
  52. warp/examples/fem/example_convection_diffusion_dg.py +219 -209
  53. warp/examples/fem/example_convection_diffusion_dg0.py +204 -194
  54. warp/examples/fem/example_deformed_geometry.py +177 -159
  55. warp/examples/fem/example_diffusion.py +201 -173
  56. warp/examples/fem/example_diffusion_3d.py +177 -152
  57. warp/examples/fem/example_diffusion_mgpu.py +221 -214
  58. warp/examples/fem/example_mixed_elasticity.py +244 -222
  59. warp/examples/fem/example_navier_stokes.py +259 -243
  60. warp/examples/fem/example_stokes.py +220 -192
  61. warp/examples/fem/example_stokes_transfer.py +265 -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 +260 -246
  65. warp/examples/optim/example_cloth_throw.py +222 -209
  66. warp/examples/optim/example_diffray.py +566 -536
  67. warp/examples/optim/example_drone.py +864 -835
  68. warp/examples/optim/example_inverse_kinematics.py +176 -168
  69. warp/examples/optim/example_inverse_kinematics_torch.py +185 -169
  70. warp/examples/optim/example_spring_cage.py +239 -231
  71. warp/examples/optim/example_trajectory.py +223 -199
  72. warp/examples/optim/example_walker.py +306 -293
  73. warp/examples/sim/example_cartpole.py +139 -129
  74. warp/examples/sim/example_cloth.py +196 -186
  75. warp/examples/sim/example_granular.py +124 -111
  76. warp/examples/sim/example_granular_collision_sdf.py +197 -186
  77. warp/examples/sim/example_jacobian_ik.py +236 -214
  78. warp/examples/sim/example_particle_chain.py +118 -105
  79. warp/examples/sim/example_quadruped.py +193 -180
  80. warp/examples/sim/example_rigid_chain.py +197 -187
  81. warp/examples/sim/example_rigid_contact.py +189 -177
  82. warp/examples/sim/example_rigid_force.py +127 -125
  83. warp/examples/sim/example_rigid_gyroscopic.py +109 -95
  84. warp/examples/sim/example_rigid_soft_contact.py +134 -122
  85. warp/examples/sim/example_soft_body.py +190 -177
  86. warp/fabric.py +337 -335
  87. warp/fem/__init__.py +60 -27
  88. warp/fem/cache.py +401 -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 +15 -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 +744 -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 +441 -435
  104. warp/fem/geometry/hexmesh.py +953 -953
  105. warp/fem/geometry/partition.py +374 -376
  106. warp/fem/geometry/quadmesh_2d.py +532 -532
  107. warp/fem/geometry/tetmesh.py +840 -840
  108. warp/fem/geometry/trimesh_2d.py +577 -577
  109. warp/fem/integrate.py +1630 -1615
  110. warp/fem/operator.py +190 -191
  111. warp/fem/polynomial.py +214 -213
  112. warp/fem/quadrature/__init__.py +2 -2
  113. warp/fem/quadrature/pic_quadrature.py +243 -245
  114. warp/fem/quadrature/quadrature.py +295 -294
  115. warp/fem/space/__init__.py +294 -292
  116. warp/fem/space/basis_space.py +488 -489
  117. warp/fem/space/collocated_function_space.py +100 -105
  118. warp/fem/space/dof_mapper.py +236 -236
  119. warp/fem/space/function_space.py +148 -145
  120. warp/fem/space/grid_2d_function_space.py +267 -267
  121. warp/fem/space/grid_3d_function_space.py +305 -306
  122. warp/fem/space/hexmesh_function_space.py +350 -352
  123. warp/fem/space/partition.py +350 -350
  124. warp/fem/space/quadmesh_2d_function_space.py +368 -369
  125. warp/fem/space/restriction.py +158 -160
  126. warp/fem/space/shape/__init__.py +13 -15
  127. warp/fem/space/shape/cube_shape_function.py +738 -738
  128. warp/fem/space/shape/shape_function.py +102 -103
  129. warp/fem/space/shape/square_shape_function.py +611 -611
  130. warp/fem/space/shape/tet_shape_function.py +565 -567
  131. warp/fem/space/shape/triangle_shape_function.py +429 -429
  132. warp/fem/space/tetmesh_function_space.py +294 -292
  133. warp/fem/space/topology.py +297 -295
  134. warp/fem/space/trimesh_2d_function_space.py +223 -221
  135. warp/fem/types.py +77 -77
  136. warp/fem/utils.py +495 -495
  137. warp/jax.py +166 -141
  138. warp/jax_experimental.py +341 -339
  139. warp/native/array.h +1072 -1025
  140. warp/native/builtin.h +1560 -1560
  141. warp/native/bvh.cpp +398 -398
  142. warp/native/bvh.cu +525 -525
  143. warp/native/bvh.h +429 -429
  144. warp/native/clang/clang.cpp +495 -464
  145. warp/native/crt.cpp +31 -31
  146. warp/native/crt.h +334 -334
  147. warp/native/cuda_crt.h +1049 -1049
  148. warp/native/cuda_util.cpp +549 -540
  149. warp/native/cuda_util.h +288 -203
  150. warp/native/cutlass_gemm.cpp +34 -34
  151. warp/native/cutlass_gemm.cu +372 -372
  152. warp/native/error.cpp +66 -66
  153. warp/native/error.h +27 -27
  154. warp/native/fabric.h +228 -228
  155. warp/native/hashgrid.cpp +301 -278
  156. warp/native/hashgrid.cu +78 -77
  157. warp/native/hashgrid.h +227 -227
  158. warp/native/initializer_array.h +32 -32
  159. warp/native/intersect.h +1204 -1204
  160. warp/native/intersect_adj.h +365 -365
  161. warp/native/intersect_tri.h +322 -322
  162. warp/native/marching.cpp +2 -2
  163. warp/native/marching.cu +497 -497
  164. warp/native/marching.h +2 -2
  165. warp/native/mat.h +1498 -1498
  166. warp/native/matnn.h +333 -333
  167. warp/native/mesh.cpp +203 -203
  168. warp/native/mesh.cu +293 -293
  169. warp/native/mesh.h +1887 -1887
  170. warp/native/nanovdb/NanoVDB.h +4782 -4782
  171. warp/native/nanovdb/PNanoVDB.h +2553 -2553
  172. warp/native/nanovdb/PNanoVDBWrite.h +294 -294
  173. warp/native/noise.h +850 -850
  174. warp/native/quat.h +1084 -1084
  175. warp/native/rand.h +299 -299
  176. warp/native/range.h +108 -108
  177. warp/native/reduce.cpp +156 -156
  178. warp/native/reduce.cu +348 -348
  179. warp/native/runlength_encode.cpp +61 -61
  180. warp/native/runlength_encode.cu +46 -46
  181. warp/native/scan.cpp +30 -30
  182. warp/native/scan.cu +36 -36
  183. warp/native/scan.h +7 -7
  184. warp/native/solid_angle.h +442 -442
  185. warp/native/sort.cpp +94 -94
  186. warp/native/sort.cu +97 -97
  187. warp/native/sort.h +14 -14
  188. warp/native/sparse.cpp +337 -337
  189. warp/native/sparse.cu +544 -544
  190. warp/native/spatial.h +630 -630
  191. warp/native/svd.h +562 -562
  192. warp/native/temp_buffer.h +30 -30
  193. warp/native/vec.h +1132 -1132
  194. warp/native/volume.cpp +297 -297
  195. warp/native/volume.cu +32 -32
  196. warp/native/volume.h +538 -538
  197. warp/native/volume_builder.cu +425 -425
  198. warp/native/volume_builder.h +19 -19
  199. warp/native/warp.cpp +1057 -1052
  200. warp/native/warp.cu +2943 -2828
  201. warp/native/warp.h +313 -305
  202. warp/optim/__init__.py +9 -9
  203. warp/optim/adam.py +120 -120
  204. warp/optim/linear.py +1104 -939
  205. warp/optim/sgd.py +104 -92
  206. warp/render/__init__.py +10 -10
  207. warp/render/render_opengl.py +3217 -3204
  208. warp/render/render_usd.py +768 -749
  209. warp/render/utils.py +152 -150
  210. warp/sim/__init__.py +52 -59
  211. warp/sim/articulation.py +685 -685
  212. warp/sim/collide.py +1594 -1590
  213. warp/sim/import_mjcf.py +489 -481
  214. warp/sim/import_snu.py +220 -221
  215. warp/sim/import_urdf.py +536 -516
  216. warp/sim/import_usd.py +887 -881
  217. warp/sim/inertia.py +316 -317
  218. warp/sim/integrator.py +234 -233
  219. warp/sim/integrator_euler.py +1956 -1956
  220. warp/sim/integrator_featherstone.py +1910 -1991
  221. warp/sim/integrator_xpbd.py +3294 -3312
  222. warp/sim/model.py +4473 -4314
  223. warp/sim/particles.py +113 -112
  224. warp/sim/render.py +417 -403
  225. warp/sim/utils.py +413 -410
  226. warp/sparse.py +1227 -1227
  227. warp/stubs.py +2109 -2469
  228. warp/tape.py +1162 -225
  229. warp/tests/__init__.py +1 -1
  230. warp/tests/__main__.py +4 -4
  231. warp/tests/assets/torus.usda +105 -105
  232. warp/tests/aux_test_class_kernel.py +26 -26
  233. warp/tests/aux_test_compile_consts_dummy.py +10 -10
  234. warp/tests/aux_test_conditional_unequal_types_kernels.py +21 -21
  235. warp/tests/aux_test_dependent.py +22 -22
  236. warp/tests/aux_test_grad_customs.py +23 -23
  237. warp/tests/aux_test_reference.py +11 -11
  238. warp/tests/aux_test_reference_reference.py +10 -10
  239. warp/tests/aux_test_square.py +17 -17
  240. warp/tests/aux_test_unresolved_func.py +14 -14
  241. warp/tests/aux_test_unresolved_symbol.py +14 -14
  242. warp/tests/disabled_kinematics.py +239 -239
  243. warp/tests/run_coverage_serial.py +31 -31
  244. warp/tests/test_adam.py +157 -157
  245. warp/tests/test_arithmetic.py +1124 -1124
  246. warp/tests/test_array.py +2417 -2326
  247. warp/tests/test_array_reduce.py +150 -150
  248. warp/tests/test_async.py +668 -656
  249. warp/tests/test_atomic.py +141 -141
  250. warp/tests/test_bool.py +204 -149
  251. warp/tests/test_builtins_resolution.py +1292 -1292
  252. warp/tests/test_bvh.py +164 -171
  253. warp/tests/test_closest_point_edge_edge.py +228 -228
  254. warp/tests/test_codegen.py +566 -553
  255. warp/tests/test_compile_consts.py +97 -101
  256. warp/tests/test_conditional.py +246 -246
  257. warp/tests/test_copy.py +232 -215
  258. warp/tests/test_ctypes.py +632 -632
  259. warp/tests/test_dense.py +67 -67
  260. warp/tests/test_devices.py +91 -98
  261. warp/tests/test_dlpack.py +530 -529
  262. warp/tests/test_examples.py +400 -378
  263. warp/tests/test_fabricarray.py +955 -955
  264. warp/tests/test_fast_math.py +62 -54
  265. warp/tests/test_fem.py +1277 -1278
  266. warp/tests/test_fp16.py +130 -130
  267. warp/tests/test_func.py +338 -337
  268. warp/tests/test_generics.py +571 -571
  269. warp/tests/test_grad.py +746 -640
  270. warp/tests/test_grad_customs.py +333 -336
  271. warp/tests/test_hash_grid.py +210 -164
  272. warp/tests/test_import.py +39 -39
  273. warp/tests/test_indexedarray.py +1134 -1134
  274. warp/tests/test_intersect.py +67 -67
  275. warp/tests/test_jax.py +307 -307
  276. warp/tests/test_large.py +167 -164
  277. warp/tests/test_launch.py +354 -354
  278. warp/tests/test_lerp.py +261 -261
  279. warp/tests/test_linear_solvers.py +191 -171
  280. warp/tests/test_lvalue.py +421 -493
  281. warp/tests/test_marching_cubes.py +65 -65
  282. warp/tests/test_mat.py +1801 -1827
  283. warp/tests/test_mat_lite.py +115 -115
  284. warp/tests/test_mat_scalar_ops.py +2907 -2889
  285. warp/tests/test_math.py +126 -193
  286. warp/tests/test_matmul.py +500 -499
  287. warp/tests/test_matmul_lite.py +410 -410
  288. warp/tests/test_mempool.py +188 -190
  289. warp/tests/test_mesh.py +284 -324
  290. warp/tests/test_mesh_query_aabb.py +228 -241
  291. warp/tests/test_mesh_query_point.py +692 -702
  292. warp/tests/test_mesh_query_ray.py +292 -303
  293. warp/tests/test_mlp.py +276 -276
  294. warp/tests/test_model.py +110 -110
  295. warp/tests/test_modules_lite.py +39 -39
  296. warp/tests/test_multigpu.py +163 -163
  297. warp/tests/test_noise.py +248 -248
  298. warp/tests/test_operators.py +250 -250
  299. warp/tests/test_options.py +123 -125
  300. warp/tests/test_peer.py +133 -137
  301. warp/tests/test_pinned.py +78 -78
  302. warp/tests/test_print.py +54 -54
  303. warp/tests/test_quat.py +2086 -2086
  304. warp/tests/test_rand.py +288 -288
  305. warp/tests/test_reload.py +217 -217
  306. warp/tests/test_rounding.py +179 -179
  307. warp/tests/test_runlength_encode.py +190 -190
  308. warp/tests/test_sim_grad.py +243 -0
  309. warp/tests/test_sim_kinematics.py +91 -97
  310. warp/tests/test_smoothstep.py +168 -168
  311. warp/tests/test_snippet.py +305 -266
  312. warp/tests/test_sparse.py +468 -460
  313. warp/tests/test_spatial.py +2148 -2148
  314. warp/tests/test_streams.py +486 -473
  315. warp/tests/test_struct.py +710 -675
  316. warp/tests/test_tape.py +173 -148
  317. warp/tests/test_torch.py +743 -743
  318. warp/tests/test_transient_module.py +87 -87
  319. warp/tests/test_types.py +556 -659
  320. warp/tests/test_utils.py +490 -499
  321. warp/tests/test_vec.py +1264 -1268
  322. warp/tests/test_vec_lite.py +73 -73
  323. warp/tests/test_vec_scalar_ops.py +2099 -2099
  324. warp/tests/test_verify_fp.py +94 -94
  325. warp/tests/test_volume.py +737 -736
  326. warp/tests/test_volume_write.py +255 -265
  327. warp/tests/unittest_serial.py +37 -37
  328. warp/tests/unittest_suites.py +363 -359
  329. warp/tests/unittest_utils.py +603 -578
  330. warp/tests/unused_test_misc.py +71 -71
  331. warp/tests/walkthrough_debug.py +85 -85
  332. warp/thirdparty/appdirs.py +598 -598
  333. warp/thirdparty/dlpack.py +143 -143
  334. warp/thirdparty/unittest_parallel.py +566 -561
  335. warp/torch.py +321 -295
  336. warp/types.py +4504 -4450
  337. warp/utils.py +1008 -821
  338. {warp_lang-1.0.2.dist-info → warp_lang-1.1.0.dist-info}/LICENSE.md +126 -126
  339. {warp_lang-1.0.2.dist-info → warp_lang-1.1.0.dist-info}/METADATA +338 -400
  340. warp_lang-1.1.0.dist-info/RECORD +352 -0
  341. warp/examples/assets/cube.usda +0 -42
  342. warp/examples/assets/sphere.usda +0 -56
  343. warp/examples/assets/torus.usda +0 -105
  344. warp_lang-1.0.2.dist-info/RECORD +0 -352
  345. {warp_lang-1.0.2.dist-info → warp_lang-1.1.0.dist-info}/WHEEL +0 -0
  346. {warp_lang-1.0.2.dist-info → warp_lang-1.1.0.dist-info}/top_level.txt +0 -0
warp/tests/test_volume.py CHANGED
@@ -1,736 +1,737 @@
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
+
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
+ if case_num == 0:
109
+ values[tid] = val
110
+ elif case_num == 1:
111
+ values[tid] = grad[0]
112
+ elif case_num == 2:
113
+ values[tid] = grad[1]
114
+ elif case_num == 3:
115
+ values[tid] = grad[2]
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
+ if case_num == 0:
139
+ values[tid] = val
140
+ elif case_num == 1:
141
+ values[tid] = grad[0]
142
+ elif case_num == 2:
143
+ values[tid] = grad[1]
144
+ elif case_num == 3:
145
+ values[tid] = grad[2]
146
+
147
+
148
+ # vec3f volume tests
149
+ @wp.kernel
150
+ def test_volume_lookup_v(volume: wp.uint64, points: wp.array(dtype=wp.vec3)):
151
+ tid = wp.tid()
152
+
153
+ p = points[tid]
154
+ expected = wp.vec3(
155
+ 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]
156
+ )
157
+ if abs(p[0]) > 10.0 or abs(p[1]) > 10.0 or abs(p[2]) > 10.0:
158
+ expected = wp.vec3(10.8, -4.13, 10.26)
159
+
160
+ i = int(p[0])
161
+ j = int(p[1])
162
+ k = int(p[2])
163
+
164
+ expect_eq(wp.volume_lookup_v(volume, i, j, k), expected)
165
+
166
+
167
+ @wp.kernel
168
+ def test_volume_sample_closest_v(volume: wp.uint64, points: wp.array(dtype=wp.vec3)):
169
+ tid = wp.tid()
170
+
171
+ p = points[tid]
172
+ i = round(p[0])
173
+ j = round(p[1])
174
+ k = round(p[2])
175
+ 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)
176
+ if abs(i) > 10.0 or abs(j) > 10.0 or abs(k) > 10.0:
177
+ expected = wp.vec3(10.8, -4.13, 10.26)
178
+
179
+ expect_eq(wp.volume_sample_v(volume, p, wp.Volume.CLOSEST), expected)
180
+
181
+ q = wp.volume_index_to_world(volume, p)
182
+ q_inv = wp.volume_world_to_index(volume, q)
183
+ expect_eq(p, q_inv)
184
+
185
+
186
+ @wp.kernel
187
+ def test_volume_sample_linear_v(volume: wp.uint64, points: wp.array(dtype=wp.vec3)):
188
+ tid = wp.tid()
189
+
190
+ p = points[tid]
191
+
192
+ expected = wp.vec3(
193
+ 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]
194
+ )
195
+ if abs(p[0]) > 10.0 or abs(p[1]) > 10.0 or abs(p[2]) > 10.0:
196
+ return # not testing against background values
197
+
198
+ expect_near(wp.volume_sample_v(volume, p, wp.Volume.LINEAR), expected, 2.0e-4)
199
+
200
+
201
+ @wp.kernel
202
+ def test_volume_sample_local_v_linear_values(
203
+ volume: wp.uint64, points: wp.array(dtype=wp.vec3), values: wp.array(dtype=wp.float32)
204
+ ):
205
+ tid = wp.tid()
206
+ p = points[tid]
207
+ ones = wp.vec3(1.0, 1.0, 1.0)
208
+ values[tid] = wp.dot(wp.volume_sample_v(volume, p, wp.Volume.LINEAR), ones)
209
+
210
+
211
+ @wp.kernel
212
+ def test_volume_sample_world_v_linear_values(
213
+ volume: wp.uint64, points: wp.array(dtype=wp.vec3), values: wp.array(dtype=wp.float32)
214
+ ):
215
+ tid = wp.tid()
216
+ q = points[tid]
217
+ p = wp.volume_world_to_index(volume, q)
218
+ ones = wp.vec3(1.0, 1.0, 1.0)
219
+ values[tid] = wp.dot(wp.volume_sample_v(volume, p, wp.Volume.LINEAR), ones)
220
+
221
+
222
+ # int32 volume tests
223
+ @wp.kernel
224
+ def test_volume_lookup_i(volume: wp.uint64, points: wp.array(dtype=wp.vec3)):
225
+ tid = wp.tid()
226
+
227
+ p = points[tid]
228
+ i = int(p[0])
229
+ j = int(p[1])
230
+ k = int(p[2])
231
+ expected = i * j * k
232
+ if abs(i) > 10 or abs(j) > 10 or abs(k) > 10:
233
+ expected = 10
234
+
235
+ expect_eq(wp.volume_lookup_i(volume, i, j, k), expected)
236
+
237
+
238
+ @wp.kernel
239
+ def test_volume_sample_i(volume: wp.uint64, points: wp.array(dtype=wp.vec3)):
240
+ tid = wp.tid()
241
+
242
+ p = points[tid]
243
+ i = round(p[0])
244
+ j = round(p[1])
245
+ k = round(p[2])
246
+ expected = int(i * j * k)
247
+ if abs(i) > 10.0 or abs(j) > 10.0 or abs(k) > 10.0:
248
+ expected = 10
249
+
250
+ expect_eq(wp.volume_sample_i(volume, p), expected)
251
+
252
+ q = wp.volume_index_to_world(volume, p)
253
+ q_inv = wp.volume_world_to_index(volume, q)
254
+ expect_eq(p, q_inv)
255
+
256
+
257
+ # Index/world transformation tests
258
+ @wp.kernel
259
+ def test_volume_index_to_world(
260
+ volume: wp.uint64,
261
+ points: wp.array(dtype=wp.vec3),
262
+ values: wp.array(dtype=wp.float32),
263
+ grad_values: wp.array(dtype=wp.vec3),
264
+ ):
265
+ tid = wp.tid()
266
+ p = points[tid]
267
+ ones = wp.vec3(1.0, 1.0, 1.0)
268
+ values[tid] = wp.dot(wp.volume_index_to_world(volume, p), ones)
269
+ grad_values[tid] = wp.volume_index_to_world_dir(volume, ones)
270
+
271
+
272
+ @wp.kernel
273
+ def test_volume_world_to_index(
274
+ volume: wp.uint64,
275
+ points: wp.array(dtype=wp.vec3),
276
+ values: wp.array(dtype=wp.float32),
277
+ grad_values: wp.array(dtype=wp.vec3),
278
+ ):
279
+ tid = wp.tid()
280
+ p = points[tid]
281
+ ones = wp.vec3(1.0, 1.0, 1.0)
282
+ values[tid] = wp.dot(wp.volume_world_to_index(volume, p), ones)
283
+ grad_values[tid] = wp.volume_world_to_index_dir(volume, ones)
284
+
285
+
286
+ # Volume write tests
287
+ @wp.kernel
288
+ def test_volume_store_f(volume: wp.uint64, points: wp.array(dtype=wp.vec3), values: wp.array(dtype=wp.float32)):
289
+ tid = wp.tid()
290
+
291
+ p = points[tid]
292
+ i = int(p[0])
293
+ j = int(p[1])
294
+ k = int(p[2])
295
+
296
+ # NB: Writing outside the allocated domain overwrites the background value of the Volume
297
+ if abs(i) <= 11 and abs(j) <= 11 and abs(k) <= 11:
298
+ wp.volume_store_f(volume, i, j, k, float(i + 100 * j + 10000 * k))
299
+ values[tid] = wp.volume_lookup_f(volume, i, j, k)
300
+
301
+
302
+ @wp.kernel
303
+ def test_volume_store_v(volume: wp.uint64, points: wp.array(dtype=wp.vec3), values: wp.array(dtype=wp.vec3)):
304
+ tid = wp.tid()
305
+
306
+ p = points[tid]
307
+ i = int(p[0])
308
+ j = int(p[1])
309
+ k = int(p[2])
310
+
311
+ # NB: Writing outside the allocated domain overwrites the background value of the Volume
312
+ if abs(i) <= 11 and abs(j) <= 11 and abs(k) <= 11:
313
+ wp.volume_store_v(volume, i, j, k, p)
314
+ values[tid] = wp.volume_lookup_v(volume, i, j, k)
315
+
316
+
317
+ @wp.kernel
318
+ def test_volume_store_i(volume: wp.uint64, points: wp.array(dtype=wp.vec3), values: wp.array(dtype=wp.int32)):
319
+ tid = wp.tid()
320
+
321
+ p = points[tid]
322
+ i = int(p[0])
323
+ j = int(p[1])
324
+ k = int(p[2])
325
+
326
+ # NB: Writing outside the allocated domain overwrites the background value of the Volume
327
+ if abs(i) <= 11 and abs(j) <= 11 and abs(k) <= 11:
328
+ wp.volume_store_i(volume, i, j, k, i + 100 * j + 10000 * k)
329
+ values[tid] = wp.volume_lookup_i(volume, i, j, k)
330
+
331
+
332
+ devices = get_test_devices()
333
+ rng = np.random.default_rng(101215)
334
+
335
+ # Note about the test grids:
336
+ # test_grid and test_int32_grid
337
+ # active region: [-10,10]^3
338
+ # values: v[i,j,k] = i * j * k
339
+ # voxel size: 0.25
340
+ #
341
+ # test_vec_grid
342
+ # active region: [-10,10]^3
343
+ # values: v[i,j,k] = (i + 2*j + 3*k, 4*i + 5*j + 6*k, 7*i + 8*j + 9*k)
344
+ # voxel size: 0.25
345
+ #
346
+ # torus
347
+ # index to world transformation:
348
+ # [0.1, 0, 0, 0]
349
+ # [0, 0, 0.1, 0]
350
+ # [0, 0.1, 0, 0]
351
+ # [1, 2, 3, 1]
352
+ # (-90 degrees rotation along X)
353
+ # voxel size: 0.1
354
+ volume_paths = {
355
+ "float": os.path.abspath(os.path.join(os.path.dirname(__file__), "assets/test_grid.nvdb")),
356
+ "int32": os.path.abspath(os.path.join(os.path.dirname(__file__), "assets/test_int32_grid.nvdb")),
357
+ "vec3f": os.path.abspath(os.path.join(os.path.dirname(__file__), "assets/test_vec_grid.nvdb")),
358
+ "torus": os.path.abspath(os.path.join(os.path.dirname(__file__), "assets/torus.nvdb")),
359
+ "float_write": os.path.abspath(os.path.join(os.path.dirname(__file__), "assets/test_grid.nvdb")),
360
+ }
361
+
362
+ test_volume_tiles = (
363
+ 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
364
+ )
365
+
366
+ volumes = {}
367
+ for value_type, path in volume_paths.items():
368
+ volumes[value_type] = {}
369
+ volume_data = open(path, "rb").read()
370
+ for device in devices:
371
+ try:
372
+ volume = wp.Volume.load_from_nvdb(volume_data, device)
373
+ except RuntimeError as e:
374
+ raise RuntimeError(f'Failed to load volume from "{path}" to {device} memory:\n{e}') from e
375
+
376
+ volumes[value_type][device.alias] = volume
377
+
378
+ axis = np.linspace(-1, 1, 3)
379
+ point_grid = np.array([[x, y, z] for x in axis for y in axis for z in axis], dtype=np.float32)
380
+
381
+
382
+ def test_volume_sample_linear_f_gradient(test, device):
383
+ points = rng.uniform(-10.0, 10.0, size=(100, 3))
384
+ values = wp.array(np.zeros(1), dtype=wp.float32, device=device, requires_grad=True)
385
+ for test_case in points:
386
+ uvws = wp.array(test_case, dtype=wp.vec3, device=device, requires_grad=True)
387
+ xyzs = wp.array(test_case * 0.25, dtype=wp.vec3, device=device, requires_grad=True)
388
+
389
+ tape = wp.Tape()
390
+ with tape:
391
+ wp.launch(
392
+ test_volume_sample_local_f_linear_values,
393
+ dim=1,
394
+ inputs=[volumes["float"][device.alias].id, uvws, values],
395
+ device=device,
396
+ )
397
+ tape.backward(values)
398
+
399
+ x, y, z = test_case
400
+ grad_expected = np.array([y * z, x * z, x * y])
401
+ grad_computed = tape.gradients[uvws].numpy()[0]
402
+ np.testing.assert_allclose(grad_computed, grad_expected, rtol=1e-4)
403
+
404
+ tape = wp.Tape()
405
+ with tape:
406
+ wp.launch(
407
+ test_volume_sample_world_f_linear_values,
408
+ dim=1,
409
+ inputs=[volumes["float"][device.alias].id, xyzs, values],
410
+ device=device,
411
+ )
412
+ tape.backward(values)
413
+
414
+ x, y, z = test_case
415
+ grad_expected = np.array([y * z, x * z, x * y]) / 0.25
416
+ grad_computed = tape.gradients[xyzs].numpy()[0]
417
+ np.testing.assert_allclose(grad_computed, grad_expected, rtol=1e-4)
418
+
419
+
420
+ def test_volume_sample_grad_linear_f_gradient(test, device):
421
+ points = rng.uniform(-10.0, 10.0, size=(100, 3))
422
+ values = wp.array(np.zeros(1), dtype=wp.float32, device=device, requires_grad=True)
423
+ for test_case in points:
424
+ uvws = wp.array(test_case, dtype=wp.vec3, device=device, requires_grad=True)
425
+ xyzs = wp.array(test_case * 0.25, dtype=wp.vec3, device=device, requires_grad=True)
426
+
427
+ for case_num in range(4):
428
+ tape = wp.Tape()
429
+ with tape:
430
+ wp.launch(
431
+ test_volume_sample_grad_local_f_linear_values,
432
+ dim=1,
433
+ inputs=[volumes["float"][device.alias].id, uvws, values, case_num],
434
+ device=device,
435
+ )
436
+ tape.backward(values)
437
+
438
+ x, y, z = test_case
439
+ grad_computed = tape.gradients[uvws].numpy()[0]
440
+ if case_num == 0:
441
+ grad_expected = np.array([y * z, x * z, x * y])
442
+ elif case_num == 1:
443
+ grad_expected = np.array([0.0, z, y])
444
+ elif case_num == 2:
445
+ grad_expected = np.array([z, 0.0, x])
446
+ elif case_num == 3:
447
+ grad_expected = np.array([y, x, 0.0])
448
+
449
+ np.testing.assert_allclose(grad_computed, grad_expected, rtol=1e-4)
450
+ tape.zero()
451
+
452
+ for case_num in range(4):
453
+ tape = wp.Tape()
454
+ with tape:
455
+ wp.launch(
456
+ test_volume_sample_grad_world_f_linear_values,
457
+ dim=1,
458
+ inputs=[volumes["float"][device.alias].id, xyzs, values, case_num],
459
+ device=device,
460
+ )
461
+ tape.backward(values)
462
+
463
+ x, y, z = test_case
464
+ grad_computed = tape.gradients[xyzs].numpy()[0]
465
+ if case_num == 0:
466
+ grad_expected = np.array([y * z, x * z, x * y]) / 0.25
467
+ elif case_num == 1:
468
+ grad_expected = np.array([0.0, z, y]) / 0.25
469
+ elif case_num == 2:
470
+ grad_expected = np.array([z, 0.0, x]) / 0.25
471
+ elif case_num == 3:
472
+ grad_expected = np.array([y, x, 0.0]) / 0.25
473
+
474
+ np.testing.assert_allclose(grad_computed, grad_expected, rtol=1e-4)
475
+ tape.zero()
476
+
477
+
478
+ def test_volume_sample_linear_v_gradient(test, device):
479
+ points = rng.uniform(-10.0, 10.0, size=(100, 3))
480
+ values = wp.zeros(1, dtype=wp.float32, device=device, requires_grad=True)
481
+ for test_case in points:
482
+ uvws = wp.array(test_case, dtype=wp.vec3, device=device, requires_grad=True)
483
+ xyzs = wp.array(test_case * 0.25, dtype=wp.vec3, device=device, requires_grad=True)
484
+
485
+ tape = wp.Tape()
486
+ with tape:
487
+ wp.launch(
488
+ test_volume_sample_local_v_linear_values,
489
+ dim=1,
490
+ inputs=[volumes["vec3f"][device.alias].id, uvws, values],
491
+ device=device,
492
+ )
493
+ tape.backward(values)
494
+
495
+ grad_expected = np.array([6.0, 15.0, 24.0])
496
+ grad_computed = tape.gradients[uvws].numpy()[0]
497
+ np.testing.assert_allclose(grad_computed, grad_expected, rtol=1e-4)
498
+
499
+ tape = wp.Tape()
500
+ with tape:
501
+ wp.launch(
502
+ test_volume_sample_world_v_linear_values,
503
+ dim=1,
504
+ inputs=[volumes["vec3f"][device.alias].id, xyzs, values],
505
+ device=device,
506
+ )
507
+ tape.backward(values)
508
+
509
+ grad_expected = np.array([6.0, 15.0, 24.0]) / 0.25
510
+ grad_computed = tape.gradients[xyzs].numpy()[0]
511
+ np.testing.assert_allclose(grad_computed, grad_expected, rtol=1e-4)
512
+
513
+
514
+ def test_volume_transform_gradient(test, device):
515
+ values = wp.zeros(1, dtype=wp.float32, device=device, requires_grad=True)
516
+ grad_values = wp.zeros(1, dtype=wp.vec3, device=device)
517
+ test_points = rng.uniform(-10.0, 10.0, size=(10, 3))
518
+ for test_case in test_points:
519
+ points = wp.array(test_case, dtype=wp.vec3, device=device, requires_grad=True)
520
+ tape = wp.Tape()
521
+ with tape:
522
+ wp.launch(
523
+ test_volume_index_to_world,
524
+ dim=1,
525
+ inputs=[volumes["torus"][device.alias].id, points, values, grad_values],
526
+ device=device,
527
+ )
528
+ tape.backward(values)
529
+
530
+ grad_computed = tape.gradients[points].numpy()
531
+ grad_expected = grad_values.numpy()
532
+ np.testing.assert_allclose(grad_computed, grad_expected, rtol=1e-4)
533
+
534
+ grad_computed = tape.gradients[points].numpy()
535
+ grad_expected = grad_values.numpy()
536
+ np.testing.assert_allclose(grad_computed, grad_expected, rtol=1e-4)
537
+
538
+
539
+ def test_volume_store(test, device):
540
+ values_ref = np.array([x + 100 * y + 10000 * z for x, y, z in point_grid])
541
+ points = wp.array(point_grid, dtype=wp.vec3, device=device)
542
+ values = wp.empty(len(point_grid), dtype=wp.float32, device=device)
543
+ wp.launch(
544
+ test_volume_store_f,
545
+ dim=len(point_grid),
546
+ inputs=[volumes["float_write"][device.alias].id, points, values],
547
+ device=device,
548
+ )
549
+
550
+ values_res = values.numpy()
551
+ np.testing.assert_equal(values_res, values_ref)
552
+
553
+
554
+ def test_volume_allocation_f(test, device):
555
+ bg_value = -123.0
556
+ points_np = np.append(point_grid, [[8096, 8096, 8096]], axis=0)
557
+ values_ref = np.append(np.array([x + 100 * y + 10000 * z for x, y, z in point_grid]), bg_value)
558
+
559
+ volume = wp.Volume.allocate(min=[-11, -11, -11], max=[11, 11, 11], voxel_size=0.1, bg_value=bg_value, device=device)
560
+ points = wp.array(points_np, dtype=wp.vec3, device=device)
561
+ values = wp.empty(len(points_np), dtype=wp.float32, device=device)
562
+ wp.launch(test_volume_store_f, dim=len(points_np), inputs=[volume.id, points, values], device=device)
563
+
564
+ values_res = values.numpy()
565
+ np.testing.assert_equal(values_res, values_ref)
566
+
567
+
568
+ def test_volume_allocation_v(test, device):
569
+ bg_value = (-1, 2.0, -3)
570
+ points_np = np.append(point_grid, [[8096, 8096, 8096]], axis=0)
571
+ values_ref = np.append(point_grid, [bg_value], axis=0)
572
+
573
+ volume = wp.Volume.allocate(min=[-11, -11, -11], max=[11, 11, 11], voxel_size=0.1, bg_value=bg_value, device=device)
574
+ points = wp.array(points_np, dtype=wp.vec3, device=device)
575
+ values = wp.empty(len(points_np), dtype=wp.vec3, device=device)
576
+ wp.launch(test_volume_store_v, dim=len(points_np), inputs=[volume.id, points, values], device=device)
577
+
578
+ values_res = values.numpy()
579
+ np.testing.assert_equal(values_res, values_ref)
580
+
581
+
582
+ def test_volume_allocation_i(test, device):
583
+ bg_value = -123
584
+ points_np = np.append(point_grid, [[8096, 8096, 8096]], axis=0)
585
+ values_ref = np.append(np.array([x + 100 * y + 10000 * z for x, y, z in point_grid], dtype=np.int32), bg_value)
586
+
587
+ volume = wp.Volume.allocate(min=[-11, -11, -11], max=[11, 11, 11], voxel_size=0.1, bg_value=bg_value, device=device)
588
+ points = wp.array(points_np, dtype=wp.vec3, device=device)
589
+ values = wp.empty(len(points_np), dtype=wp.int32, device=device)
590
+ wp.launch(test_volume_store_i, dim=len(points_np), inputs=[volume.id, points, values], device=device)
591
+
592
+ values_res = values.numpy()
593
+ np.testing.assert_equal(values_res, values_ref)
594
+
595
+
596
+ def test_volume_introspection(test, device):
597
+ for volume_names in ("float", "vec3f"):
598
+ with test.subTest(volume_names=volume_names):
599
+ volume = volumes[volume_names][device.alias]
600
+ tiles_actual = volume.get_tiles().numpy()
601
+ tiles_sorted = tiles_actual[np.lexsort(tiles_actual.T[::-1])]
602
+ voxel_size = np.array(volume.get_voxel_size())
603
+
604
+ np.testing.assert_equal(test_volume_tiles, tiles_sorted)
605
+ np.testing.assert_equal([0.25] * 3, voxel_size)
606
+
607
+
608
+ def test_volume_from_numpy(test, device):
609
+ # Volume.allocate_from_tiles() is only available with CUDA
610
+ mins = np.array([-3.0, -3.0, -3.0])
611
+ voxel_size = 0.2
612
+ maxs = np.array([3.0, 3.0, 3.0])
613
+ nums = np.ceil((maxs - mins) / (voxel_size)).astype(dtype=int)
614
+ center = np.array([0.0, 0.0, 0.0])
615
+ rad = 2.5
616
+ sphere_sdf_np = np.zeros(tuple(nums))
617
+ for x in range(nums[0]):
618
+ for y in range(nums[1]):
619
+ for z in range(nums[2]):
620
+ pos = mins + voxel_size * np.array([x, y, z])
621
+ dis = np.linalg.norm(pos - center)
622
+ sphere_sdf_np[x, y, z] = dis - rad
623
+ sphere_vdb = wp.Volume.load_from_numpy(sphere_sdf_np, mins, voxel_size, rad + 3.0 * voxel_size, device=device)
624
+
625
+ test.assertNotEqual(sphere_vdb.id, 0)
626
+
627
+ sphere_vdb_array = sphere_vdb.array()
628
+ test.assertEqual(sphere_vdb_array.dtype, wp.uint8)
629
+ test.assertIsNone(sphere_vdb_array.deleter)
630
+
631
+
632
+ class TestVolume(unittest.TestCase):
633
+ pass
634
+
635
+
636
+ add_function_test(
637
+ TestVolume, "test_volume_sample_linear_f_gradient", test_volume_sample_linear_f_gradient, devices=devices
638
+ )
639
+ add_function_test(
640
+ TestVolume, "test_volume_sample_grad_linear_f_gradient", test_volume_sample_grad_linear_f_gradient, devices=devices
641
+ )
642
+ add_function_test(
643
+ TestVolume, "test_volume_sample_linear_v_gradient", test_volume_sample_linear_v_gradient, devices=devices
644
+ )
645
+ add_function_test(TestVolume, "test_volume_transform_gradient", test_volume_transform_gradient, devices=devices)
646
+ add_function_test(TestVolume, "test_volume_store", test_volume_store, devices=devices)
647
+ add_function_test(
648
+ TestVolume, "test_volume_allocation_f", test_volume_allocation_f, devices=get_selected_cuda_test_devices()
649
+ )
650
+ add_function_test(
651
+ TestVolume, "test_volume_allocation_v", test_volume_allocation_v, devices=get_selected_cuda_test_devices()
652
+ )
653
+ add_function_test(
654
+ TestVolume, "test_volume_allocation_i", test_volume_allocation_i, devices=get_selected_cuda_test_devices()
655
+ )
656
+ add_function_test(TestVolume, "test_volume_introspection", test_volume_introspection, devices=devices)
657
+ add_function_test(
658
+ TestVolume, "test_volume_from_numpy", test_volume_from_numpy, devices=get_selected_cuda_test_devices()
659
+ )
660
+
661
+ points = {}
662
+ points_jittered = {}
663
+ for device in devices:
664
+ points_jittered_np = point_grid + rng.uniform(-0.5, 0.5, size=point_grid.shape)
665
+ points[device.alias] = wp.array(point_grid, dtype=wp.vec3, device=device)
666
+ points_jittered[device.alias] = wp.array(points_jittered_np, dtype=wp.vec3, device=device)
667
+
668
+ add_kernel_test(
669
+ TestVolume,
670
+ test_volume_lookup_f,
671
+ dim=len(point_grid),
672
+ inputs=[volumes["float"][device.alias].id, points[device.alias]],
673
+ devices=[device],
674
+ )
675
+ add_kernel_test(
676
+ TestVolume,
677
+ test_volume_sample_closest_f,
678
+ dim=len(point_grid),
679
+ inputs=[volumes["float"][device.alias].id, points_jittered[device.alias]],
680
+ devices=[device.alias],
681
+ )
682
+ add_kernel_test(
683
+ TestVolume,
684
+ test_volume_sample_linear_f,
685
+ dim=len(point_grid),
686
+ inputs=[volumes["float"][device.alias].id, points_jittered[device.alias]],
687
+ devices=[device.alias],
688
+ )
689
+ add_kernel_test(
690
+ TestVolume,
691
+ test_volume_sample_grad_linear_f,
692
+ dim=len(point_grid),
693
+ inputs=[volumes["float"][device.alias].id, points_jittered[device.alias]],
694
+ devices=[device.alias],
695
+ )
696
+
697
+ add_kernel_test(
698
+ TestVolume,
699
+ test_volume_lookup_v,
700
+ dim=len(point_grid),
701
+ inputs=[volumes["vec3f"][device.alias].id, points[device.alias]],
702
+ devices=[device.alias],
703
+ )
704
+ add_kernel_test(
705
+ TestVolume,
706
+ test_volume_sample_closest_v,
707
+ dim=len(point_grid),
708
+ inputs=[volumes["vec3f"][device.alias].id, points_jittered[device.alias]],
709
+ devices=[device.alias],
710
+ )
711
+ add_kernel_test(
712
+ TestVolume,
713
+ test_volume_sample_linear_v,
714
+ dim=len(point_grid),
715
+ inputs=[volumes["vec3f"][device.alias].id, points_jittered[device.alias]],
716
+ devices=[device.alias],
717
+ )
718
+
719
+ add_kernel_test(
720
+ TestVolume,
721
+ test_volume_lookup_i,
722
+ dim=len(point_grid),
723
+ inputs=[volumes["int32"][device.alias].id, points[device.alias]],
724
+ devices=[device.alias],
725
+ )
726
+ add_kernel_test(
727
+ TestVolume,
728
+ test_volume_sample_i,
729
+ dim=len(point_grid),
730
+ inputs=[volumes["int32"][device.alias].id, points_jittered[device.alias]],
731
+ devices=[device.alias],
732
+ )
733
+
734
+
735
+ if __name__ == "__main__":
736
+ wp.build.clear_kernel_cache()
737
+ unittest.main(verbosity=2)