@series-inc/rundot-syncplay 6.0.0-rc.2 → 6.0.0-rc.21

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.
Files changed (329) hide show
  1. package/README.md +133 -0
  2. package/core/deterministic-math.d.ts +75 -1
  3. package/core/native/instance_ledger.hpp +1095 -0
  4. package/core/native/legacy_checkpoint_ids.hpp +107 -0
  5. package/core/native/physics/abi.cpp +39 -211
  6. package/core/native/physics/abi.hpp +2 -6
  7. package/core/native/physics/articulation.cpp +1434 -0
  8. package/core/native/physics/articulation.hpp +279 -0
  9. package/core/native/physics/buoyancy.cpp +1588 -0
  10. package/core/native/physics/buoyancy.hpp +254 -0
  11. package/core/native/physics/capsule_triangle.cpp +168 -0
  12. package/core/native/physics/capsule_triangle.hpp +40 -0
  13. package/core/native/physics/capsule_triangle_native.inc +291 -0
  14. package/core/native/physics/certified_sum.hpp +218 -0
  15. package/core/native/physics/character.cpp +638 -0
  16. package/core/native/physics/character.hpp +169 -0
  17. package/core/native/physics/checkpoint_schema.hpp +379 -0
  18. package/core/native/physics/cloth.cpp +1401 -0
  19. package/core/native/physics/cloth.hpp +357 -0
  20. package/core/native/physics/constraint_rows.cpp +363 -2
  21. package/core/native/physics/constraint_rows.hpp +72 -1
  22. package/core/native/physics/deformable_query.cpp +1630 -0
  23. package/core/native/physics/deformable_query.hpp +578 -0
  24. package/core/native/physics/distance.cpp +1166 -0
  25. package/core/native/physics/distance.hpp +151 -0
  26. package/core/native/physics/exact/exact-applied.cpp +101 -0
  27. package/core/native/physics/exact/exact-applied.hpp +112 -0
  28. package/core/native/physics/exact/exact-dyadic.cpp +691 -0
  29. package/core/native/physics/exact/exact-dyadic.hpp +58 -0
  30. package/core/native/physics/exact/exact-operation.hpp +51 -0
  31. package/core/native/physics/exact/word-core.hpp +262 -0
  32. package/core/native/physics/extended_abi.cpp +1242 -0
  33. package/core/native/physics/extended_abi.hpp +355 -0
  34. package/core/native/physics/extended_abi_characters.cpp +261 -0
  35. package/core/native/physics/extended_abi_characters.hpp +434 -0
  36. package/core/native/physics/extended_abi_core_features.cpp +713 -0
  37. package/core/native/physics/extended_abi_core_features.hpp +226 -0
  38. package/core/native/physics/extended_abi_deformables.cpp +448 -0
  39. package/core/native/physics/extended_abi_deformables.hpp +867 -0
  40. package/core/native/physics/extended_abi_vehicles.cpp +411 -0
  41. package/core/native/physics/extended_abi_vehicles.hpp +222 -0
  42. package/core/native/physics/fracture.cpp +1981 -0
  43. package/core/native/physics/fracture.hpp +460 -0
  44. package/core/native/physics/gear.cpp +563 -0
  45. package/core/native/physics/gear.hpp +158 -0
  46. package/core/native/physics/geometry_memory.hpp +476 -0
  47. package/core/native/physics/geometry_store.cpp +547 -0
  48. package/core/native/physics/geometry_store.hpp +134 -0
  49. package/core/native/physics/lag_compensation.cpp +1626 -0
  50. package/core/native/physics/lag_compensation.hpp +405 -0
  51. package/core/native/physics/mass_properties.cpp +449 -0
  52. package/core/native/physics/mass_properties.hpp +23 -0
  53. package/core/native/physics/material_pose.hpp +988 -0
  54. package/core/native/physics/particles.cpp +1065 -0
  55. package/core/native/physics/particles.hpp +261 -0
  56. package/core/native/physics/path_joint.cpp +781 -0
  57. package/core/native/physics/path_joint.hpp +154 -0
  58. package/core/native/physics/physics.cpp +13043 -1065
  59. package/core/native/physics/physics.hpp +184 -8
  60. package/core/native/physics/planar_deformable.cpp +2433 -0
  61. package/core/native/physics/planar_deformable.hpp +889 -0
  62. package/core/native/physics/platformer_2d.cpp +1353 -0
  63. package/core/native/physics/platformer_2d.hpp +340 -0
  64. package/core/native/physics/powered_ragdoll.cpp +1100 -0
  65. package/core/native/physics/powered_ragdoll.hpp +279 -0
  66. package/core/native/physics/powertrain.cpp +586 -0
  67. package/core/native/physics/powertrain.hpp +180 -0
  68. package/core/native/physics/pulley.cpp +311 -0
  69. package/core/native/physics/pulley.hpp +121 -0
  70. package/core/native/physics/ragdoll.cpp +586 -0
  71. package/core/native/physics/ragdoll.hpp +221 -0
  72. package/core/native/physics/rope.cpp +692 -0
  73. package/core/native/physics/rope.hpp +215 -0
  74. package/core/native/physics/rounded_convex.cpp +1229 -0
  75. package/core/native/physics/rounded_convex.hpp +287 -0
  76. package/core/native/physics/sdf_geometry.cpp +1821 -0
  77. package/core/native/physics/sdf_geometry.hpp +474 -0
  78. package/core/native/physics/soft_body.cpp +1449 -0
  79. package/core/native/physics/soft_body.hpp +328 -0
  80. package/core/native/physics/soft_collision.cpp +1862 -0
  81. package/core/native/physics/soft_collision.hpp +561 -0
  82. package/core/native/physics/surface_velocity.cpp +366 -0
  83. package/core/native/physics/surface_velocity.hpp +243 -0
  84. package/core/native/physics/topology.cpp +357 -39
  85. package/core/native/physics/topology.hpp +97 -6
  86. package/core/native/physics/tracks.cpp +1179 -0
  87. package/core/native/physics/tracks.hpp +404 -0
  88. package/core/native/physics/vehicle.cpp +629 -0
  89. package/core/native/physics/vehicle.hpp +191 -0
  90. package/core/native/physics/voxel_cooking.cpp +1929 -0
  91. package/core/native/physics/voxel_cooking.hpp +395 -0
  92. package/core/native/physics/world.cpp +5290 -854
  93. package/core/native/physics/world.hpp +796 -24
  94. package/core/native/physics/world_abi.cpp +3840 -306
  95. package/core/native/physics/world_abi.hpp +530 -10
  96. package/core/native/physics/world_digest_visitor.hpp +277 -0
  97. package/core/native/physics/world_strong_digest.hpp +557 -0
  98. package/core/native/physics2d/ccd.cpp +1302 -0
  99. package/core/native/physics2d/ccd.hpp +141 -0
  100. package/core/native/physics2d/checkpoint_memory.hpp +349 -0
  101. package/core/native/physics2d/clock_primitives.hpp +138 -0
  102. package/core/native/physics2d/commands.cpp +174 -28
  103. package/core/native/physics2d/commands.hpp +3 -1
  104. package/core/native/physics2d/compound.cpp +581 -0
  105. package/core/native/physics2d/compound.hpp +248 -0
  106. package/core/native/physics2d/contour.cpp +325 -0
  107. package/core/native/physics2d/contour.hpp +143 -0
  108. package/core/native/physics2d/frame_scope.hpp +124 -0
  109. package/core/native/physics2d/joint_phases.hpp +93 -0
  110. package/core/native/physics2d/joints.cpp +587 -150
  111. package/core/native/physics2d/joints.hpp +5 -0
  112. package/core/native/physics2d/memory_private.hpp +125 -0
  113. package/core/native/physics2d/owned_abi.cpp +1354 -879
  114. package/core/native/physics2d/owned_abi.hpp +190 -52
  115. package/core/native/physics2d/phase_test_hooks.hpp +17 -0
  116. package/core/native/physics2d/physics2d.cpp +2073 -120
  117. package/core/native/physics2d/physics2d.hpp +154 -2
  118. package/core/native/physics2d/queries.cpp +1055 -79
  119. package/core/native/physics2d/queries.hpp +71 -5
  120. package/core/native/physics2d/world.cpp +1313 -353
  121. package/core/native/physics2d/world.hpp +124 -13
  122. package/core/native/sha256/sha256_compress_unsigned.hpp +68 -0
  123. package/core/native/sha256/sha256_fips_constants.hpp +24 -0
  124. package/core/native/sha256/sha256_length.hpp +56 -0
  125. package/core/native/sha256/sha256_memo.hpp +48 -0
  126. package/core/native/sha256/sha256_sink.hpp +234 -0
  127. package/core/physics2d-owned.d.ts +466 -126
  128. package/core/runtime.d.ts +192 -19
  129. package/core/scripts/build-native.mjs +34 -0
  130. package/core/scripts/build-wasm-physics.mjs +272 -23
  131. package/core/scripts/extended-physics-profile.mjs +634 -0
  132. package/core/scripts/generate-physics2d-vectors.mjs +59 -0
  133. package/core/src/kinetix-deterministic-math.d.ts +2 -0
  134. package/core/src/kinetix-deterministic-math.mjs +513 -27
  135. package/core/src/kinetix-owned-runtime-adapter.mjs +788 -0
  136. package/core/src/kinetix-runtime-adapter-utils.mjs +54 -6
  137. package/core/src/kinetix-runtime-contract.mjs +55 -9
  138. package/core/src/kinetix-runtime-identity.mjs +12 -1
  139. package/core/src/kinetix-session-config.mjs +7 -2
  140. package/core/src/kinetix-state-report.mjs +36 -0
  141. package/core/src/physics/abi-constants.mjs +5 -4
  142. package/core/src/physics/articulation.d.mts +194 -0
  143. package/core/src/physics/articulation.mjs +1141 -0
  144. package/core/src/physics/buoyancy.d.mts +143 -0
  145. package/core/src/physics/buoyancy.mjs +268 -0
  146. package/core/src/physics/cloth.d.mts +314 -0
  147. package/core/src/physics/cloth.mjs +1156 -0
  148. package/core/src/physics/deformable-query.d.mts +217 -0
  149. package/core/src/physics/deformable-query.mjs +1412 -0
  150. package/core/src/physics/distance.d.mts +160 -0
  151. package/core/src/physics/distance.mjs +175 -0
  152. package/core/src/physics/extended-wasm-instance.d.mts +5 -0
  153. package/core/src/physics/extended-wasm-instance.mjs +48 -0
  154. package/core/src/physics/feature-api.d.mts +74 -0
  155. package/core/src/physics/feature-api.mjs +98 -0
  156. package/core/src/physics/fracture.d.mts +133 -0
  157. package/core/src/physics/fracture.mjs +1324 -0
  158. package/core/src/physics/gear.d.mts +585 -0
  159. package/core/src/physics/gear.mjs +955 -0
  160. package/core/src/physics/lag-compensation.d.mts +101 -0
  161. package/core/src/physics/lag-compensation.mjs +1100 -0
  162. package/core/src/physics/mass-properties.d.mts +114 -0
  163. package/core/src/physics/mass-properties.mjs +137 -468
  164. package/core/src/physics/particles.d.mts +177 -0
  165. package/core/src/physics/particles.mjs +758 -0
  166. package/core/src/physics/path-joint.d.mts +171 -0
  167. package/core/src/physics/path-joint.mjs +1231 -0
  168. package/core/src/physics/physics-extended-wasm-module.generated.mjs +13 -0
  169. package/core/src/physics/physics-profile-wasm-module.generated.mjs +48 -0
  170. package/core/src/physics/physics-wasm-module.generated.mjs +34 -5
  171. package/core/src/physics/powered-ragdoll.d.mts +188 -0
  172. package/core/src/physics/powered-ragdoll.mjs +1043 -0
  173. package/core/src/physics/powertrain.d.mts +260 -0
  174. package/core/src/physics/powertrain.mjs +621 -0
  175. package/core/src/physics/pulley.d.mts +98 -0
  176. package/core/src/physics/pulley.mjs +686 -0
  177. package/core/src/physics/ragdoll.d.mts +289 -0
  178. package/core/src/physics/ragdoll.mjs +1323 -0
  179. package/core/src/physics/rope.d.mts +241 -0
  180. package/core/src/physics/rope.mjs +319 -0
  181. package/core/src/physics/rounded-convex.d.mts +328 -0
  182. package/core/src/physics/rounded-convex.mjs +1404 -0
  183. package/core/src/physics/sdf-geometry.d.mts +117 -0
  184. package/core/src/physics/sdf-geometry.mjs +1204 -0
  185. package/core/src/physics/soft-body.d.mts +144 -0
  186. package/core/src/physics/soft-body.mjs +1285 -0
  187. package/core/src/physics/soft-collision.d.mts +651 -0
  188. package/core/src/physics/soft-collision.mjs +1883 -0
  189. package/core/src/physics/surface-velocity.d.mts +156 -0
  190. package/core/src/physics/surface-velocity.mjs +722 -0
  191. package/core/src/physics/tracks.d.mts +411 -0
  192. package/core/src/physics/tracks.mjs +1074 -0
  193. package/core/src/physics/vehicle.d.mts +3 -0
  194. package/core/src/physics/vehicle.mjs +172 -0
  195. package/core/src/physics/voxel-cooking.d.mts +219 -0
  196. package/core/src/physics/voxel-cooking.mjs +1499 -0
  197. package/core/src/physics/voxel-swept-quad.mjs +110 -0
  198. package/core/src/physics/wasm-instance.mjs +160 -23
  199. package/core/src/physics/world.d.mts +62 -0
  200. package/core/src/physics/world.mjs +1947 -152
  201. package/core/src/physics2d/body-state.mjs +218 -15
  202. package/core/src/physics2d/collision.d.mts +52 -0
  203. package/core/src/physics2d/collision.mjs +234 -0
  204. package/core/src/physics2d/compound.mjs +454 -0
  205. package/core/src/physics2d/contour-state.mjs +771 -0
  206. package/core/src/physics2d/feature-api.d.mts +211 -0
  207. package/core/src/physics2d/feature-api.mjs +3 -0
  208. package/core/src/physics2d/fluid-state.mjs +145 -0
  209. package/core/src/physics2d/joint-state.mjs +1 -1
  210. package/core/src/physics2d/owned-queries.mjs +3 -1
  211. package/core/src/physics2d/owned-transport.mjs +268 -122
  212. package/core/src/physics2d/owned-world.mjs +353 -230
  213. package/core/src/physics2d/particle-fluid.d.mts +152 -0
  214. package/core/src/physics2d/particle-fluid.mjs +233 -0
  215. package/core/src/physics2d/physics2d-wasm-module.generated.mjs +27 -10
  216. package/core/src/physics2d/query-codec.mjs +61 -13
  217. package/core/src/physics2d/submerged-geometry.d.mts +51 -0
  218. package/core/src/physics2d/submerged-geometry.mjs +186 -0
  219. package/core/src/runtime.mjs +23 -0
  220. package/core/src/shared/bytes.d.mts +2 -0
  221. package/core/src/shared/bytes.mjs +33 -0
  222. package/core/src/shared/sha256.d.mts +3 -0
  223. package/core/src/shared/sha256.mjs +72 -22
  224. package/core/units.d.ts +50 -0
  225. package/core/world.d.ts +253 -5
  226. package/dist/authority-room.d.ts +9 -0
  227. package/dist/authority-room.js +45 -16
  228. package/dist/browser.d.ts +6 -5
  229. package/dist/browser.js +4 -3
  230. package/dist/cadence.d.ts +7 -0
  231. package/dist/cadence.js +19 -0
  232. package/dist/canonical.d.ts +1 -0
  233. package/dist/canonical.js +3 -0
  234. package/dist/checksum-policy.d.ts +10 -0
  235. package/dist/checksum-policy.js +14 -0
  236. package/dist/collider-cooking-hull.d.ts +18 -0
  237. package/dist/collider-cooking-hull.js +248 -0
  238. package/dist/collider-cooking.d.ts +140 -0
  239. package/dist/collider-cooking.js +794 -0
  240. package/dist/compat/v5.d.ts +121 -0
  241. package/dist/compat/v5.js +479 -0
  242. package/dist/creator.d.ts +7 -6
  243. package/dist/creator.js +4 -3
  244. package/dist/game-byte-section.d.ts +36 -0
  245. package/dist/game-byte-section.js +350 -0
  246. package/dist/index.d.ts +13 -6
  247. package/dist/index.js +9 -4
  248. package/dist/instant-replay.d.ts +27 -2
  249. package/dist/instant-replay.js +91 -16
  250. package/dist/lag-compensation3d.js +11 -10
  251. package/dist/match-log.d.ts +21 -4
  252. package/dist/match-log.js +92 -9
  253. package/dist/math.d.ts +2 -0
  254. package/dist/math.js +1 -0
  255. package/dist/modules/character-3d.d.ts +16 -1
  256. package/dist/modules/character-3d.js +42 -5
  257. package/dist/modules/combat.js +6 -5
  258. package/dist/movement3d-revision1.d.ts +2 -0
  259. package/dist/movement3d-revision1.js +973 -0
  260. package/dist/movement3d.d.ts +53 -3
  261. package/dist/movement3d.js +917 -247
  262. package/dist/networked-client.d.ts +15 -0
  263. package/dist/networked-client.js +378 -105
  264. package/dist/node.d.ts +3 -2
  265. package/dist/node.js +2 -1
  266. package/dist/physics/2d.d.ts +38 -4
  267. package/dist/physics/2d.js +57 -36
  268. package/dist/physics/3d.d.ts +755 -111
  269. package/dist/physics/3d.js +2355 -158
  270. package/dist/physics/edit-result-graph.d.ts +36 -0
  271. package/dist/physics/edit-result-graph.js +141 -0
  272. package/dist/physics/game-codec3d.d.ts +41 -0
  273. package/dist/physics/game-codec3d.js +544 -0
  274. package/dist/physics/installed-checkpoint3d.d.ts +13 -0
  275. package/dist/physics/installed-checkpoint3d.js +120 -0
  276. package/dist/physics/installed-runtime3d.d.ts +51 -0
  277. package/dist/physics/installed-runtime3d.js +372 -0
  278. package/dist/physics/installed-state-commitment3d.d.ts +2 -0
  279. package/dist/physics/installed-state-commitment3d.js +24 -0
  280. package/dist/physics/units.d.ts +38 -0
  281. package/dist/physics/units.js +56 -0
  282. package/dist/physics/vehicle-state3d.d.ts +93 -0
  283. package/dist/physics/vehicle-state3d.js +1191 -0
  284. package/dist/physics/voxel-body-state.d.ts +61 -0
  285. package/dist/physics/voxel-body-state.js +410 -0
  286. package/dist/r3f-render-adapter.d.ts +59 -1
  287. package/dist/r3f-render-adapter.js +137 -2
  288. package/dist/replay.d.ts +13 -0
  289. package/dist/replay.js +95 -70
  290. package/dist/run-room-transport.js +5 -1
  291. package/dist/runner.d.ts +17 -2
  292. package/dist/runner.js +472 -84
  293. package/dist/runtime-cleanup.d.ts +54 -0
  294. package/dist/runtime-cleanup.js +266 -0
  295. package/dist/runtime-lease.d.ts +11 -1
  296. package/dist/runtime-lease.js +23 -10
  297. package/dist/runtime-room-metadata.d.ts +2 -8
  298. package/dist/runtime-room-metadata.js +11 -14
  299. package/dist/runtime-session.d.ts +8 -0
  300. package/dist/runtime-session.js +222 -58
  301. package/dist/schema-codegen.js +3 -0
  302. package/dist/schema-dsl-binary.d.ts +10 -4
  303. package/dist/schema-dsl-binary.js +375 -76
  304. package/dist/scratch-pool.d.ts +32 -0
  305. package/dist/scratch-pool.js +60 -0
  306. package/dist/sdk-session.d.ts +1 -0
  307. package/dist/sdk-session.js +4 -0
  308. package/dist/secret-config.js +2 -60
  309. package/dist/secret-crypto.js +70 -36
  310. package/dist/session-snapshot.d.ts +15 -4
  311. package/dist/session-snapshot.js +35 -33
  312. package/dist/session-wire.js +14 -22
  313. package/dist/sparse-set.d.ts +1 -0
  314. package/dist/sparse-set.js +22 -0
  315. package/dist/spatial/spatial-grid-3d.d.ts +41 -0
  316. package/dist/spatial/spatial-grid-3d.js +115 -0
  317. package/dist/synctest.d.ts +9 -0
  318. package/dist/synctest.js +106 -17
  319. package/dist/testing-network.d.ts +2 -0
  320. package/dist/testing-network.js +122 -18
  321. package/dist/testing.d.ts +27 -6
  322. package/dist/testing.js +297 -56
  323. package/dist/tools/index.d.ts +3 -2
  324. package/dist/tools/index.js +3 -2
  325. package/dist/tools/module-certification.d.ts +6 -0
  326. package/dist/tools/module-certification.js +26 -2
  327. package/package.json +18 -5
  328. package/core/scripts/run-box3d-benchmark.mjs +0 -350
  329. package/core/src/physics/index.mjs +0 -3169
@@ -0,0 +1,1630 @@
1
+ #include "deformable_query.hpp"
2
+
3
+ #include <algorithm>
4
+ #include <cmath>
5
+ #include <cstdint>
6
+ #include <cstring>
7
+ #include <limits>
8
+ #include <span>
9
+
10
+ namespace rundot::kinetix::physics {
11
+
12
+ namespace {
13
+
14
+ constexpr double kEpsilon = 1e-12;
15
+ constexpr double kZeroTolerance = 1e-24;
16
+
17
+ inline std::uint32_t computeFnv1a32(const std::uint8_t* data, std::size_t size) {
18
+ std::uint32_t hash = 2166136261u;
19
+ for (std::size_t i = 0; i < size; ++i) {
20
+ hash ^= static_cast<std::uint32_t>(data[i]);
21
+ hash *= 16777619u;
22
+ }
23
+ return hash;
24
+ }
25
+
26
+ } // namespace
27
+
28
+ // ---------------------------------------------------------------------------
29
+ // Deterministic Math Functions (double only)
30
+ // ---------------------------------------------------------------------------
31
+ DeformableVec3 addVec3(const DeformableVec3& a, const DeformableVec3& b) {
32
+ return {a.x + b.x, a.y + b.y, a.z + b.z};
33
+ }
34
+
35
+ DeformableVec3 subVec3(const DeformableVec3& a, const DeformableVec3& b) {
36
+ return {a.x - b.x, a.y - b.y, a.z - b.z};
37
+ }
38
+
39
+ DeformableVec3 scaleVec3(const DeformableVec3& v, double s) {
40
+ return {v.x * s, v.y * s, v.z * s};
41
+ }
42
+
43
+ double dotVec3(const DeformableVec3& a, const DeformableVec3& b) {
44
+ return a.x * b.x + a.y * b.y + a.z * b.z;
45
+ }
46
+
47
+ DeformableVec3 crossVec3(const DeformableVec3& a, const DeformableVec3& b) {
48
+ return {
49
+ a.y * b.z - a.z * b.y,
50
+ a.z * b.x - a.x * b.z,
51
+ a.x * b.y - a.y * b.x
52
+ };
53
+ }
54
+
55
+ double lengthSquaredVec3(const DeformableVec3& v) {
56
+ return dotVec3(v, v);
57
+ }
58
+
59
+ double lengthVec3(const DeformableVec3& v) {
60
+ return std::sqrt(dotVec3(v, v));
61
+ }
62
+
63
+ DeformableVec3 normalizeVec3(const DeformableVec3& v) {
64
+ const double len = lengthVec3(v);
65
+ if (len > kZeroTolerance) {
66
+ const double inv = 1.0 / len;
67
+ return {v.x * inv, v.y * inv, v.z * inv};
68
+ }
69
+ return {0.0, 1.0, 0.0};
70
+ }
71
+
72
+ DeformableVec3 rotateByQuat(const DeformableQuat& q, const DeformableVec3& v) {
73
+ const DeformableVec3 qv{q.x, q.y, q.z};
74
+ const DeformableVec3 t = scaleVec3(crossVec3(qv, v), 2.0);
75
+ return addVec3(addVec3(v, scaleVec3(t, q.w)), crossVec3(qv, t));
76
+ }
77
+
78
+ DeformableVec3 rotateByConjugateQuat(const DeformableQuat& q, const DeformableVec3& v) {
79
+ const DeformableVec3 qv{-q.x, -q.y, -q.z};
80
+ const DeformableVec3 t = scaleVec3(crossVec3(qv, v), 2.0);
81
+ return addVec3(addVec3(v, scaleVec3(t, q.w)), crossVec3(qv, t));
82
+ }
83
+
84
+ // ---------------------------------------------------------------------------
85
+ // Geometric Computations
86
+ // ---------------------------------------------------------------------------
87
+ DeformableAABB computeDeformableBounds(
88
+ std::span<const DeformableNode> nodes,
89
+ double margin) {
90
+ if (nodes.empty()) {
91
+ return {{-margin, -margin, -margin}, {margin, margin, margin}};
92
+ }
93
+ DeformableVec3 bmin = nodes[0].position;
94
+ DeformableVec3 bmax = nodes[0].position;
95
+ for (std::size_t i = 0; i < nodes.size(); ++i) {
96
+ const auto& pos = nodes[i].position;
97
+ const double r = nodes[i].radius;
98
+ bmin.x = std::min(bmin.x, pos.x - r);
99
+ bmin.y = std::min(bmin.y, pos.y - r);
100
+ bmin.z = std::min(bmin.z, pos.z - r);
101
+ bmax.x = std::max(bmax.x, pos.x + r);
102
+ bmax.y = std::max(bmax.y, pos.y + r);
103
+ bmax.z = std::max(bmax.z, pos.z + r);
104
+ }
105
+ bmin.x -= margin;
106
+ bmin.y -= margin;
107
+ bmin.z -= margin;
108
+ bmax.x += margin;
109
+ bmax.y += margin;
110
+ bmax.z += margin;
111
+ return {bmin, bmax};
112
+ }
113
+
114
+ double computeDeformableVolume(
115
+ std::span<const DeformableNode> nodes,
116
+ std::span<const DeformableTriangle> triangles) {
117
+ if (nodes.empty() || triangles.empty()) return 0.0;
118
+ double volume6 = 0.0;
119
+ for (std::size_t i = 0; i < triangles.size(); ++i) {
120
+ const auto& tri = triangles[i];
121
+ if (tri.nodeIndices[0] >= nodes.size() ||
122
+ tri.nodeIndices[1] >= nodes.size() ||
123
+ tri.nodeIndices[2] >= nodes.size()) {
124
+ continue;
125
+ }
126
+ const DeformableVec3& v0 = nodes[tri.nodeIndices[0]].position;
127
+ const DeformableVec3& v1 = nodes[tri.nodeIndices[1]].position;
128
+ const DeformableVec3& v2 = nodes[tri.nodeIndices[2]].position;
129
+ volume6 += dotVec3(v0, crossVec3(v1, v2));
130
+ }
131
+ return std::abs(volume6) * (1.0 / 6.0);
132
+ }
133
+
134
+ void updateDeformableNormals(
135
+ std::span<DeformableNode> nodes,
136
+ std::span<const DeformableTriangle> triangles) {
137
+ for (std::size_t i = 0; i < nodes.size(); ++i) {
138
+ nodes[i].normal = {0.0, 0.0, 0.0};
139
+ }
140
+ for (std::size_t i = 0; i < triangles.size(); ++i) {
141
+ const auto& tri = triangles[i];
142
+ if (tri.nodeIndices[0] >= nodes.size() ||
143
+ tri.nodeIndices[1] >= nodes.size() ||
144
+ tri.nodeIndices[2] >= nodes.size()) {
145
+ continue;
146
+ }
147
+ const DeformableVec3& v0 = nodes[tri.nodeIndices[0]].position;
148
+ const DeformableVec3& v1 = nodes[tri.nodeIndices[1]].position;
149
+ const DeformableVec3& v2 = nodes[tri.nodeIndices[2]].position;
150
+ const DeformableVec3 fn = crossVec3(subVec3(v1, v0), subVec3(v2, v0));
151
+ nodes[tri.nodeIndices[0]].normal = addVec3(nodes[tri.nodeIndices[0]].normal, fn);
152
+ nodes[tri.nodeIndices[1]].normal = addVec3(nodes[tri.nodeIndices[1]].normal, fn);
153
+ nodes[tri.nodeIndices[2]].normal = addVec3(nodes[tri.nodeIndices[2]].normal, fn);
154
+ }
155
+ for (std::size_t i = 0; i < nodes.size(); ++i) {
156
+ nodes[i].normal = normalizeVec3(nodes[i].normal);
157
+ }
158
+ }
159
+
160
+ // ---------------------------------------------------------------------------
161
+ // Voronoi Closest Point on Triangle (Christer Ericson, RTCD)
162
+ // ---------------------------------------------------------------------------
163
+ DeformableVec3 closestPointOnTriangle(
164
+ const DeformableVec3& p,
165
+ const DeformableVec3& a,
166
+ const DeformableVec3& b,
167
+ const DeformableVec3& c,
168
+ double& outU,
169
+ double& outV,
170
+ double& outW) {
171
+ const DeformableVec3 ab = subVec3(b, a);
172
+ const DeformableVec3 ac = subVec3(c, a);
173
+ const DeformableVec3 ap = subVec3(p, a);
174
+ const double d1 = dotVec3(ab, ap);
175
+ const double d2 = dotVec3(ac, ap);
176
+ if (d1 <= 0.0 && d2 <= 0.0) {
177
+ outU = 0.0; outV = 0.0; outW = 1.0;
178
+ return a;
179
+ }
180
+
181
+ const DeformableVec3 bp = subVec3(p, b);
182
+ const double d3 = dotVec3(ab, bp);
183
+ const double d4 = dotVec3(ac, bp);
184
+ if (d3 >= 0.0 && d4 <= d3) {
185
+ outU = 1.0; outV = 0.0; outW = 0.0;
186
+ return b;
187
+ }
188
+
189
+ const double vc = d1 * d4 - d3 * d2;
190
+ if (vc <= 0.0 && d1 >= 0.0 && d3 <= 0.0) {
191
+ const double denom = d1 - d3;
192
+ const double v = (denom > kEpsilon) ? (d1 / denom) : 0.0;
193
+ outU = v; outV = 0.0; outW = 1.0 - v;
194
+ return addVec3(a, scaleVec3(ab, v));
195
+ }
196
+
197
+ const DeformableVec3 cp = subVec3(p, c);
198
+ const double d5 = dotVec3(ab, cp);
199
+ const double d6 = dotVec3(ac, cp);
200
+ if (d6 >= 0.0 && d5 <= d6) {
201
+ outU = 0.0; outV = 1.0; outW = 0.0;
202
+ return c;
203
+ }
204
+
205
+ const double vb = d5 * d2 - d1 * d6;
206
+ if (vb <= 0.0 && d2 >= 0.0 && d6 <= 0.0) {
207
+ const double denom = d2 - d6;
208
+ const double w = (denom > kEpsilon) ? (d2 / denom) : 0.0;
209
+ outU = 0.0; outV = w; outW = 1.0 - w;
210
+ return addVec3(a, scaleVec3(ac, w));
211
+ }
212
+
213
+ const double va = d3 * d6 - d5 * d4;
214
+ if (va <= 0.0 && (d4 - d3) >= 0.0 && (d5 - d6) >= 0.0) {
215
+ const double denom = (d4 - d3) + (d5 - d6);
216
+ const double w = (denom > kEpsilon) ? ((d4 - d3) / denom) : 0.0;
217
+ outU = 1.0 - w; outV = w; outW = 0.0;
218
+ return addVec3(b, scaleVec3(subVec3(c, b), w));
219
+ }
220
+
221
+ const double denom = 1.0 / (va + vb + vc);
222
+ const double v = vb * denom;
223
+ const double w = vc * denom;
224
+ const double u = 1.0 - v - w;
225
+ outU = v; outV = w; outW = u;
226
+ return addVec3(addVec3(scaleVec3(a, u), scaleVec3(b, v)), scaleVec3(c, w));
227
+ }
228
+
229
+ // ---------------------------------------------------------------------------
230
+ // Raycast Query
231
+ // ---------------------------------------------------------------------------
232
+ DeformableStatus raycastDeformableMesh(
233
+ std::span<const DeformableNode> nodes,
234
+ std::span<const DeformableTriangle> triangles,
235
+ const DeformableRayQuery& ray,
236
+ DeformableRayHit& outHit) {
237
+ outHit = {};
238
+ if (nodes.empty()) return DeformableStatus::NoHit;
239
+
240
+ const double dirLenSq = lengthSquaredVec3(ray.direction);
241
+ if (dirLenSq <= kZeroTolerance || ray.maxDistance <= 0.0) {
242
+ return DeformableStatus::InvalidInput;
243
+ }
244
+ const DeformableVec3 dir = normalizeVec3(ray.direction);
245
+
246
+ // Broadphase AABB test
247
+ const DeformableAABB bounds = computeDeformableBounds(nodes, 0.05);
248
+ double tmin = 0.0;
249
+ double tmax = ray.maxDistance;
250
+ for (int i = 0; i < 3; ++i) {
251
+ const double originVal = (i == 0 ? ray.origin.x : (i == 1 ? ray.origin.y : ray.origin.z));
252
+ const double dirVal = (i == 0 ? dir.x : (i == 1 ? dir.y : dir.z));
253
+ const double minVal = (i == 0 ? bounds.min.x : (i == 1 ? bounds.min.y : bounds.min.z));
254
+ const double maxVal = (i == 0 ? bounds.max.x : (i == 1 ? bounds.max.y : bounds.max.z));
255
+ if (std::abs(dirVal) < kZeroTolerance) {
256
+ if (originVal < minVal || originVal > maxVal) return DeformableStatus::NoHit;
257
+ } else {
258
+ double t1 = (minVal - originVal) / dirVal;
259
+ double t2 = (maxVal - originVal) / dirVal;
260
+ if (t1 > t2) std::swap(t1, t2);
261
+ tmin = std::max(tmin, t1);
262
+ tmax = std::min(tmax, t2);
263
+ if (tmin > tmax) return DeformableStatus::NoHit;
264
+ }
265
+ }
266
+
267
+ double bestDist = ray.maxDistance;
268
+ bool foundHit = false;
269
+
270
+ // 1. Surface Triangle Raycast (Möller–Trumbore)
271
+ if ((ray.flags & 1) != 0) {
272
+ for (std::size_t i = 0; i < triangles.size(); ++i) {
273
+ const auto& tri = triangles[i];
274
+ if (tri.nodeIndices[0] >= nodes.size() ||
275
+ tri.nodeIndices[1] >= nodes.size() ||
276
+ tri.nodeIndices[2] >= nodes.size()) {
277
+ continue;
278
+ }
279
+ const DeformableVec3& v0 = nodes[tri.nodeIndices[0]].position;
280
+ const DeformableVec3& v1 = nodes[tri.nodeIndices[1]].position;
281
+ const DeformableVec3& v2 = nodes[tri.nodeIndices[2]].position;
282
+
283
+ const DeformableVec3 edge1 = subVec3(v1, v0);
284
+ const DeformableVec3 edge2 = subVec3(v2, v0);
285
+ const DeformableVec3 pvec = crossVec3(dir, edge2);
286
+ const double det = dotVec3(edge1, pvec);
287
+ if (std::abs(det) < kZeroTolerance) continue;
288
+
289
+ const double invDet = 1.0 / det;
290
+ const DeformableVec3 tvec = subVec3(ray.origin, v0);
291
+ const double u = dotVec3(tvec, pvec) * invDet;
292
+ if (u < 0.0 || u > 1.0) continue;
293
+
294
+ const DeformableVec3 qvec = crossVec3(tvec, edge1);
295
+ const double v = dotVec3(dir, qvec) * invDet;
296
+ if (v < 0.0 || u + v > 1.0) continue;
297
+
298
+ const double t = dotVec3(edge2, qvec) * invDet;
299
+ if (t >= 0.0 && t <= bestDist) {
300
+ bestDist = t;
301
+ foundHit = true;
302
+
303
+ outHit.hit = true;
304
+ outHit.distance = t;
305
+ outHit.point = addVec3(ray.origin, scaleVec3(dir, t));
306
+ DeformableVec3 geomNormal = normalizeVec3(crossVec3(edge1, edge2));
307
+ if (dotVec3(geomNormal, dir) > 0.0) {
308
+ geomNormal = scaleVec3(geomNormal, -1.0);
309
+ }
310
+ outHit.normal = geomNormal;
311
+ outHit.elementIndex = static_cast<std::uint32_t>(i);
312
+
313
+ const double w = 1.0 - u - v;
314
+ outHit.barycentricU = u;
315
+ outHit.barycentricV = v;
316
+ outHit.barycentricW = w;
317
+
318
+ // Nearest node selection
319
+ if (w >= u && w >= v) {
320
+ outHit.nodeIndex = tri.nodeIndices[0];
321
+ } else if (u >= v) {
322
+ outHit.nodeIndex = tri.nodeIndices[1];
323
+ } else {
324
+ outHit.nodeIndex = tri.nodeIndices[2];
325
+ }
326
+
327
+ // Interpolated velocity at hit point
328
+ const DeformableVec3& vel0 = nodes[tri.nodeIndices[0]].velocity;
329
+ const DeformableVec3& vel1 = nodes[tri.nodeIndices[1]].velocity;
330
+ const DeformableVec3& vel2 = nodes[tri.nodeIndices[2]].velocity;
331
+ outHit.velocity = addVec3(
332
+ addVec3(scaleVec3(vel0, w), scaleVec3(vel1, u)),
333
+ scaleVec3(vel2, v));
334
+ }
335
+ }
336
+ }
337
+
338
+ // 2. Node Sphere Raycast (if flag enabled)
339
+ if ((ray.flags & 2) != 0) {
340
+ for (std::size_t i = 0; i < nodes.size(); ++i) {
341
+ const auto& node = nodes[i];
342
+ const double r = node.radius;
343
+ if (r <= 0.0) continue;
344
+
345
+ const DeformableVec3 m = subVec3(ray.origin, node.position);
346
+ const double b = dotVec3(m, dir);
347
+ const double c = dotVec3(m, m) - r * r;
348
+ if (c > 0.0 && b > 0.0) continue;
349
+
350
+ const double disc = b * b - c;
351
+ if (disc < 0.0) continue;
352
+
353
+ double t = -b - std::sqrt(disc);
354
+ if (t < 0.0) t = 0.0;
355
+ if (t <= bestDist) {
356
+ bestDist = t;
357
+ foundHit = true;
358
+
359
+ outHit.hit = true;
360
+ outHit.distance = t;
361
+ outHit.point = addVec3(ray.origin, scaleVec3(dir, t));
362
+ outHit.normal = normalizeVec3(subVec3(outHit.point, node.position));
363
+ outHit.elementIndex = std::numeric_limits<std::uint32_t>::max();
364
+ outHit.nodeIndex = static_cast<std::uint32_t>(i);
365
+ outHit.barycentricU = 0.0;
366
+ outHit.barycentricV = 0.0;
367
+ outHit.barycentricW = 1.0;
368
+ outHit.velocity = node.velocity;
369
+ }
370
+ }
371
+ }
372
+
373
+ return foundHit ? DeformableStatus::Success : DeformableStatus::NoHit;
374
+ }
375
+
376
+ // ---------------------------------------------------------------------------
377
+ // Overlap Query
378
+ // ---------------------------------------------------------------------------
379
+ DeformableStatus overlapDeformableMesh(
380
+ std::span<const DeformableNode> nodes,
381
+ std::span<const DeformableTriangle> triangles,
382
+ const DeformableOverlapQuery& query,
383
+ DeformableOverlapResult& outResult) {
384
+ outResult = {};
385
+ if (nodes.empty()) return DeformableStatus::NoHit;
386
+
387
+ std::uint32_t overlapCount = 0;
388
+ double maxPen = 0.0;
389
+ DeformableVec3 bestNormal{0.0, 1.0, 0.0};
390
+ DeformableVec3 bestPoint{0.0, 0.0, 0.0};
391
+ std::uint32_t bestElement = std::numeric_limits<std::uint32_t>::max();
392
+ std::uint32_t bestNode = 0;
393
+
394
+ switch (query.shapeType) {
395
+ case DeformableOverlapShapeType::Sphere: {
396
+ const double sphereRadius = query.halfExtents.x + query.margin;
397
+ const double radiusSq = sphereRadius * sphereRadius;
398
+
399
+ // Test against surface triangles
400
+ for (std::size_t i = 0; i < triangles.size(); ++i) {
401
+ const auto& tri = triangles[i];
402
+ if (tri.nodeIndices[0] >= nodes.size() ||
403
+ tri.nodeIndices[1] >= nodes.size() ||
404
+ tri.nodeIndices[2] >= nodes.size()) {
405
+ continue;
406
+ }
407
+ const DeformableVec3& v0 = nodes[tri.nodeIndices[0]].position;
408
+ const DeformableVec3& v1 = nodes[tri.nodeIndices[1]].position;
409
+ const DeformableVec3& v2 = nodes[tri.nodeIndices[2]].position;
410
+
411
+ double u = 0.0, v = 0.0, w = 0.0;
412
+ const DeformableVec3 cp = closestPointOnTriangle(query.center, v0, v1, v2, u, v, w);
413
+ const DeformableVec3 diff = subVec3(query.center, cp);
414
+ const double dsq = lengthSquaredVec3(diff);
415
+
416
+ if (dsq <= radiusSq) {
417
+ overlapCount += 1;
418
+ const double dist = std::sqrt(dsq);
419
+ const double pen = sphereRadius - dist;
420
+ if (pen > maxPen) {
421
+ maxPen = pen;
422
+ bestPoint = cp;
423
+ bestNormal = (dist > kEpsilon) ? normalizeVec3(diff) : normalizeVec3(crossVec3(subVec3(v1, v0), subVec3(v2, v0)));
424
+ bestElement = static_cast<std::uint32_t>(i);
425
+ bestNode = (w >= u && w >= v) ? tri.nodeIndices[0] : ((u >= v) ? tri.nodeIndices[1] : tri.nodeIndices[2]);
426
+ }
427
+ }
428
+ }
429
+
430
+ // Test against individual node spheres
431
+ for (std::size_t i = 0; i < nodes.size(); ++i) {
432
+ const auto& node = nodes[i];
433
+ const double combinedR = sphereRadius + node.radius;
434
+ const DeformableVec3 diff = subVec3(query.center, node.position);
435
+ const double dsq = lengthSquaredVec3(diff);
436
+ if (dsq <= combinedR * combinedR) {
437
+ overlapCount += 1;
438
+ const double dist = std::sqrt(dsq);
439
+ const double pen = combinedR - dist;
440
+ if (pen > maxPen) {
441
+ maxPen = pen;
442
+ bestPoint = addVec3(node.position, scaleVec3(normalizeVec3(diff), node.radius));
443
+ bestNormal = (dist > kEpsilon) ? normalizeVec3(diff) : DeformableVec3{0.0, 1.0, 0.0};
444
+ bestElement = std::numeric_limits<std::uint32_t>::max();
445
+ bestNode = static_cast<std::uint32_t>(i);
446
+ }
447
+ }
448
+ }
449
+ break;
450
+ }
451
+
452
+ case DeformableOverlapShapeType::Box: {
453
+ const DeformableVec3 boxExt = addVec3(query.halfExtents, {query.margin, query.margin, query.margin});
454
+ for (std::size_t i = 0; i < triangles.size(); ++i) {
455
+ const auto& tri = triangles[i];
456
+ if (tri.nodeIndices[0] >= nodes.size() ||
457
+ tri.nodeIndices[1] >= nodes.size() ||
458
+ tri.nodeIndices[2] >= nodes.size()) {
459
+ continue;
460
+ }
461
+ // Transform triangle vertices to box local coordinates
462
+ const DeformableVec3 v0 = rotateByConjugateQuat(query.orientation, subVec3(nodes[tri.nodeIndices[0]].position, query.center));
463
+ const DeformableVec3 v1 = rotateByConjugateQuat(query.orientation, subVec3(nodes[tri.nodeIndices[1]].position, query.center));
464
+ const DeformableVec3 v2 = rotateByConjugateQuat(query.orientation, subVec3(nodes[tri.nodeIndices[2]].position, query.center));
465
+
466
+ // 13 SAT axes test
467
+ // 3 box face axes
468
+ const double minX = std::min({v0.x, v1.x, v2.x});
469
+ const double maxX = std::max({v0.x, v1.x, v2.x});
470
+ if (minX > boxExt.x || maxX < -boxExt.x) continue;
471
+
472
+ const double minY = std::min({v0.y, v1.y, v2.y});
473
+ const double maxY = std::max({v0.y, v1.y, v2.y});
474
+ if (minY > boxExt.y || maxY < -boxExt.y) continue;
475
+
476
+ const double minZ = std::min({v0.z, v1.z, v2.z});
477
+ const double maxZ = std::max({v0.z, v1.z, v2.z});
478
+ if (minZ > boxExt.z || maxZ < -boxExt.z) continue;
479
+
480
+ // Triangle normal axis
481
+ const DeformableVec3 triNorm = crossVec3(subVec3(v1, v0), subVec3(v2, v0));
482
+ const double normLen = lengthVec3(triNorm);
483
+ if (normLen <= kZeroTolerance) continue;
484
+ const DeformableVec3 unitNorm = scaleVec3(triNorm, 1.0 / normLen);
485
+
486
+ const double boxRadiusOnNorm = boxExt.x * std::abs(unitNorm.x) + boxExt.y * std::abs(unitNorm.y) + boxExt.z * std::abs(unitNorm.z);
487
+ const double triProj = dotVec3(v0, unitNorm);
488
+ if (std::abs(triProj) > boxRadiusOnNorm) continue;
489
+
490
+ // Passed face tests -> overlap detected
491
+ overlapCount += 1;
492
+ const double pen = boxRadiusOnNorm - std::abs(triProj);
493
+ if (pen > maxPen) {
494
+ maxPen = pen;
495
+ const DeformableVec3 worldNorm = rotateByQuat(query.orientation, unitNorm);
496
+ bestNormal = (triProj < 0.0) ? worldNorm : scaleVec3(worldNorm, -1.0);
497
+ bestPoint = rotateByQuat(query.orientation, scaleVec3(addVec3(addVec3(v0, v1), v2), 1.0 / 3.0));
498
+ bestPoint = addVec3(bestPoint, query.center);
499
+ bestElement = static_cast<std::uint32_t>(i);
500
+ bestNode = tri.nodeIndices[0];
501
+ }
502
+ }
503
+ break;
504
+ }
505
+
506
+ case DeformableOverlapShapeType::Capsule: {
507
+ const double capRadius = query.halfExtents.x + query.margin;
508
+ const DeformableVec3 capA = query.center;
509
+ const DeformableVec3 capB = query.secondaryPoint;
510
+ const DeformableVec3 capDir = subVec3(capB, capA);
511
+ const double capLenSq = lengthSquaredVec3(capDir);
512
+
513
+ for (std::size_t i = 0; i < triangles.size(); ++i) {
514
+ const auto& tri = triangles[i];
515
+ if (tri.nodeIndices[0] >= nodes.size() ||
516
+ tri.nodeIndices[1] >= nodes.size() ||
517
+ tri.nodeIndices[2] >= nodes.size()) {
518
+ continue;
519
+ }
520
+ const DeformableVec3& v0 = nodes[tri.nodeIndices[0]].position;
521
+ const DeformableVec3& v1 = nodes[tri.nodeIndices[1]].position;
522
+ const DeformableVec3& v2 = nodes[tri.nodeIndices[2]].position;
523
+
524
+ // Sample points along capsule segment
525
+ for (int step = 0; step <= 4; ++step) {
526
+ const double t = step * 0.25;
527
+ const DeformableVec3 pt = (capLenSq > kZeroTolerance) ? addVec3(capA, scaleVec3(capDir, t)) : capA;
528
+ double u = 0.0, v = 0.0, w = 0.0;
529
+ const DeformableVec3 cp = closestPointOnTriangle(pt, v0, v1, v2, u, v, w);
530
+ const double distSq = lengthSquaredVec3(subVec3(pt, cp));
531
+ if (distSq <= capRadius * capRadius) {
532
+ overlapCount += 1;
533
+ const double dist = std::sqrt(distSq);
534
+ const double pen = capRadius - dist;
535
+ if (pen > maxPen) {
536
+ maxPen = pen;
537
+ bestPoint = cp;
538
+ bestNormal = (dist > kEpsilon) ? normalizeVec3(subVec3(pt, cp)) : normalizeVec3(crossVec3(subVec3(v1, v0), subVec3(v2, v0)));
539
+ bestElement = static_cast<std::uint32_t>(i);
540
+ bestNode = tri.nodeIndices[0];
541
+ }
542
+ break;
543
+ }
544
+ }
545
+ }
546
+ break;
547
+ }
548
+
549
+ case DeformableOverlapShapeType::Point: {
550
+ const double ptMargin = query.margin;
551
+ for (std::size_t i = 0; i < triangles.size(); ++i) {
552
+ const auto& tri = triangles[i];
553
+ if (tri.nodeIndices[0] >= nodes.size() ||
554
+ tri.nodeIndices[1] >= nodes.size() ||
555
+ tri.nodeIndices[2] >= nodes.size()) {
556
+ continue;
557
+ }
558
+ const DeformableVec3& v0 = nodes[tri.nodeIndices[0]].position;
559
+ const DeformableVec3& v1 = nodes[tri.nodeIndices[1]].position;
560
+ const DeformableVec3& v2 = nodes[tri.nodeIndices[2]].position;
561
+
562
+ double u = 0.0, v = 0.0, w = 0.0;
563
+ const DeformableVec3 cp = closestPointOnTriangle(query.center, v0, v1, v2, u, v, w);
564
+ const double distSq = lengthSquaredVec3(subVec3(query.center, cp));
565
+ if (distSq <= ptMargin * ptMargin) {
566
+ overlapCount += 1;
567
+ const double dist = std::sqrt(distSq);
568
+ const double pen = ptMargin - dist;
569
+ if (pen > maxPen) {
570
+ maxPen = pen;
571
+ bestPoint = cp;
572
+ bestNormal = (dist > kEpsilon) ? normalizeVec3(subVec3(query.center, cp)) : DeformableVec3{0.0, 1.0, 0.0};
573
+ bestElement = static_cast<std::uint32_t>(i);
574
+ bestNode = tri.nodeIndices[0];
575
+ }
576
+ }
577
+ }
578
+ break;
579
+ }
580
+ }
581
+
582
+ if (overlapCount > 0) {
583
+ outResult.hasOverlap = true;
584
+ outResult.overlapCount = overlapCount;
585
+ outResult.maxPenetration = maxPen;
586
+ outResult.contactNormal = bestNormal;
587
+ outResult.contactPoint = bestPoint;
588
+ outResult.elementIndex = bestElement;
589
+ outResult.nodeIndex = bestNode;
590
+ return DeformableStatus::Success;
591
+ }
592
+
593
+ return DeformableStatus::NoHit;
594
+ }
595
+
596
+ // ---------------------------------------------------------------------------
597
+ // Sweep Query (Continuous Collision Detection / Shape Cast)
598
+ // ---------------------------------------------------------------------------
599
+ DeformableStatus sweepDeformableMesh(
600
+ std::span<const DeformableNode> nodes,
601
+ std::span<const DeformableTriangle> triangles,
602
+ const DeformableSweepQuery& query,
603
+ DeformableSweepHit& outHit) {
604
+ outHit = {};
605
+ if (nodes.empty()) return DeformableStatus::NoHit;
606
+
607
+ const double dirLenSq = lengthSquaredVec3(query.direction);
608
+ if (dirLenSq <= kZeroTolerance || query.maxDistance <= 0.0) {
609
+ return DeformableStatus::InvalidInput;
610
+ }
611
+ const DeformableVec3 dir = normalizeVec3(query.direction);
612
+ const double sweepRadius = (query.shapeType == DeformableSweepShapeType::Point) ? 0.0 : query.radius;
613
+
614
+ double bestDist = query.maxDistance;
615
+ bool foundHit = false;
616
+
617
+ for (std::size_t i = 0; i < triangles.size(); ++i) {
618
+ const auto& tri = triangles[i];
619
+ if (tri.nodeIndices[0] >= nodes.size() ||
620
+ tri.nodeIndices[1] >= nodes.size() ||
621
+ tri.nodeIndices[2] >= nodes.size()) {
622
+ continue;
623
+ }
624
+ const DeformableVec3& v0 = nodes[tri.nodeIndices[0]].position;
625
+ const DeformableVec3& v1 = nodes[tri.nodeIndices[1]].position;
626
+ const DeformableVec3& v2 = nodes[tri.nodeIndices[2]].position;
627
+
628
+ const DeformableVec3 fn = crossVec3(subVec3(v1, v0), subVec3(v2, v0));
629
+ const double fnLen = lengthVec3(fn);
630
+ if (fnLen <= kZeroTolerance) continue;
631
+ const DeformableVec3 triNorm = scaleVec3(fn, 1.0 / fnLen);
632
+
633
+ // Swept sphere vs triangle test
634
+ // 1. Plane test
635
+ const double distStart = dotVec3(subVec3(query.startCenter, v0), triNorm);
636
+ const double denom = dotVec3(dir, triNorm);
637
+
638
+ if (std::abs(denom) > kZeroTolerance) {
639
+ const double sign = (distStart >= 0.0) ? 1.0 : -1.0;
640
+ const double targetDist = sign * sweepRadius;
641
+ const double t = (targetDist - distStart) / denom;
642
+
643
+ if (t >= 0.0 && t <= bestDist) {
644
+ const DeformableVec3 centerAtT = addVec3(query.startCenter, scaleVec3(dir, t));
645
+ double u = 0.0, v = 0.0, w = 0.0;
646
+ const DeformableVec3 cp = closestPointOnTriangle(centerAtT, v0, v1, v2, u, v, w);
647
+ const double dsq = lengthSquaredVec3(subVec3(centerAtT, cp));
648
+
649
+ if (dsq <= (sweepRadius + 1e-6) * (sweepRadius + 1e-6)) {
650
+ bestDist = t;
651
+ foundHit = true;
652
+ outHit.hit = true;
653
+ outHit.distance = t;
654
+ outHit.point = cp;
655
+ outHit.normal = (sign >= 0.0) ? triNorm : scaleVec3(triNorm, -1.0);
656
+ outHit.elementIndex = static_cast<std::uint32_t>(i);
657
+ outHit.nodeIndex = (w >= u && w >= v) ? tri.nodeIndices[0] : ((u >= v) ? tri.nodeIndices[1] : tri.nodeIndices[2]);
658
+ outHit.barycentricU = u;
659
+ outHit.barycentricV = v;
660
+ outHit.barycentricW = w;
661
+ outHit.timeOfImpact = t / query.maxDistance;
662
+ }
663
+ }
664
+ }
665
+
666
+ // 2. Cylinder Edge Tests
667
+ if (sweepRadius > 0.0) {
668
+ for (int e = 0; e < 3; ++e) {
669
+ const DeformableVec3& ea = (e == 0 ? v0 : (e == 1 ? v1 : v2));
670
+ const DeformableVec3& eb = (e == 0 ? v1 : (e == 1 ? v2 : v0));
671
+ const DeformableVec3 edge = subVec3(eb, ea);
672
+ const double edgeLenSq = lengthSquaredVec3(edge);
673
+ if (edgeLenSq <= kZeroTolerance) continue;
674
+
675
+ const DeformableVec3 edgeDir = scaleVec3(edge, 1.0 / std::sqrt(edgeLenSq));
676
+ const DeformableVec3 m = subVec3(query.startCenter, ea);
677
+ const DeformableVec3 dPerp = subVec3(dir, scaleVec3(edgeDir, dotVec3(dir, edgeDir)));
678
+ const DeformableVec3 mPerp = subVec3(m, scaleVec3(edgeDir, dotVec3(m, edgeDir)));
679
+
680
+ const double A = dotVec3(dPerp, dPerp);
681
+ const double B = 2.0 * dotVec3(dPerp, mPerp);
682
+ const double C = dotVec3(mPerp, mPerp) - sweepRadius * sweepRadius;
683
+
684
+ if (A > kZeroTolerance) {
685
+ const double disc = B * B - 4.0 * A * C;
686
+ if (disc >= 0.0) {
687
+ const double t = (-B - std::sqrt(disc)) / (2.0 * A);
688
+ if (t >= 0.0 && t <= bestDist) {
689
+ const DeformableVec3 centerAtT = addVec3(query.startCenter, scaleVec3(dir, t));
690
+ const double edgeParam = dotVec3(subVec3(centerAtT, ea), edgeDir);
691
+ if (edgeParam >= 0.0 && edgeParam * edgeParam <= edgeLenSq) {
692
+ bestDist = t;
693
+ foundHit = true;
694
+ const DeformableVec3 ptOnEdge = addVec3(ea, scaleVec3(edgeDir, edgeParam));
695
+ outHit.hit = true;
696
+ outHit.distance = t;
697
+ outHit.point = ptOnEdge;
698
+ outHit.normal = normalizeVec3(subVec3(centerAtT, ptOnEdge));
699
+ outHit.elementIndex = static_cast<std::uint32_t>(i);
700
+ outHit.nodeIndex = (e == 0 ? tri.nodeIndices[0] : (e == 1 ? tri.nodeIndices[1] : tri.nodeIndices[2]));
701
+ outHit.timeOfImpact = t / query.maxDistance;
702
+ }
703
+ }
704
+ }
705
+ }
706
+ }
707
+
708
+ // 3. Vertex Sphere Tests
709
+ for (int vidx = 0; vidx < 3; ++vidx) {
710
+ const DeformableVec3& vert = (vidx == 0 ? v0 : (vidx == 1 ? v1 : v2));
711
+ const DeformableVec3 m = subVec3(query.startCenter, vert);
712
+ const double b = dotVec3(m, dir);
713
+ const double c = dotVec3(m, m) - sweepRadius * sweepRadius;
714
+ if (c > 0.0 && b > 0.0) continue;
715
+
716
+ const double disc = b * b - c;
717
+ if (disc >= 0.0) {
718
+ const double t = -b - std::sqrt(disc);
719
+ if (t >= 0.0 && t <= bestDist) {
720
+ bestDist = t;
721
+ foundHit = true;
722
+ outHit.hit = true;
723
+ outHit.distance = t;
724
+ outHit.point = vert;
725
+ outHit.normal = normalizeVec3(subVec3(addVec3(query.startCenter, scaleVec3(dir, t)), vert));
726
+ outHit.elementIndex = static_cast<std::uint32_t>(i);
727
+ outHit.nodeIndex = tri.nodeIndices[vidx];
728
+ outHit.timeOfImpact = t / query.maxDistance;
729
+ }
730
+ }
731
+ }
732
+ }
733
+ }
734
+
735
+ return foundHit ? DeformableStatus::Success : DeformableStatus::NoHit;
736
+ }
737
+
738
+ // ---------------------------------------------------------------------------
739
+ // Snapshot Capture, Restore, and Verification
740
+ // ---------------------------------------------------------------------------
741
+ std::size_t calculateSnapshotSize(
742
+ std::uint32_t nodeCount,
743
+ std::uint32_t triangleCount,
744
+ std::uint32_t tetraCount,
745
+ std::uint32_t constraintCount,
746
+ std::uint32_t contactCount,
747
+ bool hasPressure) {
748
+ const std::size_t payloadBytes =
749
+ nodeCount * kDeformableNodeStride * sizeof(double) +
750
+ triangleCount * kDeformableTriangleStride * sizeof(double) +
751
+ tetraCount * kDeformableTetraStride * sizeof(double) +
752
+ constraintCount * kDeformableConstraintStride * sizeof(double) +
753
+ contactCount * kDeformableContactStride * sizeof(double) +
754
+ (hasPressure ? kDeformablePressureStride * sizeof(double) : 0);
755
+ return sizeof(DeformableSnapshotHeader) + payloadBytes;
756
+ }
757
+
758
+ DeformableStatus captureDeformableSnapshot(
759
+ std::uint64_t frame,
760
+ std::span<const DeformableNode> nodes,
761
+ std::span<const DeformableTriangle> triangles,
762
+ std::span<const DeformableTetrahedron> tetras,
763
+ std::span<const DeformableConstraint> constraints,
764
+ std::span<const DeformableContact> contacts,
765
+ const DeformablePressureState* pressure,
766
+ std::span<std::uint8_t> outBuffer,
767
+ std::size_t& outWrittenBytes) {
768
+ const bool hasPressure = (pressure != nullptr);
769
+ const std::size_t requiredSize = calculateSnapshotSize(
770
+ static_cast<std::uint32_t>(nodes.size()),
771
+ static_cast<std::uint32_t>(triangles.size()),
772
+ static_cast<std::uint32_t>(tetras.size()),
773
+ static_cast<std::uint32_t>(constraints.size()),
774
+ static_cast<std::uint32_t>(contacts.size()),
775
+ hasPressure);
776
+
777
+ if (outBuffer.size() < requiredSize) {
778
+ return DeformableStatus::BufferTooSmall;
779
+ }
780
+
781
+ DeformableSnapshotHeader header{};
782
+ std::memcpy(header.magic, kDeformableSnapshotMagic.data(), 4);
783
+ header.version = kDeformableSnapshotVersion;
784
+ header.frame = frame;
785
+ header.nodeCount = static_cast<std::uint32_t>(nodes.size());
786
+ header.triangleCount = static_cast<std::uint32_t>(triangles.size());
787
+ header.tetraCount = static_cast<std::uint32_t>(tetras.size());
788
+ header.constraintCount = static_cast<std::uint32_t>(constraints.size());
789
+ header.contactCount = static_cast<std::uint32_t>(contacts.size());
790
+ header.hasPressure = hasPressure ? 1 : 0;
791
+ header.payloadBytes = requiredSize - sizeof(DeformableSnapshotHeader);
792
+
793
+ std::uint8_t* writePtr = outBuffer.data() + sizeof(DeformableSnapshotHeader);
794
+
795
+ // Write nodes
796
+ for (std::size_t i = 0; i < nodes.size(); ++i) {
797
+ const auto& n = nodes[i];
798
+ double rec[kDeformableNodeStride] = {
799
+ n.position.x, n.position.y, n.position.z,
800
+ n.previousPosition.x, n.previousPosition.y, n.previousPosition.z,
801
+ n.velocity.x, n.velocity.y, n.velocity.z,
802
+ n.invMass,
803
+ n.normal.x, n.normal.y, n.normal.z,
804
+ n.radius
805
+ };
806
+ std::memcpy(writePtr, rec, sizeof(rec));
807
+ writePtr += sizeof(rec);
808
+ }
809
+
810
+ // Write triangles
811
+ for (std::size_t i = 0; i < triangles.size(); ++i) {
812
+ const auto& t = triangles[i];
813
+ double rec[kDeformableTriangleStride] = {
814
+ static_cast<double>(t.nodeIndices[0]),
815
+ static_cast<double>(t.nodeIndices[1]),
816
+ static_cast<double>(t.nodeIndices[2]),
817
+ t.restArea,
818
+ t.restNormal.x, t.restNormal.y, t.restNormal.z,
819
+ static_cast<double>(t.materialTag),
820
+ 0.0, 0.0
821
+ };
822
+ std::memcpy(writePtr, rec, sizeof(rec));
823
+ writePtr += sizeof(rec);
824
+ }
825
+
826
+ // Write tetrahedra
827
+ for (std::size_t i = 0; i < tetras.size(); ++i) {
828
+ const auto& tet = tetras[i];
829
+ double rec[kDeformableTetraStride] = {
830
+ static_cast<double>(tet.nodeIndices[0]),
831
+ static_cast<double>(tet.nodeIndices[1]),
832
+ static_cast<double>(tet.nodeIndices[2]),
833
+ static_cast<double>(tet.nodeIndices[3]),
834
+ tet.restVolume,
835
+ static_cast<double>(tet.materialTag)
836
+ };
837
+ std::memcpy(writePtr, rec, sizeof(rec));
838
+ writePtr += sizeof(rec);
839
+ }
840
+
841
+ // Write constraints
842
+ for (std::size_t i = 0; i < constraints.size(); ++i) {
843
+ const auto& c = constraints[i];
844
+ double rec[kDeformableConstraintStride] = {
845
+ static_cast<double>(c.kind),
846
+ static_cast<double>(c.nodeIndices[0]),
847
+ static_cast<double>(c.nodeIndices[1]),
848
+ static_cast<double>(c.nodeIndices[2]),
849
+ static_cast<double>(c.nodeIndices[3]),
850
+ c.restValue,
851
+ c.stiffness,
852
+ c.damping,
853
+ c.accumulatedImpulse,
854
+ c.broken ? 1.0 : 0.0
855
+ };
856
+ std::memcpy(writePtr, rec, sizeof(rec));
857
+ writePtr += sizeof(rec);
858
+ }
859
+
860
+ // Write contacts
861
+ for (std::size_t i = 0; i < contacts.size(); ++i) {
862
+ const auto& ct = contacts[i];
863
+ double rec[kDeformableContactStride] = {
864
+ static_cast<double>(ct.contactType),
865
+ static_cast<double>(ct.nodeIndex),
866
+ static_cast<double>(ct.elementIndex),
867
+ ct.point.x, ct.point.y, ct.point.z,
868
+ ct.normal.x, ct.normal.y, ct.normal.z,
869
+ ct.penetration,
870
+ ct.normalImpulse,
871
+ ct.frictionImpulses[0], ct.frictionImpulses[1],
872
+ ct.active ? 1.0 : 0.0
873
+ };
874
+ std::memcpy(writePtr, rec, sizeof(rec));
875
+ writePtr += sizeof(rec);
876
+ }
877
+
878
+ // Write pressure
879
+ if (hasPressure) {
880
+ double rec[kDeformablePressureStride] = {
881
+ static_cast<double>(pressure->pressureType),
882
+ pressure->restVolume,
883
+ pressure->currentVolume,
884
+ pressure->targetVolume,
885
+ pressure->internalPressure,
886
+ pressure->overpressureScale,
887
+ pressure->accumulatedImpulse,
888
+ pressure->gasConstant
889
+ };
890
+ std::memcpy(writePtr, rec, sizeof(rec));
891
+ writePtr += sizeof(rec);
892
+ }
893
+
894
+ // Compute checksum over payload
895
+ const std::uint8_t* payloadStart = outBuffer.data() + sizeof(DeformableSnapshotHeader);
896
+ header.checksum = computeFnv1a32(payloadStart, header.payloadBytes);
897
+
898
+ // Write header
899
+ std::memcpy(outBuffer.data(), &header, sizeof(header));
900
+ outWrittenBytes = requiredSize;
901
+ return DeformableStatus::Success;
902
+ }
903
+
904
+ DeformableStatus restoreDeformableSnapshot(
905
+ std::span<const std::uint8_t> inBuffer,
906
+ std::uint64_t& outFrame,
907
+ std::span<DeformableNode> outNodes,
908
+ std::uint32_t& outNodeCount,
909
+ std::span<DeformableTriangle> outTriangles,
910
+ std::uint32_t& outTriangleCount,
911
+ std::span<DeformableTetrahedron> outTetras,
912
+ std::uint32_t& outTetraCount,
913
+ std::span<DeformableConstraint> outConstraints,
914
+ std::uint32_t& outConstraintCount,
915
+ std::span<DeformableContact> outContacts,
916
+ std::uint32_t& outContactCount,
917
+ DeformablePressureState* outPressure,
918
+ bool& outHasPressure) {
919
+ if (inBuffer.size() < sizeof(DeformableSnapshotHeader)) {
920
+ return DeformableStatus::SnapshotTruncated;
921
+ }
922
+
923
+ DeformableSnapshotHeader header{};
924
+ std::memcpy(&header, inBuffer.data(), sizeof(header));
925
+
926
+ if (std::memcmp(header.magic, kDeformableSnapshotMagic.data(), 4) != 0) {
927
+ return DeformableStatus::SnapshotMalformed;
928
+ }
929
+ if (header.version != kDeformableSnapshotVersion) {
930
+ return DeformableStatus::SnapshotVersionMismatch;
931
+ }
932
+ if (inBuffer.size() < sizeof(header) + header.payloadBytes) {
933
+ return DeformableStatus::SnapshotTruncated;
934
+ }
935
+
936
+ // Verify checksum
937
+ const std::uint8_t* payloadStart = inBuffer.data() + sizeof(header);
938
+ const std::uint32_t calcSum = computeFnv1a32(payloadStart, header.payloadBytes);
939
+ if (calcSum != header.checksum) {
940
+ return DeformableStatus::SnapshotChecksumMismatch;
941
+ }
942
+
943
+ outFrame = header.frame;
944
+ outNodeCount = header.nodeCount;
945
+ outTriangleCount = header.triangleCount;
946
+ outTetraCount = header.tetraCount;
947
+ outConstraintCount = header.constraintCount;
948
+ outContactCount = header.contactCount;
949
+ outHasPressure = (header.hasPressure != 0);
950
+
951
+ const std::uint8_t* readPtr = payloadStart;
952
+
953
+ // Unpack nodes
954
+ for (std::uint32_t i = 0; i < header.nodeCount; ++i) {
955
+ double rec[kDeformableNodeStride];
956
+ std::memcpy(rec, readPtr, sizeof(rec));
957
+ readPtr += sizeof(rec);
958
+ if (i < outNodes.size()) {
959
+ outNodes[i].position = {rec[0], rec[1], rec[2]};
960
+ outNodes[i].previousPosition = {rec[3], rec[4], rec[5]};
961
+ outNodes[i].velocity = {rec[6], rec[7], rec[8]};
962
+ outNodes[i].invMass = rec[9];
963
+ outNodes[i].normal = {rec[10], rec[11], rec[12]};
964
+ outNodes[i].radius = rec[13];
965
+ }
966
+ }
967
+
968
+ // Unpack triangles
969
+ for (std::uint32_t i = 0; i < header.triangleCount; ++i) {
970
+ double rec[kDeformableTriangleStride];
971
+ std::memcpy(rec, readPtr, sizeof(rec));
972
+ readPtr += sizeof(rec);
973
+ if (i < outTriangles.size()) {
974
+ outTriangles[i].nodeIndices[0] = static_cast<std::uint32_t>(rec[0]);
975
+ outTriangles[i].nodeIndices[1] = static_cast<std::uint32_t>(rec[1]);
976
+ outTriangles[i].nodeIndices[2] = static_cast<std::uint32_t>(rec[2]);
977
+ outTriangles[i].restArea = rec[3];
978
+ outTriangles[i].restNormal = {rec[4], rec[5], rec[6]};
979
+ outTriangles[i].materialTag = static_cast<std::uint32_t>(rec[7]);
980
+ }
981
+ }
982
+
983
+ // Unpack tetrahedra
984
+ for (std::uint32_t i = 0; i < header.tetraCount; ++i) {
985
+ double rec[kDeformableTetraStride];
986
+ std::memcpy(rec, readPtr, sizeof(rec));
987
+ readPtr += sizeof(rec);
988
+ if (i < outTetras.size()) {
989
+ outTetras[i].nodeIndices[0] = static_cast<std::uint32_t>(rec[0]);
990
+ outTetras[i].nodeIndices[1] = static_cast<std::uint32_t>(rec[1]);
991
+ outTetras[i].nodeIndices[2] = static_cast<std::uint32_t>(rec[2]);
992
+ outTetras[i].nodeIndices[3] = static_cast<std::uint32_t>(rec[3]);
993
+ outTetras[i].restVolume = rec[4];
994
+ outTetras[i].materialTag = static_cast<std::uint32_t>(rec[5]);
995
+ }
996
+ }
997
+
998
+ // Unpack constraints
999
+ for (std::uint32_t i = 0; i < header.constraintCount; ++i) {
1000
+ double rec[kDeformableConstraintStride];
1001
+ std::memcpy(rec, readPtr, sizeof(rec));
1002
+ readPtr += sizeof(rec);
1003
+ if (i < outConstraints.size()) {
1004
+ outConstraints[i].kind = static_cast<DeformableConstraintKind>(static_cast<std::uint32_t>(rec[0]));
1005
+ outConstraints[i].nodeIndices[0] = static_cast<std::uint32_t>(rec[1]);
1006
+ outConstraints[i].nodeIndices[1] = static_cast<std::uint32_t>(rec[2]);
1007
+ outConstraints[i].nodeIndices[2] = static_cast<std::uint32_t>(rec[3]);
1008
+ outConstraints[i].nodeIndices[3] = static_cast<std::uint32_t>(rec[4]);
1009
+ outConstraints[i].restValue = rec[5];
1010
+ outConstraints[i].stiffness = rec[6];
1011
+ outConstraints[i].damping = rec[7];
1012
+ outConstraints[i].accumulatedImpulse = rec[8];
1013
+ outConstraints[i].broken = (rec[9] > 0.5);
1014
+ }
1015
+ }
1016
+
1017
+ // Unpack contacts
1018
+ for (std::uint32_t i = 0; i < header.contactCount; ++i) {
1019
+ double rec[kDeformableContactStride];
1020
+ std::memcpy(rec, readPtr, sizeof(rec));
1021
+ readPtr += sizeof(rec);
1022
+ if (i < outContacts.size()) {
1023
+ outContacts[i].contactType = static_cast<DeformableContactType>(static_cast<std::uint32_t>(rec[0]));
1024
+ outContacts[i].nodeIndex = static_cast<std::uint32_t>(rec[1]);
1025
+ outContacts[i].elementIndex = static_cast<std::uint32_t>(rec[2]);
1026
+ outContacts[i].point = {rec[3], rec[4], rec[5]};
1027
+ outContacts[i].normal = {rec[6], rec[7], rec[8]};
1028
+ outContacts[i].penetration = rec[9];
1029
+ outContacts[i].normalImpulse = rec[10];
1030
+ outContacts[i].frictionImpulses[0] = rec[11];
1031
+ outContacts[i].frictionImpulses[1] = rec[12];
1032
+ outContacts[i].active = (rec[13] > 0.5);
1033
+ }
1034
+ }
1035
+
1036
+ // Unpack pressure
1037
+ if (header.hasPressure != 0 && outPressure != nullptr) {
1038
+ double rec[kDeformablePressureStride];
1039
+ std::memcpy(rec, readPtr, sizeof(rec));
1040
+ outPressure->pressureType = static_cast<DeformablePressureType>(static_cast<std::uint32_t>(rec[0]));
1041
+ outPressure->restVolume = rec[1];
1042
+ outPressure->currentVolume = rec[2];
1043
+ outPressure->targetVolume = rec[3];
1044
+ outPressure->internalPressure = rec[4];
1045
+ outPressure->overpressureScale = rec[5];
1046
+ outPressure->accumulatedImpulse = rec[6];
1047
+ outPressure->gasConstant = rec[7];
1048
+ }
1049
+
1050
+ return DeformableStatus::Success;
1051
+ }
1052
+
1053
+ DeformableStatus verifyDeformableSnapshot(std::span<const std::uint8_t> inBuffer) {
1054
+ if (inBuffer.size() < sizeof(DeformableSnapshotHeader)) {
1055
+ return DeformableStatus::SnapshotTruncated;
1056
+ }
1057
+ DeformableSnapshotHeader header{};
1058
+ std::memcpy(&header, inBuffer.data(), sizeof(header));
1059
+ if (std::memcmp(header.magic, kDeformableSnapshotMagic.data(), 4) != 0) {
1060
+ return DeformableStatus::SnapshotMalformed;
1061
+ }
1062
+ if (header.version != kDeformableSnapshotVersion) {
1063
+ return DeformableStatus::SnapshotVersionMismatch;
1064
+ }
1065
+ if (inBuffer.size() != sizeof(header) + header.payloadBytes) {
1066
+ return DeformableStatus::SnapshotTruncated;
1067
+ }
1068
+ const std::uint8_t* payloadStart = inBuffer.data() + sizeof(header);
1069
+ const std::uint32_t calcSum = computeFnv1a32(payloadStart, header.payloadBytes);
1070
+ if (calcSum != header.checksum) {
1071
+ return DeformableStatus::SnapshotChecksumMismatch;
1072
+ }
1073
+ return DeformableStatus::Success;
1074
+ }
1075
+
1076
+ } // namespace rundot::kinetix::physics
1077
+
1078
+ // ---------------------------------------------------------------------------
1079
+ // C ABI Implementation
1080
+ // ---------------------------------------------------------------------------
1081
+ extern "C" {
1082
+
1083
+ using namespace rundot::kinetix::physics;
1084
+
1085
+ std::uint32_t rundot_kinetix_ext_deformable_abi_version() {
1086
+ return kDeformableAbiVersion;
1087
+ }
1088
+
1089
+ std::uint32_t rundot_kinetix_ext_deformable_snapshot_version() {
1090
+ return kDeformableSnapshotVersion;
1091
+ }
1092
+
1093
+ std::uint32_t rundot_kinetix_ext_deformable_node_stride() {
1094
+ return kDeformableNodeStride;
1095
+ }
1096
+
1097
+ std::uint32_t rundot_kinetix_ext_deformable_triangle_stride() {
1098
+ return kDeformableTriangleStride;
1099
+ }
1100
+
1101
+ std::uint32_t rundot_kinetix_ext_deformable_tetra_stride() {
1102
+ return kDeformableTetraStride;
1103
+ }
1104
+
1105
+ std::uint32_t rundot_kinetix_ext_deformable_constraint_stride() {
1106
+ return kDeformableConstraintStride;
1107
+ }
1108
+
1109
+ std::uint32_t rundot_kinetix_ext_deformable_contact_stride() {
1110
+ return kDeformableContactStride;
1111
+ }
1112
+
1113
+ std::uint32_t rundot_kinetix_ext_deformable_pressure_stride() {
1114
+ return kDeformablePressureStride;
1115
+ }
1116
+
1117
+ std::uint32_t rundot_kinetix_ext_deformable_ray_query_stride() {
1118
+ return kDeformableRayQueryStride;
1119
+ }
1120
+
1121
+ std::uint32_t rundot_kinetix_ext_deformable_ray_hit_stride() {
1122
+ return kDeformableRayHitStride;
1123
+ }
1124
+
1125
+ std::uint32_t rundot_kinetix_ext_deformable_overlap_query_stride() {
1126
+ return kDeformableOverlapQueryStride;
1127
+ }
1128
+
1129
+ std::uint32_t rundot_kinetix_ext_deformable_overlap_result_stride() {
1130
+ return kDeformableOverlapResultStride;
1131
+ }
1132
+
1133
+ std::uint32_t rundot_kinetix_ext_deformable_sweep_query_stride() {
1134
+ return kDeformableSweepQueryStride;
1135
+ }
1136
+
1137
+ std::uint32_t rundot_kinetix_ext_deformable_sweep_hit_stride() {
1138
+ return kDeformableSweepHitStride;
1139
+ }
1140
+
1141
+ std::uint32_t rundot_kinetix_ext_deformable_snapshot_header_size() {
1142
+ return static_cast<std::uint32_t>(sizeof(DeformableSnapshotHeader));
1143
+ }
1144
+
1145
+ std::int32_t rundot_kinetix_ext_deformable_raycast(
1146
+ const double* nodeData,
1147
+ std::uint32_t nodeCount,
1148
+ const double* triangleData,
1149
+ std::uint32_t triangleCount,
1150
+ const double* rayQueryData,
1151
+ double* hitData) {
1152
+ if (!nodeData || !rayQueryData || !hitData) return -1;
1153
+
1154
+ // Build temporary typed views from contiguous SoA buffers
1155
+ std::array<DeformableNode, kDeformableMaxNodes> nodes{};
1156
+ const std::uint32_t actualNodes = std::min(nodeCount, kDeformableMaxNodes);
1157
+ for (std::uint32_t i = 0; i < actualNodes; ++i) {
1158
+ const double* ptr = nodeData + i * kDeformableNodeStride;
1159
+ nodes[i].position = {ptr[0], ptr[1], ptr[2]};
1160
+ nodes[i].previousPosition = {ptr[3], ptr[4], ptr[5]};
1161
+ nodes[i].velocity = {ptr[6], ptr[7], ptr[8]};
1162
+ nodes[i].invMass = ptr[9];
1163
+ nodes[i].normal = {ptr[10], ptr[11], ptr[12]};
1164
+ nodes[i].radius = ptr[13];
1165
+ }
1166
+
1167
+ std::array<DeformableTriangle, kDeformableMaxTriangles> triangles{};
1168
+ const std::uint32_t actualTriangles = std::min(triangleCount, kDeformableMaxTriangles);
1169
+ for (std::uint32_t i = 0; i < actualTriangles; ++i) {
1170
+ const double* ptr = triangleData + i * kDeformableTriangleStride;
1171
+ triangles[i].nodeIndices[0] = static_cast<std::uint32_t>(ptr[0]);
1172
+ triangles[i].nodeIndices[1] = static_cast<std::uint32_t>(ptr[1]);
1173
+ triangles[i].nodeIndices[2] = static_cast<std::uint32_t>(ptr[2]);
1174
+ triangles[i].restArea = ptr[3];
1175
+ triangles[i].restNormal = {ptr[4], ptr[5], ptr[6]};
1176
+ triangles[i].materialTag = static_cast<std::uint32_t>(ptr[7]);
1177
+ }
1178
+
1179
+ DeformableRayQuery ray{};
1180
+ ray.origin = {rayQueryData[0], rayQueryData[1], rayQueryData[2]};
1181
+ ray.direction = {rayQueryData[3], rayQueryData[4], rayQueryData[5]};
1182
+ ray.maxDistance = rayQueryData[6];
1183
+ ray.flags = static_cast<std::uint32_t>(rayQueryData[7]);
1184
+
1185
+ DeformableRayHit hit{};
1186
+ const DeformableStatus status = raycastDeformableMesh(
1187
+ std::span<const DeformableNode>(nodes.data(), actualNodes),
1188
+ std::span<const DeformableTriangle>(triangles.data(), actualTriangles),
1189
+ ray,
1190
+ hit);
1191
+
1192
+ hitData[0] = hit.hit ? 1.0 : 0.0;
1193
+ hitData[1] = hit.distance;
1194
+ hitData[2] = hit.point.x;
1195
+ hitData[3] = hit.point.y;
1196
+ hitData[4] = hit.point.z;
1197
+ hitData[5] = hit.normal.x;
1198
+ hitData[6] = hit.normal.y;
1199
+ hitData[7] = hit.normal.z;
1200
+ hitData[8] = static_cast<double>(hit.elementIndex);
1201
+ hitData[9] = static_cast<double>(hit.nodeIndex);
1202
+ hitData[10] = hit.barycentricU;
1203
+ hitData[11] = hit.barycentricV;
1204
+ hitData[12] = hit.barycentricW;
1205
+ hitData[13] = hit.velocity.x;
1206
+ hitData[14] = hit.velocity.y;
1207
+ hitData[15] = hit.velocity.z;
1208
+
1209
+ return static_cast<std::int32_t>(status);
1210
+ }
1211
+
1212
+ std::int32_t rundot_kinetix_ext_deformable_overlap(
1213
+ const double* nodeData,
1214
+ std::uint32_t nodeCount,
1215
+ const double* triangleData,
1216
+ std::uint32_t triangleCount,
1217
+ const double* overlapQueryData,
1218
+ double* resultData) {
1219
+ if (!nodeData || !overlapQueryData || !resultData) return -1;
1220
+
1221
+ std::array<DeformableNode, kDeformableMaxNodes> nodes{};
1222
+ const std::uint32_t actualNodes = std::min(nodeCount, kDeformableMaxNodes);
1223
+ for (std::uint32_t i = 0; i < actualNodes; ++i) {
1224
+ const double* ptr = nodeData + i * kDeformableNodeStride;
1225
+ nodes[i].position = {ptr[0], ptr[1], ptr[2]};
1226
+ nodes[i].radius = ptr[13];
1227
+ }
1228
+
1229
+ std::array<DeformableTriangle, kDeformableMaxTriangles> triangles{};
1230
+ const std::uint32_t actualTriangles = std::min(triangleCount, kDeformableMaxTriangles);
1231
+ for (std::uint32_t i = 0; i < actualTriangles; ++i) {
1232
+ const double* ptr = triangleData + i * kDeformableTriangleStride;
1233
+ triangles[i].nodeIndices[0] = static_cast<std::uint32_t>(ptr[0]);
1234
+ triangles[i].nodeIndices[1] = static_cast<std::uint32_t>(ptr[1]);
1235
+ triangles[i].nodeIndices[2] = static_cast<std::uint32_t>(ptr[2]);
1236
+ }
1237
+
1238
+ DeformableOverlapQuery query{};
1239
+ query.shapeType = static_cast<DeformableOverlapShapeType>(static_cast<std::uint32_t>(overlapQueryData[0]));
1240
+ query.center = {overlapQueryData[1], overlapQueryData[2], overlapQueryData[3]};
1241
+ query.halfExtents = {overlapQueryData[4], overlapQueryData[5], overlapQueryData[6]};
1242
+ query.orientation = {overlapQueryData[7], overlapQueryData[8], overlapQueryData[9], overlapQueryData[10]};
1243
+ query.secondaryPoint = {overlapQueryData[11], overlapQueryData[12], overlapQueryData[13]};
1244
+ query.margin = overlapQueryData[14];
1245
+
1246
+ DeformableOverlapResult result{};
1247
+ const DeformableStatus status = overlapDeformableMesh(
1248
+ std::span<const DeformableNode>(nodes.data(), actualNodes),
1249
+ std::span<const DeformableTriangle>(triangles.data(), actualTriangles),
1250
+ query,
1251
+ result);
1252
+
1253
+ resultData[0] = result.hasOverlap ? 1.0 : 0.0;
1254
+ resultData[1] = static_cast<double>(result.overlapCount);
1255
+ resultData[2] = result.maxPenetration;
1256
+ resultData[3] = result.contactNormal.x;
1257
+ resultData[4] = result.contactNormal.y;
1258
+ resultData[5] = result.contactNormal.z;
1259
+ resultData[6] = result.contactPoint.x;
1260
+ resultData[7] = result.contactPoint.y;
1261
+ resultData[8] = result.contactPoint.z;
1262
+ resultData[9] = static_cast<double>(result.elementIndex);
1263
+ resultData[10] = static_cast<double>(result.nodeIndex);
1264
+ resultData[11] = 0.0;
1265
+
1266
+ return static_cast<std::int32_t>(status);
1267
+ }
1268
+
1269
+ std::int32_t rundot_kinetix_ext_deformable_sweep(
1270
+ const double* nodeData,
1271
+ std::uint32_t nodeCount,
1272
+ const double* triangleData,
1273
+ std::uint32_t triangleCount,
1274
+ const double* sweepQueryData,
1275
+ double* hitData) {
1276
+ if (!nodeData || !sweepQueryData || !hitData) return -1;
1277
+
1278
+ std::array<DeformableNode, kDeformableMaxNodes> nodes{};
1279
+ const std::uint32_t actualNodes = std::min(nodeCount, kDeformableMaxNodes);
1280
+ for (std::uint32_t i = 0; i < actualNodes; ++i) {
1281
+ const double* ptr = nodeData + i * kDeformableNodeStride;
1282
+ nodes[i].position = {ptr[0], ptr[1], ptr[2]};
1283
+ nodes[i].radius = ptr[13];
1284
+ }
1285
+
1286
+ std::array<DeformableTriangle, kDeformableMaxTriangles> triangles{};
1287
+ const std::uint32_t actualTriangles = std::min(triangleCount, kDeformableMaxTriangles);
1288
+ for (std::uint32_t i = 0; i < actualTriangles; ++i) {
1289
+ const double* ptr = triangleData + i * kDeformableTriangleStride;
1290
+ triangles[i].nodeIndices[0] = static_cast<std::uint32_t>(ptr[0]);
1291
+ triangles[i].nodeIndices[1] = static_cast<std::uint32_t>(ptr[1]);
1292
+ triangles[i].nodeIndices[2] = static_cast<std::uint32_t>(ptr[2]);
1293
+ }
1294
+
1295
+ DeformableSweepQuery query{};
1296
+ query.shapeType = static_cast<DeformableSweepShapeType>(static_cast<std::uint32_t>(sweepQueryData[0]));
1297
+ query.startCenter = {sweepQueryData[1], sweepQueryData[2], sweepQueryData[3]};
1298
+ query.direction = {sweepQueryData[4], sweepQueryData[5], sweepQueryData[6]};
1299
+ query.radius = sweepQueryData[7];
1300
+ query.halfExtents = {sweepQueryData[8], sweepQueryData[9], sweepQueryData[10]};
1301
+ query.maxDistance = sweepQueryData[13];
1302
+
1303
+ DeformableSweepHit hit{};
1304
+ const DeformableStatus status = sweepDeformableMesh(
1305
+ std::span<const DeformableNode>(nodes.data(), actualNodes),
1306
+ std::span<const DeformableTriangle>(triangles.data(), actualTriangles),
1307
+ query,
1308
+ hit);
1309
+
1310
+ hitData[0] = hit.hit ? 1.0 : 0.0;
1311
+ hitData[1] = hit.distance;
1312
+ hitData[2] = hit.point.x;
1313
+ hitData[3] = hit.point.y;
1314
+ hitData[4] = hit.point.z;
1315
+ hitData[5] = hit.normal.x;
1316
+ hitData[6] = hit.normal.y;
1317
+ hitData[7] = hit.normal.z;
1318
+ hitData[8] = static_cast<double>(hit.elementIndex);
1319
+ hitData[9] = static_cast<double>(hit.nodeIndex);
1320
+ hitData[10] = hit.barycentricU;
1321
+ hitData[11] = hit.barycentricV;
1322
+ hitData[12] = hit.barycentricW;
1323
+ hitData[13] = hit.timeOfImpact;
1324
+ hitData[14] = 0.0;
1325
+ hitData[15] = 0.0;
1326
+
1327
+ return static_cast<std::int32_t>(status);
1328
+ }
1329
+
1330
+ std::int32_t rundot_kinetix_ext_deformable_closest_point(
1331
+ const double* nodeData,
1332
+ std::uint32_t nodeCount,
1333
+ const double* triangleData,
1334
+ std::uint32_t triangleCount,
1335
+ const double* pointData,
1336
+ double* outClosestPoint,
1337
+ double* outBarycentrics,
1338
+ std::uint32_t* outTriangleIndex) {
1339
+ if (!nodeData || !triangleData || !pointData || !outClosestPoint) return -1;
1340
+
1341
+ const DeformableVec3 qp{pointData[0], pointData[1], pointData[2]};
1342
+ double bestDsq = std::numeric_limits<double>::infinity();
1343
+ DeformableVec3 bestCp{0.0, 0.0, 0.0};
1344
+ double bestU = 0.0, bestV = 0.0, bestW = 1.0;
1345
+ std::uint32_t bestIdx = 0;
1346
+
1347
+ for (std::uint32_t i = 0; i < triangleCount; ++i) {
1348
+ const double* triPtr = triangleData + i * kDeformableTriangleStride;
1349
+ const auto n0 = static_cast<std::uint32_t>(triPtr[0]);
1350
+ const auto n1 = static_cast<std::uint32_t>(triPtr[1]);
1351
+ const auto n2 = static_cast<std::uint32_t>(triPtr[2]);
1352
+ if (n0 >= nodeCount || n1 >= nodeCount || n2 >= nodeCount) continue;
1353
+
1354
+ const double* p0 = nodeData + n0 * kDeformableNodeStride;
1355
+ const double* p1 = nodeData + n1 * kDeformableNodeStride;
1356
+ const double* p2 = nodeData + n2 * kDeformableNodeStride;
1357
+
1358
+ const DeformableVec3 v0{p0[0], p0[1], p0[2]};
1359
+ const DeformableVec3 v1{p1[0], p1[1], p1[2]};
1360
+ const DeformableVec3 v2{p2[0], p2[1], p2[2]};
1361
+
1362
+ double u = 0.0, v = 0.0, w = 0.0;
1363
+ const DeformableVec3 cp = closestPointOnTriangle(qp, v0, v1, v2, u, v, w);
1364
+ const double dsq = lengthSquaredVec3(subVec3(qp, cp));
1365
+ if (dsq < bestDsq) {
1366
+ bestDsq = dsq;
1367
+ bestCp = cp;
1368
+ bestU = u; bestV = v; bestW = w;
1369
+ bestIdx = i;
1370
+ }
1371
+ }
1372
+
1373
+ outClosestPoint[0] = bestCp.x;
1374
+ outClosestPoint[1] = bestCp.y;
1375
+ outClosestPoint[2] = bestCp.z;
1376
+ if (outBarycentrics) {
1377
+ outBarycentrics[0] = bestU;
1378
+ outBarycentrics[1] = bestV;
1379
+ outBarycentrics[2] = bestW;
1380
+ }
1381
+ if (outTriangleIndex) {
1382
+ *outTriangleIndex = bestIdx;
1383
+ }
1384
+ return 0;
1385
+ }
1386
+
1387
+ std::uint32_t rundot_kinetix_ext_deformable_snapshot_size(
1388
+ std::uint32_t nodeCount,
1389
+ std::uint32_t triangleCount,
1390
+ std::uint32_t tetraCount,
1391
+ std::uint32_t constraintCount,
1392
+ std::uint32_t contactCount,
1393
+ std::uint32_t hasPressure) {
1394
+ return static_cast<std::uint32_t>(calculateSnapshotSize(
1395
+ nodeCount, triangleCount, tetraCount, constraintCount, contactCount, hasPressure != 0));
1396
+ }
1397
+
1398
+ std::int32_t rundot_kinetix_ext_deformable_capture_snapshot(
1399
+ std::uint64_t frame,
1400
+ const double* nodeData,
1401
+ std::uint32_t nodeCount,
1402
+ const double* triangleData,
1403
+ std::uint32_t triangleCount,
1404
+ const double* tetraData,
1405
+ std::uint32_t tetraCount,
1406
+ const double* constraintData,
1407
+ std::uint32_t constraintCount,
1408
+ const double* contactData,
1409
+ std::uint32_t contactCount,
1410
+ const double* pressureData,
1411
+ std::uint32_t hasPressure,
1412
+ std::uint8_t* outBuffer,
1413
+ std::uint32_t bufferCapacity,
1414
+ std::uint32_t* outWrittenBytes) {
1415
+ if (!outBuffer || !outWrittenBytes) return -1;
1416
+
1417
+ const std::size_t reqSize = calculateSnapshotSize(
1418
+ nodeCount, triangleCount, tetraCount, constraintCount, contactCount, hasPressure != 0);
1419
+ if (bufferCapacity < reqSize) return -2;
1420
+
1421
+ DeformableSnapshotHeader header{};
1422
+ std::memcpy(header.magic, kDeformableSnapshotMagic.data(), 4);
1423
+ header.version = kDeformableSnapshotVersion;
1424
+ header.frame = frame;
1425
+ header.nodeCount = nodeCount;
1426
+ header.triangleCount = triangleCount;
1427
+ header.tetraCount = tetraCount;
1428
+ header.constraintCount = constraintCount;
1429
+ header.contactCount = contactCount;
1430
+ header.hasPressure = hasPressure ? 1 : 0;
1431
+ header.payloadBytes = reqSize - sizeof(DeformableSnapshotHeader);
1432
+
1433
+ std::uint8_t* writePtr = outBuffer + sizeof(DeformableSnapshotHeader);
1434
+
1435
+ if (nodeCount > 0 && nodeData) {
1436
+ const std::size_t nodeBytes = nodeCount * kDeformableNodeStride * sizeof(double);
1437
+ std::memcpy(writePtr, nodeData, nodeBytes);
1438
+ writePtr += nodeBytes;
1439
+ }
1440
+ if (triangleCount > 0 && triangleData) {
1441
+ const std::size_t triBytes = triangleCount * kDeformableTriangleStride * sizeof(double);
1442
+ std::memcpy(writePtr, triangleData, triBytes);
1443
+ writePtr += triBytes;
1444
+ }
1445
+ if (tetraCount > 0 && tetraData) {
1446
+ const std::size_t tetBytes = tetraCount * kDeformableTetraStride * sizeof(double);
1447
+ std::memcpy(writePtr, tetraData, tetBytes);
1448
+ writePtr += tetBytes;
1449
+ }
1450
+ if (constraintCount > 0 && constraintData) {
1451
+ const std::size_t conBytes = constraintCount * kDeformableConstraintStride * sizeof(double);
1452
+ std::memcpy(writePtr, constraintData, conBytes);
1453
+ writePtr += conBytes;
1454
+ }
1455
+ if (contactCount > 0 && contactData) {
1456
+ const std::size_t ctcBytes = contactCount * kDeformableContactStride * sizeof(double);
1457
+ std::memcpy(writePtr, contactData, ctcBytes);
1458
+ writePtr += ctcBytes;
1459
+ }
1460
+ if (hasPressure != 0 && pressureData) {
1461
+ const std::size_t presBytes = kDeformablePressureStride * sizeof(double);
1462
+ std::memcpy(writePtr, pressureData, presBytes);
1463
+ writePtr += presBytes;
1464
+ }
1465
+
1466
+ const std::uint8_t* payloadStart = outBuffer + sizeof(DeformableSnapshotHeader);
1467
+ header.checksum = computeFnv1a32(payloadStart, header.payloadBytes);
1468
+ std::memcpy(outBuffer, &header, sizeof(header));
1469
+
1470
+ *outWrittenBytes = static_cast<std::uint32_t>(reqSize);
1471
+ return 0;
1472
+ }
1473
+
1474
+ std::int32_t rundot_kinetix_ext_deformable_restore_snapshot(
1475
+ const std::uint8_t* inBuffer,
1476
+ std::uint32_t inBufferSize,
1477
+ std::uint64_t* outFrame,
1478
+ double* outNodeData,
1479
+ std::uint32_t maxNodes,
1480
+ std::uint32_t* outNodeCount,
1481
+ double* outTriangleData,
1482
+ std::uint32_t maxTriangles,
1483
+ std::uint32_t* outTriangleCount,
1484
+ double* outTetraData,
1485
+ std::uint32_t maxTetras,
1486
+ std::uint32_t* outTetraCount,
1487
+ double* outConstraintData,
1488
+ std::uint32_t maxConstraints,
1489
+ std::uint32_t* outConstraintCount,
1490
+ double* outContactData,
1491
+ std::uint32_t maxContacts,
1492
+ std::uint32_t* outContactCount,
1493
+ double* outPressureData,
1494
+ std::uint32_t* outHasPressure) {
1495
+ if (!inBuffer) return -1;
1496
+ if (inBufferSize < sizeof(DeformableSnapshotHeader)) return -4;
1497
+
1498
+ DeformableSnapshotHeader header{};
1499
+ std::memcpy(&header, inBuffer, sizeof(header));
1500
+
1501
+ if (std::memcmp(header.magic, kDeformableSnapshotMagic.data(), 4) != 0) return -5;
1502
+ if (header.version != kDeformableSnapshotVersion) return -6;
1503
+ if (inBufferSize < sizeof(header) + header.payloadBytes) return -4;
1504
+
1505
+ const std::uint8_t* payloadStart = inBuffer + sizeof(header);
1506
+ const std::uint32_t calcSum = computeFnv1a32(payloadStart, header.payloadBytes);
1507
+ if (calcSum != header.checksum) return -7;
1508
+
1509
+ if (outFrame) *outFrame = header.frame;
1510
+ if (outNodeCount) *outNodeCount = header.nodeCount;
1511
+ if (outTriangleCount) *outTriangleCount = header.triangleCount;
1512
+ if (outTetraCount) *outTetraCount = header.tetraCount;
1513
+ if (outConstraintCount) *outConstraintCount = header.constraintCount;
1514
+ if (outContactCount) *outContactCount = header.contactCount;
1515
+ if (outHasPressure) *outHasPressure = header.hasPressure;
1516
+
1517
+ const std::uint8_t* readPtr = payloadStart;
1518
+
1519
+ // Nodes
1520
+ const std::uint32_t copyNodes = std::min(header.nodeCount, maxNodes);
1521
+ if (outNodeData && copyNodes > 0) {
1522
+ std::memcpy(outNodeData, readPtr, copyNodes * kDeformableNodeStride * sizeof(double));
1523
+ }
1524
+ readPtr += header.nodeCount * kDeformableNodeStride * sizeof(double);
1525
+
1526
+ // Triangles
1527
+ const std::uint32_t copyTri = std::min(header.triangleCount, maxTriangles);
1528
+ if (outTriangleData && copyTri > 0) {
1529
+ std::memcpy(outTriangleData, readPtr, copyTri * kDeformableTriangleStride * sizeof(double));
1530
+ }
1531
+ readPtr += header.triangleCount * kDeformableTriangleStride * sizeof(double);
1532
+
1533
+ // Tetrahedra
1534
+ const std::uint32_t copyTet = std::min(header.tetraCount, maxTetras);
1535
+ if (outTetraData && copyTet > 0) {
1536
+ std::memcpy(outTetraData, readPtr, copyTet * kDeformableTetraStride * sizeof(double));
1537
+ }
1538
+ readPtr += header.tetraCount * kDeformableTetraStride * sizeof(double);
1539
+
1540
+ // Constraints
1541
+ const std::uint32_t copyCon = std::min(header.constraintCount, maxConstraints);
1542
+ if (outConstraintData && copyCon > 0) {
1543
+ std::memcpy(outConstraintData, readPtr, copyCon * kDeformableConstraintStride * sizeof(double));
1544
+ }
1545
+ readPtr += header.constraintCount * kDeformableConstraintStride * sizeof(double);
1546
+
1547
+ // Contacts
1548
+ const std::uint32_t copyCtc = std::min(header.contactCount, maxContacts);
1549
+ if (outContactData && copyCtc > 0) {
1550
+ std::memcpy(outContactData, readPtr, copyCtc * kDeformableContactStride * sizeof(double));
1551
+ }
1552
+ readPtr += header.contactCount * kDeformableContactStride * sizeof(double);
1553
+
1554
+ // Pressure
1555
+ if (header.hasPressure != 0) {
1556
+ if (outPressureData) {
1557
+ std::memcpy(outPressureData, readPtr, kDeformablePressureStride * sizeof(double));
1558
+ }
1559
+ }
1560
+
1561
+ return 0;
1562
+ }
1563
+
1564
+ std::int32_t rundot_kinetix_ext_deformable_verify_snapshot(
1565
+ const std::uint8_t* inBuffer,
1566
+ std::uint32_t inBufferSize) {
1567
+ if (!inBuffer) return -1;
1568
+ const DeformableStatus status = verifyDeformableSnapshot(
1569
+ std::span<const std::uint8_t>(inBuffer, inBufferSize));
1570
+ return static_cast<std::int32_t>(status);
1571
+ }
1572
+
1573
+ std::int32_t rundot_kinetix_ext_deformable_compute_bounds(
1574
+ const double* nodeData,
1575
+ std::uint32_t nodeCount,
1576
+ double* outMinMax) {
1577
+ if (!nodeData || !outMinMax || nodeCount == 0) return -1;
1578
+
1579
+ double minX = nodeData[0], maxX = nodeData[0];
1580
+ double minY = nodeData[1], maxY = nodeData[1];
1581
+ double minZ = nodeData[2], maxZ = nodeData[2];
1582
+
1583
+ for (std::uint32_t i = 0; i < nodeCount; ++i) {
1584
+ const double* p = nodeData + i * kDeformableNodeStride;
1585
+ const double r = p[13];
1586
+ minX = std::min(minX, p[0] - r);
1587
+ maxX = std::max(maxX, p[0] + r);
1588
+ minY = std::min(minY, p[1] - r);
1589
+ maxY = std::max(maxY, p[1] + r);
1590
+ minZ = std::min(minZ, p[2] - r);
1591
+ maxZ = std::max(maxZ, p[2] + r);
1592
+ }
1593
+
1594
+ outMinMax[0] = minX; outMinMax[1] = minY; outMinMax[2] = minZ;
1595
+ outMinMax[3] = maxX; outMinMax[4] = maxY; outMinMax[5] = maxZ;
1596
+ return 0;
1597
+ }
1598
+
1599
+ std::int32_t rundot_kinetix_ext_deformable_compute_volume(
1600
+ const double* nodeData,
1601
+ std::uint32_t nodeCount,
1602
+ const double* triangleData,
1603
+ std::uint32_t triangleCount,
1604
+ double* outVolume) {
1605
+ if (!nodeData || !triangleData || !outVolume || nodeCount == 0 || triangleCount == 0) return -1;
1606
+
1607
+ double vol6 = 0.0;
1608
+ for (std::uint32_t i = 0; i < triangleCount; ++i) {
1609
+ const double* tri = triangleData + i * kDeformableTriangleStride;
1610
+ const auto n0 = static_cast<std::uint32_t>(tri[0]);
1611
+ const auto n1 = static_cast<std::uint32_t>(tri[1]);
1612
+ const auto n2 = static_cast<std::uint32_t>(tri[2]);
1613
+ if (n0 >= nodeCount || n1 >= nodeCount || n2 >= nodeCount) continue;
1614
+
1615
+ const double* p0 = nodeData + n0 * kDeformableNodeStride;
1616
+ const double* p1 = nodeData + n1 * kDeformableNodeStride;
1617
+ const double* p2 = nodeData + n2 * kDeformableNodeStride;
1618
+
1619
+ const double crossX = p1[1] * p2[2] - p1[2] * p2[1];
1620
+ const double crossY = p1[2] * p2[0] - p1[0] * p2[2];
1621
+ const double crossZ = p1[0] * p2[1] - p1[1] * p2[0];
1622
+
1623
+ vol6 += p0[0] * crossX + p0[1] * crossY + p0[2] * crossZ;
1624
+ }
1625
+
1626
+ *outVolume = std::abs(vol6) * (1.0 / 6.0);
1627
+ return 0;
1628
+ }
1629
+
1630
+ }