Skip to content

Commit 6cef6f2

Browse files
authored
pass item bbox to filterFn in search (#68)
1 parent 511b50d commit 6cef6f2

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
@@ -210,7 +210,7 @@ export default class Flatbush {
210210
* @param {number} minY
211211
* @param {number} maxX
212212
* @param {number} maxY
213-
* @param {(index: number) => boolean} [filterFn] An optional function for filtering the results.
213+
* @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.
214214
* @returns {number[]} An array of indices of items intersecting or touching the given bounding box.
215215
*/
216216
search(minX, minY, maxX, maxY, filterFn) {
@@ -230,17 +230,21 @@ export default class Flatbush {
230230
// search through child nodes
231231
for (let /** @type number */ pos = nodeIndex; pos < end; pos += 4) {
232232
// check if node bbox intersects with query bbox
233-
if (maxX < this._boxes[pos]) continue; // maxX < nodeMinX
234-
if (maxY < this._boxes[pos + 1]) continue; // maxY < nodeMinY
235-
if (minX > this._boxes[pos + 2]) continue; // minX > nodeMaxX
236-
if (minY > this._boxes[pos + 3]) continue; // minY > nodeMaxY
233+
const x0 = this._boxes[pos];
234+
if (maxX < x0) continue;
235+
const y0 = this._boxes[pos + 1];
236+
if (maxY < y0) continue;
237+
const x1 = this._boxes[pos + 2];
238+
if (minX > x1) continue;
239+
const y1 = this._boxes[pos + 3];
240+
if (minY > y1) continue;
237241

238242
const index = this._indices[pos >> 2] | 0;
239243

240244
if (nodeIndex >= this.numItems * 4) {
241245
queue.push(index); // node; add it to the search queue
242246

243-
} else if (filterFn === undefined || filterFn(index)) {
247+
} else if (filterFn === undefined || filterFn(index, x0, y0, x1, y1)) {
244248
results.push(index); // leaf item
245249
}
246250
}

0 commit comments

Comments
 (0)