jeudi 4 juin 2020

Why is this randomly generated spherical point cloud not uniformly distributed?

I'm trying to simulate radiation emitting from a point source. To do this, given the coordinates of a source and the desired length of emitted rays, I randomly generate a direction vector in spherical coordinates, convert it to cartesian, and return the correct end point. However, when I run this, and visualize the resulting point cloud (consisting of all the randomly generated end points) in Blender, I see that it's more densely populated at the "poles" of the sphere. I'd like the points to be uniformly distributed along the sphere. How can I achieve this?

Resulting Point Cloud

The random generation function:

def getRadiationEmissionLineSeg(p, t):
    if(p.size == 4):
        #polar angle spans [0, pi] from +Z axis to -Z axis
        #azimuthal angle spans [0, 2*pi] orthogonal to the zenith (in the XY plane)
        theta = math.pi * random.random()
        phi = 2 * math.pi * random.random()

        #use r = 1 to get a unit direction vector
        v = sphericalToCartesian(1, theta, phi)

        #parametric vector form: vec = p + tv
        #p = point that lies on vector (origin point in case of a ray)
        #t = parameter (-inf, inf) for lines, [0, inf) for rays
        #v = direction vector (must be normalized)
        return p + t * v

The spherical coordinates -> cartesian conversion function:

def sphericalToCartesian(r, theta, phi):

    x = r * math.sin(theta) * math.cos(phi)
    y = r * math.sin(theta) * math.sin(phi)
    z = r * math.cos(theta)

    return npy.array([x, y, z, 0])



Aucun commentaire:

Enregistrer un commentaire