@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,1166 @@
|
|
|
1
|
+
#include "distance.hpp"
|
|
2
|
+
|
|
3
|
+
#include <algorithm>
|
|
4
|
+
#include <array>
|
|
5
|
+
#include <cmath>
|
|
6
|
+
#include <cstdint>
|
|
7
|
+
#include <limits>
|
|
8
|
+
#include <span>
|
|
9
|
+
|
|
10
|
+
namespace rundot::kinetix::physics {
|
|
11
|
+
|
|
12
|
+
namespace {
|
|
13
|
+
|
|
14
|
+
inline double solverSqrt(double value) {
|
|
15
|
+
if (value <= 0.0) return 0.0;
|
|
16
|
+
return std::sqrt(value);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
inline double cleanZero(double value) {
|
|
20
|
+
return std::abs(value) <= 1e-15 ? 0.0 : value;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
struct SimplexVertex {
|
|
24
|
+
DistanceVec3 v{}; // Minkowski difference: a - b
|
|
25
|
+
DistanceVec3 a{}; // Support point on shape A
|
|
26
|
+
DistanceVec3 b{}; // Support point on shape B
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
struct Simplex {
|
|
30
|
+
std::array<SimplexVertex, 4> vertices{};
|
|
31
|
+
std::uint32_t count = 0;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
struct SimplexClosestResult {
|
|
35
|
+
DistanceVec3 point{};
|
|
36
|
+
std::array<double, 4> lambda{};
|
|
37
|
+
std::uint32_t count = 0;
|
|
38
|
+
std::array<std::uint32_t, 4> keep{};
|
|
39
|
+
bool inside = false;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
// Voronoi simplex closest point to origin
|
|
43
|
+
SimplexClosestResult solveSimplex1(const Simplex& s) {
|
|
44
|
+
SimplexClosestResult res{};
|
|
45
|
+
res.point = s.vertices[0].v;
|
|
46
|
+
res.lambda[0] = 1.0;
|
|
47
|
+
res.count = 1;
|
|
48
|
+
res.keep[0] = 0;
|
|
49
|
+
res.inside = false;
|
|
50
|
+
return res;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
SimplexClosestResult solveSimplex2(const Simplex& s) {
|
|
54
|
+
SimplexClosestResult res{};
|
|
55
|
+
const DistanceVec3 a = s.vertices[0].v;
|
|
56
|
+
const DistanceVec3 b = s.vertices[1].v;
|
|
57
|
+
const DistanceVec3 ab = subVec3(b, a);
|
|
58
|
+
const DistanceVec3 ap = scaleVec3(a, -1.0);
|
|
59
|
+
const double d = dotVec3(ab, ab);
|
|
60
|
+
if (d <= 1e-24) {
|
|
61
|
+
res.point = a;
|
|
62
|
+
res.lambda[0] = 1.0;
|
|
63
|
+
res.count = 1;
|
|
64
|
+
res.keep[0] = 0;
|
|
65
|
+
return res;
|
|
66
|
+
}
|
|
67
|
+
const double t = dotVec3(ap, ab) / d;
|
|
68
|
+
if (t <= 0.0) {
|
|
69
|
+
res.point = a;
|
|
70
|
+
res.lambda[0] = 1.0;
|
|
71
|
+
res.count = 1;
|
|
72
|
+
res.keep[0] = 0;
|
|
73
|
+
} else if (t >= 1.0) {
|
|
74
|
+
res.point = b;
|
|
75
|
+
res.lambda[0] = 1.0;
|
|
76
|
+
res.count = 1;
|
|
77
|
+
res.keep[0] = 1;
|
|
78
|
+
} else {
|
|
79
|
+
res.point = addVec3(a, scaleVec3(ab, t));
|
|
80
|
+
res.lambda[0] = 1.0 - t;
|
|
81
|
+
res.lambda[1] = t;
|
|
82
|
+
res.count = 2;
|
|
83
|
+
res.keep[0] = 0;
|
|
84
|
+
res.keep[1] = 1;
|
|
85
|
+
}
|
|
86
|
+
return res;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
SimplexClosestResult solveSimplex3(const Simplex& s, std::uint32_t i0 = 0, std::uint32_t i1 = 1, std::uint32_t i2 = 2) {
|
|
90
|
+
SimplexClosestResult res{};
|
|
91
|
+
const DistanceVec3 a = s.vertices[i0].v;
|
|
92
|
+
const DistanceVec3 b = s.vertices[i1].v;
|
|
93
|
+
const DistanceVec3 c = s.vertices[i2].v;
|
|
94
|
+
|
|
95
|
+
const DistanceVec3 ab = subVec3(b, a);
|
|
96
|
+
const DistanceVec3 ac = subVec3(c, a);
|
|
97
|
+
const DistanceVec3 ap = scaleVec3(a, -1.0);
|
|
98
|
+
const double d1 = dotVec3(ab, ap);
|
|
99
|
+
const double d2 = dotVec3(ac, ap);
|
|
100
|
+
if (d1 <= 0.0 && d2 <= 0.0) {
|
|
101
|
+
res.point = a;
|
|
102
|
+
res.lambda[0] = 1.0;
|
|
103
|
+
res.count = 1;
|
|
104
|
+
res.keep[0] = i0;
|
|
105
|
+
return res;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const DistanceVec3 bp = scaleVec3(b, -1.0);
|
|
109
|
+
const double d3 = dotVec3(ab, bp);
|
|
110
|
+
const double d4 = dotVec3(ac, bp);
|
|
111
|
+
if (d3 >= 0.0 && d4 <= d3) {
|
|
112
|
+
res.point = b;
|
|
113
|
+
res.lambda[0] = 1.0;
|
|
114
|
+
res.count = 1;
|
|
115
|
+
res.keep[0] = i1;
|
|
116
|
+
return res;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const double vc = d1 * d4 - d3 * d2;
|
|
120
|
+
if (vc <= 0.0 && d1 >= 0.0 && d3 <= 0.0) {
|
|
121
|
+
const double v = (d1 - d3 <= 1e-24) ? 0.0 : d1 / (d1 - d3);
|
|
122
|
+
res.point = addVec3(a, scaleVec3(ab, v));
|
|
123
|
+
res.lambda[0] = 1.0 - v;
|
|
124
|
+
res.lambda[1] = v;
|
|
125
|
+
res.count = 2;
|
|
126
|
+
res.keep[0] = i0;
|
|
127
|
+
res.keep[1] = i1;
|
|
128
|
+
return res;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const DistanceVec3 cp = scaleVec3(c, -1.0);
|
|
132
|
+
const double d5 = dotVec3(ab, cp);
|
|
133
|
+
const double d6 = dotVec3(ac, cp);
|
|
134
|
+
if (d6 >= 0.0 && d5 <= d6) {
|
|
135
|
+
res.point = c;
|
|
136
|
+
res.lambda[0] = 1.0;
|
|
137
|
+
res.count = 1;
|
|
138
|
+
res.keep[0] = i2;
|
|
139
|
+
return res;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const double vb = d5 * d2 - d1 * d6;
|
|
143
|
+
if (vb <= 0.0 && d2 >= 0.0 && d6 <= 0.0) {
|
|
144
|
+
const double w = (d2 - d6 <= 1e-24) ? 0.0 : d2 / (d2 - d6);
|
|
145
|
+
res.point = addVec3(a, scaleVec3(ac, w));
|
|
146
|
+
res.lambda[0] = 1.0 - w;
|
|
147
|
+
res.lambda[1] = w;
|
|
148
|
+
res.count = 2;
|
|
149
|
+
res.keep[0] = i0;
|
|
150
|
+
res.keep[1] = i2;
|
|
151
|
+
return res;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const double va = d3 * d6 - d5 * d4;
|
|
155
|
+
if (va <= 0.0 && (d4 - d3) >= 0.0 && (d5 - d6) >= 0.0) {
|
|
156
|
+
const double denom = (d4 - d3) + (d5 - d6);
|
|
157
|
+
const double w = (denom <= 1e-24) ? 0.0 : (d4 - d3) / denom;
|
|
158
|
+
res.point = addVec3(b, scaleVec3(subVec3(c, b), w));
|
|
159
|
+
res.lambda[0] = 1.0 - w;
|
|
160
|
+
res.lambda[1] = w;
|
|
161
|
+
res.count = 2;
|
|
162
|
+
res.keep[0] = i1;
|
|
163
|
+
res.keep[1] = i2;
|
|
164
|
+
return res;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const double denom = 1.0 / (va + vb + vc);
|
|
168
|
+
const double v = vb * denom;
|
|
169
|
+
const double w = vc * denom;
|
|
170
|
+
const double u = 1.0 - v - w;
|
|
171
|
+
res.point = addVec3(a, addVec3(scaleVec3(ab, v), scaleVec3(ac, w)));
|
|
172
|
+
res.lambda[0] = u;
|
|
173
|
+
res.lambda[1] = v;
|
|
174
|
+
res.lambda[2] = w;
|
|
175
|
+
res.count = 3;
|
|
176
|
+
res.keep[0] = i0;
|
|
177
|
+
res.keep[1] = i1;
|
|
178
|
+
res.keep[2] = i2;
|
|
179
|
+
return res;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
bool outsideTetrahedronFace(
|
|
183
|
+
const DistanceVec3& point,
|
|
184
|
+
const DistanceVec3& v0,
|
|
185
|
+
const DistanceVec3& v1,
|
|
186
|
+
const DistanceVec3& v2,
|
|
187
|
+
const DistanceVec3& vOpposite) {
|
|
188
|
+
const DistanceVec3 normal = crossVec3(subVec3(v1, v0), subVec3(v2, v0));
|
|
189
|
+
return dotVec3(subVec3(point, v0), normal) * dotVec3(subVec3(vOpposite, v0), normal) < 0.0;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
SimplexClosestResult solveSimplex4(const Simplex& s) {
|
|
193
|
+
const DistanceVec3 origin{};
|
|
194
|
+
const std::uint32_t faces[4][4] = {
|
|
195
|
+
{0, 1, 2, 3},
|
|
196
|
+
{0, 2, 3, 1},
|
|
197
|
+
{0, 3, 1, 2},
|
|
198
|
+
{1, 3, 2, 0},
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
SimplexClosestResult best{};
|
|
202
|
+
double bestDistSq = 1e300;
|
|
203
|
+
bool outsideAny = false;
|
|
204
|
+
|
|
205
|
+
for (const auto& face : faces) {
|
|
206
|
+
if (!outsideTetrahedronFace(origin, s.vertices[face[0]].v, s.vertices[face[1]].v, s.vertices[face[2]].v, s.vertices[face[3]].v)) {
|
|
207
|
+
continue;
|
|
208
|
+
}
|
|
209
|
+
outsideAny = true;
|
|
210
|
+
const SimplexClosestResult candidate = solveSimplex3(s, face[0], face[1], face[2]);
|
|
211
|
+
const double dSq = lengthSquaredVec3(candidate.point);
|
|
212
|
+
if (dSq < bestDistSq) {
|
|
213
|
+
bestDistSq = dSq;
|
|
214
|
+
best = candidate;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
if (!outsideAny) {
|
|
219
|
+
best.inside = true;
|
|
220
|
+
best.point = origin;
|
|
221
|
+
}
|
|
222
|
+
return best;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
SimplexClosestResult solveSimplex(const Simplex& s) {
|
|
226
|
+
if (s.count == 1) return solveSimplex1(s);
|
|
227
|
+
if (s.count == 2) return solveSimplex2(s);
|
|
228
|
+
if (s.count == 3) return solveSimplex3(s);
|
|
229
|
+
return solveSimplex4(s);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
DistanceVec3 convexSupport(const DistanceShapeDef& shape, const DistanceVec3& worldDir) {
|
|
233
|
+
if (shape.shape == ShapeKind::Sphere) {
|
|
234
|
+
const double len = lengthVec3(worldDir);
|
|
235
|
+
if (len <= 1e-15) return addVec3(shape.center, {shape.dimensions.x, 0.0, 0.0});
|
|
236
|
+
return addVec3(shape.center, scaleVec3(worldDir, shape.dimensions.x / len));
|
|
237
|
+
}
|
|
238
|
+
if (shape.shape == ShapeKind::Box) {
|
|
239
|
+
const DistanceVec3 localDir = invRotateByQuat(shape.orientation, worldDir);
|
|
240
|
+
const DistanceVec3 localSupport{
|
|
241
|
+
localDir.x >= 0.0 ? shape.dimensions.x : -shape.dimensions.x,
|
|
242
|
+
localDir.y >= 0.0 ? shape.dimensions.y : -shape.dimensions.y,
|
|
243
|
+
localDir.z >= 0.0 ? shape.dimensions.z : -shape.dimensions.z,
|
|
244
|
+
};
|
|
245
|
+
return addVec3(shape.center, rotateByQuat(shape.orientation, localSupport));
|
|
246
|
+
}
|
|
247
|
+
if (shape.shape == ShapeKind::Capsule) {
|
|
248
|
+
const DistanceVec3 localDir = invRotateByQuat(shape.orientation, worldDir);
|
|
249
|
+
const double halfHeight = shape.dimensions.y;
|
|
250
|
+
const double radius = shape.dimensions.x;
|
|
251
|
+
DistanceVec3 localPoint{0.0, localDir.y >= 0.0 ? halfHeight : -halfHeight, 0.0};
|
|
252
|
+
const double len = lengthVec3(localDir);
|
|
253
|
+
if (len > 1e-15) {
|
|
254
|
+
localPoint = addVec3(localPoint, scaleVec3(localDir, radius / len));
|
|
255
|
+
} else {
|
|
256
|
+
localPoint.x += radius;
|
|
257
|
+
}
|
|
258
|
+
return addVec3(shape.center, rotateByQuat(shape.orientation, localPoint));
|
|
259
|
+
}
|
|
260
|
+
if (shape.shape == ShapeKind::Hull) {
|
|
261
|
+
if (shape.geometry.empty() || shape.geometryCount == 0) return shape.center;
|
|
262
|
+
const DistanceVec3 localDir = invRotateByQuat(shape.orientation, worldDir);
|
|
263
|
+
double bestDot = -1e300;
|
|
264
|
+
std::size_t bestIdx = 0;
|
|
265
|
+
for (std::uint32_t i = 0; i < shape.geometryCount; ++i) {
|
|
266
|
+
const std::size_t idx = (shape.geometryOffset + i) * 3;
|
|
267
|
+
if (idx + 2 >= shape.geometry.size()) break;
|
|
268
|
+
const double d = shape.geometry[idx] * localDir.x
|
|
269
|
+
+ shape.geometry[idx + 1] * localDir.y
|
|
270
|
+
+ shape.geometry[idx + 2] * localDir.z;
|
|
271
|
+
if (d > bestDot) {
|
|
272
|
+
bestDot = d;
|
|
273
|
+
bestIdx = idx;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
const DistanceVec3 localSupport{shape.geometry[bestIdx], shape.geometry[bestIdx + 1], shape.geometry[bestIdx + 2]};
|
|
277
|
+
return addVec3(shape.center, rotateByQuat(shape.orientation, localSupport));
|
|
278
|
+
}
|
|
279
|
+
return shape.center;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// EPA (Expanding Polytope Algorithm) for computing penetrating distance and witness points
|
|
283
|
+
struct EpaFace {
|
|
284
|
+
std::uint32_t a = 0;
|
|
285
|
+
std::uint32_t b = 0;
|
|
286
|
+
std::uint32_t c = 0;
|
|
287
|
+
DistanceVec3 normal{};
|
|
288
|
+
double distance = 0.0;
|
|
289
|
+
};
|
|
290
|
+
|
|
291
|
+
bool computeEpaFace(const SimplexVertex* vertices, std::uint32_t a, std::uint32_t b, std::uint32_t c, EpaFace& face) {
|
|
292
|
+
const DistanceVec3 ab = subVec3(vertices[b].v, vertices[a].v);
|
|
293
|
+
const DistanceVec3 ac = subVec3(vertices[c].v, vertices[a].v);
|
|
294
|
+
DistanceVec3 n = crossVec3(ab, ac);
|
|
295
|
+
const double len = lengthVec3(n);
|
|
296
|
+
if (len <= 1e-12) return false;
|
|
297
|
+
n = scaleVec3(n, 1.0 / len);
|
|
298
|
+
const double d = dotVec3(n, vertices[a].v);
|
|
299
|
+
if (d < 0.0) {
|
|
300
|
+
// Flip normal to face away from origin
|
|
301
|
+
face.a = b;
|
|
302
|
+
face.b = a;
|
|
303
|
+
face.c = c;
|
|
304
|
+
face.normal = scaleVec3(n, -1.0);
|
|
305
|
+
face.distance = -d;
|
|
306
|
+
} else {
|
|
307
|
+
face.a = a;
|
|
308
|
+
face.b = b;
|
|
309
|
+
face.c = c;
|
|
310
|
+
face.normal = n;
|
|
311
|
+
face.distance = d;
|
|
312
|
+
}
|
|
313
|
+
return true;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
DistanceResult runEpa(
|
|
317
|
+
const DistanceShapeDef& shapeA,
|
|
318
|
+
const DistanceShapeDef& shapeB,
|
|
319
|
+
const Simplex& initialSimplex,
|
|
320
|
+
InteriorPolicy policy) {
|
|
321
|
+
DistanceResult result{};
|
|
322
|
+
result.status = DistanceStatus::Penetrating;
|
|
323
|
+
result.isInside = true;
|
|
324
|
+
|
|
325
|
+
constexpr std::size_t kMaxEpaVertices = 64;
|
|
326
|
+
constexpr std::size_t kMaxEpaFaces = 128;
|
|
327
|
+
std::array<SimplexVertex, kMaxEpaVertices> vertices{};
|
|
328
|
+
std::array<EpaFace, kMaxEpaFaces> faces{};
|
|
329
|
+
std::size_t vertexCount = 4;
|
|
330
|
+
std::size_t faceCount = 0;
|
|
331
|
+
|
|
332
|
+
for (std::size_t i = 0; i < 4; ++i) {
|
|
333
|
+
vertices[i] = initialSimplex.vertices[i];
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
// Initial 4 faces of tetrahedron
|
|
337
|
+
const std::uint32_t baseIndices[4][3] = {
|
|
338
|
+
{0, 1, 2}, {0, 2, 3}, {0, 3, 1}, {1, 3, 2}
|
|
339
|
+
};
|
|
340
|
+
for (const auto& idx : baseIndices) {
|
|
341
|
+
EpaFace face;
|
|
342
|
+
if (computeEpaFace(vertices.data(), idx[0], idx[1], idx[2], face)) {
|
|
343
|
+
faces[faceCount++] = face;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
if (faceCount == 0) {
|
|
348
|
+
result.distance = (policy == InteriorPolicy::ZeroOnInterior) ? 0.0 : -1e-6;
|
|
349
|
+
result.witnessOnA = shapeA.center;
|
|
350
|
+
result.witnessOnB = shapeB.center;
|
|
351
|
+
result.normal = {0.0, 1.0, 0.0};
|
|
352
|
+
return result;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
EpaFace bestFace = faces[0];
|
|
356
|
+
for (std::size_t iter = 0; iter < 24; ++iter) {
|
|
357
|
+
// Find closest face to origin
|
|
358
|
+
std::size_t closestFaceIdx = 0;
|
|
359
|
+
double minFaceDist = faces[0].distance;
|
|
360
|
+
for (std::size_t f = 1; f < faceCount; ++f) {
|
|
361
|
+
if (faces[f].distance < minFaceDist) {
|
|
362
|
+
minFaceDist = faces[f].distance;
|
|
363
|
+
closestFaceIdx = f;
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
bestFace = faces[closestFaceIdx];
|
|
367
|
+
|
|
368
|
+
// Search support in face normal direction
|
|
369
|
+
const DistanceVec3 searchDir = bestFace.normal;
|
|
370
|
+
const DistanceVec3 suppA = convexSupport(shapeA, searchDir);
|
|
371
|
+
const DistanceVec3 suppB = convexSupport(shapeB, scaleVec3(searchDir, -1.0));
|
|
372
|
+
const DistanceVec3 suppV = subVec3(suppA, suppB);
|
|
373
|
+
|
|
374
|
+
const double supportDist = dotVec3(bestFace.normal, suppV);
|
|
375
|
+
if (supportDist - bestFace.distance <= 1e-6 || vertexCount >= kMaxEpaVertices) {
|
|
376
|
+
break;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
const std::uint32_t newVertexIdx = static_cast<std::uint32_t>(vertexCount++);
|
|
380
|
+
vertices[newVertexIdx] = {suppV, suppA, suppB};
|
|
381
|
+
|
|
382
|
+
// Find horizon edges: edges shared by exactly one visible face
|
|
383
|
+
struct Edge {
|
|
384
|
+
std::uint32_t u = 0;
|
|
385
|
+
std::uint32_t v = 0;
|
|
386
|
+
};
|
|
387
|
+
std::array<Edge, 64> horizonEdges{};
|
|
388
|
+
std::size_t horizonCount = 0;
|
|
389
|
+
|
|
390
|
+
auto addOrRemoveEdge = [&](std::uint32_t u, std::uint32_t v) {
|
|
391
|
+
for (std::size_t e = 0; e < horizonCount; ++e) {
|
|
392
|
+
if (horizonEdges[e].u == v && horizonEdges[e].v == u) {
|
|
393
|
+
// Shared edge: remove
|
|
394
|
+
horizonEdges[e] = horizonEdges[--horizonCount];
|
|
395
|
+
return;
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
if (horizonCount < horizonEdges.size()) {
|
|
399
|
+
horizonEdges[horizonCount++] = {u, v};
|
|
400
|
+
}
|
|
401
|
+
};
|
|
402
|
+
|
|
403
|
+
// Filter faces
|
|
404
|
+
std::size_t writeIdx = 0;
|
|
405
|
+
for (std::size_t f = 0; f < faceCount; ++f) {
|
|
406
|
+
if (dotVec3(faces[f].normal, subVec3(suppV, vertices[faces[f].a].v)) > 1e-8) {
|
|
407
|
+
// Face is visible to new point: add edges
|
|
408
|
+
addOrRemoveEdge(faces[f].a, faces[f].b);
|
|
409
|
+
addOrRemoveEdge(faces[f].b, faces[f].c);
|
|
410
|
+
addOrRemoveEdge(faces[f].c, faces[f].a);
|
|
411
|
+
} else {
|
|
412
|
+
faces[writeIdx++] = faces[f];
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
faceCount = writeIdx;
|
|
416
|
+
|
|
417
|
+
// Create new faces from horizon edges to new vertex
|
|
418
|
+
for (std::size_t e = 0; e < horizonCount; ++e) {
|
|
419
|
+
if (faceCount >= kMaxEpaFaces) break;
|
|
420
|
+
EpaFace newFace;
|
|
421
|
+
if (computeEpaFace(vertices.data(), horizonEdges[e].u, horizonEdges[e].v, newVertexIdx, newFace)) {
|
|
422
|
+
faces[faceCount++] = newFace;
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
// Compute witness points on closest face
|
|
428
|
+
const DistanceVec3 a = vertices[bestFace.a].v;
|
|
429
|
+
const DistanceVec3 b = vertices[bestFace.b].v;
|
|
430
|
+
const DistanceVec3 c = vertices[bestFace.c].v;
|
|
431
|
+
const DistanceVec3 ab = subVec3(b, a);
|
|
432
|
+
const DistanceVec3 ac = subVec3(c, a);
|
|
433
|
+
const DistanceVec3 ap = scaleVec3(a, -1.0);
|
|
434
|
+
const double d1 = dotVec3(ab, ap);
|
|
435
|
+
const double d2 = dotVec3(ac, ap);
|
|
436
|
+
const DistanceVec3 bp = scaleVec3(b, -1.0);
|
|
437
|
+
const double d3 = dotVec3(ab, bp);
|
|
438
|
+
const double d4 = dotVec3(ac, bp);
|
|
439
|
+
const double vc = d1 * d4 - d3 * d2;
|
|
440
|
+
const DistanceVec3 cp = scaleVec3(c, -1.0);
|
|
441
|
+
const double d5 = dotVec3(ab, cp);
|
|
442
|
+
const double d6 = dotVec3(ac, cp);
|
|
443
|
+
const double vb = d5 * d2 - d1 * d6;
|
|
444
|
+
const double va = d3 * d6 - d5 * d4;
|
|
445
|
+
const double denom = (va + vb + vc > 1e-24) ? 1.0 / (va + vb + vc) : 1.0 / 3.0;
|
|
446
|
+
const double v = std::max(0.0, vb * denom);
|
|
447
|
+
const double w = std::max(0.0, vc * denom);
|
|
448
|
+
const double u = std::max(0.0, 1.0 - v - w);
|
|
449
|
+
|
|
450
|
+
result.witnessOnA = addVec3(
|
|
451
|
+
scaleVec3(vertices[bestFace.a].a, u),
|
|
452
|
+
addVec3(scaleVec3(vertices[bestFace.b].a, v), scaleVec3(vertices[bestFace.c].a, w)));
|
|
453
|
+
result.witnessOnB = addVec3(
|
|
454
|
+
scaleVec3(vertices[bestFace.a].b, u),
|
|
455
|
+
addVec3(scaleVec3(vertices[bestFace.b].b, v), scaleVec3(vertices[bestFace.c].b, w)));
|
|
456
|
+
result.normal = bestFace.normal;
|
|
457
|
+
|
|
458
|
+
const double penDepth = bestFace.distance;
|
|
459
|
+
if (policy == InteriorPolicy::ZeroOnInterior) {
|
|
460
|
+
result.distance = 0.0;
|
|
461
|
+
} else {
|
|
462
|
+
result.distance = -cleanZero(penDepth);
|
|
463
|
+
}
|
|
464
|
+
return result;
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
DistanceResult gjkConvexDistance(
|
|
468
|
+
const DistanceShapeDef& shapeA,
|
|
469
|
+
const DistanceShapeDef& shapeB,
|
|
470
|
+
const DistanceOptions& options) {
|
|
471
|
+
DistanceResult result{};
|
|
472
|
+
|
|
473
|
+
DistanceVec3 dir = subVec3(shapeB.center, shapeA.center);
|
|
474
|
+
if (lengthSquaredVec3(dir) <= 1e-24) dir = {1.0, 0.0, 0.0};
|
|
475
|
+
|
|
476
|
+
Simplex simplex{};
|
|
477
|
+
const DistanceVec3 initA = convexSupport(shapeA, dir);
|
|
478
|
+
const DistanceVec3 initB = convexSupport(shapeB, scaleVec3(dir, -1.0));
|
|
479
|
+
simplex.vertices[0] = {subVec3(initA, initB), initA, initB};
|
|
480
|
+
simplex.count = 1;
|
|
481
|
+
|
|
482
|
+
DistanceVec3 closestPoint = simplex.vertices[0].v;
|
|
483
|
+
SimplexClosestResult closestResult = solveSimplex1(simplex);
|
|
484
|
+
|
|
485
|
+
for (std::uint32_t iter = 0; iter < 32; ++iter) {
|
|
486
|
+
closestResult = solveSimplex(simplex);
|
|
487
|
+
|
|
488
|
+
if (closestResult.inside) {
|
|
489
|
+
// Shapes overlap / penetrate
|
|
490
|
+
return runEpa(shapeA, shapeB, simplex, options.interiorPolicy);
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
closestPoint = closestResult.point;
|
|
494
|
+
const double distSq = lengthSquaredVec3(closestPoint);
|
|
495
|
+
if (distSq <= 1e-24) {
|
|
496
|
+
// Touching with exact zero distance
|
|
497
|
+
result.status = DistanceStatus::Touching;
|
|
498
|
+
result.distance = 0.0;
|
|
499
|
+
result.isInside = false;
|
|
500
|
+
DistanceVec3 wA{};
|
|
501
|
+
DistanceVec3 wB{};
|
|
502
|
+
for (std::uint32_t i = 0; i < closestResult.count; ++i) {
|
|
503
|
+
const std::uint32_t idx = closestResult.keep[i];
|
|
504
|
+
wA = addVec3(wA, scaleVec3(simplex.vertices[idx].a, closestResult.lambda[i]));
|
|
505
|
+
wB = addVec3(wB, scaleVec3(simplex.vertices[idx].b, closestResult.lambda[i]));
|
|
506
|
+
}
|
|
507
|
+
result.witnessOnA = wA;
|
|
508
|
+
result.witnessOnB = wB;
|
|
509
|
+
result.normal = {0.0, 1.0, 0.0};
|
|
510
|
+
return result;
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
// Reduce simplex to retained vertices
|
|
514
|
+
Simplex reduced{};
|
|
515
|
+
reduced.count = closestResult.count;
|
|
516
|
+
for (std::uint32_t i = 0; i < closestResult.count; ++i) {
|
|
517
|
+
reduced.vertices[i] = simplex.vertices[closestResult.keep[i]];
|
|
518
|
+
}
|
|
519
|
+
simplex = reduced;
|
|
520
|
+
|
|
521
|
+
// Search direction towards origin
|
|
522
|
+
const DistanceVec3 searchDir = scaleVec3(closestPoint, -1.0);
|
|
523
|
+
const DistanceVec3 newA = convexSupport(shapeA, searchDir);
|
|
524
|
+
const DistanceVec3 newB = convexSupport(shapeB, scaleVec3(searchDir, -1.0));
|
|
525
|
+
const DistanceVec3 candidateV = subVec3(newA, newB);
|
|
526
|
+
|
|
527
|
+
// Convergence test
|
|
528
|
+
const double projCandidate = dotVec3(candidateV, searchDir);
|
|
529
|
+
const double projCurrent = dotVec3(closestPoint, searchDir);
|
|
530
|
+
if (projCandidate - projCurrent < options.tolerance) {
|
|
531
|
+
break;
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
// Duplicate check
|
|
535
|
+
bool duplicate = false;
|
|
536
|
+
for (std::uint32_t i = 0; i < simplex.count; ++i) {
|
|
537
|
+
if (lengthSquaredVec3(subVec3(simplex.vertices[i].v, candidateV)) < 1e-20) {
|
|
538
|
+
duplicate = true;
|
|
539
|
+
break;
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
if (duplicate || simplex.count >= 4) {
|
|
543
|
+
break;
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
simplex.vertices[simplex.count++] = {candidateV, newA, newB};
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
// Calculate witness points from barycentric coordinates
|
|
550
|
+
DistanceVec3 wA{};
|
|
551
|
+
DistanceVec3 wB{};
|
|
552
|
+
for (std::uint32_t i = 0; i < closestResult.count; ++i) {
|
|
553
|
+
wA = addVec3(wA, scaleVec3(simplex.vertices[i].a, closestResult.lambda[i]));
|
|
554
|
+
wB = addVec3(wB, scaleVec3(simplex.vertices[i].b, closestResult.lambda[i]));
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
const DistanceVec3 diff = subVec3(wA, wB);
|
|
558
|
+
const double dist = lengthVec3(diff);
|
|
559
|
+
|
|
560
|
+
if (dist <= 1e-12) {
|
|
561
|
+
result.status = DistanceStatus::Touching;
|
|
562
|
+
result.distance = 0.0;
|
|
563
|
+
result.witnessOnA = wA;
|
|
564
|
+
result.witnessOnB = wB;
|
|
565
|
+
result.normal = {0.0, 1.0, 0.0};
|
|
566
|
+
result.isInside = false;
|
|
567
|
+
} else {
|
|
568
|
+
result.status = DistanceStatus::Disjoint;
|
|
569
|
+
result.distance = cleanZero(dist);
|
|
570
|
+
result.witnessOnA = wA;
|
|
571
|
+
result.witnessOnB = wB;
|
|
572
|
+
result.normal = scaleVec3(diff, 1.0 / dist);
|
|
573
|
+
result.isInside = false;
|
|
574
|
+
}
|
|
575
|
+
return result;
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
} // namespace
|
|
579
|
+
|
|
580
|
+
DistanceVec3 addVec3(const DistanceVec3& a, const DistanceVec3& b) {
|
|
581
|
+
return {a.x + b.x, a.y + b.y, a.z + b.z};
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
DistanceVec3 subVec3(const DistanceVec3& a, const DistanceVec3& b) {
|
|
585
|
+
return {a.x - b.x, a.y - b.y, a.z - b.z};
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
DistanceVec3 scaleVec3(const DistanceVec3& v, double s) {
|
|
589
|
+
return {v.x * s, v.y * s, v.z * s};
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
double dotVec3(const DistanceVec3& a, const DistanceVec3& b) {
|
|
593
|
+
return a.x * b.x + a.y * b.y + a.z * b.z;
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
DistanceVec3 crossVec3(const DistanceVec3& a, const DistanceVec3& b) {
|
|
597
|
+
return {
|
|
598
|
+
a.y * b.z - a.z * b.y,
|
|
599
|
+
a.z * b.x - a.x * b.z,
|
|
600
|
+
a.x * b.y - a.y * b.x,
|
|
601
|
+
};
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
double lengthSquaredVec3(const DistanceVec3& v) {
|
|
605
|
+
return dotVec3(v, v);
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
double lengthVec3(const DistanceVec3& v) {
|
|
609
|
+
return solverSqrt(lengthSquaredVec3(v));
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
DistanceVec3 normalizeVec3(const DistanceVec3& v) {
|
|
613
|
+
const double len = lengthVec3(v);
|
|
614
|
+
if (len <= 1e-15) return {0.0, 0.0, 0.0};
|
|
615
|
+
return scaleVec3(v, 1.0 / len);
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
DistanceVec3 rotateByQuat(const DistanceQuat& q, const DistanceVec3& v) {
|
|
619
|
+
const DistanceVec3 u{q.x, q.y, q.z};
|
|
620
|
+
const DistanceVec3 uv = crossVec3(u, v);
|
|
621
|
+
const DistanceVec3 uuv = crossVec3(u, uv);
|
|
622
|
+
return addVec3(v, addVec3(scaleVec3(uv, 2.0 * q.w), scaleVec3(uuv, 2.0)));
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
DistanceVec3 invRotateByQuat(const DistanceQuat& q, const DistanceVec3& v) {
|
|
626
|
+
const DistanceQuat invQ{-q.x, -q.y, -q.z, q.w};
|
|
627
|
+
return rotateByQuat(invQ, v);
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
DistanceResult pointToSphereDistance(
|
|
631
|
+
const DistanceVec3& point,
|
|
632
|
+
const DistanceVec3& sphereCenter,
|
|
633
|
+
double radius,
|
|
634
|
+
InteriorPolicy policy) {
|
|
635
|
+
DistanceResult result{};
|
|
636
|
+
const DistanceVec3 delta = subVec3(point, sphereCenter);
|
|
637
|
+
const double distFromCenter = lengthVec3(delta);
|
|
638
|
+
DistanceVec3 dir = {1.0, 0.0, 0.0};
|
|
639
|
+
if (distFromCenter > 1e-15) {
|
|
640
|
+
dir = scaleVec3(delta, 1.0 / distFromCenter);
|
|
641
|
+
}
|
|
642
|
+
const DistanceVec3 surfacePoint = addVec3(sphereCenter, scaleVec3(dir, radius));
|
|
643
|
+
const double signedDist = distFromCenter - radius;
|
|
644
|
+
result.witnessOnA = point;
|
|
645
|
+
result.witnessOnB = surfacePoint;
|
|
646
|
+
result.normal = dir;
|
|
647
|
+
|
|
648
|
+
if (signedDist > 1e-12) {
|
|
649
|
+
result.status = DistanceStatus::Disjoint;
|
|
650
|
+
result.distance = signedDist;
|
|
651
|
+
result.isInside = false;
|
|
652
|
+
} else if (signedDist >= -1e-12) {
|
|
653
|
+
result.status = DistanceStatus::Touching;
|
|
654
|
+
result.distance = 0.0;
|
|
655
|
+
result.witnessOnA = surfacePoint;
|
|
656
|
+
result.witnessOnB = surfacePoint;
|
|
657
|
+
result.isInside = false;
|
|
658
|
+
} else {
|
|
659
|
+
result.status = DistanceStatus::Penetrating;
|
|
660
|
+
result.isInside = true;
|
|
661
|
+
result.distance = (policy == InteriorPolicy::ZeroOnInterior) ? 0.0 : signedDist;
|
|
662
|
+
}
|
|
663
|
+
return result;
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
DistanceResult pointToBoxDistance(
|
|
667
|
+
const DistanceVec3& point,
|
|
668
|
+
const DistanceVec3& boxCenter,
|
|
669
|
+
const DistanceQuat& boxOrientation,
|
|
670
|
+
const DistanceVec3& halfExtents,
|
|
671
|
+
InteriorPolicy policy) {
|
|
672
|
+
DistanceResult result{};
|
|
673
|
+
const DistanceVec3 localP = invRotateByQuat(boxOrientation, subVec3(point, boxCenter));
|
|
674
|
+
|
|
675
|
+
const DistanceVec3 clamped{
|
|
676
|
+
std::max(-halfExtents.x, std::min(halfExtents.x, localP.x)),
|
|
677
|
+
std::max(-halfExtents.y, std::min(halfExtents.y, localP.y)),
|
|
678
|
+
std::max(-halfExtents.z, std::min(halfExtents.z, localP.z)),
|
|
679
|
+
};
|
|
680
|
+
|
|
681
|
+
const DistanceVec3 delta = subVec3(localP, clamped);
|
|
682
|
+
const double distOutsideSq = lengthSquaredVec3(delta);
|
|
683
|
+
|
|
684
|
+
if (distOutsideSq > 1e-24) {
|
|
685
|
+
const double dist = solverSqrt(distOutsideSq);
|
|
686
|
+
const DistanceVec3 worldWitnessB = addVec3(boxCenter, rotateByQuat(boxOrientation, clamped));
|
|
687
|
+
result.status = dist <= 1e-12 ? DistanceStatus::Touching : DistanceStatus::Disjoint;
|
|
688
|
+
result.distance = dist <= 1e-12 ? 0.0 : dist;
|
|
689
|
+
result.witnessOnA = point;
|
|
690
|
+
result.witnessOnB = worldWitnessB;
|
|
691
|
+
result.normal = rotateByQuat(boxOrientation, scaleVec3(delta, 1.0 / dist));
|
|
692
|
+
result.isInside = false;
|
|
693
|
+
} else {
|
|
694
|
+
const double dx = halfExtents.x - std::abs(localP.x);
|
|
695
|
+
const double dy = halfExtents.y - std::abs(localP.y);
|
|
696
|
+
const double dz = halfExtents.z - std::abs(localP.z);
|
|
697
|
+
|
|
698
|
+
double minPen = dx;
|
|
699
|
+
DistanceVec3 projLocal = localP;
|
|
700
|
+
DistanceVec3 localNorm = {localP.x >= 0.0 ? 1.0 : -1.0, 0.0, 0.0};
|
|
701
|
+
projLocal.x = localP.x >= 0.0 ? halfExtents.x : -halfExtents.x;
|
|
702
|
+
|
|
703
|
+
if (dy < minPen) {
|
|
704
|
+
minPen = dy;
|
|
705
|
+
projLocal = localP;
|
|
706
|
+
projLocal.y = localP.y >= 0.0 ? halfExtents.y : -halfExtents.y;
|
|
707
|
+
localNorm = {0.0, localP.y >= 0.0 ? 1.0 : -1.0, 0.0};
|
|
708
|
+
}
|
|
709
|
+
if (dz < minPen) {
|
|
710
|
+
minPen = dz;
|
|
711
|
+
projLocal = localP;
|
|
712
|
+
projLocal.z = localP.z >= 0.0 ? halfExtents.z : -halfExtents.z;
|
|
713
|
+
localNorm = {0.0, 0.0, localP.z >= 0.0 ? 1.0 : -1.0};
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
const DistanceVec3 worldWitnessB = addVec3(boxCenter, rotateByQuat(boxOrientation, projLocal));
|
|
717
|
+
const DistanceVec3 worldNorm = rotateByQuat(boxOrientation, localNorm);
|
|
718
|
+
|
|
719
|
+
if (minPen <= 1e-12) {
|
|
720
|
+
result.status = DistanceStatus::Touching;
|
|
721
|
+
result.distance = 0.0;
|
|
722
|
+
result.witnessOnA = point;
|
|
723
|
+
result.witnessOnB = worldWitnessB;
|
|
724
|
+
result.normal = worldNorm;
|
|
725
|
+
result.isInside = false;
|
|
726
|
+
} else {
|
|
727
|
+
result.status = DistanceStatus::Penetrating;
|
|
728
|
+
result.isInside = true;
|
|
729
|
+
result.distance = (policy == InteriorPolicy::ZeroOnInterior) ? 0.0 : -minPen;
|
|
730
|
+
result.witnessOnA = point;
|
|
731
|
+
result.witnessOnB = worldWitnessB;
|
|
732
|
+
result.normal = worldNorm;
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
return result;
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
DistanceResult pointToCapsuleDistance(
|
|
739
|
+
const DistanceVec3& point,
|
|
740
|
+
const DistanceVec3& capsuleCenter,
|
|
741
|
+
const DistanceQuat& capsuleOrientation,
|
|
742
|
+
double radius,
|
|
743
|
+
double halfHeight,
|
|
744
|
+
InteriorPolicy policy) {
|
|
745
|
+
DistanceResult result{};
|
|
746
|
+
const DistanceVec3 localP = invRotateByQuat(capsuleOrientation, subVec3(point, capsuleCenter));
|
|
747
|
+
const double clampedY = std::max(-halfHeight, std::min(halfHeight, localP.y));
|
|
748
|
+
const DistanceVec3 segPoint{0.0, clampedY, 0.0};
|
|
749
|
+
const DistanceVec3 delta = subVec3(localP, segPoint);
|
|
750
|
+
const double distToSeg = lengthVec3(delta);
|
|
751
|
+
|
|
752
|
+
DistanceVec3 localDir = {1.0, 0.0, 0.0};
|
|
753
|
+
if (distToSeg > 1e-15) {
|
|
754
|
+
localDir = scaleVec3(delta, 1.0 / distToSeg);
|
|
755
|
+
}
|
|
756
|
+
const DistanceVec3 localSurface = addVec3(segPoint, scaleVec3(localDir, radius));
|
|
757
|
+
const DistanceVec3 worldWitnessB = addVec3(capsuleCenter, rotateByQuat(capsuleOrientation, localSurface));
|
|
758
|
+
const DistanceVec3 worldNorm = rotateByQuat(capsuleOrientation, localDir);
|
|
759
|
+
const double signedDist = distToSeg - radius;
|
|
760
|
+
|
|
761
|
+
result.witnessOnA = point;
|
|
762
|
+
result.witnessOnB = worldWitnessB;
|
|
763
|
+
result.normal = worldNorm;
|
|
764
|
+
|
|
765
|
+
if (signedDist > 1e-12) {
|
|
766
|
+
result.status = DistanceStatus::Disjoint;
|
|
767
|
+
result.distance = signedDist;
|
|
768
|
+
result.isInside = false;
|
|
769
|
+
} else if (signedDist >= -1e-12) {
|
|
770
|
+
result.status = DistanceStatus::Touching;
|
|
771
|
+
result.distance = 0.0;
|
|
772
|
+
result.witnessOnA = worldWitnessB;
|
|
773
|
+
result.witnessOnB = worldWitnessB;
|
|
774
|
+
result.isInside = false;
|
|
775
|
+
} else {
|
|
776
|
+
result.status = DistanceStatus::Penetrating;
|
|
777
|
+
result.isInside = true;
|
|
778
|
+
result.distance = (policy == InteriorPolicy::ZeroOnInterior) ? 0.0 : signedDist;
|
|
779
|
+
}
|
|
780
|
+
return result;
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
DistanceResult pointToTriangleDistance(
|
|
784
|
+
const DistanceVec3& point,
|
|
785
|
+
const DistanceVec3& a,
|
|
786
|
+
const DistanceVec3& b,
|
|
787
|
+
const DistanceVec3& c,
|
|
788
|
+
InteriorPolicy /*policy*/) {
|
|
789
|
+
DistanceResult result{};
|
|
790
|
+
const DistanceVec3 ab = subVec3(b, a);
|
|
791
|
+
const DistanceVec3 ac = subVec3(c, a);
|
|
792
|
+
const DistanceVec3 ap = subVec3(point, a);
|
|
793
|
+
|
|
794
|
+
const double d1 = dotVec3(ab, ap);
|
|
795
|
+
const double d2 = dotVec3(ac, ap);
|
|
796
|
+
if (d1 <= 0.0 && d2 <= 0.0) {
|
|
797
|
+
const DistanceVec3 cp = a;
|
|
798
|
+
const DistanceVec3 diff = subVec3(point, cp);
|
|
799
|
+
const double dist = lengthVec3(diff);
|
|
800
|
+
result.witnessOnA = point;
|
|
801
|
+
result.witnessOnB = cp;
|
|
802
|
+
result.distance = dist <= 1e-12 ? 0.0 : dist;
|
|
803
|
+
result.status = dist <= 1e-12 ? DistanceStatus::Touching : DistanceStatus::Disjoint;
|
|
804
|
+
result.normal = dist > 1e-15 ? scaleVec3(diff, 1.0 / dist) : DistanceVec3{0.0, 1.0, 0.0};
|
|
805
|
+
result.isInside = false;
|
|
806
|
+
return result;
|
|
807
|
+
}
|
|
808
|
+
|
|
809
|
+
const DistanceVec3 bp = subVec3(point, b);
|
|
810
|
+
const double d3 = dotVec3(ab, bp);
|
|
811
|
+
const double d4 = dotVec3(ac, bp);
|
|
812
|
+
if (d3 >= 0.0 && d4 <= d3) {
|
|
813
|
+
const DistanceVec3 cp = b;
|
|
814
|
+
const DistanceVec3 diff = subVec3(point, cp);
|
|
815
|
+
const double dist = lengthVec3(diff);
|
|
816
|
+
result.witnessOnA = point;
|
|
817
|
+
result.witnessOnB = cp;
|
|
818
|
+
result.distance = dist <= 1e-12 ? 0.0 : dist;
|
|
819
|
+
result.status = dist <= 1e-12 ? DistanceStatus::Touching : DistanceStatus::Disjoint;
|
|
820
|
+
result.normal = dist > 1e-15 ? scaleVec3(diff, 1.0 / dist) : DistanceVec3{0.0, 1.0, 0.0};
|
|
821
|
+
result.isInside = false;
|
|
822
|
+
return result;
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
const double vc = d1 * d4 - d3 * d2;
|
|
826
|
+
if (vc <= 0.0 && d1 >= 0.0 && d3 <= 0.0) {
|
|
827
|
+
const double v = (d1 - d3 <= 1e-24) ? 0.0 : d1 / (d1 - d3);
|
|
828
|
+
const DistanceVec3 cp = addVec3(a, scaleVec3(ab, v));
|
|
829
|
+
const DistanceVec3 diff = subVec3(point, cp);
|
|
830
|
+
const double dist = lengthVec3(diff);
|
|
831
|
+
result.witnessOnA = point;
|
|
832
|
+
result.witnessOnB = cp;
|
|
833
|
+
result.distance = dist <= 1e-12 ? 0.0 : dist;
|
|
834
|
+
result.status = dist <= 1e-12 ? DistanceStatus::Touching : DistanceStatus::Disjoint;
|
|
835
|
+
result.normal = dist > 1e-15 ? scaleVec3(diff, 1.0 / dist) : DistanceVec3{0.0, 1.0, 0.0};
|
|
836
|
+
result.isInside = false;
|
|
837
|
+
return result;
|
|
838
|
+
}
|
|
839
|
+
|
|
840
|
+
const DistanceVec3 cp3 = subVec3(point, c);
|
|
841
|
+
const double d5 = dotVec3(ab, cp3);
|
|
842
|
+
const double d6 = dotVec3(ac, cp3);
|
|
843
|
+
if (d6 >= 0.0 && d5 <= d6) {
|
|
844
|
+
const DistanceVec3 closest = c;
|
|
845
|
+
const DistanceVec3 diff = subVec3(point, closest);
|
|
846
|
+
const double dist = lengthVec3(diff);
|
|
847
|
+
result.witnessOnA = point;
|
|
848
|
+
result.witnessOnB = closest;
|
|
849
|
+
result.distance = dist <= 1e-12 ? 0.0 : dist;
|
|
850
|
+
result.status = dist <= 1e-12 ? DistanceStatus::Touching : DistanceStatus::Disjoint;
|
|
851
|
+
result.normal = dist > 1e-15 ? scaleVec3(diff, 1.0 / dist) : DistanceVec3{0.0, 1.0, 0.0};
|
|
852
|
+
result.isInside = false;
|
|
853
|
+
return result;
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
const double vb = d5 * d2 - d1 * d6;
|
|
857
|
+
if (vb <= 0.0 && d2 >= 0.0 && d6 <= 0.0) {
|
|
858
|
+
const double w = (d2 - d6 <= 1e-24) ? 0.0 : d2 / (d2 - d6);
|
|
859
|
+
const DistanceVec3 closest = addVec3(a, scaleVec3(ac, w));
|
|
860
|
+
const DistanceVec3 diff = subVec3(point, closest);
|
|
861
|
+
const double dist = lengthVec3(diff);
|
|
862
|
+
result.witnessOnA = point;
|
|
863
|
+
result.witnessOnB = closest;
|
|
864
|
+
result.distance = dist <= 1e-12 ? 0.0 : dist;
|
|
865
|
+
result.status = dist <= 1e-12 ? DistanceStatus::Touching : DistanceStatus::Disjoint;
|
|
866
|
+
result.normal = dist > 1e-15 ? scaleVec3(diff, 1.0 / dist) : DistanceVec3{0.0, 1.0, 0.0};
|
|
867
|
+
result.isInside = false;
|
|
868
|
+
return result;
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
const double va = d3 * d6 - d5 * d4;
|
|
872
|
+
if (va <= 0.0 && (d4 - d3) >= 0.0 && (d5 - d6) >= 0.0) {
|
|
873
|
+
const double denom = (d4 - d3) + (d5 - d6);
|
|
874
|
+
const double w = (denom <= 1e-24) ? 0.0 : (d4 - d3) / denom;
|
|
875
|
+
const DistanceVec3 closest = addVec3(b, scaleVec3(subVec3(c, b), w));
|
|
876
|
+
const DistanceVec3 diff = subVec3(point, closest);
|
|
877
|
+
const double dist = lengthVec3(diff);
|
|
878
|
+
result.witnessOnA = point;
|
|
879
|
+
result.witnessOnB = closest;
|
|
880
|
+
result.distance = dist <= 1e-12 ? 0.0 : dist;
|
|
881
|
+
result.status = dist <= 1e-12 ? DistanceStatus::Touching : DistanceStatus::Disjoint;
|
|
882
|
+
result.normal = dist > 1e-15 ? scaleVec3(diff, 1.0 / dist) : DistanceVec3{0.0, 1.0, 0.0};
|
|
883
|
+
result.isInside = false;
|
|
884
|
+
return result;
|
|
885
|
+
}
|
|
886
|
+
|
|
887
|
+
const double denom = 1.0 / (va + vb + vc);
|
|
888
|
+
const double v = vb * denom;
|
|
889
|
+
const double w = vc * denom;
|
|
890
|
+
const DistanceVec3 closest = addVec3(a, addVec3(scaleVec3(ab, v), scaleVec3(ac, w)));
|
|
891
|
+
const DistanceVec3 diff = subVec3(point, closest);
|
|
892
|
+
const double dist = lengthVec3(diff);
|
|
893
|
+
|
|
894
|
+
result.witnessOnA = point;
|
|
895
|
+
result.witnessOnB = closest;
|
|
896
|
+
result.distance = dist <= 1e-12 ? 0.0 : dist;
|
|
897
|
+
result.status = dist <= 1e-12 ? DistanceStatus::Touching : DistanceStatus::Disjoint;
|
|
898
|
+
result.normal = dist > 1e-15 ? scaleVec3(diff, 1.0 / dist) : DistanceVec3{0.0, 1.0, 0.0};
|
|
899
|
+
result.isInside = false;
|
|
900
|
+
return result;
|
|
901
|
+
}
|
|
902
|
+
|
|
903
|
+
DistanceResult pointToMeshDistance(
|
|
904
|
+
const DistanceVec3& point,
|
|
905
|
+
const DistanceShapeDef& mesh,
|
|
906
|
+
InteriorPolicy policy) {
|
|
907
|
+
DistanceResult bestResult{};
|
|
908
|
+
bestResult.distance = 1e300;
|
|
909
|
+
bestResult.status = DistanceStatus::Disjoint;
|
|
910
|
+
|
|
911
|
+
if (mesh.geometry.empty() || mesh.geometryCount < 3) {
|
|
912
|
+
bestResult.status = DistanceStatus::InvalidInput;
|
|
913
|
+
return bestResult;
|
|
914
|
+
}
|
|
915
|
+
|
|
916
|
+
const std::uint32_t triangleCount = mesh.geometryCount / 3;
|
|
917
|
+
const std::uint32_t offset = mesh.geometryOffset;
|
|
918
|
+
|
|
919
|
+
for (std::uint32_t t = 0; t < triangleCount; ++t) {
|
|
920
|
+
const std::size_t base = (offset + t * 3) * 3;
|
|
921
|
+
if (base + 8 >= mesh.geometry.size()) break;
|
|
922
|
+
|
|
923
|
+
const DistanceVec3 aLocal{mesh.geometry[base + 0], mesh.geometry[base + 1], mesh.geometry[base + 2]};
|
|
924
|
+
const DistanceVec3 bLocal{mesh.geometry[base + 3], mesh.geometry[base + 4], mesh.geometry[base + 5]};
|
|
925
|
+
const DistanceVec3 cLocal{mesh.geometry[base + 6], mesh.geometry[base + 7], mesh.geometry[base + 8]};
|
|
926
|
+
|
|
927
|
+
const DistanceVec3 aWorld = addVec3(mesh.center, rotateByQuat(mesh.orientation, aLocal));
|
|
928
|
+
const DistanceVec3 bWorld = addVec3(mesh.center, rotateByQuat(mesh.orientation, bLocal));
|
|
929
|
+
const DistanceVec3 cWorld = addVec3(mesh.center, rotateByQuat(mesh.orientation, cLocal));
|
|
930
|
+
|
|
931
|
+
DistanceResult triResult = pointToTriangleDistance(point, aWorld, bWorld, cWorld, policy);
|
|
932
|
+
triResult.featureB = static_cast<std::int32_t>(t);
|
|
933
|
+
|
|
934
|
+
if (triResult.distance < bestResult.distance) {
|
|
935
|
+
bestResult = triResult;
|
|
936
|
+
}
|
|
937
|
+
}
|
|
938
|
+
return bestResult;
|
|
939
|
+
}
|
|
940
|
+
|
|
941
|
+
DistanceResult shapeToShapeDistance(
|
|
942
|
+
const DistanceShapeDef& shapeA,
|
|
943
|
+
const DistanceShapeDef& shapeB,
|
|
944
|
+
const DistanceOptions& options) {
|
|
945
|
+
// If target B is Mesh: iterate through all triangles of Mesh B
|
|
946
|
+
if (shapeB.shape == ShapeKind::Mesh) {
|
|
947
|
+
DistanceResult bestResult{};
|
|
948
|
+
bestResult.distance = 1e300;
|
|
949
|
+
bestResult.status = DistanceStatus::Disjoint;
|
|
950
|
+
|
|
951
|
+
if (shapeB.geometry.empty() || shapeB.geometryCount < 3) {
|
|
952
|
+
bestResult.status = DistanceStatus::InvalidInput;
|
|
953
|
+
return bestResult;
|
|
954
|
+
}
|
|
955
|
+
|
|
956
|
+
const std::uint32_t triangleCount = shapeB.geometryCount / 3;
|
|
957
|
+
const std::uint32_t offset = shapeB.geometryOffset;
|
|
958
|
+
|
|
959
|
+
for (std::uint32_t t = 0; t < triangleCount; ++t) {
|
|
960
|
+
const std::size_t base = (offset + t * 3) * 3;
|
|
961
|
+
if (base + 8 >= shapeB.geometry.size()) break;
|
|
962
|
+
|
|
963
|
+
const DistanceVec3 aLocal{shapeB.geometry[base + 0], shapeB.geometry[base + 1], shapeB.geometry[base + 2]};
|
|
964
|
+
const DistanceVec3 bLocal{shapeB.geometry[base + 3], shapeB.geometry[base + 4], shapeB.geometry[base + 5]};
|
|
965
|
+
const DistanceVec3 cLocal{shapeB.geometry[base + 6], shapeB.geometry[base + 7], shapeB.geometry[base + 8]};
|
|
966
|
+
|
|
967
|
+
// Each triangle is treated as a 3-vertex Hull
|
|
968
|
+
std::array<double, 9> triVerts{
|
|
969
|
+
aLocal.x, aLocal.y, aLocal.z,
|
|
970
|
+
bLocal.x, bLocal.y, bLocal.z,
|
|
971
|
+
cLocal.x, cLocal.y, cLocal.z,
|
|
972
|
+
};
|
|
973
|
+
|
|
974
|
+
DistanceShapeDef triDef{};
|
|
975
|
+
triDef.shape = ShapeKind::Hull;
|
|
976
|
+
triDef.center = shapeB.center;
|
|
977
|
+
triDef.orientation = shapeB.orientation;
|
|
978
|
+
triDef.geometry = triVerts;
|
|
979
|
+
triDef.geometryOffset = 0;
|
|
980
|
+
triDef.geometryCount = 3;
|
|
981
|
+
|
|
982
|
+
DistanceResult triResult = shapeToShapeDistance(shapeA, triDef, options);
|
|
983
|
+
triResult.featureB = static_cast<std::int32_t>(t);
|
|
984
|
+
|
|
985
|
+
if (triResult.distance < bestResult.distance) {
|
|
986
|
+
bestResult = triResult;
|
|
987
|
+
}
|
|
988
|
+
}
|
|
989
|
+
return bestResult;
|
|
990
|
+
}
|
|
991
|
+
|
|
992
|
+
// If shape A is Mesh and shape B is convex: flip
|
|
993
|
+
if (shapeA.shape == ShapeKind::Mesh && shapeB.shape != ShapeKind::Mesh) {
|
|
994
|
+
DistanceResult flipped = shapeToShapeDistance(shapeB, shapeA, options);
|
|
995
|
+
std::swap(flipped.witnessOnA, flipped.witnessOnB);
|
|
996
|
+
std::swap(flipped.featureA, flipped.featureB);
|
|
997
|
+
flipped.normal = scaleVec3(flipped.normal, -1.0);
|
|
998
|
+
return flipped;
|
|
999
|
+
}
|
|
1000
|
+
|
|
1001
|
+
// Special fast analytical cases
|
|
1002
|
+
if (shapeA.shape == ShapeKind::Sphere && shapeB.shape == ShapeKind::Sphere) {
|
|
1003
|
+
DistanceResult res{};
|
|
1004
|
+
const DistanceVec3 delta = subVec3(shapeB.center, shapeA.center);
|
|
1005
|
+
const double centerDist = lengthVec3(delta);
|
|
1006
|
+
DistanceVec3 dir = {1.0, 0.0, 0.0};
|
|
1007
|
+
if (centerDist > 1e-15) dir = scaleVec3(delta, 1.0 / centerDist);
|
|
1008
|
+
|
|
1009
|
+
const double rA = shapeA.dimensions.x;
|
|
1010
|
+
const double rB = shapeB.dimensions.x;
|
|
1011
|
+
const double signedDist = centerDist - (rA + rB);
|
|
1012
|
+
|
|
1013
|
+
res.witnessOnA = addVec3(shapeA.center, scaleVec3(dir, rA));
|
|
1014
|
+
res.witnessOnB = subVec3(shapeB.center, scaleVec3(dir, rB));
|
|
1015
|
+
res.normal = scaleVec3(dir, -1.0);
|
|
1016
|
+
|
|
1017
|
+
if (signedDist > 1e-12) {
|
|
1018
|
+
res.status = DistanceStatus::Disjoint;
|
|
1019
|
+
res.distance = signedDist;
|
|
1020
|
+
res.isInside = false;
|
|
1021
|
+
} else if (signedDist >= -1e-12) {
|
|
1022
|
+
res.status = DistanceStatus::Touching;
|
|
1023
|
+
res.distance = 0.0;
|
|
1024
|
+
res.isInside = false;
|
|
1025
|
+
} else {
|
|
1026
|
+
res.status = DistanceStatus::Penetrating;
|
|
1027
|
+
res.isInside = true;
|
|
1028
|
+
res.distance = (options.interiorPolicy == InteriorPolicy::ZeroOnInterior) ? 0.0 : signedDist;
|
|
1029
|
+
}
|
|
1030
|
+
return res;
|
|
1031
|
+
}
|
|
1032
|
+
|
|
1033
|
+
// General convex to convex via GJK
|
|
1034
|
+
return gjkConvexDistance(shapeA, shapeB, options);
|
|
1035
|
+
}
|
|
1036
|
+
|
|
1037
|
+
} // namespace rundot::kinetix::physics
|
|
1038
|
+
|
|
1039
|
+
// C-ABI functions
|
|
1040
|
+
extern "C" {
|
|
1041
|
+
|
|
1042
|
+
std::uint32_t rundot_kinetix_physics_distance_abi_version() {
|
|
1043
|
+
return rundot::kinetix::physics::kDistanceAbiVersion;
|
|
1044
|
+
}
|
|
1045
|
+
|
|
1046
|
+
std::uint32_t rundot_kinetix_physics_distance_input_stride() {
|
|
1047
|
+
return rundot::kinetix::physics::kDistanceInputStride;
|
|
1048
|
+
}
|
|
1049
|
+
|
|
1050
|
+
std::uint32_t rundot_kinetix_physics_distance_options_stride() {
|
|
1051
|
+
return rundot::kinetix::physics::kDistanceOptionsStride;
|
|
1052
|
+
}
|
|
1053
|
+
|
|
1054
|
+
std::uint32_t rundot_kinetix_physics_distance_output_stride() {
|
|
1055
|
+
return rundot::kinetix::physics::kDistanceOutputStride;
|
|
1056
|
+
}
|
|
1057
|
+
|
|
1058
|
+
static rundot::kinetix::physics::DistanceShapeDef parseShapeDef(
|
|
1059
|
+
const double* data,
|
|
1060
|
+
const double* geometryData,
|
|
1061
|
+
std::uint32_t geometryCount) {
|
|
1062
|
+
rundot::kinetix::physics::DistanceShapeDef s{};
|
|
1063
|
+
s.shape = static_cast<rundot::kinetix::physics::ShapeKind>(static_cast<std::uint32_t>(data[0]));
|
|
1064
|
+
s.center = {data[1], data[2], data[3]};
|
|
1065
|
+
s.orientation = {data[4], data[5], data[6], data[7]};
|
|
1066
|
+
s.dimensions = {data[8], data[9], data[10]};
|
|
1067
|
+
s.geometryOffset = static_cast<std::uint32_t>(data[11]);
|
|
1068
|
+
s.geometryCount = static_cast<std::uint32_t>(data[12]);
|
|
1069
|
+
s.geometryFormat = static_cast<std::uint32_t>(data[13]);
|
|
1070
|
+
if (geometryData != nullptr && geometryCount > 0) {
|
|
1071
|
+
s.geometry = std::span<const double>(geometryData, geometryCount);
|
|
1072
|
+
}
|
|
1073
|
+
return s;
|
|
1074
|
+
}
|
|
1075
|
+
|
|
1076
|
+
static rundot::kinetix::physics::DistanceOptions parseOptions(const double* data) {
|
|
1077
|
+
rundot::kinetix::physics::DistanceOptions opts{};
|
|
1078
|
+
if (data != nullptr) {
|
|
1079
|
+
opts.interiorPolicy = static_cast<rundot::kinetix::physics::InteriorPolicy>(
|
|
1080
|
+
static_cast<std::uint32_t>(data[0]));
|
|
1081
|
+
if (data[1] > 0.0) opts.maxDistance = data[1];
|
|
1082
|
+
if (data[2] > 0.0) opts.tolerance = data[2];
|
|
1083
|
+
}
|
|
1084
|
+
return opts;
|
|
1085
|
+
}
|
|
1086
|
+
|
|
1087
|
+
static void writeResult(
|
|
1088
|
+
const rundot::kinetix::physics::DistanceResult& res,
|
|
1089
|
+
double* outputData) {
|
|
1090
|
+
outputData[0] = static_cast<double>(res.status);
|
|
1091
|
+
outputData[1] = res.distance;
|
|
1092
|
+
outputData[2] = res.witnessOnA.x;
|
|
1093
|
+
outputData[3] = res.witnessOnA.y;
|
|
1094
|
+
outputData[4] = res.witnessOnA.z;
|
|
1095
|
+
outputData[5] = res.witnessOnB.x;
|
|
1096
|
+
outputData[6] = res.witnessOnB.y;
|
|
1097
|
+
outputData[7] = res.witnessOnB.z;
|
|
1098
|
+
outputData[8] = res.normal.x;
|
|
1099
|
+
outputData[9] = res.normal.y;
|
|
1100
|
+
outputData[10] = res.normal.z;
|
|
1101
|
+
outputData[11] = res.isInside ? 1.0 : 0.0;
|
|
1102
|
+
outputData[12] = static_cast<double>(res.featureA);
|
|
1103
|
+
outputData[13] = static_cast<double>(res.featureB);
|
|
1104
|
+
}
|
|
1105
|
+
|
|
1106
|
+
std::int32_t rundot_kinetix_physics_shape_distance(
|
|
1107
|
+
const double* shapeAData,
|
|
1108
|
+
const double* shapeBData,
|
|
1109
|
+
const double* geometryData,
|
|
1110
|
+
std::uint32_t geometryCount,
|
|
1111
|
+
const double* optionsData,
|
|
1112
|
+
double* outputData) {
|
|
1113
|
+
if (shapeAData == nullptr || shapeBData == nullptr || outputData == nullptr) {
|
|
1114
|
+
return -1;
|
|
1115
|
+
}
|
|
1116
|
+
const auto shapeA = parseShapeDef(shapeAData, geometryData, geometryCount);
|
|
1117
|
+
const auto shapeB = parseShapeDef(shapeBData, geometryData, geometryCount);
|
|
1118
|
+
const auto options = parseOptions(optionsData);
|
|
1119
|
+
|
|
1120
|
+
const auto result = rundot::kinetix::physics::shapeToShapeDistance(shapeA, shapeB, options);
|
|
1121
|
+
writeResult(result, outputData);
|
|
1122
|
+
return 0;
|
|
1123
|
+
}
|
|
1124
|
+
|
|
1125
|
+
std::int32_t rundot_kinetix_physics_point_distance(
|
|
1126
|
+
const double* pointData,
|
|
1127
|
+
const double* shapeData,
|
|
1128
|
+
const double* geometryData,
|
|
1129
|
+
std::uint32_t geometryCount,
|
|
1130
|
+
const double* optionsData,
|
|
1131
|
+
double* outputData) {
|
|
1132
|
+
if (pointData == nullptr || shapeData == nullptr || outputData == nullptr) {
|
|
1133
|
+
return -1;
|
|
1134
|
+
}
|
|
1135
|
+
const rundot::kinetix::physics::DistanceVec3 point{pointData[0], pointData[1], pointData[2]};
|
|
1136
|
+
const auto shape = parseShapeDef(shapeData, geometryData, geometryCount);
|
|
1137
|
+
const auto options = parseOptions(optionsData);
|
|
1138
|
+
|
|
1139
|
+
rundot::kinetix::physics::DistanceResult result;
|
|
1140
|
+
using rundot::kinetix::physics::ShapeKind;
|
|
1141
|
+
if (shape.shape == ShapeKind::Sphere) {
|
|
1142
|
+
result = rundot::kinetix::physics::pointToSphereDistance(
|
|
1143
|
+
point, shape.center, shape.dimensions.x, options.interiorPolicy);
|
|
1144
|
+
} else if (shape.shape == ShapeKind::Box) {
|
|
1145
|
+
result = rundot::kinetix::physics::pointToBoxDistance(
|
|
1146
|
+
point, shape.center, shape.orientation, shape.dimensions, options.interiorPolicy);
|
|
1147
|
+
} else if (shape.shape == ShapeKind::Capsule) {
|
|
1148
|
+
result = rundot::kinetix::physics::pointToCapsuleDistance(
|
|
1149
|
+
point, shape.center, shape.orientation, shape.dimensions.x, shape.dimensions.y, options.interiorPolicy);
|
|
1150
|
+
} else if (shape.shape == ShapeKind::Mesh) {
|
|
1151
|
+
result = rundot::kinetix::physics::pointToMeshDistance(
|
|
1152
|
+
point, shape, options.interiorPolicy);
|
|
1153
|
+
} else {
|
|
1154
|
+
// Treat as 1-point sphere vs shape
|
|
1155
|
+
rundot::kinetix::physics::DistanceShapeDef pointDef{};
|
|
1156
|
+
pointDef.shape = ShapeKind::Sphere;
|
|
1157
|
+
pointDef.center = point;
|
|
1158
|
+
pointDef.dimensions = {0.0, 0.0, 0.0};
|
|
1159
|
+
result = rundot::kinetix::physics::shapeToShapeDistance(pointDef, shape, options);
|
|
1160
|
+
}
|
|
1161
|
+
|
|
1162
|
+
writeResult(result, outputData);
|
|
1163
|
+
return 0;
|
|
1164
|
+
}
|
|
1165
|
+
|
|
1166
|
+
} // extern "C"
|