Skip to content

Commit 3aed8bb

Browse files
committed
Add Isaac Gym renderer adapters
Mirror the existing MuJoCo/MotrixSim adapter pair for Isaac Gym so that robogym-nav and similar projects can use the package directly: - GSRendererIsaacGym: single-scene multi-camera adapter (analogous to GSRendererMuJoCo / GSRendererMotrixSim). Pulls body name->id from gym.get_actor_rigid_body_dict(env, actor_handle); handles xyzw quaternions. - IsaacBatchSplatRenderer: multi-env batched adapter (analogous to MjxBatchSplatRenderer / MtxBatchSplatRenderer), for Isaac Gym training loops with one camera per env.
1 parent 470126e commit 3aed8bb

4 files changed

Lines changed: 274 additions & 2 deletions

File tree

‎src/gaussian_renderer/__init__.py‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,19 @@
2222
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2323
# SOFTWARE.
2424

25-
from .batch_splat import BatchSplatConfig, BatchSplatRenderer, MjxBatchSplatRenderer, MtxBatchSplatRenderer
25+
from .batch_splat import (
26+
BatchSplatConfig,
27+
BatchSplatRenderer,
28+
IsaacBatchSplatRenderer,
29+
MjxBatchSplatRenderer,
30+
MtxBatchSplatRenderer,
31+
)
2632
from .core.batch_rasterization import batch_env_render, batch_render, batch_update_gaussians
2733
from .core.gaussiandata import GaussianBatchData, GaussianData
2834
from .core.gs_renderer import GSRenderer
2935
from .core.super_splat_loader import is_super_splat_format, load_super_splat_ply, save_super_splat_ply
3036
from .core.util_gau import load_ply, save_ply, transform_shs
37+
from .gs_renderer_isaacgym import GSRendererIsaacGym
3138
from .gs_renderer_motrixsim import GSRendererMotrixSim
3239
from .gs_renderer_mujoco import GSRendererMuJoCo
3340

‎src/gaussian_renderer/batch_splat.py‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,4 +229,29 @@ def batch_update_gaussians(self, body_pos: Tensor, body_quat: Tensor) -> Gaussia
229229
return super().batch_update_gaussians(body_pos, body_quat, scalar_first=False)
230230

231231

232-
__all__ = ["BatchSplatConfig", "BatchSplatRenderer", "MjxBatchSplatRenderer", "MtxBatchSplatRenderer"]
232+
if TYPE_CHECKING:
233+
from isaacgym import gymapi
234+
235+
236+
class IsaacBatchSplatRenderer(BatchSplatRenderer):
237+
def __init__(
238+
self,
239+
cfg: BatchSplatConfig,
240+
gym: "gymapi.Gym",
241+
env: "gymapi.Env",
242+
actor_handle: int,
243+
) -> None:
244+
body_name_to_id: Dict[str, int] = gym.get_actor_rigid_body_dict(env, actor_handle)
245+
super().__init__(cfg, body_name_to_id)
246+
247+
def batch_update_gaussians(self, body_pos: Tensor, body_quat: Tensor) -> GaussianBatchData:
248+
return super().batch_update_gaussians(body_pos, body_quat, scalar_first=False)
249+
250+
251+
__all__ = [
252+
"BatchSplatConfig",
253+
"BatchSplatRenderer",
254+
"MjxBatchSplatRenderer",
255+
"MtxBatchSplatRenderer",
256+
"IsaacBatchSplatRenderer",
257+
]
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# SPDX-License-Identifier: MIT
2+
#
3+
# MIT License
4+
#
5+
# Copyright (c) 2025 Yufei Jia
6+
#
7+
# Permission is hereby granted, free of charge, to any person obtaining a copy
8+
# of this software and associated documentation files (the "Software"), to deal
9+
# in the Software without restriction, including without limitation the rights
10+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
# copies of the Software, and to permit persons to whom the Software is
12+
# furnished to do so, subject to the following conditions:
13+
#
14+
# The above copyright notice and this permission notice shall be included in all
15+
# copies or substantial portions of the Software.
16+
#
17+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
# SOFTWARE.
24+
from typing import TYPE_CHECKING, Dict, Tuple, Union
25+
26+
import numpy as np
27+
from scipy.spatial.transform import Rotation
28+
from torch import Tensor
29+
30+
if TYPE_CHECKING: # pragma: no cover - import-time only
31+
from isaacgym import gymapi # type: ignore
32+
33+
from .core.gs_renderer import GSRenderer
34+
35+
36+
class GSRendererIsaacGym(GSRenderer):
37+
def __init__(
38+
self,
39+
models_dict: Dict[str, str],
40+
gym: "gymapi.Gym",
41+
env: "gymapi.Env",
42+
actor_handle: int,
43+
) -> None:
44+
super().__init__(models_dict)
45+
self.init_renderer(gym, env, actor_handle)
46+
47+
def init_renderer(self, gym: "gymapi.Gym", env: "gymapi.Env", actor_handle: int) -> None:
48+
body_name_to_id: Dict[str, int] = gym.get_actor_rigid_body_dict(env, actor_handle)
49+
50+
self.gs_idx_start = []
51+
self.gs_idx_end = []
52+
self.gs_body_ids = []
53+
54+
objects_info = []
55+
for body_name, body_id in body_name_to_id.items():
56+
if body_name in self.gaussian_model_names:
57+
start_idx = self.gaussian_start_indices[body_name]
58+
end_idx = self.gaussian_end_indices[body_name]
59+
self.gs_idx_start.append(start_idx)
60+
self.gs_idx_end.append(end_idx)
61+
self.gs_body_ids.append(body_id)
62+
objects_info.append((body_name, start_idx, end_idx))
63+
64+
self.gs_idx_start = np.array(self.gs_idx_start)
65+
self.gs_idx_end = np.array(self.gs_idx_end)
66+
self.gs_body_ids = np.array(self.gs_body_ids)
67+
68+
self.set_objects_mapping(objects_info)
69+
70+
def update_gaussians(self, body_pos: Union[np.ndarray, Tensor], body_quat: Union[np.ndarray, Tensor]) -> None:
71+
"""Pull rigid body poses for the gaussian-bearing bodies and update.
72+
73+
Args:
74+
body_pos: (Nbody, 3) world positions, indexable by body id.
75+
body_quat: (Nbody, 4) world quaternions in xyzw order (Isaac Gym convention).
76+
"""
77+
if not hasattr(self, "gs_idx_start") or len(self.gs_idx_start) == 0:
78+
return
79+
80+
pos_values = body_pos[self.gs_body_ids]
81+
quat_values = body_quat[self.gs_body_ids]
82+
83+
self.update_gaussian_properties(pos_values, quat_values, scalar_first=False)
84+
85+
def render(
86+
self,
87+
cam_pos: Union[np.ndarray, Tensor],
88+
cam_quat: Union[np.ndarray, Tensor],
89+
width: int,
90+
height: int,
91+
fovy: Union[float, np.ndarray],
92+
y_up: bool = True,
93+
) -> Tuple[Tensor, Tensor]:
94+
"""Render one batch of cameras given their world poses.
95+
96+
Args:
97+
cam_pos: (Ncam, 3) camera world positions.
98+
cam_quat: (Ncam, 4) camera world quaternions in xyzw order.
99+
width, height: image size in pixels.
100+
fovy: scalar or (Ncam,) array of vertical FOV in degrees.
101+
y_up: forwarded to ``render_batch``; controls the OpenGL-style
102+
Y/Z flip applied to the camera rotation.
103+
Returns:
104+
``(rgb, depth)`` tensors of shape ``(Ncam, H, W, 3)`` and
105+
``(Ncam, H, W, 1)``.
106+
"""
107+
cam_pos_arr = np.asarray(cam_pos).reshape(-1, 3)
108+
cam_quat_arr = np.asarray(cam_quat).reshape(-1, 4)
109+
cam_xmat = Rotation.from_quat(cam_quat_arr).as_matrix().reshape(-1, 9)
110+
fovy_arr = np.asarray(fovy, dtype=np.float32).reshape(-1)
111+
if fovy_arr.shape[0] == 1 and cam_pos_arr.shape[0] > 1:
112+
fovy_arr = np.broadcast_to(fovy_arr, (cam_pos_arr.shape[0],)).copy()
113+
114+
return self.render_batch(cam_pos_arr, cam_xmat, height, width, fovy_arr, y_up=y_up)

‎tests/test_isaacgym_renderer.py‎

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
"""Tests for GSRendererIsaacGym — CPU-only, no real CUDA or Isaac Gym binary."""
2+
3+
from unittest.mock import MagicMock, patch
4+
5+
import numpy as np
6+
import pytest
7+
from conftest import BANANA_PLY
8+
9+
from gaussian_renderer.gs_renderer_isaacgym import GSRendererIsaacGym
10+
11+
# ---------------------------------------------------------------------------
12+
# Helpers
13+
# ---------------------------------------------------------------------------
14+
15+
16+
def _make_gym(body_name_to_id):
17+
gym = MagicMock()
18+
gym.get_actor_rigid_body_dict.return_value = dict(body_name_to_id)
19+
return gym
20+
21+
22+
def _make_renderer(body_name_to_id, models_dict):
23+
gym = _make_gym(body_name_to_id)
24+
env = MagicMock()
25+
actor_handle = 0
26+
with (
27+
patch("gaussian_renderer.core.gs_renderer.GSPLAT_AVAILABLE", True),
28+
patch("torch.Tensor.cuda", lambda self: self),
29+
):
30+
from gaussian_renderer.core.gs_renderer import GSRenderer
31+
32+
renderer = GSRendererIsaacGym.__new__(GSRendererIsaacGym)
33+
GSRenderer.__init__(renderer, models_dict)
34+
renderer.init_renderer(gym, env, actor_handle)
35+
return renderer
36+
37+
38+
# ---------------------------------------------------------------------------
39+
# Tests
40+
# ---------------------------------------------------------------------------
41+
42+
43+
@pytest.mark.skipif(not BANANA_PLY.exists(), reason="banana.ply not found")
44+
def test_init_renderer_maps_bodies():
45+
"""init_renderer maps matching body names to gaussian indices."""
46+
renderer = _make_renderer({"world": 0, "banana": 1}, {"banana": str(BANANA_PLY)})
47+
48+
assert len(renderer.gs_body_ids) == 1
49+
assert renderer.gs_body_ids[0] == 1
50+
assert renderer.dynamic_mask.any()
51+
52+
53+
@pytest.mark.skipif(not BANANA_PLY.exists(), reason="banana.ply not found")
54+
def test_init_renderer_queries_actor_rigid_body_dict():
55+
"""init_renderer pulls body mapping from gym.get_actor_rigid_body_dict."""
56+
gym = _make_gym({"world": 0, "banana": 1})
57+
env = MagicMock()
58+
actor = 7
59+
with (
60+
patch("gaussian_renderer.core.gs_renderer.GSPLAT_AVAILABLE", True),
61+
patch("torch.Tensor.cuda", lambda self: self),
62+
):
63+
from gaussian_renderer.core.gs_renderer import GSRenderer
64+
65+
renderer = GSRendererIsaacGym.__new__(GSRendererIsaacGym)
66+
GSRenderer.__init__(renderer, {"banana": str(BANANA_PLY)})
67+
renderer.init_renderer(gym, env, actor)
68+
69+
gym.get_actor_rigid_body_dict.assert_called_once_with(env, actor)
70+
71+
72+
@pytest.mark.skipif(not BANANA_PLY.exists(), reason="banana.ply not found")
73+
def test_init_renderer_no_matching_bodies():
74+
"""No matching bodies → empty arrays, dynamic_mask all False."""
75+
renderer = _make_renderer({"world": 0, "robot": 1}, {"banana": str(BANANA_PLY)})
76+
77+
assert len(renderer.gs_body_ids) == 0
78+
assert not renderer.dynamic_mask.any()
79+
80+
81+
@pytest.mark.skipif(not BANANA_PLY.exists(), reason="banana.ply not found")
82+
def test_update_gaussians_passes_correct_slices():
83+
"""update_gaussians extracts the right body rows and forwards xyzw."""
84+
renderer = _make_renderer({"world": 0, "banana": 1}, {"banana": str(BANANA_PLY)})
85+
86+
body_pos = np.zeros((2, 3))
87+
body_pos[1] = [1.0, 2.0, 3.0]
88+
body_quat = np.tile([0.0, 0.0, 0.0, 1.0], (2, 1))
89+
90+
with patch.object(renderer, "update_gaussian_properties") as mock_upd:
91+
renderer.update_gaussians(body_pos, body_quat)
92+
mock_upd.assert_called_once()
93+
pos_arg, quat_arg = mock_upd.call_args[0]
94+
kwargs = mock_upd.call_args[1]
95+
np.testing.assert_array_equal(pos_arg, body_pos[[1]])
96+
np.testing.assert_array_equal(quat_arg, body_quat[[1]])
97+
assert kwargs.get("scalar_first") is False
98+
99+
100+
@pytest.mark.skipif(not BANANA_PLY.exists(), reason="banana.ply not found")
101+
def test_update_gaussians_empty_skips():
102+
"""update_gaussians with no mapped bodies returns without error."""
103+
renderer = _make_renderer({"world": 0}, {"banana": str(BANANA_PLY)})
104+
renderer.update_gaussians(np.zeros((1, 3)), np.tile([0.0, 0.0, 0.0, 1.0], (1, 1))) # should not raise
105+
106+
107+
@pytest.mark.skipif(not BANANA_PLY.exists(), reason="banana.ply not found")
108+
def test_render_calls_render_batch_with_xyzw_to_xmat():
109+
"""render() converts cam_quat (xyzw) to a flattened rotation matrix."""
110+
renderer = _make_renderer({"world": 0, "banana": 1}, {"banana": str(BANANA_PLY)})
111+
112+
cam_pos = np.array([[1.0, 2.0, 3.0]], dtype=np.float32)
113+
cam_quat = np.array([[0.0, 0.0, 0.0, 1.0]], dtype=np.float32)
114+
115+
with patch.object(renderer, "render_batch", return_value=("rgb", "depth")) as mock_rb:
116+
rgb, depth = renderer.render(cam_pos, cam_quat, width=64, height=48, fovy=45.0)
117+
118+
assert (rgb, depth) == ("rgb", "depth")
119+
mock_rb.assert_called_once()
120+
args, kwargs = mock_rb.call_args
121+
np.testing.assert_array_equal(args[0], cam_pos)
122+
expected_xmat = np.eye(3).reshape(1, 9)
123+
np.testing.assert_allclose(args[1], expected_xmat, atol=1e-6)
124+
assert args[2] == 48 and args[3] == 64
125+
np.testing.assert_array_equal(args[4], np.array([45.0], dtype=np.float32))
126+
assert kwargs.get("y_up") is True

0 commit comments

Comments
 (0)