@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,291 @@
|
|
|
1
|
+
namespace triangle_adapter_private {
|
|
2
|
+
namespace tp=triangle_private;
|
|
3
|
+
exact_dyadic::Operation& arithmetic(SolverOperation& operation){
|
|
4
|
+
if(!operation.triangleArithmetic){
|
|
5
|
+
const auto& quota=operation.inputs.geometryQuota;constexpr std::uint64_t fixedScratch=1024u*1024u;
|
|
6
|
+
if(quota.totalScratchBytes<fixedScratch||quota.arithmeticRequestedWordBytes>quota.totalScratchBytes-fixedScratch
|
|
7
|
+
||quota.stagingRequestedBytes>quota.totalScratchBytes-fixedScratch-quota.arithmeticRequestedWordBytes
|
|
8
|
+
||quota.arithmeticRequestedWordBytes>std::numeric_limits<std::size_t>::max())throw TriangleNativeFailure{tp::Failure::ArithmeticExhausted};
|
|
9
|
+
operation.triangleArithmetic.emplace(exact_dyadic::Budget{std::size_t(quota.arithmeticRequestedWordBytes)});
|
|
10
|
+
}
|
|
11
|
+
return *operation.triangleArithmetic;
|
|
12
|
+
}
|
|
13
|
+
void work(SolverOperation& operation,std::uint64_t amount=1){if(!operation.triangleQuota.charge(amount))throw TriangleNativeFailure{tp::Failure::WorkExhausted};}
|
|
14
|
+
void stage(SolverOperation& operation,std::uint64_t bytes){if(bytes>operation.triangleStagingRemaining)throw TriangleNativeFailure{tp::Failure::ArithmeticExhausted};operation.triangleStagingRemaining-=bytes;}
|
|
15
|
+
template<class T>void append(SolverOperation& operation,std::vector<T>& list,T value){
|
|
16
|
+
work(operation);
|
|
17
|
+
if(list.size()==list.capacity()){
|
|
18
|
+
if(list.capacity()>list.max_size()/2)throw TriangleNativeFailure{tp::Failure::ArithmeticExhausted};
|
|
19
|
+
const auto capacity=list.capacity()?2*list.capacity():1;
|
|
20
|
+
if(capacity>std::numeric_limits<std::uint64_t>::max()/sizeof(T))throw TriangleNativeFailure{tp::Failure::ArithmeticExhausted};
|
|
21
|
+
stage(operation,capacity*sizeof(T));list.reserve(capacity);
|
|
22
|
+
}
|
|
23
|
+
list.push_back(std::move(value));
|
|
24
|
+
}
|
|
25
|
+
double sum(SolverOperation& operation,std::initializer_list<double> values,certified_sum::SumSite site){
|
|
26
|
+
work(operation,values.size());auto& op=arithmetic(operation);
|
|
27
|
+
double certified=0;
|
|
28
|
+
if(certified_sum::selectCertifiedSum(site,values,certified))return certified;
|
|
29
|
+
exact_dyadic::Dyadic total;
|
|
30
|
+
for(const auto value:values){exact_dyadic::Atom atom;exact_dyadic::Dyadic term;
|
|
31
|
+
if(exact_dyadic::Atom::validate(std::bit_cast<std::uint64_t>(value),atom)!=exact_dyadic::Status::Ok)throw TriangleNativeFailure{tp::Failure::InvalidInput};
|
|
32
|
+
if(op.fromAtom(atom,term)!=exact_dyadic::Status::Ok||op.sum(total,term,total)!=exact_dyadic::Status::Ok)throw TriangleNativeFailure{tp::Failure::ArithmeticExhausted};
|
|
33
|
+
}
|
|
34
|
+
exact_dyadic::Atom one;exact_dyadic::Dyadic denominator;std::uint64_t bits=0;
|
|
35
|
+
if(exact_dyadic::Atom::validate(std::bit_cast<std::uint64_t>(1.),one)!=exact_dyadic::Status::Ok||op.fromAtom(one,denominator)!=exact_dyadic::Status::Ok)throw TriangleNativeFailure{tp::Failure::ArithmeticExhausted};
|
|
36
|
+
const auto status=op.roundRatioBits(total,denominator,bits);
|
|
37
|
+
if(status!=exact_dyadic::RatioReadStatus::Ok)throw TriangleNativeFailure{status==exact_dyadic::RatioReadStatus::ResourceExhausted?tp::Failure::ArithmeticExhausted:tp::Failure::UnrepresentableWitness};
|
|
38
|
+
return std::bit_cast<double>(bits);
|
|
39
|
+
}
|
|
40
|
+
tp::Point worldPoint(SolverOperation& operation,const Collision& collision,std::uint32_t point){
|
|
41
|
+
const auto* basis=std::get_if<TriangleManifoldBasis>(&collision.coordinates);
|
|
42
|
+
if(basis==nullptr||collision.pointCount>basis->midpoints.size()||point>=collision.pointCount)throw TriangleNativeFailure{tp::Failure::InvalidInput};
|
|
43
|
+
tp::Point result;for(unsigned axis=0;axis<3;++axis)result[axis]=sum(operation,{basis->anchor[axis],basis->shapeOffset[axis],basis->midpoints[point][axis]},certified_sum::SumSite::WorldPoint);return result;
|
|
44
|
+
}
|
|
45
|
+
std::array<Vec3,2> levers(SolverOperation& operation,const TriangleContactBasis& basis,const Body& a,const Body& b){
|
|
46
|
+
std::array<Vec3,2> output;
|
|
47
|
+
for(unsigned endpoint=0;endpoint<2;++endpoint){const auto& body=endpoint==0?a:b;
|
|
48
|
+
const auto center=rotateByOrientation(body,{body.centerOfMassX,body.centerOfMassY,body.centerOfMassZ});
|
|
49
|
+
output[endpoint]={sum(operation,{basis.anchor[0],basis.shapeOffset[0],basis.midpoint[0],-body.x,-center.x},certified_sum::SumSite::TriangleLever),
|
|
50
|
+
sum(operation,{basis.anchor[1],basis.shapeOffset[1],basis.midpoint[1],-body.y,-center.y},certified_sum::SumSite::TriangleLever),
|
|
51
|
+
sum(operation,{basis.anchor[2],basis.shapeOffset[2],basis.midpoint[2],-body.z,-center.z},certified_sum::SumSite::TriangleLever)};
|
|
52
|
+
}
|
|
53
|
+
return output;
|
|
54
|
+
}
|
|
55
|
+
std::array<Vec3,2> levers(SolverOperation& operation,const RelativeContactBasis& basis,const Body& a,const Body& b){
|
|
56
|
+
if(const auto* triangle=std::get_if<TriangleContactBasis>(&basis.coordinates))return levers(operation,*triangle,a,b);
|
|
57
|
+
return materialContactLevers(operation,basis,a,b);
|
|
58
|
+
}
|
|
59
|
+
std::array<Vec3,2> levers(SolverOperation& operation,const Collision& collision,std::uint32_t point,const Body& a,const Body& b){
|
|
60
|
+
const auto* basis=std::get_if<TriangleManifoldBasis>(&collision.coordinates);
|
|
61
|
+
if(basis==nullptr||collision.pointCount>basis->midpoints.size()||point>=collision.pointCount)throw TriangleNativeFailure{tp::Failure::InvalidInput};
|
|
62
|
+
return levers(operation,TriangleContactBasis{basis->anchor,basis->shapeOffset,basis->midpoints[point]},a,b);
|
|
63
|
+
}
|
|
64
|
+
const RelativeContactBasis* find(SolverOperation& operation,const ContactConstraintBlock& constraint){
|
|
65
|
+
if(operation.relativeContacts.empty())return nullptr;
|
|
66
|
+
for(const auto& basis:operation.relativeContacts){work(operation);if(basis.bodyA==constraint.bodyA&&basis.bodyB==constraint.bodyB&&basis.feature==constraint.featureId)return &basis;}
|
|
67
|
+
return nullptr;
|
|
68
|
+
}
|
|
69
|
+
void retain(SolverOperation& operation,const ContactConstraintBlock& constraint,const Collision& collision,std::uint32_t point){
|
|
70
|
+
if(std::holds_alternative<WorldContactBasis>(collision.coordinates))return;
|
|
71
|
+
const auto* basis=std::get_if<TriangleManifoldBasis>(&collision.coordinates);
|
|
72
|
+
if(basis!=nullptr&&(collision.pointCount>basis->midpoints.size()||point>=collision.pointCount))throw TriangleNativeFailure{tp::Failure::InvalidInput};
|
|
73
|
+
RelativeContactBasis next=basis!=nullptr
|
|
74
|
+
?RelativeContactBasis{constraint.bodyA,constraint.bodyB,constraint.featureId,TriangleContactBasis{basis->anchor,basis->shapeOffset,basis->midpoints[point]}}
|
|
75
|
+
:materialRetainedBasis(operation,constraint,collision,point);
|
|
76
|
+
for(auto& basis:operation.relativeContacts){work(operation);if(basis.bodyA==next.bodyA&&basis.bodyB==next.bodyB&&basis.feature==next.feature){basis=next;return;}}
|
|
77
|
+
append(operation,operation.relativeContacts,std::move(next));
|
|
78
|
+
}
|
|
79
|
+
std::array<Vec3,2> collisionLevers(SolverOperation& operation,const Collision& collision,std::uint32_t pointIndex,const Body& a,const Body& b,const Vec3& point){
|
|
80
|
+
if(std::holds_alternative<TriangleManifoldBasis>(collision.coordinates))return levers(operation,collision,pointIndex,a,b);
|
|
81
|
+
if(std::holds_alternative<MaterialManifoldBasis>(collision.coordinates))return materialCollisionLevers(operation,collision,pointIndex,a,b);
|
|
82
|
+
return {subtractVec3(point,worldCenterOfMass(a)),subtractVec3(point,worldCenterOfMass(b))};
|
|
83
|
+
}
|
|
84
|
+
std::array<Vec3,2> constraintLevers(SolverOperation& operation,const ContactConstraintBlock& constraint,const Body& a,const Body& b,const std::optional<RelativeContactBasis>* explicitBasis=nullptr){
|
|
85
|
+
if(explicitBasis!=nullptr){if(*explicitBasis)return levers(operation,**explicitBasis,a,b);}
|
|
86
|
+
else if(const auto* basis=find(operation,constraint))return levers(operation,*basis,a,b);
|
|
87
|
+
const Vec3 point{constraint.point[0],constraint.point[1],constraint.point[2]};
|
|
88
|
+
return {subtractVec3(point,worldCenterOfMass(a)),subtractVec3(point,worldCenterOfMass(b))};
|
|
89
|
+
}
|
|
90
|
+
std::uint32_t ordinal(std::uint64_t offset,std::uint64_t triangle){
|
|
91
|
+
const auto value=2*std::uint64_t(offset)+triangle;
|
|
92
|
+
constexpr std::uint64_t maxPoints=12600000u/3u;
|
|
93
|
+
static_assert(2*maxPoints*42+42<0x80000000u);
|
|
94
|
+
if(value>=2*maxPoints)throw TriangleNativeFailure{tp::Failure::FeatureCapacity};return std::uint32_t(value);
|
|
95
|
+
}
|
|
96
|
+
struct Node{ShapeKind kind;std::uint32_t offset,count,format;Vec3 translation;std::array<double,3> parameters{};std::int32_t outerChild=-1;};
|
|
97
|
+
bool separatedBounds(SolverOperation& operation,const tp::Input& input){
|
|
98
|
+
for(unsigned axis=0;axis<3;++axis){
|
|
99
|
+
work(operation);
|
|
100
|
+
const auto first=input.segment[0][axis],second=input.segment[1][axis];
|
|
101
|
+
if(!finite(first)||!finite(second))return false;
|
|
102
|
+
const auto lower=std::nextafter(std::min(first,second)-input.radius,-std::numeric_limits<double>::infinity());
|
|
103
|
+
const auto upper=std::nextafter(std::max(first,second)+input.radius,std::numeric_limits<double>::infinity());
|
|
104
|
+
const auto triangleLower=std::min({input.triangle[0][axis],input.triangle[1][axis],input.triangle[2][axis]});
|
|
105
|
+
const auto triangleUpper=std::max({input.triangle[0][axis],input.triangle[1][axis],input.triangle[2][axis]});
|
|
106
|
+
if(triangleUpper<lower||triangleLower>upper)return true;
|
|
107
|
+
}
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
struct BoundInterval {
|
|
111
|
+
double lower = -std::numeric_limits<double>::infinity();
|
|
112
|
+
double upper = std::numeric_limits<double>::infinity();
|
|
113
|
+
bool finiteBounds() const { return finite(lower) && finite(upper) && lower <= upper; }
|
|
114
|
+
};
|
|
115
|
+
BoundInterval intervalAtom(double value) { return {value, value}; }
|
|
116
|
+
BoundInterval intervalSum(BoundInterval a, BoundInterval b) {
|
|
117
|
+
if (!a.finiteBounds() || !b.finiteBounds()) return {};
|
|
118
|
+
const auto lower = a.lower + b.lower, upper = a.upper + b.upper;
|
|
119
|
+
if (!finite(lower) || !finite(upper)) return {};
|
|
120
|
+
return {std::nextafter(lower, -std::numeric_limits<double>::infinity()),
|
|
121
|
+
std::nextafter(upper, std::numeric_limits<double>::infinity())};
|
|
122
|
+
}
|
|
123
|
+
BoundInterval intervalNegate(BoundInterval value) { return {-value.upper, -value.lower}; }
|
|
124
|
+
BoundInterval intervalScale(BoundInterval value, double scale) {
|
|
125
|
+
if (!value.finiteBounds() || !finite(scale)) return {};
|
|
126
|
+
const auto first = value.lower * scale, second = value.upper * scale;
|
|
127
|
+
if (!finite(first) || !finite(second)) return {};
|
|
128
|
+
return {std::nextafter(std::min(first, second), -std::numeric_limits<double>::infinity()),
|
|
129
|
+
std::nextafter(std::max(first, second), std::numeric_limits<double>::infinity())};
|
|
130
|
+
}
|
|
131
|
+
using BoundVector = std::array<BoundInterval, 3>;
|
|
132
|
+
BoundVector intervalCross(const std::array<double, 3>& q, const BoundVector& value) {
|
|
133
|
+
return {intervalSum(intervalScale(value[2], q[1]), intervalNegate(intervalScale(value[1], q[2]))),
|
|
134
|
+
intervalSum(intervalScale(value[0], q[2]), intervalNegate(intervalScale(value[2], q[0]))),
|
|
135
|
+
intervalSum(intervalScale(value[1], q[0]), intervalNegate(intervalScale(value[0], q[1])))};
|
|
136
|
+
}
|
|
137
|
+
bool separatedNode(SolverOperation& operation, const geometry_private::GeometryTriangleNode& bounds,
|
|
138
|
+
const Node& node, const Body& geometry, const Body& capsule, const tp::Input& input) {
|
|
139
|
+
work(operation);
|
|
140
|
+
const std::array<double, 3> translation{node.translation.x, node.translation.y, node.translation.z};
|
|
141
|
+
const std::array<double, 3> position{geometry.x, geometry.y, geometry.z};
|
|
142
|
+
const std::array<double, 3> offset{geometry.shapeOffsetX, geometry.shapeOffsetY, geometry.shapeOffsetZ};
|
|
143
|
+
const std::array<double, 3> capsulePosition{capsule.x, capsule.y, capsule.z};
|
|
144
|
+
const std::array<double, 3> capsuleOffset{capsule.shapeOffsetX, capsule.shapeOffsetY, capsule.shapeOffsetZ};
|
|
145
|
+
const std::array<double, 3> q{geometry.orientationX, geometry.orientationY, geometry.orientationZ};
|
|
146
|
+
BoundVector local;
|
|
147
|
+
for (unsigned axis = 0; axis < 3; ++axis) {
|
|
148
|
+
local[axis] = intervalSum({bounds.minimum[axis], bounds.maximum[axis]}, intervalAtom(translation[axis]));
|
|
149
|
+
}
|
|
150
|
+
const auto uv = intervalCross(q, local), uuv = intervalCross(q, uv);
|
|
151
|
+
BoundVector relative;
|
|
152
|
+
std::array<double, 3> lower, upper;
|
|
153
|
+
bool valid = true;
|
|
154
|
+
for (unsigned axis = 0; axis < 3; ++axis) {
|
|
155
|
+
const auto rotated = intervalSum(local[axis], intervalSum(intervalScale(uv[axis], 2. * geometry.orientationW),
|
|
156
|
+
intervalScale(uuv[axis], 2.)));
|
|
157
|
+
auto value = intervalSum(intervalAtom(position[axis]), intervalAtom(offset[axis]));
|
|
158
|
+
value = intervalSum(value, rotated);
|
|
159
|
+
value = intervalSum(value, intervalAtom(-capsulePosition[axis]));
|
|
160
|
+
relative[axis] = intervalSum(value, intervalAtom(-capsuleOffset[axis]));
|
|
161
|
+
lower[axis] = std::nextafter(std::min(input.segment[0][axis], input.segment[1][axis]) - input.radius,
|
|
162
|
+
-std::numeric_limits<double>::infinity());
|
|
163
|
+
upper[axis] = std::nextafter(std::max(input.segment[0][axis], input.segment[1][axis]) + input.radius,
|
|
164
|
+
std::numeric_limits<double>::infinity());
|
|
165
|
+
valid = valid && relative[axis].finiteBounds() && finite(lower[axis]) && finite(upper[axis]);
|
|
166
|
+
}
|
|
167
|
+
if (!valid) return false;
|
|
168
|
+
for (unsigned axis = 0; axis < 3; ++axis) {
|
|
169
|
+
if (relative[axis].upper < lower[axis] || relative[axis].lower > upper[axis]) return true;
|
|
170
|
+
}
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
Collision observeGeometry(SolverOperation& operation,const Body& capsule,const Body& geometry){
|
|
174
|
+
work(operation);
|
|
175
|
+
arithmetic(operation);
|
|
176
|
+
if(!orientation_private::validUnit(capsule.orientationX,capsule.orientationY,capsule.orientationZ,capsule.orientationW)
|
|
177
|
+
||!orientation_private::validUnit(geometry.orientationX,geometry.orientationY,geometry.orientationZ,geometry.orientationW))throw TriangleNativeFailure{tp::Failure::InvalidInput};
|
|
178
|
+
const auto payload=geometryForBody(operation,geometry);
|
|
179
|
+
if(payload.size()>12600000u||payload.size()%3)throw TriangleNativeFailure{tp::Failure::FeatureCapacity};
|
|
180
|
+
const auto capsuleAxis=rotateByOrientation(capsule,{0.,1.,0.});
|
|
181
|
+
tp::Input input;input.radius=capsule.shape0;input.projection=tp::ProjectionAssessment::RoundedNativePose;
|
|
182
|
+
input.segment={tp::Point{-capsuleAxis.x*capsule.shape1,-capsuleAxis.y*capsule.shape1,-capsuleAxis.z*capsule.shape1},tp::Point{capsuleAxis.x*capsule.shape1,capsuleAxis.y*capsule.shape1,capsuleAxis.z*capsule.shape1}};
|
|
183
|
+
Collision best;
|
|
184
|
+
std::vector<Node> pending;append(operation,pending,Node{geometry.shape,geometry.geometryOffset,geometry.geometryCount,geometry.geometryFormat,{},{geometry.shape0,geometry.shape1,geometry.shape2}});
|
|
185
|
+
auto select=[&](Collision candidate){if(candidate.hit&&(!best.hit||candidate.penetration>best.penetration
|
|
186
|
+
||(candidate.penetration==best.penetration&&candidate.featureIds[0]<best.featureIds[0])))best=std::move(candidate);};
|
|
187
|
+
auto toCollision=[&](const tp::Hit& hit,std::int32_t child){Collision collision;collision.hit=true;collision.pointCount=hit.count;collision.geometryChildIndex=child;
|
|
188
|
+
auto& basis=collision.coordinates.emplace<TriangleManifoldBasis>();basis.anchor={capsule.x,capsule.y,capsule.z};basis.shapeOffset={capsule.shapeOffsetX,capsule.shapeOffsetY,capsule.shapeOffsetZ};
|
|
189
|
+
collision.nx=hit.contacts[0].normal[0];collision.ny=hit.contacts[0].normal[1];collision.nz=hit.contacts[0].normal[2];collision.penetration=hit.contacts[0].penetration;
|
|
190
|
+
for(unsigned point=0;point<hit.count;++point){basis.midpoints[point]=hit.contacts[point].midpoint;collision.featureIds[point]=hit.contacts[point].feature;collision.points[point]=worldPoint(operation,collision,point);}
|
|
191
|
+
collision.pointX=collision.points[0][0];collision.pointY=collision.points[0][1];collision.pointZ=collision.points[0][2];return collision;
|
|
192
|
+
};
|
|
193
|
+
for(std::size_t index=0;index<pending.size();++index){
|
|
194
|
+
work(operation);const auto node=pending[index];const auto total=payload.size()/3;
|
|
195
|
+
if(node.offset>total||node.count>total-node.offset)throw TriangleNativeFailure{tp::Failure::InvalidInput};
|
|
196
|
+
auto point=[&](std::uint32_t pointIndex){work(operation);if(pointIndex>=node.count)throw TriangleNativeFailure{tp::Failure::InvalidInput};
|
|
197
|
+
const auto at=(std::size_t(node.offset)+pointIndex)*3;
|
|
198
|
+
const Vec3 local{sum(operation,{node.translation.x,payload[at]},certified_sum::SumSite::LocalTranslation),sum(operation,{node.translation.y,payload[at+1]},certified_sum::SumSite::LocalTranslation),sum(operation,{node.translation.z,payload[at+2]},certified_sum::SumSite::LocalTranslation)};
|
|
199
|
+
const auto rotated=rotateByOrientation(geometry,local);
|
|
200
|
+
return tp::Point{sum(operation,{geometry.x,geometry.shapeOffsetX,rotated.x,-capsule.x,-capsule.shapeOffsetX},certified_sum::SumSite::WorldRelative),
|
|
201
|
+
sum(operation,{geometry.y,geometry.shapeOffsetY,rotated.y,-capsule.y,-capsule.shapeOffsetY},certified_sum::SumSite::WorldRelative),sum(operation,{geometry.z,geometry.shapeOffsetZ,rotated.z,-capsule.z,-capsule.shapeOffsetZ},certified_sum::SumSite::WorldRelative)};
|
|
202
|
+
};
|
|
203
|
+
auto triangle=[&](std::uint32_t a,std::uint32_t b,std::uint32_t c,std::uint64_t triangleIndex){work(operation);input.triangle={point(a),point(b),point(c)};input.immutableTraversalOrdinal=ordinal(std::uint64_t(node.offset)+geometry.geometryFeatureOffset,triangleIndex);
|
|
204
|
+
if(separatedBounds(operation,input))return;
|
|
205
|
+
auto observation=tp::observe(input,arithmetic(operation),operation.triangleQuota);
|
|
206
|
+
if(const auto* error=std::get_if<tp::Failure>(&observation))throw TriangleNativeFailure{*error};if(const auto* hit=std::get_if<tp::Hit>(&observation))select(toCollision(*hit,node.outerChild));};
|
|
207
|
+
const auto indexedTriangles = [&]() {
|
|
208
|
+
if (geometry.geometryRecord == nullptr) return false;
|
|
209
|
+
const auto* segment = geometry.geometryRecord->triangleSegment(node.kind, node.offset, node.count, node.format);
|
|
210
|
+
if (segment == nullptr) throw TriangleNativeFailure{tp::Failure::InvalidInput};
|
|
211
|
+
const auto nodes = geometry.geometryRecord->triangleNodes();
|
|
212
|
+
const auto triangles = geometry.geometryRecord->triangles();
|
|
213
|
+
const auto visit = [&](auto&& self, std::uint32_t index) -> void {
|
|
214
|
+
const auto& bounds = nodes[index];
|
|
215
|
+
if (separatedNode(operation, bounds, node, geometry, capsule, input)) return;
|
|
216
|
+
if (bounds.count == 0) {
|
|
217
|
+
self(self, bounds.left); self(self, bounds.right);
|
|
218
|
+
} else {
|
|
219
|
+
for (std::uint32_t at = bounds.first; at < bounds.first + bounds.count; ++at) {
|
|
220
|
+
const auto& candidate = triangles[at];
|
|
221
|
+
triangle(candidate.points[0], candidate.points[1], candidate.points[2], candidate.ordinal);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
};
|
|
225
|
+
visit(visit, segment->root);
|
|
226
|
+
return true;
|
|
227
|
+
};
|
|
228
|
+
if(node.kind==ShapeKind::Mesh){if(node.format!=2||node.count<3||node.count%3)throw TriangleNativeFailure{tp::Failure::InvalidInput};
|
|
229
|
+
if (indexedTriangles()) continue;
|
|
230
|
+
for(std::uint32_t vertex=0;vertex<node.count;vertex+=3)triangle(vertex,vertex+1,vertex+2,vertex/3);continue;}
|
|
231
|
+
if(node.kind==ShapeKind::Heightfield){if(node.format!=3||node.count<5)throw TriangleNativeFailure{tp::Failure::InvalidInput};
|
|
232
|
+
const auto metadata=std::size_t(node.offset)*3;const auto columnsValue=payload[metadata],rowsValue=payload[metadata+1];
|
|
233
|
+
if(!finite(columnsValue)||!finite(rowsValue)||columnsValue<2||rowsValue<2||columnsValue>node.count||rowsValue>node.count
|
|
234
|
+
||std::floor(columnsValue)!=columnsValue||std::floor(rowsValue)!=rowsValue||payload[metadata+2]!=0.)throw TriangleNativeFailure{tp::Failure::InvalidInput};
|
|
235
|
+
const auto columns=std::uint32_t(columnsValue),rows=std::uint32_t(rowsValue);
|
|
236
|
+
if(std::uint64_t(columns)*rows+1!=node.count)throw TriangleNativeFailure{tp::Failure::InvalidInput};
|
|
237
|
+
if (indexedTriangles()) continue;
|
|
238
|
+
for(std::uint32_t row=0;row+1<rows;++row)for(std::uint32_t column=0;column+1<columns;++column){const auto first=row*columns+column;
|
|
239
|
+
triangle(first+1,first+columns+1,first+2,2*std::uint64_t(first));triangle(first+columns+2,first+2,first+columns+1,2*std::uint64_t(first)+1);}continue;
|
|
240
|
+
}
|
|
241
|
+
if(node.kind==ShapeKind::Compound&&node.format==5){
|
|
242
|
+
if(node.count<6)throw TriangleNativeFailure{tp::Failure::InvalidInput};const auto base=std::size_t(node.offset)*3;const auto countValue=payload[base+17];
|
|
243
|
+
if(!finite(countValue)||countValue<1||countValue>node.count/6||std::floor(countValue)!=countValue)throw TriangleNativeFailure{tp::Failure::InvalidInput};
|
|
244
|
+
const auto count=std::uint32_t(countValue);std::uint32_t previousEnd=count*6;
|
|
245
|
+
for(std::uint32_t child=0;child<count;++child){const auto at=base+std::size_t(child)*18;
|
|
246
|
+
auto code=[&](unsigned lane){const auto value=payload[at+lane];if(!finite(value)||value<0||value>std::numeric_limits<std::uint32_t>::max()||std::floor(value)!=value)throw TriangleNativeFailure{tp::Failure::InvalidInput};return std::uint32_t(value);};
|
|
247
|
+
const auto kind=code(0),offset=code(13),size=code(14),format=code(15);
|
|
248
|
+
if(kind>11||(payload[at+16]!=0.&&payload[at+16]!=1.)||code(17)!=count)throw TriangleNativeFailure{tp::Failure::InvalidInput};
|
|
249
|
+
const bool complex=kind==3||kind==4||kind==5||kind==6;
|
|
250
|
+
if(complex){if(offset<previousEnd||offset>node.count||size>node.count-offset)throw TriangleNativeFailure{tp::Failure::InvalidInput};previousEnd=offset+size;}
|
|
251
|
+
else if(offset||size||format)throw TriangleNativeFailure{tp::Failure::InvalidInput};
|
|
252
|
+
const Vec3 translation{sum(operation,{node.translation.x,payload[at+1]},certified_sum::SumSite::CompoundChild),sum(operation,{node.translation.y,payload[at+2]},certified_sum::SumSite::CompoundChild),sum(operation,{node.translation.z,payload[at+3]},certified_sum::SumSite::CompoundChild)};
|
|
253
|
+
if(!complex){
|
|
254
|
+
const auto translated=rotateByOrientation(geometry,translation);
|
|
255
|
+
const double cX=geometry.x+geometry.shapeOffsetX+translated.x-(capsule.x+capsule.shapeOffsetX);
|
|
256
|
+
const double cY=geometry.y+geometry.shapeOffsetY+translated.y-(capsule.y+capsule.shapeOffsetY);
|
|
257
|
+
const double cZ=geometry.z+geometry.shapeOffsetZ+translated.z-(capsule.z+capsule.shapeOffsetZ);
|
|
258
|
+
const double p0=payload[at+4],p1=payload[at+5],p2=payload[at+6];
|
|
259
|
+
const double childRadius=kind==static_cast<std::uint32_t>(ShapeKind::Box)?std::sqrt(p0*p0+p1*p1+p2*p2):(kind==static_cast<std::uint32_t>(ShapeKind::Sphere)?p0:(p0+p1));
|
|
260
|
+
const double capsuleRadius=capsule.shape0+capsule.shape1;
|
|
261
|
+
const double maxDist=childRadius+capsuleRadius;
|
|
262
|
+
if(cX*cX+cY*cY+cZ*cZ>maxDist*maxDist)continue;
|
|
263
|
+
}
|
|
264
|
+
work(operation);
|
|
265
|
+
if(child>std::uint32_t(std::numeric_limits<std::int32_t>::max()))throw TriangleNativeFailure{tp::Failure::FeatureCapacity};
|
|
266
|
+
append(operation,pending,Node{ShapeKind(kind),complex?node.offset+offset:node.offset+child*6,size,format,translation,{payload[at+4],payload[at+5],payload[at+6]},node.outerChild<0?std::int32_t(child):node.outerChild});
|
|
267
|
+
}continue;
|
|
268
|
+
}
|
|
269
|
+
if(node.kind==ShapeKind::Sphere||node.kind==ShapeKind::Box||node.kind==ShapeKind::Capsule){
|
|
270
|
+
Body localCapsule=capsule;localCapsule.x=localCapsule.y=localCapsule.z=0.;localCapsule.shapeOffsetX=localCapsule.shapeOffsetY=localCapsule.shapeOffsetZ=0.;
|
|
271
|
+
Body child=geometry;child.shape=node.kind;child.shape0=node.parameters[0];child.shape1=node.parameters[1];child.shape2=node.parameters[2];
|
|
272
|
+
const auto translated=rotateByOrientation(geometry,node.translation);
|
|
273
|
+
child.x=sum(operation,{geometry.x,geometry.shapeOffsetX,translated.x,-capsule.x,-capsule.shapeOffsetX},certified_sum::SumSite::ChildPose);child.y=sum(operation,{geometry.y,geometry.shapeOffsetY,translated.y,-capsule.y,-capsule.shapeOffsetY},certified_sum::SumSite::ChildPose);child.z=sum(operation,{geometry.z,geometry.shapeOffsetZ,translated.z,-capsule.z,-capsule.shapeOffsetZ},certified_sum::SumSite::ChildPose);
|
|
274
|
+
child.shapeOffsetX=child.shapeOffsetY=child.shapeOffsetZ=0.;
|
|
275
|
+
const double p0=child.shape0,p1=child.shape1,p2=child.shape2;
|
|
276
|
+
const double childRadius=child.shape==ShapeKind::Box?std::sqrt(p0*p0+p1*p1+p2*p2):(child.shape==ShapeKind::Sphere?p0:(p0+p1));
|
|
277
|
+
const double capsuleRadius=capsule.shape0+capsule.shape1;
|
|
278
|
+
const double maxDist=childRadius+capsuleRadius;
|
|
279
|
+
if(child.x*child.x+child.y*child.y+child.z*child.z>maxDist*maxDist)continue;
|
|
280
|
+
auto collision=node.kind==ShapeKind::Sphere?capsuleSphere(localCapsule,child):node.kind==ShapeKind::Box?capsuleBox(localCapsule,child):capsuleCapsule(localCapsule,child);
|
|
281
|
+
if(collision.hit){if(!collision.pointCount){collision.pointCount=1;collision.points[0]={collision.pointX,collision.pointY,collision.pointZ};}
|
|
282
|
+
auto& basis=collision.coordinates.emplace<TriangleManifoldBasis>();collision.geometryChildIndex=node.outerChild;basis.anchor={capsule.x,capsule.y,capsule.z};basis.shapeOffset={capsule.shapeOffsetX,capsule.shapeOffsetY,capsule.shapeOffsetZ};
|
|
283
|
+
for(unsigned pointIndex=0;pointIndex<collision.pointCount;++pointIndex){basis.midpoints[pointIndex]=collision.points[pointIndex];const auto key=std::uint64_t(ordinal(std::uint64_t(node.offset)+geometry.geometryFeatureOffset,0))*42+pointIndex+1;
|
|
284
|
+
if(key>0x7fffffffu)throw TriangleNativeFailure{tp::Failure::FeatureCapacity};collision.featureIds[pointIndex]=0x80000000u|std::uint32_t(key);collision.points[pointIndex]=worldPoint(operation,collision,pointIndex);}
|
|
285
|
+
collision.pointX=collision.points[0][0];collision.pointY=collision.points[0][1];collision.pointZ=collision.points[0][2];select(std::move(collision));}continue;
|
|
286
|
+
}
|
|
287
|
+
throw TriangleNativeFailure{tp::Failure::UnsupportedGeometry};
|
|
288
|
+
}
|
|
289
|
+
return best;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include <bit>
|
|
4
|
+
#include <cstddef>
|
|
5
|
+
#include <cstdint>
|
|
6
|
+
#include <initializer_list>
|
|
7
|
+
#include <limits>
|
|
8
|
+
|
|
9
|
+
#if defined(__clang__)
|
|
10
|
+
#pragma float_control(precise, on, push)
|
|
11
|
+
#pragma clang fp reassociate(off) contract(off)
|
|
12
|
+
#elif defined(__GNUC__)
|
|
13
|
+
#pragma GCC push_options
|
|
14
|
+
#pragma GCC optimize("no-fast-math", "fp-contract=off")
|
|
15
|
+
#else
|
|
16
|
+
#error "certified sum requires a compiler with precise floating-point control"
|
|
17
|
+
#endif
|
|
18
|
+
|
|
19
|
+
namespace rundot::kinetix::physics::certified_sum {
|
|
20
|
+
|
|
21
|
+
static_assert(std::numeric_limits<double>::is_iec559,
|
|
22
|
+
"certified sum requires IEC 559 binary64");
|
|
23
|
+
#ifdef __FLT_EVAL_METHOD__
|
|
24
|
+
static_assert(__FLT_EVAL_METHOD__ == 0, "certified sum requires FLT_EVAL_METHOD 0");
|
|
25
|
+
#else
|
|
26
|
+
static_assert(sizeof(double) == 0, "certified sum requires a defined FLT_EVAL_METHOD");
|
|
27
|
+
#endif
|
|
28
|
+
#if defined(__FAST_MATH__)
|
|
29
|
+
static_assert(sizeof(double) == 0, "certified sum rejects fast-math");
|
|
30
|
+
#endif
|
|
31
|
+
#if defined(__FINITE_MATH_ONLY__) && __FINITE_MATH_ONLY__
|
|
32
|
+
static_assert(sizeof(double) == 0, "certified sum rejects finite-math-only");
|
|
33
|
+
#endif
|
|
34
|
+
static_assert(std::bit_cast<std::uint64_t>(std::numeric_limits<double>::denorm_min()) == 1u,
|
|
35
|
+
"binary64 min subnormal must be the integer 1");
|
|
36
|
+
|
|
37
|
+
// 2^-60. The biased exponent is 1023-60. The probe compares this bit pattern.
|
|
38
|
+
inline constexpr std::uint64_t kTwoSumProbeResidualBits =
|
|
39
|
+
static_cast<std::uint64_t>(1023 - 60) << 52;
|
|
40
|
+
// 2 * 2^-1074 = 2^-1073. This bit pattern is not computed by a floating add.
|
|
41
|
+
inline constexpr std::uint64_t kTwoMinSubnormalBits = 2u;
|
|
42
|
+
inline constexpr std::uint64_t kNearestProbeBits = 0x3ff0000000000001u;
|
|
43
|
+
inline constexpr std::uint64_t kEvenTieProbeBits = 0x3ff0000000000000u;
|
|
44
|
+
inline constexpr std::uint64_t kOddTieProbeBits = 0x3ff0000000000002u;
|
|
45
|
+
|
|
46
|
+
enum class SumSite : std::uint8_t {
|
|
47
|
+
WorldPoint = 0,
|
|
48
|
+
TriangleLever = 1,
|
|
49
|
+
LocalTranslation = 2,
|
|
50
|
+
WorldRelative = 3,
|
|
51
|
+
CompoundChild = 4,
|
|
52
|
+
ChildPose = 5,
|
|
53
|
+
Count = 6
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
inline bool binary64NonFinite(double value) noexcept {
|
|
57
|
+
const auto bits = std::bit_cast<std::uint64_t>(value);
|
|
58
|
+
return ((bits >> 52) & 0x7ffu) == 0x7ffu;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
struct EnvironmentProbe {
|
|
62
|
+
std::uint64_t residualBits = 0;
|
|
63
|
+
std::uint64_t subnormalBits = 0;
|
|
64
|
+
std::uint64_t nearestBits = 0;
|
|
65
|
+
std::uint64_t evenTieBits = 0;
|
|
66
|
+
std::uint64_t oddTieBits = 0;
|
|
67
|
+
bool cancellationRejected = false;
|
|
68
|
+
bool valid = false;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
inline bool certifyExactSum(const double* values, std::size_t count, double& output) noexcept;
|
|
72
|
+
|
|
73
|
+
// Check residuals, subnormals, and the rounding mode on each call.
|
|
74
|
+
inline EnvironmentProbe environmentProbe() noexcept {
|
|
75
|
+
volatile double one = 1.0;
|
|
76
|
+
volatile double tiny = 0x1p-60;
|
|
77
|
+
volatile double rounded = one + tiny;
|
|
78
|
+
volatile double virt = rounded - one;
|
|
79
|
+
volatile double recovered = rounded - virt;
|
|
80
|
+
volatile double low = one - recovered;
|
|
81
|
+
volatile double high = tiny - virt;
|
|
82
|
+
volatile double residual = low + high;
|
|
83
|
+
volatile double unit = std::numeric_limits<double>::denorm_min();
|
|
84
|
+
volatile double subnormalSum = unit + unit;
|
|
85
|
+
volatile double aboveHalfUlp = 0x1.8p-53;
|
|
86
|
+
volatile double nearestSum = one + aboveHalfUlp;
|
|
87
|
+
volatile double halfUlp = 0x1p-53;
|
|
88
|
+
volatile double evenTie = one + halfUlp;
|
|
89
|
+
volatile double odd = std::bit_cast<double>(kNearestProbeBits);
|
|
90
|
+
volatile double oddTie = odd + halfUlp;
|
|
91
|
+
const double cancellationValues[]{tiny, one, -one};
|
|
92
|
+
double cancellationOutput = 42.0;
|
|
93
|
+
const bool cancellationAccepted = certifyExactSum(cancellationValues, 3, cancellationOutput);
|
|
94
|
+
EnvironmentProbe probe;
|
|
95
|
+
probe.residualBits = std::bit_cast<std::uint64_t>(static_cast<double>(residual));
|
|
96
|
+
probe.subnormalBits = std::bit_cast<std::uint64_t>(static_cast<double>(subnormalSum));
|
|
97
|
+
probe.nearestBits = std::bit_cast<std::uint64_t>(static_cast<double>(nearestSum));
|
|
98
|
+
probe.evenTieBits = std::bit_cast<std::uint64_t>(static_cast<double>(evenTie));
|
|
99
|
+
probe.oddTieBits = std::bit_cast<std::uint64_t>(static_cast<double>(oddTie));
|
|
100
|
+
probe.cancellationRejected = !cancellationAccepted && cancellationOutput == 42.0;
|
|
101
|
+
probe.valid = probe.residualBits == kTwoSumProbeResidualBits
|
|
102
|
+
&& probe.subnormalBits == kTwoMinSubnormalBits
|
|
103
|
+
&& probe.nearestBits == kNearestProbeBits
|
|
104
|
+
&& probe.evenTieBits == kEvenTieProbeBits
|
|
105
|
+
&& probe.oddTieBits == kOddTieProbeBits
|
|
106
|
+
&& probe.cancellationRejected;
|
|
107
|
+
return probe;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
inline bool numericEnvironmentValid() noexcept {
|
|
111
|
+
return environmentProbe().valid;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
inline bool certifyExactSum(const double* values, std::size_t count, double& output) noexcept {
|
|
115
|
+
if (values == nullptr && count != 0) return false;
|
|
116
|
+
std::size_t last = count;
|
|
117
|
+
for (std::size_t index = 0; index < count; ++index) {
|
|
118
|
+
if (binary64NonFinite(values[index])) return false;
|
|
119
|
+
if (!(values[index] == 0.0)) last = index;
|
|
120
|
+
}
|
|
121
|
+
if (last == count) {
|
|
122
|
+
output = 0.0;
|
|
123
|
+
return true;
|
|
124
|
+
}
|
|
125
|
+
double total = 0.0;
|
|
126
|
+
bool seen = false;
|
|
127
|
+
for (std::size_t index = 0; index < count; ++index) {
|
|
128
|
+
const double value = values[index];
|
|
129
|
+
if (value == 0.0) continue;
|
|
130
|
+
if (!seen) {
|
|
131
|
+
total = value;
|
|
132
|
+
seen = true;
|
|
133
|
+
if (index == last) {
|
|
134
|
+
output = 0.0;
|
|
135
|
+
if (!(total == 0.0)) output = total;
|
|
136
|
+
return true;
|
|
137
|
+
}
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
if (index == last) {
|
|
141
|
+
// The last active add is round-to-nearest-even of the exact sum.
|
|
142
|
+
volatile double result = total + value;
|
|
143
|
+
if (binary64NonFinite(result)) return false;
|
|
144
|
+
const double rounded = result;
|
|
145
|
+
output = 0.0;
|
|
146
|
+
if (!(rounded == 0.0)) output = rounded;
|
|
147
|
+
return true;
|
|
148
|
+
}
|
|
149
|
+
// TwoSum. A residual other than 0 means this add is not exact.
|
|
150
|
+
volatile double rounded = total + value;
|
|
151
|
+
if (binary64NonFinite(rounded)) return false;
|
|
152
|
+
volatile double virt = rounded - total;
|
|
153
|
+
if (binary64NonFinite(virt)) return false;
|
|
154
|
+
volatile double recovered = rounded - virt;
|
|
155
|
+
volatile double low = total - recovered;
|
|
156
|
+
volatile double high = value - virt;
|
|
157
|
+
volatile double residual = low + high;
|
|
158
|
+
if (binary64NonFinite(low) || binary64NonFinite(high) || binary64NonFinite(residual)
|
|
159
|
+
|| !(residual == 0.0)) return false;
|
|
160
|
+
total = rounded;
|
|
161
|
+
}
|
|
162
|
+
return false;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
#if defined(RUNDOT_CERTIFIED_SUM_TEST_COUNTERS)
|
|
166
|
+
struct SumSiteCounters {
|
|
167
|
+
std::uint64_t accepted[static_cast<unsigned>(SumSite::Count)]{};
|
|
168
|
+
std::uint64_t fallback[static_cast<unsigned>(SumSite::Count)]{};
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
inline SumSiteCounters& sumSiteCounters() noexcept {
|
|
172
|
+
static SumSiteCounters counters;
|
|
173
|
+
return counters;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
inline bool& forceLegacySum() noexcept {
|
|
177
|
+
static bool force = false;
|
|
178
|
+
return force;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
inline void resetSumSiteCounters() noexcept {
|
|
182
|
+
sumSiteCounters() = {};
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
inline void noteSumSite(SumSite site, bool accepted) noexcept {
|
|
186
|
+
const auto index = static_cast<unsigned>(site);
|
|
187
|
+
if (index >= static_cast<unsigned>(SumSite::Count)) return;
|
|
188
|
+
if (accepted) ++sumSiteCounters().accepted[index];
|
|
189
|
+
else ++sumSiteCounters().fallback[index];
|
|
190
|
+
}
|
|
191
|
+
#endif
|
|
192
|
+
|
|
193
|
+
inline bool selectCertifiedSum(SumSite site, const double* values, std::size_t count, double& output) noexcept {
|
|
194
|
+
double staged = 0.0;
|
|
195
|
+
const bool certified = numericEnvironmentValid() && certifyExactSum(values, count, staged);
|
|
196
|
+
#if defined(RUNDOT_CERTIFIED_SUM_TEST_COUNTERS)
|
|
197
|
+
const bool useFast = certified && !forceLegacySum();
|
|
198
|
+
noteSumSite(site, useFast);
|
|
199
|
+
#else
|
|
200
|
+
(void)site;
|
|
201
|
+
const bool useFast = certified;
|
|
202
|
+
#endif
|
|
203
|
+
if (!useFast) return false;
|
|
204
|
+
output = staged;
|
|
205
|
+
return true;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
inline bool selectCertifiedSum(SumSite site, std::initializer_list<double> values, double& output) noexcept {
|
|
209
|
+
return selectCertifiedSum(site, values.begin(), values.size(), output);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
#if defined(__clang__)
|
|
215
|
+
#pragma float_control(pop)
|
|
216
|
+
#elif defined(__GNUC__)
|
|
217
|
+
#pragma GCC pop_options
|
|
218
|
+
#endif
|