Skip to content

Instantly share code, notes, and snippets.

@marmakoide
Created April 17, 2018 07:08
Show Gist options
  • Save marmakoide/681ca3e50447317148f999335bf7bcc3 to your computer and use it in GitHub Desktop.
Save marmakoide/681ca3e50447317148f999335bf7bcc3 to your computer and use it in GitHub Desktop.
Point picking (ie. uniform sampling) of the unit sphere (ie. a sphere centered at the origin and radius equals to 1)
import numpy
'''
Pick N points uniformly from the unit disc
This sampling algorithm does not use rejection sampling.
'''
def disc_uniform_pick(N):
angle = (2 * numpy.pi) * numpy.random.random(N)
out = numpy.stack([numpy.cos(angle), numpy.sin(angle)], axis = 1)
out *= numpy.sqrt(numpy.random.random(N))[:,None]
return out
'''
Pick N points uniformly from the unit sphere
This sampling algorithm does not use rejection sampling.
'''
def sphere_uniform_pick(N):
U = disc_uniform_pick(N)
U_norm_sqr = numpy.sum(U ** 2, axis = 1)
out = numpy.concatenate([U, (1. - 2. * U_norm_sqr)[:,None]], axis = 1)
out[:,:2] *= 2. * numpy.sqrt((1. - U_norm_sqr))[:,None]
return out
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment