Skip to content
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

[BUG FIX] Update texrepeat handling for mjcf #675

Merged

Conversation

bxtbold
Copy link
Contributor

@bxtbold bxtbold commented Feb 5, 2025

Description

Replaced converting numpy array elements (array.astype(int)) with np.ceil operation (np.ceil(array).astype(int)). The reason I used this operation is texrepeat values are positive.

Related Issue

Resolves #616

Motivation and Context

When mjcf texrepeat is handled, it is converted to integer. That causes some issues for values such as [0.5, 0.5] which is rounded down to [0, 0]. The zeros array ends up emptying the image_array at 307 and 341 of utils/mjcf.py.

How Has This Been / Can This Be Tested?

This is a minimal example to reproduce the bug.

import numpy as np

def reproduce_error():
    # emulating utils/mjcf.py from line 305 to 307
    image_array_before = np.ones((2048, 2048, 4))
    tex_repeat = np.array([0.5, 0.5]).astype(int)
    image_array_after = np.tile(
        image_array_before, (tex_repeat[0], tex_repeat[1], 1)
    )  # this becomes an empty array
    print(
        image_array_before.shape, image_array_after.shape
    )  # (2048, 2048, 4) (0, 0, 4)

    # emulate options/textures.py line 155
    np.max(image_array_after, axis=(0, 1))  # This will throw ValueError

def fix_error():
    # emulating utils/mjcf.py from line 305 to 307
    image_array_before = np.ones((2048, 2048, 4))
    tex_repeat = np.ceil([0.5, 0.5]).astype(int)
    image_array_after = np.tile(image_array_before, (tex_repeat[0], tex_repeat[1], 1))
    print(
        image_array_before.shape, image_array_after.shape
    )  # (2048, 2048, 4) (2048, 2048, 4)

    # emulate options/textures.py line 155
    np.max(image_array_after, axis=(0, 1))  # Works yeay

if __name__ == "__main__":
    reproduce_error()
    fix_error()

To test the changes in Genesis, download the zip folder from #616 and run the following:

import genesis as gs
import time

gs.init(backend=gs.cpu)
scene = gs.Scene(show_FPS=False, show_viewer=True)
scene.add_entity(gs.morphs.MJCF(file='floor_backing_room/model.xml'))

scene.build()

i = 0
while i < 100:
    scene.step()
    time.sleep(0.1)
    i += 1

Screenshots (if appropriate):

Checklist:

  • I read the CONTRIBUTING document.
  • I followed the Submitting Code Changes section of CONTRIBUTING document.
  • I tagged the title correctly (including BUG FIX/FEATURE/MISC/BREAKING)
  • I updated the documentation accordingly or no change is needed.
  • I tested my changes and added instructions on how to test it for reviewers.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@YilingQiao
Copy link
Collaborator

Thank you!

@YilingQiao YilingQiao merged commit 2d39cfc into Genesis-Embodied-AI:main Feb 5, 2025
1 check passed
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.

Bug. Error occurs when load MJCF.
2 participants