Skip to content

Instantly share code, notes, and snippets.

@Eagle-E
Created January 25, 2024 14:33
Show Gist options
  • Save Eagle-E/1e6c158b60cbc9693abeba68de5cc84d to your computer and use it in GitHub Desktop.
Save Eagle-E/1e6c158b60cbc9693abeba68de5cc84d to your computer and use it in GitHub Desktop.
def sample_points_on_sphere(radius = 1, vres = 10, hres = 10):
""" Sample points on a sphere that has the given radius.
Args:
- vres: vertical resolution, the amount of heights at which the sphere will
be sampled, the vertical distances are sampled at even distances
- hres: horizontal resolution, same as vres but horizontally
"""
def sample(r, theta, phi):
x = r * np.sin(theta) * np.cos(phi)
y = r * np.sin(theta) * np.sin(phi)
z = r * np.cos(theta)
return [x, y, z]
# theta is vertical angle with range [0, pi]
# phi is horizontal angle with range [0, 2pi]
dtheta = np.pi / (vres-1)
dphi = 2 * np.pi / (hres-1)
samples = np.ndarray((vres * hres, 3), dtype=np.float64)
for i in range(vres):
for j in range(hres):
samples[i*hres+j] = sample(radius, i * dtheta, j*dphi)
return samples
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment