I have created a structure and populated an array with variables that can be thought of as coordinates in the x y z plane, or as distance in 3 dimensions from a point.
for (a=1; a<=lst; a++) {
for (b=1; b<=hst; b++) {
for (c=1; c<=wst; c++) {
point[d].x=a*k+px;
point[d].y=b*k+py;
point[d].z=c*k+pz;
d++;
}
}
}
The variables px … are there as random additions a fraction of the generic k to make a less ‘rigid’ cube. I would like to identify ‘points’ that are within a given radius of a chosen ‘point’ in an iteration of a loop that runs for as many times as there are points. However, I would like to do this without running a loop to check every single point in the point[num] array to see if it is close. Is there anything I can do to avoid this without doing a check which is based on the order of the point in the point[num] array?
I can see two good ways to do this.
If the points you are looking for are in some ordered axially aligned grid, then you can access the ones that are in a certain radius immediately. The way to do this is to get the x, y, z point and convert it to its grid position along with the radius to define the min and max grid positions for x, y, and z. You can then access those variables immediately.
If the points are not in a form that’s not axially aligned and ordered into a grid then you need to put it inot a form that will be quick to search. I would suggest the kd tree. It takes a searching operation from being
O(n)toO(log(n). The way it does it is to split the point set in half along the mean and repeat untill you have a fast searchable tree:The PCL (point cloud Library) will do this for your as well!
Here is a link:
http://pointclouds.org/
And a tutorial on kd trees with pcl:
http://pointclouds.org/documentation/tutorials/kdtree_search.php#kdtree-search
In fact it even shows you the code for a radius search from an entered point into a kd tree stored point cloud. Look at neighbours in radius search on the tutorial page i have provided.
Good Luck!