Files
Dreierpack-Manager/src/modern/ui/stadium_model.hpp
T
MichaelandClaude Fable 5.2 b2500eb7b6 The surroundings on a plot of their own; the stadium views fill the window
The surroundings no longer show the ground: their fourteen objects stand along
an avenue on their own plot. Both views use the whole width of the window; the
lists on the right are gone, the pointer names a part and its state, and the
card of a chosen part lies over the picture.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-15 08:44:54 +01:00

519 lines
31 KiB
C++

// SPDX-License-Identifier: GPL-3.0-only
#pragma once
// The stadium and its surroundings as a card model: bodies built from the blocks,
// facilities and periphery of the stadium overview, seen through a camera, sorted by depth
// and drawn as a vector picture. No renderer is involved: the same picture comes out on
// every platform, and the same input always gives the same picture.
//
// World: x east, y north, z up, the centre spot at the origin; lengths roughly in metres.
// Camera: an axonometric view (no perspective) turned by `yaw` around the vertical axis and
// raised by `pitch` above the ground, both in degrees.
#include "modern/ui/catalog.hpp"
#include <algorithm>
#include <array>
#include <cmath>
#include <map>
#include <sstream>
#include <string>
#include <vector>
namespace bmh::modern_ui::stadium3d {
struct Vec { double x = 0, y = 0, z = 0; };
struct Camera { double yaw = 28, pitch = 40, zoom = 1, tx = 0, ty = 0; };
struct Face {
std::vector<Vec> points;
std::string fill; // "#rrggbb"; empty for an outline
std::string part; // the selectable part it belongs to, e.g. "block:3"
bool dashed = false, open = false, flat = false; // flat: keep the fill as it is (no shading)
double stroke = 2, opacity = 1;
std::string ink; // stroke colour; empty: the scene's ink
std::vector<std::vector<Vec>> lines; // drawn on the face, in its ink at 40 %
double bias = 0; // moves the face towards the viewer in the depth order
};
struct Part { std::string id; Vec centre; double x0 = 0, y0 = 0, x1 = 0, y1 = 0, cx = 0, cy = 0; bool seen = false; };
struct Anchor { std::string id; double x = 0, y = 0; };
// centres: where a part's name stands and the camera flies to; anchors: every spot a click
// may choose the part at (the four masts of the floodlights are four anchors of one part)
struct Scene { std::vector<Face> faces; std::map<std::string, Vec> centres; std::vector<std::pair<std::string, Vec>> anchors; std::string ink = "#3d362a", accent = "#25617a"; };
struct Drawing { std::string svg; std::vector<Part> parts; std::vector<Anchor> anchors; };
inline std::string num(double value) {
std::ostringstream out;
out.imbue(std::locale::classic());
out << std::round(value * 10) / 10;
return out.str();
}
inline std::string shade(const std::string &colour, double factor) {
if (colour.size() != 7 || colour[0] != '#') return colour;
std::string out = "#";
for (size_t at = 1; at < 7; at += 2) {
const int value = std::clamp(int(std::lround(std::stoi(colour.substr(at, 2), nullptr, 16) * factor)), 0, 255);
static constexpr char digits[] = "0123456789abcdef";
out += digits[value / 16];
out += digits[value % 16];
}
return out;
}
inline Camera mix(const Camera &a, const Camera &b, double t) {
const double s = t * t * (3 - 2 * t); // ease in and out
double turn = std::fmod(b.yaw - a.yaw, 360.0);
if (turn > 180) turn -= 360;
if (turn < -180) turn += 360;
return {a.yaw + turn * s, a.pitch + (b.pitch - a.pitch) * s, a.zoom + (b.zoom - a.zoom) * s, a.tx + (b.tx - a.tx) * s, a.ty + (b.ty - a.ty) * s};
}
// The eight stands lie on a ring around the pitch; angles in degrees, counted from the east
// towards the south (as on the plan: A north-west, B north, ... H west).
constexpr std::array<std::array<double, 2>, 8> spans{{{{205, 245}}, {{245, 295}}, {{295, 335}}, {{-25, 25}}, {{25, 65}}, {{65, 115}}, {{115, 155}}, {{155, 205}}}};
inline Vec ring(double degrees, double offset, double z) {
const double a = degrees * 3.14159265358979 / 180;
return {(74 + offset) * std::cos(a), -(52 + offset) * std::sin(a), z};
}
inline double stand_depth(int64_t places, int64_t greatest) { return 12 + 44.0 * double(std::max<int64_t>(0, places)) / double(std::max<int64_t>(1, greatest)); }
inline void box(Scene &scene, const std::string &part, double x, double y, double w, double d, double h, const std::string &fill, bool planned = false, double z = 0) {
const Vec a{x - w / 2, y - d / 2, z}, b{x + w / 2, y - d / 2, z}, c{x + w / 2, y + d / 2, z}, e{x - w / 2, y + d / 2, z};
if (planned) { // not built: its footprint only
scene.faces.push_back({{a, b, c, e}, "", part, true, false, false, 2.5, .8, "", {}, 2});
return;
}
const auto up = [&](Vec v) { v.z = z + h; return v; };
for (const auto &[p, q] : std::array<std::pair<Vec, Vec>, 4>{{{a, b}, {b, c}, {c, e}, {e, a}}})
scene.faces.push_back({{p, q, up(q), up(p)}, fill, part});
scene.faces.push_back({{up(a), up(b), up(c), up(e)}, fill, part});
}
// Builds the model. `seats` is the club's colour; `paper` selects the light skin's tones.
inline Scene build(const Json &overview, const std::string &seats, bool paper, int mode = 0) {
Scene scene;
scene.ink = paper ? "#3d362a" : "#d5dde3";
scene.accent = paper ? "#25617a" : "#9ac7d8";
const std::string plate = paper ? "#efe4c8" : "#27323c", wall = paper ? "#f6efdc" : "#55616c", terrace = paper ? "#d6ccb4" : "#6a7680",
roof = paper ? "#5a646e" : "#161d24", glass = "#9fc4d6", gold = "#c9962e", lamp = "#f3d778";
const auto level_of = [](const Json &row) { return row.value("display_level", row.value("level", 0)); };
const auto building = [](const Json &row) { return row.value("building", false); };
// the plate everything stands on
// inside, the plate runs beyond the picture; around, it is the plot with its edge
const double px = mode == 1 ? 262 : 900, py = mode == 1 ? 208 : 900;
scene.faces.push_back({{{-px, -py, 0}, {px, -py, 0}, {px, py, 0}, {-px, py, 0}}, plate, "", false, false, true, 3, 1, "", {}, -1000});
// the pitch: its colour tells the state of the turf (facility 4)
const auto &facilities = overview.value("facilities", Json::array());
const auto facility = [&](size_t index) -> const Json & { static const Json none = Json::object(); return index < facilities.size() ? facilities[index] : none; };
const int turf = facilities.size() > 4 ? level_of(facility(4)) : 3;
const std::array<std::array<const char *, 2>, 5> greens{{{"#a39a5c", "#b3a868"}, {"#8f9e58", "#9dab62"}, {"#6fa25c", "#7bae66"}, {"#4f9a5c", "#57a765"}, {"#3f9456", "#49a260"}}};
const auto &green = greens[size_t(std::clamp(turf, 0, 4))];
{
std::vector<Vec> oval;
for (int a = 0; a < 360; a += 10) oval.push_back(ring(a, -4, 0));
scene.faces.push_back({oval, paper ? "#e2d6b8" : "#323e49", "", false, false, true, 2.5, 1, "", {}, -900});
for (int stripe = 0; stripe < 10; ++stripe) {
const double x0 = -52.5 + stripe * 10.5;
Face f{{{x0, -34, 0}, {x0 + 10.5, -34, 0}, {x0 + 10.5, 34, 0}, {x0, 34, 0}}, green[size_t(stripe % 2)], "facility:4", false, false, true, 0, 1, "", {}, -800};
scene.faces.push_back(f);
}
Face lines{{{-52.5, -34, 0}, {52.5, -34, 0}, {52.5, 34, 0}, {-52.5, 34, 0}}, "", "facility:4", false, false, true, 3, 1, "#f4f7ef", {}, -700};
scene.faces.push_back(lines);
scene.faces.push_back({{{0, -34, 0}, {0, 34, 0}}, "", "", false, true, true, 3, 1, "#f4f7ef", {}, -700});
std::vector<Vec> circle;
for (int a = 0; a <= 360; a += 20) circle.push_back({9.15 * std::cos(a * 3.14159265 / 180), 9.15 * std::sin(a * 3.14159265 / 180), 0});
scene.faces.push_back({circle, "", "", false, true, true, 3, 1, "#f4f7ef", {}, -700});
for (const double side : {-1.0, 1.0})
scene.faces.push_back({{{side * 52.5, -20, 0}, {side * 36, -20, 0}, {side * 36, 20, 0}, {side * 52.5, 20, 0}}, "", "", false, true, true, 3, 1, "#f4f7ef", {}, -700});
// turf heating (facility 2): pipes under the grass show through
if (facilities.size() > 2 && level_of(facility(2)) > 1)
for (double y = -28; y <= 28; y += 8)
scene.faces.push_back({{{-48, y, 0}, {48, y, 0}}, "", "facility:2", true, true, true, 2.5, .7, "#c8553d", {}, -650});
scene.centres["facility:4"] = {0, 0, 0};
}
// the stands
const auto &blocks = overview.at("blocks");
int64_t greatest = 1;
for (const auto &b : blocks) greatest = std::max(greatest, b.at("max_places").get<int64_t>());
double deepest = 0;
for (size_t i = 0; i < 8 && i < blocks.size(); ++i) {
const auto &b = blocks[i];
const std::string part = "block:" + std::to_string(i);
const double from = spans[i][0] + 1.6, to = spans[i][1] - 1.6, inner = 5;
const double depth = stand_depth(b.at("display_places").get<int64_t>(), greatest), most = stand_depth(b.at("max_places").get<int64_t>(), greatest);
const double top = 3 + depth * .58, outer = inner + depth;
deepest = std::max(deepest, inner + most);
const bool seated = b.at("display_seats").get<bool>(), roofed = b.at("display_roof").get<bool>();
const std::string tread = seated ? seats : terrace;
const int steps = 4;
for (int s = 0; s < steps; ++s) {
const double a0 = from + (to - from) * s / steps, a1 = from + (to - from) * (s + 1) / steps;
Face slope{{ring(a0, inner, 2), ring(a1, inner, 2), ring(a1, outer, top), ring(a0, outer, top)}, tread, part};
for (int row = 1; row < 5; ++row) { // rows of seats, or the steps of a terrace
const double t = row / 5.0;
slope.lines.push_back({ring(a0, inner + depth * t, 2 + (top - 2) * t), ring(a1, inner + depth * t, 2 + (top - 2) * t)});
}
scene.faces.push_back(slope);
scene.faces.push_back({{ring(a0, outer, 0), ring(a1, outer, 0), ring(a1, outer, top), ring(a0, outer, top)}, wall, part});
scene.faces.push_back({{ring(a0, inner, 0), ring(a1, inner, 0), ring(a1, inner, 2), ring(a0, inner, 2)}, wall, part});
if (roofed) {
const double edge = inner + depth * .45, height = top + 7;
Face slab{{ring(a0, edge, height + 2), ring(a1, edge, height + 2), ring(a1, outer + 3, height), ring(a0, outer + 3, height)}, roof, part};
slab.bias = 6;
slab.lines.push_back({ring(a0, outer, top), ring(a0, outer + 1, height)}); // a post
scene.faces.push_back(slab);
}
}
for (const double a : {from, to})
scene.faces.push_back({{ring(a, inner, 0), ring(a, inner, 2), ring(a, outer, top), ring(a, outer, 0)}, wall, part});
// how far the stand may grow: its footprint as a dashed line
std::vector<Vec> limit;
for (int s = 0; s <= steps; ++s) limit.push_back(ring(from + (to - from) * s / steps, inner + most, 0));
limit.push_back(ring(to, outer, 0));
limit.insert(limit.begin(), ring(from, outer, 0));
scene.faces.push_back({limit, "", part, true, true, true, 2.5, .55, "", {}, 0});
if (building(b)) { // a frame of scaffolding at the size being built
const double target = inner + stand_depth(b.at("job").value("places", b.at("display_places").get<int64_t>()), greatest);
const double height = 3 + (target - inner) * .58;
for (int s = 0; s < steps; ++s) {
const double a0 = from + (to - from) * s / steps, a1 = from + (to - from) * (s + 1) / steps;
Face frame{{ring(a0, target + 2, 0), ring(a1, target + 2, 0), ring(a1, target + 2, height + 9), ring(a0, target + 2, height + 9)}, gold, part, true, false, true, 3, .28, gold, {}, 4};
scene.faces.push_back(frame);
}
}
scene.centres[part] = ring((from + to) / 2, inner + depth / 2, top / 2);
}
// facility 0: four floodlight masts, taller and brighter with every level
if (!facilities.empty()) {
const int level = level_of(facility(0));
for (const double a : {45.0, 135.0, 225.0, 315.0}) {
const Vec foot = ring(a, deepest + 16, 0);
const double height = 34 + 9 * level;
if (level <= 0) { box(scene, "facility:0", foot.x, foot.y, 8, 8, 1, wall, true); continue; }
scene.faces.push_back({{foot, {foot.x, foot.y, height}}, "", "facility:0", false, true, true, 5, 1, "", {}, 3});
const double w = 7 + 2 * level, h = 4 + 1.5 * level;
const double nx = -foot.y / std::hypot(foot.x, foot.y), ny = foot.x / std::hypot(foot.x, foot.y);
Face panel{{{foot.x - nx * w, foot.y - ny * w, height - h}, {foot.x + nx * w, foot.y + ny * w, height - h}, {foot.x + nx * w, foot.y + ny * w, height + h}, {foot.x - nx * w, foot.y - ny * w, height + h}},
lamp, "facility:0", false, false, true, 2.5, 1, "", {}, 5};
for (int row = 1; row <= level; ++row) panel.lines.push_back({{foot.x - nx * w, foot.y - ny * w, height - h + 2 * h * row / (level + 1)}, {foot.x + nx * w, foot.y + ny * w, height - h + 2 * h * row / (level + 1)}});
scene.faces.push_back(panel);
scene.anchors.push_back({"facility:0", {foot.x, foot.y, height}});
}
scene.centres["facility:0"] = ring(45, deepest + 16, 30);
}
// facility 1: the score board behind the east curve
if (facilities.size() > 1) {
const int level = level_of(facility(1));
const Vec at = ring(0, deepest + 14, 0);
if (level <= 0) box(scene, "facility:1", at.x, at.y, 6, 30, 1, wall, true);
else {
box(scene, "facility:1", at.x, at.y - 12, 2, 2, 14, wall);
box(scene, "facility:1", at.x, at.y + 12, 2, 2, 14, wall);
box(scene, "facility:1", at.x, at.y, 4, 26 + 4 * level, 8 + 3 * level, "#2c3640", false, 14);
}
scene.centres["facility:1"] = {at.x, at.y, 16};
}
// facility 3: the lounge, a glass box on the south stand
if (facilities.size() > 3 && blocks.size() > 5) {
const int level = level_of(facility(3)) - 1;
const Vec at = ring(90, 5 + stand_depth(blocks[5].at("display_places").get<int64_t>(), greatest) + 9, 0);
if (level <= 0) box(scene, "facility:3", at.x, at.y, 40, 10, 1, wall, true);
else box(scene, "facility:3", at.x, at.y, 34 + 8 * level, 10, 6 + 4 * level, glass);
scene.centres["facility:3"] = {at.x, at.y, 8};
}
// facilities 2, 5, 6, 7 as small houses beside the ground: boiler, kiosks, workshop, gate
const std::array<std::tuple<size_t, double, double, const char *>, 4> houses{{{2, -160, -66, "#c8a58a"}, {5, 140, -92, "#d9c38a"}, {6, -148, 72, "#b9b3a2"}, {7, 160, 82, "#a9b7a0"}}};
for (const auto &[index, x, y, colour] : houses) {
if (mode == 1) break;
if (index >= facilities.size()) continue;
const std::string part = "facility:" + std::to_string(index);
const int level = level_of(facility(index)) - (index == 2 || index == 5 ? 1 : 0);
if (level <= 0) box(scene, part, x, y, 26, 18, 1, wall, true);
else
for (int n = 0; n < std::min(level, 4); ++n) box(scene, part, x + (n - (std::min(level, 4) - 1) / 2.0) * 20, y, 16, 16, 7 + 2 * level, colour);
scene.centres[part] = {x, y, 6};
}
// The surroundings (mode 1): every object in its own shape and at its own place, drawn at
// its level. Level 0 is a dashed footprint, a level being built a dashed frame in gold.
if (mode == 1) {
// the surroundings stand on a plot of their own, without the ground: only the plate stays
scene.faces.resize(1);
scene.centres.clear();
scene.anchors.clear();
const std::string asphalt = paper ? "#b9b2a4" : "#3c444c", brick = "#c9a58c", plaster = "#efe6cf", tile = "#a8574f", slate = paper ? "#6b747d" : "#20272e",
wood = "#b98d5a", green = "#7fa86b", white = "#f7f4ea";
const auto &periphery = overview.value("periphery", Json::array());
const auto flat = [&](const std::string &part, double x, double y, double w, double d, const std::string &fill, double bias = -500) {
scene.faces.push_back({{{x - w / 2, y - d / 2, 0.2}, {x + w / 2, y - d / 2, 0.2}, {x + w / 2, y + d / 2, 0.2}, {x - w / 2, y + d / 2, 0.2}}, fill, part, false, false, true, 2, 1, "", {}, bias});
};
const auto stripe = [&](double x0, double y0, double x1, double y1, const std::string &ink, bool dashed = false) {
scene.faces.push_back({{{x0, y0, 0.3}, {x1, y1, 0.3}}, "", "", dashed, true, true, 2.2, 1, ink, {}, -450});
};
// a house with a pitched roof; the ridge runs along x
const auto gabled = [&](const std::string &part, double x, double y, double w, double d, double h, double rise, const std::string &walls, const std::string &roofing) {
box(scene, part, x, y, w, d, h, walls);
const Vec a{x - w / 2, y - d / 2, h}, b{x + w / 2, y - d / 2, h}, c{x + w / 2, y + d / 2, h}, e{x - w / 2, y + d / 2, h}, r0{x - w / 2, y, h + rise}, r1{x + w / 2, y, h + rise};
scene.faces.push_back({{a, b, r1, r0}, roofing, part, false, false, false, 2, 1, "", {}, 3});
scene.faces.push_back({{e, c, r1, r0}, roofing, part, false, false, false, 2, 1, "", {}, 3});
scene.faces.push_back({{a, e, r0}, walls, part, false, false, false, 2, 1, "", {}, 3});
scene.faces.push_back({{b, c, r1}, walls, part, false, false, false, 2, 1, "", {}, 3});
};
// windows as lines on the front (the side towards the viewer, lower y)
const auto windows = [&](const std::string &part, double x, double y, double w, double d, double z0, double z1, int count) {
Face glassy{{{x - w / 2 + 2, y - d / 2 - .2, z0}, {x + w / 2 - 2, y - d / 2 - .2, z0}, {x + w / 2 - 2, y - d / 2 - .2, z1}, {x - w / 2 + 2, y - d / 2 - .2, z1}}, glass, part, false, false, true, 1.5, 1, "", {}, 4};
for (int n = 1; n < count; ++n) glassy.lines.push_back({{x - w / 2 + 2 + (w - 4) * n / count, y - d / 2 - .2, z0}, {x - w / 2 + 2 + (w - 4) * n / count, y - d / 2 - .2, z1}});
scene.faces.push_back(glassy);
};
const auto tree = [&](double x, double y) {
scene.faces.push_back({{{x, y, 0}, {x, y, 5}}, "", "", false, true, true, 3, 1, "#6b5335", {}, 2});
box(scene, "", x, y, 7, 7, 8, green, false, 5);
};
const auto car = [&](const std::string &part, double x, double y, int tone) {
static constexpr std::array<const char *, 5> paints{"#c8553d", "#3f6f9f", "#e8e2d2", "#5b6168", "#d9b13b"};
box(scene, part, x, y, 4.2, 8.5, 2.6, paints[size_t(tone) % paints.size()]);
};
const auto level_at = [&](size_t i) { return i < periphery.size() ? periphery[i].value("level", 0) : 0; };
const auto planned = [&](const std::string &part, double x, double y, double w, double d, bool busy) {
box(scene, part, x, y, w, d, 1, wall, true);
if (busy) scene.faces.push_back({{{x - w / 2, y - d / 2, 0}, {x + w / 2, y - d / 2, 0}, {x + w / 2, y - d / 2, 14}, {x - w / 2, y - d / 2, 14}}, gold, part, true, false, true, 3, .25, gold, {}, 4});
};
const auto centre = [&](size_t i, double x, double y, double z) { scene.centres["periphery:" + std::to_string(i)] = {x, y, z}; };
const auto busy_at = [&](size_t i) { return i < periphery.size() && building(periphery[i]); };
// 0 transport: a road, then a bus stop, a tram line, a station
if (!periphery.empty()) {
const std::string part = "periphery:0";
const int level = level_at(0);
if (level <= 0) planned(part, 0, -160, 480, 26, busy_at(0));
else {
flat(part, 0, -160, 524, 24, asphalt);
stripe(-262, -160, 262, -160, white, true);
flat(part, 0, -30, 22, 236, asphalt); // the avenue through the quarter
if (level >= 2) { // bus stop: shelter and two buses
box(scene, part, -70, -141, 30, 5, 7, glass);
box(scene, part, -120, -157, 26, 7, 8, "#d9b13b");
box(scene, part, 60, -164, 26, 7, 8, "#d9b13b");
}
if (level >= 3) { // tram: rails and a tram
stripe(-262, -183, 262, -183, slate);
stripe(-262, -187, 262, -187, slate);
box(scene, part, 150, -185, 44, 6, 8, "#c8553d");
box(scene, part, 110, -176, 60, 4, 1.5, plaster);
}
if (level >= 4) { // station hall
gabled(part, -180, -186, 90, 30, 16, 10, brick, slate);
windows(part, -180, -186, 90, 30, 4, 12, 9);
}
if (busy_at(0)) planned(part, -180, -186, 96, 36, true);
}
centre(0, level >= 4 ? -180 : -70, -160, 6);
}
// 4 parking: a lot per level, with bays and cars
if (periphery.size() > 4) {
const std::string part = "periphery:4";
const int level = level_at(4), most = std::max(1, periphery[4].value("max_level", 3));
for (int lot = 0; lot < most; ++lot) {
const double x = -192, y = 132 - lot * 72;
if (lot >= level) { planned(part, x, y, 110, 62, lot == level && busy_at(4)); continue; }
flat(part, x, y, 110, 62, asphalt);
for (int bay = 0; bay <= 10; ++bay) { stripe(x - 50 + bay * 10, y - 28, x - 50 + bay * 10, y - 10, white); stripe(x - 50 + bay * 10, y + 10, x - 50 + bay * 10, y + 28, white); }
for (int bay = 0; bay < 10; ++bay) {
if ((bay * 7 + lot * 3) % 4 != 3) car(part, x - 45 + bay * 10, y - 19, bay + lot);
if ((bay * 5 + lot) % 3 != 0) car(part, x - 45 + bay * 10, y + 19, bay * 2 + lot);
}
}
centre(4, -192, 132, 4);
}
// 2 shopping centre: two wings, a glass front, a roof terrace
if (periphery.size() > 2) {
const std::string part = "periphery:2";
if (level_at(2) <= 0) planned(part, 150, 130, 130, 70, busy_at(2));
else {
box(scene, part, 150, 140, 130, 44, 20, plaster);
box(scene, part, 105, 105, 40, 30, 14, brick);
windows(part, 150, 140, 130, 44, 3, 16, 13);
windows(part, 105, 105, 40, 30, 3, 10, 4);
box(scene, part, 180, 140, 30, 16, 5, glass, false, 20);
box(scene, part, 125, 140, 14, 10, 3, slate, false, 20);
for (double x : {167.0, 187.0, 207.0}) tree(x, 103);
}
centre(2, 150, 130, 12);
}
// 1 arcade: a hall with a sign
if (periphery.size() > 1) {
const std::string part = "periphery:1";
if (level_at(1) <= 0) planned(part, -72, 140, 84, 50, busy_at(1));
else {
box(scene, part, -72, 140, 84, 46, 15, "#b9a6c9");
windows(part, -72, 140, 84, 46, 2, 8, 6);
box(scene, part, -72, 119, 46, 3, 7, "#d9b13b", false, 15);
box(scene, part, -47, 150, 16, 16, 4, slate, false, 15);
}
centre(1, -72, 140, 9);
}
// 3 club house: a pitched roof, a terrace, trees
if (periphery.size() > 3) {
const std::string part = "periphery:3";
if (level_at(3) <= 0) planned(part, 82, 40, 70, 44, busy_at(3));
else {
gabled(part, 82, 45, 66, 30, 11, 9, plaster, tile);
windows(part, 82, 45, 66, 30, 3, 8, 6);
flat(part, 82, 22, 66, 14, wood, -400);
for (double x : {58.0, 74.0, 90.0, 106.0}) box(scene, part, x, 22, 5, 5, 2.4, white);
tree(38, 39); tree(126, 39);
}
centre(3, 82, 40, 8);
}
// 5 restaurant: a house with an awning and tables
if (periphery.size() > 5) {
const std::string part = "periphery:5";
if (level_at(5) <= 0) planned(part, 205, 40, 60, 44, busy_at(5));
else {
gabled(part, 205, 48, 56, 30, 12, 8, brick, slate);
windows(part, 205, 48, 56, 30, 3, 9, 5);
scene.faces.push_back({{{177, 33, 9}, {233, 33, 9}, {233, 22, 6}, {177, 22, 6}}, "#c8553d", part, false, false, false, 2, 1, "", {}, 5});
for (double x : {185.0, 199.0, 213.0, 227.0}) box(scene, part, x, 16, 5, 5, 2.4, white);
}
centre(5, 205, 40, 8);
}
// 6-9 snack bars: a kiosk with a striped awning
const std::array<std::array<double, 2>, 4> kiosks{{{{-72, 62}}, {{-72, -6}}, {{72, -22}}, {{152, -22}}}};
for (size_t n = 0; n < 4 && 6 + n < periphery.size(); ++n) {
const std::string part = "periphery:" + std::to_string(6 + n);
const double x = kiosks[n][0], y = kiosks[n][1];
if (level_at(6 + n) <= 0) planned(part, x, y, 26, 20, busy_at(6 + n));
else {
box(scene, part, x, y, 32, 20, 12, white);
box(scene, part, x, y, 34, 22, 2, tile, false, 12);
Face awning{{{x - 18, y - 10, 11}, {x + 18, y - 10, 11}, {x + 18, y - 21, 7.5}, {x - 18, y - 21, 7.5}}, "#d9b13b", part, false, false, false, 2, 1, "", {}, 5};
for (int s = 1; s < 8; ++s) awning.lines.push_back({{x - 18 + 36.0 * s / 8, y - 10, 11}, {x - 18 + 36.0 * s / 8, y - 21, 7.5}});
scene.faces.push_back(awning);
for (double dx : {-22.0, -8.0, 8.0, 22.0}) box(scene, part, x + dx, y - 28, 5, 5, 3.4, wood); // standing tables
box(scene, part, x + 22, y + 4, 5, 5, 5, slate); // bins
}
centre(6 + n, x, y, 5);
}
// 10-13 fan shops: a shop in the club's colour with a flag
const std::array<std::array<double, 2>, 4> shops{{{{62, -88}}, {{132, -88}}, {{202, -88}}, {{-72, -84}}}};
for (size_t n = 0; n < 4 && 10 + n < periphery.size(); ++n) {
const std::string part = "periphery:" + std::to_string(10 + n);
const double x = shops[n][0], y = shops[n][1];
if (level_at(10 + n) <= 0) planned(part, x, y, 40, 30, busy_at(10 + n));
else {
box(scene, part, x, y, 48, 30, 13, plaster);
windows(part, x, y, 48, 30, 2, 9, 6);
box(scene, part, x, y, 50, 32, 3, seats, false, 13);
box(scene, part, x - 14, y - 17, 14, 3, 5, seats, false, 9); // the sign over the door
scene.faces.push_back({{{x + 20, y + 10, 16}, {x + 20, y + 10, 34}}, "", part, false, true, true, 3, 1, "", {}, 6});
scene.faces.push_back({{{x + 20, y + 10, 34}, {x + 33, y + 10, 30.5}, {x + 20, y + 10, 27}}, seats, part, false, false, true, 1.5, 1, "", {}, 7});
}
centre(10 + n, x, y, 6);
}
// any further object of a data set: a house that grows with its level
for (size_t i = 14; i < periphery.size(); ++i) {
const std::string part = "periphery:" + std::to_string(i);
const double x = -230 + double(i - 14) * 60, y = -100;
if (level_at(i) <= 0) planned(part, x, y, 40, 30, busy_at(i));
else box(scene, part, x, y, 30 + 6 * level_at(i), 24, 6 + 5 * level_at(i), brick);
centre(i, x, y, 6);
}
for (double x : {-245.0, -18.0, 18.0, 245.0}) tree(x, 190);
for (double y : {-112.0, -56.0, 0.0, 56.0}) { tree(-18, y); tree(18, y); } // the avenue
for (const auto &[id, at] : scene.centres) scene.anchors.push_back({id, at});
return scene;
}
scene.centres["stadium"] = {0, 0, 0};
for (const auto &[id, centre] : scene.centres)
if (id != "stadium" && id != "facility:0") scene.anchors.push_back({id, centre});
return scene;
}
// Draws the scene. Faces of `highlight` get an accented outline; `parts` lists where every
// selectable part lies in the picture (in its own units, 0..width and 0..height).
inline Drawing draw(const Scene &scene, const Camera &camera, double width, double height, const std::string &highlight = {}) {
const double yaw = camera.yaw * 3.14159265358979 / 180, pitch = camera.pitch * 3.14159265358979 / 180;
const double cs = std::cos(yaw), sn = std::sin(yaw), sp = std::sin(pitch), cp = std::cos(pitch);
const double scale = camera.zoom * height / 620.0;
struct Flat { double x, y, depth; };
const auto project = [&](const Vec &v) {
const double x = (v.x - camera.tx) * cs - (v.y - camera.ty) * sn, y = (v.x - camera.tx) * sn + (v.y - camera.ty) * cs;
return Flat{width / 2 + x * scale, height / 2 - (y * sp + v.z * cp) * scale, y * cp - v.z * sp};
};
struct Item { const Face *face; double depth; };
std::vector<Item> order;
order.reserve(scene.faces.size());
for (const auto &face : scene.faces) {
double depth = 0;
for (const auto &p : face.points) depth += project(p).depth;
order.push_back({&face, depth / double(std::max<size_t>(1, face.points.size())) - face.bias});
}
std::stable_sort(order.begin(), order.end(), [](const Item &a, const Item &b) { return a.depth > b.depth; });
std::map<std::string, Part> parts;
Drawing out;
out.svg = "<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 " + num(width) + " " + num(height) + "\">";
const Vec light{-0.42, -0.5, 0.76};
for (const auto &item : order) {
const auto &face = *item.face;
std::string path;
for (size_t i = 0; i < face.points.size(); ++i) {
const auto p = project(face.points[i]);
path += (i ? "L" : "M") + num(p.x) + " " + num(p.y);
if (!face.part.empty()) {
auto &part = parts[face.part];
if (!part.seen) { part.id = face.part; part.x0 = part.x1 = p.x; part.y0 = part.y1 = p.y; part.seen = true; }
part.x0 = std::min(part.x0, p.x); part.x1 = std::max(part.x1, p.x); part.y0 = std::min(part.y0, p.y); part.y1 = std::max(part.y1, p.y);
}
}
if (!face.open) path += "Z";
std::string fill = "none";
if (!face.fill.empty()) {
double factor = 1;
if (!face.flat && face.points.size() >= 3) {
const Vec &a = face.points[0], &b = face.points[1], &c = face.points[2];
Vec n{(b.y - a.y) * (c.z - a.z) - (b.z - a.z) * (c.y - a.y), (b.z - a.z) * (c.x - a.x) - (b.x - a.x) * (c.z - a.z), (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x)};
const double length = std::sqrt(n.x * n.x + n.y * n.y + n.z * n.z);
if (length > 0) factor = .74 + .26 * std::abs((n.x * light.x + n.y * light.y + n.z * light.z) / length);
}
fill = shade(face.fill, factor);
}
const bool lit = !highlight.empty() && face.part == highlight;
const std::string ink = lit ? scene.accent : face.ink.empty() ? scene.ink : face.ink;
out.svg += "<path d=\"" + path + "\" fill=\"" + fill + "\" fill-opacity=\"" + (face.fill.empty() ? std::string("1") : num(face.opacity)) + "\"" +
(face.stroke > 0 || lit ? " stroke=\"" + ink + "\" stroke-width=\"" + num((lit ? std::max(face.stroke, 2.0) + 2 : face.stroke) * std::sqrt(camera.zoom)) + "\" stroke-linejoin=\"round\" stroke-linecap=\"round\"" : std::string()) +
(face.dashed ? " stroke-dasharray=\"" + num(9 * std::sqrt(camera.zoom)) + " " + num(7 * std::sqrt(camera.zoom)) + "\"" : std::string()) +
(face.fill.empty() && face.opacity < 1 ? " stroke-opacity=\"" + num(face.opacity) + "\"" : std::string()) + "/>";
for (const auto &line : face.lines) {
std::string d;
for (size_t i = 0; i < line.size(); ++i) { const auto p = project(line[i]); d += (i ? "L" : "M") + num(p.x) + " " + num(p.y); }
out.svg += "<path d=\"" + d + "\" fill=\"none\" stroke=\"" + (face.fill == "" ? ink : std::string("#ffffff")) + "\" stroke-opacity=\".45\" stroke-width=\"" + num(1.6 * std::sqrt(camera.zoom)) + "\"/>";
}
}
out.svg += "</svg>";
for (auto &[id, part] : parts) {
if (const auto found = scene.centres.find(id); found != scene.centres.end()) {
part.centre = found->second;
const auto p = project(found->second);
part.cx = p.x; part.cy = p.y;
} else { part.cx = (part.x0 + part.x1) / 2; part.cy = (part.y0 + part.y1) / 2; }
out.parts.push_back(part);
}
for (const auto &[id, at] : scene.anchors) {
const auto p = project(at);
out.anchors.push_back({id, p.x, p.y});
}
return out;
}
// The camera never moves: one station for the stadium, one for its surroundings.
inline Camera fixed_camera(bool surroundings) { return surroundings ? Camera{22, 42, 1.42, 0, 0} : Camera{28, 44, 2.15, 0, 0}; }
// Where the camera stands for a focus: "" the whole ground with its surroundings, "stadium"
// the bowl, a part id that part, turned so that it lies in front.
inline Camera camera_for(const Scene &scene, const std::string &focus) {
if (focus.empty()) return {28, 42, 1.32, 0, 0};
if (focus == "stadium") return {28, 44, 2.0, 0, 0};
if (focus == "periphery") return {28, 52, 1.15, 0, 0};
const auto found = scene.centres.find(focus);
if (found == scene.centres.end()) return {28, 44, 2.0, 0, 0};
const Vec c = found->second;
if (std::hypot(c.x, c.y) < 1) return {28, 50, 3.0, 0, 0};
// turn the part to the near side, a little off the axis so that its flank shows
const double towards = std::atan2(c.y, c.x) * 180 / 3.14159265358979;
const bool outer = focus.starts_with("periphery:");
return {-90 - towards + 22, 38, outer ? 2.6 : focus.starts_with("block:") ? 3.0 : 3.2, c.x * .82, c.y * .82};
}
} // namespace bmh::modern_ui::stadium3d