Bucket Search Algorithm

Bucket search algorithm: Applicable for nearest neighbour problems

This script implements a bucket search algorithm. Two functions are included:

MakeBuckets – this function creates the 4D bucket structure. It has three input arguments:
– m (model object)    =    model to set the buckets up for.
– size (number)    =    size of the buckets.
– flag (flag)    =    to identify which nodes to put into the buckets.

MakeBuckets returns an object containing the bucket structure. In addition to the buckets the object has the following extra properties which may be useful:
.xmax | .xmin | .ymax | .ymin | .zmax | .zmin    =    bounding coordinates for the bucket space.
.xdim | .ydim |.zdim    =    dimensions of the bucket space.
.xnum | .ynum | .znum    =    number of buckets in each axis.
.xsize | .ysize | .zsize    =    size of buckets (will be close to the input size argument).

FindBucket is a function which returns the bucket indices for a given point/node. It has two inputs:
– node    =    this is the node/point (.x/.y/.z property) which we want the indices for.
-arr    =    this is the bucket structure as returned from MakeBuckets.

FindBucket returns an array containing 3 integer values. They represent the X, Y and Z bucket indices for the input node/point.

Once the bucket indices are known the bucket of interest is given by: Bucket_Array[X_index][Y_index][Z_index].

In the example, the user inputs a search tolerence and then selects a node on one part and the script sketches the nearest node on the neighbouring part (within the tolerence). A timer is included for interest.

Note if the search distance is large or unknown then for large problems the kd tree search algorithm is likely to be more efficient.