warp-lang 1.7.0__py3-none-manylinux_2_28_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 (429) hide show
  1. warp/__init__.py +139 -0
  2. warp/__init__.pyi +1 -0
  3. warp/autograd.py +1142 -0
  4. warp/bin/warp-clang.so +0 -0
  5. warp/bin/warp.so +0 -0
  6. warp/build.py +557 -0
  7. warp/build_dll.py +405 -0
  8. warp/builtins.py +6855 -0
  9. warp/codegen.py +3969 -0
  10. warp/config.py +158 -0
  11. warp/constants.py +57 -0
  12. warp/context.py +6812 -0
  13. warp/dlpack.py +462 -0
  14. warp/examples/__init__.py +24 -0
  15. warp/examples/assets/bear.usd +0 -0
  16. warp/examples/assets/bunny.usd +0 -0
  17. warp/examples/assets/cartpole.urdf +110 -0
  18. warp/examples/assets/crazyflie.usd +0 -0
  19. warp/examples/assets/cube.usd +0 -0
  20. warp/examples/assets/nonuniform.usd +0 -0
  21. warp/examples/assets/nv_ant.xml +92 -0
  22. warp/examples/assets/nv_humanoid.xml +183 -0
  23. warp/examples/assets/nvidia_logo.png +0 -0
  24. warp/examples/assets/pixel.jpg +0 -0
  25. warp/examples/assets/quadruped.urdf +268 -0
  26. warp/examples/assets/rocks.nvdb +0 -0
  27. warp/examples/assets/rocks.usd +0 -0
  28. warp/examples/assets/sphere.usd +0 -0
  29. warp/examples/assets/square_cloth.usd +0 -0
  30. warp/examples/benchmarks/benchmark_api.py +389 -0
  31. warp/examples/benchmarks/benchmark_cloth.py +296 -0
  32. warp/examples/benchmarks/benchmark_cloth_cupy.py +96 -0
  33. warp/examples/benchmarks/benchmark_cloth_jax.py +105 -0
  34. warp/examples/benchmarks/benchmark_cloth_numba.py +161 -0
  35. warp/examples/benchmarks/benchmark_cloth_numpy.py +85 -0
  36. warp/examples/benchmarks/benchmark_cloth_paddle.py +94 -0
  37. warp/examples/benchmarks/benchmark_cloth_pytorch.py +94 -0
  38. warp/examples/benchmarks/benchmark_cloth_taichi.py +120 -0
  39. warp/examples/benchmarks/benchmark_cloth_warp.py +153 -0
  40. warp/examples/benchmarks/benchmark_gemm.py +164 -0
  41. warp/examples/benchmarks/benchmark_interop_paddle.py +166 -0
  42. warp/examples/benchmarks/benchmark_interop_torch.py +166 -0
  43. warp/examples/benchmarks/benchmark_launches.py +301 -0
  44. warp/examples/benchmarks/benchmark_tile_load_store.py +103 -0
  45. warp/examples/browse.py +37 -0
  46. warp/examples/core/example_cupy.py +86 -0
  47. warp/examples/core/example_dem.py +241 -0
  48. warp/examples/core/example_fluid.py +299 -0
  49. warp/examples/core/example_graph_capture.py +150 -0
  50. warp/examples/core/example_marching_cubes.py +194 -0
  51. warp/examples/core/example_mesh.py +180 -0
  52. warp/examples/core/example_mesh_intersect.py +211 -0
  53. warp/examples/core/example_nvdb.py +182 -0
  54. warp/examples/core/example_raycast.py +111 -0
  55. warp/examples/core/example_raymarch.py +205 -0
  56. warp/examples/core/example_render_opengl.py +193 -0
  57. warp/examples/core/example_sample_mesh.py +300 -0
  58. warp/examples/core/example_sph.py +411 -0
  59. warp/examples/core/example_torch.py +211 -0
  60. warp/examples/core/example_wave.py +269 -0
  61. warp/examples/fem/example_adaptive_grid.py +286 -0
  62. warp/examples/fem/example_apic_fluid.py +423 -0
  63. warp/examples/fem/example_burgers.py +261 -0
  64. warp/examples/fem/example_convection_diffusion.py +178 -0
  65. warp/examples/fem/example_convection_diffusion_dg.py +204 -0
  66. warp/examples/fem/example_deformed_geometry.py +172 -0
  67. warp/examples/fem/example_diffusion.py +196 -0
  68. warp/examples/fem/example_diffusion_3d.py +225 -0
  69. warp/examples/fem/example_diffusion_mgpu.py +220 -0
  70. warp/examples/fem/example_distortion_energy.py +228 -0
  71. warp/examples/fem/example_magnetostatics.py +240 -0
  72. warp/examples/fem/example_mixed_elasticity.py +291 -0
  73. warp/examples/fem/example_navier_stokes.py +261 -0
  74. warp/examples/fem/example_nonconforming_contact.py +298 -0
  75. warp/examples/fem/example_stokes.py +213 -0
  76. warp/examples/fem/example_stokes_transfer.py +262 -0
  77. warp/examples/fem/example_streamlines.py +352 -0
  78. warp/examples/fem/utils.py +1000 -0
  79. warp/examples/interop/example_jax_callable.py +116 -0
  80. warp/examples/interop/example_jax_ffi_callback.py +132 -0
  81. warp/examples/interop/example_jax_kernel.py +205 -0
  82. warp/examples/optim/example_bounce.py +266 -0
  83. warp/examples/optim/example_cloth_throw.py +228 -0
  84. warp/examples/optim/example_diffray.py +561 -0
  85. warp/examples/optim/example_drone.py +870 -0
  86. warp/examples/optim/example_fluid_checkpoint.py +497 -0
  87. warp/examples/optim/example_inverse_kinematics.py +182 -0
  88. warp/examples/optim/example_inverse_kinematics_torch.py +191 -0
  89. warp/examples/optim/example_softbody_properties.py +400 -0
  90. warp/examples/optim/example_spring_cage.py +245 -0
  91. warp/examples/optim/example_trajectory.py +227 -0
  92. warp/examples/sim/example_cartpole.py +143 -0
  93. warp/examples/sim/example_cloth.py +225 -0
  94. warp/examples/sim/example_cloth_self_contact.py +322 -0
  95. warp/examples/sim/example_granular.py +130 -0
  96. warp/examples/sim/example_granular_collision_sdf.py +202 -0
  97. warp/examples/sim/example_jacobian_ik.py +244 -0
  98. warp/examples/sim/example_particle_chain.py +124 -0
  99. warp/examples/sim/example_quadruped.py +203 -0
  100. warp/examples/sim/example_rigid_chain.py +203 -0
  101. warp/examples/sim/example_rigid_contact.py +195 -0
  102. warp/examples/sim/example_rigid_force.py +133 -0
  103. warp/examples/sim/example_rigid_gyroscopic.py +115 -0
  104. warp/examples/sim/example_rigid_soft_contact.py +140 -0
  105. warp/examples/sim/example_soft_body.py +196 -0
  106. warp/examples/tile/example_tile_cholesky.py +87 -0
  107. warp/examples/tile/example_tile_convolution.py +66 -0
  108. warp/examples/tile/example_tile_fft.py +55 -0
  109. warp/examples/tile/example_tile_filtering.py +113 -0
  110. warp/examples/tile/example_tile_matmul.py +85 -0
  111. warp/examples/tile/example_tile_mlp.py +383 -0
  112. warp/examples/tile/example_tile_nbody.py +199 -0
  113. warp/examples/tile/example_tile_walker.py +327 -0
  114. warp/fabric.py +355 -0
  115. warp/fem/__init__.py +106 -0
  116. warp/fem/adaptivity.py +508 -0
  117. warp/fem/cache.py +572 -0
  118. warp/fem/dirichlet.py +202 -0
  119. warp/fem/domain.py +411 -0
  120. warp/fem/field/__init__.py +125 -0
  121. warp/fem/field/field.py +619 -0
  122. warp/fem/field/nodal_field.py +326 -0
  123. warp/fem/field/restriction.py +37 -0
  124. warp/fem/field/virtual.py +848 -0
  125. warp/fem/geometry/__init__.py +32 -0
  126. warp/fem/geometry/adaptive_nanogrid.py +857 -0
  127. warp/fem/geometry/closest_point.py +84 -0
  128. warp/fem/geometry/deformed_geometry.py +221 -0
  129. warp/fem/geometry/element.py +776 -0
  130. warp/fem/geometry/geometry.py +362 -0
  131. warp/fem/geometry/grid_2d.py +392 -0
  132. warp/fem/geometry/grid_3d.py +452 -0
  133. warp/fem/geometry/hexmesh.py +911 -0
  134. warp/fem/geometry/nanogrid.py +571 -0
  135. warp/fem/geometry/partition.py +389 -0
  136. warp/fem/geometry/quadmesh.py +663 -0
  137. warp/fem/geometry/tetmesh.py +855 -0
  138. warp/fem/geometry/trimesh.py +806 -0
  139. warp/fem/integrate.py +2335 -0
  140. warp/fem/linalg.py +419 -0
  141. warp/fem/operator.py +293 -0
  142. warp/fem/polynomial.py +229 -0
  143. warp/fem/quadrature/__init__.py +17 -0
  144. warp/fem/quadrature/pic_quadrature.py +299 -0
  145. warp/fem/quadrature/quadrature.py +591 -0
  146. warp/fem/space/__init__.py +228 -0
  147. warp/fem/space/basis_function_space.py +468 -0
  148. warp/fem/space/basis_space.py +667 -0
  149. warp/fem/space/dof_mapper.py +251 -0
  150. warp/fem/space/function_space.py +309 -0
  151. warp/fem/space/grid_2d_function_space.py +177 -0
  152. warp/fem/space/grid_3d_function_space.py +227 -0
  153. warp/fem/space/hexmesh_function_space.py +257 -0
  154. warp/fem/space/nanogrid_function_space.py +201 -0
  155. warp/fem/space/partition.py +367 -0
  156. warp/fem/space/quadmesh_function_space.py +223 -0
  157. warp/fem/space/restriction.py +179 -0
  158. warp/fem/space/shape/__init__.py +143 -0
  159. warp/fem/space/shape/cube_shape_function.py +1105 -0
  160. warp/fem/space/shape/shape_function.py +133 -0
  161. warp/fem/space/shape/square_shape_function.py +926 -0
  162. warp/fem/space/shape/tet_shape_function.py +834 -0
  163. warp/fem/space/shape/triangle_shape_function.py +672 -0
  164. warp/fem/space/tetmesh_function_space.py +271 -0
  165. warp/fem/space/topology.py +424 -0
  166. warp/fem/space/trimesh_function_space.py +194 -0
  167. warp/fem/types.py +99 -0
  168. warp/fem/utils.py +420 -0
  169. warp/jax.py +187 -0
  170. warp/jax_experimental/__init__.py +16 -0
  171. warp/jax_experimental/custom_call.py +351 -0
  172. warp/jax_experimental/ffi.py +698 -0
  173. warp/jax_experimental/xla_ffi.py +602 -0
  174. warp/math.py +244 -0
  175. warp/native/array.h +1145 -0
  176. warp/native/builtin.h +1800 -0
  177. warp/native/bvh.cpp +492 -0
  178. warp/native/bvh.cu +791 -0
  179. warp/native/bvh.h +554 -0
  180. warp/native/clang/clang.cpp +536 -0
  181. warp/native/coloring.cpp +613 -0
  182. warp/native/crt.cpp +51 -0
  183. warp/native/crt.h +362 -0
  184. warp/native/cuda_crt.h +1058 -0
  185. warp/native/cuda_util.cpp +646 -0
  186. warp/native/cuda_util.h +307 -0
  187. warp/native/error.cpp +77 -0
  188. warp/native/error.h +36 -0
  189. warp/native/exports.h +1878 -0
  190. warp/native/fabric.h +245 -0
  191. warp/native/hashgrid.cpp +311 -0
  192. warp/native/hashgrid.cu +87 -0
  193. warp/native/hashgrid.h +240 -0
  194. warp/native/initializer_array.h +41 -0
  195. warp/native/intersect.h +1230 -0
  196. warp/native/intersect_adj.h +375 -0
  197. warp/native/intersect_tri.h +339 -0
  198. warp/native/marching.cpp +19 -0
  199. warp/native/marching.cu +514 -0
  200. warp/native/marching.h +19 -0
  201. warp/native/mat.h +2220 -0
  202. warp/native/mathdx.cpp +87 -0
  203. warp/native/matnn.h +343 -0
  204. warp/native/mesh.cpp +266 -0
  205. warp/native/mesh.cu +404 -0
  206. warp/native/mesh.h +1980 -0
  207. warp/native/nanovdb/GridHandle.h +366 -0
  208. warp/native/nanovdb/HostBuffer.h +590 -0
  209. warp/native/nanovdb/NanoVDB.h +6624 -0
  210. warp/native/nanovdb/PNanoVDB.h +3390 -0
  211. warp/native/noise.h +859 -0
  212. warp/native/quat.h +1371 -0
  213. warp/native/rand.h +342 -0
  214. warp/native/range.h +139 -0
  215. warp/native/reduce.cpp +174 -0
  216. warp/native/reduce.cu +364 -0
  217. warp/native/runlength_encode.cpp +79 -0
  218. warp/native/runlength_encode.cu +61 -0
  219. warp/native/scan.cpp +47 -0
  220. warp/native/scan.cu +53 -0
  221. warp/native/scan.h +23 -0
  222. warp/native/solid_angle.h +466 -0
  223. warp/native/sort.cpp +251 -0
  224. warp/native/sort.cu +277 -0
  225. warp/native/sort.h +33 -0
  226. warp/native/sparse.cpp +378 -0
  227. warp/native/sparse.cu +524 -0
  228. warp/native/spatial.h +657 -0
  229. warp/native/svd.h +702 -0
  230. warp/native/temp_buffer.h +46 -0
  231. warp/native/tile.h +2584 -0
  232. warp/native/tile_reduce.h +264 -0
  233. warp/native/vec.h +1426 -0
  234. warp/native/volume.cpp +501 -0
  235. warp/native/volume.cu +67 -0
  236. warp/native/volume.h +969 -0
  237. warp/native/volume_builder.cu +477 -0
  238. warp/native/volume_builder.h +52 -0
  239. warp/native/volume_impl.h +70 -0
  240. warp/native/warp.cpp +1082 -0
  241. warp/native/warp.cu +3636 -0
  242. warp/native/warp.h +381 -0
  243. warp/optim/__init__.py +17 -0
  244. warp/optim/adam.py +163 -0
  245. warp/optim/linear.py +1137 -0
  246. warp/optim/sgd.py +112 -0
  247. warp/paddle.py +407 -0
  248. warp/render/__init__.py +18 -0
  249. warp/render/render_opengl.py +3518 -0
  250. warp/render/render_usd.py +784 -0
  251. warp/render/utils.py +160 -0
  252. warp/sim/__init__.py +65 -0
  253. warp/sim/articulation.py +793 -0
  254. warp/sim/collide.py +2395 -0
  255. warp/sim/graph_coloring.py +300 -0
  256. warp/sim/import_mjcf.py +790 -0
  257. warp/sim/import_snu.py +227 -0
  258. warp/sim/import_urdf.py +579 -0
  259. warp/sim/import_usd.py +894 -0
  260. warp/sim/inertia.py +324 -0
  261. warp/sim/integrator.py +242 -0
  262. warp/sim/integrator_euler.py +1997 -0
  263. warp/sim/integrator_featherstone.py +2101 -0
  264. warp/sim/integrator_vbd.py +2048 -0
  265. warp/sim/integrator_xpbd.py +3292 -0
  266. warp/sim/model.py +4791 -0
  267. warp/sim/particles.py +121 -0
  268. warp/sim/render.py +427 -0
  269. warp/sim/utils.py +428 -0
  270. warp/sparse.py +2057 -0
  271. warp/stubs.py +3333 -0
  272. warp/tape.py +1203 -0
  273. warp/tests/__init__.py +1 -0
  274. warp/tests/__main__.py +4 -0
  275. warp/tests/assets/curlnoise_golden.npy +0 -0
  276. warp/tests/assets/mlp_golden.npy +0 -0
  277. warp/tests/assets/pixel.npy +0 -0
  278. warp/tests/assets/pnoise_golden.npy +0 -0
  279. warp/tests/assets/spiky.usd +0 -0
  280. warp/tests/assets/test_grid.nvdb +0 -0
  281. warp/tests/assets/test_index_grid.nvdb +0 -0
  282. warp/tests/assets/test_int32_grid.nvdb +0 -0
  283. warp/tests/assets/test_vec_grid.nvdb +0 -0
  284. warp/tests/assets/torus.nvdb +0 -0
  285. warp/tests/assets/torus.usda +105 -0
  286. warp/tests/aux_test_class_kernel.py +34 -0
  287. warp/tests/aux_test_compile_consts_dummy.py +18 -0
  288. warp/tests/aux_test_conditional_unequal_types_kernels.py +29 -0
  289. warp/tests/aux_test_dependent.py +29 -0
  290. warp/tests/aux_test_grad_customs.py +29 -0
  291. warp/tests/aux_test_instancing_gc.py +26 -0
  292. warp/tests/aux_test_module_unload.py +23 -0
  293. warp/tests/aux_test_name_clash1.py +40 -0
  294. warp/tests/aux_test_name_clash2.py +40 -0
  295. warp/tests/aux_test_reference.py +9 -0
  296. warp/tests/aux_test_reference_reference.py +8 -0
  297. warp/tests/aux_test_square.py +16 -0
  298. warp/tests/aux_test_unresolved_func.py +22 -0
  299. warp/tests/aux_test_unresolved_symbol.py +22 -0
  300. warp/tests/cuda/__init__.py +0 -0
  301. warp/tests/cuda/test_async.py +676 -0
  302. warp/tests/cuda/test_ipc.py +124 -0
  303. warp/tests/cuda/test_mempool.py +233 -0
  304. warp/tests/cuda/test_multigpu.py +169 -0
  305. warp/tests/cuda/test_peer.py +139 -0
  306. warp/tests/cuda/test_pinned.py +84 -0
  307. warp/tests/cuda/test_streams.py +634 -0
  308. warp/tests/geometry/__init__.py +0 -0
  309. warp/tests/geometry/test_bvh.py +200 -0
  310. warp/tests/geometry/test_hash_grid.py +221 -0
  311. warp/tests/geometry/test_marching_cubes.py +74 -0
  312. warp/tests/geometry/test_mesh.py +316 -0
  313. warp/tests/geometry/test_mesh_query_aabb.py +399 -0
  314. warp/tests/geometry/test_mesh_query_point.py +932 -0
  315. warp/tests/geometry/test_mesh_query_ray.py +311 -0
  316. warp/tests/geometry/test_volume.py +1103 -0
  317. warp/tests/geometry/test_volume_write.py +346 -0
  318. warp/tests/interop/__init__.py +0 -0
  319. warp/tests/interop/test_dlpack.py +729 -0
  320. warp/tests/interop/test_jax.py +371 -0
  321. warp/tests/interop/test_paddle.py +800 -0
  322. warp/tests/interop/test_torch.py +1001 -0
  323. warp/tests/run_coverage_serial.py +39 -0
  324. warp/tests/sim/__init__.py +0 -0
  325. warp/tests/sim/disabled_kinematics.py +244 -0
  326. warp/tests/sim/flaky_test_sim_grad.py +290 -0
  327. warp/tests/sim/test_collision.py +604 -0
  328. warp/tests/sim/test_coloring.py +258 -0
  329. warp/tests/sim/test_model.py +224 -0
  330. warp/tests/sim/test_sim_grad_bounce_linear.py +212 -0
  331. warp/tests/sim/test_sim_kinematics.py +98 -0
  332. warp/tests/sim/test_vbd.py +597 -0
  333. warp/tests/test_adam.py +163 -0
  334. warp/tests/test_arithmetic.py +1096 -0
  335. warp/tests/test_array.py +2972 -0
  336. warp/tests/test_array_reduce.py +156 -0
  337. warp/tests/test_assert.py +250 -0
  338. warp/tests/test_atomic.py +153 -0
  339. warp/tests/test_bool.py +220 -0
  340. warp/tests/test_builtins_resolution.py +1298 -0
  341. warp/tests/test_closest_point_edge_edge.py +327 -0
  342. warp/tests/test_codegen.py +810 -0
  343. warp/tests/test_codegen_instancing.py +1495 -0
  344. warp/tests/test_compile_consts.py +215 -0
  345. warp/tests/test_conditional.py +252 -0
  346. warp/tests/test_context.py +42 -0
  347. warp/tests/test_copy.py +238 -0
  348. warp/tests/test_ctypes.py +638 -0
  349. warp/tests/test_dense.py +73 -0
  350. warp/tests/test_devices.py +97 -0
  351. warp/tests/test_examples.py +482 -0
  352. warp/tests/test_fabricarray.py +996 -0
  353. warp/tests/test_fast_math.py +74 -0
  354. warp/tests/test_fem.py +2003 -0
  355. warp/tests/test_fp16.py +136 -0
  356. warp/tests/test_func.py +454 -0
  357. warp/tests/test_future_annotations.py +98 -0
  358. warp/tests/test_generics.py +656 -0
  359. warp/tests/test_grad.py +893 -0
  360. warp/tests/test_grad_customs.py +339 -0
  361. warp/tests/test_grad_debug.py +341 -0
  362. warp/tests/test_implicit_init.py +411 -0
  363. warp/tests/test_import.py +45 -0
  364. warp/tests/test_indexedarray.py +1140 -0
  365. warp/tests/test_intersect.py +73 -0
  366. warp/tests/test_iter.py +76 -0
  367. warp/tests/test_large.py +177 -0
  368. warp/tests/test_launch.py +411 -0
  369. warp/tests/test_lerp.py +151 -0
  370. warp/tests/test_linear_solvers.py +193 -0
  371. warp/tests/test_lvalue.py +427 -0
  372. warp/tests/test_mat.py +2089 -0
  373. warp/tests/test_mat_lite.py +122 -0
  374. warp/tests/test_mat_scalar_ops.py +2913 -0
  375. warp/tests/test_math.py +178 -0
  376. warp/tests/test_mlp.py +282 -0
  377. warp/tests/test_module_hashing.py +258 -0
  378. warp/tests/test_modules_lite.py +44 -0
  379. warp/tests/test_noise.py +252 -0
  380. warp/tests/test_operators.py +299 -0
  381. warp/tests/test_options.py +129 -0
  382. warp/tests/test_overwrite.py +551 -0
  383. warp/tests/test_print.py +339 -0
  384. warp/tests/test_quat.py +2315 -0
  385. warp/tests/test_rand.py +339 -0
  386. warp/tests/test_reload.py +302 -0
  387. warp/tests/test_rounding.py +185 -0
  388. warp/tests/test_runlength_encode.py +196 -0
  389. warp/tests/test_scalar_ops.py +105 -0
  390. warp/tests/test_smoothstep.py +108 -0
  391. warp/tests/test_snippet.py +318 -0
  392. warp/tests/test_sparse.py +582 -0
  393. warp/tests/test_spatial.py +2229 -0
  394. warp/tests/test_special_values.py +361 -0
  395. warp/tests/test_static.py +592 -0
  396. warp/tests/test_struct.py +734 -0
  397. warp/tests/test_tape.py +204 -0
  398. warp/tests/test_transient_module.py +93 -0
  399. warp/tests/test_triangle_closest_point.py +145 -0
  400. warp/tests/test_types.py +562 -0
  401. warp/tests/test_utils.py +588 -0
  402. warp/tests/test_vec.py +1487 -0
  403. warp/tests/test_vec_lite.py +80 -0
  404. warp/tests/test_vec_scalar_ops.py +2327 -0
  405. warp/tests/test_verify_fp.py +100 -0
  406. warp/tests/tile/__init__.py +0 -0
  407. warp/tests/tile/test_tile.py +780 -0
  408. warp/tests/tile/test_tile_load.py +407 -0
  409. warp/tests/tile/test_tile_mathdx.py +208 -0
  410. warp/tests/tile/test_tile_mlp.py +402 -0
  411. warp/tests/tile/test_tile_reduce.py +447 -0
  412. warp/tests/tile/test_tile_shared_memory.py +247 -0
  413. warp/tests/tile/test_tile_view.py +173 -0
  414. warp/tests/unittest_serial.py +47 -0
  415. warp/tests/unittest_suites.py +427 -0
  416. warp/tests/unittest_utils.py +468 -0
  417. warp/tests/walkthrough_debug.py +93 -0
  418. warp/thirdparty/__init__.py +0 -0
  419. warp/thirdparty/appdirs.py +598 -0
  420. warp/thirdparty/dlpack.py +145 -0
  421. warp/thirdparty/unittest_parallel.py +570 -0
  422. warp/torch.py +391 -0
  423. warp/types.py +5230 -0
  424. warp/utils.py +1137 -0
  425. warp_lang-1.7.0.dist-info/METADATA +516 -0
  426. warp_lang-1.7.0.dist-info/RECORD +429 -0
  427. warp_lang-1.7.0.dist-info/WHEEL +5 -0
  428. warp_lang-1.7.0.dist-info/licenses/LICENSE.md +202 -0
  429. warp_lang-1.7.0.dist-info/top_level.txt +1 -0
warp/native/mesh.h ADDED
@@ -0,0 +1,1980 @@
1
+ /*
2
+ * SPDX-FileCopyrightText: Copyright (c) 2022 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+
18
+ #pragma once
19
+
20
+ #include "builtin.h"
21
+ #include "bvh.h"
22
+ #include "intersect.h"
23
+ #include "array.h"
24
+ #include "solid_angle.h"
25
+
26
+ #define BVH_DEBUG 0
27
+
28
+ namespace wp
29
+ {
30
+
31
+ struct Mesh
32
+ {
33
+ array_t<vec3> points;
34
+ array_t<vec3> velocities;
35
+
36
+ array_t<int> indices;
37
+
38
+ vec3* lowers;
39
+ vec3* uppers;
40
+
41
+ SolidAngleProps* solid_angle_props;
42
+
43
+ int num_points;
44
+ int num_tris;
45
+
46
+ BVH bvh;
47
+
48
+ void* context;
49
+ float average_edge_length;
50
+
51
+ inline CUDA_CALLABLE Mesh(int id = 0)
52
+ {
53
+ // for backward a = 0 initialization syntax
54
+ lowers = nullptr;
55
+ uppers = nullptr;
56
+ num_points = 0;
57
+ num_tris = 0;
58
+ context = nullptr;
59
+ solid_angle_props = nullptr;
60
+ average_edge_length = 0.0f;
61
+ }
62
+
63
+ inline CUDA_CALLABLE Mesh(
64
+ array_t<vec3> points,
65
+ array_t<vec3> velocities,
66
+ array_t<int> indices,
67
+ int num_points,
68
+ int num_tris,
69
+ void* context = nullptr
70
+ ) : points(points), velocities(velocities), indices(indices), num_points(num_points), num_tris(num_tris), context(context)
71
+ {
72
+ lowers = nullptr;
73
+ uppers = nullptr;
74
+ solid_angle_props = nullptr;
75
+ average_edge_length = 0.0f;
76
+ }
77
+ };
78
+
79
+ CUDA_CALLABLE inline Mesh mesh_get(uint64_t id)
80
+ {
81
+ return *(Mesh*)(id);
82
+ }
83
+
84
+
85
+ CUDA_CALLABLE inline Mesh& operator += (Mesh& a, const Mesh& b) {
86
+ // dummy operator needed for adj_select involving meshes
87
+ return a;
88
+ }
89
+
90
+ CUDA_CALLABLE inline float distance_to_aabb_sq(const vec3& p, const vec3& lower, const vec3& upper)
91
+ {
92
+ const float dx = min(upper[0], max(lower[0], p[0])) - p[0];
93
+ const float dy = min(upper[1], max(lower[1], p[1])) - p[1];
94
+ const float dz = min(upper[2], max(lower[2], p[2])) - p[2];
95
+ return dx * dx + dy * dy + dz * dz;
96
+ }
97
+
98
+ CUDA_CALLABLE inline float furthest_distance_to_aabb_sq(const vec3& p, const vec3& lower, const vec3& upper)
99
+ {
100
+ // X-axis
101
+ float dist_lower_x = fabs(p[0] - lower[0]);
102
+ float dist_upper_x = fabs(p[0] - upper[0]);
103
+ float corner_diff_x = (dist_lower_x > dist_upper_x) ? dist_lower_x : dist_upper_x;
104
+
105
+ // Y-axis
106
+ float dist_lower_y = fabs(p[1] - lower[1]);
107
+ float dist_upper_y = fabs(p[1] - upper[1]);
108
+ float corner_diff_y = (dist_lower_y > dist_upper_y) ? dist_lower_y : dist_upper_y;
109
+
110
+ // Z-axis
111
+ float dist_lower_z = fabs(p[2] - lower[2]);
112
+ float dist_upper_z = fabs(p[2] - upper[2]);
113
+ float corner_diff_z = (dist_lower_z > dist_upper_z) ? dist_lower_z : dist_upper_z;
114
+
115
+ // Calculate and return the distance
116
+ return corner_diff_x* corner_diff_x + corner_diff_y * corner_diff_y + corner_diff_z * corner_diff_z;
117
+ }
118
+
119
+ CUDA_CALLABLE inline float mesh_query_inside(uint64_t id, const vec3& p);
120
+
121
+ // returns true if there is a point (strictly) < distance max_dist
122
+ CUDA_CALLABLE inline bool mesh_query_point(uint64_t id, const vec3& point, float max_dist, float& inside, int& face, float& u, float& v)
123
+ {
124
+ Mesh mesh = mesh_get(id);
125
+
126
+ int stack[BVH_QUERY_STACK_SIZE];
127
+ stack[0] = *mesh.bvh.root;
128
+
129
+ int count = 1;
130
+
131
+ float min_dist_sq = max_dist*max_dist;
132
+ int min_face;
133
+ float min_v;
134
+ float min_w;
135
+
136
+ #if BVH_DEBUG
137
+ int tests = 0;
138
+ int secondary_culls = 0;
139
+
140
+ std::vector<int> test_history;
141
+ std::vector<vec3> test_centers;
142
+ std::vector<vec3> test_extents;
143
+ #endif
144
+
145
+ while (count)
146
+ {
147
+ const int nodeIndex = stack[--count];
148
+
149
+ BVHPackedNodeHalf lower = bvh_load_node(mesh.bvh.node_lowers, nodeIndex);
150
+ BVHPackedNodeHalf upper = bvh_load_node(mesh.bvh.node_uppers, nodeIndex);
151
+
152
+ // re-test distance
153
+ float node_dist_sq = distance_to_aabb_sq(point, vec3(lower.x, lower.y, lower.z), vec3(upper.x, upper.y, upper.z));
154
+ if (node_dist_sq > min_dist_sq)
155
+ {
156
+ #if BVH_DEBUG
157
+ secondary_culls++;
158
+ #endif
159
+ continue;
160
+ }
161
+
162
+ const int left_index = lower.i;
163
+ const int right_index = upper.i;
164
+
165
+ if (lower.b)
166
+ {
167
+ const int start = left_index;
168
+ const int end = right_index;
169
+ // loops through primitives in the leaf
170
+ for (int primitive_counter = start; primitive_counter < end; primitive_counter++)
171
+ {
172
+ int primitive_index = mesh.bvh.primitive_indices[primitive_counter];
173
+ int i = mesh.indices[primitive_index * 3 + 0];
174
+ int j = mesh.indices[primitive_index * 3 + 1];
175
+ int k = mesh.indices[primitive_index * 3 + 2];
176
+
177
+ vec3 p = mesh.points[i];
178
+ vec3 q = mesh.points[j];
179
+ vec3 r = mesh.points[k];
180
+
181
+ vec3 e0 = q - p;
182
+ vec3 e1 = r - p;
183
+ vec3 e2 = r - q;
184
+ vec3 normal = cross(e0, e1);
185
+
186
+ // sliver detection
187
+ if (length(normal) / (dot(e0, e0) + dot(e1, e1) + dot(e2, e2)) < 1.e-6f)
188
+ continue;
189
+
190
+ vec2 barycentric = closest_point_to_triangle(p, q, r, point);
191
+ float u = barycentric[0];
192
+ float v = barycentric[1];
193
+ float w = 1.f - u - v;
194
+ vec3 c = u * p + v * q + w * r;
195
+
196
+ float dist_sq = length_sq(c - point);
197
+
198
+ if (dist_sq < min_dist_sq)
199
+ {
200
+ min_dist_sq = dist_sq;
201
+ min_v = v;
202
+ min_w = w;
203
+ min_face = primitive_index;
204
+ }
205
+ }
206
+
207
+ #if BVH_DEBUG
208
+
209
+ tests++;
210
+
211
+ bounds3 b;
212
+ b = bounds_union(b, p);
213
+ b = bounds_union(b, q);
214
+ b = bounds_union(b, r);
215
+
216
+ if (distance_to_aabb_sq(point, b.lower, b.upper) < max_dist*max_dist)
217
+ {
218
+ //if (dist_sq < max_dist*max_dist)
219
+ test_history.push_back(left_index);
220
+ test_centers.push_back(b.center());
221
+ test_extents.push_back(b.edges());
222
+ }
223
+ #endif
224
+
225
+ }
226
+ else
227
+ {
228
+ BVHPackedNodeHalf left_lower = bvh_load_node(mesh.bvh.node_lowers, left_index);
229
+ BVHPackedNodeHalf left_upper = bvh_load_node(mesh.bvh.node_uppers, left_index);
230
+
231
+ BVHPackedNodeHalf right_lower = bvh_load_node(mesh.bvh.node_lowers, right_index);
232
+ BVHPackedNodeHalf right_upper = bvh_load_node(mesh.bvh.node_uppers, right_index);
233
+
234
+ float left_dist_sq = distance_to_aabb_sq(point, vec3(left_lower.x, left_lower.y, left_lower.z), vec3(left_upper.x, left_upper.y, left_upper.z));
235
+ float right_dist_sq = distance_to_aabb_sq(point, vec3(right_lower.x, right_lower.y, right_lower.z), vec3(right_upper.x, right_upper.y, right_upper.z));
236
+
237
+ wp::vec2i child_indices;
238
+ wp::vec2 child_dist;
239
+ if (left_dist_sq < right_dist_sq)
240
+ {
241
+ child_indices = wp::vec2i(right_index, left_index);
242
+ child_dist = wp::vec2(right_dist_sq, left_dist_sq);
243
+ }
244
+ else
245
+ {
246
+ child_indices = wp::vec2i(left_index, right_index);
247
+ child_dist = wp::vec2(left_dist_sq, right_dist_sq);
248
+ }
249
+
250
+ if (child_dist[0] < min_dist_sq)
251
+ stack[count++] = child_indices[0];
252
+
253
+ if (child_dist[1] < min_dist_sq)
254
+ stack[count++] = child_indices[1];
255
+ }
256
+ }
257
+
258
+
259
+ #if BVH_DEBUG
260
+ printf("%d\n", tests);
261
+
262
+ static int max_tests = 0;
263
+ static vec3 max_point;
264
+ static float max_point_dist = 0.0f;
265
+ static int max_secondary_culls = 0;
266
+
267
+ if (secondary_culls > max_secondary_culls)
268
+ max_secondary_culls = secondary_culls;
269
+
270
+ if (tests > max_tests)
271
+ {
272
+ max_tests = tests;
273
+ max_point = point;
274
+ max_point_dist = sqrtf(min_dist_sq);
275
+
276
+ printf("max_tests: %d max_point: %f %f %f max_point_dist: %f max_second_culls: %d\n", max_tests, max_point[0], max_point[1], max_point[2], max_point_dist, max_secondary_culls);
277
+
278
+ FILE* f = fopen("test_history.txt", "w");
279
+ for (int i=0; i < test_history.size(); ++i)
280
+ {
281
+ fprintf(f, "%d, %f, %f, %f, %f, %f, %f\n",
282
+ test_history[i],
283
+ test_centers[i][0], test_centers[i][1], test_centers[i][2],
284
+ test_extents[i][0], test_extents[i][1], test_extents[i][2]);
285
+ }
286
+
287
+ fclose(f);
288
+ }
289
+ #endif
290
+
291
+ // check if we found a point, and write outputs
292
+ if (min_dist_sq < max_dist*max_dist)
293
+ {
294
+ u = 1.0f - min_v - min_w;
295
+ v = min_v;
296
+ face = min_face;
297
+
298
+ // determine inside outside using ray-cast parity check
299
+ inside = mesh_query_inside(id, point);
300
+
301
+ return true;
302
+ }
303
+ else
304
+ {
305
+ return false;
306
+ }
307
+ }
308
+
309
+ // returns true if there is a point (strictly) < distance max_dist
310
+ CUDA_CALLABLE inline bool mesh_query_point_no_sign(uint64_t id, const vec3& point, float max_dist, int& face, float& u, float& v)
311
+ {
312
+ Mesh mesh = mesh_get(id);
313
+
314
+ int stack[BVH_QUERY_STACK_SIZE];
315
+ stack[0] = *mesh.bvh.root;
316
+
317
+ int count = 1;
318
+
319
+ float min_dist_sq = max_dist*max_dist;
320
+ int min_face;
321
+ float min_v;
322
+ float min_w;
323
+
324
+ #if BVH_DEBUG
325
+ int tests = 0;
326
+ int secondary_culls = 0;
327
+
328
+ std::vector<int> test_history;
329
+ std::vector<vec3> test_centers;
330
+ std::vector<vec3> test_extents;
331
+ #endif
332
+
333
+ while (count)
334
+ {
335
+ const int nodeIndex = stack[--count];
336
+
337
+ BVHPackedNodeHalf lower = bvh_load_node(mesh.bvh.node_lowers, nodeIndex);
338
+ BVHPackedNodeHalf upper = bvh_load_node(mesh.bvh.node_uppers, nodeIndex);
339
+
340
+ // re-test distance
341
+ float node_dist_sq = distance_to_aabb_sq(point, vec3(lower.x, lower.y, lower.z), vec3(upper.x, upper.y, upper.z));
342
+ if (node_dist_sq > min_dist_sq)
343
+ {
344
+ #if BVH_DEBUG
345
+ secondary_culls++;
346
+ #endif
347
+ continue;
348
+ }
349
+
350
+ const int left_index = lower.i;
351
+ const int right_index = upper.i;
352
+
353
+ if (lower.b)
354
+ {
355
+ const int start = left_index;
356
+ const int end = right_index;
357
+ // loops through primitives in the leaf
358
+ for (int primitive_counter = start; primitive_counter < end; primitive_counter++)
359
+ {
360
+ int primitive_index = mesh.bvh.primitive_indices[primitive_counter];
361
+ int i = mesh.indices[primitive_index * 3 + 0];
362
+ int j = mesh.indices[primitive_index * 3 + 1];
363
+ int k = mesh.indices[primitive_index * 3 + 2];
364
+
365
+ vec3 p = mesh.points[i];
366
+ vec3 q = mesh.points[j];
367
+ vec3 r = mesh.points[k];
368
+ vec3 e0 = q - p;
369
+ vec3 e1 = r - p;
370
+ vec3 e2 = r - q;
371
+ vec3 normal = cross(e0, e1);
372
+
373
+ // sliver detection
374
+ if (length(normal) / (dot(e0, e0) + dot(e1, e1) + dot(e2, e2)) < 1.e-6f)
375
+ continue;
376
+
377
+ vec2 barycentric = closest_point_to_triangle(p, q, r, point);
378
+ float u = barycentric[0];
379
+ float v = barycentric[1];
380
+ float w = 1.f - u - v;
381
+ vec3 c = u * p + v * q + w * r;
382
+
383
+ float dist_sq = length_sq(c - point);
384
+
385
+ if (dist_sq < min_dist_sq)
386
+ {
387
+ min_dist_sq = dist_sq;
388
+ min_v = v;
389
+ min_w = w;
390
+ min_face = primitive_index;
391
+ }
392
+ }
393
+
394
+ #if BVH_DEBUG
395
+
396
+ tests++;
397
+
398
+ bounds3 b;
399
+ b = bounds_union(b, p);
400
+ b = bounds_union(b, q);
401
+ b = bounds_union(b, r);
402
+
403
+ if (distance_to_aabb_sq(point, b.lower, b.upper) < max_dist*max_dist)
404
+ {
405
+ //if (dist_sq < max_dist*max_dist)
406
+ test_history.push_back(left_index);
407
+ test_centers.push_back(b.center());
408
+ test_extents.push_back(b.edges());
409
+ }
410
+ #endif
411
+
412
+ }
413
+ else
414
+ {
415
+ BVHPackedNodeHalf left_lower = bvh_load_node(mesh.bvh.node_lowers, left_index);
416
+ BVHPackedNodeHalf left_upper = bvh_load_node(mesh.bvh.node_uppers, left_index);
417
+
418
+ BVHPackedNodeHalf right_lower = bvh_load_node(mesh.bvh.node_lowers, right_index);
419
+ BVHPackedNodeHalf right_upper = bvh_load_node(mesh.bvh.node_uppers, right_index);
420
+
421
+ float left_dist_sq = distance_to_aabb_sq(point, vec3(left_lower.x, left_lower.y, left_lower.z), vec3(left_upper.x, left_upper.y, left_upper.z));
422
+ float right_dist_sq = distance_to_aabb_sq(point, vec3(right_lower.x, right_lower.y, right_lower.z), vec3(right_upper.x, right_upper.y, right_upper.z));
423
+
424
+ wp::vec2i child_indices;
425
+ wp::vec2 child_dist;
426
+ if (left_dist_sq < right_dist_sq)
427
+ {
428
+ child_indices = wp::vec2i(right_index, left_index);
429
+ child_dist = wp::vec2(right_dist_sq, left_dist_sq);
430
+ }
431
+ else
432
+ {
433
+ child_indices = wp::vec2i(left_index, right_index);
434
+ child_dist = wp::vec2(left_dist_sq, right_dist_sq);
435
+ }
436
+
437
+ if (child_dist[0] < min_dist_sq)
438
+ stack[count++] = child_indices[0];
439
+
440
+ if (child_dist[1] < min_dist_sq)
441
+ stack[count++] = child_indices[1];
442
+ }
443
+ }
444
+
445
+
446
+ #if BVH_DEBUG
447
+ printf("%d\n", tests);
448
+
449
+ static int max_tests = 0;
450
+ static vec3 max_point;
451
+ static float max_point_dist = 0.0f;
452
+ static int max_secondary_culls = 0;
453
+
454
+ if (secondary_culls > max_secondary_culls)
455
+ max_secondary_culls = secondary_culls;
456
+
457
+ if (tests > max_tests)
458
+ {
459
+ max_tests = tests;
460
+ max_point = point;
461
+ max_point_dist = sqrtf(min_dist_sq);
462
+
463
+ printf("max_tests: %d max_point: %f %f %f max_point_dist: %f max_second_culls: %d\n", max_tests, max_point[0], max_point[1], max_point[2], max_point_dist, max_secondary_culls);
464
+
465
+ FILE* f = fopen("test_history.txt", "w");
466
+ for (int i=0; i < test_history.size(); ++i)
467
+ {
468
+ fprintf(f, "%d, %f, %f, %f, %f, %f, %f\n",
469
+ test_history[i],
470
+ test_centers[i][0], test_centers[i][1], test_centers[i][2],
471
+ test_extents[i][0], test_extents[i][1], test_extents[i][2]);
472
+ }
473
+
474
+ fclose(f);
475
+ }
476
+ #endif
477
+
478
+ // check if we found a point, and write outputs
479
+ if (min_dist_sq < max_dist*max_dist)
480
+ {
481
+ u = 1.0f - min_v - min_w;
482
+ v = min_v;
483
+ face = min_face;
484
+
485
+ return true;
486
+ }
487
+ else
488
+ {
489
+ return false;
490
+ }
491
+ }
492
+
493
+ // returns true if there is a point (strictly) > distance min_dist
494
+ CUDA_CALLABLE inline bool mesh_query_furthest_point_no_sign(uint64_t id, const vec3& point, float min_dist, int& face, float& u, float& v)
495
+ {
496
+ Mesh mesh = mesh_get(id);
497
+
498
+ int stack[BVH_QUERY_STACK_SIZE];
499
+ stack[0] = *mesh.bvh.root;
500
+
501
+ int count = 1;
502
+
503
+ float min_dist_sq = min_dist*min_dist;
504
+ int max_face;
505
+ float max_v;
506
+ float max_w;
507
+
508
+ #if BVH_DEBUG
509
+ int tests = 0;
510
+ int secondary_culls = 0;
511
+
512
+ std::vector<int> test_history;
513
+ std::vector<vec3> test_centers;
514
+ std::vector<vec3> test_extents;
515
+ #endif
516
+
517
+ while (count)
518
+ {
519
+ const int nodeIndex = stack[--count];
520
+
521
+ BVHPackedNodeHalf lower = bvh_load_node(mesh.bvh.node_lowers, nodeIndex);
522
+ BVHPackedNodeHalf upper = bvh_load_node(mesh.bvh.node_uppers, nodeIndex);
523
+
524
+ // re-test distance
525
+ float node_dist_sq = furthest_distance_to_aabb_sq(point, vec3(lower.x, lower.y, lower.z), vec3(upper.x, upper.y, upper.z));
526
+
527
+ // if maximum distance to this node is less than our existing furthest max then skip
528
+ if (node_dist_sq < min_dist_sq)
529
+ {
530
+ #if BVH_DEBUG
531
+ secondary_culls++;
532
+ #endif
533
+ continue;
534
+ }
535
+
536
+ const int left_index = lower.i;
537
+ const int right_index = upper.i;
538
+
539
+ if (lower.b)
540
+ {
541
+ const int start = left_index;
542
+ const int end = right_index;
543
+ // loops through primitives in the leaf
544
+ for (int primitive_counter = start; primitive_counter < end; primitive_counter++)
545
+ {
546
+ int primitive_index = mesh.bvh.primitive_indices[primitive_counter];
547
+ int i = mesh.indices[primitive_index * 3 + 0];
548
+ int j = mesh.indices[primitive_index * 3 + 1];
549
+ int k = mesh.indices[primitive_index * 3 + 2];
550
+
551
+ vec3 p = mesh.points[i];
552
+ vec3 q = mesh.points[j];
553
+ vec3 r = mesh.points[k];
554
+
555
+ vec3 e0 = q-p;
556
+ vec3 e1 = r-p;
557
+ vec3 e2 = r-q;
558
+ vec3 normal = cross(e0, e1);
559
+
560
+ // sliver detection
561
+ if (length(normal)/(dot(e0,e0) + dot(e1,e1) + dot(e2,e2)) < 1.e-6f)
562
+ continue;
563
+
564
+ vec2 barycentric = furthest_point_to_triangle(p, q, r, point);
565
+ float u = barycentric[0];
566
+ float v = barycentric[1];
567
+ float w = 1.f - u - v;
568
+ vec3 c = u*p + v*q + w*r;
569
+
570
+ float dist_sq = length_sq(c-point);
571
+
572
+ if (dist_sq > min_dist_sq)
573
+ {
574
+ min_dist_sq = dist_sq;
575
+ max_v = v;
576
+ max_w = w;
577
+ max_face = primitive_index;
578
+ }
579
+ }
580
+
581
+ #if BVH_DEBUG
582
+
583
+ tests++;
584
+
585
+ bounds3 b;
586
+ b = bounds_union(b, p);
587
+ b = bounds_union(b, q);
588
+ b = bounds_union(b, r);
589
+
590
+ if (distance_to_aabb_sq(point, b.lower, b.upper) > max_dist*max_dist)
591
+ {
592
+ //if (dist_sq < max_dist*max_dist)
593
+ test_history.push_back(left_index);
594
+ test_centers.push_back(b.center());
595
+ test_extents.push_back(b.edges());
596
+ }
597
+ #endif
598
+
599
+ }
600
+ else
601
+ {
602
+ BVHPackedNodeHalf left_lower = bvh_load_node(mesh.bvh.node_lowers, left_index);
603
+ BVHPackedNodeHalf left_upper = bvh_load_node(mesh.bvh.node_uppers, left_index);
604
+
605
+ BVHPackedNodeHalf right_lower = bvh_load_node(mesh.bvh.node_lowers, right_index);
606
+ BVHPackedNodeHalf right_upper = bvh_load_node(mesh.bvh.node_uppers, right_index);
607
+
608
+ float left_dist_sq = furthest_distance_to_aabb_sq(point, vec3(left_lower.x, left_lower.y, left_lower.z), vec3(left_upper.x, left_upper.y, left_upper.z));
609
+ float right_dist_sq = furthest_distance_to_aabb_sq(point, vec3(right_lower.x, right_lower.y, right_lower.z), vec3(right_upper.x, right_upper.y, right_upper.z));
610
+
611
+ wp::vec2i child_indices;
612
+ wp::vec2 child_dist;
613
+ if (left_dist_sq > right_dist_sq)
614
+ {
615
+ child_indices = wp::vec2i(right_index, left_index);
616
+ child_dist = wp::vec2(right_dist_sq, left_dist_sq);
617
+ }
618
+ else
619
+ {
620
+ child_indices = wp::vec2i(left_index, right_index);
621
+ child_dist = wp::vec2(left_dist_sq, right_dist_sq);
622
+ }
623
+
624
+ if (child_dist[0] > min_dist_sq)
625
+ stack[count++] = child_indices[0];
626
+
627
+ if (child_dist[1] > min_dist_sq)
628
+ stack[count++] = child_indices[1];
629
+ }
630
+ }
631
+
632
+
633
+ #if BVH_DEBUG
634
+ printf("%d\n", tests);
635
+
636
+ static int max_tests = 0;
637
+ static vec3 max_point;
638
+ static float max_point_dist = 0.0f;
639
+ static int max_secondary_culls = 0;
640
+
641
+ if (secondary_culls > max_secondary_culls)
642
+ max_secondary_culls = secondary_culls;
643
+
644
+ if (tests > max_tests)
645
+ {
646
+ max_tests = tests;
647
+ max_point = point;
648
+ max_point_dist = sqrtf(min_dist_sq);
649
+
650
+ printf("max_tests: %d max_point: %f %f %f max_point_dist: %f max_second_culls: %d\n", max_tests, max_point[0], max_point[1], max_point[2], max_point_dist, max_secondary_culls);
651
+
652
+ FILE* f = fopen("test_history.txt", "w");
653
+ for (int i=0; i < test_history.size(); ++i)
654
+ {
655
+ fprintf(f, "%d, %f, %f, %f, %f, %f, %f\n",
656
+ test_history[i],
657
+ test_centers[i][0], test_centers[i][1], test_centers[i][2],
658
+ test_extents[i][0], test_extents[i][1], test_extents[i][2]);
659
+ }
660
+
661
+ fclose(f);
662
+ }
663
+ #endif
664
+
665
+ // check if we found a point, and write outputs
666
+ if (min_dist_sq > min_dist*min_dist)
667
+ {
668
+ u = 1.0f - max_v - max_w;
669
+ v = max_v;
670
+ face = max_face;
671
+
672
+ return true;
673
+ }
674
+ else
675
+ {
676
+ return false;
677
+ }
678
+ }
679
+
680
+ // returns true if there is a point (strictly) < distance max_dist
681
+ CUDA_CALLABLE inline bool mesh_query_point_sign_normal(uint64_t id, const vec3& point, float max_dist, float& inside, int& face, float& u, float& v, const float epsilon = 1e-3f)
682
+ {
683
+ Mesh mesh = mesh_get(id);
684
+
685
+ int stack[BVH_QUERY_STACK_SIZE];
686
+ stack[0] = *mesh.bvh.root;
687
+ int count = 1;
688
+ float min_dist = max_dist;
689
+ int min_face;
690
+ float min_v;
691
+ float min_w;
692
+ vec3 accumulated_angle_weighted_normal;
693
+
694
+ #if BVH_DEBUG
695
+ int tests = 0;
696
+ int secondary_culls = 0;
697
+ std::vector<int> test_history;
698
+ std::vector<vec3> test_centers;
699
+ std::vector<vec3> test_extents;
700
+ #endif
701
+ float epsilon_min_dist = mesh.average_edge_length * epsilon;
702
+ float epsilon_min_dist_sq = epsilon_min_dist*epsilon_min_dist;
703
+
704
+ while (count)
705
+ {
706
+ const int nodeIndex = stack[--count];
707
+ BVHPackedNodeHalf lower = bvh_load_node(mesh.bvh.node_lowers, nodeIndex);
708
+ BVHPackedNodeHalf upper = bvh_load_node(mesh.bvh.node_uppers, nodeIndex);
709
+
710
+ // re-test distance
711
+ float node_dist_sq = distance_to_aabb_sq(point, vec3(lower.x, lower.y, lower.z), vec3(upper.x, upper.y, upper.z));
712
+ if (node_dist_sq > (min_dist + epsilon_min_dist)*(min_dist + epsilon_min_dist))
713
+ {
714
+ #if BVH_DEBUG
715
+ secondary_culls++;
716
+ #endif
717
+ continue;
718
+ }
719
+
720
+ const int left_index = lower.i;
721
+ const int right_index = upper.i;
722
+
723
+ if (lower.b)
724
+ {
725
+ const int start = left_index;
726
+ const int end = right_index;
727
+ // loops through primitives in the leaf
728
+ for (int primitive_counter = start; primitive_counter < end; primitive_counter++)
729
+ {
730
+ int primitive_index = mesh.bvh.primitive_indices[primitive_counter];
731
+ int i = mesh.indices[primitive_index * 3 + 0];
732
+ int j = mesh.indices[primitive_index * 3 + 1];
733
+ int k = mesh.indices[primitive_index * 3 + 2];
734
+
735
+ vec3 p = mesh.points[i];
736
+ vec3 q = mesh.points[j];
737
+ vec3 r = mesh.points[k];
738
+
739
+ vec3 e0 = q-p;
740
+ vec3 e1 = r-p;
741
+ vec3 e2 = r-q;
742
+ vec3 normal = cross(e0, e1);
743
+
744
+ // sliver detection
745
+ float e0_norm_sq = dot(e0,e0);
746
+ float e1_norm_sq = dot(e1,e1);
747
+ float e2_norm_sq = dot(e2,e2);
748
+ if (length(normal)/(e0_norm_sq + e1_norm_sq + e2_norm_sq) < 1.e-6f)
749
+ continue;
750
+
751
+ vec2 barycentric = closest_point_to_triangle(p, q, r, point);
752
+ float u = barycentric[0];
753
+ float v = barycentric[1];
754
+ float w = 1.f - u - v;
755
+ vec3 c = u*p + v*q + w*r;
756
+ float dist = sqrtf(length_sq(c-point));
757
+ if (dist < min_dist + epsilon_min_dist)
758
+ {
759
+ float weight = 0.0f;
760
+ vec3 cp = c - p;
761
+ vec3 cq = c - q;
762
+ vec3 cr = c - r;
763
+ float len_cp_sq = length_sq(cp);
764
+ float len_cq_sq = length_sq(cq);
765
+ float len_cr_sq = length_sq(cr);
766
+
767
+ // Check if near vertex
768
+ if (len_cp_sq < epsilon_min_dist_sq)
769
+ {
770
+ // Vertex 0 is the closest feature
771
+ weight = acosf(dot(normalize(e0), normalize(e1)));
772
+ }
773
+ else
774
+ if (len_cq_sq < epsilon_min_dist_sq)
775
+ {
776
+ // Vertex 1 is the closest feature
777
+ weight = acosf(dot(normalize(e2), normalize(-e0)));
778
+ }
779
+ else
780
+ if (len_cr_sq < epsilon_min_dist_sq)
781
+ {
782
+ // Vertex 2 is the closest feature
783
+ weight = acosf(dot(normalize(-e1), normalize(-e2)));
784
+ }
785
+ else
786
+ {
787
+ float e0cp = dot(e0, cp);
788
+ float e2cq = dot(e2, cq);
789
+ float e1cp = dot(e1, cp);
790
+
791
+ if ((len_cp_sq * e0_norm_sq - e0cp * e0cp < epsilon_min_dist_sq * e0_norm_sq) ||
792
+ (len_cq_sq * e2_norm_sq - e2cq * e2cq < epsilon_min_dist_sq * e2_norm_sq) ||
793
+ (len_cp_sq * e1_norm_sq - e1cp * e1cp < epsilon_min_dist_sq * e1_norm_sq)) {
794
+ // One of the edge
795
+ weight = 3.14159265359f; // PI
796
+ }
797
+ else {
798
+ weight = 2.0f * 3.14159265359f; // 2*PI
799
+ }
800
+ }
801
+
802
+ if (dist > min_dist - epsilon_min_dist)
803
+ {
804
+ // Treat as equal
805
+ accumulated_angle_weighted_normal += weight * normalize(normal);
806
+ if (dist < min_dist)
807
+ {
808
+ min_dist = dist;
809
+ min_v = v;
810
+ min_w = w;
811
+ min_face = primitive_index;
812
+ }
813
+ }
814
+ else {
815
+ // Less
816
+ min_dist = dist;
817
+ min_v = v;
818
+ min_w = w;
819
+ min_face = primitive_index;
820
+ accumulated_angle_weighted_normal = weight * normalize(normal);
821
+ }
822
+ }
823
+ }
824
+ #if BVH_DEBUG
825
+ tests++;
826
+ bounds3 b;
827
+ b = bounds_union(b, p);
828
+ b = bounds_union(b, q);
829
+ b = bounds_union(b, r);
830
+ if (distance_to_aabb_sq(point, b.lower, b.upper) < (max_dist+epsilon_min_dist)*(max_dist+epsilon_min_dist))
831
+ {
832
+ //if (dist_sq < max_dist*max_dist)
833
+ test_history.push_back(left_index);
834
+ test_centers.push_back(b.center());
835
+ test_extents.push_back(b.edges());
836
+ }
837
+ #endif
838
+ }
839
+ else
840
+ {
841
+ BVHPackedNodeHalf left_lower = bvh_load_node(mesh.bvh.node_lowers, left_index);
842
+ BVHPackedNodeHalf left_upper = bvh_load_node(mesh.bvh.node_uppers, left_index);
843
+
844
+ BVHPackedNodeHalf right_lower = bvh_load_node(mesh.bvh.node_lowers, right_index);
845
+ BVHPackedNodeHalf right_upper = bvh_load_node(mesh.bvh.node_uppers, right_index);
846
+
847
+ float left_dist_sq = distance_to_aabb_sq(point, vec3(left_lower.x, left_lower.y, left_lower.z), vec3(left_upper.x, left_upper.y, left_upper.z));
848
+ float right_dist_sq = distance_to_aabb_sq(point, vec3(right_lower.x, right_lower.y, right_lower.z), vec3(right_upper.x, right_upper.y, right_upper.z));
849
+
850
+ wp::vec2i child_indices;
851
+ wp::vec2 child_dist;
852
+ if (left_dist_sq < right_dist_sq)
853
+ {
854
+ child_indices = wp::vec2i(right_index, left_index);
855
+ child_dist = wp::vec2(right_dist_sq, left_dist_sq);
856
+ }
857
+ else
858
+ {
859
+ child_indices = wp::vec2i(left_index, right_index);
860
+ child_dist = wp::vec2(left_dist_sq, right_dist_sq);
861
+ }
862
+
863
+ if (child_dist[0] < (min_dist + epsilon_min_dist) * (min_dist + epsilon_min_dist))
864
+ stack[count++] = child_indices[0];
865
+
866
+ if (child_dist[1] < (min_dist + epsilon_min_dist) * (min_dist + epsilon_min_dist))
867
+ stack[count++] = child_indices[1];
868
+ }
869
+ }
870
+ #if BVH_DEBUG
871
+ printf("%d\n", tests);
872
+ static int max_tests = 0;
873
+ static vec3 max_point;
874
+ static float max_point_dist = 0.0f;
875
+ static int max_secondary_culls = 0;
876
+ if (secondary_culls > max_secondary_culls)
877
+ max_secondary_culls = secondary_culls;
878
+ if (tests > max_tests)
879
+ {
880
+ max_tests = tests;
881
+ max_point = point;
882
+ max_point_dist = min_dist;
883
+ printf("max_tests: %d max_point: %f %f %f max_point_dist: %f max_second_culls: %d\n", max_tests, max_point[0], max_point[1], max_point[2], max_point_dist, max_secondary_culls);
884
+ FILE* f = fopen("test_history.txt", "w");
885
+ for (int i=0; i < test_history.size(); ++i)
886
+ {
887
+ fprintf(f, "%d, %f, %f, %f, %f, %f, %f\n",
888
+ test_history[i],
889
+ test_centers[i][0], test_centers[i][1], test_centers[i][2],
890
+ test_extents[i][0], test_extents[i][1], test_extents[i][2]);
891
+ }
892
+ fclose(f);
893
+ }
894
+ #endif
895
+ // check if we found a point, and write outputs
896
+ if (min_dist < max_dist)
897
+ {
898
+ u = 1.0f - min_v - min_w;
899
+ v = min_v;
900
+ face = min_face;
901
+ // determine inside outside using ray-cast parity check
902
+ //inside = mesh_query_inside(id, point);
903
+ int i = mesh.indices[min_face*3+0];
904
+ int j = mesh.indices[min_face*3+1];
905
+ int k = mesh.indices[min_face*3+2];
906
+ vec3 p = mesh.points[i];
907
+ vec3 q = mesh.points[j];
908
+ vec3 r = mesh.points[k];
909
+ vec3 closest_point = p*u+q*v+r*min_w;
910
+ if (dot(accumulated_angle_weighted_normal, point-closest_point) > 0.0)
911
+ {
912
+ inside = 1.0f;
913
+ } else
914
+ {
915
+ inside = -1.0f;
916
+ }
917
+ return true;
918
+ }
919
+ else
920
+ {
921
+ return false;
922
+ }
923
+ }
924
+
925
+ CUDA_CALLABLE inline float solid_angle_iterative(uint64_t id, const vec3& p, const float accuracy_sq)
926
+ {
927
+ Mesh mesh = mesh_get(id);
928
+
929
+ int stack[BVH_QUERY_STACK_SIZE];
930
+ int at_child[BVH_QUERY_STACK_SIZE]; // 0 for left, 1 for right, 2 for done
931
+ float angle[BVH_QUERY_STACK_SIZE];
932
+ stack[0] = *mesh.bvh.root;
933
+ at_child[0] = 0;
934
+
935
+ int count = 1;
936
+ angle[0] = 0.0f;
937
+
938
+ while (count)
939
+ {
940
+ const int nodeIndex = stack[count - 1];
941
+ BVHPackedNodeHalf lower = bvh_load_node(mesh.bvh.node_lowers, nodeIndex);
942
+ BVHPackedNodeHalf upper = bvh_load_node(mesh.bvh.node_uppers, nodeIndex);
943
+
944
+ const int left_index = lower.i;
945
+ const int right_index = upper.i;
946
+ if (lower.b)
947
+ {
948
+ // compute closest point on tri
949
+ const int start = left_index;
950
+ const int end = right_index;
951
+ angle[count - 1] = 0.f;
952
+ for (int primitive_counter = start; primitive_counter < end; primitive_counter++)
953
+ {
954
+ int primitive_index = mesh.bvh.primitive_indices[primitive_counter];
955
+ int i = mesh.indices[primitive_index * 3 + 0];
956
+ int j = mesh.indices[primitive_index * 3 + 1];
957
+ int k = mesh.indices[primitive_index * 3 + 2];
958
+ angle[count - 1] += robust_solid_angle(
959
+ mesh.points[i],
960
+ mesh.points[j],
961
+ mesh.points[k], p);
962
+ //printf("Leaf %d, got %f\n", leaf_index, my_data[count - 1]);
963
+ }
964
+ count--;
965
+ }
966
+ else
967
+ {
968
+ // See if I have to descend
969
+ if (at_child[count - 1] == 0)
970
+ {
971
+ // First visit
972
+ bool des = evaluate_node_solid_angle(p, &mesh.solid_angle_props[nodeIndex], angle[count - 1], accuracy_sq);
973
+
974
+ //printf("Non-Leaf %d, got %f\n", nodeIndex, angle[count - 1]);
975
+ if (des)
976
+ {
977
+ // Go left
978
+ stack[count] = left_index;
979
+ at_child[count - 1] = 1;
980
+ angle[count] = 0.0f;
981
+ at_child[count] = 0;
982
+ count++;
983
+ } else
984
+ {
985
+ // Does not descend done
986
+ count--;
987
+ }
988
+ } else
989
+ if (at_child[count - 1] == 1)
990
+ {
991
+ // Add data to parent
992
+ angle[count - 1] += angle[count];
993
+ // Go right
994
+ stack[count] = right_index;
995
+ at_child[count - 1] = 2;
996
+ angle[count] = 0.0f;
997
+ at_child[count] = 0;
998
+ count++;
999
+ } else {
1000
+ // Descend both sides already
1001
+ angle[count - 1] += angle[count];
1002
+ count--;
1003
+ }
1004
+ }
1005
+ }
1006
+ return angle[0];
1007
+ }
1008
+
1009
+ CUDA_CALLABLE inline float mesh_query_winding_number(uint64_t id, const vec3& p, const float accuracy)
1010
+ {
1011
+ float angle = solid_angle_iterative(id, p, accuracy*accuracy);
1012
+ return angle * 0.07957747154; // divided by 4 PI
1013
+ }
1014
+
1015
+ // returns true if there is a point (strictly) < distance max_dist
1016
+ CUDA_CALLABLE inline bool mesh_query_point_sign_winding_number(uint64_t id, const vec3& point, float max_dist, float& inside, int& face, float& u, float& v, const float accuracy, const float winding_number_threshold)
1017
+ {
1018
+ Mesh mesh = mesh_get(id);
1019
+
1020
+ int stack[BVH_QUERY_STACK_SIZE];
1021
+ stack[0] = *mesh.bvh.root;
1022
+
1023
+ int count = 1;
1024
+
1025
+ float min_dist_sq = max_dist*max_dist;
1026
+ int min_face;
1027
+ float min_v;
1028
+ float min_w;
1029
+
1030
+ #if BVH_DEBUG
1031
+ int tests = 0;
1032
+ int secondary_culls = 0;
1033
+
1034
+ std::vector<int> test_history;
1035
+ std::vector<vec3> test_centers;
1036
+ std::vector<vec3> test_extents;
1037
+ #endif
1038
+
1039
+ while (count)
1040
+ {
1041
+ const int nodeIndex = stack[--count];
1042
+
1043
+ BVHPackedNodeHalf lower = bvh_load_node(mesh.bvh.node_lowers, nodeIndex);
1044
+ BVHPackedNodeHalf upper = bvh_load_node(mesh.bvh.node_uppers, nodeIndex);
1045
+
1046
+ // re-test distance
1047
+ float node_dist_sq = distance_to_aabb_sq(point, vec3(lower.x, lower.y, lower.z), vec3(upper.x, upper.y, upper.z));
1048
+ if (node_dist_sq > min_dist_sq)
1049
+ {
1050
+ #if BVH_DEBUG
1051
+ secondary_culls++;
1052
+ #endif
1053
+ continue;
1054
+ }
1055
+
1056
+ const int left_index = lower.i;
1057
+ const int right_index = upper.i;
1058
+
1059
+ if (lower.b)
1060
+ {
1061
+ const int start = left_index;
1062
+ const int end = right_index;
1063
+ // loops through primitives in the leaf
1064
+ for (int primitive_counter = start; primitive_counter < end; primitive_counter++)
1065
+ {
1066
+ int primitive_index = mesh.bvh.primitive_indices[primitive_counter];
1067
+ int i = mesh.indices[primitive_index * 3 + 0];
1068
+ int j = mesh.indices[primitive_index * 3 + 1];
1069
+ int k = mesh.indices[primitive_index * 3 + 2];
1070
+
1071
+ vec3 p = mesh.points[i];
1072
+ vec3 q = mesh.points[j];
1073
+ vec3 r = mesh.points[k];
1074
+
1075
+ vec3 e0 = q - p;
1076
+ vec3 e1 = r - p;
1077
+ vec3 e2 = r - q;
1078
+ vec3 normal = cross(e0, e1);
1079
+
1080
+ // sliver detection
1081
+ if (length(normal) / (dot(e0, e0) + dot(e1, e1) + dot(e2, e2)) < 1.e-6f)
1082
+ continue;
1083
+
1084
+ vec2 barycentric = closest_point_to_triangle(p, q, r, point);
1085
+ float u = barycentric[0];
1086
+ float v = barycentric[1];
1087
+ float w = 1.f - u - v;
1088
+ vec3 c = u * p + v * q + w * r;
1089
+
1090
+ float dist_sq = length_sq(c - point);
1091
+
1092
+ if (dist_sq < min_dist_sq)
1093
+ {
1094
+ min_dist_sq = dist_sq;
1095
+ min_v = v;
1096
+ min_w = w;
1097
+ min_face = primitive_index;
1098
+ }
1099
+ }
1100
+ #if BVH_DEBUG
1101
+
1102
+ tests++;
1103
+
1104
+ bounds3 b;
1105
+ b = bounds_union(b, p);
1106
+ b = bounds_union(b, q);
1107
+ b = bounds_union(b, r);
1108
+
1109
+ if (distance_to_aabb_sq(point, b.lower, b.upper) < max_dist*max_dist)
1110
+ {
1111
+ //if (dist_sq < max_dist*max_dist)
1112
+ test_history.push_back(left_index);
1113
+ test_centers.push_back(b.center());
1114
+ test_extents.push_back(b.edges());
1115
+ }
1116
+ #endif
1117
+
1118
+ }
1119
+ else
1120
+ {
1121
+ BVHPackedNodeHalf left_lower = bvh_load_node(mesh.bvh.node_lowers, left_index);
1122
+ BVHPackedNodeHalf left_upper = bvh_load_node(mesh.bvh.node_uppers, left_index);
1123
+
1124
+ BVHPackedNodeHalf right_lower = bvh_load_node(mesh.bvh.node_lowers, right_index);
1125
+ BVHPackedNodeHalf right_upper = bvh_load_node(mesh.bvh.node_uppers, right_index);
1126
+
1127
+ float left_dist_sq = distance_to_aabb_sq(point, vec3(left_lower.x, left_lower.y, left_lower.z), vec3(left_upper.x, left_upper.y, left_upper.z));
1128
+ float right_dist_sq = distance_to_aabb_sq(point, vec3(right_lower.x, right_lower.y, right_lower.z), vec3(right_upper.x, right_upper.y, right_upper.z));
1129
+
1130
+ wp::vec2i child_indices;
1131
+ wp::vec2 child_dist;
1132
+ if (left_dist_sq < right_dist_sq)
1133
+ {
1134
+ child_indices = wp::vec2i(right_index, left_index);
1135
+ child_dist = wp::vec2(right_dist_sq, left_dist_sq);
1136
+ }
1137
+ else
1138
+ {
1139
+ child_indices = wp::vec2i(left_index, right_index);
1140
+ child_dist = wp::vec2(left_dist_sq, right_dist_sq);
1141
+ }
1142
+
1143
+ if (child_dist[0] < min_dist_sq)
1144
+ stack[count++] = child_indices[0];
1145
+
1146
+ if (child_dist[1] < min_dist_sq)
1147
+ stack[count++] = child_indices[1];
1148
+ }
1149
+ }
1150
+
1151
+
1152
+ #if BVH_DEBUG
1153
+ printf("%d\n", tests);
1154
+
1155
+ static int max_tests = 0;
1156
+ static vec3 max_point;
1157
+ static float max_point_dist = 0.0f;
1158
+ static int max_secondary_culls = 0;
1159
+
1160
+ if (secondary_culls > max_secondary_culls)
1161
+ max_secondary_culls = secondary_culls;
1162
+
1163
+ if (tests > max_tests)
1164
+ {
1165
+ max_tests = tests;
1166
+ max_point = point;
1167
+ max_point_dist = sqrtf(min_dist_sq);
1168
+
1169
+ printf("max_tests: %d max_point: %f %f %f max_point_dist: %f max_second_culls: %d\n", max_tests, max_point[0], max_point[1], max_point[2], max_point_dist, max_secondary_culls);
1170
+
1171
+ FILE* f = fopen("test_history.txt", "w");
1172
+ for (int i=0; i < test_history.size(); ++i)
1173
+ {
1174
+ fprintf(f, "%d, %f, %f, %f, %f, %f, %f\n",
1175
+ test_history[i],
1176
+ test_centers[i][0], test_centers[i][1], test_centers[i][2],
1177
+ test_extents[i][0], test_extents[i][1], test_extents[i][2]);
1178
+ }
1179
+
1180
+ fclose(f);
1181
+ }
1182
+ #endif
1183
+
1184
+ // check if we found a point, and write outputs
1185
+ if (min_dist_sq < max_dist*max_dist)
1186
+ {
1187
+ u = 1.0f - min_v - min_w;
1188
+ v = min_v;
1189
+ face = min_face;
1190
+
1191
+ // determine inside outside using ray-cast parity check
1192
+ if (!mesh.solid_angle_props) {
1193
+ inside = mesh_query_inside(id, point);
1194
+ }
1195
+ else {
1196
+ float winding_number = mesh_query_winding_number(id, point, accuracy);
1197
+ inside = (winding_number > winding_number_threshold) ? -1.0f:1.0f;
1198
+ }
1199
+
1200
+ return true;
1201
+ }
1202
+ else
1203
+ {
1204
+ return false;
1205
+ }
1206
+ }
1207
+
1208
+ CUDA_CALLABLE inline void adj_mesh_query_point_no_sign(uint64_t id, const vec3& point, float max_dist, const int& face, const float& u, const float& v,
1209
+ uint64_t adj_id, vec3& adj_point, float& adj_max_dist, int& adj_face, float& adj_u, float& adj_v, bool& adj_ret)
1210
+ {
1211
+ Mesh mesh = mesh_get(id);
1212
+
1213
+ // face is determined by BVH in forward pass
1214
+ int i = mesh.indices[face*3+0];
1215
+ int j = mesh.indices[face*3+1];
1216
+ int k = mesh.indices[face*3+2];
1217
+
1218
+ vec3 p = mesh.points[i];
1219
+ vec3 q = mesh.points[j];
1220
+ vec3 r = mesh.points[k];
1221
+
1222
+ vec3 adj_p, adj_q, adj_r;
1223
+
1224
+ vec2 adj_uv(adj_u, adj_v);
1225
+
1226
+ adj_closest_point_to_triangle(p, q, r, point, adj_p, adj_q, adj_r, adj_point, adj_uv);
1227
+ }
1228
+
1229
+ CUDA_CALLABLE inline void adj_mesh_query_furthest_point_no_sign(uint64_t id, const vec3& point, float min_dist, const int& face, const float& u, const float& v,
1230
+ uint64_t adj_id, vec3& adj_point, float& adj_min_dist, int& adj_face, float& adj_u, float& adj_v, bool& adj_ret)
1231
+ {
1232
+ Mesh mesh = mesh_get(id);
1233
+
1234
+ // face is determined by BVH in forward pass
1235
+ int i = mesh.indices[face*3+0];
1236
+ int j = mesh.indices[face*3+1];
1237
+ int k = mesh.indices[face*3+2];
1238
+
1239
+ vec3 p = mesh.points[i];
1240
+ vec3 q = mesh.points[j];
1241
+ vec3 r = mesh.points[k];
1242
+
1243
+ vec3 adj_p, adj_q, adj_r;
1244
+
1245
+ vec2 adj_uv(adj_u, adj_v);
1246
+
1247
+ adj_closest_point_to_triangle(p, q, r, point, adj_p, adj_q, adj_r, adj_point, adj_uv); // Todo for Miles :>
1248
+ }
1249
+
1250
+ CUDA_CALLABLE inline void adj_mesh_query_point(uint64_t id, const vec3& point, float max_dist, const float& inside, const int& face, const float& u, const float& v,
1251
+ uint64_t adj_id, vec3& adj_point, float& adj_max_dist, float& adj_inside, int& adj_face, float& adj_u, float& adj_v, bool& adj_ret)
1252
+ {
1253
+ adj_mesh_query_point_no_sign(id, point, max_dist, face, u, v, adj_id, adj_point, adj_max_dist, adj_face, adj_u, adj_v, adj_ret);
1254
+ }
1255
+
1256
+ CUDA_CALLABLE inline void adj_mesh_query_point_sign_normal(uint64_t id, const vec3& point, float max_dist, const float& inside, const int& face, const float& u, const float& v, const float epsilon,
1257
+ uint64_t adj_id, vec3& adj_point, float& adj_max_dist, float& adj_inside, int& adj_face, float& adj_u, float& adj_v, float& adj_epsilon, bool& adj_ret)
1258
+ {
1259
+ adj_mesh_query_point_no_sign(id, point, max_dist, face, u, v, adj_id, adj_point, adj_max_dist, adj_face, adj_u, adj_v, adj_ret);
1260
+ }
1261
+
1262
+ CUDA_CALLABLE inline void adj_mesh_query_point_sign_winding_number(uint64_t id, const vec3& point, float max_dist, const float& inside, const int& face, const float& u, const float& v, const float accuracy, const float winding_number_threshold,
1263
+ uint64_t adj_id, vec3& adj_point, float& adj_max_dist, float& adj_inside, int& adj_face, float& adj_u, float& adj_v, float& adj_accuracy, float& adj_winding_number_threshold, bool& adj_ret)
1264
+ {
1265
+ adj_mesh_query_point_no_sign(id, point, max_dist, face, u, v, adj_id, adj_point, adj_max_dist, adj_face, adj_u, adj_v, adj_ret);
1266
+ }
1267
+
1268
+
1269
+ // Stores the result of querying the closest point on a mesh.
1270
+ struct mesh_query_point_t
1271
+ {
1272
+ CUDA_CALLABLE mesh_query_point_t()
1273
+ : result(false),
1274
+ sign(0.0f),
1275
+ face(0),
1276
+ u(0.0f),
1277
+ v(0.0f)
1278
+ {}
1279
+
1280
+ // Required for adjoint computations.
1281
+ CUDA_CALLABLE inline mesh_query_point_t& operator+=(const mesh_query_point_t& other)
1282
+ {
1283
+ result += other.result;
1284
+ sign += other.sign;
1285
+ face += other.face;
1286
+ u += other.u;
1287
+ v += other.v;
1288
+ return *this;
1289
+ }
1290
+
1291
+ bool result;
1292
+ float sign;
1293
+ int face;
1294
+ float u;
1295
+ float v;
1296
+ };
1297
+
1298
+ CUDA_CALLABLE inline mesh_query_point_t mesh_query_point(uint64_t id, const vec3& point, float max_dist)
1299
+ {
1300
+ mesh_query_point_t query;
1301
+ query.result = mesh_query_point(id, point, max_dist, query.sign, query.face, query.u, query.v);
1302
+ return query;
1303
+ }
1304
+
1305
+ CUDA_CALLABLE inline mesh_query_point_t mesh_query_point_no_sign(uint64_t id, const vec3& point, float max_dist)
1306
+ {
1307
+ mesh_query_point_t query;
1308
+ query.sign = 0.0;
1309
+ query.result = mesh_query_point_no_sign(id, point, max_dist, query.face, query.u, query.v);
1310
+ return query;
1311
+ }
1312
+
1313
+ CUDA_CALLABLE inline mesh_query_point_t mesh_query_furthest_point_no_sign(uint64_t id, const vec3& point, float min_dist)
1314
+ {
1315
+ mesh_query_point_t query;
1316
+ query.sign = 0.0;
1317
+ query.result = mesh_query_furthest_point_no_sign(id, point, min_dist, query.face, query.u, query.v);
1318
+ return query;
1319
+ }
1320
+
1321
+ CUDA_CALLABLE inline mesh_query_point_t mesh_query_point_sign_normal(uint64_t id, const vec3& point, float max_dist, const float epsilon = 1e-3f)
1322
+ {
1323
+ mesh_query_point_t query;
1324
+ query.result = mesh_query_point_sign_normal(id, point, max_dist, query.sign, query.face, query.u, query.v, epsilon);
1325
+ return query;
1326
+ }
1327
+
1328
+ CUDA_CALLABLE inline mesh_query_point_t mesh_query_point_sign_winding_number(uint64_t id, const vec3& point, float max_dist, float accuracy, float winding_number_threshold)
1329
+ {
1330
+ mesh_query_point_t query;
1331
+ query.result = mesh_query_point_sign_winding_number(id, point, max_dist, query.sign, query.face, query.u, query.v, accuracy, winding_number_threshold);
1332
+ return query;
1333
+ }
1334
+
1335
+ CUDA_CALLABLE inline void adj_mesh_query_point(uint64_t id, const vec3& point, float max_dist, const mesh_query_point_t& ret,
1336
+ uint64_t adj_id, vec3& adj_point, float& adj_max_dist, mesh_query_point_t& adj_ret)
1337
+ {
1338
+ adj_mesh_query_point(id, point, max_dist, ret.sign, ret.face, ret.u, ret.v,
1339
+ adj_id, adj_point, adj_max_dist, adj_ret.sign, adj_ret.face, adj_ret.u, adj_ret.v, adj_ret.result);
1340
+ }
1341
+
1342
+ CUDA_CALLABLE inline void adj_mesh_query_point_no_sign(uint64_t id, const vec3& point, float max_dist, const mesh_query_point_t& ret,
1343
+ uint64_t adj_id, vec3& adj_point, float& adj_max_dist, mesh_query_point_t& adj_ret)
1344
+ {
1345
+ adj_mesh_query_point_no_sign(id, point, max_dist, ret.face, ret.u, ret.v,
1346
+ adj_id, adj_point, adj_max_dist, adj_ret.face, adj_ret.u, adj_ret.v, adj_ret.result);
1347
+ }
1348
+
1349
+ CUDA_CALLABLE inline void adj_mesh_query_furthest_point_no_sign(uint64_t id, const vec3& point, float min_dist, const mesh_query_point_t& ret,
1350
+ uint64_t adj_id, vec3& adj_point, float& adj_min_dist, mesh_query_point_t& adj_ret)
1351
+ {
1352
+ adj_mesh_query_furthest_point_no_sign(id, point, min_dist, ret.face, ret.u, ret.v,
1353
+ adj_id, adj_point, adj_min_dist, adj_ret.face, adj_ret.u, adj_ret.v, adj_ret.result);
1354
+ }
1355
+
1356
+ CUDA_CALLABLE inline void adj_mesh_query_point_sign_normal(uint64_t id, const vec3& point, float max_dist, float epsilon, const mesh_query_point_t& ret,
1357
+ uint64_t adj_id, vec3& adj_point, float& adj_max_dist, float& adj_epsilon, mesh_query_point_t& adj_ret)
1358
+ {
1359
+ adj_mesh_query_point_sign_normal(id, point, max_dist, ret.sign, ret.face, ret.u, ret.v, epsilon,
1360
+ adj_id, adj_point, adj_max_dist, adj_ret.sign, adj_ret.face, adj_ret.u, adj_ret.v, epsilon, adj_ret.result);
1361
+ }
1362
+
1363
+ CUDA_CALLABLE inline void adj_mesh_query_point_sign_winding_number(uint64_t id, const vec3& point, float max_dist, float accuracy, float winding_number_threshold, const mesh_query_point_t& ret,
1364
+ uint64_t adj_id, vec3& adj_point, float& adj_max_dist, float& adj_accuracy, float& adj_winding_number_threshold, mesh_query_point_t& adj_ret)
1365
+ {
1366
+ adj_mesh_query_point_sign_winding_number(id, point, max_dist, ret.sign, ret.face, ret.u, ret.v, accuracy, winding_number_threshold,
1367
+ adj_id, adj_point, adj_max_dist, adj_ret.sign, adj_ret.face, adj_ret.u, adj_ret.v, adj_accuracy, adj_winding_number_threshold, adj_ret.result);
1368
+ }
1369
+
1370
+ CUDA_CALLABLE inline bool mesh_query_ray(uint64_t id, const vec3& start, const vec3& dir, float max_t, float& t, float& u, float& v, float& sign, vec3& normal, int& face)
1371
+ {
1372
+ Mesh mesh = mesh_get(id);
1373
+
1374
+ int stack[BVH_QUERY_STACK_SIZE];
1375
+ stack[0] = *mesh.bvh.root;
1376
+ int count = 1;
1377
+
1378
+ vec3 rcp_dir = vec3(1.0f/dir[0], 1.0f/dir[1], 1.0f/dir[2]);
1379
+
1380
+ float min_t = max_t;
1381
+ int min_face;
1382
+ float min_u;
1383
+ float min_v;
1384
+ float min_sign = 1.0f;
1385
+ vec3 min_normal;
1386
+
1387
+ while (count)
1388
+ {
1389
+ const int nodeIndex = stack[--count];
1390
+
1391
+ BVHPackedNodeHalf lower = bvh_load_node(mesh.bvh.node_lowers, nodeIndex);
1392
+ BVHPackedNodeHalf upper = bvh_load_node(mesh.bvh.node_uppers, nodeIndex);
1393
+
1394
+ // todo: switch to robust ray-aabb, or expand bounds in build stage
1395
+ float eps = 1.e-3f;
1396
+ float t = 0.0f;
1397
+ bool hit = intersect_ray_aabb(start, rcp_dir, vec3(lower.x-eps, lower.y-eps, lower.z-eps), vec3(upper.x+eps, upper.y+eps, upper.z+eps), t);
1398
+
1399
+ if (hit && t < min_t)
1400
+ {
1401
+ const int left_index = lower.i;
1402
+ const int right_index = upper.i;
1403
+
1404
+ if (lower.b)
1405
+ {
1406
+ const int start_index = left_index;
1407
+ const int end_index = right_index;
1408
+ // loops through primitives in the leaf
1409
+ for (int primitive_counter = start_index; primitive_counter < end_index ; primitive_counter++)
1410
+ {
1411
+ int primitive_index = mesh.bvh.primitive_indices[primitive_counter];
1412
+ int i = mesh.indices[primitive_index * 3 + 0];
1413
+ int j = mesh.indices[primitive_index * 3 + 1];
1414
+ int k = mesh.indices[primitive_index * 3 + 2];
1415
+
1416
+ vec3 p = mesh.points[i];
1417
+ vec3 q = mesh.points[j];
1418
+ vec3 r = mesh.points[k];
1419
+
1420
+ float t, u, v, sign;
1421
+ vec3 n;
1422
+
1423
+ if (intersect_ray_tri_woop(start, dir, p, q, r, t, u, v, sign, &n))
1424
+ {
1425
+ if (t < min_t && t >= 0.0f)
1426
+ {
1427
+ min_t = t;
1428
+ min_face = primitive_index;
1429
+ min_u = u;
1430
+ min_v = v;
1431
+ min_sign = sign;
1432
+ min_normal = n;
1433
+ }
1434
+ }
1435
+ }
1436
+ }
1437
+ else
1438
+ {
1439
+ stack[count++] = left_index;
1440
+ stack[count++] = right_index;
1441
+ }
1442
+ }
1443
+ }
1444
+
1445
+ if (min_t < max_t)
1446
+ {
1447
+ // write outputs
1448
+ u = min_u;
1449
+ v = min_v;
1450
+ sign = min_sign;
1451
+ t = min_t;
1452
+ normal = normalize(min_normal);
1453
+ face = min_face;
1454
+
1455
+ return true;
1456
+ }
1457
+ else
1458
+ {
1459
+ return false;
1460
+ }
1461
+
1462
+ }
1463
+
1464
+
1465
+ CUDA_CALLABLE inline void adj_mesh_query_ray(
1466
+ uint64_t id, const vec3& start, const vec3& dir, float max_t, float t, float u, float v, float sign, const vec3& n, int face,
1467
+ uint64_t adj_id, vec3& adj_start, vec3& adj_dir, float& adj_max_t, float& adj_t, float& adj_u, float& adj_v, float& adj_sign, vec3& adj_n, int& adj_face, bool& adj_ret)
1468
+ {
1469
+
1470
+ Mesh mesh = mesh_get(id);
1471
+
1472
+ // face is determined by BVH in forward pass
1473
+ int i = mesh.indices[face*3+0];
1474
+ int j = mesh.indices[face*3+1];
1475
+ int k = mesh.indices[face*3+2];
1476
+
1477
+ vec3 a = mesh.points[i];
1478
+ vec3 b = mesh.points[j];
1479
+ vec3 c = mesh.points[k];
1480
+
1481
+ vec3 adj_a, adj_b, adj_c;
1482
+
1483
+ adj_intersect_ray_tri_woop(start, dir, a, b, c, t, u, v, sign, n, adj_start, adj_dir, adj_a, adj_b, adj_c, adj_t, adj_u, adj_v, adj_sign, adj_n, adj_ret);
1484
+
1485
+ }
1486
+
1487
+
1488
+ // Stores the result of querying the closest point on a mesh.
1489
+ struct mesh_query_ray_t
1490
+ {
1491
+ CUDA_CALLABLE mesh_query_ray_t()
1492
+ : result(false),
1493
+ sign(0.0f),
1494
+ face(0),
1495
+ t(0.0f),
1496
+ u(0.0f),
1497
+ v(0.0f),
1498
+ normal()
1499
+ {
1500
+ }
1501
+
1502
+ // Required for adjoint computations.
1503
+ CUDA_CALLABLE inline mesh_query_ray_t& operator+=(const mesh_query_ray_t& other)
1504
+ {
1505
+ result += other.result;
1506
+ sign += other.sign;
1507
+ face += other.face;
1508
+ t += other.t;
1509
+ u += other.u;
1510
+ v += other.v;
1511
+ normal += other.normal;
1512
+ return *this;
1513
+ }
1514
+
1515
+ float sign;
1516
+ int face;
1517
+ float t;
1518
+ float u;
1519
+ float v;
1520
+ vec3 normal;
1521
+ bool result;
1522
+ };
1523
+
1524
+ CUDA_CALLABLE inline mesh_query_ray_t mesh_query_ray(uint64_t id, const vec3& start, const vec3& dir, float max_t)
1525
+ {
1526
+ mesh_query_ray_t query;
1527
+ query.result = mesh_query_ray(id, start, dir, max_t, query.t, query.u, query.v, query.sign, query.normal, query.face);
1528
+ return query;
1529
+ }
1530
+
1531
+ CUDA_CALLABLE inline void
1532
+ adj_mesh_query_ray(
1533
+ uint64_t id, const vec3& start, const vec3& dir, float max_t, const mesh_query_ray_t& ret,
1534
+ uint64_t adj_id, vec3& adj_start, vec3& adj_dir, float& adj_max_t, mesh_query_ray_t& adj_ret
1535
+ )
1536
+ {
1537
+ adj_mesh_query_ray(
1538
+ id, start, dir, max_t, ret.t, ret.u, ret.v, ret.sign, ret.normal, ret.face,
1539
+ adj_id, adj_start, adj_dir, adj_max_t, adj_ret.t, adj_ret.u, adj_ret.v, adj_ret.sign, adj_ret.normal, adj_ret.face, adj_ret.result
1540
+ );
1541
+ }
1542
+
1543
+
1544
+ // determine if a point is inside (ret < 0 ) or outside the mesh (ret > 0)
1545
+ CUDA_CALLABLE inline float mesh_query_inside(uint64_t id, const vec3& p)
1546
+ {
1547
+ float t, u, v, sign;
1548
+ vec3 n;
1549
+ int face;
1550
+
1551
+ int vote = 0;
1552
+
1553
+ for(int i = 0; i <3; ++i)
1554
+ {
1555
+ if (mesh_query_ray(id, p, vec3(float(i==0), float(i==1), float(i==2)), FLT_MAX, t, u, v, sign, n, face) && sign < 0)
1556
+ {
1557
+ vote++;
1558
+ }
1559
+ }
1560
+
1561
+ if (vote >= 2)
1562
+ return -1.0f;
1563
+ else
1564
+ return 1.0f;
1565
+ }
1566
+
1567
+ // stores state required to traverse the BVH nodes that
1568
+ // overlap with a query AABB.
1569
+ struct mesh_query_aabb_t
1570
+ {
1571
+ CUDA_CALLABLE mesh_query_aabb_t()
1572
+ : mesh(),
1573
+ stack(),
1574
+ count(0),
1575
+ input_lower(),
1576
+ input_upper(),
1577
+ face(0),
1578
+ primitive_counter(-1)
1579
+ {}
1580
+
1581
+ // Required for adjoint computations.
1582
+ CUDA_CALLABLE inline mesh_query_aabb_t& operator+=(const mesh_query_aabb_t& other)
1583
+ {
1584
+ return *this;
1585
+ }
1586
+
1587
+ // Mesh Id
1588
+ Mesh mesh;
1589
+ // BVH traversal stack:
1590
+ int stack[BVH_QUERY_STACK_SIZE];
1591
+ int count;
1592
+
1593
+ // inputs
1594
+ wp::vec3 input_lower;
1595
+ wp::vec3 input_upper;
1596
+
1597
+ // >= 0 if currently in a packed leaf node
1598
+ int primitive_counter;
1599
+
1600
+ // Face
1601
+ int face;
1602
+ };
1603
+
1604
+
1605
+
1606
+ CUDA_CALLABLE inline mesh_query_aabb_t mesh_query_aabb(
1607
+ uint64_t id, const vec3& lower, const vec3& upper)
1608
+ {
1609
+ // This routine traverses the BVH tree until it finds
1610
+ // the first triangle with an overlapping bvh.
1611
+
1612
+ // initialize empty
1613
+ mesh_query_aabb_t query;
1614
+ query.face = -1;
1615
+
1616
+ Mesh mesh = mesh_get(id);
1617
+ query.mesh = mesh;
1618
+
1619
+ query.stack[0] = *mesh.bvh.root;
1620
+ query.count = 1;
1621
+ query.input_lower = lower;
1622
+ query.input_upper = upper;
1623
+
1624
+ wp::bounds3 input_bounds(query.input_lower, query.input_upper);
1625
+
1626
+ // Navigate through the bvh, find the first overlapping leaf node.
1627
+ while (query.count)
1628
+ {
1629
+ const int nodeIndex = query.stack[--query.count];
1630
+ BVHPackedNodeHalf node_lower = bvh_load_node(mesh.bvh.node_lowers, nodeIndex);
1631
+ BVHPackedNodeHalf node_upper = bvh_load_node(mesh.bvh.node_uppers, nodeIndex);
1632
+
1633
+ if (!input_bounds.overlaps((vec3&)node_lower, (vec3&)node_upper))
1634
+ {
1635
+ // Skip this box, it doesn't overlap with our target box.
1636
+ continue;
1637
+ }
1638
+
1639
+ const int left_index = node_lower.i;
1640
+ const int right_index = node_upper.i;
1641
+
1642
+ // Make bounds from this AABB
1643
+ if (node_lower.b)
1644
+ {
1645
+ // Reached a leaf node, point to its first primitive
1646
+ // Back up one level and return
1647
+ query.primitive_counter = left_index;
1648
+ query.stack[query.count++] = nodeIndex;
1649
+ return query;
1650
+ }
1651
+ else
1652
+ {
1653
+ query.stack[query.count++] = left_index;
1654
+ query.stack[query.count++] = right_index;
1655
+ }
1656
+ }
1657
+
1658
+ return query;
1659
+ }
1660
+
1661
+ //Stub
1662
+ CUDA_CALLABLE inline void adj_mesh_query_aabb(uint64_t id, const vec3& lower, const vec3& upper,
1663
+ uint64_t, vec3&, vec3&, mesh_query_aabb_t&)
1664
+ {
1665
+
1666
+ }
1667
+
1668
+ CUDA_CALLABLE inline bool mesh_query_aabb_next(mesh_query_aabb_t& query, int& index)
1669
+ {
1670
+ Mesh mesh = query.mesh;
1671
+
1672
+ wp::bounds3 input_bounds(query.input_lower, query.input_upper);
1673
+
1674
+ if (query.primitive_counter != -1)
1675
+ // currently in a leaf node which is the last node in the stack
1676
+ {
1677
+ const int node_index = query.stack[query.count - 1];
1678
+ BVHPackedNodeHalf node_lower = bvh_load_node(mesh.bvh.node_lowers, node_index);
1679
+ BVHPackedNodeHalf node_upper = bvh_load_node(mesh.bvh.node_uppers, node_index);
1680
+
1681
+ const int end = node_upper.i;
1682
+ for (int primitive_counter = query.primitive_counter; primitive_counter < end; primitive_counter++)
1683
+ {
1684
+ int primitive_index = mesh.bvh.primitive_indices[primitive_counter];
1685
+ if (input_bounds.overlaps(mesh.lowers[primitive_index], mesh.uppers[primitive_index]))
1686
+ {
1687
+ if (primitive_counter < end - 1)
1688
+ // still need to come back to this leaf node for the leftover primitives
1689
+ {
1690
+ query.primitive_counter = primitive_counter + 1;
1691
+ }
1692
+ else
1693
+ // no need to come back to this leaf node
1694
+ {
1695
+ query.count--;
1696
+ query.primitive_counter = -1;
1697
+ }
1698
+ index = primitive_index;
1699
+ query.face = primitive_index;
1700
+
1701
+ return true;
1702
+ }
1703
+ }
1704
+ // if we reach here it means we have finished the current leaf node without finding intersections
1705
+ query.primitive_counter = -1;
1706
+ // remove the leaf node from the back of the stack because it is finished
1707
+ // and continue the bvh traversal
1708
+ query.count--;
1709
+ }
1710
+
1711
+ // Navigate through the bvh, find the first overlapping leaf node.
1712
+ while (query.count)
1713
+ {
1714
+ const int node_index = query.stack[--query.count];
1715
+ BVHPackedNodeHalf node_lower = bvh_load_node(mesh.bvh.node_lowers, node_index);
1716
+ BVHPackedNodeHalf node_upper = bvh_load_node(mesh.bvh.node_uppers, node_index);
1717
+
1718
+ if (!input_bounds.overlaps((vec3&)node_lower, (vec3&)node_upper))
1719
+ {
1720
+ // Skip this box, it doesn't overlap with our target box.
1721
+ continue;
1722
+ }
1723
+
1724
+ const int left_index = node_lower.i;
1725
+ const int right_index = node_upper.i;
1726
+
1727
+ // Make bounds from this AABB
1728
+ if (node_lower.b)
1729
+ {
1730
+ // found leaf, loop through its content primitives
1731
+ const int start = left_index;
1732
+ const int end = right_index;
1733
+
1734
+ for (int primitive_counter = start; primitive_counter < end; primitive_counter++)
1735
+ {
1736
+ int primitive_index = mesh.bvh.primitive_indices[primitive_counter];
1737
+ if (input_bounds.overlaps(mesh.lowers[primitive_index], mesh.uppers[primitive_index]))
1738
+ {
1739
+ if (primitive_counter < end - 1)
1740
+ // still need to come back to this leaf node for the leftover primitives
1741
+ {
1742
+ query.primitive_counter = primitive_counter + 1;
1743
+ query.stack[query.count++] = node_index;
1744
+ }
1745
+ else
1746
+ // no need to come back to this leaf node
1747
+ {
1748
+ query.primitive_counter = -1;
1749
+ }
1750
+ index = primitive_index;
1751
+ query.face = primitive_index;
1752
+
1753
+ return true;
1754
+ }
1755
+ }
1756
+ }
1757
+ else
1758
+ {
1759
+ query.stack[query.count++] = left_index;
1760
+ query.stack[query.count++] = right_index;
1761
+ }
1762
+ }
1763
+ return false;
1764
+ }
1765
+
1766
+
1767
+ CUDA_CALLABLE inline int iter_next(mesh_query_aabb_t& query)
1768
+ {
1769
+ return query.face;
1770
+ }
1771
+
1772
+ CUDA_CALLABLE inline bool iter_cmp(mesh_query_aabb_t& query)
1773
+ {
1774
+ bool finished = mesh_query_aabb_next(query, query.face);
1775
+ return finished;
1776
+ }
1777
+
1778
+ CUDA_CALLABLE inline mesh_query_aabb_t iter_reverse(const mesh_query_aabb_t& query)
1779
+ {
1780
+ // can't reverse BVH queries, users should not rely on neighbor ordering
1781
+ return query;
1782
+ }
1783
+
1784
+ CUDA_CALLABLE inline void adj_iter_reverse(const mesh_query_aabb_t& query, mesh_query_aabb_t& adj_query, mesh_query_aabb_t& adj_ret)
1785
+ {
1786
+ }
1787
+
1788
+
1789
+ // stub
1790
+ CUDA_CALLABLE inline void adj_mesh_query_aabb_next(mesh_query_aabb_t& query, int& index, mesh_query_aabb_t&, int&, bool&)
1791
+ {
1792
+
1793
+ }
1794
+
1795
+
1796
+ CUDA_CALLABLE inline vec3 mesh_eval_position(uint64_t id, int tri, float u, float v)
1797
+ {
1798
+ Mesh mesh = mesh_get(id);
1799
+
1800
+ if (!mesh.points)
1801
+ return vec3();
1802
+
1803
+ assert(tri < mesh.num_tris);
1804
+
1805
+ int i = mesh.indices[tri*3+0];
1806
+ int j = mesh.indices[tri*3+1];
1807
+ int k = mesh.indices[tri*3+2];
1808
+
1809
+ vec3 p = mesh.points[i];
1810
+ vec3 q = mesh.points[j];
1811
+ vec3 r = mesh.points[k];
1812
+
1813
+ return p*u + q*v + r*(1.0f-u-v);
1814
+ }
1815
+
1816
+ CUDA_CALLABLE inline vec3 mesh_eval_velocity(uint64_t id, int tri, float u, float v)
1817
+ {
1818
+ Mesh mesh = mesh_get(id);
1819
+
1820
+ if (!mesh.velocities)
1821
+ return vec3();
1822
+
1823
+ assert(tri < mesh.num_tris);
1824
+
1825
+ int i = mesh.indices[tri*3+0];
1826
+ int j = mesh.indices[tri*3+1];
1827
+ int k = mesh.indices[tri*3+2];
1828
+
1829
+ vec3 vp = mesh.velocities[i];
1830
+ vec3 vq = mesh.velocities[j];
1831
+ vec3 vr = mesh.velocities[k];
1832
+
1833
+ return vp*u + vq*v + vr*(1.0f-u-v);
1834
+ }
1835
+
1836
+
1837
+ CUDA_CALLABLE inline void adj_mesh_eval_position(uint64_t id, int tri, float u, float v,
1838
+ uint64_t& adj_id, int& adj_tri, float& adj_u, float& adj_v, const vec3& adj_ret)
1839
+ {
1840
+ Mesh mesh = mesh_get(id);
1841
+
1842
+ if (!mesh.points)
1843
+ return;
1844
+
1845
+ assert(tri < mesh.num_tris);
1846
+
1847
+ int i = mesh.indices[tri*3+0];
1848
+ int j = mesh.indices[tri*3+1];
1849
+ int k = mesh.indices[tri*3+2];
1850
+
1851
+ vec3 p = mesh.points[i];
1852
+ vec3 q = mesh.points[j];
1853
+ vec3 r = mesh.points[k];
1854
+
1855
+ adj_u += (p[0] - r[0]) * adj_ret[0] + (p[1] - r[1]) * adj_ret[1] + (p[2] - r[2]) * adj_ret[2];
1856
+ adj_v += (q[0] - r[0]) * adj_ret[0] + (q[1] - r[1]) * adj_ret[1] + (q[2] - r[2]) * adj_ret[2];
1857
+ }
1858
+
1859
+ CUDA_CALLABLE inline void adj_mesh_eval_velocity(uint64_t id, int tri, float u, float v,
1860
+ uint64_t& adj_id, int& adj_tri, float& adj_u, float& adj_v, const vec3& adj_ret)
1861
+ {
1862
+ Mesh mesh = mesh_get(id);
1863
+
1864
+ if (!mesh.velocities)
1865
+ return;
1866
+
1867
+ assert(tri < mesh.num_tris);
1868
+
1869
+ int i = mesh.indices[tri*3+0];
1870
+ int j = mesh.indices[tri*3+1];
1871
+ int k = mesh.indices[tri*3+2];
1872
+
1873
+ vec3 vp = mesh.velocities[i];
1874
+ vec3 vq = mesh.velocities[j];
1875
+ vec3 vr = mesh.velocities[k];
1876
+
1877
+ adj_u += (vp[0] - vr[0]) * adj_ret[0] + (vp[1] - vr[1]) * adj_ret[1] + (vp[2] - vr[2]) * adj_ret[2];
1878
+ adj_v += (vq[0] - vr[0]) * adj_ret[0] + (vq[1] - vr[1]) * adj_ret[1] + (vq[2] - vr[2]) * adj_ret[2];
1879
+ }
1880
+
1881
+ CUDA_CALLABLE inline vec3 mesh_eval_face_normal(uint64_t id, int tri)
1882
+ {
1883
+ Mesh mesh = mesh_get(id);
1884
+
1885
+ if (!mesh.points)
1886
+ return vec3();
1887
+
1888
+ assert(tri < mesh.num_tris);
1889
+
1890
+ int i = mesh.indices[tri*3+0];
1891
+ int j = mesh.indices[tri*3+1];
1892
+ int k = mesh.indices[tri*3+2];
1893
+
1894
+ vec3 p = mesh.points[i];
1895
+ vec3 q = mesh.points[j];
1896
+ vec3 r = mesh.points[k];
1897
+
1898
+ return normalize(cross(q - p, r - p));
1899
+ }
1900
+
1901
+ CUDA_CALLABLE inline void adj_mesh_eval_face_normal(uint64_t id, int tri,
1902
+ uint64_t& adj_id, int& adj_tri, const vec3& adj_ret)
1903
+ {
1904
+ // no-op
1905
+ }
1906
+
1907
+ CUDA_CALLABLE inline vec3 mesh_get_point(uint64_t id, int index)
1908
+ {
1909
+ Mesh mesh = mesh_get(id);
1910
+
1911
+ if (!mesh.points)
1912
+ return vec3();
1913
+
1914
+ #if FP_CHECK
1915
+ if (index >= mesh.num_tris * 3)
1916
+ {
1917
+ printf("mesh_get_point (%llu, %d) out of bounds at %s:%d\n", id, index, __FILE__, __LINE__);
1918
+ assert(0);
1919
+ }
1920
+ #endif
1921
+
1922
+ int i = mesh.indices[index];
1923
+ return mesh.points[i];
1924
+ }
1925
+
1926
+ CUDA_CALLABLE inline void adj_mesh_get_point(uint64_t id, int index,
1927
+ uint64_t& adj_id, int& adj_index, const vec3& adj_ret)
1928
+ {
1929
+ // no-op
1930
+ }
1931
+
1932
+ CUDA_CALLABLE inline vec3 mesh_get_velocity(uint64_t id, int index)
1933
+ {
1934
+ Mesh mesh = mesh_get(id);
1935
+
1936
+ if (!mesh.velocities)
1937
+ return vec3();
1938
+
1939
+ #if FP_CHECK
1940
+ if (index >= mesh.num_tris * 3)
1941
+ {
1942
+ printf("mesh_get_velocity (%llu, %d) out of bounds at %s:%d\n", id, index, __FILE__, __LINE__);
1943
+ assert(0);
1944
+ }
1945
+ #endif
1946
+
1947
+ int i = mesh.indices[index];
1948
+ return mesh.velocities[i];
1949
+ }
1950
+
1951
+ CUDA_CALLABLE inline void adj_mesh_get_velocity(uint64_t id, int index,
1952
+ uint64_t& adj_id, int& adj_index, const vec3& adj_ret)
1953
+ {
1954
+ // no-op
1955
+ }
1956
+
1957
+ CUDA_CALLABLE inline int mesh_get_index(uint64_t id, int face_vertex_index)
1958
+ {
1959
+ Mesh mesh = mesh_get(id);
1960
+
1961
+ if (!mesh.indices)
1962
+ return -1;
1963
+
1964
+ assert(face_vertex_index < mesh.num_tris * 3);
1965
+
1966
+ return mesh.indices[face_vertex_index];
1967
+ }
1968
+
1969
+ CUDA_CALLABLE inline void adj_mesh_get_index(uint64_t id, int index,
1970
+ uint64_t& adj_id, int& adj_index, const vec3& adj_ret)
1971
+ {
1972
+ // no-op
1973
+ }
1974
+
1975
+ CUDA_CALLABLE bool mesh_get_descriptor(uint64_t id, Mesh& mesh);
1976
+ CUDA_CALLABLE bool mesh_set_descriptor(uint64_t id, const Mesh& mesh);
1977
+ CUDA_CALLABLE void mesh_add_descriptor(uint64_t id, const Mesh& mesh);
1978
+ CUDA_CALLABLE void mesh_rem_descriptor(uint64_t id);
1979
+
1980
+ } // namespace wp