### A better way to sample a sphere (w.r.t. solid angle)

Posted:

**Thu Jan 16, 2014 5:23 pm**Hi all,

The usual method for sampling a sphere from a point outside the sphere is to calculate the angle of the cone of the visible portion and uniformly sample within that cone, as described in Shirley/Wang.

However, one detail that is glossed over is that you still need to map from the sampled direction to the point on the sphere. The usual method is to simply generate a ray from the point and the sampled direction and intersect it with the sphere. However, this intersection test may fail due to floating point inaccuracies (e.g., if the sphere is small and the distance from the point is large).

I've found a couple of ways to deal with this -- As described in the pbrt book, pbrt simply assumes that the ray just grazes the sphere if the intersection fails, and then projects the center of the sphere onto the ray (code here). mitsuba moves the origin of the ray closer to the sphere (in fact, from within it) before doing the test (falling back to projecting the center onto the ray if that still fails) (code here).

However, this seems inelegant. I've come up with a better way, which involves converting the sampled cone angle θ (as measured from the segment connecting the point to the sphere center) into an angle α from the inside of the sphere, and then simply using α and the sampled polar angle φ onto the sphere. This turns out to be simple, and in my unscientific tests a bit faster.

I can post more details of this method if anyone is interested, but I was wondering if this was already known, or if there are problems with it. I did some searching myself, but nothing turned up.

Thanks!

-- Fred

The usual method for sampling a sphere from a point outside the sphere is to calculate the angle of the cone of the visible portion and uniformly sample within that cone, as described in Shirley/Wang.

However, one detail that is glossed over is that you still need to map from the sampled direction to the point on the sphere. The usual method is to simply generate a ray from the point and the sampled direction and intersect it with the sphere. However, this intersection test may fail due to floating point inaccuracies (e.g., if the sphere is small and the distance from the point is large).

I've found a couple of ways to deal with this -- As described in the pbrt book, pbrt simply assumes that the ray just grazes the sphere if the intersection fails, and then projects the center of the sphere onto the ray (code here). mitsuba moves the origin of the ray closer to the sphere (in fact, from within it) before doing the test (falling back to projecting the center onto the ray if that still fails) (code here).

However, this seems inelegant. I've come up with a better way, which involves converting the sampled cone angle θ (as measured from the segment connecting the point to the sphere center) into an angle α from the inside of the sphere, and then simply using α and the sampled polar angle φ onto the sphere. This turns out to be simple, and in my unscientific tests a bit faster.

I can post more details of this method if anyone is interested, but I was wondering if this was already known, or if there are problems with it. I did some searching myself, but nothing turned up.

Thanks!

-- Fred