map-tile-toolkit 0.3.0

Clip integer polylines and polygons into per-tile pieces on a simple tile grid
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
//! The public slicing API: [`Slicer`] takes **one geometry** at a time — a line, a point or a polygon —
//! and returns a lazy iterator over its slices: [`Slicer::line`] / [`Slicer::point`] for every tile
//! the geometry reaches, or [`Slicer::tile_line`] / [`Slicer::tile_point`] for one given tile.
//!
//! A multi-geometry is sliced one element at a time, by the caller: whoever keeps a feature's
//! attributes with each of its slices, tile by tile, has to do that for a multi-feature too, so the
//! slicer has no parts of its own to index. Slices of several elements that land in one tile are
//! simply several slices there, which the caller groups (a `MultiLineString` in that tile, say).
//!
//! A line run is never copied: it borrows the caller's vertices ([`Local`]) and is computed only when
//! the iterator reaches it, so a line with millions of vertices costs no memory beyond the iterator's
//! own few fields, and stopping early stops the work. A polygon piece is built in the slicer's
//! working memory, one at a time (a polygon inside one tile is lent as it is).
//!
//! Each call reads the whole geometry once, to bound it. That settles every possible overflow up
//! front: the call returns a [`TileError`] or nothing at all, and the line and point iterators are
//! plain [`Iterator`]s whose items cannot fail. (The polygon iterator still yields `Result`s, for a
//! piece too large to index; see [`PolygonSlices`].)
//!
//! **Order is unspecified** for the all-tiles iterators: a line that leaves and re-enters a tile
//! gives that tile several runs, each its own item.
//!
//! Runs follow the keep-original rule (see [`clip_polyline`](crate::clip_polyline)): a vertex is kept
//! when one of its segments touches the tile's buffered box, and a run breaks only where a vertex is
//! dropped. So a run is always a **contiguous range** of the line's vertices, which [`Local`] lends
//! together with the tile origin.
//!
//! In terms of a line's segments (skipping zero-length ones), with "touches" meaning
//! [`segment_intersects`] against the tile's buffered box, a run of tile `t` is the vertex range of
//! segments `k..=e` where
//! - `k` touches `t`, but neither `k − 1` nor `k − 2` does ([`Prev::run_started`]: the run starts
//!   there, and only there, so each run is yielded once, at its first segment), and
//! - the run ends at the first `e` after which neither `e + 1` nor `e + 2` touches `t`
//!   ([`extend_run`]: one missed segment is bridged, keeping both its endpoints).
//!
//! [`LineSlices`] finds each `k` while walking the segments in order, [`TileLines`] by scanning the
//! line for one tile, and both extend with the same [`extend_run`], so they agree by construction.

use core::iter::FusedIterator;

use geo_types::Coord;

use crate::TileError;
use crate::clip_polyline::segment_intersects;
use crate::grid::{Grid, Located, SegmentTiles};
use crate::polygon::{
    FillRun, PolygonPiece, PolygonSlices, Scratch, clip_polygon_to_tile, fill_ring,
};
use crate::tile::TileId;
use crate::vertex::{PolyVertex, Vertex};

/// Slices one geometry at a time into per-tile pieces on an integer grid, lazily.
///
/// Coordinates are integers in tile space: a vertex belongs to tile `x.div_euclid(extent)`, whose own
/// area is `[0, extent)` in its local frame (`x − tile·extent`); every tile's clip box grows `buffer`
/// on each side. Slices come in that local frame, so a vertex outside the tile's own area — in its
/// buffer, a polygon's synthetic corner one unit past the buffer, or a line's first vertex beyond the
/// box — lies outside `[0, extent)`. Project, simplify, and scale float source data into this space
/// before slicing.
///
/// Generic over the [`Vertex`] type `V` (defaults to [`Coord<i32>`]): plain coordinates or
/// payload-carrying vertices (e.g. a per-vertex M value via [`Measured`](crate::Measured)), whose
/// payload rides through slicing unchanged.
///
/// The slicer never panics: bad input yields a [`TileError`] from the call itself (and, for a polygon
/// piece too large to index, as an `Err` item that ends the iteration).
///
/// Lines and points take `&self`, so one slicer can serve many threads. Polygons take `&mut self`:
/// they are sliced in the slicer's reusable working memory, so use one slicer per worker.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Slicer<V: Vertex = Coord<i32>> {
    grid: Grid,
    /// Working memory for polygon slicing, allocated by the first polygon and kept for its capacity
    /// (never part of the slicer's value).
    poly: Scratch<V>,
}

impl<V: Vertex> Slicer<V> {
    /// Create a slicer with the given tile side / per-tile output resolution `extent` and `buffer`.
    ///
    /// # Errors
    ///
    /// - [`TileError::InvalidExtent`] if `extent` is `0`, or `extent + buffer` exceeds `i32::MAX`.
    /// - [`TileError::BufferTooLarge`] if `buffer` is not strictly less than half the `extent`.
    pub fn new(extent: u32, buffer: u16) -> Result<Self, TileError> {
        Ok(Self {
            grid: Grid::new(extent, buffer)?,
            poly: Scratch::new(),
        })
    }

    /// The tile side / per-tile output resolution: a tile's own area is `0..extent` in its local
    /// frame (its buffer and kept vertices beyond it lie outside).
    #[must_use]
    pub fn extent(&self) -> u32 {
        self.grid.extent()
    }

    /// The buffer kept around every tile, in tile-space units.
    #[must_use]
    pub fn buffer(&self) -> u16 {
        self.grid.buffer()
    }

    /// Release the working memory the slicer keeps between features. It only ever grows to what the
    /// largest polygon row or piece needed, but after an outlier a long-lived slicer would otherwise
    /// hold that peak forever.
    pub fn shrink_to_fit(&mut self) {
        self.poly = Scratch::new();
    }

    /// Slice a line into every tile it touches.
    ///
    /// Yields one [`LineSlice`] per run, in no particular order. A line that leaves a tile and comes
    /// back gives that tile several items. Consecutive duplicate vertices are skipped. For a
    /// multi-line, call this for each of its lines.
    ///
    /// The call reads every vertex once to bound the line, so that nothing can fail once the
    /// iteration starts; the slices themselves are still computed one at a time as it advances.
    ///
    /// # Errors
    ///
    /// [`TileError::Overflow`] if the line lies too close to the `i32` limits for this `extent` and
    /// `buffer`: a tile box beyond `i32`, or a vertex more than an `i32` span from the origin of a
    /// tile its bounding box reaches. (So a line spanning more than `2^31` units on one axis is
    /// rejected whole, even if each run would fit its tile.)
    pub fn line<'a>(&self, line: &'a [V]) -> Result<LineSlices<'a, V>, TileError> {
        if let Some((lo, hi)) = bounds(line) {
            self.grid.check_bounds(lo, hi)?;
        }
        Ok(LineSlices {
            grid: self.grid,
            pts: line,
            cursor: None,
        })
    }

    /// Clip a line to one `tile`: yields its runs there, each in walk order — exactly the runs
    /// [`line`](Self::line) yields for that tile.
    ///
    /// Like [`line`](Self::line), the call reads every vertex once; a line whose bounds miss the
    /// tile's buffered box yields nothing without being scanned.
    ///
    /// # Errors
    ///
    /// [`TileError::Overflow`] if `tile`'s buffered box overflows `i32` (a tile far outside the
    /// representable range for this `extent`), or if the line reaches the tile but has a vertex more
    /// than an `i32` span from the tile origin.
    pub fn tile_line<'a>(
        &self,
        tile: TileId,
        line: &'a [V],
    ) -> Result<TileLines<'a, V>, TileError> {
        let (min, max) = self.grid.tile_buffered_bounds(tile)?;
        let origin = self.grid.origin_of(min);
        let mut next = 0;
        match bounds(line) {
            Some((lo, hi)) if hi.x >= min.x && lo.x <= max.x && hi.y >= min.y && lo.y <= max.y => {
                // The line reaches the tile: every vertex must fit the local frame.
                let fits = |p: Coord<i32>| {
                    p.x.checked_sub(origin.x).is_some() && p.y.checked_sub(origin.y).is_some()
                };
                if !fits(lo) || !fits(hi) {
                    return Err(TileError::Overflow);
                }
            }
            // Nothing to scan: no vertices, or none near the tile.
            _ => next = line.len(),
        }
        Ok(TileLines {
            min,
            max,
            origin,
            tile,
            pts: line,
            next,
        })
    }

    /// Slice a point into every tile whose buffered box holds it: its own tile, plus up to three
    /// neighbors when it lies within `buffer` of an edge. Yields one [`PointSlice`] per tile. For a
    /// multi-point, call this for each of its points.
    ///
    /// # Errors
    ///
    /// [`TileError::Overflow`] if one of those tiles has a buffered box beyond `i32`.
    pub fn point(&self, point: V) -> Result<PointSlices<V>, TileError> {
        let p = point.position();
        self.grid.check_tiles(p, p)?;
        Ok(PointSlices::new(self.grid, point))
    }

    /// The `point` in `tile`'s local frame, if the tile's buffered box holds it.
    ///
    /// # Errors
    ///
    /// [`TileError::Overflow`] if `tile`'s buffered box overflows `i32`.
    pub fn tile_point(&self, tile: TileId, point: V) -> Result<Option<V>, TileError> {
        let (extent, buffer) = (i64::from(self.grid.extent()), i64::from(self.grid.buffer()));
        let p = point.position();
        // Per axis: the tile's buffered box `[base − buffer, base + extent − 1 + buffer]`, which must fit
        // `i32` (what `Grid::tile_buffered_bounds` checks), and the point's offset from its base.
        let axis = |tile: i32, v: i32| {
            let base = i64::from(tile) * extent;
            let (lo, hi) = (base - buffer, base + extent - 1 + buffer);
            if lo < i64::from(i32::MIN) || hi > i64::from(i32::MAX) {
                return Err(TileError::Overflow);
            }
            let v = i64::from(v);
            // Inside the box, so within `extent + buffer` of the base: the offset fits `i32`.
            #[expect(clippy::cast_possible_truncation, reason = "within the box, see above")]
            Ok((lo..=hi).contains(&v).then_some((v - base) as i32))
        };
        let (x, y) = (axis(tile.x, p.x)?, axis(tile.y, p.y)?);
        Ok(x.zip(y).map(|(x, y)| point.with_position(Coord { x, y })))
    }
}

impl<V: PolyVertex> Slicer<V> {
    /// Slice a polygon into every tile it reaches. `rings` are its rings — the exterior first, then
    /// the holes — each open or closed (a repeated first/last vertex is fine). Rings with fewer than
    /// 3 vertices once consecutive duplicates are dropped are ignored, and so is a polygon whose
    /// exterior is. For a multipolygon, call this for each of its polygons.
    ///
    /// Yields a [`PolygonPiece`] for every tile a ring edge touches (its
    /// **edge tiles**), exactly what [`tile_polygon`](Self::tile_polygon) yields there, and a
    /// [`FillRun`] for every row span of tiles the polygon covers entirely. Output
    /// rings keep the winding of the input rings (see [`signed_area_2x`](crate::signed_area_2x) to
    /// normalize it first).
    ///
    /// Lazy and lending: see [`PolygonSlices`]. The call reads the rings once, to bound and index
    /// them (only a ring with consecutive duplicate vertices is copied, to drop them); after that the
    /// iterator holds one tile row of the polygon at a time, and builds each piece as it is reached.
    /// Every row of the polygon's bounding box is visited, including rows where it yields nothing.
    ///
    /// # Errors
    ///
    /// - [`TileError::Overflow`] if the polygon lies too close to the `i32` limits for this `extent`
    ///   and `buffer`: a tile box (grown one unit, for the synthetic corners) beyond `i32`, or a
    ///   vertex more than an `i32` span from the origin of a tile its bounding box reaches.
    /// - [`TileError::GeometryTooLarge`] if there are more than `u32::MAX` rings or edges.
    ///
    /// The only error left for the iterator is a piece of more than `u32::MAX` vertices.
    #[inline]
    pub fn polygon<'s, R: AsRef<[V]>>(
        &'s mut self,
        rings: &'s [R],
    ) -> Result<PolygonSlices<'s, V, R>, TileError> {
        PolygonSlices::new(self.grid, rings, self.poly.get())
    }

    /// Clip a polygon to one `tile`: its piece there, or `None` if it leaves nothing — what
    /// [`polygon`](Self::polygon) yields for that tile, with a covered tile's
    /// [`FillRun`] as its fill ring. The piece borrows the slicer's working memory
    /// until the next call.
    ///
    /// # Errors
    ///
    /// - [`TileError::Overflow`] if `tile`'s buffered box overflows `i32`, or a synthetic corner or
    ///   kept vertex does.
    /// - [`TileError::GeometryTooLarge`] if there are more than `u32::MAX` rings, or the piece has
    ///   more than `u32::MAX` vertices.
    pub fn tile_polygon<'s, R: AsRef<[V]>>(
        &'s mut self,
        tile: TileId,
        rings: &'s [R],
    ) -> Result<Option<PolygonPiece<'s, V, R>>, TileError> {
        u32::try_from(rings.len()).map_err(|_| TileError::GeometryTooLarge)?;
        clip_polygon_to_tile(self.grid, tile, rings, &mut self.poly.get().scratch)
    }

    /// The ring [`tile_polygon`](Self::tile_polygon) yields for `run`'s polygon in each tile of the
    /// run: the tile's buffered box grown by one unit, tile-local and closed (its first vertex
    /// repeated), all synthetic vertices, wound like the polygon's exterior. See also
    /// [`PolygonSlices::fill_ring`], for use while the iterator borrows the slicer.
    #[must_use]
    pub fn fill_ring(&self, run: &FillRun) -> [V; 5] {
        fill_ring(self.grid, run)
    }
}

/// Vertices in a tile's local frame — `v − origin`, computed as they are read — every position checked
/// to fit `i32` before the slice is handed out.
///
/// A line run is lent from the caller's input, and skips consecutive duplicate positions (the first
/// of each is kept). A polygon ring has no consecutive duplicates, and is closed: its first vertex
/// repeated at the end, which a ring lent open has [`iter`](Self::iter) append (drop it for an MVT
/// `ClosePath`). It is lent from the input when the polygon lies inside one tile, else built in the
/// slicer's working memory, clipped and with synthetic corners.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Local<'a, V> {
    verts: &'a [V],
    origin: Coord<i32>,
    shape: Shape,
}

/// What [`Local::iter`] does besides subtracting the origin.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Shape {
    /// A line run: skip consecutive duplicates.
    Run,
    /// A distinct ring that is already closed.
    Ring,
    /// A distinct ring lent open: repeat its first vertex at the end.
    OpenRing,
}

impl<'a, V: Vertex> Local<'a, V> {
    /// A line run, lent as it is.
    pub(crate) fn run(verts: &'a [V], origin: Coord<i32>) -> Self {
        Self {
            verts,
            origin,
            shape: Shape::Run,
        }
    }

    /// A polygon ring (distinct vertices), closed by `iter` unless it `is_closed` already.
    pub(crate) fn ring(verts: &'a [V], origin: Coord<i32>, is_closed: bool) -> Self {
        Self {
            verts,
            origin,
            shape: if is_closed {
                Shape::Ring
            } else {
                Shape::OpenRing
            },
        }
    }

    /// The vertices in the tile's local frame.
    #[must_use]
    #[inline]
    pub fn iter(&self) -> LocalIter<'a, V> {
        LocalIter {
            verts: self.verts.iter(),
            origin: self.origin,
            dedupe: self.shape == Shape::Run,
            last: None,
            closing: if self.shape == Shape::OpenRing {
                self.verts.first()
            } else {
                None
            },
        }
    }

    /// The vertices collected into a `Vec`, in the tile's local frame.
    #[must_use]
    pub fn to_vec(self) -> Vec<V> {
        self.iter().collect()
    }

    /// The vertices in the **global** frame: each local vertex plus [`origin`](Self::origin),
    /// consecutive duplicates included. For a line run this is a subslice of the input line, so its
    /// position in the line tells which vertices the run covers; for a clipped polygon ring it is the
    /// slicer's built ring. For a ring lent open this is without the repeated first vertex.
    #[must_use]
    pub fn global(&self) -> &'a [V] {
        self.verts
    }

    /// The tile origin (`tile · extent`) the local frame is relative to.
    #[must_use]
    pub fn origin(&self) -> Coord<i32> {
        self.origin
    }
}

impl<'a, V: Vertex> IntoIterator for &Local<'a, V> {
    type Item = V;
    type IntoIter = LocalIter<'a, V>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

impl<'a, V: Vertex> IntoIterator for Local<'a, V> {
    type Item = V;
    type IntoIter = LocalIter<'a, V>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

/// Iterator over a [`Local`]'s vertices.
#[derive(Debug, Clone)]
pub struct LocalIter<'a, V> {
    verts: core::slice::Iter<'a, V>,
    origin: Coord<i32>,
    /// Whether to skip consecutive duplicates, and the last position seen if so.
    dedupe: bool,
    last: Option<Coord<i32>>,
    /// The vertex that closes the ring, once the rest is done.
    closing: Option<&'a V>,
}

impl<V: Vertex> LocalIter<'_, V> {
    /// `v` in the local frame (checked to fit before the slice was yielded).
    #[inline]
    fn local(&self, v: V) -> V {
        let p = v.position();
        v.with_position(Coord {
            x: p.x.wrapping_sub(self.origin.x),
            y: p.y.wrapping_sub(self.origin.y),
        })
    }
}

impl<V: Vertex> Iterator for LocalIter<'_, V> {
    type Item = V;

    #[inline]
    fn next(&mut self) -> Option<V> {
        for &v in self.verts.by_ref() {
            if self.dedupe {
                let p = v.position();
                if self.last == Some(p) {
                    continue;
                }
                self.last = Some(p);
            }
            return Some(self.local(v));
        }
        let closing = *self.closing.take()?;
        Some(self.local(closing))
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let closing = usize::from(self.closing.is_some());
        let left = self.verts.len();
        // Skipped duplicates make a run shorter, but never empty while vertices are left.
        let low = if self.dedupe {
            usize::from(left > 0)
        } else {
            left
        };
        (low + closing, Some(left + closing))
    }
}

impl<V: Vertex> FusedIterator for LocalIter<'_, V> {}

/// One run of a line in one tile: a contiguous piece of it, in the tile's local frame.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct LineSlice<'a, V> {
    tile: TileId,
    run: Local<'a, V>,
}

impl<'a, V: Vertex> LineSlice<'a, V> {
    /// The tile this run belongs to.
    #[must_use]
    pub fn tile(&self) -> TileId {
        self.tile
    }

    /// The run's vertices, in the tile's local frame (at least two distinct positions).
    #[must_use]
    pub fn run(&self) -> Local<'a, V> {
        self.run
    }
}

/// The index of the first vertex after `i` at a different position, if any.
fn next_distinct<V: Vertex>(pts: &[V], i: usize) -> Option<usize> {
    let p = pts.get(i)?.position();
    (i + 1..pts.len()).find(|&j| pts[j].position() != p)
}

/// The bounding box `(lo, hi)` of every vertex of `pts`, or `None` if there is none. One pass, which
/// is what lets the iterators skip every per-vertex overflow check afterwards.
fn bounds<V: Vertex>(pts: &[V]) -> Option<(Coord<i32>, Coord<i32>)> {
    let (mut lo, mut hi) = (
        Coord {
            x: i32::MAX,
            y: i32::MAX,
        },
        Coord {
            x: i32::MIN,
            y: i32::MIN,
        },
    );
    for v in pts {
        let p = v.position();
        (lo.x, lo.y) = (lo.x.min(p.x), lo.y.min(p.y));
        (hi.x, hi.y) = (hi.x.max(p.x), hi.y.max(p.y));
    }
    (lo.x <= hi.x).then_some((lo, hi))
}

/// Extend a run that has reached vertex `end` (inside the line `pts`) while the next segment, or the one
/// after it (a single-segment excursion out and back), touches the box `[min, max]`. Returns the
/// run's last vertex index.
fn extend_run<V: Vertex>(pts: &[V], mut end: usize, min: Coord<i32>, max: Coord<i32>) -> usize {
    while let Some(n1) = next_distinct(pts, end) {
        let (a, b) = (pts[end].position(), pts[n1].position());
        if segment_intersects(a, b, min, max) {
            end = n1;
            continue;
        }
        let Some(n2) = next_distinct(pts, n1) else {
            break;
        };
        if segment_intersects(b, pts[n2].position(), min, max) {
            end = n2;
            continue;
        }
        break;
    }
    end
}

/// A segment, as its two endpoints.
type Segment = (Coord<i32>, Coord<i32>);

/// The two segments before the current one in the line, most recent first.
#[derive(Debug, Clone, Copy, Default)]
struct Prev([Option<Segment>; 2]);

impl Prev {
    /// `self` after `seg`, the segment just left, joins it.
    #[inline]
    fn pushed(self, seg: Segment) -> Self {
        Self([Some(seg), self.0[0]])
    }

    /// Whether the previous segment touches the box `[min, max]`: the current one only continues its
    /// run there.
    #[inline]
    fn continues(self, min: Coord<i32>, max: Coord<i32>) -> bool {
        self.0[0].is_some_and(|(a, b)| segment_intersects(a, b, min, max))
    }

    /// Whether a run in the box `[min, max]` already started before the current segment: the previous
    /// one continues it, and the one before bridges a single missed segment (see [`extend_run`]).
    #[expect(
        clippy::inline_always,
        reason = "run per tile of a walk; left out of line it costs the line benchmarks 3%"
    )]
    #[inline(always)]
    fn run_started(self, min: Coord<i32>, max: Coord<i32>) -> bool {
        self.0
            .iter()
            .flatten()
            .any(|&(a, b)| segment_intersects(a, b, min, max))
    }
}

/// The tiles of the current segment still to be considered.
#[derive(Debug, Clone)]
enum SegTiles {
    /// The whole segment lies in one tile's inner box, so it touches only that tile.
    One(TileId),
    /// Any other segment: walked row by row through exactly the tiles it touches.
    Walk(SegmentTiles),
    /// Nothing left: every tile is done, or the segment only continues a run already yielded.
    Done,
}

/// Position inside a [`LineSlices`] walk.
#[derive(Debug, Clone)]
struct LineCursor {
    /// The current segment, as indices of its two (distinct) vertices.
    a: usize,
    c: usize,
    prev: Prev,
    tiles: SegTiles,
    /// The tile owning `c`, carried to the next segment's fast-path test.
    loc_c: Located,
}

/// Where the next run lies: its tile, first and last vertex indices, and the tile origin.
struct RunAt {
    tile: TileId,
    first: usize,
    last: usize,
    origin: Coord<i32>,
}

/// A segment inside one tile's inner box touches only that tile; when the previous segment touches it
/// too, the segment only continues a run already yielded. So do all the following segments that stay
/// in that inner box: jump past them at once, with one containment test each, and leave the cursor on
/// the last of them with nothing to yield ([`SegTiles::Done`]).
///
/// Only the previous segment is consulted, not the one before: a segment that merely bridges to a
/// run takes the general path in [`LineSlices::next_run`].
fn skip_inside<V: Vertex>(grid: Grid, pts: &[V], cur: &mut LineCursor) {
    let loc = cur.loc_c;
    let (min, max) = grid.box_of(&loc);
    if !cur.prev.continues(min, max) {
        return;
    }
    while let Some(n) = next_distinct(pts, cur.c) {
        if !loc.contains_inner(pts[n].position()) {
            break;
        }
        cur.prev = cur
            .prev
            .pushed((pts[cur.a].position(), pts[cur.c].position()));
        (cur.a, cur.c) = (cur.c, n);
    }
    cur.tiles = SegTiles::Done;
}

/// Lazy iterator over the runs of a line in every tile it touches; see [`Slicer::line`].
#[derive(Debug, Clone)]
pub struct LineSlices<'a, V> {
    grid: Grid,
    pts: &'a [V],
    cursor: Option<LineCursor>,
}

impl<V: Vertex> LineSlices<'_, V> {
    /// Route the segment `a`–`c`, with `la` the tile owning `a`.
    fn route(&self, a: usize, c: usize, la: Located) -> (SegTiles, Located) {
        let (pa, pc) = (self.pts[a].position(), self.pts[c].position());
        if la.contains_inner(pa) && la.contains_inner(pc) {
            return (SegTiles::One(la.owner), la);
        }
        let loc_c = if la.contains_core(pc) {
            la
        } else {
            self.grid.locate_fit(pc)
        };
        (SegTiles::Walk(SegmentTiles::new(self.grid, pa, pc)), loc_c)
    }

    /// Move to the next segment; `false` when there is none.
    fn advance(&mut self) -> bool {
        let pts = self.pts;
        // The next segment: its first vertex, second vertex, the segments before it, and the tile
        // owning the first vertex if the walk already knows it (everywhere but at the start).
        let next = match &self.cursor {
            None => next_distinct(pts, 0).map(|c| (0, c, Prev::default(), None)),
            Some(cur) => next_distinct(pts, cur.c).map(|c| {
                let seg = (pts[cur.a].position(), pts[cur.c].position());
                (cur.c, c, cur.prev.pushed(seg), Some(cur.loc_c))
            }),
        };
        let Some((a, c, prev, known)) = next else {
            return false;
        };
        let la = known.unwrap_or_else(|| self.grid.locate_fit(pts[a].position()));
        let (tiles, loc_c) = self.route(a, c, la);
        let mut cur = LineCursor {
            a,
            c,
            prev,
            tiles,
            loc_c,
        };
        if matches!(cur.tiles, SegTiles::One(_)) {
            skip_inside(self.grid, pts, &mut cur);
        }
        self.cursor = Some(cur);
        true
    }

    /// Where the next run lies, if there is one.
    #[cfg_attr(feature = "hotpath", hotpath::measure)]
    fn next_run(&mut self) -> Option<RunAt> {
        loop {
            let Some(cur) = &mut self.cursor else {
                if !self.advance() {
                    return None;
                }
                continue;
            };
            // A segment in one tile's inner box touches only that tile, whose box is cached.
            let (tile, fast) = match &mut cur.tiles {
                SegTiles::One(tile) => {
                    let tile = *tile;
                    cur.tiles = SegTiles::Done;
                    (Some(tile), true)
                }
                SegTiles::Walk(walk) => (walk.next(), false),
                SegTiles::Done => (None, false),
            };
            let Some(tile) = tile else {
                if !self.advance() {
                    return None;
                }
                continue;
            };
            let (min, max) = if fast {
                self.grid.box_of(&cur.loc_c)
            } else {
                self.grid.tile_box(tile)
            };
            if cur.prev.run_started(min, max) {
                continue;
            }
            return Some(RunAt {
                tile,
                first: cur.a,
                last: extend_run(self.pts, cur.c, min, max),
                origin: self.grid.origin_of(min),
            });
        }
    }
}

impl<'a, V: Vertex> Iterator for LineSlices<'a, V> {
    type Item = LineSlice<'a, V>;

    fn next(&mut self) -> Option<Self::Item> {
        let at = self.next_run()?;
        Some(LineSlice {
            tile: at.tile,
            run: Local::run(&self.pts[at.first..=at.last], at.origin),
        })
    }
}

impl<V: Vertex> FusedIterator for LineSlices<'_, V> {}

/// Lazy iterator over the runs of a line in one tile; see [`Slicer::tile_line`].
#[derive(Debug, Clone)]
pub struct TileLines<'a, V> {
    tile: TileId,
    min: Coord<i32>,
    max: Coord<i32>,
    origin: Coord<i32>,
    pts: &'a [V],
    /// The vertex the scan resumes from.
    next: usize,
}

impl<'a, V: Vertex> Iterator for TileLines<'a, V> {
    type Item = LineSlice<'a, V>;

    #[cfg_attr(feature = "hotpath", hotpath::measure)]
    fn next(&mut self) -> Option<Self::Item> {
        let (pts, min, max) = (self.pts, self.min, self.max);
        // Find the next segment touching the box; the run starts at its first vertex.
        let mut a = self.next;
        while a < pts.len() {
            let Some(c) = next_distinct(pts, a) else {
                break;
            };
            if segment_intersects(pts[a].position(), pts[c].position(), min, max) {
                let end = extend_run(pts, c, min, max);
                self.next = end;
                return Some(LineSlice {
                    tile: self.tile,
                    run: Local::run(&pts[a..=end], self.origin),
                });
            }
            a = c;
        }
        self.next = pts.len();
        None
    }
}

impl<V: Vertex> FusedIterator for TileLines<'_, V> {}

/// One axis of a point's tiles: its own, and the neighbor within `buffer` of an edge (the buffer is
/// under half a tile, so at most one per axis). The point lies `rem` past its own tile's origin; the
/// tile at offset `d` from its own (`-1`, `0` or `1`) has the point `rem − d · extent` past its
/// origin.
#[derive(Debug, Clone, Copy)]
struct AxisTiles {
    /// The point's own tile, and its offset from that tile's origin.
    tile: i32,
    rem: i32,
    /// The first offset `d` (`-1` when the previous tile holds the point too, else `0`), and how many
    /// tiles there are.
    first: i32,
    len: u8,
}

impl AxisTiles {
    /// One division. A neighbor needs `buffer > 0`, so `extent >= 3` and the neighbor's index stays in
    /// `i32`; the offsets fit because [`Slicer::point`] proved the neighbor's whole buffered box does.
    fn new(v: i32, extent: i32, buffer: i32) -> Self {
        // Floor division with one `/` and `%` (`div_euclid` + `rem_euclid` would divide twice).
        let (mut tile, mut rem) = (v / extent, v % extent);
        if rem < 0 {
            (tile, rem) = (tile - 1, rem + extent);
        }
        // In the first `buffer` units of its tile the point is in the previous tile's buffer, in the
        // last `buffer` units in the next one's (with `buffer == 0` neither).
        let (before, after) = (rem < buffer, rem >= extent - buffer);
        Self {
            tile,
            rem,
            first: -i32::from(before),
            len: 1 + u8::from(before) + u8::from(after),
        }
    }
}

/// A point in one tile, in the tile's local frame.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PointSlice<V> {
    tile: TileId,
    point: V,
}

impl<V: Vertex> PointSlice<V> {
    /// The tile this point lands in.
    #[must_use]
    #[inline]
    pub fn tile(&self) -> TileId {
        self.tile
    }

    /// The point in the tile's local frame: `position − tile · extent`, the payload unchanged.
    #[must_use]
    #[inline]
    pub fn point(&self) -> V {
        self.point
    }
}

/// The tiles a point lands in, with the point in each; see [`Slicer::point`]. One to four items, columns
/// first, computed with a single division per axis.
#[derive(Debug, Clone)]
pub struct PointSlices<V> {
    point: V,
    extent: i32,
    x: AxisTiles,
    y: AxisTiles,
    /// The next item, `0..x.len * y.len`.
    next: u8,
}

impl<V: Vertex> PointSlices<V> {
    fn new(grid: Grid, point: V) -> Self {
        let p = point.position();
        let (extent, buffer) = (grid.extent_i32(), grid.buffer_i32());
        Self {
            point,
            extent,
            x: AxisTiles::new(p.x, extent, buffer),
            y: AxisTiles::new(p.y, extent, buffer),
            next: 0,
        }
    }
}

impl<V: Vertex> Iterator for PointSlices<V> {
    type Item = PointSlice<V>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.next >= self.x.len * self.y.len {
            return None;
        }
        // Columns first: the column is `next / y.len` and the row `next % y.len`, with `y.len` 1 or 2.
        let shift = self.y.len - 1;
        let (i, j) = (self.next >> shift, self.next & shift);
        self.next += 1;
        let (dx, dy) = (self.x.first + i32::from(i), self.y.first + i32::from(j));
        Some(PointSlice {
            tile: TileId::new(self.x.tile + dx, self.y.tile + dy),
            point: self.point.with_position(Coord {
                x: self.x.rem - dx * self.extent,
                y: self.y.rem - dy * self.extent,
            }),
        })
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let left = usize::from(self.x.len * self.y.len - self.next);
        (left, Some(left))
    }
}

impl<V: Vertex> ExactSizeIterator for PointSlices<V> {}

impl<V: Vertex> FusedIterator for PointSlices<V> {}