rustial-engine 0.0.1

Framework-agnostic 2.5D map engine for rustial
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
//! Geographic and projected bounding boxes.
//!
//! # Overview
//!
//! This module defines two bounding-box types used throughout the rustial
//! workspace:
//!
//! - [`GeoBounds`] -- a geographic bounding box defined by its southwest and
//!   northeast corners in WGS-84 degrees.  This is the Rust equivalent of
//!   MapLibre GL JS's `LngLatBounds`.
//! - [`WorldBounds`] -- an axis-aligned bounding box in projected world space
//!   (meters, typically EPSG:3857 / Web Mercator).
//!
//! # Coordinate conventions
//!
//! | Type | SW corner | NE corner |
//! |------|-----------|-----------|
//! | `GeoBounds` | min lon, min lat | max lon, max lat |
//! | `WorldBounds` | min x, min y, min z | max x, max y, max z |
//!
//! # Antimeridian handling
//!
//! [`GeoBounds`] supports bounding boxes that cross the 180th meridian.
//! When `west > east` (i.e. `sw.lon > ne.lon`), the box is understood to
//! wrap around the antimeridian.  The [`contains_coord`](GeoBounds::contains_coord),
//! [`intersects`](GeoBounds::intersects), and
//! [`adjust_antimeridian`](GeoBounds::adjust_antimeridian) methods handle
//! this correctly.

use crate::coord::{GeoCoord, WorldCoord};
use std::fmt;

/// Wrap a value into the range `[min, max)`.
///
/// Equivalent to MapLibre's `wrap(value, min, max)` utility.
#[inline]
fn wrap(value: f64, min: f64, max: f64) -> f64 {
    let range = max - min;
    if range == 0.0 {
        return min;
    }
    ((value - min) % range + range) % range + min
}

// ---------------------------------------------------------------------------
// GeoBounds
// ---------------------------------------------------------------------------

/// A geographic bounding box defined by its southwest and northeast corners
/// in WGS-84 degrees.
///
/// This is the Rust equivalent of MapLibre GL JS's `LngLatBounds` and
/// Mapbox GL JS's `LngLatBounds`.
///
/// # Construction
///
/// | Method | Description |
/// |--------|-------------|
/// | [`new`](Self::new) | From explicit sw/ne corners |
/// | [`from_coords`](Self::from_coords) | From west, south, east, north |
/// | [`from_center_radius`](Self::from_center_radius) | Expand a center point by a radius in meters |
/// | `From<[f64; 4]>` | `[west, south, east, north]` |
///
/// # Antimeridian
///
/// When `sw.lon > ne.lon`, the bounds wrap across the 180th meridian.
/// Use [`adjust_antimeridian`](Self::adjust_antimeridian) to unwrap
/// into a contiguous longitude range (ne.lon may exceed 180).
///
/// # Display
///
/// Formats as `"GeoBounds(sw: (lat, lon), ne: (lat, lon))"`.
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct GeoBounds {
    /// Southwest corner (minimum latitude, minimum longitude).
    sw: GeoCoord,
    /// Northeast corner (maximum latitude, maximum longitude).
    ne: GeoCoord,
}

impl GeoBounds {
    /// Create a geographic bounding box from explicit southwest and
    /// northeast corners.
    ///
    /// The caller should ensure `sw.lat <= ne.lat`.  Longitude may
    /// have `sw.lon > ne.lon` to represent antimeridian-crossing bounds.
    #[inline]
    pub fn new(sw: GeoCoord, ne: GeoCoord) -> Self {
        Self { sw, ne }
    }

    /// Create a geographic bounding box from edge values.
    ///
    /// Equivalent to MapLibre's `new LngLatBounds([west, south, east, north])`.
    #[inline]
    pub fn from_coords(west: f64, south: f64, east: f64, north: f64) -> Self {
        Self {
            sw: GeoCoord::from_lat_lon(south, west),
            ne: GeoCoord::from_lat_lon(north, east),
        }
    }

    /// Create a bounding box by expanding a center point by a radius
    /// in meters.
    ///
    /// Uses the same approximation as MapLibre's `LngLatBounds.fromLngLat`:
    /// latitude accuracy is computed from the Earth's equatorial
    /// circumference, and longitude accuracy is scaled by `cos(lat)`.
    ///
    /// # Arguments
    ///
    /// * `center` - The center geographic coordinate.
    /// * `radius_m` - Distance in meters to expand in all directions.
    pub fn from_center_radius(center: GeoCoord, radius_m: f64) -> Self {
        const EARTH_CIRCUMFERENCE_M: f64 = 40_075_017.0;
        let lat_accuracy = 360.0 * radius_m / EARTH_CIRCUMFERENCE_M;
        let lon_accuracy = lat_accuracy / (std::f64::consts::PI / 180.0 * center.lat).cos();

        Self {
            sw: GeoCoord::from_lat_lon(center.lat - lat_accuracy, center.lon - lon_accuracy),
            ne: GeoCoord::from_lat_lon(center.lat + lat_accuracy, center.lon + lon_accuracy),
        }
    }

    // -- Accessors --------------------------------------------------------

    /// Southwest corner.
    #[inline]
    pub fn sw(&self) -> GeoCoord {
        self.sw
    }

    /// Northeast corner.
    #[inline]
    pub fn ne(&self) -> GeoCoord {
        self.ne
    }

    /// Northwest corner.
    #[inline]
    pub fn nw(&self) -> GeoCoord {
        GeoCoord::from_lat_lon(self.ne.lat, self.sw.lon)
    }

    /// Southeast corner.
    #[inline]
    pub fn se(&self) -> GeoCoord {
        GeoCoord::from_lat_lon(self.sw.lat, self.ne.lon)
    }

    /// West edge (longitude).
    #[inline]
    pub fn west(&self) -> f64 {
        self.sw.lon
    }

    /// South edge (latitude).
    #[inline]
    pub fn south(&self) -> f64 {
        self.sw.lat
    }

    /// East edge (longitude).
    #[inline]
    pub fn east(&self) -> f64 {
        self.ne.lon
    }

    /// North edge (latitude).
    #[inline]
    pub fn north(&self) -> f64 {
        self.ne.lat
    }

    /// Geographic center of the bounding box.
    ///
    /// Equivalent to MapLibre's `LngLatBounds.getCenter()`.
    #[inline]
    pub fn center(&self) -> GeoCoord {
        GeoCoord::from_lat_lon(
            (self.sw.lat + self.ne.lat) / 2.0,
            (self.sw.lon + self.ne.lon) / 2.0,
        )
    }

    // -- Mutation / extension ---------------------------------------------

    /// Extend the bounds to include a single geographic coordinate.
    ///
    /// Equivalent to MapLibre's `LngLatBounds.extend(LngLat)`.
    pub fn extend_coord(&mut self, coord: GeoCoord) {
        self.sw.lat = self.sw.lat.min(coord.lat);
        self.sw.lon = self.sw.lon.min(coord.lon);
        self.ne.lat = self.ne.lat.max(coord.lat);
        self.ne.lon = self.ne.lon.max(coord.lon);
    }

    /// Extend the bounds to include another bounding box.
    ///
    /// Equivalent to MapLibre's `LngLatBounds.extend(LngLatBounds)`.
    pub fn extend_bounds(&mut self, other: &GeoBounds) {
        self.sw.lat = self.sw.lat.min(other.sw.lat);
        self.sw.lon = self.sw.lon.min(other.sw.lon);
        self.ne.lat = self.ne.lat.max(other.ne.lat);
        self.ne.lon = self.ne.lon.max(other.ne.lon);
    }

    // -- Queries ----------------------------------------------------------

    /// Check if a geographic coordinate is within the bounding box.
    ///
    /// Handles antimeridian-crossing bounds (where `west > east`).
    ///
    /// Equivalent to MapLibre's `LngLatBounds.contains()`.
    pub fn contains_coord(&self, coord: &GeoCoord) -> bool {
        let lat_ok = self.sw.lat <= coord.lat && coord.lat <= self.ne.lat;

        let lon_ok = if self.sw.lon > self.ne.lon {
            // Antimeridian-crossing: longitude is inside when it is NOT
            // in the gap between ne.lon and sw.lon.
            self.sw.lon <= coord.lon || coord.lon <= self.ne.lon
        } else {
            self.sw.lon <= coord.lon && coord.lon <= self.ne.lon
        };

        lat_ok && lon_ok
    }

    /// Check if this bounding box intersects another.
    ///
    /// Returns `true` if the bounding boxes share any area, including
    /// cases where they only touch along an edge or at a corner.
    ///
    /// Properly handles cases where either or both bounding boxes cross
    /// the antimeridian.
    ///
    /// Equivalent to MapLibre's `LngLatBounds.intersects()`.
    pub fn intersects(&self, other: &GeoBounds) -> bool {
        // Latitude check (simple range overlap).
        let lat_ok = other.north() >= self.south() && other.south() <= self.north();
        if !lat_ok {
            return false;
        }

        // Check if either bound covers the full world (span >= 360 deg).
        let this_span = (self.east() - self.west()).abs();
        let other_span = (other.east() - other.west()).abs();
        if this_span >= 360.0 || other_span >= 360.0 {
            return true;
        }

        // Normalise longitudes to [-180, 180].
        let this_west = wrap(self.west(), -180.0, 180.0);
        let this_east = wrap(self.east(), -180.0, 180.0);
        let other_west = wrap(other.west(), -180.0, 180.0);
        let other_east = wrap(other.east(), -180.0, 180.0);

        // Strict inequality: equal values indicate zero-width (point),
        // not wrapping.
        let this_wraps = this_west > this_east;
        let other_wraps = other_west > other_east;

        if this_wraps && other_wraps {
            return true;
        }

        if this_wraps {
            return other_east >= this_west || other_west <= this_east;
        }

        if other_wraps {
            return this_east >= other_west || this_west <= other_east;
        }

        // Neither wraps: standard interval overlap.
        other_west <= this_east && other_east >= this_west
    }

    // -- Antimeridian adjustment ------------------------------------------

    /// Adjust bounds that cross the antimeridian so that
    /// `ne.lon >= sw.lon`.
    ///
    /// When `sw.lon > ne.lon` (crossing the 180th meridian), this adds
    /// 360 to `ne.lon` so the bounds form a contiguous longitude range.
    /// The resulting `ne.lon` may exceed 180.
    ///
    /// Equivalent to MapLibre's `LngLatBounds.adjustAntiMeridian()`.
    pub fn adjust_antimeridian(&self) -> Self {
        if self.sw.lon > self.ne.lon {
            Self {
                sw: self.sw,
                ne: GeoCoord {
                    lat: self.ne.lat,
                    lon: self.ne.lon + 360.0,
                    alt: self.ne.alt,
                },
            }
        } else {
            *self
        }
    }

    /// Return the bounding box as `[west, south, east, north]`.
    #[inline]
    pub fn to_array(&self) -> [f64; 4] {
        [self.sw.lon, self.sw.lat, self.ne.lon, self.ne.lat]
    }
}

impl fmt::Display for GeoBounds {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "GeoBounds(sw: ({:.6}, {:.6}), ne: ({:.6}, {:.6}))",
            self.sw.lat, self.sw.lon, self.ne.lat, self.ne.lon
        )
    }
}

// -- From conversions for GeoBounds ----------------------------------------

impl From<[f64; 4]> for GeoBounds {
    /// Create from `[west, south, east, north]`.
    #[inline]
    fn from(arr: [f64; 4]) -> Self {
        Self::from_coords(arr[0], arr[1], arr[2], arr[3])
    }
}

impl From<GeoBounds> for [f64; 4] {
    /// Convert to `[west, south, east, north]`.
    #[inline]
    fn from(b: GeoBounds) -> Self {
        b.to_array()
    }
}

// ---------------------------------------------------------------------------
// WorldBounds
// ---------------------------------------------------------------------------

/// An axis-aligned bounding box in projected world space (meters).
///
/// The coordinate system matches [`WorldCoord`]: right-handed Z-up,
/// X east, Y north, Z up.
///
/// `min` contains the component-wise minimum and `max` the component-wise
/// maximum.  For 2D tile operations `min.z` and `max.z` are typically 0.
///
/// # Construction
///
/// | Method | Description |
/// |--------|-------------|
/// | [`new`](Self::new) | From explicit min/max corners |
/// | [`from_min_max`](Self::from_min_max) | Alias for `new` |
///
/// # Display
///
/// Formats as `"WorldBounds(min: ..., max: ...)"`.
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct WorldBounds {
    /// Component-wise minimum corner (south-west-bottom).
    pub min: WorldCoord,
    /// Component-wise maximum corner (north-east-top).
    pub max: WorldCoord,
}

impl WorldBounds {
    /// Create a world-space bounding box from explicit min/max corners.
    #[inline]
    pub fn new(min: WorldCoord, max: WorldCoord) -> Self {
        Self { min, max }
    }

    /// Alias for [`new`](Self::new).
    #[inline]
    pub fn from_min_max(min: WorldCoord, max: WorldCoord) -> Self {
        Self { min, max }
    }

    /// Center point of the bounding box.
    #[inline]
    pub fn center(&self) -> WorldCoord {
        WorldCoord::new(
            (self.min.position.x + self.max.position.x) * 0.5,
            (self.min.position.y + self.max.position.y) * 0.5,
            (self.min.position.z + self.max.position.z) * 0.5,
        )
    }

    /// Size of the bounding box along each axis (meters).
    #[inline]
    pub fn size(&self) -> (f64, f64, f64) {
        (
            self.max.position.x - self.min.position.x,
            self.max.position.y - self.min.position.y,
            self.max.position.z - self.min.position.z,
        )
    }

    /// Whether a world-space point is inside the bounding box (inclusive).
    #[inline]
    pub fn contains_point(&self, point: &WorldCoord) -> bool {
        point.position.x >= self.min.position.x
            && point.position.x <= self.max.position.x
            && point.position.y >= self.min.position.y
            && point.position.y <= self.max.position.y
            && point.position.z >= self.min.position.z
            && point.position.z <= self.max.position.z
    }

    /// Whether two world-space bounding boxes overlap (inclusive).
    #[inline]
    pub fn intersects(&self, other: &WorldBounds) -> bool {
        self.min.position.x <= other.max.position.x
            && self.max.position.x >= other.min.position.x
            && self.min.position.y <= other.max.position.y
            && self.max.position.y >= other.min.position.y
            && self.min.position.z <= other.max.position.z
            && self.max.position.z >= other.min.position.z
    }

    /// Extend the bounds to include another bounding box.
    pub fn extend(&mut self, other: &WorldBounds) {
        self.min = WorldCoord::new(
            self.min.position.x.min(other.min.position.x),
            self.min.position.y.min(other.min.position.y),
            self.min.position.z.min(other.min.position.z),
        );
        self.max = WorldCoord::new(
            self.max.position.x.max(other.max.position.x),
            self.max.position.y.max(other.max.position.y),
            self.max.position.z.max(other.max.position.z),
        );
    }

    /// Extend the bounds to include a single world-space point.
    pub fn extend_point(&mut self, point: &WorldCoord) {
        self.min = WorldCoord::new(
            self.min.position.x.min(point.position.x),
            self.min.position.y.min(point.position.y),
            self.min.position.z.min(point.position.z),
        );
        self.max = WorldCoord::new(
            self.max.position.x.max(point.position.x),
            self.max.position.y.max(point.position.y),
            self.max.position.z.max(point.position.z),
        );
    }
}

impl fmt::Display for WorldBounds {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "WorldBounds(min: {}, max: {})", self.min, self.max)
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    // -- GeoBounds construction -------------------------------------------

    #[test]
    fn geo_bounds_new() {
        let sw = GeoCoord::from_lat_lon(40.7661, -73.9876);
        let ne = GeoCoord::from_lat_lon(40.8002, -73.9397);
        let b = GeoBounds::new(sw, ne);
        assert_eq!(b.sw(), sw);
        assert_eq!(b.ne(), ne);
    }

    #[test]
    fn geo_bounds_from_coords() {
        let b = GeoBounds::from_coords(-73.9876, 40.7661, -73.9397, 40.8002);
        assert!((b.west() - (-73.9876)).abs() < 1e-10);
        assert!((b.south() - 40.7661).abs() < 1e-10);
        assert!((b.east() - (-73.9397)).abs() < 1e-10);
        assert!((b.north() - 40.8002).abs() < 1e-10);
    }

    #[test]
    fn geo_bounds_from_array() {
        let b: GeoBounds = [-73.9876, 40.7661, -73.9397, 40.8002].into();
        assert!((b.west() - (-73.9876)).abs() < 1e-10);
        assert!((b.north() - 40.8002).abs() < 1e-10);
    }

    #[test]
    fn geo_bounds_to_array_roundtrip() {
        let b = GeoBounds::from_coords(-73.9876, 40.7661, -73.9397, 40.8002);
        let arr: [f64; 4] = b.into();
        assert_eq!(arr, b.to_array());
    }

    // -- GeoBounds corner/edge accessors ----------------------------------

    #[test]
    fn geo_bounds_corners() {
        let b = GeoBounds::from_coords(-73.9876, 40.7661, -73.9397, 40.8002);
        let nw = b.nw();
        assert!((nw.lat - 40.8002).abs() < 1e-10);
        assert!((nw.lon - (-73.9876)).abs() < 1e-10);
        let se = b.se();
        assert!((se.lat - 40.7661).abs() < 1e-10);
        assert!((se.lon - (-73.9397)).abs() < 1e-10);
    }

    // -- GeoBounds center -------------------------------------------------

    #[test]
    fn geo_bounds_center() {
        let b = GeoBounds::from_coords(-73.9876, 40.7661, -73.9397, 40.8002);
        let c = b.center();
        assert!((c.lon - (-73.96365)).abs() < 1e-4);
        assert!((c.lat - 40.78315).abs() < 1e-4);
    }

    // -- GeoBounds extend -------------------------------------------------

    #[test]
    fn geo_bounds_extend_coord() {
        let mut b = GeoBounds::from_coords(-73.9876, 40.7661, -73.9397, 40.8002);
        b.extend_coord(GeoCoord::from_lat_lon(41.0, -74.0));
        assert!((b.north() - 41.0).abs() < 1e-10);
        assert!((b.west() - (-74.0)).abs() < 1e-10);
    }

    #[test]
    fn geo_bounds_extend_bounds() {
        let mut a = GeoBounds::from_coords(-73.9876, 40.7661, -73.9397, 40.8002);
        let b = GeoBounds::from_coords(-74.0, 40.5, -73.5, 41.0);
        a.extend_bounds(&b);
        assert!((a.west() - (-74.0)).abs() < 1e-10);
        assert!((a.south() - 40.5).abs() < 1e-10);
        assert!((a.east() - (-73.5)).abs() < 1e-10);
        assert!((a.north() - 41.0).abs() < 1e-10);
    }

    // -- GeoBounds contains -----------------------------------------------

    #[test]
    fn geo_bounds_contains_inside() {
        let b = GeoBounds::from_coords(-73.9876, 40.7661, -73.9397, 40.8002);
        let p = GeoCoord::from_lat_lon(40.7789, -73.9567);
        assert!(b.contains_coord(&p));
    }

    #[test]
    fn geo_bounds_contains_outside() {
        let b = GeoBounds::from_coords(-73.9876, 40.7661, -73.9397, 40.8002);
        let p = GeoCoord::from_lat_lon(41.0, -73.9567);
        assert!(!b.contains_coord(&p));
    }

    #[test]
    fn geo_bounds_contains_antimeridian() {
        // Bounds crossing the antimeridian: from 170 E to 170 W (= -170)
        let b = GeoBounds::from_coords(170.0, -20.0, -170.0, -10.0);
        // A point at 175 E should be inside.
        let inside = GeoCoord::from_lat_lon(-15.0, 175.0);
        assert!(b.contains_coord(&inside));
        // A point at 0 should be outside.
        let outside = GeoCoord::from_lat_lon(-15.0, 0.0);
        assert!(!b.contains_coord(&outside));
    }

    // -- GeoBounds intersects ---------------------------------------------

    #[test]
    fn geo_bounds_intersects_overlapping() {
        let a = GeoBounds::from_coords(-74.0, 40.0, -73.0, 41.0);
        let b = GeoBounds::from_coords(-73.5, 40.5, -72.5, 41.5);
        assert!(a.intersects(&b));
        assert!(b.intersects(&a));
    }

    #[test]
    fn geo_bounds_intersects_disjoint() {
        let a = GeoBounds::from_coords(-74.0, 40.0, -73.0, 41.0);
        let b = GeoBounds::from_coords(10.0, 50.0, 11.0, 51.0);
        assert!(!a.intersects(&b));
    }

    #[test]
    fn geo_bounds_intersects_touching_edge() {
        let a = GeoBounds::from_coords(-74.0, 40.0, -73.0, 41.0);
        let b = GeoBounds::from_coords(-73.0, 41.0, -72.0, 42.0);
        assert!(a.intersects(&b));
    }

    #[test]
    fn geo_bounds_intersects_antimeridian_both_wrap() {
        let a = GeoBounds::from_coords(170.0, -20.0, -170.0, -10.0);
        let b = GeoBounds::from_coords(160.0, -25.0, -160.0, -5.0);
        assert!(a.intersects(&b));
    }

    #[test]
    fn geo_bounds_intersects_full_world() {
        let full = GeoBounds::from_coords(-180.0, -90.0, 180.0, 90.0);
        let small = GeoBounds::from_coords(10.0, 10.0, 11.0, 11.0);
        assert!(full.intersects(&small));
        assert!(small.intersects(&full));
    }

    // -- GeoBounds from_center_radius -------------------------------------

    #[test]
    fn geo_bounds_from_center_radius_zero() {
        let center = GeoCoord::from_lat_lon(40.7736, -73.9749);
        let b = GeoBounds::from_center_radius(center, 0.0);
        assert!((b.sw().lat - center.lat).abs() < 1e-10);
        assert!((b.ne().lat - center.lat).abs() < 1e-10);
    }

    #[test]
    fn geo_bounds_from_center_radius_100m() {
        let center = GeoCoord::from_lat_lon(40.7736, -73.9749);
        let b = GeoBounds::from_center_radius(center, 100.0);
        assert!(b.sw().lat < center.lat);
        assert!(b.ne().lat > center.lat);
        assert!(b.sw().lon < center.lon);
        assert!(b.ne().lon > center.lon);
        // Lat span: 2 * 360 * 100 / 40075017 ~ 0.001796 deg.
        let lat_span = b.ne().lat - b.sw().lat;
        assert!((lat_span - 0.001796).abs() < 0.0001);
    }

    // -- GeoBounds adjust_antimeridian ------------------------------------

    #[test]
    fn geo_bounds_adjust_antimeridian_no_wrap() {
        let b = GeoBounds::from_coords(-73.9876, 40.7661, -73.9397, 40.8002);
        let adjusted = b.adjust_antimeridian();
        assert_eq!(b, adjusted);
    }

    #[test]
    fn geo_bounds_adjust_antimeridian_wrap() {
        let b = GeoBounds::from_coords(175.0, -20.0, -178.0, -15.0);
        let adjusted = b.adjust_antimeridian();
        assert!((adjusted.sw().lon - 175.0).abs() < 1e-10);
        assert!((adjusted.ne().lon - 182.0).abs() < 1e-10);
    }

    // -- GeoBounds Display ------------------------------------------------

    #[test]
    fn geo_bounds_display() {
        let b = GeoBounds::from_coords(-73.9876, 40.7661, -73.9397, 40.8002);
        let s = format!("{b}");
        assert!(s.contains("GeoBounds"));
        assert!(s.contains("sw:"));
        assert!(s.contains("ne:"));
    }

    // -- WorldBounds construction -----------------------------------------

    #[test]
    fn world_bounds_new() {
        let b = WorldBounds::new(
            WorldCoord::new(-100.0, -200.0, 0.0),
            WorldCoord::new(100.0, 200.0, 50.0),
        );
        assert_eq!(b.min.position.x, -100.0);
        assert_eq!(b.max.position.y, 200.0);
    }

    // -- WorldBounds center -----------------------------------------------

    #[test]
    fn world_bounds_center() {
        let b = WorldBounds::new(
            WorldCoord::new(-100.0, -200.0, 0.0),
            WorldCoord::new(100.0, 200.0, 50.0),
        );
        let c = b.center();
        assert!((c.position.x).abs() < 1e-10);
        assert!((c.position.y).abs() < 1e-10);
        assert!((c.position.z - 25.0).abs() < 1e-10);
    }

    // -- WorldBounds size -------------------------------------------------

    #[test]
    fn world_bounds_size() {
        let b = WorldBounds::new(
            WorldCoord::new(-100.0, -200.0, 0.0),
            WorldCoord::new(100.0, 200.0, 50.0),
        );
        let (sx, sy, sz) = b.size();
        assert!((sx - 200.0).abs() < 1e-10);
        assert!((sy - 400.0).abs() < 1e-10);
        assert!((sz - 50.0).abs() < 1e-10);
    }

    // -- WorldBounds contains_point ---------------------------------------

    #[test]
    fn world_bounds_contains_point() {
        let b = WorldBounds::new(
            WorldCoord::new(-100.0, -200.0, 0.0),
            WorldCoord::new(100.0, 200.0, 50.0),
        );
        assert!(b.contains_point(&WorldCoord::new(0.0, 0.0, 25.0)));
        assert!(!b.contains_point(&WorldCoord::new(200.0, 0.0, 0.0)));
    }

    // -- WorldBounds intersects -------------------------------------------

    #[test]
    fn world_bounds_intersects() {
        let a = WorldBounds::new(
            WorldCoord::new(-100.0, -100.0, 0.0),
            WorldCoord::new(100.0, 100.0, 0.0),
        );
        let b = WorldBounds::new(
            WorldCoord::new(50.0, 50.0, 0.0),
            WorldCoord::new(200.0, 200.0, 0.0),
        );
        assert!(a.intersects(&b));
    }

    #[test]
    fn world_bounds_disjoint() {
        let a = WorldBounds::new(
            WorldCoord::new(-100.0, -100.0, 0.0),
            WorldCoord::new(-50.0, -50.0, 0.0),
        );
        let b = WorldBounds::new(
            WorldCoord::new(50.0, 50.0, 0.0),
            WorldCoord::new(200.0, 200.0, 0.0),
        );
        assert!(!a.intersects(&b));
    }

    // -- WorldBounds extend -----------------------------------------------

    #[test]
    fn world_bounds_extend() {
        let mut a = WorldBounds::new(
            WorldCoord::new(-100.0, -100.0, 0.0),
            WorldCoord::new(100.0, 100.0, 0.0),
        );
        let b = WorldBounds::new(
            WorldCoord::new(-200.0, 50.0, -10.0),
            WorldCoord::new(50.0, 300.0, 10.0),
        );
        a.extend(&b);
        assert!((a.min.position.x - (-200.0)).abs() < 1e-10);
        assert!((a.min.position.y - (-100.0)).abs() < 1e-10);
        assert!((a.max.position.y - 300.0).abs() < 1e-10);
    }

    #[test]
    fn world_bounds_extend_point() {
        let mut a = WorldBounds::new(
            WorldCoord::new(0.0, 0.0, 0.0),
            WorldCoord::new(10.0, 10.0, 0.0),
        );
        a.extend_point(&WorldCoord::new(-5.0, 15.0, 3.0));
        assert!((a.min.position.x - (-5.0)).abs() < 1e-10);
        assert!((a.max.position.y - 15.0).abs() < 1e-10);
        assert!((a.max.position.z - 3.0).abs() < 1e-10);
    }

    // -- WorldBounds Display ----------------------------------------------

    #[test]
    fn world_bounds_display() {
        let b = WorldBounds::new(
            WorldCoord::new(1.0, 2.0, 3.0),
            WorldCoord::new(4.0, 5.0, 6.0),
        );
        let s = format!("{b}");
        assert!(s.contains("WorldBounds"));
    }
}