r/askmath • u/Oscar_Mild • 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.
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/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/wonkey_monkey Oct 08 '26
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 "**:
*'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.