Skip to content

Instantly share code, notes, and snippets.

@alisterburt
Created August 4, 2021 08:28
Show Gist options
  • Save alisterburt/b90988a6beb0f7e9d134655d4a7d0d61 to your computer and use it in GitHub Desktop.
Save alisterburt/b90988a6beb0f7e9d134655d4a7d0d61 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