Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
translate ▾

Modern-Apps.git / library / map / src / main / rust / src / overlay_tests.rs

overlay_tests.rs245 lines · 10.1 KiBRaw
1use super::*;
2use crate::camera::{project, Camera};
3use crate::tess::stroke;
4
5/// The car's route paint: `#1A73E8` over a white casing, at the phone's authored route
6/// width (`maps/.../ui/map/RouteOverlayBuilder.kt`). The old renderer's `12f`/`18f` were
7/// physical px and have no Dp equivalent — see `RouteStyle`'s KDoc on the Kotlin side.
8fn style() -> RouteStyle {
9 RouteStyle { width_dp: 8.0, casing_dp: 2.0, casing_color: 0xFFFF_FFFF }
10}
11
12/// The fill colour the car draws: what a single-run route is painted.
13const FILL: u32 = 0xFF1A_73E8;
14
15/// Wrap a bare polyline as the one coloured run a single-colour route is.
16fn one(points: &[(f64, f64)]) -> Vec<RouteSegment> {
17 vec![RouteSegment { points: points.to_vec(), color: FILL }]
18}
19
20/// A three-point route across San Francisco.
21fn sf_route() -> Vec<(f64, f64)> {
22 vec![(-122.4194, 37.7749), (-122.3894, 37.7949), (-122.3694, 37.7849)]
23}
24
25fn camera(zoom: f64, bearing: f64) -> Camera {
26 Camera {
27 center_lon: -122.4194,
28 center_lat: 37.7749,
29 zoom,
30 width_dp: 411.0,
31 height_dp: 891.0,
32 density: 3.0,
33 bearing_deg: bearing,
34 pitch_deg: 0.0,
35 pivot_dp: 0.0,
36 time_seconds: 0.0,
37 globe: false,
38 moon: false,
39 }
40}
41
42/// Where a local `(u, v)` in the route's square lands on screen, in Dp from the
43/// viewport centre — the same arithmetic the vertex shader and the viewport transform
44/// do between them.
45fn screen_dp(mesh: &RouteMesh, camera: &Camera, u: f32, v: f32) -> (f64, f64) {
46 let (origin, span) = mesh.placement.at_zoom(camera.zoom);
47 let m = camera.world_quad_to_clip(origin, span);
48 let x = m[0] * u + m[4] * v + m[12];
49 let y = m[1] * u + m[5] * v + m[13];
50 (x as f64 * camera.width_dp as f64 / 2.0, y as f64 * camera.height_dp as f64 / 2.0)
51}
52
53#[test]
54fn a_route_becomes_a_stroked_band() {
55 let mesh = tessellate(&one(&sf_route()), style()).expect("three points stroke");
56 assert_eq!(
57 mesh.vertices.len() / stroke::FLOATS_PER_VERTEX,
58 6,
59 "two vertices per point, one band",
60 );
61 assert_eq!(mesh.indices.len(), 12, "two triangles per segment");
62 assert!(mesh.vertices.iter().all(|f| f.is_finite()));
63 // One run, owning the whole index buffer, in the fill colour.
64 assert_eq!(mesh.segments.len(), 1);
65 assert_eq!(mesh.segments[0].color, FILL);
66 assert_eq!(mesh.segments[0].index_offset, 0);
67 assert_eq!(mesh.segments[0].index_count, mesh.indices.len() as u32);
68}
69
70#[test]
71fn coloured_runs_each_get_their_own_index_slice() {
72 // Two runs, two colours: the mesh is one buffer, but each run owns a contiguous,
73 // non-overlapping slice of the indices that together cover the whole thing — which
74 // is what lets the casing draw the lot once and each fill draw its own slice.
75 let segments = vec![
76 RouteSegment { points: vec![(-122.42, 37.77), (-122.40, 37.79)], color: 0xFF00_FF00 },
77 RouteSegment { points: vec![(-122.40, 37.79), (-122.38, 37.78)], color: 0xFFFF_0000 },
78 ];
79 let mesh = tessellate(&segments, style()).expect("two runs");
80 assert_eq!(mesh.segments.len(), 2);
81 assert_eq!(mesh.segments[0].color, 0xFF00_FF00);
82 assert_eq!(mesh.segments[1].color, 0xFFFF_0000);
83 assert_eq!(mesh.segments[0].index_offset, 0);
84 assert_eq!(
85 mesh.segments[1].index_offset, mesh.segments[0].index_count,
86 "the second run starts where the first ends",
87 );
88 assert_eq!(
89 mesh.segments[1].index_offset + mesh.segments[1].index_count,
90 mesh.indices.len() as u32,
91 "the runs together cover every index",
92 );
93 assert!(mesh.vertices.iter().all(|f| f.is_finite()));
94}
95
96#[test]
97fn a_degenerate_run_is_skipped_without_failing_the_route() {
98 // One good run and one glitchy single-point run: the good one still draws, and the
99 // degenerate one contributes no range rather than blanking the whole route.
100 let segments = vec![
101 RouteSegment { points: vec![(-122.42, 37.77), (-122.40, 37.79)], color: FILL },
102 RouteSegment { points: vec![(-122.40, 37.79)], color: 0xFFFF_0000 },
103 ];
104 let mesh = tessellate(&segments, style()).expect("the good run draws");
105 assert_eq!(mesh.segments.len(), 1);
106 assert_eq!(mesh.segments[0].color, FILL);
107}
108
109#[test]
110fn a_degenerate_route_draws_nothing_rather_than_dividing_by_zero() {
111 assert!(tessellate(&[], style()).is_none());
112 assert!(tessellate(&one(&[(-122.4, 37.7)]), style()).is_none());
113 // Every point in the same place: no bounding box, no direction, no line.
114 assert!(tessellate(&one(&[(-122.4, 37.7), (-122.4, 37.7), (-122.4, 37.7)]), style()).is_none());
115}
116
117#[test]
118fn a_route_running_due_north_still_has_a_square_to_live_in() {
119 // Zero width on x, which is what squaring the bounding box exists to survive.
120 let mesh = tessellate(&one(&[(-122.4, 37.7), (-122.4, 37.8)]), style()).expect("a meridian");
121 assert!(mesh.placement.span > 0.0);
122 assert!(mesh.vertices.iter().all(|f| f.is_finite()));
123}
124
125#[test]
126fn repeated_points_do_not_produce_a_nan() {
127 // A router emits coincident points, and a zero-length segment has no direction —
128 // a NaN normal takes the whole strip off screen, not just that segment.
129 let doubled = vec![
130 (-122.4194, 37.7749),
131 (-122.4194, 37.7749),
132 (-122.3894, 37.7949),
133 (-122.3894, 37.7949),
134 (-122.3694, 37.7849),
135 ];
136 let mesh = tessellate(&one(&doubled), style()).expect("a route");
137 for (at, value) in mesh.vertices.iter().enumerate() {
138 assert!(value.is_finite(), "float {at} is {value}");
139 }
140}
141
142#[test]
143fn the_local_geometry_is_the_same_number_at_every_zoom() {
144 // The claim the whole design rests on: Mercator is a pure scale in zoom, so
145 // normalising by the bounding box divides that scale out and the mesh never needs
146 // rebuilding. If this stops holding, a route in a car starts re-tessellating on
147 // every pinch — which is the cost this design exists to avoid.
148 let route = sf_route();
149 let mesh = tessellate(&one(&route), style()).expect("a route");
150 let reference: Vec<(f64, f64)> = route
151 .iter()
152 .map(|&(lon, lat)| {
153 let p = project(lon, lat, 0.0);
154 (
155 (p.x - mesh.placement.origin.x) / mesh.placement.span,
156 (p.y - mesh.placement.origin.y) / mesh.placement.span,
157 )
158 })
159 .collect();
160 for zoom in [4.0, 10.0, 14.0, 18.0, 22.0] {
161 let (origin, span) = mesh.placement.at_zoom(zoom);
162 for (at, &(lon, lat)) in route.iter().enumerate() {
163 let p = project(lon, lat, zoom);
164 let u = (p.x - origin.x) / span;
165 let v = (p.y - origin.y) / span;
166 let (ru, rv) = reference[at];
167 assert!((u - ru).abs() < 1e-12, "z{zoom} point {at}: u {u} vs {ru}");
168 assert!((v - rv).abs() < 1e-12, "z{zoom} point {at}: v {v} vs {rv}");
169 }
170 }
171}
172
173#[test]
174fn a_routes_points_land_where_the_camera_projects_them() {
175 // The overlay is glued to the ground or it is useless, so its square has to place
176 // a point exactly where `project` puts it. Checked in Dp from the viewport centre,
177 // which is where a drift of even a few pixels would be visible against the road
178 // the route is following.
179 let route = sf_route();
180 let mesh = tessellate(&one(&route), style()).expect("a route");
181 for zoom in [8.0, 12.0, 16.0] {
182 let camera = camera(zoom, 0.0);
183 let centre = project(camera.center_lon, camera.center_lat, zoom);
184 for &(lon, lat) in &route {
185 let world = project(lon, lat, zoom);
186 let (origin, span) = mesh.placement.at_zoom(zoom);
187 let u = ((world.x - origin.x) / span) as f32;
188 let v = ((world.y - origin.y) / span) as f32;
189 let (sx, sy) = screen_dp(&mesh, &camera, u, v);
190 let (wx, wy) = (world.x - centre.x, world.y - centre.y);
191 assert!((sx - wx).abs() < 0.05, "z{zoom} x {sx} vs {wx}");
192 assert!((sy - wy).abs() < 0.05, "z{zoom} y {sy} vs {wy}");
193 }
194 }
195}
196
197#[test]
198fn a_route_turns_with_the_camera() {
199 // Under a bearing the overlay has to rotate with the basemap, not stay north-up:
200 // it goes through the same matrix, so this pins that it really is the same one.
201 let route = sf_route();
202 let mesh = tessellate(&one(&route), style()).expect("a route");
203 let zoom = 14.0;
204 let north_up = camera(zoom, 0.0);
205 let turned = camera(zoom, 90.0);
206 let world = project(route[1].0, route[1].1, zoom);
207 let (origin, span) = mesh.placement.at_zoom(zoom);
208 let u = ((world.x - origin.x) / span) as f32;
209 let v = ((world.y - origin.y) / span) as f32;
210
211 let (nx, ny) = screen_dp(&mesh, &north_up, u, v);
212 let (tx, ty) = screen_dp(&mesh, &turned, u, v);
213 // A 90-degree bearing maps (x, y) to (y, -x).
214 assert!((tx - ny).abs() < 0.05, "{tx} should be the north-up y {ny}");
215 assert!((ty - -nx).abs() < 0.05, "{ty} should be minus the north-up x {nx}");
216 // And the distance from the centre is unchanged, because a rotation is not a
217 // scale.
218 assert!(
219 ((tx * tx + ty * ty).sqrt() - (nx * nx + ny * ny).sqrt()).abs() < 0.05,
220 "the rotation scaled the route",
221 );
222}
223
224#[test]
225fn the_casing_is_wider_than_the_route() {
226 let mesh = tessellate(&one(&sf_route()), style()).expect("a route");
227 let fill = mesh.placement.fill_half(3.0);
228 let casing = mesh.placement.casing_half(3.0).expect("a casing");
229 assert!(casing > fill, "{casing} must stand outside {fill}");
230 // 8 Dp wide at density 3 is a 12 device-px half-width, plus 2 Dp of casing a side.
231 assert!((fill - 12.0).abs() < 1e-6, "{fill}");
232 assert!((casing - 18.0).abs() < 1e-6, "{casing}");
233}
234
235#[test]
236fn no_casing_means_no_casing_pass_at_all() {
237 // `casing_dp = 0` is the documented way to switch the outline off. It must give no
238 // casing half-width rather than a zero one: `line.vert` floors a band at half a
239 // pixel so a hairline still rasterises, so a zero-width casing would poke a pixel
240 // of casing colour out from under the route.
241 let plain = RouteStyle { casing_dp: 0.0, ..style() };
242 let mesh = tessellate(&one(&sf_route()), plain).expect("a route");
243 assert!(mesh.placement.casing_half(2.0).is_none());
244 assert!(mesh.placement.fill_half(2.0) > 0.0);
245}