sciart-core 0.7.0

Internal: terminal-agnostic primitives of sciart (canvas, generator trait, fixed-step clock). No semver promise; use the `sciart` crate
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
//! Dense 2-D fields for automata, lattices and PDEs (SPEC-001 CORE-R33..R36, CORE-R43,
//! CORE-R48, CORE-R52, CORE-R53, CORE-R68, CORE-R69).
//!
//! One contiguous row-major `Vec<T>`: rows are cache-line friendly for the
//! `for y { for x { .. } }` sweeps every grid scene does, and the same layout as
//! [`Canvas`](crate::Canvas), so a grid maps onto the screen cell for cell.
//!
//! Coordinates are `i32` like the rest of the drawing API, so callers can probe
//! `x - 1` at the edge without casts; anything off-grid is reported as `None` /
//! `false`, never a panic (GEN-R4). Index arithmetic is done in `usize` after the
//! bounds check, because a `u16 × u16` area does not fit in `i32`.
//!
//! No `Index<(i32, i32)>` impl on purpose: `grid[(x - 1, y)]` reads naturally but
//! panics on the edge, which is exactly the GEN-R4 failure mode. [`Grid::get`]
//! plus `unwrap_or` is barely longer and cannot crash a scene.

use std::iter::FusedIterator;

use crate::Size;

/// A dense `width × height` field of `T`, stored row-major (CORE-R33).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Grid<T> {
    // `u16` like `Size`, but kept separately because a grid may be 0×0 while a
    // `Size` is always at least 1×1 (CORE-R1).
    width: u16,
    height: u16,
    cells: Vec<T>,
}

/// The 0×0 grid (CORE-R33). Does not allocate, so it is the natural initial value
/// for a scene field or a [`Grid::step`] scratch buffer before the first resize.
impl<T> Default for Grid<T> {
    fn default() -> Self {
        Self {
            width: 0,
            height: 0,
            cells: Vec::new(),
        }
    }
}

impl<T: Clone> Grid<T> {
    /// A `size` grid with every cell set to `fill` (CORE-R33).
    pub fn new(size: Size, fill: T) -> Self {
        Self {
            width: size.width(),
            height: size.height(),
            cells: vec![fill; size.area()],
        }
    }

    /// Set every cell to `v` (CORE-R33).
    pub fn fill(&mut self, v: T) {
        self.cells.fill(v);
    }

    /// Change the dimensions, keeping every cell whose `(x, y)` exists in both the
    /// old and the new size; new cells become `fill` (CORE-R34).
    ///
    /// Works in place: surviving cells are moved to their new row-major slot by
    /// `swap`, which needs neither `Copy` nor extra clones. The buffer only grows
    /// (never shrinks), so a shrink-then-grow on terminal resize does not allocate.
    pub fn resize(&mut self, size: Size, fill: T) {
        let (ow, oh) = (usize::from(self.width), usize::from(self.height));
        let (nw, nh) = (usize::from(size.width()), usize::from(size.height()));
        if (ow, oh) == (nw, nh) {
            return;
        }
        let (mw, mh) = (ow.min(nw), oh.min(nh));
        let new_len = nw * nh;

        // Make room first so every destination index is valid during the moves.
        if new_len > self.cells.len() {
            self.cells.resize(new_len, fill.clone());
        }

        // Move each kept cell from `y*ow + x` to `y*nw + x`. The iteration order
        // guarantees a source slot is never clobbered before it is read: when rows
        // get narrower every destination is <= its source, so walk forwards; when
        // they get wider every destination is >= its source, so walk backwards.
        // The value swapped *into* a source slot is garbage that is later either
        // overwritten by `fill` below or truncated away.
        let mut shift = |y: usize, x: usize| self.cells.swap(y * ow + x, y * nw + x);
        if nw <= ow {
            (0..mh).for_each(|y| (0..mw).for_each(|x| shift(y, x)));
        } else {
            (0..mh)
                .rev()
                .for_each(|y| (0..mw).rev().for_each(|x| shift(y, x)));
        }

        self.cells.truncate(new_len);
        // Everything outside the kept `mw × mh` rectangle is new.
        if mw < nw {
            for y in 0..mh {
                self.cells[y * nw + mw..(y + 1) * nw].fill(fill.clone());
            }
        }
        self.cells[mh * nw..].fill(fill);
        self.width = size.width();
        self.height = size.height();
    }

    /// Change the dimensions to `size` with every cell `fill`, discarding the contents
    /// but reusing the buffer: the cheap reshape for a buffer about to be overwritten.
    fn reshape(&mut self, size: Size, fill: T) {
        self.cells.clear();
        self.cells.resize(size.area(), fill);
        self.width = size.width();
        self.height = size.height();
    }
}

impl<T> Grid<T> {
    /// The grid's dimensions (CORE-R33).
    ///
    /// A `Size` cannot be 0×0 (CORE-R1), so for the empty [`Grid::default`] this
    /// reports 1×1; use [`width`](Self::width)/[`height`](Self::height) or
    /// [`is_empty`](Self::is_empty) when the grid may be empty.
    #[inline]
    pub fn size(&self) -> Size {
        Size::new(self.width, self.height)
    }

    /// Number of columns; `0` only for the empty grid (CORE-R33).
    #[inline]
    pub fn width(&self) -> u16 {
        self.width
    }

    /// Number of rows; `0` only for the empty grid (CORE-R33).
    #[inline]
    pub fn height(&self) -> u16 {
        self.height
    }

    /// Number of cells, `width * height`.
    #[inline]
    pub fn len(&self) -> usize {
        self.cells.len()
    }

    #[inline]
    pub fn is_empty(&self) -> bool {
        self.cells.is_empty()
    }

    /// Whether the grid is exactly `size` (never true for the empty grid).
    #[inline]
    fn size_is(&self, size: Size) -> bool {
        (self.width, self.height) == (size.width(), size.height())
    }

    /// Row-major buffer index of `(x, y)`, or `None` off-grid.
    ///
    /// The bounds check happens in `i32` (so negative and huge values are simply
    /// out of range) and only the validated values are widened to `usize` for the
    /// multiply, which cannot overflow: `(65535 - 1) * 65535 + 65534 < usize::MAX`.
    #[inline]
    fn index(&self, x: i32, y: i32) -> Option<usize> {
        let in_x = (0..i32::from(self.width)).contains(&x);
        let in_y = (0..i32::from(self.height)).contains(&y);
        (in_x && in_y).then(|| y as usize * usize::from(self.width) + x as usize)
    }

    /// The cell at `(x, y)`, or `None` off-grid; never panics (CORE-R33).
    #[inline]
    pub fn get(&self, x: i32, y: i32) -> Option<&T> {
        self.index(x, y).map(|i| &self.cells[i])
    }

    /// Mutable access to the cell at `(x, y)`, or `None` off-grid (CORE-R33).
    #[inline]
    pub fn get_mut(&mut self, x: i32, y: i32) -> Option<&mut T> {
        self.index(x, y).map(|i| &mut self.cells[i])
    }

    /// Store `v` at `(x, y)`; returns `false` (dropping `v`) off-grid (CORE-R33).
    #[inline]
    pub fn set(&mut self, x: i32, y: i32, v: T) -> bool {
        match self.get_mut(x, y) {
            Some(cell) => {
                *cell = v;
                true
            }
            None => false,
        }
    }

    /// Every cell with its coordinates, in row-major order (CORE-R33).
    pub fn iter(&self) -> Iter<'_, T> {
        Iter {
            cells: self.cells.iter(),
            width: i32::from(self.width),
            x: 0,
            y: 0,
        }
    }

    /// The row-major buffer: cell `(x, y)` is at `y * width + x` (CORE-R33).
    #[inline]
    pub fn as_slice(&self) -> &[T] {
        &self.cells
    }

    /// Mutable row-major buffer, for bulk per-cell updates that need no coordinates.
    #[inline]
    pub fn as_mut_slice(&mut self) -> &mut [T] {
        &mut self.cells
    }

    /// In-grid neighbour coordinates of `(x, y)`, excluding `(x, y)` itself
    /// (CORE-R35). Yields nothing if `(x, y)` is off-grid.
    ///
    /// Order follows the offset table (row-major around the cell). With
    /// [`Edges::Wrap`] on a grid narrower or shorter than 3, several offsets can
    /// land on the same cell or on the cell itself; those are yielded once / skipped.
    /// Allocation-free: a fixed offset table and an 8-slot inline "seen" list.
    pub fn neighbors(&self, x: i32, y: i32, hood: Hood, edges: Edges) -> Neighbors {
        let offsets: &'static [(i32, i32)] = match hood {
            Hood::VonNeumann => &VON_NEUMANN,
            Hood::Moore => &MOORE,
        };
        let (w, h) = (i32::from(self.width), i32::from(self.height));
        Neighbors {
            x,
            y,
            w,
            h,
            edges,
            // An off-grid centre has no neighbourhood; starting exhausted also
            // keeps the arithmetic below within `-1..=w` (no overflow possible).
            offsets: if self.index(x, y).is_some() {
                offsets
            } else {
                &[]
            },
            // Only small wrapped grids can alias; skip the de-dup work otherwise.
            dedup: edges == Edges::Wrap && (w < 3 || h < 3),
            seen: [(0, 0); 8],
            n_seen: 0,
        }
    }

    /// One synchronous update: every cell of the next generation is
    /// `rule(&old, x, y)`, computed from the old grid only, then the buffers are
    /// swapped (CORE-R36). Afterwards `scratch` holds the previous generation.
    ///
    /// `scratch` is rebuilt to this grid's size by clear-and-push, which keeps its
    /// capacity, so after the first call the two buffers just ping-pong with no
    /// allocation. This also means `T` needs no `Clone`/`Default` for the scratch.
    pub fn step(&mut self, scratch: &mut Grid<T>, mut rule: impl FnMut(&Grid<T>, i32, i32) -> T) {
        scratch.cells.clear();
        scratch.cells.reserve_exact(self.cells.len());
        for y in 0..i32::from(self.height) {
            for x in 0..i32::from(self.width) {
                scratch.cells.push(rule(self, x, y));
            }
        }
        scratch.width = self.width;
        scratch.height = self.height;
        std::mem::swap(self, scratch);
    }
}

/// Coordinate `d` on an axis of `dn` cells mapped onto an axis of `sn` cells with cell
/// centres aligned, `(d + ½) / dn = (s + ½) / sn` (CORE-R43, CORE-R68): the convention of
/// every resampler here and of [`SampleMap`]. `d` may be fractional (an agent's position,
/// a pixel centre). Exact for equal sizes.
#[inline]
pub fn centre_of(d: f64, dn: usize, sn: usize) -> f64 {
    (d + 0.5) * sn as f64 / dn as f64 - 0.5
}

/// Carry a continuous field onto a new lattice (ADR-0012, CORE-R68): `scratch` is
/// reshaped to `size` (its old contents are discarded, its buffer reused), filled by
/// [`resample_bilinear`] from `field`, and swapped in, so afterwards `field` has `size`
/// and `scratch` holds the old field. No-op if `field` is empty or already `size`.
pub fn regrid_bilinear(field: &mut Grid<f32>, size: Size, scratch: &mut Grid<f32>) {
    if field.is_empty() || field.size_is(size) {
        return;
    }
    scratch.reshape(size, 0.0);
    resample_bilinear(field, scratch);
    std::mem::swap(field, scratch);
}

/// [`regrid_bilinear`] for discrete states: nearest-neighbour via [`resample_nearest`]
/// (CORE-R68), so only values present in `field` appear.
pub fn regrid_nearest<T: Clone>(field: &mut Grid<T>, size: Size, scratch: &mut Grid<T>) {
    if field.is_empty() || field.size_is(size) {
        return;
    }
    // Any value will do as the placeholder: every cell is overwritten by the resample.
    let fill = field.cells[0].clone();
    scratch.reshape(size, fill);
    resample_nearest(field, scratch);
    std::mem::swap(field, scratch);
}

/// Fill `dst` (keeping its size) by bilinear sampling of `src`, cell-centre aligned and
/// clamped at the edges (CORE-R43). No-op if either grid is empty; never allocates.
///
/// For moving a continuous field (concentration, ρ, u) onto a new simulation lattice
/// after a resize (ADR-0012). Interpolation uses `a + (b − a)·f`, so a constant field
/// comes through bit-exact, and equal sizes are the identity.
pub fn resample_bilinear(src: &Grid<f32>, dst: &mut Grid<f32>) {
    if src.is_empty() || dst.is_empty() {
        return;
    }
    let (sw, sh) = (usize::from(src.width), usize::from(src.height));
    let (dw, dh) = (usize::from(dst.width), usize::from(dst.height));
    for (y, row) in dst.cells.chunks_exact_mut(dw).enumerate() {
        let (y0, y1, fy) = bilinear_taps(y, dh, sh);
        let (r0, r1) = (&src.cells[y0 * sw..][..sw], &src.cells[y1 * sw..][..sw]);
        for (x, out) in row.iter_mut().enumerate() {
            let (x0, x1, fx) = bilinear_taps(x, dw, sw);
            *out = bilerp(r0[x0], r0[x1], r1[x0], r1[x1], fx, fy);
        }
    }
}

/// Neighbouring source indices and weight along one axis for destination index `d` of
/// `dn` sampling a source of `sn ≥ 1` (CORE-R43): cell-centre aligned, edge-clamped.
#[inline]
fn bilinear_taps(d: usize, dn: usize, sn: usize) -> (usize, usize, f32) {
    let s = centre_of(d as f64, dn, sn).clamp(0.0, (sn - 1) as f64);
    let i0 = s as usize; // s ≥ 0, so truncation is floor
    let i1 = (i0 + 1).min(sn - 1);
    (i0, i1, (s - i0 as f64) as f32)
}

/// `a + (b − a)·f`, returning `a` itself at `f = 0` so constants survive bit-exactly.
#[inline(always)]
fn lerp32(a: f32, b: f32, f: f32) -> f32 {
    if f == 0.0 { a } else { a + (b - a) * f }
}

/// Bilinear blend of a 2×2 neighbourhood: along x on both rows, then along y. The one
/// formula shared by [`resample_bilinear`] and [`SampleMap::sample`] (CORE-R52).
#[inline(always)]
fn bilerp(top0: f32, top1: f32, bottom0: f32, bottom1: f32, fx: f32, fy: f32) -> f32 {
    lerp32(lerp32(top0, top1, fx), lerp32(bottom0, bottom1, fx), fy)
}

/// Nearest source index for destination index `d`: `floor((d + ½)·sn/dn)` in integers,
/// exact and the identity for equal sizes (CORE-R43).
#[inline]
fn nearest_tap(d: usize, dn: usize, sn: usize) -> usize {
    ((2 * d + 1) * sn / (2 * dn)).min(sn - 1)
}

/// One axis of a [`SampleMap`]: the bilinear pair and weight, and the nearest index.
/// `u16` indices suffice because grid axes are `u16`.
#[derive(Debug, Clone, Copy, PartialEq)]
struct Tap {
    i0: u16,
    i1: u16,
    near: u16,
    f: f32,
}

impl Tap {
    fn new(d: usize, dn: usize, sn: usize) -> Self {
        let (i0, i1, f) = bilinear_taps(d, dn, sn);
        // Every index is < sn ≤ u16::MAX, so the narrowing is lossless.
        Self {
            i0: i0 as u16,
            i1: i1 as u16,
            near: nearest_tap(d, dn, sn) as u16,
            f,
        }
    }
}

/// Precomputed per-axis sampling tables from a simulation lattice onto a display region
/// (CORE-R52, ADR-0012): the per-cell work of [`resample_bilinear`] /
/// [`resample_nearest`] without the float divisions, and without a destination grid,
/// so a scene can sample its capped lattice straight into `Pixels` or `Dots`.
///
/// Built in `resize` with [`rebuild`](Self::rebuild); the tables allocate only when
/// they grow. The [`Default`] map is empty: `sample` gives 0 and `nearest` `(0, 0)`.
#[derive(Debug, Clone, Default)]
pub struct SampleMap {
    src: (u16, u16),
    xs: Vec<Tap>,
    ys: Vec<Tap>,
    /// Catmull-Rom taps per axis (CORE-R102).
    cxs: Vec<Cubic>,
    cys: Vec<Cubic>,
    magnification: f32,
}

/// One axis of a [`SampleMap`]'s cubic mode: four edge-clamped source indices around
/// the sample point and their Catmull-Rom weights (CORE-R102).
#[derive(Debug, Clone, Copy, PartialEq)]
struct Cubic {
    i: [u16; 4],
    w: [f32; 4],
}

impl Cubic {
    fn new(d: usize, dn: usize, sn: usize) -> Self {
        // The bilinear pair is taps 1 and 2; the outer two are one further out, clamped.
        let s = centre_of(d as f64, dn, sn).clamp(0.0, (sn.max(1) - 1) as f64);
        let i1 = s as usize; // s ≥ 0, so truncation is floor
        let last = sn.max(1) - 1;
        let t = s - i1 as f64;
        let (t2, t3) = (t * t, t * t * t);
        let w = [
            0.5 * (-t3 + 2.0 * t2 - t),
            0.5 * (3.0 * t3 - 5.0 * t2 + 2.0),
            0.5 * (-3.0 * t3 + 4.0 * t2 + t),
            0.5 * (t3 - t2),
        ];
        // Every index is < sn ≤ u16::MAX, so the narrowing is lossless.
        Self {
            i: [
                i1.saturating_sub(1) as u16,
                i1 as u16,
                (i1 + 1).min(last) as u16,
                (i1 + 2).min(last) as u16,
            ],
            w: w.map(|w| w as f32),
        }
    }
}

/// One destination row of a [`SampleMap`]'s cubic mode over one source grid
/// (CORE-R102), from [`SampleMap::row_cubic`].
#[derive(Debug, Clone, Copy)]
pub struct CubicRow<'a> {
    xs: &'a [Cubic],
    rows: [&'a [f32]; 4],
    wy: [f32; 4],
}

impl CubicRow<'_> {
    /// Catmull-Rom value at destination column `x` (clamped onto the region): 16
    /// multiply-adds, clamped to the four central source values' range.
    #[inline(always)]
    pub fn at(&self, x: i32) -> f32 {
        let Some(last) = self.xs.len().checked_sub(1) else {
            return 0.0;
        };
        if self.rows[0].is_empty() {
            return 0.0;
        }
        let tx = self.xs[usize::try_from(x).map_or(0, |i| i.min(last))];
        let [a, b, c, d] = tx.i.map(usize::from);
        let across = |r: &[f32]| tx.w[0] * r[a] + tx.w[1] * r[b] + tx.w[2] * r[c] + tx.w[3] * r[d];
        let v = self.wy[0] * across(self.rows[0])
            + self.wy[1] * across(self.rows[1])
            + self.wy[2] * across(self.rows[2])
            + self.wy[3] * across(self.rows[3]);
        let (r1, r2) = (self.rows[1], self.rows[2]);
        let lo = r1[b].min(r1[c]).min(r2[b]).min(r2[c]);
        let hi = r1[b].max(r1[c]).max(r2[b]).max(r2[c]);
        // `max` then `min` also maps a NaN sum onto the range's low end.
        v.max(lo).min(hi)
    }
}

impl SampleMap {
    /// Tables for sampling a `src` grid at every cell of a `dst` region, with the
    /// cell-centre, edge-clamp convention of [`resample_bilinear`] (CORE-R52).
    pub fn rebuild(&mut self, src: Size, dst: Size) {
        let (sw, sh) = (usize::from(src.width()), usize::from(src.height()));
        let (dw, dh) = (usize::from(dst.width()), usize::from(dst.height()));
        self.src = (src.width(), src.height());
        self.xs.clear();
        self.xs.extend((0..dw).map(|x| Tap::new(x, dw, sw)));
        self.ys.clear();
        self.ys.extend((0..dh).map(|y| Tap::new(y, dh, sh)));
        self.cxs.clear();
        self.cxs.extend((0..dw).map(|x| Cubic::new(x, dw, sw)));
        self.cys.clear();
        self.cys.extend((0..dh).map(|y| Cubic::new(y, dh, sh)));
        let ratio = |d: usize, s: usize| if s == 0 { 0.0 } else { d as f32 / s as f32 };
        self.magnification = if dw == 0 || dh == 0 {
            0.0
        } else {
            ratio(dw, sw).max(ratio(dh, sh))
        };
    }

    /// How many destination cells span one source cell, along the more magnified axis
    /// (0 for the empty map). GEN-R17: bilinear up to 2, [`sample_cubic`](Self::sample_cubic)
    /// above it.
    #[inline]
    pub fn magnification(&self) -> f32 {
        self.magnification
    }

    /// Separable Catmull-Rom value of `src` at destination cell `(x, y)` (clamped onto
    /// the region), CORE-R102: 4 × 4 taps with weights from the `rebuild` tables (no
    /// transcendental maths), clamped to the range of the four central source values so
    /// it never overshoots into a new colour. Exact at source centres for equal sizes;
    /// a wrong-size source or the empty map gives 0, as [`sample`](Self::sample).
    #[inline]
    pub fn sample_cubic(&self, src: &Grid<f32>, x: i32, y: i32) -> f32 {
        self.row_cubic(src, y).at(x)
    }

    /// Row `y` (clamped) of [`sample_cubic`](Self::sample_cubic) with its four source
    /// rows and weights looked up once (CORE-R102, as [`row`](Self::row)).
    #[inline(always)]
    pub fn row_cubic<'a>(&'a self, src: &'a Grid<f32>, y: i32) -> CubicRow<'a> {
        let mut out = CubicRow {
            xs: &[],
            rows: [&[]; 4],
            wy: [0.0; 4],
        };
        if (src.width, src.height) != self.src {
            return out;
        }
        let Some(last) = self.cys.len().checked_sub(1) else {
            return out;
        };
        let ty = self.cys[usize::try_from(y).map_or(0, |i| i.min(last))];
        let w = usize::from(src.width);
        for (row, &i) in out.rows.iter_mut().zip(&ty.i) {
            *row = &src.cells[usize::from(i) * w..][..w];
        }
        out.xs = &self.cxs;
        out.wy = ty.w;
        out
    }

    /// Taps for destination `(x, y)`, clamped onto the table.
    #[inline(always)]
    fn taps(&self, x: i32, y: i32) -> Option<(Tap, Tap)> {
        let pick = |t: &[Tap], i: i32| {
            let last = t.len().checked_sub(1)?;
            // Negative ⇒ 0, beyond ⇒ last: `usize::try_from` fails only for negatives.
            Some(t[usize::try_from(i).map_or(0, |i| i.min(last))])
        };
        Some((pick(&self.xs, x)?, pick(&self.ys, y)?))
    }

    /// Bilinear value of `src` at destination cell `(x, y)` (clamped onto the region),
    /// bit-identical to [`resample_bilinear`]'s output for that cell (CORE-R52).
    ///
    /// `src` must have the size passed to [`rebuild`](Self::rebuild); any other size
    /// (a caller bug) and the empty map give 0.
    #[inline(always)]
    pub fn sample(&self, src: &Grid<f32>, x: i32, y: i32) -> f32 {
        if (src.width, src.height) != self.src {
            return 0.0;
        }
        let Some((tx, ty)) = self.taps(x, y) else {
            return 0.0;
        };
        let w = usize::from(src.width);
        let row = |i: u16| &src.cells[usize::from(i) * w..][..w];
        let (r0, r1) = (row(ty.i0), row(ty.i1));
        let (x0, x1) = (usize::from(tx.i0), usize::from(tx.i1));
        bilerp(r0[x0], r0[x1], r1[x0], r1[x1], tx.f, ty.f)
    }

    /// Row `y` of the destination region (clamped onto it) as a [`MapRow`]: its two
    /// source rows and weight looked up once, so sampling a whole row costs one x-tap
    /// per cell (CORE-R104). `row(src, y).at(x)` is bit-identical to
    /// [`sample(src, x, y)`](Self::sample), including 0 for the empty map or a source of
    /// another size.
    #[inline(always)]
    pub fn row<'a>(&'a self, src: &'a Grid<f32>, y: i32) -> MapRow<'a> {
        let empty = MapRow {
            xs: &[],
            r0: &[],
            r1: &[],
            fy: 0.0,
        };
        if (src.width, src.height) != self.src {
            return empty;
        }
        let Some(last) = self.ys.len().checked_sub(1) else {
            return empty;
        };
        let ty = self.ys[usize::try_from(y).map_or(0, |i| i.min(last))];
        let w = usize::from(src.width);
        let row = |i: u16| &src.cells[usize::from(i) * w..][..w];
        MapRow {
            xs: &self.xs,
            r0: row(ty.i0),
            r1: row(ty.i1),
            fy: ty.f,
        }
    }

    /// The source cell nearest to destination cell `(x, y)` (clamped onto the region),
    /// the one [`resample_nearest`] copies; `(0, 0)` for the empty map (CORE-R52).
    #[inline(always)]
    pub fn nearest(&self, x: i32, y: i32) -> (i32, i32) {
        self.taps(x, y)
            .map_or((0, 0), |(tx, ty)| (i32::from(tx.near), i32::from(ty.near)))
    }
}

/// One destination row of a [`SampleMap`] over one source grid (CORE-R104), from
/// [`SampleMap::row`]: hoist it out of the x loop (or rebuild it when `y` changes inside
/// a [`Pixels::shade`](crate::raster::Pixels::shade) closure).
#[derive(Debug, Clone, Copy)]
pub struct MapRow<'a> {
    xs: &'a [Tap],
    r0: &'a [f32],
    r1: &'a [f32],
    fy: f32,
}

impl MapRow<'_> {
    /// Bilinear value at destination column `x` (clamped onto the region), bit-identical
    /// to [`SampleMap::sample`] for this row.
    #[inline(always)]
    pub fn at(&self, x: i32) -> f32 {
        let Some(last) = self.xs.len().checked_sub(1) else {
            return 0.0;
        };
        if self.r0.is_empty() {
            return 0.0;
        }
        let tx = self.xs[usize::try_from(x).map_or(0, |i| i.min(last))];
        let (x0, x1) = (usize::from(tx.i0), usize::from(tx.i1));
        bilerp(
            self.r0[x0],
            self.r0[x1],
            self.r1[x0],
            self.r1[x1],
            tx.f,
            self.fy,
        )
    }
}

/// The isotropic 9-point Laplacian of `src` into `dst` at unit spacing (CORE-R53):
/// `(4·(N + S + E + W) + (NE + NW + SE + SW) − 20·c) / 6`.
///
/// `Edges::Wrap` is periodic; `Edges::Clip` is zero-flux (Neumann): a ghost cell beyond
/// a wall repeats the edge cell next to it, so nothing flows through the wall. `dst` is
/// reshaped to `src` (reusing its buffer when large enough).
///
/// Its error is isotropic to leading order, so reaction–diffusion spots stay round
/// instead of going square on the lattice. Mirror pairs are added first (`W + E`,
/// `N + S`, `NW + NE`, `SW + SE`), and float addition is commutative, so a mirrored
/// field gives a bitwise mirrored result and symmetric patterns stay symmetric forever.
pub fn laplacian9(src: &Grid<f32>, dst: &mut Grid<f32>, edges: Edges) {
    let (w, h) = (usize::from(src.width), usize::from(src.height));
    dst.cells.clear();
    dst.cells.resize(w * h, 0.0);
    dst.width = src.width;
    dst.height = src.height;
    if w == 0 || h == 0 {
        return;
    }
    // Previous/next index along an axis of length n under the edge rule.
    let around = |i: usize, n: usize| match edges {
        Edges::Wrap => ((i + n - 1) % n, (i + 1) % n),
        Edges::Clip => (i.saturating_sub(1), (i + 1).min(n - 1)),
    };
    let row = |y: usize| &src.cells[y * w..][..w];
    for (y, out) in dst.cells.chunks_exact_mut(w).enumerate() {
        let (yn, ys) = around(y, h);
        let (north, mid, south) = (row(yn), row(y), row(ys));
        for (x, o) in out.iter_mut().enumerate() {
            let (xw, xe) = around(x, w);
            let orth = (mid[xw] + mid[xe]) + (north[x] + south[x]);
            let diag = (north[xw] + north[xe]) + (south[xw] + south[xe]);
            *o = (4.0 * orth + diag - 20.0 * mid[x]) / 6.0;
        }
    }
}

/// Fill `dst` (keeping its size) by nearest-neighbour sampling of `src`, cell-centre
/// aligned (CORE-R43), so only values present in `src` appear. For discrete states such
/// as Ising spins. No-op if either grid is empty; never allocates.
pub fn resample_nearest<T: Clone>(src: &Grid<T>, dst: &mut Grid<T>) {
    if src.is_empty() || dst.is_empty() {
        return;
    }
    let (sw, sh) = (usize::from(src.width), usize::from(src.height));
    let (dw, dh) = (usize::from(dst.width), usize::from(dst.height));
    for (y, row) in dst.cells.chunks_exact_mut(dw).enumerate() {
        let src_row = &src.cells[nearest_tap(y, dh, sh) * sw..][..sw];
        for (x, out) in row.iter_mut().enumerate() {
            out.clone_from(&src_row[nearest_tap(x, dw, sw)]);
        }
    }
}

/// A capped simulation lattice for a display region measured in square units
/// (ADR-0012, CORE-R48).
///
/// The display aspect `w / h` is snapped to the nearest power of `bucket` (e.g. 1.25;
/// `bucket ≤ 1` or NaN keeps the exact aspect), and the result is the largest lattice of
/// that aspect with `w·h ≤ max_nodes`, clamped per axis to the display (so small
/// displays simulate at display size) and at least 1×1 (`max_nodes = 0` ⇒ 1×1).
///
/// Because the uncapped shape depends only on the bucket and `max_nodes`, any display at
/// least that large in both axes, within one bucket, maps to the same lattice: resizing
/// there never touches simulation state.
pub fn lattice_for(display: Size, max_nodes: usize, bucket: f64) -> Size {
    if max_nodes == 0 {
        return Size::new(1, 1);
    }
    let (dw, dh) = (display.width(), display.height());
    let aspect = f64::from(dw) / f64::from(dh);
    let snapped = if bucket > 1.0 {
        let step = bucket.ln();
        ((aspect.ln() / step).round() * step).exp()
    } else {
        aspect
    };
    // Degenerate buckets (∞, 1 + ε) can produce 0, ∞ or NaN here: fall back to exact.
    let aspect = if snapped.is_finite() && snapped > 0.0 {
        snapped
    } else {
        aspect
    };
    let full = lattice_for_aspect(aspect, max_nodes);
    Size::new(full.width().min(dw), full.height().min(dh))
}

/// The largest lattice of aspect `w / h ≈ aspect` with `w·h ≤ max_nodes`, at least 1×1
/// and at most `u16::MAX` per axis (CORE-R69): the uncapped shape behind [`lattice_for`],
/// for a scene that picks its own aspect (say, a fixed-bucket channel) rather than a
/// display's. Never panics: a zero, negative or NaN `aspect` gives 1 column, and `max_nodes
/// = 0` gives 1×1.
pub fn lattice_for_aspect(aspect: f64, max_nodes: usize) -> Size {
    if max_nodes == 0 {
        return Size::new(1, 1);
    }
    // h·(aspect·h) = max_nodes. Flooring both axes (and the w ≤ max/h guard for float
    // round-off and the 1-wide/1-tall extremes) keeps w·h ≤ max_nodes; the float → usize
    // casts saturate (NaN ⇒ 0), so the clamps also absorb degenerate aspects.
    let h_ideal = (max_nodes as f64 / aspect).sqrt();
    let h = (h_ideal as usize).clamp(1, max_nodes);
    let w = ((h_ideal * aspect) as usize).clamp(1, max_nodes / h);
    let axis = |n: usize| u16::try_from(n).unwrap_or(u16::MAX);
    Size::new(axis(w), axis(h))
}

/// Which cells count as neighbours (CORE-R35).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Hood {
    /// The 4 orthogonal cells.
    VonNeumann,
    /// The 8 orthogonal and diagonal cells.
    Moore,
}

/// What happens at the border (CORE-R35).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Edges {
    /// Off-grid neighbours are dropped.
    Clip,
    /// The grid is a torus: coordinates wrap around both axes.
    Wrap,
}

// Row-major around the centre, so iteration order is stable and predictable.
const VON_NEUMANN: [(i32, i32); 4] = [(0, -1), (-1, 0), (1, 0), (0, 1)];
const MOORE: [(i32, i32); 8] = [
    (-1, -1),
    (0, -1),
    (1, -1),
    (-1, 0),
    (1, 0),
    (-1, 1),
    (0, 1),
    (1, 1),
];

/// Row-major iterator over `((x, y), &cell)`, from [`Grid::iter`] (CORE-R33).
///
/// Tracks `x`/`y` incrementally instead of dividing the flat index each step.
#[derive(Debug, Clone)]
pub struct Iter<'a, T> {
    cells: std::slice::Iter<'a, T>,
    width: i32,
    x: i32,
    y: i32,
}

impl<'a, T> Iterator for Iter<'a, T> {
    type Item = ((i32, i32), &'a T);

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        let cell = self.cells.next()?;
        let pos = (self.x, self.y);
        self.x += 1;
        if self.x == self.width {
            self.x = 0;
            self.y += 1;
        }
        Some((pos, cell))
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        self.cells.size_hint()
    }
}

impl<T> ExactSizeIterator for Iter<'_, T> {}
impl<T> FusedIterator for Iter<'_, T> {}

/// Neighbour coordinates of one cell, from [`Grid::neighbors`] (CORE-R35).
///
/// Holds no borrow of the grid, so a caller can iterate neighbours while mutating
/// the grid (e.g. to scatter into them).
#[derive(Debug, Clone)]
pub struct Neighbors {
    x: i32,
    y: i32,
    w: i32,
    h: i32,
    edges: Edges,
    offsets: &'static [(i32, i32)],
    dedup: bool,
    seen: [(i32, i32); 8],
    n_seen: usize,
}

impl Iterator for Neighbors {
    type Item = (i32, i32);

    fn next(&mut self) -> Option<(i32, i32)> {
        while let Some((&(dx, dy), rest)) = self.offsets.split_first() {
            self.offsets = rest;
            // The centre is in-grid (checked at construction), so these are in
            // `-1..=w` / `-1..=h` and cannot overflow.
            let (mut nx, mut ny) = (self.x + dx, self.y + dy);
            match self.edges {
                Edges::Clip => {
                    if !(0..self.w).contains(&nx) || !(0..self.h).contains(&ny) {
                        continue;
                    }
                }
                Edges::Wrap => {
                    nx = nx.rem_euclid(self.w);
                    ny = ny.rem_euclid(self.h);
                }
            }
            if self.dedup {
                let p = (nx, ny);
                if p == (self.x, self.y) || self.seen[..self.n_seen].contains(&p) {
                    continue;
                }
                self.seen[self.n_seen] = p;
                self.n_seen += 1;
            }
            return Some((nx, ny));
        }
        None
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        (0, Some(self.offsets.len()))
    }
}

impl FusedIterator for Neighbors {}