I have a lot of points on the surface of the sphere. How can I calculate the area/spot of the sphere that has the largest point density? I need this to be done very fast. If this was a square for example I guess I could create a grid and then let the points vote which part of the grid is the best. I have tried with transforming the points to spherical coordinates and then do a grid, both this did not work well since points around north pole are close on the sphere but distant after the transform.
Thanks
This is really just an inverse of this answer of mine
just invert the equations of equidistant sphere surface vertexes to surface cell index. Don't even try to visualize the cell different then circle or you go mad. But if someone actually do it then please post the result here (and let me now)
Now just create 2D cell map and do the density computation in
O(N)
(like histograms are done) similar to what Darren Engwirda propose in his answerThis is how the code looks like in C++
The result looks like this:
so now just see what is in the
map[][]
array you can find the global/local min/max of density or whatever you need... Just do not forget that the size ismap[na][nb[i]]
wherei
is the first index in array. The grid size is controlled byna
constant andcm
is just density to color scale ...[edit1] got the Quad grid which is far more accurate representation of used mapping
this is with
na=16
the worst rounding errors are on poles. If you want to be precise then you can weight density by cell surface size. For all non pole cells it is simple quad. For poles its triangle fan (regular polygon)This is the grid draw code:
the
mm
is the grid cell sizemm=0.5
is full cell size , less creates a space between cellsIf I understand correctly, you are trying to find the densepoint on sphere.
if points are denser at some point
Consider Cartesian coordinates and find the mean X,Y,Z of points
Find closest point to mean X,Y,Z that is on sphere (you may consider using spherical coordinates, just extend the radius to original radius).
Constraints
Partition the sphere into equal-area regions (bounded by parallels and meridians) as described in my answer there and count the points in each region.
The aspect ratio of the regions will not be uniform (the equatorial regions will be more "squarish" when
N~M
, while the polar regions will be more elongated). This is not a problem because the diameters of the regions go to 0 asN
andM
increase. The computational simplicity of this method trumps the better uniformity of domains in the other excellent answers which contain beautiful pictures.One simple modification would be to add two "polar cap" regions to the
N*M
regions described in the linked answer to improve the numeric stability (when the point is very close to a pole, its longitude is not well defined). This way the aspect ratio of the regions is bounded.