@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,771 @@
1
+ export const contourAbi = Object.freeze({
2
+ headerTag: 0x434f4e54, // "CONT"
3
+ headerBytes: 32,
4
+ edgeBytes: 72,
5
+ maxLoops: 32,
6
+ maxEdges: 256,
7
+ maxCoordinate: 1_000_000,
8
+ });
9
+
10
+ export const arcQuadrants = Object.freeze(new Set(['ne', 'nw', 'sw', 'se']));
11
+
12
+ function fail(detail) {
13
+ throw new Error(`KINETIX_PHYSICS2D_${detail}`);
14
+ }
15
+
16
+ function finiteNumber(value, fallback, name, prefix = 'CONTOUR_INVALID') {
17
+ const result = value === undefined ? fallback : value;
18
+ if (typeof result !== 'number' || !Number.isFinite(result)) {
19
+ fail(`${prefix}: ${name} must be finite`);
20
+ }
21
+ return result;
22
+ }
23
+
24
+ export function deriveQuadrantEndpoints(center, radius, quadrant) {
25
+ const cx = center.x;
26
+ const cy = center.y;
27
+ const R = radius;
28
+ switch (quadrant) {
29
+ case 'ne':
30
+ return {
31
+ A: Object.freeze({ x: cx + R, y: cy }),
32
+ B: Object.freeze({ x: cx, y: cy + R }),
33
+ };
34
+ case 'nw':
35
+ return {
36
+ A: Object.freeze({ x: cx, y: cy + R }),
37
+ B: Object.freeze({ x: cx - R, y: cy }),
38
+ };
39
+ case 'sw':
40
+ return {
41
+ A: Object.freeze({ x: cx - R, y: cy }),
42
+ B: Object.freeze({ x: cx, y: cy - R }),
43
+ };
44
+ case 'se':
45
+ return {
46
+ A: Object.freeze({ x: cx, y: cy - R }),
47
+ B: Object.freeze({ x: cx + R, y: cy }),
48
+ };
49
+ default:
50
+ fail('ARC_INVALID: quadrant must be ne, nw, sw, or se');
51
+ }
52
+ }
53
+
54
+ function normalizeVertex(vertex, label, prefix = 'CONTOUR_INVALID') {
55
+ if (!vertex || typeof vertex !== 'object' || Array.isArray(vertex)) {
56
+ fail(`${prefix}: ${label} is invalid`);
57
+ }
58
+ const x = finiteNumber(vertex.x, undefined, `${label}.x`, prefix);
59
+ const y = finiteNumber(vertex.y, undefined, `${label}.y`, prefix);
60
+ if (Math.abs(x) > contourAbi.maxCoordinate || Math.abs(y) > contourAbi.maxCoordinate) {
61
+ fail(`${prefix}: ${label} exceeds the coordinate limit`);
62
+ }
63
+ return Object.freeze({ x, y });
64
+ }
65
+
66
+ export function normalizeArcShape(shape) {
67
+ if (!shape || typeof shape !== 'object') {
68
+ fail('ARC_INVALID: supply an arc object');
69
+ }
70
+ if (!arcQuadrants.has(shape.quadrant)) {
71
+ fail('ARC_INVALID: quadrant must be ne, nw, sw, or se');
72
+ }
73
+ if (typeof shape.convex !== 'boolean') {
74
+ fail('ARC_INVALID: convex must be a boolean');
75
+ }
76
+ const radius = finiteNumber(shape.radius, undefined, 'radius', 'ARC_INVALID');
77
+ if (radius <= 0) {
78
+ fail('ARC_INVALID: radius must be positive');
79
+ }
80
+ if (radius > contourAbi.maxCoordinate) {
81
+ fail('ARC_INVALID: radius exceeds the coordinate limit');
82
+ }
83
+ const center = normalizeVertex(shape.center, 'center', 'ARC_INVALID');
84
+ const { A, B } = deriveQuadrantEndpoints(center, radius, shape.quadrant);
85
+ const start = shape.convex ? A : B;
86
+ const loop = {
87
+ start,
88
+ edges: [
89
+ {
90
+ kind: 'arc',
91
+ center,
92
+ radius,
93
+ quadrant: shape.quadrant,
94
+ },
95
+ ],
96
+ closed: false,
97
+ };
98
+ return normalizeContourShape({
99
+ type: 'contour',
100
+ loops: [loop],
101
+ fill: 'boundary',
102
+ radius: 0,
103
+ });
104
+ }
105
+
106
+ // Check if point P (on circle C, R) lies within the quadrant Q
107
+ function pointInQuadrant(P, C, quadrant) {
108
+ const eps = 1e-9;
109
+ switch (quadrant) {
110
+ case 'ne':
111
+ return P.x >= C.x - eps && P.y >= C.y - eps;
112
+ case 'nw':
113
+ return P.x <= C.x + eps && P.y >= C.y - eps;
114
+ case 'sw':
115
+ return P.x <= C.x + eps && P.y <= C.y + eps;
116
+ case 'se':
117
+ return P.x >= C.x - eps && P.y <= C.y + eps;
118
+ default:
119
+ return false;
120
+ }
121
+ }
122
+
123
+ function pointsEqual(p1, p2, eps = 1e-9) {
124
+ return Math.abs(p1.x - p2.x) <= eps && Math.abs(p1.y - p2.y) <= eps;
125
+ }
126
+
127
+ function lineIntersectsLine(p1, p2, p3, p4, isAdjacent) {
128
+ const d1x = p2.x - p1.x;
129
+ const d1y = p2.y - p1.y;
130
+ const d2x = p4.x - p3.x;
131
+ const d2y = p4.y - p3.y;
132
+ const denom = d1x * d2y - d1y * d2x;
133
+
134
+ if (Math.abs(denom) < 1e-12) {
135
+ // Parallel or collinear
136
+ const cross = (p3.x - p1.x) * d1y - (p3.y - p1.y) * d1x;
137
+ if (Math.abs(cross) > 1e-9) return false; // Parallel, separated
138
+
139
+ // Collinear: project onto segment 1
140
+ const len1sq = d1x * d1x + d1y * d1y;
141
+ const t3 = ((p3.x - p1.x) * d1x + (p3.y - p1.y) * d1y) / len1sq;
142
+ const t4 = ((p4.x - p1.x) * d1x + (p4.y - p1.y) * d1y) / len1sq;
143
+ const minT = Math.min(t3, t4);
144
+ const maxT = Math.max(t3, t4);
145
+
146
+ if (isAdjacent) {
147
+ // Adjacent collinear edges overlap if they overlap by more than an endpoint
148
+ return maxT > 1e-9 && minT < 1.0 - 1e-9;
149
+ }
150
+ return maxT >= -1e-9 && minT <= 1.0 + 1e-9;
151
+ }
152
+
153
+ const t = ((p3.x - p1.x) * d2y - (p3.y - p1.y) * d2x) / denom;
154
+ const u = ((p3.x - p1.x) * d1y - (p3.y - p1.y) * d1x) / denom;
155
+
156
+ if (t < -1e-9 || t > 1.0 + 1e-9 || u < -1e-9 || u > 1.0 + 1e-9) {
157
+ return false;
158
+ }
159
+
160
+ if (isAdjacent) {
161
+ // If adjacent, intersection strictly at the shared endpoint is allowed
162
+ const atT0 = Math.abs(t) <= 1e-9;
163
+ const atT1 = Math.abs(t - 1.0) <= 1e-9;
164
+ const atU0 = Math.abs(u) <= 1e-9;
165
+ const atU1 = Math.abs(u - 1.0) <= 1e-9;
166
+ if ((atT0 || atT1) && (atU0 || atU1)) {
167
+ return false; // Shared endpoint
168
+ }
169
+ }
170
+
171
+ return true;
172
+ }
173
+
174
+ function lineIntersectsArc(p1, p2, arc, isAdjacent) {
175
+ const dx = p2.x - p1.x;
176
+ const dy = p2.y - p1.y;
177
+ const lenSq = dx * dx + dy * dy;
178
+ if (lenSq <= 1e-14) return false;
179
+
180
+ const vx = p1.x - arc.center.x;
181
+ const vy = p1.y - arc.center.y;
182
+ const a = lenSq;
183
+ const b = 2 * (vx * dx + vy * dy);
184
+ const c = vx * vx + vy * vy - arc.radius * arc.radius;
185
+ const discr = b * b - 4 * a * c;
186
+
187
+ if (discr < -1e-12) return false;
188
+ const sqrtDiscr = Math.sqrt(Math.max(0, discr));
189
+ const tVals = [(-b - sqrtDiscr) / (2 * a), (-b + sqrtDiscr) / (2 * a)];
190
+
191
+ for (const t of tVals) {
192
+ if (t >= -1e-9 && t <= 1.0 + 1e-9) {
193
+ const clampedT = Math.max(0, Math.min(1, t));
194
+ const pt = { x: p1.x + clampedT * dx, y: p1.y + clampedT * dy };
195
+ if (pointInQuadrant(pt, arc.center, arc.quadrant)) {
196
+ if (isAdjacent) {
197
+ const atLineEnd = pointsEqual(pt, p1) || pointsEqual(pt, p2);
198
+ const atArcEnd = pointsEqual(pt, arc.from) || pointsEqual(pt, arc.to);
199
+ if (atLineEnd && atArcEnd) {
200
+ continue; // Allowed shared endpoint
201
+ }
202
+ }
203
+ return true;
204
+ }
205
+ }
206
+ }
207
+ return false;
208
+ }
209
+
210
+ function arcIntersectsArc(arc1, arc2, isAdjacent) {
211
+ const dx = arc2.center.x - arc1.center.x;
212
+ const dy = arc2.center.y - arc1.center.y;
213
+ const distSq = dx * dx + dy * dy;
214
+ const dist = Math.sqrt(distSq);
215
+
216
+ if (dist < 1e-9) {
217
+ // Concentric
218
+ if (Math.abs(arc1.radius - arc2.radius) < 1e-9) {
219
+ if (arc1.quadrant === arc2.quadrant) return true; // Overlap
220
+ if (isAdjacent) return false; // Adjacent quadrants sharing endpoint is ok
221
+ // If not adjacent but same circle, check endpoint sharing
222
+ const shareFromTo = pointsEqual(arc1.from, arc2.from) || pointsEqual(arc1.from, arc2.to)
223
+ || pointsEqual(arc1.to, arc2.from) || pointsEqual(arc1.to, arc2.to);
224
+ return shareFromTo;
225
+ }
226
+ return false;
227
+ }
228
+
229
+ const r1 = arc1.radius;
230
+ const r2 = arc2.radius;
231
+ if (dist > r1 + r2 + 1e-9 || dist < Math.abs(r1 - r2) - 1e-9) {
232
+ return false;
233
+ }
234
+
235
+ const a = (r1 * r1 - r2 * r2 + distSq) / (2 * dist);
236
+ const h = Math.sqrt(Math.max(0, r1 * r1 - a * a));
237
+ const p0x = arc1.center.x + (a * dx) / dist;
238
+ const p0y = arc1.center.y + (a * dy) / dist;
239
+
240
+ const pts = [
241
+ { x: p0x + (h * -dy) / dist, y: p0y + (h * dx) / dist },
242
+ { x: p0x - (h * -dy) / dist, y: p0y - (h * dx) / dist },
243
+ ];
244
+
245
+ for (const pt of pts) {
246
+ if (pointInQuadrant(pt, arc1.center, arc1.quadrant) && pointInQuadrant(pt, arc2.center, arc2.quadrant)) {
247
+ if (isAdjacent) {
248
+ const atArc1End = pointsEqual(pt, arc1.from) || pointsEqual(pt, arc1.to);
249
+ const atArc2End = pointsEqual(pt, arc2.from) || pointsEqual(pt, arc2.to);
250
+ if (atArc1End && atArc2End) {
251
+ continue; // Allowed shared endpoint
252
+ }
253
+ }
254
+ return true;
255
+ }
256
+ }
257
+ return false;
258
+ }
259
+
260
+ function edgesIntersect(edge1, edge2, isAdjacent) {
261
+ if (edge1.kind === 'line' && edge2.kind === 'line') {
262
+ return lineIntersectsLine(edge1.from, edge1.to, edge2.from, edge2.to, isAdjacent);
263
+ }
264
+ if (edge1.kind === 'line' && edge2.kind === 'arc') {
265
+ return lineIntersectsArc(edge1.from, edge1.to, edge2, isAdjacent);
266
+ }
267
+ if (edge1.kind === 'arc' && edge2.kind === 'line') {
268
+ return lineIntersectsArc(edge2.from, edge2.to, edge1, isAdjacent);
269
+ }
270
+ return arcIntersectsArc(edge1, edge2, isAdjacent);
271
+ }
272
+
273
+ // Signed area of a loop with line and arc edges
274
+ function loopSignedArea(loop) {
275
+ let area = 0;
276
+ for (const edge of loop.resolvedEdges) {
277
+ if (edge.kind === 'line') {
278
+ area += ((edge.from.x + edge.to.x) * (edge.to.y - edge.from.y)) / 2;
279
+ } else {
280
+ // Arc integral: cx*(y2 - y1) + R^2/2 * deltaTheta
281
+ const cx = edge.center.x;
282
+ const R = edge.radius;
283
+ const dy = edge.to.y - edge.from.y;
284
+ const deltaTheta = edge.isCcw ? Math.PI / 2 : -Math.PI / 2;
285
+ area += cx * dy + (R * R * deltaTheta) / 2;
286
+ }
287
+ }
288
+ return area;
289
+ }
290
+
291
+ // Point in polygon (winding number) for a closed loop with lines and arcs
292
+ function pointWindingNumber(P, loop) {
293
+ let winding = 0;
294
+ for (const edge of loop.resolvedEdges) {
295
+ if (edge.kind === 'line') {
296
+ const p1 = edge.from;
297
+ const p2 = edge.to;
298
+ if (p1.y <= P.y) {
299
+ if (p2.y > P.y) {
300
+ // Upward crossing
301
+ const cross = (p2.x - p1.x) * (P.y - p1.y) - (P.x - p1.x) * (p2.y - p1.y);
302
+ if (cross > 0) winding += 1;
303
+ }
304
+ } else if (p2.y <= P.y) {
305
+ // Downward crossing
306
+ const cross = (p2.x - p1.x) * (P.y - p1.y) - (P.x - p1.x) * (p2.y - p1.y);
307
+ if (cross < 0) winding -= 1;
308
+ }
309
+ } else {
310
+ // Approximate arc with 8 line segments for winding test
311
+ const cx = edge.center.x;
312
+ const cy = edge.center.y;
313
+ const R = edge.radius;
314
+ let startAngle = Math.atan2(edge.from.y - cy, edge.from.x - cx);
315
+ let endAngle = Math.atan2(edge.to.y - cy, edge.to.x - cx);
316
+ if (edge.isCcw && endAngle < startAngle) endAngle += 2 * Math.PI;
317
+ if (!edge.isCcw && endAngle > startAngle) endAngle -= 2 * Math.PI;
318
+ const steps = 8;
319
+ let prevPt = edge.from;
320
+ for (let s = 1; s <= steps; s += 1) {
321
+ const theta = startAngle + (endAngle - startAngle) * (s / steps);
322
+ const currPt = s === steps ? edge.to : { x: cx + R * Math.cos(theta), y: cy + R * Math.sin(theta) };
323
+ if (prevPt.y <= P.y) {
324
+ if (currPt.y > P.y) {
325
+ const cross = (currPt.x - prevPt.x) * (P.y - prevPt.y) - (P.x - prevPt.x) * (currPt.y - prevPt.y);
326
+ if (cross > 0) winding += 1;
327
+ }
328
+ } else if (currPt.y <= P.y) {
329
+ const cross = (currPt.x - prevPt.x) * (P.y - prevPt.y) - (P.x - prevPt.x) * (currPt.y - prevPt.y);
330
+ if (cross < 0) winding -= 1;
331
+ }
332
+ prevPt = currPt;
333
+ }
334
+ }
335
+ }
336
+ return winding;
337
+ }
338
+
339
+ export function normalizeContourShape(shape) {
340
+ if (!shape || typeof shape !== 'object') {
341
+ fail('CONTOUR_INVALID: supply a contour object');
342
+ }
343
+ const fill = shape.fill === undefined ? 'boundary' : shape.fill;
344
+ if (fill !== 'boundary' && fill !== 'solid') {
345
+ fail('CONTOUR_INVALID: fill must be boundary or solid');
346
+ }
347
+ const radius = finiteNumber(shape.radius ?? shape.skin, 0, 'radius');
348
+ if (radius < 0) {
349
+ fail('CONTOUR_INVALID: contour radius must be non-negative');
350
+ }
351
+
352
+ const rawLoops = shape.loops;
353
+ if (!Array.isArray(rawLoops) || rawLoops.length < 1 || rawLoops.length > contourAbi.maxLoops) {
354
+ fail('CONTOUR_INVALID: contour needs 1-32 loops and 1-256 edges');
355
+ }
356
+
357
+ const allEdges = [];
358
+ const normalizedLoops = [];
359
+ let globalEdgeIndex = 0;
360
+
361
+ for (let loopIdx = 0; loopIdx < rawLoops.length; loopIdx += 1) {
362
+ const L = loopIdx + 1;
363
+ const rawLoop = rawLoops[loopIdx];
364
+ if (!rawLoop || typeof rawLoop !== 'object') {
365
+ fail(`CONTOUR_INVALID: loop ${L} is invalid`);
366
+ }
367
+ const start = normalizeVertex(rawLoop.start, `loop ${L} start`);
368
+ const closed = rawLoop.closed === undefined ? false : rawLoop.closed;
369
+ if (typeof closed !== 'boolean') {
370
+ fail(`CONTOUR_INVALID: loop ${L} closed must be a boolean`);
371
+ }
372
+ let before;
373
+ let after;
374
+ if (closed) {
375
+ if (rawLoop.before !== undefined || rawLoop.after !== undefined) {
376
+ fail(`CONTOUR_INVALID: loop ${L} before and after are open-loop only`);
377
+ }
378
+ } else {
379
+ if (rawLoop.before !== undefined) {
380
+ before = normalizeVertex(rawLoop.before, `loop ${L} before`);
381
+ }
382
+ if (rawLoop.after !== undefined) {
383
+ after = normalizeVertex(rawLoop.after, `loop ${L} after`);
384
+ }
385
+ }
386
+
387
+ const rawEdges = rawLoop.edges;
388
+ if (!Array.isArray(rawEdges) || rawEdges.length < 1) {
389
+ fail(`CONTOUR_INVALID: loop ${L} has no edges`);
390
+ }
391
+ if (closed && rawEdges.length < 2) {
392
+ fail(`CONTOUR_INVALID: loop ${L} is not closed at start`);
393
+ }
394
+
395
+ let pen = start;
396
+ const resolvedEdges = [];
397
+ const loopStartGlobalIndex = globalEdgeIndex + 1;
398
+
399
+ for (let edgeIdx = 0; edgeIdx < rawEdges.length; edgeIdx += 1) {
400
+ globalEdgeIndex += 1;
401
+ const E = globalEdgeIndex;
402
+ if (globalEdgeIndex > contourAbi.maxEdges) {
403
+ fail('CONTOUR_INVALID: contour needs 1-32 loops and 1-256 edges');
404
+ }
405
+ const rawEdge = rawEdges[edgeIdx];
406
+ if (!rawEdge || typeof rawEdge !== 'object') {
407
+ fail(`CONTOUR_INVALID: loop ${L} edge ${E} is invalid`);
408
+ }
409
+ if (rawEdge.kind === 'line') {
410
+ const to = normalizeVertex(rawEdge.to, `loop ${L} edge ${E} to`);
411
+ const dx = to.x - pen.x;
412
+ const dy = to.y - pen.y;
413
+ if (dx * dx + dy * dy <= 1e-12) {
414
+ fail(`CONTOUR_INVALID: loop ${L} edge ${E} has zero length`);
415
+ }
416
+ const edge = Object.freeze({
417
+ kind: 'line',
418
+ from: pen,
419
+ to,
420
+ globalIndex: E,
421
+ loopIndex: L,
422
+ });
423
+ resolvedEdges.push(edge);
424
+ allEdges.push(edge);
425
+ pen = to;
426
+ } else if (rawEdge.kind === 'arc') {
427
+ if (!arcQuadrants.has(rawEdge.quadrant)) {
428
+ fail(`CONTOUR_INVALID: loop ${L} edge ${E} quadrant must be ne, nw, sw, or se`);
429
+ }
430
+ const edgeRadius = finiteNumber(rawEdge.radius, undefined, `loop ${L} edge ${E} radius`);
431
+ if (edgeRadius <= 0) {
432
+ fail(`CONTOUR_INVALID: loop ${L} edge ${E} arc radius must be positive`);
433
+ }
434
+ if (edgeRadius > contourAbi.maxCoordinate) {
435
+ fail(`CONTOUR_INVALID: loop ${L} edge ${E} arc radius exceeds the coordinate limit`);
436
+ }
437
+ const center = normalizeVertex(rawEdge.center, `loop ${L} edge ${E} center`);
438
+ const { A, B } = deriveQuadrantEndpoints(center, edgeRadius, rawEdge.quadrant);
439
+ let from;
440
+ let to;
441
+ let isCcw = false;
442
+ if (pen.x === A.x && pen.y === A.y) {
443
+ from = A;
444
+ to = B;
445
+ isCcw = true;
446
+ } else if (pen.x === B.x && pen.y === B.y) {
447
+ from = B;
448
+ to = A;
449
+ isCcw = false;
450
+ } else {
451
+ // Expected is either A or B; report A
452
+ fail(`CONTOUR_INVALID: loop ${L} edge ${E} arc does not start at the previous vertex; expected (${A.x}, ${A.y})`);
453
+ }
454
+ const edge = Object.freeze({
455
+ kind: 'arc',
456
+ quadrant: rawEdge.quadrant,
457
+ radius: edgeRadius,
458
+ center,
459
+ from,
460
+ to,
461
+ isCcw,
462
+ globalIndex: E,
463
+ loopIndex: L,
464
+ });
465
+ resolvedEdges.push(edge);
466
+ allEdges.push(edge);
467
+ pen = to;
468
+ } else {
469
+ fail(`CONTOUR_INVALID: loop ${L} edge ${E} kind must be line or arc`);
470
+ }
471
+ }
472
+
473
+ if (closed) {
474
+ if (pen.x !== start.x || pen.y !== start.y) {
475
+ fail(`CONTOUR_INVALID: loop ${L} is not closed at start`);
476
+ }
477
+ }
478
+
479
+ normalizedLoops.push(Object.freeze({
480
+ start,
481
+ edges: Object.freeze(rawEdges.map((e) => Object.freeze({ ...e }))),
482
+ closed,
483
+ before,
484
+ after,
485
+ resolvedEdges: Object.freeze(resolvedEdges),
486
+ loopIndex: L,
487
+ startGlobalIndex: loopStartGlobalIndex,
488
+ endGlobalIndex: globalEdgeIndex,
489
+ }));
490
+ }
491
+
492
+ if (allEdges.length < 1 || allEdges.length > contourAbi.maxEdges) {
493
+ fail('CONTOUR_INVALID: contour needs 1-32 loops and 1-256 edges');
494
+ }
495
+
496
+ // O(E^2) edge intersection check
497
+ for (let i = 0; i < allEdges.length; i += 1) {
498
+ const e1 = allEdges[i];
499
+ for (let j = i + 1; j < allEdges.length; j += 1) {
500
+ const e2 = allEdges[j];
501
+ const sameLoop = e1.loopIndex === e2.loopIndex;
502
+ const loop = normalizedLoops[e1.loopIndex - 1];
503
+ const isConsecutive = sameLoop && e2.globalIndex === e1.globalIndex + 1;
504
+ const isLoopWrap = sameLoop && loop.closed
505
+ && e1.globalIndex === loop.startGlobalIndex
506
+ && e2.globalIndex === loop.endGlobalIndex;
507
+ const isAdjacent = isConsecutive || isLoopWrap;
508
+
509
+ if (edgesIntersect(e1, e2, isAdjacent)) {
510
+ fail(`CONTOUR_INVALID: edges ${e1.globalIndex} and ${e2.globalIndex} intersect`);
511
+ }
512
+ }
513
+ }
514
+
515
+ // Solid fill validation
516
+ if (fill === 'solid') {
517
+ for (let loopIdx = 0; loopIdx < normalizedLoops.length; loopIdx += 1) {
518
+ const loop = normalizedLoops[loopIdx];
519
+ const L = loop.loopIndex;
520
+ if (!loop.closed) {
521
+ fail('CONTOUR_INVALID: solid contour loops must be closed');
522
+ }
523
+ const area = loopSignedArea(loop);
524
+ // Determine if loop is outer or hole
525
+ // A loop is a hole if its start is contained within an odd number of other loops
526
+ let enclosingCount = 0;
527
+ for (let oIdx = 0; oIdx < normalizedLoops.length; oIdx += 1) {
528
+ if (oIdx === loopIdx) continue;
529
+ const otherLoop = normalizedLoops[oIdx];
530
+ // Test interior point of loop against otherLoop
531
+ // Use midpoint of first edge nudged slightly normal to the edge
532
+ const firstEdge = loop.resolvedEdges[0];
533
+ let midX = (firstEdge.from.x + firstEdge.to.x) / 2;
534
+ let midY = (firstEdge.from.y + firstEdge.to.y) / 2;
535
+ if (firstEdge.kind === 'arc') {
536
+ const vx = (firstEdge.from.x - firstEdge.center.x) + (firstEdge.to.x - firstEdge.center.x);
537
+ const vy = (firstEdge.from.y - firstEdge.center.y) + (firstEdge.to.y - firstEdge.center.y);
538
+ const len = Math.hypot(vx, vy);
539
+ if (len > 1e-9) {
540
+ midX = firstEdge.center.x + (vx / len) * firstEdge.radius;
541
+ midY = firstEdge.center.y + (vy / len) * firstEdge.radius;
542
+ }
543
+ }
544
+ if (Math.abs(pointWindingNumber({ x: midX, y: midY }, otherLoop)) > 0) {
545
+ enclosingCount += 1;
546
+ }
547
+ }
548
+ const isHole = enclosingCount % 2 === 1;
549
+ if (!isHole && area < 0) {
550
+ fail(`CONTOUR_INVALID: loop ${L} winds clockwise; outer loops must be counter-clockwise`);
551
+ }
552
+ if (isHole && area > 0) {
553
+ fail(`CONTOUR_INVALID: loop ${L} winds counter-clockwise; holes must be clockwise`);
554
+ }
555
+ }
556
+ }
557
+
558
+ return Object.freeze({
559
+ type: 'contour',
560
+ loops: Object.freeze(normalizedLoops.map((l) => Object.freeze({
561
+ start: l.start,
562
+ edges: l.edges,
563
+ closed: l.closed,
564
+ ...(l.before !== undefined ? { before: l.before } : {}),
565
+ ...(l.after !== undefined ? { after: l.after } : {}),
566
+ }))),
567
+ fill,
568
+ radius,
569
+ skin: radius,
570
+ });
571
+ }
572
+
573
+ const quadrantIndices = Object.freeze({
574
+ ne: 0,
575
+ nw: 1,
576
+ sw: 2,
577
+ se: 3,
578
+ });
579
+ const quadrantNames = Object.freeze(["ne", "nw", "sw", "se"]);
580
+
581
+ export function writeContourHeader(view, offset, header) {
582
+ view.setUint32(offset, header.firstEdge, true);
583
+ view.setUint32(offset + 4, header.edgeCount, true);
584
+ view.setUint32(offset + 8, header.loopCount, true);
585
+ view.setUint32(offset + 12, header.fill === "solid" || header.fill === 1 ? 1 : 0, true);
586
+ view.setUint32(offset + 16, header.tag ?? contourAbi.headerTag, true);
587
+ view.setUint32(offset + 20, header.reserved ?? header.edgeCount, true);
588
+ view.setFloat64(offset + 24, header.skin ?? header.radius ?? 0, true);
589
+ }
590
+
591
+ export function serializeContourShape(shape, firstEdgeOffset = 0) {
592
+ const edges = [];
593
+ let authoredEdgeCount = 0;
594
+
595
+ for (let loopIdx = 0; loopIdx < shape.loops.length; loopIdx += 1) {
596
+ const loop = shape.loops[loopIdx];
597
+ const loopIndex = loopIdx + 1;
598
+ let loopFlags = 0;
599
+ if (loop.closed) loopFlags |= 1;
600
+ if (loop.before !== undefined) loopFlags |= 2;
601
+ if (loop.after !== undefined) loopFlags |= 4;
602
+
603
+ if (!loop.closed && loop.before !== undefined) {
604
+ edges.push({
605
+ kind: 2, // ghost
606
+ quadrant: 0,
607
+ from: loop.before,
608
+ to: loop.start,
609
+ center: { x: 0, y: 0 },
610
+ arcRadius: 0,
611
+ loopIndex,
612
+ flags: loopFlags,
613
+ });
614
+ }
615
+
616
+ let pen = loop.start;
617
+ for (let eIdx = 0; eIdx < loop.edges.length; eIdx += 1) {
618
+ const e = loop.edges[eIdx];
619
+ authoredEdgeCount += 1;
620
+ if (e.kind === "line") {
621
+ edges.push({
622
+ kind: 0, // line
623
+ quadrant: 0,
624
+ from: pen,
625
+ to: e.to,
626
+ center: { x: 0, y: 0 },
627
+ arcRadius: 0,
628
+ loopIndex,
629
+ flags: loopFlags,
630
+ });
631
+ pen = e.to;
632
+ } else if (e.kind === "arc") {
633
+ const { A, B } = deriveQuadrantEndpoints(e.center, e.radius, e.quadrant);
634
+ let from, to;
635
+ if (Math.abs(pen.x - A.x) <= 1e-9 && Math.abs(pen.y - A.y) <= 1e-9) {
636
+ from = A;
637
+ to = B;
638
+ } else {
639
+ from = B;
640
+ to = A;
641
+ }
642
+ edges.push({
643
+ kind: 1, // arc
644
+ quadrant: quadrantIndices[e.quadrant],
645
+ from,
646
+ to,
647
+ center: e.center,
648
+ arcRadius: e.radius,
649
+ loopIndex,
650
+ flags: loopFlags,
651
+ });
652
+ pen = to;
653
+ }
654
+ }
655
+
656
+ if (!loop.closed && loop.after !== undefined) {
657
+ edges.push({
658
+ kind: 2, // ghost
659
+ quadrant: 0,
660
+ from: pen,
661
+ to: loop.after,
662
+ center: { x: 0, y: 0 },
663
+ arcRadius: 0,
664
+ loopIndex,
665
+ flags: loopFlags,
666
+ });
667
+ }
668
+ }
669
+
670
+ const header = Object.freeze({
671
+ firstEdge: firstEdgeOffset,
672
+ edgeCount: authoredEdgeCount,
673
+ loopCount: shape.loops.length,
674
+ fill: shape.fill === "solid" ? 1 : 0,
675
+ tag: contourAbi.headerTag,
676
+ reserved: edges.length,
677
+ skin: shape.radius ?? 0,
678
+ });
679
+
680
+ return { header, edges };
681
+ }
682
+
683
+ export function writeContourEdge(view, offset, edge) {
684
+ view.setUint32(offset, edge.kind, true);
685
+ view.setUint32(offset + 4, edge.quadrant, true);
686
+ view.setFloat64(offset + 8, edge.from.x, true);
687
+ view.setFloat64(offset + 16, edge.from.y, true);
688
+ view.setFloat64(offset + 24, edge.to.x, true);
689
+ view.setFloat64(offset + 32, edge.to.y, true);
690
+ view.setFloat64(offset + 40, edge.center.x, true);
691
+ view.setFloat64(offset + 48, edge.center.y, true);
692
+ view.setFloat64(offset + 56, edge.arcRadius, true);
693
+ view.setUint32(offset + 64, edge.loopIndex, true);
694
+ view.setUint32(offset + 68, edge.flags, true);
695
+ }
696
+
697
+ export function readContourEdge(view, offset) {
698
+ const kind = view.getUint32(offset, true);
699
+ const quadrant = view.getUint32(offset + 4, true);
700
+ const from = Object.freeze({ x: view.getFloat64(offset + 8, true), y: view.getFloat64(offset + 16, true) });
701
+ const to = Object.freeze({ x: view.getFloat64(offset + 24, true), y: view.getFloat64(offset + 32, true) });
702
+ const center = Object.freeze({ x: view.getFloat64(offset + 40, true), y: view.getFloat64(offset + 48, true) });
703
+ const arcRadius = view.getFloat64(offset + 56, true);
704
+ const loopIndex = view.getUint32(offset + 64, true);
705
+ const flags = view.getUint32(offset + 68, true);
706
+ return { kind, quadrant, from, to, center, arcRadius, loopIndex, flags };
707
+ }
708
+
709
+ export function reconstructContourShape(header, rawEdges) {
710
+ const loops = [];
711
+ const edgesByLoop = new Map();
712
+ for (const edge of rawEdges) {
713
+ if (!edgesByLoop.has(edge.loopIndex)) {
714
+ edgesByLoop.set(edge.loopIndex, []);
715
+ }
716
+ edgesByLoop.get(edge.loopIndex).push(edge);
717
+ }
718
+
719
+ for (let loopIdx = 1; loopIdx <= header.loopCount; loopIdx += 1) {
720
+ const loopEdges = edgesByLoop.get(loopIdx) ?? [];
721
+ if (loopEdges.length === 0) continue;
722
+ let before;
723
+ let after;
724
+ let startIndex = 0;
725
+ let endIndex = loopEdges.length;
726
+
727
+ if (loopEdges[0].kind === 2) {
728
+ before = loopEdges[0].from;
729
+ startIndex = 1;
730
+ }
731
+ if (loopEdges.length > startIndex && loopEdges[loopEdges.length - 1].kind === 2) {
732
+ after = loopEdges[loopEdges.length - 1].to;
733
+ endIndex = loopEdges.length - 1;
734
+ }
735
+
736
+ const firstAuthored = loopEdges[startIndex];
737
+ const start = firstAuthored ? firstAuthored.from : { x: 0, y: 0 };
738
+ const closed = (firstAuthored ? (firstAuthored.flags & 1) !== 0 : false);
739
+
740
+ const edges = [];
741
+ for (let i = startIndex; i < endIndex; i += 1) {
742
+ const e = loopEdges[i];
743
+ if (e.kind === 0) {
744
+ edges.push(Object.freeze({ kind: "line", to: e.to }));
745
+ } else if (e.kind === 1) {
746
+ edges.push(Object.freeze({
747
+ kind: "arc",
748
+ center: e.center,
749
+ radius: e.arcRadius,
750
+ quadrant: quadrantNames[e.quadrant] ?? "ne",
751
+ }));
752
+ }
753
+ }
754
+
755
+ const loop = {
756
+ start,
757
+ edges: Object.freeze(edges),
758
+ closed,
759
+ ...(before !== undefined ? { before } : {}),
760
+ ...(after !== undefined ? { after } : {}),
761
+ };
762
+ loops.push(Object.freeze(loop));
763
+ }
764
+
765
+ return normalizeContourShape({
766
+ type: "contour",
767
+ loops,
768
+ fill: header.fill === 1 || header.fill === "solid" ? "solid" : "boundary",
769
+ radius: header.skin ?? header.radius ?? 0,
770
+ });
771
+ }