0

I have a bunch of spheres with different radius scattered all over the place in a 3D system.

What would be the fastest way to determine what sphere a point is inside, and if it is inside more than one - also the closest sphere based on the sphere's center.

The bruteforce method is to simply loop over all spheres, calculate the distance to the point, check if that distance is smaller than the sphere's radius and then find the sphere with the shortest disrance.

However, I got a couple million points to check (with about 100k spheres), so this would be incredibly slow.

Another idea of mine would be to build some kind of BVH Acceleration structure and save for each cell what sphere is the closest. However, there are also cases where one cell could be overlapped by two or more spheres etc. So a lot of edge-cases to consider.

And after all, the computation of the BVH tree should not be slower than the bruteforce.

Any ideas?

buddemat
  • 4,552
  • 14
  • 29
  • 49
Daniela
  • 169
  • 2
  • 5

2 Answers2

0

I ended up doing it with the brute force method on the GPU via OpenCL - that's super fast :)

Daniela
  • 169
  • 2
  • 5
0

Maybe a Sweep and Prune¹-esque approach could work here?

Handwavy algorithm (2D case):

  1. Create two arrays Ax and Ay.
  2. Pick one circle out of n circles and project into onto the x-axis, i.e store the x-component of the circles center plusminus the radius in Ax. Project the circle onto the y-axis as well.
  3. Repeat step 2 for all remaining circles.
  4. Store the components for each point in Ax and Ay as well.
  5. Sort Ax and Ay

From here on, a point P can only be within a sphere S iff it is contained within all three intervals of S.

Now, it's possible to determine for each point if it is contained by a sphere: Iterate over Ax and increment a counter k each time you "enter" an interval and decrement k when you "exit" an interval. If the counter is k when you sweep into a point, then the point is contained by a set I of k intervals. Check if the point is contained by any corresponding interval of I in Ay².

Again, sortedness of Ax and Ay should be of help when finding what sphere was closest to a point.

I'm confident that this approach can be (much) improved upon, and in practice the parallelized brute-force could be faster still.

Handwavy algorithm (3D case): Analogous to the 2D case.

¹. http://www.cs.jhu.edu/~cohen/Publications/icollide.pdf

². I obviously omit lots of d̶e̶t̶a̶i̶l̶s̶ ̶I̶ ̶h̶a̶v̶e̶ ̶y̶e̶t̶ ̶t̶o̶ ̶f̶i̶g̶u̶r̶e̶ ̶o̶u̶t boring bookkeeping details.

Joakim Thorén
  • 1,111
  • 10
  • 17