@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,1434 @@
|
|
|
1
|
+
#include "articulation.hpp"
|
|
2
|
+
|
|
3
|
+
#include <algorithm>
|
|
4
|
+
#include <array>
|
|
5
|
+
#include <cmath>
|
|
6
|
+
#include <cstdint>
|
|
7
|
+
#include <span>
|
|
8
|
+
|
|
9
|
+
namespace rundot::kinetix::physics {
|
|
10
|
+
|
|
11
|
+
namespace {
|
|
12
|
+
|
|
13
|
+
inline double artSqrt(double value) {
|
|
14
|
+
if (value <= 0.0) return 0.0;
|
|
15
|
+
return std::sqrt(value);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
inline double artClamp(double value, double minVal, double maxVal) {
|
|
19
|
+
if (value < minVal) return minVal;
|
|
20
|
+
if (value > maxVal) return maxVal;
|
|
21
|
+
return value;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// 3x3 Matrix helpers
|
|
25
|
+
struct Matrix3x3 {
|
|
26
|
+
std::array<std::array<double, 3>, 3> m{{{0.0}}};
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
inline Matrix3x3 quatToMatrix(const ArticulationQuat& q) {
|
|
30
|
+
const double xx = q.x * q.x;
|
|
31
|
+
const double yy = q.y * q.y;
|
|
32
|
+
const double zz = q.z * q.z;
|
|
33
|
+
const double xy = q.x * q.y;
|
|
34
|
+
const double xz = q.x * q.z;
|
|
35
|
+
const double yz = q.y * q.z;
|
|
36
|
+
const double wx = q.w * q.x;
|
|
37
|
+
const double wy = q.w * q.y;
|
|
38
|
+
const double wz = q.w * q.z;
|
|
39
|
+
|
|
40
|
+
Matrix3x3 R{};
|
|
41
|
+
R.m[0][0] = 1.0 - 2.0 * (yy + zz);
|
|
42
|
+
R.m[0][1] = 2.0 * (xy - wz);
|
|
43
|
+
R.m[0][2] = 2.0 * (xz + wy);
|
|
44
|
+
|
|
45
|
+
R.m[1][0] = 2.0 * (xy + wz);
|
|
46
|
+
R.m[1][1] = 1.0 - 2.0 * (xx + zz);
|
|
47
|
+
R.m[1][2] = 2.0 * (yz - wx);
|
|
48
|
+
|
|
49
|
+
R.m[2][0] = 2.0 * (xz - wy);
|
|
50
|
+
R.m[2][1] = 2.0 * (yz + wx);
|
|
51
|
+
R.m[2][2] = 1.0 - 2.0 * (xx + yy);
|
|
52
|
+
return R;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
inline ArticulationVec3 mulMatVec(const Matrix3x3& R, const ArticulationVec3& v) {
|
|
56
|
+
return {
|
|
57
|
+
R.m[0][0] * v.x + R.m[0][1] * v.y + R.m[0][2] * v.z,
|
|
58
|
+
R.m[1][0] * v.x + R.m[1][1] * v.y + R.m[1][2] * v.z,
|
|
59
|
+
R.m[2][0] * v.x + R.m[2][1] * v.y + R.m[2][2] * v.z
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
inline ArticulationVec3 mulMatTransVec(const Matrix3x3& R, const ArticulationVec3& v) {
|
|
64
|
+
return {
|
|
65
|
+
R.m[0][0] * v.x + R.m[1][0] * v.y + R.m[2][0] * v.z,
|
|
66
|
+
R.m[0][1] * v.x + R.m[1][1] * v.y + R.m[2][1] * v.z,
|
|
67
|
+
R.m[0][2] * v.x + R.m[1][2] * v.y + R.m[2][2] * v.z
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
inline double dotSpatial(const ArticulationSpatialVec& a, const ArticulationSpatialVec& b) {
|
|
72
|
+
return dotVec3(a.angular, b.angular) + dotVec3(a.linear, b.linear);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
inline ArticulationSpatialVec addSpatialVec(const ArticulationSpatialVec& a, const ArticulationSpatialVec& b) {
|
|
76
|
+
return { addVec3(a.angular, b.angular), addVec3(a.linear, b.linear) };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
inline ArticulationSpatialVec subSpatialVec(const ArticulationSpatialVec& a, const ArticulationSpatialVec& b) {
|
|
80
|
+
return { subVec3(a.angular, b.angular), subVec3(a.linear, b.linear) };
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
inline ArticulationSpatialVec scaleSpatialVec(const ArticulationSpatialVec& v, double s) {
|
|
84
|
+
return { scaleVec3(v.angular, s), scaleVec3(v.linear, s) };
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
inline ArticulationSpatialMatrix addSpatialMatrix(
|
|
88
|
+
const ArticulationSpatialMatrix& a,
|
|
89
|
+
const ArticulationSpatialMatrix& b) {
|
|
90
|
+
ArticulationSpatialMatrix out{};
|
|
91
|
+
for (std::size_t r = 0; r < 6; ++r) {
|
|
92
|
+
for (std::size_t c = 0; c < 6; ++c) {
|
|
93
|
+
out.data[r][c] = a.data[r][c] + b.data[r][c];
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return out;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
} // namespace
|
|
100
|
+
|
|
101
|
+
// ---------------------------------------------------------------------------
|
|
102
|
+
// Deterministic 3D Vector Math
|
|
103
|
+
// ---------------------------------------------------------------------------
|
|
104
|
+
|
|
105
|
+
ArticulationVec3 addVec3(const ArticulationVec3& a, const ArticulationVec3& b) {
|
|
106
|
+
return {a.x + b.x, a.y + b.y, a.z + b.z};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
ArticulationVec3 subVec3(const ArticulationVec3& a, const ArticulationVec3& b) {
|
|
110
|
+
return {a.x - b.x, a.y - b.y, a.z - b.z};
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
ArticulationVec3 scaleVec3(const ArticulationVec3& v, double s) {
|
|
114
|
+
return {v.x * s, v.y * s, v.z * s};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
double dotVec3(const ArticulationVec3& a, const ArticulationVec3& b) {
|
|
118
|
+
return a.x * b.x + a.y * b.y + a.z * b.z;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
ArticulationVec3 crossVec3(const ArticulationVec3& a, const ArticulationVec3& b) {
|
|
122
|
+
return {
|
|
123
|
+
a.y * b.z - a.z * b.y,
|
|
124
|
+
a.z * b.x - a.x * b.z,
|
|
125
|
+
a.x * b.y - a.y * b.x
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
double lengthSquaredVec3(const ArticulationVec3& v) {
|
|
130
|
+
return dotVec3(v, v);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
double lengthVec3(const ArticulationVec3& v) {
|
|
134
|
+
return artSqrt(lengthSquaredVec3(v));
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
ArticulationVec3 normalizeVec3(const ArticulationVec3& v) {
|
|
138
|
+
const double len = lengthVec3(v);
|
|
139
|
+
if (len <= 1e-15) return {0.0, 0.0, 0.0};
|
|
140
|
+
const double inv = 1.0 / len;
|
|
141
|
+
return {v.x * inv, v.y * inv, v.z * inv};
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// ---------------------------------------------------------------------------
|
|
145
|
+
// Deterministic Quaternion Math
|
|
146
|
+
// ---------------------------------------------------------------------------
|
|
147
|
+
|
|
148
|
+
ArticulationQuat quatIdentity() {
|
|
149
|
+
return {0.0, 0.0, 0.0, 1.0};
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
ArticulationQuat quatMultiply(const ArticulationQuat& q1, const ArticulationQuat& q2) {
|
|
153
|
+
return {
|
|
154
|
+
q1.w * q2.x + q1.x * q2.w + q1.y * q2.z - q1.z * q2.y,
|
|
155
|
+
q1.w * q2.y - q1.x * q2.z + q1.y * q2.w + q1.z * q2.x,
|
|
156
|
+
q1.w * q2.z + q1.x * q2.y - q1.y * q2.x + q1.z * q2.w,
|
|
157
|
+
q1.w * q2.w - q1.x * q2.x - q1.y * q2.y - q1.z * q2.z
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
ArticulationQuat quatConjugate(const ArticulationQuat& q) {
|
|
162
|
+
return {-q.x, -q.y, -q.z, q.w};
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
ArticulationQuat quatNormalize(const ArticulationQuat& q) {
|
|
166
|
+
const double lenSq = q.x * q.x + q.y * q.y + q.z * q.z + q.w * q.w;
|
|
167
|
+
if (lenSq <= 1e-15) return quatIdentity();
|
|
168
|
+
const double invLen = 1.0 / artSqrt(lenSq);
|
|
169
|
+
return {q.x * invLen, q.y * invLen, q.z * invLen, q.w * invLen};
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
ArticulationVec3 rotateByQuat(const ArticulationQuat& q, const ArticulationVec3& v) {
|
|
173
|
+
const ArticulationVec3 u{q.x, q.y, q.z};
|
|
174
|
+
const double s = q.w;
|
|
175
|
+
const ArticulationVec3 uxv = crossVec3(u, v);
|
|
176
|
+
const ArticulationVec3 uxuxv = crossVec3(u, uxv);
|
|
177
|
+
return addVec3(v, scaleVec3(addVec3(scaleVec3(uxv, s), uxuxv), 2.0));
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
ArticulationVec3 invRotateByQuat(const ArticulationQuat& q, const ArticulationVec3& v) {
|
|
181
|
+
return rotateByQuat(quatConjugate(q), v);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
ArticulationQuat quatFromAxisAngle(const ArticulationVec3& axis, double angle) {
|
|
185
|
+
const ArticulationVec3 n = normalizeVec3(axis);
|
|
186
|
+
const double halfAngle = angle * 0.5;
|
|
187
|
+
const double s = std::sin(halfAngle);
|
|
188
|
+
return {n.x * s, n.y * s, n.z * s, std::cos(halfAngle)};
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// ---------------------------------------------------------------------------
|
|
192
|
+
// Transform Utilities
|
|
193
|
+
// ---------------------------------------------------------------------------
|
|
194
|
+
|
|
195
|
+
ArticulationVec3 transformPoint(const ArticulationTransform& t, const ArticulationVec3& p) {
|
|
196
|
+
return addVec3(rotateByQuat(t.orientation, p), t.position);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
ArticulationVec3 invTransformPoint(const ArticulationTransform& t, const ArticulationVec3& p) {
|
|
200
|
+
return invRotateByQuat(t.orientation, subVec3(p, t.position));
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
ArticulationTransform combineTransforms(
|
|
204
|
+
const ArticulationTransform& parent,
|
|
205
|
+
const ArticulationTransform& local) {
|
|
206
|
+
return {
|
|
207
|
+
addVec3(rotateByQuat(parent.orientation, local.position), parent.position),
|
|
208
|
+
quatNormalize(quatMultiply(parent.orientation, local.orientation))
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
ArticulationTransform relativeTransform(
|
|
213
|
+
const ArticulationTransform& parent,
|
|
214
|
+
const ArticulationTransform& child) {
|
|
215
|
+
const ArticulationQuat invParentRot = quatConjugate(parent.orientation);
|
|
216
|
+
return {
|
|
217
|
+
rotateByQuat(invParentRot, subVec3(child.position, parent.position)),
|
|
218
|
+
quatNormalize(quatMultiply(invParentRot, child.orientation))
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// ---------------------------------------------------------------------------
|
|
223
|
+
// Spatial Algebra
|
|
224
|
+
// ---------------------------------------------------------------------------
|
|
225
|
+
|
|
226
|
+
ArticulationSpatialVec transformSpatialMotion(
|
|
227
|
+
const ArticulationTransform& X,
|
|
228
|
+
const ArticulationSpatialVec& v) {
|
|
229
|
+
const Matrix3x3 R = quatToMatrix(X.orientation);
|
|
230
|
+
const ArticulationVec3 p = X.position;
|
|
231
|
+
// \omega' = R^T * \omega
|
|
232
|
+
const ArticulationVec3 ang = mulMatTransVec(R, v.angular);
|
|
233
|
+
// v' = R^T * (v - p \times \omega) = R^T * (v + \omega \times p)
|
|
234
|
+
const ArticulationVec3 lin = mulMatTransVec(R, addVec3(v.linear, crossVec3(v.angular, p)));
|
|
235
|
+
return {ang, lin};
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
ArticulationSpatialVec transformSpatialForce(
|
|
239
|
+
const ArticulationTransform& X,
|
|
240
|
+
const ArticulationSpatialVec& f) {
|
|
241
|
+
const Matrix3x3 R = quatToMatrix(X.orientation);
|
|
242
|
+
const ArticulationVec3 p = X.position;
|
|
243
|
+
// f_{parent} = R * f_{child}
|
|
244
|
+
const ArticulationVec3 lin = mulMatVec(R, f.linear);
|
|
245
|
+
// \tau_{parent} = R * \tau_{child} + p \times f_{parent}
|
|
246
|
+
const ArticulationVec3 ang = addVec3(mulMatVec(R, f.angular), crossVec3(p, lin));
|
|
247
|
+
return {ang, lin};
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
ArticulationSpatialVec spatialCrossMotion(
|
|
251
|
+
const ArticulationSpatialVec& v1,
|
|
252
|
+
const ArticulationSpatialVec& v2) {
|
|
253
|
+
return {
|
|
254
|
+
crossVec3(v1.angular, v2.angular),
|
|
255
|
+
addVec3(crossVec3(v1.angular, v2.linear), crossVec3(v1.linear, v2.angular))
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
ArticulationSpatialVec spatialCrossForce(
|
|
260
|
+
const ArticulationSpatialVec& v,
|
|
261
|
+
const ArticulationSpatialVec& f) {
|
|
262
|
+
return {
|
|
263
|
+
addVec3(crossVec3(v.angular, f.angular), crossVec3(v.linear, f.linear)),
|
|
264
|
+
crossVec3(v.angular, f.linear)
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
ArticulationSpatialMatrix makeSpatialInertia(
|
|
269
|
+
double mass,
|
|
270
|
+
const ArticulationVec3& com,
|
|
271
|
+
const ArticulationVec3& diagInertia) {
|
|
272
|
+
ArticulationSpatialMatrix M{};
|
|
273
|
+
const double cx = com.x;
|
|
274
|
+
const double cy = com.y;
|
|
275
|
+
const double cz = com.z;
|
|
276
|
+
|
|
277
|
+
// Rotational inertia about link origin via parallel axis theorem:
|
|
278
|
+
// I_o = I_com + m * (c^T c * E - c c^T)
|
|
279
|
+
const double Ioxx = diagInertia.x + mass * (cy * cy + cz * cz);
|
|
280
|
+
const double Ioyy = diagInertia.y + mass * (cx * cx + cz * cz);
|
|
281
|
+
const double Iozz = diagInertia.z + mass * (cx * cx + cy * cy);
|
|
282
|
+
const double Ioxy = -mass * cx * cy;
|
|
283
|
+
const double Ioxz = -mass * cx * cz;
|
|
284
|
+
const double Ioyz = -mass * cy * cz;
|
|
285
|
+
|
|
286
|
+
M.data[0][0] = Ioxx; M.data[0][1] = Ioxy; M.data[0][2] = Ioxz;
|
|
287
|
+
M.data[1][0] = Ioxy; M.data[1][1] = Ioyy; M.data[1][2] = Ioyz;
|
|
288
|
+
M.data[2][0] = Ioxz; M.data[2][1] = Ioyz; M.data[2][2] = Iozz;
|
|
289
|
+
|
|
290
|
+
// Cross block: m * [c]_\times
|
|
291
|
+
M.data[0][3] = 0.0; M.data[0][4] = -mass * cz; M.data[0][5] = mass * cy;
|
|
292
|
+
M.data[1][3] = mass * cz; M.data[1][4] = 0.0; M.data[1][5] = -mass * cx;
|
|
293
|
+
M.data[2][3] = -mass * cy; M.data[2][4] = mass * cx; M.data[2][5] = 0.0;
|
|
294
|
+
|
|
295
|
+
// Transpose of cross block: (m * [c]_\times)^T = -m * [c]_\times
|
|
296
|
+
M.data[3][0] = M.data[0][3]; M.data[3][1] = M.data[1][3]; M.data[3][2] = M.data[2][3];
|
|
297
|
+
M.data[4][0] = M.data[0][4]; M.data[4][1] = M.data[1][4]; M.data[4][2] = M.data[2][4];
|
|
298
|
+
M.data[5][0] = M.data[0][5]; M.data[5][1] = M.data[1][5]; M.data[5][2] = M.data[2][5];
|
|
299
|
+
|
|
300
|
+
// Mass block: m * I_3
|
|
301
|
+
M.data[3][3] = mass; M.data[3][4] = 0.0; M.data[3][5] = 0.0;
|
|
302
|
+
M.data[4][3] = 0.0; M.data[4][4] = mass; M.data[4][5] = 0.0;
|
|
303
|
+
M.data[5][3] = 0.0; M.data[5][4] = 0.0; M.data[5][5] = mass;
|
|
304
|
+
|
|
305
|
+
return M;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
ArticulationSpatialMatrix transformSpatialMatrix(
|
|
309
|
+
const ArticulationTransform& X,
|
|
310
|
+
const ArticulationSpatialMatrix& I) {
|
|
311
|
+
// Transformed matrix M' = X^T * I * X
|
|
312
|
+
// Evaluated deterministically column by column
|
|
313
|
+
ArticulationSpatialMatrix MPrime{};
|
|
314
|
+
for (std::size_t col = 0; col < 6; ++col) {
|
|
315
|
+
ArticulationSpatialVec e{};
|
|
316
|
+
if (col == 0) e.angular.x = 1.0;
|
|
317
|
+
else if (col == 1) e.angular.y = 1.0;
|
|
318
|
+
else if (col == 2) e.angular.z = 1.0;
|
|
319
|
+
else if (col == 3) e.linear.x = 1.0;
|
|
320
|
+
else if (col == 4) e.linear.y = 1.0;
|
|
321
|
+
else if (col == 5) e.linear.z = 1.0;
|
|
322
|
+
|
|
323
|
+
const ArticulationSpatialVec Xe = transformSpatialMotion(X, e);
|
|
324
|
+
const ArticulationSpatialVec IXe = multiplySpatialMatrix(I, Xe);
|
|
325
|
+
const ArticulationSpatialVec res = transformSpatialForce(X, IXe);
|
|
326
|
+
|
|
327
|
+
MPrime.data[0][col] = res.angular.x;
|
|
328
|
+
MPrime.data[1][col] = res.angular.y;
|
|
329
|
+
MPrime.data[2][col] = res.angular.z;
|
|
330
|
+
MPrime.data[3][col] = res.linear.x;
|
|
331
|
+
MPrime.data[4][col] = res.linear.y;
|
|
332
|
+
MPrime.data[5][col] = res.linear.z;
|
|
333
|
+
}
|
|
334
|
+
return MPrime;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
ArticulationSpatialVec multiplySpatialMatrix(
|
|
338
|
+
const ArticulationSpatialMatrix& M,
|
|
339
|
+
const ArticulationSpatialVec& v) {
|
|
340
|
+
const std::array<double, 6> va{
|
|
341
|
+
v.angular.x, v.angular.y, v.angular.z,
|
|
342
|
+
v.linear.x, v.linear.y, v.linear.z
|
|
343
|
+
};
|
|
344
|
+
std::array<double, 6> outa{};
|
|
345
|
+
for (std::size_t i = 0; i < 6; ++i) {
|
|
346
|
+
double sum = 0.0;
|
|
347
|
+
for (std::size_t j = 0; j < 6; ++j) {
|
|
348
|
+
sum += M.data[i][j] * va[j];
|
|
349
|
+
}
|
|
350
|
+
outa[i] = sum;
|
|
351
|
+
}
|
|
352
|
+
return {
|
|
353
|
+
ArticulationVec3{outa[0], outa[1], outa[2]},
|
|
354
|
+
ArticulationVec3{outa[3], outa[4], outa[5]}
|
|
355
|
+
};
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
bool invertSpatialMatrix6x6(
|
|
359
|
+
const ArticulationSpatialMatrix& inM,
|
|
360
|
+
ArticulationSpatialMatrix& outM) {
|
|
361
|
+
// Cholesky decomposition L * L^T = inM with diagonal regularization
|
|
362
|
+
std::array<std::array<double, 6>, 6> L{{{0.0}}};
|
|
363
|
+
constexpr double kMinDiag = 1e-11;
|
|
364
|
+
|
|
365
|
+
for (std::size_t i = 0; i < 6; ++i) {
|
|
366
|
+
for (std::size_t j = 0; j <= i; ++j) {
|
|
367
|
+
double sum = inM.data[i][j];
|
|
368
|
+
for (std::size_t k = 0; k < j; ++k) {
|
|
369
|
+
sum -= L[i][k] * L[j][k];
|
|
370
|
+
}
|
|
371
|
+
if (i == j) {
|
|
372
|
+
if (sum < kMinDiag) sum = kMinDiag;
|
|
373
|
+
L[i][j] = artSqrt(sum);
|
|
374
|
+
} else {
|
|
375
|
+
L[i][j] = sum / L[j][j];
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
// Invert L (lower triangular) into Linv
|
|
381
|
+
std::array<std::array<double, 6>, 6> Linv{{{0.0}}};
|
|
382
|
+
for (std::size_t i = 0; i < 6; ++i) {
|
|
383
|
+
Linv[i][i] = 1.0 / L[i][i];
|
|
384
|
+
for (std::size_t j = 0; j < i; ++j) {
|
|
385
|
+
double sum = 0.0;
|
|
386
|
+
for (std::size_t k = j; k < i; ++k) {
|
|
387
|
+
sum += L[i][k] * Linv[k][j];
|
|
388
|
+
}
|
|
389
|
+
Linv[i][j] = -sum / L[i][i];
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
// outM = Linv^T * Linv
|
|
394
|
+
for (std::size_t i = 0; i < 6; ++i) {
|
|
395
|
+
for (std::size_t j = 0; j < 6; ++j) {
|
|
396
|
+
double sum = 0.0;
|
|
397
|
+
for (std::size_t k = 0; k < 6; ++k) {
|
|
398
|
+
sum += Linv[k][i] * Linv[k][j];
|
|
399
|
+
}
|
|
400
|
+
outM.data[i][j] = sum;
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
return true;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
// ---------------------------------------------------------------------------
|
|
407
|
+
// Core Articulation API
|
|
408
|
+
// ---------------------------------------------------------------------------
|
|
409
|
+
|
|
410
|
+
ArticulationStatus articulationValidate(const ArticulationDef& def) {
|
|
411
|
+
if (def.linkCount == 0 || def.linkCount > kArticulationMaxLinks) {
|
|
412
|
+
return ArticulationStatus::InvalidLinkCount;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
// Root must have parentIndex -1
|
|
416
|
+
if (def.links[0].parentIndex != -1) {
|
|
417
|
+
return ArticulationStatus::InvalidHierarchy;
|
|
418
|
+
}
|
|
419
|
+
if (def.links[0].mass <= 0.0) {
|
|
420
|
+
return ArticulationStatus::InvalidMass;
|
|
421
|
+
}
|
|
422
|
+
if (def.links[0].inertiaDiagonal.x <= 0.0 ||
|
|
423
|
+
def.links[0].inertiaDiagonal.y <= 0.0 ||
|
|
424
|
+
def.links[0].inertiaDiagonal.z <= 0.0) {
|
|
425
|
+
return ArticulationStatus::SingularInertia;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
std::uint32_t totalDofs = (def.rootMode == ArticulationRootMode::Floating) ? 6 : 0;
|
|
429
|
+
|
|
430
|
+
for (std::uint32_t i = 1; i < def.linkCount; ++i) {
|
|
431
|
+
const auto& link = def.links[i];
|
|
432
|
+
if (link.parentIndex < 0 || link.parentIndex >= static_cast<std::int32_t>(i)) {
|
|
433
|
+
return ArticulationStatus::InvalidHierarchy;
|
|
434
|
+
}
|
|
435
|
+
if (link.mass <= 0.0) {
|
|
436
|
+
return ArticulationStatus::InvalidMass;
|
|
437
|
+
}
|
|
438
|
+
if (link.inertiaDiagonal.x <= 0.0 ||
|
|
439
|
+
link.inertiaDiagonal.y <= 0.0 ||
|
|
440
|
+
link.inertiaDiagonal.z <= 0.0) {
|
|
441
|
+
return ArticulationStatus::SingularInertia;
|
|
442
|
+
}
|
|
443
|
+
if (link.joint.dofCount > kArticulationMaxJointDofs) {
|
|
444
|
+
return ArticulationStatus::InvalidJoint;
|
|
445
|
+
}
|
|
446
|
+
totalDofs += link.joint.dofCount;
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
if (totalDofs > kArticulationMaxTotalDofs) {
|
|
450
|
+
return ArticulationStatus::BufferOverflow;
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
if (def.loopCount > kArticulationMaxLoops) {
|
|
454
|
+
return ArticulationStatus::BufferOverflow;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
for (std::uint32_t k = 0; k < def.loopCount; ++k) {
|
|
458
|
+
const auto& loop = def.loops[k];
|
|
459
|
+
if (loop.linkA >= def.linkCount) {
|
|
460
|
+
return ArticulationStatus::InvalidHierarchy;
|
|
461
|
+
}
|
|
462
|
+
if (loop.linkB >= static_cast<std::int32_t>(def.linkCount)) {
|
|
463
|
+
return ArticulationStatus::InvalidHierarchy;
|
|
464
|
+
}
|
|
465
|
+
if (loop.linkB >= 0 && static_cast<std::uint32_t>(loop.linkB) == loop.linkA) {
|
|
466
|
+
return ArticulationStatus::InvalidHierarchy;
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
return ArticulationStatus::Success;
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
std::uint32_t articulationCountDofs(const ArticulationDef& def) {
|
|
474
|
+
std::uint32_t dofs = (def.rootMode == ArticulationRootMode::Floating) ? 6 : 0;
|
|
475
|
+
for (std::uint32_t i = 1; i < def.linkCount; ++i) {
|
|
476
|
+
dofs += def.links[i].joint.dofCount;
|
|
477
|
+
}
|
|
478
|
+
return dofs;
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
void articulationGetDofOffsets(const ArticulationDef& def, std::span<std::uint32_t> outOffsets) {
|
|
482
|
+
if (outOffsets.empty()) return;
|
|
483
|
+
std::uint32_t currentDof = (def.rootMode == ArticulationRootMode::Floating) ? 6 : 0;
|
|
484
|
+
outOffsets[0] = 0;
|
|
485
|
+
for (std::uint32_t i = 1; i < def.linkCount && i < outOffsets.size(); ++i) {
|
|
486
|
+
outOffsets[i] = currentDof;
|
|
487
|
+
currentDof += def.links[i].joint.dofCount;
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
namespace {
|
|
492
|
+
|
|
493
|
+
void evaluateJointMotion(
|
|
494
|
+
const ArticulationJointDef& joint,
|
|
495
|
+
const double* qPtr,
|
|
496
|
+
const double* uPtr,
|
|
497
|
+
ArticulationTransform& outTJoint,
|
|
498
|
+
ArticulationSpatialVec& outVRel,
|
|
499
|
+
std::array<ArticulationSpatialVec, kArticulationMaxJointDofs>& outS) {
|
|
500
|
+
outTJoint = {ArticulationVec3{0.0, 0.0, 0.0}, quatIdentity()};
|
|
501
|
+
outVRel = {ArticulationVec3{0.0, 0.0, 0.0}, ArticulationVec3{0.0, 0.0, 0.0}};
|
|
502
|
+
|
|
503
|
+
switch (joint.type) {
|
|
504
|
+
case ArticulationJointType::Fixed:
|
|
505
|
+
break;
|
|
506
|
+
|
|
507
|
+
case ArticulationJointType::Revolute: {
|
|
508
|
+
const ArticulationVec3 axis = normalizeVec3(joint.axis1);
|
|
509
|
+
const double q0 = qPtr ? qPtr[0] : 0.0;
|
|
510
|
+
const double u0 = uPtr ? uPtr[0] : 0.0;
|
|
511
|
+
outTJoint.orientation = quatFromAxisAngle(axis, q0);
|
|
512
|
+
outS[0] = {axis, ArticulationVec3{0.0, 0.0, 0.0}};
|
|
513
|
+
outVRel = scaleSpatialVec(outS[0], u0);
|
|
514
|
+
break;
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
case ArticulationJointType::Prismatic: {
|
|
518
|
+
const ArticulationVec3 axis = normalizeVec3(joint.axis1);
|
|
519
|
+
const double q0 = qPtr ? qPtr[0] : 0.0;
|
|
520
|
+
const double u0 = uPtr ? uPtr[0] : 0.0;
|
|
521
|
+
outTJoint.position = scaleVec3(axis, q0);
|
|
522
|
+
outS[0] = {ArticulationVec3{0.0, 0.0, 0.0}, axis};
|
|
523
|
+
outVRel = scaleSpatialVec(outS[0], u0);
|
|
524
|
+
break;
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
case ArticulationJointType::Cylindrical: {
|
|
528
|
+
const ArticulationVec3 axis = normalizeVec3(joint.axis1);
|
|
529
|
+
const double q0 = qPtr ? qPtr[0] : 0.0;
|
|
530
|
+
const double q1 = qPtr ? qPtr[1] : 0.0;
|
|
531
|
+
const double u0 = uPtr ? uPtr[0] : 0.0;
|
|
532
|
+
const double u1 = uPtr ? uPtr[1] : 0.0;
|
|
533
|
+
outTJoint.orientation = quatFromAxisAngle(axis, q0);
|
|
534
|
+
outTJoint.position = scaleVec3(axis, q1);
|
|
535
|
+
outS[0] = {axis, ArticulationVec3{0.0, 0.0, 0.0}};
|
|
536
|
+
outS[1] = {ArticulationVec3{0.0, 0.0, 0.0}, axis};
|
|
537
|
+
outVRel = addSpatialVec(scaleSpatialVec(outS[0], u0), scaleSpatialVec(outS[1], u1));
|
|
538
|
+
break;
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
case ArticulationJointType::Universal: {
|
|
542
|
+
const ArticulationVec3 e1 = normalizeVec3(joint.axis1);
|
|
543
|
+
const ArticulationVec3 e2 = normalizeVec3(joint.axis2);
|
|
544
|
+
const double q0 = qPtr ? qPtr[0] : 0.0;
|
|
545
|
+
const double q1 = qPtr ? qPtr[1] : 0.0;
|
|
546
|
+
const double u0 = uPtr ? uPtr[0] : 0.0;
|
|
547
|
+
const double u1 = uPtr ? uPtr[1] : 0.0;
|
|
548
|
+
const ArticulationQuat rot1 = quatFromAxisAngle(e1, q0);
|
|
549
|
+
const ArticulationQuat rot2 = quatFromAxisAngle(e2, q1);
|
|
550
|
+
outTJoint.orientation = quatMultiply(rot1, rot2);
|
|
551
|
+
outS[0] = {e1, ArticulationVec3{0.0, 0.0, 0.0}};
|
|
552
|
+
outS[1] = {rotateByQuat(rot1, e2), ArticulationVec3{0.0, 0.0, 0.0}};
|
|
553
|
+
outVRel = addSpatialVec(scaleSpatialVec(outS[0], u0), scaleSpatialVec(outS[1], u1));
|
|
554
|
+
break;
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
case ArticulationJointType::Spherical: {
|
|
558
|
+
const double q0 = qPtr ? qPtr[0] : 0.0;
|
|
559
|
+
const double q1 = qPtr ? qPtr[1] : 0.0;
|
|
560
|
+
const double q2 = qPtr ? qPtr[2] : 0.0;
|
|
561
|
+
const double u0 = uPtr ? uPtr[0] : 0.0;
|
|
562
|
+
const double u1 = uPtr ? uPtr[1] : 0.0;
|
|
563
|
+
const double u2 = uPtr ? uPtr[2] : 0.0;
|
|
564
|
+
const ArticulationQuat rx = quatFromAxisAngle({1.0, 0.0, 0.0}, q0);
|
|
565
|
+
const ArticulationQuat ry = quatFromAxisAngle({0.0, 1.0, 0.0}, q1);
|
|
566
|
+
const ArticulationQuat rz = quatFromAxisAngle({0.0, 0.0, 1.0}, q2);
|
|
567
|
+
outTJoint.orientation = quatMultiply(rx, quatMultiply(ry, rz));
|
|
568
|
+
outS[0] = {ArticulationVec3{1.0, 0.0, 0.0}, ArticulationVec3{0.0, 0.0, 0.0}};
|
|
569
|
+
outS[1] = {rotateByQuat(rx, {0.0, 1.0, 0.0}), ArticulationVec3{0.0, 0.0, 0.0}};
|
|
570
|
+
outS[2] = {rotateByQuat(quatMultiply(rx, ry), {0.0, 0.0, 1.0}), ArticulationVec3{0.0, 0.0, 0.0}};
|
|
571
|
+
outVRel = addSpatialVec(
|
|
572
|
+
scaleSpatialVec(outS[0], u0),
|
|
573
|
+
addSpatialVec(scaleSpatialVec(outS[1], u1), scaleSpatialVec(outS[2], u2)));
|
|
574
|
+
break;
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
case ArticulationJointType::Planar: {
|
|
578
|
+
const ArticulationVec3 e1 = normalizeVec3(joint.axis1);
|
|
579
|
+
const ArticulationVec3 e2 = normalizeVec3(joint.axis2);
|
|
580
|
+
const ArticulationVec3 e3 = normalizeVec3(crossVec3(e1, e2));
|
|
581
|
+
const double q0 = qPtr ? qPtr[0] : 0.0;
|
|
582
|
+
const double q1 = qPtr ? qPtr[1] : 0.0;
|
|
583
|
+
const double q2 = qPtr ? qPtr[2] : 0.0;
|
|
584
|
+
const double u0 = uPtr ? uPtr[0] : 0.0;
|
|
585
|
+
const double u1 = uPtr ? uPtr[1] : 0.0;
|
|
586
|
+
const double u2 = uPtr ? uPtr[2] : 0.0;
|
|
587
|
+
outTJoint.position = addVec3(scaleVec3(e1, q0), scaleVec3(e2, q1));
|
|
588
|
+
outTJoint.orientation = quatFromAxisAngle(e3, q2);
|
|
589
|
+
outS[0] = {ArticulationVec3{0.0, 0.0, 0.0}, e1};
|
|
590
|
+
outS[1] = {ArticulationVec3{0.0, 0.0, 0.0}, e2};
|
|
591
|
+
outS[2] = {e3, ArticulationVec3{0.0, 0.0, 0.0}};
|
|
592
|
+
outVRel = addSpatialVec(
|
|
593
|
+
scaleSpatialVec(outS[0], u0),
|
|
594
|
+
addSpatialVec(scaleSpatialVec(outS[1], u1), scaleSpatialVec(outS[2], u2)));
|
|
595
|
+
break;
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
case ArticulationJointType::Floating: {
|
|
599
|
+
const double q0 = qPtr ? qPtr[0] : 0.0;
|
|
600
|
+
const double q1 = qPtr ? qPtr[1] : 0.0;
|
|
601
|
+
const double q2 = qPtr ? qPtr[2] : 0.0;
|
|
602
|
+
const double q3 = qPtr ? qPtr[3] : 0.0;
|
|
603
|
+
const double q4 = qPtr ? qPtr[4] : 0.0;
|
|
604
|
+
const double q5 = qPtr ? qPtr[5] : 0.0;
|
|
605
|
+
outTJoint.position = {q0, q1, q2};
|
|
606
|
+
const ArticulationQuat rx = quatFromAxisAngle({1.0, 0.0, 0.0}, q3);
|
|
607
|
+
const ArticulationQuat ry = quatFromAxisAngle({0.0, 1.0, 0.0}, q4);
|
|
608
|
+
const ArticulationQuat rz = quatFromAxisAngle({0.0, 0.0, 1.0}, q5);
|
|
609
|
+
outTJoint.orientation = quatMultiply(rx, quatMultiply(ry, rz));
|
|
610
|
+
|
|
611
|
+
outS[0] = {ArticulationVec3{0.0, 0.0, 0.0}, ArticulationVec3{1.0, 0.0, 0.0}};
|
|
612
|
+
outS[1] = {ArticulationVec3{0.0, 0.0, 0.0}, ArticulationVec3{0.0, 1.0, 0.0}};
|
|
613
|
+
outS[2] = {ArticulationVec3{0.0, 0.0, 0.0}, ArticulationVec3{0.0, 0.0, 1.0}};
|
|
614
|
+
outS[3] = {ArticulationVec3{1.0, 0.0, 0.0}, ArticulationVec3{0.0, 0.0, 0.0}};
|
|
615
|
+
outS[4] = {ArticulationVec3{0.0, 1.0, 0.0}, ArticulationVec3{0.0, 0.0, 0.0}};
|
|
616
|
+
outS[5] = {ArticulationVec3{0.0, 0.0, 1.0}, ArticulationVec3{0.0, 0.0, 0.0}};
|
|
617
|
+
|
|
618
|
+
ArticulationSpatialVec vTotal{};
|
|
619
|
+
for (std::uint32_t k = 0; k < 6; ++k) {
|
|
620
|
+
const double uk = uPtr ? uPtr[k] : 0.0;
|
|
621
|
+
vTotal = addSpatialVec(vTotal, scaleSpatialVec(outS[k], uk));
|
|
622
|
+
}
|
|
623
|
+
outVRel = vTotal;
|
|
624
|
+
break;
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
} // namespace
|
|
630
|
+
|
|
631
|
+
ArticulationStatus articulationForwardKinematics(
|
|
632
|
+
const ArticulationDef& def,
|
|
633
|
+
ArticulationState& state) {
|
|
634
|
+
const auto status = articulationValidate(def);
|
|
635
|
+
if (status != ArticulationStatus::Success) return status;
|
|
636
|
+
|
|
637
|
+
std::array<std::uint32_t, kArticulationMaxLinks> dofOffsets{};
|
|
638
|
+
articulationGetDofOffsets(def, dofOffsets);
|
|
639
|
+
state.totalDofs = articulationCountDofs(def);
|
|
640
|
+
|
|
641
|
+
// Root link kinematics
|
|
642
|
+
state.linkTransforms[0] = state.rootPose;
|
|
643
|
+
if (def.rootMode == ArticulationRootMode::Fixed) {
|
|
644
|
+
state.linkVelocities[0] = {ArticulationVec3{0.0, 0.0, 0.0}, ArticulationVec3{0.0, 0.0, 0.0}};
|
|
645
|
+
} else {
|
|
646
|
+
state.linkVelocities[0] = state.rootVelocity;
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
// Propagate to children in strict topological parent < child order
|
|
650
|
+
for (std::uint32_t i = 1; i < def.linkCount; ++i) {
|
|
651
|
+
const auto& link = def.links[i];
|
|
652
|
+
const std::uint32_t p = static_cast<std::uint32_t>(link.parentIndex);
|
|
653
|
+
const std::uint32_t dOffset = dofOffsets[i];
|
|
654
|
+
|
|
655
|
+
ArticulationTransform TJoint{};
|
|
656
|
+
ArticulationSpatialVec vRel{};
|
|
657
|
+
std::array<ArticulationSpatialVec, kArticulationMaxJointDofs> S{};
|
|
658
|
+
evaluateJointMotion(
|
|
659
|
+
link.joint,
|
|
660
|
+
&state.q[dOffset],
|
|
661
|
+
&state.u[dOffset],
|
|
662
|
+
TJoint,
|
|
663
|
+
vRel,
|
|
664
|
+
S);
|
|
665
|
+
|
|
666
|
+
// Local transform: parentAnchor * TJoint * childAnchor^{-1}
|
|
667
|
+
const ArticulationTransform invChildAnchor{
|
|
668
|
+
invRotateByQuat(link.joint.childAnchor.orientation, scaleVec3(link.joint.childAnchor.position, -1.0)),
|
|
669
|
+
quatConjugate(link.joint.childAnchor.orientation)
|
|
670
|
+
};
|
|
671
|
+
const ArticulationTransform TLocal = combineTransforms(
|
|
672
|
+
combineTransforms(link.joint.parentAnchor, TJoint),
|
|
673
|
+
invChildAnchor);
|
|
674
|
+
|
|
675
|
+
// Child world transform: parentWorld * TLocal
|
|
676
|
+
state.linkTransforms[i] = combineTransforms(state.linkTransforms[p], TLocal);
|
|
677
|
+
|
|
678
|
+
// Spatial velocity: transform parent velocity into child frame, then add vRel
|
|
679
|
+
const ArticulationSpatialVec vParentInChild = transformSpatialMotion(TLocal, state.linkVelocities[p]);
|
|
680
|
+
state.linkVelocities[i] = addSpatialVec(vParentInChild, vRel);
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
return ArticulationStatus::Success;
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
void articulationComputeJointForces(
|
|
687
|
+
const ArticulationDef& def,
|
|
688
|
+
ArticulationState& state,
|
|
689
|
+
[[maybe_unused]] double dt) {
|
|
690
|
+
std::array<std::uint32_t, kArticulationMaxLinks> dofOffsets{};
|
|
691
|
+
articulationGetDofOffsets(def, dofOffsets);
|
|
692
|
+
|
|
693
|
+
for (std::uint32_t i = 1; i < def.linkCount; ++i) {
|
|
694
|
+
const auto& link = def.links[i];
|
|
695
|
+
const std::uint32_t dOffset = dofOffsets[i];
|
|
696
|
+
const std::uint32_t m = link.joint.dofCount;
|
|
697
|
+
|
|
698
|
+
for (std::uint32_t k = 0; k < m; ++k) {
|
|
699
|
+
const std::uint32_t d = dOffset + k;
|
|
700
|
+
const auto& cfg = link.joint.dofConfigs[k];
|
|
701
|
+
const double q = state.q[d];
|
|
702
|
+
const double u = state.u[d];
|
|
703
|
+
double force = state.tau[d];
|
|
704
|
+
|
|
705
|
+
// Joint Drive (PD servo)
|
|
706
|
+
if (cfg.driveEnabled) {
|
|
707
|
+
double fDrive = cfg.driveStiffness * (cfg.targetPosition - q) +
|
|
708
|
+
cfg.driveDamping * (cfg.targetVelocity - u);
|
|
709
|
+
fDrive = artClamp(fDrive, -cfg.driveMaxForce, cfg.driveMaxForce);
|
|
710
|
+
force += fDrive;
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
// Joint Limits
|
|
714
|
+
if (cfg.hasLimits) {
|
|
715
|
+
if (q < cfg.lowerLimit) {
|
|
716
|
+
const double penetration = cfg.lowerLimit - q;
|
|
717
|
+
const double fLimit = cfg.limitStiffness * penetration - cfg.limitDamping * u;
|
|
718
|
+
if (fLimit > 0.0) force += fLimit;
|
|
719
|
+
} else if (q > cfg.upperLimit) {
|
|
720
|
+
const double penetration = q - cfg.upperLimit;
|
|
721
|
+
const double fLimit = -cfg.limitStiffness * penetration - cfg.limitDamping * u;
|
|
722
|
+
if (fLimit < 0.0) force += fLimit;
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
// Joint Friction
|
|
727
|
+
if (cfg.maxFrictionForce > 0.0) {
|
|
728
|
+
double fFric = 0.0;
|
|
729
|
+
if (std::abs(u) > 1e-5) {
|
|
730
|
+
fFric = -std::copysign(cfg.maxFrictionForce, u);
|
|
731
|
+
} else {
|
|
732
|
+
fFric = -u * (cfg.maxFrictionForce / 1e-5);
|
|
733
|
+
}
|
|
734
|
+
force += fFric;
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
state.tau[d] = force;
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
// ---------------------------------------------------------------------------
|
|
743
|
+
// Featherstone's Articulated Body Algorithm (ABA)
|
|
744
|
+
// Stable high-ratio chain dynamics solved in O(N)
|
|
745
|
+
// ---------------------------------------------------------------------------
|
|
746
|
+
|
|
747
|
+
ArticulationStatus articulationForwardDynamicsABA(
|
|
748
|
+
const ArticulationDef& def,
|
|
749
|
+
ArticulationState& state) {
|
|
750
|
+
const auto status = articulationValidate(def);
|
|
751
|
+
if (status != ArticulationStatus::Success) return status;
|
|
752
|
+
|
|
753
|
+
std::array<std::uint32_t, kArticulationMaxLinks> dofOffsets{};
|
|
754
|
+
articulationGetDofOffsets(def, dofOffsets);
|
|
755
|
+
|
|
756
|
+
// Preallocated working buffers
|
|
757
|
+
std::array<ArticulationSpatialMatrix, kArticulationMaxLinks> IA{};
|
|
758
|
+
std::array<ArticulationSpatialVec, kArticulationMaxLinks> pA{};
|
|
759
|
+
std::array<ArticulationSpatialVec, kArticulationMaxLinks> cTerm{};
|
|
760
|
+
std::array<ArticulationTransform, kArticulationMaxLinks> TLocal{};
|
|
761
|
+
std::array<std::array<ArticulationSpatialVec, kArticulationMaxJointDofs>, kArticulationMaxLinks> S{};
|
|
762
|
+
std::array<std::array<ArticulationSpatialVec, kArticulationMaxJointDofs>, kArticulationMaxLinks> U{};
|
|
763
|
+
std::array<std::array<std::array<double, kArticulationMaxJointDofs>, kArticulationMaxJointDofs>, kArticulationMaxLinks> DInv{};
|
|
764
|
+
std::array<std::array<double, kArticulationMaxJointDofs>, kArticulationMaxLinks> uBias{};
|
|
765
|
+
|
|
766
|
+
// Pass 1: Initialize spatial articulated inertias and bias forces
|
|
767
|
+
for (std::uint32_t i = 0; i < def.linkCount; ++i) {
|
|
768
|
+
const auto& link = def.links[i];
|
|
769
|
+
IA[i] = makeSpatialInertia(link.mass, link.centerOfMass, link.inertiaDiagonal);
|
|
770
|
+
|
|
771
|
+
const ArticulationSpatialVec v = state.linkVelocities[i];
|
|
772
|
+
const ArticulationSpatialVec momentum = multiplySpatialMatrix(IA[i], v);
|
|
773
|
+
ArticulationSpatialVec pBias = spatialCrossForce(v, momentum);
|
|
774
|
+
|
|
775
|
+
// Gravity in local link frame
|
|
776
|
+
const Matrix3x3 R = quatToMatrix(state.linkTransforms[i].orientation);
|
|
777
|
+
const ArticulationVec3 gLocal = mulMatTransVec(R, def.gravity);
|
|
778
|
+
const ArticulationSpatialVec fGravity{
|
|
779
|
+
crossVec3(link.centerOfMass, scaleVec3(gLocal, link.mass)),
|
|
780
|
+
scaleVec3(gLocal, link.mass)
|
|
781
|
+
};
|
|
782
|
+
|
|
783
|
+
pA[i] = subSpatialVec(pBias, fGravity);
|
|
784
|
+
|
|
785
|
+
if (i > 0) {
|
|
786
|
+
const std::uint32_t dOffset = dofOffsets[i];
|
|
787
|
+
ArticulationTransform TJoint{};
|
|
788
|
+
ArticulationSpatialVec vRel{};
|
|
789
|
+
evaluateJointMotion(
|
|
790
|
+
link.joint,
|
|
791
|
+
&state.q[dOffset],
|
|
792
|
+
&state.u[dOffset],
|
|
793
|
+
TJoint,
|
|
794
|
+
vRel,
|
|
795
|
+
S[i]);
|
|
796
|
+
|
|
797
|
+
const ArticulationTransform invChildAnchor{
|
|
798
|
+
invRotateByQuat(link.joint.childAnchor.orientation, scaleVec3(link.joint.childAnchor.position, -1.0)),
|
|
799
|
+
quatConjugate(link.joint.childAnchor.orientation)
|
|
800
|
+
};
|
|
801
|
+
TLocal[i] = combineTransforms(
|
|
802
|
+
combineTransforms(link.joint.parentAnchor, TJoint),
|
|
803
|
+
invChildAnchor);
|
|
804
|
+
|
|
805
|
+
cTerm[i] = spatialCrossMotion(v, vRel);
|
|
806
|
+
} else {
|
|
807
|
+
cTerm[0] = {ArticulationVec3{0.0, 0.0, 0.0}, ArticulationVec3{0.0, 0.0, 0.0}};
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
// Pass 2: Backward sweep from leaves to root (i = linkCount - 1 down to 1)
|
|
812
|
+
for (std::uint32_t i = def.linkCount - 1; i >= 1; --i) {
|
|
813
|
+
const auto& link = def.links[i];
|
|
814
|
+
const std::uint32_t p = static_cast<std::uint32_t>(link.parentIndex);
|
|
815
|
+
const std::uint32_t m = link.joint.dofCount;
|
|
816
|
+
const std::uint32_t dOffset = dofOffsets[i];
|
|
817
|
+
|
|
818
|
+
if (m == 0) {
|
|
819
|
+
// Fixed joint transmits complete articulated inertia and bias
|
|
820
|
+
const ArticulationSpatialVec pM = addSpatialVec(pA[i], multiplySpatialMatrix(IA[i], cTerm[i]));
|
|
821
|
+
pA[p] = addSpatialVec(pA[p], transformSpatialForce(TLocal[i], pM));
|
|
822
|
+
IA[p] = addSpatialMatrix(IA[p], transformSpatialMatrix(TLocal[i], IA[i]));
|
|
823
|
+
continue;
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
// Compute U = IA * S and uBias = tau - S^T * pA
|
|
827
|
+
for (std::uint32_t k = 0; k < m; ++k) {
|
|
828
|
+
U[i][k] = multiplySpatialMatrix(IA[i], S[i][k]);
|
|
829
|
+
uBias[i][k] = state.tau[dOffset + k] - dotSpatial(S[i][k], pA[i]);
|
|
830
|
+
}
|
|
831
|
+
|
|
832
|
+
// Compute D = S^T * U (m x m positive definite matrix)
|
|
833
|
+
if (m == 1) {
|
|
834
|
+
double D = dotSpatial(S[i][0], U[i][0]);
|
|
835
|
+
if (D < 1e-11) D = 1e-11;
|
|
836
|
+
DInv[i][0][0] = 1.0 / D;
|
|
837
|
+
} else if (m == 2) {
|
|
838
|
+
const double d00 = dotSpatial(S[i][0], U[i][0]);
|
|
839
|
+
const double d01 = dotSpatial(S[i][0], U[i][1]);
|
|
840
|
+
const double d11 = dotSpatial(S[i][1], U[i][1]);
|
|
841
|
+
double det = d00 * d11 - d01 * d01;
|
|
842
|
+
if (det < 1e-14) det = 1e-14;
|
|
843
|
+
const double invDet = 1.0 / det;
|
|
844
|
+
DInv[i][0][0] = d11 * invDet;
|
|
845
|
+
DInv[i][0][1] = -d01 * invDet;
|
|
846
|
+
DInv[i][1][0] = -d01 * invDet;
|
|
847
|
+
DInv[i][1][1] = d00 * invDet;
|
|
848
|
+
} else {
|
|
849
|
+
// General small DOF inverse (m up to 6)
|
|
850
|
+
// DInv regularized via diagonal loading
|
|
851
|
+
for (std::uint32_t j = 0; j < m; ++j) {
|
|
852
|
+
double djj = dotSpatial(S[i][j], U[i][j]);
|
|
853
|
+
if (djj < 1e-11) djj = 1e-11;
|
|
854
|
+
DInv[i][j][j] = 1.0 / djj;
|
|
855
|
+
}
|
|
856
|
+
}
|
|
857
|
+
|
|
858
|
+
// Articulated inertia transmitted to parent:
|
|
859
|
+
// I^M = IA - U * D^{-1} * U^T
|
|
860
|
+
ArticulationSpatialMatrix IM = IA[i];
|
|
861
|
+
for (std::uint32_t r = 0; r < m; ++r) {
|
|
862
|
+
for (std::uint32_t c = 0; c < m; ++c) {
|
|
863
|
+
const double dInvVal = DInv[i][r][c];
|
|
864
|
+
if (std::abs(dInvVal) < 1e-15) continue;
|
|
865
|
+
const auto& Ur = U[i][r];
|
|
866
|
+
const auto& Uc = U[i][c];
|
|
867
|
+
const std::array<double, 6> urA{Ur.angular.x, Ur.angular.y, Ur.angular.z, Ur.linear.x, Ur.linear.y, Ur.linear.z};
|
|
868
|
+
const std::array<double, 6> ucA{Uc.angular.x, Uc.angular.y, Uc.angular.z, Uc.linear.x, Uc.linear.y, Uc.linear.z};
|
|
869
|
+
for (std::size_t row = 0; row < 6; ++row) {
|
|
870
|
+
for (std::size_t col = 0; col < 6; ++col) {
|
|
871
|
+
IM.data[row][col] -= dInvVal * urA[row] * ucA[col];
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
}
|
|
875
|
+
}
|
|
876
|
+
|
|
877
|
+
// Bias force transmitted to parent:
|
|
878
|
+
// p^M = pA + I^M * c + U * D^{-1} * uBias
|
|
879
|
+
ArticulationSpatialVec pM = addSpatialVec(pA[i], multiplySpatialMatrix(IM, cTerm[i]));
|
|
880
|
+
for (std::uint32_t r = 0; r < m; ++r) {
|
|
881
|
+
double dInvUBias = 0.0;
|
|
882
|
+
for (std::uint32_t c = 0; c < m; ++c) {
|
|
883
|
+
dInvUBias += DInv[i][r][c] * uBias[i][c];
|
|
884
|
+
}
|
|
885
|
+
pM = addSpatialVec(pM, scaleSpatialVec(U[i][r], dInvUBias));
|
|
886
|
+
}
|
|
887
|
+
|
|
888
|
+
// Accumulate to parent
|
|
889
|
+
IA[p] = addSpatialMatrix(IA[p], transformSpatialMatrix(TLocal[i], IM));
|
|
890
|
+
pA[p] = addSpatialVec(pA[p], transformSpatialForce(TLocal[i], pM));
|
|
891
|
+
}
|
|
892
|
+
|
|
893
|
+
// Root acceleration calculation
|
|
894
|
+
if (def.rootMode == ArticulationRootMode::Fixed) {
|
|
895
|
+
state.linkAccelerations[0] = {ArticulationVec3{0.0, 0.0, 0.0}, ArticulationVec3{0.0, 0.0, 0.0}};
|
|
896
|
+
state.rootAcceleration = state.linkAccelerations[0];
|
|
897
|
+
} else {
|
|
898
|
+
// Floating root: solve IA[0] * a_0 = -pA[0]
|
|
899
|
+
ArticulationSpatialMatrix invIA0{};
|
|
900
|
+
invertSpatialMatrix6x6(IA[0], invIA0);
|
|
901
|
+
const ArticulationSpatialVec negPA0{scaleVec3(pA[0].angular, -1.0), scaleVec3(pA[0].linear, -1.0)};
|
|
902
|
+
state.linkAccelerations[0] = multiplySpatialMatrix(invIA0, negPA0);
|
|
903
|
+
state.rootAcceleration = state.linkAccelerations[0];
|
|
904
|
+
}
|
|
905
|
+
|
|
906
|
+
// Pass 3: Forward sweep from root to leaves (i = 1 to linkCount - 1)
|
|
907
|
+
for (std::uint32_t i = 1; i < def.linkCount; ++i) {
|
|
908
|
+
const auto& link = def.links[i];
|
|
909
|
+
const std::uint32_t p = static_cast<std::uint32_t>(link.parentIndex);
|
|
910
|
+
const std::uint32_t m = link.joint.dofCount;
|
|
911
|
+
const std::uint32_t dOffset = dofOffsets[i];
|
|
912
|
+
|
|
913
|
+
const ArticulationSpatialVec aParentInChild = transformSpatialMotion(TLocal[i], state.linkAccelerations[p]);
|
|
914
|
+
|
|
915
|
+
if (m == 0) {
|
|
916
|
+
state.linkAccelerations[i] = addSpatialVec(aParentInChild, cTerm[i]);
|
|
917
|
+
continue;
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
// \ddot{q} = D^{-1} * (uBias - U^T * a'_{parent})
|
|
921
|
+
std::array<double, kArticulationMaxJointDofs> uPrimeBias{};
|
|
922
|
+
for (std::uint32_t k = 0; k < m; ++k) {
|
|
923
|
+
uPrimeBias[k] = uBias[i][k] - dotSpatial(U[i][k], aParentInChild);
|
|
924
|
+
}
|
|
925
|
+
|
|
926
|
+
std::array<double, kArticulationMaxJointDofs> qDdot{};
|
|
927
|
+
for (std::uint32_t r = 0; r < m; ++r) {
|
|
928
|
+
double sum = 0.0;
|
|
929
|
+
for (std::uint32_t c = 0; c < m; ++c) {
|
|
930
|
+
sum += DInv[i][r][c] * uPrimeBias[c];
|
|
931
|
+
}
|
|
932
|
+
qDdot[r] = sum;
|
|
933
|
+
state.udot[dOffset + r] = sum;
|
|
934
|
+
}
|
|
935
|
+
|
|
936
|
+
// a_i = a'_{parent} + c_i + S * \ddot{q}
|
|
937
|
+
ArticulationSpatialVec aTotal = addSpatialVec(aParentInChild, cTerm[i]);
|
|
938
|
+
for (std::uint32_t k = 0; k < m; ++k) {
|
|
939
|
+
aTotal = addSpatialVec(aTotal, scaleSpatialVec(S[i][k], qDdot[k]));
|
|
940
|
+
}
|
|
941
|
+
state.linkAccelerations[i] = aTotal;
|
|
942
|
+
}
|
|
943
|
+
|
|
944
|
+
return ArticulationStatus::Success;
|
|
945
|
+
}
|
|
946
|
+
|
|
947
|
+
// ---------------------------------------------------------------------------
|
|
948
|
+
// Explicit Loop Closure through Impulse Constraints
|
|
949
|
+
// ---------------------------------------------------------------------------
|
|
950
|
+
|
|
951
|
+
ArticulationStatus articulationSolveLoopConstraints(
|
|
952
|
+
const ArticulationDef& def,
|
|
953
|
+
ArticulationState& state,
|
|
954
|
+
double dt,
|
|
955
|
+
std::uint32_t iterations) {
|
|
956
|
+
if (def.loopCount == 0 || dt <= 0.0) return ArticulationStatus::Success;
|
|
957
|
+
|
|
958
|
+
const double invDt = 1.0 / dt;
|
|
959
|
+
std::array<std::uint32_t, kArticulationMaxLinks> dofOffsets{};
|
|
960
|
+
articulationGetDofOffsets(def, dofOffsets);
|
|
961
|
+
|
|
962
|
+
for (std::uint32_t iter = 0; iter < iterations; ++iter) {
|
|
963
|
+
for (std::uint32_t k = 0; k < def.loopCount; ++k) {
|
|
964
|
+
auto& loop = const_cast<ArticulationDef&>(def).loops[k];
|
|
965
|
+
const std::uint32_t linkA = loop.linkA;
|
|
966
|
+
const std::int32_t linkB = loop.linkB;
|
|
967
|
+
|
|
968
|
+
const auto& tA = state.linkTransforms[linkA];
|
|
969
|
+
const ArticulationVec3 rA = rotateByQuat(tA.orientation, loop.pivotA);
|
|
970
|
+
const ArticulationVec3 wA = addVec3(tA.position, rA);
|
|
971
|
+
const ArticulationSpatialVec& vA = state.linkVelocities[linkA];
|
|
972
|
+
const ArticulationVec3 vptA = addVec3(vA.linear, crossVec3(vA.angular, rA));
|
|
973
|
+
|
|
974
|
+
ArticulationVec3 wB = loop.pivotB;
|
|
975
|
+
ArticulationVec3 rB{0.0, 0.0, 0.0};
|
|
976
|
+
ArticulationVec3 vptB{0.0, 0.0, 0.0};
|
|
977
|
+
const double massB = (linkB >= 0) ? def.links[linkB].mass : 0.0;
|
|
978
|
+
ArticulationVec3 diagInertiaB = (linkB >= 0) ? def.links[linkB].inertiaDiagonal : ArticulationVec3{0.1, 0.1, 0.1};
|
|
979
|
+
|
|
980
|
+
if (linkB >= 0) {
|
|
981
|
+
const auto& tB = state.linkTransforms[linkB];
|
|
982
|
+
rB = rotateByQuat(tB.orientation, loop.pivotB);
|
|
983
|
+
wB = addVec3(tB.position, rB);
|
|
984
|
+
const ArticulationSpatialVec& vB = state.linkVelocities[linkB];
|
|
985
|
+
vptB = addVec3(vB.linear, crossVec3(vB.angular, rB));
|
|
986
|
+
}
|
|
987
|
+
|
|
988
|
+
const double massA = def.links[linkA].mass;
|
|
989
|
+
const ArticulationVec3 diagInertiaA = def.links[linkA].inertiaDiagonal;
|
|
990
|
+
|
|
991
|
+
const double invMassA = (massA > 0.0) ? (1.0 / massA) : 0.0;
|
|
992
|
+
const double invMassB = (massB > 0.0) ? (1.0 / massB) : 0.0;
|
|
993
|
+
|
|
994
|
+
// Effective linear mass matrix K along 3 axes
|
|
995
|
+
auto computeEffMassDiag = [&](const ArticulationVec3& r, double invM, const ArticulationVec3& inertia) {
|
|
996
|
+
const double invIxx = 1.0 / inertia.x;
|
|
997
|
+
const double invIyy = 1.0 / inertia.y;
|
|
998
|
+
const double invIzz = 1.0 / inertia.z;
|
|
999
|
+
return ArticulationVec3{
|
|
1000
|
+
invM + (r.y * r.y * invIzz + r.z * r.z * invIyy),
|
|
1001
|
+
invM + (r.x * r.x * invIzz + r.z * r.z * invIxx),
|
|
1002
|
+
invM + (r.x * r.x * invIyy + r.y * r.y * invIxx)
|
|
1003
|
+
};
|
|
1004
|
+
};
|
|
1005
|
+
|
|
1006
|
+
const ArticulationVec3 effDiagA = computeEffMassDiag(rA, invMassA, diagInertiaA);
|
|
1007
|
+
const ArticulationVec3 effDiagB = (linkB >= 0) ? computeEffMassDiag(rB, invMassB, diagInertiaB) : ArticulationVec3{0.0, 0.0, 0.0};
|
|
1008
|
+
const ArticulationVec3 effTotal = addVec3(effDiagA, effDiagB);
|
|
1009
|
+
|
|
1010
|
+
const double erp = artClamp(loop.stiffness, 0.01, 1.0);
|
|
1011
|
+
|
|
1012
|
+
switch (loop.kind) {
|
|
1013
|
+
case ArticulationLoopKind::Point: {
|
|
1014
|
+
// Point-to-point 3D impulse constraint
|
|
1015
|
+
const ArticulationVec3 posErr = subVec3(wA, wB);
|
|
1016
|
+
const ArticulationVec3 vRel = subVec3(vptA, vptB);
|
|
1017
|
+
const ArticulationVec3 vTarget = scaleVec3(posErr, -erp * invDt);
|
|
1018
|
+
const ArticulationVec3 deltaV = subVec3(vTarget, vRel);
|
|
1019
|
+
|
|
1020
|
+
ArticulationVec3 impulse{
|
|
1021
|
+
deltaV.x / (effTotal.x + 1e-9),
|
|
1022
|
+
deltaV.y / (effTotal.y + 1e-9),
|
|
1023
|
+
deltaV.z / (effTotal.z + 1e-9)
|
|
1024
|
+
};
|
|
1025
|
+
|
|
1026
|
+
const double impLen = lengthVec3(impulse);
|
|
1027
|
+
if (impLen > loop.maxImpulse) {
|
|
1028
|
+
impulse = scaleVec3(impulse, loop.maxImpulse / impLen);
|
|
1029
|
+
}
|
|
1030
|
+
|
|
1031
|
+
// Apply spatial impulse to link A
|
|
1032
|
+
state.linkVelocities[linkA].linear = addVec3(state.linkVelocities[linkA].linear, scaleVec3(impulse, invMassA));
|
|
1033
|
+
state.linkVelocities[linkA].angular = addVec3(
|
|
1034
|
+
state.linkVelocities[linkA].angular,
|
|
1035
|
+
crossVec3(rA, scaleVec3(impulse, 1.0 / diagInertiaA.x)));
|
|
1036
|
+
|
|
1037
|
+
// Apply opposite impulse to link B
|
|
1038
|
+
if (linkB >= 0) {
|
|
1039
|
+
state.linkVelocities[linkB].linear = subVec3(state.linkVelocities[linkB].linear, scaleVec3(impulse, invMassB));
|
|
1040
|
+
state.linkVelocities[linkB].angular = subVec3(
|
|
1041
|
+
state.linkVelocities[linkB].angular,
|
|
1042
|
+
crossVec3(rB, scaleVec3(impulse, 1.0 / diagInertiaB.x)));
|
|
1043
|
+
}
|
|
1044
|
+
|
|
1045
|
+
loop.accumulatedImpulse[0] += impulse.x;
|
|
1046
|
+
loop.accumulatedImpulse[1] += impulse.y;
|
|
1047
|
+
loop.accumulatedImpulse[2] += impulse.z;
|
|
1048
|
+
loop.lastImpulse[0] = impulse.x;
|
|
1049
|
+
loop.lastImpulse[1] = impulse.y;
|
|
1050
|
+
loop.lastImpulse[2] = impulse.z;
|
|
1051
|
+
break;
|
|
1052
|
+
}
|
|
1053
|
+
|
|
1054
|
+
case ArticulationLoopKind::Distance: {
|
|
1055
|
+
// Distance constraint along connecting normal
|
|
1056
|
+
const ArticulationVec3 diff = subVec3(wA, wB);
|
|
1057
|
+
const double curDist = lengthVec3(diff);
|
|
1058
|
+
ArticulationVec3 normal{1.0, 0.0, 0.0};
|
|
1059
|
+
if (curDist > 1e-9) {
|
|
1060
|
+
normal = scaleVec3(diff, 1.0 / curDist);
|
|
1061
|
+
}
|
|
1062
|
+
|
|
1063
|
+
const double distErr = curDist - loop.targetDistance;
|
|
1064
|
+
const double vRelN = dotVec3(subVec3(vptA, vptB), normal);
|
|
1065
|
+
const double vTargetN = -erp * distErr * invDt;
|
|
1066
|
+
const double effMassN = dotVec3(effTotal, {normal.x * normal.x, normal.y * normal.y, normal.z * normal.z}) + 1e-9;
|
|
1067
|
+
|
|
1068
|
+
double deltaImpulse = (vTargetN - vRelN) / effMassN;
|
|
1069
|
+
deltaImpulse = artClamp(deltaImpulse, -loop.maxImpulse, loop.maxImpulse);
|
|
1070
|
+
|
|
1071
|
+
const ArticulationVec3 impulseVec = scaleVec3(normal, deltaImpulse);
|
|
1072
|
+
|
|
1073
|
+
state.linkVelocities[linkA].linear = addVec3(state.linkVelocities[linkA].linear, scaleVec3(impulseVec, invMassA));
|
|
1074
|
+
state.linkVelocities[linkA].angular = addVec3(
|
|
1075
|
+
state.linkVelocities[linkA].angular,
|
|
1076
|
+
crossVec3(rA, scaleVec3(impulseVec, 1.0 / diagInertiaA.x)));
|
|
1077
|
+
|
|
1078
|
+
if (linkB >= 0) {
|
|
1079
|
+
state.linkVelocities[linkB].linear = subVec3(state.linkVelocities[linkB].linear, scaleVec3(impulseVec, invMassB));
|
|
1080
|
+
state.linkVelocities[linkB].angular = subVec3(
|
|
1081
|
+
state.linkVelocities[linkB].angular,
|
|
1082
|
+
crossVec3(rB, scaleVec3(impulseVec, 1.0 / diagInertiaB.x)));
|
|
1083
|
+
}
|
|
1084
|
+
|
|
1085
|
+
loop.accumulatedImpulse[0] += deltaImpulse;
|
|
1086
|
+
loop.lastImpulse[0] = deltaImpulse;
|
|
1087
|
+
break;
|
|
1088
|
+
}
|
|
1089
|
+
|
|
1090
|
+
case ArticulationLoopKind::Weld: {
|
|
1091
|
+
// Point constraint + 3-DOF angular lock
|
|
1092
|
+
const ArticulationVec3 posErr = subVec3(wA, wB);
|
|
1093
|
+
const ArticulationVec3 vRel = subVec3(vptA, vptB);
|
|
1094
|
+
const ArticulationVec3 vTarget = scaleVec3(posErr, -erp * invDt);
|
|
1095
|
+
const ArticulationVec3 deltaV = subVec3(vTarget, vRel);
|
|
1096
|
+
|
|
1097
|
+
const ArticulationVec3 linImpulse{
|
|
1098
|
+
deltaV.x / (effTotal.x + 1e-9),
|
|
1099
|
+
deltaV.y / (effTotal.y + 1e-9),
|
|
1100
|
+
deltaV.z / (effTotal.z + 1e-9)
|
|
1101
|
+
};
|
|
1102
|
+
|
|
1103
|
+
// Relative orientation error
|
|
1104
|
+
const ArticulationQuat rotB = (linkB >= 0) ? state.linkTransforms[linkB].orientation : quatIdentity();
|
|
1105
|
+
const ArticulationQuat qRel = quatMultiply(tA.orientation, quatConjugate(rotB));
|
|
1106
|
+
const double signW = (qRel.w < 0.0) ? -1.0 : 1.0;
|
|
1107
|
+
const ArticulationVec3 rotErr = scaleVec3({qRel.x, qRel.y, qRel.z}, 2.0 * signW);
|
|
1108
|
+
|
|
1109
|
+
const ArticulationVec3 omegaB = (linkB >= 0) ? state.linkVelocities[linkB].angular : ArticulationVec3{0.0, 0.0, 0.0};
|
|
1110
|
+
const ArticulationVec3 relOmega = subVec3(vA.angular, omegaB);
|
|
1111
|
+
const ArticulationVec3 angTarget = scaleVec3(rotErr, -erp * invDt);
|
|
1112
|
+
const ArticulationVec3 deltaOmega = subVec3(angTarget, relOmega);
|
|
1113
|
+
|
|
1114
|
+
const ArticulationVec3 invInertiaTotal{
|
|
1115
|
+
(1.0 / diagInertiaA.x) + ((linkB >= 0) ? (1.0 / diagInertiaB.x) : 0.0) + 1e-9,
|
|
1116
|
+
(1.0 / diagInertiaA.y) + ((linkB >= 0) ? (1.0 / diagInertiaB.y) : 0.0) + 1e-9,
|
|
1117
|
+
(1.0 / diagInertiaA.z) + ((linkB >= 0) ? (1.0 / diagInertiaB.z) : 0.0) + 1e-9
|
|
1118
|
+
};
|
|
1119
|
+
|
|
1120
|
+
const ArticulationVec3 angImpulse{
|
|
1121
|
+
deltaOmega.x / invInertiaTotal.x,
|
|
1122
|
+
deltaOmega.y / invInertiaTotal.y,
|
|
1123
|
+
deltaOmega.z / invInertiaTotal.z
|
|
1124
|
+
};
|
|
1125
|
+
|
|
1126
|
+
state.linkVelocities[linkA].linear = addVec3(state.linkVelocities[linkA].linear, scaleVec3(linImpulse, invMassA));
|
|
1127
|
+
state.linkVelocities[linkA].angular = addVec3(
|
|
1128
|
+
state.linkVelocities[linkA].angular,
|
|
1129
|
+
addVec3(crossVec3(rA, scaleVec3(linImpulse, 1.0 / diagInertiaA.x)), scaleVec3(angImpulse, 1.0 / diagInertiaA.x)));
|
|
1130
|
+
|
|
1131
|
+
if (linkB >= 0) {
|
|
1132
|
+
state.linkVelocities[linkB].linear = subVec3(state.linkVelocities[linkB].linear, scaleVec3(linImpulse, invMassB));
|
|
1133
|
+
state.linkVelocities[linkB].angular = subVec3(
|
|
1134
|
+
state.linkVelocities[linkB].angular,
|
|
1135
|
+
addVec3(crossVec3(rB, scaleVec3(linImpulse, 1.0 / diagInertiaB.x)), scaleVec3(angImpulse, 1.0 / diagInertiaB.x)));
|
|
1136
|
+
}
|
|
1137
|
+
|
|
1138
|
+
loop.accumulatedImpulse[0] += linImpulse.x;
|
|
1139
|
+
loop.accumulatedImpulse[1] += linImpulse.y;
|
|
1140
|
+
loop.accumulatedImpulse[2] += linImpulse.z;
|
|
1141
|
+
loop.accumulatedImpulse[3] += angImpulse.x;
|
|
1142
|
+
loop.accumulatedImpulse[4] += angImpulse.y;
|
|
1143
|
+
loop.accumulatedImpulse[5] += angImpulse.z;
|
|
1144
|
+
break;
|
|
1145
|
+
}
|
|
1146
|
+
|
|
1147
|
+
case ArticulationLoopKind::Revolute: {
|
|
1148
|
+
// Point constraint + 2-DOF angular alignment locking orthogonal axes
|
|
1149
|
+
const ArticulationVec3 posErr = subVec3(wA, wB);
|
|
1150
|
+
const ArticulationVec3 vRel = subVec3(vptA, vptB);
|
|
1151
|
+
const ArticulationVec3 vTarget = scaleVec3(posErr, -erp * invDt);
|
|
1152
|
+
const ArticulationVec3 deltaV = subVec3(vTarget, vRel);
|
|
1153
|
+
|
|
1154
|
+
const ArticulationVec3 linImpulse{
|
|
1155
|
+
deltaV.x / (effTotal.x + 1e-9),
|
|
1156
|
+
deltaV.y / (effTotal.y + 1e-9),
|
|
1157
|
+
deltaV.z / (effTotal.z + 1e-9)
|
|
1158
|
+
};
|
|
1159
|
+
|
|
1160
|
+
// Hinge axis alignment in world space
|
|
1161
|
+
const ArticulationVec3 worldAxisA = rotateByQuat(tA.orientation, loop.axisA);
|
|
1162
|
+
const ArticulationVec3 worldAxisB = (linkB >= 0) ?
|
|
1163
|
+
rotateByQuat(state.linkTransforms[linkB].orientation, loop.axisB) : loop.axisB;
|
|
1164
|
+
const ArticulationVec3 axisErr = crossVec3(worldAxisA, worldAxisB);
|
|
1165
|
+
|
|
1166
|
+
const ArticulationVec3 omegaB = (linkB >= 0) ? state.linkVelocities[linkB].angular : ArticulationVec3{0.0, 0.0, 0.0};
|
|
1167
|
+
const ArticulationVec3 relOmega = subVec3(vA.angular, omegaB);
|
|
1168
|
+
const ArticulationVec3 deltaAngV = subVec3(scaleVec3(axisErr, -erp * invDt), relOmega);
|
|
1169
|
+
|
|
1170
|
+
// Project deltaAngV onto plane orthogonal to worldAxisA
|
|
1171
|
+
const ArticulationVec3 deltaAngOrtho = subVec3(deltaAngV, scaleVec3(worldAxisA, dotVec3(deltaAngV, worldAxisA)));
|
|
1172
|
+
const ArticulationVec3 angImpulse{
|
|
1173
|
+
deltaAngOrtho.x / (1.0 / diagInertiaA.x + ((linkB >= 0) ? (1.0 / diagInertiaB.x) : 0.0) + 1e-9),
|
|
1174
|
+
deltaAngOrtho.y / (1.0 / diagInertiaA.y + ((linkB >= 0) ? (1.0 / diagInertiaB.y) : 0.0) + 1e-9),
|
|
1175
|
+
deltaAngOrtho.z / (1.0 / diagInertiaA.z + ((linkB >= 0) ? (1.0 / diagInertiaB.z) : 0.0) + 1e-9)
|
|
1176
|
+
};
|
|
1177
|
+
|
|
1178
|
+
state.linkVelocities[linkA].linear = addVec3(state.linkVelocities[linkA].linear, scaleVec3(linImpulse, invMassA));
|
|
1179
|
+
state.linkVelocities[linkA].angular = addVec3(
|
|
1180
|
+
state.linkVelocities[linkA].angular,
|
|
1181
|
+
addVec3(crossVec3(rA, scaleVec3(linImpulse, 1.0 / diagInertiaA.x)), scaleVec3(angImpulse, 1.0 / diagInertiaA.x)));
|
|
1182
|
+
|
|
1183
|
+
if (linkB >= 0) {
|
|
1184
|
+
state.linkVelocities[linkB].linear = subVec3(state.linkVelocities[linkB].linear, scaleVec3(linImpulse, invMassB));
|
|
1185
|
+
state.linkVelocities[linkB].angular = subVec3(
|
|
1186
|
+
state.linkVelocities[linkB].angular,
|
|
1187
|
+
addVec3(crossVec3(rB, scaleVec3(linImpulse, 1.0 / diagInertiaB.x)), scaleVec3(angImpulse, 1.0 / diagInertiaB.x)));
|
|
1188
|
+
}
|
|
1189
|
+
|
|
1190
|
+
loop.accumulatedImpulse[0] += linImpulse.x;
|
|
1191
|
+
loop.accumulatedImpulse[1] += linImpulse.y;
|
|
1192
|
+
loop.accumulatedImpulse[2] += linImpulse.z;
|
|
1193
|
+
loop.accumulatedImpulse[3] += angImpulse.x;
|
|
1194
|
+
loop.accumulatedImpulse[4] += angImpulse.y;
|
|
1195
|
+
loop.accumulatedImpulse[5] += angImpulse.z;
|
|
1196
|
+
break;
|
|
1197
|
+
}
|
|
1198
|
+
}
|
|
1199
|
+
}
|
|
1200
|
+
}
|
|
1201
|
+
|
|
1202
|
+
return ArticulationStatus::Success;
|
|
1203
|
+
}
|
|
1204
|
+
|
|
1205
|
+
// ---------------------------------------------------------------------------
|
|
1206
|
+
// Numerical Integration
|
|
1207
|
+
// ---------------------------------------------------------------------------
|
|
1208
|
+
|
|
1209
|
+
void articulationIntegrate(
|
|
1210
|
+
const ArticulationDef& def,
|
|
1211
|
+
ArticulationState& state,
|
|
1212
|
+
double dt) {
|
|
1213
|
+
if (dt <= 0.0) return;
|
|
1214
|
+
|
|
1215
|
+
std::array<std::uint32_t, kArticulationMaxLinks> dofOffsets{};
|
|
1216
|
+
articulationGetDofOffsets(def, dofOffsets);
|
|
1217
|
+
const std::uint32_t totalDofs = articulationCountDofs(def);
|
|
1218
|
+
|
|
1219
|
+
// Symplectic Euler on joint generalized coordinates
|
|
1220
|
+
for (std::uint32_t d = 0; d < totalDofs; ++d) {
|
|
1221
|
+
state.u[d] += state.udot[d] * dt;
|
|
1222
|
+
state.q[d] += state.u[d] * dt;
|
|
1223
|
+
}
|
|
1224
|
+
|
|
1225
|
+
// Floating root integration
|
|
1226
|
+
if (def.rootMode == ArticulationRootMode::Floating) {
|
|
1227
|
+
state.rootVelocity.linear = addVec3(
|
|
1228
|
+
state.rootVelocity.linear,
|
|
1229
|
+
scaleVec3(state.rootAcceleration.linear, dt));
|
|
1230
|
+
state.rootVelocity.angular = addVec3(
|
|
1231
|
+
state.rootVelocity.angular,
|
|
1232
|
+
scaleVec3(state.rootAcceleration.angular, dt));
|
|
1233
|
+
|
|
1234
|
+
state.rootPose.position = addVec3(
|
|
1235
|
+
state.rootPose.position,
|
|
1236
|
+
scaleVec3(state.rootVelocity.linear, dt));
|
|
1237
|
+
|
|
1238
|
+
// Quaternion integration: \dot{q} = 0.5 * \omega * q
|
|
1239
|
+
const ArticulationVec3 w = state.rootVelocity.angular;
|
|
1240
|
+
const double wLen = lengthVec3(w);
|
|
1241
|
+
if (wLen > 1e-9) {
|
|
1242
|
+
const ArticulationQuat dq = quatFromAxisAngle(w, wLen * dt);
|
|
1243
|
+
state.rootPose.orientation = quatNormalize(quatMultiply(dq, state.rootPose.orientation));
|
|
1244
|
+
}
|
|
1245
|
+
}
|
|
1246
|
+
}
|
|
1247
|
+
|
|
1248
|
+
// ---------------------------------------------------------------------------
|
|
1249
|
+
// Full Simulation Step
|
|
1250
|
+
// ---------------------------------------------------------------------------
|
|
1251
|
+
|
|
1252
|
+
ArticulationStatus articulationStep(
|
|
1253
|
+
const ArticulationDef& def,
|
|
1254
|
+
ArticulationState& state,
|
|
1255
|
+
double dt,
|
|
1256
|
+
std::uint32_t loopIterations) {
|
|
1257
|
+
if (dt <= 0.0) return ArticulationStatus::Success;
|
|
1258
|
+
|
|
1259
|
+
// 1. Forward kinematics to update transforms and spatial velocities from (q, u)
|
|
1260
|
+
auto status = articulationForwardKinematics(def, state);
|
|
1261
|
+
if (status != ArticulationStatus::Success) return status;
|
|
1262
|
+
|
|
1263
|
+
// 2. Compute internal joint forces (motors, limits, friction)
|
|
1264
|
+
articulationComputeJointForces(def, state, dt);
|
|
1265
|
+
|
|
1266
|
+
// 3. Featherstone ABA to compute accelerations
|
|
1267
|
+
status = articulationForwardDynamicsABA(def, state);
|
|
1268
|
+
if (status != ArticulationStatus::Success) return status;
|
|
1269
|
+
|
|
1270
|
+
// 4. Integrate generalized velocities and positions
|
|
1271
|
+
articulationIntegrate(def, state, dt);
|
|
1272
|
+
|
|
1273
|
+
// 5. Update kinematics with new velocities
|
|
1274
|
+
status = articulationForwardKinematics(def, state);
|
|
1275
|
+
if (status != ArticulationStatus::Success) return status;
|
|
1276
|
+
|
|
1277
|
+
// 6. Explicit loop closure constraints via impulse solver
|
|
1278
|
+
if (def.loopCount > 0 && loopIterations > 0) {
|
|
1279
|
+
status = articulationSolveLoopConstraints(def, state, dt, loopIterations);
|
|
1280
|
+
if (status != ArticulationStatus::Success) return status;
|
|
1281
|
+
}
|
|
1282
|
+
|
|
1283
|
+
return ArticulationStatus::Success;
|
|
1284
|
+
}
|
|
1285
|
+
|
|
1286
|
+
void articulationApplyExternalImpulse(
|
|
1287
|
+
const ArticulationDef& def,
|
|
1288
|
+
ArticulationState& state,
|
|
1289
|
+
std::uint32_t linkIndex,
|
|
1290
|
+
const ArticulationVec3& impulse,
|
|
1291
|
+
const ArticulationVec3& point) {
|
|
1292
|
+
if (linkIndex >= def.linkCount) return;
|
|
1293
|
+
const double mass = def.links[linkIndex].mass;
|
|
1294
|
+
if (mass <= 0.0) return;
|
|
1295
|
+
|
|
1296
|
+
const auto& t = state.linkTransforms[linkIndex];
|
|
1297
|
+
const ArticulationVec3 r = subVec3(point, t.position);
|
|
1298
|
+
const double invMass = 1.0 / mass;
|
|
1299
|
+
const auto& inertia = def.links[linkIndex].inertiaDiagonal;
|
|
1300
|
+
|
|
1301
|
+
state.linkVelocities[linkIndex].linear = addVec3(
|
|
1302
|
+
state.linkVelocities[linkIndex].linear,
|
|
1303
|
+
scaleVec3(impulse, invMass));
|
|
1304
|
+
|
|
1305
|
+
const ArticulationVec3 torque = crossVec3(r, impulse);
|
|
1306
|
+
state.linkVelocities[linkIndex].angular = addVec3(
|
|
1307
|
+
state.linkVelocities[linkIndex].angular,
|
|
1308
|
+
ArticulationVec3{torque.x / inertia.x, torque.y / inertia.y, torque.z / inertia.z});
|
|
1309
|
+
}
|
|
1310
|
+
|
|
1311
|
+
void articulationSyncBodies(
|
|
1312
|
+
const ArticulationDef& def,
|
|
1313
|
+
const ArticulationState& state,
|
|
1314
|
+
std::span<Body> outBodies) {
|
|
1315
|
+
const std::size_t count = std::min(static_cast<std::size_t>(def.linkCount), outBodies.size());
|
|
1316
|
+
for (std::size_t i = 0; i < count; ++i) {
|
|
1317
|
+
auto& b = outBodies[i];
|
|
1318
|
+
const auto& t = state.linkTransforms[i];
|
|
1319
|
+
const auto& v = state.linkVelocities[i];
|
|
1320
|
+
const auto& link = def.links[i];
|
|
1321
|
+
|
|
1322
|
+
b.x = t.position.x;
|
|
1323
|
+
b.y = t.position.y;
|
|
1324
|
+
b.z = t.position.z;
|
|
1325
|
+
|
|
1326
|
+
b.orientationX = t.orientation.x;
|
|
1327
|
+
b.orientationY = t.orientation.y;
|
|
1328
|
+
b.orientationZ = t.orientation.z;
|
|
1329
|
+
b.orientationW = t.orientation.w;
|
|
1330
|
+
|
|
1331
|
+
b.vx = v.linear.x;
|
|
1332
|
+
b.vy = v.linear.y;
|
|
1333
|
+
b.vz = v.linear.z;
|
|
1334
|
+
|
|
1335
|
+
b.angularVelX = v.angular.x;
|
|
1336
|
+
b.angularVelY = v.angular.y;
|
|
1337
|
+
b.angularVelZ = v.angular.z;
|
|
1338
|
+
|
|
1339
|
+
b.mass = link.mass;
|
|
1340
|
+
b.inertiaXX = link.inertiaDiagonal.x;
|
|
1341
|
+
b.inertiaYY = link.inertiaDiagonal.y;
|
|
1342
|
+
b.inertiaZZ = link.inertiaDiagonal.z;
|
|
1343
|
+
b.centerOfMassX = link.centerOfMass.x;
|
|
1344
|
+
b.centerOfMassY = link.centerOfMass.y;
|
|
1345
|
+
b.centerOfMassZ = link.centerOfMass.z;
|
|
1346
|
+
b.shape = link.shapeKind;
|
|
1347
|
+
}
|
|
1348
|
+
}
|
|
1349
|
+
|
|
1350
|
+
// ---------------------------------------------------------------------------
|
|
1351
|
+
// C ABI Export Implementations
|
|
1352
|
+
// ---------------------------------------------------------------------------
|
|
1353
|
+
|
|
1354
|
+
extern "C" {
|
|
1355
|
+
|
|
1356
|
+
std::uint32_t rundot_kinetix_ext_articulation_abi_version() {
|
|
1357
|
+
return kArticulationAbiVersion;
|
|
1358
|
+
}
|
|
1359
|
+
|
|
1360
|
+
std::uint32_t rundot_kinetix_ext_articulation_link_stride() {
|
|
1361
|
+
return kArticulationLinkStride;
|
|
1362
|
+
}
|
|
1363
|
+
|
|
1364
|
+
std::uint32_t rundot_kinetix_ext_articulation_joint_stride() {
|
|
1365
|
+
return kArticulationJointStride;
|
|
1366
|
+
}
|
|
1367
|
+
|
|
1368
|
+
std::uint32_t rundot_kinetix_ext_articulation_state_stride() {
|
|
1369
|
+
return kArticulationStateStride;
|
|
1370
|
+
}
|
|
1371
|
+
|
|
1372
|
+
std::uint32_t rundot_kinetix_ext_articulation_loop_stride() {
|
|
1373
|
+
return kArticulationLoopStride;
|
|
1374
|
+
}
|
|
1375
|
+
|
|
1376
|
+
std::uint32_t rundot_kinetix_ext_articulation_max_links() {
|
|
1377
|
+
return kArticulationMaxLinks;
|
|
1378
|
+
}
|
|
1379
|
+
|
|
1380
|
+
std::uint32_t rundot_kinetix_ext_articulation_max_dofs() {
|
|
1381
|
+
return kArticulationMaxTotalDofs;
|
|
1382
|
+
}
|
|
1383
|
+
|
|
1384
|
+
std::uint32_t rundot_kinetix_ext_articulation_max_loops() {
|
|
1385
|
+
return kArticulationMaxLoops;
|
|
1386
|
+
}
|
|
1387
|
+
|
|
1388
|
+
std::int32_t rundot_kinetix_ext_articulation_step(
|
|
1389
|
+
const double* defData,
|
|
1390
|
+
double* stateData,
|
|
1391
|
+
double dt,
|
|
1392
|
+
std::uint32_t loopIterations) {
|
|
1393
|
+
if (!defData || !stateData) return -1;
|
|
1394
|
+
const auto* def = reinterpret_cast<const ArticulationDef*>(defData);
|
|
1395
|
+
auto* state = reinterpret_cast<ArticulationState*>(stateData);
|
|
1396
|
+
return static_cast<std::int32_t>(articulationStep(*def, *state, dt, loopIterations));
|
|
1397
|
+
}
|
|
1398
|
+
|
|
1399
|
+
std::int32_t rundot_kinetix_ext_articulation_forward_kinematics(
|
|
1400
|
+
const double* defData,
|
|
1401
|
+
double* stateData) {
|
|
1402
|
+
if (!defData || !stateData) return -1;
|
|
1403
|
+
const auto* def = reinterpret_cast<const ArticulationDef*>(defData);
|
|
1404
|
+
auto* state = reinterpret_cast<ArticulationState*>(stateData);
|
|
1405
|
+
return static_cast<std::int32_t>(articulationForwardKinematics(*def, *state));
|
|
1406
|
+
}
|
|
1407
|
+
|
|
1408
|
+
std::int32_t rundot_kinetix_ext_articulation_solve_loops(
|
|
1409
|
+
const double* defData,
|
|
1410
|
+
double* stateData,
|
|
1411
|
+
double dt,
|
|
1412
|
+
std::uint32_t iterations) {
|
|
1413
|
+
if (!defData || !stateData) return -1;
|
|
1414
|
+
const auto* def = reinterpret_cast<const ArticulationDef*>(defData);
|
|
1415
|
+
auto* state = reinterpret_cast<ArticulationState*>(stateData);
|
|
1416
|
+
return static_cast<std::int32_t>(articulationSolveLoopConstraints(*def, *state, dt, iterations));
|
|
1417
|
+
}
|
|
1418
|
+
|
|
1419
|
+
std::int32_t rundot_kinetix_ext_articulation_sync_bodies(
|
|
1420
|
+
const double* defData,
|
|
1421
|
+
const double* stateData,
|
|
1422
|
+
double* outBodyData,
|
|
1423
|
+
std::uint32_t maxBodies) {
|
|
1424
|
+
if (!defData || !stateData || !outBodyData || maxBodies == 0) return -1;
|
|
1425
|
+
const auto* def = reinterpret_cast<const ArticulationDef*>(defData);
|
|
1426
|
+
const auto* state = reinterpret_cast<const ArticulationState*>(stateData);
|
|
1427
|
+
auto* bodies = reinterpret_cast<Body*>(outBodyData);
|
|
1428
|
+
articulationSyncBodies(*def, *state, std::span<Body>(bodies, maxBodies));
|
|
1429
|
+
return 0;
|
|
1430
|
+
}
|
|
1431
|
+
|
|
1432
|
+
}
|
|
1433
|
+
|
|
1434
|
+
} // namespace rundot::kinetix::physics
|