2

I'm trying to interpolate and visualize a function on the surface of a sphere. For this, I need a meshgrid with the coordinates of interpolation points on the sphere.

Initially, I tried to generate my sphere using

phi, theta = np.mgrid[0:pi:n, 0:2 * pi:n]

which generates a mesh of points like this:

enter image description here

def make_coor(n):
    phi, theta = np.mgrid[0:pi-0:n, 0:2 * pi:n]
    Coor = namedtuple('Coor', 'r phi theta x y z')
    r = 1
    x = r * sin(phi) * cos(theta)
    y = r * sin(phi) * sin(theta)
    z = r * cos(phi)
    return Coor(r, phi, theta, x, y, z)

pts=make_coor(15j)

mlab.figure()
mlab.points3d(pts.x,pts.y,pts.z, scale_factor=0.1)
mlab.mesh(pts.x,pts.y,pts.z)
mlab.savefig('spheretest.png')
mlab.show()

This was great because it let me generate my interpolation points and plot the sphere mesh all in one go. However, notice the point at the top of the sphere? Well, in the mgrid the north and south poles are actually represented many times. (Hopefully it's apparent why this would occur)

This is a problem because when I try to do my interpolation, I can't have multiple interpolation points representing the same point on the sphere. This means that I need a way to generate my interpolation points and my meshgrid in such a way that I do not have duplicate points anywhere (especially at the poles). (The meshgrid is so that I can send the interpolation points to mlab.mesh() and have the sphere mesh drawn)

Does anyone know of a way I can do this?

Jesse
  • 1,662
  • 3
  • 17
  • 18
  • possible duplicate of [Make a sphere with equidistant vertices](http://stackoverflow.com/questions/25012737/make-a-sphere-with-equidistant-vertices) – Spektre Aug 12 '14 at 07:34
  • look here: http://stackoverflow.com/a/25031737/2521214 and here: http://stackoverflow.com/a/25082674/2521214 – Spektre Aug 12 '14 at 07:35
  • I've looked at a few ways to find uniformly spaced points on a sphere, but I'm specifically looking for a way to have the points associated with their connectivity in the same way np.meshgrid() outputs. – Jesse Aug 12 '14 at 16:23
  • Can you explain what your end goal is such that having the duplicate points is a problem? – aestrivex Aug 12 '14 at 16:38
  • I'm trying to train a SciPy RBF interpolation for a function on the surface of the sphere. I first use a small number of points to train, then a large number of points create the sphere mesh and colour according to the interpolation results. More info on that here: http://stackoverflow.com/questions/25169236/using-radial-basis-functions-to-interpolate-a-function-on-a-sphere The repeated points at the poles cause the interpolation matrix to be singular. – Jesse Aug 12 '14 at 17:17
  • 2
    @Jesse I do not see any way to avoid singularities on poles while using only basis functions. better way is to made own grid that covers entire surface with (pseudo)equidistant points like in mine answer or use sphere like meshes from triangles or hexagons like other answers on linked questions suggests. With both of these approaches you can easily do even projections like this: `surface_point=f(t) where t=<0.0,1.0>` 1D->2D(3D) which can come in handy for some tasks – Spektre Aug 13 '14 at 05:45
  • @Spektre the inscribed polyhedra looked to be a good solution. But the links you posted were all in C++ and C#. I don't know how to translate them into python in such a way that it can be fed into SciPy Rbf and MayaVi Mesh plotting. – Jesse Aug 13 '14 at 16:15
  • @Jesse then extract the equations only and code it yourself (cant help you with Python or MayaVi) – Spektre Aug 14 '14 at 07:32
  • Hi @Spektre, thanks so much for your suggestions! I ended up using this algorithm to cover the surface of the sphere with (pseudo)equidistant points (like you said), http://stackoverflow.com/a/14805715/2543578. Then I used Dipy's Sphere function (http://nipy.org/dipy/reference/dipy.core.html#dipy.core.sphere.Sphere) to add a triangle mesh to those points. It worked out perfectly. – Jesse Aug 14 '14 at 16:16

0 Answers0