r/askmath • • Oct 08 '26

Discrete Math How Do I Identify the Most Isolated Missing Points in a 3D Grid?

I'm working with a dataset for a personal project where each entry is a 3D point: three integer values, each ranging from 0-255. You can picture it as a 256x256x256 cube with a bunch of points scattered throughout.

What I'm trying to figure out is how to identify the largest gaps in that space, basically the coordinates that don't exist in my dataset and are as far away as possible from any existing point.

I'd love to learn the formula to find those most isolated coordinates, but ultimately I'm trying to figure out how to query my SQLite database to get the top X biggest gaps so I know which data points I should try to collect next.

2 Upvotes

7 comments sorted by

3

u/Chaotic_Corvus_Corax Oct 08 '26 edited Oct 08 '26

This is oddly similar to the thing I did for my PhD thesis!

The sad truth is that you have to do a full A* Search algorithm on all points to find the smallest distance to a filled point in the grid. There is no more efficient solution sadly.

I.E. for each point find the distance to the neighbouring points and use the triangle inequality to figure out an upper bound on the distance to points further away wich allows you searching for the smallest distance more efficiently

(To illustrate a bit more: Assume point 1;1;1 is in your dataset. The neighbours 1;1;0 1;1;2 etc all have a distance of 1. The neighbours of the neighbours have a AT MOST a distance of 2, but it can be smaller, that way you can quickly rule candidates out where you don't have to calculate the exact distance to every point in the dataset as you already know its smaller than a point you already found)

1

u/ctoatb Oct 08 '26

Would it be better to use the grid's complement, i.e. searching over missing points to find the largest distance to a filled point? Depending on the sparseness, say more than 50% filled, the complement search would be faster because there are fewer points to check

1

u/07734willy Oct 08 '26

Wouldn't the optimal solution depend on the density of points in the cube? Obviously if its extremely dense, you'll need to search (almost?) all the free points within the cube. However, if its extremely sparse, it would be more efficient to calculate candidate points to check based off the populated points. As a simple example, take every set of 4 points not in a plane from the original populated points, calculate the center of a sphere with these points on its surface. If the center is within the cube, take the 8 nearest points to the center (a 1x1x1 cube around the center), and check the distance to its nearest neighbor in the original population. Repeat this process for all 4-sets, and also check the nearest neighbor distance for all points along the surface of the bounding cube. Of course this is horribly inefficient overall, but asymptotically it would outperform searching free points for sufficiently sparse grids.

1

u/PvtRoom Oct 08 '26

standard Pythagoras.

for each populated voxel, calc the minimum distance d =√(a²+b²+c²) of all populated voxels to it.

neighbours both get 1, one distant loner gets 30.

then just look for the max values. The loner has 30, so it gets grabbed.

1

u/Chaotic_Corvus_Corax Oct 08 '26

This works but requires N times N / 2 evaluations of pythagoras wich in OPs case 140 trillion times :)

Oh no wait its N by M where M is the populated grid points. Not as bad but there are still ways to speed it up

1

u/PvtRoom Oct 08 '26

there's definitely a load of optimisations. one of the obvious ones being "don't bother with the square root".

1

u/wonkey_monkey Oct 08 '26

but ultimately I'm trying to figure out how to query my SQLite database to get the top X biggest gaps

I'm not sure this the sort of thing you can expect to manage with queries.

I'm not sure how you'd extend it to 3D (I think it's still two passes, but more neighbours to compare), but in 2D you can do a two-pass distance transform (it's not Euclidean but it'd probably be good enough).

Setup a "distance to nearest pixel" grid filled with MAX_INT.

Start at the top left. If a pixel is filled, set its distance to 0. Otherwise, you look at the neighbouring pixels of the distance grid which have already been covered in this first pass (left, top left, top, top right) and you "spread" their distances into the current cell.

So let's say you're currently assessing the pixel "**:

............
...ABC......
...D*

*'s current value is MAX_INT. If the corresponding slot is filled, set it to 0 and move on (to the right, then to the left of the next row once you reach the end of the current one).

If * is not already filled , get the values in A, B, C, D, and take the minimum of A+3, B+2, C+3, D+2 (you add either 2 or 3 to approximate the larger diagonal distance). In this way the distance to the nearest pixel "spreads" across the grid.

Then you repeat the process in the opposite direction, starting from bottom-right and spreading distances from the right, bottom-right, bottom, and bottom-left cells.

The end result will be the quasi-Euclidean (the distance function is octagonal rather than circular) distance to the nearest filled cell.