Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save alexlib/092685b62d7ca713cda740dea29c47e5 to your computer and use it in GitHub Desktop.
Save alexlib/092685b62d7ca713cda740dea29c47e5 to your computer and use it in GitHub Desktop.
uniform rotation on sphere
def random_uniform_rotation(size=1):
"""
Return uniform rotation vectors sampled on a sphere
Args:
size : int
The number of vectors
Returns:
vector: np.ndarray
The rotation vectors
"""
u1 = numpy.random.uniform(0, 1, size=size)
u2 = numpy.random.uniform(0, 1, size=size)
u3 = numpy.random.uniform(0, 1, size=size)
theta = numpy.arccos(2 * u1 - 1)
phi = 2 * pi * u2
x = numpy.sin(theta) * numpy.cos(phi)
y = numpy.sin(theta) * numpy.sin(phi)
z = numpy.cos(theta)
vectors = numpy.array((x, y, z)).T
vectors *= 2 * pi * u3.reshape((u3.size, 1))
return vectors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment