Created
August 4, 2021 08:28
-
-
Save alisterburt/b90988a6beb0f7e9d134655d4a7d0d61 to your computer and use it in GitHub Desktop.
uniform rotation on sphere
This file contains hidden or 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
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