space-time 0.5.0

A nightly only library of space-time filling curves that supports no-std.
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
//
// Copyright 2020, Gobsmacked Labs, LLC.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Space-Time filling curve for non-points

use crate::index_range::{CoveredRange, IndexRange, OverlappingRange};
use alloc::{boxed::Box, collections::VecDeque, vec, vec::Vec};
use num_integer::div_floor;
#[allow(unused_imports)]
use num_traits::Float;

/// An extended z-order curve for space-time indexing with non-points.
pub struct XZ3SFC {
    g: u32,
    x_min: f64,
    x_max: f64,
    y_min: f64,
    y_max: f64,
    z_min: f64,
    z_max: f64,
}

const LEVEL_TERMINATOR: Option<XElement> = None;

impl XZ3SFC {
    /// Maximum supported resolution. Above this, the `7 * (8.pow(g - i) - 1)`
    /// terms in `sequence_code` overflow `u64` (`7 * 8^21 > 2^64`).
    pub const MAX_G: u32 = 20;

    /// Create an 3D extended z-order curve in unprojected coordinates.
    ///
    /// # Panics
    ///
    /// Panics if `g > MAX_G` (20), the resolution above which the curve's
    /// index arithmetic overflows `u64`.
    #[must_use]
    pub fn wgs84(g: u32, z_min: f64, z_max: f64) -> Self {
        assert!(g <= Self::MAX_G, "resolution g must be <= {}", Self::MAX_G);
        XZ3SFC {
            g,
            x_min: -180.0,
            x_max: 180.0,
            y_min: -90.0,
            y_max: 90.0,
            z_min,
            z_max,
        }
    }

    /// General constructor for XZ3SFC.
    ///
    /// # Panics
    ///
    /// Panics if `g > MAX_G` (20).
    #[must_use]
    pub fn new(
        g: u32,
        x_min: f64,
        y_min: f64,
        z_min: f64,
        x_max: f64,
        y_max: f64,
        z_max: f64,
    ) -> Self {
        assert!(g <= Self::MAX_G, "resolution g must be <= {}", Self::MAX_G);
        XZ3SFC {
            g,
            x_min,
            x_max,
            y_min,
            y_max,
            z_min,
            z_max,
        }
    }

    fn x_size(&self) -> f64 {
        self.x_max - self.x_min
    }

    fn y_size(&self) -> f64 {
        self.y_max - self.y_min
    }

    fn z_size(&self) -> f64 {
        self.z_max - self.z_min
    }

    /// Compute the index for a bounding box with a time (z) component
    pub fn index(
        &self,
        x_min: f64,
        y_min: f64,
        z_min: f64,
        x_max: f64,
        y_max: f64,
        z_max: f64,
    ) -> u64 {
        let (nxmin, nymin, nzmin, nxmax, nymax, nzmax) =
            self.normalize(x_min, y_min, z_min, x_max, y_max, z_max);

        let max_dim = (nxmax - nxmin).max(nymax - nymin).max(nzmax - nzmin);

        let el_1 = max_dim.log(0.5).floor() as i32;

        let length = if el_1 as u32 >= self.g {
            self.g
        } else {
            let w2 = 0.5_f64.powi(el_1 + 1);
            if Self::predicate(nxmin, nxmax, w2)
                && Self::predicate(nymin, nymax, w2)
                && Self::predicate(nzmin, nzmax, w2)
            {
                (el_1 + 1) as u32
            } else {
                el_1 as u32
            }
        };

        self.sequence_code(nxmin, nymin, nzmin, length)
    }

    fn predicate(min: f64, max: f64, w2: f64) -> bool {
        max <= (min / w2).floor() * w2 + (2.0 * w2)
    }

    /// Compute the index range that are contained or overlap the bounding box.
    #[allow(clippy::too_many_arguments)]
    pub fn ranges(
        &self,
        xmin: f64,
        ymin: f64,
        zmin: f64,
        xmax: f64,
        ymax: f64,
        zmax: f64,
        max_ranges: Option<u16>,
    ) -> Vec<Box<dyn IndexRange>> {
        let windows = {
            let (nxmin, nymin, nzmin, nxmax, nymax, nzmax) =
                self.normalize(xmin, ymin, zmin, xmax, ymax, zmax);
            &[QueryWindow {
                x_min: nxmin,
                y_min: nymin,
                z_min: nzmin,
                x_max: nxmax,
                y_max: nymax,
                z_max: nzmax,
            }]
        };

        let range_stop = max_ranges.unwrap_or(u16::MAX);
        self.ranges_impl(windows, range_stop)
    }

    fn ranges_impl(&self, query: &[QueryWindow], range_stop: u16) -> Vec<Box<dyn IndexRange>> {
        let mut ranges = Vec::with_capacity(100);

        let mut remaining = VecDeque::with_capacity(100);

        for el in XElement::level_one_elements() {
            remaining.push_back(Some(el));
        }
        remaining.push_back(LEVEL_TERMINATOR);

        let mut level = 1;

        while level < self.g && !remaining.is_empty() && ranges.len() < range_stop.into() {
            match remaining.pop_front() {
                Some(LEVEL_TERMINATOR) if !remaining.is_empty() => {
                    level += 1;
                    remaining.push_back(LEVEL_TERMINATOR);
                }
                Some(Some(oct)) => {
                    self.check_value(&oct, level, query, &mut ranges, &mut remaining);
                }
                _ => (),
            }
        }

        while let Some(el) = remaining.pop_front() {
            if let Some(oct) = el {
                let (min, max) =
                    self.sequence_interval(oct.x_min, oct.y_min, oct.z_min, level, false);
                ranges.push(Box::new(OverlappingRange::new(min, max)));
            } else {
                level += 1;
            }
        }

        ranges.sort();

        let mut current: Option<Box<dyn IndexRange>> = None;
        let mut results = vec![];
        for range in ranges {
            if let Some(cur) = current {
                if range.lower() <= cur.upper().saturating_add(1) {
                    let max = cur.upper().max(range.upper());
                    let min = cur.lower();
                    if cur.contained() && range.contained() {
                        current = Some(Box::new(CoveredRange::new(min, max)));
                    } else {
                        current = Some(Box::new(OverlappingRange::new(min, max)));
                    }
                } else {
                    results.push(cur);
                    current = Some(range);
                }
            } else {
                current = Some(range);
            }
        }

        if let Some(current) = current {
            results.push(current);
        }
        results
    }

    fn is_contained(oct: &XElement, query: &[QueryWindow]) -> bool {
        for q in query {
            if oct.is_contained(q) {
                return true;
            }
        }
        false
    }

    fn is_overlapped(oct: &XElement, query: &[QueryWindow]) -> bool {
        for q in query {
            if oct.is_overlapped(q) {
                return true;
            }
        }
        false
    }

    fn check_value(
        &self,
        oct: &XElement,
        level: u32,
        query: &[QueryWindow],
        ranges: &mut Vec<Box<dyn IndexRange>>,
        remaining: &mut VecDeque<Option<XElement>>,
    ) {
        if Self::is_contained(oct, query) {
            let (min, max) = self.sequence_interval(oct.x_min, oct.y_min, oct.z_min, level, false);
            ranges.push(Box::new(CoveredRange::new(min, max)));
        } else if Self::is_overlapped(oct, query) {
            let (min, max) = self.sequence_interval(oct.x_min, oct.y_min, oct.z_min, level, true);
            ranges.push(Box::new(OverlappingRange::new(min, max)));
            for el in oct.children() {
                remaining.push_back(Some(el));
            }
        }
    }

    fn normalize(
        &self,
        x_min: f64,
        y_min: f64,
        z_min: f64,
        x_max: f64,
        y_max: f64,
        z_max: f64,
    ) -> (f64, f64, f64, f64, f64, f64) {
        (
            (x_min - self.x_min) / self.x_size(),
            (y_min - self.y_min) / self.y_size(),
            (z_min - self.z_min) / self.z_size(),
            (x_max - self.x_min) / self.x_size(),
            (y_max - self.y_min) / self.y_size(),
            (z_max - self.z_min) / self.z_size(),
        )
    }

    /// Curve-code offset contributed by octant `q` (`0..=7`) at remaining
    /// resolution `k`: the number of cells skipped by stepping past octants
    /// `0..q` of a node whose subtree spans `k` more levels. Shared by
    /// `sequence_code` and `sequence_interval` so the kani overflow proof
    /// exercises the exact arithmetic used in production. Does not overflow for
    /// `k <= MAX_G` (see `code_offset_doesnt_overflow`).
    fn code_offset(q: u64, k: u32) -> u64 {
        div_floor(q * (8_u64.pow(k) - 1), 7)
    }

    fn sequence_code(&self, x: f64, y: f64, z: f64, length: u32) -> u64 {
        let mut x_min = 0.0;
        let mut y_min = 0.0;
        let mut z_min = 0.0;
        let mut x_max = 1.0;
        let mut y_max = 1.0;
        let mut z_max = 1.0;

        let mut cs = 0_u64;

        for i in 0..length {
            let x_center = (x_min + x_max) / 2.0;
            let y_center = (y_min + y_max) / 2.0;
            let z_center = (z_min + z_max) / 2.0;

            match (x < x_center, y < y_center, z < z_center) {
                (true, true, true) => {
                    cs += 1;
                    x_max = x_center;
                    y_max = y_center;
                    z_max = z_center;
                }
                (false, true, true) => {
                    cs += 1 + Self::code_offset(1, self.g - i);
                    x_min = x_center;
                    y_max = y_center;
                    z_max = z_center;
                }
                (true, false, true) => {
                    cs += 1 + Self::code_offset(2, self.g - i);
                    x_max = x_center;
                    y_min = y_center;
                    z_max = z_center;
                }
                (false, false, true) => {
                    cs += 1 + Self::code_offset(3, self.g - i);
                    x_min = x_center;
                    y_min = y_center;
                    z_max = z_center;
                }
                (true, true, false) => {
                    cs += 1 + Self::code_offset(4, self.g - i);
                    x_max = x_center;
                    y_max = y_center;
                    z_min = z_center;
                }
                (false, true, false) => {
                    cs += 1 + Self::code_offset(5, self.g - i);
                    x_min = x_center;
                    y_max = y_center;
                    z_min = z_center;
                }
                (true, false, false) => {
                    cs += 1 + Self::code_offset(6, self.g - i);
                    x_max = x_center;
                    y_min = y_center;
                    z_min = z_center;
                }
                (false, false, false) => {
                    cs += 1 + Self::code_offset(7, self.g - i);
                    x_min = x_center;
                    y_min = y_center;
                    z_min = z_center;
                }
            }
        }
        cs
    }

    fn sequence_interval(&self, x: f64, y: f64, z: f64, length: u32, partial: bool) -> (u64, u64) {
        let min = self.sequence_code(x, y, z, length);

        let max = if partial {
            min
        } else {
            min + Self::code_offset(1, self.g - length + 1)
        };

        (min, max)
    }
}

struct QueryWindow {
    x_min: f64,
    y_min: f64,
    z_min: f64,
    x_max: f64,
    y_max: f64,
    z_max: f64,
}

#[derive(Debug, PartialEq)]
struct XElement {
    x_min: f64,
    y_min: f64,
    z_min: f64,
    x_max: f64,
    y_max: f64,
    z_max: f64,
    length: f64,
}

impl XElement {
    fn new(
        x_min: f64,
        y_min: f64,
        z_min: f64,
        x_max: f64,
        y_max: f64,
        z_max: f64,
        length: f64,
    ) -> Self {
        XElement {
            x_min,
            y_min,
            z_min,
            x_max,
            y_max,
            z_max,
            length,
        }
    }

    fn xext(&self) -> f64 {
        self.x_max + self.length
    }

    fn yext(&self) -> f64 {
        self.y_max + self.length
    }

    fn zext(&self) -> f64 {
        self.z_max + self.length
    }

    fn level_one_elements() -> Vec<XElement> {
        XElement::new(0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0).children()
    }

    fn is_contained(&self, window: &QueryWindow) -> bool {
        window.x_min <= self.x_min
            && window.y_min <= self.y_min
            && window.z_min <= self.z_min
            && window.x_max >= self.xext()
            && window.y_max >= self.yext()
            && window.z_max >= self.zext()
    }

    fn is_overlapped(&self, window: &QueryWindow) -> bool {
        window.x_max >= self.x_min
            && window.y_max >= self.y_min
            && window.z_max >= self.z_min
            && window.x_min <= self.xext()
            && window.y_min <= self.yext()
            && window.z_min <= self.zext()
    }

    fn children(&self) -> Vec<XElement> {
        let x_center = (self.x_min + self.x_max) / 2.0;
        let y_center = (self.y_min + self.y_max) / 2.0;
        let z_center = (self.z_min + self.z_max) / 2.0;
        let len = self.length / 2.0;

        vec![
            XElement {
                x_min: self.x_min,
                x_max: x_center,
                y_min: self.y_min,
                y_max: y_center,
                z_min: self.z_min,
                z_max: z_center,
                length: len,
            },
            XElement {
                x_min: x_center,
                x_max: self.x_max,
                y_min: self.y_min,
                y_max: y_center,
                z_min: self.z_min,
                z_max: z_center,
                length: len,
            },
            XElement {
                x_min: self.x_min,
                x_max: x_center,
                y_min: y_center,
                y_max: self.y_max,
                z_min: self.z_min,
                z_max: z_center,
                length: len,
            },
            XElement {
                x_min: x_center,
                x_max: self.x_max,
                y_min: y_center,
                y_max: self.y_max,
                z_min: self.z_min,
                z_max: z_center,
                length: len,
            },
            XElement {
                x_min: self.x_min,
                x_max: x_center,
                y_min: self.y_min,
                y_max: y_center,
                z_min: z_center,
                z_max: self.z_max,
                length: len,
            },
            XElement {
                x_min: x_center,
                x_max: self.x_max,
                y_min: self.y_min,
                y_max: y_center,
                z_min: z_center,
                z_max: self.z_max,
                length: len,
            },
            XElement {
                x_min: self.x_min,
                x_max: x_center,
                y_min: y_center,
                y_max: self.y_max,
                z_min: z_center,
                z_max: self.z_max,
                length: len,
            },
            XElement {
                x_min: x_center,
                x_max: self.x_max,
                y_min: y_center,
                y_max: self.y_max,
                z_min: z_center,
                z_max: self.z_max,
                length: len,
            },
        ]
    }
}

#[cfg(kani)]
mod kani_proofs {
    use super::*;

    /// `XZ3SFC::code_offset` — the single arithmetic helper behind every
    /// `sequence_code`/`sequence_interval` curve-code term — never overflows
    /// `u64` under the `g <= MAX_G` bound enforced by the constructors. The
    /// resolution argument `k` (`g - i` with `i` in `0..length <= g`, or
    /// `g - length + 1`) lies in `1..=g`, and the octant `q` in `0..=7`, so the
    /// proof ranges over every value reachable in production.
    #[kani::proof]
    #[kani::unwind(33)]
    fn code_offset_doesnt_overflow() {
        let g: u32 = kani::any();
        kani::assume(g <= XZ3SFC::MAX_G);

        let k: u32 = kani::any();
        kani::assume(k >= 1 && k <= g);

        let q: u64 = kani::any();
        kani::assume(q <= 7);

        let _ = XZ3SFC::code_offset(q, k);
    }
}

#[cfg(test)]
mod tests {

    use super::*;

    #[test]
    fn test_bounding_boxes_query_polygon() {
        let sfc = XZ3SFC::wgs84(12, 0.0, 13000.0);

        let polygon = sfc.index(10.0, 10.0, 1000.0, 12.0, 12.0, 1000.0);

        let containing = [
            (9.0, 9.0, 900.0, 13.0, 13.0, 1100.0),
            (-180.0, -90.0, 900.0, 180.0, 90.0, 1100.0),
            (0.0, 0.0, 900.0, 180.0, 90.0, 1100.0),
            (0.0, 0.0, 900.0, 20.0, 20.0, 1100.0),
        ];

        let overlapping = [
            (11.0, 11.0, 900.0, 13.0, 13.0, 1100.0),
            (9.0, 9.0, 900.0, 11.0, 11.0, 1100.0),
            (10.5, 10.5, 900.0, 11.5, 11.5, 1100.0),
            (11.0, 11.0, 900.0, 11.0, 11.0, 1100.0),
        ];

        let disjoint = [
            (-180.0, -90.0, 900.0, 8.0, 8.0, 1100.0),
            (0.0, 0.0, 900.0, 8.0, 8.0, 1100.0),
            (9.0, 9.0, 900.0, 9.5, 9.5, 1100.0),
            (20.0, 20.0, 900.0, 180.0, 90.0, 1100.0),
        ];

        for bbox in &[containing, overlapping].concat() {
            let ranges = sfc.ranges(bbox.0, bbox.1, bbox.2, bbox.3, bbox.4, bbox.5, Some(10000));

            assert!(
                ranges
                    .iter()
                    .any(|r| r.lower() <= polygon && polygon <= r.upper())
            );
        }

        for bbox in &disjoint {
            let ranges = sfc.ranges(bbox.0, bbox.1, bbox.2, bbox.3, bbox.4, bbox.5, Some(10000));

            assert!(
                !ranges
                    .iter()
                    .any(|r| r.lower() <= polygon && polygon <= r.upper())
            );
        }
    }

    #[test]
    fn test_indexing() {
        let sfc = XZ3SFC::wgs84(12, 0.0, 100000.0);

        assert_eq!(
            sfc.index(-80.0, -45.0, 1000.0, -78.8, -40.0, 1000.0),
            3_681_700_138
        );

        assert_eq!(
            sfc.index(-80.0, -45.0, 2000.0, -78.8, -40.0, 2000.0),
            3_682_898_510
        );

        assert_eq!(
            sfc.index(80.0, 25.0, 2000.0, 87.8, 40.0, 2000.0),
            29_930_553_347
        );
    }

    #[test]
    fn test_queries() {
        let sfc = XZ3SFC::wgs84(12, 0.0, 100_000.0);

        let ranges = sfc.ranges(-80.0, -45.0, 900.0, -78.8, -40.0, 1100.0, None);

        assert_eq!(ranges.len(), 912);

        assert_eq!(ranges.first().map(|r| r.lower()), Some(1));

        assert_eq!(ranges.last().map(|r| r.upper()), Some(3_682_578_823));
    }
}

#[cfg(test)]
mod range_query_tests {
    use super::*;
    use quickcheck_macros::quickcheck;

    /// `index` must not panic on the float→int cast edge cases: a degenerate
    /// point box (`max_dim == 0`, where `log(0.5)` is `+inf` and the `el_1`
    /// cast saturates to `i32::MAX`), the full-extent box (`max_dim == 1`), and
    /// a sub-cell box.
    #[test]
    fn index_handles_cast_edge_cases() {
        let sfc = XZ3SFC::wgs84(12, 0.0, 100.0);
        let _ = sfc.index(10.0, 20.0, 5.0, 10.0, 20.0, 5.0); // point box -> length = g
        let _ = sfc.index(-180.0, -90.0, 0.0, 180.0, 90.0, 100.0); // full extent
        let _ = sfc.index(-179.999_9, -89.999_9, 0.0, -179.999_8, -89.999_8, 0.01);
    }

    /// Every `(code, element)` in the octree down to depth `g`.
    fn enumerate(sfc: &XZ3SFC) -> Vec<(u64, XElement)> {
        let mut out = Vec::new();
        let mut stack: Vec<(XElement, u32)> = XElement::level_one_elements()
            .into_iter()
            .map(|e| (e, 1))
            .collect();
        while let Some((e, level)) = stack.pop() {
            let code = sfc.sequence_code(e.x_min, e.y_min, e.z_min, level);
            if level < sfc.g {
                for c in e.children() {
                    stack.push((c, level + 1));
                }
            }
            out.push((code, e));
        }
        out
    }

    /// End-to-end property for the XZ3 range engine. For any query window and
    /// any `range_stop` (including the small values that force early
    /// `bottom_out`), `ranges_impl` must be
    ///
    ///  * COMPLETE — every element whose (enlarged) extent overlaps the query is covered
    ///    by some returned range, so no candidate object is missed, and
    ///  * SOUND — every element whose code lands in a `CoveredRange` is fully contained
    ///    in the query (overlapping ranges may spill, covered ones may not).
    ///
    /// Driven in normalized `[0, 1]` space with dyadic query coordinates so all
    /// element-boundary comparisons are exact in `f64`.
    #[quickcheck]
    fn xz3_ranges_complete_and_sound(
        g_seed: u8,
        coords: (u8, u8, u8, u8, u8, u8),
        range_stop: u8,
    ) -> bool {
        let g = u32::from(g_seed % 3) + 1; // 1..=3 (octree stays small)
        let sfc = XZ3SFC::wgs84(g, 0.0, 1.0);

        let denom = f64::from(1u32 << g); // 2^g
        let to_unit = |v: u8| f64::from(u32::from(v) % ((1u32 << g) + 1)) / denom;
        let (x0, y0, z0, x1, y1, z1) = coords;
        let (mut x_min, mut x_max) = (to_unit(x0), to_unit(x1));
        if x_min > x_max {
            core::mem::swap(&mut x_min, &mut x_max);
        }
        let (mut y_min, mut y_max) = (to_unit(y0), to_unit(y1));
        if y_min > y_max {
            core::mem::swap(&mut y_min, &mut y_max);
        }
        let (mut z_min, mut z_max) = (to_unit(z0), to_unit(z1));
        if z_min > z_max {
            core::mem::swap(&mut z_min, &mut z_max);
        }
        let window = QueryWindow {
            x_min,
            y_min,
            z_min,
            x_max,
            y_max,
            z_max,
        };

        // `0` means "no limit"; otherwise an aggressive cap that forces bottom-out.
        let range_stop = if range_stop == 0 {
            u16::MAX
        } else {
            u16::from(range_stop)
        };
        let ranges = sfc.ranges_impl(core::slice::from_ref(&window), range_stop);

        let elements = enumerate(&sfc);

        // Completeness.
        for (code, e) in &elements {
            if e.is_overlapped(&window)
                && !ranges
                    .iter()
                    .any(|r| r.lower() <= *code && *code <= r.upper())
            {
                return false;
            }
        }

        // Soundness of covered ranges.
        for (code, e) in &elements {
            for r in &ranges {
                if r.contained()
                    && r.lower() <= *code
                    && *code <= r.upper()
                    && !e.is_contained(&window)
                {
                    return false;
                }
            }
        }

        true
    }

    /// Real-world SFC query contract (the one `osmflat` relies on, and the one
    /// the `enumerate`-based test above does NOT exercise): a feature is stored
    /// under the single code `index(feature_box)`, and a bbox query returns
    /// `ranges(query_box)`. The contract is COMPLETENESS — if the feature box
    /// geometrically overlaps the query box, then `index(feature)` must land in
    /// one of the returned ranges, or the query silently drops a true hit.
    ///
    /// This is the 3D analogue of `xz2_index_in_ranges_when_overlaps`. The XZ2
    /// version of this test surfaced a copy-paste bug in `XZ2SFC::index` (its
    /// fit predicate checked the x-extent twice and never the y-extent), which
    /// assigned boundary-spanning features to an element too fine to contain
    /// them so queries missed them. `XZ3SFC::index` checks x, y, and z, so this
    /// guards against the same class of regression here.
    ///
    /// `cap == 0` maps to `max_ranges = None` (full refinement). Coordinates use
    /// denominator 255 (not a power of two) so they fall *between* element
    /// boundaries.
    #[quickcheck]
    fn xz3_index_in_ranges_when_overlaps(
        feat: (u8, u8, u8, u8, u8, u8),
        query: (u8, u8, u8, u8, u8, u8),
        cap: u8,
    ) -> quickcheck::TestResult {
        let sfc = XZ3SFC::new(8, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0);
        let to = |v: u8| f64::from(v) / 255.0;
        let mk = |(a, b, c, d, e, f): (u8, u8, u8, u8, u8, u8)| {
            let (mut xmin, mut xmax) = (to(a), to(d));
            if xmin > xmax {
                core::mem::swap(&mut xmin, &mut xmax);
            }
            let (mut ymin, mut ymax) = (to(b), to(e));
            if ymin > ymax {
                core::mem::swap(&mut ymin, &mut ymax);
            }
            let (mut zmin, mut zmax) = (to(c), to(f));
            if zmin > zmax {
                core::mem::swap(&mut zmin, &mut zmax);
            }
            (xmin, ymin, zmin, xmax, ymax, zmax)
        };
        let (fxmin, fymin, fzmin, fxmax, fymax, fzmax) = mk(feat);
        let (qxmin, qymin, qzmin, qxmax, qymax, qzmax) = mk(query);

        // Require positive-volume boxes (real features/queries, not degenerate).
        if fxmin == fxmax
            || fymin == fymax
            || fzmin == fzmax
            || qxmin == qxmax
            || qymin == qymax
            || qzmin == qzmax
        {
            return quickcheck::TestResult::discard();
        }
        // Ground truth: do the box INTERIORS overlap (strict, not mere touch)?
        let overlap = fxmin < qxmax
            && qxmin < fxmax
            && fymin < qymax
            && qymin < fymax
            && fzmin < qzmax
            && qzmin < fzmax;
        if !overlap {
            return quickcheck::TestResult::discard();
        }

        let code = sfc.index(fxmin, fymin, fzmin, fxmax, fymax, fzmax);
        let max_ranges = if cap == 0 { None } else { Some(u16::from(cap)) };
        let ranges = sfc.ranges(qxmin, qymin, qzmin, qxmax, qymax, qzmax, max_ranges);

        let covered = ranges
            .iter()
            .any(|r| r.lower() <= code && code <= r.upper());
        quickcheck::TestResult::from_bool(covered)
    }
}