@@ -33,6 +33,15 @@ function createIndex() {
33
33
return index ;
34
34
}
35
35
36
+ function createSmallIndex ( numItems , nodeSize ) {
37
+ const index = new Flatbush ( numItems , nodeSize ) ;
38
+ for ( let i = 0 ; i < 4 * numItems ; i += 4 ) {
39
+ index . add ( data [ i ] , data [ i + 1 ] , data [ i + 2 ] , data [ i + 3 ] ) ;
40
+ }
41
+ index . finish ( ) ;
42
+ return index ;
43
+ }
44
+
36
45
37
46
test ( 'indexes a bunch of rectangles' , ( t ) => {
38
47
const index = createIndex ( ) ;
@@ -45,6 +54,39 @@ test('indexes a bunch of rectangles', (t) => {
45
54
t . end ( ) ;
46
55
} ) ;
47
56
57
+ test ( 'skips sorting less than nodeSize number of rectangles' , ( t ) => {
58
+ const numItems = 14 ;
59
+ const nodeSize = 16 ;
60
+ const index = createSmallIndex ( numItems , nodeSize ) ;
61
+
62
+ // compute expected root box extents
63
+ let rootXMin = Infinity ;
64
+ let rootYMin = Infinity ;
65
+ let rootXMax = - Infinity ;
66
+ let rootYMax = - Infinity ;
67
+ for ( let i = 0 ; i < 4 * numItems ; i += 4 ) {
68
+ if ( data [ i ] < rootXMin ) rootXMin = data [ i ] ;
69
+ if ( data [ i + 1 ] < rootYMin ) rootYMin = data [ i + 1 ] ;
70
+ if ( data [ i + 2 ] > rootXMax ) rootXMax = data [ i + 2 ] ;
71
+ if ( data [ i + 3 ] > rootYMax ) rootYMax = data [ i + 3 ] ;
72
+ }
73
+
74
+ // sort should be skipped, ordered progressing indices expected
75
+ const expectedIndices = [ ] ;
76
+ for ( let i = 0 ; i < numItems ; ++ i ) {
77
+ expectedIndices . push ( i ) ;
78
+ }
79
+ expectedIndices . push ( 0 ) ;
80
+
81
+ const len = index . _boxes . length ;
82
+
83
+ t . same ( index . _indices , expectedIndices ) ;
84
+ t . equal ( len , ( numItems + 1 ) * 4 ) ;
85
+ t . same ( index . _boxes . subarray ( len - 4 , len ) , [ rootXMin , rootYMin , rootXMax , rootYMax ] ) ;
86
+
87
+ t . end ( ) ;
88
+ } ) ;
89
+
48
90
test ( 'performs bbox search' , ( t ) => {
49
91
const index = createIndex ( ) ;
50
92
0 commit comments