User:Vossman/3D Line Regression
From Wikipedia, the free encyclopedia
Contents |
[edit] Fit a 3-Dimensional Line to Data Points
[edit] Setup variables
This problem seems similar to what simple linear regression does: fit a straight line to a set of data points. However, ordinary linear regression minimizes the sum of the squared deviations between the points and the line, and it defines the deviation as the distance in the vertical (Y) direction. The problem we are going to solve in this example minimizes the direct distance between the points and the line. The direct distance is along a line that runs from the point and is perpendicular to the target line. In the following figure, the distance d is the direct distance from the point at (x0,y0,z0) to the line.
The parametric equation for a 3D line is:
Where (x0,y0,z0) is some point on the line and (vx,vy,vz) is a vector defining the direction of the line. t is the parameter whose value is varied to define points on the line.
With this definition, there are six parameters: x0,y0,z0,vx,vy,vz. But this overspecifies the line because a 3D line can be defined by 4 parameters as long as it is not parallel to one of the X, Y or Z planes.
When fitting a function to data, it is important that there are no mutually dependent (redundant) parameters in the function. If there are mutually dependent parameters, then there is no unique solution, and the fitting process will not converge.
So we need to eliminate two parameters.
[edit] Removing a parameter from the point on the line
Rather than allowing an arbitrary point (x0,y0,z0) to specify a point on the line, we will force z0 to be 0 and make x0 and y0 be the coordinates on the X-Y plane where the line penetrates the plane (i.e., where Z is zero). This eliminates z0 as a parameter that needs to be computed. We can do this as long as we know that the line is not parallel to the X-Y plane, so it intersects it at some point.

[edit] Removing a parameter from the direction vector
Next, we will work on the direction vector (vx,vy,vz) that defines the direction of the line. Scaling the direction vector by a non-zero factor changes its length but not its direction (e.g., the direction defined by the vector <1,2,3> is the same as <2,4,6>, but the second vector is twice as long). If we scale the direction vector by 1 / vz to force vz to be 1, then we can define a revised direction vector,

So we will force vz to be 1 and define vx and vy as multiples of vz.
This eliminates vz as a parameter that needs to be computed. Note that this is only valid if vz is not zero which means the line is not parallel to the X-Y plane. You can divide by vx or vy if you want to allow the line to be parallel to the X-Y plane but not some other plane.
[edit] Bad Optimization
So, now we have 4 variables: x0,y0,vx',vy' to optimize from our list of points.
from this it is obvious that
for each point, therefore:
we want to minimize this equation with respect to our four variables
, so we can take the derivative with respect to each which in turn generates four equations for our four unknowns:
simplifying:
rearranging two of the equations:
therefore:
where N is the number of points
[edit] Try again
Distance from line to point,
:



![\sum_i \left[ (v_x'*t + x_0 - x_i)^2 + (v_y'*t + y_0 - y_i)^2 + (v_z'*t + z_0 - z_i)^2 \right]](../../../../math/c/8/8/c88e7bd39e1b78cbeaf39ad64388c485.png)
![= \sum_i \left[ (v_x'*t + x_0 - x_i)^2 + (v_y'*t + y_0 - y_i)^2 + (t - z_i)^2 \right]](../../../../math/5/4/d/54d05aaa0dd8b972c8320552707a9d32.png)
![\Longrightarrow \sum_i \left[ (v_x'*z_i + x_0 - x_i)^2 + (v_y'*z_i + y_0 - y_i)^2 \right]](../../../../math/0/a/d/0ad33e5e038d9591dd8693303793ab2a.png)















