@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,254 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include <cstdint>
|
|
4
|
+
#include <span>
|
|
5
|
+
|
|
6
|
+
#include "physics.hpp"
|
|
7
|
+
|
|
8
|
+
namespace rundot::kinetix::physics {
|
|
9
|
+
|
|
10
|
+
constexpr std::uint32_t kBuoyancyInputStride = 19;
|
|
11
|
+
constexpr std::uint32_t kBuoyancyOutputStride = 10;
|
|
12
|
+
constexpr std::uint32_t kFluidForcesInputStride = 51;
|
|
13
|
+
constexpr std::uint32_t kFluidForcesOutputStride = 27;
|
|
14
|
+
constexpr std::uint32_t kFluidVolumeInputStride = 20;
|
|
15
|
+
constexpr std::uint32_t kFluidStateForcesInputStride = 353;
|
|
16
|
+
|
|
17
|
+
enum class BuoyancyStatus : std::int32_t {
|
|
18
|
+
Success = 0,
|
|
19
|
+
InvalidInput = -1,
|
|
20
|
+
UnsupportedShape = -2,
|
|
21
|
+
OutputTooSmall = -3,
|
|
22
|
+
InvalidBounds = -4,
|
|
23
|
+
InvalidPlaneNormal = -5,
|
|
24
|
+
InvalidDensity = -6,
|
|
25
|
+
OverlapUnsupported = -7,
|
|
26
|
+
NumericRange = -8,
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
struct BuoyancyVector3 {
|
|
30
|
+
double x = 0.0;
|
|
31
|
+
double y = 0.0;
|
|
32
|
+
double z = 0.0;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
struct BuoyancyQuaternion {
|
|
36
|
+
double x = 0.0;
|
|
37
|
+
double y = 0.0;
|
|
38
|
+
double z = 0.0;
|
|
39
|
+
double w = 1.0;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
struct SubmergedGeometry {
|
|
43
|
+
double volume = 0.0;
|
|
44
|
+
BuoyancyVector3 center{};
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
struct BuoyancyInput {
|
|
48
|
+
ShapeKind shape = ShapeKind::Box;
|
|
49
|
+
BuoyancyVector3 shapeCenter{};
|
|
50
|
+
BuoyancyQuaternion orientation{};
|
|
51
|
+
BuoyancyVector3 shapeSize{};
|
|
52
|
+
double waterY = 0.0;
|
|
53
|
+
double density = 1.0;
|
|
54
|
+
BuoyancyVector3 gravity{};
|
|
55
|
+
BuoyancyVector3 bodyCenter{};
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
struct BuoyancyResult {
|
|
59
|
+
SubmergedGeometry submerged{};
|
|
60
|
+
BuoyancyVector3 force{};
|
|
61
|
+
BuoyancyVector3 torque{};
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
BuoyancyStatus calculateSubmergedGeometry(
|
|
65
|
+
ShapeKind shape,
|
|
66
|
+
const BuoyancyVector3& shapeCenter,
|
|
67
|
+
const BuoyancyQuaternion& orientation,
|
|
68
|
+
const BuoyancyVector3& shapeSize,
|
|
69
|
+
double waterY,
|
|
70
|
+
SubmergedGeometry& output);
|
|
71
|
+
|
|
72
|
+
BuoyancyStatus calculateBuoyancy(
|
|
73
|
+
const BuoyancyInput& input,
|
|
74
|
+
BuoyancyResult& output);
|
|
75
|
+
|
|
76
|
+
struct FluidPlane3D {
|
|
77
|
+
bool enabled = false;
|
|
78
|
+
BuoyancyVector3 normal{0.0, 1.0, 0.0};
|
|
79
|
+
double offset = 0.0;
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
struct FluidBounds3D {
|
|
83
|
+
double minX = 0.0;
|
|
84
|
+
double maxX = 0.0;
|
|
85
|
+
double minY = 0.0;
|
|
86
|
+
double maxY = 0.0;
|
|
87
|
+
double minZ = 0.0;
|
|
88
|
+
double maxZ = 0.0;
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
enum class FluidOverlapPolicy : std::int32_t {
|
|
92
|
+
RejectOverlap = 0,
|
|
93
|
+
PriorityPartition = 1,
|
|
94
|
+
Accumulate = 2,
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
struct FluidVolume3D {
|
|
98
|
+
std::uint32_t id = 0;
|
|
99
|
+
FluidBounds3D bounds{};
|
|
100
|
+
FluidPlane3D plane{};
|
|
101
|
+
double density = 1.0;
|
|
102
|
+
double linearDrag = 0.0;
|
|
103
|
+
double angularDrag = 0.0;
|
|
104
|
+
BuoyancyVector3 flowVelocity{};
|
|
105
|
+
std::uint32_t layer = 1;
|
|
106
|
+
std::uint32_t mask = 1;
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
constexpr std::size_t kMaxFluidVolumes3D = 16;
|
|
110
|
+
|
|
111
|
+
struct FluidState3D {
|
|
112
|
+
std::uint32_t volumeCount = 0;
|
|
113
|
+
FluidOverlapPolicy overlapPolicy = FluidOverlapPolicy::RejectOverlap;
|
|
114
|
+
FluidVolume3D volumes[kMaxFluidVolumes3D]{};
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
struct FluidBodyInput3D {
|
|
118
|
+
ShapeKind shape = ShapeKind::Box;
|
|
119
|
+
BuoyancyVector3 position{};
|
|
120
|
+
BuoyancyQuaternion orientation{};
|
|
121
|
+
BuoyancyVector3 size{};
|
|
122
|
+
BuoyancyVector3 centerOfMass{}; // local offset from position
|
|
123
|
+
BuoyancyVector3 linearVelocity{};
|
|
124
|
+
BuoyancyVector3 angularVelocity{};
|
|
125
|
+
double mass = 1.0;
|
|
126
|
+
BuoyancyVector3 inertiaDiag{1.0, 1.0, 1.0};
|
|
127
|
+
double gravityScale = 1.0;
|
|
128
|
+
bool sleeping = false;
|
|
129
|
+
std::uint32_t layer = 1;
|
|
130
|
+
std::uint32_t mask = 1;
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
struct VoxelBoxShape3D {
|
|
134
|
+
BuoyancyVector3 halfSize{};
|
|
135
|
+
BuoyancyVector3 offset{};
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
struct FluidForcesResult3D {
|
|
139
|
+
SubmergedGeometry submerged{};
|
|
140
|
+
BuoyancyVector3 buoyantForce{};
|
|
141
|
+
BuoyancyVector3 buoyantTorque{};
|
|
142
|
+
BuoyancyVector3 dragForce{};
|
|
143
|
+
BuoyancyVector3 dragTorque{};
|
|
144
|
+
BuoyancyVector3 totalForce{};
|
|
145
|
+
BuoyancyVector3 totalTorque{};
|
|
146
|
+
BuoyancyVector3 relativeVelocity{};
|
|
147
|
+
bool shouldWake = false;
|
|
148
|
+
bool isInEquilibrium = false;
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
BuoyancyStatus validateFluidVolume(const FluidVolume3D& volume);
|
|
152
|
+
|
|
153
|
+
BuoyancyStatus validateFluidState(const FluidState3D& state);
|
|
154
|
+
|
|
155
|
+
BuoyancyStatus calculateSubmergedGeometryWithPlane(
|
|
156
|
+
ShapeKind shape,
|
|
157
|
+
const BuoyancyVector3& shapeCenter,
|
|
158
|
+
const BuoyancyQuaternion& orientation,
|
|
159
|
+
const BuoyancyVector3& shapeSize,
|
|
160
|
+
const FluidPlane3D& plane,
|
|
161
|
+
const FluidBounds3D& bounds,
|
|
162
|
+
SubmergedGeometry& output);
|
|
163
|
+
|
|
164
|
+
BuoyancyStatus calculateFluidForces(
|
|
165
|
+
const FluidBodyInput3D& body,
|
|
166
|
+
const FluidVolume3D& volume,
|
|
167
|
+
const BuoyancyVector3& gravity,
|
|
168
|
+
FluidForcesResult3D& output);
|
|
169
|
+
|
|
170
|
+
BuoyancyStatus validateVoxelBoxes(std::span<const VoxelBoxShape3D> boxes);
|
|
171
|
+
|
|
172
|
+
BuoyancyStatus calculateVoxelBuoyancy(
|
|
173
|
+
const BuoyancyInput& input,
|
|
174
|
+
std::span<const VoxelBoxShape3D> boxes,
|
|
175
|
+
BuoyancyResult& output);
|
|
176
|
+
|
|
177
|
+
BuoyancyStatus calculateVoxelFluidForces(
|
|
178
|
+
const FluidBodyInput3D& body,
|
|
179
|
+
std::span<const VoxelBoxShape3D> boxes,
|
|
180
|
+
const FluidVolume3D& volume,
|
|
181
|
+
const BuoyancyVector3& gravity,
|
|
182
|
+
FluidForcesResult3D& output);
|
|
183
|
+
|
|
184
|
+
BuoyancyStatus calculateValidatedVoxelFluidForces(
|
|
185
|
+
const FluidBodyInput3D& body,
|
|
186
|
+
std::span<const VoxelBoxShape3D> boxes,
|
|
187
|
+
const FluidVolume3D& volume,
|
|
188
|
+
const BuoyancyVector3& gravity,
|
|
189
|
+
FluidForcesResult3D& output);
|
|
190
|
+
|
|
191
|
+
BuoyancyStatus calculateVoxelFluidStateForces(
|
|
192
|
+
const FluidBodyInput3D& body,
|
|
193
|
+
std::span<const VoxelBoxShape3D> boxes,
|
|
194
|
+
const FluidState3D& state,
|
|
195
|
+
const BuoyancyVector3& gravity,
|
|
196
|
+
FluidForcesResult3D& output);
|
|
197
|
+
|
|
198
|
+
BuoyancyStatus stepBodyInFluids(
|
|
199
|
+
FluidBodyInput3D& body,
|
|
200
|
+
const FluidState3D& state,
|
|
201
|
+
const BuoyancyVector3& gravity,
|
|
202
|
+
double dt,
|
|
203
|
+
FluidForcesResult3D* stepResult = nullptr);
|
|
204
|
+
|
|
205
|
+
} // namespace rundot::kinetix::physics
|
|
206
|
+
|
|
207
|
+
extern "C" std::int32_t rundot_kinetix_physics_buoyancy(
|
|
208
|
+
const double* input,
|
|
209
|
+
std::uint32_t input_values,
|
|
210
|
+
double* output,
|
|
211
|
+
std::uint32_t output_values);
|
|
212
|
+
|
|
213
|
+
extern "C" std::uint32_t rundot_kinetix_physics_buoyancy_abi_version();
|
|
214
|
+
extern "C" std::uint32_t rundot_kinetix_physics_buoyancy_input_stride();
|
|
215
|
+
extern "C" std::uint32_t rundot_kinetix_physics_buoyancy_output_stride();
|
|
216
|
+
|
|
217
|
+
extern "C" std::uint32_t rundot_kinetix_physics_fluid_abi_version();
|
|
218
|
+
extern "C" std::uint32_t rundot_kinetix_physics_fluid_forces_input_stride();
|
|
219
|
+
extern "C" std::uint32_t rundot_kinetix_physics_fluid_forces_output_stride();
|
|
220
|
+
extern "C" std::uint32_t rundot_kinetix_physics_fluid_volume_input_stride();
|
|
221
|
+
extern "C" std::uint32_t rundot_kinetix_physics_fluid_state_forces_input_stride();
|
|
222
|
+
extern "C" std::int32_t rundot_kinetix_physics_fluid_forces(
|
|
223
|
+
const double* input,
|
|
224
|
+
std::uint32_t input_values,
|
|
225
|
+
double* output,
|
|
226
|
+
std::uint32_t output_values);
|
|
227
|
+
extern "C" std::int32_t rundot_kinetix_physics_fluid_state_forces(
|
|
228
|
+
const double* input,
|
|
229
|
+
std::uint32_t input_values,
|
|
230
|
+
double* output,
|
|
231
|
+
std::uint32_t output_values);
|
|
232
|
+
extern "C" std::int32_t rundot_kinetix_physics_voxel_buoyancy(
|
|
233
|
+
const double* input,
|
|
234
|
+
std::uint32_t input_values,
|
|
235
|
+
const double* children,
|
|
236
|
+
std::uint32_t child_values,
|
|
237
|
+
double* output,
|
|
238
|
+
std::uint32_t output_values);
|
|
239
|
+
extern "C" std::int32_t rundot_kinetix_physics_voxel_fluid_forces(
|
|
240
|
+
const double* input,
|
|
241
|
+
std::uint32_t input_values,
|
|
242
|
+
const double* children,
|
|
243
|
+
std::uint32_t child_values,
|
|
244
|
+
double* output,
|
|
245
|
+
std::uint32_t output_values);
|
|
246
|
+
extern "C" std::int32_t rundot_kinetix_physics_validate_fluid_volume(
|
|
247
|
+
const rundot::kinetix::physics::FluidVolume3D* volume);
|
|
248
|
+
extern "C" std::int32_t rundot_kinetix_physics_validate_fluid_state(
|
|
249
|
+
const rundot::kinetix::physics::FluidState3D* state);
|
|
250
|
+
extern "C" std::int32_t rundot_kinetix_physics_calculate_fluid_forces(
|
|
251
|
+
const rundot::kinetix::physics::FluidBodyInput3D* body,
|
|
252
|
+
const rundot::kinetix::physics::FluidVolume3D* volume,
|
|
253
|
+
const double* gravityXyz,
|
|
254
|
+
rundot::kinetix::physics::FluidForcesResult3D* output);
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
#include "capsule_triangle.hpp"
|
|
2
|
+
#include <algorithm>
|
|
3
|
+
#include <bit>
|
|
4
|
+
#include <cmath>
|
|
5
|
+
#include <limits>
|
|
6
|
+
#include <stdexcept>
|
|
7
|
+
|
|
8
|
+
namespace rundot::kinetix::physics::triangle_private {
|
|
9
|
+
namespace {
|
|
10
|
+
namespace ed=exact_dyadic;
|
|
11
|
+
struct Stop{Failure failure;};
|
|
12
|
+
struct Rat{ed::Dyadic n,d;};
|
|
13
|
+
using Vec=std::array<Rat,3>;
|
|
14
|
+
struct Candidate{Vec segment,triangle;Rat parameter;std::array<Rat,3> bary;Rat squared;std::uint8_t tf=0,sf=0;};
|
|
15
|
+
struct Kernel{
|
|
16
|
+
ed::Operation& op;CallQuota& quota;
|
|
17
|
+
static void checked(ed::Status status){if(status!=ed::Status::Ok)throw Stop{status==ed::Status::ResourceExhausted?Failure::ArithmeticExhausted:Failure::InvalidInput};}
|
|
18
|
+
void work(std::uint64_t count=1){if(!quota.charge(count))throw Stop{Failure::WorkExhausted};}
|
|
19
|
+
ed::Dyadic atom(double value){work();ed::Atom a;checked(ed::Atom::validate(std::bit_cast<std::uint64_t>(value),a));ed::Dyadic out;checked(op.fromAtom(a,out));return out;}
|
|
20
|
+
Rat value(double x){return {atom(x),atom(1.)};}
|
|
21
|
+
Rat clone(const Rat& a){work();Rat out;checked(op.clone(a.n,out.n));checked(op.clone(a.d,out.d));return out;}
|
|
22
|
+
Vec clone(const Vec& a){return {clone(a[0]),clone(a[1]),clone(a[2])};}
|
|
23
|
+
std::array<Rat,3> cloneBary(const std::array<Rat,3>& a){return {clone(a[0]),clone(a[1]),clone(a[2])};}
|
|
24
|
+
int sign(const Rat& a)const noexcept{return op.sign(a.n)==ed::Sign::Zero?0:op.sign(a.n)==ed::Sign::Positive?1:-1;}
|
|
25
|
+
Rat negate(const Rat& a){return mul(a,value(-1.));}
|
|
26
|
+
Rat add(const Rat& a,const Rat& b){work();Rat out;ed::Dyadic left,right;checked(op.product(a.n,b.d,left));checked(op.product(b.n,a.d,right));checked(op.sum(left,right,out.n));checked(op.product(a.d,b.d,out.d));return out;}
|
|
27
|
+
Rat sub(const Rat& a,const Rat& b){work();Rat out;ed::Dyadic left,right;checked(op.product(a.n,b.d,left));checked(op.product(b.n,a.d,right));checked(op.difference(left,right,out.n));checked(op.product(a.d,b.d,out.d));return out;}
|
|
28
|
+
Rat mul(const Rat& a,const Rat& b){work();Rat out;checked(op.product(a.n,b.n,out.n));checked(op.product(a.d,b.d,out.d));return out;}
|
|
29
|
+
Rat div(const Rat& a,const Rat& b){work();if(!sign(b))throw Stop{Failure::InvalidInput};Rat out;
|
|
30
|
+
checked(op.product(a.n,b.d,out.n));checked(op.product(a.d,b.n,out.d));
|
|
31
|
+
if(op.sign(out.d)==ed::Sign::Negative){auto minus=atom(-1.);checked(op.product(out.n,minus,out.n));checked(op.product(out.d,minus,out.d));}return out;}
|
|
32
|
+
int compare(const Rat& a,const Rat& b){work();ed::Dyadic left,right;int result=0;checked(op.product(a.n,b.d,left));checked(op.product(b.n,a.d,right));checked(op.compare(left,right,result));return result;}
|
|
33
|
+
Vec from(Point p){return {value(p[0]),value(p[1]),value(p[2])};}
|
|
34
|
+
Vec add(const Vec& a,const Vec& b){return {add(a[0],b[0]),add(a[1],b[1]),add(a[2],b[2])};}
|
|
35
|
+
Vec sub(const Vec& a,const Vec& b){return {sub(a[0],b[0]),sub(a[1],b[1]),sub(a[2],b[2])};}
|
|
36
|
+
Vec mul(const Vec& a,const Rat& b){return {mul(a[0],b),mul(a[1],b),mul(a[2],b)};}
|
|
37
|
+
Vec cross(const Vec& a,const Vec& b){return {sub(mul(a[1],b[2]),mul(a[2],b[1])),sub(mul(a[2],b[0]),mul(a[0],b[2])),sub(mul(a[0],b[1]),mul(a[1],b[0]))};}
|
|
38
|
+
Rat dot(const Vec& a,const Vec& b){return add(add(mul(a[0],b[0]),mul(a[1],b[1])),mul(a[2],b[2]));}
|
|
39
|
+
Vec interpolate(const Vec& a,const Vec& b,const Rat& t){return add(a,mul(sub(b,a),t));}
|
|
40
|
+
Rat clamp01(const Rat& a){const auto zero=value(0.),one=value(1.);if(compare(a,zero)<0)return clone(zero);if(compare(a,one)>0)return clone(one);return clone(a);}
|
|
41
|
+
std::uint8_t segmentFeature(const Rat& a){return compare(a,value(0.))==0?0:compare(a,value(1.))==0?2:1;}
|
|
42
|
+
std::uint8_t triangleFeature(const std::array<Rat,3>& b){
|
|
43
|
+
const bool z0=sign(b[0])==0,z1=sign(b[1])==0,z2=sign(b[2])==0;
|
|
44
|
+
if(z1&&z2)return 4;if(z0&&z2)return 5;if(z0&&z1)return 6;
|
|
45
|
+
if(z2)return 1;if(z0)return 2;if(z1)return 3;return 0;
|
|
46
|
+
}
|
|
47
|
+
Candidate candidate(Vec segment,Vec triangle,Rat t,std::array<Rat,3> bary){Candidate out;
|
|
48
|
+
out.sf=segmentFeature(t);out.tf=triangleFeature(bary);auto difference=sub(triangle,segment);out.squared=dot(difference,difference);
|
|
49
|
+
out.segment=std::move(segment);out.triangle=std::move(triangle);out.parameter=std::move(t);out.bary=std::move(bary);return out;}
|
|
50
|
+
Candidate copy(const Candidate& a){return candidate(clone(a.segment),clone(a.triangle),clone(a.parameter),cloneBary(a.bary));}
|
|
51
|
+
struct Minima{std::array<Candidate,2> values;std::uint32_t count=0;};
|
|
52
|
+
void consider(Minima& minima,Candidate next){work();if(!minima.count){minima.values[0]=std::move(next);minima.count=1;return;}
|
|
53
|
+
const auto comparison=compare(next.squared,minima.values[0].squared);if(comparison>0)return;
|
|
54
|
+
if(comparison<0){minima.values[0]=std::move(next);minima.count=1;return;}
|
|
55
|
+
const auto order=compare(next.parameter,minima.values[0].parameter);
|
|
56
|
+
if(order==0){if(next.tf<minima.values[0].tf)minima.values[0]=std::move(next);return;}
|
|
57
|
+
if(order<0){if(minima.count==1){minima.values[1]=std::move(minima.values[0]);minima.count=2;}minima.values[0]=std::move(next);}
|
|
58
|
+
else if(minima.count==1||compare(next.parameter,minima.values[1].parameter)>0){minima.values[1]=std::move(next);minima.count=2;}
|
|
59
|
+
}
|
|
60
|
+
std::array<Rat,3> edgeBary(unsigned edge,const Rat& t){std::array<Rat,3> out{value(0.),value(0.),value(0.)};out[edge]=sub(value(1.),t);out[(edge+1)%3]=clone(t);return out;}
|
|
61
|
+
void segmentEdge(Minima& minima,const std::array<Vec,2>& segment,const std::array<Vec,3>& triangle,unsigned edge){
|
|
62
|
+
const auto& p=segment[0];const auto& q=triangle[edge];const auto u=sub(segment[1],p),v=sub(triangle[(edge+1)%3],q),w=sub(p,q);
|
|
63
|
+
const auto a=dot(u,u),b=dot(u,v),c=dot(v,v),d=dot(u,w),e=dot(v,w);
|
|
64
|
+
const auto determinant=sub(mul(a,c),mul(b,b));
|
|
65
|
+
auto insert=[&](Rat s,Rat t){consider(minima,candidate(interpolate(segment[0],segment[1],s),interpolate(triangle[edge],triangle[(edge+1)%3],t),clone(s),edgeBary(edge,t)));};
|
|
66
|
+
for(unsigned endpoint=0;endpoint<2;++endpoint){auto s=value(endpoint);auto t=sign(c)?clamp01(div(add(e,mul(b,s)),c)):value(0.);insert(std::move(s),std::move(t));}
|
|
67
|
+
for(unsigned endpoint=0;endpoint<2;++endpoint){auto t=value(endpoint);auto s=sign(a)?clamp01(div(sub(mul(b,t),d),a)):value(0.);insert(std::move(s),std::move(t));}
|
|
68
|
+
if(sign(determinant)>0){auto s=div(sub(mul(b,e),mul(c,d)),determinant),t=div(sub(mul(a,e),mul(b,d)),determinant);
|
|
69
|
+
if(compare(s,value(0.))>=0&&compare(s,value(1.))<=0&&compare(t,value(0.))>=0&&compare(t,value(1.))<=0)insert(std::move(s),std::move(t));}
|
|
70
|
+
}
|
|
71
|
+
bool barycentric(const Vec& p,const std::array<Vec,3>& triangle,std::array<Rat,3>& out){
|
|
72
|
+
const auto u=sub(triangle[1],triangle[0]),v=sub(triangle[2],triangle[0]),w=sub(p,triangle[0]);
|
|
73
|
+
const auto uu=dot(u,u),uv=dot(u,v),vv=dot(v,v),wu=dot(w,u),wv=dot(w,v),den=sub(mul(uu,vv),mul(uv,uv));
|
|
74
|
+
if(sign(den)<=0)return false;auto b=div(sub(mul(wu,vv),mul(wv,uv)),den),c=div(sub(mul(wv,uu),mul(wu,uv)),den),a=sub(sub(value(1.),b),c);
|
|
75
|
+
if(sign(a)<0||sign(b)<0||sign(c)<0)return false;out={std::move(a),std::move(b),std::move(c)};return true;
|
|
76
|
+
}
|
|
77
|
+
Minima distance(const std::array<Vec,2>& segment,const std::array<Vec,3>& triangle){
|
|
78
|
+
Minima minima;const auto n=cross(sub(triangle[1],triangle[0]),sub(triangle[2],triangle[0]));const auto nn=dot(n,n);
|
|
79
|
+
if(!sign(nn)){
|
|
80
|
+
unsigned longest=0;auto length=dot(sub(triangle[1],triangle[0]),sub(triangle[1],triangle[0]));
|
|
81
|
+
for(unsigned edge=1;edge<3;++edge){auto delta=sub(triangle[(edge+1)%3],triangle[edge]);auto square=dot(delta,delta);if(compare(square,length)>0){longest=edge;length=std::move(square);}}
|
|
82
|
+
segmentEdge(minima,segment,triangle,longest);return minima;
|
|
83
|
+
}
|
|
84
|
+
for(unsigned edge=0;edge<3;++edge)segmentEdge(minima,segment,triangle,edge);
|
|
85
|
+
for(unsigned endpoint=0;endpoint<2;++endpoint){
|
|
86
|
+
const auto height=div(dot(n,sub(segment[endpoint],triangle[0])),nn);auto projection=sub(segment[endpoint],mul(n,height));std::array<Rat,3> bary;
|
|
87
|
+
if(barycentric(projection,triangle,bary))consider(minima,candidate(clone(segment[endpoint]),std::move(projection),value(endpoint),std::move(bary)));
|
|
88
|
+
}
|
|
89
|
+
const auto direction=sub(segment[1],segment[0]);const auto den=dot(n,direction);
|
|
90
|
+
if(sign(den)){auto t=div(dot(n,sub(triangle[0],segment[0])),den);
|
|
91
|
+
if(compare(t,value(0.))>=0&&compare(t,value(1.))<=0){auto point=interpolate(segment[0],segment[1],t);std::array<Rat,3> bary;
|
|
92
|
+
if(barycentric(point,triangle,bary))consider(minima,candidate(clone(point),std::move(point),std::move(t),std::move(bary)));}}
|
|
93
|
+
if(!minima.count)throw Stop{Failure::ProjectionUnresolved};return minima;
|
|
94
|
+
}
|
|
95
|
+
ExactRatio capture(const Rat& a){work();ExactRatio out;checked(op.capture(a.n,out.numerator));checked(op.capture(a.d,out.positiveDenominator));return out;}
|
|
96
|
+
double read(const Rat& a){work();std::uint64_t bits=0;const auto status=op.roundRatioBits(a.n,a.d,bits);
|
|
97
|
+
if(status!=ed::RatioReadStatus::Ok)throw Stop{status==ed::RatioReadStatus::ResourceExhausted?Failure::ArithmeticExhausted:Failure::UnrepresentableWitness};return std::bit_cast<double>(bits);}
|
|
98
|
+
Point read(const Vec& v){return {read(v[0]),read(v[1]),read(v[2])};}
|
|
99
|
+
static std::int64_t exponent(const ed::Encoding& a){
|
|
100
|
+
if(a.exponent.magnitude.size()>1||(!a.exponent.magnitude.empty()&&a.exponent.magnitude[0]>std::uint64_t(std::numeric_limits<std::int64_t>::max())))throw Stop{Failure::UnrepresentableWitness};
|
|
101
|
+
const auto magnitude=a.exponent.magnitude.empty()?0:std::int64_t(a.exponent.magnitude[0]);return a.exponent.sign==ed::Sign::Negative?-magnitude:magnitude;
|
|
102
|
+
}
|
|
103
|
+
static std::int64_t leading(const ed::Encoding& a){if(a.oddMagnitude.empty())throw Stop{Failure::InvalidInput};
|
|
104
|
+
if(a.oddMagnitude.size()>std::uint64_t(std::numeric_limits<std::int64_t>::max()/64))throw Stop{Failure::UnrepresentableWitness};
|
|
105
|
+
return exponent(a)+std::int64_t((a.oddMagnitude.size()-1)*64+std::bit_width(a.oddMagnitude.back())-1);
|
|
106
|
+
}
|
|
107
|
+
Rat scalePower(const Rat& a,std::int64_t power){auto out=clone(a);while(power){work();const auto step=std::clamp<std::int64_t>(power,-1022,1023);out=mul(out,value(std::scalbn(1.,int(step))));power-=step;}return out;}
|
|
108
|
+
struct RootRead{double scaled;std::int64_t power;};
|
|
109
|
+
RootRead squareRoot(const Rat& a){if(sign(a)<=0)throw Stop{Failure::InvalidInput};const auto saved=capture(a);auto binary=leading(saved.numerator)-leading(saved.positiveDenominator);
|
|
110
|
+
const auto half=binary>=0?binary/2:-((-binary+1)/2);const auto scaled=read(scalePower(a,-2*half));
|
|
111
|
+
if(!(scaled>0.)||!std::isfinite(scaled))throw Stop{Failure::UnrepresentableWitness};return {std::sqrt(scaled),half};}
|
|
112
|
+
double rootValue(RootRead value){if(value.power<std::numeric_limits<int>::min()||value.power>std::numeric_limits<int>::max())throw Stop{Failure::UnrepresentableWitness};
|
|
113
|
+
const auto result=std::scalbn(value.scaled,int(value.power));if(!(result>0.)||!std::isfinite(result))throw Stop{Failure::UnrepresentableWitness};return result;}
|
|
114
|
+
Point normal(const Vec& vector,const Rat& square){const auto length=squareRoot(square);auto out=read(mul(vector,scalePower(value(1.),-length.power)));
|
|
115
|
+
for(auto& component:out){component/=length.scaled;if(!std::isfinite(component))throw Stop{Failure::UnrepresentableWitness};}return out;}
|
|
116
|
+
Contact emit(const Candidate& candidate,const Vec& originalSegment,Point n,double penetration,std::uint32_t ordinal,unsigned interval){
|
|
117
|
+
work();Contact out;out.segmentCore=read(originalSegment);out.triangleCore=read(candidate.triangle);out.normal=n;out.penetration=penetration;
|
|
118
|
+
out.authority.segment=capture(candidate.parameter);for(unsigned axis=0;axis<3;++axis)out.authority.triangle[axis]=capture(candidate.bary[axis]);
|
|
119
|
+
out.authority.triangleFeature=candidate.tf;out.authority.segmentFeature=candidate.sf;
|
|
120
|
+
constexpr std::uint64_t featureWidth=42;const auto feature=std::uint64_t(ordinal)*featureWidth+(candidate.tf*3+candidate.sf)*2+interval+1;
|
|
121
|
+
if(feature>0x7fffffffu)throw Stop{Failure::FeatureCapacity};out.feature=0x80000000u|std::uint32_t(feature);return out;
|
|
122
|
+
}
|
|
123
|
+
Observation run(const Input& input){work();if(!(input.radius>=0.)||!std::isfinite(input.radius))throw Stop{Failure::InvalidInput};
|
|
124
|
+
std::array<Vec,2> segment{from(input.segment[0]),from(input.segment[1])};std::array<Vec,3> triangle{from(input.triangle[0]),from(input.triangle[1]),from(input.triangle[2])};
|
|
125
|
+
auto minima=distance(segment,triangle);const auto radius=value(input.radius),radiusSquared=mul(radius,radius);
|
|
126
|
+
if(compare(minima.values[0].squared,radiusSquared)>0){const auto& minimum=minima.values[0];Clear clear;clear.squaredDistance=capture(minimum.squared);clear.basis=input;
|
|
127
|
+
clear.authority.segment=capture(minimum.parameter);for(unsigned axis=0;axis<3;++axis)clear.authority.triangle[axis]=capture(minimum.bary[axis]);
|
|
128
|
+
clear.authority.triangleFeature=minimum.tf;clear.authority.segmentFeature=minimum.sf;clear.segmentCore=read(minimum.segment);clear.triangleCore=read(minimum.triangle);return clear;}
|
|
129
|
+
Point n{};double depth=0.;Vec escapeFoot{value(0.),value(0.),value(0.)};bool translated=false;
|
|
130
|
+
if(sign(minima.values[0].squared)>0){const auto difference=sub(minima.values[0].triangle,minima.values[0].segment);n=normal(difference,minima.values[0].squared);depth=input.radius-rootValue(squareRoot(minima.values[0].squared));if(depth<0.)throw Stop{Failure::ProjectionUnresolved};}
|
|
131
|
+
else{
|
|
132
|
+
const auto u=sub(triangle[1],triangle[0]),v=sub(triangle[2],triangle[0]),direction=sub(segment[1],segment[0]);auto spanNormal=cross(u,v);auto normalSquare=dot(spanNormal,spanNormal);
|
|
133
|
+
if(sign(dot(spanNormal,direction))){
|
|
134
|
+
std::array<Vec,6> core{sub(triangle[0],segment[0]),sub(triangle[1],segment[0]),sub(triangle[2],segment[0]),sub(triangle[0],segment[1]),sub(triangle[1],segment[1]),sub(triangle[2],segment[1])};
|
|
135
|
+
Vec selected;Rat selectedHeight,selectedSquare,selectedDistance;bool found=false;
|
|
136
|
+
auto facet=[&](Vec outward,const Vec& base){work();auto square=dot(outward,outward);if(!sign(square))return;auto height=dot(outward,base);int side=0;
|
|
137
|
+
for(const auto& vertex:core){const auto delta=sub(dot(outward,vertex),height);const auto current=sign(delta);if(current&&side&¤t!=side)return;if(current)side=current;}
|
|
138
|
+
if(!side)return;if(side>0){outward=mul(outward,value(-1.));height=negate(height);}if(sign(height)<0)throw Stop{Failure::ProjectionUnresolved};
|
|
139
|
+
auto ratio=div(mul(height,height),square);if(!found||compare(ratio,selectedDistance)<0){selected=std::move(outward);selectedHeight=std::move(height);selectedSquare=std::move(square);selectedDistance=std::move(ratio);found=true;}};
|
|
140
|
+
facet(clone(spanNormal),core[0]);facet(clone(spanNormal),core[3]);
|
|
141
|
+
for(unsigned edge=0;edge<3;++edge)facet(cross(sub(triangle[(edge+1)%3],triangle[edge]),direction),core[edge]);
|
|
142
|
+
if(!found)throw Stop{Failure::ProjectionUnresolved};const auto length=squareRoot(selectedSquare);auto outward=normal(selected,selectedSquare);
|
|
143
|
+
for(unsigned axis=0;axis<3;++axis)n[axis]=-outward[axis];const auto faceDistance=read(scalePower(selectedHeight,-length.power))/length.scaled;depth=input.radius+faceDistance;
|
|
144
|
+
escapeFoot=mul(selected,div(selectedHeight,selectedSquare));std::array<Vec,2> shifted{add(segment[0],escapeFoot),add(segment[1],escapeFoot)};
|
|
145
|
+
minima=distance(shifted,triangle);if(sign(minima.values[0].squared))throw Stop{Failure::ProjectionUnresolved};translated=true;
|
|
146
|
+
}else{
|
|
147
|
+
if(!sign(normalSquare)){spanNormal=cross(u,direction);normalSquare=dot(spanNormal,spanNormal);}
|
|
148
|
+
if(!sign(normalSquare)){spanNormal=cross(v,direction);normalSquare=dot(spanNormal,spanNormal);}
|
|
149
|
+
if(!sign(normalSquare)){
|
|
150
|
+
Vec basis=clone(u);if(!sign(dot(basis,basis)))basis=clone(v);if(!sign(dot(basis,basis)))basis=clone(direction);
|
|
151
|
+
if(!sign(dot(basis,basis))){spanNormal={value(1.),value(0.),value(0.)};normalSquare=value(1.);}
|
|
152
|
+
else{unsigned axis=0;auto smallest=mul(basis[0],basis[0]);for(unsigned candidateAxis=1;candidateAxis<3;++candidateAxis){auto component=mul(basis[candidateAxis],basis[candidateAxis]);if(compare(component,smallest)<0){axis=candidateAxis;smallest=std::move(component);}}
|
|
153
|
+
Vec tie{value(0.),value(0.),value(0.)};tie[axis]=value(1.);spanNormal=cross(basis,tie);normalSquare=dot(spanNormal,spanNormal);}
|
|
154
|
+
}
|
|
155
|
+
n=normal(spanNormal,normalSquare);for(const auto component:n)if(component!=0.){if(component<0.)for(auto& value:n)value=-value;break;}depth=input.radius;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
if(!std::isfinite(depth))throw Stop{Failure::UnrepresentableWitness};Hit result;result.count=minima.count;
|
|
159
|
+
const Vec radiusNormal=mul(from(n),radius);
|
|
160
|
+
for(unsigned index=0;index<result.count;++index){const auto& minimum=minima.values[index];auto original=translated?sub(minimum.segment,escapeFoot):clone(minimum.segment);
|
|
161
|
+
auto contact=emit(minimum,original,n,depth,input.immutableTraversalOrdinal,index);contact.basis=input;auto capsulePoint=add(original,radiusNormal);
|
|
162
|
+
contact.capsuleSurface=read(capsulePoint);contact.triangleSurface=read(minimum.triangle);contact.midpoint=read(mul(add(capsulePoint,minimum.triangle),value(.5)));result.contacts[index]=std::move(contact);}
|
|
163
|
+
return result;
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
Observation observe(const Input& input,ed::Operation& op,CallQuota& quota){try{return Kernel{op,quota}.run(input);}catch(const Stop& stop){return stop.failure;}catch(const std::bad_alloc&){return Failure::ArithmeticExhausted;}catch(const std::length_error&){return Failure::ArithmeticExhausted;}}
|
|
168
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
#include "exact/exact-operation.hpp"
|
|
3
|
+
#include <array>
|
|
4
|
+
#include <cstdint>
|
|
5
|
+
#include <variant>
|
|
6
|
+
|
|
7
|
+
namespace rundot::kinetix::physics::triangle_private {
|
|
8
|
+
using Point=std::array<double,3>;
|
|
9
|
+
enum class Failure:std::uint8_t{InvalidInput,ArithmeticExhausted,WorkExhausted,
|
|
10
|
+
FeatureCapacity,UnrepresentableWitness,ProjectionUnresolved,UnsupportedGeometry};
|
|
11
|
+
struct CallQuota{
|
|
12
|
+
std::uint64_t remainingWork;
|
|
13
|
+
bool charge(std::uint64_t amount)noexcept{if(amount>remainingWork)return false;remainingWork-=amount;return true;}
|
|
14
|
+
};
|
|
15
|
+
enum class ProjectionAssessment:std::uint8_t{DeclaredFiniteBasis,RoundedNativePose};
|
|
16
|
+
struct Input{
|
|
17
|
+
std::array<Point,2> segment;
|
|
18
|
+
std::array<Point,3> triangle;
|
|
19
|
+
double radius=0;
|
|
20
|
+
std::uint32_t immutableTraversalOrdinal=0;
|
|
21
|
+
ProjectionAssessment projection=ProjectionAssessment::DeclaredFiniteBasis;
|
|
22
|
+
};
|
|
23
|
+
struct ExactRatio{exact_dyadic::Encoding numerator,positiveDenominator;};
|
|
24
|
+
struct Parameters{
|
|
25
|
+
ExactRatio segment;
|
|
26
|
+
std::array<ExactRatio,3> triangle;
|
|
27
|
+
std::uint8_t triangleFeature=0,segmentFeature=0;
|
|
28
|
+
};
|
|
29
|
+
struct Contact{
|
|
30
|
+
Input basis;
|
|
31
|
+
Point segmentCore,triangleCore,capsuleSurface,triangleSurface,midpoint,normal;
|
|
32
|
+
double penetration=0;
|
|
33
|
+
std::uint32_t feature=0;
|
|
34
|
+
Parameters authority;
|
|
35
|
+
};
|
|
36
|
+
struct Hit{std::array<Contact,2> contacts;std::uint32_t count=0;};
|
|
37
|
+
struct Clear{ExactRatio squaredDistance;Input basis;Parameters authority;Point segmentCore,triangleCore;};
|
|
38
|
+
using Observation=std::variant<Clear,Hit,Failure>;
|
|
39
|
+
Observation observe(const Input&,exact_dyadic::Operation&,CallQuota&);
|
|
40
|
+
}
|