Let say you have a set of n points in 3D and want to fit a plane to them. In this article I will derive a simple, numerically stable method and give you the source code for it. Sounds like fun? Let's go!
First of all - if you look around the web for an answer you'll get answers that include doing a singular value decomposition on a covariance matrix to find the eigenvector of the smallest eigenvalue. It turns out, however, that this is making things more complicated than they need to be. Let's start with the basics:
A plane is generally described by a normal vector n = [a, b, c]ᵀ and a distance d so that for point p = [x, y, z]ᵀ on the plane n · p + d = 0. We can write this as:
ax + by + cz + d = 0
Note, however, that this is overdetermined - the solution space (a plane) is three-dimensional, but the above description uses four values. So let’s start off with removing one component by constraining the solution space. We do that by arbitrarily assigning c = 1, i.e. that the z-component of the plane normal is always one (note that the length of the normal does not need to be one). If you think this is a potentially problematic assumption you are right - we shall return to this later. For now, let’s define:
ax + by + z + d = 0
ax + by + d = -z
and solve for a,b,d. In matrix form:
Next we take this matrix, transpose it, and multiply it from the left to perform a linear least squares:
After multiplying in the transpose:
Where N is the number of points. Now here’s the clever part: let us define the x,y,z above to be relative to the centroid (average) of point cloud. Now Σx = Σy = Σz = 0 and so we can simplify to:
From the last row (N·d = 0) we can conclude that d = 0. This means that if all points are relative to the centroid of the point cloud, then the plane runs through the origin. In other words: the plane always runs through the average of the input points. We can now get rid of a dimension:
Cramer's rule gives us:
D = Σxx*Σyy - Σxy*Σxy
a = (Σyz*Σxy - Σxz*Σyy) / D
b = (Σxy*Σxz - Σxx*Σyz) / D
n = [a, b, 1]ᵀ
And that's it! But remember our assumption: the z-component of the plane normal is non-zero. What if it is zero? Then it can be shown that the determinant D above becomes zero and we have division by zero. And even if it is not exactly zero, but close, we still get bad conditioning and thus bad results. So what can we do? Well, at least one of the components of the normal must be non-zero if the points do span a plane. So let’s do the above calculations for three separate assumptions of which component is non-zero. Then we simply pick the most well-behaved one, i.e. the one with the largest determinant.
Edit: as the commenter Paul pointed out, this method will minimize the squares of the residuals as perpendicular to the main axis, not the residuals perpendicular to the plane. If the residuals are small (i.e. your points all lie close to the resulting plane), then this method will probably suffice. However, if your points are more spread then this method may not be the best fit.
Edit: I have made an improvement to this method which you can read about here.
Edit: as the commenter DeMoney points out we can avoid a division by D by multiplying the resulting normal with D (we will later normalize it anyway), saving us two divisions. The argument about bad conditioning still holds however – when D is small, so will n be. Here is the modified equation:
D = Σxx*Σyy - Σxy*Σxy
a = Σyz*Σxy - Σxz*Σyy
b = Σxy*Σxz - Σxx*Σyz
n = [a, b, D]ᵀ
Here’s the code (in Rust):