Skip to content

Adds raycast implementation and tests #146

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

Merged
merged 33 commits into from
May 12, 2025

Conversation

nvtw
Copy link
Collaborator

@nvtw nvtw commented Apr 11, 2025

See #94

@nvtw
Copy link
Collaborator Author

nvtw commented Apr 11, 2025

I assume the tests currently fail because wp.tile_argmin was only merged yesterday into warp and might not yet be in the warp build we are using. Will investigate more on Monday.

@thowell thowell linked an issue Apr 11, 2025 that may be closed by this pull request
m.mesh_vertadr = wp.array(mjm.mesh_vertadr, dtype=wp.int32, ndim=1)
m.mesh_vertnum = wp.array(mjm.mesh_vertnum, dtype=wp.int32, ndim=1)
m.mesh_vert = wp.array(mjm.mesh_vert, dtype=wp.vec3, ndim=1)
m.mesh_faceadr = wp.array(mjm.mesh_faceadr, dtype=wp.int32, ndim=1)
m.mesh_face = wp.array(mjm.mesh_face, dtype=wp.int32, ndim=2)
Copy link
Collaborator

Choose a reason for hiding this comment

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

since mesh_face is (nmeshface, 3) this array could be wp.array(mjm.mesh_face, dtype=wp.vec3i, ndim=1)

class RayTest(absltest.TestCase):
def test_ray_nothing(self):
"""Tests that ray returns -1 when nothing is hit."""
m, d, _, _ = test_util.fixture("ray.xml")
Copy link
Collaborator

Choose a reason for hiding this comment

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

following the convention of other tests:

mjm, mjd, m, d = ...

then it is not necessary to call mujoco.mj_forward, mjwarp.put_model, and mjwarp.put_data below since these functions are called by test_util.fixture.


def test_ray_plane(self):
"""Tests ray<>plane matches MuJoCo."""
m, d, _, _ = test_util.fixture("ray.xml")
Copy link
Collaborator

Choose a reason for hiding this comment

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

mjm, mjd, m, d = ...


def test_ray_sphere(self):
"""Tests ray<>sphere matches MuJoCo."""
m, d, _, _ = test_util.fixture("ray.xml")
Copy link
Collaborator

Choose a reason for hiding this comment

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

mjm, mjd, m, d = ...


def test_ray_capsule(self):
"""Tests ray<>capsule matches MuJoCo."""
m, d, _, _ = test_util.fixture("ray.xml")
Copy link
Collaborator

Choose a reason for hiding this comment

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

mjm, mjd, m, d = ...


def test_ray_box(self):
"""Tests ray<>box matches MuJoCo."""
m, d, _, _ = test_util.fixture("ray.xml")
Copy link
Collaborator

Choose a reason for hiding this comment

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

mjm, mjd, m, d = ...

_assert_eq(dist_np, mj_dist, "dist")

def test_ray_mesh(self):
"""Tests ray<>mesh matches MuJoCo."""
Copy link
Collaborator

Choose a reason for hiding this comment

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

mjm, mjd, m, d = ...


def test_ray_geomgroup(self):
"""Tests ray geomgroup filter."""
m, d, _, _ = test_util.fixture("ray.xml")
Copy link
Collaborator

Choose a reason for hiding this comment

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

mjm, mjd, m, d = ...


def test_ray_flg_static(self):
"""Tests ray flg_static filter."""
m, d, _, _ = test_util.fixture("ray.xml")
Copy link
Collaborator

Choose a reason for hiding this comment

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

mjm, mjd, m, d = ...


def test_ray_bodyexclude(self):
"""Tests ray bodyexclude filter."""
m, d, _, _ = test_util.fixture("ray.xml")
Copy link
Collaborator

Choose a reason for hiding this comment

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

mjm, mjd, m, d = ...

dist_np = dist.numpy()[0][0]
_assert_eq(geomid_np, -1, "geom_id")
_assert_eq(dist_np, -1, "dist")

Copy link
Collaborator

Choose a reason for hiding this comment

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

@@ -576,6 +583,7 @@ class Model:
geom_condim: contact dimensionality (1, 3, 4, 6) (ngeom,)
geom_bodyid: id of geom's body (ngeom,)
geom_dataid: id of geom's mesh/hfield; -1: none (ngeom,)
geom_group: geom group inclusion/exclusion mask (ngeom, 1)
Copy link
Collaborator

Choose a reason for hiding this comment

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

(ngeom,)

@@ -788,6 +801,8 @@ class Model:
mesh_vertadr: wp.array(dtype=wp.int32, ndim=1)
mesh_vertnum: wp.array(dtype=wp.int32, ndim=1)
mesh_vert: wp.array(dtype=wp.vec3, ndim=1)
mesh_faceadr: wp.array(dtype=wp.int32, ndim=1)
mesh_face: wp.array(dtype=wp.int32, ndim=2)
Copy link
Collaborator

Choose a reason for hiding this comment

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

wp.array(dtype=wp.vec3i, ndim=1)

triangle.v2 = m.mesh_vert[vert_start + v_idx.z]

# Calculate intersection
dist = _ray_triangle(triangle, pnt, vec, basis)
Copy link
Collaborator

Choose a reason for hiding this comment

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

@nvtw
Copy link
Collaborator Author

nvtw commented Apr 22, 2025

I don't understand why the ray unit tests fail on github. They succeed on my Linux and on my Windows machine.

@erikfrey
Copy link
Collaborator

@nvtw Is it a CPU vs GPU issue? Do you have a way to force CPU on one of your dev machines and rerun the tests?

@thowell
Copy link
Collaborator

thowell commented Apr 29, 2025

@nvtw it should be possible to reproduce the test failure locally with #200

@thowell
Copy link
Collaborator

thowell commented May 1, 2025

@erikfrey is it ok to add these .stl files? it looks like they are from https://github.com/google-deepmind/mujoco/tree/main/mjx/mujoco/mjx/test_data/meshes.

the alternative would be to define meshes directly in xml?

# Conflicts:
#	mujoco_warp/_src/io.py
@thowell
Copy link
Collaborator

thowell commented May 5, 2025

request that would be useful for the touch sensor implementation #213:

  • update _ray_geom to follow the mujoco implementation api, something like:
    ray_geom(pos: wp.vec3, mat: wp.mat33, size: wp.vec3, pnt: wp.vec3, vec: wp.vec3, geomtype: int) -> float

here is the mujoco reference for mju_rayGeom: https://github.com/google-deepmind/mujoco/blob/253abad210823254f0f238ef3ad4cc8d975372d3/src/engine/engine_ray.c#L818

here is the mujoco reference for mjSENS_TOUCH: https://github.com/google-deepmind/mujoco/blob/253abad210823254f0f238ef3ad4cc8d975372d3/src/engine/engine_sensor.c#L777 where mju_rayGeom is utilized

thanks!

@erikfrey
Copy link
Collaborator

erikfrey commented May 9, 2025

@thowell yes it's OK to bring those meshes in.

@nvtw
Copy link
Collaborator Author

nvtw commented May 12, 2025

Should be all done now

@@ -127,17 +127,29 @@ def put_model(mjm: mujoco.MjModel) -> types.Model:
tile_beg = tile_corners[i]
tile_end = mjm.nv if i == len(tile_corners) - 1 else tile_corners[i + 1]
tiles.setdefault(tile_end - tile_beg, []).append(tile_beg)

# Handle empty tiles case
Copy link
Collaborator

Choose a reason for hiding this comment

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

is this block of code still utilized?

Copy link
Collaborator

Choose a reason for hiding this comment

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

is this file needed?

@thowell
Copy link
Collaborator

thowell commented May 12, 2025

@nvtw please take a look at the comment about the block of code in io.py and the comment about the mesh file. thanks!

@thowell thowell merged commit cd6bfbb into google-deepmind:main May 12, 2025
5 checks 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.

Ray casting.
4 participants