Skip to content

Commit d948b04

Browse files
committed
pass item bbox to filterFn in search
1 parent 7277cd2 commit d948b04

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,21 @@ Returns an array of indices of items intersecting or touching a given bounding b
9292
const ids = index.search(10, 10, 20, 20);
9393
```
9494

95-
If given a `filterFn`, calls it on every found item (passing an item index)
95+
If given a `filterFn`, calls it on every found item (passing the item's index & bounding box coordinates)
9696
and only includes it if the function returned a truthy value.
9797

9898
```js
9999
const ids = index.search(10, 10, 20, 20, (i) => items[i].foo === 'bar');
100100
```
101101

102+
Alternatively, instead of using the array of indices returned by `search`, you can handle the results in the function:
103+
104+
```js
105+
index.search(10, 10, 20, 20, (i, x0, y0, x1, y1) => {
106+
console.log(`Item found: ${items[i]}, bbox: ${x0} ${y0} ${x1} ${y1}`);
107+
})
108+
```
109+
102110
#### `index.neighbors(x, y[, maxResults, maxDistance, filterFn])`
103111

104112
Returns an array of item indices in order of distance from the given `x, y`
@@ -109,7 +117,10 @@ const ids = index.neighbors(10, 10, 5); // returns 5 ids
109117
```
110118

111119
`maxResults` and `maxDistance` are `Infinity` by default.
112-
Also accepts a `filterFn` similar to `index.search`.
120+
121+
If given a `filterFn`, calls it on items that potentially belong to the results (passing the item's index)
122+
and only includes an item if the function returned a truthy value.
123+
Unlike `search`, it shouldn't be used for handling results.
113124

114125
#### `Flatbush.from(data[, byteOffset])`
115126

index.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ export default class Flatbush {
206206
* @param {number} minY
207207
* @param {number} maxX
208208
* @param {number} maxY
209-
* @param {(index: number) => boolean} [filterFn] An optional function for filtering the results.
209+
* @param {(index: number, x0: number, y0: number, x1: number, y1: number) => boolean} [filterFn] An optional function that is called on every found item; if supplied, only items for which this function returns true will be included in the results array.
210210
* @returns {number[]} An array of indices of items intersecting or touching the given bounding box.
211211
*/
212212
search(minX, minY, maxX, maxY, filterFn) {
@@ -226,17 +226,21 @@ export default class Flatbush {
226226
// search through child nodes
227227
for (let /** @type number */ pos = nodeIndex; pos < end; pos += 4) {
228228
// check if node bbox intersects with query bbox
229-
if (maxX < this._boxes[pos]) continue; // maxX < nodeMinX
230-
if (maxY < this._boxes[pos + 1]) continue; // maxY < nodeMinY
231-
if (minX > this._boxes[pos + 2]) continue; // minX > nodeMaxX
232-
if (minY > this._boxes[pos + 3]) continue; // minY > nodeMaxY
229+
const x0 = this._boxes[pos];
230+
if (maxX < x0) continue;
231+
const y0 = this._boxes[pos + 1];
232+
if (maxY < y0) continue;
233+
const x1 = this._boxes[pos + 2];
234+
if (minX > x1) continue;
235+
const y1 = this._boxes[pos + 3];
236+
if (minY > y1) continue;
233237

234238
const index = this._indices[pos >> 2] | 0;
235239

236240
if (nodeIndex >= this.numItems * 4) {
237241
queue.push(index); // node; add it to the search queue
238242

239-
} else if (filterFn === undefined || filterFn(index)) {
243+
} else if (filterFn === undefined || filterFn(index, x0, y0, x1, y1)) {
240244
results.push(index); // leaf item
241245
}
242246
}

0 commit comments

Comments
 (0)