warp-lang 1.0.1__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 -279
  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 -28
  36. warp/examples/core/example_dem.py +234 -221
  37. warp/examples/core/example_fluid.py +293 -267
  38. warp/examples/core/example_graph_capture.py +144 -129
  39. warp/examples/core/example_marching_cubes.py +188 -176
  40. warp/examples/core/example_mesh.py +174 -154
  41. warp/examples/core/example_mesh_intersect.py +205 -193
  42. warp/examples/core/example_nvdb.py +176 -169
  43. warp/examples/core/example_raycast.py +105 -89
  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 -389
  47. warp/examples/core/example_torch.py +222 -181
  48. warp/examples/core/example_wave.py +263 -249
  49. warp/examples/fem/bsr_utils.py +378 -380
  50. warp/examples/fem/example_apic_fluid.py +407 -391
  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 -248
  65. warp/examples/optim/example_cloth_throw.py +222 -210
  66. warp/examples/optim/example_diffray.py +566 -535
  67. warp/examples/optim/example_drone.py +864 -835
  68. warp/examples/optim/example_inverse_kinematics.py +176 -169
  69. warp/examples/optim/example_inverse_kinematics_torch.py +185 -170
  70. warp/examples/optim/example_spring_cage.py +239 -234
  71. warp/examples/optim/example_trajectory.py +223 -201
  72. warp/examples/optim/example_walker.py +306 -292
  73. warp/examples/sim/example_cartpole.py +139 -128
  74. warp/examples/sim/example_cloth.py +196 -184
  75. warp/examples/sim/example_granular.py +124 -113
  76. warp/examples/sim/example_granular_collision_sdf.py +197 -185
  77. warp/examples/sim/example_jacobian_ik.py +236 -213
  78. warp/examples/sim/example_particle_chain.py +118 -106
  79. warp/examples/sim/example_quadruped.py +193 -179
  80. warp/examples/sim/example_rigid_chain.py +197 -189
  81. warp/examples/sim/example_rigid_contact.py +189 -176
  82. warp/examples/sim/example_rigid_force.py +127 -126
  83. warp/examples/sim/example_rigid_gyroscopic.py +109 -97
  84. warp/examples/sim/example_rigid_soft_contact.py +134 -124
  85. warp/examples/sim/example_soft_body.py +190 -178
  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.1.dist-info → warp_lang-1.1.0.dist-info}/LICENSE.md +126 -126
  339. {warp_lang-1.0.1.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.1.dist-info/RECORD +0 -352
  345. {warp_lang-1.0.1.dist-info → warp_lang-1.1.0.dist-info}/WHEEL +0 -0
  346. {warp_lang-1.0.1.dist-info → warp_lang-1.1.0.dist-info}/top_level.txt +0 -0
warp/native/vec.h CHANGED
@@ -1,1133 +1,1133 @@
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
-
9
- #pragma once
10
-
11
- #include "initializer_array.h"
12
-
13
- namespace wp
14
- {
15
-
16
- template<unsigned Length, typename Type>
17
- struct vec_t
18
- {
19
- Type c[Length];
20
-
21
- inline CUDA_CALLABLE vec_t()
22
- : c()
23
- {}
24
-
25
- inline CUDA_CALLABLE vec_t(Type s)
26
- {
27
- for( unsigned i=0; i < Length; ++i )
28
- {
29
- c[i] = s;
30
- }
31
- }
32
-
33
- template <typename OtherType>
34
- inline explicit CUDA_CALLABLE vec_t(const vec_t<Length, OtherType>& other)
35
- {
36
- for( unsigned i=0; i < Length; ++i )
37
- {
38
- c[i] = static_cast<Type>(other[i]);
39
- }
40
- }
41
-
42
- inline CUDA_CALLABLE vec_t(Type x, Type y)
43
- {
44
- assert(Length == 2);
45
- c[0]=x;
46
- c[1]=y;
47
- }
48
-
49
- inline CUDA_CALLABLE vec_t(Type x, Type y, Type z)
50
- {
51
- assert(Length == 3);
52
- c[0]=x;
53
- c[1]=y;
54
- c[2]=z;
55
- }
56
-
57
-
58
- inline CUDA_CALLABLE vec_t(Type x, Type y, Type z, Type w)
59
- {
60
- assert(Length == 4);
61
- c[0]=x;
62
- c[1]=y;
63
- c[2]=z;
64
- c[3]=w;
65
- }
66
-
67
- inline CUDA_CALLABLE vec_t(const initializer_array<Length, Type> &l)
68
- {
69
- for( unsigned i=0; i < Length; ++i )
70
- {
71
- c[i] = l[i];
72
- }
73
- }
74
-
75
- // special screw vector constructor for spatial_vectors:
76
- inline CUDA_CALLABLE vec_t(vec_t<3,Type> w, vec_t<3,Type> v)
77
- {
78
- c[0] = w[0];
79
- c[1] = w[1];
80
- c[2] = w[2];
81
- c[3] = v[0];
82
- c[4] = v[1];
83
- c[5] = v[2];
84
- }
85
-
86
- inline CUDA_CALLABLE Type operator[](int index) const
87
- {
88
- assert(index < Length);
89
- return c[index];
90
- }
91
-
92
- inline CUDA_CALLABLE Type& operator[](int index)
93
- {
94
- assert(index < Length);
95
- return c[index];
96
- }
97
- };
98
-
99
- using vec2b = vec_t<2,int8>;
100
- using vec3b = vec_t<3,int8>;
101
- using vec4b = vec_t<4,int8>;
102
- using vec2ub = vec_t<2,uint8>;
103
- using vec3ub = vec_t<3,uint8>;
104
- using vec4ub = vec_t<4,uint8>;
105
-
106
- using vec2s = vec_t<2,int16>;
107
- using vec3s = vec_t<3,int16>;
108
- using vec4s = vec_t<4,int16>;
109
- using vec2us = vec_t<2,uint16>;
110
- using vec3us = vec_t<3,uint16>;
111
- using vec4us = vec_t<4,uint16>;
112
-
113
- using vec2i = vec_t<2,int32>;
114
- using vec3i = vec_t<3,int32>;
115
- using vec4i = vec_t<4,int32>;
116
- using vec2ui = vec_t<2,uint32>;
117
- using vec3ui = vec_t<3,uint32>;
118
- using vec4ui = vec_t<4,uint32>;
119
-
120
- using vec2l = vec_t<2,int64>;
121
- using vec3l = vec_t<3,int64>;
122
- using vec4l = vec_t<4,int64>;
123
- using vec2ul = vec_t<2,uint64>;
124
- using vec3ul = vec_t<3,uint64>;
125
- using vec4ul = vec_t<4,uint64>;
126
-
127
- using vec2h = vec_t<2,half>;
128
- using vec3h = vec_t<3,half>;
129
- using vec4h = vec_t<4,half>;
130
-
131
- using vec2 = vec_t<2,float>;
132
- using vec3 = vec_t<3,float>;
133
- using vec4 = vec_t<4,float>;
134
-
135
- using vec2f = vec_t<2,float>;
136
- using vec3f = vec_t<3,float>;
137
- using vec4f = vec_t<4,float>;
138
-
139
- using vec2d = vec_t<2,double>;
140
- using vec3d = vec_t<3,double>;
141
- using vec4d = vec_t<4,double>;
142
-
143
- //--------------
144
- // vec<Length, Type> methods
145
-
146
- // Should these accept const references as arguments? It's all
147
- // inlined so maybe it doesn't matter? Even if it does, it
148
- // probably depends on the Length of the vector...
149
-
150
- // negation:
151
- template<unsigned Length, typename Type>
152
- inline CUDA_CALLABLE vec_t<Length, Type> operator - (vec_t<Length, Type> a)
153
- {
154
- // NB: this constructor will initialize all ret's components to 0, which is
155
- // unnecessary...
156
- vec_t<Length, Type> ret;
157
- for( unsigned i=0; i < Length; ++i )
158
- {
159
- ret[i] = -a[i];
160
- }
161
-
162
- // Wonder if this does a load of copying when it returns... hopefully not as it's inlined?
163
- return ret;
164
- }
165
-
166
- template<unsigned Length, typename Type>
167
- CUDA_CALLABLE inline vec_t<Length, Type> pos(const vec_t<Length, Type>& x)
168
- {
169
- return x;
170
- }
171
-
172
- template<unsigned Length, typename Type>
173
- CUDA_CALLABLE inline vec_t<Length, Type> neg(const vec_t<Length, Type>& x)
174
- {
175
- return -x;
176
- }
177
-
178
- template<typename Type>
179
- CUDA_CALLABLE inline vec_t<3, Type> neg(const vec_t<3, Type>& x)
180
- {
181
- return vec_t<3, Type>(-x.c[0], -x.c[1], -x.c[2]);
182
- }
183
-
184
- template<typename Type>
185
- CUDA_CALLABLE inline vec_t<2, Type> neg(const vec_t<2, Type>& x)
186
- {
187
- return vec_t<2, Type>(-x.c[0], -x.c[1]);
188
- }
189
-
190
- template<unsigned Length, typename Type>
191
- CUDA_CALLABLE inline void adj_neg(const vec_t<Length, Type>& x, vec_t<Length, Type>& adj_x, const vec_t<Length, Type>& adj_ret)
192
- {
193
- adj_x -= adj_ret;
194
- }
195
-
196
- // equality:
197
- template<unsigned Length, typename Type>
198
- inline CUDA_CALLABLE bool operator ==(const vec_t<Length, Type>& a, const vec_t<Length, Type>& b)
199
- {
200
- for( unsigned i=0; i < Length; ++i )
201
- {
202
- if(a[i] != b[i])
203
- {
204
- return false;
205
- }
206
- }
207
- return true;
208
- }
209
-
210
- // scalar multiplication:
211
- template<unsigned Length, typename Type>
212
- inline CUDA_CALLABLE vec_t<Length, Type> mul(vec_t<Length, Type> a, Type s)
213
- {
214
- vec_t<Length, Type> ret;
215
- for( unsigned i=0; i < Length; ++i )
216
- {
217
- ret[i] = a[i] * s;
218
- }
219
- return ret;
220
- }
221
-
222
- template<typename Type>
223
- inline CUDA_CALLABLE vec_t<3, Type> mul(vec_t<3, Type> a, Type s)
224
- {
225
- return vec_t<3, Type>(a.c[0]*s,a.c[1]*s,a.c[2]*s);
226
- }
227
-
228
- template<typename Type>
229
- inline CUDA_CALLABLE vec_t<2, Type> mul(vec_t<2, Type> a, Type s)
230
- {
231
- return vec_t<2, Type>(a.c[0]*s,a.c[1]*s);
232
- }
233
-
234
- template<unsigned Length, typename Type>
235
- inline CUDA_CALLABLE vec_t<Length, Type> mul(Type s, vec_t<Length, Type> a)
236
- {
237
- return mul(a, s);
238
- }
239
-
240
- template<unsigned Length, typename Type>
241
- inline CUDA_CALLABLE vec_t<Length, Type> operator*(Type s, vec_t<Length, Type> a)
242
- {
243
- return mul(a, s);
244
- }
245
-
246
- template<unsigned Length, typename Type>
247
- inline CUDA_CALLABLE vec_t<Length, Type> operator*(vec_t<Length, Type> a, Type s)
248
- {
249
- return mul(a, s);
250
- }
251
-
252
-
253
- // component wise multiplication:
254
- template<unsigned Length, typename Type>
255
- inline CUDA_CALLABLE vec_t<Length, Type> cw_mul(vec_t<Length, Type> a, vec_t<Length, Type> b)
256
- {
257
- vec_t<Length, Type> ret;
258
- for( unsigned i=0; i < Length; ++i )
259
- {
260
- ret[i] = a[i] * b[i];
261
- }
262
- return ret;
263
- }
264
-
265
- // division
266
- template<unsigned Length, typename Type>
267
- inline CUDA_CALLABLE vec_t<Length, Type> div(vec_t<Length, Type> a, Type s)
268
- {
269
- vec_t<Length, Type> ret;
270
- for( unsigned i=0; i < Length; ++i )
271
- {
272
- ret[i] = a[i] / s;
273
- }
274
- return ret;
275
- }
276
-
277
- template<typename Type>
278
- inline CUDA_CALLABLE vec_t<3, Type> div(vec_t<3, Type> a, Type s)
279
- {
280
- return vec_t<3, Type>(a.c[0]/s,a.c[1]/s,a.c[2]/s);
281
- }
282
-
283
- template<typename Type>
284
- inline CUDA_CALLABLE vec_t<2, Type> div(vec_t<2, Type> a, Type s)
285
- {
286
- return vec_t<2, Type>(a.c[0]/s,a.c[1]/s);
287
- }
288
-
289
- template<unsigned Length, typename Type>
290
- inline CUDA_CALLABLE vec_t<Length, Type> div(Type s, vec_t<Length, Type> a)
291
- {
292
- vec_t<Length, Type> ret;
293
- for (unsigned i=0; i < Length; ++i)
294
- {
295
- ret[i] = s / a[i];
296
- }
297
- return ret;
298
- }
299
-
300
- template<typename Type>
301
- inline CUDA_CALLABLE vec_t<3, Type> div(Type s, vec_t<3, Type> a)
302
- {
303
- return vec_t<3, Type>(s/a.c[0],s/a.c[1],s/a.c[2]);
304
- }
305
-
306
- template<typename Type>
307
- inline CUDA_CALLABLE vec_t<2, Type> div(Type s, vec_t<2, Type> a)
308
- {
309
- return vec_t<2, Type>(s/a.c[0],s/a.c[1]);
310
- }
311
-
312
- template<unsigned Length, typename Type>
313
- inline CUDA_CALLABLE vec_t<Length, Type> operator / (vec_t<Length, Type> a, Type s)
314
- {
315
- return div(a,s);
316
- }
317
-
318
- template<unsigned Length, typename Type>
319
- inline CUDA_CALLABLE vec_t<Length, Type> operator / (Type s, vec_t<Length, Type> a)
320
- {
321
- return div(s, a);
322
- }
323
-
324
- // component wise division
325
- template<unsigned Length, typename Type>
326
- inline CUDA_CALLABLE vec_t<Length, Type> cw_div(vec_t<Length, Type> a, vec_t<Length, Type> b)
327
- {
328
- vec_t<Length, Type> ret;
329
- for( unsigned i=0; i < Length; ++i )
330
- {
331
- ret[i] = a[i] / b[i];
332
- }
333
- return ret;
334
- }
335
-
336
- // addition
337
- template<unsigned Length, typename Type>
338
- inline CUDA_CALLABLE vec_t<Length, Type> add(vec_t<Length, Type> a, vec_t<Length, Type> b)
339
- {
340
- vec_t<Length, Type> ret;
341
- for( unsigned i=0; i < Length; ++i )
342
- {
343
- ret[i] = a[i] + b[i];
344
- }
345
- return ret;
346
- }
347
-
348
- template<typename Type>
349
- inline CUDA_CALLABLE vec_t<2, Type> add(vec_t<2, Type> a, vec_t<2, Type> b)
350
- {
351
- return vec_t<2, Type>( a.c[0] + b.c[0], a.c[1] + b.c[1]);
352
- }
353
-
354
- template<typename Type>
355
- inline CUDA_CALLABLE vec_t<3, Type> add(vec_t<3, Type> a, vec_t<3, Type> b)
356
- {
357
- return vec_t<3, Type>( a.c[0] + b.c[0], a.c[1] + b.c[1], a.c[2] + b.c[2]);
358
- }
359
-
360
- // subtraction
361
- template<unsigned Length, typename Type>
362
- inline CUDA_CALLABLE vec_t<Length, Type> sub(vec_t<Length, Type> a, vec_t<Length, Type> b)
363
- {
364
- vec_t<Length, Type> ret;
365
- for( unsigned i=0; i < Length; ++i )
366
- {
367
- ret[i] = Type(a[i] - b[i]);
368
- }
369
- return ret;
370
- }
371
-
372
- template<typename Type>
373
- inline CUDA_CALLABLE vec_t<2, Type> sub(vec_t<2, Type> a, vec_t<2, Type> b)
374
- {
375
- return vec_t<2, Type>( a.c[0] - b.c[0], a.c[1] - b.c[1]);
376
- }
377
-
378
- template<typename Type>
379
- inline CUDA_CALLABLE vec_t<3, Type> sub(vec_t<3, Type> a, vec_t<3, Type> b)
380
- {
381
- return vec_t<3, Type>( a.c[0] - b.c[0], a.c[1] - b.c[1], a.c[2] - b.c[2]);
382
- }
383
-
384
- // dot product:
385
- template<unsigned Length, typename Type>
386
- inline CUDA_CALLABLE Type dot(vec_t<Length, Type> a, vec_t<Length, Type> b)
387
- {
388
- Type ret(0);
389
- for( unsigned i=0; i < Length; ++i )
390
- {
391
- ret += a[i] * b[i];
392
- }
393
- return ret;
394
- }
395
-
396
- template<typename Type>
397
- inline CUDA_CALLABLE Type dot(vec_t<2, Type> a, vec_t<2, Type> b)
398
- {
399
- return a.c[0] * b.c[0] + a.c[1] * b.c[1];
400
- }
401
-
402
- template<typename Type>
403
- inline CUDA_CALLABLE Type dot(vec_t<3, Type> a, vec_t<3, Type> b)
404
- {
405
- return a.c[0] * b.c[0] + a.c[1] * b.c[1] + a.c[2] * b.c[2];
406
- }
407
-
408
- template<unsigned Length, typename Type>
409
- inline CUDA_CALLABLE Type tensordot(vec_t<Length, Type> a, vec_t<Length, Type> b)
410
- {
411
- // corresponds to `np.tensordot()` with all axes being contracted
412
- return dot(a, b);
413
- }
414
-
415
-
416
- template<unsigned Length, typename Type>
417
- inline CUDA_CALLABLE Type extract(const vec_t<Length, Type> & a, int idx)
418
- {
419
- #ifndef NDEBUG
420
- if (idx < 0 || idx >= Length)
421
- {
422
- printf("vec index %d out of bounds at %s %d\n", idx, __FILE__, __LINE__);
423
- assert(0);
424
- }
425
- #endif
426
-
427
- return a[idx];
428
- }
429
-
430
- template<unsigned Length, typename Type>
431
- inline CUDA_CALLABLE Type* index(vec_t<Length, Type>& v, int idx)
432
- {
433
- #ifndef NDEBUG
434
- if (idx < 0 || idx >= Length)
435
- {
436
- printf("vec index %d out of bounds at %s %d\n", idx, __FILE__, __LINE__);
437
- assert(0);
438
- }
439
- #endif
440
-
441
- return &v[idx];
442
- }
443
-
444
- template<unsigned Length, typename Type>
445
- inline CUDA_CALLABLE Type* indexref(vec_t<Length, Type>* v, int idx)
446
- {
447
- #ifndef NDEBUG
448
- if (idx < 0 || idx >= Length)
449
- {
450
- printf("vec store %d out of bounds at %s %d\n", idx, __FILE__, __LINE__);
451
- assert(0);
452
- }
453
- #endif
454
-
455
- return &((*v)[idx]);
456
- }
457
-
458
- template<unsigned Length, typename Type>
459
- inline CUDA_CALLABLE void adj_index(vec_t<Length, Type>& v, int idx,
460
- vec_t<Length, Type>& adj_v, int adj_idx, const Type& adj_value)
461
- {
462
- // nop
463
- }
464
-
465
-
466
- template<unsigned Length, typename Type>
467
- inline CUDA_CALLABLE void adj_indexref(vec_t<Length, Type>* v, int idx,
468
- vec_t<Length, Type>& adj_v, int adj_idx, const Type& adj_value)
469
- {
470
- // nop
471
- }
472
-
473
-
474
- template<unsigned Length, typename Type>
475
- inline CUDA_CALLABLE Type length(vec_t<Length, Type> a)
476
- {
477
- return sqrt(dot(a, a));
478
- }
479
-
480
- template<unsigned Length, typename Type>
481
- inline CUDA_CALLABLE Type length_sq(vec_t<Length, Type> a)
482
- {
483
- return dot(a, a);
484
- }
485
-
486
-
487
- template<typename Type>
488
- inline CUDA_CALLABLE Type length(vec_t<2, Type> a)
489
- {
490
- return sqrt(a.c[0] * a.c[0] + a.c[1] * a.c[1]);
491
- }
492
-
493
- template<typename Type>
494
- inline CUDA_CALLABLE Type length(vec_t<3, Type> a)
495
- {
496
- return sqrt(a.c[0] * a.c[0] + a.c[1] * a.c[1] + a.c[2] * a.c[2]);
497
- }
498
-
499
- template<unsigned Length, typename Type>
500
- inline CUDA_CALLABLE vec_t<Length, Type> normalize(vec_t<Length, Type> a)
501
- {
502
- Type l = length(a);
503
- if (l > Type(kEps))
504
- return div(a,l);
505
- else
506
- return vec_t<Length, Type>();
507
- }
508
-
509
- template<typename Type>
510
- inline CUDA_CALLABLE vec_t<2, Type> normalize(vec_t<2, Type> a)
511
- {
512
- Type l = sqrt(a.c[0] * a.c[0] + a.c[1] * a.c[1]);
513
- if (l > Type(kEps))
514
- return vec_t<2, Type>(a.c[0]/l,a.c[1]/l);
515
- else
516
- return vec_t<2, Type>();
517
- }
518
-
519
- template<typename Type>
520
- inline CUDA_CALLABLE vec_t<3, Type> normalize(vec_t<3, Type> a)
521
- {
522
- Type l = sqrt(a.c[0] * a.c[0] + a.c[1] * a.c[1] + a.c[2] * a.c[2]);
523
- if (l > Type(kEps))
524
- return vec_t<3, Type>(a.c[0]/l,a.c[1]/l,a.c[2]/l);
525
- else
526
- return vec_t<3, Type>();
527
- }
528
-
529
-
530
- template<typename Type>
531
- inline CUDA_CALLABLE vec_t<3,Type> cross(vec_t<3,Type> a, vec_t<3,Type> b)
532
- {
533
- return {
534
- Type(a[1]*b[2] - a[2]*b[1]),
535
- Type(a[2]*b[0] - a[0]*b[2]),
536
- Type(a[0]*b[1] - a[1]*b[0])
537
- };
538
- }
539
-
540
-
541
- template<unsigned Length, typename Type>
542
- inline bool CUDA_CALLABLE isfinite(vec_t<Length, Type> x)
543
- {
544
- for( unsigned i=0; i < Length; ++i )
545
- {
546
- if(!isfinite(x[i]))
547
- {
548
- return false;
549
- }
550
- }
551
- return true;
552
- }
553
-
554
- // These two functions seem to compile very slowly
555
- template<unsigned Length, typename Type>
556
- inline CUDA_CALLABLE vec_t<Length,Type> min(vec_t<Length,Type> a, vec_t<Length,Type> b)
557
- {
558
- vec_t<Length,Type> ret;
559
- for( unsigned i=0; i < Length; ++i )
560
- {
561
- ret[i] = a[i] < b[i] ? a[i] : b[i];
562
- }
563
- return ret;
564
- }
565
-
566
- template<unsigned Length, typename Type>
567
- inline CUDA_CALLABLE vec_t<Length,Type> max(vec_t<Length,Type> a, vec_t<Length,Type> b)
568
- {
569
- vec_t<Length,Type> ret;
570
- for( unsigned i=0; i < Length; ++i )
571
- {
572
- ret[i] = a[i] > b[i] ? a[i] : b[i];
573
- }
574
- return ret;
575
- }
576
-
577
- template<unsigned Length, typename Type>
578
- inline CUDA_CALLABLE Type min(vec_t<Length,Type> v)
579
- {
580
- Type ret = v[0];
581
- for( unsigned i=1; i < Length; ++i )
582
- {
583
- if (v[i] < ret)
584
- ret = v[i];
585
- }
586
- return ret;
587
- }
588
-
589
- template<unsigned Length, typename Type>
590
- inline CUDA_CALLABLE Type max(vec_t<Length,Type> v)
591
- {
592
- Type ret = v[0];
593
- for( unsigned i=1; i < Length; ++i )
594
- {
595
- if (v[i] > ret)
596
- ret = v[i];
597
- }
598
- return ret;
599
- }
600
-
601
- template<unsigned Length, typename Type>
602
- inline CUDA_CALLABLE unsigned argmin(vec_t<Length,Type> v)
603
- {
604
- unsigned ret = 0;
605
- for( unsigned i=1; i < Length; ++i )
606
- {
607
- if (v[i] < v[ret])
608
- ret = i;
609
- }
610
- return ret;
611
- }
612
-
613
- template<unsigned Length, typename Type>
614
- inline CUDA_CALLABLE unsigned argmax(vec_t<Length,Type> v)
615
- {
616
- unsigned ret = 0;
617
- for( unsigned i=1; i < Length; ++i )
618
- {
619
- if (v[i] > v[ret])
620
- ret = i;
621
- }
622
- return ret;
623
- }
624
-
625
- template<unsigned Length, typename Type>
626
- inline CUDA_CALLABLE void expect_near(const vec_t<Length, Type>& actual, const vec_t<Length, Type>& expected, const Type& tolerance)
627
- {
628
- const Type diff(0);
629
- for(size_t i=0; i<Length; ++i)
630
- {
631
- diff = max(diff,abs(actual[i] - expected[i]));
632
- }
633
- if (diff > tolerance)
634
- {
635
- printf("Error, expect_near() failed with tolerance "); print(tolerance);
636
- printf("\t Expected: "); print(expected);
637
- printf("\t Actual: "); print(actual);
638
- }
639
- }
640
-
641
- template<unsigned Length, typename Type>
642
- inline CUDA_CALLABLE void adj_expect_near(const vec_t<Length, Type>& actual, const vec_t<Length, Type>& expected, Type tolerance, vec_t<Length, Type>& adj_actual, vec_t<Length, Type>& adj_expected, Type adj_tolerance)
643
- {
644
- // nop
645
- }
646
-
647
- // adjoint for the initializer_array constructor:
648
- template<unsigned Length, typename Type>
649
- inline CUDA_CALLABLE void adj_vec_t(const initializer_array<Length, Type> &cmps, const initializer_array<Length, Type*> &adj_cmps, const vec_t<Length, Type>& adj_ret)
650
- {
651
- for(unsigned i=0; i < Length; ++i)
652
- {
653
- *(adj_cmps[i]) += adj_ret[i];
654
- }
655
- }
656
-
657
-
658
- // adjoint for the component constructors:
659
- template<typename Type>
660
- inline CUDA_CALLABLE void adj_vec_t(Type cmpx, Type cmpy, Type &adj_cmpx, Type &adj_cmpy, const vec_t<2, Type>& adj_ret)
661
- {
662
- adj_cmpx += adj_ret.c[0];
663
- adj_cmpy += adj_ret.c[1];
664
- }
665
-
666
- template<typename Type>
667
- inline CUDA_CALLABLE void adj_vec_t(Type cmpx, Type cmpy, Type cmpz, Type &adj_cmpx, Type &adj_cmpy, Type &adj_cmpz, const vec_t<3, Type>& adj_ret)
668
- {
669
- adj_cmpx += adj_ret.c[0];
670
- adj_cmpy += adj_ret.c[1];
671
- adj_cmpz += adj_ret.c[2];
672
- }
673
-
674
- template<typename Type>
675
- inline CUDA_CALLABLE void adj_vec_t(Type cmpx, Type cmpy, Type cmpz, Type cmpw, Type &adj_cmpx, Type &adj_cmpy, Type &adj_cmpz, Type &adj_cmpw, const vec_t<4, Type>& adj_ret)
676
- {
677
- adj_cmpx += adj_ret.c[0];
678
- adj_cmpy += adj_ret.c[1];
679
- adj_cmpz += adj_ret.c[2];
680
- adj_cmpw += adj_ret.c[3];
681
- }
682
-
683
- // adjoint for the constant constructor:
684
- template<unsigned Length, typename Type>
685
- inline CUDA_CALLABLE void adj_vec_t(Type s, Type& adj_s, const vec_t<Length, Type>& adj_ret)
686
- {
687
- for( unsigned i=0; i < Length; ++i )
688
- {
689
- adj_s += adj_ret[i];
690
- }
691
- }
692
-
693
- // adjoint for the casting constructor
694
- template<unsigned Length, typename Type, typename OtherType>
695
- inline CUDA_CALLABLE void adj_vec_t(const vec_t<Length, OtherType>& other, vec_t<Length, OtherType>& adj_other, const vec_t<Length, Type>& adj_ret)
696
- {
697
- for( unsigned i=0; i < Length; ++i )
698
- {
699
- adj_other[i] += static_cast<OtherType>(adj_ret[i]);
700
- }
701
- }
702
-
703
- template<typename Type>
704
- CUDA_CALLABLE inline void adj_vec_t(const vec_t<3,Type>& w, const vec_t<3,Type>& v, vec_t<3,Type>& adj_w, vec_t<3,Type>& adj_v, const vec_t<6,Type>& adj_ret)
705
- {
706
- adj_w[0] += adj_ret[0];
707
- adj_w[1] += adj_ret[1];
708
- adj_w[2] += adj_ret[2];
709
- adj_v[0] += adj_ret[3];
710
- adj_v[1] += adj_ret[4];
711
- adj_v[2] += adj_ret[5];
712
- }
713
-
714
- template<unsigned Length, typename Type>
715
- inline CUDA_CALLABLE void adj_mul(vec_t<Length, Type> a, Type s, vec_t<Length, Type>& adj_a, Type& adj_s, const vec_t<Length, Type>& adj_ret)
716
- {
717
- for( unsigned i=0; i < Length; ++i )
718
- {
719
- adj_a[i] += s*adj_ret[i];
720
- }
721
-
722
- adj_s += dot(a, adj_ret);
723
-
724
- #if FP_CHECK
725
- if (!isfinite(a) || !isfinite(s) || !isfinite(adj_a) || !isfinite(adj_s) || !isfinite(adj_ret))
726
- {
727
- // \TODO: How shall we implement this error message?
728
- //printf("adj_mul((%f %f %f %f), %f, (%f %f %f %f), %f, (%f %f %f %f)\n", a.x, a.y, a.z, a.w, s, adj_a.x, adj_a.y, adj_a.z, adj_a.w, adj_s, adj_ret.x, adj_ret.y, adj_ret.z, adj_ret.w);
729
- assert(0);
730
- }
731
- #endif
732
- }
733
-
734
- template<unsigned Length, typename Type>
735
- inline CUDA_CALLABLE void adj_mul(Type s, vec_t<Length, Type> a, Type& adj_s, vec_t<Length, Type>& adj_a, const vec_t<Length, Type>& adj_ret)
736
- {
737
- adj_mul(a, s, adj_a, adj_s, adj_ret);
738
- }
739
-
740
- template<unsigned Length, typename Type>
741
- inline CUDA_CALLABLE void adj_cw_mul(vec_t<Length, Type> a, vec_t<Length, Type> b, vec_t<Length, Type>& adj_a, vec_t<Length, Type>& adj_b, const vec_t<Length, Type>& adj_ret)
742
- {
743
- adj_a += cw_mul(b, adj_ret);
744
- adj_b += cw_mul(a, adj_ret);
745
- }
746
-
747
- template<unsigned Length, typename Type>
748
- inline CUDA_CALLABLE void adj_div(vec_t<Length, Type> a, Type s, vec_t<Length, Type>& adj_a, Type& adj_s, const vec_t<Length, Type>& adj_ret)
749
- {
750
-
751
- adj_s -= dot(a , adj_ret)/ (s * s); // - a / s^2
752
-
753
- for( unsigned i=0; i < Length; ++i )
754
- {
755
- adj_a[i] += adj_ret[i] / s;
756
- }
757
-
758
- #if FP_CHECK
759
- if (!isfinite(a) || !isfinite(s) || !isfinite(adj_a) || !isfinite(adj_s) || !isfinite(adj_ret))
760
- {
761
- // \TODO: How shall we implement this error message?
762
- // printf("adj_div((%f %f %f %f), %f, (%f %f %f %f), %f, (%f %f %f %f)\n", a.x, a.y, a.z, a.w, s, adj_a.x, adj_a.y, adj_a.z, adj_a.w, adj_s, adj_ret.x, adj_ret.y, adj_ret.z, adj_ret.w);
763
- assert(0);
764
- }
765
- #endif
766
- }
767
-
768
- template<unsigned Length, typename Type>
769
- inline CUDA_CALLABLE void adj_div(Type s, vec_t<Length, Type> a, Type& adj_s, vec_t<Length, Type>& adj_a, const vec_t<Length, Type>& adj_ret)
770
- {
771
-
772
- adj_s -= dot(a , adj_ret)/ (s * s); // - a / s^2
773
-
774
- for( unsigned i=0; i < Length; ++i )
775
- {
776
- adj_a[i] += s / adj_ret[i];
777
- }
778
-
779
- #if FP_CHECK
780
- if (!isfinite(a) || !isfinite(s) || !isfinite(adj_a) || !isfinite(adj_s) || !isfinite(adj_ret))
781
- {
782
- // \TODO: How shall we implement this error message?
783
- // printf("adj_div((%f %f %f %f), %f, (%f %f %f %f), %f, (%f %f %f %f)\n", a.x, a.y, a.z, a.w, s, adj_a.x, adj_a.y, adj_a.z, adj_a.w, adj_s, adj_ret.x, adj_ret.y, adj_ret.z, adj_ret.w);
784
- assert(0);
785
- }
786
- #endif
787
- }
788
-
789
- template<unsigned Length, typename Type>
790
- inline CUDA_CALLABLE void adj_cw_div(vec_t<Length, Type> a, vec_t<Length, Type> b, vec_t<Length, Type>& ret, vec_t<Length, Type>& adj_a, vec_t<Length, Type>& adj_b, const vec_t<Length, Type>& adj_ret) {
791
- adj_a += cw_div(adj_ret, b);
792
- adj_b -= cw_mul(adj_ret, cw_div(ret, b));
793
- }
794
-
795
- template<unsigned Length, typename Type>
796
- inline CUDA_CALLABLE void adj_add(vec_t<Length, Type> a, vec_t<Length, Type> b, vec_t<Length, Type>& adj_a, vec_t<Length, Type>& adj_b, const vec_t<Length, Type>& adj_ret)
797
- {
798
- adj_a += adj_ret;
799
- adj_b += adj_ret;
800
- }
801
-
802
- template<typename Type>
803
- inline CUDA_CALLABLE void adj_add(vec_t<2, Type> a, vec_t<2, Type> b, vec_t<2, Type>& adj_a, vec_t<2, Type>& adj_b, const vec_t<2, Type>& adj_ret)
804
- {
805
- adj_a.c[0] += adj_ret.c[0];
806
- adj_a.c[1] += adj_ret.c[1];
807
- adj_b.c[0] += adj_ret.c[0];
808
- adj_b.c[1] += adj_ret.c[1];
809
- }
810
-
811
- template<typename Type>
812
- inline CUDA_CALLABLE void adj_add(vec_t<3, Type> a, vec_t<3, Type> b, vec_t<3, Type>& adj_a, vec_t<3, Type>& adj_b, const vec_t<3, Type>& adj_ret)
813
- {
814
- adj_a.c[0] += adj_ret.c[0];
815
- adj_a.c[1] += adj_ret.c[1];
816
- adj_a.c[2] += adj_ret.c[2];
817
- adj_b.c[0] += adj_ret.c[0];
818
- adj_b.c[1] += adj_ret.c[1];
819
- adj_b.c[2] += adj_ret.c[2];
820
- }
821
-
822
- template<unsigned Length, typename Type>
823
- inline CUDA_CALLABLE void adj_sub(vec_t<Length, Type> a, vec_t<Length, Type> b, vec_t<Length, Type>& adj_a, vec_t<Length, Type>& adj_b, const vec_t<Length, Type>& adj_ret)
824
- {
825
- adj_a += adj_ret;
826
- adj_b -= adj_ret;
827
- }
828
-
829
- template<typename Type>
830
- inline CUDA_CALLABLE void adj_sub(vec_t<2, Type> a, vec_t<2, Type> b, vec_t<2, Type>& adj_a, vec_t<2, Type>& adj_b, const vec_t<2, Type>& adj_ret)
831
- {
832
- adj_a.c[0] += adj_ret.c[0];
833
- adj_a.c[1] += adj_ret.c[1];
834
- adj_b.c[0] -= adj_ret.c[0];
835
- adj_b.c[1] -= adj_ret.c[1];
836
- }
837
-
838
- template<typename Type>
839
- inline CUDA_CALLABLE void adj_sub(vec_t<3, Type> a, vec_t<3, Type> b, vec_t<3, Type>& adj_a, vec_t<3, Type>& adj_b, const vec_t<3, Type>& adj_ret)
840
- {
841
- adj_a.c[0] += adj_ret.c[0];
842
- adj_a.c[1] += adj_ret.c[1];
843
- adj_a.c[2] += adj_ret.c[2];
844
- adj_b.c[0] -= adj_ret.c[0];
845
- adj_b.c[1] -= adj_ret.c[1];
846
- adj_b.c[2] -= adj_ret.c[2];
847
- }
848
-
849
- template<unsigned Length, typename Type>
850
- inline CUDA_CALLABLE void adj_dot(vec_t<Length, Type> a, vec_t<Length, Type> b, vec_t<Length, Type>& adj_a, vec_t<Length, Type>& adj_b, const Type adj_ret)
851
- {
852
- adj_a += b*adj_ret;
853
- adj_b += a*adj_ret;
854
-
855
- #if FP_CHECK
856
- if (!isfinite(a) || !isfinite(b) || !isfinite(adj_a) || !isfinite(adj_b) || !isfinite(adj_ret))
857
- {
858
- // \TODO: How shall we implement this error message?
859
- //printf("adj_dot((%f %f %f %f), (%f %f %f %f), (%f %f %f %f), (%f %f %f %f), %f)\n", a.x, a.y, a.z, a.w, b.x, b.y, b.z, b.w, adj_a.x, adj_a.y, adj_a.z, adj_a.w, adj_b.x, adj_b.y, adj_b.z, adj_b.w, adj_ret);
860
- assert(0);
861
- }
862
- #endif
863
- }
864
-
865
-
866
-
867
- template<typename Type>
868
- inline CUDA_CALLABLE void adj_dot(vec_t<2, Type> a, vec_t<2, Type> b, vec_t<2, Type>& adj_a, vec_t<2, Type>& adj_b, const Type adj_ret)
869
- {
870
- adj_a.c[0] += b.c[0]*adj_ret;
871
- adj_a.c[1] += b.c[1]*adj_ret;
872
-
873
- adj_b.c[0] += a.c[0]*adj_ret;
874
- adj_b.c[1] += a.c[1]*adj_ret;
875
- }
876
-
877
- template<typename Type>
878
- inline CUDA_CALLABLE void adj_dot(vec_t<3, Type> a, vec_t<3, Type> b, vec_t<3, Type>& adj_a, vec_t<3, Type>& adj_b, const Type adj_ret)
879
- {
880
- adj_a.c[0] += b.c[0]*adj_ret;
881
- adj_a.c[1] += b.c[1]*adj_ret;
882
- adj_a.c[2] += b.c[2]*adj_ret;
883
-
884
- adj_b.c[0] += a.c[0]*adj_ret;
885
- adj_b.c[1] += a.c[1]*adj_ret;
886
- adj_b.c[2] += a.c[2]*adj_ret;
887
- }
888
-
889
-
890
- template<unsigned Length, typename Type>
891
- inline CUDA_CALLABLE void adj_extract(const vec_t<Length, Type> & a, int idx, vec_t<Length, Type> & adj_a, int & adj_idx, Type & adj_ret)
892
- {
893
- #ifndef NDEBUG
894
- if (idx < 0 || idx > Length)
895
- {
896
- printf("Tvec2<Scalar> index %d out of bounds at %s %d\n", idx, __FILE__, __LINE__);
897
- assert(0);
898
- }
899
- #endif
900
-
901
- adj_a[idx] += adj_ret;
902
- }
903
-
904
- template<unsigned Length, typename Type>
905
- inline CUDA_CALLABLE void adj_length(vec_t<Length, Type> a, Type ret, vec_t<Length, Type>& adj_a, const Type adj_ret)
906
- {
907
- if (ret > Type(kEps))
908
- {
909
- adj_a += div(a, ret) * adj_ret;
910
- }
911
-
912
- #if FP_CHECK
913
- if (!isfinite(adj_a))
914
- {
915
- // \TODO: How shall we implement this error message?
916
- //printf("%s:%d - adj_length((%f %f %f %f), (%f %f %f %f), (%f))\n", __FILE__, __LINE__, a.x, a.y, a.z, a.w, adj_a.x, adj_a.y, adj_a.z, adj_a.w, adj_ret);
917
- assert(0);
918
- }
919
- #endif
920
- }
921
-
922
- template<unsigned Length, typename Type>
923
- inline CUDA_CALLABLE void adj_length_sq(vec_t<Length, Type> a, vec_t<Length, Type>& adj_a, const Type adj_ret)
924
- {
925
- adj_a += Type(2.0)*a*adj_ret;
926
-
927
- #if FP_CHECK
928
- if (!isfinite(adj_a))
929
- {
930
- // \TODO: How shall we implement this error message?
931
- //printf("%s:%d - adj_length((%f %f %f %f), (%f %f %f %f), (%f))\n", __FILE__, __LINE__, a.x, a.y, a.z, a.w, adj_a.x, adj_a.y, adj_a.z, adj_a.w, adj_ret);
932
- assert(0);
933
- }
934
- #endif
935
- }
936
-
937
- template<unsigned Length, typename Type>
938
- inline CUDA_CALLABLE void adj_normalize(vec_t<Length, Type> a, vec_t<Length, Type>& ret, vec_t<Length, Type>& adj_a, const vec_t<Length, Type>& adj_ret)
939
- {
940
- Type d = length(a);
941
-
942
- if (d > Type(kEps))
943
- {
944
- Type invd = Type(1.0f)/d;
945
-
946
- adj_a += (adj_ret*invd - ret*(dot(ret, adj_ret))*invd);
947
-
948
- #if FP_CHECK
949
- if (!isfinite(adj_a))
950
- {
951
- // \TODO: How shall we implement this error message?
952
- //printf("%s:%d - adj_normalize((%f %f %f %f), (%f %f %f %f), (%f, %f, %f, %f))\n", __FILE__, __LINE__, a.x, a.y, a.z, a.w, adj_a.x, adj_a.y, adj_a.z, adj_a.w, adj_ret.x, adj_ret.y, adj_ret.z, adj_ret.w);
953
- assert(0);
954
- }
955
- #endif
956
- }
957
- }
958
-
959
- template<typename Type>
960
- inline CUDA_CALLABLE void adj_cross(vec_t<3,Type> a, vec_t<3,Type> b, vec_t<3,Type>& adj_a, vec_t<3,Type>& adj_b, const vec_t<3,Type>& adj_ret)
961
- {
962
- // todo: sign check
963
- adj_a += cross(b, adj_ret);
964
- adj_b -= cross(a, adj_ret);
965
- }
966
-
967
- template<unsigned Length, typename Type>
968
- inline CUDA_CALLABLE void adj_min(const vec_t<Length,Type> &a, const vec_t<Length,Type> &b, vec_t<Length,Type>& adj_a, vec_t<Length,Type>& adj_b, const vec_t<Length,Type> &adj_ret)
969
- {
970
- for( unsigned i=0; i < Length; ++i )
971
- {
972
- if (a[i] < b[i])
973
- adj_a[i] += adj_ret[i];
974
- else
975
- adj_b[i] += adj_ret[i];
976
- }
977
- }
978
-
979
- template<unsigned Length, typename Type>
980
- inline CUDA_CALLABLE void adj_max(const vec_t<Length,Type> &a, const vec_t<Length,Type> &b, vec_t<Length,Type>& adj_a, vec_t<Length,Type>& adj_b, const vec_t<Length,Type> &adj_ret)
981
- {
982
- for( unsigned i=0; i < Length; ++i )
983
- {
984
- if (a[i] > b[i])
985
- adj_a[i] += adj_ret[i];
986
- else
987
- adj_b[i] += adj_ret[i];
988
- }
989
- }
990
-
991
- template<unsigned Length, typename Type>
992
- inline CUDA_CALLABLE void adj_min(const vec_t<Length,Type> &v, vec_t<Length,Type>& adj_v, const Type &adj_ret)
993
- {
994
- unsigned i = argmin(v);
995
- adj_v[i] += adj_ret;
996
- }
997
-
998
- template<unsigned Length, typename Type>
999
- inline CUDA_CALLABLE void adj_max(const vec_t<Length,Type> &v, vec_t<Length,Type>& adj_v, const Type &adj_ret)
1000
- {
1001
- unsigned i = argmax(v);
1002
- adj_v[i] += adj_ret;
1003
- }
1004
-
1005
- // Do I need to specialize these for different lengths?
1006
- template<unsigned Length, typename Type>
1007
- inline CUDA_CALLABLE vec_t<Length, Type> atomic_add(vec_t<Length, Type> * addr, vec_t<Length, Type> value)
1008
- {
1009
- vec_t<Length, Type> ret;
1010
- for( unsigned i=0; i < Length; ++i )
1011
- {
1012
- ret[i] = atomic_add(&(addr -> c[i]), value[i]);
1013
- }
1014
-
1015
- return ret;
1016
- }
1017
-
1018
- template<unsigned Length, typename Type>
1019
- inline CUDA_CALLABLE vec_t<Length, Type> atomic_min(vec_t<Length, Type> * addr, vec_t<Length, Type> value)
1020
- {
1021
- vec_t<Length, Type> ret;
1022
- for( unsigned i=0; i < Length; ++i )
1023
- {
1024
- ret[i] = atomic_min(&(addr -> c[i]), value[i]);
1025
- }
1026
-
1027
- return ret;
1028
- }
1029
-
1030
- template<unsigned Length, typename Type>
1031
- inline CUDA_CALLABLE vec_t<Length, Type> atomic_max(vec_t<Length, Type> * addr, vec_t<Length, Type> value)
1032
- {
1033
- vec_t<Length, Type> ret;
1034
- for( unsigned i=0; i < Length; ++i )
1035
- {
1036
- ret[i] = atomic_max(&(addr -> c[i]), value[i]);
1037
- }
1038
-
1039
- return ret;
1040
- }
1041
-
1042
- template<unsigned Length, typename Type>
1043
- inline CUDA_CALLABLE void adj_atomic_minmax(
1044
- vec_t<Length,Type> *addr,
1045
- vec_t<Length,Type> *adj_addr,
1046
- const vec_t<Length,Type> &value,
1047
- vec_t<Length,Type> &adj_value)
1048
- {
1049
- for (unsigned i=0; i < Length; ++i)
1050
- adj_atomic_minmax(&(addr->c[i]), &(adj_addr->c[i]), value[i], adj_value[i]);
1051
- }
1052
-
1053
- // ok, the original implementation of this didn't take the absolute values.
1054
- // I wouldn't consider this expected behavior. It looks like it's only
1055
- // being used for bounding boxes at the moment, where this doesn't matter,
1056
- // but you often use it for ray tracing where it does. Not sure if the
1057
- // fabs() incurs a performance hit...
1058
- template<unsigned Length, typename Type>
1059
- CUDA_CALLABLE inline int longest_axis(const vec_t<Length, Type>& v)
1060
- {
1061
- Type lmax = abs(v[0]);
1062
- int ret(0);
1063
- for( unsigned i=1; i < Length; ++i )
1064
- {
1065
- Type l = abs(v[i]);
1066
- if( l > lmax )
1067
- {
1068
- ret = i;
1069
- lmax = l;
1070
- }
1071
- }
1072
- return ret;
1073
- }
1074
-
1075
- template<unsigned Length, typename Type>
1076
- CUDA_CALLABLE inline vec_t<Length,Type> lerp(const vec_t<Length,Type>& a, const vec_t<Length,Type>& b, Type t)
1077
- {
1078
- return a*(Type(1)-t) + b*t;
1079
- }
1080
-
1081
- template<unsigned Length, typename Type>
1082
- CUDA_CALLABLE inline void adj_lerp(const vec_t<Length,Type>& a, const vec_t<Length,Type>& b, Type t, vec_t<Length,Type>& adj_a, vec_t<Length,Type>& adj_b, Type& adj_t, const vec_t<Length,Type>& adj_ret)
1083
- {
1084
- adj_a += adj_ret*(Type(1)-t);
1085
- adj_b += adj_ret*t;
1086
- adj_t += tensordot(b, adj_ret) - tensordot(a, adj_ret);
1087
- }
1088
-
1089
- // for integral types we do not accumulate gradients
1090
- template<unsigned Length> CUDA_CALLABLE inline void adj_atomic_add(vec_t<Length, int8>* buf, const vec_t<Length, int8> &value) { }
1091
- template<unsigned Length> CUDA_CALLABLE inline void adj_atomic_add(vec_t<Length, uint8>* buf, const vec_t<Length, uint8> &value) { }
1092
- template<unsigned Length> CUDA_CALLABLE inline void adj_atomic_add(vec_t<Length, int16>* buf, const vec_t<Length, int16> &value) { }
1093
- template<unsigned Length> CUDA_CALLABLE inline void adj_atomic_add(vec_t<Length, uint16>* buf, const vec_t<Length, uint16> &value) { }
1094
- template<unsigned Length> CUDA_CALLABLE inline void adj_atomic_add(vec_t<Length, int32>* buf, const vec_t<Length, int32> &value) { }
1095
- template<unsigned Length> CUDA_CALLABLE inline void adj_atomic_add(vec_t<Length, uint32>* buf, const vec_t<Length, uint32> &value) { }
1096
- template<unsigned Length> CUDA_CALLABLE inline void adj_atomic_add(vec_t<Length, int64>* buf, const vec_t<Length, int64> &value) { }
1097
- template<unsigned Length> CUDA_CALLABLE inline void adj_atomic_add(vec_t<Length, uint64>* buf, const vec_t<Length, uint64> &value) { }
1098
-
1099
-
1100
- // adjoints for some of the constructors, used in intersect.h
1101
- inline CUDA_CALLABLE void adj_vec2(float x, float y, float& adj_x, float& adj_y, const vec2& adj_ret)
1102
- {
1103
- adj_x += adj_ret[0];
1104
- adj_y += adj_ret[1];
1105
- }
1106
-
1107
- inline CUDA_CALLABLE void adj_vec3(float x, float y, float z, float& adj_x, float& adj_y, float& adj_z, const vec3& adj_ret)
1108
- {
1109
- adj_x += adj_ret[0];
1110
- adj_y += adj_ret[1];
1111
- adj_z += adj_ret[2];
1112
- }
1113
-
1114
- inline CUDA_CALLABLE void adj_vec4(float x, float y, float z, float w, float& adj_x, float& adj_y, float& adj_z, float& adj_w, const vec4& adj_ret)
1115
- {
1116
- adj_x += adj_ret[0];
1117
- adj_y += adj_ret[1];
1118
- adj_z += adj_ret[2];
1119
- adj_w += adj_ret[3];
1120
- }
1121
-
1122
- inline CUDA_CALLABLE void adj_vec3(float s, float& adj_s, const vec3& adj_ret)
1123
- {
1124
- adj_vec_t(s, adj_s, adj_ret);
1125
- }
1126
-
1127
- inline CUDA_CALLABLE void adj_vec4(float s, float& adj_s, const vec4& adj_ret)
1128
- {
1129
- adj_vec_t(s, adj_s, adj_ret);
1130
- }
1131
-
1132
-
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
+
9
+ #pragma once
10
+
11
+ #include "initializer_array.h"
12
+
13
+ namespace wp
14
+ {
15
+
16
+ template<unsigned Length, typename Type>
17
+ struct vec_t
18
+ {
19
+ Type c[Length];
20
+
21
+ inline CUDA_CALLABLE vec_t()
22
+ : c()
23
+ {}
24
+
25
+ inline CUDA_CALLABLE vec_t(Type s)
26
+ {
27
+ for( unsigned i=0; i < Length; ++i )
28
+ {
29
+ c[i] = s;
30
+ }
31
+ }
32
+
33
+ template <typename OtherType>
34
+ inline explicit CUDA_CALLABLE vec_t(const vec_t<Length, OtherType>& other)
35
+ {
36
+ for( unsigned i=0; i < Length; ++i )
37
+ {
38
+ c[i] = static_cast<Type>(other[i]);
39
+ }
40
+ }
41
+
42
+ inline CUDA_CALLABLE vec_t(Type x, Type y)
43
+ {
44
+ assert(Length == 2);
45
+ c[0]=x;
46
+ c[1]=y;
47
+ }
48
+
49
+ inline CUDA_CALLABLE vec_t(Type x, Type y, Type z)
50
+ {
51
+ assert(Length == 3);
52
+ c[0]=x;
53
+ c[1]=y;
54
+ c[2]=z;
55
+ }
56
+
57
+
58
+ inline CUDA_CALLABLE vec_t(Type x, Type y, Type z, Type w)
59
+ {
60
+ assert(Length == 4);
61
+ c[0]=x;
62
+ c[1]=y;
63
+ c[2]=z;
64
+ c[3]=w;
65
+ }
66
+
67
+ inline CUDA_CALLABLE vec_t(const initializer_array<Length, Type> &l)
68
+ {
69
+ for( unsigned i=0; i < Length; ++i )
70
+ {
71
+ c[i] = l[i];
72
+ }
73
+ }
74
+
75
+ // special screw vector constructor for spatial_vectors:
76
+ inline CUDA_CALLABLE vec_t(vec_t<3,Type> w, vec_t<3,Type> v)
77
+ {
78
+ c[0] = w[0];
79
+ c[1] = w[1];
80
+ c[2] = w[2];
81
+ c[3] = v[0];
82
+ c[4] = v[1];
83
+ c[5] = v[2];
84
+ }
85
+
86
+ inline CUDA_CALLABLE Type operator[](int index) const
87
+ {
88
+ assert(index < Length);
89
+ return c[index];
90
+ }
91
+
92
+ inline CUDA_CALLABLE Type& operator[](int index)
93
+ {
94
+ assert(index < Length);
95
+ return c[index];
96
+ }
97
+ };
98
+
99
+ using vec2b = vec_t<2,int8>;
100
+ using vec3b = vec_t<3,int8>;
101
+ using vec4b = vec_t<4,int8>;
102
+ using vec2ub = vec_t<2,uint8>;
103
+ using vec3ub = vec_t<3,uint8>;
104
+ using vec4ub = vec_t<4,uint8>;
105
+
106
+ using vec2s = vec_t<2,int16>;
107
+ using vec3s = vec_t<3,int16>;
108
+ using vec4s = vec_t<4,int16>;
109
+ using vec2us = vec_t<2,uint16>;
110
+ using vec3us = vec_t<3,uint16>;
111
+ using vec4us = vec_t<4,uint16>;
112
+
113
+ using vec2i = vec_t<2,int32>;
114
+ using vec3i = vec_t<3,int32>;
115
+ using vec4i = vec_t<4,int32>;
116
+ using vec2ui = vec_t<2,uint32>;
117
+ using vec3ui = vec_t<3,uint32>;
118
+ using vec4ui = vec_t<4,uint32>;
119
+
120
+ using vec2l = vec_t<2,int64>;
121
+ using vec3l = vec_t<3,int64>;
122
+ using vec4l = vec_t<4,int64>;
123
+ using vec2ul = vec_t<2,uint64>;
124
+ using vec3ul = vec_t<3,uint64>;
125
+ using vec4ul = vec_t<4,uint64>;
126
+
127
+ using vec2h = vec_t<2,half>;
128
+ using vec3h = vec_t<3,half>;
129
+ using vec4h = vec_t<4,half>;
130
+
131
+ using vec2 = vec_t<2,float>;
132
+ using vec3 = vec_t<3,float>;
133
+ using vec4 = vec_t<4,float>;
134
+
135
+ using vec2f = vec_t<2,float>;
136
+ using vec3f = vec_t<3,float>;
137
+ using vec4f = vec_t<4,float>;
138
+
139
+ using vec2d = vec_t<2,double>;
140
+ using vec3d = vec_t<3,double>;
141
+ using vec4d = vec_t<4,double>;
142
+
143
+ //--------------
144
+ // vec<Length, Type> methods
145
+
146
+ // Should these accept const references as arguments? It's all
147
+ // inlined so maybe it doesn't matter? Even if it does, it
148
+ // probably depends on the Length of the vector...
149
+
150
+ // negation:
151
+ template<unsigned Length, typename Type>
152
+ inline CUDA_CALLABLE vec_t<Length, Type> operator - (vec_t<Length, Type> a)
153
+ {
154
+ // NB: this constructor will initialize all ret's components to 0, which is
155
+ // unnecessary...
156
+ vec_t<Length, Type> ret;
157
+ for( unsigned i=0; i < Length; ++i )
158
+ {
159
+ ret[i] = -a[i];
160
+ }
161
+
162
+ // Wonder if this does a load of copying when it returns... hopefully not as it's inlined?
163
+ return ret;
164
+ }
165
+
166
+ template<unsigned Length, typename Type>
167
+ CUDA_CALLABLE inline vec_t<Length, Type> pos(const vec_t<Length, Type>& x)
168
+ {
169
+ return x;
170
+ }
171
+
172
+ template<unsigned Length, typename Type>
173
+ CUDA_CALLABLE inline vec_t<Length, Type> neg(const vec_t<Length, Type>& x)
174
+ {
175
+ return -x;
176
+ }
177
+
178
+ template<typename Type>
179
+ CUDA_CALLABLE inline vec_t<3, Type> neg(const vec_t<3, Type>& x)
180
+ {
181
+ return vec_t<3, Type>(-x.c[0], -x.c[1], -x.c[2]);
182
+ }
183
+
184
+ template<typename Type>
185
+ CUDA_CALLABLE inline vec_t<2, Type> neg(const vec_t<2, Type>& x)
186
+ {
187
+ return vec_t<2, Type>(-x.c[0], -x.c[1]);
188
+ }
189
+
190
+ template<unsigned Length, typename Type>
191
+ CUDA_CALLABLE inline void adj_neg(const vec_t<Length, Type>& x, vec_t<Length, Type>& adj_x, const vec_t<Length, Type>& adj_ret)
192
+ {
193
+ adj_x -= adj_ret;
194
+ }
195
+
196
+ // equality:
197
+ template<unsigned Length, typename Type>
198
+ inline CUDA_CALLABLE bool operator ==(const vec_t<Length, Type>& a, const vec_t<Length, Type>& b)
199
+ {
200
+ for( unsigned i=0; i < Length; ++i )
201
+ {
202
+ if(a[i] != b[i])
203
+ {
204
+ return false;
205
+ }
206
+ }
207
+ return true;
208
+ }
209
+
210
+ // scalar multiplication:
211
+ template<unsigned Length, typename Type>
212
+ inline CUDA_CALLABLE vec_t<Length, Type> mul(vec_t<Length, Type> a, Type s)
213
+ {
214
+ vec_t<Length, Type> ret;
215
+ for( unsigned i=0; i < Length; ++i )
216
+ {
217
+ ret[i] = a[i] * s;
218
+ }
219
+ return ret;
220
+ }
221
+
222
+ template<typename Type>
223
+ inline CUDA_CALLABLE vec_t<3, Type> mul(vec_t<3, Type> a, Type s)
224
+ {
225
+ return vec_t<3, Type>(a.c[0]*s,a.c[1]*s,a.c[2]*s);
226
+ }
227
+
228
+ template<typename Type>
229
+ inline CUDA_CALLABLE vec_t<2, Type> mul(vec_t<2, Type> a, Type s)
230
+ {
231
+ return vec_t<2, Type>(a.c[0]*s,a.c[1]*s);
232
+ }
233
+
234
+ template<unsigned Length, typename Type>
235
+ inline CUDA_CALLABLE vec_t<Length, Type> mul(Type s, vec_t<Length, Type> a)
236
+ {
237
+ return mul(a, s);
238
+ }
239
+
240
+ template<unsigned Length, typename Type>
241
+ inline CUDA_CALLABLE vec_t<Length, Type> operator*(Type s, vec_t<Length, Type> a)
242
+ {
243
+ return mul(a, s);
244
+ }
245
+
246
+ template<unsigned Length, typename Type>
247
+ inline CUDA_CALLABLE vec_t<Length, Type> operator*(vec_t<Length, Type> a, Type s)
248
+ {
249
+ return mul(a, s);
250
+ }
251
+
252
+
253
+ // component wise multiplication:
254
+ template<unsigned Length, typename Type>
255
+ inline CUDA_CALLABLE vec_t<Length, Type> cw_mul(vec_t<Length, Type> a, vec_t<Length, Type> b)
256
+ {
257
+ vec_t<Length, Type> ret;
258
+ for( unsigned i=0; i < Length; ++i )
259
+ {
260
+ ret[i] = a[i] * b[i];
261
+ }
262
+ return ret;
263
+ }
264
+
265
+ // division
266
+ template<unsigned Length, typename Type>
267
+ inline CUDA_CALLABLE vec_t<Length, Type> div(vec_t<Length, Type> a, Type s)
268
+ {
269
+ vec_t<Length, Type> ret;
270
+ for( unsigned i=0; i < Length; ++i )
271
+ {
272
+ ret[i] = a[i] / s;
273
+ }
274
+ return ret;
275
+ }
276
+
277
+ template<typename Type>
278
+ inline CUDA_CALLABLE vec_t<3, Type> div(vec_t<3, Type> a, Type s)
279
+ {
280
+ return vec_t<3, Type>(a.c[0]/s,a.c[1]/s,a.c[2]/s);
281
+ }
282
+
283
+ template<typename Type>
284
+ inline CUDA_CALLABLE vec_t<2, Type> div(vec_t<2, Type> a, Type s)
285
+ {
286
+ return vec_t<2, Type>(a.c[0]/s,a.c[1]/s);
287
+ }
288
+
289
+ template<unsigned Length, typename Type>
290
+ inline CUDA_CALLABLE vec_t<Length, Type> div(Type s, vec_t<Length, Type> a)
291
+ {
292
+ vec_t<Length, Type> ret;
293
+ for (unsigned i=0; i < Length; ++i)
294
+ {
295
+ ret[i] = s / a[i];
296
+ }
297
+ return ret;
298
+ }
299
+
300
+ template<typename Type>
301
+ inline CUDA_CALLABLE vec_t<3, Type> div(Type s, vec_t<3, Type> a)
302
+ {
303
+ return vec_t<3, Type>(s/a.c[0],s/a.c[1],s/a.c[2]);
304
+ }
305
+
306
+ template<typename Type>
307
+ inline CUDA_CALLABLE vec_t<2, Type> div(Type s, vec_t<2, Type> a)
308
+ {
309
+ return vec_t<2, Type>(s/a.c[0],s/a.c[1]);
310
+ }
311
+
312
+ template<unsigned Length, typename Type>
313
+ inline CUDA_CALLABLE vec_t<Length, Type> operator / (vec_t<Length, Type> a, Type s)
314
+ {
315
+ return div(a,s);
316
+ }
317
+
318
+ template<unsigned Length, typename Type>
319
+ inline CUDA_CALLABLE vec_t<Length, Type> operator / (Type s, vec_t<Length, Type> a)
320
+ {
321
+ return div(s, a);
322
+ }
323
+
324
+ // component wise division
325
+ template<unsigned Length, typename Type>
326
+ inline CUDA_CALLABLE vec_t<Length, Type> cw_div(vec_t<Length, Type> a, vec_t<Length, Type> b)
327
+ {
328
+ vec_t<Length, Type> ret;
329
+ for( unsigned i=0; i < Length; ++i )
330
+ {
331
+ ret[i] = a[i] / b[i];
332
+ }
333
+ return ret;
334
+ }
335
+
336
+ // addition
337
+ template<unsigned Length, typename Type>
338
+ inline CUDA_CALLABLE vec_t<Length, Type> add(vec_t<Length, Type> a, vec_t<Length, Type> b)
339
+ {
340
+ vec_t<Length, Type> ret;
341
+ for( unsigned i=0; i < Length; ++i )
342
+ {
343
+ ret[i] = a[i] + b[i];
344
+ }
345
+ return ret;
346
+ }
347
+
348
+ template<typename Type>
349
+ inline CUDA_CALLABLE vec_t<2, Type> add(vec_t<2, Type> a, vec_t<2, Type> b)
350
+ {
351
+ return vec_t<2, Type>( a.c[0] + b.c[0], a.c[1] + b.c[1]);
352
+ }
353
+
354
+ template<typename Type>
355
+ inline CUDA_CALLABLE vec_t<3, Type> add(vec_t<3, Type> a, vec_t<3, Type> b)
356
+ {
357
+ return vec_t<3, Type>( a.c[0] + b.c[0], a.c[1] + b.c[1], a.c[2] + b.c[2]);
358
+ }
359
+
360
+ // subtraction
361
+ template<unsigned Length, typename Type>
362
+ inline CUDA_CALLABLE vec_t<Length, Type> sub(vec_t<Length, Type> a, vec_t<Length, Type> b)
363
+ {
364
+ vec_t<Length, Type> ret;
365
+ for( unsigned i=0; i < Length; ++i )
366
+ {
367
+ ret[i] = Type(a[i] - b[i]);
368
+ }
369
+ return ret;
370
+ }
371
+
372
+ template<typename Type>
373
+ inline CUDA_CALLABLE vec_t<2, Type> sub(vec_t<2, Type> a, vec_t<2, Type> b)
374
+ {
375
+ return vec_t<2, Type>( a.c[0] - b.c[0], a.c[1] - b.c[1]);
376
+ }
377
+
378
+ template<typename Type>
379
+ inline CUDA_CALLABLE vec_t<3, Type> sub(vec_t<3, Type> a, vec_t<3, Type> b)
380
+ {
381
+ return vec_t<3, Type>( a.c[0] - b.c[0], a.c[1] - b.c[1], a.c[2] - b.c[2]);
382
+ }
383
+
384
+ // dot product:
385
+ template<unsigned Length, typename Type>
386
+ inline CUDA_CALLABLE Type dot(vec_t<Length, Type> a, vec_t<Length, Type> b)
387
+ {
388
+ Type ret(0);
389
+ for( unsigned i=0; i < Length; ++i )
390
+ {
391
+ ret += a[i] * b[i];
392
+ }
393
+ return ret;
394
+ }
395
+
396
+ template<typename Type>
397
+ inline CUDA_CALLABLE Type dot(vec_t<2, Type> a, vec_t<2, Type> b)
398
+ {
399
+ return a.c[0] * b.c[0] + a.c[1] * b.c[1];
400
+ }
401
+
402
+ template<typename Type>
403
+ inline CUDA_CALLABLE Type dot(vec_t<3, Type> a, vec_t<3, Type> b)
404
+ {
405
+ return a.c[0] * b.c[0] + a.c[1] * b.c[1] + a.c[2] * b.c[2];
406
+ }
407
+
408
+ template<unsigned Length, typename Type>
409
+ inline CUDA_CALLABLE Type tensordot(vec_t<Length, Type> a, vec_t<Length, Type> b)
410
+ {
411
+ // corresponds to `np.tensordot()` with all axes being contracted
412
+ return dot(a, b);
413
+ }
414
+
415
+
416
+ template<unsigned Length, typename Type>
417
+ inline CUDA_CALLABLE Type extract(const vec_t<Length, Type> & a, int idx)
418
+ {
419
+ #ifndef NDEBUG
420
+ if (idx < 0 || idx >= Length)
421
+ {
422
+ printf("vec index %d out of bounds at %s %d\n", idx, __FILE__, __LINE__);
423
+ assert(0);
424
+ }
425
+ #endif
426
+
427
+ return a[idx];
428
+ }
429
+
430
+ template<unsigned Length, typename Type>
431
+ inline CUDA_CALLABLE Type* index(vec_t<Length, Type>& v, int idx)
432
+ {
433
+ #ifndef NDEBUG
434
+ if (idx < 0 || idx >= Length)
435
+ {
436
+ printf("vec index %d out of bounds at %s %d\n", idx, __FILE__, __LINE__);
437
+ assert(0);
438
+ }
439
+ #endif
440
+
441
+ return &v[idx];
442
+ }
443
+
444
+ template<unsigned Length, typename Type>
445
+ inline CUDA_CALLABLE Type* indexref(vec_t<Length, Type>* v, int idx)
446
+ {
447
+ #ifndef NDEBUG
448
+ if (idx < 0 || idx >= Length)
449
+ {
450
+ printf("vec store %d out of bounds at %s %d\n", idx, __FILE__, __LINE__);
451
+ assert(0);
452
+ }
453
+ #endif
454
+
455
+ return &((*v)[idx]);
456
+ }
457
+
458
+ template<unsigned Length, typename Type>
459
+ inline CUDA_CALLABLE void adj_index(vec_t<Length, Type>& v, int idx,
460
+ vec_t<Length, Type>& adj_v, int adj_idx, const Type& adj_value)
461
+ {
462
+ // nop
463
+ }
464
+
465
+
466
+ template<unsigned Length, typename Type>
467
+ inline CUDA_CALLABLE void adj_indexref(vec_t<Length, Type>* v, int idx,
468
+ vec_t<Length, Type>& adj_v, int adj_idx, const Type& adj_value)
469
+ {
470
+ // nop
471
+ }
472
+
473
+
474
+ template<unsigned Length, typename Type>
475
+ inline CUDA_CALLABLE Type length(vec_t<Length, Type> a)
476
+ {
477
+ return sqrt(dot(a, a));
478
+ }
479
+
480
+ template<unsigned Length, typename Type>
481
+ inline CUDA_CALLABLE Type length_sq(vec_t<Length, Type> a)
482
+ {
483
+ return dot(a, a);
484
+ }
485
+
486
+
487
+ template<typename Type>
488
+ inline CUDA_CALLABLE Type length(vec_t<2, Type> a)
489
+ {
490
+ return sqrt(a.c[0] * a.c[0] + a.c[1] * a.c[1]);
491
+ }
492
+
493
+ template<typename Type>
494
+ inline CUDA_CALLABLE Type length(vec_t<3, Type> a)
495
+ {
496
+ return sqrt(a.c[0] * a.c[0] + a.c[1] * a.c[1] + a.c[2] * a.c[2]);
497
+ }
498
+
499
+ template<unsigned Length, typename Type>
500
+ inline CUDA_CALLABLE vec_t<Length, Type> normalize(vec_t<Length, Type> a)
501
+ {
502
+ Type l = length(a);
503
+ if (l > Type(kEps))
504
+ return div(a,l);
505
+ else
506
+ return vec_t<Length, Type>();
507
+ }
508
+
509
+ template<typename Type>
510
+ inline CUDA_CALLABLE vec_t<2, Type> normalize(vec_t<2, Type> a)
511
+ {
512
+ Type l = sqrt(a.c[0] * a.c[0] + a.c[1] * a.c[1]);
513
+ if (l > Type(kEps))
514
+ return vec_t<2, Type>(a.c[0]/l,a.c[1]/l);
515
+ else
516
+ return vec_t<2, Type>();
517
+ }
518
+
519
+ template<typename Type>
520
+ inline CUDA_CALLABLE vec_t<3, Type> normalize(vec_t<3, Type> a)
521
+ {
522
+ Type l = sqrt(a.c[0] * a.c[0] + a.c[1] * a.c[1] + a.c[2] * a.c[2]);
523
+ if (l > Type(kEps))
524
+ return vec_t<3, Type>(a.c[0]/l,a.c[1]/l,a.c[2]/l);
525
+ else
526
+ return vec_t<3, Type>();
527
+ }
528
+
529
+
530
+ template<typename Type>
531
+ inline CUDA_CALLABLE vec_t<3,Type> cross(vec_t<3,Type> a, vec_t<3,Type> b)
532
+ {
533
+ return {
534
+ Type(a[1]*b[2] - a[2]*b[1]),
535
+ Type(a[2]*b[0] - a[0]*b[2]),
536
+ Type(a[0]*b[1] - a[1]*b[0])
537
+ };
538
+ }
539
+
540
+
541
+ template<unsigned Length, typename Type>
542
+ inline bool CUDA_CALLABLE isfinite(vec_t<Length, Type> x)
543
+ {
544
+ for( unsigned i=0; i < Length; ++i )
545
+ {
546
+ if(!isfinite(x[i]))
547
+ {
548
+ return false;
549
+ }
550
+ }
551
+ return true;
552
+ }
553
+
554
+ // These two functions seem to compile very slowly
555
+ template<unsigned Length, typename Type>
556
+ inline CUDA_CALLABLE vec_t<Length,Type> min(vec_t<Length,Type> a, vec_t<Length,Type> b)
557
+ {
558
+ vec_t<Length,Type> ret;
559
+ for( unsigned i=0; i < Length; ++i )
560
+ {
561
+ ret[i] = a[i] < b[i] ? a[i] : b[i];
562
+ }
563
+ return ret;
564
+ }
565
+
566
+ template<unsigned Length, typename Type>
567
+ inline CUDA_CALLABLE vec_t<Length,Type> max(vec_t<Length,Type> a, vec_t<Length,Type> b)
568
+ {
569
+ vec_t<Length,Type> ret;
570
+ for( unsigned i=0; i < Length; ++i )
571
+ {
572
+ ret[i] = a[i] > b[i] ? a[i] : b[i];
573
+ }
574
+ return ret;
575
+ }
576
+
577
+ template<unsigned Length, typename Type>
578
+ inline CUDA_CALLABLE Type min(vec_t<Length,Type> v)
579
+ {
580
+ Type ret = v[0];
581
+ for( unsigned i=1; i < Length; ++i )
582
+ {
583
+ if (v[i] < ret)
584
+ ret = v[i];
585
+ }
586
+ return ret;
587
+ }
588
+
589
+ template<unsigned Length, typename Type>
590
+ inline CUDA_CALLABLE Type max(vec_t<Length,Type> v)
591
+ {
592
+ Type ret = v[0];
593
+ for( unsigned i=1; i < Length; ++i )
594
+ {
595
+ if (v[i] > ret)
596
+ ret = v[i];
597
+ }
598
+ return ret;
599
+ }
600
+
601
+ template<unsigned Length, typename Type>
602
+ inline CUDA_CALLABLE unsigned argmin(vec_t<Length,Type> v)
603
+ {
604
+ unsigned ret = 0;
605
+ for( unsigned i=1; i < Length; ++i )
606
+ {
607
+ if (v[i] < v[ret])
608
+ ret = i;
609
+ }
610
+ return ret;
611
+ }
612
+
613
+ template<unsigned Length, typename Type>
614
+ inline CUDA_CALLABLE unsigned argmax(vec_t<Length,Type> v)
615
+ {
616
+ unsigned ret = 0;
617
+ for( unsigned i=1; i < Length; ++i )
618
+ {
619
+ if (v[i] > v[ret])
620
+ ret = i;
621
+ }
622
+ return ret;
623
+ }
624
+
625
+ template<unsigned Length, typename Type>
626
+ inline CUDA_CALLABLE void expect_near(const vec_t<Length, Type>& actual, const vec_t<Length, Type>& expected, const Type& tolerance)
627
+ {
628
+ const Type diff(0);
629
+ for(size_t i=0; i<Length; ++i)
630
+ {
631
+ diff = max(diff,abs(actual[i] - expected[i]));
632
+ }
633
+ if (diff > tolerance)
634
+ {
635
+ printf("Error, expect_near() failed with tolerance "); print(tolerance);
636
+ printf("\t Expected: "); print(expected);
637
+ printf("\t Actual: "); print(actual);
638
+ }
639
+ }
640
+
641
+ template<unsigned Length, typename Type>
642
+ inline CUDA_CALLABLE void adj_expect_near(const vec_t<Length, Type>& actual, const vec_t<Length, Type>& expected, Type tolerance, vec_t<Length, Type>& adj_actual, vec_t<Length, Type>& adj_expected, Type adj_tolerance)
643
+ {
644
+ // nop
645
+ }
646
+
647
+ // adjoint for the initializer_array constructor:
648
+ template<unsigned Length, typename Type>
649
+ inline CUDA_CALLABLE void adj_vec_t(const initializer_array<Length, Type> &cmps, const initializer_array<Length, Type*> &adj_cmps, const vec_t<Length, Type>& adj_ret)
650
+ {
651
+ for(unsigned i=0; i < Length; ++i)
652
+ {
653
+ *(adj_cmps[i]) += adj_ret[i];
654
+ }
655
+ }
656
+
657
+
658
+ // adjoint for the component constructors:
659
+ template<typename Type>
660
+ inline CUDA_CALLABLE void adj_vec_t(Type cmpx, Type cmpy, Type &adj_cmpx, Type &adj_cmpy, const vec_t<2, Type>& adj_ret)
661
+ {
662
+ adj_cmpx += adj_ret.c[0];
663
+ adj_cmpy += adj_ret.c[1];
664
+ }
665
+
666
+ template<typename Type>
667
+ inline CUDA_CALLABLE void adj_vec_t(Type cmpx, Type cmpy, Type cmpz, Type &adj_cmpx, Type &adj_cmpy, Type &adj_cmpz, const vec_t<3, Type>& adj_ret)
668
+ {
669
+ adj_cmpx += adj_ret.c[0];
670
+ adj_cmpy += adj_ret.c[1];
671
+ adj_cmpz += adj_ret.c[2];
672
+ }
673
+
674
+ template<typename Type>
675
+ inline CUDA_CALLABLE void adj_vec_t(Type cmpx, Type cmpy, Type cmpz, Type cmpw, Type &adj_cmpx, Type &adj_cmpy, Type &adj_cmpz, Type &adj_cmpw, const vec_t<4, Type>& adj_ret)
676
+ {
677
+ adj_cmpx += adj_ret.c[0];
678
+ adj_cmpy += adj_ret.c[1];
679
+ adj_cmpz += adj_ret.c[2];
680
+ adj_cmpw += adj_ret.c[3];
681
+ }
682
+
683
+ // adjoint for the constant constructor:
684
+ template<unsigned Length, typename Type>
685
+ inline CUDA_CALLABLE void adj_vec_t(Type s, Type& adj_s, const vec_t<Length, Type>& adj_ret)
686
+ {
687
+ for( unsigned i=0; i < Length; ++i )
688
+ {
689
+ adj_s += adj_ret[i];
690
+ }
691
+ }
692
+
693
+ // adjoint for the casting constructor
694
+ template<unsigned Length, typename Type, typename OtherType>
695
+ inline CUDA_CALLABLE void adj_vec_t(const vec_t<Length, OtherType>& other, vec_t<Length, OtherType>& adj_other, const vec_t<Length, Type>& adj_ret)
696
+ {
697
+ for( unsigned i=0; i < Length; ++i )
698
+ {
699
+ adj_other[i] += static_cast<OtherType>(adj_ret[i]);
700
+ }
701
+ }
702
+
703
+ template<typename Type>
704
+ CUDA_CALLABLE inline void adj_vec_t(const vec_t<3,Type>& w, const vec_t<3,Type>& v, vec_t<3,Type>& adj_w, vec_t<3,Type>& adj_v, const vec_t<6,Type>& adj_ret)
705
+ {
706
+ adj_w[0] += adj_ret[0];
707
+ adj_w[1] += adj_ret[1];
708
+ adj_w[2] += adj_ret[2];
709
+ adj_v[0] += adj_ret[3];
710
+ adj_v[1] += adj_ret[4];
711
+ adj_v[2] += adj_ret[5];
712
+ }
713
+
714
+ template<unsigned Length, typename Type>
715
+ inline CUDA_CALLABLE void adj_mul(vec_t<Length, Type> a, Type s, vec_t<Length, Type>& adj_a, Type& adj_s, const vec_t<Length, Type>& adj_ret)
716
+ {
717
+ for( unsigned i=0; i < Length; ++i )
718
+ {
719
+ adj_a[i] += s*adj_ret[i];
720
+ }
721
+
722
+ adj_s += dot(a, adj_ret);
723
+
724
+ #if FP_CHECK
725
+ if (!isfinite(a) || !isfinite(s) || !isfinite(adj_a) || !isfinite(adj_s) || !isfinite(adj_ret))
726
+ {
727
+ // \TODO: How shall we implement this error message?
728
+ //printf("adj_mul((%f %f %f %f), %f, (%f %f %f %f), %f, (%f %f %f %f)\n", a.x, a.y, a.z, a.w, s, adj_a.x, adj_a.y, adj_a.z, adj_a.w, adj_s, adj_ret.x, adj_ret.y, adj_ret.z, adj_ret.w);
729
+ assert(0);
730
+ }
731
+ #endif
732
+ }
733
+
734
+ template<unsigned Length, typename Type>
735
+ inline CUDA_CALLABLE void adj_mul(Type s, vec_t<Length, Type> a, Type& adj_s, vec_t<Length, Type>& adj_a, const vec_t<Length, Type>& adj_ret)
736
+ {
737
+ adj_mul(a, s, adj_a, adj_s, adj_ret);
738
+ }
739
+
740
+ template<unsigned Length, typename Type>
741
+ inline CUDA_CALLABLE void adj_cw_mul(vec_t<Length, Type> a, vec_t<Length, Type> b, vec_t<Length, Type>& adj_a, vec_t<Length, Type>& adj_b, const vec_t<Length, Type>& adj_ret)
742
+ {
743
+ adj_a += cw_mul(b, adj_ret);
744
+ adj_b += cw_mul(a, adj_ret);
745
+ }
746
+
747
+ template<unsigned Length, typename Type>
748
+ inline CUDA_CALLABLE void adj_div(vec_t<Length, Type> a, Type s, vec_t<Length, Type>& adj_a, Type& adj_s, const vec_t<Length, Type>& adj_ret)
749
+ {
750
+
751
+ adj_s -= dot(a , adj_ret)/ (s * s); // - a / s^2
752
+
753
+ for( unsigned i=0; i < Length; ++i )
754
+ {
755
+ adj_a[i] += adj_ret[i] / s;
756
+ }
757
+
758
+ #if FP_CHECK
759
+ if (!isfinite(a) || !isfinite(s) || !isfinite(adj_a) || !isfinite(adj_s) || !isfinite(adj_ret))
760
+ {
761
+ // \TODO: How shall we implement this error message?
762
+ // printf("adj_div((%f %f %f %f), %f, (%f %f %f %f), %f, (%f %f %f %f)\n", a.x, a.y, a.z, a.w, s, adj_a.x, adj_a.y, adj_a.z, adj_a.w, adj_s, adj_ret.x, adj_ret.y, adj_ret.z, adj_ret.w);
763
+ assert(0);
764
+ }
765
+ #endif
766
+ }
767
+
768
+ template<unsigned Length, typename Type>
769
+ inline CUDA_CALLABLE void adj_div(Type s, vec_t<Length, Type> a, Type& adj_s, vec_t<Length, Type>& adj_a, const vec_t<Length, Type>& adj_ret)
770
+ {
771
+
772
+ adj_s -= dot(a , adj_ret)/ (s * s); // - a / s^2
773
+
774
+ for( unsigned i=0; i < Length; ++i )
775
+ {
776
+ adj_a[i] += s / adj_ret[i];
777
+ }
778
+
779
+ #if FP_CHECK
780
+ if (!isfinite(a) || !isfinite(s) || !isfinite(adj_a) || !isfinite(adj_s) || !isfinite(adj_ret))
781
+ {
782
+ // \TODO: How shall we implement this error message?
783
+ // printf("adj_div((%f %f %f %f), %f, (%f %f %f %f), %f, (%f %f %f %f)\n", a.x, a.y, a.z, a.w, s, adj_a.x, adj_a.y, adj_a.z, adj_a.w, adj_s, adj_ret.x, adj_ret.y, adj_ret.z, adj_ret.w);
784
+ assert(0);
785
+ }
786
+ #endif
787
+ }
788
+
789
+ template<unsigned Length, typename Type>
790
+ inline CUDA_CALLABLE void adj_cw_div(vec_t<Length, Type> a, vec_t<Length, Type> b, vec_t<Length, Type>& ret, vec_t<Length, Type>& adj_a, vec_t<Length, Type>& adj_b, const vec_t<Length, Type>& adj_ret) {
791
+ adj_a += cw_div(adj_ret, b);
792
+ adj_b -= cw_mul(adj_ret, cw_div(ret, b));
793
+ }
794
+
795
+ template<unsigned Length, typename Type>
796
+ inline CUDA_CALLABLE void adj_add(vec_t<Length, Type> a, vec_t<Length, Type> b, vec_t<Length, Type>& adj_a, vec_t<Length, Type>& adj_b, const vec_t<Length, Type>& adj_ret)
797
+ {
798
+ adj_a += adj_ret;
799
+ adj_b += adj_ret;
800
+ }
801
+
802
+ template<typename Type>
803
+ inline CUDA_CALLABLE void adj_add(vec_t<2, Type> a, vec_t<2, Type> b, vec_t<2, Type>& adj_a, vec_t<2, Type>& adj_b, const vec_t<2, Type>& adj_ret)
804
+ {
805
+ adj_a.c[0] += adj_ret.c[0];
806
+ adj_a.c[1] += adj_ret.c[1];
807
+ adj_b.c[0] += adj_ret.c[0];
808
+ adj_b.c[1] += adj_ret.c[1];
809
+ }
810
+
811
+ template<typename Type>
812
+ inline CUDA_CALLABLE void adj_add(vec_t<3, Type> a, vec_t<3, Type> b, vec_t<3, Type>& adj_a, vec_t<3, Type>& adj_b, const vec_t<3, Type>& adj_ret)
813
+ {
814
+ adj_a.c[0] += adj_ret.c[0];
815
+ adj_a.c[1] += adj_ret.c[1];
816
+ adj_a.c[2] += adj_ret.c[2];
817
+ adj_b.c[0] += adj_ret.c[0];
818
+ adj_b.c[1] += adj_ret.c[1];
819
+ adj_b.c[2] += adj_ret.c[2];
820
+ }
821
+
822
+ template<unsigned Length, typename Type>
823
+ inline CUDA_CALLABLE void adj_sub(vec_t<Length, Type> a, vec_t<Length, Type> b, vec_t<Length, Type>& adj_a, vec_t<Length, Type>& adj_b, const vec_t<Length, Type>& adj_ret)
824
+ {
825
+ adj_a += adj_ret;
826
+ adj_b -= adj_ret;
827
+ }
828
+
829
+ template<typename Type>
830
+ inline CUDA_CALLABLE void adj_sub(vec_t<2, Type> a, vec_t<2, Type> b, vec_t<2, Type>& adj_a, vec_t<2, Type>& adj_b, const vec_t<2, Type>& adj_ret)
831
+ {
832
+ adj_a.c[0] += adj_ret.c[0];
833
+ adj_a.c[1] += adj_ret.c[1];
834
+ adj_b.c[0] -= adj_ret.c[0];
835
+ adj_b.c[1] -= adj_ret.c[1];
836
+ }
837
+
838
+ template<typename Type>
839
+ inline CUDA_CALLABLE void adj_sub(vec_t<3, Type> a, vec_t<3, Type> b, vec_t<3, Type>& adj_a, vec_t<3, Type>& adj_b, const vec_t<3, Type>& adj_ret)
840
+ {
841
+ adj_a.c[0] += adj_ret.c[0];
842
+ adj_a.c[1] += adj_ret.c[1];
843
+ adj_a.c[2] += adj_ret.c[2];
844
+ adj_b.c[0] -= adj_ret.c[0];
845
+ adj_b.c[1] -= adj_ret.c[1];
846
+ adj_b.c[2] -= adj_ret.c[2];
847
+ }
848
+
849
+ template<unsigned Length, typename Type>
850
+ inline CUDA_CALLABLE void adj_dot(vec_t<Length, Type> a, vec_t<Length, Type> b, vec_t<Length, Type>& adj_a, vec_t<Length, Type>& adj_b, const Type adj_ret)
851
+ {
852
+ adj_a += b*adj_ret;
853
+ adj_b += a*adj_ret;
854
+
855
+ #if FP_CHECK
856
+ if (!isfinite(a) || !isfinite(b) || !isfinite(adj_a) || !isfinite(adj_b) || !isfinite(adj_ret))
857
+ {
858
+ // \TODO: How shall we implement this error message?
859
+ //printf("adj_dot((%f %f %f %f), (%f %f %f %f), (%f %f %f %f), (%f %f %f %f), %f)\n", a.x, a.y, a.z, a.w, b.x, b.y, b.z, b.w, adj_a.x, adj_a.y, adj_a.z, adj_a.w, adj_b.x, adj_b.y, adj_b.z, adj_b.w, adj_ret);
860
+ assert(0);
861
+ }
862
+ #endif
863
+ }
864
+
865
+
866
+
867
+ template<typename Type>
868
+ inline CUDA_CALLABLE void adj_dot(vec_t<2, Type> a, vec_t<2, Type> b, vec_t<2, Type>& adj_a, vec_t<2, Type>& adj_b, const Type adj_ret)
869
+ {
870
+ adj_a.c[0] += b.c[0]*adj_ret;
871
+ adj_a.c[1] += b.c[1]*adj_ret;
872
+
873
+ adj_b.c[0] += a.c[0]*adj_ret;
874
+ adj_b.c[1] += a.c[1]*adj_ret;
875
+ }
876
+
877
+ template<typename Type>
878
+ inline CUDA_CALLABLE void adj_dot(vec_t<3, Type> a, vec_t<3, Type> b, vec_t<3, Type>& adj_a, vec_t<3, Type>& adj_b, const Type adj_ret)
879
+ {
880
+ adj_a.c[0] += b.c[0]*adj_ret;
881
+ adj_a.c[1] += b.c[1]*adj_ret;
882
+ adj_a.c[2] += b.c[2]*adj_ret;
883
+
884
+ adj_b.c[0] += a.c[0]*adj_ret;
885
+ adj_b.c[1] += a.c[1]*adj_ret;
886
+ adj_b.c[2] += a.c[2]*adj_ret;
887
+ }
888
+
889
+
890
+ template<unsigned Length, typename Type>
891
+ inline CUDA_CALLABLE void adj_extract(const vec_t<Length, Type> & a, int idx, vec_t<Length, Type> & adj_a, int & adj_idx, Type & adj_ret)
892
+ {
893
+ #ifndef NDEBUG
894
+ if (idx < 0 || idx > Length)
895
+ {
896
+ printf("Tvec2<Scalar> index %d out of bounds at %s %d\n", idx, __FILE__, __LINE__);
897
+ assert(0);
898
+ }
899
+ #endif
900
+
901
+ adj_a[idx] += adj_ret;
902
+ }
903
+
904
+ template<unsigned Length, typename Type>
905
+ inline CUDA_CALLABLE void adj_length(vec_t<Length, Type> a, Type ret, vec_t<Length, Type>& adj_a, const Type adj_ret)
906
+ {
907
+ if (ret > Type(kEps))
908
+ {
909
+ adj_a += div(a, ret) * adj_ret;
910
+ }
911
+
912
+ #if FP_CHECK
913
+ if (!isfinite(adj_a))
914
+ {
915
+ // \TODO: How shall we implement this error message?
916
+ //printf("%s:%d - adj_length((%f %f %f %f), (%f %f %f %f), (%f))\n", __FILE__, __LINE__, a.x, a.y, a.z, a.w, adj_a.x, adj_a.y, adj_a.z, adj_a.w, adj_ret);
917
+ assert(0);
918
+ }
919
+ #endif
920
+ }
921
+
922
+ template<unsigned Length, typename Type>
923
+ inline CUDA_CALLABLE void adj_length_sq(vec_t<Length, Type> a, vec_t<Length, Type>& adj_a, const Type adj_ret)
924
+ {
925
+ adj_a += Type(2.0)*a*adj_ret;
926
+
927
+ #if FP_CHECK
928
+ if (!isfinite(adj_a))
929
+ {
930
+ // \TODO: How shall we implement this error message?
931
+ //printf("%s:%d - adj_length((%f %f %f %f), (%f %f %f %f), (%f))\n", __FILE__, __LINE__, a.x, a.y, a.z, a.w, adj_a.x, adj_a.y, adj_a.z, adj_a.w, adj_ret);
932
+ assert(0);
933
+ }
934
+ #endif
935
+ }
936
+
937
+ template<unsigned Length, typename Type>
938
+ inline CUDA_CALLABLE void adj_normalize(vec_t<Length, Type> a, vec_t<Length, Type>& ret, vec_t<Length, Type>& adj_a, const vec_t<Length, Type>& adj_ret)
939
+ {
940
+ Type d = length(a);
941
+
942
+ if (d > Type(kEps))
943
+ {
944
+ Type invd = Type(1.0f)/d;
945
+
946
+ adj_a += (adj_ret*invd - ret*(dot(ret, adj_ret))*invd);
947
+
948
+ #if FP_CHECK
949
+ if (!isfinite(adj_a))
950
+ {
951
+ // \TODO: How shall we implement this error message?
952
+ //printf("%s:%d - adj_normalize((%f %f %f %f), (%f %f %f %f), (%f, %f, %f, %f))\n", __FILE__, __LINE__, a.x, a.y, a.z, a.w, adj_a.x, adj_a.y, adj_a.z, adj_a.w, adj_ret.x, adj_ret.y, adj_ret.z, adj_ret.w);
953
+ assert(0);
954
+ }
955
+ #endif
956
+ }
957
+ }
958
+
959
+ template<typename Type>
960
+ inline CUDA_CALLABLE void adj_cross(vec_t<3,Type> a, vec_t<3,Type> b, vec_t<3,Type>& adj_a, vec_t<3,Type>& adj_b, const vec_t<3,Type>& adj_ret)
961
+ {
962
+ // todo: sign check
963
+ adj_a += cross(b, adj_ret);
964
+ adj_b -= cross(a, adj_ret);
965
+ }
966
+
967
+ template<unsigned Length, typename Type>
968
+ inline CUDA_CALLABLE void adj_min(const vec_t<Length,Type> &a, const vec_t<Length,Type> &b, vec_t<Length,Type>& adj_a, vec_t<Length,Type>& adj_b, const vec_t<Length,Type> &adj_ret)
969
+ {
970
+ for( unsigned i=0; i < Length; ++i )
971
+ {
972
+ if (a[i] < b[i])
973
+ adj_a[i] += adj_ret[i];
974
+ else
975
+ adj_b[i] += adj_ret[i];
976
+ }
977
+ }
978
+
979
+ template<unsigned Length, typename Type>
980
+ inline CUDA_CALLABLE void adj_max(const vec_t<Length,Type> &a, const vec_t<Length,Type> &b, vec_t<Length,Type>& adj_a, vec_t<Length,Type>& adj_b, const vec_t<Length,Type> &adj_ret)
981
+ {
982
+ for( unsigned i=0; i < Length; ++i )
983
+ {
984
+ if (a[i] > b[i])
985
+ adj_a[i] += adj_ret[i];
986
+ else
987
+ adj_b[i] += adj_ret[i];
988
+ }
989
+ }
990
+
991
+ template<unsigned Length, typename Type>
992
+ inline CUDA_CALLABLE void adj_min(const vec_t<Length,Type> &v, vec_t<Length,Type>& adj_v, const Type &adj_ret)
993
+ {
994
+ unsigned i = argmin(v);
995
+ adj_v[i] += adj_ret;
996
+ }
997
+
998
+ template<unsigned Length, typename Type>
999
+ inline CUDA_CALLABLE void adj_max(const vec_t<Length,Type> &v, vec_t<Length,Type>& adj_v, const Type &adj_ret)
1000
+ {
1001
+ unsigned i = argmax(v);
1002
+ adj_v[i] += adj_ret;
1003
+ }
1004
+
1005
+ // Do I need to specialize these for different lengths?
1006
+ template<unsigned Length, typename Type>
1007
+ inline CUDA_CALLABLE vec_t<Length, Type> atomic_add(vec_t<Length, Type> * addr, vec_t<Length, Type> value)
1008
+ {
1009
+ vec_t<Length, Type> ret;
1010
+ for( unsigned i=0; i < Length; ++i )
1011
+ {
1012
+ ret[i] = atomic_add(&(addr -> c[i]), value[i]);
1013
+ }
1014
+
1015
+ return ret;
1016
+ }
1017
+
1018
+ template<unsigned Length, typename Type>
1019
+ inline CUDA_CALLABLE vec_t<Length, Type> atomic_min(vec_t<Length, Type> * addr, vec_t<Length, Type> value)
1020
+ {
1021
+ vec_t<Length, Type> ret;
1022
+ for( unsigned i=0; i < Length; ++i )
1023
+ {
1024
+ ret[i] = atomic_min(&(addr -> c[i]), value[i]);
1025
+ }
1026
+
1027
+ return ret;
1028
+ }
1029
+
1030
+ template<unsigned Length, typename Type>
1031
+ inline CUDA_CALLABLE vec_t<Length, Type> atomic_max(vec_t<Length, Type> * addr, vec_t<Length, Type> value)
1032
+ {
1033
+ vec_t<Length, Type> ret;
1034
+ for( unsigned i=0; i < Length; ++i )
1035
+ {
1036
+ ret[i] = atomic_max(&(addr -> c[i]), value[i]);
1037
+ }
1038
+
1039
+ return ret;
1040
+ }
1041
+
1042
+ template<unsigned Length, typename Type>
1043
+ inline CUDA_CALLABLE void adj_atomic_minmax(
1044
+ vec_t<Length,Type> *addr,
1045
+ vec_t<Length,Type> *adj_addr,
1046
+ const vec_t<Length,Type> &value,
1047
+ vec_t<Length,Type> &adj_value)
1048
+ {
1049
+ for (unsigned i=0; i < Length; ++i)
1050
+ adj_atomic_minmax(&(addr->c[i]), &(adj_addr->c[i]), value[i], adj_value[i]);
1051
+ }
1052
+
1053
+ // ok, the original implementation of this didn't take the absolute values.
1054
+ // I wouldn't consider this expected behavior. It looks like it's only
1055
+ // being used for bounding boxes at the moment, where this doesn't matter,
1056
+ // but you often use it for ray tracing where it does. Not sure if the
1057
+ // fabs() incurs a performance hit...
1058
+ template<unsigned Length, typename Type>
1059
+ CUDA_CALLABLE inline int longest_axis(const vec_t<Length, Type>& v)
1060
+ {
1061
+ Type lmax = abs(v[0]);
1062
+ int ret(0);
1063
+ for( unsigned i=1; i < Length; ++i )
1064
+ {
1065
+ Type l = abs(v[i]);
1066
+ if( l > lmax )
1067
+ {
1068
+ ret = i;
1069
+ lmax = l;
1070
+ }
1071
+ }
1072
+ return ret;
1073
+ }
1074
+
1075
+ template<unsigned Length, typename Type>
1076
+ CUDA_CALLABLE inline vec_t<Length,Type> lerp(const vec_t<Length,Type>& a, const vec_t<Length,Type>& b, Type t)
1077
+ {
1078
+ return a*(Type(1)-t) + b*t;
1079
+ }
1080
+
1081
+ template<unsigned Length, typename Type>
1082
+ CUDA_CALLABLE inline void adj_lerp(const vec_t<Length,Type>& a, const vec_t<Length,Type>& b, Type t, vec_t<Length,Type>& adj_a, vec_t<Length,Type>& adj_b, Type& adj_t, const vec_t<Length,Type>& adj_ret)
1083
+ {
1084
+ adj_a += adj_ret*(Type(1)-t);
1085
+ adj_b += adj_ret*t;
1086
+ adj_t += tensordot(b, adj_ret) - tensordot(a, adj_ret);
1087
+ }
1088
+
1089
+ // for integral types we do not accumulate gradients
1090
+ template<unsigned Length> CUDA_CALLABLE inline void adj_atomic_add(vec_t<Length, int8>* buf, const vec_t<Length, int8> &value) { }
1091
+ template<unsigned Length> CUDA_CALLABLE inline void adj_atomic_add(vec_t<Length, uint8>* buf, const vec_t<Length, uint8> &value) { }
1092
+ template<unsigned Length> CUDA_CALLABLE inline void adj_atomic_add(vec_t<Length, int16>* buf, const vec_t<Length, int16> &value) { }
1093
+ template<unsigned Length> CUDA_CALLABLE inline void adj_atomic_add(vec_t<Length, uint16>* buf, const vec_t<Length, uint16> &value) { }
1094
+ template<unsigned Length> CUDA_CALLABLE inline void adj_atomic_add(vec_t<Length, int32>* buf, const vec_t<Length, int32> &value) { }
1095
+ template<unsigned Length> CUDA_CALLABLE inline void adj_atomic_add(vec_t<Length, uint32>* buf, const vec_t<Length, uint32> &value) { }
1096
+ template<unsigned Length> CUDA_CALLABLE inline void adj_atomic_add(vec_t<Length, int64>* buf, const vec_t<Length, int64> &value) { }
1097
+ template<unsigned Length> CUDA_CALLABLE inline void adj_atomic_add(vec_t<Length, uint64>* buf, const vec_t<Length, uint64> &value) { }
1098
+
1099
+
1100
+ // adjoints for some of the constructors, used in intersect.h
1101
+ inline CUDA_CALLABLE void adj_vec2(float x, float y, float& adj_x, float& adj_y, const vec2& adj_ret)
1102
+ {
1103
+ adj_x += adj_ret[0];
1104
+ adj_y += adj_ret[1];
1105
+ }
1106
+
1107
+ inline CUDA_CALLABLE void adj_vec3(float x, float y, float z, float& adj_x, float& adj_y, float& adj_z, const vec3& adj_ret)
1108
+ {
1109
+ adj_x += adj_ret[0];
1110
+ adj_y += adj_ret[1];
1111
+ adj_z += adj_ret[2];
1112
+ }
1113
+
1114
+ inline CUDA_CALLABLE void adj_vec4(float x, float y, float z, float w, float& adj_x, float& adj_y, float& adj_z, float& adj_w, const vec4& adj_ret)
1115
+ {
1116
+ adj_x += adj_ret[0];
1117
+ adj_y += adj_ret[1];
1118
+ adj_z += adj_ret[2];
1119
+ adj_w += adj_ret[3];
1120
+ }
1121
+
1122
+ inline CUDA_CALLABLE void adj_vec3(float s, float& adj_s, const vec3& adj_ret)
1123
+ {
1124
+ adj_vec_t(s, adj_s, adj_ret);
1125
+ }
1126
+
1127
+ inline CUDA_CALLABLE void adj_vec4(float s, float& adj_s, const vec4& adj_ret)
1128
+ {
1129
+ adj_vec_t(s, adj_s, adj_ret);
1130
+ }
1131
+
1132
+
1133
1133
  } // namespace wp