Skip to content

Commit

Permalink
Add some docs to vis_block (#529)
Browse files Browse the repository at this point in the history
* fix vis_block

* format
  • Loading branch information
pengzhenghao committed Oct 26, 2023
1 parent 41f60f9 commit 13c934b
Show file tree
Hide file tree
Showing 17 changed files with 36 additions and 59 deletions.
2 changes: 1 addition & 1 deletion metadrive/component/pgblock/pg_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def _sample_topology(self) -> bool:
def get_socket(self, index: Union[str, int]) -> PGBlockSocket:
if isinstance(index, int):
if index < 0 or index >= len(self._sockets):
raise ValueError("Socket of {}: index out of range".format(self.class_name))
raise ValueError("Socket of {}: index out of range {}".format(self.class_name, len(self._sockets)))
socket_index = list(self._sockets)[index]
else:
assert index.startswith(self.name)
Expand Down
5 changes: 3 additions & 2 deletions metadrive/tests/vis_block/vis_a_small_town.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""This file visualizes a small town. Please zoom out in the pop-up window."""
from metadrive.component.pg_space import Parameter
from metadrive.component.pgblock.curve import Curve
from metadrive.component.pgblock.first_block import FirstPGBlock
from metadrive.component.pgblock.roundabout import Roundabout
Expand All @@ -7,12 +9,11 @@
from metadrive.component.road_network.node_road_network import NodeRoadNetwork
from metadrive.engine.asset_loader import initialize_asset_loader
from metadrive.tests.vis_block.vis_block_base import TestBlock
from metadrive.component.pg_space import Parameter

if __name__ == "__main__":
FirstPGBlock.ENTRANCE_LENGTH = 0.5
test = TestBlock(False)
initialize_asset_loader(test)
initialize_asset_loader(engine=test)
global_network = NodeRoadNetwork()
blocks = []
init_block = FirstPGBlock(global_network, 3.0, 3, test.render, test.world, 1)
Expand Down
3 changes: 2 additions & 1 deletion metadrive/tests/vis_block/vis_big.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""This file visualizes the process of BIG algorithm, a procedural generation algorithm."""
from metadrive.component.algorithm.BIG import BIG
from metadrive.component.road_network.node_road_network import NodeRoadNetwork
from metadrive.engine.asset_loader import initialize_asset_loader
Expand All @@ -9,7 +10,7 @@ def vis_big(debug: bool = False):

test.cam.setPos(250, 100, 2000)

initialize_asset_loader(test)
initialize_asset_loader(engine=test)
global_network = NodeRoadNetwork()

big = BIG(2, 3.5, global_network, test.render, test.world, random_seed=5)
Expand Down
6 changes: 5 additions & 1 deletion metadrive/tests/vis_block/vis_block_base.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
"""
This file create a TestBlock class, which resembles the ``engine'' in normal code. By building this TestBlock,
we can directly pop up a Panda3D window and visualize the content, e.g. the road blocks that are constructed via
block.construct_block(test_block.render, test_block.world).
"""
from typing import Union, Tuple

from direct.showbase import ShowBase
Expand Down Expand Up @@ -115,7 +120,6 @@ def draw_line_3d(self, start_p: Union[Vec3, Tuple], end_p: Union[Vec3, Tuple], c
line_seg.setThickness(thickness)
np = NodePath(line_seg.create(False))
np.reparentTo(self.render)
# TODO(PZH): NodePath is not registered.

def show_bounding_box(self, road_network):
bound_box = road_network.get_bounding_box()
Expand Down
1 change: 1 addition & 0 deletions metadrive/tests/vis_block/vis_bottleneck.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""This file visualizes a Bottleneck block."""
from metadrive.component.pgblock.bottleneck import Merge, Split
from metadrive.component.pgblock.first_block import FirstPGBlock
from metadrive.component.road_network.node_road_network import NodeRoadNetwork
Expand Down
1 change: 1 addition & 0 deletions metadrive/tests/vis_block/vis_curve_block.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""This file visualizes a Curve block. Use mouse left button to draw down for zooming out."""
from metadrive.component.pgblock.curve import Curve
from metadrive.component.pgblock.first_block import FirstPGBlock
from metadrive.component.road_network.node_road_network import NodeRoadNetwork
Expand Down
5 changes: 3 additions & 2 deletions metadrive/tests/vis_block/vis_in_ramp.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""This file visualizes a InRampOnStraight block. Use mouse left button to draw down for zooming out."""
from metadrive.component.pgblock.first_block import FirstPGBlock
from metadrive.component.pgblock.ramp import InRampOnStraight
from metadrive.component.pgblock.straight import Straight
Expand All @@ -14,11 +15,11 @@
global_network = NodeRoadNetwork()
straight = FirstPGBlock(global_network, 3.0, 1, test.render, test.world, 1)
straight = Straight(4, straight.get_socket(0), global_network, 1)
# print(straight.construct_block(test.render, test.world))
straight.construct_block(test.render, test.world)
# print(len(straight.dynamic_nodes))
for i in range(1, 3):
straight = InRampOnStraight(i, straight.get_socket(0), global_network, i)
# print(straight.construct_block(test.render, test.world))
straight.construct_block(test.render, test.world)
# print(len(straight.dynamic_nodes))
test.show_bounding_box(global_network)
test.run()
11 changes: 3 additions & 8 deletions metadrive/tests/vis_block/vis_intersection.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""This file visualizes an InterSection block. Use mouse left button to draw down for zooming out."""
from metadrive.component.pgblock.curve import Curve
from metadrive.component.pgblock.first_block import FirstPGBlock
from metadrive.component.pgblock.intersection import InterSection
Expand All @@ -7,23 +8,17 @@
if __name__ == "__main__":
test = TestBlock()
from metadrive.engine.asset_loader import initialize_asset_loader

initialize_asset_loader(test)

initialize_asset_loader(engine=test)
global_network = NodeRoadNetwork()
first = FirstPGBlock(global_network, 3.0, 2, test.render, test.world, 20)

intersection = InterSection(3, first.get_socket(0), global_network, 1)
# print(intersection.construct_block(test.render, test.world))

intersection.construct_block(test.render, test.world)
id = 4
for socket_idx in range(intersection.SOCKET_NUM):
block = Curve(id, intersection.get_socket(socket_idx), global_network, id)
block.construct_block(test.render, test.world)
id += 1

intersection = InterSection(id, block.get_socket(0), global_network, 1)
intersection.construct_block(test.render, test.world)

test.show_bounding_box(global_network)
test.run()
20 changes: 0 additions & 20 deletions metadrive/tests/vis_block/vis_no_render.py

This file was deleted.

7 changes: 3 additions & 4 deletions metadrive/tests/vis_block/vis_out_ramp.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""This file visualizes a OutRampOnStraight block. Use mouse left button to draw down for zooming out."""
from metadrive.component.pgblock.first_block import FirstPGBlock
from metadrive.component.pgblock.ramp import OutRampOnStraight
from metadrive.component.road_network.node_road_network import NodeRoadNetwork
Expand All @@ -6,14 +7,12 @@

if __name__ == "__main__":
test = TestBlock()

initialize_asset_loader(test)

initialize_asset_loader(engine=test)
global_network = NodeRoadNetwork()
straight = FirstPGBlock(global_network, 3.0, 1, test.render, test.world, 1)
for i in range(1, 3):
straight = OutRampOnStraight(i, straight.get_socket(0), global_network, i)
# print(straight.construct_block(test.render, test.world))
straight.construct_block(test.render, test.world)
# print(len(straight.dynamic_nodes))
test.show_bounding_box(global_network)
test.run()
1 change: 1 addition & 0 deletions metadrive/tests/vis_block/vis_parking_lot.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""This file visualizes a ParkingLot block. Use mouse left button to draw down for zooming out."""
from metadrive.component.pgblock.first_block import FirstPGBlock
from metadrive.component.pgblock.parking_lot import ParkingLot
from metadrive.component.pgblock.std_intersection import StdInterSection
Expand Down
6 changes: 2 additions & 4 deletions metadrive/tests/vis_block/vis_roundabout.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""This file visualizes a Roundabout block."""
from metadrive.component.pgblock.curve import Curve
from metadrive.component.pgblock.first_block import FirstPGBlock
from metadrive.component.pgblock.roundabout import Roundabout
Expand All @@ -9,13 +10,10 @@
from metadrive.engine.asset_loader import initialize_asset_loader

initialize_asset_loader(test)

global_network = NodeRoadNetwork()
straight = FirstPGBlock(global_network, 3.0, 1, test.render, test.world, 1)

rd = Roundabout(1, straight.get_socket(0), global_network, 1)
# print(rd.construct_block(test.render, test.world))

rd.construct_block(test.render, test.world)
id = 4
for socket_idx in range(rd.SOCKET_NUM):
block = Curve(id, rd.get_socket(socket_idx), global_network, id + 1)
Expand Down
6 changes: 2 additions & 4 deletions metadrive/tests/vis_block/vis_std_intersection.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""This file visualizes a StdInterSection block and a StdTInterSection block."""
from metadrive.component.pgblock.first_block import FirstPGBlock
from metadrive.component.pgblock.std_intersection import StdInterSection
from metadrive.component.pgblock.std_t_intersection import StdTInterSection
Expand All @@ -9,13 +10,10 @@
from metadrive.engine.asset_loader import initialize_asset_loader

initialize_asset_loader(test)

global_network = NodeRoadNetwork()
first = FirstPGBlock(global_network, 3.0, 1, test.render, test.world, 1)

intersection = StdInterSection(3, first.get_socket(0), global_network, 1)
# print(intersection.construct_block(test.render, test.world))

intersection.construct_block(test.render, test.world)
id = 4
for socket_idx in range(intersection.SOCKET_NUM):
block = StdTInterSection(id, intersection.get_socket(socket_idx), global_network, id)
Expand Down
7 changes: 2 additions & 5 deletions metadrive/tests/vis_block/vis_std_t_intersection.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""This file visualizes a StdTInterSection block."""
from metadrive.component.pgblock.curve import Curve
from metadrive.component.pgblock.first_block import FirstPGBlock
from metadrive.component.pgblock.std_t_intersection import StdTInterSection
Expand All @@ -10,18 +11,14 @@
from metadrive.engine.asset_loader import initialize_asset_loader

initialize_asset_loader(test)

global_network = NodeRoadNetwork()
first = FirstPGBlock(global_network, 3.0, 2, test.render, test.world, 1)

curve = Curve(1, first.get_socket(0), global_network, 1)
curve.construct_block(test.render, test.world)

straight = Straight(2, curve.get_socket(0), global_network, 1)
straight.construct_block(test.render, test.world)

intersection = StdTInterSection(3, straight.get_socket(0), global_network, 1)
# print(intersection.construct_block(test.render, test.world))
intersection.construct_block(test.render, test.world)
id = 4
for socket_idx in range(intersection.SOCKET_NUM):
block = Curve(id, intersection.get_socket(socket_idx), global_network, id + 1)
Expand Down
4 changes: 1 addition & 3 deletions metadrive/tests/vis_block/vis_straight_block.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""This file visualizes a Straight block."""
from metadrive.component.pgblock.first_block import FirstPGBlock
from metadrive.component.pgblock.straight import Straight
from metadrive.component.road_network.node_road_network import NodeRoadNetwork
Expand All @@ -6,14 +7,11 @@

if __name__ == "__main__":
test = TestBlock()

initialize_asset_loader(test)

global_network = NodeRoadNetwork()
straight = FirstPGBlock(global_network, 3.0, 1, test.render, test.world, 1)
for i in range(1, 3):
straight = Straight(i, straight.get_socket(0), global_network, i)
straight.construct_block(test.render, test.world)
# print(len(straight.dynamic_nodes))
test.show_bounding_box(global_network)
test.run()
6 changes: 2 additions & 4 deletions metadrive/tests/vis_block/vis_t_intersection.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""This file visualizes a TInterSection block."""
from metadrive.component.pgblock.curve import Curve
from metadrive.component.pgblock.first_block import FirstPGBlock
from metadrive.component.pgblock.straight import Straight
Expand All @@ -13,15 +14,12 @@

global_network = NodeRoadNetwork()
first = FirstPGBlock(global_network, 3.0, 2, test.render, test.world, 1)

curve = Curve(1, first.get_socket(0), global_network, 1)
curve.construct_block(test.render, test.world)

straight = Straight(2, curve.get_socket(0), global_network, 1)
straight.construct_block(test.render, test.world)

intersection = TInterSection(3, straight.get_socket(0), global_network, 20)
# print(intersection.construct_block(test.render, test.world))
intersection.construct_block(test.render, test.world)
id = 4
for socket_idx in range(intersection.SOCKET_NUM):
block = Curve(id, intersection.get_socket(socket_idx), global_network, id + 1)
Expand Down
4 changes: 4 additions & 0 deletions metadrive/tests/vis_block/vis_yy.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
"""
This file visualizes a map with Merge block and a Split block, which is similar to a Bottleneck block.
This environment is interactive.
"""
from metadrive.component.map.base_map import BaseMap
from metadrive.component.map.pg_map import MapGenerateMethod
from metadrive.envs.metadrive_env import MetaDriveEnv
Expand Down

0 comments on commit 13c934b

Please sign in to comment.