11use fenris_geometry:: AxisAlignedBoundingBox ;
2- use nalgebra:: { point, U2 } ;
2+ use nalgebra:: { DefaultAllocator , DimName , OPoint , point, U2 } ;
3+ use nalgebra:: allocator:: Allocator ;
4+ use fenris:: allocators:: DimAllocator ;
35
46#[ test]
57fn aabb_intersects_2d ( ) {
@@ -39,4 +41,75 @@ fn aabb_intersects_2d() {
3941 assert_intersection ! ( Aabb :: new( point![ 1.5 , 1.5 ] , point![ 4.5 , 3.5 ] ) ) ;
4042 assert_intersection ! ( Aabb :: new( point![ 0.0 , 0.0 ] , point![ 2.0 , 2.0 ] ) ) ;
4143 assert_intersection ! ( Aabb :: new( point![ 0.0 , 0.0 ] , point![ 5.0 , 4.0 ] ) ) ;
44+ }
45+
46+ /// Helper function for collecting corners from corner iterator.
47+ fn corners < D : DimName > ( aabb : & AxisAlignedBoundingBox < f64 , D > ) -> Vec < OPoint < f64 , D > >
48+ where
49+ DefaultAllocator : DimAllocator < f64 , D >
50+ {
51+ aabb. corners_iter ( ) . collect ( )
52+ }
53+
54+ fn compare_unordered_points < D : DimName > ( points1 : & [ OPoint < f64 , D > ] , points2 : & [ OPoint < f64 , D > ] ) -> bool
55+ where
56+ DefaultAllocator : Allocator < f64 , D >
57+ {
58+ assert_eq ! ( points1. len( ) , points2. len( ) ) ;
59+ // This is O(n^2) but is acceptable for use in tests here
60+ points1. iter ( )
61+ . all ( |p1| points2. contains ( p1) )
62+ }
63+
64+ macro_rules! assert_unordered_eq {
65+ ( $p1: expr, $p2: expr) => {
66+ {
67+ let eq = compare_unordered_points( ( & $p1) . as_ref( ) , ( & $p2) . as_ref( ) ) ;
68+ if !eq {
69+ dbg!( & $p1, & $p2) ;
70+ assert!( eq, "Point lists do not contain the same points" ) ;
71+ }
72+ }
73+ }
74+ }
75+
76+
77+ #[ test]
78+ fn test_aabb_corners_iter ( ) {
79+
80+ // 1D
81+ {
82+ let aabb = AxisAlignedBoundingBox :: new ( point ! [ 3.0 ] , point ! [ 4.0 ] ) ;
83+ assert_eq ! ( corners( & aabb) , vec![ point![ 3.0 ] , point![ 4.0 ] ] ) ;
84+ }
85+
86+ // 2D
87+ {
88+ let aabb = AxisAlignedBoundingBox :: new ( point ! [ 3.0 , 4.0 ] , point ! [ 5.0 , 6.0 ] ) ;
89+ let expected = [
90+ [ 3.0 , 4.0 ] ,
91+ [ 3.0 , 6.0 ] ,
92+ [ 5.0 , 4.0 ] ,
93+ [ 5.0 , 6.0 ]
94+ ] . map ( OPoint :: from) ;
95+ assert_unordered_eq ! ( corners( & aabb) , & expected) ;
96+ }
97+
98+ // 3D
99+ {
100+ let aabb = AxisAlignedBoundingBox :: new ( point ! [ 1.0 , 2.0 , 3.0 ] , point ! [ 4.0 , 5.0 , 6.0 ] ) ;
101+ let expected = [
102+ [ 1.0 , 2.0 , 3.0 ] ,
103+ [ 1.0 , 2.0 , 6.0 ] ,
104+ [ 1.0 , 5.0 , 3.0 ] ,
105+ [ 1.0 , 5.0 , 6.0 ] ,
106+ [ 4.0 , 2.0 , 3.0 ] ,
107+ [ 4.0 , 2.0 , 6.0 ] ,
108+ [ 4.0 , 5.0 , 3.0 ] ,
109+ [ 4.0 , 5.0 , 6.0 ]
110+ ] . map ( OPoint :: from) ;
111+ assert_unordered_eq ! ( corners( & aabb) , expected) ;
112+ }
113+
114+
42115}
0 commit comments