team-3/src/c3nav/mapdata/render/renderer.py

173 lines
9.1 KiB
Python
Raw Normal View History

from django.utils.functional import cached_property
from shapely import prepared
2017-10-10 17:49:53 +02:00
from shapely.geometry import box
from c3nav.mapdata.models import Level
2017-11-05 12:32:07 +01:00
from c3nav.mapdata.render.engines.base import FillAttribs, StrokeAttribs
from c3nav.mapdata.render.geometry import hybrid_union
from c3nav.mapdata.render.renderdata import LevelRenderData
2024-01-06 13:26:34 +01:00
from c3nav.mapdata.render.theme import ColorManager
2018-12-03 23:39:28 +01:00
from c3nav.mapdata.render.utils import get_full_levels, get_min_altitude
from c3nav.mapdata.utils.color import color_to_rgb, rgb_to_color
2017-10-10 14:39:11 +02:00
2023-12-11 18:47:48 +01:00
2017-11-06 11:18:45 +01:00
class MapRenderer:
2018-12-06 21:00:55 +01:00
def __init__(self, level, minx, miny, maxx, maxy, scale=1, access_permissions=None, full_levels=False,
min_width=None):
self.level = level.pk if isinstance(level, Level) else level
self.minx = minx
2017-10-29 11:32:44 +01:00
self.miny = miny
self.maxx = maxx
2017-10-29 11:32:44 +01:00
self.maxy = maxy
self.scale = scale
self.access_permissions = set(access_permissions) if access_permissions else set()
2017-11-09 23:25:08 +01:00
self.full_levels = full_levels
2018-12-06 22:23:42 +01:00
self.min_width = min_width/self.scale if min_width else None
2017-10-10 14:39:11 +02:00
self.width = int(round((maxx - minx) * scale))
self.height = int(round((maxy - miny) * scale))
@cached_property
def bbox(self):
return box(self.minx-1, self.miny-1, self.maxx+1, self.maxy+1)
2017-10-10 17:49:53 +02:00
2024-01-06 13:26:34 +01:00
def render(self, engine_cls, theme, center=True):
color_manager = ColorManager.for_theme(theme)
2017-10-19 17:55:41 +02:00
# add no access restriction to “unlocked“ access restrictions so lookup gets easier
access_permissions = self.access_permissions | {None}
2017-11-08 17:52:27 +01:00
bbox = prepared.prep(self.bbox)
2024-01-06 13:26:34 +01:00
level_render_data = LevelRenderData.get(self.level, theme)
2017-12-05 18:13:06 +01:00
engine = engine_cls(self.width, self.height, self.minx, self.miny, float(level_render_data.base_altitude),
2024-01-06 13:26:34 +01:00
scale=self.scale, buffer=1, background=color_manager.background,
2023-12-11 18:47:48 +01:00
center=center, min_width=self.min_width)
2017-12-05 18:13:06 +01:00
2018-12-03 23:39:28 +01:00
if hasattr(engine, 'custom_render'):
2018-12-06 17:48:50 +01:00
engine.custom_render(level_render_data, access_permissions, self.full_levels)
2018-12-03 23:39:28 +01:00
return engine
2017-11-09 23:25:08 +01:00
if self.full_levels:
2018-12-03 23:39:28 +01:00
levels = get_full_levels(level_render_data)
2017-11-09 23:25:08 +01:00
else:
levels = level_render_data.levels
2017-11-09 23:25:08 +01:00
2018-12-03 23:39:28 +01:00
min_altitude = get_min_altitude(levels, default=level_render_data.base_altitude)
2017-11-10 00:25:27 +01:00
not_full_levels = engine.is_3d # always do non-full-levels until after the first primary level
full_levels = self.full_levels and engine.is_3d
for geoms in levels:
2017-12-20 12:21:44 +01:00
engine.add_group('level_%s' % geoms.short_label)
if geoms.pk == level_render_data.lowest_important_level:
engine.darken(level_render_data.darken_area)
2017-11-08 17:52:27 +01:00
if not bbox.intersects(geoms.affected_area):
continue
2017-10-19 17:55:41 +02:00
# hide indoor and outdoor rooms if their access restriction was not unlocked
2017-11-08 17:52:27 +01:00
add_walls = hybrid_union(tuple(area for access_restriction, area in geoms.restricted_spaces_indoors.items()
if access_restriction not in access_permissions))
2017-11-08 17:52:27 +01:00
crop_areas = hybrid_union(
tuple(area for access_restriction, area in geoms.restricted_spaces_outdoors.items()
if access_restriction not in access_permissions)
).union(add_walls)
if not_full_levels:
2024-01-06 13:26:34 +01:00
engine.add_geometry(geoms.walls_base, fill=FillAttribs(color_manager.wall_fill), category='walls')
engine.add_geometry(geoms.walls_bottom.fit(scale=geoms.min_altitude-min_altitude,
offset=min_altitude-int(0.7*1000)),
2024-01-06 13:26:34 +01:00
fill=FillAttribs(color_manager.wall_fill), category='walls')
for i, altitudearea in enumerate(geoms.altitudeareas):
base = altitudearea.base.difference(crop_areas)
bottom = altitudearea.bottom.difference(crop_areas)
2024-01-06 13:26:34 +01:00
engine.add_geometry(base, fill=FillAttribs(color_manager.ground_fill), category='ground', item=i)
engine.add_geometry(bottom.fit(scale=geoms.min_altitude - min_altitude,
offset=min_altitude - int(0.7 * 1000)),
2024-01-06 13:26:34 +01:00
fill=FillAttribs(color_manager.wall_fill), category='ground')
2017-11-10 00:25:27 +01:00
2017-10-19 17:55:41 +02:00
# render altitude areas in default ground color and add ground colors to each one afterwards
# shadows are directly calculated and added by the engine
for i, altitudearea in enumerate(geoms.altitudeareas):
geometry = altitudearea.geometry.difference(crop_areas)
if not_full_levels:
geometry = geometry.filter(bottom=False)
2023-12-11 19:02:19 +01:00
engine.add_geometry(geometry, altitude=altitudearea.altitude,
2024-01-06 13:26:34 +01:00
fill=FillAttribs(color_manager.ground_fill), category='ground', item=i)
j = 0
2018-12-21 19:06:29 +01:00
for (order, color), areas in altitudearea.colors.items():
2017-10-19 17:55:41 +02:00
# only select ground colors if their access restriction is unlocked
areas = tuple(area for access_restriction, area in areas.items()
if access_restriction in access_permissions)
if areas:
j += 1
2017-11-26 13:10:31 +01:00
hexcolor = ''.join(hex(int(i*255))[2:].zfill(2) for i in engine.color_to_rgb(color)).upper()
engine.add_geometry(hybrid_union(areas), fill=FillAttribs(color),
2017-11-26 13:13:20 +01:00
category='ground_%s' % hexcolor, item=j)
2023-12-14 20:07:25 +01:00
# add obstacles after everything related to ground for the nice right order
for i, altitudearea in enumerate(geoms.altitudeareas):
for height, height_obstacles in altitudearea.obstacles.items():
for color, color_obstacles in height_obstacles.items():
for obstacle in color_obstacles:
obstacle_geom = obstacle.difference(crop_areas)
2023-12-14 20:07:25 +01:00
if color:
fill_rgb = color_to_rgb(color)
stroke_color = rgb_to_color((*((0.75*i) for i in fill_rgb[:3]), fill_rgb[3]))
engine.add_geometry(
obstacle_geom,
2023-12-14 20:07:25 +01:00
fill=FillAttribs(color),
stroke=StrokeAttribs(stroke_color, 0.05, min_px=0.2),
category='obstacles'
)
else:
engine.add_geometry(
obstacle_geom,
2024-01-06 13:26:34 +01:00
fill=FillAttribs(color_manager.obstacles_default_fill),
stroke=StrokeAttribs(color_manager.obstacles_default_border, 0.05, min_px=0.2),
2023-12-14 20:07:25 +01:00
category='obstacles'
)
2017-11-14 22:18:53 +01:00
2017-10-19 17:55:41 +02:00
# add walls, stroke_px makes sure that all walls are at least 1px thick on all zoom levels,
2017-10-29 09:32:15 +01:00
walls = None
2023-12-18 00:19:17 +01:00
# we use all_walls instead of walls, because the short wall rendering stuff doesn't work
if not add_walls.is_empty or not geoms.all_walls.is_empty:
walls = geoms.all_walls.union(add_walls)
2017-10-29 09:32:15 +01:00
walls_extended = geoms.walls_extended and full_levels
2017-10-29 09:32:15 +01:00
if walls is not None:
2023-12-11 18:47:48 +01:00
engine.add_geometry(
walls.filter(bottom=not not_full_levels,
top=not walls_extended),
height=geoms.default_height, shadow_color=color_manager.wall_border, fill=FillAttribs(color_manager.wall_fill), category='walls'
2023-12-11 18:47:48 +01:00
)
for short_wall in geoms.short_walls:
engine.add_geometry(short_wall.filter(bottom=not not_full_levels),
2024-01-06 13:26:34 +01:00
fill=FillAttribs(color_manager.wall_fill), category='walls')
if walls_extended:
2024-01-06 13:26:34 +01:00
engine.add_geometry(geoms.walls_extended, fill=FillAttribs(color_manager.wall_fill), category='walls')
doors_extended = geoms.doors_extended and full_levels
if not geoms.doors.is_empty:
engine.add_geometry(geoms.doors.difference(add_walls).filter(top=not doors_extended),
2024-01-06 13:26:34 +01:00
fill=FillAttribs(color_manager.door_fill),
stroke=StrokeAttribs(color_manager.door_fill, 0.05, min_px=0.2),
category='doors')
if doors_extended:
2024-01-06 13:26:34 +01:00
engine.add_geometry(geoms.doors_extended, fill=FillAttribs(color_manager.wall_fill), category='doors')
2017-10-29 09:32:15 +01:00
if walls is not None:
2023-12-11 18:47:48 +01:00
engine.add_geometry(walls,
2024-01-06 13:26:34 +01:00
stroke=StrokeAttribs(color_manager.wall_border, 0.1, min_px=1),
2023-12-11 18:47:48 +01:00
category='walls')
if geoms.on_top_of_id is None:
not_full_levels = not self.full_levels and engine.is_3d
2017-11-06 11:18:45 +01:00
return engine