I am looking for an efficient algorithm that for a space with a known height, width and length, given a fixed radius R, and a list of points N with three-dimensional coordinates in this space will find all points within a fixed radius R of an arbitrary point on the grid. This request will be executed many times with different points, so an expensive preprocessing / sorting step in exchange for quick requests may be worth it. This is a slightly narrow application step I'm working on, so any time I can cut off from it is useful
Things I've tried so far:
- naive algorithm, iterating over all points and calculating the distance
. Place space in a grid with cubes of length R and place dots in them. Thus, for each point I should always request immediate neighboring buckets. It speeds up significantly
- I tried to use the Manhattan distance as a heuristic. That is, within the buckets, before calculating the distance to any point, use the distance in Manhattan to filter out those that cannot be in radius R (that is, those with Manhattan distance <= sqrt (3) * P). I thought this would offer acceleration, since it only needs to add instead of multiplication, but it actually slowed down the program a bit
EDIT: to compare distances, I use the square of the distances to eliminate the need to use the sqrt function.
Obviously, there will be some restriction on how much I can speed this up, but I could use any suggestions on things to try now.
Not that it probably mattered at the algorithmic level, but I work on C.
source share