diff --git a/examples/example02.py b/examples/example02.py index 64ff761..1336dc9 100644 --- a/examples/example02.py +++ b/examples/example02.py @@ -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): diff --git a/examples/example03.py b/examples/example03.py index a76cb86..9965c2e 100644 --- a/examples/example03.py +++ b/examples/example03.py @@ -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() diff --git a/maps/default_map.json b/maps/default_map.json new file mode 100644 index 0000000..583c060 --- /dev/null +++ b/maps/default_map.json @@ -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] + ] + } + ] +} diff --git a/pysocialforce/__init__.py b/pysocialforce/__init__.py index 328e324..5b95a6a 100644 --- a/pysocialforce/__init__.py +++ b/pysocialforce/__init__.py @@ -15,3 +15,4 @@ from .map_config import \ Circle, Line2D, Rect, Zone, Vec2D, \ GlobalRoute, Obstacle, MapDefinition +from .map_loader import load_map diff --git a/pysocialforce/map_loader.py b/pysocialforce/map_loader.py new file mode 100644 index 0000000..63ca174 --- /dev/null +++ b/pysocialforce/map_loader.py @@ -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)