Bevy-Rust-GPU / rust-gpu-sdf

Signed distance field library usable on both CPU and GPU.
Apache License 2.0
12 stars 1 forks source link

Type-level bound / field differentiation #12

Open Shfty opened 1 year ago

Shfty commented 1 year ago

SignedDistanceField is incorrectly-named as-is, since it will accept arbitrary distance functions which may not be valid signed distance fields.

It's desirable to be able to use non-field functions, but they come with various caveats (increased raymarching cost, being unsuitable for collision detection) that justify encoding a distiction.

Currently, the difference is encoded via cargo tests; each test asserts that a shape is a field via BoundChecker, with bounds being marked as should_panic so tests will pass with a note and panic message. This is sufficient for informational purposes (even with BoundChecker's current non-comprehensive implementation), but is too expensive for runtime checks, and provides nothing in terms of type-level machinery.

Thus, SignedDistanceField should be renamed to DistanceFunction or DistanceFn, and extension traits for inner / outer field validity should be implemented. This will demonstrate boundedness more explicitly in code, and allow type-level checking for cases like passing a non-field-interior SDF into a collision support function.

Need to research the semantics around unsafe traits - these would be simple marker traits (or blanket impls with a const bool, need to weigh the pros and cons) and thus not perform any actual unsafe rust, but would result in logical unsoundness if implemented on a non-field type. Simply being markers may be sufficient.