Rise
The Vieneo Province
Loading...
Searching...
No Matches
Tests/StreetMapProjectionTests.cpp

Source at documentation-generation time. See Test catalog for fixture associations.

1#include "../Instruments/StreetMapProjection.h"
2
3#include <cmath>
4#include <cstdlib>
5#include <iostream>
6
7static int checks = 0;
8static void Check(bool condition, const char* message)
9{
10 ++checks;
11 if (!condition) { std::cerr << "FAIL: " << message << '\n'; std::exit(1); }
12}
13
14static bool Near(float actual, float expected)
15{
16 return std::fabs(actual - expected) < 0.001f;
17}
18
19int main()
20{
22 Check(!map.valid, "a missing StreetView must not provide a route viewport");
23 map.width = 137;
24 map.height = 64;
25 // A fractional crop reproduces the mismatch between a fixed route center
26 // and the vehicle's actual location within an integer texture rectangle.
27 map.source = { 989, 1007, 1058, 1039 };
28 const StreetMapProjection::Point vehicle = { 520.5f, 1102.5f };
30
31 for (int originY : { 208, 213 })
32 {
33 map.x = 0;
34 map.y = originY;
35 for (float scale : { 5.0f, 10.0f, 20.0f, 40.0f, 80.0f, 160.0f })
36 {
37 map.magnification = 10.0f / scale;
38 const auto center = map.TextureToScreen(map.centerTexture);
39 const auto sameNode = map.WorldToScreen(vehicle);
40 Check(Near(sameNode.x, center.x) && Near(sameNode.y, center.y),
41 "a route node at the vehicle has a separate center offset");
42 const auto corner = map.TextureToScreen({ 989.0f, 1007.0f });
43 Check(Near(corner.x, 0.0f) && Near(corner.y, static_cast<float>(originY)),
44 "the sampled map corner does not use the vehicle layout origin");
45
46 // 3/2048 of a sector is one texture pixel at every zoom.
47 const auto east = map.WorldToScreen({ vehicle.x + 3.0f / 2048.0f, vehicle.y });
48 const auto south = map.WorldToScreen({ vehicle.x, vehicle.y + 3.0f / 2048.0f });
49 Check(Near(east.x - center.x, map.magnification) && Near(east.y, center.y),
50 "east/west route distances use a different magnification");
51 Check(Near(south.y - center.y, map.magnification) && Near(south.x, center.x),
52 "north/south route distances use a different magnification");
53
54 // Panning or adjusting the raster crop must move every route point
55 // immediately, including when no route node has been passed.
56 map.source.top -= 2;
57 map.source.bottom -= 2;
58 const auto shifted = map.WorldToScreen(vehicle);
59 Check(Near(shifted.y - center.y, 2.0f * map.magnification),
60 "the route does not follow a changed map crop");
61 map.source.top += 2;
62 map.source.bottom += 2;
63 }
64 }
65
66 map.magnification = 2.0f;
67 map.x = 0;
68 map.y = 208;
69 const auto before = map.WorldToScreen(vehicle);
70 Check(Near(before.x, 70.0f) && Near(before.y, 242.0f),
71 "integer crop rounding was replaced with a hard-coded geometric center");
72 map.x = 31;
73 map.y = 213;
74 const auto relocated = map.WorldToScreen(vehicle);
75 Check(Near(relocated.x - before.x, 31.0f) && Near(relocated.y - before.y, 5.0f),
76 "switching vehicle layouts leaves the route at the old origin");
77 map.source.left += 1;
78 map.source.right += 1;
79 Check(Near(map.WorldToScreen(vehicle).x, relocated.x - 2.0f),
80 "a one-texel pan must move the route exactly two pixels at maximum zoom");
81
82 // Both XML layouts must fill their viewport while the vehicle stays centered.
83 for (int height : { 64, 69 })
84 {
85 for (float magnification : { 2.0f, 1.0f, 0.5f, 0.25f, 0.125f, 0.0625f })
86 {
87 for (float fraction : { 0.0f, 0.125f, 0.5f, 0.999f })
88 {
89 StreetMapProjection centered;
90 centered.x = 31;
91 centered.y = height == 69 ? 208 : 213;
92 centered.width = 138;
93 centered.height = height;
94 centered.magnification = magnification;
95 centered.centerTexture = { 1024.0f + fraction, 1024.0f + fraction };
96 centered.source.left = static_cast<long>(centered.centerTexture.x - 69.0f / magnification);
97 centered.source.top = static_cast<long>(centered.centerTexture.y - 0.5f * height / magnification);
98 const float markerX = 99.0f; // x + center pixel of the 138-pixel screen.
99 centered.CenterOverlays(markerX, 3.0f);
100 const auto route = centered.OverlayToScreen(centered.centerTexture);
101 const auto bitmap = centered.TextureToScreen(centered.centerTexture);
102 Check(std::fabs(route.x - markerX) <= 0.5001f,
103 "the route center drifts from the fixed marker during panning or zooming");
104 Check(Near(route.x - bitmap.x, 3.0f) && Near(route.y, bitmap.y),
105 "the road calibration must remain three screen pixels at every zoom");
106 const auto topLeft = centered.TextureToScreen({
107 static_cast<float>(centered.source.left), static_cast<float>(centered.source.top) });
108 const auto bottomRight = centered.TextureToScreen({
109 static_cast<float>(centered.source.right), static_cast<float>(centered.source.bottom) });
110 Check(topLeft.x <= centered.x && Near(topLeft.y, static_cast<float>(centered.y)),
111 "panning exposes an empty strip at the screen origin");
112 Check(bottomRight.x >= centered.x + centered.width && bottomRight.y >= centered.y + height,
113 "the bitmap fails to cover the right or bottom edge of the XML screen");
114 const auto nextNode = centered.OverlayToScreen({
115 centered.centerTexture.x + 1.0f, centered.centerTexture.y + 1.0f });
116 Check(Near(nextNode.x - route.x, magnification) && Near(nextNode.y - route.y, magnification),
117 "calibration changes the scale of the route relative to the map");
118 centered.CenterOverlays(markerX, 3.0f);
119 const auto repeated = centered.OverlayToScreen(centered.centerTexture);
120 Check(Near(repeated.x, route.x) && Near(repeated.y, route.y),
121 "repeated frames accumulate the bitmap pan");
122 }
123 }
124 }
125 std::cout << "PASS: " << checks << " street-map projection checks\n";
126}
bool Near(const Body &b, const Triangle &t, double extra)
Definition RigidProp.h:141
Point TextureToScreen(Point position) const
static Point WorldToTexture(Point position)
Point WorldToScreen(Point position) const
void CenterOverlays(float vehicleCenterX, float roadCorrectionPixels)
Point OverlayToScreen(Point texturePosition) const