Skip to content

Commit

Permalink
add map loader and default map
Browse files Browse the repository at this point in the history
  • Loading branch information
Bonifatius94 committed Jan 6, 2024
1 parent e79b24b commit c1ac9a4
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 28 deletions.
15 changes: 1 addition & 14 deletions examples/example02.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,7 @@
"""
import pysocialforce as pysf

obstacle01 = pysf.Obstacle(
[(10, 10), (15,10), (15, 15), (10, 15)])
obstacle02 = pysf.Obstacle(
[(20, 10), (25,10), (25, 15), (20, 15)])

route01 = pysf.GlobalRoute(
[(0, 0), (10, 10), (20, 10), (30, 0)])
crowded_zone01 = ((10, 10), (20, 10), (20, 20))

map_def = pysf.MapDefinition(
obstacles=[obstacle01, obstacle02],
routes=[route01],
crowded_zones=[crowded_zone01])

map_def = pysf.load_map("./maps/default_map.json")
simulator = pysf.Simulator_v2(map_def)

for step in range(10):
Expand Down
15 changes: 1 addition & 14 deletions examples/example03.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,7 @@
import pysocialforce as pysf
import numpy as np

obstacle01 = pysf.Obstacle(
[(10, 10), (15,10), (15, 15), (10, 15)])
obstacle02 = pysf.Obstacle(
[(20, 10), (25,10), (25, 15), (20, 15)])

route01 = pysf.GlobalRoute(
[(0, 0), (10, 10), (20, 10), (30, 0)])
crowded_zone01 = ((10, 10), (20, 10), (20, 20))

map_def = pysf.MapDefinition(
obstacles=[obstacle01, obstacle02],
routes=[route01],
crowded_zones=[crowded_zone01])

map_def = pysf.load_map("./maps/default_map.json")
simulator = pysf.Simulator_v2(map_def)
sim_view = pysf.SimulationView(obstacles=map_def.obstacles, scaling=10)
sim_view.show()
Expand Down
42 changes: 42 additions & 0 deletions maps/default_map.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"version": "1.0",
"name": "default_map",
"bounds": {
"x_margin": [-100, 100],
"y_margin": [-200, 200]
},
"ped_routes": [
{
"name": "route_1",
"waypoints": [
[0, 0],
[0, 100],
[100, 100],
[100, 0],
[0, 0]
],
"reversible": "true"
}
],
"crowded_zones": [
{
"name": "zone_1",
"zone_rect": [
[40, -40],
[40, -20],
[60, -20]
]
}
],
"obstacles": [
{
"name": "obstacle_1",
"vertices": [
[20, 20],
[20, 40],
[40, 40],
[40, 20]
]
}
]
}
1 change: 1 addition & 0 deletions pysocialforce/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@
from .map_config import \
Circle, Line2D, Rect, Zone, Vec2D, \
GlobalRoute, Obstacle, MapDefinition
from .map_loader import load_map
18 changes: 18 additions & 0 deletions pysocialforce/map_loader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import json
from pysocialforce.map_config import \
MapDefinition, GlobalRoute, Obstacle


def load_map(file_path: str) -> MapDefinition:
"""Load map data from the given file path."""

with open(file_path, 'r') as file:
map_json = json.load(file)

obstacles = [Obstacle(o["vertices"]) for o in map_json['obstacles']]
routes = [GlobalRoute(r['waypoints']) for r in map_json['ped_routes']]
routes += [GlobalRoute(list(reversed(r['waypoints'])))
for r in map_json['ped_routes'] if r["reversible"]]
crowded_zones = [tuple(z["zone_rect"]) for z in map_json['crowded_zones']]

return MapDefinition(obstacles, routes, crowded_zones)

0 comments on commit c1ac9a4

Please sign in to comment.