Created
April 17, 2018 07:08
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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