u-geometry 0.1.0

Domain-agnostic computational geometry: primitives, polygons, NFP, collision detection, spatial indexing.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
//! Core geometric primitives.
//!
//! Provides fundamental 2D and 3D types used throughout the library.
//!
//! # Design
//!
//! Points and vectors are mathematically distinct: points live in affine space,
//! vectors in linear space. Operator overloading enforces this:
//! - `Point - Point = Vector`
//! - `Point + Vector = Point`
//! - `Vector + Vector = Vector`

use std::ops::{Add, Mul, Sub};

/// A 2D point.
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Point2 {
    pub x: f64,
    pub y: f64,
}

impl Point2 {
    /// Creates a new point.
    #[inline]
    pub fn new(x: f64, y: f64) -> Self {
        Self { x, y }
    }

    /// Origin point (0, 0).
    pub const ORIGIN: Self = Self { x: 0.0, y: 0.0 };

    /// Euclidean distance to another point.
    ///
    /// # Complexity
    /// O(1)
    #[inline]
    pub fn distance_to(&self, other: &Self) -> f64 {
        let dx = self.x - other.x;
        let dy = self.y - other.y;
        (dx * dx + dy * dy).sqrt()
    }

    /// Squared Euclidean distance (avoids sqrt).
    #[inline]
    pub fn distance_sq(&self, other: &Self) -> f64 {
        let dx = self.x - other.x;
        let dy = self.y - other.y;
        dx * dx + dy * dy
    }

    /// Converts to a tuple.
    #[inline]
    pub fn to_tuple(self) -> (f64, f64) {
        (self.x, self.y)
    }

    /// Creates from a tuple.
    #[inline]
    pub fn from_tuple(t: (f64, f64)) -> Self {
        Self { x: t.0, y: t.1 }
    }
}

impl From<(f64, f64)> for Point2 {
    fn from(t: (f64, f64)) -> Self {
        Self::from_tuple(t)
    }
}

impl From<Point2> for (f64, f64) {
    fn from(p: Point2) -> Self {
        p.to_tuple()
    }
}

impl Sub for Point2 {
    type Output = Vector2;

    fn sub(self, rhs: Self) -> Vector2 {
        Vector2::new(self.x - rhs.x, self.y - rhs.y)
    }
}

impl Add<Vector2> for Point2 {
    type Output = Point2;

    fn add(self, rhs: Vector2) -> Point2 {
        Point2::new(self.x + rhs.x, self.y + rhs.y)
    }
}

/// A 2D vector.
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Vector2 {
    pub x: f64,
    pub y: f64,
}

impl Vector2 {
    /// Creates a new vector.
    #[inline]
    pub fn new(x: f64, y: f64) -> Self {
        Self { x, y }
    }

    /// Zero vector.
    pub const ZERO: Self = Self { x: 0.0, y: 0.0 };

    /// Euclidean length.
    #[inline]
    pub fn length(&self) -> f64 {
        (self.x * self.x + self.y * self.y).sqrt()
    }

    /// Squared length (avoids sqrt).
    #[inline]
    pub fn length_sq(&self) -> f64 {
        self.x * self.x + self.y * self.y
    }

    /// Returns a normalized (unit-length) vector, or zero vector if length is ~0.
    #[inline]
    pub fn normalized(&self) -> Self {
        let len = self.length();
        if len < 1e-15 {
            Self::ZERO
        } else {
            Self::new(self.x / len, self.y / len)
        }
    }

    /// 2D cross product (z-component of the 3D cross product).
    ///
    /// Returns a positive value if `other` is counter-clockwise from `self`,
    /// negative if clockwise, zero if parallel.
    #[inline]
    pub fn cross(&self, other: &Self) -> f64 {
        self.x * other.y - self.y * other.x
    }

    /// Dot product.
    #[inline]
    pub fn dot(&self, other: &Self) -> f64 {
        self.x * other.x + self.y * other.y
    }

    /// Returns the perpendicular vector (rotated 90 degrees CCW).
    #[inline]
    pub fn perp(&self) -> Self {
        Self::new(-self.y, self.x)
    }
}

impl Add for Vector2 {
    type Output = Self;

    fn add(self, rhs: Self) -> Self {
        Self::new(self.x + rhs.x, self.y + rhs.y)
    }
}

impl Sub for Vector2 {
    type Output = Self;

    fn sub(self, rhs: Self) -> Self {
        Self::new(self.x - rhs.x, self.y - rhs.y)
    }
}

impl Mul<f64> for Vector2 {
    type Output = Self;

    fn mul(self, rhs: f64) -> Self {
        Self::new(self.x * rhs, self.y * rhs)
    }
}

/// A 2D line segment.
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Segment2 {
    pub start: Point2,
    pub end: Point2,
}

impl Segment2 {
    /// Creates a new segment.
    pub fn new(start: Point2, end: Point2) -> Self {
        Self { start, end }
    }

    /// Segment length.
    #[inline]
    pub fn length(&self) -> f64 {
        self.start.distance_to(&self.end)
    }

    /// Squared length.
    #[inline]
    pub fn length_sq(&self) -> f64 {
        self.start.distance_sq(&self.end)
    }

    /// Direction vector (not normalized).
    #[inline]
    pub fn direction(&self) -> Vector2 {
        self.end - self.start
    }

    /// Midpoint.
    #[inline]
    pub fn midpoint(&self) -> Point2 {
        Point2::new(
            (self.start.x + self.end.x) * 0.5,
            (self.start.y + self.end.y) * 0.5,
        )
    }
}

/// A 2D axis-aligned bounding box.
///
/// # Invariant
/// `min.x <= max.x` and `min.y <= max.y`.
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct AABB2 {
    /// Minimum corner.
    pub min: Point2,
    /// Maximum corner.
    pub max: Point2,
}

impl AABB2 {
    /// Creates an AABB from min/max corners.
    pub fn new(min_x: f64, min_y: f64, max_x: f64, max_y: f64) -> Self {
        Self {
            min: Point2::new(min_x, min_y),
            max: Point2::new(max_x, max_y),
        }
    }

    /// Creates an AABB enclosing a set of points.
    ///
    /// Returns `None` if the slice is empty.
    ///
    /// # Complexity
    /// O(n)
    pub fn from_points(points: &[Point2]) -> Option<Self> {
        let first = points.first()?;
        let mut min_x = first.x;
        let mut min_y = first.y;
        let mut max_x = first.x;
        let mut max_y = first.y;

        for p in points.iter().skip(1) {
            min_x = min_x.min(p.x);
            min_y = min_y.min(p.y);
            max_x = max_x.max(p.x);
            max_y = max_y.max(p.y);
        }

        Some(Self::new(min_x, min_y, max_x, max_y))
    }

    /// Width (x extent).
    #[inline]
    pub fn width(&self) -> f64 {
        self.max.x - self.min.x
    }

    /// Height (y extent).
    #[inline]
    pub fn height(&self) -> f64 {
        self.max.y - self.min.y
    }

    /// Area.
    #[inline]
    pub fn area(&self) -> f64 {
        self.width() * self.height()
    }

    /// Center point.
    #[inline]
    pub fn center(&self) -> Point2 {
        Point2::new(
            (self.min.x + self.max.x) * 0.5,
            (self.min.y + self.max.y) * 0.5,
        )
    }

    /// Whether this AABB contains a point.
    #[inline]
    pub fn contains_point(&self, p: &Point2) -> bool {
        p.x >= self.min.x && p.x <= self.max.x && p.y >= self.min.y && p.y <= self.max.y
    }

    /// Whether two AABBs intersect.
    #[inline]
    pub fn intersects(&self, other: &Self) -> bool {
        self.min.x <= other.max.x
            && self.max.x >= other.min.x
            && self.min.y <= other.max.y
            && self.max.y >= other.min.y
    }

    /// Returns the intersection of two AABBs, or `None` if they don't overlap.
    pub fn intersection(&self, other: &Self) -> Option<Self> {
        if !self.intersects(other) {
            return None;
        }
        Some(Self::new(
            self.min.x.max(other.min.x),
            self.min.y.max(other.min.y),
            self.max.x.min(other.max.x),
            self.max.y.min(other.max.y),
        ))
    }

    /// Returns the union (bounding box) of two AABBs.
    pub fn union(&self, other: &Self) -> Self {
        Self::new(
            self.min.x.min(other.min.x),
            self.min.y.min(other.min.y),
            self.max.x.max(other.max.x),
            self.max.y.max(other.max.y),
        )
    }

    /// Returns a new AABB expanded by `margin` on all sides.
    pub fn expand(&self, margin: f64) -> Self {
        Self::new(
            self.min.x - margin,
            self.min.y - margin,
            self.max.x + margin,
            self.max.y + margin,
        )
    }
}

// ======================== 3D Primitives ========================

/// A 3D point.
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Point3 {
    pub x: f64,
    pub y: f64,
    pub z: f64,
}

impl Point3 {
    /// Creates a new 3D point.
    #[inline]
    pub fn new(x: f64, y: f64, z: f64) -> Self {
        Self { x, y, z }
    }

    /// Origin point (0, 0, 0).
    pub const ORIGIN: Self = Self {
        x: 0.0,
        y: 0.0,
        z: 0.0,
    };

    /// Euclidean distance to another point.
    ///
    /// # Complexity
    /// O(1)
    #[inline]
    pub fn distance_to(&self, other: &Self) -> f64 {
        self.distance_sq(other).sqrt()
    }

    /// Squared Euclidean distance (avoids sqrt).
    #[inline]
    pub fn distance_sq(&self, other: &Self) -> f64 {
        let dx = self.x - other.x;
        let dy = self.y - other.y;
        let dz = self.z - other.z;
        dx * dx + dy * dy + dz * dz
    }

    /// Converts to a tuple.
    #[inline]
    pub fn to_tuple(self) -> (f64, f64, f64) {
        (self.x, self.y, self.z)
    }

    /// Creates from a tuple.
    #[inline]
    pub fn from_tuple(t: (f64, f64, f64)) -> Self {
        Self {
            x: t.0,
            y: t.1,
            z: t.2,
        }
    }

    /// Converts to an array `[x, y, z]`.
    #[inline]
    pub fn to_array(self) -> [f64; 3] {
        [self.x, self.y, self.z]
    }

    /// Creates from an array `[x, y, z]`.
    #[inline]
    pub fn from_array(a: [f64; 3]) -> Self {
        Self {
            x: a[0],
            y: a[1],
            z: a[2],
        }
    }
}

impl From<(f64, f64, f64)> for Point3 {
    fn from(t: (f64, f64, f64)) -> Self {
        Self::from_tuple(t)
    }
}

impl From<Point3> for (f64, f64, f64) {
    fn from(p: Point3) -> Self {
        p.to_tuple()
    }
}

impl From<[f64; 3]> for Point3 {
    fn from(a: [f64; 3]) -> Self {
        Self::from_array(a)
    }
}

impl Sub for Point3 {
    type Output = Vector3;

    fn sub(self, rhs: Self) -> Vector3 {
        Vector3::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z)
    }
}

impl Add<Vector3> for Point3 {
    type Output = Point3;

    fn add(self, rhs: Vector3) -> Point3 {
        Point3::new(self.x + rhs.x, self.y + rhs.y, self.z + rhs.z)
    }
}

impl Sub<Vector3> for Point3 {
    type Output = Point3;

    fn sub(self, rhs: Vector3) -> Point3 {
        Point3::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z)
    }
}

/// A 3D vector.
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Vector3 {
    pub x: f64,
    pub y: f64,
    pub z: f64,
}

impl Vector3 {
    /// Creates a new 3D vector.
    #[inline]
    pub fn new(x: f64, y: f64, z: f64) -> Self {
        Self { x, y, z }
    }

    /// Zero vector.
    pub const ZERO: Self = Self {
        x: 0.0,
        y: 0.0,
        z: 0.0,
    };

    /// Unit vector along the X axis.
    pub const UNIT_X: Self = Self {
        x: 1.0,
        y: 0.0,
        z: 0.0,
    };

    /// Unit vector along the Y axis.
    pub const UNIT_Y: Self = Self {
        x: 0.0,
        y: 1.0,
        z: 0.0,
    };

    /// Unit vector along the Z axis.
    pub const UNIT_Z: Self = Self {
        x: 0.0,
        y: 0.0,
        z: 1.0,
    };

    /// Euclidean length.
    #[inline]
    pub fn length(&self) -> f64 {
        self.length_sq().sqrt()
    }

    /// Squared length (avoids sqrt).
    #[inline]
    pub fn length_sq(&self) -> f64 {
        self.x * self.x + self.y * self.y + self.z * self.z
    }

    /// Returns a normalized (unit-length) vector, or zero vector if length is ~0.
    #[inline]
    pub fn normalized(&self) -> Self {
        let len = self.length();
        if len < 1e-15 {
            Self::ZERO
        } else {
            Self::new(self.x / len, self.y / len, self.z / len)
        }
    }

    /// Cross product.
    ///
    /// Returns a vector perpendicular to both `self` and `other`,
    /// following the right-hand rule.
    ///
    /// # Reference
    /// Standard 3D cross product: `a × b = (a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x)`
    #[inline]
    pub fn cross(&self, other: &Self) -> Self {
        Self::new(
            self.y * other.z - self.z * other.y,
            self.z * other.x - self.x * other.z,
            self.x * other.y - self.y * other.x,
        )
    }

    /// Dot product.
    #[inline]
    pub fn dot(&self, other: &Self) -> f64 {
        self.x * other.x + self.y * other.y + self.z * other.z
    }

    /// Converts to an array `[x, y, z]`.
    #[inline]
    pub fn to_array(self) -> [f64; 3] {
        [self.x, self.y, self.z]
    }
}

impl Add for Vector3 {
    type Output = Self;

    fn add(self, rhs: Self) -> Self {
        Self::new(self.x + rhs.x, self.y + rhs.y, self.z + rhs.z)
    }
}

impl Sub for Vector3 {
    type Output = Self;

    fn sub(self, rhs: Self) -> Self {
        Self::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z)
    }
}

impl Mul<f64> for Vector3 {
    type Output = Self;

    fn mul(self, rhs: f64) -> Self {
        Self::new(self.x * rhs, self.y * rhs, self.z * rhs)
    }
}

/// A 3D axis-aligned bounding box.
///
/// # Invariant
/// `min.x <= max.x`, `min.y <= max.y`, `min.z <= max.z`.
///
/// # Reference
/// Ericson (2005), "Real-Time Collision Detection", Ch. 4.2
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct AABB3 {
    /// Minimum corner.
    pub min: Point3,
    /// Maximum corner.
    pub max: Point3,
}

impl AABB3 {
    /// Creates an AABB from min/max coordinates.
    pub fn new(min_x: f64, min_y: f64, min_z: f64, max_x: f64, max_y: f64, max_z: f64) -> Self {
        Self {
            min: Point3::new(min_x, min_y, min_z),
            max: Point3::new(max_x, max_y, max_z),
        }
    }

    /// Creates an AABB from min corner and dimensions.
    pub fn from_min_size(min: Point3, width: f64, depth: f64, height: f64) -> Self {
        Self {
            min,
            max: Point3::new(min.x + width, min.y + depth, min.z + height),
        }
    }

    /// Creates an AABB enclosing a set of 3D points.
    ///
    /// Returns `None` if the slice is empty.
    ///
    /// # Complexity
    /// O(n)
    pub fn from_points(points: &[Point3]) -> Option<Self> {
        let first = points.first()?;
        let mut min_x = first.x;
        let mut min_y = first.y;
        let mut min_z = first.z;
        let mut max_x = first.x;
        let mut max_y = first.y;
        let mut max_z = first.z;

        for p in points.iter().skip(1) {
            min_x = min_x.min(p.x);
            min_y = min_y.min(p.y);
            min_z = min_z.min(p.z);
            max_x = max_x.max(p.x);
            max_y = max_y.max(p.y);
            max_z = max_z.max(p.z);
        }

        Some(Self::new(min_x, min_y, min_z, max_x, max_y, max_z))
    }

    /// Width (x extent).
    #[inline]
    pub fn width(&self) -> f64 {
        self.max.x - self.min.x
    }

    /// Depth (y extent).
    #[inline]
    pub fn depth(&self) -> f64 {
        self.max.y - self.min.y
    }

    /// Height (z extent).
    #[inline]
    pub fn height(&self) -> f64 {
        self.max.z - self.min.z
    }

    /// Volume.
    #[inline]
    pub fn volume(&self) -> f64 {
        self.width() * self.depth() * self.height()
    }

    /// Surface area.
    ///
    /// Useful as a BVH splitting heuristic (SAH).
    #[inline]
    pub fn surface_area(&self) -> f64 {
        let w = self.width();
        let d = self.depth();
        let h = self.height();
        2.0 * (w * d + w * h + d * h)
    }

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

    /// Whether this AABB contains a point.
    #[inline]
    pub fn contains_point(&self, p: &Point3) -> bool {
        p.x >= self.min.x
            && p.x <= self.max.x
            && p.y >= self.min.y
            && p.y <= self.max.y
            && p.z >= self.min.z
            && p.z <= self.max.z
    }

    /// Whether this AABB fully contains another.
    #[inline]
    pub fn contains(&self, other: &Self) -> bool {
        self.min.x <= other.min.x
            && self.min.y <= other.min.y
            && self.min.z <= other.min.z
            && self.max.x >= other.max.x
            && self.max.y >= other.max.y
            && self.max.z >= other.max.z
    }

    /// Whether two AABBs intersect.
    ///
    /// # Complexity
    /// O(1)
    #[inline]
    pub fn intersects(&self, other: &Self) -> bool {
        self.min.x <= other.max.x
            && self.max.x >= other.min.x
            && self.min.y <= other.max.y
            && self.max.y >= other.min.y
            && self.min.z <= other.max.z
            && self.max.z >= other.min.z
    }

    /// Returns the intersection of two AABBs, or `None` if they don't overlap.
    pub fn intersection(&self, other: &Self) -> Option<Self> {
        if !self.intersects(other) {
            return None;
        }
        Some(Self::new(
            self.min.x.max(other.min.x),
            self.min.y.max(other.min.y),
            self.min.z.max(other.min.z),
            self.max.x.min(other.max.x),
            self.max.y.min(other.max.y),
            self.max.z.min(other.max.z),
        ))
    }

    /// Returns the union (bounding box) of two AABBs.
    pub fn union(&self, other: &Self) -> Self {
        Self::new(
            self.min.x.min(other.min.x),
            self.min.y.min(other.min.y),
            self.min.z.min(other.min.z),
            self.max.x.max(other.max.x),
            self.max.y.max(other.max.y),
            self.max.z.max(other.max.z),
        )
    }

    /// Returns a new AABB expanded by `margin` on all sides.
    pub fn expand(&self, margin: f64) -> Self {
        Self::new(
            self.min.x - margin,
            self.min.y - margin,
            self.min.z - margin,
            self.max.x + margin,
            self.max.y + margin,
            self.max.z + margin,
        )
    }
}

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

    #[test]
    fn test_point2_distance() {
        let a = Point2::new(0.0, 0.0);
        let b = Point2::new(3.0, 4.0);
        assert!((a.distance_to(&b) - 5.0).abs() < 1e-10);
    }

    #[test]
    fn test_point2_sub_gives_vector() {
        let a = Point2::new(3.0, 5.0);
        let b = Point2::new(1.0, 2.0);
        let v = a - b;
        assert!((v.x - 2.0).abs() < 1e-10);
        assert!((v.y - 3.0).abs() < 1e-10);
    }

    #[test]
    fn test_point2_add_vector() {
        let p = Point2::new(1.0, 2.0);
        let v = Vector2::new(3.0, 4.0);
        let q = p + v;
        assert!((q.x - 4.0).abs() < 1e-10);
        assert!((q.y - 6.0).abs() < 1e-10);
    }

    #[test]
    fn test_vector2_operations() {
        let v = Vector2::new(3.0, 4.0);
        assert!((v.length() - 5.0).abs() < 1e-10);
        assert!((v.length_sq() - 25.0).abs() < 1e-10);

        let n = v.normalized();
        assert!((n.length() - 1.0).abs() < 1e-10);
    }

    #[test]
    fn test_vector2_cross() {
        let a = Vector2::new(1.0, 0.0);
        let b = Vector2::new(0.0, 1.0);
        assert!((a.cross(&b) - 1.0).abs() < 1e-10);
        assert!((b.cross(&a) - (-1.0)).abs() < 1e-10);
    }

    #[test]
    fn test_vector2_dot() {
        let a = Vector2::new(1.0, 2.0);
        let b = Vector2::new(3.0, 4.0);
        assert!((a.dot(&b) - 11.0).abs() < 1e-10);
    }

    #[test]
    fn test_vector2_perp() {
        let v = Vector2::new(1.0, 0.0);
        let p = v.perp();
        assert!((p.x - 0.0).abs() < 1e-10);
        assert!((p.y - 1.0).abs() < 1e-10);
    }

    #[test]
    fn test_vector2_normalized_zero() {
        let v = Vector2::ZERO;
        let n = v.normalized();
        assert!((n.length()).abs() < 1e-10);
    }

    #[test]
    fn test_segment2() {
        let s = Segment2::new(Point2::new(0.0, 0.0), Point2::new(3.0, 4.0));
        assert!((s.length() - 5.0).abs() < 1e-10);
        let mid = s.midpoint();
        assert!((mid.x - 1.5).abs() < 1e-10);
        assert!((mid.y - 2.0).abs() < 1e-10);
    }

    #[test]
    fn test_aabb2_from_points() {
        let points = vec![
            Point2::new(0.0, 0.0),
            Point2::new(10.0, 5.0),
            Point2::new(3.0, 8.0),
        ];
        let aabb = AABB2::from_points(&points).unwrap();
        assert!((aabb.min.x - 0.0).abs() < 1e-10);
        assert!((aabb.min.y - 0.0).abs() < 1e-10);
        assert!((aabb.max.x - 10.0).abs() < 1e-10);
        assert!((aabb.max.y - 8.0).abs() < 1e-10);
    }

    #[test]
    fn test_aabb2_from_points_empty() {
        assert!(AABB2::from_points(&[]).is_none());
    }

    #[test]
    fn test_aabb2_area() {
        let aabb = AABB2::new(0.0, 0.0, 10.0, 20.0);
        assert!((aabb.area() - 200.0).abs() < 1e-10);
    }

    #[test]
    fn test_aabb2_contains() {
        let aabb = AABB2::new(0.0, 0.0, 10.0, 10.0);
        assert!(aabb.contains_point(&Point2::new(5.0, 5.0)));
        assert!(aabb.contains_point(&Point2::new(0.0, 0.0))); // on boundary
        assert!(!aabb.contains_point(&Point2::new(11.0, 5.0)));
    }

    #[test]
    fn test_aabb2_intersection() {
        let a = AABB2::new(0.0, 0.0, 10.0, 10.0);
        let b = AABB2::new(5.0, 5.0, 15.0, 15.0);
        let int = a.intersection(&b).unwrap();
        assert!((int.min.x - 5.0).abs() < 1e-10);
        assert!((int.min.y - 5.0).abs() < 1e-10);
        assert!((int.max.x - 10.0).abs() < 1e-10);
        assert!((int.max.y - 10.0).abs() < 1e-10);

        let c = AABB2::new(20.0, 20.0, 30.0, 30.0);
        assert!(a.intersection(&c).is_none());
    }

    #[test]
    fn test_aabb2_union() {
        let a = AABB2::new(0.0, 0.0, 10.0, 10.0);
        let b = AABB2::new(5.0, 5.0, 15.0, 15.0);
        let u = a.union(&b);
        assert!((u.min.x - 0.0).abs() < 1e-10);
        assert!((u.min.y - 0.0).abs() < 1e-10);
        assert!((u.max.x - 15.0).abs() < 1e-10);
        assert!((u.max.y - 15.0).abs() < 1e-10);
    }

    #[test]
    fn test_aabb2_expand() {
        let aabb = AABB2::new(5.0, 5.0, 10.0, 10.0);
        let expanded = aabb.expand(1.0);
        assert!((expanded.min.x - 4.0).abs() < 1e-10);
        assert!((expanded.min.y - 4.0).abs() < 1e-10);
        assert!((expanded.max.x - 11.0).abs() < 1e-10);
        assert!((expanded.max.y - 11.0).abs() < 1e-10);
    }

    #[test]
    fn test_point2_from_tuple() {
        let p: Point2 = (3.0, 4.0).into();
        assert!((p.x - 3.0).abs() < 1e-10);
        assert!((p.y - 4.0).abs() < 1e-10);

        let t: (f64, f64) = p.into();
        assert!((t.0 - 3.0).abs() < 1e-10);
    }

    // ======================== 3D Tests ========================

    #[test]
    fn test_point3_distance() {
        let a = Point3::new(0.0, 0.0, 0.0);
        let b = Point3::new(1.0, 2.0, 2.0);
        assert!((a.distance_to(&b) - 3.0).abs() < 1e-10);
    }

    #[test]
    fn test_point3_sub_gives_vector() {
        let a = Point3::new(3.0, 5.0, 7.0);
        let b = Point3::new(1.0, 2.0, 3.0);
        let v = a - b;
        assert!((v.x - 2.0).abs() < 1e-10);
        assert!((v.y - 3.0).abs() < 1e-10);
        assert!((v.z - 4.0).abs() < 1e-10);
    }

    #[test]
    fn test_point3_add_vector() {
        let p = Point3::new(1.0, 2.0, 3.0);
        let v = Vector3::new(4.0, 5.0, 6.0);
        let q = p + v;
        assert!((q.x - 5.0).abs() < 1e-10);
        assert!((q.y - 7.0).abs() < 1e-10);
        assert!((q.z - 9.0).abs() < 1e-10);
    }

    #[test]
    fn test_point3_sub_vector() {
        let p = Point3::new(5.0, 7.0, 9.0);
        let v = Vector3::new(1.0, 2.0, 3.0);
        let q = p - v;
        assert!((q.x - 4.0).abs() < 1e-10);
        assert!((q.y - 5.0).abs() < 1e-10);
        assert!((q.z - 6.0).abs() < 1e-10);
    }

    #[test]
    fn test_point3_conversions() {
        let p = Point3::new(1.0, 2.0, 3.0);
        let t: (f64, f64, f64) = p.into();
        assert!((t.0 - 1.0).abs() < 1e-10);
        assert!((t.1 - 2.0).abs() < 1e-10);
        assert!((t.2 - 3.0).abs() < 1e-10);

        let p2: Point3 = (4.0, 5.0, 6.0).into();
        assert!((p2.x - 4.0).abs() < 1e-10);

        let a = p.to_array();
        assert!((a[0] - 1.0).abs() < 1e-10);
        let p3 = Point3::from_array([7.0, 8.0, 9.0]);
        assert!((p3.z - 9.0).abs() < 1e-10);
    }

    #[test]
    fn test_vector3_operations() {
        let v = Vector3::new(1.0, 2.0, 2.0);
        assert!((v.length() - 3.0).abs() < 1e-10);
        assert!((v.length_sq() - 9.0).abs() < 1e-10);

        let n = v.normalized();
        assert!((n.length() - 1.0).abs() < 1e-10);
    }

    #[test]
    fn test_vector3_cross() {
        let x = Vector3::UNIT_X;
        let y = Vector3::UNIT_Y;
        let z = x.cross(&y);
        assert!((z.x - 0.0).abs() < 1e-10);
        assert!((z.y - 0.0).abs() < 1e-10);
        assert!((z.z - 1.0).abs() < 1e-10);

        // Anti-commutativity
        let neg_z = y.cross(&x);
        assert!((neg_z.z - (-1.0)).abs() < 1e-10);
    }

    #[test]
    fn test_vector3_dot() {
        let a = Vector3::new(1.0, 2.0, 3.0);
        let b = Vector3::new(4.0, 5.0, 6.0);
        assert!((a.dot(&b) - 32.0).abs() < 1e-10); // 4+10+18
    }

    #[test]
    fn test_vector3_normalized_zero() {
        let v = Vector3::ZERO;
        let n = v.normalized();
        assert!(n.length() < 1e-10);
    }

    #[test]
    fn test_vector3_arithmetic() {
        let a = Vector3::new(1.0, 2.0, 3.0);
        let b = Vector3::new(4.0, 5.0, 6.0);
        let sum = a + b;
        assert!((sum.x - 5.0).abs() < 1e-10);
        let diff = b - a;
        assert!((diff.x - 3.0).abs() < 1e-10);
        let scaled = a * 2.0;
        assert!((scaled.z - 6.0).abs() < 1e-10);
    }

    #[test]
    fn test_aabb3_basics() {
        let aabb = AABB3::new(0.0, 0.0, 0.0, 10.0, 20.0, 30.0);
        assert!((aabb.width() - 10.0).abs() < 1e-10);
        assert!((aabb.depth() - 20.0).abs() < 1e-10);
        assert!((aabb.height() - 30.0).abs() < 1e-10);
        assert!((aabb.volume() - 6000.0).abs() < 1e-10);
        assert!((aabb.surface_area() - 2200.0).abs() < 1e-10); // 2*(200+300+600)
    }

    #[test]
    fn test_aabb3_from_min_size() {
        let aabb = AABB3::from_min_size(Point3::new(1.0, 2.0, 3.0), 4.0, 5.0, 6.0);
        assert!((aabb.max.x - 5.0).abs() < 1e-10);
        assert!((aabb.max.y - 7.0).abs() < 1e-10);
        assert!((aabb.max.z - 9.0).abs() < 1e-10);
    }

    #[test]
    fn test_aabb3_from_points() {
        let points = vec![
            Point3::new(0.0, 0.0, 0.0),
            Point3::new(10.0, 5.0, 3.0),
            Point3::new(3.0, 8.0, 7.0),
        ];
        let aabb = AABB3::from_points(&points).unwrap();
        assert!((aabb.min.x - 0.0).abs() < 1e-10);
        assert!((aabb.max.y - 8.0).abs() < 1e-10);
        assert!((aabb.max.z - 7.0).abs() < 1e-10);
    }

    #[test]
    fn test_aabb3_from_points_empty() {
        assert!(AABB3::from_points(&[]).is_none());
    }

    #[test]
    fn test_aabb3_contains_point() {
        let aabb = AABB3::new(0.0, 0.0, 0.0, 10.0, 10.0, 10.0);
        assert!(aabb.contains_point(&Point3::new(5.0, 5.0, 5.0)));
        assert!(aabb.contains_point(&Point3::new(0.0, 0.0, 0.0))); // on boundary
        assert!(!aabb.contains_point(&Point3::new(11.0, 5.0, 5.0)));
    }

    #[test]
    fn test_aabb3_contains_aabb() {
        let outer = AABB3::new(0.0, 0.0, 0.0, 10.0, 10.0, 10.0);
        let inner = AABB3::new(2.0, 2.0, 2.0, 8.0, 8.0, 8.0);
        assert!(outer.contains(&inner));
        assert!(!inner.contains(&outer));
    }

    #[test]
    fn test_aabb3_intersects() {
        let a = AABB3::new(0.0, 0.0, 0.0, 10.0, 10.0, 10.0);
        let b = AABB3::new(5.0, 5.0, 5.0, 15.0, 15.0, 15.0);
        assert!(a.intersects(&b));

        let c = AABB3::new(20.0, 20.0, 20.0, 30.0, 30.0, 30.0);
        assert!(!a.intersects(&c));
    }

    #[test]
    fn test_aabb3_intersection() {
        let a = AABB3::new(0.0, 0.0, 0.0, 10.0, 10.0, 10.0);
        let b = AABB3::new(5.0, 5.0, 5.0, 15.0, 15.0, 15.0);
        let int = a.intersection(&b).unwrap();
        assert!((int.min.x - 5.0).abs() < 1e-10);
        assert!((int.max.z - 10.0).abs() < 1e-10);
        assert!((int.volume() - 125.0).abs() < 1e-10); // 5*5*5

        let c = AABB3::new(20.0, 20.0, 20.0, 30.0, 30.0, 30.0);
        assert!(a.intersection(&c).is_none());
    }

    #[test]
    fn test_aabb3_union() {
        let a = AABB3::new(0.0, 0.0, 0.0, 10.0, 10.0, 10.0);
        let b = AABB3::new(5.0, 5.0, 5.0, 15.0, 15.0, 15.0);
        let u = a.union(&b);
        assert!((u.min.x - 0.0).abs() < 1e-10);
        assert!((u.max.x - 15.0).abs() < 1e-10);
        assert!((u.volume() - 3375.0).abs() < 1e-10); // 15*15*15
    }

    #[test]
    fn test_aabb3_expand() {
        let aabb = AABB3::new(5.0, 5.0, 5.0, 10.0, 10.0, 10.0);
        let expanded = aabb.expand(1.0);
        assert!((expanded.min.x - 4.0).abs() < 1e-10);
        assert!((expanded.max.z - 11.0).abs() < 1e-10);
    }

    #[test]
    fn test_aabb3_center() {
        let aabb = AABB3::new(0.0, 0.0, 0.0, 10.0, 20.0, 30.0);
        let c = aabb.center();
        assert!((c.x - 5.0).abs() < 1e-10);
        assert!((c.y - 10.0).abs() < 1e-10);
        assert!((c.z - 15.0).abs() < 1e-10);
    }

    // ======================== Serde Tests ========================

    #[cfg(feature = "serde")]
    mod serde_tests {
        use super::*;

        #[test]
        fn test_point2_roundtrip() {
            let p = Point2::new(3.14, 2.72);
            let json = serde_json::to_string(&p).unwrap();
            let p2: Point2 = serde_json::from_str(&json).unwrap();
            assert_eq!(p, p2);
        }

        #[test]
        fn test_vector2_roundtrip() {
            let v = Vector2::new(-1.0, 5.5);
            let json = serde_json::to_string(&v).unwrap();
            let v2: Vector2 = serde_json::from_str(&json).unwrap();
            assert_eq!(v, v2);
        }

        #[test]
        fn test_segment2_roundtrip() {
            let s = Segment2::new(Point2::new(0.0, 0.0), Point2::new(10.0, 20.0));
            let json = serde_json::to_string(&s).unwrap();
            let s2: Segment2 = serde_json::from_str(&json).unwrap();
            assert_eq!(s, s2);
        }

        #[test]
        fn test_aabb2_roundtrip() {
            let aabb = AABB2::new(1.0, 2.0, 10.0, 20.0);
            let json = serde_json::to_string(&aabb).unwrap();
            let aabb2: AABB2 = serde_json::from_str(&json).unwrap();
            assert_eq!(aabb, aabb2);
        }

        #[test]
        fn test_point3_roundtrip() {
            let p = Point3::new(1.0, 2.0, 3.0);
            let json = serde_json::to_string(&p).unwrap();
            let p2: Point3 = serde_json::from_str(&json).unwrap();
            assert_eq!(p, p2);
        }

        #[test]
        fn test_vector3_roundtrip() {
            let v = Vector3::new(4.0, 5.0, 6.0);
            let json = serde_json::to_string(&v).unwrap();
            let v2: Vector3 = serde_json::from_str(&json).unwrap();
            assert_eq!(v, v2);
        }

        #[test]
        fn test_aabb3_roundtrip() {
            let aabb = AABB3::new(0.0, 0.0, 0.0, 10.0, 20.0, 30.0);
            let json = serde_json::to_string(&aabb).unwrap();
            let aabb2: AABB3 = serde_json::from_str(&json).unwrap();
            assert_eq!(aabb, aabb2);
        }
    }
}