Surfaces in 3D
A curve has one degree of freedom, a surface has two. This lesson describes surfaces in three ways: as graphs , as level sets , and as parametric patches . It also shows how to find the normal vector that lighting and collision code need.
Three ways to describe a surface
| Description | Example | Good for |
|---|---|---|
| Graph | (a bowl) | terrain heightmaps, loss surfaces |
| Level set | (a sphere) | inside/outside tests, signed distance fields |
| Parametric | generating meshes, texture coordinates |
Each has a use. A level set answers "is this point inside?" in one evaluation ( or ). A parametric form generates points for a mesh by looping over a grid of . A graph is the special case .
Planes
The simplest surface is a plane. Pick a point on it and a normal vector perpendicular to it. A point lies on the plane exactly when is perpendicular to :
The plane through three points , , has normal , from the cross product. The signed distance from a point to the plane is . Ray-plane intersection in a renderer uses the same formula; see Ray-Plane.
import numpy as np
A, B, C = np.array([1.0, 0.0, 0.0]), np.array([0.0, 2.0, 0.0]), np.array([0.0, 0.0, 3.0])
n = np.cross(B - A, C - A)
d = np.dot(n, A)
print(f"plane: {n[0]:g}x + {n[1]:g}y + {n[2]:g}z = {d:g}")
for Q in [np.array([0.0, 0.0, 0.0]), np.array([1.0, 1.0, 1.0]), B]:
print(Q, "signed distance", round((np.dot(n, Q) - d) / np.linalg.norm(n), 4))plane: 6x + 3y + 2z = 6
[0. 0. 0.] signed distance -0.8571
[1. 1. 1.] signed distance 0.7143
[0. 2. 0.] signed distance 0.0
The origin and (1, 1, 1) lie on opposite sides of the plane, which shows up as opposite signs.
Quadric surfaces
Second-degree equations in , , give the quadric surfaces, the 3D cousins of the conic sections:
| Equation | Surface | Cross-sections |
|---|---|---|
| sphere | circles | |
| ellipsoid | ellipses | |
| (circular) paraboloid, a bowl | circles (), parabolas () | |
| hyperbolic paraboloid, a saddle | hyperbolas (), parabolas | |
| cylinder | the same circle at every height | |
| double cone | circles, lines |
To identify a surface, slice it: set and look at the curve that remains, then try . The saddle is worth knowing in particular: along the x-direction it curves up, along the y-direction it curves down, and its centre is a saddle point that is neither a maximum nor a minimum. Loss surfaces in machine learning are full of them.
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-2, 2, 41)
X, Y = np.meshgrid(x, x)
Z = X**2 - Y**2
fig = plt.figure(figsize=(6, 4))
ax = fig.add_subplot(111, projection="3d")
ax.plot_surface(X, Y, Z, cmap="viridis", linewidth=0, antialiased=True)
ax.set_xlabel("x"); ax.set_ylabel("y"); ax.set_zlabel("z")
ax.set_title("Saddle z = x² − y²")
print("z along the x-axis:", Z[20, ::10])
print("z along the y-axis:", Z[::10, 20])z along the x-axis: [4. 1. 0. 1. 4.]
z along the y-axis: [-4. -1. 0. -1. -4.]
Parametric surfaces and their normals
A parametric surface has two families of curves on it: fix and vary , or fix and vary . Their tangent vectors are the partial derivatives and (treated properly in Partial Derivatives). Both lie in the tangent plane, so their cross product is a normal:
For a sphere of radius with (, ), the normal at each point is parallel to the position vector itself, as you would expect for a sphere, and is the area scale factor used later in surface integrals.
import sympy as sp
u, v, R = sp.symbols('u v R', positive=True)
r = sp.Matrix([R*sp.cos(u)*sp.sin(v), R*sp.sin(u)*sp.sin(v), R*sp.cos(v)])
r_u, r_v = r.diff(u), r.diff(v)
n = r_u.cross(r_v)
print("n =", [sp.simplify(c) for c in n])
print("n parallel to r? ", sp.simplify(n + R*sp.sin(v)*r) == sp.zeros(3, 1))
print("|n| =", sp.simplify(sp.sqrt(n.dot(n))))n = [-R**2*sin(v)**2*cos(u), -R**2*sin(u)*sin(v)**2, -R**2*sin(2*v)/2]
n parallel to r? True
|n| = R**2*Abs(sin(v))
SymPy writes the last component with and keeps an Abs because it does not know that , where . Here , so this ordering of and gives an inward normal; swap the factors () to point outward. Mesh code has the same issue with triangle winding.
In software
- Terrain. A heightmap is sampled on a grid, often generated with noise (Procedural Maps). Its normal is , estimated from neighbouring heights; lighting uses it.
- Signed distance fields. Shaders and font renderers describe shapes as level sets , where is the distance to the surface, negative inside.
- Meshes. Spheres, tori and cylinders in 3D engines are produced by looping over a parametric grid; the same become texture coordinates.
Exercises
Exercise 1: Plane through a point
Find the equation of the plane through with normal .
Answer
.
Solution
Exercise 2: Name the surface
Identify each surface: (a) , (b) , (c) .
Answer
(a) circular paraboloid (bowl), (b) double cone, (c) ellipsoid.
Solution
(a) Slices are circles and slices are parabolas : a paraboloid. (b) Rewrite as ; slices are circles of radius , which grow linearly, and the slice is the pair of lines : a cone. (c) Every slice is an ellipse and the surface is bounded (, , ): an ellipsoid.
Exercise 3: Inside or outside?
For the ellipsoid of Exercise 2(c), which of (2.5, 1, 0.5) and (1, 1, 0.5) are inside?
Answer
(1, 1, 0.5) is inside; (2.5, 1, 0.5) is outside.
Solution
Evaluate and compare with 1:
Exercise 4: Terrain normal (code)
A terrain is . Compute its unit upward normal at using with , estimated by central differences ().
Answer
, , so the unit normal is about .
Solution
and at . Normalise (length ).
import numpy as np
f = lambda x, y: 0.1 * x**2 + 0.05 * x * y
h, x, y = 1e-3, 2.0, 4.0
fx = (f(x + h, y) - f(x - h, y)) / (2 * h)
fy = (f(x, y + h) - f(x, y - h)) / (2 * h)
n = np.array([-fx, -fy, 1.0])
print(round(fx, 6), round(fy, 6), np.round(n / np.linalg.norm(n), 4))0.6 0.1 [-0.5126 -0.0854 0.8544]
Test yourself
Which surface is ?
- A bowl
- A cone
- A saddle (hyperbolic paraboloid)
- A cylinder
How do you get a normal vector of a parametric surface ?
What is a normal vector of the plane ?