Skip to content

Commit d70e124

Browse files
Add parsing of wall graph
Signed-off-by: Luca Della Vedova <[email protected]>
1 parent c085ffd commit d70e124

File tree

1 file changed

+43
-23
lines changed
  • rmf_building_map_tools/building_map_server

1 file changed

+43
-23
lines changed

rmf_building_map_tools/building_map_server/site.py

+43-23
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ def load_site_json(map_path):
2929
lift = parse_lift(lift_data, site)
3030
if lift is not None:
3131
map_msg.lifts.append(lift)
32-
print(map_msg.lifts)
3332
return map_msg
3433

3534
def parse_angle(angle):
@@ -38,6 +37,14 @@ def parse_angle(angle):
3837
else:
3938
return math.pi * angle["deg"] / 180.0
4039

40+
def parse_anchor(anchor):
41+
if anchor is None:
42+
return None
43+
if "Translate2D" in anchor:
44+
return anchor["Translate2D"]
45+
else:
46+
return None
47+
4148
def parse_lift(data, site):
4249
lift_msg = Lift()
4350
props = data["properties"]
@@ -53,17 +60,11 @@ def parse_lift(data, site):
5360
center_y = 0.0
5461
left_anchor = site["anchors"].get(str(props["reference_anchors"][0]))
5562
right_anchor = site["anchors"].get(str(props["reference_anchors"][1]))
63+
left_anchor = parse_anchor(left_anchor)
64+
right_anchor = parse_anchor(right_anchor)
5665
if left_anchor is None or right_anchor is None:
5766
# ERROR
5867
return None
59-
if "Translate2D" in left_anchor:
60-
left_anchor = left_anchor["Translate2D"]
61-
else:
62-
return None
63-
if "Translate2D" in right_anchor:
64-
right_anchor = right_anchor["Translate2D"]
65-
else:
66-
return None
6768
lift_msg.ref_yaw = math.atan2(left_anchor[1] - right_anchor[1], left_anchor[0] - right_anchor[0])
6869
midpoint_x = (left_anchor[0] + right_anchor[0]) / 2.0
6970
midpoint_y = (left_anchor[1] + right_anchor[1]) / 2.0
@@ -72,6 +73,31 @@ def parse_lift(data, site):
7273
# TODO(luca) cabin doors
7374
return lift_msg
7475

76+
def generate_wall_graph(data):
77+
# Start by getting all the vertices that are used
78+
graph = Graph()
79+
graph.name = "wall_graph"
80+
site_id_to_wall_idx = {}
81+
for wall in data["walls"].values():
82+
for anchor_id in wall["anchors"]:
83+
if anchor_id not in site_id_to_wall_idx:
84+
anchor = data["anchors"].get(str(anchor_id), None)
85+
anchor = parse_anchor(anchor)
86+
if anchor is None:
87+
return None
88+
node = GraphNode()
89+
node.x = anchor[0]
90+
node.y = anchor[1]
91+
site_id_to_wall_idx[anchor_id] = len(graph.vertices)
92+
# We don't need other properties for wall graphs
93+
graph.vertices.append(node)
94+
edge = GraphEdge()
95+
edge.edge_type = GraphEdge.EDGE_TYPE_BIDIRECTIONAL
96+
edge.v1_idx = site_id_to_wall_idx[wall["anchors"][0]]
97+
edge.v2_idx = site_id_to_wall_idx[wall["anchors"][1]]
98+
graph.edges.append(edge)
99+
return graph
100+
75101
def parse_level(map_dir, data):
76102
level_msg = Level()
77103
level_msg.name = data["properties"]["name"]
@@ -108,16 +134,9 @@ def parse_level(map_dir, data):
108134
door_msg = Door()
109135
door_msg.name = door["name"]
110136

111-
v1 = data["anchors"].get(str(door["anchors"][0]))
112-
v2 = data["anchors"].get(str(door["anchors"][1]))
113-
if "Translate2D" in v1:
114-
v1 = v1["Translate2D"]
115-
else:
116-
# LOG ERROR
117-
continue
118-
if "Translate2D" in v2:
119-
v2 = v2["Translate2D"]
120-
else:
137+
v1 = parse_anchor(data["anchors"].get(str(door["anchors"][0])))
138+
v2 = parse_anchor(data["anchors"].get(str(door["anchors"][1])))
139+
if v1 is None or v2 is None:
121140
# LOG ERROR
122141
continue
123142
# TODO(luca) Change this based on pivot side
@@ -147,9 +166,10 @@ def parse_level(map_dir, data):
147166
pass
148167
level_msg.doors.append(door_msg)
149168

150-
# TODO(luca), needed for rmf-web frontend, populate
151-
for wall in data.get("walls", {}).values():
152-
pass
153-
169+
wall_graph = generate_wall_graph(data)
170+
if wall_graph is not None:
171+
level_msg.wall_graph = wall_graph
172+
else:
173+
print("Failed generating wall graph, skipping it...")
154174

155175
return level_msg

0 commit comments

Comments
 (0)