Metadata
aliases: []
shorthands: {}
created: 2022-02-26 18:07:01
modified: 2022-02-26 18:34:36
This is the standard way of generating a sphere mesh, using the spherical coordinate system with a constant
This method results in quite a non-uniform distribution of vertices, the "density" of vertices diverges to phi_res = 20, theta_res = 20).
The ratio of areas between the largest and smallest triangle on the mesh:
This example uses the Separate arrays convention for storing triangle meshes.
The arguments of the function have the following meanings:
r: radius of the sphereres_phi: the number of vertices ("resolution") of the sphere with constant res_theta: the number of vertices ("resolution") of the sphere with constant import numpy as np
def generate_sphere(r = 1.0, res_phi=10, res_theta=10):
# Generate the inclusive uniform spacing of phi and theta
phis = np.linspace(0, np.pi * 2.0, res_phi, True)
thetas = np.linspace(0, np.pi, res_phi, True)
# Arrays for the vertex coordinates
x = []; y = []; z = []
# Iterate through the (phi, theta) pairs and generate the vertices
for phi in phis:
for theta in thetas:
cosphi = np.cos(phi)
sinphi = np.sin(phi)
costheta = np.cos(theta)
sintheta = np.sin(theta)
x.append(cosphi * sintheta * r)
y.append(sinphi * sintheta * r)
z.append(costheta)
# Arrays for the triangle indices
i = []; j = []; k = []
for ti in range(res_phi - 1):
for tt in range(res_theta - 1):
# This lambda function gives the absolute index of a given
# vertex in the vertex arrays
pixd = lambda i1, i2 : i1 * (res_theta) + i2
# The vertex indices for the corners of the quad
idx = pixd(ti, tt)
idx2 = pixd(ti, tt + 1)
idx3 = pixd(ti + 1, tt)
idx4 = pixd(ti + 1, tt + 1)
# Add both triangles to the list
i.append(idx)
i.append(idx2)
j.append(idx2)
j.append(idx4)
k.append(idx3)
k.append(idx3)
return x, y, z, i, j, k