team-3/src/c3nav/routing/route.py

88 lines
2.6 KiB
Python
Raw Normal View History

2016-12-17 13:24:42 +01:00
import numpy as np
from django.utils.functional import cached_property
from c3nav.mapdata.utils.misc import get_dimensions
2016-12-17 13:24:42 +01:00
2016-12-17 14:46:15 +01:00
class Route:
def __init__(self, connections, distance=None):
self.connections = tuple(connections)
self.distance = sum(connection.distance for connection in self.connections)
self.from_point = connections[0].from_point
self.to_point = connections[-1].to_point
2016-12-17 14:46:15 +01:00
2016-12-17 13:24:42 +01:00
def __repr__(self):
return ('<Route (\n %s\n) distance=%f>' %
('\n '.join(repr(connection) for connection in self.connections), self.distance))
2016-12-17 13:24:42 +01:00
@cached_property
def routeparts(self):
routeparts = []
connections = []
level = self.connections[0].from_point.level
for connection in self.connections:
connections.append(connection)
point = connection.to_point
if point.level and point.level != level:
routeparts.append(RoutePart(level, connections))
level = point.level
connections = []
if connections:
routeparts.append(RoutePart(level, connections))
print(routeparts)
return tuple(routeparts)
class RoutePart:
def __init__(self, level, connections):
2016-12-17 13:24:42 +01:00
self.level = level
self.level_name = level.level.name
self.connections = connections
2016-12-17 14:46:15 +01:00
width, height = get_dimensions()
2016-12-17 14:46:15 +01:00
points = (connections[0].from_point, ) + tuple(connection.to_point for connection in connections)
for point in points:
point.svg_x = point.x * 6
point.svg_y = (height - point.y) * 6
2016-12-17 13:24:42 +01:00
x, y = zip(*((point.svg_x, point.svg_y) for point in points if point.level == level))
2016-12-17 13:24:42 +01:00
self.distance = sum(connection.distance for connection in connections)
2016-12-17 13:24:42 +01:00
# bounds for rendering
self.min_x = min(x) - 20
self.max_x = max(x) + 20
self.min_y = min(y) - 20
self.max_y = max(y) + 20
2016-12-17 13:24:42 +01:00
width = self.max_x - self.min_x
height = self.max_y - self.min_y
2016-12-17 14:46:15 +01:00
if width < 150:
self.min_x -= (10 - width) / 2
self.max_x += (10 - width) / 2
2016-12-17 14:46:15 +01:00
if height < 150:
self.min_y -= (10 - height) / 2
self.max_y += (10 - height) / 2
2016-12-17 14:46:15 +01:00
self.width = self.max_x - self.min_x
self.height = self.max_y - self.min_y
def __str__(self):
return repr(self.__dict__)
class RouteLine:
def __init__(self, from_point, to_point, distance):
self.from_point = from_point
self.to_point = to_point
self.distance = distance
2016-12-17 14:46:15 +01:00
2016-12-17 13:24:42 +01:00
class NoRoute:
distance = np.inf