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

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

Potentially problematic release.


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

Files changed (356) hide show
  1. warp/__init__.py +108 -97
  2. warp/__init__.pyi +1 -1
  3. warp/bin/warp-clang.so +0 -0
  4. warp/bin/warp.so +0 -0
  5. warp/build.py +88 -113
  6. warp/build_dll.py +383 -375
  7. warp/builtins.py +3693 -3354
  8. warp/codegen.py +2925 -2792
  9. warp/config.py +40 -36
  10. warp/constants.py +49 -45
  11. warp/context.py +5409 -5102
  12. warp/dlpack.py +442 -442
  13. warp/examples/__init__.py +16 -16
  14. warp/examples/assets/bear.usd +0 -0
  15. warp/examples/assets/bunny.usd +0 -0
  16. warp/examples/assets/cartpole.urdf +110 -110
  17. warp/examples/assets/crazyflie.usd +0 -0
  18. warp/examples/assets/cube.usd +0 -0
  19. warp/examples/assets/nv_ant.xml +92 -92
  20. warp/examples/assets/nv_humanoid.xml +183 -183
  21. warp/examples/assets/quadruped.urdf +267 -267
  22. warp/examples/assets/rocks.nvdb +0 -0
  23. warp/examples/assets/rocks.usd +0 -0
  24. warp/examples/assets/sphere.usd +0 -0
  25. warp/examples/benchmarks/benchmark_api.py +381 -383
  26. warp/examples/benchmarks/benchmark_cloth.py +278 -277
  27. warp/examples/benchmarks/benchmark_cloth_cupy.py +88 -88
  28. warp/examples/benchmarks/benchmark_cloth_jax.py +97 -100
  29. warp/examples/benchmarks/benchmark_cloth_numba.py +146 -142
  30. warp/examples/benchmarks/benchmark_cloth_numpy.py +77 -77
  31. warp/examples/benchmarks/benchmark_cloth_pytorch.py +86 -86
  32. warp/examples/benchmarks/benchmark_cloth_taichi.py +112 -112
  33. warp/examples/benchmarks/benchmark_cloth_warp.py +145 -146
  34. warp/examples/benchmarks/benchmark_launches.py +293 -295
  35. warp/examples/browse.py +29 -29
  36. warp/examples/core/example_dem.py +232 -219
  37. warp/examples/core/example_fluid.py +291 -267
  38. warp/examples/core/example_graph_capture.py +142 -126
  39. warp/examples/core/example_marching_cubes.py +186 -174
  40. warp/examples/core/example_mesh.py +172 -155
  41. warp/examples/core/example_mesh_intersect.py +203 -193
  42. warp/examples/core/example_nvdb.py +174 -170
  43. warp/examples/core/example_raycast.py +103 -90
  44. warp/examples/core/example_raymarch.py +197 -178
  45. warp/examples/core/example_render_opengl.py +183 -141
  46. warp/examples/core/example_sph.py +403 -387
  47. warp/examples/core/example_torch.py +219 -181
  48. warp/examples/core/example_wave.py +261 -248
  49. warp/examples/fem/bsr_utils.py +378 -380
  50. warp/examples/fem/example_apic_fluid.py +432 -389
  51. warp/examples/fem/example_burgers.py +262 -0
  52. warp/examples/fem/example_convection_diffusion.py +180 -168
  53. warp/examples/fem/example_convection_diffusion_dg.py +217 -209
  54. warp/examples/fem/example_deformed_geometry.py +175 -159
  55. warp/examples/fem/example_diffusion.py +199 -173
  56. warp/examples/fem/example_diffusion_3d.py +178 -152
  57. warp/examples/fem/example_diffusion_mgpu.py +219 -214
  58. warp/examples/fem/example_mixed_elasticity.py +242 -222
  59. warp/examples/fem/example_navier_stokes.py +257 -243
  60. warp/examples/fem/example_stokes.py +218 -192
  61. warp/examples/fem/example_stokes_transfer.py +263 -249
  62. warp/examples/fem/mesh_utils.py +133 -109
  63. warp/examples/fem/plot_utils.py +292 -287
  64. warp/examples/optim/example_bounce.py +258 -246
  65. warp/examples/optim/example_cloth_throw.py +220 -209
  66. warp/examples/optim/example_diffray.py +564 -536
  67. warp/examples/optim/example_drone.py +862 -835
  68. warp/examples/optim/example_inverse_kinematics.py +174 -168
  69. warp/examples/optim/example_inverse_kinematics_torch.py +183 -169
  70. warp/examples/optim/example_spring_cage.py +237 -231
  71. warp/examples/optim/example_trajectory.py +221 -199
  72. warp/examples/optim/example_walker.py +304 -293
  73. warp/examples/sim/example_cartpole.py +137 -129
  74. warp/examples/sim/example_cloth.py +194 -186
  75. warp/examples/sim/example_granular.py +122 -111
  76. warp/examples/sim/example_granular_collision_sdf.py +195 -186
  77. warp/examples/sim/example_jacobian_ik.py +234 -214
  78. warp/examples/sim/example_particle_chain.py +116 -105
  79. warp/examples/sim/example_quadruped.py +191 -180
  80. warp/examples/sim/example_rigid_chain.py +195 -187
  81. warp/examples/sim/example_rigid_contact.py +187 -177
  82. warp/examples/sim/example_rigid_force.py +125 -125
  83. warp/examples/sim/example_rigid_gyroscopic.py +107 -95
  84. warp/examples/sim/example_rigid_soft_contact.py +132 -122
  85. warp/examples/sim/example_soft_body.py +188 -177
  86. warp/fabric.py +337 -335
  87. warp/fem/__init__.py +61 -27
  88. warp/fem/cache.py +403 -388
  89. warp/fem/dirichlet.py +178 -179
  90. warp/fem/domain.py +262 -263
  91. warp/fem/field/__init__.py +100 -101
  92. warp/fem/field/field.py +148 -149
  93. warp/fem/field/nodal_field.py +298 -299
  94. warp/fem/field/restriction.py +22 -21
  95. warp/fem/field/test.py +180 -181
  96. warp/fem/field/trial.py +183 -183
  97. warp/fem/geometry/__init__.py +16 -19
  98. warp/fem/geometry/closest_point.py +69 -70
  99. warp/fem/geometry/deformed_geometry.py +270 -271
  100. warp/fem/geometry/element.py +748 -744
  101. warp/fem/geometry/geometry.py +184 -186
  102. warp/fem/geometry/grid_2d.py +380 -373
  103. warp/fem/geometry/grid_3d.py +437 -435
  104. warp/fem/geometry/hexmesh.py +953 -953
  105. warp/fem/geometry/nanogrid.py +455 -0
  106. warp/fem/geometry/partition.py +374 -376
  107. warp/fem/geometry/quadmesh_2d.py +532 -532
  108. warp/fem/geometry/tetmesh.py +840 -840
  109. warp/fem/geometry/trimesh_2d.py +577 -577
  110. warp/fem/integrate.py +1684 -1615
  111. warp/fem/operator.py +190 -191
  112. warp/fem/polynomial.py +214 -213
  113. warp/fem/quadrature/__init__.py +2 -2
  114. warp/fem/quadrature/pic_quadrature.py +243 -245
  115. warp/fem/quadrature/quadrature.py +295 -294
  116. warp/fem/space/__init__.py +179 -292
  117. warp/fem/space/basis_space.py +522 -489
  118. warp/fem/space/collocated_function_space.py +100 -105
  119. warp/fem/space/dof_mapper.py +236 -236
  120. warp/fem/space/function_space.py +148 -145
  121. warp/fem/space/grid_2d_function_space.py +148 -267
  122. warp/fem/space/grid_3d_function_space.py +167 -306
  123. warp/fem/space/hexmesh_function_space.py +253 -352
  124. warp/fem/space/nanogrid_function_space.py +202 -0
  125. warp/fem/space/partition.py +350 -350
  126. warp/fem/space/quadmesh_2d_function_space.py +261 -369
  127. warp/fem/space/restriction.py +161 -160
  128. warp/fem/space/shape/__init__.py +90 -15
  129. warp/fem/space/shape/cube_shape_function.py +728 -738
  130. warp/fem/space/shape/shape_function.py +102 -103
  131. warp/fem/space/shape/square_shape_function.py +611 -611
  132. warp/fem/space/shape/tet_shape_function.py +565 -567
  133. warp/fem/space/shape/triangle_shape_function.py +429 -429
  134. warp/fem/space/tetmesh_function_space.py +224 -292
  135. warp/fem/space/topology.py +297 -295
  136. warp/fem/space/trimesh_2d_function_space.py +153 -221
  137. warp/fem/types.py +77 -77
  138. warp/fem/utils.py +495 -495
  139. warp/jax.py +166 -141
  140. warp/jax_experimental.py +341 -339
  141. warp/native/array.h +1081 -1025
  142. warp/native/builtin.h +1603 -1560
  143. warp/native/bvh.cpp +402 -398
  144. warp/native/bvh.cu +533 -525
  145. warp/native/bvh.h +430 -429
  146. warp/native/clang/clang.cpp +496 -464
  147. warp/native/crt.cpp +42 -32
  148. warp/native/crt.h +352 -335
  149. warp/native/cuda_crt.h +1049 -1049
  150. warp/native/cuda_util.cpp +549 -540
  151. warp/native/cuda_util.h +288 -203
  152. warp/native/cutlass_gemm.cpp +34 -34
  153. warp/native/cutlass_gemm.cu +372 -372
  154. warp/native/error.cpp +66 -66
  155. warp/native/error.h +27 -27
  156. warp/native/exports.h +187 -0
  157. warp/native/fabric.h +228 -228
  158. warp/native/hashgrid.cpp +301 -278
  159. warp/native/hashgrid.cu +78 -77
  160. warp/native/hashgrid.h +227 -227
  161. warp/native/initializer_array.h +32 -32
  162. warp/native/intersect.h +1204 -1204
  163. warp/native/intersect_adj.h +365 -365
  164. warp/native/intersect_tri.h +322 -322
  165. warp/native/marching.cpp +2 -2
  166. warp/native/marching.cu +497 -497
  167. warp/native/marching.h +2 -2
  168. warp/native/mat.h +1545 -1498
  169. warp/native/matnn.h +333 -333
  170. warp/native/mesh.cpp +203 -203
  171. warp/native/mesh.cu +292 -293
  172. warp/native/mesh.h +1887 -1887
  173. warp/native/nanovdb/GridHandle.h +366 -0
  174. warp/native/nanovdb/HostBuffer.h +590 -0
  175. warp/native/nanovdb/NanoVDB.h +6624 -4782
  176. warp/native/nanovdb/PNanoVDB.h +3390 -2553
  177. warp/native/noise.h +850 -850
  178. warp/native/quat.h +1112 -1085
  179. warp/native/rand.h +303 -299
  180. warp/native/range.h +108 -108
  181. warp/native/reduce.cpp +156 -156
  182. warp/native/reduce.cu +348 -348
  183. warp/native/runlength_encode.cpp +61 -61
  184. warp/native/runlength_encode.cu +46 -46
  185. warp/native/scan.cpp +30 -30
  186. warp/native/scan.cu +36 -36
  187. warp/native/scan.h +7 -7
  188. warp/native/solid_angle.h +442 -442
  189. warp/native/sort.cpp +94 -94
  190. warp/native/sort.cu +97 -97
  191. warp/native/sort.h +14 -14
  192. warp/native/sparse.cpp +337 -337
  193. warp/native/sparse.cu +544 -544
  194. warp/native/spatial.h +630 -630
  195. warp/native/svd.h +562 -562
  196. warp/native/temp_buffer.h +30 -30
  197. warp/native/vec.h +1177 -1133
  198. warp/native/volume.cpp +529 -297
  199. warp/native/volume.cu +58 -32
  200. warp/native/volume.h +960 -538
  201. warp/native/volume_builder.cu +446 -425
  202. warp/native/volume_builder.h +34 -19
  203. warp/native/volume_impl.h +61 -0
  204. warp/native/warp.cpp +1057 -1052
  205. warp/native/warp.cu +2949 -2828
  206. warp/native/warp.h +321 -305
  207. warp/optim/__init__.py +9 -9
  208. warp/optim/adam.py +120 -120
  209. warp/optim/linear.py +1104 -939
  210. warp/optim/sgd.py +104 -92
  211. warp/render/__init__.py +10 -10
  212. warp/render/render_opengl.py +3356 -3204
  213. warp/render/render_usd.py +768 -749
  214. warp/render/utils.py +152 -150
  215. warp/sim/__init__.py +52 -59
  216. warp/sim/articulation.py +685 -685
  217. warp/sim/collide.py +1594 -1590
  218. warp/sim/import_mjcf.py +489 -481
  219. warp/sim/import_snu.py +220 -221
  220. warp/sim/import_urdf.py +536 -516
  221. warp/sim/import_usd.py +887 -881
  222. warp/sim/inertia.py +316 -317
  223. warp/sim/integrator.py +234 -233
  224. warp/sim/integrator_euler.py +1956 -1956
  225. warp/sim/integrator_featherstone.py +1917 -1991
  226. warp/sim/integrator_xpbd.py +3288 -3312
  227. warp/sim/model.py +4473 -4314
  228. warp/sim/particles.py +113 -112
  229. warp/sim/render.py +417 -403
  230. warp/sim/utils.py +413 -410
  231. warp/sparse.py +1289 -1227
  232. warp/stubs.py +2192 -2469
  233. warp/tape.py +1162 -225
  234. warp/tests/__init__.py +1 -1
  235. warp/tests/__main__.py +4 -4
  236. warp/tests/assets/test_index_grid.nvdb +0 -0
  237. warp/tests/assets/torus.usda +105 -105
  238. warp/tests/aux_test_class_kernel.py +26 -26
  239. warp/tests/aux_test_compile_consts_dummy.py +10 -10
  240. warp/tests/aux_test_conditional_unequal_types_kernels.py +21 -21
  241. warp/tests/aux_test_dependent.py +20 -22
  242. warp/tests/aux_test_grad_customs.py +21 -23
  243. warp/tests/aux_test_reference.py +9 -11
  244. warp/tests/aux_test_reference_reference.py +8 -10
  245. warp/tests/aux_test_square.py +15 -17
  246. warp/tests/aux_test_unresolved_func.py +14 -14
  247. warp/tests/aux_test_unresolved_symbol.py +14 -14
  248. warp/tests/disabled_kinematics.py +237 -239
  249. warp/tests/run_coverage_serial.py +31 -31
  250. warp/tests/test_adam.py +155 -157
  251. warp/tests/test_arithmetic.py +1088 -1124
  252. warp/tests/test_array.py +2415 -2326
  253. warp/tests/test_array_reduce.py +148 -150
  254. warp/tests/test_async.py +666 -656
  255. warp/tests/test_atomic.py +139 -141
  256. warp/tests/test_bool.py +212 -149
  257. warp/tests/test_builtins_resolution.py +1290 -1292
  258. warp/tests/test_bvh.py +162 -171
  259. warp/tests/test_closest_point_edge_edge.py +227 -228
  260. warp/tests/test_codegen.py +562 -553
  261. warp/tests/test_compile_consts.py +217 -101
  262. warp/tests/test_conditional.py +244 -246
  263. warp/tests/test_copy.py +230 -215
  264. warp/tests/test_ctypes.py +630 -632
  265. warp/tests/test_dense.py +65 -67
  266. warp/tests/test_devices.py +89 -98
  267. warp/tests/test_dlpack.py +528 -529
  268. warp/tests/test_examples.py +403 -378
  269. warp/tests/test_fabricarray.py +952 -955
  270. warp/tests/test_fast_math.py +60 -54
  271. warp/tests/test_fem.py +1298 -1278
  272. warp/tests/test_fp16.py +128 -130
  273. warp/tests/test_func.py +336 -337
  274. warp/tests/test_generics.py +596 -571
  275. warp/tests/test_grad.py +885 -640
  276. warp/tests/test_grad_customs.py +331 -336
  277. warp/tests/test_hash_grid.py +208 -164
  278. warp/tests/test_import.py +37 -39
  279. warp/tests/test_indexedarray.py +1132 -1134
  280. warp/tests/test_intersect.py +65 -67
  281. warp/tests/test_jax.py +305 -307
  282. warp/tests/test_large.py +169 -164
  283. warp/tests/test_launch.py +352 -354
  284. warp/tests/test_lerp.py +217 -261
  285. warp/tests/test_linear_solvers.py +189 -171
  286. warp/tests/test_lvalue.py +419 -493
  287. warp/tests/test_marching_cubes.py +63 -65
  288. warp/tests/test_mat.py +1799 -1827
  289. warp/tests/test_mat_lite.py +113 -115
  290. warp/tests/test_mat_scalar_ops.py +2905 -2889
  291. warp/tests/test_math.py +124 -193
  292. warp/tests/test_matmul.py +498 -499
  293. warp/tests/test_matmul_lite.py +408 -410
  294. warp/tests/test_mempool.py +186 -190
  295. warp/tests/test_mesh.py +281 -324
  296. warp/tests/test_mesh_query_aabb.py +226 -241
  297. warp/tests/test_mesh_query_point.py +690 -702
  298. warp/tests/test_mesh_query_ray.py +290 -303
  299. warp/tests/test_mlp.py +274 -276
  300. warp/tests/test_model.py +108 -110
  301. warp/tests/test_module_hashing.py +111 -0
  302. warp/tests/test_modules_lite.py +36 -39
  303. warp/tests/test_multigpu.py +161 -163
  304. warp/tests/test_noise.py +244 -248
  305. warp/tests/test_operators.py +248 -250
  306. warp/tests/test_options.py +121 -125
  307. warp/tests/test_peer.py +131 -137
  308. warp/tests/test_pinned.py +76 -78
  309. warp/tests/test_print.py +52 -54
  310. warp/tests/test_quat.py +2084 -2086
  311. warp/tests/test_rand.py +324 -288
  312. warp/tests/test_reload.py +207 -217
  313. warp/tests/test_rounding.py +177 -179
  314. warp/tests/test_runlength_encode.py +188 -190
  315. warp/tests/test_sim_grad.py +241 -0
  316. warp/tests/test_sim_kinematics.py +89 -97
  317. warp/tests/test_smoothstep.py +166 -168
  318. warp/tests/test_snippet.py +303 -266
  319. warp/tests/test_sparse.py +466 -460
  320. warp/tests/test_spatial.py +2146 -2148
  321. warp/tests/test_special_values.py +362 -0
  322. warp/tests/test_streams.py +484 -473
  323. warp/tests/test_struct.py +708 -675
  324. warp/tests/test_tape.py +171 -148
  325. warp/tests/test_torch.py +741 -743
  326. warp/tests/test_transient_module.py +85 -87
  327. warp/tests/test_types.py +554 -659
  328. warp/tests/test_utils.py +488 -499
  329. warp/tests/test_vec.py +1262 -1268
  330. warp/tests/test_vec_lite.py +71 -73
  331. warp/tests/test_vec_scalar_ops.py +2097 -2099
  332. warp/tests/test_verify_fp.py +92 -94
  333. warp/tests/test_volume.py +961 -736
  334. warp/tests/test_volume_write.py +338 -265
  335. warp/tests/unittest_serial.py +38 -37
  336. warp/tests/unittest_suites.py +367 -359
  337. warp/tests/unittest_utils.py +434 -578
  338. warp/tests/unused_test_misc.py +69 -71
  339. warp/tests/walkthrough_debug.py +85 -85
  340. warp/thirdparty/appdirs.py +598 -598
  341. warp/thirdparty/dlpack.py +143 -143
  342. warp/thirdparty/unittest_parallel.py +563 -561
  343. warp/torch.py +321 -295
  344. warp/types.py +4941 -4450
  345. warp/utils.py +1008 -821
  346. {warp_lang-1.0.2.dist-info → warp_lang-1.2.0.dist-info}/LICENSE.md +126 -126
  347. {warp_lang-1.0.2.dist-info → warp_lang-1.2.0.dist-info}/METADATA +365 -400
  348. warp_lang-1.2.0.dist-info/RECORD +359 -0
  349. warp/examples/assets/cube.usda +0 -42
  350. warp/examples/assets/sphere.usda +0 -56
  351. warp/examples/assets/torus.usda +0 -105
  352. warp/examples/fem/example_convection_diffusion_dg0.py +0 -194
  353. warp/native/nanovdb/PNanoVDBWrite.h +0 -295
  354. warp_lang-1.0.2.dist-info/RECORD +0 -352
  355. {warp_lang-1.0.2.dist-info → warp_lang-1.2.0.dist-info}/WHEEL +0 -0
  356. {warp_lang-1.0.2.dist-info → warp_lang-1.2.0.dist-info}/top_level.txt +0 -0
warp/native/quat.h CHANGED
@@ -1,1085 +1,1112 @@
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 "mat.h"
12
-
13
- namespace wp
14
- {
15
-
16
- template<typename Type>
17
- struct quat_t
18
- {
19
- // zero constructor for adjoint variable initialization
20
- inline CUDA_CALLABLE quat_t(Type x=Type(0), Type y=Type(0), Type z=Type(0), Type w=Type(0)) : x(x), y(y), z(z), w(w) {}
21
- explicit inline CUDA_CALLABLE quat_t(const vec_t<3,Type>& v, Type w=Type(0)) : x(v[0]), y(v[1]), z(v[2]), w(w) {}
22
-
23
- template<typename OtherType>
24
- explicit inline CUDA_CALLABLE quat_t(const quat_t<OtherType>& other)
25
- {
26
- x = static_cast<Type>(other.x);
27
- y = static_cast<Type>(other.y);
28
- z = static_cast<Type>(other.z);
29
- w = static_cast<Type>(other.w);
30
- }
31
-
32
- // imaginary part
33
- Type x;
34
- Type y;
35
- Type z;
36
-
37
- // real part
38
- Type w;
39
- };
40
-
41
- using quat = quat_t<float>;
42
- using quath = quat_t<half>;
43
- using quatf = quat_t<float>;
44
- using quatd = quat_t<double>;
45
-
46
-
47
- template<typename Type>
48
- inline CUDA_CALLABLE bool operator==(const quat_t<Type>& a, const quat_t<Type>& b)
49
- {
50
- return a.x == b.x && a.y == b.y && a.z == b.z && a.w == b.w;
51
- }
52
-
53
- template<typename Type>
54
- inline bool CUDA_CALLABLE isfinite(const quat_t<Type>& q)
55
- {
56
- return isfinite(q.x) && isfinite(q.y) && isfinite(q.z) && isfinite(q.w);
57
- }
58
-
59
- template<typename Type>
60
- inline CUDA_CALLABLE quat_t<Type> atomic_add(quat_t<Type> * addr, quat_t<Type> value)
61
- {
62
- Type x = atomic_add(&(addr -> x), value.x);
63
- Type y = atomic_add(&(addr -> y), value.y);
64
- Type z = atomic_add(&(addr -> z), value.z);
65
- Type w = atomic_add(&(addr -> w), value.w);
66
-
67
- return quat_t<Type>(x, y, z, w);
68
- }
69
-
70
- template<typename Type>
71
- inline CUDA_CALLABLE void adj_quat_t(Type x, Type y, Type z, Type w, Type& adj_x, Type& adj_y, Type& adj_z, Type& adj_w, quat_t<Type> adj_ret)
72
- {
73
- adj_x += adj_ret.x;
74
- adj_y += adj_ret.y;
75
- adj_z += adj_ret.z;
76
- adj_w += adj_ret.w;
77
- }
78
-
79
- template<typename Type>
80
- inline CUDA_CALLABLE void adj_quat_t(const vec_t<3,Type>& v, Type w, vec_t<3,Type>& adj_v, Type& adj_w, quat_t<Type> adj_ret)
81
- {
82
- adj_v[0] += adj_ret.x;
83
- adj_v[1] += adj_ret.y;
84
- adj_v[2] += adj_ret.z;
85
- adj_w += adj_ret.w;
86
- }
87
-
88
- // casting constructor adjoint
89
- template<typename Type, typename OtherType>
90
- inline CUDA_CALLABLE void adj_quat_t(const quat_t<OtherType>& other, quat_t<OtherType>& adj_other, const quat_t<Type>& adj_ret)
91
- {
92
- adj_other.x += static_cast<OtherType>(adj_ret.x);
93
- adj_other.y += static_cast<OtherType>(adj_ret.y);
94
- adj_other.z += static_cast<OtherType>(adj_ret.z);
95
- adj_other.w += static_cast<OtherType>(adj_ret.w);
96
- }
97
-
98
- // forward methods
99
-
100
- template<typename Type>
101
- inline CUDA_CALLABLE quat_t<Type> quat_from_axis_angle(const vec_t<3,Type>& axis, Type angle)
102
- {
103
- Type half = angle*Type(Type(0.5));
104
- Type w = cos(half);
105
-
106
- Type sin_theta_over_two = sin(half);
107
- vec_t<3,Type> v = axis*sin_theta_over_two;
108
-
109
- return quat_t<Type>(v[0], v[1], v[2], w);
110
- }
111
-
112
- template<typename Type>
113
- inline CUDA_CALLABLE void quat_to_axis_angle(const quat_t<Type>& q, vec_t<3,Type>& axis, Type& angle)
114
- {
115
- vec_t<3,Type> v = vec_t<3,Type>(q.x, q.y, q.z);
116
- axis = q.w < Type(0) ? -normalize(v) : normalize(v);
117
- angle = Type(2) * atan2(length(v), abs(q.w));
118
- }
119
-
120
- template<typename Type>
121
- inline CUDA_CALLABLE quat_t<Type> quat_rpy(Type roll, Type pitch, Type yaw)
122
- {
123
- Type cy = cos(yaw * Type(0.5));
124
- Type sy = sin(yaw * Type(0.5));
125
- Type cr = cos(roll * Type(0.5));
126
- Type sr = sin(roll * Type(0.5));
127
- Type cp = cos(pitch * Type(0.5));
128
- Type sp = sin(pitch * Type(0.5));
129
-
130
- Type w = (cy * cr * cp + sy * sr * sp);
131
- Type x = (cy * sr * cp - sy * cr * sp);
132
- Type y = (cy * cr * sp + sy * sr * cp);
133
- Type z = (sy * cr * cp - cy * sr * sp);
134
-
135
- return quat_t<Type>(x, y, z, w);
136
- }
137
-
138
-
139
-
140
- template<typename Type>
141
- inline CUDA_CALLABLE quat_t<Type> quat_inverse(const quat_t<Type>& q)
142
- {
143
- return quat_t<Type>(-q.x, -q.y, -q.z, q.w);
144
- }
145
-
146
-
147
- template<typename Type>
148
- inline CUDA_CALLABLE Type dot(const quat_t<Type>& a, const quat_t<Type>& b)
149
- {
150
- return a.x*b.x + a.y*b.y + a.z*b.z + a.w*b.w;
151
- }
152
-
153
- template<typename Type>
154
- inline CUDA_CALLABLE Type tensordot(const quat_t<Type>& a, const quat_t<Type>& b)
155
- {
156
- // corresponds to `np.tensordot()` with all axes being contracted
157
- return dot(a, b);
158
- }
159
-
160
- template<typename Type>
161
- inline CUDA_CALLABLE Type length(const quat_t<Type>& q)
162
- {
163
- return sqrt(dot(q, q));
164
- }
165
-
166
- template<typename Type>
167
- inline CUDA_CALLABLE Type length_sq(const quat_t<Type>& q)
168
- {
169
- return dot(q, q);
170
- }
171
-
172
- template<typename Type>
173
- inline CUDA_CALLABLE quat_t<Type> normalize(const quat_t<Type>& q)
174
- {
175
- Type l = length(q);
176
- if (l > Type(kEps))
177
- {
178
- Type inv_l = Type(1)/l;
179
-
180
- return quat_t<Type>(q.x*inv_l, q.y*inv_l, q.z*inv_l, q.w*inv_l);
181
- }
182
- else
183
- {
184
- return quat_t<Type>(Type(0), Type(0), Type(0), Type(1));
185
- }
186
- }
187
-
188
- template<typename Type>
189
- inline CUDA_CALLABLE quat_t<Type> add(const quat_t<Type>& a, const quat_t<Type>& b)
190
- {
191
- return quat_t<Type>(a.x+b.x, a.y+b.y, a.z+b.z, a.w+b.w);
192
- }
193
-
194
- template<typename Type>
195
- inline CUDA_CALLABLE quat_t<Type> sub(const quat_t<Type>& a, const quat_t<Type>& b)
196
- {
197
- return quat_t<Type>(a.x-b.x, a.y-b.y, a.z-b.z, a.w-b.w);}
198
-
199
-
200
- template<typename Type>
201
- inline CUDA_CALLABLE quat_t<Type> mul(const quat_t<Type>& a, const quat_t<Type>& b)
202
- {
203
- return quat_t<Type>(a.w*b.x + b.w*a.x + a.y*b.z - b.y*a.z,
204
- a.w*b.y + b.w*a.y + a.z*b.x - b.z*a.x,
205
- a.w*b.z + b.w*a.z + a.x*b.y - b.x*a.y,
206
- a.w*b.w - a.x*b.x - a.y*b.y - a.z*b.z);
207
- }
208
-
209
- template<typename Type>
210
- inline CUDA_CALLABLE quat_t<Type> mul(const quat_t<Type>& a, Type s)
211
- {
212
- return quat_t<Type>(a.x*s, a.y*s, a.z*s, a.w*s);
213
- }
214
-
215
- template<typename Type>
216
- inline CUDA_CALLABLE quat_t<Type> mul(Type s, const quat_t<Type>& a)
217
- {
218
- return mul(a, s);
219
- }
220
-
221
- // division
222
- template<typename Type>
223
- inline CUDA_CALLABLE quat_t<Type> div(quat_t<Type> q, Type s)
224
- {
225
- return quat_t<Type>(q.x/s, q.y/s, q.z/s, q.w/s);
226
- }
227
-
228
- template<typename Type>
229
- inline CUDA_CALLABLE quat_t<Type> div(Type s, quat_t<Type> q)
230
- {
231
- return quat_t<Type>(s/q.x, s/q.y, s/q.z, s/q.w);
232
- }
233
-
234
- template<typename Type>
235
- inline CUDA_CALLABLE quat_t<Type> operator / (quat_t<Type> a, Type s)
236
- {
237
- return div(a,s);
238
- }
239
-
240
- template<typename Type>
241
- inline CUDA_CALLABLE quat_t<Type> operator / (Type s, quat_t<Type> a)
242
- {
243
- return div(s,a);
244
- }
245
-
246
- template<typename Type>
247
- inline CUDA_CALLABLE quat_t<Type> operator*(Type s, const quat_t<Type>& a)
248
- {
249
- return mul(a, s);
250
- }
251
-
252
- template<typename Type>
253
- inline CUDA_CALLABLE quat_t<Type> operator*(const quat_t<Type>& a, Type s)
254
- {
255
- return mul(a, s);
256
- }
257
-
258
- template<typename Type>
259
- inline CUDA_CALLABLE vec_t<3,Type> quat_rotate(const quat_t<Type>& q, const vec_t<3,Type>& x)
260
- {
261
- Type c = (Type(2)*q.w*q.w-Type(1));
262
- Type d = Type(2)*(q.x*x.c[0] + q.y*x.c[1] + q.z*x.c[2]);
263
- return vec_t<3,Type>(
264
- x.c[0]*c + q.x*d + (q.y * x[2] - q.z * x[1])*q.w*Type(2),
265
- x.c[1]*c + q.y*d + (q.z * x[0] - q.x * x[2])*q.w*Type(2),
266
- x.c[2]*c + q.z*d + (q.x * x[1] - q.y * x[0])*q.w*Type(2)
267
- );
268
- }
269
-
270
- template<typename Type>
271
- inline CUDA_CALLABLE vec_t<3,Type> quat_rotate_inv(const quat_t<Type>& q, const vec_t<3,Type>& x)
272
- {
273
- Type c = (Type(2)*q.w*q.w-Type(1));
274
- Type d = Type(2)*(q.x*x.c[0] + q.y*x.c[1] + q.z*x.c[2]);
275
- return vec_t<3,Type>(
276
- x.c[0]*c + q.x*d - (q.y * x[2] - q.z * x[1])*q.w*Type(2),
277
- x.c[1]*c + q.y*d - (q.z * x[0] - q.x * x[2])*q.w*Type(2),
278
- x.c[2]*c + q.z*d - (q.x * x[1] - q.y * x[0])*q.w*Type(2)
279
- );
280
- }
281
-
282
- template<typename Type>
283
- inline CUDA_CALLABLE quat_t<Type> quat_slerp(const quat_t<Type>& q0, const quat_t<Type>& q1, Type t)
284
- {
285
- vec_t<3,Type> axis;
286
- Type angle;
287
- quat_to_axis_angle(mul(quat_inverse(q0), q1), axis, angle);
288
- return mul(q0, quat_from_axis_angle(axis, t * angle));
289
- }
290
-
291
- template<typename Type>
292
- inline CUDA_CALLABLE mat_t<3,3,Type> quat_to_matrix(const quat_t<Type>& q)
293
- {
294
- vec_t<3,Type> c1 = quat_rotate(q, vec_t<3,Type>(1.0, 0.0, 0.0));
295
- vec_t<3,Type> c2 = quat_rotate(q, vec_t<3,Type>(0.0, 1.0, 0.0));
296
- vec_t<3,Type> c3 = quat_rotate(q, vec_t<3,Type>(0.0, 0.0, 1.0));
297
-
298
- return mat_t<3,3,Type>(c1, c2, c3);
299
- }
300
-
301
- template<typename Type>
302
- inline CUDA_CALLABLE quat_t<Type> quat_from_matrix(const mat_t<3,3,Type>& m)
303
- {
304
- const Type tr = m.data[0][0] + m.data[1][1] + m.data[2][2];
305
- Type x, y, z, w, h = Type(0);
306
-
307
- if (tr >= Type(0)) {
308
- h = sqrt(tr + Type(1));
309
- w = Type(0.5) * h;
310
- h = Type(0.5) / h;
311
-
312
- x = (m.data[2][1] - m.data[1][2]) * h;
313
- y = (m.data[0][2] - m.data[2][0]) * h;
314
- z = (m.data[1][0] - m.data[0][1]) * h;
315
- } else {
316
- size_t max_diag = 0;
317
- if (m.data[1][1] > m.data[0][0]) {
318
- max_diag = 1;
319
- }
320
- if (m.data[2][2] > m.data[max_diag][max_diag]) {
321
- max_diag = 2;
322
- }
323
-
324
- if (max_diag == 0) {
325
- h = sqrt((m.data[0][0] - (m.data[1][1] + m.data[2][2])) + Type(1));
326
- x = Type(0.5) * h;
327
- h = Type(0.5) / h;
328
-
329
- y = (m.data[0][1] + m.data[1][0]) * h;
330
- z = (m.data[2][0] + m.data[0][2]) * h;
331
- w = (m.data[2][1] - m.data[1][2]) * h;
332
- } else if (max_diag == 1) {
333
- h = sqrt((m.data[1][1] - (m.data[2][2] + m.data[0][0])) + Type(1));
334
- y = Type(0.5) * h;
335
- h = Type(0.5) / h;
336
-
337
- z = (m.data[1][2] + m.data[2][1]) * h;
338
- x = (m.data[0][1] + m.data[1][0]) * h;
339
- w = (m.data[0][2] - m.data[2][0]) * h;
340
- } if (max_diag == 2) {
341
- h = sqrt((m.data[2][2] - (m.data[0][0] + m.data[1][1])) + Type(1));
342
- z = Type(0.5) * h;
343
- h = Type(0.5) / h;
344
-
345
- x = (m.data[2][0] + m.data[0][2]) * h;
346
- y = (m.data[1][2] + m.data[2][1]) * h;
347
- w = (m.data[1][0] - m.data[0][1]) * h;
348
- }
349
- }
350
-
351
- return normalize(quat_t<Type>(x, y, z, w));
352
- }
353
-
354
- template<typename Type>
355
- inline CUDA_CALLABLE Type extract(const quat_t<Type>& a, int idx)
356
- {
357
- #if FP_CHECK
358
- if (idx < 0 || idx > 3)
359
- {
360
- printf("quat_t index %d out of bounds at %s %d", idx, __FILE__, __LINE__);
361
- assert(0);
362
- }
363
- #endif
364
-
365
- /*
366
- * Because quat data is not stored in an array, we index the quaternion by checking all possible idx values.
367
- * (&a.x)[idx] would be the preferred access strategy, but this results in undefined behavior in the clang compiler
368
- * at optimization level 3.
369
- */
370
- if (idx == 0) {return a.x;}
371
- else if (idx == 1) {return a.y;}
372
- else if (idx == 2) {return a.z;}
373
- else {return a.w;}
374
- }
375
-
376
- template<typename Type>
377
- CUDA_CALLABLE inline quat_t<Type> lerp(const quat_t<Type>& a, const quat_t<Type>& b, Type t)
378
- {
379
- return a*(Type(1)-t) + b*t;
380
- }
381
-
382
- template<typename Type>
383
- CUDA_CALLABLE inline void adj_lerp(const quat_t<Type>& a, const quat_t<Type>& b, Type t, quat_t<Type>& adj_a, quat_t<Type>& adj_b, Type& adj_t, const quat_t<Type>& adj_ret)
384
- {
385
- adj_a += adj_ret*(Type(1)-t);
386
- adj_b += adj_ret*t;
387
- adj_t += tensordot(b, adj_ret) - tensordot(a, adj_ret);
388
- }
389
-
390
- template<typename Type>
391
- inline CUDA_CALLABLE void adj_extract(const quat_t<Type>& a, int idx, quat_t<Type>& adj_a, int & adj_idx, Type & adj_ret)
392
- {
393
- #if FP_CHECK
394
- if (idx < 0 || idx > 3)
395
- {
396
- printf("quat_t index %d out of bounds at %s %d", idx, __FILE__, __LINE__);
397
- assert(0);
398
- }
399
- #endif
400
-
401
- // See wp::extract(const quat_t<Type>& a, int idx) note
402
- if (idx == 0) {adj_a.x += adj_ret;}
403
- else if (idx == 1) {adj_a.y += adj_ret;}
404
- else if (idx == 2) {adj_a.z += adj_ret;}
405
- else {adj_a.w += adj_ret;}
406
- }
407
-
408
-
409
- // backward methods
410
- template<typename Type>
411
- inline CUDA_CALLABLE void adj_quat_from_axis_angle(const vec_t<3,Type>& axis, Type angle, vec_t<3,Type>& adj_axis, Type& adj_angle, const quat_t<Type>& adj_ret)
412
- {
413
- vec_t<3,Type> v = vec_t<3,Type>(adj_ret.x, adj_ret.y, adj_ret.z);
414
-
415
- Type s = sin(angle*Type(0.5));
416
- Type c = cos(angle*Type(0.5));
417
-
418
- quat_t<Type> dqda = quat_t<Type>(axis[0]*c, axis[1]*c, axis[2]*c, -s)*Type(0.5);
419
-
420
- adj_axis += v*s;
421
- adj_angle += dot(dqda, adj_ret);
422
- }
423
-
424
- template<typename Type>
425
- inline CUDA_CALLABLE void adj_quat_to_axis_angle(const quat_t<Type>& q, vec_t<3,Type>& axis, Type& angle, quat_t<Type>& adj_q, const vec_t<3,Type>& adj_axis, const Type& adj_angle)
426
- {
427
- Type l = length(vec_t<3,Type>(q.x, q.y, q.z));
428
-
429
- Type ax_qx = Type(0);
430
- Type ax_qy = Type(0);
431
- Type ax_qz = Type(0);
432
- Type ay_qx = Type(0);
433
- Type ay_qy = Type(0);
434
- Type ay_qz = Type(0);
435
- Type az_qx = Type(0);
436
- Type az_qy = Type(0);
437
- Type az_qz = Type(0);
438
-
439
- Type t_qx = Type(0);
440
- Type t_qy = Type(0);
441
- Type t_qz = Type(0);
442
- Type t_qw = Type(0);
443
-
444
- Type flip = q.w < Type(0) ? -1.0 : 1.0;
445
-
446
- if (l > Type(0))
447
- {
448
- Type l_sq = l*l;
449
- Type l_inv = Type(1) / l;
450
- Type l_inv_sq = l_inv * l_inv;
451
- Type l_inv_cu = l_inv_sq * l_inv;
452
-
453
- Type C = flip * l_inv_cu;
454
- ax_qx = C * (q.y*q.y + q.z*q.z);
455
- ax_qy = -C * q.x*q.y;
456
- ax_qz = -C * q.x*q.z;
457
- ay_qx = -C * q.y*q.x;
458
- ay_qy = C * (q.x*q.x + q.z*q.z);
459
- ay_qz = -C * q.y*q.z;
460
- az_qx = -C * q.z*q.x;
461
- az_qy = -C * q.z*q.y;
462
- az_qz = C * (q.x*q.x + q.y*q.y);
463
-
464
- Type D = Type(2) * flip / (l_sq + q.w*q.w);
465
- t_qx = D * l_inv * q.x * q.w;
466
- t_qy = D * l_inv * q.y * q.w;
467
- t_qz = D * l_inv * q.z * q.w;
468
- t_qw = -D * l;
469
- }
470
- else
471
- {
472
- if (abs(q.w) > Type(kEps))
473
- {
474
- Type t_qx = Type(2) / (sqrt(Type(3)) * abs(q.w));
475
- Type t_qy = Type(2) / (sqrt(Type(3)) * abs(q.w));
476
- Type t_qz = Type(2) / (sqrt(Type(3)) * abs(q.w));
477
- }
478
- // o/w we have a null quat_t which cannot backpropagate
479
- }
480
-
481
- adj_q.x += ax_qx * adj_axis[0] + ay_qx * adj_axis[1] + az_qx * adj_axis[2] + t_qx * adj_angle;
482
- adj_q.y += ax_qy * adj_axis[0] + ay_qy * adj_axis[1] + az_qy * adj_axis[2] + t_qy * adj_angle;
483
- adj_q.z += ax_qz * adj_axis[0] + ay_qz * adj_axis[1] + az_qz * adj_axis[2] + t_qz * adj_angle;
484
- adj_q.w += t_qw * adj_angle;
485
- }
486
-
487
- template<typename Type>
488
- inline CUDA_CALLABLE void adj_quat_rpy(Type roll, Type pitch, Type yaw, Type& adj_roll, Type& adj_pitch, Type& adj_yaw, const quat_t<Type>& adj_ret)
489
- {
490
- Type cy = cos(yaw * Type(0.5));
491
- Type sy = sin(yaw * Type(0.5));
492
- Type cr = cos(roll * Type(0.5));
493
- Type sr = sin(roll * Type(0.5));
494
- Type cp = cos(pitch * Type(0.5));
495
- Type sp = sin(pitch * Type(0.5));
496
-
497
- Type w = (cy * cr * cp + sy * sr * sp);
498
- Type x = (cy * sr * cp - sy * cr * sp);
499
- Type y = (cy * cr * sp + sy * sr * cp);
500
- Type z = (sy * cr * cp - cy * sr * sp);
501
-
502
- Type dx_dr = Type(0.5) * w;
503
- Type dx_dp = -Type(0.5) * cy * sr * sp - Type(0.5) * sy * cr * cp;
504
- Type dx_dy = -Type(0.5) * y;
505
-
506
- Type dy_dr = Type(0.5) * z;
507
- Type dy_dp = Type(0.5) * cy * cr * cp - Type(0.5) * sy * sr * sp;
508
- Type dy_dy = Type(0.5) * x;
509
-
510
- Type dz_dr = -Type(0.5) * y;
511
- Type dz_dp = -Type(0.5) * sy * cr * sp - Type(0.5) * cy * sr * cp;
512
- Type dz_dy = Type(0.5) * w;
513
-
514
- Type dw_dr = -Type(0.5) * x;
515
- Type dw_dp = -Type(0.5) * cy * cr * sp + Type(0.5) * sy * sr * cp;
516
- Type dw_dy = -Type(0.5) * z;
517
-
518
- adj_roll += dot(quat_t<Type>(dx_dr, dy_dr, dz_dr, dw_dr), adj_ret);
519
- adj_pitch += dot(quat_t<Type>(dx_dp, dy_dp, dz_dp, dw_dp), adj_ret);
520
- adj_yaw += dot(quat_t<Type>(dx_dy, dy_dy, dz_dy, dw_dy), adj_ret);
521
- }
522
-
523
-
524
- template<typename Type>
525
- inline CUDA_CALLABLE void adj_dot(const quat_t<Type>& a, const quat_t<Type>& b, quat_t<Type>& adj_a, quat_t<Type>& adj_b, const Type adj_ret)
526
- {
527
- adj_a += b*adj_ret;
528
- adj_b += a*adj_ret;
529
- }
530
-
531
- template<typename Type>
532
- inline CUDA_CALLABLE void tensordot(const quat_t<Type>& a, const quat_t<Type>& b, quat_t<Type>& adj_a, quat_t<Type>& adj_b, const Type adj_ret)
533
- {
534
- adj_dot(a, b, adj_a, adj_b, adj_ret);
535
- }
536
-
537
- template<typename Type>
538
- inline CUDA_CALLABLE void adj_length(const quat_t<Type>& a, Type ret, quat_t<Type>& adj_a, const Type adj_ret)
539
- {
540
- if (ret > Type(kEps))
541
- {
542
- Type inv_l = Type(1)/ret;
543
-
544
- adj_a += quat_t<Type>(a.x*inv_l, a.y*inv_l, a.z*inv_l, a.w*inv_l) * adj_ret;
545
- }
546
- }
547
-
548
- template<typename Type>
549
- inline CUDA_CALLABLE void adj_length_sq(const quat_t<Type>& a, quat_t<Type>& adj_a, const Type adj_ret)
550
- {
551
- adj_a += Type(2)*a*adj_ret;
552
- }
553
-
554
- template<typename Type>
555
- inline CUDA_CALLABLE void adj_normalize(const quat_t<Type>& q, quat_t<Type>& adj_q, const quat_t<Type>& adj_ret)
556
- {
557
- Type l = length(q);
558
-
559
- if (l > Type(kEps))
560
- {
561
- Type l_inv = Type(1)/l;
562
-
563
- adj_q += adj_ret*l_inv - q*(l_inv*l_inv*l_inv*dot(q, adj_ret));
564
- }
565
- }
566
-
567
- template<typename Type>
568
- inline CUDA_CALLABLE void adj_quat_inverse(const quat_t<Type>& q, quat_t<Type>& adj_q, const quat_t<Type>& adj_ret)
569
- {
570
- adj_q.x -= adj_ret.x;
571
- adj_q.y -= adj_ret.y;
572
- adj_q.z -= adj_ret.z;
573
- adj_q.w += adj_ret.w;
574
- }
575
-
576
- template<typename Type>
577
- inline CUDA_CALLABLE void adj_add(const quat_t<Type>& a, const quat_t<Type>& b, quat_t<Type>& adj_a, quat_t<Type>& adj_b, const quat_t<Type>& adj_ret)
578
- {
579
- adj_a += adj_ret;
580
- adj_b += adj_ret;
581
- }
582
-
583
- template<typename Type>
584
- inline CUDA_CALLABLE void adj_sub(const quat_t<Type>& a, const quat_t<Type>& b, quat_t<Type>& adj_a, quat_t<Type>& adj_b, const quat_t<Type>& adj_ret)
585
- {
586
- adj_a += adj_ret;
587
- adj_b -= adj_ret;
588
- }
589
-
590
- template<typename Type>
591
- inline CUDA_CALLABLE void adj_mul(const quat_t<Type>& a, const quat_t<Type>& b, quat_t<Type>& adj_a, quat_t<Type>& adj_b, const quat_t<Type>& adj_ret)
592
- {
593
- // shorthand
594
- const quat_t<Type>& r = adj_ret;
595
-
596
- adj_a += quat_t<Type>(b.w*r.x - b.x*r.w + b.y*r.z - b.z*r.y,
597
- b.w*r.y - b.y*r.w - b.x*r.z + b.z*r.x,
598
- b.w*r.z + b.x*r.y - b.y*r.x - b.z*r.w,
599
- b.w*r.w + b.x*r.x + b.y*r.y + b.z*r.z);
600
-
601
- adj_b += quat_t<Type>(a.w*r.x - a.x*r.w - a.y*r.z + a.z*r.y,
602
- a.w*r.y - a.y*r.w + a.x*r.z - a.z*r.x,
603
- a.w*r.z - a.x*r.y + a.y*r.x - a.z*r.w,
604
- a.w*r.w + a.x*r.x + a.y*r.y + a.z*r.z);
605
-
606
- }
607
-
608
- template<typename Type>
609
- inline CUDA_CALLABLE void adj_mul(const quat_t<Type>& a, Type s, quat_t<Type>& adj_a, Type& adj_s, const quat_t<Type>& adj_ret)
610
- {
611
- adj_a += adj_ret*s;
612
- adj_s += dot(a, adj_ret);
613
- }
614
-
615
- template<typename Type>
616
- inline CUDA_CALLABLE void adj_mul(Type s, const quat_t<Type>& a, Type& adj_s, quat_t<Type>& adj_a, const quat_t<Type>& adj_ret)
617
- {
618
- adj_mul(a, s, adj_a, adj_s, adj_ret);
619
- }
620
-
621
- template<typename Type>
622
- inline CUDA_CALLABLE void adj_div(quat_t<Type> a, Type s, quat_t<Type>& adj_a, Type& adj_s, const quat_t<Type>& adj_ret)
623
- {
624
- adj_s -= dot(a, adj_ret)/ (s * s); // - a / s^2
625
- adj_a += adj_ret / s;
626
- }
627
-
628
- template<typename Type>
629
- inline CUDA_CALLABLE void adj_div(Type s, quat_t<Type> a, Type& adj_s, quat_t<Type>& adj_a, const quat_t<Type>& adj_ret)
630
- {
631
- adj_s -= dot(a, adj_ret)/ (s * s); // - a / s^2
632
- adj_a += s / adj_ret;
633
- }
634
-
635
- template<typename Type>
636
- inline CUDA_CALLABLE void adj_quat_rotate(const quat_t<Type>& q, const vec_t<3,Type>& p, quat_t<Type>& adj_q, vec_t<3,Type>& adj_p, const vec_t<3,Type>& adj_ret)
637
- {
638
-
639
- {
640
- Type t2 = p[2]*q.z*Type(2);
641
- Type t3 = p[1]*q.w*Type(2);
642
- Type t4 = p[0]*q.w*Type(2);
643
- Type t5 = p[0]*q.x*Type(2);
644
- Type t6 = p[1]*q.y*Type(2);
645
- Type t7 = p[2]*q.y*Type(2);
646
- Type t8 = p[0]*q.z*Type(2);
647
- Type t9 = p[0]*q.y*Type(2);
648
- Type t10 = p[1]*q.x*Type(2);
649
- adj_q.x += adj_ret[2]*(t3+t8)+adj_ret[0]*(t2+t6+p[0]*q.x*Type(4))+adj_ret[1]*(t9-p[2]*q.w*Type(2));
650
- adj_q.y += adj_ret[1]*(t2+t5+p[1]*q.y*Type(4))+adj_ret[0]*(t10+p[2]*q.w*Type(2))-adj_ret[2]*(t4-p[1]*q.z*Type(2));
651
- adj_q.z += adj_ret[1]*(t4+t7)+adj_ret[2]*(t5+t6+p[2]*q.z*Type(4))-adj_ret[0]*(t3-p[2]*q.x*Type(2));
652
- adj_q.w += adj_ret[0]*(t7+p[0]*q.w*Type(4)-p[1]*q.z*Type(2))+adj_ret[1]*(t8+p[1]*q.w*Type(4)-p[2]*q.x*Type(2))+adj_ret[2]*(-t9+t10+p[2]*q.w*Type(4));
653
- }
654
-
655
- {
656
- Type t2 = q.w*q.w;
657
- Type t3 = t2*Type(2);
658
- Type t4 = q.w*q.z*Type(2);
659
- Type t5 = q.x*q.y*Type(2);
660
- Type t6 = q.w*q.y*Type(2);
661
- Type t7 = q.w*q.x*Type(2);
662
- Type t8 = q.y*q.z*Type(2);
663
- adj_p[0] += adj_ret[1]*(t4+t5)+adj_ret[0]*(t3+(q.x*q.x)*Type(2)-Type(1))-adj_ret[2]*(t6-q.x*q.z*Type(2));
664
- adj_p[1] += adj_ret[2]*(t7+t8)-adj_ret[0]*(t4-t5)+adj_ret[1]*(t3+(q.y*q.y)*Type(2)-Type(1));
665
- adj_p[2] += -adj_ret[1]*(t7-t8)+adj_ret[2]*(t3+(q.z*q.z)*Type(2)-Type(1))+adj_ret[0]*(t6+q.x*q.z*Type(2));
666
- }
667
- }
668
-
669
- template<typename Type>
670
- inline CUDA_CALLABLE void adj_quat_rotate_inv(const quat_t<Type>& q, const vec_t<3,Type>& p, quat_t<Type>& adj_q, vec_t<3,Type>& adj_p, const vec_t<3,Type>& adj_ret)
671
- {
672
- const vec_t<3,Type>& r = adj_ret;
673
-
674
- {
675
- Type t2 = p[2]*q.w*Type(2);
676
- Type t3 = p[2]*q.z*Type(2);
677
- Type t4 = p[1]*q.w*Type(2);
678
- Type t5 = p[0]*q.w*Type(2);
679
- Type t6 = p[0]*q.x*Type(2);
680
- Type t7 = p[1]*q.y*Type(2);
681
- Type t8 = p[1]*q.z*Type(2);
682
- Type t9 = p[2]*q.x*Type(2);
683
- Type t10 = p[0]*q.y*Type(2);
684
- adj_q.x += r[1]*(t2+t10)+r[0]*(t3+t7+p[0]*q.x*Type(4))-r[2]*(t4-p[0]*q.z*Type(2));
685
- adj_q.y += r[2]*(t5+t8)+r[1]*(t3+t6+p[1]*q.y*Type(4))-r[0]*(t2-p[1]*q.x*Type(2));
686
- adj_q.z += r[0]*(t4+t9)+r[2]*(t6+t7+p[2]*q.z*Type(4))-r[1]*(t5-p[2]*q.y*Type(2));
687
- adj_q.w += r[0]*(t8+p[0]*q.w*Type(4)-p[2]*q.y*Type(2))+r[1]*(t9+p[1]*q.w*Type(4)-p[0]*q.z*Type(2))+r[2]*(t10-p[1]*q.x*Type(2)+p[2]*q.w*Type(4));
688
- }
689
-
690
- {
691
- Type t2 = q.w*q.w;
692
- Type t3 = t2*Type(2);
693
- Type t4 = q.w*q.z*Type(2);
694
- Type t5 = q.w*q.y*Type(2);
695
- Type t6 = q.x*q.z*Type(2);
696
- Type t7 = q.w*q.x*Type(2);
697
- adj_p[0] += r[2]*(t5+t6)+r[0]*(t3+(q.x*q.x)*Type(2)-Type(1))-r[1]*(t4-q.x*q.y*Type(2));
698
- adj_p[1] += r[1]*(t3+(q.y*q.y)*Type(2)-Type(1))+r[0]*(t4+q.x*q.y*Type(2))-r[2]*(t7-q.y*q.z*Type(2));
699
- adj_p[2] += -r[0]*(t5-t6)+r[2]*(t3+(q.z*q.z)*Type(2)-Type(1))+r[1]*(t7+q.y*q.z*Type(2));
700
- }
701
- }
702
-
703
- template<typename Type>
704
- inline CUDA_CALLABLE void adj_quat_slerp(const quat_t<Type>& q0, const quat_t<Type>& q1, Type t, quat_t<Type>& ret, quat_t<Type>& adj_q0, quat_t<Type>& adj_q1, Type& adj_t, const quat_t<Type>& adj_ret)
705
- {
706
- vec_t<3,Type> axis;
707
- Type angle;
708
- quat_t<Type> q0_inv = quat_inverse(q0);
709
- quat_t<Type> q_inc = mul(q0_inv, q1);
710
- quat_to_axis_angle(q_inc, axis, angle);
711
- quat_t<Type> qt = quat_from_axis_angle(axis, angle * t);
712
- angle = angle * 0.5;
713
-
714
- // adj_t
715
- adj_t += dot(mul(ret, quat_t<Type>(angle*axis[0], angle*axis[1], angle*axis[2], Type(0))), adj_ret);
716
-
717
- // adj_q0
718
- quat_t<Type> q_inc_x_q0;
719
- quat_t<Type> q_inc_y_q0;
720
- quat_t<Type> q_inc_z_q0;
721
- quat_t<Type> q_inc_w_q0;
722
-
723
- quat_t<Type> q_inc_x_q1;
724
- quat_t<Type> q_inc_y_q1;
725
- quat_t<Type> q_inc_z_q1;
726
- quat_t<Type> q_inc_w_q1;
727
-
728
- adj_mul(q0_inv, q1, q_inc_x_q0, q_inc_x_q1, quat_t<Type>(1.f, Type(0), Type(0), Type(0)));
729
- adj_mul(q0_inv, q1, q_inc_y_q0, q_inc_y_q1, quat_t<Type>(Type(0), 1.f, Type(0), Type(0)));
730
- adj_mul(q0_inv, q1, q_inc_z_q0, q_inc_z_q1, quat_t<Type>(Type(0), Type(0), 1.f, Type(0)));
731
- adj_mul(q0_inv, q1, q_inc_w_q0, q_inc_w_q1, quat_t<Type>(Type(0), Type(0), Type(0), 1.f));
732
-
733
- quat_t<Type> a_x_q_inc;
734
- quat_t<Type> a_y_q_inc;
735
- quat_t<Type> a_z_q_inc;
736
- quat_t<Type> t_q_inc;
737
-
738
- adj_quat_to_axis_angle(q_inc, axis, angle, a_x_q_inc, vec_t<3,Type>(1.f, Type(0), Type(0)), Type(0));
739
- adj_quat_to_axis_angle(q_inc, axis, angle, a_y_q_inc, vec_t<3,Type>(Type(0), 1.f, Type(0)), Type(0));
740
- adj_quat_to_axis_angle(q_inc, axis, angle, a_z_q_inc, vec_t<3,Type>(Type(0), Type(0), 1.f), Type(0));
741
- adj_quat_to_axis_angle(q_inc, axis, angle, t_q_inc, vec_t<3,Type>(Type(0), Type(0), Type(0)), Type(1));
742
-
743
- Type cs = cos(angle*t);
744
- Type sn = sin(angle*t);
745
-
746
- quat_t<Type> q_inc_q0_x = quat_t<Type>(-q_inc_x_q0.x, -q_inc_y_q0.x, -q_inc_z_q0.x, -q_inc_w_q0.x);
747
- quat_t<Type> q_inc_q0_y = quat_t<Type>(-q_inc_x_q0.y, -q_inc_y_q0.y, -q_inc_z_q0.y, -q_inc_w_q0.y);
748
- quat_t<Type> q_inc_q0_z = quat_t<Type>(-q_inc_x_q0.z, -q_inc_y_q0.z, -q_inc_z_q0.z, -q_inc_w_q0.z);
749
- quat_t<Type> q_inc_q0_w = quat_t<Type>(q_inc_x_q0.w, q_inc_y_q0.w, q_inc_z_q0.w, q_inc_w_q0.w);
750
-
751
- Type a_x_q0_x = dot(a_x_q_inc, q_inc_q0_x);
752
- Type a_x_q0_y = dot(a_x_q_inc, q_inc_q0_y);
753
- Type a_x_q0_z = dot(a_x_q_inc, q_inc_q0_z);
754
- Type a_x_q0_w = dot(a_x_q_inc, q_inc_q0_w);
755
- Type a_y_q0_x = dot(a_y_q_inc, q_inc_q0_x);
756
- Type a_y_q0_y = dot(a_y_q_inc, q_inc_q0_y);
757
- Type a_y_q0_z = dot(a_y_q_inc, q_inc_q0_z);
758
- Type a_y_q0_w = dot(a_y_q_inc, q_inc_q0_w);
759
- Type a_z_q0_x = dot(a_z_q_inc, q_inc_q0_x);
760
- Type a_z_q0_y = dot(a_z_q_inc, q_inc_q0_y);
761
- Type a_z_q0_z = dot(a_z_q_inc, q_inc_q0_z);
762
- Type a_z_q0_w = dot(a_z_q_inc, q_inc_q0_w);
763
- Type t_q0_x = dot(t_q_inc, q_inc_q0_x);
764
- Type t_q0_y = dot(t_q_inc, q_inc_q0_y);
765
- Type t_q0_z = dot(t_q_inc, q_inc_q0_z);
766
- Type t_q0_w = dot(t_q_inc, q_inc_q0_w);
767
-
768
- quat_t<Type> q_s_q0_x = mul(quat_t<Type>(1.f, Type(0), Type(0), Type(0)), qt) + mul(q0, quat_t<Type>(
769
- 0.5 * t * axis[0] * t_q0_x * cs + a_x_q0_x * sn,
770
- 0.5 * t * axis[1] * t_q0_x * cs + a_y_q0_x * sn,
771
- 0.5 * t * axis[2] * t_q0_x * cs + a_z_q0_x * sn,
772
- -0.5 * t * t_q0_x * sn));
773
-
774
- quat_t<Type> q_s_q0_y = mul(quat_t<Type>(Type(0), 1.f, Type(0), Type(0)), qt) + mul(q0, quat_t<Type>(
775
- 0.5 * t * axis[0] * t_q0_y * cs + a_x_q0_y * sn,
776
- 0.5 * t * axis[1] * t_q0_y * cs + a_y_q0_y * sn,
777
- 0.5 * t * axis[2] * t_q0_y * cs + a_z_q0_y * sn,
778
- -0.5 * t * t_q0_y * sn));
779
-
780
- quat_t<Type> q_s_q0_z = mul(quat_t<Type>(Type(0), Type(0), 1.f, Type(0)), qt) + mul(q0, quat_t<Type>(
781
- 0.5 * t * axis[0] * t_q0_z * cs + a_x_q0_z * sn,
782
- 0.5 * t * axis[1] * t_q0_z * cs + a_y_q0_z * sn,
783
- 0.5 * t * axis[2] * t_q0_z * cs + a_z_q0_z * sn,
784
- -0.5 * t * t_q0_z * sn));
785
-
786
- quat_t<Type> q_s_q0_w = mul(quat_t<Type>(Type(0), Type(0), Type(0), 1.f), qt) + mul(q0, quat_t<Type>(
787
- 0.5 * t * axis[0] * t_q0_w * cs + a_x_q0_w * sn,
788
- 0.5 * t * axis[1] * t_q0_w * cs + a_y_q0_w * sn,
789
- 0.5 * t * axis[2] * t_q0_w * cs + a_z_q0_w * sn,
790
- -0.5 * t * t_q0_w * sn));
791
-
792
- adj_q0.x += dot(q_s_q0_x, adj_ret);
793
- adj_q0.y += dot(q_s_q0_y, adj_ret);
794
- adj_q0.z += dot(q_s_q0_z, adj_ret);
795
- adj_q0.w += dot(q_s_q0_w, adj_ret);
796
-
797
- // adj_q1
798
- quat_t<Type> q_inc_q1_x = quat_t<Type>(q_inc_x_q1.x, q_inc_y_q1.x, q_inc_z_q1.x, q_inc_w_q1.x);
799
- quat_t<Type> q_inc_q1_y = quat_t<Type>(q_inc_x_q1.y, q_inc_y_q1.y, q_inc_z_q1.y, q_inc_w_q1.y);
800
- quat_t<Type> q_inc_q1_z = quat_t<Type>(q_inc_x_q1.z, q_inc_y_q1.z, q_inc_z_q1.z, q_inc_w_q1.z);
801
- quat_t<Type> q_inc_q1_w = quat_t<Type>(q_inc_x_q1.w, q_inc_y_q1.w, q_inc_z_q1.w, q_inc_w_q1.w);
802
-
803
- Type a_x_q1_x = dot(a_x_q_inc, q_inc_q1_x);
804
- Type a_x_q1_y = dot(a_x_q_inc, q_inc_q1_y);
805
- Type a_x_q1_z = dot(a_x_q_inc, q_inc_q1_z);
806
- Type a_x_q1_w = dot(a_x_q_inc, q_inc_q1_w);
807
- Type a_y_q1_x = dot(a_y_q_inc, q_inc_q1_x);
808
- Type a_y_q1_y = dot(a_y_q_inc, q_inc_q1_y);
809
- Type a_y_q1_z = dot(a_y_q_inc, q_inc_q1_z);
810
- Type a_y_q1_w = dot(a_y_q_inc, q_inc_q1_w);
811
- Type a_z_q1_x = dot(a_z_q_inc, q_inc_q1_x);
812
- Type a_z_q1_y = dot(a_z_q_inc, q_inc_q1_y);
813
- Type a_z_q1_z = dot(a_z_q_inc, q_inc_q1_z);
814
- Type a_z_q1_w = dot(a_z_q_inc, q_inc_q1_w);
815
- Type t_q1_x = dot(t_q_inc, q_inc_q1_x);
816
- Type t_q1_y = dot(t_q_inc, q_inc_q1_y);
817
- Type t_q1_z = dot(t_q_inc, q_inc_q1_z);
818
- Type t_q1_w = dot(t_q_inc, q_inc_q1_w);
819
-
820
- quat_t<Type> q_s_q1_x = mul(q0, quat_t<Type>(
821
- 0.5 * t * axis[0] * t_q1_x * cs + a_x_q1_x * sn,
822
- 0.5 * t * axis[1] * t_q1_x * cs + a_y_q1_x * sn,
823
- 0.5 * t * axis[2] * t_q1_x * cs + a_z_q1_x * sn,
824
- -0.5 * t * t_q1_x * sn));
825
-
826
- quat_t<Type> q_s_q1_y = mul(q0, quat_t<Type>(
827
- 0.5 * t * axis[0] * t_q1_y * cs + a_x_q1_y * sn,
828
- 0.5 * t * axis[1] * t_q1_y * cs + a_y_q1_y * sn,
829
- 0.5 * t * axis[2] * t_q1_y * cs + a_z_q1_y * sn,
830
- -0.5 * t * t_q1_y * sn));
831
-
832
- quat_t<Type> q_s_q1_z = mul(q0, quat_t<Type>(
833
- 0.5 * t * axis[0] * t_q1_z * cs + a_x_q1_z * sn,
834
- 0.5 * t * axis[1] * t_q1_z * cs + a_y_q1_z * sn,
835
- 0.5 * t * axis[2] * t_q1_z * cs + a_z_q1_z * sn,
836
- -0.5 * t * t_q1_z * sn));
837
-
838
- quat_t<Type> q_s_q1_w = mul(q0, quat_t<Type>(
839
- 0.5 * t * axis[0] * t_q1_w * cs + a_x_q1_w * sn,
840
- 0.5 * t * axis[1] * t_q1_w * cs + a_y_q1_w * sn,
841
- 0.5 * t * axis[2] * t_q1_w * cs + a_z_q1_w * sn,
842
- -0.5 * t * t_q1_w * sn));
843
-
844
- adj_q1.x += dot(q_s_q1_x, adj_ret);
845
- adj_q1.y += dot(q_s_q1_y, adj_ret);
846
- adj_q1.z += dot(q_s_q1_z, adj_ret);
847
- adj_q1.w += dot(q_s_q1_w, adj_ret);
848
-
849
- }
850
-
851
- template<typename Type>
852
- inline CUDA_CALLABLE void adj_quat_to_matrix(const quat_t<Type>& q, quat_t<Type>& adj_q, mat_t<3,3,Type>& adj_ret)
853
- {
854
- // we don't care about adjoint w.r.t. constant identity matrix
855
- vec_t<3,Type> t;
856
-
857
- adj_quat_rotate(q, vec_t<3,Type>(1.0, 0.0, 0.0), adj_q, t, adj_ret.get_col(0));
858
- adj_quat_rotate(q, vec_t<3,Type>(0.0, 1.0, 0.0), adj_q, t, adj_ret.get_col(1));
859
- adj_quat_rotate(q, vec_t<3,Type>(0.0, 0.0, 1.0), adj_q, t, adj_ret.get_col(2));
860
- }
861
-
862
- template<typename Type>
863
- inline CUDA_CALLABLE void adj_quat_from_matrix(const mat_t<3,3,Type>& m, mat_t<3,3,Type>& adj_m, const quat_t<Type>& adj_ret)
864
- {
865
- const Type tr = m.data[0][0] + m.data[1][1] + m.data[2][2];
866
- Type x, y, z, w, h = Type(0);
867
-
868
- Type dx_dm00 = Type(0), dx_dm01 = Type(0), dx_dm02 = Type(0);
869
- Type dx_dm10 = Type(0), dx_dm11 = Type(0), dx_dm12 = Type(0);
870
- Type dx_dm20 = Type(0), dx_dm21 = Type(0), dx_dm22 = Type(0);
871
- Type dy_dm00 = Type(0), dy_dm01 = Type(0), dy_dm02 = Type(0);
872
- Type dy_dm10 = Type(0), dy_dm11 = Type(0), dy_dm12 = Type(0);
873
- Type dy_dm20 = Type(0), dy_dm21 = Type(0), dy_dm22 = Type(0);
874
- Type dz_dm00 = Type(0), dz_dm01 = Type(0), dz_dm02 = Type(0);
875
- Type dz_dm10 = Type(0), dz_dm11 = Type(0), dz_dm12 = Type(0);
876
- Type dz_dm20 = Type(0), dz_dm21 = Type(0), dz_dm22 = Type(0);
877
- Type dw_dm00 = Type(0), dw_dm01 = Type(0), dw_dm02 = Type(0);
878
- Type dw_dm10 = Type(0), dw_dm11 = Type(0), dw_dm12 = Type(0);
879
- Type dw_dm20 = Type(0), dw_dm21 = Type(0), dw_dm22 = Type(0);
880
-
881
- if (tr >= Type(0)) {
882
- h = sqrt(tr + Type(1));
883
- w = Type(0.5) * h;
884
- h = Type(0.5) / h;
885
-
886
- x = (m.data[2][1] - m.data[1][2]) * h;
887
- y = (m.data[0][2] - m.data[2][0]) * h;
888
- z = (m.data[1][0] - m.data[0][1]) * h;
889
-
890
- dw_dm00 = Type(0.5) * h;
891
- dw_dm11 = Type(0.5) * h;
892
- dw_dm22 = Type(0.5) * h;
893
- dx_dm21 = h;
894
- dx_dm12 = -h;
895
- dx_dm00 = Type(2) * h*h*h * (m.data[1][2] - m.data[2][1]);
896
- dx_dm11 = Type(2) * h*h*h * (m.data[1][2] - m.data[2][1]);
897
- dx_dm22 = Type(2) * h*h*h * (m.data[1][2] - m.data[2][1]);
898
- dy_dm02 = h;
899
- dy_dm20 = -h;
900
- dy_dm00 = Type(2) * h*h*h * (m.data[2][0] - m.data[0][2]);
901
- dy_dm11 = Type(2) * h*h*h * (m.data[2][0] - m.data[0][2]);
902
- dy_dm22 = Type(2) * h*h*h * (m.data[2][0] - m.data[0][2]);
903
- dz_dm10 = h;
904
- dz_dm01 = -h;
905
- dz_dm00 = Type(2) * h*h*h * (m.data[0][1] - m.data[1][0]);
906
- dz_dm11 = Type(2) * h*h*h * (m.data[0][1] - m.data[1][0]);
907
- dz_dm22 = Type(2) * h*h*h * (m.data[0][1] - m.data[1][0]);
908
- } else {
909
- size_t max_diag = 0;
910
- if (m.data[1][1] > m.data[0][0]) {
911
- max_diag = 1;
912
- }
913
- if (m.data[2][2] > m.data[max_diag][max_diag]) {
914
- max_diag = 2;
915
- }
916
-
917
- if (max_diag == 0) {
918
- h = sqrt((m.data[0][0] - (m.data[1][1] + m.data[2][2])) + Type(1));
919
- x = Type(0.5) * h;
920
- h = Type(0.5) / h;
921
-
922
- y = (m.data[0][1] + m.data[1][0]) * h;
923
- z = (m.data[2][0] + m.data[0][2]) * h;
924
- w = (m.data[2][1] - m.data[1][2]) * h;
925
-
926
- dx_dm00 = Type(0.5) * h;
927
- dx_dm11 = -Type(0.5) * h;
928
- dx_dm22 = -Type(0.5) * h;
929
- dy_dm01 = h;
930
- dy_dm10 = h;
931
- dy_dm00 = -Type(2) * h*h*h * (m.data[0][1] + m.data[1][0]);
932
- dy_dm11 = Type(2) * h*h*h * (m.data[0][1] + m.data[1][0]);
933
- dy_dm22 = Type(2) * h*h*h * (m.data[0][1] + m.data[1][0]);
934
- dz_dm20 = h;
935
- dz_dm02 = h;
936
- dz_dm00 = -Type(2) * h*h*h * (m.data[2][0] + m.data[0][2]);
937
- dz_dm11 = Type(2) * h*h*h * (m.data[2][0] + m.data[0][2]);
938
- dz_dm22 = Type(2) * h*h*h * (m.data[2][0] + m.data[0][2]);
939
- dw_dm21 = h;
940
- dw_dm12 = -h;
941
- dw_dm00 = Type(2) * h*h*h * (m.data[1][2] - m.data[2][1]);
942
- dw_dm11 = Type(2) * h*h*h * (m.data[2][1] - m.data[1][2]);
943
- dw_dm22 = Type(2) * h*h*h * (m.data[2][1] - m.data[1][2]);
944
- } else if (max_diag == 1) {
945
- h = sqrt((m.data[1][1] - (m.data[2][2] + m.data[0][0])) + Type(1));
946
- y = Type(0.5) * h;
947
- h = Type(0.5) / h;
948
-
949
- z = (m.data[1][2] + m.data[2][1]) * h;
950
- x = (m.data[0][1] + m.data[1][0]) * h;
951
- w = (m.data[0][2] - m.data[2][0]) * h;
952
-
953
- dy_dm00 = -Type(0.5) * h;
954
- dy_dm11 = Type(0.5) * h;
955
- dy_dm22 = -Type(0.5) * h;
956
- dz_dm12 = h;
957
- dz_dm21 = h;
958
- dz_dm00 = Type(2) * h*h*h * (m.data[1][2] + m.data[2][1]);
959
- dz_dm11 = -Type(2) * h*h*h * (m.data[1][2] + m.data[2][1]);
960
- dz_dm22 = Type(2) * h*h*h * (m.data[1][2] + m.data[2][1]);
961
- dx_dm01 = h;
962
- dx_dm10 = h;
963
- dx_dm00 = Type(2) * h*h*h * (m.data[0][1] + m.data[1][0]);
964
- dx_dm11 = -Type(2) * h*h*h * (m.data[0][1] + m.data[1][0]);
965
- dx_dm22 = Type(2) * h*h*h * (m.data[0][1] + m.data[1][0]);
966
- dw_dm02 = h;
967
- dw_dm20 = -h;
968
- dw_dm00 = Type(2) * h*h*h * (m.data[0][2] - m.data[2][0]);
969
- dw_dm11 = Type(2) * h*h*h * (m.data[2][0] - m.data[0][2]);
970
- dw_dm22 = Type(2) * h*h*h * (m.data[0][2] - m.data[2][0]);
971
- } if (max_diag == 2) {
972
- h = sqrt((m.data[2][2] - (m.data[0][0] + m.data[1][1])) + Type(1));
973
- z = Type(0.5) * h;
974
- h = Type(0.5) / h;
975
-
976
- x = (m.data[2][0] + m.data[0][2]) * h;
977
- y = (m.data[1][2] + m.data[2][1]) * h;
978
- w = (m.data[1][0] - m.data[0][1]) * h;
979
-
980
- dz_dm00 = -Type(0.5) * h;
981
- dz_dm11 = -Type(0.5) * h;
982
- dz_dm22 = Type(0.5) * h;
983
- dx_dm20 = h;
984
- dx_dm02 = h;
985
- dx_dm00 = Type(2) * h*h*h * (m.data[2][0] + m.data[0][2]);
986
- dx_dm11 = Type(2) * h*h*h * (m.data[2][0] + m.data[0][2]);
987
- dx_dm22 = -Type(2) * h*h*h * (m.data[2][0] + m.data[0][2]);
988
- dy_dm12 = h;
989
- dy_dm21 = h;
990
- dy_dm00 = Type(2) * h*h*h * (m.data[1][2] + m.data[2][1]);
991
- dy_dm11 = Type(2) * h*h*h * (m.data[1][2] + m.data[2][1]);
992
- dy_dm22 = -Type(2) * h*h*h * (m.data[1][2] + m.data[2][1]);
993
- dw_dm10 = h;
994
- dw_dm01 = -h;
995
- dw_dm00 = Type(2) * h*h*h * (m.data[1][0] - m.data[0][1]);
996
- dw_dm11 = Type(2) * h*h*h * (m.data[1][0] - m.data[0][1]);
997
- dw_dm22 = Type(2) * h*h*h * (m.data[0][1] - m.data[1][0]);
998
- }
999
- }
1000
-
1001
- quat_t<Type> dq_dm00 = quat_t<Type>(dx_dm00, dy_dm00, dz_dm00, dw_dm00);
1002
- quat_t<Type> dq_dm01 = quat_t<Type>(dx_dm01, dy_dm01, dz_dm01, dw_dm01);
1003
- quat_t<Type> dq_dm02 = quat_t<Type>(dx_dm02, dy_dm02, dz_dm02, dw_dm02);
1004
- quat_t<Type> dq_dm10 = quat_t<Type>(dx_dm10, dy_dm10, dz_dm10, dw_dm10);
1005
- quat_t<Type> dq_dm11 = quat_t<Type>(dx_dm11, dy_dm11, dz_dm11, dw_dm11);
1006
- quat_t<Type> dq_dm12 = quat_t<Type>(dx_dm12, dy_dm12, dz_dm12, dw_dm12);
1007
- quat_t<Type> dq_dm20 = quat_t<Type>(dx_dm20, dy_dm20, dz_dm20, dw_dm20);
1008
- quat_t<Type> dq_dm21 = quat_t<Type>(dx_dm21, dy_dm21, dz_dm21, dw_dm21);
1009
- quat_t<Type> dq_dm22 = quat_t<Type>(dx_dm22, dy_dm22, dz_dm22, dw_dm22);
1010
-
1011
- quat_t<Type> adj_q;
1012
- adj_normalize(quat_t<Type>(x, y, z, w), adj_q, adj_ret);
1013
-
1014
- adj_m.data[0][0] += dot(dq_dm00, adj_q);
1015
- adj_m.data[0][1] += dot(dq_dm01, adj_q);
1016
- adj_m.data[0][2] += dot(dq_dm02, adj_q);
1017
- adj_m.data[1][0] += dot(dq_dm10, adj_q);
1018
- adj_m.data[1][1] += dot(dq_dm11, adj_q);
1019
- adj_m.data[1][2] += dot(dq_dm12, adj_q);
1020
- adj_m.data[2][0] += dot(dq_dm20, adj_q);
1021
- adj_m.data[2][1] += dot(dq_dm21, adj_q);
1022
- adj_m.data[2][2] += dot(dq_dm22, adj_q);
1023
- }
1024
-
1025
- template<typename Type>
1026
- inline CUDA_CALLABLE void adj_mat_t(const vec_t<3,Type>& pos, const quat_t<Type>& rot, const vec_t<3,Type>& scale,
1027
- vec_t<3,Type>& adj_pos, quat_t<Type>& adj_rot, vec_t<3,Type>& adj_scale, const mat_t<4,4,Type>& adj_ret)
1028
- {
1029
- mat_t<3,3,Type> R = quat_to_matrix(rot);
1030
- mat_t<3,3,Type> adj_R(0);
1031
-
1032
- adj_pos[0] += adj_ret.data[0][3];
1033
- adj_pos[1] += adj_ret.data[1][3];
1034
- adj_pos[2] += adj_ret.data[2][3];
1035
-
1036
- adj_mul(R.data[0][0], scale[0], adj_R.data[0][0], adj_scale[0], adj_ret.data[0][0]);
1037
- adj_mul(R.data[1][0], scale[0], adj_R.data[1][0], adj_scale[0], adj_ret.data[1][0]);
1038
- adj_mul(R.data[2][0], scale[0], adj_R.data[2][0], adj_scale[0], adj_ret.data[2][0]);
1039
-
1040
- adj_mul(R.data[0][1], scale[1], adj_R.data[0][1], adj_scale[1], adj_ret.data[0][1]);
1041
- adj_mul(R.data[1][1], scale[1], adj_R.data[1][1], adj_scale[1], adj_ret.data[1][1]);
1042
- adj_mul(R.data[2][1], scale[1], adj_R.data[2][1], adj_scale[1], adj_ret.data[2][1]);
1043
-
1044
- adj_mul(R.data[0][2], scale[2], adj_R.data[0][2], adj_scale[2], adj_ret.data[0][2]);
1045
- adj_mul(R.data[1][2], scale[2], adj_R.data[1][2], adj_scale[2], adj_ret.data[1][2]);
1046
- adj_mul(R.data[2][2], scale[2], adj_R.data[2][2], adj_scale[2], adj_ret.data[2][2]);
1047
-
1048
- adj_quat_to_matrix(rot, adj_rot, adj_R);
1049
- }
1050
-
1051
- template<unsigned Rows, unsigned Cols, typename Type>
1052
- inline CUDA_CALLABLE mat_t<Rows,Cols,Type>::mat_t(const vec_t<3,Type>& pos, const quat_t<Type>& rot, const vec_t<3,Type>& scale)
1053
- {
1054
- mat_t<3,3,Type> R = quat_to_matrix(rot);
1055
-
1056
- data[0][0] = R.data[0][0]*scale[0];
1057
- data[1][0] = R.data[1][0]*scale[0];
1058
- data[2][0] = R.data[2][0]*scale[0];
1059
- data[3][0] = Type(0);
1060
-
1061
- data[0][1] = R.data[0][1]*scale[1];
1062
- data[1][1] = R.data[1][1]*scale[1];
1063
- data[2][1] = R.data[2][1]*scale[1];
1064
- data[3][1] = Type(0);
1065
-
1066
- data[0][2] = R.data[0][2]*scale[2];
1067
- data[1][2] = R.data[1][2]*scale[2];
1068
- data[2][2] = R.data[2][2]*scale[2];
1069
- data[3][2] = Type(0);
1070
-
1071
- data[0][3] = pos[0];
1072
- data[1][3] = pos[1];
1073
- data[2][3] = pos[2];
1074
- data[3][3] = Type(1);
1075
- }
1076
-
1077
- template<typename Type=float32>
1078
- inline CUDA_CALLABLE quat_t<Type> quat_identity()
1079
- {
1080
- return quat_t<Type>(Type(0), Type(0), Type(0), Type(1));
1081
- }
1082
-
1083
-
1084
-
1085
- } // namespace wp
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 "mat.h"
12
+
13
+ namespace wp
14
+ {
15
+
16
+ template<typename Type>
17
+ struct quat_t
18
+ {
19
+ // zero constructor for adjoint variable initialization
20
+ inline CUDA_CALLABLE quat_t(Type x=Type(0), Type y=Type(0), Type z=Type(0), Type w=Type(0)) : x(x), y(y), z(z), w(w) {}
21
+ explicit inline CUDA_CALLABLE quat_t(const vec_t<3,Type>& v, Type w=Type(0)) : x(v[0]), y(v[1]), z(v[2]), w(w) {}
22
+
23
+ template<typename OtherType>
24
+ explicit inline CUDA_CALLABLE quat_t(const quat_t<OtherType>& other)
25
+ {
26
+ x = static_cast<Type>(other.x);
27
+ y = static_cast<Type>(other.y);
28
+ z = static_cast<Type>(other.z);
29
+ w = static_cast<Type>(other.w);
30
+ }
31
+
32
+ // imaginary part
33
+ Type x;
34
+ Type y;
35
+ Type z;
36
+
37
+ // real part
38
+ Type w;
39
+ };
40
+
41
+ using quat = quat_t<float>;
42
+ using quath = quat_t<half>;
43
+ using quatf = quat_t<float>;
44
+ using quatd = quat_t<double>;
45
+
46
+
47
+ template<typename Type>
48
+ inline CUDA_CALLABLE bool operator==(const quat_t<Type>& a, const quat_t<Type>& b)
49
+ {
50
+ return a.x == b.x && a.y == b.y && a.z == b.z && a.w == b.w;
51
+ }
52
+
53
+ template<typename Type>
54
+ inline bool CUDA_CALLABLE isfinite(const quat_t<Type>& q)
55
+ {
56
+ return isfinite(q.x) && isfinite(q.y) && isfinite(q.z) && isfinite(q.w);
57
+ }
58
+
59
+ template<typename Type>
60
+ inline void CUDA_CALLABLE adj_isfinite(const quat_t<Type>& q, quat_t<Type>& adj_q, const bool &adj_ret)
61
+ {
62
+ }
63
+
64
+ template<typename Type>
65
+ inline bool CUDA_CALLABLE isnan(const quat_t<Type>& q)
66
+ {
67
+ return isnan(q.x) || isnan(q.y) || isnan(q.z) || isnan(q.w);
68
+ }
69
+
70
+ template<typename Type>
71
+ inline void CUDA_CALLABLE adj_isnan(const quat_t<Type>& q, quat_t<Type>& adj_q, const bool &adj_ret)
72
+ {
73
+ }
74
+
75
+ template<typename Type>
76
+ inline bool CUDA_CALLABLE isinf(const quat_t<Type>& q)
77
+ {
78
+ return isinf(q.x) || isinf(q.y) || isinf(q.z) || isinf(q.w);
79
+ }
80
+
81
+ template<typename Type>
82
+ inline void CUDA_CALLABLE adj_isinf(const quat_t<Type>& q, quat_t<Type>& adj_q, const bool &adj_ret)
83
+ {
84
+ }
85
+
86
+ template<typename Type>
87
+ inline CUDA_CALLABLE quat_t<Type> atomic_add(quat_t<Type> * addr, quat_t<Type> value)
88
+ {
89
+ Type x = atomic_add(&(addr -> x), value.x);
90
+ Type y = atomic_add(&(addr -> y), value.y);
91
+ Type z = atomic_add(&(addr -> z), value.z);
92
+ Type w = atomic_add(&(addr -> w), value.w);
93
+
94
+ return quat_t<Type>(x, y, z, w);
95
+ }
96
+
97
+ template<typename Type>
98
+ inline CUDA_CALLABLE void adj_quat_t(Type x, Type y, Type z, Type w, Type& adj_x, Type& adj_y, Type& adj_z, Type& adj_w, quat_t<Type> adj_ret)
99
+ {
100
+ adj_x += adj_ret.x;
101
+ adj_y += adj_ret.y;
102
+ adj_z += adj_ret.z;
103
+ adj_w += adj_ret.w;
104
+ }
105
+
106
+ template<typename Type>
107
+ inline CUDA_CALLABLE void adj_quat_t(const vec_t<3,Type>& v, Type w, vec_t<3,Type>& adj_v, Type& adj_w, quat_t<Type> adj_ret)
108
+ {
109
+ adj_v[0] += adj_ret.x;
110
+ adj_v[1] += adj_ret.y;
111
+ adj_v[2] += adj_ret.z;
112
+ adj_w += adj_ret.w;
113
+ }
114
+
115
+ // casting constructor adjoint
116
+ template<typename Type, typename OtherType>
117
+ inline CUDA_CALLABLE void adj_quat_t(const quat_t<OtherType>& other, quat_t<OtherType>& adj_other, const quat_t<Type>& adj_ret)
118
+ {
119
+ adj_other.x += static_cast<OtherType>(adj_ret.x);
120
+ adj_other.y += static_cast<OtherType>(adj_ret.y);
121
+ adj_other.z += static_cast<OtherType>(adj_ret.z);
122
+ adj_other.w += static_cast<OtherType>(adj_ret.w);
123
+ }
124
+
125
+ // forward methods
126
+
127
+ template<typename Type>
128
+ inline CUDA_CALLABLE quat_t<Type> quat_from_axis_angle(const vec_t<3,Type>& axis, Type angle)
129
+ {
130
+ Type half = angle*Type(Type(0.5));
131
+ Type w = cos(half);
132
+
133
+ Type sin_theta_over_two = sin(half);
134
+ vec_t<3,Type> v = axis*sin_theta_over_two;
135
+
136
+ return quat_t<Type>(v[0], v[1], v[2], w);
137
+ }
138
+
139
+ template<typename Type>
140
+ inline CUDA_CALLABLE void quat_to_axis_angle(const quat_t<Type>& q, vec_t<3,Type>& axis, Type& angle)
141
+ {
142
+ vec_t<3,Type> v = vec_t<3,Type>(q.x, q.y, q.z);
143
+ axis = q.w < Type(0) ? -normalize(v) : normalize(v);
144
+ angle = Type(2) * atan2(length(v), abs(q.w));
145
+ }
146
+
147
+ template<typename Type>
148
+ inline CUDA_CALLABLE quat_t<Type> quat_rpy(Type roll, Type pitch, Type yaw)
149
+ {
150
+ Type cy = cos(yaw * Type(0.5));
151
+ Type sy = sin(yaw * Type(0.5));
152
+ Type cr = cos(roll * Type(0.5));
153
+ Type sr = sin(roll * Type(0.5));
154
+ Type cp = cos(pitch * Type(0.5));
155
+ Type sp = sin(pitch * Type(0.5));
156
+
157
+ Type w = (cy * cr * cp + sy * sr * sp);
158
+ Type x = (cy * sr * cp - sy * cr * sp);
159
+ Type y = (cy * cr * sp + sy * sr * cp);
160
+ Type z = (sy * cr * cp - cy * sr * sp);
161
+
162
+ return quat_t<Type>(x, y, z, w);
163
+ }
164
+
165
+
166
+
167
+ template<typename Type>
168
+ inline CUDA_CALLABLE quat_t<Type> quat_inverse(const quat_t<Type>& q)
169
+ {
170
+ return quat_t<Type>(-q.x, -q.y, -q.z, q.w);
171
+ }
172
+
173
+
174
+ template<typename Type>
175
+ inline CUDA_CALLABLE Type dot(const quat_t<Type>& a, const quat_t<Type>& b)
176
+ {
177
+ return a.x*b.x + a.y*b.y + a.z*b.z + a.w*b.w;
178
+ }
179
+
180
+ template<typename Type>
181
+ inline CUDA_CALLABLE Type tensordot(const quat_t<Type>& a, const quat_t<Type>& b)
182
+ {
183
+ // corresponds to `np.tensordot()` with all axes being contracted
184
+ return dot(a, b);
185
+ }
186
+
187
+ template<typename Type>
188
+ inline CUDA_CALLABLE Type length(const quat_t<Type>& q)
189
+ {
190
+ return sqrt(dot(q, q));
191
+ }
192
+
193
+ template<typename Type>
194
+ inline CUDA_CALLABLE Type length_sq(const quat_t<Type>& q)
195
+ {
196
+ return dot(q, q);
197
+ }
198
+
199
+ template<typename Type>
200
+ inline CUDA_CALLABLE quat_t<Type> normalize(const quat_t<Type>& q)
201
+ {
202
+ Type l = length(q);
203
+ if (l > Type(kEps))
204
+ {
205
+ Type inv_l = Type(1)/l;
206
+
207
+ return quat_t<Type>(q.x*inv_l, q.y*inv_l, q.z*inv_l, q.w*inv_l);
208
+ }
209
+ else
210
+ {
211
+ return quat_t<Type>(Type(0), Type(0), Type(0), Type(1));
212
+ }
213
+ }
214
+
215
+ template<typename Type>
216
+ inline CUDA_CALLABLE quat_t<Type> add(const quat_t<Type>& a, const quat_t<Type>& b)
217
+ {
218
+ return quat_t<Type>(a.x+b.x, a.y+b.y, a.z+b.z, a.w+b.w);
219
+ }
220
+
221
+ template<typename Type>
222
+ inline CUDA_CALLABLE quat_t<Type> sub(const quat_t<Type>& a, const quat_t<Type>& b)
223
+ {
224
+ return quat_t<Type>(a.x-b.x, a.y-b.y, a.z-b.z, a.w-b.w);}
225
+
226
+
227
+ template<typename Type>
228
+ inline CUDA_CALLABLE quat_t<Type> mul(const quat_t<Type>& a, const quat_t<Type>& b)
229
+ {
230
+ return quat_t<Type>(a.w*b.x + b.w*a.x + a.y*b.z - b.y*a.z,
231
+ a.w*b.y + b.w*a.y + a.z*b.x - b.z*a.x,
232
+ a.w*b.z + b.w*a.z + a.x*b.y - b.x*a.y,
233
+ a.w*b.w - a.x*b.x - a.y*b.y - a.z*b.z);
234
+ }
235
+
236
+ template<typename Type>
237
+ inline CUDA_CALLABLE quat_t<Type> mul(const quat_t<Type>& a, Type s)
238
+ {
239
+ return quat_t<Type>(a.x*s, a.y*s, a.z*s, a.w*s);
240
+ }
241
+
242
+ template<typename Type>
243
+ inline CUDA_CALLABLE quat_t<Type> mul(Type s, const quat_t<Type>& a)
244
+ {
245
+ return mul(a, s);
246
+ }
247
+
248
+ // division
249
+ template<typename Type>
250
+ inline CUDA_CALLABLE quat_t<Type> div(quat_t<Type> q, Type s)
251
+ {
252
+ return quat_t<Type>(q.x/s, q.y/s, q.z/s, q.w/s);
253
+ }
254
+
255
+ template<typename Type>
256
+ inline CUDA_CALLABLE quat_t<Type> div(Type s, quat_t<Type> q)
257
+ {
258
+ return quat_t<Type>(s/q.x, s/q.y, s/q.z, s/q.w);
259
+ }
260
+
261
+ template<typename Type>
262
+ inline CUDA_CALLABLE quat_t<Type> operator / (quat_t<Type> a, Type s)
263
+ {
264
+ return div(a,s);
265
+ }
266
+
267
+ template<typename Type>
268
+ inline CUDA_CALLABLE quat_t<Type> operator / (Type s, quat_t<Type> a)
269
+ {
270
+ return div(s,a);
271
+ }
272
+
273
+ template<typename Type>
274
+ inline CUDA_CALLABLE quat_t<Type> operator*(Type s, const quat_t<Type>& a)
275
+ {
276
+ return mul(a, s);
277
+ }
278
+
279
+ template<typename Type>
280
+ inline CUDA_CALLABLE quat_t<Type> operator*(const quat_t<Type>& a, Type s)
281
+ {
282
+ return mul(a, s);
283
+ }
284
+
285
+ template<typename Type>
286
+ inline CUDA_CALLABLE vec_t<3,Type> quat_rotate(const quat_t<Type>& q, const vec_t<3,Type>& x)
287
+ {
288
+ Type c = (Type(2)*q.w*q.w-Type(1));
289
+ Type d = Type(2)*(q.x*x.c[0] + q.y*x.c[1] + q.z*x.c[2]);
290
+ return vec_t<3,Type>(
291
+ x.c[0]*c + q.x*d + (q.y * x[2] - q.z * x[1])*q.w*Type(2),
292
+ x.c[1]*c + q.y*d + (q.z * x[0] - q.x * x[2])*q.w*Type(2),
293
+ x.c[2]*c + q.z*d + (q.x * x[1] - q.y * x[0])*q.w*Type(2)
294
+ );
295
+ }
296
+
297
+ template<typename Type>
298
+ inline CUDA_CALLABLE vec_t<3,Type> quat_rotate_inv(const quat_t<Type>& q, const vec_t<3,Type>& x)
299
+ {
300
+ Type c = (Type(2)*q.w*q.w-Type(1));
301
+ Type d = Type(2)*(q.x*x.c[0] + q.y*x.c[1] + q.z*x.c[2]);
302
+ return vec_t<3,Type>(
303
+ x.c[0]*c + q.x*d - (q.y * x[2] - q.z * x[1])*q.w*Type(2),
304
+ x.c[1]*c + q.y*d - (q.z * x[0] - q.x * x[2])*q.w*Type(2),
305
+ x.c[2]*c + q.z*d - (q.x * x[1] - q.y * x[0])*q.w*Type(2)
306
+ );
307
+ }
308
+
309
+ template<typename Type>
310
+ inline CUDA_CALLABLE quat_t<Type> quat_slerp(const quat_t<Type>& q0, const quat_t<Type>& q1, Type t)
311
+ {
312
+ vec_t<3,Type> axis;
313
+ Type angle;
314
+ quat_to_axis_angle(mul(quat_inverse(q0), q1), axis, angle);
315
+ return mul(q0, quat_from_axis_angle(axis, t * angle));
316
+ }
317
+
318
+ template<typename Type>
319
+ inline CUDA_CALLABLE mat_t<3,3,Type> quat_to_matrix(const quat_t<Type>& q)
320
+ {
321
+ vec_t<3,Type> c1 = quat_rotate(q, vec_t<3,Type>(1.0, 0.0, 0.0));
322
+ vec_t<3,Type> c2 = quat_rotate(q, vec_t<3,Type>(0.0, 1.0, 0.0));
323
+ vec_t<3,Type> c3 = quat_rotate(q, vec_t<3,Type>(0.0, 0.0, 1.0));
324
+
325
+ return mat_t<3,3,Type>(c1, c2, c3);
326
+ }
327
+
328
+ template<typename Type>
329
+ inline CUDA_CALLABLE quat_t<Type> quat_from_matrix(const mat_t<3,3,Type>& m)
330
+ {
331
+ const Type tr = m.data[0][0] + m.data[1][1] + m.data[2][2];
332
+ Type x, y, z, w, h = Type(0);
333
+
334
+ if (tr >= Type(0)) {
335
+ h = sqrt(tr + Type(1));
336
+ w = Type(0.5) * h;
337
+ h = Type(0.5) / h;
338
+
339
+ x = (m.data[2][1] - m.data[1][2]) * h;
340
+ y = (m.data[0][2] - m.data[2][0]) * h;
341
+ z = (m.data[1][0] - m.data[0][1]) * h;
342
+ } else {
343
+ size_t max_diag = 0;
344
+ if (m.data[1][1] > m.data[0][0]) {
345
+ max_diag = 1;
346
+ }
347
+ if (m.data[2][2] > m.data[max_diag][max_diag]) {
348
+ max_diag = 2;
349
+ }
350
+
351
+ if (max_diag == 0) {
352
+ h = sqrt((m.data[0][0] - (m.data[1][1] + m.data[2][2])) + Type(1));
353
+ x = Type(0.5) * h;
354
+ h = Type(0.5) / h;
355
+
356
+ y = (m.data[0][1] + m.data[1][0]) * h;
357
+ z = (m.data[2][0] + m.data[0][2]) * h;
358
+ w = (m.data[2][1] - m.data[1][2]) * h;
359
+ } else if (max_diag == 1) {
360
+ h = sqrt((m.data[1][1] - (m.data[2][2] + m.data[0][0])) + Type(1));
361
+ y = Type(0.5) * h;
362
+ h = Type(0.5) / h;
363
+
364
+ z = (m.data[1][2] + m.data[2][1]) * h;
365
+ x = (m.data[0][1] + m.data[1][0]) * h;
366
+ w = (m.data[0][2] - m.data[2][0]) * h;
367
+ } if (max_diag == 2) {
368
+ h = sqrt((m.data[2][2] - (m.data[0][0] + m.data[1][1])) + Type(1));
369
+ z = Type(0.5) * h;
370
+ h = Type(0.5) / h;
371
+
372
+ x = (m.data[2][0] + m.data[0][2]) * h;
373
+ y = (m.data[1][2] + m.data[2][1]) * h;
374
+ w = (m.data[1][0] - m.data[0][1]) * h;
375
+ }
376
+ }
377
+
378
+ return normalize(quat_t<Type>(x, y, z, w));
379
+ }
380
+
381
+ template<typename Type>
382
+ inline CUDA_CALLABLE Type extract(const quat_t<Type>& a, int idx)
383
+ {
384
+ #if FP_CHECK
385
+ if (idx < 0 || idx > 3)
386
+ {
387
+ printf("quat_t index %d out of bounds at %s %d", idx, __FILE__, __LINE__);
388
+ assert(0);
389
+ }
390
+ #endif
391
+
392
+ /*
393
+ * Because quat data is not stored in an array, we index the quaternion by checking all possible idx values.
394
+ * (&a.x)[idx] would be the preferred access strategy, but this results in undefined behavior in the clang compiler
395
+ * at optimization level 3.
396
+ */
397
+ if (idx == 0) {return a.x;}
398
+ else if (idx == 1) {return a.y;}
399
+ else if (idx == 2) {return a.z;}
400
+ else {return a.w;}
401
+ }
402
+
403
+ template<typename Type>
404
+ CUDA_CALLABLE inline quat_t<Type> lerp(const quat_t<Type>& a, const quat_t<Type>& b, Type t)
405
+ {
406
+ return a*(Type(1)-t) + b*t;
407
+ }
408
+
409
+ template<typename Type>
410
+ CUDA_CALLABLE inline void adj_lerp(const quat_t<Type>& a, const quat_t<Type>& b, Type t, quat_t<Type>& adj_a, quat_t<Type>& adj_b, Type& adj_t, const quat_t<Type>& adj_ret)
411
+ {
412
+ adj_a += adj_ret*(Type(1)-t);
413
+ adj_b += adj_ret*t;
414
+ adj_t += tensordot(b, adj_ret) - tensordot(a, adj_ret);
415
+ }
416
+
417
+ template<typename Type>
418
+ inline CUDA_CALLABLE void adj_extract(const quat_t<Type>& a, int idx, quat_t<Type>& adj_a, int & adj_idx, Type & adj_ret)
419
+ {
420
+ #if FP_CHECK
421
+ if (idx < 0 || idx > 3)
422
+ {
423
+ printf("quat_t index %d out of bounds at %s %d", idx, __FILE__, __LINE__);
424
+ assert(0);
425
+ }
426
+ #endif
427
+
428
+ // See wp::extract(const quat_t<Type>& a, int idx) note
429
+ if (idx == 0) {adj_a.x += adj_ret;}
430
+ else if (idx == 1) {adj_a.y += adj_ret;}
431
+ else if (idx == 2) {adj_a.z += adj_ret;}
432
+ else {adj_a.w += adj_ret;}
433
+ }
434
+
435
+
436
+ // backward methods
437
+ template<typename Type>
438
+ inline CUDA_CALLABLE void adj_quat_from_axis_angle(const vec_t<3,Type>& axis, Type angle, vec_t<3,Type>& adj_axis, Type& adj_angle, const quat_t<Type>& adj_ret)
439
+ {
440
+ vec_t<3,Type> v = vec_t<3,Type>(adj_ret.x, adj_ret.y, adj_ret.z);
441
+
442
+ Type s = sin(angle*Type(0.5));
443
+ Type c = cos(angle*Type(0.5));
444
+
445
+ quat_t<Type> dqda = quat_t<Type>(axis[0]*c, axis[1]*c, axis[2]*c, -s)*Type(0.5);
446
+
447
+ adj_axis += v*s;
448
+ adj_angle += dot(dqda, adj_ret);
449
+ }
450
+
451
+ template<typename Type>
452
+ inline CUDA_CALLABLE void adj_quat_to_axis_angle(const quat_t<Type>& q, vec_t<3,Type>& axis, Type& angle, quat_t<Type>& adj_q, const vec_t<3,Type>& adj_axis, const Type& adj_angle)
453
+ {
454
+ Type l = length(vec_t<3,Type>(q.x, q.y, q.z));
455
+
456
+ Type ax_qx = Type(0);
457
+ Type ax_qy = Type(0);
458
+ Type ax_qz = Type(0);
459
+ Type ay_qx = Type(0);
460
+ Type ay_qy = Type(0);
461
+ Type ay_qz = Type(0);
462
+ Type az_qx = Type(0);
463
+ Type az_qy = Type(0);
464
+ Type az_qz = Type(0);
465
+
466
+ Type t_qx = Type(0);
467
+ Type t_qy = Type(0);
468
+ Type t_qz = Type(0);
469
+ Type t_qw = Type(0);
470
+
471
+ Type flip = q.w < Type(0) ? -1.0 : 1.0;
472
+
473
+ if (l > Type(0))
474
+ {
475
+ Type l_sq = l*l;
476
+ Type l_inv = Type(1) / l;
477
+ Type l_inv_sq = l_inv * l_inv;
478
+ Type l_inv_cu = l_inv_sq * l_inv;
479
+
480
+ Type C = flip * l_inv_cu;
481
+ ax_qx = C * (q.y*q.y + q.z*q.z);
482
+ ax_qy = -C * q.x*q.y;
483
+ ax_qz = -C * q.x*q.z;
484
+ ay_qx = -C * q.y*q.x;
485
+ ay_qy = C * (q.x*q.x + q.z*q.z);
486
+ ay_qz = -C * q.y*q.z;
487
+ az_qx = -C * q.z*q.x;
488
+ az_qy = -C * q.z*q.y;
489
+ az_qz = C * (q.x*q.x + q.y*q.y);
490
+
491
+ Type D = Type(2) * flip / (l_sq + q.w*q.w);
492
+ t_qx = D * l_inv * q.x * q.w;
493
+ t_qy = D * l_inv * q.y * q.w;
494
+ t_qz = D * l_inv * q.z * q.w;
495
+ t_qw = -D * l;
496
+ }
497
+ else
498
+ {
499
+ if (abs(q.w) > Type(kEps))
500
+ {
501
+ Type t_qx = Type(2) / (sqrt(Type(3)) * abs(q.w));
502
+ Type t_qy = Type(2) / (sqrt(Type(3)) * abs(q.w));
503
+ Type t_qz = Type(2) / (sqrt(Type(3)) * abs(q.w));
504
+ }
505
+ // o/w we have a null quat_t which cannot backpropagate
506
+ }
507
+
508
+ adj_q.x += ax_qx * adj_axis[0] + ay_qx * adj_axis[1] + az_qx * adj_axis[2] + t_qx * adj_angle;
509
+ adj_q.y += ax_qy * adj_axis[0] + ay_qy * adj_axis[1] + az_qy * adj_axis[2] + t_qy * adj_angle;
510
+ adj_q.z += ax_qz * adj_axis[0] + ay_qz * adj_axis[1] + az_qz * adj_axis[2] + t_qz * adj_angle;
511
+ adj_q.w += t_qw * adj_angle;
512
+ }
513
+
514
+ template<typename Type>
515
+ inline CUDA_CALLABLE void adj_quat_rpy(Type roll, Type pitch, Type yaw, Type& adj_roll, Type& adj_pitch, Type& adj_yaw, const quat_t<Type>& adj_ret)
516
+ {
517
+ Type cy = cos(yaw * Type(0.5));
518
+ Type sy = sin(yaw * Type(0.5));
519
+ Type cr = cos(roll * Type(0.5));
520
+ Type sr = sin(roll * Type(0.5));
521
+ Type cp = cos(pitch * Type(0.5));
522
+ Type sp = sin(pitch * Type(0.5));
523
+
524
+ Type w = (cy * cr * cp + sy * sr * sp);
525
+ Type x = (cy * sr * cp - sy * cr * sp);
526
+ Type y = (cy * cr * sp + sy * sr * cp);
527
+ Type z = (sy * cr * cp - cy * sr * sp);
528
+
529
+ Type dx_dr = Type(0.5) * w;
530
+ Type dx_dp = -Type(0.5) * cy * sr * sp - Type(0.5) * sy * cr * cp;
531
+ Type dx_dy = -Type(0.5) * y;
532
+
533
+ Type dy_dr = Type(0.5) * z;
534
+ Type dy_dp = Type(0.5) * cy * cr * cp - Type(0.5) * sy * sr * sp;
535
+ Type dy_dy = Type(0.5) * x;
536
+
537
+ Type dz_dr = -Type(0.5) * y;
538
+ Type dz_dp = -Type(0.5) * sy * cr * sp - Type(0.5) * cy * sr * cp;
539
+ Type dz_dy = Type(0.5) * w;
540
+
541
+ Type dw_dr = -Type(0.5) * x;
542
+ Type dw_dp = -Type(0.5) * cy * cr * sp + Type(0.5) * sy * sr * cp;
543
+ Type dw_dy = -Type(0.5) * z;
544
+
545
+ adj_roll += dot(quat_t<Type>(dx_dr, dy_dr, dz_dr, dw_dr), adj_ret);
546
+ adj_pitch += dot(quat_t<Type>(dx_dp, dy_dp, dz_dp, dw_dp), adj_ret);
547
+ adj_yaw += dot(quat_t<Type>(dx_dy, dy_dy, dz_dy, dw_dy), adj_ret);
548
+ }
549
+
550
+
551
+ template<typename Type>
552
+ inline CUDA_CALLABLE void adj_dot(const quat_t<Type>& a, const quat_t<Type>& b, quat_t<Type>& adj_a, quat_t<Type>& adj_b, const Type adj_ret)
553
+ {
554
+ adj_a += b*adj_ret;
555
+ adj_b += a*adj_ret;
556
+ }
557
+
558
+ template<typename Type>
559
+ inline CUDA_CALLABLE void tensordot(const quat_t<Type>& a, const quat_t<Type>& b, quat_t<Type>& adj_a, quat_t<Type>& adj_b, const Type adj_ret)
560
+ {
561
+ adj_dot(a, b, adj_a, adj_b, adj_ret);
562
+ }
563
+
564
+ template<typename Type>
565
+ inline CUDA_CALLABLE void adj_length(const quat_t<Type>& a, Type ret, quat_t<Type>& adj_a, const Type adj_ret)
566
+ {
567
+ if (ret > Type(kEps))
568
+ {
569
+ Type inv_l = Type(1)/ret;
570
+
571
+ adj_a += quat_t<Type>(a.x*inv_l, a.y*inv_l, a.z*inv_l, a.w*inv_l) * adj_ret;
572
+ }
573
+ }
574
+
575
+ template<typename Type>
576
+ inline CUDA_CALLABLE void adj_length_sq(const quat_t<Type>& a, quat_t<Type>& adj_a, const Type adj_ret)
577
+ {
578
+ adj_a += Type(2)*a*adj_ret;
579
+ }
580
+
581
+ template<typename Type>
582
+ inline CUDA_CALLABLE void adj_normalize(const quat_t<Type>& q, quat_t<Type>& adj_q, const quat_t<Type>& adj_ret)
583
+ {
584
+ Type l = length(q);
585
+
586
+ if (l > Type(kEps))
587
+ {
588
+ Type l_inv = Type(1)/l;
589
+
590
+ adj_q += adj_ret*l_inv - q*(l_inv*l_inv*l_inv*dot(q, adj_ret));
591
+ }
592
+ }
593
+
594
+ template<typename Type>
595
+ inline CUDA_CALLABLE void adj_quat_inverse(const quat_t<Type>& q, quat_t<Type>& adj_q, const quat_t<Type>& adj_ret)
596
+ {
597
+ adj_q.x -= adj_ret.x;
598
+ adj_q.y -= adj_ret.y;
599
+ adj_q.z -= adj_ret.z;
600
+ adj_q.w += adj_ret.w;
601
+ }
602
+
603
+ template<typename Type>
604
+ inline CUDA_CALLABLE void adj_add(const quat_t<Type>& a, const quat_t<Type>& b, quat_t<Type>& adj_a, quat_t<Type>& adj_b, const quat_t<Type>& adj_ret)
605
+ {
606
+ adj_a += adj_ret;
607
+ adj_b += adj_ret;
608
+ }
609
+
610
+ template<typename Type>
611
+ inline CUDA_CALLABLE void adj_sub(const quat_t<Type>& a, const quat_t<Type>& b, quat_t<Type>& adj_a, quat_t<Type>& adj_b, const quat_t<Type>& adj_ret)
612
+ {
613
+ adj_a += adj_ret;
614
+ adj_b -= adj_ret;
615
+ }
616
+
617
+ template<typename Type>
618
+ inline CUDA_CALLABLE void adj_mul(const quat_t<Type>& a, const quat_t<Type>& b, quat_t<Type>& adj_a, quat_t<Type>& adj_b, const quat_t<Type>& adj_ret)
619
+ {
620
+ // shorthand
621
+ const quat_t<Type>& r = adj_ret;
622
+
623
+ adj_a += quat_t<Type>(b.w*r.x - b.x*r.w + b.y*r.z - b.z*r.y,
624
+ b.w*r.y - b.y*r.w - b.x*r.z + b.z*r.x,
625
+ b.w*r.z + b.x*r.y - b.y*r.x - b.z*r.w,
626
+ b.w*r.w + b.x*r.x + b.y*r.y + b.z*r.z);
627
+
628
+ adj_b += quat_t<Type>(a.w*r.x - a.x*r.w - a.y*r.z + a.z*r.y,
629
+ a.w*r.y - a.y*r.w + a.x*r.z - a.z*r.x,
630
+ a.w*r.z - a.x*r.y + a.y*r.x - a.z*r.w,
631
+ a.w*r.w + a.x*r.x + a.y*r.y + a.z*r.z);
632
+
633
+ }
634
+
635
+ template<typename Type>
636
+ inline CUDA_CALLABLE void adj_mul(const quat_t<Type>& a, Type s, quat_t<Type>& adj_a, Type& adj_s, const quat_t<Type>& adj_ret)
637
+ {
638
+ adj_a += adj_ret*s;
639
+ adj_s += dot(a, adj_ret);
640
+ }
641
+
642
+ template<typename Type>
643
+ inline CUDA_CALLABLE void adj_mul(Type s, const quat_t<Type>& a, Type& adj_s, quat_t<Type>& adj_a, const quat_t<Type>& adj_ret)
644
+ {
645
+ adj_mul(a, s, adj_a, adj_s, adj_ret);
646
+ }
647
+
648
+ template<typename Type>
649
+ inline CUDA_CALLABLE void adj_div(quat_t<Type> a, Type s, quat_t<Type>& adj_a, Type& adj_s, const quat_t<Type>& adj_ret)
650
+ {
651
+ adj_s -= dot(a, adj_ret)/ (s * s); // - a / s^2
652
+ adj_a += adj_ret / s;
653
+ }
654
+
655
+ template<typename Type>
656
+ inline CUDA_CALLABLE void adj_div(Type s, quat_t<Type> a, Type& adj_s, quat_t<Type>& adj_a, const quat_t<Type>& adj_ret)
657
+ {
658
+ adj_s -= dot(a, adj_ret)/ (s * s); // - a / s^2
659
+ adj_a += s / adj_ret;
660
+ }
661
+
662
+ template<typename Type>
663
+ inline CUDA_CALLABLE void adj_quat_rotate(const quat_t<Type>& q, const vec_t<3,Type>& p, quat_t<Type>& adj_q, vec_t<3,Type>& adj_p, const vec_t<3,Type>& adj_ret)
664
+ {
665
+
666
+ {
667
+ Type t2 = p[2]*q.z*Type(2);
668
+ Type t3 = p[1]*q.w*Type(2);
669
+ Type t4 = p[0]*q.w*Type(2);
670
+ Type t5 = p[0]*q.x*Type(2);
671
+ Type t6 = p[1]*q.y*Type(2);
672
+ Type t7 = p[2]*q.y*Type(2);
673
+ Type t8 = p[0]*q.z*Type(2);
674
+ Type t9 = p[0]*q.y*Type(2);
675
+ Type t10 = p[1]*q.x*Type(2);
676
+ adj_q.x += adj_ret[2]*(t3+t8)+adj_ret[0]*(t2+t6+p[0]*q.x*Type(4))+adj_ret[1]*(t9-p[2]*q.w*Type(2));
677
+ adj_q.y += adj_ret[1]*(t2+t5+p[1]*q.y*Type(4))+adj_ret[0]*(t10+p[2]*q.w*Type(2))-adj_ret[2]*(t4-p[1]*q.z*Type(2));
678
+ adj_q.z += adj_ret[1]*(t4+t7)+adj_ret[2]*(t5+t6+p[2]*q.z*Type(4))-adj_ret[0]*(t3-p[2]*q.x*Type(2));
679
+ adj_q.w += adj_ret[0]*(t7+p[0]*q.w*Type(4)-p[1]*q.z*Type(2))+adj_ret[1]*(t8+p[1]*q.w*Type(4)-p[2]*q.x*Type(2))+adj_ret[2]*(-t9+t10+p[2]*q.w*Type(4));
680
+ }
681
+
682
+ {
683
+ Type t2 = q.w*q.w;
684
+ Type t3 = t2*Type(2);
685
+ Type t4 = q.w*q.z*Type(2);
686
+ Type t5 = q.x*q.y*Type(2);
687
+ Type t6 = q.w*q.y*Type(2);
688
+ Type t7 = q.w*q.x*Type(2);
689
+ Type t8 = q.y*q.z*Type(2);
690
+ adj_p[0] += adj_ret[1]*(t4+t5)+adj_ret[0]*(t3+(q.x*q.x)*Type(2)-Type(1))-adj_ret[2]*(t6-q.x*q.z*Type(2));
691
+ adj_p[1] += adj_ret[2]*(t7+t8)-adj_ret[0]*(t4-t5)+adj_ret[1]*(t3+(q.y*q.y)*Type(2)-Type(1));
692
+ adj_p[2] += -adj_ret[1]*(t7-t8)+adj_ret[2]*(t3+(q.z*q.z)*Type(2)-Type(1))+adj_ret[0]*(t6+q.x*q.z*Type(2));
693
+ }
694
+ }
695
+
696
+ template<typename Type>
697
+ inline CUDA_CALLABLE void adj_quat_rotate_inv(const quat_t<Type>& q, const vec_t<3,Type>& p, quat_t<Type>& adj_q, vec_t<3,Type>& adj_p, const vec_t<3,Type>& adj_ret)
698
+ {
699
+ const vec_t<3,Type>& r = adj_ret;
700
+
701
+ {
702
+ Type t2 = p[2]*q.w*Type(2);
703
+ Type t3 = p[2]*q.z*Type(2);
704
+ Type t4 = p[1]*q.w*Type(2);
705
+ Type t5 = p[0]*q.w*Type(2);
706
+ Type t6 = p[0]*q.x*Type(2);
707
+ Type t7 = p[1]*q.y*Type(2);
708
+ Type t8 = p[1]*q.z*Type(2);
709
+ Type t9 = p[2]*q.x*Type(2);
710
+ Type t10 = p[0]*q.y*Type(2);
711
+ adj_q.x += r[1]*(t2+t10)+r[0]*(t3+t7+p[0]*q.x*Type(4))-r[2]*(t4-p[0]*q.z*Type(2));
712
+ adj_q.y += r[2]*(t5+t8)+r[1]*(t3+t6+p[1]*q.y*Type(4))-r[0]*(t2-p[1]*q.x*Type(2));
713
+ adj_q.z += r[0]*(t4+t9)+r[2]*(t6+t7+p[2]*q.z*Type(4))-r[1]*(t5-p[2]*q.y*Type(2));
714
+ adj_q.w += r[0]*(t8+p[0]*q.w*Type(4)-p[2]*q.y*Type(2))+r[1]*(t9+p[1]*q.w*Type(4)-p[0]*q.z*Type(2))+r[2]*(t10-p[1]*q.x*Type(2)+p[2]*q.w*Type(4));
715
+ }
716
+
717
+ {
718
+ Type t2 = q.w*q.w;
719
+ Type t3 = t2*Type(2);
720
+ Type t4 = q.w*q.z*Type(2);
721
+ Type t5 = q.w*q.y*Type(2);
722
+ Type t6 = q.x*q.z*Type(2);
723
+ Type t7 = q.w*q.x*Type(2);
724
+ adj_p[0] += r[2]*(t5+t6)+r[0]*(t3+(q.x*q.x)*Type(2)-Type(1))-r[1]*(t4-q.x*q.y*Type(2));
725
+ adj_p[1] += r[1]*(t3+(q.y*q.y)*Type(2)-Type(1))+r[0]*(t4+q.x*q.y*Type(2))-r[2]*(t7-q.y*q.z*Type(2));
726
+ adj_p[2] += -r[0]*(t5-t6)+r[2]*(t3+(q.z*q.z)*Type(2)-Type(1))+r[1]*(t7+q.y*q.z*Type(2));
727
+ }
728
+ }
729
+
730
+ template<typename Type>
731
+ inline CUDA_CALLABLE void adj_quat_slerp(const quat_t<Type>& q0, const quat_t<Type>& q1, Type t, quat_t<Type>& ret, quat_t<Type>& adj_q0, quat_t<Type>& adj_q1, Type& adj_t, const quat_t<Type>& adj_ret)
732
+ {
733
+ vec_t<3,Type> axis;
734
+ Type angle;
735
+ quat_t<Type> q0_inv = quat_inverse(q0);
736
+ quat_t<Type> q_inc = mul(q0_inv, q1);
737
+ quat_to_axis_angle(q_inc, axis, angle);
738
+ quat_t<Type> qt = quat_from_axis_angle(axis, angle * t);
739
+ angle = angle * 0.5;
740
+
741
+ // adj_t
742
+ adj_t += dot(mul(ret, quat_t<Type>(angle*axis[0], angle*axis[1], angle*axis[2], Type(0))), adj_ret);
743
+
744
+ // adj_q0
745
+ quat_t<Type> q_inc_x_q0;
746
+ quat_t<Type> q_inc_y_q0;
747
+ quat_t<Type> q_inc_z_q0;
748
+ quat_t<Type> q_inc_w_q0;
749
+
750
+ quat_t<Type> q_inc_x_q1;
751
+ quat_t<Type> q_inc_y_q1;
752
+ quat_t<Type> q_inc_z_q1;
753
+ quat_t<Type> q_inc_w_q1;
754
+
755
+ adj_mul(q0_inv, q1, q_inc_x_q0, q_inc_x_q1, quat_t<Type>(1.f, Type(0), Type(0), Type(0)));
756
+ adj_mul(q0_inv, q1, q_inc_y_q0, q_inc_y_q1, quat_t<Type>(Type(0), 1.f, Type(0), Type(0)));
757
+ adj_mul(q0_inv, q1, q_inc_z_q0, q_inc_z_q1, quat_t<Type>(Type(0), Type(0), 1.f, Type(0)));
758
+ adj_mul(q0_inv, q1, q_inc_w_q0, q_inc_w_q1, quat_t<Type>(Type(0), Type(0), Type(0), 1.f));
759
+
760
+ quat_t<Type> a_x_q_inc;
761
+ quat_t<Type> a_y_q_inc;
762
+ quat_t<Type> a_z_q_inc;
763
+ quat_t<Type> t_q_inc;
764
+
765
+ adj_quat_to_axis_angle(q_inc, axis, angle, a_x_q_inc, vec_t<3,Type>(1.f, Type(0), Type(0)), Type(0));
766
+ adj_quat_to_axis_angle(q_inc, axis, angle, a_y_q_inc, vec_t<3,Type>(Type(0), 1.f, Type(0)), Type(0));
767
+ adj_quat_to_axis_angle(q_inc, axis, angle, a_z_q_inc, vec_t<3,Type>(Type(0), Type(0), 1.f), Type(0));
768
+ adj_quat_to_axis_angle(q_inc, axis, angle, t_q_inc, vec_t<3,Type>(Type(0), Type(0), Type(0)), Type(1));
769
+
770
+ Type cs = cos(angle*t);
771
+ Type sn = sin(angle*t);
772
+
773
+ quat_t<Type> q_inc_q0_x = quat_t<Type>(-q_inc_x_q0.x, -q_inc_y_q0.x, -q_inc_z_q0.x, -q_inc_w_q0.x);
774
+ quat_t<Type> q_inc_q0_y = quat_t<Type>(-q_inc_x_q0.y, -q_inc_y_q0.y, -q_inc_z_q0.y, -q_inc_w_q0.y);
775
+ quat_t<Type> q_inc_q0_z = quat_t<Type>(-q_inc_x_q0.z, -q_inc_y_q0.z, -q_inc_z_q0.z, -q_inc_w_q0.z);
776
+ quat_t<Type> q_inc_q0_w = quat_t<Type>(q_inc_x_q0.w, q_inc_y_q0.w, q_inc_z_q0.w, q_inc_w_q0.w);
777
+
778
+ Type a_x_q0_x = dot(a_x_q_inc, q_inc_q0_x);
779
+ Type a_x_q0_y = dot(a_x_q_inc, q_inc_q0_y);
780
+ Type a_x_q0_z = dot(a_x_q_inc, q_inc_q0_z);
781
+ Type a_x_q0_w = dot(a_x_q_inc, q_inc_q0_w);
782
+ Type a_y_q0_x = dot(a_y_q_inc, q_inc_q0_x);
783
+ Type a_y_q0_y = dot(a_y_q_inc, q_inc_q0_y);
784
+ Type a_y_q0_z = dot(a_y_q_inc, q_inc_q0_z);
785
+ Type a_y_q0_w = dot(a_y_q_inc, q_inc_q0_w);
786
+ Type a_z_q0_x = dot(a_z_q_inc, q_inc_q0_x);
787
+ Type a_z_q0_y = dot(a_z_q_inc, q_inc_q0_y);
788
+ Type a_z_q0_z = dot(a_z_q_inc, q_inc_q0_z);
789
+ Type a_z_q0_w = dot(a_z_q_inc, q_inc_q0_w);
790
+ Type t_q0_x = dot(t_q_inc, q_inc_q0_x);
791
+ Type t_q0_y = dot(t_q_inc, q_inc_q0_y);
792
+ Type t_q0_z = dot(t_q_inc, q_inc_q0_z);
793
+ Type t_q0_w = dot(t_q_inc, q_inc_q0_w);
794
+
795
+ quat_t<Type> q_s_q0_x = mul(quat_t<Type>(1.f, Type(0), Type(0), Type(0)), qt) + mul(q0, quat_t<Type>(
796
+ 0.5 * t * axis[0] * t_q0_x * cs + a_x_q0_x * sn,
797
+ 0.5 * t * axis[1] * t_q0_x * cs + a_y_q0_x * sn,
798
+ 0.5 * t * axis[2] * t_q0_x * cs + a_z_q0_x * sn,
799
+ -0.5 * t * t_q0_x * sn));
800
+
801
+ quat_t<Type> q_s_q0_y = mul(quat_t<Type>(Type(0), 1.f, Type(0), Type(0)), qt) + mul(q0, quat_t<Type>(
802
+ 0.5 * t * axis[0] * t_q0_y * cs + a_x_q0_y * sn,
803
+ 0.5 * t * axis[1] * t_q0_y * cs + a_y_q0_y * sn,
804
+ 0.5 * t * axis[2] * t_q0_y * cs + a_z_q0_y * sn,
805
+ -0.5 * t * t_q0_y * sn));
806
+
807
+ quat_t<Type> q_s_q0_z = mul(quat_t<Type>(Type(0), Type(0), 1.f, Type(0)), qt) + mul(q0, quat_t<Type>(
808
+ 0.5 * t * axis[0] * t_q0_z * cs + a_x_q0_z * sn,
809
+ 0.5 * t * axis[1] * t_q0_z * cs + a_y_q0_z * sn,
810
+ 0.5 * t * axis[2] * t_q0_z * cs + a_z_q0_z * sn,
811
+ -0.5 * t * t_q0_z * sn));
812
+
813
+ quat_t<Type> q_s_q0_w = mul(quat_t<Type>(Type(0), Type(0), Type(0), 1.f), qt) + mul(q0, quat_t<Type>(
814
+ 0.5 * t * axis[0] * t_q0_w * cs + a_x_q0_w * sn,
815
+ 0.5 * t * axis[1] * t_q0_w * cs + a_y_q0_w * sn,
816
+ 0.5 * t * axis[2] * t_q0_w * cs + a_z_q0_w * sn,
817
+ -0.5 * t * t_q0_w * sn));
818
+
819
+ adj_q0.x += dot(q_s_q0_x, adj_ret);
820
+ adj_q0.y += dot(q_s_q0_y, adj_ret);
821
+ adj_q0.z += dot(q_s_q0_z, adj_ret);
822
+ adj_q0.w += dot(q_s_q0_w, adj_ret);
823
+
824
+ // adj_q1
825
+ quat_t<Type> q_inc_q1_x = quat_t<Type>(q_inc_x_q1.x, q_inc_y_q1.x, q_inc_z_q1.x, q_inc_w_q1.x);
826
+ quat_t<Type> q_inc_q1_y = quat_t<Type>(q_inc_x_q1.y, q_inc_y_q1.y, q_inc_z_q1.y, q_inc_w_q1.y);
827
+ quat_t<Type> q_inc_q1_z = quat_t<Type>(q_inc_x_q1.z, q_inc_y_q1.z, q_inc_z_q1.z, q_inc_w_q1.z);
828
+ quat_t<Type> q_inc_q1_w = quat_t<Type>(q_inc_x_q1.w, q_inc_y_q1.w, q_inc_z_q1.w, q_inc_w_q1.w);
829
+
830
+ Type a_x_q1_x = dot(a_x_q_inc, q_inc_q1_x);
831
+ Type a_x_q1_y = dot(a_x_q_inc, q_inc_q1_y);
832
+ Type a_x_q1_z = dot(a_x_q_inc, q_inc_q1_z);
833
+ Type a_x_q1_w = dot(a_x_q_inc, q_inc_q1_w);
834
+ Type a_y_q1_x = dot(a_y_q_inc, q_inc_q1_x);
835
+ Type a_y_q1_y = dot(a_y_q_inc, q_inc_q1_y);
836
+ Type a_y_q1_z = dot(a_y_q_inc, q_inc_q1_z);
837
+ Type a_y_q1_w = dot(a_y_q_inc, q_inc_q1_w);
838
+ Type a_z_q1_x = dot(a_z_q_inc, q_inc_q1_x);
839
+ Type a_z_q1_y = dot(a_z_q_inc, q_inc_q1_y);
840
+ Type a_z_q1_z = dot(a_z_q_inc, q_inc_q1_z);
841
+ Type a_z_q1_w = dot(a_z_q_inc, q_inc_q1_w);
842
+ Type t_q1_x = dot(t_q_inc, q_inc_q1_x);
843
+ Type t_q1_y = dot(t_q_inc, q_inc_q1_y);
844
+ Type t_q1_z = dot(t_q_inc, q_inc_q1_z);
845
+ Type t_q1_w = dot(t_q_inc, q_inc_q1_w);
846
+
847
+ quat_t<Type> q_s_q1_x = mul(q0, quat_t<Type>(
848
+ 0.5 * t * axis[0] * t_q1_x * cs + a_x_q1_x * sn,
849
+ 0.5 * t * axis[1] * t_q1_x * cs + a_y_q1_x * sn,
850
+ 0.5 * t * axis[2] * t_q1_x * cs + a_z_q1_x * sn,
851
+ -0.5 * t * t_q1_x * sn));
852
+
853
+ quat_t<Type> q_s_q1_y = mul(q0, quat_t<Type>(
854
+ 0.5 * t * axis[0] * t_q1_y * cs + a_x_q1_y * sn,
855
+ 0.5 * t * axis[1] * t_q1_y * cs + a_y_q1_y * sn,
856
+ 0.5 * t * axis[2] * t_q1_y * cs + a_z_q1_y * sn,
857
+ -0.5 * t * t_q1_y * sn));
858
+
859
+ quat_t<Type> q_s_q1_z = mul(q0, quat_t<Type>(
860
+ 0.5 * t * axis[0] * t_q1_z * cs + a_x_q1_z * sn,
861
+ 0.5 * t * axis[1] * t_q1_z * cs + a_y_q1_z * sn,
862
+ 0.5 * t * axis[2] * t_q1_z * cs + a_z_q1_z * sn,
863
+ -0.5 * t * t_q1_z * sn));
864
+
865
+ quat_t<Type> q_s_q1_w = mul(q0, quat_t<Type>(
866
+ 0.5 * t * axis[0] * t_q1_w * cs + a_x_q1_w * sn,
867
+ 0.5 * t * axis[1] * t_q1_w * cs + a_y_q1_w * sn,
868
+ 0.5 * t * axis[2] * t_q1_w * cs + a_z_q1_w * sn,
869
+ -0.5 * t * t_q1_w * sn));
870
+
871
+ adj_q1.x += dot(q_s_q1_x, adj_ret);
872
+ adj_q1.y += dot(q_s_q1_y, adj_ret);
873
+ adj_q1.z += dot(q_s_q1_z, adj_ret);
874
+ adj_q1.w += dot(q_s_q1_w, adj_ret);
875
+
876
+ }
877
+
878
+ template<typename Type>
879
+ inline CUDA_CALLABLE void adj_quat_to_matrix(const quat_t<Type>& q, quat_t<Type>& adj_q, mat_t<3,3,Type>& adj_ret)
880
+ {
881
+ // we don't care about adjoint w.r.t. constant identity matrix
882
+ vec_t<3,Type> t;
883
+
884
+ adj_quat_rotate(q, vec_t<3,Type>(1.0, 0.0, 0.0), adj_q, t, adj_ret.get_col(0));
885
+ adj_quat_rotate(q, vec_t<3,Type>(0.0, 1.0, 0.0), adj_q, t, adj_ret.get_col(1));
886
+ adj_quat_rotate(q, vec_t<3,Type>(0.0, 0.0, 1.0), adj_q, t, adj_ret.get_col(2));
887
+ }
888
+
889
+ template<typename Type>
890
+ inline CUDA_CALLABLE void adj_quat_from_matrix(const mat_t<3,3,Type>& m, mat_t<3,3,Type>& adj_m, const quat_t<Type>& adj_ret)
891
+ {
892
+ const Type tr = m.data[0][0] + m.data[1][1] + m.data[2][2];
893
+ Type x, y, z, w, h = Type(0);
894
+
895
+ Type dx_dm00 = Type(0), dx_dm01 = Type(0), dx_dm02 = Type(0);
896
+ Type dx_dm10 = Type(0), dx_dm11 = Type(0), dx_dm12 = Type(0);
897
+ Type dx_dm20 = Type(0), dx_dm21 = Type(0), dx_dm22 = Type(0);
898
+ Type dy_dm00 = Type(0), dy_dm01 = Type(0), dy_dm02 = Type(0);
899
+ Type dy_dm10 = Type(0), dy_dm11 = Type(0), dy_dm12 = Type(0);
900
+ Type dy_dm20 = Type(0), dy_dm21 = Type(0), dy_dm22 = Type(0);
901
+ Type dz_dm00 = Type(0), dz_dm01 = Type(0), dz_dm02 = Type(0);
902
+ Type dz_dm10 = Type(0), dz_dm11 = Type(0), dz_dm12 = Type(0);
903
+ Type dz_dm20 = Type(0), dz_dm21 = Type(0), dz_dm22 = Type(0);
904
+ Type dw_dm00 = Type(0), dw_dm01 = Type(0), dw_dm02 = Type(0);
905
+ Type dw_dm10 = Type(0), dw_dm11 = Type(0), dw_dm12 = Type(0);
906
+ Type dw_dm20 = Type(0), dw_dm21 = Type(0), dw_dm22 = Type(0);
907
+
908
+ if (tr >= Type(0)) {
909
+ h = sqrt(tr + Type(1));
910
+ w = Type(0.5) * h;
911
+ h = Type(0.5) / h;
912
+
913
+ x = (m.data[2][1] - m.data[1][2]) * h;
914
+ y = (m.data[0][2] - m.data[2][0]) * h;
915
+ z = (m.data[1][0] - m.data[0][1]) * h;
916
+
917
+ dw_dm00 = Type(0.5) * h;
918
+ dw_dm11 = Type(0.5) * h;
919
+ dw_dm22 = Type(0.5) * h;
920
+ dx_dm21 = h;
921
+ dx_dm12 = -h;
922
+ dx_dm00 = Type(2) * h*h*h * (m.data[1][2] - m.data[2][1]);
923
+ dx_dm11 = Type(2) * h*h*h * (m.data[1][2] - m.data[2][1]);
924
+ dx_dm22 = Type(2) * h*h*h * (m.data[1][2] - m.data[2][1]);
925
+ dy_dm02 = h;
926
+ dy_dm20 = -h;
927
+ dy_dm00 = Type(2) * h*h*h * (m.data[2][0] - m.data[0][2]);
928
+ dy_dm11 = Type(2) * h*h*h * (m.data[2][0] - m.data[0][2]);
929
+ dy_dm22 = Type(2) * h*h*h * (m.data[2][0] - m.data[0][2]);
930
+ dz_dm10 = h;
931
+ dz_dm01 = -h;
932
+ dz_dm00 = Type(2) * h*h*h * (m.data[0][1] - m.data[1][0]);
933
+ dz_dm11 = Type(2) * h*h*h * (m.data[0][1] - m.data[1][0]);
934
+ dz_dm22 = Type(2) * h*h*h * (m.data[0][1] - m.data[1][0]);
935
+ } else {
936
+ size_t max_diag = 0;
937
+ if (m.data[1][1] > m.data[0][0]) {
938
+ max_diag = 1;
939
+ }
940
+ if (m.data[2][2] > m.data[max_diag][max_diag]) {
941
+ max_diag = 2;
942
+ }
943
+
944
+ if (max_diag == 0) {
945
+ h = sqrt((m.data[0][0] - (m.data[1][1] + m.data[2][2])) + Type(1));
946
+ x = Type(0.5) * h;
947
+ h = Type(0.5) / h;
948
+
949
+ y = (m.data[0][1] + m.data[1][0]) * h;
950
+ z = (m.data[2][0] + m.data[0][2]) * h;
951
+ w = (m.data[2][1] - m.data[1][2]) * h;
952
+
953
+ dx_dm00 = Type(0.5) * h;
954
+ dx_dm11 = -Type(0.5) * h;
955
+ dx_dm22 = -Type(0.5) * h;
956
+ dy_dm01 = h;
957
+ dy_dm10 = h;
958
+ dy_dm00 = -Type(2) * h*h*h * (m.data[0][1] + m.data[1][0]);
959
+ dy_dm11 = Type(2) * h*h*h * (m.data[0][1] + m.data[1][0]);
960
+ dy_dm22 = Type(2) * h*h*h * (m.data[0][1] + m.data[1][0]);
961
+ dz_dm20 = h;
962
+ dz_dm02 = h;
963
+ dz_dm00 = -Type(2) * h*h*h * (m.data[2][0] + m.data[0][2]);
964
+ dz_dm11 = Type(2) * h*h*h * (m.data[2][0] + m.data[0][2]);
965
+ dz_dm22 = Type(2) * h*h*h * (m.data[2][0] + m.data[0][2]);
966
+ dw_dm21 = h;
967
+ dw_dm12 = -h;
968
+ dw_dm00 = Type(2) * h*h*h * (m.data[1][2] - m.data[2][1]);
969
+ dw_dm11 = Type(2) * h*h*h * (m.data[2][1] - m.data[1][2]);
970
+ dw_dm22 = Type(2) * h*h*h * (m.data[2][1] - m.data[1][2]);
971
+ } else if (max_diag == 1) {
972
+ h = sqrt((m.data[1][1] - (m.data[2][2] + m.data[0][0])) + Type(1));
973
+ y = Type(0.5) * h;
974
+ h = Type(0.5) / h;
975
+
976
+ z = (m.data[1][2] + m.data[2][1]) * h;
977
+ x = (m.data[0][1] + m.data[1][0]) * h;
978
+ w = (m.data[0][2] - m.data[2][0]) * h;
979
+
980
+ dy_dm00 = -Type(0.5) * h;
981
+ dy_dm11 = Type(0.5) * h;
982
+ dy_dm22 = -Type(0.5) * h;
983
+ dz_dm12 = h;
984
+ dz_dm21 = h;
985
+ dz_dm00 = Type(2) * h*h*h * (m.data[1][2] + m.data[2][1]);
986
+ dz_dm11 = -Type(2) * h*h*h * (m.data[1][2] + m.data[2][1]);
987
+ dz_dm22 = Type(2) * h*h*h * (m.data[1][2] + m.data[2][1]);
988
+ dx_dm01 = h;
989
+ dx_dm10 = h;
990
+ dx_dm00 = Type(2) * h*h*h * (m.data[0][1] + m.data[1][0]);
991
+ dx_dm11 = -Type(2) * h*h*h * (m.data[0][1] + m.data[1][0]);
992
+ dx_dm22 = Type(2) * h*h*h * (m.data[0][1] + m.data[1][0]);
993
+ dw_dm02 = h;
994
+ dw_dm20 = -h;
995
+ dw_dm00 = Type(2) * h*h*h * (m.data[0][2] - m.data[2][0]);
996
+ dw_dm11 = Type(2) * h*h*h * (m.data[2][0] - m.data[0][2]);
997
+ dw_dm22 = Type(2) * h*h*h * (m.data[0][2] - m.data[2][0]);
998
+ } if (max_diag == 2) {
999
+ h = sqrt((m.data[2][2] - (m.data[0][0] + m.data[1][1])) + Type(1));
1000
+ z = Type(0.5) * h;
1001
+ h = Type(0.5) / h;
1002
+
1003
+ x = (m.data[2][0] + m.data[0][2]) * h;
1004
+ y = (m.data[1][2] + m.data[2][1]) * h;
1005
+ w = (m.data[1][0] - m.data[0][1]) * h;
1006
+
1007
+ dz_dm00 = -Type(0.5) * h;
1008
+ dz_dm11 = -Type(0.5) * h;
1009
+ dz_dm22 = Type(0.5) * h;
1010
+ dx_dm20 = h;
1011
+ dx_dm02 = h;
1012
+ dx_dm00 = Type(2) * h*h*h * (m.data[2][0] + m.data[0][2]);
1013
+ dx_dm11 = Type(2) * h*h*h * (m.data[2][0] + m.data[0][2]);
1014
+ dx_dm22 = -Type(2) * h*h*h * (m.data[2][0] + m.data[0][2]);
1015
+ dy_dm12 = h;
1016
+ dy_dm21 = h;
1017
+ dy_dm00 = Type(2) * h*h*h * (m.data[1][2] + m.data[2][1]);
1018
+ dy_dm11 = Type(2) * h*h*h * (m.data[1][2] + m.data[2][1]);
1019
+ dy_dm22 = -Type(2) * h*h*h * (m.data[1][2] + m.data[2][1]);
1020
+ dw_dm10 = h;
1021
+ dw_dm01 = -h;
1022
+ dw_dm00 = Type(2) * h*h*h * (m.data[1][0] - m.data[0][1]);
1023
+ dw_dm11 = Type(2) * h*h*h * (m.data[1][0] - m.data[0][1]);
1024
+ dw_dm22 = Type(2) * h*h*h * (m.data[0][1] - m.data[1][0]);
1025
+ }
1026
+ }
1027
+
1028
+ quat_t<Type> dq_dm00 = quat_t<Type>(dx_dm00, dy_dm00, dz_dm00, dw_dm00);
1029
+ quat_t<Type> dq_dm01 = quat_t<Type>(dx_dm01, dy_dm01, dz_dm01, dw_dm01);
1030
+ quat_t<Type> dq_dm02 = quat_t<Type>(dx_dm02, dy_dm02, dz_dm02, dw_dm02);
1031
+ quat_t<Type> dq_dm10 = quat_t<Type>(dx_dm10, dy_dm10, dz_dm10, dw_dm10);
1032
+ quat_t<Type> dq_dm11 = quat_t<Type>(dx_dm11, dy_dm11, dz_dm11, dw_dm11);
1033
+ quat_t<Type> dq_dm12 = quat_t<Type>(dx_dm12, dy_dm12, dz_dm12, dw_dm12);
1034
+ quat_t<Type> dq_dm20 = quat_t<Type>(dx_dm20, dy_dm20, dz_dm20, dw_dm20);
1035
+ quat_t<Type> dq_dm21 = quat_t<Type>(dx_dm21, dy_dm21, dz_dm21, dw_dm21);
1036
+ quat_t<Type> dq_dm22 = quat_t<Type>(dx_dm22, dy_dm22, dz_dm22, dw_dm22);
1037
+
1038
+ quat_t<Type> adj_q;
1039
+ adj_normalize(quat_t<Type>(x, y, z, w), adj_q, adj_ret);
1040
+
1041
+ adj_m.data[0][0] += dot(dq_dm00, adj_q);
1042
+ adj_m.data[0][1] += dot(dq_dm01, adj_q);
1043
+ adj_m.data[0][2] += dot(dq_dm02, adj_q);
1044
+ adj_m.data[1][0] += dot(dq_dm10, adj_q);
1045
+ adj_m.data[1][1] += dot(dq_dm11, adj_q);
1046
+ adj_m.data[1][2] += dot(dq_dm12, adj_q);
1047
+ adj_m.data[2][0] += dot(dq_dm20, adj_q);
1048
+ adj_m.data[2][1] += dot(dq_dm21, adj_q);
1049
+ adj_m.data[2][2] += dot(dq_dm22, adj_q);
1050
+ }
1051
+
1052
+ template<typename Type>
1053
+ inline CUDA_CALLABLE void adj_mat_t(const vec_t<3,Type>& pos, const quat_t<Type>& rot, const vec_t<3,Type>& scale,
1054
+ vec_t<3,Type>& adj_pos, quat_t<Type>& adj_rot, vec_t<3,Type>& adj_scale, const mat_t<4,4,Type>& adj_ret)
1055
+ {
1056
+ mat_t<3,3,Type> R = quat_to_matrix(rot);
1057
+ mat_t<3,3,Type> adj_R(0);
1058
+
1059
+ adj_pos[0] += adj_ret.data[0][3];
1060
+ adj_pos[1] += adj_ret.data[1][3];
1061
+ adj_pos[2] += adj_ret.data[2][3];
1062
+
1063
+ adj_mul(R.data[0][0], scale[0], adj_R.data[0][0], adj_scale[0], adj_ret.data[0][0]);
1064
+ adj_mul(R.data[1][0], scale[0], adj_R.data[1][0], adj_scale[0], adj_ret.data[1][0]);
1065
+ adj_mul(R.data[2][0], scale[0], adj_R.data[2][0], adj_scale[0], adj_ret.data[2][0]);
1066
+
1067
+ adj_mul(R.data[0][1], scale[1], adj_R.data[0][1], adj_scale[1], adj_ret.data[0][1]);
1068
+ adj_mul(R.data[1][1], scale[1], adj_R.data[1][1], adj_scale[1], adj_ret.data[1][1]);
1069
+ adj_mul(R.data[2][1], scale[1], adj_R.data[2][1], adj_scale[1], adj_ret.data[2][1]);
1070
+
1071
+ adj_mul(R.data[0][2], scale[2], adj_R.data[0][2], adj_scale[2], adj_ret.data[0][2]);
1072
+ adj_mul(R.data[1][2], scale[2], adj_R.data[1][2], adj_scale[2], adj_ret.data[1][2]);
1073
+ adj_mul(R.data[2][2], scale[2], adj_R.data[2][2], adj_scale[2], adj_ret.data[2][2]);
1074
+
1075
+ adj_quat_to_matrix(rot, adj_rot, adj_R);
1076
+ }
1077
+
1078
+ template<unsigned Rows, unsigned Cols, typename Type>
1079
+ inline CUDA_CALLABLE mat_t<Rows,Cols,Type>::mat_t(const vec_t<3,Type>& pos, const quat_t<Type>& rot, const vec_t<3,Type>& scale)
1080
+ {
1081
+ mat_t<3,3,Type> R = quat_to_matrix(rot);
1082
+
1083
+ data[0][0] = R.data[0][0]*scale[0];
1084
+ data[1][0] = R.data[1][0]*scale[0];
1085
+ data[2][0] = R.data[2][0]*scale[0];
1086
+ data[3][0] = Type(0);
1087
+
1088
+ data[0][1] = R.data[0][1]*scale[1];
1089
+ data[1][1] = R.data[1][1]*scale[1];
1090
+ data[2][1] = R.data[2][1]*scale[1];
1091
+ data[3][1] = Type(0);
1092
+
1093
+ data[0][2] = R.data[0][2]*scale[2];
1094
+ data[1][2] = R.data[1][2]*scale[2];
1095
+ data[2][2] = R.data[2][2]*scale[2];
1096
+ data[3][2] = Type(0);
1097
+
1098
+ data[0][3] = pos[0];
1099
+ data[1][3] = pos[1];
1100
+ data[2][3] = pos[2];
1101
+ data[3][3] = Type(1);
1102
+ }
1103
+
1104
+ template<typename Type=float32>
1105
+ inline CUDA_CALLABLE quat_t<Type> quat_identity()
1106
+ {
1107
+ return quat_t<Type>(Type(0), Type(0), Type(0), Type(1));
1108
+ }
1109
+
1110
+
1111
+
1112
+ } // namespace wp