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

Modern-Apps.git / scripts / maps / tile_build / src / geom_part1.rs

geom_part1.rs147 lines · 4.9 KiBRaw
1#[allow(clippy::too_many_arguments)]
2fn push_box(
3 out: &mut Vec<(u64, u64)>,
4 ax: f64,
5 ay: f64,
6 bx: f64,
7 by: f64,
8 z: u8,
9 extent: u32,
10 pad: f64,
11) {
12 if !ax.is_finite() || !ay.is_finite() || !bx.is_finite() || !by.is_finite() {
13 return;
14 }
15 let b = Rect {
16 min_x: ax.min(bx),
17 min_y: ay.min(by),
18 max_x: ax.max(bx),
19 max_y: ay.max(by),
20 };
21 if let Some(r) = tile_range(&b, z, extent, pad) {
22 out.extend(r.iter());
23 }
24}
25
26/// The tiles a world-space bounding box touches at zoom `z`, grown by `pad`
27/// world units so a feature just outside a tile still reaches into its buffer.
28///
29/// Returns `None` when the box lies entirely off the grid. It is otherwise
30/// clamped to the grid: a geometry straddling the antimeridian is truncated
31/// rather than wrapped, which is what the published archives do and what the
32/// renderer expects.
33pub fn tile_range(b: &Rect, z: u8, extent: u32, pad: f64) -> Option<TileRange> {
34 let n = 1u64 << z;
35 let e = extent as f64;
36 let last = (n - 1) as f64;
37
38 let fx0 = ((b.min_x - pad) / e).floor();
39 let fy0 = ((b.min_y - pad) / e).floor();
40 let fx1 = ((b.max_x + pad) / e).floor();
41 let fy1 = ((b.max_y + pad) / e).floor();
42 if !fx0.is_finite() || !fy0.is_finite() || !fx1.is_finite() || !fy1.is_finite() {
43 return None;
44 }
45 // Entirely off the grid on either axis: no tile can hold it.
46 if fx1 < 0.0 || fy1 < 0.0 || fx0 > last || fy0 > last {
47 return None;
48 }
49 Some(TileRange {
50 x0: fx0.max(0.0) as u64,
51 y0: fy0.max(0.0) as u64,
52 x1: fx1.min(last) as u64,
53 y1: fy1.min(last) as u64,
54 })
55}
56
57/// One tile's clip rect in **world** coordinates, grown by `buffer`.
58pub fn tile_rect(tx: u64, ty: u64, extent: u32, buffer: f64) -> Rect {
59 let e = extent as f64;
60 let (ox, oy) = (tx as f64 * e, ty as f64 * e);
61 Rect {
62 min_x: ox - buffer,
63 min_y: oy - buffer,
64 max_x: ox + e + buffer,
65 max_y: oy + e + buffer,
66 }
67}
68
69/// Shift a geometry by `(dx, dy)`. Applied after the clip, with the tile's world
70/// origin negated, to put the geometry in tile coordinates.
71pub fn translate<V: Vertex>(g: &Geometry<V>, dx: f64, dy: f64) -> Geometry<V> {
72 let t = |v: &V| {
73 let (x, y) = v.xy();
74 v.moved((x + dx, y + dy))
75 };
76 match g {
77 Geometry::Points(pts) => Geometry::Points(pts.iter().map(t).collect()),
78 Geometry::Lines(lines) => {
79 Geometry::Lines(lines.iter().map(|l| l.iter().map(t).collect()).collect())
80 }
81 Geometry::Polygons(polys) => Geometry::Polygons(
82 polys
83 .iter()
84 .map(|rings| rings.iter().map(|r| r.iter().map(t).collect()).collect())
85 .collect(),
86 ),
87 }
88}
89
90/// Convenience for the two steps that always follow a clip: translate into the
91/// tile, then round onto the integer grid.
92pub fn to_tile<V: Vertex>(g: &Geometry<V>, tx: u64, ty: u64, extent: u32) -> IntGeometry {
93 let e = extent as f64;
94 quantize(&translate(g, -(tx as f64 * e), -(ty as f64 * e)))
95}
96
97/// Round onto the integer tile grid, dropping consecutive duplicates.
98///
99/// Quantisation is what creates duplicates: two vertices a thousandth of a unit
100/// apart become the same integer, and an MVT `LineTo` with a zero delta is a
101/// wasted three bytes that some renderers treat as a degenerate segment. Rings
102/// keep their explicit closing vertex here; [`crate::mvt::encode_polygons`]
103/// strips it, since `ClosePath` implies it.
104pub fn quantize<V: Vertex>(g: &Geometry<V>) -> IntGeometry {
105 match g {
106 Geometry::Points(pts) => IntGeometry::Points(pts.iter().map(round_pt).collect()),
107 Geometry::Lines(lines) => {
108 IntGeometry::Lines(lines.iter().map(|l| dedup(l.iter().map(round_pt))).collect())
109 }
110 Geometry::Polygons(polys) => IntGeometry::Polygons(
111 polys
112 .iter()
113 .map(|rings| rings.iter().map(|r| dedup(r.iter().map(round_pt))).collect())
114 .collect(),
115 ),
116 }
117}
118
119fn round_pt<V: Vertex>(v: &V) -> IPt {
120 let (x, y) = v.xy();
121 (round_i32(x), round_i32(y))
122}
123
124/// Round to the nearest integer, saturating rather than wrapping.
125///
126/// Quantisation runs after the clip, so a coordinate here is always within a
127/// buffered tile and nowhere near the `i32` limits. If one is not, `as i32` on a
128/// `NaN` is silently `0` and on a huge float is silently the saturated bound --
129/// both plausible-looking coordinates. Saturating deliberately keeps the bogus
130/// value bogus, so it shows up as an obviously wrong vertex rather than a subtly
131/// wrong one.
132fn round_i32(v: f64) -> i32 {
133 if v.is_nan() {
134 return 0;
135 }
136 v.round().clamp(i32::MIN as f64, i32::MAX as f64) as i32
137}
138
139fn dedup(it: impl Iterator<Item = IPt>) -> Vec<IPt> {
140 let mut out: Vec<IPt> = Vec::new();
141 for p in it {
142 if out.last() != Some(&p) {
143 out.push(p);
144 }
145 }
146 out
147}