Skip to content

Commit

Permalink
Mesh terrain (#535)
Browse files Browse the repository at this point in the history
* allow mesh terrain

* done

* format

* update collision rule

* allow use mesh terrain

* mesh terrain test

* format

* add docstring

* fix bug
  • Loading branch information
QuanyiLi committed Oct 28, 2023
1 parent ad7d2cb commit f09148d
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 105 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@

**/tests/**/*.jpg
**/tests/**/*.png
/metadrive/engine/core/run_time_map_mesh.png
2 changes: 1 addition & 1 deletion metadrive/component/map/base_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ def get_height_map(
cv2.fillPoly(mask, np.asarray([points]).astype(np.int32), color=[height])
if need_scale:
# Define a kernel. A 3x3 rectangle kernel
kernel = np.ones((extension * pixels_per_meter + 1, extension * pixels_per_meter + 1), np.uint8)
kernel = np.ones(((extension + 1) * pixels_per_meter, (extension + 1) * pixels_per_meter), np.uint8)

# Apply dilation
mask = cv2.dilate(mask, kernel, iterations=1)
Expand Down
3 changes: 3 additions & 0 deletions metadrive/component/sensors/rgb_camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@


class RGBCamera(BaseCamera):
"""
Create a new RGBCamera
"""
# shape(dim_1, dim_2)
BUFFER_W = 84 # dim 1
BUFFER_H = 84 # dim 2
Expand Down
3 changes: 2 additions & 1 deletion metadrive/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class TerminationState:
MetaDriveType.LINE_BROKEN_SINGLE_WHITE: "green",
MetaDriveType.LANE_SURFACE_STREET: "green",
MetaDriveType.LANE_SURFACE_UNSTRUCTURE: "green",
MetaDriveType.LANE_BIKE_LANE: "green",
MetaDriveType.VEHICLE: "red",
MetaDriveType.GROUND: "yellow",
MetaDriveType.TRAFFIC_OBJECT: "yellow",
Expand Down Expand Up @@ -144,7 +145,7 @@ def collision_rules(cls):
(cls.Terrain, cls.Vehicle, True),
(cls.Terrain, cls.ContinuousLaneLine, False),
(cls.Terrain, cls.InvisibleWall, False),
(cls.Terrain, cls.Sidewalk, True),
(cls.Terrain, cls.Sidewalk, False),
(cls.Terrain, cls.LidarBroadDetector, False),
(cls.Terrain, cls.TrafficObject, True),
(cls.Terrain, cls.TrafficParticipants, True),
Expand Down
3 changes: 2 additions & 1 deletion metadrive/engine/base_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,8 @@ def process_memory():
self.terrain.reset(center_p)

# init shadow if required
if hasattr(self, "pssm") and self.pssm.buffer is None and self.global_config["show_terrain"]:
if hasattr(self, "pssm") and self.pssm.buffer is None and self.global_config["show_terrain"] \
and not self.global_config["debug_physics_world"]:
self.pssm.init()

# move skybox
Expand Down
213 changes: 123 additions & 90 deletions metadrive/engine/core/terrain.py

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions metadrive/envs/base_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,11 @@

# ===== Mesh Terrain =====
# road will have a marin whose width is determined by this value, unit: [m]
drivable_area_extension=6,
drivable_area_extension=7,
# height scale for mountains, unit: [m]
height_scale=120,
height_scale=50,
# use plane collision or mesh collision
plane_terrain=True,

# ===== Others =====
# Force to generate objects in the left lane.
Expand Down
5 changes: 4 additions & 1 deletion metadrive/tests/test_policy/test_expert_performance.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import time
import pytest

import numpy as np

Expand Down Expand Up @@ -56,11 +57,13 @@ def _evaluate(env_config, num_episode, has_traffic=True, need_on_same_lane=True)
return ep_reward_mean, success_rate


def test_expert_with_traffic(use_render=False):
@pytest.mark.parametrize("plane", [True, False], ids=["plane", "mesh"])
def test_expert_with_traffic(plane, use_render=False):
ep_reward, success_rate = _evaluate(
dict(
num_scenarios=1,
map="CCC",
plane_terrain=plane,
start_seed=2,
random_traffic=False,
# debug_static_world=True,
Expand Down
13 changes: 8 additions & 5 deletions metadrive/tests/test_policy/test_idm_policy.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import numpy as np
import pytest

from metadrive.component.vehicle.vehicle_type import DefaultVehicle
from metadrive.engine.engine_utils import initialize_engine
Expand Down Expand Up @@ -27,8 +28,9 @@ def _create_vehicle():
return v


def test_idm_policy_briefly():
env = MetaDriveEnv()
@pytest.mark.parametrize("plane_terrain", [True, False], ids=["plane", "mesh"])
def test_idm_policy_briefly(plane_terrain):
env = MetaDriveEnv({"plane_terrain": plane_terrain})
env.reset()
try:
vehicles = env.engine.traffic_manager.traffic_vehicles
Expand All @@ -52,9 +54,10 @@ def test_idm_policy_briefly():
env.close()


def test_idm_policy_is_moving(render=False, in_test=True):
@pytest.mark.parametrize("plane_terrain", [True, False], ids=["plane", "mesh"])
def test_idm_policy_is_moving(plane_terrain, render=False, in_test=True):
# config = {"traffic_mode": "hybrid", "map": "SS", "traffic_density": 1.0}
config = {"traffic_mode": "respawn", "map": "SS", "traffic_density": 1.0}
config = {"plane_terrain": plane_terrain, "traffic_mode": "respawn", "map": "SS", "traffic_density": 1.0}
if render:
config.update({"use_render": True, "manual_control": True})
env = MetaDriveEnv(config)
Expand All @@ -76,4 +79,4 @@ def test_idm_policy_is_moving(render=False, in_test=True):

if __name__ == '__main__':
# test_idm_policy_briefly()
test_idm_policy_is_moving(render=True, in_test=False)
test_idm_policy_is_moving(False, render=True, in_test=False)
10 changes: 6 additions & 4 deletions metadrive/tests/vis_env/vis_metadrive_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
env = MetaDriveEnv(
{
"num_scenarios": 10,
"traffic_density": 0.15,
"traffic_mode": "hybrid",
"traffic_density": 0,
"start_seed": 74,
# "_disable_detector_mask":True,
# "debug_physics_world": True,
"debug": True,
# "global_light": False,
# "debug_static_world": True,
"pstats": True,
"static_traffic_object": False,
"show_interface": True,
"cull_scene": False,
Expand All @@ -25,6 +25,7 @@
"random_agent_model": False,
"manual_control": True,
"use_render": True,
"plane_terrain": False,
"accident_prob": 1,
"decision_repeat": 5,
"daytime": "19:00",
Expand All @@ -38,9 +39,10 @@
"driving_reward": 1.0,
# "pstats": True,
"force_destroy": False,
# "show_terrain"
# "show_skybox": False,
"show_fps": False,
"render_pipeline": True,
"render_pipeline": False,
# "camera_dist": 8,
"window_size": (1200, 800),
"camera_dist": 9,
Expand All @@ -49,7 +51,7 @@
# "camera_smooth": False,
# "camera_height": -1,
"vehicle_config": {
"enable_reverse": False,
"enable_reverse": True,
# "vehicle_model": "xl",
# "rgb_camera": (1024, 1024),
# "spawn_velocity": [8.728615581032535, -0.24411703918728195],
Expand Down

0 comments on commit f09148d

Please sign in to comment.