Skip to content

MJCF Importer Incorrectly Converts Cylinder Colliders to Capsules #333#771

Merged
eric-heiden merged 2 commits into
newton-physics:mainfrom
vreutskyy:333-mjcf-importer-incorrectly-converts-cylinder-colliders-to-capsules
Sep 16, 2025
Merged

MJCF Importer Incorrectly Converts Cylinder Colliders to Capsules #333#771
eric-heiden merged 2 commits into
newton-physics:mainfrom
vreutskyy:333-mjcf-importer-incorrectly-converts-cylinder-colliders-to-capsules

Conversation

@vreutskyy

@vreutskyy vreutskyy commented Sep 16, 2025

Copy link
Copy Markdown
Member

Description

This PR fixes the MJCF and URDF importers to properly preserve cylinder collision shapes instead of incorrectly converting them to capsules. Closes #333

Changes Made:

  • MJCF Importer: Changed add_shape_capsule to add_shape_cylinder for cylinder geometries in newton/_src/utils/import_mjcf.py
  • URDF Importer: Changed add_shape_capsule to add_shape_cylinder for cylinder geometries in newton/_src/utils/import_urdf.py
  • USD Importer: No changes needed - already correctly handles cylinders
  • Tests: Added unit tests to verify cylinder shapes are properly imported and their properties (radius, height) are preserved

Newton Migration Guide

Please ensure the migration guide for warp.sim users is up-to-date with the changes made in this PR.

  • The migration guide in docs/migration.rst is up-to date
    • Note: No migration guide updates needed for this bug fix

Before your PR is "Ready for review"

  • Necessary tests have been added and new examples are tested (see newton/tests/test_examples.py)
    • Added test_cylinder_shapes_preserved and test_cylinder_properties_preserved tests for MJCF importer
    • Added test_cylinder_shapes_preserved test for URDF importer
  • Documentation is up-to-date
  • Code passes formatting and linting checks with pre-commit run -a

Summary by CodeRabbit

  • Bug Fixes

    • Cylinder geometries are now correctly imported as cylinders (not capsules) from both MJCF and URDF files, preserving dimensions and orientation for visual and collision elements. This improves geometric fidelity during import.
  • Tests

    • Added tests for MJCF and URDF imports to verify cylinder shape preservation and correct mapping of radius and half-height properties.

@coderabbitai

coderabbitai Bot commented Sep 16, 2025

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

Imports for cylinder geometries in MJCF and URDF now call builder.add_shape_cylinder instead of builder.add_shape_capsule. Capsule handling is unchanged. Tests were added to verify cylinder shape type and dimensions during import. MJCF tests include duplicate cylinder tests.

Changes

Cohort / File(s) Summary
Utils: Cylinder import handling
newton/_src/utils/import_mjcf.py, newton/_src/utils/import_urdf.py
For geom_type "cylinder", replace calls to add_shape_capsule(...) with add_shape_cylinder(...), preserving existing arguments (transform, radius, half_height, axis/up-axis, scale/cfg). Capsule paths unchanged.
Tests: MJCF importer
newton/tests/test_import_mjcf.py
Add tests asserting cylinders import as GeoType.CYLINDER and preserve radius/half_height mapping via shape_scale; note duplicate definitions of the two cylinder tests.
Tests: URDF importer
newton/tests/test_import_urdf.py
Add test ensuring URDF cylinder geometries import as GeoType.CYLINDER (visual and collision) and capsules remain GeoType.CAPSULE; verifies cylinder dimensions via shape_scale.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor User
  participant Importer as MJCF/URDF Importer
  participant Builder as ModelBuilder

  User->>Importer: load(file)
  Importer->>Importer: parse geometry elements
  alt geom_type == "cylinder"
    Importer->>Builder: add_shape_cylinder(xform, radius, half_height, up_axis/axis, ...)
    Builder-->>Importer: shape_id (type=Cylinder)
  else geom_type == "capsule"
    Importer->>Builder: add_shape_capsule(xform, radius, half_height, up_axis/axis, ...)
    Builder-->>Importer: shape_id (type=Capsule)
  end
  Importer-->>User: model with shapes
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Suggested reviewers

  • AntoineRichard
  • eric-heiden
  • adenzler-nvidia

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title succinctly and accurately describes the primary bug: the MJCF importer converting cylinder colliders to capsules and references the linked issue #333; this matches the main intent of the changeset (fixing cylinder handling in importers). It is concise, focused, and clear for a teammate scanning history. Although the PR also updates the URDF importer and tests, the title correctly captures the core reported problem.
Linked Issues Check ✅ Passed The PR implements the linked issue's core fix by replacing add_shape_capsule with add_shape_cylinder for cylinder geometries in import_mjcf.py and import_urdf.py and adds unit tests that assert GeoType.CYLINDER and preserved radius/half_height, which directly addresses issue #333. The PR notes USD required no change and the tests exercise both collision and visual geometry, aligning with the issue requirements. One minor test-quality concern is that the MJCF tests appear duplicated in the diff and should be deduplicated before merging.
Out of Scope Changes Check ✅ Passed All reported code changes are limited to the MJCF and URDF importer modules and their tests, which is consistent with the linked issue scope of preserving cylinder colliders, and no unrelated files or functionality are indicated in the provided summary. The only anomaly is the duplicated MJCF test definitions in the diff, but those are still related to the importer fixes. No out-of-scope behavior or unrelated feature edits are evident from the summary.
✨ Finishing touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Tip

👮 Agentic pre-merge checks are now available in preview!

Pro plan users can now enable pre-merge checks in their settings to enforce checklists before merging PRs.

  • Built-in checks – Quickly apply ready-made checks to enforce title conventions, require pull request descriptions that follow templates, validate linked issues for compliance, and more.
  • Custom agentic checks – Define your own rules using CodeRabbit’s advanced agentic capabilities to enforce organization-specific policies and workflows. For example, you can instruct CodeRabbit’s agent to verify that API documentation is updated whenever API schema files are modified in a PR. Note: Upto 5 custom checks are currently allowed during the preview period. Pricing for this feature will be announced in a few weeks.

Please see the documentation for more information.

Example:

reviews:
  pre_merge_checks:
    custom_checks:
      - name: "Undocumented Breaking Changes"
        mode: "warning"
        instructions: |
          Pass/fail criteria: All breaking changes to public APIs, CLI flags, environment variables, configuration keys, database schemas, or HTTP/GraphQL endpoints must be documented in the "Breaking Change" section of the PR description and in CHANGELOG.md. Exclude purely internal or private changes (e.g., code not exported from package entry points or explicitly marked as internal).

Please share your feedback with us on this Discord post.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (5)
newton/_src/utils/import_mjcf.py (1)

386-405: Edge-case note: fromto collinearity can yield a zero rotation axis

When start→end aligns with the reference axis, cross(...) becomes zero and normalization can NaN the quaternion. Not introduced here, but this block is susceptible. Add a small epsilon guard and pick an orthogonal fallback axis when |cross| ≈ 0.

-                    axis = wp.normalize(wp.cross(axis, wp.vec3(0.0, 1.0, 0.0)))
+                    cross_axis = wp.cross(axis, wp.vec3(0.0, 1.0, 0.0))
+                    if wp.length(cross_axis) < 1e-6:
+                        cross_axis = wp.vec3(0.0, 0.0, 1.0)  # fallback orthogonal
+                    axis = wp.normalize(cross_axis)
newton/tests/test_import_urdf.py (2)

16-16: Prefer public API over private internals in tests

Import GeoType via the public surface to avoid tight coupling to internals.

-from newton._src.geometry.types import GeoType
+from newton import GeoType

251-306: Good coverage; tweak assertions to avoid order-coupling and fix misleading comment

Visuals are added before colliders in parse_urdf, so “First shape … (collision)” is misleading. Also, indexing makes the test brittle across importer reorderings. Count by type instead.

-            # First shape should be cylinder (collision)
-            self.assertEqual(shape_types[0], GeoType.CYLINDER)
-
-            # Second shape should be cylinder (visual)
-            self.assertEqual(shape_types[1], GeoType.CYLINDER)
-
-            # Third shape should be capsule
-            self.assertEqual(shape_types[2], GeoType.CAPSULE)
+            # We expect exactly two cylinders (visual + collision) and one capsule.
+            from collections import Counter
+            counts = Counter(shape_types)
+            self.assertEqual(counts[GeoType.CYLINDER], 2)
+            self.assertEqual(counts[GeoType.CAPSULE], 1)
newton/tests/test_import_mjcf.py (2)

25-25: Prefer public API import for GeoType

Use newton.GeoType to decouple tests from internal module layout.

-from newton._src.geometry.types import GeoType
+from newton import GeoType

570-609: Solid test; simplify and de-ambiguate the fromto case

MJCF ignores size[1] when fromto is present; keeping it in the XML can confuse readers. Also, assert type counts to avoid depending on insertion order.

-            <geom type="cylinder" size="0.3 0.8" fromto="0 0 0 1 0 0" />
+            <geom type="cylinder" size="0.3 0.0" fromto="0 0 0 1 0 0" />
-            # Check shape types
-            shape_types = list(builder.shape_type)
-            # First two shapes should be cylinders
-            self.assertEqual(shape_types[0], GeoType.CYLINDER)
-            self.assertEqual(shape_types[1], GeoType.CYLINDER)
-            # Third shape should be capsule
-            self.assertEqual(shape_types[2], GeoType.CAPSULE)
-            # Fourth shape should be box
-            self.assertEqual(shape_types[3], GeoType.BOX)
+            # Check shape type counts (order-independent)
+            from collections import Counter
+            counts = Counter(builder.shape_type)
+            self.assertEqual(counts[GeoType.CYLINDER], 2)
+            self.assertEqual(counts[GeoType.CAPSULE], 1)
+            self.assertEqual(counts[GeoType.BOX], 1)
📜 Review details

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8b88181 and ce4830f.

📒 Files selected for processing (4)
  • newton/_src/utils/import_mjcf.py (1 hunks)
  • newton/_src/utils/import_urdf.py (1 hunks)
  • newton/tests/test_import_mjcf.py (2 hunks)
  • newton/tests/test_import_urdf.py (3 hunks)
🧰 Additional context used
🧠 Learnings (2)
📓 Common learnings
Learnt from: eric-heiden
PR: newton-physics/newton#584
File: newton/_src/utils/import_urdf.py:159-167
Timestamp: 2025-08-20T03:30:16.037Z
Learning: In the URDF importer (newton/_src/utils/import_urdf.py), cylinders are intentionally created using add_shape_capsule() instead of add_shape_cylinder() because cylinder collisions are not fully supported yet. This is a deliberate workaround until proper cylinder collision support is implemented.
📚 Learning: 2025-08-20T03:30:16.037Z
Learnt from: eric-heiden
PR: newton-physics/newton#584
File: newton/_src/utils/import_urdf.py:159-167
Timestamp: 2025-08-20T03:30:16.037Z
Learning: In the URDF importer (newton/_src/utils/import_urdf.py), cylinders are intentionally created using add_shape_capsule() instead of add_shape_cylinder() because cylinder collisions are not fully supported yet. This is a deliberate workaround until proper cylinder collision support is implemented.

Applied to files:

  • newton/_src/utils/import_urdf.py
  • newton/tests/test_import_urdf.py
  • newton/_src/utils/import_mjcf.py
  • newton/tests/test_import_mjcf.py
🧬 Code graph analysis (4)
newton/_src/utils/import_urdf.py (1)
newton/_src/sim/builder.py (1)
  • add_shape_cylinder (2633-2673)
newton/tests/test_import_urdf.py (1)
newton/_src/geometry/types.py (1)
  • GeoType (25-64)
newton/_src/utils/import_mjcf.py (1)
newton/_src/sim/builder.py (1)
  • add_shape_cylinder (2633-2673)
newton/tests/test_import_mjcf.py (2)
newton/_src/geometry/types.py (1)
  • GeoType (25-64)
newton/_src/sim/builder.py (1)
  • shape_count (508-512)
⏰ Context from checks skipped due to timeout of 900000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (4)
  • GitHub Check: Run GPU Benchmarks (Pull Request)
  • GitHub Check: Run GPU Unit Tests on AWS EC2 (Pull Request)
  • GitHub Check: run-newton-tests / newton-unittests (ubuntu-latest)
  • GitHub Check: run-newton-tests / newton-unittests (windows-latest)
🔇 Additional comments (3)
newton/_src/utils/import_mjcf.py (1)

410-420: LGTM: cylinder geometries now correctly call add_shape_cylinder

Mapping keeps radius/half_height semantics and existing axis-rotation. Tests cover both size= and fromto= paths. Nice.

newton/tests/test_import_mjcf.py (1)

610-639: LGTM: verifies radius and half_height mapping for cylinders

Asserts match builder’s scale layout (radius, half_height, 0). Nice.

newton/_src/utils/import_urdf.py (1)

158-167: Ignore — mapping URDF cylinders to add_shape_cylinder is correct

Repository already implements cylinder collision end-to-end and tests expect cylinders, so no blanket fallback to capsules is needed. Key evidence: newton/_src/sim/builder.py (add_shape_cylinder), newton/_src/geometry/kernels.py (cylinder SDF/grad + kernel handling), newton/_src/geometry/inertia.py (compute_cylinder_inertia), newton/_src/geometry/raycast.py (raycast support) and tests newton/tests/test_import_urdf.py, newton/tests/test_import_mjcf.py (assert GeoType.CYLINDER). Ignore the suggested capsule fallback.

Likely an incorrect or invalid review comment.

@eric-heiden eric-heiden left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@eric-heiden eric-heiden enabled auto-merge (squash) September 16, 2025 19:14
@eric-heiden eric-heiden merged commit 5e1cb4a into newton-physics:main Sep 16, 2025
12 checks passed
eric-heiden added a commit to eric-heiden/newton that referenced this pull request Jan 28, 2026
…ton-physics#333 (newton-physics#771)

Signed-off-by: Viktor Reutskyy <vreutskyy@nvidia.com>
Co-authored-by: Eric Heiden <eheiden@nvidia.com>
mmacklin pushed a commit to mmacklin/newton that referenced this pull request Apr 7, 2026
…ton-physics#333 (newton-physics#771)

Signed-off-by: Viktor Reutskyy <vreutskyy@nvidia.com>
Co-authored-by: Eric Heiden <eheiden@nvidia.com>
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.

MJCF Importer Incorrectly Converts Cylinder Colliders to Capsules

2 participants