Search code examples
algorithmmatlabimage-processingknn

find all points within a range to any point of an other set


I have two sets of points A and B.

I want to find all points in B that are within a certain range r to A, where a point b in B is said to be within range r to A if there is at least one point a in A whose (Euclidean) distance to b is equal or smaller to r.

Each of the both sets of points is a coherent set of points. They are generated from the voxel locations of two non overlapping objects.

In 1D this problem fairly easy: all points of B within [min(A)-r max(A)+r]

But I am in 3D.

What is the best way to do this?

I currently repetitively search for every point in A all points in B that within range using some knn algorithm (ie. matlab's rangesearch) and then unite all those sets. But I got a feeling that there should be a better way to do this. I'd prefer a high level/vectorized solution in matlab, but pseudo code is fine too :)

I also thought of writing all the points to images and using image dilation on object A with a radius of r. But that sounds like quite an overhead.


Solution

  • You can use a k-d tree to store all points of A.

    Iterate points b of B, and for each point - find the nearest point in A (let it be a) in the k-d tree. The point b should be included in the result if and only if the distance d(a,b) is smaller then r.

    Complexity will be O(|B| * log(|A|) + |A|*log(|A|))