Add helper utilities for raw surface-to-surface distance computations ( geom_distance_utils.py ) #2689
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation
MuJoCo’s mj_geomDistance function returns a signed distance with each geom's geom_margin already subtracted, which can result in negative values even when objects are not in contact. This behavior frequently confuses users and requires boilerplate code to handle correctly. Additionally, the function incurs per-call buffer allocation overhead, which can be costly in performance-critical applications.
Key Additions
New module:
python/geom_distance_utils.py
sdf(model, data, gid1, gid2, ...)
– A thin wrapper around mj_geomDistance that returns the raw signed distance while managing buffer allocation internally.
raw_gap(model, data, gid1, gid2, ...)
– Returns the actual surface-to-surface gap by re-adding both geoms' margins to the signed distance.
batch_raw_gap(model, data, gid_pairs, ...)
– A vectorized, memory-efficient method for computing multiple raw gaps, ideal for performance-critical loops.
Self-contained CLI Example
– Run python
geom_distance_utils.py <model.xml>
to see the utility in action.Documentation & Comments
– Includes clear docstrings and inline comments to explain MuJoCo's margin behavior and how this module resolves common pitfalls.
Benefits
Removes ambiguity around MuJoCo’s margin-subtracted signed distances.
Reduces overhead by reusing buffers and avoiding repeated allocations.
Simplifies research scripts by avoiding manual margin bookkeeping.
Completely additive: no changes to existing files or build systems required.