diff --git a/src/c3nav/routing/area.py b/src/c3nav/routing/area.py index 9e3f103e..8a270ded 100644 --- a/src/c3nav/routing/area.py +++ b/src/c3nav/routing/area.py @@ -30,69 +30,69 @@ class GraphArea(): def build_connections(self): for point1, point2 in combinations(self._built_points, 2): - path = Path(np.vstack((point1.xy, point2.xy))) - # lies within room - if self.mpl_clear.intersects_path(path): + there, back = self.check_connection(point1, point2) + + if there is not None: + point1.connect_to(point2, ctype=there) + + if back is not None: + point2.connect_to(point1, ctype=back) + + def check_connection(self, point1, point2): + path = Path(np.vstack((point1.xy, point2.xy))) + + # lies within room + if self.mpl_clear.intersects_path(path): + return None, None + + # stair checker + angle = coord_angle(point1.xy, point2.xy) + stair_direction_up = None + for stair_path, stair_angle in self.mpl_stairs: + if not path.intersects_path(stair_path): continue - # stair checker - angle = coord_angle(point1.xy, point2.xy) - valid = True - stair_direction_up = None - for stair_path, stair_angle in self.mpl_stairs: - if not path.intersects_path(stair_path): - continue + angle_diff = ((stair_angle - angle + 180) % 360) - 180 - angle_diff = ((stair_angle - angle + 180) % 360) - 180 + new_direction_up = (angle_diff > 0) + if stair_direction_up is None: + stair_direction_up = new_direction_up + elif stair_direction_up != new_direction_up: + return None, None - new_direction_up = (angle_diff > 0) - if stair_direction_up is None: - stair_direction_up = new_direction_up - elif stair_direction_up != new_direction_up: - valid = False - break + if not (40 < abs(angle_diff) < 150): + return None, None - if not (40 < abs(angle_diff) < 150): - valid = False - break - - if not valid: + # escalator checker + angle = coord_angle(point1.xy, point2.xy) + escalator_direction_up = None + escalator_swap_direction = False + for escalator in self.escalators: + if not escalator.mpl_geom.intersects_path(path, filled=True): continue - # escalator checker - angle = coord_angle(point1.xy, point2.xy) - valid = True - escalator_direction_up = None - escalator_swap_direction = False - for escalator in self.escalators: - if not escalator.mpl_geom.intersects_path(path, filled=True): - continue + if escalator_direction_up is not None: + # only one escalator per connection + return None, None - if escalator_direction_up is not None: - # only one escalator per connection - valid = False - break + angle_diff = ((escalator.angle - angle + 180) % 360) - 180 - angle_diff = ((escalator.angle - angle + 180) % 360) - 180 + escalator_direction_up = (angle_diff > 0) + escalator_swap_direction = (escalator_direction_up != escalator.direction_up) - escalator_direction_up = (angle_diff > 0) - escalator_swap_direction = (escalator_direction_up != escalator.direction_up) - - if not valid: - continue - - if stair_direction_up is not None: - point1.connect_to(point2, ctype=('stairs_up' if stair_direction_up else 'stairs_down')) - point2.connect_to(point1, ctype=('stairs_down' if stair_direction_up else 'stairs_up')) - elif escalator_direction_up is not None: - if not escalator_swap_direction: - point1.connect_to(point2, ctype=('escalator_up' if escalator_direction_up else 'escalator_down')) - else: - point2.connect_to(point1, ctype=('escalator_down' if escalator_direction_up else 'escalator_up')) + if stair_direction_up is not None: + return ( + ('stairs_up' if stair_direction_up else 'stairs_down'), + ('stairs_down' if stair_direction_up else 'stairs_up'), + ) + elif escalator_direction_up is not None: + if not escalator_swap_direction: + return ('escalator_up' if escalator_direction_up else 'escalator_down'), None else: - point1.connect_to(point2) - point2.connect_to(point1) + return None, ('escalator_down' if escalator_direction_up else 'escalator_up') + + return '', '' def add_point(self, point): if not self.mpl_clear.contains_point(point.xy):