Max Slater

Computer Graphics, Programming, and Math

Spherical Integration

Or, where does that $$\sin\theta$$ come from?

Integrating functions over spheres is a ubiquitous task in graphics—and a common source of confusion for beginners. In particular, understanding why integration in spherical coordinates requires multiplying by $$\sin\theta$$ takes some thought.

The Confusion

So, we want to integrate a function $$f$$ over the unit sphere. For simplicity, let’s assume $$f = 1$$. Integrating $$1$$ over any surface computes the area of that surface: for a unit sphere, we should end up with $$4\pi$$.

Integrating over spheres is much easier in spherical coordinates, so let’s define $$f$$ in terms of $$\theta, \phi$$:

$$\theta$$ ranges from $$0$$ to $$\pi$$, representing latitude, and $$\phi$$ ranges from $$0$$ to $$2\pi$$, representing longitude. Naively, we might try to integrate $$f$$ by ranging over the two parameters:

\[ \begin{align*} \int_{0}^{2\pi}\int_0^\pi 1\, d\theta d\phi &= \int_0^{2\pi} \theta\Big|_0^\pi\, d\phi\\ &= \int_0^{2\pi} \pi\, d\phi \\ &= \pi \left(\phi\Big|_0^{2\pi}\right) \\ &= 2\pi^2 \end{align*} \]

That’s not $$4\pi$$—we didn’t integrate over the sphere! All we did was integrate over a flat rectangle of height $$\pi$$ and width $$2\pi$$. One way to conceptualize this integral is by adding up the differential area $$dA$$ of many small rectangular patches of the domain.

Each patch has area $$dA = d\theta d\phi$$, so adding them up results in the area of the rectangle. What we actually want is to add up the areas of patches on the sphere, where they are smaller.

In the limit (small $$d\theta,d\phi$$), the spherical patch $$d\mathcal{S}$$ is a factor of $$\sin\theta$$ smaller than the rectangular patch $$dA$$.1 Intuitively, the closer to the poles the patch is, the smaller its area.

When integrating over the sphere $$\mathcal{S}$$, we call the area differential $$d\mathcal{S} = \sin\theta\, d\theta d\phi$$.2

Let’s try using it:

\[ \begin{align*} \iint_\mathcal{S} 1\,d\mathcal{S} &= \int_{0}^{2\pi}\int_0^\pi \sin\theta\, d\theta d\phi\\ &= \int_0^{2\pi} (-\cos\theta)\Big|_0^\pi\, d\phi\\ &= \int_0^{2\pi} 2\, d\phi \\ &= 2 \left(\phi\Big|_0^{2\pi}\right) \\ &= 4\pi \end{align*} \]

It works! If you just wanted the intuition, you can stop reading here.

But why $$\sin\theta$$?

It’s illustrative to analyze how $$\sin\theta$$ arises from parameterizing the cartesian ($$x,y,z$$) sphere using spherical coordinates.

In cartesian coordinates, a unit sphere is defined by $$x^2 + y^2 + z^2 = 1$$. It’s possible to formulate a cartesian surface integral based on this definition, but it would be ugly.

\[ \iint_{x^2+y^2+z^2=1} f dA = \text{?} \]

Instead, we can perform a change of coordinates from cartesian to spherical coordinates. To do so, we will define $$\Phi : \theta,\phi \mapsto x,y,z$$:

\[ \begin{align*} \Phi(\theta,\phi) = \begin{bmatrix}\sin\theta\cos\phi\\ \sin\theta\sin\phi\\ \cos\theta\end{bmatrix} \end{align*} \]

The function $$\Phi$$ is a parameterization of the unit sphere $$\mathcal{S}$$. We can check that it satisfies $$x^2+y^2+z^2=1$$ regardless of $$\theta$$ and $$\phi$$:

\[ \begin{align*} |\Phi(\theta,\phi)|^2 &= (\sin\theta\cos\phi)^2 + (\sin\theta\sin\phi)^2 + \cos^2\theta\\ &= \sin^2\theta(\cos^2\phi+\sin^2\phi) + \cos^2\theta \\ &= \sin^2\theta + \cos^2\theta\\ &= 1 \end{align*} \]

Applying $$\Phi$$ to the rectangular domain $$\theta\in[0,\pi],\phi\in[0,2\pi]$$ in fact describes all of $$\mathcal{S}$$, giving us a much simpler parameterization of the integral.

To integrate over $$d\theta$$ and $$d\phi$$, we also need to compute how they relate to $$d\mathcal{S}$$. Luckily, there’s a formula that holds for any parametric surface $$\mathbf{r}(u,v)$$ describing a three-dimensional domain $$\mathcal{R}$$:3

\[ d\mathcal{R} = \left\lVert \frac{\partial \mathbf{r}}{\partial u} \times \frac{\partial \mathbf{r}}{\partial v} \right\rVert du dv \]

The two partial derivatives represent tangent vectors on $$\mathcal{R}$$ along the $$u$$ and $$v$$ axes, respectively. Intuitively, each tangent vector describes how a $$u,v$$ patch is stretched along the corresponding axis when mapped onto $$\mathcal{R}$$. The magnitude of their cross product then computes the area of the resulting parallelogram.

Finally, we can actually apply the change of coordinates:

\[ \begin{align*} \iint_\mathcal{S} f\, d\mathcal{S} &= \int_0^{2\pi}\int_0^\pi f(\Phi(\theta,\phi)) \left\lVert \frac{\partial\Phi}{\partial\theta} \times \frac{\partial\Phi}{\partial\phi} \right\rVert d\theta d\phi \end{align*} \]

We just need to compute the area term:

\[ \begin{align*} \frac{\partial\Phi}{\partial\theta} &= \begin{bmatrix}\cos\phi\cos\theta & \sin\phi\cos\theta & -\sin\theta\end{bmatrix}\\ \frac{\partial\Phi}{\partial\phi} &= \begin{bmatrix}-\sin\phi\sin\theta & \cos\phi\sin\theta & 0\end{bmatrix}\\ \frac{\partial\Phi}{\partial\theta} \times \frac{\partial\Phi}{\partial\phi} &= \begin{bmatrix}\sin^2\theta\cos\phi & \sin^2\theta\sin\phi & \cos^2\phi\cos\theta\sin\theta + \sin^2\phi\cos\theta\sin\theta\end{bmatrix}\\ &= \begin{bmatrix}\sin^2\theta\cos\phi & \sin^2\theta\sin\phi & \cos\theta\sin\theta \end{bmatrix}\\ \left\lVert\frac{\partial\Phi}{\partial\theta} \times \frac{\partial\Phi}{\partial\phi}\right\rVert &= \sqrt{\sin^4\theta(\cos^2\phi+\sin^2\phi) + \cos^2\theta\sin^2\theta}\\ &= \sqrt{\sin^2\theta(\sin^2\theta + \cos^2\theta)}\\ &= \lvert\sin\theta\rvert \end{align*} \]

Since $$ \theta\in[0,\pi] $$, we can say $$ \lvert\sin\theta\rvert = \sin\theta $$. That means $$d\mathcal{S} = \sin\theta\, d\theta d\phi$$! Our final result is the familiar spherical integral:

\[ \iint_\mathcal{S} f dS = \int_0^{2\pi}\int_0^\pi f(\theta,\phi) \sin\theta\, d\theta d\phi \]

Footnotes


  1. Interestingly, if we knew the formula for the area of a spherical patch, we could do some slightly illegal math to derive the area form:

    \[\begin{align*} dA &= (\phi_1-\phi_0)(\cos\theta_0-\cos\theta_1)\\ &= ((\phi + d\phi) - \phi)(\cos(\theta)-\cos(\theta+d\theta))\\ &= d\phi d\theta \frac{(\cos(\theta)-\cos(\theta+d\theta))}{d\theta}\\ &= d\phi d\theta \sin\theta \tag{Def. derivative} \end{align*}\]
     ↩︎
  2. In graphics, also known as differential solid angle, $$d\mathbf{\omega} = \sin\theta\, d\theta d\phi$$. ↩︎

  3. Even more generally, the scale is related to the inner product on the tangent space of the surface. This inner product is defined by the first fundamental form $$\mathrm{I}$$ of the surface, and the scale factor is $$\sqrt{\det\mathrm{I}}$$, regardless of dimension. For surfaces immersed in 3D, this simplifies to the cross product mentioned above. Changes of coordinates that don’t change dimensionality have a more straightforward scale factor: it’s the determinant of their Jacobian.4 ↩︎

  4. These topics are often more easily understood using the language of exterior calculus, where integrals can be uniformly expressed regardless of dimension. I will write about it at some point. ↩︎

Written on