Skip to content

Commit ddf0f47

Browse files
committed
Improve search results vector size approximation
1 parent c0e65a0 commit ddf0f47

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

flatbush.h

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -304,32 +304,29 @@ inline size_t approximateResultsSize(const Box<ArrayType>& iBoxIndex,
304304
const auto wBoundsIndex = static_cast<const Box<double>>(iBoxIndex);
305305
const auto wBoundsSearch = static_cast<const Box<double>>(iBoxSearch);
306306

307-
// Calculate intersection area
308-
const auto wWidth = std::min(wBoundsIndex.mMaxX, wBoundsSearch.mMaxX) -
309-
std::max(wBoundsIndex.mMinX, wBoundsSearch.mMinX);
310-
const auto wHeight = std::min(wBoundsIndex.mMaxY, wBoundsSearch.mMaxY) -
311-
std::max(wBoundsIndex.mMinY, wBoundsSearch.mMinY);
312-
const auto wIntersectionArea = wWidth * wHeight;
307+
// Calculate index area
308+
const auto wIndexWidth = wBoundsIndex.mMaxX - wBoundsIndex.mMinX;
309+
const auto wIndexHeight = wBoundsIndex.mMaxY - wBoundsIndex.mMinY;
310+
const auto wIndexArea = wIndexWidth * wIndexHeight;
313311

314-
// Calculate search area
312+
// Calculate intersection area
315313
const auto wSearchWidth = wBoundsSearch.mMaxX - wBoundsSearch.mMinX;
316314
const auto wSearchHeight = wBoundsSearch.mMaxY - wBoundsSearch.mMinY;
317315
const auto wSearchArea = wSearchWidth * wSearchHeight;
318316

319-
if (wWidth <= 0 || wHeight <= 0 || wIntersectionArea <= 0 || wSearchWidth <= 0 ||
317+
if (wIndexWidth <= 0 || wIndexHeight <= 0 || wIndexArea <= 0 || wSearchWidth <= 0 ||
320318
wSearchHeight <= 0 || !std::isfinite(wSearchWidth) || !std::isfinite(wSearchHeight) ||
321319
!std::isfinite(wSearchArea) || wSearchArea <= 0) {
322320
return 0UL;
323321
}
324322

325323
// Approximate results size based as ratio of areas, assuming uniform distribution
326-
const auto wAreaRatio = wSearchArea / wIntersectionArea;
327-
324+
const auto wAreaRatio = wSearchArea / wIndexArea;
328325
if (!std::isfinite(wAreaRatio) || wAreaRatio > 1.0) {
329326
return iNumItems;
330327
}
331328

332-
return static_cast<size_t>(wAreaRatio * static_cast<double>(iNumItems));
329+
return iNumItems * static_cast<size_t>(std::min(1.0, wAreaRatio * 1.8));
333330
}
334331

335332
template <typename ArrayType>

0 commit comments

Comments
 (0)