@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,1929 @@
1
+ #include "voxel_cooking.hpp"
2
+
3
+ #include <algorithm>
4
+ #include <array>
5
+ #include <cmath>
6
+ #include <cstdint>
7
+ #include <cstring>
8
+ #include <vector>
9
+
10
+ namespace rundot::kinetix::physics::voxel {
11
+
12
+ namespace {
13
+
14
+ inline double voxelClamp(double val, double minVal, double maxVal) {
15
+ if (val < minVal) return minVal;
16
+ if (val > maxVal) return maxVal;
17
+ return val;
18
+ }
19
+
20
+ inline std::int32_t voxelClampInt(std::int32_t val, std::int32_t minVal, std::int32_t maxVal) {
21
+ if (val < minVal) return minVal;
22
+ if (val > maxVal) return maxVal;
23
+ return val;
24
+ }
25
+
26
+ inline VoxelCellAABB deduceCellBounds(const VoxelBoxChild& child, double cellSize) {
27
+ if (child.cellBounds.maxX >= child.cellBounds.minX &&
28
+ child.cellBounds.maxY >= child.cellBounds.minY &&
29
+ child.cellBounds.maxZ >= child.cellBounds.minZ) {
30
+ return child.cellBounds;
31
+ }
32
+ VoxelCellAABB b;
33
+ b.minX = static_cast<std::int32_t>(std::round((child.offset.x - child.halfExtents.x) / cellSize + 0.5));
34
+ b.maxX = static_cast<std::int32_t>(std::round((child.offset.x + child.halfExtents.x) / cellSize - 0.5));
35
+ b.minY = static_cast<std::int32_t>(std::round((child.offset.y - child.halfExtents.y) / cellSize + 0.5));
36
+ b.maxY = static_cast<std::int32_t>(std::round((child.offset.y + child.halfExtents.y) / cellSize - 0.5));
37
+ b.minZ = static_cast<std::int32_t>(std::round((child.offset.z - child.halfExtents.z) / cellSize + 0.5));
38
+ b.maxZ = static_cast<std::int32_t>(std::round((child.offset.z + child.halfExtents.z) / cellSize - 0.5));
39
+ return b;
40
+ }
41
+
42
+ struct SubChunkSortOrder {
43
+ bool operator()(const VoxelSubChunk& a, const VoxelSubChunk& b) const {
44
+ if (a.anchored != b.anchored) {
45
+ return a.anchored > b.anchored; // anchored first
46
+ }
47
+ if (a.offsetCells.z != b.offsetCells.z) return a.offsetCells.z < b.offsetCells.z;
48
+ if (a.offsetCells.y != b.offsetCells.y) return a.offsetCells.y < b.offsetCells.y;
49
+ if (a.offsetCells.x != b.offsetCells.x) return a.offsetCells.x < b.offsetCells.x;
50
+ if (a.dimZ != b.dimZ) return a.dimZ < b.dimZ;
51
+ if (a.dimY != b.dimY) return a.dimY < b.dimY;
52
+ if (a.dimX != b.dimX) return a.dimX < b.dimX;
53
+ return a.occupiedCount < b.occupiedCount;
54
+ }
55
+ };
56
+
57
+ } // namespace
58
+
59
+ void packBoxChild(const VoxelBoxChild& child, double* outData) {
60
+ if (!outData) return;
61
+ outData[0] = static_cast<double>(child.childId);
62
+ outData[1] = child.halfExtents.x;
63
+ outData[2] = child.halfExtents.y;
64
+ outData[3] = child.halfExtents.z;
65
+ outData[4] = child.offset.x;
66
+ outData[5] = child.offset.y;
67
+ outData[6] = child.offset.z;
68
+ outData[7] = static_cast<double>(child.cellBounds.minX);
69
+ outData[8] = static_cast<double>(child.cellBounds.minY);
70
+ outData[9] = static_cast<double>(child.cellBounds.minZ);
71
+ outData[10] = static_cast<double>(child.cellBounds.maxX);
72
+ outData[11] = static_cast<double>(child.cellBounds.maxY);
73
+ outData[12] = static_cast<double>(child.cellBounds.maxZ);
74
+ outData[13] = static_cast<double>(child.flags);
75
+ }
76
+
77
+ VoxelBoxChild unpackBoxChild(const double* inData) {
78
+ VoxelBoxChild child;
79
+ if (!inData) return child;
80
+ child.childId = static_cast<std::uint32_t>(inData[0]);
81
+ child.halfExtents.x = inData[1];
82
+ child.halfExtents.y = inData[2];
83
+ child.halfExtents.z = inData[3];
84
+ child.offset.x = inData[4];
85
+ child.offset.y = inData[5];
86
+ child.offset.z = inData[6];
87
+ child.cellBounds.minX = static_cast<std::int32_t>(inData[7]);
88
+ child.cellBounds.minY = static_cast<std::int32_t>(inData[8]);
89
+ child.cellBounds.minZ = static_cast<std::int32_t>(inData[9]);
90
+ child.cellBounds.maxX = static_cast<std::int32_t>(inData[10]);
91
+ child.cellBounds.maxY = static_cast<std::int32_t>(inData[11]);
92
+ child.cellBounds.maxZ = static_cast<std::int32_t>(inData[12]);
93
+ child.flags = static_cast<std::uint32_t>(inData[13]);
94
+ return child;
95
+ }
96
+
97
+ void packCookStats(const VoxelCookStats& stats, double* outData) {
98
+ if (!outData) return;
99
+ outData[0] = static_cast<double>(stats.totalBoxes);
100
+ outData[1] = static_cast<double>(stats.preservedBoxes);
101
+ outData[2] = static_cast<double>(stats.recookedBoxes);
102
+ outData[3] = static_cast<double>(stats.retiredBoxes);
103
+ outData[4] = static_cast<double>(stats.cellsEvaluated);
104
+ outData[5] = static_cast<double>(stats.dirtyAABB.minX);
105
+ outData[6] = static_cast<double>(stats.dirtyAABB.minY);
106
+ outData[7] = static_cast<double>(stats.dirtyAABB.minZ);
107
+ outData[8] = static_cast<double>(stats.dirtyAABB.maxX);
108
+ outData[9] = static_cast<double>(stats.dirtyAABB.maxY);
109
+ outData[10] = static_cast<double>(stats.dirtyAABB.maxZ);
110
+ outData[11] = static_cast<double>(stats.expandedAABB.minX);
111
+ outData[12] = static_cast<double>(stats.expandedAABB.minY);
112
+ outData[13] = static_cast<double>(stats.expandedAABB.minZ);
113
+ outData[14] = static_cast<double>(stats.expandedAABB.maxX);
114
+ outData[15] = static_cast<double>(stats.expandedAABB.maxY);
115
+ outData[16] = static_cast<double>(stats.expandedAABB.maxZ);
116
+ }
117
+
118
+ VoxelCookStats unpackCookStats(const double* inData) {
119
+ VoxelCookStats stats;
120
+ if (!inData) return stats;
121
+ stats.totalBoxes = static_cast<std::uint32_t>(inData[0]);
122
+ stats.preservedBoxes = static_cast<std::uint32_t>(inData[1]);
123
+ stats.recookedBoxes = static_cast<std::uint32_t>(inData[2]);
124
+ stats.retiredBoxes = static_cast<std::uint32_t>(inData[3]);
125
+ stats.cellsEvaluated = static_cast<std::uint32_t>(inData[4]);
126
+ stats.dirtyAABB.minX = static_cast<std::int32_t>(inData[5]);
127
+ stats.dirtyAABB.minY = static_cast<std::int32_t>(inData[6]);
128
+ stats.dirtyAABB.minZ = static_cast<std::int32_t>(inData[7]);
129
+ stats.dirtyAABB.maxX = static_cast<std::int32_t>(inData[8]);
130
+ stats.dirtyAABB.maxY = static_cast<std::int32_t>(inData[9]);
131
+ stats.dirtyAABB.maxZ = static_cast<std::int32_t>(inData[10]);
132
+ stats.expandedAABB.minX = static_cast<std::int32_t>(inData[11]);
133
+ stats.expandedAABB.minY = static_cast<std::int32_t>(inData[12]);
134
+ stats.expandedAABB.minZ = static_cast<std::int32_t>(inData[13]);
135
+ stats.expandedAABB.maxX = static_cast<std::int32_t>(inData[14]);
136
+ stats.expandedAABB.maxY = static_cast<std::int32_t>(inData[15]);
137
+ stats.expandedAABB.maxZ = static_cast<std::int32_t>(inData[16]);
138
+ return stats;
139
+ }
140
+
141
+ void packSubChunk(const VoxelSubChunk& subChunk, double* outData) {
142
+ if (!outData) return;
143
+ outData[0] = static_cast<double>(subChunk.subChunkId);
144
+ outData[1] = static_cast<double>(subChunk.offsetCells.x);
145
+ outData[2] = static_cast<double>(subChunk.offsetCells.y);
146
+ outData[3] = static_cast<double>(subChunk.offsetCells.z);
147
+ outData[4] = static_cast<double>(subChunk.dimX);
148
+ outData[5] = static_cast<double>(subChunk.dimY);
149
+ outData[6] = static_cast<double>(subChunk.dimZ);
150
+ outData[7] = subChunk.cellSize;
151
+ outData[8] = static_cast<double>(subChunk.occupiedCount);
152
+ outData[9] = subChunk.anchored ? 1.0 : 0.0;
153
+ }
154
+
155
+ VoxelSubChunk unpackSubChunk(const double* inData) {
156
+ VoxelSubChunk subChunk;
157
+ if (!inData) return subChunk;
158
+ subChunk.subChunkId = static_cast<std::uint32_t>(inData[0]);
159
+ subChunk.offsetCells.x = static_cast<std::int32_t>(inData[1]);
160
+ subChunk.offsetCells.y = static_cast<std::int32_t>(inData[2]);
161
+ subChunk.offsetCells.z = static_cast<std::int32_t>(inData[3]);
162
+ subChunk.dimX = static_cast<std::int32_t>(inData[4]);
163
+ subChunk.dimY = static_cast<std::int32_t>(inData[5]);
164
+ subChunk.dimZ = static_cast<std::int32_t>(inData[6]);
165
+ subChunk.cellSize = inData[7];
166
+ subChunk.occupiedCount = static_cast<std::uint32_t>(inData[8]);
167
+ subChunk.anchored = (inData[9] != 0.0);
168
+ return subChunk;
169
+ }
170
+
171
+ void packRaycastHit(const VoxelRaycastHit& hit, double* outData) {
172
+ if (!outData) return;
173
+ outData[0] = hit.hit ? 1.0 : 0.0;
174
+ outData[1] = hit.distance;
175
+ outData[2] = static_cast<double>(hit.voxelCoords.x);
176
+ outData[3] = static_cast<double>(hit.voxelCoords.y);
177
+ outData[4] = static_cast<double>(hit.voxelCoords.z);
178
+ outData[5] = hit.point.x;
179
+ outData[6] = hit.point.y;
180
+ outData[7] = hit.point.z;
181
+ outData[8] = hit.normal.x;
182
+ outData[9] = hit.normal.y;
183
+ outData[10] = hit.normal.z;
184
+ outData[11] = static_cast<double>(hit.childIndex);
185
+ }
186
+
187
+ VoxelRaycastHit unpackRaycastHit(const double* inData) {
188
+ VoxelRaycastHit hit;
189
+ if (!inData) return hit;
190
+ hit.hit = (inData[0] != 0.0);
191
+ hit.distance = inData[1];
192
+ hit.voxelCoords.x = static_cast<std::int32_t>(inData[2]);
193
+ hit.voxelCoords.y = static_cast<std::int32_t>(inData[3]);
194
+ hit.voxelCoords.z = static_cast<std::int32_t>(inData[4]);
195
+ hit.point.x = inData[5];
196
+ hit.point.y = inData[6];
197
+ hit.point.z = inData[7];
198
+ hit.normal.x = inData[8];
199
+ hit.normal.y = inData[9];
200
+ hit.normal.z = inData[10];
201
+ hit.childIndex = static_cast<std::int32_t>(inData[11]);
202
+ return hit;
203
+ }
204
+
205
+ void packSeamResult(const VoxelSeamResult& result, double* outData) {
206
+ if (!outData) return;
207
+ outData[0] = result.hasSeam ? 1.0 : 0.0;
208
+ outData[1] = static_cast<double>(result.seamAxis);
209
+ outData[2] = static_cast<double>(result.seamCoord);
210
+ outData[3] = static_cast<double>(result.sharedFaceCount);
211
+ outData[4] = static_cast<double>(result.exposedFaceCountA);
212
+ outData[5] = static_cast<double>(result.exposedFaceCountB);
213
+ outData[6] = result.seamArea;
214
+ outData[7] = result.seamCentroid.x;
215
+ outData[8] = result.seamCentroid.y;
216
+ outData[9] = result.seamCentroid.z;
217
+ outData[10] = result.seamNormal.x;
218
+ outData[11] = result.seamNormal.y;
219
+ outData[12] = result.seamNormal.z;
220
+ outData[13] = result.isWatertight ? 1.0 : 0.0;
221
+ outData[14] = static_cast<double>(result.seamBounds.minX);
222
+ outData[15] = static_cast<double>(result.seamBounds.minY);
223
+ outData[16] = static_cast<double>(result.seamBounds.minZ);
224
+ outData[17] = static_cast<double>(result.seamBounds.maxX);
225
+ outData[18] = static_cast<double>(result.seamBounds.maxY);
226
+ outData[19] = static_cast<double>(result.seamBounds.maxZ);
227
+ }
228
+
229
+ VoxelSeamResult unpackSeamResult(const double* inData) {
230
+ VoxelSeamResult res;
231
+ if (!inData) return res;
232
+ res.hasSeam = (inData[0] != 0.0);
233
+ res.seamAxis = static_cast<std::int32_t>(inData[1]);
234
+ res.seamCoord = static_cast<std::int32_t>(inData[2]);
235
+ res.sharedFaceCount = static_cast<std::uint32_t>(inData[3]);
236
+ res.exposedFaceCountA = static_cast<std::uint32_t>(inData[4]);
237
+ res.exposedFaceCountB = static_cast<std::uint32_t>(inData[5]);
238
+ res.seamArea = inData[6];
239
+ res.seamCentroid.x = inData[7];
240
+ res.seamCentroid.y = inData[8];
241
+ res.seamCentroid.z = inData[9];
242
+ res.seamNormal.x = inData[10];
243
+ res.seamNormal.y = inData[11];
244
+ res.seamNormal.z = inData[12];
245
+ res.isWatertight = (inData[13] != 0.0);
246
+ res.seamBounds.minX = static_cast<std::int32_t>(inData[14]);
247
+ res.seamBounds.minY = static_cast<std::int32_t>(inData[15]);
248
+ res.seamBounds.minZ = static_cast<std::int32_t>(inData[16]);
249
+ res.seamBounds.maxX = static_cast<std::int32_t>(inData[17]);
250
+ res.seamBounds.maxY = static_cast<std::int32_t>(inData[18]);
251
+ res.seamBounds.maxZ = static_cast<std::int32_t>(inData[19]);
252
+ return res;
253
+ }
254
+
255
+ VoxelStatus cookVoxelChunk(
256
+ const VoxelChunk& chunk,
257
+ VoxelBoxChild* outChildren,
258
+ std::uint32_t maxChildren,
259
+ std::uint32_t* outChildCount,
260
+ std::uint32_t* inOutNextChildId,
261
+ std::uint8_t* consumedScratch) {
262
+ if (chunk.dimX < 1 || chunk.dimY < 1 || chunk.dimZ < 1) {
263
+ return VoxelStatus::InvalidDimensions;
264
+ }
265
+ if (!std::isfinite(chunk.cellSize) || chunk.cellSize <= 0.0) {
266
+ return VoxelStatus::InvalidCellSize;
267
+ }
268
+ if (!chunk.occupancy) {
269
+ return VoxelStatus::NullBuffer;
270
+ }
271
+ if (!outChildren || !outChildCount) {
272
+ return VoxelStatus::NullBuffer;
273
+ }
274
+
275
+ std::uint32_t nextId = inOutNextChildId ? *inOutNextChildId : 0;
276
+ const std::int32_t total = chunk.totalCells();
277
+
278
+ std::vector<std::uint8_t> localScratch;
279
+ std::uint8_t* consumed = consumedScratch;
280
+ if (!consumed) {
281
+ localScratch.assign(static_cast<std::size_t>(total), 0);
282
+ consumed = localScratch.data();
283
+ } else {
284
+ std::memset(consumed, 0, static_cast<std::size_t>(total));
285
+ }
286
+
287
+ auto solid = [&](std::int32_t x, std::int32_t y, std::int32_t z) -> bool {
288
+ return chunk.occupancy[chunk.cellIndex(x, y, z)] != 0;
289
+ };
290
+ auto freeCell = [&](std::int32_t x, std::int32_t y, std::int32_t z) -> bool {
291
+ return solid(x, y, z) && consumed[chunk.cellIndex(x, y, z)] == 0;
292
+ };
293
+
294
+ std::uint32_t childCount = 0;
295
+
296
+ for (std::int32_t z = 0; z < chunk.dimZ; ++z) {
297
+ for (std::int32_t y = 0; y < chunk.dimY; ++y) {
298
+ for (std::int32_t x = 0; x < chunk.dimX; ++x) {
299
+ if (!freeCell(x, y, z)) {
300
+ continue;
301
+ }
302
+
303
+ std::int32_t x1 = x;
304
+ while (x1 + 1 < chunk.dimX && freeCell(x1 + 1, y, z)) {
305
+ x1 += 1;
306
+ }
307
+
308
+ auto rowFree = [&](std::int32_t yy, std::int32_t zz) -> bool {
309
+ for (std::int32_t xx = x; xx <= x1; ++xx) {
310
+ if (!freeCell(xx, yy, zz)) {
311
+ return false;
312
+ }
313
+ }
314
+ return true;
315
+ };
316
+
317
+ std::int32_t y1 = y;
318
+ while (y1 + 1 < chunk.dimY && rowFree(y1 + 1, z)) {
319
+ y1 += 1;
320
+ }
321
+
322
+ auto slabFree = [&](std::int32_t zz) -> bool {
323
+ for (std::int32_t yy = y; yy <= y1; ++yy) {
324
+ if (!rowFree(yy, zz)) {
325
+ return false;
326
+ }
327
+ }
328
+ return true;
329
+ };
330
+
331
+ std::int32_t z1 = z;
332
+ while (z1 + 1 < chunk.dimZ && slabFree(z1 + 1)) {
333
+ z1 += 1;
334
+ }
335
+
336
+ for (std::int32_t zz = z; zz <= z1; ++zz) {
337
+ for (std::int32_t yy = y; yy <= y1; ++yy) {
338
+ for (std::int32_t xx = x; xx <= x1; ++xx) {
339
+ consumed[chunk.cellIndex(xx, yy, zz)] = 1;
340
+ }
341
+ }
342
+ }
343
+
344
+ if (childCount >= maxChildren) {
345
+ *outChildCount = childCount;
346
+ if (inOutNextChildId) *inOutNextChildId = nextId;
347
+ return VoxelStatus::BufferOverflow;
348
+ }
349
+
350
+ VoxelBoxChild& child = outChildren[childCount++];
351
+ child.childId = nextId++;
352
+ child.halfExtents = {
353
+ ((x1 - x + 1) * chunk.cellSize) * 0.5,
354
+ ((y1 - y + 1) * chunk.cellSize) * 0.5,
355
+ ((z1 - z + 1) * chunk.cellSize) * 0.5
356
+ };
357
+ child.offset = {
358
+ ((x + x1) * 0.5) * chunk.cellSize,
359
+ ((y + y1) * 0.5) * chunk.cellSize,
360
+ ((z + z1) * 0.5) * chunk.cellSize
361
+ };
362
+ child.cellBounds = {x, y, z, x1, y1, z1};
363
+ child.flags = ChildFlagRecooked;
364
+ }
365
+ }
366
+ }
367
+
368
+ *outChildCount = childCount;
369
+ if (inOutNextChildId) *inOutNextChildId = nextId;
370
+
371
+ if (childCount == 0) {
372
+ return VoxelStatus::NoOccupiedVoxels;
373
+ }
374
+
375
+ return VoxelStatus::Success;
376
+ }
377
+
378
+ VoxelStatus carveVoxelChunk(
379
+ std::int32_t dimX,
380
+ std::int32_t dimY,
381
+ std::int32_t dimZ,
382
+ double cellSize,
383
+ std::uint8_t* occupancy,
384
+ const VoxelCarveRegion& region,
385
+ VoxelCellAABB* outDirtyRegion,
386
+ bool* outChanged) {
387
+ if (dimX < 1 || dimY < 1 || dimZ < 1 || cellSize <= 0.0 || !occupancy) {
388
+ return VoxelStatus::InvalidDimensions;
389
+ }
390
+
391
+ std::int32_t startX = 0;
392
+ std::int32_t endX = dimX - 1;
393
+ std::int32_t startY = 0;
394
+ std::int32_t endY = dimY - 1;
395
+ std::int32_t startZ = 0;
396
+ std::int32_t endZ = dimZ - 1;
397
+
398
+ if (region.shapeType == VoxelCarveShapeType::Sphere) {
399
+ if (region.sphereRadius <= 0.0) {
400
+ if (outChanged) *outChanged = false;
401
+ return VoxelStatus::Success;
402
+ }
403
+ startX = voxelClampInt(static_cast<std::int32_t>(std::floor(region.sphereCenter.x - region.sphereRadius)), 0, dimX - 1);
404
+ endX = voxelClampInt(static_cast<std::int32_t>(std::ceil(region.sphereCenter.x + region.sphereRadius)), 0, dimX - 1);
405
+ startY = voxelClampInt(static_cast<std::int32_t>(std::floor(region.sphereCenter.y - region.sphereRadius)), 0, dimY - 1);
406
+ endY = voxelClampInt(static_cast<std::int32_t>(std::ceil(region.sphereCenter.y + region.sphereRadius)), 0, dimY - 1);
407
+ startZ = voxelClampInt(static_cast<std::int32_t>(std::floor(region.sphereCenter.z - region.sphereRadius)), 0, dimZ - 1);
408
+ endZ = voxelClampInt(static_cast<std::int32_t>(std::ceil(region.sphereCenter.z + region.sphereRadius)), 0, dimZ - 1);
409
+ } else if (region.shapeType == VoxelCarveShapeType::Capsule) {
410
+ if (region.capsuleRadius <= 0.0) {
411
+ if (outChanged) *outChanged = false;
412
+ return VoxelStatus::Success;
413
+ }
414
+ double minPx = std::min(region.capsuleP0.x, region.capsuleP1.x);
415
+ double maxPx = std::max(region.capsuleP0.x, region.capsuleP1.x);
416
+ double minPy = std::min(region.capsuleP0.y, region.capsuleP1.y);
417
+ double maxPy = std::max(region.capsuleP0.y, region.capsuleP1.y);
418
+ double minPz = std::min(region.capsuleP0.z, region.capsuleP1.z);
419
+ double maxPz = std::max(region.capsuleP0.z, region.capsuleP1.z);
420
+ startX = voxelClampInt(static_cast<std::int32_t>(std::floor(minPx - region.capsuleRadius)), 0, dimX - 1);
421
+ endX = voxelClampInt(static_cast<std::int32_t>(std::ceil(maxPx + region.capsuleRadius)), 0, dimX - 1);
422
+ startY = voxelClampInt(static_cast<std::int32_t>(std::floor(minPy - region.capsuleRadius)), 0, dimY - 1);
423
+ endY = voxelClampInt(static_cast<std::int32_t>(std::ceil(maxPy + region.capsuleRadius)), 0, dimY - 1);
424
+ startZ = voxelClampInt(static_cast<std::int32_t>(std::floor(minPz - region.capsuleRadius)), 0, dimZ - 1);
425
+ endZ = voxelClampInt(static_cast<std::int32_t>(std::ceil(maxPz + region.capsuleRadius)), 0, dimZ - 1);
426
+ } else {
427
+ startX = voxelClampInt(std::min(region.box.minX, region.box.maxX), 0, dimX - 1);
428
+ endX = voxelClampInt(std::max(region.box.minX, region.box.maxX), 0, dimX - 1);
429
+ startY = voxelClampInt(std::min(region.box.minY, region.box.maxY), 0, dimY - 1);
430
+ endY = voxelClampInt(std::max(region.box.minY, region.box.maxY), 0, dimY - 1);
431
+ startZ = voxelClampInt(std::min(region.box.minZ, region.box.maxZ), 0, dimZ - 1);
432
+ endZ = voxelClampInt(std::max(region.box.minZ, region.box.maxZ), 0, dimZ - 1);
433
+ }
434
+
435
+ if (startX > endX || startY > endY || startZ > endZ) {
436
+ if (outChanged) *outChanged = false;
437
+ return VoxelStatus::Success;
438
+ }
439
+
440
+ const std::uint8_t targetVal = (region.op == VoxelCarveOp::Carve) ? 0 : 1;
441
+ bool changed = false;
442
+
443
+ std::int32_t actualDirtyMinX = dimX;
444
+ std::int32_t actualDirtyMaxX = -1;
445
+ std::int32_t actualDirtyMinY = dimY;
446
+ std::int32_t actualDirtyMaxY = -1;
447
+ std::int32_t actualDirtyMinZ = dimZ;
448
+ std::int32_t actualDirtyMaxZ = -1;
449
+
450
+ auto isInside = [&](std::int32_t x, std::int32_t y, std::int32_t z) -> bool {
451
+ if (region.shapeType == VoxelCarveShapeType::Sphere) {
452
+ double dx = static_cast<double>(x) - region.sphereCenter.x;
453
+ double dy = static_cast<double>(y) - region.sphereCenter.y;
454
+ double dz = static_cast<double>(z) - region.sphereCenter.z;
455
+ return (dx * dx + dy * dy + dz * dz) <= (region.sphereRadius * region.sphereRadius);
456
+ }
457
+ if (region.shapeType == VoxelCarveShapeType::Capsule) {
458
+ double segX = region.capsuleP1.x - region.capsuleP0.x;
459
+ double segY = region.capsuleP1.y - region.capsuleP0.y;
460
+ double segZ = region.capsuleP1.z - region.capsuleP0.z;
461
+ double segLenSq = segX * segX + segY * segY + segZ * segZ;
462
+ double px = static_cast<double>(x) - region.capsuleP0.x;
463
+ double py = static_cast<double>(y) - region.capsuleP0.y;
464
+ double pz = static_cast<double>(z) - region.capsuleP0.z;
465
+ if (segLenSq < 1e-12) {
466
+ return (px * px + py * py + pz * pz) <= (region.capsuleRadius * region.capsuleRadius);
467
+ }
468
+ double dot = px * segX + py * segY + pz * segZ;
469
+ double t = voxelClamp(dot / segLenSq, 0.0, 1.0);
470
+ double projX = region.capsuleP0.x + t * segX;
471
+ double projY = region.capsuleP0.y + t * segY;
472
+ double projZ = region.capsuleP0.z + t * segZ;
473
+ double dx = static_cast<double>(x) - projX;
474
+ double dy = static_cast<double>(y) - projY;
475
+ double dz = static_cast<double>(z) - projZ;
476
+ return (dx * dx + dy * dy + dz * dz) <= (region.capsuleRadius * region.capsuleRadius);
477
+ }
478
+ return true; // Box is already covered by start/end ranges
479
+ };
480
+
481
+ for (std::int32_t z = startZ; z <= endZ; ++z) {
482
+ for (std::int32_t y = startY; y <= endY; ++y) {
483
+ for (std::int32_t x = startX; x <= endX; ++x) {
484
+ if (!isInside(x, y, z)) {
485
+ continue;
486
+ }
487
+ std::int32_t idx = x + dimX * (y + dimY * z);
488
+ if (occupancy[idx] != targetVal) {
489
+ occupancy[idx] = targetVal;
490
+ changed = true;
491
+ if (x < actualDirtyMinX) actualDirtyMinX = x;
492
+ if (x > actualDirtyMaxX) actualDirtyMaxX = x;
493
+ if (y < actualDirtyMinY) actualDirtyMinY = y;
494
+ if (y > actualDirtyMaxY) actualDirtyMaxY = y;
495
+ if (z < actualDirtyMinZ) actualDirtyMinZ = z;
496
+ if (z > actualDirtyMaxZ) actualDirtyMaxZ = z;
497
+ }
498
+ }
499
+ }
500
+ }
501
+
502
+ if (outChanged) *outChanged = changed;
503
+
504
+ if (outDirtyRegion) {
505
+ if (changed) {
506
+ outDirtyRegion->minX = actualDirtyMinX;
507
+ outDirtyRegion->maxX = actualDirtyMaxX;
508
+ outDirtyRegion->minY = actualDirtyMinY;
509
+ outDirtyRegion->maxY = actualDirtyMaxY;
510
+ outDirtyRegion->minZ = actualDirtyMinZ;
511
+ outDirtyRegion->maxZ = actualDirtyMaxZ;
512
+ } else {
513
+ outDirtyRegion->minX = startX;
514
+ outDirtyRegion->maxX = endX;
515
+ outDirtyRegion->minY = startY;
516
+ outDirtyRegion->maxY = endY;
517
+ outDirtyRegion->minZ = startZ;
518
+ outDirtyRegion->maxZ = endZ;
519
+ }
520
+ }
521
+
522
+ return VoxelStatus::Success;
523
+ }
524
+
525
+ VoxelStatus incrementalCookVoxel(
526
+ const VoxelChunk& chunk,
527
+ const VoxelBoxChild* existingChildren,
528
+ std::uint32_t existingChildCount,
529
+ const VoxelCellAABB& dirtyRegion,
530
+ VoxelBoxChild* outChildren,
531
+ std::uint32_t maxOutChildren,
532
+ std::uint32_t* outChildCount,
533
+ std::uint32_t* inOutNextChildId,
534
+ VoxelCookStats* outStats,
535
+ std::uint8_t* scratch) {
536
+ if (chunk.dimX < 1 || chunk.dimY < 1 || chunk.dimZ < 1 || chunk.cellSize <= 0.0 || !chunk.occupancy) {
537
+ return VoxelStatus::InvalidDimensions;
538
+ }
539
+ if (!outChildren || !outChildCount) {
540
+ return VoxelStatus::NullBuffer;
541
+ }
542
+
543
+ std::int32_t activeMinX = voxelClampInt(std::min(dirtyRegion.minX, dirtyRegion.maxX), 0, chunk.dimX - 1);
544
+ std::int32_t activeMaxX = voxelClampInt(std::max(dirtyRegion.minX, dirtyRegion.maxX), 0, chunk.dimX - 1);
545
+ std::int32_t activeMinY = voxelClampInt(std::min(dirtyRegion.minY, dirtyRegion.maxY), 0, chunk.dimY - 1);
546
+ std::int32_t activeMaxY = voxelClampInt(std::max(dirtyRegion.minY, dirtyRegion.maxY), 0, chunk.dimY - 1);
547
+ std::int32_t activeMinZ = voxelClampInt(std::min(dirtyRegion.minZ, dirtyRegion.maxZ), 0, chunk.dimZ - 1);
548
+ std::int32_t activeMaxZ = voxelClampInt(std::max(dirtyRegion.minZ, dirtyRegion.maxZ), 0, chunk.dimZ - 1);
549
+
550
+ if (activeMinX > activeMaxX || activeMinY > activeMaxY || activeMinZ > activeMaxZ) {
551
+ if (existingChildCount > maxOutChildren) return VoxelStatus::BufferOverflow;
552
+ std::uint32_t countToCopy = existingChildCount;
553
+ for (std::uint32_t i = 0; i < countToCopy; ++i) {
554
+ outChildren[i] = existingChildren[i];
555
+ }
556
+ *outChildCount = countToCopy;
557
+ if (outStats) {
558
+ *outStats = VoxelCookStats{};
559
+ outStats->totalBoxes = countToCopy;
560
+ outStats->preservedBoxes = countToCopy;
561
+ }
562
+ return VoxelStatus::Unchanged;
563
+ }
564
+
565
+ VoxelCellAABB initialClamped = {activeMinX, activeMinY, activeMinZ, activeMaxX, activeMaxY, activeMaxZ};
566
+
567
+ bool expanded = true;
568
+ while (expanded) {
569
+ expanded = false;
570
+ for (std::uint32_t i = 0; i < existingChildCount; ++i) {
571
+ VoxelCellAABB b = deduceCellBounds(existingChildren[i], chunk.cellSize);
572
+ bool overlaps = (b.minX <= activeMaxX && b.maxX >= activeMinX &&
573
+ b.minY <= activeMaxY && b.maxY >= activeMinY &&
574
+ b.minZ <= activeMaxZ && b.maxZ >= activeMinZ);
575
+ if (overlaps) {
576
+ if (b.minX < activeMinX) { activeMinX = std::max(0, b.minX); expanded = true; }
577
+ if (b.maxX > activeMaxX) { activeMaxX = std::min(chunk.dimX - 1, b.maxX); expanded = true; }
578
+ if (b.minY < activeMinY) { activeMinY = std::max(0, b.minY); expanded = true; }
579
+ if (b.maxY > activeMaxY) { activeMaxY = std::min(chunk.dimY - 1, b.maxY); expanded = true; }
580
+ if (b.minZ < activeMinZ) { activeMinZ = std::max(0, b.minZ); expanded = true; }
581
+ if (b.maxZ > activeMaxZ) { activeMaxZ = std::min(chunk.dimZ - 1, b.maxZ); expanded = true; }
582
+ }
583
+ }
584
+ }
585
+
586
+ std::uint32_t preservedCount = 0;
587
+ std::uint32_t retiredCount = 0;
588
+ for (std::uint32_t i = 0; i < existingChildCount; ++i) {
589
+ VoxelCellAABB b = deduceCellBounds(existingChildren[i], chunk.cellSize);
590
+ bool overlaps = (b.minX <= activeMaxX && b.maxX >= activeMinX &&
591
+ b.minY <= activeMaxY && b.maxY >= activeMinY &&
592
+ b.minZ <= activeMaxZ && b.maxZ >= activeMinZ);
593
+ if (!overlaps) {
594
+ if (preservedCount >= maxOutChildren) return VoxelStatus::BufferOverflow;
595
+ outChildren[preservedCount] = existingChildren[i];
596
+ outChildren[preservedCount].cellBounds = b;
597
+ outChildren[preservedCount].flags = ChildFlagPreserved;
598
+ preservedCount++;
599
+ } else {
600
+ retiredCount++;
601
+ }
602
+ }
603
+
604
+ std::int32_t subDimX = activeMaxX - activeMinX + 1;
605
+ std::int32_t subDimY = activeMaxY - activeMinY + 1;
606
+ std::int32_t subDimZ = activeMaxZ - activeMinZ + 1;
607
+ std::int32_t subTotal = subDimX * subDimY * subDimZ;
608
+
609
+ std::vector<std::uint8_t> localScratch;
610
+ std::uint8_t* consumed = scratch;
611
+ if (!consumed) {
612
+ localScratch.assign(static_cast<std::size_t>(subTotal), 0);
613
+ consumed = localScratch.data();
614
+ } else {
615
+ std::memset(consumed, 0, static_cast<std::size_t>(subTotal));
616
+ }
617
+
618
+ auto localIndex = [&](std::int32_t x, std::int32_t y, std::int32_t z) -> std::int32_t {
619
+ return (x - activeMinX) + subDimX * ((y - activeMinY) + subDimY * (z - activeMinZ));
620
+ };
621
+
622
+ auto freeSubCell = [&](std::int32_t x, std::int32_t y, std::int32_t z) -> bool {
623
+ std::int32_t cIdx = chunk.cellIndex(x, y, z);
624
+ if (chunk.occupancy[cIdx] == 0) return false;
625
+ return consumed[localIndex(x, y, z)] == 0;
626
+ };
627
+
628
+ std::uint32_t nextId = inOutNextChildId ? *inOutNextChildId : 0;
629
+ std::uint32_t recookedCount = 0;
630
+
631
+ for (std::int32_t z = activeMinZ; z <= activeMaxZ; ++z) {
632
+ for (std::int32_t y = activeMinY; y <= activeMaxY; ++y) {
633
+ for (std::int32_t x = activeMinX; x <= activeMaxX; ++x) {
634
+ if (!freeSubCell(x, y, z)) {
635
+ continue;
636
+ }
637
+
638
+ std::int32_t x1 = x;
639
+ while (x1 + 1 <= activeMaxX && freeSubCell(x1 + 1, y, z)) {
640
+ x1 += 1;
641
+ }
642
+
643
+ auto rowFree = [&](std::int32_t yy, std::int32_t zz) -> bool {
644
+ for (std::int32_t xx = x; xx <= x1; ++xx) {
645
+ if (!freeSubCell(xx, yy, zz)) {
646
+ return false;
647
+ }
648
+ }
649
+ return true;
650
+ };
651
+
652
+ std::int32_t y1 = y;
653
+ while (y1 + 1 <= activeMaxY && rowFree(y1 + 1, z)) {
654
+ y1 += 1;
655
+ }
656
+
657
+ auto slabFree = [&](std::int32_t zz) -> bool {
658
+ for (std::int32_t yy = y; yy <= y1; ++yy) {
659
+ if (!rowFree(yy, zz)) {
660
+ return false;
661
+ }
662
+ }
663
+ return true;
664
+ };
665
+
666
+ std::int32_t z1 = z;
667
+ while (z1 + 1 <= activeMaxZ && slabFree(z1 + 1)) {
668
+ z1 += 1;
669
+ }
670
+
671
+ for (std::int32_t zz = z; zz <= z1; ++zz) {
672
+ for (std::int32_t yy = y; yy <= y1; ++yy) {
673
+ for (std::int32_t xx = x; xx <= x1; ++xx) {
674
+ consumed[localIndex(xx, yy, zz)] = 1;
675
+ }
676
+ }
677
+ }
678
+
679
+ std::uint32_t destIdx = preservedCount + recookedCount;
680
+ if (destIdx >= maxOutChildren) {
681
+ *outChildCount = destIdx;
682
+ if (inOutNextChildId) *inOutNextChildId = nextId;
683
+ return VoxelStatus::BufferOverflow;
684
+ }
685
+
686
+ VoxelBoxChild& box = outChildren[destIdx];
687
+ box.childId = nextId++;
688
+ box.halfExtents = {
689
+ ((x1 - x + 1) * chunk.cellSize) * 0.5,
690
+ ((y1 - y + 1) * chunk.cellSize) * 0.5,
691
+ ((z1 - z + 1) * chunk.cellSize) * 0.5
692
+ };
693
+ box.offset = {
694
+ ((x + x1) * 0.5) * chunk.cellSize,
695
+ ((y + y1) * 0.5) * chunk.cellSize,
696
+ ((z + z1) * 0.5) * chunk.cellSize
697
+ };
698
+ box.cellBounds = {x, y, z, x1, y1, z1};
699
+ box.flags = ChildFlagRecooked;
700
+ recookedCount++;
701
+ }
702
+ }
703
+ }
704
+
705
+ std::uint32_t totalResultBoxes = preservedCount + recookedCount;
706
+ *outChildCount = totalResultBoxes;
707
+ if (inOutNextChildId) *inOutNextChildId = nextId;
708
+
709
+ if (outStats) {
710
+ outStats->totalBoxes = totalResultBoxes;
711
+ outStats->preservedBoxes = preservedCount;
712
+ outStats->recookedBoxes = recookedCount;
713
+ outStats->retiredBoxes = retiredCount;
714
+ outStats->cellsEvaluated = static_cast<std::uint32_t>(subTotal);
715
+ outStats->dirtyAABB = initialClamped;
716
+ outStats->expandedAABB = {activeMinX, activeMinY, activeMinZ, activeMaxX, activeMaxY, activeMaxZ};
717
+ }
718
+
719
+ if (totalResultBoxes == 0) {
720
+ return VoxelStatus::NoOccupiedVoxels;
721
+ }
722
+
723
+ return VoxelStatus::Success;
724
+ }
725
+
726
+ VoxelStatus splitVoxelComponents(
727
+ const VoxelChunk& chunk,
728
+ std::uint32_t anchorFacesMask,
729
+ VoxelSubChunk* outSubChunks,
730
+ std::uint32_t maxSubChunks,
731
+ std::uint32_t* outSubChunkCount,
732
+ std::uint8_t* outSubOccupancyBuffer,
733
+ std::uint32_t maxSubOccupancyBytes,
734
+ std::uint32_t* outSubOccupancyBytesUsed,
735
+ std::uint8_t* scratch) {
736
+ if (chunk.dimX < 1 || chunk.dimY < 1 || chunk.dimZ < 1 || chunk.cellSize <= 0.0 || !chunk.occupancy) {
737
+ return VoxelStatus::InvalidDimensions;
738
+ }
739
+ if (!outSubChunks || !outSubChunkCount) {
740
+ return VoxelStatus::NullBuffer;
741
+ }
742
+
743
+ const std::int32_t totalCells = chunk.totalCells();
744
+ if (totalCells == 0) {
745
+ *outSubChunkCount = 0;
746
+ if (outSubOccupancyBytesUsed) *outSubOccupancyBytesUsed = 0;
747
+ return VoxelStatus::Success;
748
+ }
749
+
750
+ const std::int32_t sliceSize = chunk.dimX * chunk.dimY;
751
+
752
+ std::vector<std::uint8_t> localVisited;
753
+ std::vector<std::int32_t> localQueue;
754
+ std::uint8_t* visited = nullptr;
755
+ std::int32_t* queueIdx = nullptr;
756
+
757
+ if (scratch) {
758
+ queueIdx = reinterpret_cast<std::int32_t*>(scratch);
759
+ visited = scratch + static_cast<std::size_t>(totalCells) * sizeof(std::int32_t);
760
+ } else {
761
+ localVisited.assign(static_cast<std::size_t>(totalCells), 0);
762
+ localQueue.resize(static_cast<std::size_t>(totalCells), 0);
763
+ visited = localVisited.data();
764
+ queueIdx = localQueue.data();
765
+ }
766
+ std::memset(visited, 0, static_cast<std::size_t>(totalCells));
767
+
768
+ auto checkAnchored = [&](std::int32_t x, std::int32_t y, std::int32_t z) -> bool {
769
+ if ((anchorFacesMask & AnchorNegX) && x == 0) return true;
770
+ if ((anchorFacesMask & AnchorPosX) && x == chunk.dimX - 1) return true;
771
+ if ((anchorFacesMask & AnchorNegY) && y == 0) return true;
772
+ if ((anchorFacesMask & AnchorPosY) && y == chunk.dimY - 1) return true;
773
+ if ((anchorFacesMask & AnchorNegZ) && z == 0) return true;
774
+ if ((anchorFacesMask & AnchorPosZ) && z == chunk.dimZ - 1) return true;
775
+ return false;
776
+ };
777
+
778
+ std::uint32_t subChunkCount = 0;
779
+ std::uint32_t occupancyBytesWritten = 0;
780
+
781
+ for (std::int32_t z = 0; z < chunk.dimZ; ++z) {
782
+ for (std::int32_t y = 0; y < chunk.dimY; ++y) {
783
+ for (std::int32_t x = 0; x < chunk.dimX; ++x) {
784
+ std::int32_t seedIdx = x + chunk.dimX * y + sliceSize * z;
785
+ if (visited[seedIdx] != 0 || chunk.occupancy[seedIdx] == 0) {
786
+ continue;
787
+ }
788
+
789
+ std::int32_t head = 0;
790
+ std::int32_t tail = 0;
791
+ queueIdx[tail++] = seedIdx;
792
+ visited[seedIdx] = 1;
793
+
794
+ std::int32_t minX = x, maxX = x;
795
+ std::int32_t minY = y, maxY = y;
796
+ std::int32_t minZ = z, maxZ = z;
797
+ bool isAnchored = checkAnchored(x, y, z);
798
+
799
+ while (head < tail) {
800
+ std::int32_t currIdx = queueIdx[head++];
801
+ std::int32_t cz = currIdx / sliceSize;
802
+ std::int32_t rem = currIdx % sliceSize;
803
+ std::int32_t cy = rem / chunk.dimX;
804
+ std::int32_t cx = rem % chunk.dimX;
805
+
806
+ if (cx < minX) minX = cx;
807
+ if (cx > maxX) maxX = cx;
808
+ if (cy < minY) minY = cy;
809
+ if (cy > maxY) maxY = cy;
810
+ if (cz < minZ) minZ = cz;
811
+ if (cz > maxZ) maxZ = cz;
812
+
813
+ if (!isAnchored && checkAnchored(cx, cy, cz)) {
814
+ isAnchored = true;
815
+ }
816
+
817
+ if (cx > 0) {
818
+ std::int32_t nIdx = currIdx - 1;
819
+ if (visited[nIdx] == 0 && chunk.occupancy[nIdx] != 0) {
820
+ visited[nIdx] = 1;
821
+ queueIdx[tail++] = nIdx;
822
+ }
823
+ }
824
+ if (cx + 1 < chunk.dimX) {
825
+ std::int32_t nIdx = currIdx + 1;
826
+ if (visited[nIdx] == 0 && chunk.occupancy[nIdx] != 0) {
827
+ visited[nIdx] = 1;
828
+ queueIdx[tail++] = nIdx;
829
+ }
830
+ }
831
+ if (cy > 0) {
832
+ std::int32_t nIdx = currIdx - chunk.dimX;
833
+ if (visited[nIdx] == 0 && chunk.occupancy[nIdx] != 0) {
834
+ visited[nIdx] = 1;
835
+ queueIdx[tail++] = nIdx;
836
+ }
837
+ }
838
+ if (cy + 1 < chunk.dimY) {
839
+ std::int32_t nIdx = currIdx + chunk.dimX;
840
+ if (visited[nIdx] == 0 && chunk.occupancy[nIdx] != 0) {
841
+ visited[nIdx] = 1;
842
+ queueIdx[tail++] = nIdx;
843
+ }
844
+ }
845
+ if (cz > 0) {
846
+ std::int32_t nIdx = currIdx - sliceSize;
847
+ if (visited[nIdx] == 0 && chunk.occupancy[nIdx] != 0) {
848
+ visited[nIdx] = 1;
849
+ queueIdx[tail++] = nIdx;
850
+ }
851
+ }
852
+ if (cz + 1 < chunk.dimZ) {
853
+ std::int32_t nIdx = currIdx + sliceSize;
854
+ if (visited[nIdx] == 0 && chunk.occupancy[nIdx] != 0) {
855
+ visited[nIdx] = 1;
856
+ queueIdx[tail++] = nIdx;
857
+ }
858
+ }
859
+ }
860
+
861
+ if (subChunkCount >= maxSubChunks) {
862
+ *outSubChunkCount = subChunkCount;
863
+ if (outSubOccupancyBytesUsed) *outSubOccupancyBytesUsed = occupancyBytesWritten;
864
+ return VoxelStatus::BufferOverflow;
865
+ }
866
+
867
+ VoxelSubChunk& sc = outSubChunks[subChunkCount++];
868
+ sc.subChunkId = subChunkCount - 1;
869
+ sc.dimX = maxX - minX + 1;
870
+ sc.dimY = maxY - minY + 1;
871
+ sc.dimZ = maxZ - minZ + 1;
872
+ sc.cellSize = chunk.cellSize;
873
+ sc.offsetCells = {minX, minY, minZ};
874
+ sc.occupiedCount = static_cast<std::uint32_t>(tail);
875
+ sc.anchored = isAnchored;
876
+
877
+ std::uint32_t subTotal = static_cast<std::uint32_t>(sc.dimX * sc.dimY * sc.dimZ);
878
+ if (outSubOccupancyBuffer && (occupancyBytesWritten + subTotal <= maxSubOccupancyBytes)) {
879
+ std::uint8_t* dst = outSubOccupancyBuffer + occupancyBytesWritten;
880
+ std::memset(dst, 0, subTotal);
881
+ std::int32_t subSlice = sc.dimX * sc.dimY;
882
+ for (std::int32_t i = 0; i < tail; ++i) {
883
+ std::int32_t vIdx = queueIdx[i];
884
+ std::int32_t vz = vIdx / sliceSize;
885
+ std::int32_t vrem = vIdx % sliceSize;
886
+ std::int32_t vy = vrem / chunk.dimX;
887
+ std::int32_t vx = vrem % chunk.dimX;
888
+ std::int32_t subIdx = (vx - minX) + sc.dimX * (vy - minY) + subSlice * (vz - minZ);
889
+ dst[subIdx] = chunk.occupancy[vIdx];
890
+ }
891
+ occupancyBytesWritten += subTotal;
892
+ }
893
+ }
894
+ }
895
+ }
896
+
897
+ std::stable_sort(outSubChunks, outSubChunks + subChunkCount, SubChunkSortOrder{});
898
+
899
+ for (std::uint32_t i = 0; i < subChunkCount; ++i) {
900
+ outSubChunks[i].subChunkId = i;
901
+ }
902
+
903
+ *outSubChunkCount = subChunkCount;
904
+ if (outSubOccupancyBytesUsed) *outSubOccupancyBytesUsed = occupancyBytesWritten;
905
+
906
+ return VoxelStatus::Success;
907
+ }
908
+
909
+ VoxelStatus incrementalSliceCompound(
910
+ const VoxelBoxChild* parentChildren,
911
+ std::uint32_t parentChildCount,
912
+ double cellSize,
913
+ const VoxelSubChunk* subChunks,
914
+ std::uint32_t subChunkCount,
915
+ const std::uint8_t* subOccupancies,
916
+ VoxelBoxChild* outSubChildren,
917
+ std::uint32_t maxOutSubChildren,
918
+ std::uint32_t* outSubChildCounts,
919
+ std::uint32_t* inOutNextChildId,
920
+ std::uint32_t* outPreservedTotal,
921
+ std::uint32_t* outRecookedTotal,
922
+ std::uint8_t* scratch) {
923
+ if (cellSize <= 0.0 || !parentChildren || !subChunks || !subOccupancies || !outSubChildren || !outSubChildCounts) {
924
+ return VoxelStatus::NullBuffer;
925
+ }
926
+
927
+ std::uint32_t totalPreserved = 0;
928
+ std::uint32_t totalRecooked = 0;
929
+ std::uint32_t globalChildOffset = 0;
930
+ std::uint32_t nextId = inOutNextChildId ? *inOutNextChildId : 0;
931
+ std::uint32_t occupancyByteOffset = 0;
932
+
933
+ for (std::uint32_t scIdx = 0; scIdx < subChunkCount; ++scIdx) {
934
+ const VoxelSubChunk& sc = subChunks[scIdx];
935
+ const std::uint8_t* subOcc = subOccupancies + occupancyByteOffset;
936
+ const std::int32_t scTotalCells = sc.dimX * sc.dimY * sc.dimZ;
937
+ occupancyByteOffset += static_cast<std::uint32_t>(scTotalCells);
938
+
939
+ const std::int32_t scX = sc.offsetCells.x;
940
+ const std::int32_t scY = sc.offsetCells.y;
941
+ const std::int32_t scZ = sc.offsetCells.z;
942
+ const std::int32_t maxBoundX = scX + sc.dimX - 1;
943
+ const std::int32_t maxBoundY = scY + sc.dimY - 1;
944
+ const std::int32_t maxBoundZ = scZ + sc.dimZ - 1;
945
+
946
+ std::vector<std::uint8_t> localCovered;
947
+ std::uint8_t* coveredInSubChunk = scratch;
948
+ if (!coveredInSubChunk) {
949
+ localCovered.assign(static_cast<std::size_t>(scTotalCells), 0);
950
+ coveredInSubChunk = localCovered.data();
951
+ } else {
952
+ std::memset(coveredInSubChunk, 0, static_cast<std::size_t>(scTotalCells));
953
+ }
954
+
955
+ std::uint32_t subPreservedCount = 0;
956
+
957
+ for (std::uint32_t i = 0; i < parentChildCount; ++i) {
958
+ const VoxelBoxChild& p = parentChildren[i];
959
+ VoxelCellAABB pb = deduceCellBounds(p, cellSize);
960
+
961
+ if (pb.minX >= scX && pb.maxX <= maxBoundX &&
962
+ pb.minY >= scY && pb.maxY <= maxBoundY &&
963
+ pb.minZ >= scZ && pb.maxZ <= maxBoundZ) {
964
+
965
+ bool isIntact = true;
966
+ for (std::int32_t bz = pb.minZ; bz <= pb.maxZ && isIntact; ++bz) {
967
+ for (std::int32_t by = pb.minY; by <= pb.maxY && isIntact; ++by) {
968
+ for (std::int32_t bx = pb.minX; bx <= pb.maxX; ++bx) {
969
+ std::int32_t lx = bx - scX;
970
+ std::int32_t ly = by - scY;
971
+ std::int32_t lz = bz - scZ;
972
+ std::int32_t idx = lx + sc.dimX * (ly + sc.dimY * lz);
973
+ if (subOcc[idx] == 0) {
974
+ isIntact = false;
975
+ break;
976
+ }
977
+ }
978
+ }
979
+ }
980
+
981
+ if (isIntact) {
982
+ for (std::int32_t bz = pb.minZ; bz <= pb.maxZ; ++bz) {
983
+ for (std::int32_t by = pb.minY; by <= pb.maxY; ++by) {
984
+ for (std::int32_t bx = pb.minX; bx <= pb.maxX; ++bx) {
985
+ std::int32_t lx = bx - scX;
986
+ std::int32_t ly = by - scY;
987
+ std::int32_t lz = bz - scZ;
988
+ std::int32_t idx = lx + sc.dimX * (ly + sc.dimY * lz);
989
+ coveredInSubChunk[idx] = 1;
990
+ }
991
+ }
992
+ }
993
+
994
+ if (globalChildOffset + subPreservedCount >= maxOutSubChildren) {
995
+ return VoxelStatus::BufferOverflow;
996
+ }
997
+
998
+ VoxelBoxChild& dest = outSubChildren[globalChildOffset + subPreservedCount];
999
+ dest.childId = p.childId; // Stable child identity preserved!
1000
+ dest.halfExtents = p.halfExtents;
1001
+ dest.offset = {
1002
+ p.offset.x - static_cast<double>(scX) * cellSize,
1003
+ p.offset.y - static_cast<double>(scY) * cellSize,
1004
+ p.offset.z - static_cast<double>(scZ) * cellSize
1005
+ };
1006
+ dest.cellBounds = {
1007
+ pb.minX - scX,
1008
+ pb.minY - scY,
1009
+ pb.minZ - scZ,
1010
+ pb.maxX - scX,
1011
+ pb.maxY - scY,
1012
+ pb.maxZ - scZ
1013
+ };
1014
+ dest.flags = ChildFlagPreserved;
1015
+ subPreservedCount++;
1016
+ }
1017
+ }
1018
+ }
1019
+
1020
+ totalPreserved += subPreservedCount;
1021
+
1022
+ std::int32_t uncoveredCount = 0;
1023
+ for (std::int32_t i = 0; i < scTotalCells; ++i) {
1024
+ if (subOcc[i] != 0 && coveredInSubChunk[i] == 0) {
1025
+ uncoveredCount++;
1026
+ }
1027
+ }
1028
+
1029
+ std::uint32_t subRecookedCount = 0;
1030
+
1031
+ if (uncoveredCount > 0) {
1032
+ std::vector<std::uint8_t> localConsumed(static_cast<std::size_t>(scTotalCells), 0);
1033
+
1034
+ auto freeUncovered = [&](std::int32_t x, std::int32_t y, std::int32_t z) -> bool {
1035
+ std::int32_t idx = x + sc.dimX * (y + sc.dimY * z);
1036
+ return subOcc[idx] != 0 && coveredInSubChunk[idx] == 0 && localConsumed[idx] == 0;
1037
+ };
1038
+
1039
+ for (std::int32_t z = 0; z < sc.dimZ; ++z) {
1040
+ for (std::int32_t y = 0; y < sc.dimY; ++y) {
1041
+ for (std::int32_t x = 0; x < sc.dimX; ++x) {
1042
+ if (!freeUncovered(x, y, z)) {
1043
+ continue;
1044
+ }
1045
+
1046
+ std::int32_t x1 = x;
1047
+ while (x1 + 1 < sc.dimX && freeUncovered(x1 + 1, y, z)) {
1048
+ x1 += 1;
1049
+ }
1050
+
1051
+ auto rowFree = [&](std::int32_t yy, std::int32_t zz) -> bool {
1052
+ for (std::int32_t xx = x; xx <= x1; ++xx) {
1053
+ if (!freeUncovered(xx, yy, zz)) {
1054
+ return false;
1055
+ }
1056
+ }
1057
+ return true;
1058
+ };
1059
+
1060
+ std::int32_t y1 = y;
1061
+ while (y1 + 1 < sc.dimY && rowFree(y1 + 1, z)) {
1062
+ y1 += 1;
1063
+ }
1064
+
1065
+ auto slabFree = [&](std::int32_t zz) -> bool {
1066
+ for (std::int32_t yy = y; yy <= y1; ++yy) {
1067
+ if (!rowFree(yy, zz)) {
1068
+ return false;
1069
+ }
1070
+ }
1071
+ return true;
1072
+ };
1073
+
1074
+ std::int32_t z1 = z;
1075
+ while (z1 + 1 < sc.dimZ && slabFree(z1 + 1)) {
1076
+ z1 += 1;
1077
+ }
1078
+
1079
+ for (std::int32_t zz = z; zz <= z1; ++zz) {
1080
+ for (std::int32_t yy = y; yy <= y1; ++yy) {
1081
+ for (std::int32_t xx = x; xx <= x1; ++xx) {
1082
+ localConsumed[xx + sc.dimX * (yy + sc.dimY * zz)] = 1;
1083
+ }
1084
+ }
1085
+ }
1086
+
1087
+ std::uint32_t destIdx = globalChildOffset + subPreservedCount + subRecookedCount;
1088
+ if (destIdx >= maxOutSubChildren) {
1089
+ return VoxelStatus::BufferOverflow;
1090
+ }
1091
+
1092
+ VoxelBoxChild& box = outSubChildren[destIdx];
1093
+ box.childId = nextId++;
1094
+ box.halfExtents = {
1095
+ ((x1 - x + 1) * sc.cellSize) * 0.5,
1096
+ ((y1 - y + 1) * sc.cellSize) * 0.5,
1097
+ ((z1 - z + 1) * sc.cellSize) * 0.5
1098
+ };
1099
+ box.offset = {
1100
+ ((x + x1) * 0.5) * sc.cellSize,
1101
+ ((y + y1) * 0.5) * sc.cellSize,
1102
+ ((z + z1) * 0.5) * sc.cellSize
1103
+ };
1104
+ box.cellBounds = {x, y, z, x1, y1, z1};
1105
+ box.flags = ChildFlagRecooked;
1106
+ subRecookedCount++;
1107
+ }
1108
+ }
1109
+ }
1110
+ }
1111
+
1112
+ totalRecooked += subRecookedCount;
1113
+ outSubChildCounts[scIdx] = subPreservedCount + subRecookedCount;
1114
+ globalChildOffset += (subPreservedCount + subRecookedCount);
1115
+ }
1116
+
1117
+ if (inOutNextChildId) *inOutNextChildId = nextId;
1118
+ if (outPreservedTotal) *outPreservedTotal = totalPreserved;
1119
+ if (outRecookedTotal) *outRecookedTotal = totalRecooked;
1120
+
1121
+ return VoxelStatus::Success;
1122
+ }
1123
+
1124
+ VoxelStatus raycastVoxelDDA(
1125
+ const VoxelChunk& chunk,
1126
+ const VoxelRaycastQuery& query,
1127
+ VoxelRaycastHit* outHit) {
1128
+ if (!outHit) return VoxelStatus::NullBuffer;
1129
+ outHit->hit = false;
1130
+ outHit->distance = 0.0;
1131
+ outHit->point = {0.0, 0.0, 0.0};
1132
+ outHit->normal = {0.0, 0.0, 0.0};
1133
+ outHit->voxelCoords = {0, 0, 0};
1134
+ outHit->childIndex = -1;
1135
+
1136
+ if (chunk.dimX < 1 || chunk.dimY < 1 || chunk.dimZ < 1 || chunk.cellSize <= 0.0 || !chunk.occupancy) {
1137
+ return VoxelStatus::InvalidDimensions;
1138
+ }
1139
+
1140
+ double dirLen = std::hypot(query.direction.x, query.direction.y, query.direction.z);
1141
+ if (!std::isfinite(dirLen) || dirLen <= 0) return VoxelStatus::InvalidOperation;
1142
+
1143
+ double dx = query.direction.x / dirLen;
1144
+ double dy = query.direction.y / dirLen;
1145
+ double dz = query.direction.z / dirLen;
1146
+
1147
+ if (query.maxDistance < 0.0) {
1148
+ return VoxelStatus::Success;
1149
+ }
1150
+
1151
+ double ox = query.origin.x / chunk.cellSize + 0.5;
1152
+ double oy = query.origin.y / chunk.cellSize + 0.5;
1153
+ double oz = query.origin.z / chunk.cellSize + 0.5;
1154
+
1155
+ double tMin = 0.0;
1156
+ double tMax = query.maxDistance / chunk.cellSize;
1157
+ VoxelVec3 enterNormal{0.0, 0.0, 0.0};
1158
+
1159
+ struct AxisCheck {
1160
+ double o;
1161
+ double d;
1162
+ double min;
1163
+ double max;
1164
+ double nx;
1165
+ double ny;
1166
+ double nz;
1167
+ };
1168
+
1169
+ std::array<AxisCheck, 3> axes{{
1170
+ {ox, dx, 0.0, static_cast<double>(chunk.dimX), 1.0, 0.0, 0.0},
1171
+ {oy, dy, 0.0, static_cast<double>(chunk.dimY), 0.0, 1.0, 0.0},
1172
+ {oz, dz, 0.0, static_cast<double>(chunk.dimZ), 0.0, 0.0, 1.0},
1173
+ }};
1174
+
1175
+ for (const auto& ax : axes) {
1176
+ if (std::abs(ax.d) < 1e-12) {
1177
+ if (ax.o < ax.min || ax.o > ax.max) {
1178
+ return VoxelStatus::Success;
1179
+ }
1180
+ } else {
1181
+ double t1 = (ax.min - ax.o) / ax.d;
1182
+ double t2 = (ax.max - ax.o) / ax.d;
1183
+ VoxelVec3 n1{-ax.nx, -ax.ny, -ax.nz};
1184
+ if (t1 > t2) {
1185
+ std::swap(t1, t2);
1186
+ n1 = {ax.nx, ax.ny, ax.nz};
1187
+ }
1188
+ if (t1 > tMin) {
1189
+ tMin = t1;
1190
+ enterNormal = n1;
1191
+ }
1192
+ if (t2 < tMax) {
1193
+ tMax = t2;
1194
+ }
1195
+ if (tMin > tMax) {
1196
+ return VoxelStatus::Success;
1197
+ }
1198
+ }
1199
+ }
1200
+
1201
+ if (ox >= 0.0 && ox < static_cast<double>(chunk.dimX) &&
1202
+ oy >= 0.0 && oy < static_cast<double>(chunk.dimY) &&
1203
+ oz >= 0.0 && oz < static_cast<double>(chunk.dimZ)) {
1204
+ tMin = 0.0;
1205
+ }
1206
+
1207
+ double startX = ox + dx * tMin;
1208
+ double startY = oy + dy * tMin;
1209
+ double startZ = oz + dz * tMin;
1210
+
1211
+ std::int32_t currentX = voxelClampInt(static_cast<std::int32_t>(std::floor(startX)), 0, chunk.dimX - 1);
1212
+ std::int32_t currentY = voxelClampInt(static_cast<std::int32_t>(std::floor(startY)), 0, chunk.dimY - 1);
1213
+ std::int32_t currentZ = voxelClampInt(static_cast<std::int32_t>(std::floor(startZ)), 0, chunk.dimZ - 1);
1214
+
1215
+ std::int32_t stepX = (dx > 0.0) ? 1 : ((dx < 0.0) ? -1 : 0);
1216
+ std::int32_t stepY = (dy > 0.0) ? 1 : ((dy < 0.0) ? -1 : 0);
1217
+ std::int32_t stepZ = (dz > 0.0) ? 1 : ((dz < 0.0) ? -1 : 0);
1218
+
1219
+ constexpr double kInf = 1e30;
1220
+ double tDeltaX = (stepX != 0) ? std::abs(1.0 / dx) : kInf;
1221
+ double tDeltaY = (stepY != 0) ? std::abs(1.0 / dy) : kInf;
1222
+ double tDeltaZ = (stepZ != 0) ? std::abs(1.0 / dz) : kInf;
1223
+
1224
+ double tMaxX = (stepX > 0) ? (static_cast<double>(currentX + 1) - ox) / dx
1225
+ : ((stepX < 0) ? (static_cast<double>(currentX) - ox) / dx : kInf);
1226
+ double tMaxY = (stepY > 0) ? (static_cast<double>(currentY + 1) - oy) / dy
1227
+ : ((stepY < 0) ? (static_cast<double>(currentY) - oy) / dy : kInf);
1228
+ double tMaxZ = (stepZ > 0) ? (static_cast<double>(currentZ + 1) - oz) / dz
1229
+ : ((stepZ < 0) ? (static_cast<double>(currentZ) - oz) / dz : kInf);
1230
+
1231
+ VoxelVec3 currentNormal = enterNormal;
1232
+ double hitDistanceGrid = tMin;
1233
+
1234
+ while (currentX >= 0 && currentX < chunk.dimX &&
1235
+ currentY >= 0 && currentY < chunk.dimY &&
1236
+ currentZ >= 0 && currentZ < chunk.dimZ) {
1237
+ std::int32_t cellIndex = chunk.cellIndex(currentX, currentY, currentZ);
1238
+ if (chunk.occupancy[cellIndex] != 0) {
1239
+ double worldDistance = hitDistanceGrid * chunk.cellSize;
1240
+ if (worldDistance > query.maxDistance) {
1241
+ return VoxelStatus::Success;
1242
+ }
1243
+ outHit->hit = true;
1244
+ outHit->distance = worldDistance;
1245
+ outHit->voxelCoords = {currentX, currentY, currentZ};
1246
+ outHit->point = {
1247
+ query.origin.x + dx * worldDistance,
1248
+ query.origin.y + dy * worldDistance,
1249
+ query.origin.z + dz * worldDistance
1250
+ };
1251
+ outHit->normal = currentNormal;
1252
+ outHit->childIndex = -1;
1253
+ return VoxelStatus::Success;
1254
+ }
1255
+
1256
+ if (tMaxX < tMaxY) {
1257
+ if (tMaxX < tMaxZ) {
1258
+ hitDistanceGrid = tMaxX;
1259
+ tMaxX += tDeltaX;
1260
+ currentX += stepX;
1261
+ currentNormal = {-static_cast<double>(stepX), 0.0, 0.0};
1262
+ } else {
1263
+ hitDistanceGrid = tMaxZ;
1264
+ tMaxZ += tDeltaZ;
1265
+ currentZ += stepZ;
1266
+ currentNormal = {0.0, 0.0, -static_cast<double>(stepZ)};
1267
+ }
1268
+ } else {
1269
+ if (tMaxY < tMaxZ) {
1270
+ hitDistanceGrid = tMaxY;
1271
+ tMaxY += tDeltaY;
1272
+ currentY += stepY;
1273
+ currentNormal = {0.0, -static_cast<double>(stepY), 0.0};
1274
+ } else {
1275
+ hitDistanceGrid = tMaxZ;
1276
+ tMaxZ += tDeltaZ;
1277
+ currentZ += stepZ;
1278
+ currentNormal = {0.0, 0.0, -static_cast<double>(stepZ)};
1279
+ }
1280
+ }
1281
+
1282
+ if (hitDistanceGrid > tMax) {
1283
+ break;
1284
+ }
1285
+ }
1286
+
1287
+ return VoxelStatus::Success;
1288
+ }
1289
+
1290
+ VoxelStatus queryVoxelSeam(
1291
+ const VoxelSeamQuery& query,
1292
+ VoxelSeamResult* outResult) {
1293
+ if (!outResult) return VoxelStatus::NullBuffer;
1294
+ *outResult = VoxelSeamResult{};
1295
+
1296
+ const auto& a = query.chunkA;
1297
+ const auto& b = query.chunkB;
1298
+ if (a.dimX < 1 || a.dimY < 1 || a.dimZ < 1 || b.dimX < 1 || b.dimY < 1 || b.dimZ < 1) {
1299
+ return VoxelStatus::InvalidDimensions;
1300
+ }
1301
+ if (!a.occupancy || !b.occupancy) {
1302
+ return VoxelStatus::NullBuffer;
1303
+ }
1304
+ if (std::abs(a.cellSize - b.cellSize) > 1e-9) {
1305
+ return VoxelStatus::IncompatibleGrids;
1306
+ }
1307
+ const double cellSize = a.cellSize;
1308
+
1309
+ std::int32_t minAx = query.offsetA.x;
1310
+ std::int32_t maxAx = query.offsetA.x + a.dimX - 1;
1311
+ std::int32_t minAy = query.offsetA.y;
1312
+ std::int32_t maxAy = query.offsetA.y + a.dimY - 1;
1313
+ std::int32_t minAz = query.offsetA.z;
1314
+ std::int32_t maxAz = query.offsetA.z + a.dimZ - 1;
1315
+
1316
+ std::int32_t minBx = query.offsetB.x;
1317
+ std::int32_t maxBx = query.offsetB.x + b.dimX - 1;
1318
+ std::int32_t minBy = query.offsetB.y;
1319
+ std::int32_t maxBy = query.offsetB.y + b.dimY - 1;
1320
+ std::int32_t minBz = query.offsetB.z;
1321
+ std::int32_t maxBz = query.offsetB.z + b.dimZ - 1;
1322
+
1323
+ std::int32_t seamAxis = query.cutPlaneAxis;
1324
+ std::int32_t seamCoord = query.cutCoordinate;
1325
+ VoxelVec3 normal{0.0, 0.0, 0.0};
1326
+
1327
+ if (seamAxis < 0) {
1328
+ if (maxAx + 1 == minBx) {
1329
+ seamAxis = 0; seamCoord = minBx; normal = {1.0, 0.0, 0.0};
1330
+ } else if (maxBx + 1 == minAx) {
1331
+ seamAxis = 0; seamCoord = minAx; normal = {-1.0, 0.0, 0.0};
1332
+ } else if (maxAy + 1 == minBy) {
1333
+ seamAxis = 1; seamCoord = minBy; normal = {0.0, 1.0, 0.0};
1334
+ } else if (maxBy + 1 == minAy) {
1335
+ seamAxis = 1; seamCoord = minAy; normal = {0.0, -1.0, 0.0};
1336
+ } else if (maxAz + 1 == minBz) {
1337
+ seamAxis = 2; seamCoord = minBz; normal = {0.0, 0.0, 1.0};
1338
+ } else if (maxBz + 1 == minAz) {
1339
+ seamAxis = 2; seamCoord = minAz; normal = {0.0, 0.0, -1.0};
1340
+ } else {
1341
+ outResult->hasSeam = false;
1342
+ return VoxelStatus::Success;
1343
+ }
1344
+ } else {
1345
+ if (seamAxis == 0) normal = {1.0, 0.0, 0.0};
1346
+ else if (seamAxis == 1) normal = {0.0, 1.0, 0.0};
1347
+ else normal = {0.0, 0.0, 1.0};
1348
+ }
1349
+
1350
+ std::uint32_t sharedFaces = 0;
1351
+ std::uint32_t exposedA = 0;
1352
+ std::uint32_t exposedB = 0;
1353
+ VoxelVec3 centroidSum{0.0, 0.0, 0.0};
1354
+ VoxelCellAABB seamBounds{1000000, 1000000, 1000000, -1000000, -1000000, -1000000};
1355
+
1356
+ if (seamAxis == 0) {
1357
+ std::int32_t overlapMinY = std::max(minAy, minBy);
1358
+ std::int32_t overlapMaxY = std::min(maxAy, maxBy);
1359
+ std::int32_t overlapMinZ = std::max(minAz, minBz);
1360
+ std::int32_t overlapMaxZ = std::min(maxAz, maxBz);
1361
+
1362
+ if (overlapMinY <= overlapMaxY && overlapMinZ <= overlapMaxZ) {
1363
+ std::int32_t aX = (normal.x > 0) ? (seamCoord - 1 - query.offsetA.x) : (seamCoord - query.offsetA.x);
1364
+ std::int32_t bX = (normal.x > 0) ? (seamCoord - query.offsetB.x) : (seamCoord - 1 - query.offsetB.x);
1365
+
1366
+ for (std::int32_t wz = overlapMinZ; wz <= overlapMaxZ; ++wz) {
1367
+ for (std::int32_t wy = overlapMinY; wy <= overlapMaxY; ++wy) {
1368
+ std::int32_t aY = wy - query.offsetA.y;
1369
+ std::int32_t aZ = wz - query.offsetA.z;
1370
+ std::int32_t bY = wy - query.offsetB.y;
1371
+ std::int32_t bZ = wz - query.offsetB.z;
1372
+
1373
+ bool solidA = a.isSolid(aX, aY, aZ);
1374
+ bool solidB = b.isSolid(bX, bY, bZ);
1375
+
1376
+ if (solidA && solidB) {
1377
+ sharedFaces++;
1378
+ centroidSum.x += static_cast<double>(seamCoord) * cellSize;
1379
+ centroidSum.y += (static_cast<double>(wy) + 0.5) * cellSize;
1380
+ centroidSum.z += (static_cast<double>(wz) + 0.5) * cellSize;
1381
+ if (wy < seamBounds.minY) seamBounds.minY = wy;
1382
+ if (wy > seamBounds.maxY) seamBounds.maxY = wy;
1383
+ if (wz < seamBounds.minZ) seamBounds.minZ = wz;
1384
+ if (wz > seamBounds.maxZ) seamBounds.maxZ = wz;
1385
+ } else if (solidA && !solidB) {
1386
+ exposedA++;
1387
+ } else if (!solidA && solidB) {
1388
+ exposedB++;
1389
+ }
1390
+ }
1391
+ }
1392
+ seamBounds.minX = seamCoord;
1393
+ seamBounds.maxX = seamCoord;
1394
+ }
1395
+ } else if (seamAxis == 1) {
1396
+ std::int32_t overlapMinX = std::max(minAx, minBx);
1397
+ std::int32_t overlapMaxX = std::min(maxAx, maxBx);
1398
+ std::int32_t overlapMinZ = std::max(minAz, minBz);
1399
+ std::int32_t overlapMaxZ = std::min(maxAz, maxBz);
1400
+
1401
+ if (overlapMinX <= overlapMaxX && overlapMinZ <= overlapMaxZ) {
1402
+ std::int32_t aY = (normal.y > 0) ? (seamCoord - 1 - query.offsetA.y) : (seamCoord - query.offsetA.y);
1403
+ std::int32_t bY = (normal.y > 0) ? (seamCoord - query.offsetB.y) : (seamCoord - 1 - query.offsetB.y);
1404
+
1405
+ for (std::int32_t wz = overlapMinZ; wz <= overlapMaxZ; ++wz) {
1406
+ for (std::int32_t wx = overlapMinX; wx <= overlapMaxX; ++wx) {
1407
+ std::int32_t aX = wx - query.offsetA.x;
1408
+ std::int32_t aZ = wz - query.offsetA.z;
1409
+ std::int32_t bX = wx - query.offsetB.x;
1410
+ std::int32_t bZ = wz - query.offsetB.z;
1411
+
1412
+ bool solidA = a.isSolid(aX, aY, aZ);
1413
+ bool solidB = b.isSolid(bX, bY, bZ);
1414
+
1415
+ if (solidA && solidB) {
1416
+ sharedFaces++;
1417
+ centroidSum.x += (static_cast<double>(wx) + 0.5) * cellSize;
1418
+ centroidSum.y += static_cast<double>(seamCoord) * cellSize;
1419
+ centroidSum.z += (static_cast<double>(wz) + 0.5) * cellSize;
1420
+ if (wx < seamBounds.minX) seamBounds.minX = wx;
1421
+ if (wx > seamBounds.maxX) seamBounds.maxX = wx;
1422
+ if (wz < seamBounds.minZ) seamBounds.minZ = wz;
1423
+ if (wz > seamBounds.maxZ) seamBounds.maxZ = wz;
1424
+ } else if (solidA && !solidB) {
1425
+ exposedA++;
1426
+ } else if (!solidA && solidB) {
1427
+ exposedB++;
1428
+ }
1429
+ }
1430
+ }
1431
+ seamBounds.minY = seamCoord;
1432
+ seamBounds.maxY = seamCoord;
1433
+ }
1434
+ } else {
1435
+ std::int32_t overlapMinX = std::max(minAx, minBx);
1436
+ std::int32_t overlapMaxX = std::min(maxAx, maxBx);
1437
+ std::int32_t overlapMinY = std::max(minAy, minBy);
1438
+ std::int32_t overlapMaxY = std::min(maxAy, maxBy);
1439
+
1440
+ if (overlapMinX <= overlapMaxX && overlapMinY <= overlapMaxY) {
1441
+ std::int32_t aZ = (normal.z > 0) ? (seamCoord - 1 - query.offsetA.z) : (seamCoord - query.offsetA.z);
1442
+ std::int32_t bZ = (normal.z > 0) ? (seamCoord - query.offsetB.z) : (seamCoord - 1 - query.offsetB.z);
1443
+
1444
+ for (std::int32_t wy = overlapMinY; wy <= overlapMaxY; ++wy) {
1445
+ for (std::int32_t wx = overlapMinX; wx <= overlapMaxX; ++wx) {
1446
+ std::int32_t aX = wx - query.offsetA.x;
1447
+ std::int32_t aY = wy - query.offsetA.y;
1448
+ std::int32_t bX = wx - query.offsetB.x;
1449
+ std::int32_t bY = wy - query.offsetB.y;
1450
+
1451
+ bool solidA = a.isSolid(aX, aY, aZ);
1452
+ bool solidB = b.isSolid(bX, bY, bZ);
1453
+
1454
+ if (solidA && solidB) {
1455
+ sharedFaces++;
1456
+ centroidSum.x += (static_cast<double>(wx) + 0.5) * cellSize;
1457
+ centroidSum.y += (static_cast<double>(wy) + 0.5) * cellSize;
1458
+ centroidSum.z += static_cast<double>(seamCoord) * cellSize;
1459
+ if (wx < seamBounds.minX) seamBounds.minX = wx;
1460
+ if (wx > seamBounds.maxX) seamBounds.maxX = wx;
1461
+ if (wy < seamBounds.minY) seamBounds.minY = wy;
1462
+ if (wy > seamBounds.maxY) seamBounds.maxY = wy;
1463
+ } else if (solidA && !solidB) {
1464
+ exposedA++;
1465
+ } else if (!solidA && solidB) {
1466
+ exposedB++;
1467
+ }
1468
+ }
1469
+ }
1470
+ seamBounds.minZ = seamCoord;
1471
+ seamBounds.maxZ = seamCoord;
1472
+ }
1473
+ }
1474
+
1475
+ outResult->hasSeam = (sharedFaces > 0);
1476
+ outResult->seamAxis = seamAxis;
1477
+ outResult->seamCoord = seamCoord;
1478
+ outResult->sharedFaceCount = sharedFaces;
1479
+ outResult->exposedFaceCountA = exposedA;
1480
+ outResult->exposedFaceCountB = exposedB;
1481
+ outResult->seamArea = static_cast<double>(sharedFaces) * cellSize * cellSize;
1482
+ outResult->seamNormal = normal;
1483
+ outResult->isWatertight = (sharedFaces > 0 && exposedA == 0 && exposedB == 0);
1484
+
1485
+ if (sharedFaces > 0) {
1486
+ double invCount = 1.0 / static_cast<double>(sharedFaces);
1487
+ outResult->seamCentroid = {
1488
+ (centroidSum.x * invCount) - cellSize * 0.5,
1489
+ (centroidSum.y * invCount) - cellSize * 0.5,
1490
+ (centroidSum.z * invCount) - cellSize * 0.5
1491
+ };
1492
+ outResult->seamBounds = seamBounds;
1493
+ } else {
1494
+ outResult->seamCentroid = {0.0, 0.0, 0.0};
1495
+ outResult->seamBounds = {0, 0, 0, 0, 0, 0};
1496
+ }
1497
+
1498
+ return VoxelStatus::Success;
1499
+ }
1500
+
1501
+ VoxelStatus computeVoxelMassProperties(const VoxelChunk& chunk, double totalMass,
1502
+ VoxelVec3 originOffset, VoxelMassProperties* output) {
1503
+ if (!output || !chunk.occupancy) return VoxelStatus::NullBuffer;
1504
+ if (chunk.dimX < 1 || chunk.dimY < 1 || chunk.dimZ < 1
1505
+ || chunk.dimX > kVoxelMaxDimension || chunk.dimY > kVoxelMaxDimension
1506
+ || chunk.dimZ > kVoxelMaxDimension) return VoxelStatus::InvalidDimensions;
1507
+ if (!std::isfinite(chunk.cellSize) || chunk.cellSize <= 0) return VoxelStatus::InvalidCellSize;
1508
+ if (!std::isfinite(totalMass) || totalMass <= 0 || !std::isfinite(originOffset.x)
1509
+ || !std::isfinite(originOffset.y) || !std::isfinite(originOffset.z)) return VoxelStatus::InvalidOperation;
1510
+
1511
+ std::array<std::int64_t, 10> sums{};
1512
+ for (std::int64_t z = 0; z < chunk.dimZ; ++z) {
1513
+ for (std::int64_t y = 0; y < chunk.dimY; ++y) {
1514
+ for (std::int64_t x = 0; x < chunk.dimX; ++x) {
1515
+ if (!chunk.isSolid(x, y, z)) continue;
1516
+ const std::array<std::int64_t, 10> terms{1, x, y, z, x*x, y*y, z*z, x*y, x*z, y*z};
1517
+ for (std::size_t axis = 0; axis < sums.size(); ++axis) sums[axis] += terms[axis];
1518
+ }
1519
+ }
1520
+ }
1521
+ const auto count = sums[0];
1522
+ if (count == 0) {
1523
+ *output = VoxelMassProperties{};
1524
+ return VoxelStatus::NoOccupiedVoxels;
1525
+ }
1526
+ using Wide = __int128_t;
1527
+ const Wide denominator = Wide(count) * count;
1528
+ const auto covariance = [&](unsigned a, unsigned b, unsigned second) {
1529
+ return Wide(count) * sums[second] - Wide(sums[a]) * sums[b];
1530
+ };
1531
+ const std::array<Wide, 6> central{
1532
+ covariance(2, 2, 5) + covariance(3, 3, 6),
1533
+ covariance(1, 1, 4) + covariance(3, 3, 6),
1534
+ covariance(1, 1, 4) + covariance(2, 2, 5),
1535
+ -covariance(1, 2, 7), -covariance(1, 3, 8), -covariance(2, 3, 9)};
1536
+ const double scale = totalMass * chunk.cellSize * chunk.cellSize;
1537
+ VoxelMassProperties staged;
1538
+ staged.occupiedCount = static_cast<std::uint32_t>(count);
1539
+ staged.mass = totalMass;
1540
+ staged.centerOfMass = {
1541
+ (double(sums[1]) / double(count)) * chunk.cellSize + originOffset.x,
1542
+ (double(sums[2]) / double(count)) * chunk.cellSize + originOffset.y,
1543
+ (double(sums[3]) / double(count)) * chunk.cellSize + originOffset.z};
1544
+ for (unsigned axis = 0; axis < 6; ++axis) {
1545
+ staged.inertia[axis] = scale * (double(central[axis]) / double(denominator)
1546
+ + (axis < 3 ? 1.0 / 6 : 0));
1547
+ }
1548
+ if (!std::isfinite(scale) || scale <= 0 || !std::isfinite(staged.centerOfMass.x)
1549
+ || !std::isfinite(staged.centerOfMass.y) || !std::isfinite(staged.centerOfMass.z)
1550
+ || !std::all_of(staged.inertia.begin(), staged.inertia.end(), [](double value) { return std::isfinite(value); })
1551
+ || staged.inertia[0] <= 0 || staged.inertia[1] <= 0 || staged.inertia[2] <= 0) return VoxelStatus::InvalidOperation;
1552
+ *output = staged;
1553
+ return VoxelStatus::Success;
1554
+ }
1555
+
1556
+ } // namespace rundot::kinetix::physics::voxel
1557
+
1558
+ extern "C" {
1559
+
1560
+ using namespace rundot::kinetix::physics::voxel;
1561
+
1562
+ std::uint32_t rundot_kinetix_ext_voxel_abi_version() {
1563
+ return kVoxelCookingAbiVersion;
1564
+ }
1565
+
1566
+ std::uint32_t rundot_kinetix_ext_voxel_child_stride() {
1567
+ return kVoxelBoxChildStride;
1568
+ }
1569
+
1570
+ std::uint32_t rundot_kinetix_ext_voxel_stats_stride() {
1571
+ return kVoxelCookStatsStride;
1572
+ }
1573
+
1574
+ std::uint32_t rundot_kinetix_ext_voxel_subchunk_stride() {
1575
+ return kVoxelSubChunkStride;
1576
+ }
1577
+
1578
+ std::uint32_t rundot_kinetix_ext_voxel_ray_hit_stride() {
1579
+ return kVoxelRaycastHitStride;
1580
+ }
1581
+
1582
+ std::uint32_t rundot_kinetix_ext_voxel_seam_result_stride() {
1583
+ return kVoxelSeamResultStride;
1584
+ }
1585
+
1586
+ std::uint32_t rundot_kinetix_ext_voxel_mass_properties_stride() {
1587
+ return kVoxelMassPropertiesStride;
1588
+ }
1589
+
1590
+ std::int32_t rundot_kinetix_ext_voxel_mass_properties(
1591
+ std::int32_t dimX, std::int32_t dimY, std::int32_t dimZ, double cellSize,
1592
+ const std::uint8_t* occupancy, double totalMass,
1593
+ double offsetX, double offsetY, double offsetZ, double* output) {
1594
+ if (!output) return static_cast<std::int32_t>(VoxelStatus::NullBuffer);
1595
+ const VoxelChunk chunk{dimX, dimY, dimZ, cellSize, occupancy};
1596
+ VoxelMassProperties value;
1597
+ const auto status = computeVoxelMassProperties(chunk, totalMass, {offsetX, offsetY, offsetZ}, &value);
1598
+ if (status != VoxelStatus::Success && status != VoxelStatus::NoOccupiedVoxels) return static_cast<std::int32_t>(status);
1599
+ output[0] = value.occupiedCount;
1600
+ output[1] = value.mass;
1601
+ output[2] = value.centerOfMass.x;
1602
+ output[3] = value.centerOfMass.y;
1603
+ output[4] = value.centerOfMass.z;
1604
+ for (unsigned axis = 0; axis < 6; ++axis) output[5 + axis] = value.inertia[axis];
1605
+ return static_cast<std::int32_t>(status);
1606
+ }
1607
+
1608
+ std::uint32_t rundot_kinetix_ext_voxel_scratch_size(std::int32_t dimX, std::int32_t dimY, std::int32_t dimZ) {
1609
+ if (dimX < 1 || dimY < 1 || dimZ < 1 || dimX > kVoxelMaxDimension
1610
+ || dimY > kVoxelMaxDimension || dimZ > kVoxelMaxDimension) return 0;
1611
+ const std::uint64_t bytes = static_cast<std::uint64_t>(dimX) * dimY * dimZ * 5;
1612
+ if (bytes > UINT32_MAX) return 0;
1613
+ return static_cast<std::uint32_t>(bytes);
1614
+ }
1615
+
1616
+ std::int32_t rundot_kinetix_ext_voxel_cook_chunk(
1617
+ std::int32_t dimX,
1618
+ std::int32_t dimY,
1619
+ std::int32_t dimZ,
1620
+ double cellSize,
1621
+ const std::uint8_t* occupancy,
1622
+ double* outChildren,
1623
+ std::uint32_t maxChildren,
1624
+ std::uint32_t* outChildCount,
1625
+ std::uint32_t* inOutNextChildId,
1626
+ std::uint8_t* scratch) {
1627
+ if (!outChildren || !outChildCount) return static_cast<std::int32_t>(VoxelStatus::NullBuffer);
1628
+
1629
+ VoxelChunk chunk;
1630
+ chunk.dimX = dimX;
1631
+ chunk.dimY = dimY;
1632
+ chunk.dimZ = dimZ;
1633
+ chunk.cellSize = cellSize;
1634
+ chunk.occupancy = occupancy;
1635
+
1636
+ std::vector<VoxelBoxChild> children(maxChildren);
1637
+ std::uint32_t childCount = 0;
1638
+
1639
+ VoxelStatus status = cookVoxelChunk(
1640
+ chunk, children.data(), maxChildren, &childCount, inOutNextChildId, scratch);
1641
+
1642
+ for (std::uint32_t i = 0; i < childCount; ++i) {
1643
+ packBoxChild(children[i], outChildren + i * kVoxelBoxChildStride);
1644
+ }
1645
+ *outChildCount = childCount;
1646
+
1647
+ return static_cast<std::int32_t>(status);
1648
+ }
1649
+
1650
+ std::int32_t rundot_kinetix_ext_voxel_carve(
1651
+ std::int32_t dimX,
1652
+ std::int32_t dimY,
1653
+ std::int32_t dimZ,
1654
+ double cellSize,
1655
+ std::uint8_t* occupancy,
1656
+ const double* carveParams,
1657
+ std::int32_t* outDirtyAABB,
1658
+ std::int32_t* outChanged) {
1659
+ if (!carveParams || !occupancy) return static_cast<std::int32_t>(VoxelStatus::NullBuffer);
1660
+
1661
+ VoxelCarveRegion region;
1662
+ region.shapeType = static_cast<VoxelCarveShapeType>(carveParams[0]);
1663
+ region.op = static_cast<VoxelCarveOp>(carveParams[1]);
1664
+ region.box.minX = static_cast<std::int32_t>(carveParams[2]);
1665
+ region.box.minY = static_cast<std::int32_t>(carveParams[3]);
1666
+ region.box.minZ = static_cast<std::int32_t>(carveParams[4]);
1667
+ region.box.maxX = static_cast<std::int32_t>(carveParams[5]);
1668
+ region.box.maxY = static_cast<std::int32_t>(carveParams[6]);
1669
+ region.box.maxZ = static_cast<std::int32_t>(carveParams[7]);
1670
+ region.sphereCenter = {carveParams[8], carveParams[9], carveParams[10]};
1671
+ region.sphereRadius = carveParams[11];
1672
+ region.capsuleP0 = {carveParams[12], carveParams[13], carveParams[14]};
1673
+ region.capsuleP1 = {carveParams[15], carveParams[16], carveParams[17]};
1674
+ region.capsuleRadius = carveParams[18];
1675
+
1676
+ VoxelCellAABB dirtyAABB;
1677
+ bool changed = false;
1678
+
1679
+ VoxelStatus status = carveVoxelChunk(
1680
+ dimX, dimY, dimZ, cellSize, occupancy, region, &dirtyAABB, &changed);
1681
+
1682
+ if (outDirtyAABB) {
1683
+ outDirtyAABB[0] = dirtyAABB.minX;
1684
+ outDirtyAABB[1] = dirtyAABB.minY;
1685
+ outDirtyAABB[2] = dirtyAABB.minZ;
1686
+ outDirtyAABB[3] = dirtyAABB.maxX;
1687
+ outDirtyAABB[4] = dirtyAABB.maxY;
1688
+ outDirtyAABB[5] = dirtyAABB.maxZ;
1689
+ }
1690
+ if (outChanged) {
1691
+ *outChanged = changed ? 1 : 0;
1692
+ }
1693
+
1694
+ return static_cast<std::int32_t>(status);
1695
+ }
1696
+
1697
+ std::int32_t rundot_kinetix_ext_voxel_incremental_cook(
1698
+ std::int32_t dimX,
1699
+ std::int32_t dimY,
1700
+ std::int32_t dimZ,
1701
+ double cellSize,
1702
+ const std::uint8_t* occupancy,
1703
+ const double* existingChildren,
1704
+ std::uint32_t existingCount,
1705
+ const std::int32_t* dirtyAABB,
1706
+ double* outChildren,
1707
+ std::uint32_t maxOutChildren,
1708
+ std::uint32_t* outChildCount,
1709
+ std::uint32_t* inOutNextChildId,
1710
+ double* outStats,
1711
+ std::uint8_t* scratch) {
1712
+ if (!occupancy || !existingChildren || !dirtyAABB || !outChildren || !outChildCount) {
1713
+ return static_cast<std::int32_t>(VoxelStatus::NullBuffer);
1714
+ }
1715
+
1716
+ VoxelChunk chunk;
1717
+ chunk.dimX = dimX;
1718
+ chunk.dimY = dimY;
1719
+ chunk.dimZ = dimZ;
1720
+ chunk.cellSize = cellSize;
1721
+ chunk.occupancy = occupancy;
1722
+
1723
+ std::vector<VoxelBoxChild> existing(existingCount);
1724
+ for (std::uint32_t i = 0; i < existingCount; ++i) {
1725
+ existing[i] = unpackBoxChild(existingChildren + i * kVoxelBoxChildStride);
1726
+ }
1727
+
1728
+ VoxelCellAABB dirty = {dirtyAABB[0], dirtyAABB[1], dirtyAABB[2], dirtyAABB[3], dirtyAABB[4], dirtyAABB[5]};
1729
+
1730
+ std::vector<VoxelBoxChild> resultChildren(maxOutChildren);
1731
+ std::uint32_t resultCount = 0;
1732
+ VoxelCookStats stats;
1733
+
1734
+ VoxelStatus status = incrementalCookVoxel(
1735
+ chunk,
1736
+ existing.data(),
1737
+ existingCount,
1738
+ dirty,
1739
+ resultChildren.data(),
1740
+ maxOutChildren,
1741
+ &resultCount,
1742
+ inOutNextChildId,
1743
+ outStats ? &stats : nullptr,
1744
+ scratch);
1745
+
1746
+ for (std::uint32_t i = 0; i < resultCount; ++i) {
1747
+ packBoxChild(resultChildren[i], outChildren + i * kVoxelBoxChildStride);
1748
+ }
1749
+ *outChildCount = resultCount;
1750
+
1751
+ if (outStats) {
1752
+ packCookStats(stats, outStats);
1753
+ }
1754
+
1755
+ return static_cast<std::int32_t>(status);
1756
+ }
1757
+
1758
+ std::int32_t rundot_kinetix_ext_voxel_split_components(
1759
+ std::int32_t dimX,
1760
+ std::int32_t dimY,
1761
+ std::int32_t dimZ,
1762
+ double cellSize,
1763
+ const std::uint8_t* occupancy,
1764
+ std::uint32_t anchorFacesMask,
1765
+ double* outSubChunks,
1766
+ std::uint32_t maxSubChunks,
1767
+ std::uint32_t* outSubChunkCount,
1768
+ std::uint8_t* outSubOccupancyBuffer,
1769
+ std::uint32_t maxSubOccupancyBytes,
1770
+ std::uint32_t* outSubOccupancyBytesUsed,
1771
+ std::uint8_t* scratch) {
1772
+ if (!occupancy || !outSubChunks || !outSubChunkCount) {
1773
+ return static_cast<std::int32_t>(VoxelStatus::NullBuffer);
1774
+ }
1775
+
1776
+ VoxelChunk chunk;
1777
+ chunk.dimX = dimX;
1778
+ chunk.dimY = dimY;
1779
+ chunk.dimZ = dimZ;
1780
+ chunk.cellSize = cellSize;
1781
+ chunk.occupancy = occupancy;
1782
+
1783
+ std::vector<VoxelSubChunk> subChunks(maxSubChunks);
1784
+ std::uint32_t subChunkCount = 0;
1785
+
1786
+ VoxelStatus status = splitVoxelComponents(
1787
+ chunk,
1788
+ anchorFacesMask,
1789
+ subChunks.data(),
1790
+ maxSubChunks,
1791
+ &subChunkCount,
1792
+ outSubOccupancyBuffer,
1793
+ maxSubOccupancyBytes,
1794
+ outSubOccupancyBytesUsed,
1795
+ scratch);
1796
+
1797
+ for (std::uint32_t i = 0; i < subChunkCount; ++i) {
1798
+ packSubChunk(subChunks[i], outSubChunks + i * kVoxelSubChunkStride);
1799
+ }
1800
+ *outSubChunkCount = subChunkCount;
1801
+
1802
+ return static_cast<std::int32_t>(status);
1803
+ }
1804
+
1805
+ std::int32_t rundot_kinetix_ext_voxel_incremental_slice(
1806
+ const double* parentChildren,
1807
+ std::uint32_t parentChildCount,
1808
+ double cellSize,
1809
+ const double* subChunks,
1810
+ std::uint32_t subChunkCount,
1811
+ const std::uint8_t* subOccupancies,
1812
+ double* outSubChildren,
1813
+ std::uint32_t maxOutSubChildren,
1814
+ std::uint32_t* outSubChildCounts,
1815
+ std::uint32_t* inOutNextChildId,
1816
+ std::uint32_t* outPreservedTotal,
1817
+ std::uint32_t* outRecookedTotal,
1818
+ std::uint8_t* scratch) {
1819
+ if (!parentChildren || !subChunks || !subOccupancies || !outSubChildren || !outSubChildCounts) {
1820
+ return static_cast<std::int32_t>(VoxelStatus::NullBuffer);
1821
+ }
1822
+
1823
+ std::vector<VoxelBoxChild> pChildren(parentChildCount);
1824
+ for (std::uint32_t i = 0; i < parentChildCount; ++i) {
1825
+ pChildren[i] = unpackBoxChild(parentChildren + i * kVoxelBoxChildStride);
1826
+ }
1827
+
1828
+ std::vector<VoxelSubChunk> scList(subChunkCount);
1829
+ for (std::uint32_t i = 0; i < subChunkCount; ++i) {
1830
+ scList[i] = unpackSubChunk(subChunks + i * kVoxelSubChunkStride);
1831
+ }
1832
+
1833
+ std::vector<VoxelBoxChild> outBoxes(maxOutSubChildren);
1834
+
1835
+ VoxelStatus status = incrementalSliceCompound(
1836
+ pChildren.data(),
1837
+ parentChildCount,
1838
+ cellSize,
1839
+ scList.data(),
1840
+ subChunkCount,
1841
+ subOccupancies,
1842
+ outBoxes.data(),
1843
+ maxOutSubChildren,
1844
+ outSubChildCounts,
1845
+ inOutNextChildId,
1846
+ outPreservedTotal,
1847
+ outRecookedTotal,
1848
+ scratch);
1849
+
1850
+ std::uint32_t totalBoxes = 0;
1851
+ for (std::uint32_t s = 0; s < subChunkCount; ++s) {
1852
+ totalBoxes += outSubChildCounts[s];
1853
+ }
1854
+ for (std::uint32_t i = 0; i < totalBoxes; ++i) {
1855
+ packBoxChild(outBoxes[i], outSubChildren + i * kVoxelBoxChildStride);
1856
+ }
1857
+
1858
+ return static_cast<std::int32_t>(status);
1859
+ }
1860
+
1861
+ std::int32_t rundot_kinetix_ext_voxel_raycast_dda(
1862
+ std::int32_t dimX,
1863
+ std::int32_t dimY,
1864
+ std::int32_t dimZ,
1865
+ double cellSize,
1866
+ const std::uint8_t* occupancy,
1867
+ const double* rayParams,
1868
+ double* outHit) {
1869
+ if (!occupancy || !rayParams || !outHit) {
1870
+ return static_cast<std::int32_t>(VoxelStatus::NullBuffer);
1871
+ }
1872
+
1873
+ VoxelChunk chunk;
1874
+ chunk.dimX = dimX;
1875
+ chunk.dimY = dimY;
1876
+ chunk.dimZ = dimZ;
1877
+ chunk.cellSize = cellSize;
1878
+ chunk.occupancy = occupancy;
1879
+
1880
+ VoxelRaycastQuery query;
1881
+ query.origin = {rayParams[0], rayParams[1], rayParams[2]};
1882
+ query.direction = {rayParams[3], rayParams[4], rayParams[5]};
1883
+ query.maxDistance = rayParams[6];
1884
+
1885
+ VoxelRaycastHit hit;
1886
+ VoxelStatus status = raycastVoxelDDA(chunk, query, &hit);
1887
+
1888
+ packRaycastHit(hit, outHit);
1889
+
1890
+ return static_cast<std::int32_t>(status);
1891
+ }
1892
+
1893
+ std::int32_t rundot_kinetix_ext_voxel_seam_query(
1894
+ std::int32_t dimAX,
1895
+ std::int32_t dimAY,
1896
+ std::int32_t dimAZ,
1897
+ double cellA,
1898
+ const std::uint8_t* occA,
1899
+ const std::int32_t* offA,
1900
+ std::int32_t dimBX,
1901
+ std::int32_t dimBY,
1902
+ std::int32_t dimBZ,
1903
+ double cellB,
1904
+ const std::uint8_t* occB,
1905
+ const std::int32_t* offB,
1906
+ std::int32_t cutAxis,
1907
+ std::int32_t cutCoord,
1908
+ double* outResult) {
1909
+ if (!occA || !offA || !occB || !offB || !outResult) {
1910
+ return static_cast<std::int32_t>(VoxelStatus::NullBuffer);
1911
+ }
1912
+
1913
+ VoxelSeamQuery query;
1914
+ query.chunkA = {dimAX, dimAY, dimAZ, cellA, occA};
1915
+ query.offsetA = {offA[0], offA[1], offA[2]};
1916
+ query.chunkB = {dimBX, dimBY, dimBZ, cellB, occB};
1917
+ query.offsetB = {offB[0], offB[1], offB[2]};
1918
+ query.cutPlaneAxis = cutAxis;
1919
+ query.cutCoordinate = cutCoord;
1920
+
1921
+ VoxelSeamResult result;
1922
+ VoxelStatus status = queryVoxelSeam(query, &result);
1923
+
1924
+ packSeamResult(result, outResult);
1925
+
1926
+ return static_cast<std::int32_t>(status);
1927
+ }
1928
+
1929
+ } // extern "C"