Triple Integrals in Spherical Coordinates
Spherical coordinates describe a point by its distance from the origin and two angles, like latitude and longitude. Balls, spherical shells, cones with the vertex at the origin, and anything that depends only on the distance become boxes in . The price is the volume element , and that is also the reason why choosing "random angles" does not give random directions, a classic bug in graphics code.
The survey course introduces double and triple integrals in Double and Triple Integrals.
Read Triple Integrals in Cylindrical Coordinates first; the general theory is in Change of Variables in Multiple Integrals.
The coordinate system
A point has spherical coordinates :
- , the distance from the origin;
- , the same angle as in cylindrical coordinates (longitude), ;
- , the angle between the positive -axis and (the polar angle, or colatitude), .
| Equation | Surface |
|---|---|
| sphere of radius | |
| half-plane through the -axis | |
| () | cone opening upward around the -axis |
| the -plane | |
| sphere |
Note: Conventions differ. Mathematics texts (and this course) use for longitude and for the polar angle; physics texts and many graphics papers swap the letters, and latitude in geography is . Always check which angle a formula or API means.
The volume element
A spherical wedge with small , , is almost a box with edges:
The parallels shrink toward the poles (), so wedges near the poles are thin. The same factor appears as the absolute value of the Jacobian determinant, which the code below checks.
import sympy as sp
rho, th, ph = sp.symbols("rho theta phi", positive=True)
X = sp.Matrix([rho * sp.sin(ph) * sp.cos(th), rho * sp.sin(ph) * sp.sin(th), rho * sp.cos(ph)])
J = X.jacobian([rho, th, ph])
print("det J =", sp.simplify(J.det()))
R = sp.symbols("R", positive=True)
ball = sp.integrate(rho**2 * sp.sin(ph), (rho, 0, R), (th, 0, 2 * sp.pi), (ph, 0, sp.pi))
print("volume of a ball:", ball)det J = -rho**2*sin(phi)
volume of a ball: 4*pi*R**3/3
The determinant is negative because the ordering is a left-handed one; only the absolute value matters for volume (in the order the sign is positive).
for a spherical wedge ; may also run between functions of and .
Example: the ice-cream cone again
The solid above the cone and inside the sphere (computed with cylindrical coordinates in the previous lesson) is in spherical coordinates simply
The cone is because .
Example: a radial density
A planet of radius has density (denser at the centre). Its mass:
Whenever the integrand depends only on , the angles contribute the factor and the integral becomes : the ball is a stack of shells of area and thickness .
In software: random directions and environment maps
Uniform points on a sphere. A random direction (for scattering, ambient occlusion, particle bursts, random rotations) should be uniform with respect to area on the unit sphere, whose element is . Choosing uniformly in ignores the and crowds points at the poles. The correct recipe follows from the fact that the area between heights and is (Archimedes' hat-box theorem): pick uniform in and uniform in , or normalise a vector of three independent standard normals.
import numpy as np
rng = np.random.default_rng(2024)
N = 100_000
def cap_fraction(z): # share of points within 30° of the north pole (exact: (1 - cos30°)/2)
return float(np.mean(z > np.cos(np.radians(30))))
phi_naive = rng.uniform(0, np.pi, N) # wrong: uniform angle
z_naive = np.cos(phi_naive)
z_good = rng.uniform(-1, 1, N) # right: uniform height
g = rng.standard_normal((N, 3))
z_gauss = (g / np.linalg.norm(g, axis=1, keepdims=True))[:, 2] # right: normalised Gaussian
print(f"exact cap share : {(1 - np.cos(np.radians(30))) / 2:.4f}")
print(f"phi uniform (naive) : {cap_fraction(z_naive):.4f}")
print(f"cos(phi) uniform : {cap_fraction(z_good):.4f}")
print(f"normalised Gaussian : {cap_fraction(z_gauss):.4f}")exact cap share : 0.0670
phi uniform (naive) : 0.1658
cos(phi) uniform : 0.0661
normalised Gaussian : 0.0681
The naive method puts of the points ( out of ) in a cap that holds only 6.7% of the area: 2.5 times too many.
Environment maps. An equirectangular (latitude-longitude) environment map stores the sky as a image indexed by . Each texel spans and , but its solid angle is , tiny near the poles. Image-based lighting must weight each texel by when integrating the light (for an irradiance map or a sky's average colour), or the poles count far too much. The weights add up to (almost exactly, as a midpoint sum) , the sphere's total solid angle.
import numpy as np
W, H = 64, 32
phi = (np.arange(H) + 0.5) * np.pi / H # texel-centre polar angles
dOmega = np.sin(phi) * (2 * np.pi / W) * (np.pi / H) # solid angle of one texel in each row
print("total solid angle:", round(float(dOmega.sum() * W), 5), " 4π =", round(4 * np.pi, 5))
print("polar row / equator row texel size:", round(float(dOmega[0] / dOmega[H // 2]), 4))
# average brightness of a sky that is 1 in the upper hemisphere (z > 0) and 0 below
sky = (np.cos(phi) > 0).astype(float)[:, None] * np.ones((H, W))
print("weighted average :", round(float((sky * dOmega[:, None]).sum() / (4 * np.pi)), 4))
print("plain pixel mean :", round(float(sky.mean()), 4))total solid angle: 12.57142 4π = 12.56637
polar row / equator row texel size: 0.0491
weighted average : 0.5002
plain pixel mean : 0.5
Here the plain mean happens to be right by symmetry (the weighted sum is off only by the small midpoint-rule error); for a sky that is bright only near the zenith (exercise 4), it is badly wrong.
Exercises
Exercise 1: Convert
Convert to spherical coordinates.
Answer
.
Solution
Exercise 2: A spherical shell
Evaluate , where is the spherical shell .
Answer
.
Solution
Exercise 3: A cone cap
Find the volume of the part of the ball that lies inside the cone .
Answer
.
Solution
Exercise 4: Zenith sky (code)
A sky has brightness in the upper hemisphere and 0 below. Compute its exact average over all directions, , and compare a equirectangular map averaged with weights and without.
Answer
Exact average . The weighted map gives ; the plain pixel mean gives about 0.25, 50% too bright.
Solution
import numpy as np
W, H = 64, 32
phi = (np.arange(H) + 0.5) * np.pi / H
L = np.where(phi < np.pi / 2, np.cos(phi)**2, 0.0)
w = np.sin(phi)
print(round(float((L * w).sum() / w.sum()), 4), round(float(L.mean()), 4))0.1668 0.25
Test yourself
What is the volume element in spherical coordinates?
To sample uniformly random directions, you can take uniform in and…
- uniform in
- uniform in
- uniform in
- uniform in
In spherical coordinates, describes…
- a sphere
- a plane through the -axis
- a cone around the -axis
- a cylinder