Skip to content

Add helper utilities for raw surface-to-surface distance computations ( geom_distance_utils.py ) #2689

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

sriramsowmithri9807
Copy link

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.

… (geom_distance_utils.py)

MuJoCo’s mj_geomDistance returns a signed distance with geom margins already subtracted, which often confuses users.\n\nThis commit adds python/geom_distance_utils.py providing:\n * sdf(): thin wrapper that allocates the fromto buffer for mj_geomDistance.\n * raw_gap(): returns the raw surface-to-surface gap by adding the two geom margins back.\n * batch_raw_gap(): vectorised helper for many geom pairs that avoids per-call allocations.\n\nIncludes CLI example usage when run as a script. No existing code is modified.
Copy link

google-cla bot commented Jun 18, 2025

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant