@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.
- package/README.md +133 -0
- package/core/deterministic-math.d.ts +75 -1
- package/core/native/instance_ledger.hpp +1095 -0
- package/core/native/legacy_checkpoint_ids.hpp +107 -0
- package/core/native/physics/abi.cpp +39 -211
- package/core/native/physics/abi.hpp +2 -6
- package/core/native/physics/articulation.cpp +1434 -0
- package/core/native/physics/articulation.hpp +279 -0
- package/core/native/physics/buoyancy.cpp +1588 -0
- package/core/native/physics/buoyancy.hpp +254 -0
- package/core/native/physics/capsule_triangle.cpp +168 -0
- package/core/native/physics/capsule_triangle.hpp +40 -0
- package/core/native/physics/capsule_triangle_native.inc +291 -0
- package/core/native/physics/certified_sum.hpp +218 -0
- package/core/native/physics/character.cpp +638 -0
- package/core/native/physics/character.hpp +169 -0
- package/core/native/physics/checkpoint_schema.hpp +379 -0
- package/core/native/physics/cloth.cpp +1401 -0
- package/core/native/physics/cloth.hpp +357 -0
- package/core/native/physics/constraint_rows.cpp +363 -2
- package/core/native/physics/constraint_rows.hpp +72 -1
- package/core/native/physics/deformable_query.cpp +1630 -0
- package/core/native/physics/deformable_query.hpp +578 -0
- package/core/native/physics/distance.cpp +1166 -0
- package/core/native/physics/distance.hpp +151 -0
- package/core/native/physics/exact/exact-applied.cpp +101 -0
- package/core/native/physics/exact/exact-applied.hpp +112 -0
- package/core/native/physics/exact/exact-dyadic.cpp +691 -0
- package/core/native/physics/exact/exact-dyadic.hpp +58 -0
- package/core/native/physics/exact/exact-operation.hpp +51 -0
- package/core/native/physics/exact/word-core.hpp +262 -0
- package/core/native/physics/extended_abi.cpp +1242 -0
- package/core/native/physics/extended_abi.hpp +355 -0
- package/core/native/physics/extended_abi_characters.cpp +261 -0
- package/core/native/physics/extended_abi_characters.hpp +434 -0
- package/core/native/physics/extended_abi_core_features.cpp +713 -0
- package/core/native/physics/extended_abi_core_features.hpp +226 -0
- package/core/native/physics/extended_abi_deformables.cpp +448 -0
- package/core/native/physics/extended_abi_deformables.hpp +867 -0
- package/core/native/physics/extended_abi_vehicles.cpp +411 -0
- package/core/native/physics/extended_abi_vehicles.hpp +222 -0
- package/core/native/physics/fracture.cpp +1981 -0
- package/core/native/physics/fracture.hpp +460 -0
- package/core/native/physics/gear.cpp +563 -0
- package/core/native/physics/gear.hpp +158 -0
- package/core/native/physics/geometry_memory.hpp +476 -0
- package/core/native/physics/geometry_store.cpp +547 -0
- package/core/native/physics/geometry_store.hpp +134 -0
- package/core/native/physics/lag_compensation.cpp +1626 -0
- package/core/native/physics/lag_compensation.hpp +405 -0
- package/core/native/physics/mass_properties.cpp +449 -0
- package/core/native/physics/mass_properties.hpp +23 -0
- package/core/native/physics/material_pose.hpp +988 -0
- package/core/native/physics/particles.cpp +1065 -0
- package/core/native/physics/particles.hpp +261 -0
- package/core/native/physics/path_joint.cpp +781 -0
- package/core/native/physics/path_joint.hpp +154 -0
- package/core/native/physics/physics.cpp +13043 -1065
- package/core/native/physics/physics.hpp +184 -8
- package/core/native/physics/planar_deformable.cpp +2433 -0
- package/core/native/physics/planar_deformable.hpp +889 -0
- package/core/native/physics/platformer_2d.cpp +1353 -0
- package/core/native/physics/platformer_2d.hpp +340 -0
- package/core/native/physics/powered_ragdoll.cpp +1100 -0
- package/core/native/physics/powered_ragdoll.hpp +279 -0
- package/core/native/physics/powertrain.cpp +586 -0
- package/core/native/physics/powertrain.hpp +180 -0
- package/core/native/physics/pulley.cpp +311 -0
- package/core/native/physics/pulley.hpp +121 -0
- package/core/native/physics/ragdoll.cpp +586 -0
- package/core/native/physics/ragdoll.hpp +221 -0
- package/core/native/physics/rope.cpp +692 -0
- package/core/native/physics/rope.hpp +215 -0
- package/core/native/physics/rounded_convex.cpp +1229 -0
- package/core/native/physics/rounded_convex.hpp +287 -0
- package/core/native/physics/sdf_geometry.cpp +1821 -0
- package/core/native/physics/sdf_geometry.hpp +474 -0
- package/core/native/physics/soft_body.cpp +1449 -0
- package/core/native/physics/soft_body.hpp +328 -0
- package/core/native/physics/soft_collision.cpp +1862 -0
- package/core/native/physics/soft_collision.hpp +561 -0
- package/core/native/physics/surface_velocity.cpp +366 -0
- package/core/native/physics/surface_velocity.hpp +243 -0
- package/core/native/physics/topology.cpp +357 -39
- package/core/native/physics/topology.hpp +97 -6
- package/core/native/physics/tracks.cpp +1179 -0
- package/core/native/physics/tracks.hpp +404 -0
- package/core/native/physics/vehicle.cpp +629 -0
- package/core/native/physics/vehicle.hpp +191 -0
- package/core/native/physics/voxel_cooking.cpp +1929 -0
- package/core/native/physics/voxel_cooking.hpp +395 -0
- package/core/native/physics/world.cpp +5290 -854
- package/core/native/physics/world.hpp +796 -24
- package/core/native/physics/world_abi.cpp +3840 -306
- package/core/native/physics/world_abi.hpp +530 -10
- package/core/native/physics/world_digest_visitor.hpp +277 -0
- package/core/native/physics/world_strong_digest.hpp +557 -0
- package/core/native/physics2d/ccd.cpp +1302 -0
- package/core/native/physics2d/ccd.hpp +141 -0
- package/core/native/physics2d/checkpoint_memory.hpp +349 -0
- package/core/native/physics2d/clock_primitives.hpp +138 -0
- package/core/native/physics2d/commands.cpp +174 -28
- package/core/native/physics2d/commands.hpp +3 -1
- package/core/native/physics2d/compound.cpp +581 -0
- package/core/native/physics2d/compound.hpp +248 -0
- package/core/native/physics2d/contour.cpp +325 -0
- package/core/native/physics2d/contour.hpp +143 -0
- package/core/native/physics2d/frame_scope.hpp +124 -0
- package/core/native/physics2d/joint_phases.hpp +93 -0
- package/core/native/physics2d/joints.cpp +587 -150
- package/core/native/physics2d/joints.hpp +5 -0
- package/core/native/physics2d/memory_private.hpp +125 -0
- package/core/native/physics2d/owned_abi.cpp +1354 -879
- package/core/native/physics2d/owned_abi.hpp +190 -52
- package/core/native/physics2d/phase_test_hooks.hpp +17 -0
- package/core/native/physics2d/physics2d.cpp +2073 -120
- package/core/native/physics2d/physics2d.hpp +154 -2
- package/core/native/physics2d/queries.cpp +1055 -79
- package/core/native/physics2d/queries.hpp +71 -5
- package/core/native/physics2d/world.cpp +1313 -353
- package/core/native/physics2d/world.hpp +124 -13
- package/core/native/sha256/sha256_compress_unsigned.hpp +68 -0
- package/core/native/sha256/sha256_fips_constants.hpp +24 -0
- package/core/native/sha256/sha256_length.hpp +56 -0
- package/core/native/sha256/sha256_memo.hpp +48 -0
- package/core/native/sha256/sha256_sink.hpp +234 -0
- package/core/physics2d-owned.d.ts +466 -126
- package/core/runtime.d.ts +192 -19
- package/core/scripts/build-native.mjs +34 -0
- package/core/scripts/build-wasm-physics.mjs +272 -23
- package/core/scripts/extended-physics-profile.mjs +634 -0
- package/core/scripts/generate-physics2d-vectors.mjs +59 -0
- package/core/src/kinetix-deterministic-math.d.ts +2 -0
- package/core/src/kinetix-deterministic-math.mjs +513 -27
- package/core/src/kinetix-owned-runtime-adapter.mjs +788 -0
- package/core/src/kinetix-runtime-adapter-utils.mjs +54 -6
- package/core/src/kinetix-runtime-contract.mjs +55 -9
- package/core/src/kinetix-runtime-identity.mjs +12 -1
- package/core/src/kinetix-session-config.mjs +7 -2
- package/core/src/kinetix-state-report.mjs +36 -0
- package/core/src/physics/abi-constants.mjs +5 -4
- package/core/src/physics/articulation.d.mts +194 -0
- package/core/src/physics/articulation.mjs +1141 -0
- package/core/src/physics/buoyancy.d.mts +143 -0
- package/core/src/physics/buoyancy.mjs +268 -0
- package/core/src/physics/cloth.d.mts +314 -0
- package/core/src/physics/cloth.mjs +1156 -0
- package/core/src/physics/deformable-query.d.mts +217 -0
- package/core/src/physics/deformable-query.mjs +1412 -0
- package/core/src/physics/distance.d.mts +160 -0
- package/core/src/physics/distance.mjs +175 -0
- package/core/src/physics/extended-wasm-instance.d.mts +5 -0
- package/core/src/physics/extended-wasm-instance.mjs +48 -0
- package/core/src/physics/feature-api.d.mts +74 -0
- package/core/src/physics/feature-api.mjs +98 -0
- package/core/src/physics/fracture.d.mts +133 -0
- package/core/src/physics/fracture.mjs +1324 -0
- package/core/src/physics/gear.d.mts +585 -0
- package/core/src/physics/gear.mjs +955 -0
- package/core/src/physics/lag-compensation.d.mts +101 -0
- package/core/src/physics/lag-compensation.mjs +1100 -0
- package/core/src/physics/mass-properties.d.mts +114 -0
- package/core/src/physics/mass-properties.mjs +137 -468
- package/core/src/physics/particles.d.mts +177 -0
- package/core/src/physics/particles.mjs +758 -0
- package/core/src/physics/path-joint.d.mts +171 -0
- package/core/src/physics/path-joint.mjs +1231 -0
- package/core/src/physics/physics-extended-wasm-module.generated.mjs +13 -0
- package/core/src/physics/physics-profile-wasm-module.generated.mjs +48 -0
- package/core/src/physics/physics-wasm-module.generated.mjs +34 -5
- package/core/src/physics/powered-ragdoll.d.mts +188 -0
- package/core/src/physics/powered-ragdoll.mjs +1043 -0
- package/core/src/physics/powertrain.d.mts +260 -0
- package/core/src/physics/powertrain.mjs +621 -0
- package/core/src/physics/pulley.d.mts +98 -0
- package/core/src/physics/pulley.mjs +686 -0
- package/core/src/physics/ragdoll.d.mts +289 -0
- package/core/src/physics/ragdoll.mjs +1323 -0
- package/core/src/physics/rope.d.mts +241 -0
- package/core/src/physics/rope.mjs +319 -0
- package/core/src/physics/rounded-convex.d.mts +328 -0
- package/core/src/physics/rounded-convex.mjs +1404 -0
- package/core/src/physics/sdf-geometry.d.mts +117 -0
- package/core/src/physics/sdf-geometry.mjs +1204 -0
- package/core/src/physics/soft-body.d.mts +144 -0
- package/core/src/physics/soft-body.mjs +1285 -0
- package/core/src/physics/soft-collision.d.mts +651 -0
- package/core/src/physics/soft-collision.mjs +1883 -0
- package/core/src/physics/surface-velocity.d.mts +156 -0
- package/core/src/physics/surface-velocity.mjs +722 -0
- package/core/src/physics/tracks.d.mts +411 -0
- package/core/src/physics/tracks.mjs +1074 -0
- package/core/src/physics/vehicle.d.mts +3 -0
- package/core/src/physics/vehicle.mjs +172 -0
- package/core/src/physics/voxel-cooking.d.mts +219 -0
- package/core/src/physics/voxel-cooking.mjs +1499 -0
- package/core/src/physics/voxel-swept-quad.mjs +110 -0
- package/core/src/physics/wasm-instance.mjs +160 -23
- package/core/src/physics/world.d.mts +62 -0
- package/core/src/physics/world.mjs +1947 -152
- package/core/src/physics2d/body-state.mjs +218 -15
- package/core/src/physics2d/collision.d.mts +52 -0
- package/core/src/physics2d/collision.mjs +234 -0
- package/core/src/physics2d/compound.mjs +454 -0
- package/core/src/physics2d/contour-state.mjs +771 -0
- package/core/src/physics2d/feature-api.d.mts +211 -0
- package/core/src/physics2d/feature-api.mjs +3 -0
- package/core/src/physics2d/fluid-state.mjs +145 -0
- package/core/src/physics2d/joint-state.mjs +1 -1
- package/core/src/physics2d/owned-queries.mjs +3 -1
- package/core/src/physics2d/owned-transport.mjs +268 -122
- package/core/src/physics2d/owned-world.mjs +353 -230
- package/core/src/physics2d/particle-fluid.d.mts +152 -0
- package/core/src/physics2d/particle-fluid.mjs +233 -0
- package/core/src/physics2d/physics2d-wasm-module.generated.mjs +27 -10
- package/core/src/physics2d/query-codec.mjs +61 -13
- package/core/src/physics2d/submerged-geometry.d.mts +51 -0
- package/core/src/physics2d/submerged-geometry.mjs +186 -0
- package/core/src/runtime.mjs +23 -0
- package/core/src/shared/bytes.d.mts +2 -0
- package/core/src/shared/bytes.mjs +33 -0
- package/core/src/shared/sha256.d.mts +3 -0
- package/core/src/shared/sha256.mjs +72 -22
- package/core/units.d.ts +50 -0
- package/core/world.d.ts +253 -5
- package/dist/authority-room.d.ts +9 -0
- package/dist/authority-room.js +45 -16
- package/dist/browser.d.ts +6 -5
- package/dist/browser.js +4 -3
- package/dist/cadence.d.ts +7 -0
- package/dist/cadence.js +19 -0
- package/dist/canonical.d.ts +1 -0
- package/dist/canonical.js +3 -0
- package/dist/checksum-policy.d.ts +10 -0
- package/dist/checksum-policy.js +14 -0
- package/dist/collider-cooking-hull.d.ts +18 -0
- package/dist/collider-cooking-hull.js +248 -0
- package/dist/collider-cooking.d.ts +140 -0
- package/dist/collider-cooking.js +794 -0
- package/dist/compat/v5.d.ts +121 -0
- package/dist/compat/v5.js +479 -0
- package/dist/creator.d.ts +7 -6
- package/dist/creator.js +4 -3
- package/dist/game-byte-section.d.ts +36 -0
- package/dist/game-byte-section.js +350 -0
- package/dist/index.d.ts +13 -6
- package/dist/index.js +9 -4
- package/dist/instant-replay.d.ts +27 -2
- package/dist/instant-replay.js +91 -16
- package/dist/lag-compensation3d.js +11 -10
- package/dist/match-log.d.ts +21 -4
- package/dist/match-log.js +92 -9
- package/dist/math.d.ts +2 -0
- package/dist/math.js +1 -0
- package/dist/modules/character-3d.d.ts +16 -1
- package/dist/modules/character-3d.js +42 -5
- package/dist/modules/combat.js +6 -5
- package/dist/movement3d-revision1.d.ts +2 -0
- package/dist/movement3d-revision1.js +973 -0
- package/dist/movement3d.d.ts +53 -3
- package/dist/movement3d.js +917 -247
- package/dist/networked-client.d.ts +15 -0
- package/dist/networked-client.js +378 -105
- package/dist/node.d.ts +3 -2
- package/dist/node.js +2 -1
- package/dist/physics/2d.d.ts +38 -4
- package/dist/physics/2d.js +57 -36
- package/dist/physics/3d.d.ts +755 -111
- package/dist/physics/3d.js +2355 -158
- package/dist/physics/edit-result-graph.d.ts +36 -0
- package/dist/physics/edit-result-graph.js +141 -0
- package/dist/physics/game-codec3d.d.ts +41 -0
- package/dist/physics/game-codec3d.js +544 -0
- package/dist/physics/installed-checkpoint3d.d.ts +13 -0
- package/dist/physics/installed-checkpoint3d.js +120 -0
- package/dist/physics/installed-runtime3d.d.ts +51 -0
- package/dist/physics/installed-runtime3d.js +372 -0
- package/dist/physics/installed-state-commitment3d.d.ts +2 -0
- package/dist/physics/installed-state-commitment3d.js +24 -0
- package/dist/physics/units.d.ts +38 -0
- package/dist/physics/units.js +56 -0
- package/dist/physics/vehicle-state3d.d.ts +93 -0
- package/dist/physics/vehicle-state3d.js +1191 -0
- package/dist/physics/voxel-body-state.d.ts +61 -0
- package/dist/physics/voxel-body-state.js +410 -0
- package/dist/r3f-render-adapter.d.ts +59 -1
- package/dist/r3f-render-adapter.js +137 -2
- package/dist/replay.d.ts +13 -0
- package/dist/replay.js +95 -70
- package/dist/run-room-transport.js +5 -1
- package/dist/runner.d.ts +17 -2
- package/dist/runner.js +472 -84
- package/dist/runtime-cleanup.d.ts +54 -0
- package/dist/runtime-cleanup.js +266 -0
- package/dist/runtime-lease.d.ts +11 -1
- package/dist/runtime-lease.js +23 -10
- package/dist/runtime-room-metadata.d.ts +2 -8
- package/dist/runtime-room-metadata.js +11 -14
- package/dist/runtime-session.d.ts +8 -0
- package/dist/runtime-session.js +222 -58
- package/dist/schema-codegen.js +3 -0
- package/dist/schema-dsl-binary.d.ts +10 -4
- package/dist/schema-dsl-binary.js +375 -76
- package/dist/scratch-pool.d.ts +32 -0
- package/dist/scratch-pool.js +60 -0
- package/dist/sdk-session.d.ts +1 -0
- package/dist/sdk-session.js +4 -0
- package/dist/secret-config.js +2 -60
- package/dist/secret-crypto.js +70 -36
- package/dist/session-snapshot.d.ts +15 -4
- package/dist/session-snapshot.js +35 -33
- package/dist/session-wire.js +14 -22
- package/dist/sparse-set.d.ts +1 -0
- package/dist/sparse-set.js +22 -0
- package/dist/spatial/spatial-grid-3d.d.ts +41 -0
- package/dist/spatial/spatial-grid-3d.js +115 -0
- package/dist/synctest.d.ts +9 -0
- package/dist/synctest.js +106 -17
- package/dist/testing-network.d.ts +2 -0
- package/dist/testing-network.js +122 -18
- package/dist/testing.d.ts +27 -6
- package/dist/testing.js +297 -56
- package/dist/tools/index.d.ts +3 -2
- package/dist/tools/index.js +3 -2
- package/dist/tools/module-certification.d.ts +6 -0
- package/dist/tools/module-certification.js +26 -2
- package/package.json +18 -5
- package/core/scripts/run-box3d-benchmark.mjs +0 -350
- package/core/src/physics/index.mjs +0 -3169
|
@@ -0,0 +1,1141 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reduced-coordinate Articulation (Row J17 3D Physics Capability).
|
|
3
|
+
*
|
|
4
|
+
* Implements generalized coordinate Featherstone Articulated Body Algorithm (ABA)
|
|
5
|
+
* multi-body trees with optional kinematic loop closures.
|
|
6
|
+
*
|
|
7
|
+
* All physics computations (forward kinematics, joint forces/limits/drives,
|
|
8
|
+
* articulated body forward dynamics, integration, and loop constraint solving)
|
|
9
|
+
* are executed directly in WebAssembly (physics-extended.wasm).
|
|
10
|
+
*
|
|
11
|
+
* Determinism guarantees:
|
|
12
|
+
* - IEEE 754 double precision only
|
|
13
|
+
* - Fixed iteration and link indexing order
|
|
14
|
+
* - Bit-identical across native and browser WASM runtimes
|
|
15
|
+
* - Fail-closed: Throws explicit error if WASM runtime is unavailable (no JS fallback)
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import {
|
|
19
|
+
ensureExtendedInstance as extEnsureInstance,
|
|
20
|
+
extendedPhysicsReady as extPhysicsReady,
|
|
21
|
+
} from './extended-wasm-instance.mjs';
|
|
22
|
+
import {
|
|
23
|
+
physicsExtendedWasmModuleBase64,
|
|
24
|
+
physicsExtendedWasmMetadata,
|
|
25
|
+
} from './physics-extended-wasm-module.generated.mjs';
|
|
26
|
+
|
|
27
|
+
// ---------------------------------------------------------------------------
|
|
28
|
+
// Constants & Enums
|
|
29
|
+
// ---------------------------------------------------------------------------
|
|
30
|
+
|
|
31
|
+
export const ARTICULATION_ABI_VERSION = 1;
|
|
32
|
+
export const ARTICULATION_MAX_LINKS = 64;
|
|
33
|
+
export const ARTICULATION_MAX_TOTAL_DOFS = 256;
|
|
34
|
+
export const ARTICULATION_MAX_LOOPS = 32;
|
|
35
|
+
export const ARTICULATION_MAX_JOINT_DOFS = 6;
|
|
36
|
+
|
|
37
|
+
// Serialized ABI strides (in doubles)
|
|
38
|
+
export const ARTICULATION_LINK_STRIDE = 36;
|
|
39
|
+
export const ARTICULATION_JOINT_STRIDE = 28;
|
|
40
|
+
export const ARTICULATION_LOOP_STRIDE = 24;
|
|
41
|
+
export const ARTICULATION_STATE_STRIDE = 16;
|
|
42
|
+
export const ARTICULATION_TELEMETRY_STRIDE = 12;
|
|
43
|
+
|
|
44
|
+
// Memory layout byte sizes
|
|
45
|
+
const DEF_BYTE_SIZE = 70696;
|
|
46
|
+
const STATE_BYTE_SIZE = 18080;
|
|
47
|
+
const BODY_BYTE_SIZE = 400;
|
|
48
|
+
const LINK_BYTE_SIZE = 976;
|
|
49
|
+
const JOINT_BYTE_SIZE = 864;
|
|
50
|
+
const DOF_CONFIG_BYTE_SIZE = 112;
|
|
51
|
+
const LOOP_BYTE_SIZE = 256;
|
|
52
|
+
|
|
53
|
+
export const ArticulationRootMode = Object.freeze({
|
|
54
|
+
Fixed: 0, // Root link anchored to world base
|
|
55
|
+
Floating: 1, // Root link has 6 free degrees of freedom
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
export const ArticulationJointType = Object.freeze({
|
|
59
|
+
Fixed: 0, // 0 DOF
|
|
60
|
+
Revolute: 1, // 1 angular DOF around local axis1
|
|
61
|
+
Prismatic: 2, // 1 linear DOF along local axis1
|
|
62
|
+
Cylindrical: 3, // 2 DOFs: 1 angular + 1 linear along axis1
|
|
63
|
+
Universal: 4, // 2 angular DOFs: rotation around axis1 then axis2
|
|
64
|
+
Spherical: 5, // 3 angular DOFs (full 3D rotation)
|
|
65
|
+
Planar: 6, // 3 DOFs: 2 linear along axis1, axis2 + 1 angular around normal
|
|
66
|
+
Floating: 7, // 6 DOFs: 3 linear + 3 angular
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
export const ArticulationLoopKind = Object.freeze({
|
|
70
|
+
Point: 0, // Spherical point-to-point (3 linear constraints)
|
|
71
|
+
Distance: 1, // Fixed or limited distance (1 constraint)
|
|
72
|
+
Weld: 2, // Fixed relative pose (3 linear + 3 angular constraints)
|
|
73
|
+
Revolute: 3, // Hinge alignment (3 linear + 2 angular lock constraints)
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
export const ArticulationStatus = Object.freeze({
|
|
77
|
+
Success: 0,
|
|
78
|
+
InvalidLinkCount: -1,
|
|
79
|
+
InvalidHierarchy: -2,
|
|
80
|
+
InvalidJoint: -3,
|
|
81
|
+
InvalidMass: -4,
|
|
82
|
+
BufferOverflow: -5,
|
|
83
|
+
SingularInertia: -6,
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
export const ShapeKind = Object.freeze({
|
|
87
|
+
Box: 0,
|
|
88
|
+
Sphere: 1,
|
|
89
|
+
Capsule: 2,
|
|
90
|
+
Hull: 3,
|
|
91
|
+
Mesh: 4,
|
|
92
|
+
Compound: 5,
|
|
93
|
+
Heightfield: 6,
|
|
94
|
+
Cylinder: 7,
|
|
95
|
+
Cone: 8,
|
|
96
|
+
TaperedCapsule: 9,
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
export const wasmEntryPointsUsed = Object.freeze([
|
|
100
|
+
'rundot_kinetix_ext_articulation_abi_version',
|
|
101
|
+
'rundot_kinetix_ext_articulation_link_stride',
|
|
102
|
+
'rundot_kinetix_ext_articulation_joint_stride',
|
|
103
|
+
'rundot_kinetix_ext_articulation_state_stride',
|
|
104
|
+
'rundot_kinetix_ext_articulation_loop_stride',
|
|
105
|
+
'rundot_kinetix_ext_articulation_stride',
|
|
106
|
+
'rundot_kinetix_ext_articulation_size',
|
|
107
|
+
'rundot_kinetix_ext_articulation_max_links',
|
|
108
|
+
'rundot_kinetix_ext_articulation_max_dofs',
|
|
109
|
+
'rundot_kinetix_ext_articulation_max_loops',
|
|
110
|
+
'rundot_kinetix_ext_articulation_step',
|
|
111
|
+
'rundot_kinetix_ext_articulation_forward_kinematics',
|
|
112
|
+
'rundot_kinetix_ext_articulation_solve_loops',
|
|
113
|
+
'rundot_kinetix_ext_articulation_sync_bodies',
|
|
114
|
+
]);
|
|
115
|
+
|
|
116
|
+
// ---------------------------------------------------------------------------
|
|
117
|
+
// WASM Instance Management & Memory Setup
|
|
118
|
+
// ---------------------------------------------------------------------------
|
|
119
|
+
|
|
120
|
+
let cachedInstance = null;
|
|
121
|
+
let initPromise = null;
|
|
122
|
+
|
|
123
|
+
function decodeBase64(value) {
|
|
124
|
+
if (typeof atob === 'function') {
|
|
125
|
+
const decoded = atob(value);
|
|
126
|
+
const bytes = new Uint8Array(decoded.length);
|
|
127
|
+
for (let i = 0; i < decoded.length; i += 1) bytes[i] = decoded.charCodeAt(i);
|
|
128
|
+
return bytes;
|
|
129
|
+
}
|
|
130
|
+
if (typeof Buffer !== 'undefined') return Uint8Array.from(Buffer.from(value, 'base64'));
|
|
131
|
+
throw new Error('KINETIX_EXTENDED_PHYSICS_BASE64_UNAVAILABLE: Base64 decoder unavailable');
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export async function extendedPhysicsReady() {
|
|
135
|
+
const ready = await extPhysicsReady();
|
|
136
|
+
if (cachedInstance === null) {
|
|
137
|
+
cachedInstance = await extEnsureInstance();
|
|
138
|
+
}
|
|
139
|
+
return ready;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export async function ensureExtendedInstance() {
|
|
143
|
+
if (cachedInstance !== null) return cachedInstance;
|
|
144
|
+
if (initPromise !== null) return initPromise;
|
|
145
|
+
initPromise = Promise.resolve().then(async () => {
|
|
146
|
+
try {
|
|
147
|
+
cachedInstance = await extEnsureInstance();
|
|
148
|
+
return cachedInstance;
|
|
149
|
+
} finally {
|
|
150
|
+
initPromise = null;
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
return initPromise;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export function setExtendedInstance(instance) {
|
|
157
|
+
cachedInstance = instance;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export function articulationReady() {
|
|
161
|
+
return cachedInstance !== null;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
export async function initArticulationWasm() {
|
|
165
|
+
return ensureExtendedInstance();
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export function getExtendedInstance(instanceOverride = null) {
|
|
169
|
+
if (instanceOverride) return instanceOverride;
|
|
170
|
+
if (cachedInstance !== null) return cachedInstance;
|
|
171
|
+
|
|
172
|
+
try {
|
|
173
|
+
const bytes = decodeBase64(physicsExtendedWasmModuleBase64);
|
|
174
|
+
const mod = new WebAssembly.Module(bytes);
|
|
175
|
+
const inst = new WebAssembly.Instance(mod, {});
|
|
176
|
+
inst.exports._initialize?.();
|
|
177
|
+
cachedInstance = inst;
|
|
178
|
+
return cachedInstance;
|
|
179
|
+
} catch (error) {
|
|
180
|
+
const detail = error instanceof Error ? error.message : String(error);
|
|
181
|
+
throw new Error(
|
|
182
|
+
`KINETIX_ARTICULATION_WASM_UNAVAILABLE: ${detail}. Await extendedPhysicsReady() or ensureExtendedInstance() before calling articulation solver.`,
|
|
183
|
+
{ cause: error }
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// ---------------------------------------------------------------------------
|
|
189
|
+
// Scratch Memory Management
|
|
190
|
+
// ---------------------------------------------------------------------------
|
|
191
|
+
|
|
192
|
+
const SCRATCH_MEMORY_START = 8 * 1024 * 1024; // 8 MB
|
|
193
|
+
let scratchBumpOffset = SCRATCH_MEMORY_START;
|
|
194
|
+
|
|
195
|
+
function withScratch(instance, byteCount, fn) {
|
|
196
|
+
const exports = instance.exports;
|
|
197
|
+
const useMalloc = typeof exports.malloc === 'function';
|
|
198
|
+
let ptr = 0;
|
|
199
|
+
const alignedBytes = (byteCount + 15) & ~15;
|
|
200
|
+
|
|
201
|
+
if (useMalloc) {
|
|
202
|
+
ptr = exports.malloc(alignedBytes);
|
|
203
|
+
if (!ptr) {
|
|
204
|
+
throw new Error('KINETIX_ARTICULATION_MALLOC_FAILED: Out of memory in WASM heap');
|
|
205
|
+
}
|
|
206
|
+
} else {
|
|
207
|
+
const memory = exports.memory;
|
|
208
|
+
const required = scratchBumpOffset + alignedBytes;
|
|
209
|
+
if (required > memory.buffer.byteLength) {
|
|
210
|
+
const pages = Math.ceil((required - memory.buffer.byteLength) / 65536);
|
|
211
|
+
memory.grow(pages);
|
|
212
|
+
}
|
|
213
|
+
ptr = scratchBumpOffset;
|
|
214
|
+
scratchBumpOffset += alignedBytes;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
try {
|
|
218
|
+
return fn(ptr);
|
|
219
|
+
} finally {
|
|
220
|
+
if (useMalloc) {
|
|
221
|
+
if (typeof exports.free === 'function') {
|
|
222
|
+
exports.free(ptr);
|
|
223
|
+
}
|
|
224
|
+
} else {
|
|
225
|
+
scratchBumpOffset = ptr;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// ---------------------------------------------------------------------------
|
|
231
|
+
// ABI Metrics Query
|
|
232
|
+
// ---------------------------------------------------------------------------
|
|
233
|
+
|
|
234
|
+
export function getArticulationAbiMetrics(instanceOverride = null) {
|
|
235
|
+
const instance = getExtendedInstance(instanceOverride);
|
|
236
|
+
const exp = instance.exports;
|
|
237
|
+
return Object.freeze({
|
|
238
|
+
abiVersion: exp.rundot_kinetix_ext_articulation_abi_version?.() ?? ARTICULATION_ABI_VERSION,
|
|
239
|
+
linkStride: exp.rundot_kinetix_ext_articulation_link_stride?.() ?? ARTICULATION_LINK_STRIDE,
|
|
240
|
+
jointStride: exp.rundot_kinetix_ext_articulation_joint_stride?.() ?? ARTICULATION_JOINT_STRIDE,
|
|
241
|
+
stateStride: exp.rundot_kinetix_ext_articulation_state_stride?.() ?? ARTICULATION_STATE_STRIDE,
|
|
242
|
+
loopStride: exp.rundot_kinetix_ext_articulation_loop_stride?.() ?? ARTICULATION_LOOP_STRIDE,
|
|
243
|
+
maxLinks: exp.rundot_kinetix_ext_articulation_max_links?.() ?? ARTICULATION_MAX_LINKS,
|
|
244
|
+
maxDofs: exp.rundot_kinetix_ext_articulation_max_dofs?.() ?? ARTICULATION_MAX_TOTAL_DOFS,
|
|
245
|
+
maxLoops: exp.rundot_kinetix_ext_articulation_max_loops?.() ?? ARTICULATION_MAX_LOOPS,
|
|
246
|
+
stride: exp.rundot_kinetix_ext_articulation_stride?.() ?? ARTICULATION_LINK_STRIDE,
|
|
247
|
+
size: exp.rundot_kinetix_ext_articulation_size?.() ?? ARTICULATION_LINK_STRIDE,
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// ---------------------------------------------------------------------------
|
|
252
|
+
// Marshaling: ArticulationDef
|
|
253
|
+
// ---------------------------------------------------------------------------
|
|
254
|
+
|
|
255
|
+
function marshalArticulationDef(def, view, baseOffset = 0) {
|
|
256
|
+
const rootMode = def.rootMode === 'floating' || def.rootMode === ArticulationRootMode.Floating ? 1 : 0;
|
|
257
|
+
const links = def.links ?? [];
|
|
258
|
+
const linkCount = Math.min(links.length, ARTICULATION_MAX_LINKS);
|
|
259
|
+
const loops = def.loops ?? [];
|
|
260
|
+
const loopCount = Math.min(loops.length, ARTICULATION_MAX_LOOPS);
|
|
261
|
+
|
|
262
|
+
view.setUint32(baseOffset + 0, rootMode, true);
|
|
263
|
+
view.setUint32(baseOffset + 4, linkCount, true);
|
|
264
|
+
|
|
265
|
+
for (let i = 0; i < linkCount; i += 1) {
|
|
266
|
+
const link = links[i];
|
|
267
|
+
const linkOffset = baseOffset + 8 + i * LINK_BYTE_SIZE;
|
|
268
|
+
|
|
269
|
+
view.setUint32(linkOffset + 0, link.linkIndex ?? i, true);
|
|
270
|
+
view.setInt32(linkOffset + 4, link.parentIndex !== undefined ? link.parentIndex : (i === 0 ? -1 : 0), true);
|
|
271
|
+
view.setFloat64(linkOffset + 8, link.mass ?? 1.0, true);
|
|
272
|
+
|
|
273
|
+
const inertia = link.inertiaDiagonal ?? { x: 0.1, y: 0.1, z: 0.1 };
|
|
274
|
+
view.setFloat64(linkOffset + 16, inertia.x ?? 0.1, true);
|
|
275
|
+
view.setFloat64(linkOffset + 24, inertia.y ?? 0.1, true);
|
|
276
|
+
view.setFloat64(linkOffset + 32, inertia.z ?? 0.1, true);
|
|
277
|
+
|
|
278
|
+
const com = link.centerOfMass ?? { x: 0.0, y: 0.0, z: 0.0 };
|
|
279
|
+
view.setFloat64(linkOffset + 40, com.x ?? 0.0, true);
|
|
280
|
+
view.setFloat64(linkOffset + 48, com.y ?? 0.0, true);
|
|
281
|
+
view.setFloat64(linkOffset + 56, com.z ?? 0.0, true);
|
|
282
|
+
|
|
283
|
+
view.setFloat64(linkOffset + 64, link.linearDamping ?? 0.0, true);
|
|
284
|
+
view.setFloat64(linkOffset + 72, link.angularDamping ?? 0.0, true);
|
|
285
|
+
|
|
286
|
+
const shapeKind = typeof link.shapeKind === 'number' ? link.shapeKind : (ShapeKind[link.shapeKind] ?? ShapeKind.Box);
|
|
287
|
+
view.setUint32(linkOffset + 80, shapeKind, true);
|
|
288
|
+
|
|
289
|
+
const extents = link.shapeHalfExtents ?? { x: 0.1, y: 0.1, z: 0.1 };
|
|
290
|
+
view.setFloat64(linkOffset + 88, extents.x ?? 0.1, true);
|
|
291
|
+
view.setFloat64(linkOffset + 96, extents.y ?? 0.1, true);
|
|
292
|
+
view.setFloat64(linkOffset + 104, extents.z ?? 0.1, true);
|
|
293
|
+
|
|
294
|
+
// Joint definition
|
|
295
|
+
const joint = link.joint ?? {};
|
|
296
|
+
const jointOffset = linkOffset + 112;
|
|
297
|
+
|
|
298
|
+
const jointType = typeof joint.type === 'number' ? joint.type : (ArticulationJointType[joint.type] ?? ArticulationJointType.Fixed);
|
|
299
|
+
const dofCount = joint.dofCount ?? getDefaultDofCount(jointType);
|
|
300
|
+
view.setUint32(jointOffset + 0, jointType, true);
|
|
301
|
+
view.setUint32(jointOffset + 4, dofCount, true);
|
|
302
|
+
|
|
303
|
+
const axis1 = joint.axis1 ?? { x: 1.0, y: 0.0, z: 0.0 };
|
|
304
|
+
view.setFloat64(jointOffset + 8, axis1.x ?? 1.0, true);
|
|
305
|
+
view.setFloat64(jointOffset + 16, axis1.y ?? 0.0, true);
|
|
306
|
+
view.setFloat64(jointOffset + 24, axis1.z ?? 0.0, true);
|
|
307
|
+
|
|
308
|
+
const axis2 = joint.axis2 ?? { x: 0.0, y: 1.0, z: 0.0 };
|
|
309
|
+
view.setFloat64(jointOffset + 32, axis2.x ?? 0.0, true);
|
|
310
|
+
view.setFloat64(jointOffset + 40, axis2.y ?? 1.0, true);
|
|
311
|
+
view.setFloat64(jointOffset + 48, axis2.z ?? 0.0, true);
|
|
312
|
+
|
|
313
|
+
const axis3 = joint.axis3 ?? { x: 0.0, y: 0.0, z: 1.0 };
|
|
314
|
+
view.setFloat64(jointOffset + 56, axis3.x ?? 0.0, true);
|
|
315
|
+
view.setFloat64(jointOffset + 64, axis3.y ?? 0.0, true);
|
|
316
|
+
view.setFloat64(jointOffset + 72, axis3.z ?? 1.0, true);
|
|
317
|
+
|
|
318
|
+
// Parent anchor transform
|
|
319
|
+
const parentAnchor = joint.parentAnchor ?? {};
|
|
320
|
+
const pPos = parentAnchor.position ?? { x: 0.0, y: 0.0, z: 0.0 };
|
|
321
|
+
const pOri = parentAnchor.orientation ?? { x: 0.0, y: 0.0, z: 0.0, w: 1.0 };
|
|
322
|
+
view.setFloat64(jointOffset + 80, pPos.x ?? 0.0, true);
|
|
323
|
+
view.setFloat64(jointOffset + 88, pPos.y ?? 0.0, true);
|
|
324
|
+
view.setFloat64(jointOffset + 96, pPos.z ?? 0.0, true);
|
|
325
|
+
view.setFloat64(jointOffset + 104, pOri.x ?? 0.0, true);
|
|
326
|
+
view.setFloat64(jointOffset + 112, pOri.y ?? 0.0, true);
|
|
327
|
+
view.setFloat64(jointOffset + 120, pOri.z ?? 0.0, true);
|
|
328
|
+
view.setFloat64(jointOffset + 128, pOri.w ?? 1.0, true);
|
|
329
|
+
|
|
330
|
+
// Child anchor transform
|
|
331
|
+
const childAnchor = joint.childAnchor ?? {};
|
|
332
|
+
const cPos = childAnchor.position ?? { x: 0.0, y: 0.0, z: 0.0 };
|
|
333
|
+
const cOri = childAnchor.orientation ?? { x: 0.0, y: 0.0, z: 0.0, w: 1.0 };
|
|
334
|
+
view.setFloat64(jointOffset + 136, cPos.x ?? 0.0, true);
|
|
335
|
+
view.setFloat64(jointOffset + 144, cPos.y ?? 0.0, true);
|
|
336
|
+
view.setFloat64(jointOffset + 152, cPos.z ?? 0.0, true);
|
|
337
|
+
view.setFloat64(jointOffset + 160, cOri.x ?? 0.0, true);
|
|
338
|
+
view.setFloat64(jointOffset + 168, cOri.y ?? 0.0, true);
|
|
339
|
+
view.setFloat64(jointOffset + 176, cOri.z ?? 0.0, true);
|
|
340
|
+
view.setFloat64(jointOffset + 184, cOri.w ?? 1.0, true);
|
|
341
|
+
|
|
342
|
+
// DOF configurations
|
|
343
|
+
const dofConfigs = joint.dofConfigs ?? [];
|
|
344
|
+
for (let d = 0; d < ARTICULATION_MAX_JOINT_DOFS; d += 1) {
|
|
345
|
+
const cfg = dofConfigs[d] ?? {};
|
|
346
|
+
const dofOffset = jointOffset + 192 + d * DOF_CONFIG_BYTE_SIZE;
|
|
347
|
+
|
|
348
|
+
view.setUint8(dofOffset + 0, cfg.hasLimits ? 1 : 0);
|
|
349
|
+
view.setFloat64(dofOffset + 8, cfg.lowerLimit ?? 0.0, true);
|
|
350
|
+
view.setFloat64(dofOffset + 16, cfg.upperLimit ?? 0.0, true);
|
|
351
|
+
view.setFloat64(dofOffset + 24, cfg.limitStiffness ?? 1000.0, true);
|
|
352
|
+
view.setFloat64(dofOffset + 32, cfg.limitDamping ?? 100.0, true);
|
|
353
|
+
view.setFloat64(dofOffset + 40, cfg.limitRestitution ?? 0.0, true);
|
|
354
|
+
|
|
355
|
+
view.setUint8(dofOffset + 48, cfg.driveEnabled ? 1 : 0);
|
|
356
|
+
view.setFloat64(dofOffset + 56, cfg.targetPosition ?? 0.0, true);
|
|
357
|
+
view.setFloat64(dofOffset + 64, cfg.targetVelocity ?? 0.0, true);
|
|
358
|
+
view.setFloat64(dofOffset + 72, cfg.driveStiffness ?? 0.0, true);
|
|
359
|
+
view.setFloat64(dofOffset + 80, cfg.driveDamping ?? 0.0, true);
|
|
360
|
+
view.setFloat64(dofOffset + 88, cfg.driveMaxForce ?? 1000.0, true);
|
|
361
|
+
|
|
362
|
+
view.setFloat64(dofOffset + 96, cfg.frictionCoefficient ?? 0.0, true);
|
|
363
|
+
view.setFloat64(dofOffset + 104, cfg.maxFrictionForce ?? 0.0, true);
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
// Loops
|
|
368
|
+
view.setUint32(baseOffset + 62472, loopCount, true);
|
|
369
|
+
for (let k = 0; k < loopCount; k += 1) {
|
|
370
|
+
const loop = loops[k];
|
|
371
|
+
const loopOffset = baseOffset + 62480 + k * LOOP_BYTE_SIZE;
|
|
372
|
+
|
|
373
|
+
view.setUint32(loopOffset + 0, loop.linkA ?? 0, true);
|
|
374
|
+
view.setInt32(loopOffset + 4, loop.linkB !== undefined ? loop.linkB : -1, true);
|
|
375
|
+
|
|
376
|
+
const kind = typeof loop.kind === 'number' ? loop.kind : (ArticulationLoopKind[loop.kind] ?? ArticulationLoopKind.Point);
|
|
377
|
+
view.setUint32(loopOffset + 8, kind, true);
|
|
378
|
+
|
|
379
|
+
const pA = loop.pivotA ?? { x: 0.0, y: 0.0, z: 0.0 };
|
|
380
|
+
view.setFloat64(loopOffset + 16, pA.x ?? 0.0, true);
|
|
381
|
+
view.setFloat64(loopOffset + 24, pA.y ?? 0.0, true);
|
|
382
|
+
view.setFloat64(loopOffset + 32, pA.z ?? 0.0, true);
|
|
383
|
+
|
|
384
|
+
const pB = loop.pivotB ?? { x: 0.0, y: 0.0, z: 0.0 };
|
|
385
|
+
view.setFloat64(loopOffset + 40, pB.x ?? 0.0, true);
|
|
386
|
+
view.setFloat64(loopOffset + 48, pB.y ?? 0.0, true);
|
|
387
|
+
view.setFloat64(loopOffset + 56, pB.z ?? 0.0, true);
|
|
388
|
+
|
|
389
|
+
const axA = loop.axisA ?? { x: 1.0, y: 0.0, z: 0.0 };
|
|
390
|
+
view.setFloat64(loopOffset + 64, axA.x ?? 1.0, true);
|
|
391
|
+
view.setFloat64(loopOffset + 72, axA.y ?? 0.0, true);
|
|
392
|
+
view.setFloat64(loopOffset + 80, axA.z ?? 0.0, true);
|
|
393
|
+
|
|
394
|
+
const axB = loop.axisB ?? { x: 1.0, y: 0.0, z: 0.0 };
|
|
395
|
+
view.setFloat64(loopOffset + 88, axB.x ?? 1.0, true);
|
|
396
|
+
view.setFloat64(loopOffset + 96, axB.y ?? 0.0, true);
|
|
397
|
+
view.setFloat64(loopOffset + 104, axB.z ?? 0.0, true);
|
|
398
|
+
|
|
399
|
+
view.setFloat64(loopOffset + 112, loop.targetDistance ?? 0.0, true);
|
|
400
|
+
view.setFloat64(loopOffset + 120, loop.minDistance ?? 0.0, true);
|
|
401
|
+
view.setFloat64(loopOffset + 128, loop.maxDistance ?? 0.0, true);
|
|
402
|
+
view.setFloat64(loopOffset + 136, loop.stiffness ?? 0.2, true);
|
|
403
|
+
view.setFloat64(loopOffset + 144, loop.damping ?? 0.0, true);
|
|
404
|
+
view.setFloat64(loopOffset + 152, loop.maxImpulse ?? 1e9, true);
|
|
405
|
+
|
|
406
|
+
const accImp = loop.accumulatedImpulse ?? [0, 0, 0, 0, 0, 0];
|
|
407
|
+
for (let c = 0; c < 6; c += 1) {
|
|
408
|
+
view.setFloat64(loopOffset + 160 + c * 8, accImp[c] ?? 0.0, true);
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
const lastImp = loop.lastImpulse ?? [0, 0, 0, 0, 0, 0];
|
|
412
|
+
for (let c = 0; c < 6; c += 1) {
|
|
413
|
+
view.setFloat64(loopOffset + 208 + c * 8, lastImp[c] ?? 0.0, true);
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
// Gravity
|
|
418
|
+
const grav = def.gravity ?? { x: 0.0, y: -9.81, z: 0.0 };
|
|
419
|
+
view.setFloat64(baseOffset + 70672, grav.x ?? 0.0, true);
|
|
420
|
+
view.setFloat64(baseOffset + 70680, grav.y ?? -9.81, true);
|
|
421
|
+
view.setFloat64(baseOffset + 70688, grav.z ?? 0.0, true);
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
function getDefaultDofCount(type) {
|
|
425
|
+
switch (type) {
|
|
426
|
+
case ArticulationJointType.Fixed: return 0;
|
|
427
|
+
case ArticulationJointType.Revolute: return 1;
|
|
428
|
+
case ArticulationJointType.Prismatic: return 1;
|
|
429
|
+
case ArticulationJointType.Cylindrical: return 2;
|
|
430
|
+
case ArticulationJointType.Universal: return 2;
|
|
431
|
+
case ArticulationJointType.Spherical: return 3;
|
|
432
|
+
case ArticulationJointType.Planar: return 3;
|
|
433
|
+
case ArticulationJointType.Floating: return 6;
|
|
434
|
+
default: return 0;
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
// ---------------------------------------------------------------------------
|
|
439
|
+
// Marshaling: ArticulationState
|
|
440
|
+
// ---------------------------------------------------------------------------
|
|
441
|
+
|
|
442
|
+
function marshalArticulationState(state, view, baseOffset = 0) {
|
|
443
|
+
const rootPose = state.rootPose ?? {};
|
|
444
|
+
const rpPos = rootPose.position ?? { x: 0.0, y: 0.0, z: 0.0 };
|
|
445
|
+
const rpOri = rootPose.orientation ?? { x: 0.0, y: 0.0, z: 0.0, w: 1.0 };
|
|
446
|
+
view.setFloat64(baseOffset + 0, rpPos.x ?? 0.0, true);
|
|
447
|
+
view.setFloat64(baseOffset + 8, rpPos.y ?? 0.0, true);
|
|
448
|
+
view.setFloat64(baseOffset + 16, rpPos.z ?? 0.0, true);
|
|
449
|
+
view.setFloat64(baseOffset + 24, rpOri.x ?? 0.0, true);
|
|
450
|
+
view.setFloat64(baseOffset + 32, rpOri.y ?? 0.0, true);
|
|
451
|
+
view.setFloat64(baseOffset + 40, rpOri.z ?? 0.0, true);
|
|
452
|
+
view.setFloat64(baseOffset + 48, rpOri.w ?? 1.0, true);
|
|
453
|
+
|
|
454
|
+
const rootVel = state.rootVelocity ?? {};
|
|
455
|
+
const rvAng = rootVel.angular ?? { x: 0.0, y: 0.0, z: 0.0 };
|
|
456
|
+
const rvLin = rootVel.linear ?? { x: 0.0, y: 0.0, z: 0.0 };
|
|
457
|
+
view.setFloat64(baseOffset + 56, rvAng.x ?? 0.0, true);
|
|
458
|
+
view.setFloat64(baseOffset + 64, rvAng.y ?? 0.0, true);
|
|
459
|
+
view.setFloat64(baseOffset + 72, rvAng.z ?? 0.0, true);
|
|
460
|
+
view.setFloat64(baseOffset + 80, rvLin.x ?? 0.0, true);
|
|
461
|
+
view.setFloat64(baseOffset + 88, rvLin.y ?? 0.0, true);
|
|
462
|
+
view.setFloat64(baseOffset + 96, rvLin.z ?? 0.0, true);
|
|
463
|
+
|
|
464
|
+
const rootAcc = state.rootAcceleration ?? {};
|
|
465
|
+
const raAng = rootAcc.angular ?? { x: 0.0, y: 0.0, z: 0.0 };
|
|
466
|
+
const raLin = rootAcc.linear ?? { x: 0.0, y: 0.0, z: 0.0 };
|
|
467
|
+
view.setFloat64(baseOffset + 104, raAng.x ?? 0.0, true);
|
|
468
|
+
view.setFloat64(baseOffset + 112, raAng.y ?? 0.0, true);
|
|
469
|
+
view.setFloat64(baseOffset + 120, raAng.z ?? 0.0, true);
|
|
470
|
+
view.setFloat64(baseOffset + 128, raLin.x ?? 0.0, true);
|
|
471
|
+
view.setFloat64(baseOffset + 136, raLin.y ?? 0.0, true);
|
|
472
|
+
view.setFloat64(baseOffset + 144, raLin.z ?? 0.0, true);
|
|
473
|
+
|
|
474
|
+
const totalDofs = state.totalDofs ?? (state.q?.length ?? 0);
|
|
475
|
+
view.setUint32(baseOffset + 152, totalDofs, true);
|
|
476
|
+
|
|
477
|
+
const q = state.q ?? [];
|
|
478
|
+
for (let i = 0; i < ARTICULATION_MAX_TOTAL_DOFS; i += 1) {
|
|
479
|
+
view.setFloat64(baseOffset + 160 + i * 8, q[i] ?? 0.0, true);
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
const u = state.u ?? [];
|
|
483
|
+
for (let i = 0; i < ARTICULATION_MAX_TOTAL_DOFS; i += 1) {
|
|
484
|
+
view.setFloat64(baseOffset + 2208 + i * 8, u[i] ?? 0.0, true);
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
const udot = state.udot ?? [];
|
|
488
|
+
for (let i = 0; i < ARTICULATION_MAX_TOTAL_DOFS; i += 1) {
|
|
489
|
+
view.setFloat64(baseOffset + 4256 + i * 8, udot[i] ?? 0.0, true);
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
const tau = state.tau ?? [];
|
|
493
|
+
for (let i = 0; i < ARTICULATION_MAX_TOTAL_DOFS; i += 1) {
|
|
494
|
+
view.setFloat64(baseOffset + 6304 + i * 8, tau[i] ?? 0.0, true);
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
const linkTransforms = state.linkTransforms ?? [];
|
|
498
|
+
for (let i = 0; i < ARTICULATION_MAX_LINKS; i += 1) {
|
|
499
|
+
const t = linkTransforms[i] ?? {};
|
|
500
|
+
const pos = t.position ?? { x: 0.0, y: 0.0, z: 0.0 };
|
|
501
|
+
const ori = t.orientation ?? { x: 0.0, y: 0.0, z: 0.0, w: 1.0 };
|
|
502
|
+
const tOffset = baseOffset + 8352 + i * 56;
|
|
503
|
+
view.setFloat64(tOffset + 0, pos.x ?? 0.0, true);
|
|
504
|
+
view.setFloat64(tOffset + 8, pos.y ?? 0.0, true);
|
|
505
|
+
view.setFloat64(tOffset + 16, pos.z ?? 0.0, true);
|
|
506
|
+
view.setFloat64(tOffset + 24, ori.x ?? 0.0, true);
|
|
507
|
+
view.setFloat64(tOffset + 32, ori.y ?? 0.0, true);
|
|
508
|
+
view.setFloat64(tOffset + 40, ori.z ?? 0.0, true);
|
|
509
|
+
view.setFloat64(tOffset + 48, ori.w ?? 1.0, true);
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
const linkVelocities = state.linkVelocities ?? [];
|
|
513
|
+
for (let i = 0; i < ARTICULATION_MAX_LINKS; i += 1) {
|
|
514
|
+
const v = linkVelocities[i] ?? {};
|
|
515
|
+
const ang = v.angular ?? { x: 0.0, y: 0.0, z: 0.0 };
|
|
516
|
+
const lin = v.linear ?? { x: 0.0, y: 0.0, z: 0.0 };
|
|
517
|
+
const vOffset = baseOffset + 11936 + i * 48;
|
|
518
|
+
view.setFloat64(vOffset + 0, ang.x ?? 0.0, true);
|
|
519
|
+
view.setFloat64(vOffset + 8, ang.y ?? 0.0, true);
|
|
520
|
+
view.setFloat64(vOffset + 16, ang.z ?? 0.0, true);
|
|
521
|
+
view.setFloat64(vOffset + 24, lin.x ?? 0.0, true);
|
|
522
|
+
view.setFloat64(vOffset + 32, lin.y ?? 0.0, true);
|
|
523
|
+
view.setFloat64(vOffset + 40, lin.z ?? 0.0, true);
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
const linkAccelerations = state.linkAccelerations ?? [];
|
|
527
|
+
for (let i = 0; i < ARTICULATION_MAX_LINKS; i += 1) {
|
|
528
|
+
const a = linkAccelerations[i] ?? {};
|
|
529
|
+
const ang = a.angular ?? { x: 0.0, y: 0.0, z: 0.0 };
|
|
530
|
+
const lin = a.linear ?? { x: 0.0, y: 0.0, z: 0.0 };
|
|
531
|
+
const aOffset = baseOffset + 15008 + i * 48;
|
|
532
|
+
view.setFloat64(aOffset + 0, ang.x ?? 0.0, true);
|
|
533
|
+
view.setFloat64(aOffset + 8, ang.y ?? 0.0, true);
|
|
534
|
+
view.setFloat64(aOffset + 16, ang.z ?? 0.0, true);
|
|
535
|
+
view.setFloat64(aOffset + 24, lin.x ?? 0.0, true);
|
|
536
|
+
view.setFloat64(aOffset + 32, lin.y ?? 0.0, true);
|
|
537
|
+
view.setFloat64(aOffset + 40, lin.z ?? 0.0, true);
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
function unmarshalArticulationState(view, targetState, linkCount, baseOffset = 0) {
|
|
542
|
+
targetState.rootPose = {
|
|
543
|
+
position: {
|
|
544
|
+
x: view.getFloat64(baseOffset + 0, true),
|
|
545
|
+
y: view.getFloat64(baseOffset + 8, true),
|
|
546
|
+
z: view.getFloat64(baseOffset + 16, true),
|
|
547
|
+
},
|
|
548
|
+
orientation: {
|
|
549
|
+
x: view.getFloat64(baseOffset + 24, true),
|
|
550
|
+
y: view.getFloat64(baseOffset + 32, true),
|
|
551
|
+
z: view.getFloat64(baseOffset + 40, true),
|
|
552
|
+
w: view.getFloat64(baseOffset + 48, true),
|
|
553
|
+
},
|
|
554
|
+
};
|
|
555
|
+
|
|
556
|
+
targetState.rootVelocity = {
|
|
557
|
+
angular: {
|
|
558
|
+
x: view.getFloat64(baseOffset + 56, true),
|
|
559
|
+
y: view.getFloat64(baseOffset + 64, true),
|
|
560
|
+
z: view.getFloat64(baseOffset + 72, true),
|
|
561
|
+
},
|
|
562
|
+
linear: {
|
|
563
|
+
x: view.getFloat64(baseOffset + 80, true),
|
|
564
|
+
y: view.getFloat64(baseOffset + 88, true),
|
|
565
|
+
z: view.getFloat64(baseOffset + 96, true),
|
|
566
|
+
},
|
|
567
|
+
};
|
|
568
|
+
|
|
569
|
+
targetState.rootAcceleration = {
|
|
570
|
+
angular: {
|
|
571
|
+
x: view.getFloat64(baseOffset + 104, true),
|
|
572
|
+
y: view.getFloat64(baseOffset + 112, true),
|
|
573
|
+
z: view.getFloat64(baseOffset + 120, true),
|
|
574
|
+
},
|
|
575
|
+
linear: {
|
|
576
|
+
x: view.getFloat64(baseOffset + 128, true),
|
|
577
|
+
y: view.getFloat64(baseOffset + 136, true),
|
|
578
|
+
z: view.getFloat64(baseOffset + 144, true),
|
|
579
|
+
},
|
|
580
|
+
};
|
|
581
|
+
|
|
582
|
+
const totalDofs = view.getUint32(baseOffset + 152, true);
|
|
583
|
+
targetState.totalDofs = totalDofs;
|
|
584
|
+
|
|
585
|
+
if (!Array.isArray(targetState.q) || targetState.q.length !== totalDofs) {
|
|
586
|
+
targetState.q = new Float64Array(totalDofs);
|
|
587
|
+
}
|
|
588
|
+
for (let i = 0; i < totalDofs; i += 1) {
|
|
589
|
+
targetState.q[i] = view.getFloat64(baseOffset + 160 + i * 8, true);
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
if (!Array.isArray(targetState.u) || targetState.u.length !== totalDofs) {
|
|
593
|
+
targetState.u = new Float64Array(totalDofs);
|
|
594
|
+
}
|
|
595
|
+
for (let i = 0; i < totalDofs; i += 1) {
|
|
596
|
+
targetState.u[i] = view.getFloat64(baseOffset + 2208 + i * 8, true);
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
if (!Array.isArray(targetState.udot) || targetState.udot.length !== totalDofs) {
|
|
600
|
+
targetState.udot = new Float64Array(totalDofs);
|
|
601
|
+
}
|
|
602
|
+
for (let i = 0; i < totalDofs; i += 1) {
|
|
603
|
+
targetState.udot[i] = view.getFloat64(baseOffset + 4256 + i * 8, true);
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
if (!Array.isArray(targetState.tau) || targetState.tau.length !== totalDofs) {
|
|
607
|
+
targetState.tau = new Float64Array(totalDofs);
|
|
608
|
+
}
|
|
609
|
+
for (let i = 0; i < totalDofs; i += 1) {
|
|
610
|
+
targetState.tau[i] = view.getFloat64(baseOffset + 6304 + i * 8, true);
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
const effectiveLinks = Math.max(1, Math.min(linkCount, ARTICULATION_MAX_LINKS));
|
|
614
|
+
|
|
615
|
+
if (!Array.isArray(targetState.linkTransforms) || targetState.linkTransforms.length !== effectiveLinks) {
|
|
616
|
+
targetState.linkTransforms = new Array(effectiveLinks);
|
|
617
|
+
}
|
|
618
|
+
for (let i = 0; i < effectiveLinks; i += 1) {
|
|
619
|
+
const tOffset = baseOffset + 8352 + i * 56;
|
|
620
|
+
targetState.linkTransforms[i] = {
|
|
621
|
+
position: {
|
|
622
|
+
x: view.getFloat64(tOffset + 0, true),
|
|
623
|
+
y: view.getFloat64(tOffset + 8, true),
|
|
624
|
+
z: view.getFloat64(tOffset + 16, true),
|
|
625
|
+
},
|
|
626
|
+
orientation: {
|
|
627
|
+
x: view.getFloat64(tOffset + 24, true),
|
|
628
|
+
y: view.getFloat64(tOffset + 32, true),
|
|
629
|
+
z: view.getFloat64(tOffset + 40, true),
|
|
630
|
+
w: view.getFloat64(tOffset + 48, true),
|
|
631
|
+
},
|
|
632
|
+
};
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
if (!Array.isArray(targetState.linkVelocities) || targetState.linkVelocities.length !== effectiveLinks) {
|
|
636
|
+
targetState.linkVelocities = new Array(effectiveLinks);
|
|
637
|
+
}
|
|
638
|
+
for (let i = 0; i < effectiveLinks; i += 1) {
|
|
639
|
+
const vOffset = baseOffset + 11936 + i * 48;
|
|
640
|
+
targetState.linkVelocities[i] = {
|
|
641
|
+
angular: {
|
|
642
|
+
x: view.getFloat64(vOffset + 0, true),
|
|
643
|
+
y: view.getFloat64(vOffset + 8, true),
|
|
644
|
+
z: view.getFloat64(vOffset + 16, true),
|
|
645
|
+
},
|
|
646
|
+
linear: {
|
|
647
|
+
x: view.getFloat64(vOffset + 24, true),
|
|
648
|
+
y: view.getFloat64(vOffset + 32, true),
|
|
649
|
+
z: view.getFloat64(vOffset + 40, true),
|
|
650
|
+
},
|
|
651
|
+
};
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
if (!Array.isArray(targetState.linkAccelerations) || targetState.linkAccelerations.length !== effectiveLinks) {
|
|
655
|
+
targetState.linkAccelerations = new Array(effectiveLinks);
|
|
656
|
+
}
|
|
657
|
+
for (let i = 0; i < effectiveLinks; i += 1) {
|
|
658
|
+
const aOffset = baseOffset + 15008 + i * 48;
|
|
659
|
+
targetState.linkAccelerations[i] = {
|
|
660
|
+
angular: {
|
|
661
|
+
x: view.getFloat64(aOffset + 0, true),
|
|
662
|
+
y: view.getFloat64(aOffset + 8, true),
|
|
663
|
+
z: view.getFloat64(aOffset + 16, true),
|
|
664
|
+
},
|
|
665
|
+
linear: {
|
|
666
|
+
x: view.getFloat64(aOffset + 24, true),
|
|
667
|
+
y: view.getFloat64(aOffset + 32, true),
|
|
668
|
+
z: view.getFloat64(aOffset + 40, true),
|
|
669
|
+
},
|
|
670
|
+
};
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
return targetState;
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
function unmarshalBodies(view, count, baseOffset = 0, targetBodies = null) {
|
|
677
|
+
const result = targetBodies && Array.isArray(targetBodies) ? targetBodies : new Array(count);
|
|
678
|
+
for (let i = 0; i < count; i += 1) {
|
|
679
|
+
const bOffset = baseOffset + i * BODY_BYTE_SIZE;
|
|
680
|
+
const body = result[i] ?? {};
|
|
681
|
+
body.mass = view.getFloat64(bOffset + 88, true);
|
|
682
|
+
body.shape = view.getUint32(bOffset + 112, true);
|
|
683
|
+
body.x = view.getFloat64(bOffset + 120, true);
|
|
684
|
+
body.y = view.getFloat64(bOffset + 128, true);
|
|
685
|
+
body.z = view.getFloat64(bOffset + 136, true);
|
|
686
|
+
body.vx = view.getFloat64(bOffset + 144, true);
|
|
687
|
+
body.vy = view.getFloat64(bOffset + 152, true);
|
|
688
|
+
body.vz = view.getFloat64(bOffset + 160, true);
|
|
689
|
+
body.angularVel = {
|
|
690
|
+
x: view.getFloat64(bOffset + 168, true),
|
|
691
|
+
y: view.getFloat64(bOffset + 176, true),
|
|
692
|
+
z: view.getFloat64(bOffset + 184, true),
|
|
693
|
+
};
|
|
694
|
+
body.orientation = {
|
|
695
|
+
x: view.getFloat64(bOffset + 192, true),
|
|
696
|
+
y: view.getFloat64(bOffset + 200, true),
|
|
697
|
+
z: view.getFloat64(bOffset + 208, true),
|
|
698
|
+
w: view.getFloat64(bOffset + 216, true),
|
|
699
|
+
};
|
|
700
|
+
body.inertia = {
|
|
701
|
+
xx: view.getFloat64(bOffset + 272, true),
|
|
702
|
+
yy: view.getFloat64(bOffset + 280, true),
|
|
703
|
+
zz: view.getFloat64(bOffset + 288, true),
|
|
704
|
+
};
|
|
705
|
+
body.centerOfMass = {
|
|
706
|
+
x: view.getFloat64(bOffset + 320, true),
|
|
707
|
+
y: view.getFloat64(bOffset + 328, true),
|
|
708
|
+
z: view.getFloat64(bOffset + 336, true),
|
|
709
|
+
};
|
|
710
|
+
result[i] = body;
|
|
711
|
+
}
|
|
712
|
+
return result;
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
// ---------------------------------------------------------------------------
|
|
716
|
+
// High-Level Articulation API Functions
|
|
717
|
+
// ---------------------------------------------------------------------------
|
|
718
|
+
|
|
719
|
+
export function countArticulationDofs(def) {
|
|
720
|
+
const isFloating = def.rootMode === 'floating' || def.rootMode === ArticulationRootMode.Floating;
|
|
721
|
+
let dofs = isFloating ? 6 : 0;
|
|
722
|
+
const links = def.links ?? [];
|
|
723
|
+
for (let i = 1; i < links.length; i += 1) {
|
|
724
|
+
const joint = links[i]?.joint ?? {};
|
|
725
|
+
const jointType = typeof joint.type === 'number' ? joint.type : (ArticulationJointType[joint.type] ?? ArticulationJointType.Fixed);
|
|
726
|
+
dofs += joint.dofCount ?? getDefaultDofCount(jointType);
|
|
727
|
+
}
|
|
728
|
+
return dofs;
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
export function validateArticulation(def) {
|
|
732
|
+
const links = def.links ?? [];
|
|
733
|
+
if (links.length === 0 || links.length > ARTICULATION_MAX_LINKS) {
|
|
734
|
+
return ArticulationStatus.InvalidLinkCount;
|
|
735
|
+
}
|
|
736
|
+
if (links[0].parentIndex !== undefined && links[0].parentIndex !== -1) {
|
|
737
|
+
return ArticulationStatus.InvalidHierarchy;
|
|
738
|
+
}
|
|
739
|
+
if ((links[0].mass ?? 1.0) <= 0.0) {
|
|
740
|
+
return ArticulationStatus.InvalidMass;
|
|
741
|
+
}
|
|
742
|
+
const i0 = links[0].inertiaDiagonal ?? { x: 0.1, y: 0.1, z: 0.1 };
|
|
743
|
+
if ((i0.x ?? 0) <= 0 || (i0.y ?? 0) <= 0 || (i0.z ?? 0) <= 0) {
|
|
744
|
+
return ArticulationStatus.SingularInertia;
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
const isFloating = def.rootMode === 'floating' || def.rootMode === ArticulationRootMode.Floating;
|
|
748
|
+
let totalDofs = isFloating ? 6 : 0;
|
|
749
|
+
|
|
750
|
+
for (let i = 1; i < links.length; i += 1) {
|
|
751
|
+
const link = links[i];
|
|
752
|
+
const parent = link.parentIndex ?? 0;
|
|
753
|
+
if (parent < 0 || parent >= i) {
|
|
754
|
+
return ArticulationStatus.InvalidHierarchy;
|
|
755
|
+
}
|
|
756
|
+
if ((link.mass ?? 1.0) <= 0.0) {
|
|
757
|
+
return ArticulationStatus.InvalidMass;
|
|
758
|
+
}
|
|
759
|
+
const inertia = link.inertiaDiagonal ?? { x: 0.1, y: 0.1, z: 0.1 };
|
|
760
|
+
if ((inertia.x ?? 0) <= 0 || (inertia.y ?? 0) <= 0 || (inertia.z ?? 0) <= 0) {
|
|
761
|
+
return ArticulationStatus.SingularInertia;
|
|
762
|
+
}
|
|
763
|
+
const joint = link.joint ?? {};
|
|
764
|
+
const jointType = typeof joint.type === 'number' ? joint.type : (ArticulationJointType[joint.type] ?? ArticulationJointType.Fixed);
|
|
765
|
+
const dofCount = joint.dofCount ?? getDefaultDofCount(jointType);
|
|
766
|
+
if (dofCount > ARTICULATION_MAX_JOINT_DOFS) {
|
|
767
|
+
return ArticulationStatus.InvalidJoint;
|
|
768
|
+
}
|
|
769
|
+
totalDofs += dofCount;
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
if (totalDofs > ARTICULATION_MAX_TOTAL_DOFS) {
|
|
773
|
+
return ArticulationStatus.BufferOverflow;
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
const loops = def.loops ?? [];
|
|
777
|
+
if (loops.length > ARTICULATION_MAX_LOOPS) {
|
|
778
|
+
return ArticulationStatus.BufferOverflow;
|
|
779
|
+
}
|
|
780
|
+
for (let k = 0; k < loops.length; k += 1) {
|
|
781
|
+
const loop = loops[k];
|
|
782
|
+
if (loop.linkA >= links.length) return ArticulationStatus.InvalidHierarchy;
|
|
783
|
+
if (loop.linkB !== undefined && loop.linkB >= links.length) return ArticulationStatus.InvalidHierarchy;
|
|
784
|
+
if (loop.linkB !== undefined && loop.linkB >= 0 && loop.linkB === loop.linkA) return ArticulationStatus.InvalidHierarchy;
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
return ArticulationStatus.Success;
|
|
788
|
+
}
|
|
789
|
+
|
|
790
|
+
export function createArticulationDef(config = {}) {
|
|
791
|
+
const rootMode = config.rootMode === 'floating' || config.rootMode === ArticulationRootMode.Floating
|
|
792
|
+
? ArticulationRootMode.Floating
|
|
793
|
+
: ArticulationRootMode.Fixed;
|
|
794
|
+
return {
|
|
795
|
+
rootMode,
|
|
796
|
+
linkCount: config.links?.length ?? 0,
|
|
797
|
+
links: (config.links ?? []).map((link, index) => ({
|
|
798
|
+
linkIndex: link.linkIndex ?? index,
|
|
799
|
+
parentIndex: link.parentIndex !== undefined ? link.parentIndex : (index === 0 ? -1 : 0),
|
|
800
|
+
mass: link.mass ?? 1.0,
|
|
801
|
+
inertiaDiagonal: { ...(link.inertiaDiagonal ?? { x: 0.1, y: 0.1, z: 0.1 }) },
|
|
802
|
+
centerOfMass: { ...(link.centerOfMass ?? { x: 0.0, y: 0.0, z: 0.0 }) },
|
|
803
|
+
linearDamping: link.linearDamping ?? 0.0,
|
|
804
|
+
angularDamping: link.angularDamping ?? 0.0,
|
|
805
|
+
shapeKind: link.shapeKind ?? ShapeKind.Box,
|
|
806
|
+
shapeHalfExtents: { ...(link.shapeHalfExtents ?? { x: 0.1, y: 0.1, z: 0.1 }) },
|
|
807
|
+
joint: {
|
|
808
|
+
type: link.joint?.type ?? ArticulationJointType.Fixed,
|
|
809
|
+
dofCount: link.joint?.dofCount ?? getDefaultDofCount(link.joint?.type ?? ArticulationJointType.Fixed),
|
|
810
|
+
axis1: { ...(link.joint?.axis1 ?? { x: 1.0, y: 0.0, z: 0.0 }) },
|
|
811
|
+
axis2: { ...(link.joint?.axis2 ?? { x: 0.0, y: 1.0, z: 0.0 }) },
|
|
812
|
+
axis3: { ...(link.joint?.axis3 ?? { x: 0.0, y: 0.0, z: 1.0 }) },
|
|
813
|
+
parentAnchor: {
|
|
814
|
+
position: { ...(link.joint?.parentAnchor?.position ?? { x: 0.0, y: 0.0, z: 0.0 }) },
|
|
815
|
+
orientation: { ...(link.joint?.parentAnchor?.orientation ?? { x: 0.0, y: 0.0, z: 0.0, w: 1.0 }) },
|
|
816
|
+
},
|
|
817
|
+
childAnchor: {
|
|
818
|
+
position: { ...(link.joint?.childAnchor?.position ?? { x: 0.0, y: 0.0, z: 0.0 }) },
|
|
819
|
+
orientation: { ...(link.joint?.childAnchor?.orientation ?? { x: 0.0, y: 0.0, z: 0.0, w: 1.0 }) },
|
|
820
|
+
},
|
|
821
|
+
dofConfigs: (link.joint?.dofConfigs ?? []).map((dof) => ({ ...dof })),
|
|
822
|
+
},
|
|
823
|
+
})),
|
|
824
|
+
loopCount: config.loops?.length ?? 0,
|
|
825
|
+
loops: (config.loops ?? []).map((loop) => ({ ...loop })),
|
|
826
|
+
gravity: { ...(config.gravity ?? { x: 0.0, y: -9.81, z: 0.0 }) },
|
|
827
|
+
};
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
export function createArticulationState(def, overrides = {}) {
|
|
831
|
+
const totalDofs = countArticulationDofs(def);
|
|
832
|
+
const linkCount = def.links?.length ?? 0;
|
|
833
|
+
|
|
834
|
+
const q = new Float64Array(totalDofs);
|
|
835
|
+
const u = new Float64Array(totalDofs);
|
|
836
|
+
const udot = new Float64Array(totalDofs);
|
|
837
|
+
const tau = new Float64Array(totalDofs);
|
|
838
|
+
|
|
839
|
+
if (overrides.q) {
|
|
840
|
+
for (let i = 0; i < Math.min(q.length, overrides.q.length); i += 1) {
|
|
841
|
+
q[i] = overrides.q[i];
|
|
842
|
+
}
|
|
843
|
+
}
|
|
844
|
+
if (overrides.u) {
|
|
845
|
+
for (let i = 0; i < Math.min(u.length, overrides.u.length); i += 1) {
|
|
846
|
+
u[i] = overrides.u[i];
|
|
847
|
+
}
|
|
848
|
+
}
|
|
849
|
+
if (overrides.tau) {
|
|
850
|
+
for (let i = 0; i < Math.min(tau.length, overrides.tau.length); i += 1) {
|
|
851
|
+
tau[i] = overrides.tau[i];
|
|
852
|
+
}
|
|
853
|
+
}
|
|
854
|
+
|
|
855
|
+
const linkTransforms = new Array(linkCount);
|
|
856
|
+
const linkVelocities = new Array(linkCount);
|
|
857
|
+
const linkAccelerations = new Array(linkCount);
|
|
858
|
+
|
|
859
|
+
for (let i = 0; i < linkCount; i += 1) {
|
|
860
|
+
linkTransforms[i] = {
|
|
861
|
+
position: { ...(overrides.linkTransforms?.[i]?.position ?? { x: 0.0, y: 0.0, z: 0.0 }) },
|
|
862
|
+
orientation: { ...(overrides.linkTransforms?.[i]?.orientation ?? { x: 0.0, y: 0.0, z: 0.0, w: 1.0 }) },
|
|
863
|
+
};
|
|
864
|
+
linkVelocities[i] = {
|
|
865
|
+
angular: { ...(overrides.linkVelocities?.[i]?.angular ?? { x: 0.0, y: 0.0, z: 0.0 }) },
|
|
866
|
+
linear: { ...(overrides.linkVelocities?.[i]?.linear ?? { x: 0.0, y: 0.0, z: 0.0 }) },
|
|
867
|
+
};
|
|
868
|
+
linkAccelerations[i] = {
|
|
869
|
+
angular: { ...(overrides.linkAccelerations?.[i]?.angular ?? { x: 0.0, y: 0.0, z: 0.0 }) },
|
|
870
|
+
linear: { ...(overrides.linkAccelerations?.[i]?.linear ?? { x: 0.0, y: 0.0, z: 0.0 }) },
|
|
871
|
+
};
|
|
872
|
+
}
|
|
873
|
+
|
|
874
|
+
return {
|
|
875
|
+
rootPose: {
|
|
876
|
+
position: { ...(overrides.rootPose?.position ?? { x: 0.0, y: 0.0, z: 0.0 }) },
|
|
877
|
+
orientation: { ...(overrides.rootPose?.orientation ?? { x: 0.0, y: 0.0, z: 0.0, w: 1.0 }) },
|
|
878
|
+
},
|
|
879
|
+
rootVelocity: {
|
|
880
|
+
angular: { ...(overrides.rootVelocity?.angular ?? { x: 0.0, y: 0.0, z: 0.0 }) },
|
|
881
|
+
linear: { ...(overrides.rootVelocity?.linear ?? { x: 0.0, y: 0.0, z: 0.0 }) },
|
|
882
|
+
},
|
|
883
|
+
rootAcceleration: {
|
|
884
|
+
angular: { ...(overrides.rootAcceleration?.angular ?? { x: 0.0, y: 0.0, z: 0.0 }) },
|
|
885
|
+
linear: { ...(overrides.rootAcceleration?.linear ?? { x: 0.0, y: 0.0, z: 0.0 }) },
|
|
886
|
+
},
|
|
887
|
+
totalDofs,
|
|
888
|
+
q,
|
|
889
|
+
u,
|
|
890
|
+
udot,
|
|
891
|
+
tau,
|
|
892
|
+
linkTransforms,
|
|
893
|
+
linkVelocities,
|
|
894
|
+
linkAccelerations,
|
|
895
|
+
};
|
|
896
|
+
}
|
|
897
|
+
|
|
898
|
+
// ---------------------------------------------------------------------------
|
|
899
|
+
// WASM Marshaled Execution Helpers
|
|
900
|
+
// ---------------------------------------------------------------------------
|
|
901
|
+
|
|
902
|
+
export function articulationForwardKinematics(def, state, instanceOverride = null) {
|
|
903
|
+
const instance = getExtendedInstance(instanceOverride);
|
|
904
|
+
const exp = instance.exports;
|
|
905
|
+
|
|
906
|
+
if (typeof exp.rundot_kinetix_ext_articulation_forward_kinematics !== 'function') {
|
|
907
|
+
throw new Error('KINETIX_ARTICULATION_ENTRY_POINT_MISSING: rundot_kinetix_ext_articulation_forward_kinematics');
|
|
908
|
+
}
|
|
909
|
+
|
|
910
|
+
const totalBytes = DEF_BYTE_SIZE + STATE_BYTE_SIZE;
|
|
911
|
+
return withScratch(instance, totalBytes, (ptr) => {
|
|
912
|
+
const memory = exp.memory;
|
|
913
|
+
const defPtr = ptr;
|
|
914
|
+
const statePtr = ptr + DEF_BYTE_SIZE;
|
|
915
|
+
|
|
916
|
+
new Uint8Array(memory.buffer, ptr, totalBytes).fill(0);
|
|
917
|
+
const view = new DataView(memory.buffer, ptr, totalBytes);
|
|
918
|
+
|
|
919
|
+
marshalArticulationDef(def, view, 0);
|
|
920
|
+
marshalArticulationState(state, view, DEF_BYTE_SIZE);
|
|
921
|
+
|
|
922
|
+
const status = exp.rundot_kinetix_ext_articulation_forward_kinematics(defPtr, statePtr);
|
|
923
|
+
if (status !== 0) {
|
|
924
|
+
throw new Error(`KINETIX_ARTICULATION_FK_FAILED: Status ${status}`);
|
|
925
|
+
}
|
|
926
|
+
|
|
927
|
+
unmarshalArticulationState(view, state, def.links?.length ?? 0, DEF_BYTE_SIZE);
|
|
928
|
+
return status;
|
|
929
|
+
});
|
|
930
|
+
}
|
|
931
|
+
|
|
932
|
+
export function stepArticulation(def, state, dt, loopIterations = 4, instanceOverride = null) {
|
|
933
|
+
const instance = getExtendedInstance(instanceOverride);
|
|
934
|
+
const exp = instance.exports;
|
|
935
|
+
|
|
936
|
+
if (typeof exp.rundot_kinetix_ext_articulation_step !== 'function') {
|
|
937
|
+
throw new Error('KINETIX_ARTICULATION_ENTRY_POINT_MISSING: rundot_kinetix_ext_articulation_step');
|
|
938
|
+
}
|
|
939
|
+
|
|
940
|
+
const totalBytes = DEF_BYTE_SIZE + STATE_BYTE_SIZE;
|
|
941
|
+
return withScratch(instance, totalBytes, (ptr) => {
|
|
942
|
+
const memory = exp.memory;
|
|
943
|
+
const defPtr = ptr;
|
|
944
|
+
const statePtr = ptr + DEF_BYTE_SIZE;
|
|
945
|
+
|
|
946
|
+
new Uint8Array(memory.buffer, ptr, totalBytes).fill(0);
|
|
947
|
+
const view = new DataView(memory.buffer, ptr, totalBytes);
|
|
948
|
+
|
|
949
|
+
marshalArticulationDef(def, view, 0);
|
|
950
|
+
marshalArticulationState(state, view, DEF_BYTE_SIZE);
|
|
951
|
+
|
|
952
|
+
const status = exp.rundot_kinetix_ext_articulation_step(defPtr, statePtr, dt, loopIterations);
|
|
953
|
+
if (status !== 0) {
|
|
954
|
+
throw new Error(`KINETIX_ARTICULATION_STEP_FAILED: Status ${status}`);
|
|
955
|
+
}
|
|
956
|
+
|
|
957
|
+
unmarshalArticulationState(view, state, def.links?.length ?? 0, DEF_BYTE_SIZE);
|
|
958
|
+
return status;
|
|
959
|
+
});
|
|
960
|
+
}
|
|
961
|
+
|
|
962
|
+
export function articulationSolveLoops(def, state, dt, iterations = 4, instanceOverride = null) {
|
|
963
|
+
const instance = getExtendedInstance(instanceOverride);
|
|
964
|
+
const exp = instance.exports;
|
|
965
|
+
|
|
966
|
+
if (typeof exp.rundot_kinetix_ext_articulation_solve_loops !== 'function') {
|
|
967
|
+
throw new Error('KINETIX_ARTICULATION_ENTRY_POINT_MISSING: rundot_kinetix_ext_articulation_solve_loops');
|
|
968
|
+
}
|
|
969
|
+
|
|
970
|
+
const totalBytes = DEF_BYTE_SIZE + STATE_BYTE_SIZE;
|
|
971
|
+
return withScratch(instance, totalBytes, (ptr) => {
|
|
972
|
+
const memory = exp.memory;
|
|
973
|
+
const defPtr = ptr;
|
|
974
|
+
const statePtr = ptr + DEF_BYTE_SIZE;
|
|
975
|
+
|
|
976
|
+
new Uint8Array(memory.buffer, ptr, totalBytes).fill(0);
|
|
977
|
+
const view = new DataView(memory.buffer, ptr, totalBytes);
|
|
978
|
+
|
|
979
|
+
marshalArticulationDef(def, view, 0);
|
|
980
|
+
marshalArticulationState(state, view, DEF_BYTE_SIZE);
|
|
981
|
+
|
|
982
|
+
const status = exp.rundot_kinetix_ext_articulation_solve_loops(defPtr, statePtr, dt, iterations);
|
|
983
|
+
if (status !== 0) {
|
|
984
|
+
throw new Error(`KINETIX_ARTICULATION_SOLVE_LOOPS_FAILED: Status ${status}`);
|
|
985
|
+
}
|
|
986
|
+
|
|
987
|
+
unmarshalArticulationState(view, state, def.links?.length ?? 0, DEF_BYTE_SIZE);
|
|
988
|
+
return status;
|
|
989
|
+
});
|
|
990
|
+
}
|
|
991
|
+
|
|
992
|
+
export function articulationSyncBodies(def, state, outBodies = null, maxBodies = null, instanceOverride = null) {
|
|
993
|
+
const instance = getExtendedInstance(instanceOverride);
|
|
994
|
+
const exp = instance.exports;
|
|
995
|
+
|
|
996
|
+
if (typeof exp.rundot_kinetix_ext_articulation_sync_bodies !== 'function') {
|
|
997
|
+
throw new Error('KINETIX_ARTICULATION_ENTRY_POINT_MISSING: rundot_kinetix_ext_articulation_sync_bodies');
|
|
998
|
+
}
|
|
999
|
+
|
|
1000
|
+
const linkCount = def.links?.length ?? 0;
|
|
1001
|
+
const count = maxBodies ? Math.min(linkCount, maxBodies) : linkCount;
|
|
1002
|
+
if (count <= 0) return [];
|
|
1003
|
+
|
|
1004
|
+
const bodyBytes = count * BODY_BYTE_SIZE;
|
|
1005
|
+
const totalBytes = DEF_BYTE_SIZE + STATE_BYTE_SIZE + bodyBytes;
|
|
1006
|
+
|
|
1007
|
+
return withScratch(instance, totalBytes, (ptr) => {
|
|
1008
|
+
const memory = exp.memory;
|
|
1009
|
+
const defPtr = ptr;
|
|
1010
|
+
const statePtr = ptr + DEF_BYTE_SIZE;
|
|
1011
|
+
const bodyPtr = statePtr + STATE_BYTE_SIZE;
|
|
1012
|
+
|
|
1013
|
+
new Uint8Array(memory.buffer, ptr, totalBytes).fill(0);
|
|
1014
|
+
const view = new DataView(memory.buffer, ptr, totalBytes);
|
|
1015
|
+
|
|
1016
|
+
marshalArticulationDef(def, view, 0);
|
|
1017
|
+
marshalArticulationState(state, view, DEF_BYTE_SIZE);
|
|
1018
|
+
|
|
1019
|
+
const status = exp.rundot_kinetix_ext_articulation_sync_bodies(defPtr, statePtr, bodyPtr, count);
|
|
1020
|
+
if (status !== 0) {
|
|
1021
|
+
throw new Error(`KINETIX_ARTICULATION_SYNC_BODIES_FAILED: Status ${status}`);
|
|
1022
|
+
}
|
|
1023
|
+
|
|
1024
|
+
return unmarshalBodies(view, count, DEF_BYTE_SIZE + STATE_BYTE_SIZE, outBodies);
|
|
1025
|
+
});
|
|
1026
|
+
}
|
|
1027
|
+
|
|
1028
|
+
// ---------------------------------------------------------------------------
|
|
1029
|
+
// High-Level Articulation Class
|
|
1030
|
+
// ---------------------------------------------------------------------------
|
|
1031
|
+
|
|
1032
|
+
export class Articulation {
|
|
1033
|
+
constructor(defConfig, initialState = null, options = {}) {
|
|
1034
|
+
this._def = createArticulationDef(defConfig);
|
|
1035
|
+
const validation = validateArticulation(this._def);
|
|
1036
|
+
if (validation !== ArticulationStatus.Success) {
|
|
1037
|
+
throw new Error(`KINETIX_ARTICULATION_INVALID_DEFINITION: Validation failed with status ${validation}`);
|
|
1038
|
+
}
|
|
1039
|
+
this._state = initialState ? createArticulationState(this._def, initialState) : createArticulationState(this._def);
|
|
1040
|
+
this._instance = options.instance ?? null;
|
|
1041
|
+
|
|
1042
|
+
// Initial forward kinematics to establish consistent link poses
|
|
1043
|
+
articulationForwardKinematics(this._def, this._state, this._instance);
|
|
1044
|
+
}
|
|
1045
|
+
|
|
1046
|
+
get def() {
|
|
1047
|
+
return this._def;
|
|
1048
|
+
}
|
|
1049
|
+
|
|
1050
|
+
get state() {
|
|
1051
|
+
return this._state;
|
|
1052
|
+
}
|
|
1053
|
+
|
|
1054
|
+
get linkCount() {
|
|
1055
|
+
return this._def.links.length;
|
|
1056
|
+
}
|
|
1057
|
+
|
|
1058
|
+
get totalDofs() {
|
|
1059
|
+
return this._state.totalDofs;
|
|
1060
|
+
}
|
|
1061
|
+
|
|
1062
|
+
get q() {
|
|
1063
|
+
return this._state.q;
|
|
1064
|
+
}
|
|
1065
|
+
|
|
1066
|
+
get u() {
|
|
1067
|
+
return this._state.u;
|
|
1068
|
+
}
|
|
1069
|
+
|
|
1070
|
+
get tau() {
|
|
1071
|
+
return this._state.tau;
|
|
1072
|
+
}
|
|
1073
|
+
|
|
1074
|
+
get linkTransforms() {
|
|
1075
|
+
return this._state.linkTransforms;
|
|
1076
|
+
}
|
|
1077
|
+
|
|
1078
|
+
get linkVelocities() {
|
|
1079
|
+
return this._state.linkVelocities;
|
|
1080
|
+
}
|
|
1081
|
+
|
|
1082
|
+
step(dt, loopIterations = 4) {
|
|
1083
|
+
return stepArticulation(this._def, this._state, dt, loopIterations, this._instance);
|
|
1084
|
+
}
|
|
1085
|
+
|
|
1086
|
+
forwardKinematics() {
|
|
1087
|
+
return articulationForwardKinematics(this._def, this._state, this._instance);
|
|
1088
|
+
}
|
|
1089
|
+
|
|
1090
|
+
solveLoops(dt, iterations = 4) {
|
|
1091
|
+
return articulationSolveLoops(this._def, this._state, dt, iterations, this._instance);
|
|
1092
|
+
}
|
|
1093
|
+
|
|
1094
|
+
syncBodies(outBodies = null, maxBodies = null) {
|
|
1095
|
+
return articulationSyncBodies(this._def, this._state, outBodies, maxBodies, this._instance);
|
|
1096
|
+
}
|
|
1097
|
+
|
|
1098
|
+
getLinkPose(linkIndex) {
|
|
1099
|
+
if (linkIndex < 0 || linkIndex >= this._state.linkTransforms.length) return null;
|
|
1100
|
+
const t = this._state.linkTransforms[linkIndex];
|
|
1101
|
+
return {
|
|
1102
|
+
position: { ...t.position },
|
|
1103
|
+
orientation: { ...t.orientation },
|
|
1104
|
+
};
|
|
1105
|
+
}
|
|
1106
|
+
|
|
1107
|
+
getLinkVelocity(linkIndex) {
|
|
1108
|
+
if (linkIndex < 0 || linkIndex >= this._state.linkVelocities.length) return null;
|
|
1109
|
+
const v = this._state.linkVelocities[linkIndex];
|
|
1110
|
+
return {
|
|
1111
|
+
angular: { ...v.angular },
|
|
1112
|
+
linear: { ...v.linear },
|
|
1113
|
+
};
|
|
1114
|
+
}
|
|
1115
|
+
|
|
1116
|
+
setJointPosition(dofIndex, value) {
|
|
1117
|
+
if (dofIndex >= 0 && dofIndex < this._state.q.length) {
|
|
1118
|
+
this._state.q[dofIndex] = value;
|
|
1119
|
+
}
|
|
1120
|
+
}
|
|
1121
|
+
|
|
1122
|
+
setJointVelocity(dofIndex, value) {
|
|
1123
|
+
if (dofIndex >= 0 && dofIndex < this._state.u.length) {
|
|
1124
|
+
this._state.u[dofIndex] = value;
|
|
1125
|
+
}
|
|
1126
|
+
}
|
|
1127
|
+
|
|
1128
|
+
applyJointForce(dofIndex, force) {
|
|
1129
|
+
if (dofIndex >= 0 && dofIndex < this._state.tau.length) {
|
|
1130
|
+
this._state.tau[dofIndex] = force;
|
|
1131
|
+
}
|
|
1132
|
+
}
|
|
1133
|
+
|
|
1134
|
+
resetForces() {
|
|
1135
|
+
this._state.tau.fill(0.0);
|
|
1136
|
+
}
|
|
1137
|
+
}
|
|
1138
|
+
|
|
1139
|
+
export function createArticulation(def, initialState = null, options = {}) {
|
|
1140
|
+
return new Articulation(def, initialState, options);
|
|
1141
|
+
}
|