@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,1821 @@
|
|
|
1
|
+
#include "sdf_geometry.hpp"
|
|
2
|
+
|
|
3
|
+
#include <algorithm>
|
|
4
|
+
#include <cmath>
|
|
5
|
+
#include <cstdint>
|
|
6
|
+
#include <limits>
|
|
7
|
+
#include <span>
|
|
8
|
+
|
|
9
|
+
namespace rundot::kinetix::physics::sdf {
|
|
10
|
+
|
|
11
|
+
namespace {
|
|
12
|
+
|
|
13
|
+
constexpr double kZeroTolerance = 1e-15;
|
|
14
|
+
|
|
15
|
+
inline double clampDouble(double v, double minVal, double maxVal) {
|
|
16
|
+
if (v < minVal) return minVal;
|
|
17
|
+
if (v > maxVal) return maxVal;
|
|
18
|
+
return v;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
inline double max3(double a, double b, double c) {
|
|
22
|
+
return std::max(a, std::max(b, c));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
inline double min3(double a, double b, double c) {
|
|
26
|
+
return std::min(a, std::min(b, c));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Intersect ray with axis-aligned bounding box (slab method)
|
|
30
|
+
bool intersectRayAABB(
|
|
31
|
+
const SdfVec3& rayOrigin,
|
|
32
|
+
const SdfVec3& rayDir,
|
|
33
|
+
double maxDistance,
|
|
34
|
+
const SdfAABB& aabb,
|
|
35
|
+
double& outTNear,
|
|
36
|
+
double& outTFar) {
|
|
37
|
+
double tmin = 0.0;
|
|
38
|
+
double tmax = maxDistance;
|
|
39
|
+
|
|
40
|
+
// X slab
|
|
41
|
+
if (std::abs(rayDir.x) > 1e-12) {
|
|
42
|
+
const double invD = 1.0 / rayDir.x;
|
|
43
|
+
double t1 = (aabb.min.x - rayOrigin.x) * invD;
|
|
44
|
+
double t2 = (aabb.max.x - rayOrigin.x) * invD;
|
|
45
|
+
if (t1 > t2) std::swap(t1, t2);
|
|
46
|
+
tmin = std::max(tmin, t1);
|
|
47
|
+
tmax = std::min(tmax, t2);
|
|
48
|
+
if (tmin > tmax) return false;
|
|
49
|
+
} else if (rayOrigin.x < aabb.min.x || rayOrigin.x > aabb.max.x) {
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Y slab
|
|
54
|
+
if (std::abs(rayDir.y) > 1e-12) {
|
|
55
|
+
const double invD = 1.0 / rayDir.y;
|
|
56
|
+
double t1 = (aabb.min.y - rayOrigin.y) * invD;
|
|
57
|
+
double t2 = (aabb.max.y - rayOrigin.y) * invD;
|
|
58
|
+
if (t1 > t2) std::swap(t1, t2);
|
|
59
|
+
tmin = std::max(tmin, t1);
|
|
60
|
+
tmax = std::min(tmax, t2);
|
|
61
|
+
if (tmin > tmax) return false;
|
|
62
|
+
} else if (rayOrigin.y < aabb.min.y || rayOrigin.y > aabb.max.y) {
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Z slab
|
|
67
|
+
if (std::abs(rayDir.z) > 1e-12) {
|
|
68
|
+
const double invD = 1.0 / rayDir.z;
|
|
69
|
+
double t1 = (aabb.min.z - rayOrigin.z) * invD;
|
|
70
|
+
double t2 = (aabb.max.z - rayOrigin.z) * invD;
|
|
71
|
+
if (t1 > t2) std::swap(t1, t2);
|
|
72
|
+
tmin = std::max(tmin, t1);
|
|
73
|
+
tmax = std::min(tmax, t2);
|
|
74
|
+
if (tmin > tmax) return false;
|
|
75
|
+
} else if (rayOrigin.z < aabb.min.z || rayOrigin.z > aabb.max.z) {
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
outTNear = tmin;
|
|
80
|
+
outTFar = tmax;
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
} // namespace
|
|
85
|
+
|
|
86
|
+
// ============================================================================
|
|
87
|
+
// Math utilities
|
|
88
|
+
// ============================================================================
|
|
89
|
+
|
|
90
|
+
double solverSqrt(double value) {
|
|
91
|
+
if (value <= 0.0) return 0.0;
|
|
92
|
+
return std::sqrt(value);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
SdfVec3 addVec3(const SdfVec3& a, const SdfVec3& b) {
|
|
96
|
+
return {a.x + b.x, a.y + b.y, a.z + b.z};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
SdfVec3 subVec3(const SdfVec3& a, const SdfVec3& b) {
|
|
100
|
+
return {a.x - b.x, a.y - b.y, a.z - b.z};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
SdfVec3 scaleVec3(const SdfVec3& v, double s) {
|
|
104
|
+
return {v.x * s, v.y * s, v.z * s};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
double dotVec3(const SdfVec3& a, const SdfVec3& b) {
|
|
108
|
+
return a.x * b.x + a.y * b.y + a.z * b.z;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
SdfVec3 crossVec3(const SdfVec3& a, const SdfVec3& b) {
|
|
112
|
+
return {
|
|
113
|
+
a.y * b.z - a.z * b.y,
|
|
114
|
+
a.z * b.x - a.x * b.z,
|
|
115
|
+
a.x * b.y - a.y * b.x,
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
double lengthSquaredVec3(const SdfVec3& v) {
|
|
120
|
+
return dotVec3(v, v);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
double lengthVec3(const SdfVec3& v) {
|
|
124
|
+
return solverSqrt(lengthSquaredVec3(v));
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
SdfVec3 normalizeVec3(const SdfVec3& v) {
|
|
128
|
+
const double len = lengthVec3(v);
|
|
129
|
+
if (len <= kZeroTolerance) return {0.0, 1.0, 0.0};
|
|
130
|
+
return scaleVec3(v, 1.0 / len);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
SdfVec3 rotateByQuat(const SdfQuat& q, const SdfVec3& v) {
|
|
134
|
+
const SdfVec3 u{q.x, q.y, q.z};
|
|
135
|
+
const SdfVec3 uv = crossVec3(u, v);
|
|
136
|
+
const SdfVec3 uuv = crossVec3(u, uv);
|
|
137
|
+
return addVec3(v, addVec3(scaleVec3(uv, 2.0 * q.w), scaleVec3(uuv, 2.0)));
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
SdfVec3 invRotateByQuat(const SdfQuat& q, const SdfVec3& v) {
|
|
141
|
+
const SdfQuat conj{-q.x, -q.y, -q.z, q.w};
|
|
142
|
+
return rotateByQuat(conj, v);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
SdfQuat normalizeQuat(const SdfQuat& q) {
|
|
146
|
+
const double lenSq = q.x * q.x + q.y * q.y + q.z * q.z + q.w * q.w;
|
|
147
|
+
if (lenSq <= kZeroTolerance) return {0.0, 0.0, 0.0, 1.0};
|
|
148
|
+
const double invLen = 1.0 / solverSqrt(lenSq);
|
|
149
|
+
return {q.x * invLen, q.y * invLen, q.z * invLen, q.w * invLen};
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
SdfMat3 quatToMat3(const SdfQuat& q) {
|
|
153
|
+
const double xx = q.x * q.x;
|
|
154
|
+
const double yy = q.y * q.y;
|
|
155
|
+
const double zz = q.z * q.z;
|
|
156
|
+
const double xy = q.x * q.y;
|
|
157
|
+
const double xz = q.x * q.z;
|
|
158
|
+
const double yz = q.y * q.z;
|
|
159
|
+
const double wx = q.w * q.x;
|
|
160
|
+
const double wy = q.w * q.y;
|
|
161
|
+
const double wz = q.w * q.z;
|
|
162
|
+
|
|
163
|
+
SdfMat3 m;
|
|
164
|
+
m.xx = 1.0 - 2.0 * (yy + zz);
|
|
165
|
+
m.xy = 2.0 * (xy - wz);
|
|
166
|
+
m.xz = 2.0 * (xz + wy);
|
|
167
|
+
|
|
168
|
+
m.yx = 2.0 * (xy + wz);
|
|
169
|
+
m.yy = 1.0 - 2.0 * (xx + zz);
|
|
170
|
+
m.yz = 2.0 * (yz - wx);
|
|
171
|
+
|
|
172
|
+
m.zx = 2.0 * (xz - wy);
|
|
173
|
+
m.zy = 2.0 * (yz + wx);
|
|
174
|
+
m.zz = 1.0 - 2.0 * (xx + yy);
|
|
175
|
+
return m;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
SdfVec3 mulMat3Vec3(const SdfMat3& m, const SdfVec3& v) {
|
|
179
|
+
return {
|
|
180
|
+
m.xx * v.x + m.xy * v.y + m.xz * v.z,
|
|
181
|
+
m.yx * v.x + m.yy * v.y + m.yz * v.z,
|
|
182
|
+
m.zx * v.x + m.zy * v.y + m.zz * v.z,
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
SdfMat3 absMat3(const SdfMat3& m) {
|
|
187
|
+
SdfMat3 result;
|
|
188
|
+
result.xx = std::abs(m.xx);
|
|
189
|
+
result.xy = std::abs(m.xy);
|
|
190
|
+
result.xz = std::abs(m.xz);
|
|
191
|
+
result.yx = std::abs(m.yx);
|
|
192
|
+
result.yy = std::abs(m.yy);
|
|
193
|
+
result.yz = std::abs(m.yz);
|
|
194
|
+
result.zx = std::abs(m.zx);
|
|
195
|
+
result.zy = std::abs(m.zy);
|
|
196
|
+
result.zz = std::abs(m.zz);
|
|
197
|
+
return result;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// ============================================================================
|
|
201
|
+
// Analytical primitive SDF evaluators
|
|
202
|
+
// ============================================================================
|
|
203
|
+
|
|
204
|
+
double evaluatePrimitiveSdf(
|
|
205
|
+
SdfPrimitiveType type,
|
|
206
|
+
const double* params,
|
|
207
|
+
const SdfVec3& localPoint) {
|
|
208
|
+
if (params == nullptr) return 1e30;
|
|
209
|
+
|
|
210
|
+
switch (type) {
|
|
211
|
+
case SdfPrimitiveType::Sphere: {
|
|
212
|
+
const double radius = params[0] > 0.0 ? params[0] : 0.5;
|
|
213
|
+
return lengthVec3(localPoint) - radius;
|
|
214
|
+
}
|
|
215
|
+
case SdfPrimitiveType::Box: {
|
|
216
|
+
const double hx = params[0] > 0.0 ? params[0] : 0.5;
|
|
217
|
+
const double hy = params[1] > 0.0 ? params[1] : 0.5;
|
|
218
|
+
const double hz = params[2] > 0.0 ? params[2] : 0.5;
|
|
219
|
+
const double qx = std::abs(localPoint.x) - hx;
|
|
220
|
+
const double qy = std::abs(localPoint.y) - hy;
|
|
221
|
+
const double qz = std::abs(localPoint.z) - hz;
|
|
222
|
+
const double dx = std::max(qx, 0.0);
|
|
223
|
+
const double dy = std::max(qy, 0.0);
|
|
224
|
+
const double dz = std::max(qz, 0.0);
|
|
225
|
+
const double outsideDist = solverSqrt(dx * dx + dy * dy + dz * dz);
|
|
226
|
+
const double insideDist = std::min(max3(qx, qy, qz), 0.0);
|
|
227
|
+
return outsideDist + insideDist;
|
|
228
|
+
}
|
|
229
|
+
case SdfPrimitiveType::Capsule: {
|
|
230
|
+
const double radius = params[0] > 0.0 ? params[0] : 0.5;
|
|
231
|
+
const double halfHeight = params[1] > 0.0 ? params[1] : 0.5;
|
|
232
|
+
const double clampedY = clampDouble(localPoint.y, -halfHeight, halfHeight);
|
|
233
|
+
const double dy = localPoint.y - clampedY;
|
|
234
|
+
return solverSqrt(localPoint.x * localPoint.x + dy * dy + localPoint.z * localPoint.z) - radius;
|
|
235
|
+
}
|
|
236
|
+
case SdfPrimitiveType::Cylinder: {
|
|
237
|
+
const double radius = params[0] > 0.0 ? params[0] : 0.5;
|
|
238
|
+
const double halfHeight = params[1] > 0.0 ? params[1] : 0.5;
|
|
239
|
+
const double dxz = solverSqrt(localPoint.x * localPoint.x + localPoint.z * localPoint.z) - radius;
|
|
240
|
+
const double dy = std::abs(localPoint.y) - halfHeight;
|
|
241
|
+
const double outsideX = std::max(dxz, 0.0);
|
|
242
|
+
const double outsideY = std::max(dy, 0.0);
|
|
243
|
+
const double outsideDist = solverSqrt(outsideX * outsideX + outsideY * outsideY);
|
|
244
|
+
const double insideDist = std::min(std::max(dxz, dy), 0.0);
|
|
245
|
+
return outsideDist + insideDist;
|
|
246
|
+
}
|
|
247
|
+
case SdfPrimitiveType::Plane: {
|
|
248
|
+
const SdfVec3 n = normalizeVec3({params[0], params[1], params[2]});
|
|
249
|
+
const double offset = params[3];
|
|
250
|
+
return dotVec3(localPoint, n) - offset;
|
|
251
|
+
}
|
|
252
|
+
case SdfPrimitiveType::Torus: {
|
|
253
|
+
const double majorR = params[0] > 0.0 ? params[0] : 1.0;
|
|
254
|
+
const double minorR = params[1] > 0.0 ? params[1] : 0.25;
|
|
255
|
+
const double qxz = solverSqrt(localPoint.x * localPoint.x + localPoint.z * localPoint.z) - majorR;
|
|
256
|
+
return solverSqrt(qxz * qxz + localPoint.y * localPoint.y) - minorR;
|
|
257
|
+
}
|
|
258
|
+
case SdfPrimitiveType::RoundedBox: {
|
|
259
|
+
const double hx = params[0] > 0.0 ? params[0] : 0.5;
|
|
260
|
+
const double hy = params[1] > 0.0 ? params[1] : 0.5;
|
|
261
|
+
const double hz = params[2] > 0.0 ? params[2] : 0.5;
|
|
262
|
+
const double roundR = params[3] > 0.0 ? params[3] : 0.1;
|
|
263
|
+
const double qx = std::abs(localPoint.x) - (hx - roundR);
|
|
264
|
+
const double qy = std::abs(localPoint.y) - (hy - roundR);
|
|
265
|
+
const double qz = std::abs(localPoint.z) - (hz - roundR);
|
|
266
|
+
const double dx = std::max(qx, 0.0);
|
|
267
|
+
const double dy = std::max(qy, 0.0);
|
|
268
|
+
const double dz = std::max(qz, 0.0);
|
|
269
|
+
const double outsideDist = solverSqrt(dx * dx + dy * dy + dz * dz);
|
|
270
|
+
const double insideDist = std::min(max3(qx, qy, qz), 0.0);
|
|
271
|
+
return outsideDist + insideDist - roundR;
|
|
272
|
+
}
|
|
273
|
+
case SdfPrimitiveType::Cone: {
|
|
274
|
+
const double radius = params[0] > 0.0 ? params[0] : 0.5;
|
|
275
|
+
const double height = params[1] > 0.0 ? params[1] : 1.0;
|
|
276
|
+
const double q = solverSqrt(localPoint.x * localPoint.x + localPoint.z * localPoint.z);
|
|
277
|
+
// Apex at +height/2, base at -height/2
|
|
278
|
+
const double y = localPoint.y + height * 0.5;
|
|
279
|
+
const double k = radius / height;
|
|
280
|
+
const double w = q - k * (height - y);
|
|
281
|
+
const double outsideX = std::max(w, 0.0);
|
|
282
|
+
const double outsideY = std::max(y - height, 0.0);
|
|
283
|
+
const double outsideZ = std::max(-y, 0.0);
|
|
284
|
+
if (outsideX > 0.0 || outsideY > 0.0 || outsideZ > 0.0) {
|
|
285
|
+
return solverSqrt(outsideX * outsideX + outsideY * outsideY + outsideZ * outsideZ);
|
|
286
|
+
}
|
|
287
|
+
return std::max(w, std::max(y - height, -y));
|
|
288
|
+
}
|
|
289
|
+
case SdfPrimitiveType::Ellipsoid: {
|
|
290
|
+
const double rx = params[0] > 0.0 ? params[0] : 1.0;
|
|
291
|
+
const double ry = params[1] > 0.0 ? params[1] : 0.5;
|
|
292
|
+
const double rz = params[2] > 0.0 ? params[2] : 0.5;
|
|
293
|
+
const double px = localPoint.x / rx;
|
|
294
|
+
const double py = localPoint.y / ry;
|
|
295
|
+
const double pz = localPoint.z / rz;
|
|
296
|
+
const double k0 = solverSqrt(px * px + py * py + pz * pz);
|
|
297
|
+
const double qx = px / rx;
|
|
298
|
+
const double qy = py / ry;
|
|
299
|
+
const double qz = pz / rz;
|
|
300
|
+
const double k1 = solverSqrt(qx * qx + qy * qy + qz * qz);
|
|
301
|
+
if (k1 <= kZeroTolerance) return lengthVec3(localPoint) - std::min(rx, std::min(ry, rz));
|
|
302
|
+
return k0 * (k0 - 1.0) / k1;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
return 1e30;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
// ============================================================================
|
|
309
|
+
// Deterministic grid cooking
|
|
310
|
+
// ============================================================================
|
|
311
|
+
|
|
312
|
+
SdfStatus cookPrimitiveGrid(
|
|
313
|
+
const SdfGridDesc& desc,
|
|
314
|
+
SdfPrimitiveType primitiveType,
|
|
315
|
+
const double* primitiveParams,
|
|
316
|
+
const SdfTransform& primitivePose,
|
|
317
|
+
std::uint32_t materialId,
|
|
318
|
+
std::span<double> outDistances,
|
|
319
|
+
std::span<std::uint32_t> outMaterialIds) {
|
|
320
|
+
if (desc.resX < 2 || desc.resY < 2 || desc.resZ < 2) {
|
|
321
|
+
return SdfStatus::InvalidInput;
|
|
322
|
+
}
|
|
323
|
+
if (desc.bounds.max.x <= desc.bounds.min.x ||
|
|
324
|
+
desc.bounds.max.y <= desc.bounds.min.y ||
|
|
325
|
+
desc.bounds.max.z <= desc.bounds.min.z) {
|
|
326
|
+
return SdfStatus::InvalidBounds;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
const std::uint32_t total = desc.totalSamples();
|
|
330
|
+
if (outDistances.size() < total || outMaterialIds.size() < total) {
|
|
331
|
+
return SdfStatus::BufferOverflow;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
const SdfVec3 cSize = desc.cellSize();
|
|
335
|
+
const SdfQuat normOrientation = normalizeQuat(primitivePose.orientation);
|
|
336
|
+
|
|
337
|
+
std::uint32_t index = 0;
|
|
338
|
+
for (std::uint32_t iz = 0; iz < desc.resZ; ++iz) {
|
|
339
|
+
const double pz = desc.bounds.min.z + iz * cSize.z;
|
|
340
|
+
for (std::uint32_t iy = 0; iy < desc.resY; ++iy) {
|
|
341
|
+
const double py = desc.bounds.min.y + iy * cSize.y;
|
|
342
|
+
for (std::uint32_t ix = 0; ix < desc.resX; ++ix) {
|
|
343
|
+
const double px = desc.bounds.min.x + ix * cSize.x;
|
|
344
|
+
const SdfVec3 worldPt{px, py, pz};
|
|
345
|
+
const SdfVec3 localPt = invRotateByQuat(normOrientation, subVec3(worldPt, primitivePose.position));
|
|
346
|
+
double dist = evaluatePrimitiveSdf(primitiveType, primitiveParams, localPt);
|
|
347
|
+
if (dist > desc.maxTruncationDistance) {
|
|
348
|
+
dist = desc.maxTruncationDistance;
|
|
349
|
+
} else if (dist < -desc.maxTruncationDistance) {
|
|
350
|
+
dist = -desc.maxTruncationDistance;
|
|
351
|
+
}
|
|
352
|
+
outDistances[index] = dist;
|
|
353
|
+
outMaterialIds[index] = materialId;
|
|
354
|
+
++index;
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
return SdfStatus::Success;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
SdfStatus cookCsgGrid(
|
|
363
|
+
const SdfGridDesc& desc,
|
|
364
|
+
std::span<double> inOutDistancesA,
|
|
365
|
+
std::span<std::uint32_t> inOutMaterialIdsA,
|
|
366
|
+
std::span<const double> inDistancesB,
|
|
367
|
+
std::span<const std::uint32_t> inMaterialIdsB,
|
|
368
|
+
SdfCsgOp op,
|
|
369
|
+
double blendRadius) {
|
|
370
|
+
const std::uint32_t total = desc.totalSamples();
|
|
371
|
+
if (inOutDistancesA.size() < total || inOutMaterialIdsA.size() < total ||
|
|
372
|
+
inDistancesB.size() < total || inMaterialIdsB.size() < total) {
|
|
373
|
+
return SdfStatus::BufferOverflow;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
const double k = blendRadius > 1e-12 ? blendRadius : 1e-12;
|
|
377
|
+
|
|
378
|
+
for (std::uint32_t i = 0; i < total; ++i) {
|
|
379
|
+
const double da = inOutDistancesA[i];
|
|
380
|
+
const double db = inDistancesB[i];
|
|
381
|
+
const std::uint32_t mata = inOutMaterialIdsA[i];
|
|
382
|
+
const std::uint32_t matb = inMaterialIdsB[i];
|
|
383
|
+
|
|
384
|
+
double dout = da;
|
|
385
|
+
std::uint32_t matout = mata;
|
|
386
|
+
|
|
387
|
+
switch (op) {
|
|
388
|
+
case SdfCsgOp::Union:
|
|
389
|
+
if (db < da) {
|
|
390
|
+
dout = db;
|
|
391
|
+
matout = matb;
|
|
392
|
+
} else {
|
|
393
|
+
dout = da;
|
|
394
|
+
matout = mata;
|
|
395
|
+
}
|
|
396
|
+
break;
|
|
397
|
+
|
|
398
|
+
case SdfCsgOp::Intersection:
|
|
399
|
+
if (db > da) {
|
|
400
|
+
dout = db;
|
|
401
|
+
matout = matb;
|
|
402
|
+
} else {
|
|
403
|
+
dout = da;
|
|
404
|
+
matout = mata;
|
|
405
|
+
}
|
|
406
|
+
break;
|
|
407
|
+
|
|
408
|
+
case SdfCsgOp::Difference: {
|
|
409
|
+
const double negDb = -db;
|
|
410
|
+
if (negDb > da) {
|
|
411
|
+
dout = negDb;
|
|
412
|
+
matout = matb;
|
|
413
|
+
} else {
|
|
414
|
+
dout = da;
|
|
415
|
+
matout = mata;
|
|
416
|
+
}
|
|
417
|
+
break;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
case SdfCsgOp::SmoothUnion: {
|
|
421
|
+
const double h = clampDouble(0.5 + 0.5 * (db - da) / k, 0.0, 1.0);
|
|
422
|
+
dout = (1.0 - h) * db + h * da - k * h * (1.0 - h);
|
|
423
|
+
matout = (h < 0.5) ? matb : mata;
|
|
424
|
+
break;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
case SdfCsgOp::SmoothIntersection: {
|
|
428
|
+
const double h = clampDouble(0.5 - 0.5 * (db - da) / k, 0.0, 1.0);
|
|
429
|
+
dout = (1.0 - h) * db + h * da + k * h * (1.0 - h);
|
|
430
|
+
matout = (h < 0.5) ? matb : mata;
|
|
431
|
+
break;
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
case SdfCsgOp::SmoothDifference: {
|
|
435
|
+
const double negDb = -db;
|
|
436
|
+
const double h = clampDouble(0.5 - 0.5 * (negDb - da) / k, 0.0, 1.0);
|
|
437
|
+
dout = (1.0 - h) * negDb + h * da + k * h * (1.0 - h);
|
|
438
|
+
matout = mata;
|
|
439
|
+
break;
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
inOutDistancesA[i] = dout;
|
|
444
|
+
inOutMaterialIdsA[i] = matout;
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
return SdfStatus::Success;
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
// ============================================================================
|
|
451
|
+
// Continuous evaluation and sampling
|
|
452
|
+
// ============================================================================
|
|
453
|
+
|
|
454
|
+
SdfStatus sampleSdfGrid(
|
|
455
|
+
const SdfGridDesc& desc,
|
|
456
|
+
std::span<const double> distances,
|
|
457
|
+
std::span<const std::uint32_t> materialIds,
|
|
458
|
+
const SdfTransform& gridPose,
|
|
459
|
+
const SdfVec3& queryPointWorld,
|
|
460
|
+
SdfSampleResult& outResult) {
|
|
461
|
+
const std::uint32_t total = desc.totalSamples();
|
|
462
|
+
if (distances.size() < total) {
|
|
463
|
+
return SdfStatus::BufferOverflow;
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
const SdfQuat normOrientation = normalizeQuat(gridPose.orientation);
|
|
467
|
+
const SdfVec3 localPt = invRotateByQuat(normOrientation, subVec3(queryPointWorld, gridPose.position));
|
|
468
|
+
|
|
469
|
+
const SdfVec3 cSize = desc.cellSize();
|
|
470
|
+
if (cSize.x <= 0.0 || cSize.y <= 0.0 || cSize.z <= 0.0) {
|
|
471
|
+
return SdfStatus::InvalidBounds;
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
// Check bounds and compute distance to AABB if outside
|
|
475
|
+
const double cx = clampDouble(localPt.x, desc.bounds.min.x, desc.bounds.max.x);
|
|
476
|
+
const double cy = clampDouble(localPt.y, desc.bounds.min.y, desc.bounds.max.y);
|
|
477
|
+
const double cz = clampDouble(localPt.z, desc.bounds.min.z, desc.bounds.max.z);
|
|
478
|
+
|
|
479
|
+
const double dxOut = localPt.x - cx;
|
|
480
|
+
const double dyOut = localPt.y - cy;
|
|
481
|
+
const double dzOut = localPt.z - cz;
|
|
482
|
+
const double distToAabbSq = dxOut * dxOut + dyOut * dyOut + dzOut * dzOut;
|
|
483
|
+
const bool isOutsideAabb = distToAabbSq > 1e-14;
|
|
484
|
+
|
|
485
|
+
const auto sampleIndex = [&](std::uint32_t ix, std::uint32_t iy, std::uint32_t iz) -> std::uint32_t {
|
|
486
|
+
return iz * (desc.resX * desc.resY) + iy * desc.resX + ix;
|
|
487
|
+
};
|
|
488
|
+
|
|
489
|
+
const auto interpolateTrilinear = [&](double px, double py, double pz) -> double {
|
|
490
|
+
const double u = (px - desc.bounds.min.x) / cSize.x;
|
|
491
|
+
const double v = (py - desc.bounds.min.y) / cSize.y;
|
|
492
|
+
const double w = (pz - desc.bounds.min.z) / cSize.z;
|
|
493
|
+
|
|
494
|
+
const std::uint32_t i0 = static_cast<std::uint32_t>(clampDouble(std::floor(u), 0.0, static_cast<double>(desc.resX - 2)));
|
|
495
|
+
const std::uint32_t j0 = static_cast<std::uint32_t>(clampDouble(std::floor(v), 0.0, static_cast<double>(desc.resY - 2)));
|
|
496
|
+
const std::uint32_t k0 = static_cast<std::uint32_t>(clampDouble(std::floor(w), 0.0, static_cast<double>(desc.resZ - 2)));
|
|
497
|
+
|
|
498
|
+
const double tx = clampDouble(u - static_cast<double>(i0), 0.0, 1.0);
|
|
499
|
+
const double ty = clampDouble(v - static_cast<double>(j0), 0.0, 1.0);
|
|
500
|
+
const double tz = clampDouble(w - static_cast<double>(k0), 0.0, 1.0);
|
|
501
|
+
|
|
502
|
+
const double v000 = distances[sampleIndex(i0, j0, k0)];
|
|
503
|
+
const double v100 = distances[sampleIndex(i0 + 1, j0, k0)];
|
|
504
|
+
const double v010 = distances[sampleIndex(i0, j0 + 1, k0)];
|
|
505
|
+
const double v110 = distances[sampleIndex(i0 + 1, j0 + 1, k0)];
|
|
506
|
+
const double v001 = distances[sampleIndex(i0, j0, k0 + 1)];
|
|
507
|
+
const double v101 = distances[sampleIndex(i0 + 1, j0, k0 + 1)];
|
|
508
|
+
const double v011 = distances[sampleIndex(i0, j0 + 1, k0 + 1)];
|
|
509
|
+
const double v111 = distances[sampleIndex(i0 + 1, j0 + 1, k0 + 1)];
|
|
510
|
+
|
|
511
|
+
const double c00 = (1.0 - tx) * v000 + tx * v100;
|
|
512
|
+
const double c10 = (1.0 - tx) * v010 + tx * v110;
|
|
513
|
+
const double c01 = (1.0 - tx) * v001 + tx * v101;
|
|
514
|
+
const double c11 = (1.0 - tx) * v011 + tx * v111;
|
|
515
|
+
|
|
516
|
+
const double c0 = (1.0 - ty) * c00 + ty * c10;
|
|
517
|
+
const double c1 = (1.0 - ty) * c01 + ty * c11;
|
|
518
|
+
|
|
519
|
+
return (1.0 - tz) * c0 + tz * c1;
|
|
520
|
+
};
|
|
521
|
+
|
|
522
|
+
double interpolatedDist = interpolateTrilinear(cx, cy, cz);
|
|
523
|
+
|
|
524
|
+
const double epsX = std::max(1e-5, cSize.x * 0.05);
|
|
525
|
+
const double epsY = std::max(1e-5, cSize.y * 0.05);
|
|
526
|
+
const double epsZ = std::max(1e-5, cSize.z * 0.05);
|
|
527
|
+
|
|
528
|
+
const double xPlus = std::min(desc.bounds.max.x, cx + epsX);
|
|
529
|
+
const double xMinus = std::max(desc.bounds.min.x, cx - epsX);
|
|
530
|
+
const double actualEpsX = xPlus - xMinus;
|
|
531
|
+
const double gradX = actualEpsX > 1e-12 ? (interpolateTrilinear(xPlus, cy, cz) - interpolateTrilinear(xMinus, cy, cz)) / actualEpsX : 0.0;
|
|
532
|
+
|
|
533
|
+
const double yPlus = std::min(desc.bounds.max.y, cy + epsY);
|
|
534
|
+
const double yMinus = std::max(desc.bounds.min.y, cy - epsY);
|
|
535
|
+
const double actualEpsY = yPlus - yMinus;
|
|
536
|
+
const double gradY = actualEpsY > 1e-12 ? (interpolateTrilinear(cx, yPlus, cz) - interpolateTrilinear(cx, yMinus, cz)) / actualEpsY : 0.0;
|
|
537
|
+
|
|
538
|
+
const double zPlus = std::min(desc.bounds.max.z, cz + epsZ);
|
|
539
|
+
const double zMinus = std::max(desc.bounds.min.z, cz - epsZ);
|
|
540
|
+
const double actualEpsZ = zPlus - zMinus;
|
|
541
|
+
const double gradZ = actualEpsZ > 1e-12 ? (interpolateTrilinear(cx, cy, zPlus) - interpolateTrilinear(cx, cy, zMinus)) / actualEpsZ : 0.0;
|
|
542
|
+
|
|
543
|
+
SdfVec3 localGrad{gradX, gradY, gradZ};
|
|
544
|
+
|
|
545
|
+
if (isOutsideAabb && desc.boundaryPolicy == SdfBoundaryPolicy::ExtendDistance) {
|
|
546
|
+
const double distToAabb = solverSqrt(distToAabbSq);
|
|
547
|
+
interpolatedDist += distToAabb;
|
|
548
|
+
localGrad = scaleVec3({dxOut, dyOut, dzOut}, 1.0 / distToAabb);
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
SdfVec3 normLocalGrad = normalizeVec3(localGrad);
|
|
552
|
+
const SdfVec3 worldNormal = rotateByQuat(normOrientation, normLocalGrad);
|
|
553
|
+
|
|
554
|
+
// Material ID: nearest cell corner in deterministic order
|
|
555
|
+
std::uint32_t matId = 0;
|
|
556
|
+
if (materialIds.size() >= total) {
|
|
557
|
+
const double u = (cx - desc.bounds.min.x) / cSize.x;
|
|
558
|
+
const double v = (cy - desc.bounds.min.y) / cSize.y;
|
|
559
|
+
const double w = (cz - desc.bounds.min.z) / cSize.z;
|
|
560
|
+
const std::uint32_t i0 = static_cast<std::uint32_t>(clampDouble(std::floor(u), 0.0, static_cast<double>(desc.resX - 2)));
|
|
561
|
+
const std::uint32_t j0 = static_cast<std::uint32_t>(clampDouble(std::floor(v), 0.0, static_cast<double>(desc.resY - 2)));
|
|
562
|
+
const std::uint32_t k0 = static_cast<std::uint32_t>(clampDouble(std::floor(w), 0.0, static_cast<double>(desc.resZ - 2)));
|
|
563
|
+
const double tx = clampDouble(u - static_cast<double>(i0), 0.0, 1.0);
|
|
564
|
+
const double ty = clampDouble(v - static_cast<double>(j0), 0.0, 1.0);
|
|
565
|
+
const double tz = clampDouble(w - static_cast<double>(k0), 0.0, 1.0);
|
|
566
|
+
const std::uint32_t cix = (tx >= 0.5) ? i0 + 1 : i0;
|
|
567
|
+
const std::uint32_t ciy = (ty >= 0.5) ? j0 + 1 : j0;
|
|
568
|
+
const std::uint32_t ciz = (tz >= 0.5) ? k0 + 1 : k0;
|
|
569
|
+
matId = materialIds[sampleIndex(cix, ciy, ciz)];
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
outResult.distance = interpolatedDist;
|
|
573
|
+
outResult.gradient = worldNormal;
|
|
574
|
+
outResult.materialId = matId;
|
|
575
|
+
outResult.isInside = (interpolatedDist <= 0.0);
|
|
576
|
+
outResult.surfacePoint = subVec3(queryPointWorld, scaleVec3(worldNormal, interpolatedDist));
|
|
577
|
+
|
|
578
|
+
return SdfStatus::Success;
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
// ============================================================================
|
|
582
|
+
// Conservative bounds computation
|
|
583
|
+
// ============================================================================
|
|
584
|
+
|
|
585
|
+
SdfStatus computeConservativeBounds(
|
|
586
|
+
const SdfGridDesc& desc,
|
|
587
|
+
std::span<const double> distances,
|
|
588
|
+
double isoLevel,
|
|
589
|
+
double margin,
|
|
590
|
+
const SdfTransform& gridPose,
|
|
591
|
+
SdfAABB& outLocalAabb,
|
|
592
|
+
SdfAABB& outWorldAabb,
|
|
593
|
+
SdfBoundingSphere& outBoundingSphere) {
|
|
594
|
+
const std::uint32_t total = desc.totalSamples();
|
|
595
|
+
if (distances.size() < total) {
|
|
596
|
+
return SdfStatus::BufferOverflow;
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
const SdfVec3 cSize = desc.cellSize();
|
|
600
|
+
const double cellDiag = solverSqrt(cSize.x * cSize.x + cSize.y * cSize.y + cSize.z * cSize.z);
|
|
601
|
+
const double threshold = isoLevel + margin;
|
|
602
|
+
|
|
603
|
+
const auto sampleIndex = [&](std::uint32_t ix, std::uint32_t iy, std::uint32_t iz) -> std::uint32_t {
|
|
604
|
+
return iz * (desc.resX * desc.resY) + iy * desc.resX + ix;
|
|
605
|
+
};
|
|
606
|
+
|
|
607
|
+
double minX = desc.bounds.max.x;
|
|
608
|
+
double minY = desc.bounds.max.y;
|
|
609
|
+
double minZ = desc.bounds.max.z;
|
|
610
|
+
double maxX = desc.bounds.min.x;
|
|
611
|
+
double maxY = desc.bounds.min.y;
|
|
612
|
+
double maxZ = desc.bounds.min.z;
|
|
613
|
+
bool foundActiveCell = false;
|
|
614
|
+
|
|
615
|
+
for (std::uint32_t iz = 0; iz < desc.resZ - 1; ++iz) {
|
|
616
|
+
const double cz0 = desc.bounds.min.z + iz * cSize.z;
|
|
617
|
+
const double cz1 = cz0 + cSize.z;
|
|
618
|
+
for (std::uint32_t iy = 0; iy < desc.resY - 1; ++iy) {
|
|
619
|
+
const double cy0 = desc.bounds.min.y + iy * cSize.y;
|
|
620
|
+
const double cy1 = cy0 + cSize.y;
|
|
621
|
+
for (std::uint32_t ix = 0; ix < desc.resX - 1; ++ix) {
|
|
622
|
+
const double cx0 = desc.bounds.min.x + ix * cSize.x;
|
|
623
|
+
const double cx1 = cx0 + cSize.x;
|
|
624
|
+
|
|
625
|
+
const double v000 = distances[sampleIndex(ix, iy, iz)];
|
|
626
|
+
const double v100 = distances[sampleIndex(ix + 1, iy, iz)];
|
|
627
|
+
const double v010 = distances[sampleIndex(ix, iy + 1, iz)];
|
|
628
|
+
const double v110 = distances[sampleIndex(ix + 1, iy + 1, iz)];
|
|
629
|
+
const double v001 = distances[sampleIndex(ix, iy, iz + 1)];
|
|
630
|
+
const double v101 = distances[sampleIndex(ix + 1, iy, iz + 1)];
|
|
631
|
+
const double v011 = distances[sampleIndex(ix, iy + 1, iz + 1)];
|
|
632
|
+
const double v111 = distances[sampleIndex(ix + 1, iy + 1, iz + 1)];
|
|
633
|
+
|
|
634
|
+
const double cellMinDist = std::min(
|
|
635
|
+
min3(v000, v100, v010),
|
|
636
|
+
std::min(min3(v110, v001, v101), std::min(v011, v111)));
|
|
637
|
+
|
|
638
|
+
if (cellMinDist <= threshold) {
|
|
639
|
+
foundActiveCell = true;
|
|
640
|
+
minX = std::min(minX, cx0);
|
|
641
|
+
minY = std::min(minY, cy0);
|
|
642
|
+
minZ = std::min(minZ, cz0);
|
|
643
|
+
maxX = std::max(maxX, cx1);
|
|
644
|
+
maxY = std::max(maxY, cy1);
|
|
645
|
+
maxZ = std::max(maxZ, cz1);
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
if (foundActiveCell) {
|
|
652
|
+
// Add conservative safety margin
|
|
653
|
+
const double safety = cellDiag + margin;
|
|
654
|
+
minX = std::max(desc.bounds.min.x, minX - safety);
|
|
655
|
+
minY = std::max(desc.bounds.min.y, minY - safety);
|
|
656
|
+
minZ = std::max(desc.bounds.min.z, minZ - safety);
|
|
657
|
+
maxX = std::min(desc.bounds.max.x, maxX + safety);
|
|
658
|
+
maxY = std::min(desc.bounds.max.y, maxY + safety);
|
|
659
|
+
maxZ = std::min(desc.bounds.max.z, maxZ + safety);
|
|
660
|
+
} else {
|
|
661
|
+
// If no cells matched threshold, fall back to entire grid bounds
|
|
662
|
+
minX = desc.bounds.min.x;
|
|
663
|
+
minY = desc.bounds.min.y;
|
|
664
|
+
minZ = desc.bounds.min.z;
|
|
665
|
+
maxX = desc.bounds.max.x;
|
|
666
|
+
maxY = desc.bounds.max.y;
|
|
667
|
+
maxZ = desc.bounds.max.z;
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
outLocalAabb.min = {minX, minY, minZ};
|
|
671
|
+
outLocalAabb.max = {maxX, maxY, maxZ};
|
|
672
|
+
|
|
673
|
+
// World AABB transformation using orientation matrix
|
|
674
|
+
const SdfQuat normOrientation = normalizeQuat(gridPose.orientation);
|
|
675
|
+
const SdfMat3 r = quatToMat3(normOrientation);
|
|
676
|
+
const SdfMat3 absR = absMat3(r);
|
|
677
|
+
|
|
678
|
+
const SdfVec3 localCenter = scaleVec3(addVec3(outLocalAabb.min, outLocalAabb.max), 0.5);
|
|
679
|
+
const SdfVec3 localExtents = scaleVec3(subVec3(outLocalAabb.max, outLocalAabb.min), 0.5);
|
|
680
|
+
|
|
681
|
+
const SdfVec3 worldCenter = addVec3(gridPose.position, mulMat3Vec3(r, localCenter));
|
|
682
|
+
const SdfVec3 worldExtents = addVec3(mulMat3Vec3(absR, localExtents), {margin, margin, margin});
|
|
683
|
+
|
|
684
|
+
outWorldAabb.min = subVec3(worldCenter, worldExtents);
|
|
685
|
+
outWorldAabb.max = addVec3(worldCenter, worldExtents);
|
|
686
|
+
|
|
687
|
+
outBoundingSphere.center = worldCenter;
|
|
688
|
+
outBoundingSphere.radius = lengthVec3(worldExtents);
|
|
689
|
+
|
|
690
|
+
return SdfStatus::Success;
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
// ============================================================================
|
|
694
|
+
// Sphere tracing raycast
|
|
695
|
+
// ============================================================================
|
|
696
|
+
|
|
697
|
+
SdfStatus raycastSdfGrid(
|
|
698
|
+
const SdfGridDesc& desc,
|
|
699
|
+
std::span<const double> distances,
|
|
700
|
+
std::span<const std::uint32_t> materialIds,
|
|
701
|
+
const SdfTransform& gridPose,
|
|
702
|
+
const SdfRay& worldRay,
|
|
703
|
+
std::uint32_t maxSteps,
|
|
704
|
+
double hitTolerance,
|
|
705
|
+
SdfRayHit& outHit) {
|
|
706
|
+
outHit.hit = false;
|
|
707
|
+
outHit.distance = 0.0;
|
|
708
|
+
outHit.stepsTaken = 0;
|
|
709
|
+
|
|
710
|
+
if (distances.size() < desc.totalSamples()) {
|
|
711
|
+
return SdfStatus::BufferOverflow;
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
const SdfQuat normOrientation = normalizeQuat(gridPose.orientation);
|
|
715
|
+
const SdfVec3 rayDirNorm = normalizeVec3(worldRay.direction);
|
|
716
|
+
|
|
717
|
+
const SdfVec3 localRayOrigin = invRotateByQuat(normOrientation, subVec3(worldRay.origin, gridPose.position));
|
|
718
|
+
const SdfVec3 localRayDir = invRotateByQuat(normOrientation, rayDirNorm);
|
|
719
|
+
|
|
720
|
+
double tNear = 0.0;
|
|
721
|
+
double tFar = worldRay.maxDistance;
|
|
722
|
+
if (!intersectRayAABB(localRayOrigin, localRayDir, worldRay.maxDistance, desc.bounds, tNear, tFar)) {
|
|
723
|
+
return SdfStatus::NoHit;
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
const std::uint32_t stepsLimit = std::min(maxSteps > 0 ? maxSteps : 128, 512u);
|
|
727
|
+
const double tol = hitTolerance > 1e-12 ? hitTolerance : 1e-5;
|
|
728
|
+
|
|
729
|
+
double t = tNear;
|
|
730
|
+
std::uint32_t step = 0;
|
|
731
|
+
SdfSampleResult sampleRes;
|
|
732
|
+
|
|
733
|
+
while (step < stepsLimit && t <= tFar) {
|
|
734
|
+
const SdfVec3 currentPtWorld = addVec3(worldRay.origin, scaleVec3(rayDirNorm, t));
|
|
735
|
+
const SdfStatus stat = sampleSdfGrid(desc, distances, materialIds, gridPose, currentPtWorld, sampleRes);
|
|
736
|
+
if (stat != SdfStatus::Success) return stat;
|
|
737
|
+
|
|
738
|
+
const double d = sampleRes.distance;
|
|
739
|
+
|
|
740
|
+
if (std::abs(d) <= tol) {
|
|
741
|
+
outHit.hit = true;
|
|
742
|
+
outHit.distance = t;
|
|
743
|
+
outHit.point = currentPtWorld;
|
|
744
|
+
outHit.normal = sampleRes.gradient;
|
|
745
|
+
outHit.materialId = sampleRes.materialId;
|
|
746
|
+
outHit.stepsTaken = step + 1;
|
|
747
|
+
return SdfStatus::Success;
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
if (d < 0.0) {
|
|
751
|
+
// Stepped inside surface, perform deterministic bisection refinement
|
|
752
|
+
double t0 = std::max(tNear, t - std::max(tol, 0.01));
|
|
753
|
+
double t1 = t;
|
|
754
|
+
for (std::uint32_t iter = 0; iter < 8; ++iter) {
|
|
755
|
+
const double tm = 0.5 * (t0 + t1);
|
|
756
|
+
const SdfVec3 midPt = addVec3(worldRay.origin, scaleVec3(rayDirNorm, tm));
|
|
757
|
+
sampleSdfGrid(desc, distances, materialIds, gridPose, midPt, sampleRes);
|
|
758
|
+
if (sampleRes.distance < 0.0) {
|
|
759
|
+
t1 = tm;
|
|
760
|
+
} else {
|
|
761
|
+
t0 = tm;
|
|
762
|
+
}
|
|
763
|
+
}
|
|
764
|
+
outHit.hit = true;
|
|
765
|
+
outHit.distance = 0.5 * (t0 + t1);
|
|
766
|
+
outHit.point = addVec3(worldRay.origin, scaleVec3(rayDirNorm, outHit.distance));
|
|
767
|
+
outHit.normal = sampleRes.gradient;
|
|
768
|
+
outHit.materialId = sampleRes.materialId;
|
|
769
|
+
outHit.stepsTaken = step + 1;
|
|
770
|
+
return SdfStatus::Success;
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
const double stepAdvance = std::max(d * 0.95, tol * 0.5);
|
|
774
|
+
t += stepAdvance;
|
|
775
|
+
++step;
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
outHit.stepsTaken = step;
|
|
779
|
+
return SdfStatus::NoHit;
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
// ============================================================================
|
|
783
|
+
// Narrowphase collision queries
|
|
784
|
+
// ============================================================================
|
|
785
|
+
|
|
786
|
+
SdfStatus collideSdfWithSphere(
|
|
787
|
+
const SdfGridDesc& desc,
|
|
788
|
+
std::span<const double> distances,
|
|
789
|
+
std::span<const std::uint32_t> materialIds,
|
|
790
|
+
const SdfTransform& gridPose,
|
|
791
|
+
const SdfVec3& sphereCenterWorld,
|
|
792
|
+
double sphereRadius,
|
|
793
|
+
std::uint32_t sphereMaterialId,
|
|
794
|
+
SdfCollisionResult& outCollision) {
|
|
795
|
+
outCollision.hasCollision = false;
|
|
796
|
+
|
|
797
|
+
SdfSampleResult sample;
|
|
798
|
+
const SdfStatus stat = sampleSdfGrid(desc, distances, materialIds, gridPose, sphereCenterWorld, sample);
|
|
799
|
+
if (stat != SdfStatus::Success) return stat;
|
|
800
|
+
|
|
801
|
+
const double separation = sample.distance - sphereRadius;
|
|
802
|
+
if (separation < 0.0) {
|
|
803
|
+
const double penetration = -separation;
|
|
804
|
+
outCollision.hasCollision = true;
|
|
805
|
+
outCollision.normal = sample.gradient; // Outward from SDF towards sphere
|
|
806
|
+
outCollision.penetration = penetration;
|
|
807
|
+
outCollision.witnessA = sample.surfacePoint;
|
|
808
|
+
outCollision.witnessB = subVec3(sphereCenterWorld, scaleVec3(sample.gradient, sphereRadius));
|
|
809
|
+
outCollision.contactPoint = scaleVec3(addVec3(outCollision.witnessA, outCollision.witnessB), 0.5);
|
|
810
|
+
outCollision.materialIdA = sample.materialId;
|
|
811
|
+
outCollision.materialIdB = sphereMaterialId;
|
|
812
|
+
return SdfStatus::Success;
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
return SdfStatus::Disjoint;
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
SdfStatus collideSdfWithCapsule(
|
|
819
|
+
const SdfGridDesc& desc,
|
|
820
|
+
std::span<const double> distances,
|
|
821
|
+
std::span<const std::uint32_t> materialIds,
|
|
822
|
+
const SdfTransform& gridPose,
|
|
823
|
+
const SdfVec3& capsuleAWorld,
|
|
824
|
+
const SdfVec3& capsuleBWorld,
|
|
825
|
+
double capsuleRadius,
|
|
826
|
+
std::uint32_t capsuleMaterialId,
|
|
827
|
+
SdfCollisionResult& outCollision) {
|
|
828
|
+
outCollision.hasCollision = false;
|
|
829
|
+
|
|
830
|
+
// Sample uniform points along segment to find approximate minimum distance
|
|
831
|
+
constexpr std::uint32_t kSegmentSamples = 16;
|
|
832
|
+
double bestT = 0.0;
|
|
833
|
+
double minDistance = 1e30;
|
|
834
|
+
SdfSampleResult bestSample;
|
|
835
|
+
|
|
836
|
+
for (std::uint32_t i = 0; i <= kSegmentSamples; ++i) {
|
|
837
|
+
const double t = static_cast<double>(i) / static_cast<double>(kSegmentSamples);
|
|
838
|
+
const SdfVec3 pt = addVec3(capsuleAWorld, scaleVec3(subVec3(capsuleBWorld, capsuleAWorld), t));
|
|
839
|
+
SdfSampleResult sample;
|
|
840
|
+
sampleSdfGrid(desc, distances, materialIds, gridPose, pt, sample);
|
|
841
|
+
if (sample.distance < minDistance) {
|
|
842
|
+
minDistance = sample.distance;
|
|
843
|
+
bestT = t;
|
|
844
|
+
bestSample = sample;
|
|
845
|
+
}
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
// Refine closest point using golden section search
|
|
849
|
+
double a = std::max(0.0, bestT - 1.0 / static_cast<double>(kSegmentSamples));
|
|
850
|
+
double b = std::min(1.0, bestT + 1.0 / static_cast<double>(kSegmentSamples));
|
|
851
|
+
constexpr double phi = 0.618033988749895; // (sqrt(5) - 1) / 2
|
|
852
|
+
double c = b - phi * (b - a);
|
|
853
|
+
double d = a + phi * (b - a);
|
|
854
|
+
|
|
855
|
+
for (std::uint32_t iter = 0; iter < 8; ++iter) {
|
|
856
|
+
const SdfVec3 ptC = addVec3(capsuleAWorld, scaleVec3(subVec3(capsuleBWorld, capsuleAWorld), c));
|
|
857
|
+
const SdfVec3 ptD = addVec3(capsuleAWorld, scaleVec3(subVec3(capsuleBWorld, capsuleAWorld), d));
|
|
858
|
+
SdfSampleResult sampleC, sampleD;
|
|
859
|
+
sampleSdfGrid(desc, distances, materialIds, gridPose, ptC, sampleC);
|
|
860
|
+
sampleSdfGrid(desc, distances, materialIds, gridPose, ptD, sampleD);
|
|
861
|
+
|
|
862
|
+
if (sampleC.distance < sampleD.distance) {
|
|
863
|
+
b = d;
|
|
864
|
+
d = c;
|
|
865
|
+
c = b - phi * (b - a);
|
|
866
|
+
} else {
|
|
867
|
+
a = c;
|
|
868
|
+
c = d;
|
|
869
|
+
d = a + phi * (b - a);
|
|
870
|
+
}
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
const double finalT = 0.5 * (a + b);
|
|
874
|
+
const SdfVec3 finalCenter = addVec3(capsuleAWorld, scaleVec3(subVec3(capsuleBWorld, capsuleAWorld), finalT));
|
|
875
|
+
sampleSdfGrid(desc, distances, materialIds, gridPose, finalCenter, bestSample);
|
|
876
|
+
|
|
877
|
+
const double separation = bestSample.distance - capsuleRadius;
|
|
878
|
+
if (separation < 0.0) {
|
|
879
|
+
const double penetration = -separation;
|
|
880
|
+
outCollision.hasCollision = true;
|
|
881
|
+
outCollision.normal = bestSample.gradient;
|
|
882
|
+
outCollision.penetration = penetration;
|
|
883
|
+
outCollision.witnessA = bestSample.surfacePoint;
|
|
884
|
+
outCollision.witnessB = subVec3(finalCenter, scaleVec3(bestSample.gradient, capsuleRadius));
|
|
885
|
+
outCollision.contactPoint = scaleVec3(addVec3(outCollision.witnessA, outCollision.witnessB), 0.5);
|
|
886
|
+
outCollision.materialIdA = bestSample.materialId;
|
|
887
|
+
outCollision.materialIdB = capsuleMaterialId;
|
|
888
|
+
return SdfStatus::Success;
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
return SdfStatus::Disjoint;
|
|
892
|
+
}
|
|
893
|
+
|
|
894
|
+
SdfStatus collideSdfWithBox(
|
|
895
|
+
const SdfGridDesc& desc,
|
|
896
|
+
std::span<const double> distances,
|
|
897
|
+
std::span<const std::uint32_t> materialIds,
|
|
898
|
+
const SdfTransform& gridPose,
|
|
899
|
+
const SdfTransform& boxPose,
|
|
900
|
+
const SdfVec3& boxHalfExtents,
|
|
901
|
+
std::uint32_t boxMaterialId,
|
|
902
|
+
SdfCollisionResult& outCollision) {
|
|
903
|
+
outCollision.hasCollision = false;
|
|
904
|
+
|
|
905
|
+
const SdfQuat normBoxQuat = normalizeQuat(boxPose.orientation);
|
|
906
|
+
|
|
907
|
+
// Test 8 vertices and 6 face centers
|
|
908
|
+
constexpr std::array<SdfVec3, 14> kLocalTestPoints{{
|
|
909
|
+
{-1.0, -1.0, -1.0}, {1.0, -1.0, -1.0}, {-1.0, 1.0, -1.0}, {1.0, 1.0, -1.0},
|
|
910
|
+
{-1.0, -1.0, 1.0}, {1.0, -1.0, 1.0}, {-1.0, 1.0, 1.0}, {1.0, 1.0, 1.0},
|
|
911
|
+
{1.0, 0.0, 0.0}, {-1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {0.0, -1.0, 0.0},
|
|
912
|
+
{0.0, 0.0, 1.0}, {0.0, 0.0, -1.0}
|
|
913
|
+
}};
|
|
914
|
+
|
|
915
|
+
double maxPenetration = 0.0;
|
|
916
|
+
SdfSampleResult deepestSample;
|
|
917
|
+
SdfVec3 deepestBoxPointWorld{};
|
|
918
|
+
|
|
919
|
+
for (const auto& localUnit : kLocalTestPoints) {
|
|
920
|
+
const SdfVec3 localPt{
|
|
921
|
+
localUnit.x * boxHalfExtents.x,
|
|
922
|
+
localUnit.y * boxHalfExtents.y,
|
|
923
|
+
localUnit.z * boxHalfExtents.z
|
|
924
|
+
};
|
|
925
|
+
const SdfVec3 worldPt = addVec3(boxPose.position, rotateByQuat(normBoxQuat, localPt));
|
|
926
|
+
|
|
927
|
+
SdfSampleResult sample;
|
|
928
|
+
sampleSdfGrid(desc, distances, materialIds, gridPose, worldPt, sample);
|
|
929
|
+
|
|
930
|
+
if (sample.distance < 0.0) {
|
|
931
|
+
const double pen = -sample.distance;
|
|
932
|
+
if (pen > maxPenetration) {
|
|
933
|
+
maxPenetration = pen;
|
|
934
|
+
deepestSample = sample;
|
|
935
|
+
deepestBoxPointWorld = worldPt;
|
|
936
|
+
}
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
|
|
940
|
+
if (maxPenetration > 0.0) {
|
|
941
|
+
outCollision.hasCollision = true;
|
|
942
|
+
outCollision.normal = deepestSample.gradient;
|
|
943
|
+
outCollision.penetration = maxPenetration;
|
|
944
|
+
outCollision.witnessA = deepestSample.surfacePoint;
|
|
945
|
+
outCollision.witnessB = deepestBoxPointWorld;
|
|
946
|
+
outCollision.contactPoint = scaleVec3(addVec3(outCollision.witnessA, outCollision.witnessB), 0.5);
|
|
947
|
+
outCollision.materialIdA = deepestSample.materialId;
|
|
948
|
+
outCollision.materialIdB = boxMaterialId;
|
|
949
|
+
return SdfStatus::Success;
|
|
950
|
+
}
|
|
951
|
+
|
|
952
|
+
return SdfStatus::Disjoint;
|
|
953
|
+
}
|
|
954
|
+
|
|
955
|
+
SdfStatus collideSdfWithParticles(
|
|
956
|
+
const SdfGridDesc& desc,
|
|
957
|
+
std::span<const double> distances,
|
|
958
|
+
std::span<const std::uint32_t> materialIds,
|
|
959
|
+
const SdfTransform& gridPose,
|
|
960
|
+
const double* particlePositions,
|
|
961
|
+
std::uint32_t particleCount,
|
|
962
|
+
std::uint32_t particleStride,
|
|
963
|
+
double particleRadius,
|
|
964
|
+
std::uint32_t particleMaterialId,
|
|
965
|
+
double* outParticleCollisions,
|
|
966
|
+
std::uint32_t* outHitCount) {
|
|
967
|
+
if (particlePositions == nullptr || outParticleCollisions == nullptr || outHitCount == nullptr) {
|
|
968
|
+
return SdfStatus::InvalidInput;
|
|
969
|
+
}
|
|
970
|
+
|
|
971
|
+
*outHitCount = 0;
|
|
972
|
+
const std::uint32_t stride = particleStride >= 3 ? particleStride : 3;
|
|
973
|
+
|
|
974
|
+
for (std::uint32_t i = 0; i < particleCount; ++i) {
|
|
975
|
+
const double px = particlePositions[i * stride + 0];
|
|
976
|
+
const double py = particlePositions[i * stride + 1];
|
|
977
|
+
const double pz = particlePositions[i * stride + 2];
|
|
978
|
+
const SdfVec3 posWorld{px, py, pz};
|
|
979
|
+
|
|
980
|
+
SdfSampleResult sample;
|
|
981
|
+
sampleSdfGrid(desc, distances, materialIds, gridPose, posWorld, sample);
|
|
982
|
+
|
|
983
|
+
const double separation = sample.distance - particleRadius;
|
|
984
|
+
if (separation < 0.0) {
|
|
985
|
+
const double pen = -separation;
|
|
986
|
+
const std::uint32_t outOffset = (*outHitCount) * kSdfParticleCollisionStride;
|
|
987
|
+
outParticleCollisions[outOffset + 0] = static_cast<double>(i);
|
|
988
|
+
outParticleCollisions[outOffset + 1] = sample.gradient.x;
|
|
989
|
+
outParticleCollisions[outOffset + 2] = sample.gradient.y;
|
|
990
|
+
outParticleCollisions[outOffset + 3] = sample.gradient.z;
|
|
991
|
+
outParticleCollisions[outOffset + 4] = pen;
|
|
992
|
+
outParticleCollisions[outOffset + 5] = sample.surfacePoint.x;
|
|
993
|
+
outParticleCollisions[outOffset + 6] = sample.surfacePoint.y;
|
|
994
|
+
outParticleCollisions[outOffset + 7] = sample.surfacePoint.z;
|
|
995
|
+
(*outHitCount)++;
|
|
996
|
+
}
|
|
997
|
+
}
|
|
998
|
+
|
|
999
|
+
(void)particleMaterialId;
|
|
1000
|
+
return SdfStatus::Success;
|
|
1001
|
+
}
|
|
1002
|
+
|
|
1003
|
+
// ============================================================================
|
|
1004
|
+
// Custom geometry contract
|
|
1005
|
+
// ============================================================================
|
|
1006
|
+
|
|
1007
|
+
SdfStatus evaluateCustomGeometry(
|
|
1008
|
+
const CustomGeometryDesc& desc,
|
|
1009
|
+
const SdfTransform& geomPose,
|
|
1010
|
+
const SdfVec3& queryPointWorld,
|
|
1011
|
+
SdfSampleResult& outResult) {
|
|
1012
|
+
const SdfQuat normOrientation = normalizeQuat(geomPose.orientation);
|
|
1013
|
+
const SdfVec3 localPt = invRotateByQuat(normOrientation, subVec3(queryPointWorld, geomPose.position));
|
|
1014
|
+
|
|
1015
|
+
double dist = 1e30;
|
|
1016
|
+
SdfVec3 localGrad{0.0, 1.0, 0.0};
|
|
1017
|
+
|
|
1018
|
+
switch (desc.customType) {
|
|
1019
|
+
case CustomGeometryType::ProceduralSdf: {
|
|
1020
|
+
// Standard Sphere or primitive fallback based on params[0]
|
|
1021
|
+
const double r = desc.params[0] > 0.0 ? desc.params[0] : 1.0;
|
|
1022
|
+
dist = lengthVec3(localPt) - r;
|
|
1023
|
+
localGrad = normalizeVec3(localPt);
|
|
1024
|
+
break;
|
|
1025
|
+
}
|
|
1026
|
+
case CustomGeometryType::Torus: {
|
|
1027
|
+
const double R = desc.params[0] > 0.0 ? desc.params[0] : 1.0;
|
|
1028
|
+
const double r = desc.params[1] > 0.0 ? desc.params[1] : 0.25;
|
|
1029
|
+
const double qxz = solverSqrt(localPt.x * localPt.x + localPt.z * localPt.z) - R;
|
|
1030
|
+
dist = solverSqrt(qxz * qxz + localPt.y * localPt.y) - r;
|
|
1031
|
+
|
|
1032
|
+
const double lenXZ = solverSqrt(localPt.x * localPt.x + localPt.z * localPt.z);
|
|
1033
|
+
if (lenXZ > kZeroTolerance) {
|
|
1034
|
+
const double factor = qxz / solverSqrt(qxz * qxz + localPt.y * localPt.y);
|
|
1035
|
+
localGrad = {
|
|
1036
|
+
(localPt.x / lenXZ) * factor,
|
|
1037
|
+
localPt.y / solverSqrt(qxz * qxz + localPt.y * localPt.y),
|
|
1038
|
+
(localPt.z / lenXZ) * factor
|
|
1039
|
+
};
|
|
1040
|
+
}
|
|
1041
|
+
break;
|
|
1042
|
+
}
|
|
1043
|
+
case CustomGeometryType::Superquadric: {
|
|
1044
|
+
// Ellipsoid superquadric with half-axes a, b, c and exponents e1, e2
|
|
1045
|
+
const double a = desc.params[0] > 0.0 ? desc.params[0] : 1.0;
|
|
1046
|
+
const double b = desc.params[1] > 0.0 ? desc.params[1] : 1.0;
|
|
1047
|
+
const double c = desc.params[2] > 0.0 ? desc.params[2] : 1.0;
|
|
1048
|
+
const double e1 = desc.params[3] > 0.0 ? desc.params[3] : 1.0; // east-west exponent
|
|
1049
|
+
const double e2 = desc.params[4] > 0.0 ? desc.params[4] : 1.0; // north-south exponent
|
|
1050
|
+
|
|
1051
|
+
const double px = std::abs(localPt.x / a);
|
|
1052
|
+
const double py = std::abs(localPt.y / b);
|
|
1053
|
+
const double pz = std::abs(localPt.z / c);
|
|
1054
|
+
|
|
1055
|
+
const double pxy = std::pow(std::pow(px, 2.0 / e2) + std::pow(py, 2.0 / e2), e2 / e1);
|
|
1056
|
+
const double F = pxy + std::pow(pz, 2.0 / e1) - 1.0;
|
|
1057
|
+
|
|
1058
|
+
// Approximate distance = F / |grad F|
|
|
1059
|
+
const double dpx = (2.0 / (e1 * a)) * std::pow(px, 2.0 / e2 - 1.0) *
|
|
1060
|
+
std::pow(std::pow(px, 2.0 / e2) + std::pow(py, 2.0 / e2), e2 / e1 - 1.0);
|
|
1061
|
+
const double dpy = (2.0 / (e1 * b)) * std::pow(py, 2.0 / e2 - 1.0) *
|
|
1062
|
+
std::pow(std::pow(px, 2.0 / e2) + std::pow(py, 2.0 / e2), e2 / e1 - 1.0);
|
|
1063
|
+
const double dpz = (2.0 / (e1 * c)) * std::pow(pz, 2.0 / e1 - 1.0);
|
|
1064
|
+
const double gradLen = solverSqrt(dpx * dpx + dpy * dpy + dpz * dpz);
|
|
1065
|
+
if (gradLen > kZeroTolerance) {
|
|
1066
|
+
dist = F / gradLen;
|
|
1067
|
+
localGrad = normalizeVec3({dpx * (localPt.x >= 0 ? 1.0 : -1.0),
|
|
1068
|
+
dpy * (localPt.y >= 0 ? 1.0 : -1.0),
|
|
1069
|
+
dpz * (localPt.z >= 0 ? 1.0 : -1.0)});
|
|
1070
|
+
} else {
|
|
1071
|
+
dist = lengthVec3(localPt) - std::min(a, std::min(b, c));
|
|
1072
|
+
localGrad = normalizeVec3(localPt);
|
|
1073
|
+
}
|
|
1074
|
+
break;
|
|
1075
|
+
}
|
|
1076
|
+
case CustomGeometryType::Gyroid: {
|
|
1077
|
+
// Gyroid minimal surface: sin(kx)*cos(ky) + sin(ky)*cos(kz) + sin(kz)*cos(kx) = offset
|
|
1078
|
+
const double k = desc.params[0] > 0.0 ? desc.params[0] : 1.0;
|
|
1079
|
+
const double offset = desc.params[1];
|
|
1080
|
+
const double thickness = desc.params[2] > 0.0 ? desc.params[2] : 0.1;
|
|
1081
|
+
|
|
1082
|
+
const double sx = std::sin(k * localPt.x);
|
|
1083
|
+
const double cx = std::cos(k * localPt.x);
|
|
1084
|
+
const double sy = std::sin(k * localPt.y);
|
|
1085
|
+
const double cy = std::cos(k * localPt.y);
|
|
1086
|
+
const double sz = std::sin(k * localPt.z);
|
|
1087
|
+
const double cz = std::cos(k * localPt.z);
|
|
1088
|
+
|
|
1089
|
+
const double F = sx * cy + sy * cz + sz * cx - offset;
|
|
1090
|
+
const double gx = k * (cx * cy - sz * sx);
|
|
1091
|
+
const double gy = k * (cy * cz - sx * sy);
|
|
1092
|
+
const double gz = k * (cz * cx - sy * sz);
|
|
1093
|
+
const double gradLen = solverSqrt(gx * gx + gy * gy + gz * gz);
|
|
1094
|
+
|
|
1095
|
+
if (gradLen > kZeroTolerance) {
|
|
1096
|
+
dist = std::abs(F) / gradLen - thickness * 0.5;
|
|
1097
|
+
localGrad = scaleVec3({gx, gy, gz}, (F >= 0.0 ? 1.0 : -1.0) / gradLen);
|
|
1098
|
+
} else {
|
|
1099
|
+
dist = lengthVec3(localPt) - 1.0;
|
|
1100
|
+
localGrad = normalizeVec3(localPt);
|
|
1101
|
+
}
|
|
1102
|
+
break;
|
|
1103
|
+
}
|
|
1104
|
+
case CustomGeometryType::Heightfield: {
|
|
1105
|
+
// Sine wave procedural heightfield
|
|
1106
|
+
const double amp = desc.params[0] != 0.0 ? desc.params[0] : 0.2;
|
|
1107
|
+
const double freq = desc.params[1] > 0.0 ? desc.params[1] : 1.0;
|
|
1108
|
+
const double h = amp * std::sin(freq * localPt.x) * std::cos(freq * localPt.z);
|
|
1109
|
+
const double dy = localPt.y - h;
|
|
1110
|
+
dist = dy / solverSqrt(1.0 + amp * amp * freq * freq);
|
|
1111
|
+
localGrad = normalizeVec3({-amp * freq * std::cos(freq * localPt.x) * std::cos(freq * localPt.z),
|
|
1112
|
+
1.0,
|
|
1113
|
+
amp * freq * std::sin(freq * localPt.x) * std::sin(freq * localPt.z)});
|
|
1114
|
+
break;
|
|
1115
|
+
}
|
|
1116
|
+
case CustomGeometryType::UserCallback:
|
|
1117
|
+
dist = lengthVec3(localPt) - 1.0;
|
|
1118
|
+
localGrad = normalizeVec3(localPt);
|
|
1119
|
+
break;
|
|
1120
|
+
}
|
|
1121
|
+
|
|
1122
|
+
const SdfVec3 worldNormal = rotateByQuat(normOrientation, normalizeVec3(localGrad));
|
|
1123
|
+
outResult.distance = dist;
|
|
1124
|
+
outResult.gradient = worldNormal;
|
|
1125
|
+
outResult.materialId = desc.materialId;
|
|
1126
|
+
outResult.isInside = (dist <= 0.0);
|
|
1127
|
+
outResult.surfacePoint = subVec3(queryPointWorld, scaleVec3(worldNormal, dist));
|
|
1128
|
+
|
|
1129
|
+
return SdfStatus::Success;
|
|
1130
|
+
}
|
|
1131
|
+
|
|
1132
|
+
SdfStatus computeCustomGeometryBounds(
|
|
1133
|
+
const CustomGeometryDesc& desc,
|
|
1134
|
+
const SdfTransform& geomPose,
|
|
1135
|
+
double margin,
|
|
1136
|
+
SdfAABB& outLocalAabb,
|
|
1137
|
+
SdfAABB& outWorldAabb,
|
|
1138
|
+
SdfBoundingSphere& outBoundingSphere) {
|
|
1139
|
+
outLocalAabb = desc.localBounds;
|
|
1140
|
+
|
|
1141
|
+
const SdfQuat normOrientation = normalizeQuat(geomPose.orientation);
|
|
1142
|
+
const SdfMat3 r = quatToMat3(normOrientation);
|
|
1143
|
+
const SdfMat3 absR = absMat3(r);
|
|
1144
|
+
|
|
1145
|
+
const SdfVec3 localCenter = scaleVec3(addVec3(outLocalAabb.min, outLocalAabb.max), 0.5);
|
|
1146
|
+
const SdfVec3 localExtents = scaleVec3(subVec3(outLocalAabb.max, outLocalAabb.min), 0.5);
|
|
1147
|
+
|
|
1148
|
+
const SdfVec3 worldCenter = addVec3(geomPose.position, mulMat3Vec3(r, localCenter));
|
|
1149
|
+
const SdfVec3 worldExtents = addVec3(mulMat3Vec3(absR, localExtents), {margin, margin, margin});
|
|
1150
|
+
|
|
1151
|
+
outWorldAabb.min = subVec3(worldCenter, worldExtents);
|
|
1152
|
+
outWorldAabb.max = addVec3(worldCenter, worldExtents);
|
|
1153
|
+
|
|
1154
|
+
outBoundingSphere.center = worldCenter;
|
|
1155
|
+
outBoundingSphere.radius = lengthVec3(worldExtents);
|
|
1156
|
+
|
|
1157
|
+
return SdfStatus::Success;
|
|
1158
|
+
}
|
|
1159
|
+
|
|
1160
|
+
SdfStatus raycastCustomGeometry(
|
|
1161
|
+
const CustomGeometryDesc& desc,
|
|
1162
|
+
const SdfTransform& geomPose,
|
|
1163
|
+
const SdfRay& worldRay,
|
|
1164
|
+
std::uint32_t maxSteps,
|
|
1165
|
+
double hitTolerance,
|
|
1166
|
+
SdfRayHit& outHit) {
|
|
1167
|
+
outHit.hit = false;
|
|
1168
|
+
outHit.distance = 0.0;
|
|
1169
|
+
outHit.stepsTaken = 0;
|
|
1170
|
+
|
|
1171
|
+
const SdfQuat normOrientation = normalizeQuat(geomPose.orientation);
|
|
1172
|
+
const SdfVec3 rayDirNorm = normalizeVec3(worldRay.direction);
|
|
1173
|
+
|
|
1174
|
+
const SdfVec3 localRayOrigin = invRotateByQuat(normOrientation, subVec3(worldRay.origin, geomPose.position));
|
|
1175
|
+
const SdfVec3 localRayDir = invRotateByQuat(normOrientation, rayDirNorm);
|
|
1176
|
+
|
|
1177
|
+
double tNear = 0.0;
|
|
1178
|
+
double tFar = worldRay.maxDistance;
|
|
1179
|
+
if (!intersectRayAABB(localRayOrigin, localRayDir, worldRay.maxDistance, desc.localBounds, tNear, tFar)) {
|
|
1180
|
+
return SdfStatus::NoHit;
|
|
1181
|
+
}
|
|
1182
|
+
|
|
1183
|
+
const std::uint32_t stepsLimit = std::min(maxSteps > 0 ? maxSteps : 128, 512u);
|
|
1184
|
+
const double tol = hitTolerance > 1e-12 ? hitTolerance : 1e-5;
|
|
1185
|
+
|
|
1186
|
+
double t = tNear;
|
|
1187
|
+
std::uint32_t step = 0;
|
|
1188
|
+
SdfSampleResult sampleRes;
|
|
1189
|
+
|
|
1190
|
+
while (step < stepsLimit && t <= tFar) {
|
|
1191
|
+
const SdfVec3 currentPtWorld = addVec3(worldRay.origin, scaleVec3(rayDirNorm, t));
|
|
1192
|
+
evaluateCustomGeometry(desc, geomPose, currentPtWorld, sampleRes);
|
|
1193
|
+
|
|
1194
|
+
const double d = sampleRes.distance;
|
|
1195
|
+
if (std::abs(d) <= tol) {
|
|
1196
|
+
outHit.hit = true;
|
|
1197
|
+
outHit.distance = t;
|
|
1198
|
+
outHit.point = currentPtWorld;
|
|
1199
|
+
outHit.normal = sampleRes.gradient;
|
|
1200
|
+
outHit.materialId = sampleRes.materialId;
|
|
1201
|
+
outHit.stepsTaken = step + 1;
|
|
1202
|
+
return SdfStatus::Success;
|
|
1203
|
+
}
|
|
1204
|
+
|
|
1205
|
+
if (d < 0.0) {
|
|
1206
|
+
double t0 = std::max(tNear, t - std::max(tol, 0.01));
|
|
1207
|
+
double t1 = t;
|
|
1208
|
+
for (std::uint32_t iter = 0; iter < 8; ++iter) {
|
|
1209
|
+
const double tm = 0.5 * (t0 + t1);
|
|
1210
|
+
const SdfVec3 midPt = addVec3(worldRay.origin, scaleVec3(rayDirNorm, tm));
|
|
1211
|
+
evaluateCustomGeometry(desc, geomPose, midPt, sampleRes);
|
|
1212
|
+
if (sampleRes.distance < 0.0) t1 = tm;
|
|
1213
|
+
else t0 = tm;
|
|
1214
|
+
}
|
|
1215
|
+
outHit.hit = true;
|
|
1216
|
+
outHit.distance = 0.5 * (t0 + t1);
|
|
1217
|
+
outHit.point = addVec3(worldRay.origin, scaleVec3(rayDirNorm, outHit.distance));
|
|
1218
|
+
outHit.normal = sampleRes.gradient;
|
|
1219
|
+
outHit.materialId = sampleRes.materialId;
|
|
1220
|
+
outHit.stepsTaken = step + 1;
|
|
1221
|
+
return SdfStatus::Success;
|
|
1222
|
+
}
|
|
1223
|
+
|
|
1224
|
+
const double stepAdvance = std::max(d * 0.95, tol * 0.5);
|
|
1225
|
+
t += stepAdvance;
|
|
1226
|
+
++step;
|
|
1227
|
+
}
|
|
1228
|
+
|
|
1229
|
+
outHit.stepsTaken = step;
|
|
1230
|
+
return SdfStatus::NoHit;
|
|
1231
|
+
}
|
|
1232
|
+
|
|
1233
|
+
} // namespace rundot::kinetix::physics::sdf
|
|
1234
|
+
|
|
1235
|
+
// ============================================================================
|
|
1236
|
+
// C-ABI Exports
|
|
1237
|
+
// ============================================================================
|
|
1238
|
+
|
|
1239
|
+
extern "C" {
|
|
1240
|
+
|
|
1241
|
+
using namespace rundot::kinetix::physics::sdf;
|
|
1242
|
+
|
|
1243
|
+
std::uint32_t rundot_kinetix_ext_sdf_abi_version() {
|
|
1244
|
+
return kSdfGeometryAbiVersion;
|
|
1245
|
+
}
|
|
1246
|
+
|
|
1247
|
+
std::uint32_t rundot_kinetix_ext_custom_geometry_abi_version() {
|
|
1248
|
+
return kCustomGeometryAbiVersion;
|
|
1249
|
+
}
|
|
1250
|
+
|
|
1251
|
+
std::uint32_t rundot_kinetix_ext_sdf_grid_desc_stride() {
|
|
1252
|
+
return kSdfGridDescStride;
|
|
1253
|
+
}
|
|
1254
|
+
|
|
1255
|
+
std::uint32_t rundot_kinetix_ext_sdf_pose_stride() {
|
|
1256
|
+
return kSdfPoseStride;
|
|
1257
|
+
}
|
|
1258
|
+
|
|
1259
|
+
std::uint32_t rundot_kinetix_ext_sdf_query_input_stride() {
|
|
1260
|
+
return kSdfQueryInputStride;
|
|
1261
|
+
}
|
|
1262
|
+
|
|
1263
|
+
std::uint32_t rundot_kinetix_ext_sdf_query_output_stride() {
|
|
1264
|
+
return kSdfQueryOutputStride;
|
|
1265
|
+
}
|
|
1266
|
+
|
|
1267
|
+
std::uint32_t rundot_kinetix_ext_sdf_collision_stride() {
|
|
1268
|
+
return kSdfCollisionResultStride;
|
|
1269
|
+
}
|
|
1270
|
+
|
|
1271
|
+
std::uint32_t rundot_kinetix_ext_sdf_ray_stride() {
|
|
1272
|
+
return kSdfRayStride;
|
|
1273
|
+
}
|
|
1274
|
+
|
|
1275
|
+
std::uint32_t rundot_kinetix_ext_sdf_rayhit_stride() {
|
|
1276
|
+
return kSdfRayHitStride;
|
|
1277
|
+
}
|
|
1278
|
+
|
|
1279
|
+
std::uint32_t rundot_kinetix_ext_custom_geometry_desc_stride() {
|
|
1280
|
+
return kCustomGeometryDescStride;
|
|
1281
|
+
}
|
|
1282
|
+
|
|
1283
|
+
static SdfGridDesc unpackGridDesc(const double* d) {
|
|
1284
|
+
SdfGridDesc desc;
|
|
1285
|
+
desc.bounds.min = {d[0], d[1], d[2]};
|
|
1286
|
+
desc.bounds.max = {d[3], d[4], d[5]};
|
|
1287
|
+
desc.resX = static_cast<std::uint32_t>(d[6] > 1.0 ? d[6] : 2.0);
|
|
1288
|
+
desc.resY = static_cast<std::uint32_t>(d[7] > 1.0 ? d[7] : 2.0);
|
|
1289
|
+
desc.resZ = static_cast<std::uint32_t>(d[8] > 1.0 ? d[8] : 2.0);
|
|
1290
|
+
desc.maxTruncationDistance = d[9] > 0.0 ? d[9] : 1e30;
|
|
1291
|
+
desc.interpolationMode = static_cast<SdfInterpolationMode>(static_cast<std::uint32_t>(d[10]));
|
|
1292
|
+
desc.boundaryPolicy = static_cast<SdfBoundaryPolicy>(static_cast<std::uint32_t>(d[11]));
|
|
1293
|
+
return desc;
|
|
1294
|
+
}
|
|
1295
|
+
|
|
1296
|
+
static SdfTransform unpackTransform(const double* p) {
|
|
1297
|
+
if (p == nullptr) return SdfTransform{};
|
|
1298
|
+
return SdfTransform{
|
|
1299
|
+
{p[0], p[1], p[2]},
|
|
1300
|
+
{p[3], p[4], p[5], p[6]}
|
|
1301
|
+
};
|
|
1302
|
+
}
|
|
1303
|
+
|
|
1304
|
+
std::int32_t rundot_kinetix_ext_sdf_cook_primitive(
|
|
1305
|
+
const double* gridDescData,
|
|
1306
|
+
double* outDistances,
|
|
1307
|
+
std::uint32_t* outMaterialIds,
|
|
1308
|
+
std::uint32_t primitiveType,
|
|
1309
|
+
const double* primitiveParams,
|
|
1310
|
+
const double* primitivePose,
|
|
1311
|
+
std::uint32_t materialId) {
|
|
1312
|
+
if (gridDescData == nullptr || outDistances == nullptr || outMaterialIds == nullptr || primitiveParams == nullptr) {
|
|
1313
|
+
return static_cast<std::int32_t>(SdfStatus::InvalidInput);
|
|
1314
|
+
}
|
|
1315
|
+
|
|
1316
|
+
const SdfGridDesc desc = unpackGridDesc(gridDescData);
|
|
1317
|
+
const SdfTransform pose = unpackTransform(primitivePose);
|
|
1318
|
+
const std::uint32_t total = desc.totalSamples();
|
|
1319
|
+
|
|
1320
|
+
return static_cast<std::int32_t>(cookPrimitiveGrid(
|
|
1321
|
+
desc,
|
|
1322
|
+
static_cast<SdfPrimitiveType>(primitiveType),
|
|
1323
|
+
primitiveParams,
|
|
1324
|
+
pose,
|
|
1325
|
+
materialId,
|
|
1326
|
+
std::span<double>(outDistances, total),
|
|
1327
|
+
std::span<std::uint32_t>(outMaterialIds, total)));
|
|
1328
|
+
}
|
|
1329
|
+
|
|
1330
|
+
std::int32_t rundot_kinetix_ext_sdf_cook_csg(
|
|
1331
|
+
const double* gridDescData,
|
|
1332
|
+
double* inOutDistancesA,
|
|
1333
|
+
std::uint32_t* inOutMaterialIdsA,
|
|
1334
|
+
const double* inDistancesB,
|
|
1335
|
+
const std::uint32_t* inMaterialIdsB,
|
|
1336
|
+
std::uint32_t csgOp,
|
|
1337
|
+
double blendRadius) {
|
|
1338
|
+
if (gridDescData == nullptr || inOutDistancesA == nullptr || inOutMaterialIdsA == nullptr ||
|
|
1339
|
+
inDistancesB == nullptr || inMaterialIdsB == nullptr) {
|
|
1340
|
+
return static_cast<std::int32_t>(SdfStatus::InvalidInput);
|
|
1341
|
+
}
|
|
1342
|
+
|
|
1343
|
+
const SdfGridDesc desc = unpackGridDesc(gridDescData);
|
|
1344
|
+
const std::uint32_t total = desc.totalSamples();
|
|
1345
|
+
|
|
1346
|
+
return static_cast<std::int32_t>(cookCsgGrid(
|
|
1347
|
+
desc,
|
|
1348
|
+
std::span<double>(inOutDistancesA, total),
|
|
1349
|
+
std::span<std::uint32_t>(inOutMaterialIdsA, total),
|
|
1350
|
+
std::span<const double>(inDistancesB, total),
|
|
1351
|
+
std::span<const std::uint32_t>(inMaterialIdsB, total),
|
|
1352
|
+
static_cast<SdfCsgOp>(csgOp),
|
|
1353
|
+
blendRadius));
|
|
1354
|
+
}
|
|
1355
|
+
|
|
1356
|
+
std::int32_t rundot_kinetix_ext_sdf_compute_conservative_bounds(
|
|
1357
|
+
const double* gridDescData,
|
|
1358
|
+
const double* distances,
|
|
1359
|
+
double isoLevel,
|
|
1360
|
+
double margin,
|
|
1361
|
+
const double* poseData,
|
|
1362
|
+
double* outLocalAabb,
|
|
1363
|
+
double* outWorldAabb,
|
|
1364
|
+
double* outBoundingSphere) {
|
|
1365
|
+
if (gridDescData == nullptr || distances == nullptr ||
|
|
1366
|
+
outLocalAabb == nullptr || outWorldAabb == nullptr || outBoundingSphere == nullptr) {
|
|
1367
|
+
return static_cast<std::int32_t>(SdfStatus::InvalidInput);
|
|
1368
|
+
}
|
|
1369
|
+
|
|
1370
|
+
const SdfGridDesc desc = unpackGridDesc(gridDescData);
|
|
1371
|
+
const SdfTransform pose = unpackTransform(poseData);
|
|
1372
|
+
const std::uint32_t total = desc.totalSamples();
|
|
1373
|
+
|
|
1374
|
+
SdfAABB localAabb, worldAabb;
|
|
1375
|
+
SdfBoundingSphere sphere;
|
|
1376
|
+
|
|
1377
|
+
const SdfStatus stat = computeConservativeBounds(
|
|
1378
|
+
desc,
|
|
1379
|
+
std::span<const double>(distances, total),
|
|
1380
|
+
isoLevel,
|
|
1381
|
+
margin,
|
|
1382
|
+
pose,
|
|
1383
|
+
localAabb,
|
|
1384
|
+
worldAabb,
|
|
1385
|
+
sphere);
|
|
1386
|
+
|
|
1387
|
+
if (stat != SdfStatus::Success) return static_cast<std::int32_t>(stat);
|
|
1388
|
+
|
|
1389
|
+
outLocalAabb[0] = localAabb.min.x;
|
|
1390
|
+
outLocalAabb[1] = localAabb.min.y;
|
|
1391
|
+
outLocalAabb[2] = localAabb.min.z;
|
|
1392
|
+
outLocalAabb[3] = localAabb.max.x;
|
|
1393
|
+
outLocalAabb[4] = localAabb.max.y;
|
|
1394
|
+
outLocalAabb[5] = localAabb.max.z;
|
|
1395
|
+
|
|
1396
|
+
outWorldAabb[0] = worldAabb.min.x;
|
|
1397
|
+
outWorldAabb[1] = worldAabb.min.y;
|
|
1398
|
+
outWorldAabb[2] = worldAabb.min.z;
|
|
1399
|
+
outWorldAabb[3] = worldAabb.max.x;
|
|
1400
|
+
outWorldAabb[4] = worldAabb.max.y;
|
|
1401
|
+
outWorldAabb[5] = worldAabb.max.z;
|
|
1402
|
+
|
|
1403
|
+
outBoundingSphere[0] = sphere.center.x;
|
|
1404
|
+
outBoundingSphere[1] = sphere.center.y;
|
|
1405
|
+
outBoundingSphere[2] = sphere.center.z;
|
|
1406
|
+
outBoundingSphere[3] = sphere.radius;
|
|
1407
|
+
|
|
1408
|
+
return 0;
|
|
1409
|
+
}
|
|
1410
|
+
|
|
1411
|
+
std::int32_t rundot_kinetix_ext_sdf_sample(
|
|
1412
|
+
const double* gridDescData,
|
|
1413
|
+
const double* distances,
|
|
1414
|
+
const std::uint32_t* materialIds,
|
|
1415
|
+
const double* poseData,
|
|
1416
|
+
const double* queryPoint,
|
|
1417
|
+
double* outSampleResult) {
|
|
1418
|
+
if (gridDescData == nullptr || distances == nullptr || queryPoint == nullptr || outSampleResult == nullptr) {
|
|
1419
|
+
return static_cast<std::int32_t>(SdfStatus::InvalidInput);
|
|
1420
|
+
}
|
|
1421
|
+
|
|
1422
|
+
const SdfGridDesc desc = unpackGridDesc(gridDescData);
|
|
1423
|
+
const SdfTransform pose = unpackTransform(poseData);
|
|
1424
|
+
const std::uint32_t total = desc.totalSamples();
|
|
1425
|
+
|
|
1426
|
+
const SdfVec3 ptWorld{queryPoint[0], queryPoint[1], queryPoint[2]};
|
|
1427
|
+
SdfSampleResult result;
|
|
1428
|
+
|
|
1429
|
+
const SdfStatus stat = sampleSdfGrid(
|
|
1430
|
+
desc,
|
|
1431
|
+
std::span<const double>(distances, total),
|
|
1432
|
+
materialIds ? std::span<const std::uint32_t>(materialIds, total) : std::span<const std::uint32_t>(),
|
|
1433
|
+
pose,
|
|
1434
|
+
ptWorld,
|
|
1435
|
+
result);
|
|
1436
|
+
|
|
1437
|
+
if (stat != SdfStatus::Success) return static_cast<std::int32_t>(stat);
|
|
1438
|
+
|
|
1439
|
+
outSampleResult[0] = result.distance;
|
|
1440
|
+
outSampleResult[1] = result.gradient.x;
|
|
1441
|
+
outSampleResult[2] = result.gradient.y;
|
|
1442
|
+
outSampleResult[3] = result.gradient.z;
|
|
1443
|
+
outSampleResult[4] = static_cast<double>(result.materialId);
|
|
1444
|
+
outSampleResult[5] = result.isInside ? 1.0 : 0.0;
|
|
1445
|
+
outSampleResult[6] = result.surfacePoint.x;
|
|
1446
|
+
outSampleResult[7] = result.surfacePoint.y;
|
|
1447
|
+
outSampleResult[8] = result.surfacePoint.z;
|
|
1448
|
+
outSampleResult[9] = 0.0;
|
|
1449
|
+
|
|
1450
|
+
return 0;
|
|
1451
|
+
}
|
|
1452
|
+
|
|
1453
|
+
std::int32_t rundot_kinetix_ext_sdf_raycast(
|
|
1454
|
+
const double* gridDescData,
|
|
1455
|
+
const double* distances,
|
|
1456
|
+
const std::uint32_t* materialIds,
|
|
1457
|
+
const double* poseData,
|
|
1458
|
+
const double* rayData,
|
|
1459
|
+
std::uint32_t maxSteps,
|
|
1460
|
+
double hitTolerance,
|
|
1461
|
+
double* outHit) {
|
|
1462
|
+
if (gridDescData == nullptr || distances == nullptr || rayData == nullptr || outHit == nullptr) {
|
|
1463
|
+
return static_cast<std::int32_t>(SdfStatus::InvalidInput);
|
|
1464
|
+
}
|
|
1465
|
+
|
|
1466
|
+
const SdfGridDesc desc = unpackGridDesc(gridDescData);
|
|
1467
|
+
const SdfTransform pose = unpackTransform(poseData);
|
|
1468
|
+
const std::uint32_t total = desc.totalSamples();
|
|
1469
|
+
|
|
1470
|
+
const SdfRay ray{
|
|
1471
|
+
{rayData[0], rayData[1], rayData[2]},
|
|
1472
|
+
{rayData[3], rayData[4], rayData[5]},
|
|
1473
|
+
rayData[6] > 0.0 ? rayData[6] : 1e30
|
|
1474
|
+
};
|
|
1475
|
+
|
|
1476
|
+
SdfRayHit hitResult;
|
|
1477
|
+
const SdfStatus stat = raycastSdfGrid(
|
|
1478
|
+
desc,
|
|
1479
|
+
std::span<const double>(distances, total),
|
|
1480
|
+
materialIds ? std::span<const std::uint32_t>(materialIds, total) : std::span<const std::uint32_t>(),
|
|
1481
|
+
pose,
|
|
1482
|
+
ray,
|
|
1483
|
+
maxSteps,
|
|
1484
|
+
hitTolerance,
|
|
1485
|
+
hitResult);
|
|
1486
|
+
|
|
1487
|
+
outHit[0] = hitResult.hit ? 1.0 : 0.0;
|
|
1488
|
+
outHit[1] = hitResult.distance;
|
|
1489
|
+
outHit[2] = hitResult.point.x;
|
|
1490
|
+
outHit[3] = hitResult.point.y;
|
|
1491
|
+
outHit[4] = hitResult.point.z;
|
|
1492
|
+
outHit[5] = hitResult.normal.x;
|
|
1493
|
+
outHit[6] = hitResult.normal.y;
|
|
1494
|
+
outHit[7] = hitResult.normal.z;
|
|
1495
|
+
outHit[8] = static_cast<double>(hitResult.materialId);
|
|
1496
|
+
outHit[9] = static_cast<double>(hitResult.stepsTaken);
|
|
1497
|
+
|
|
1498
|
+
return (stat == SdfStatus::Success || stat == SdfStatus::NoHit) ? 0 : static_cast<std::int32_t>(stat);
|
|
1499
|
+
}
|
|
1500
|
+
|
|
1501
|
+
std::int32_t rundot_kinetix_ext_sdf_collide_sphere(
|
|
1502
|
+
const double* gridDescData,
|
|
1503
|
+
const double* distances,
|
|
1504
|
+
const std::uint32_t* materialIds,
|
|
1505
|
+
const double* poseA,
|
|
1506
|
+
const double* spherePosB,
|
|
1507
|
+
double sphereRadiusB,
|
|
1508
|
+
std::uint32_t sphereMaterialIdB,
|
|
1509
|
+
double* outCollision) {
|
|
1510
|
+
if (gridDescData == nullptr || distances == nullptr || spherePosB == nullptr || outCollision == nullptr) {
|
|
1511
|
+
return static_cast<std::int32_t>(SdfStatus::InvalidInput);
|
|
1512
|
+
}
|
|
1513
|
+
|
|
1514
|
+
const SdfGridDesc desc = unpackGridDesc(gridDescData);
|
|
1515
|
+
const SdfTransform pose = unpackTransform(poseA);
|
|
1516
|
+
const std::uint32_t total = desc.totalSamples();
|
|
1517
|
+
|
|
1518
|
+
const SdfVec3 centerB{spherePosB[0], spherePosB[1], spherePosB[2]};
|
|
1519
|
+
SdfCollisionResult col;
|
|
1520
|
+
|
|
1521
|
+
const SdfStatus stat = collideSdfWithSphere(
|
|
1522
|
+
desc,
|
|
1523
|
+
std::span<const double>(distances, total),
|
|
1524
|
+
materialIds ? std::span<const std::uint32_t>(materialIds, total) : std::span<const std::uint32_t>(),
|
|
1525
|
+
pose,
|
|
1526
|
+
centerB,
|
|
1527
|
+
sphereRadiusB,
|
|
1528
|
+
sphereMaterialIdB,
|
|
1529
|
+
col);
|
|
1530
|
+
|
|
1531
|
+
outCollision[0] = col.hasCollision ? 1.0 : 0.0;
|
|
1532
|
+
outCollision[1] = col.normal.x;
|
|
1533
|
+
outCollision[2] = col.normal.y;
|
|
1534
|
+
outCollision[3] = col.normal.z;
|
|
1535
|
+
outCollision[4] = col.penetration;
|
|
1536
|
+
outCollision[5] = col.contactPoint.x;
|
|
1537
|
+
outCollision[6] = col.contactPoint.y;
|
|
1538
|
+
outCollision[7] = col.contactPoint.z;
|
|
1539
|
+
outCollision[8] = col.witnessA.x;
|
|
1540
|
+
outCollision[9] = col.witnessA.y;
|
|
1541
|
+
outCollision[10] = col.witnessA.z;
|
|
1542
|
+
outCollision[11] = col.witnessB.x;
|
|
1543
|
+
outCollision[12] = col.witnessB.y;
|
|
1544
|
+
outCollision[13] = col.witnessB.z;
|
|
1545
|
+
|
|
1546
|
+
return (stat == SdfStatus::Success || stat == SdfStatus::Disjoint) ? 0 : static_cast<std::int32_t>(stat);
|
|
1547
|
+
}
|
|
1548
|
+
|
|
1549
|
+
std::int32_t rundot_kinetix_ext_sdf_collide_capsule(
|
|
1550
|
+
const double* gridDescData,
|
|
1551
|
+
const double* distances,
|
|
1552
|
+
const std::uint32_t* materialIds,
|
|
1553
|
+
const double* poseA,
|
|
1554
|
+
const double* capsuleSegA,
|
|
1555
|
+
const double* capsuleSegB,
|
|
1556
|
+
double capsuleRadius,
|
|
1557
|
+
std::uint32_t capsuleMaterialId,
|
|
1558
|
+
double* outCollision) {
|
|
1559
|
+
if (gridDescData == nullptr || distances == nullptr ||
|
|
1560
|
+
capsuleSegA == nullptr || capsuleSegB == nullptr || outCollision == nullptr) {
|
|
1561
|
+
return static_cast<std::int32_t>(SdfStatus::InvalidInput);
|
|
1562
|
+
}
|
|
1563
|
+
|
|
1564
|
+
const SdfGridDesc desc = unpackGridDesc(gridDescData);
|
|
1565
|
+
const SdfTransform pose = unpackTransform(poseA);
|
|
1566
|
+
const std::uint32_t total = desc.totalSamples();
|
|
1567
|
+
|
|
1568
|
+
const SdfVec3 segA{capsuleSegA[0], capsuleSegA[1], capsuleSegA[2]};
|
|
1569
|
+
const SdfVec3 segB{capsuleSegB[0], capsuleSegB[1], capsuleSegB[2]};
|
|
1570
|
+
|
|
1571
|
+
SdfCollisionResult col;
|
|
1572
|
+
const SdfStatus stat = collideSdfWithCapsule(
|
|
1573
|
+
desc,
|
|
1574
|
+
std::span<const double>(distances, total),
|
|
1575
|
+
materialIds ? std::span<const std::uint32_t>(materialIds, total) : std::span<const std::uint32_t>(),
|
|
1576
|
+
pose,
|
|
1577
|
+
segA,
|
|
1578
|
+
segB,
|
|
1579
|
+
capsuleRadius,
|
|
1580
|
+
capsuleMaterialId,
|
|
1581
|
+
col);
|
|
1582
|
+
|
|
1583
|
+
outCollision[0] = col.hasCollision ? 1.0 : 0.0;
|
|
1584
|
+
outCollision[1] = col.normal.x;
|
|
1585
|
+
outCollision[2] = col.normal.y;
|
|
1586
|
+
outCollision[3] = col.normal.z;
|
|
1587
|
+
outCollision[4] = col.penetration;
|
|
1588
|
+
outCollision[5] = col.contactPoint.x;
|
|
1589
|
+
outCollision[6] = col.contactPoint.y;
|
|
1590
|
+
outCollision[7] = col.contactPoint.z;
|
|
1591
|
+
outCollision[8] = col.witnessA.x;
|
|
1592
|
+
outCollision[9] = col.witnessA.y;
|
|
1593
|
+
outCollision[10] = col.witnessA.z;
|
|
1594
|
+
outCollision[11] = col.witnessB.x;
|
|
1595
|
+
outCollision[12] = col.witnessB.y;
|
|
1596
|
+
outCollision[13] = col.witnessB.z;
|
|
1597
|
+
|
|
1598
|
+
return (stat == SdfStatus::Success || stat == SdfStatus::Disjoint) ? 0 : static_cast<std::int32_t>(stat);
|
|
1599
|
+
}
|
|
1600
|
+
|
|
1601
|
+
std::int32_t rundot_kinetix_ext_sdf_collide_box(
|
|
1602
|
+
const double* gridDescData,
|
|
1603
|
+
const double* distances,
|
|
1604
|
+
const std::uint32_t* materialIds,
|
|
1605
|
+
const double* poseA,
|
|
1606
|
+
const double* poseB,
|
|
1607
|
+
const double* boxHalfExtentsB,
|
|
1608
|
+
std::uint32_t boxMaterialIdB,
|
|
1609
|
+
double* outCollision) {
|
|
1610
|
+
if (gridDescData == nullptr || distances == nullptr ||
|
|
1611
|
+
poseB == nullptr || boxHalfExtentsB == nullptr || outCollision == nullptr) {
|
|
1612
|
+
return static_cast<std::int32_t>(SdfStatus::InvalidInput);
|
|
1613
|
+
}
|
|
1614
|
+
|
|
1615
|
+
const SdfGridDesc desc = unpackGridDesc(gridDescData);
|
|
1616
|
+
const SdfTransform pA = unpackTransform(poseA);
|
|
1617
|
+
const SdfTransform pB = unpackTransform(poseB);
|
|
1618
|
+
const SdfVec3 halfExtentsB{boxHalfExtentsB[0], boxHalfExtentsB[1], boxHalfExtentsB[2]};
|
|
1619
|
+
const std::uint32_t total = desc.totalSamples();
|
|
1620
|
+
|
|
1621
|
+
SdfCollisionResult col;
|
|
1622
|
+
const SdfStatus stat = collideSdfWithBox(
|
|
1623
|
+
desc,
|
|
1624
|
+
std::span<const double>(distances, total),
|
|
1625
|
+
materialIds ? std::span<const std::uint32_t>(materialIds, total) : std::span<const std::uint32_t>(),
|
|
1626
|
+
pA,
|
|
1627
|
+
pB,
|
|
1628
|
+
halfExtentsB,
|
|
1629
|
+
boxMaterialIdB,
|
|
1630
|
+
col);
|
|
1631
|
+
|
|
1632
|
+
outCollision[0] = col.hasCollision ? 1.0 : 0.0;
|
|
1633
|
+
outCollision[1] = col.normal.x;
|
|
1634
|
+
outCollision[2] = col.normal.y;
|
|
1635
|
+
outCollision[3] = col.normal.z;
|
|
1636
|
+
outCollision[4] = col.penetration;
|
|
1637
|
+
outCollision[5] = col.contactPoint.x;
|
|
1638
|
+
outCollision[6] = col.contactPoint.y;
|
|
1639
|
+
outCollision[7] = col.contactPoint.z;
|
|
1640
|
+
outCollision[8] = col.witnessA.x;
|
|
1641
|
+
outCollision[9] = col.witnessA.y;
|
|
1642
|
+
outCollision[10] = col.witnessA.z;
|
|
1643
|
+
outCollision[11] = col.witnessB.x;
|
|
1644
|
+
outCollision[12] = col.witnessB.y;
|
|
1645
|
+
outCollision[13] = col.witnessB.z;
|
|
1646
|
+
|
|
1647
|
+
return (stat == SdfStatus::Success || stat == SdfStatus::Disjoint) ? 0 : static_cast<std::int32_t>(stat);
|
|
1648
|
+
}
|
|
1649
|
+
|
|
1650
|
+
std::int32_t rundot_kinetix_ext_sdf_collide_particles(
|
|
1651
|
+
const double* gridDescData,
|
|
1652
|
+
const double* distances,
|
|
1653
|
+
const std::uint32_t* materialIds,
|
|
1654
|
+
const double* poseA,
|
|
1655
|
+
const double* particlePositions,
|
|
1656
|
+
std::uint32_t particleCount,
|
|
1657
|
+
std::uint32_t particleStride,
|
|
1658
|
+
double particleRadius,
|
|
1659
|
+
std::uint32_t particleMaterialId,
|
|
1660
|
+
double* outParticleCollisions,
|
|
1661
|
+
std::uint32_t* outHitCount) {
|
|
1662
|
+
if (gridDescData == nullptr || distances == nullptr) {
|
|
1663
|
+
return static_cast<std::int32_t>(SdfStatus::InvalidInput);
|
|
1664
|
+
}
|
|
1665
|
+
|
|
1666
|
+
const SdfGridDesc desc = unpackGridDesc(gridDescData);
|
|
1667
|
+
const SdfTransform pose = unpackTransform(poseA);
|
|
1668
|
+
const std::uint32_t total = desc.totalSamples();
|
|
1669
|
+
|
|
1670
|
+
return static_cast<std::int32_t>(collideSdfWithParticles(
|
|
1671
|
+
desc,
|
|
1672
|
+
std::span<const double>(distances, total),
|
|
1673
|
+
materialIds ? std::span<const std::uint32_t>(materialIds, total) : std::span<const std::uint32_t>(),
|
|
1674
|
+
pose,
|
|
1675
|
+
particlePositions,
|
|
1676
|
+
particleCount,
|
|
1677
|
+
particleStride,
|
|
1678
|
+
particleRadius,
|
|
1679
|
+
particleMaterialId,
|
|
1680
|
+
outParticleCollisions,
|
|
1681
|
+
outHitCount));
|
|
1682
|
+
}
|
|
1683
|
+
|
|
1684
|
+
static CustomGeometryDesc unpackCustomGeom(const double* d) {
|
|
1685
|
+
CustomGeometryDesc desc;
|
|
1686
|
+
desc.customType = static_cast<CustomGeometryType>(static_cast<std::uint32_t>(d[0]));
|
|
1687
|
+
desc.customId = static_cast<std::uint32_t>(d[1]);
|
|
1688
|
+
desc.materialId = static_cast<std::uint32_t>(d[2]);
|
|
1689
|
+
desc.flags = static_cast<std::uint32_t>(d[3]);
|
|
1690
|
+
for (std::size_t i = 0; i < 8; ++i) {
|
|
1691
|
+
desc.params[i] = d[4 + i];
|
|
1692
|
+
}
|
|
1693
|
+
desc.localBounds.min = {d[12], d[13], d[14]};
|
|
1694
|
+
desc.localBounds.max = {d[15], d[16], d[17]};
|
|
1695
|
+
return desc;
|
|
1696
|
+
}
|
|
1697
|
+
|
|
1698
|
+
std::int32_t rundot_kinetix_ext_custom_geometry_bounds(
|
|
1699
|
+
const double* customGeomData,
|
|
1700
|
+
const double* poseData,
|
|
1701
|
+
double margin,
|
|
1702
|
+
double* outLocalAabb,
|
|
1703
|
+
double* outWorldAabb,
|
|
1704
|
+
double* outBoundingSphere) {
|
|
1705
|
+
if (customGeomData == nullptr || outLocalAabb == nullptr ||
|
|
1706
|
+
outWorldAabb == nullptr || outBoundingSphere == nullptr) {
|
|
1707
|
+
return static_cast<std::int32_t>(SdfStatus::InvalidInput);
|
|
1708
|
+
}
|
|
1709
|
+
|
|
1710
|
+
const CustomGeometryDesc desc = unpackCustomGeom(customGeomData);
|
|
1711
|
+
const SdfTransform pose = unpackTransform(poseData);
|
|
1712
|
+
|
|
1713
|
+
SdfAABB localAabb, worldAabb;
|
|
1714
|
+
SdfBoundingSphere sphere;
|
|
1715
|
+
|
|
1716
|
+
const SdfStatus stat = computeCustomGeometryBounds(
|
|
1717
|
+
desc,
|
|
1718
|
+
pose,
|
|
1719
|
+
margin,
|
|
1720
|
+
localAabb,
|
|
1721
|
+
worldAabb,
|
|
1722
|
+
sphere);
|
|
1723
|
+
|
|
1724
|
+
if (stat != SdfStatus::Success) return static_cast<std::int32_t>(stat);
|
|
1725
|
+
|
|
1726
|
+
outLocalAabb[0] = localAabb.min.x;
|
|
1727
|
+
outLocalAabb[1] = localAabb.min.y;
|
|
1728
|
+
outLocalAabb[2] = localAabb.min.z;
|
|
1729
|
+
outLocalAabb[3] = localAabb.max.x;
|
|
1730
|
+
outLocalAabb[4] = localAabb.max.y;
|
|
1731
|
+
outLocalAabb[5] = localAabb.max.z;
|
|
1732
|
+
|
|
1733
|
+
outWorldAabb[0] = worldAabb.min.x;
|
|
1734
|
+
outWorldAabb[1] = worldAabb.min.y;
|
|
1735
|
+
outWorldAabb[2] = worldAabb.min.z;
|
|
1736
|
+
outWorldAabb[3] = worldAabb.max.x;
|
|
1737
|
+
outWorldAabb[4] = worldAabb.max.y;
|
|
1738
|
+
outWorldAabb[5] = worldAabb.max.z;
|
|
1739
|
+
|
|
1740
|
+
outBoundingSphere[0] = sphere.center.x;
|
|
1741
|
+
outBoundingSphere[1] = sphere.center.y;
|
|
1742
|
+
outBoundingSphere[2] = sphere.center.z;
|
|
1743
|
+
outBoundingSphere[3] = sphere.radius;
|
|
1744
|
+
|
|
1745
|
+
return 0;
|
|
1746
|
+
}
|
|
1747
|
+
|
|
1748
|
+
std::int32_t rundot_kinetix_ext_custom_geometry_evaluate(
|
|
1749
|
+
const double* customGeomData,
|
|
1750
|
+
const double* poseData,
|
|
1751
|
+
const double* queryPoint,
|
|
1752
|
+
double* outSampleResult) {
|
|
1753
|
+
if (customGeomData == nullptr || queryPoint == nullptr || outSampleResult == nullptr) {
|
|
1754
|
+
return static_cast<std::int32_t>(SdfStatus::InvalidInput);
|
|
1755
|
+
}
|
|
1756
|
+
|
|
1757
|
+
const CustomGeometryDesc desc = unpackCustomGeom(customGeomData);
|
|
1758
|
+
const SdfTransform pose = unpackTransform(poseData);
|
|
1759
|
+
const SdfVec3 ptWorld{queryPoint[0], queryPoint[1], queryPoint[2]};
|
|
1760
|
+
|
|
1761
|
+
SdfSampleResult result;
|
|
1762
|
+
const SdfStatus stat = evaluateCustomGeometry(desc, pose, ptWorld, result);
|
|
1763
|
+
if (stat != SdfStatus::Success) return static_cast<std::int32_t>(stat);
|
|
1764
|
+
|
|
1765
|
+
outSampleResult[0] = result.distance;
|
|
1766
|
+
outSampleResult[1] = result.gradient.x;
|
|
1767
|
+
outSampleResult[2] = result.gradient.y;
|
|
1768
|
+
outSampleResult[3] = result.gradient.z;
|
|
1769
|
+
outSampleResult[4] = static_cast<double>(result.materialId);
|
|
1770
|
+
outSampleResult[5] = result.isInside ? 1.0 : 0.0;
|
|
1771
|
+
outSampleResult[6] = result.surfacePoint.x;
|
|
1772
|
+
outSampleResult[7] = result.surfacePoint.y;
|
|
1773
|
+
outSampleResult[8] = result.surfacePoint.z;
|
|
1774
|
+
outSampleResult[9] = 0.0;
|
|
1775
|
+
|
|
1776
|
+
return 0;
|
|
1777
|
+
}
|
|
1778
|
+
|
|
1779
|
+
std::int32_t rundot_kinetix_ext_custom_geometry_raycast(
|
|
1780
|
+
const double* customGeomData,
|
|
1781
|
+
const double* poseData,
|
|
1782
|
+
const double* rayData,
|
|
1783
|
+
std::uint32_t maxSteps,
|
|
1784
|
+
double hitTolerance,
|
|
1785
|
+
double* outHit) {
|
|
1786
|
+
if (customGeomData == nullptr || rayData == nullptr || outHit == nullptr) {
|
|
1787
|
+
return static_cast<std::int32_t>(SdfStatus::InvalidInput);
|
|
1788
|
+
}
|
|
1789
|
+
|
|
1790
|
+
const CustomGeometryDesc desc = unpackCustomGeom(customGeomData);
|
|
1791
|
+
const SdfTransform pose = unpackTransform(poseData);
|
|
1792
|
+
const SdfRay ray{
|
|
1793
|
+
{rayData[0], rayData[1], rayData[2]},
|
|
1794
|
+
{rayData[3], rayData[4], rayData[5]},
|
|
1795
|
+
rayData[6] > 0.0 ? rayData[6] : 1e30
|
|
1796
|
+
};
|
|
1797
|
+
|
|
1798
|
+
SdfRayHit hitResult;
|
|
1799
|
+
const SdfStatus stat = raycastCustomGeometry(
|
|
1800
|
+
desc,
|
|
1801
|
+
pose,
|
|
1802
|
+
ray,
|
|
1803
|
+
maxSteps,
|
|
1804
|
+
hitTolerance,
|
|
1805
|
+
hitResult);
|
|
1806
|
+
|
|
1807
|
+
outHit[0] = hitResult.hit ? 1.0 : 0.0;
|
|
1808
|
+
outHit[1] = hitResult.distance;
|
|
1809
|
+
outHit[2] = hitResult.point.x;
|
|
1810
|
+
outHit[3] = hitResult.point.y;
|
|
1811
|
+
outHit[4] = hitResult.point.z;
|
|
1812
|
+
outHit[5] = hitResult.normal.x;
|
|
1813
|
+
outHit[6] = hitResult.normal.y;
|
|
1814
|
+
outHit[7] = hitResult.normal.z;
|
|
1815
|
+
outHit[8] = static_cast<double>(hitResult.materialId);
|
|
1816
|
+
outHit[9] = static_cast<double>(hitResult.stepsTaken);
|
|
1817
|
+
|
|
1818
|
+
return (stat == SdfStatus::Success || stat == SdfStatus::NoHit) ? 0 : static_cast<std::int32_t>(stat);
|
|
1819
|
+
}
|
|
1820
|
+
|
|
1821
|
+
} // extern "C"
|