Repository: Genesis-Embodied-AI/Genesis
File: tests/test_sensor_camera.py (Python)

Code before:
        if pose.ndim == 3:
            pose = pose[0]
        return pose[:3, 3].copy()

    cam_pos_initial = _get_camera_world_pos(raster_cam_attached)
    cam_pos_initial_offset_T = _get_camera_world_pos(raster_cam_offset_T)

    for i in range(10):  # Test over multiple steps
        scene.step()

    data_attached_moved = raster_cam_attached.read()
    cam_pos_final = _get_camera_world_pos(raster_cam_attached)
    cam_move_dist = np.linalg.norm(cam_pos_final - cam_pos_initial)
    assert cam_move_dist > 1e-2
    data_offset_T_moved = raster_cam_offset_T.read()
    cam_pos_final_offset_T = _get_camera_world_pos(raster_cam_offset_T)
    cam_move_dist_offset_T = np.linalg.norm(cam_pos_final_offset_T - cam_pos_initial_offset_T)
    assert cam_move_dist_offset_T > 1e-2
    assert_allclose(cam_move_dist_offset_T, cam_move_dist, atol=1e-2)


# ========================== Multi-environment tests ==========================


@pytest.mark.required
def test_rasterizer_camera_sensor_n_envs(png_snapshot):
    if sys.platform == "darwin":
        pytest.skip(
            "Batched rendering with Rasterizer backend is not supported on this machine because it requires OpenGL 4.2."
        )

    scene = gs.Scene(
        rigid_options=gs.options.RigidOptions(
            enable_collision=True,
            gravity=(0, 0, -9.8),
        ),
        renderer=gs.renderers.Rasterizer(),
        show_viewer=False,
    )

    # Add a plane
    scene.add_entity(
        morph=gs.morphs.Plane(),
        surface=gs.surfaces.Rough(color=(0.4, 0.4, 0.4)),
    )

    # Add a sphere
    sphere = scene.add_entity(
        morph=gs.morphs.Sphere(pos=(0.0, 0.0, 1.0), radius=0.3),
        surface=gs.surfaces.Smooth(color=(1.0, 0.5, 0.5)),
    )

Review comment (bug):
Consider supporting `show_viewer`.  This "global" flag is used to enable interactive viewer within unit tests. This is meant to use debugging in case of failure. It is enabled via the comment line argument `--vis`. Not sure if it is useful in this context. In this case, it should enable `show_viewer=True` at scene level, but also `draw_debug=True` in `gs.sensors.RasterizerCameraOptions`.

Code after:
        pose = context._scene.get_pose(node)
        if pose.ndim == 3:
            pose = pose[0]
        return pose[:3, 3].copy()

    cam_pos_initial = _get_camera_world_pos(raster_cam_attached)
    cam_pos_initial_offset_T = _get_camera_world_pos(raster_cam_offset_T)

    for i in range(10):  # Test over multiple steps
        scene.step()

    data_attached_moved = raster_cam_attached.read()
    cam_pos_final = _get_camera_world_pos(raster_cam_attached)
    cam_move_dist = np.linalg.norm(cam_pos_final - cam_pos_initial)
    assert cam_move_dist > 1e-2
    data_offset_T_moved = raster_cam_offset_T.read()
    cam_pos_final_offset_T = _get_camera_world_pos(raster_cam_offset_T)
    cam_move_dist_offset_T = np.linalg.norm(cam_pos_final_offset_T - cam_pos_initial_offset_T)
    assert cam_move_dist_offset_T > 1e-2
    assert_allclose(cam_move_dist_offset_T, cam_move_dist, atol=1e-2)


# ========================== Multi-environment tests ==========================


@pytest.mark.required
def test_rasterizer_camera_sensor_n_envs(show_viewer, png_snapshot):
    if sys.platform == "darwin":
        pytest.skip(
            "Batched rendering with Rasterizer backend is not supported on this machine because it requires OpenGL 4.2."
        )

    scene = gs.Scene(
        rigid_options=gs.options.RigidOptions(),
        renderer=gs.renderers.Rasterizer(),
        show_viewer=show_viewer,
    )

    # Add a plane
    scene.add_entity(
        morph=gs.morphs.Plane(),
        surface=gs.surfaces.Rough(color=(0.4, 0.4, 0.4)),
    )

    # Add a sphere
    sphere = scene.add_entity(
        morph=gs.morphs.Sphere(pos=(0.0, 0.0, 1.0), radius=0.3),
        surface=gs.surfaces.Smooth(color=(1.0, 0.5, 0.5)),
    )

    options = gs.sensors.RasterizerCameraOptions(