Best-fit 3D line

mutena

New member
Joined
May 16, 2023
Messages
3
I am not sure if this question should in statistic forum or here. I write it here. But if it is relevant to statistic, please let me know.

I have 3 points in space and I would like to get the best-fit line from these points. Let's say P1(0, 0, 0), P2(1, 0, 0) and P3(5, 4, 0). I just made them up. It is possible to use lots of points, let's say 1000.

I know it is really easy to do that in 2D using regression analysis method. However, it is so confusing when it is 3D.

Thank you for your help in advance.
 
Look up multiple linear regression. That gives you a plane of best fit. It will look like [imath]z = \alpha + \beta x + \gamma y[/imath].
 
I am assuming the best-fit is in the sense of the Minimum Sum of Squares of distances from points to the line.
In this case the target function is equal to the rotation momentum of the set of points relative to the line. In particular, this tells me that the line must contain the mass center of the set and its direction vector is eigenvector of the momentum matrix. The latter will have 3 eigenvector, of which you are interested in the one corresponding to the smallest eigenvalue.
Does this make sense?
 
Thank you for answers gentelmen!

the rotation momentum of the set of points relative to the line
@blamocur here the rotation momentum means the shortest distance, isn't it?

this tells me that the line must contain the mass center of the set
The mass center means the average of each coordinate in 3D?

its direction vector is eigenvector of the momentum matrix
I didn't understand exactly the momentum matrix. Is it a matrix about rotation momentum?

I also set these points in two different popular metrology software and the results are same like the following:

1684256202561.png

1684256494486.png
 
I am not sure if this question should in statistic forum or here. I write it here. But if it is relevant to statistic, please let me know.

I have 3 points in space and I would like to get the best-fit line from these points. Let's say P1(0, 0, 0), P2(1, 0, 0) and P3(5, 4, 0). I just made them up. It is possible to use lots of points, let's say 1000.

I know it is really easy to do that in 2D using regression analysis method. However, it is so confusing when it is 3D.

Thank you for your help in advance.
Just curious: is this for homework or something else?
 
Just curious: is this for homework or something else?

No, it is not a homework. Acutally, I am a mechanical engineer who has good experience on metrology. However, I am always curious about the mathematic behind it. I read lots of articles about this but it seems still confusing for me. While I am looking up some another sources on the internet, I run into the this forum.

There are lots of geometries which can be derivated from points. The basic geometries are line, plane, circle, arc, slot, rectangle, square, polygon, elipse, cylinder, cone and sphere. For each, there must be certain number of points in 3D.

Till now, I figured out only best-fit circle :)

I would like to know their mathematics for both my career and personel interest.
 
@blamocur here the rotation momentum means the shortest distance, isn't it?
Sorry, it seems I used a wrong term, whereas the right term seems to be angular momentum. If you think of the set of points as a solid body then the best fitting line will be the axis of rotation with the minimal angular momentum. In practice the computation is actually simpler: just use cross-correlation of coordinate vectors and take the eigenvector corresponding to the maximum eigenvalue.

Here are the details:

There are [imath]n[/imath] points [imath]\mathbf p_k[/imath], where [imath]1 \leq k \leq n[/imath]. Let [imath]p_{k,i}[/imath] be the [imath]i[/imath]-th coordinate of the [imath]k[/imath]-th point, and [imath]c_i[/imath] be the center of mass of all points (where [imath]1 \leq i \leq 3[/imath]), i.e.,
[math]c_i = \frac{1}{n}\sum_{k=1}^n p_{k,i}[/math]Compute a [imath]3\times 3[/imath] cross-correlation matrix [imath]M[/imath] with components
[math]M_{ij} = \sum_{k=1}^n p_{k,i} p_{k,j}[/math]Let [imath]v_i[/imath] be the eigenvalues, and [imath]\mathbf e_i[/imath] the eigenvectors of [imath]M[/imath] (where [imath]1\leq i \leq 3[/imath]). Get index [imath]j[/imath] of the largest eigenvalue [imath]v_j[/imath], then the corresponding eigenvector [imath]\mathbf e_j[/imath] is the direction of the line, and the whole line in question is given by this parametric equation:
[math]\mathbf x = \mathbf c + t \mathbf e_j[/math]For verification purposes below is a listing of a quick-and-dirty Python/Numpy which generates random points and plots both the points and a segment of the computed line in 3D:
Python:
import numpy as np
import matplotlib.pyplot as plt
import random

nn   = 10000;   ## Number of points
lLen = 22;    ## half-length of the plotted center line

# Create a  set of points with random normal 3D distribution

p1 = np.random.randn (3,nn);             # Random 0-centered points with unit variation.
c  = 10 * (np.random.rand (3,1) - 0.5);  # Random center
mm = 10 * (np.random.rand (3,3)-0.5);    # Random transform
p  = c + np.matmul (mm, p1);

#.................................... Compute and print "center line":

c1   = np.mean (p, axis = 1);   ## Compupted mass center of the points.
p0   = p - c1.reshape(3,1);    ## Points' coordinates relative to the mass center.
cMat = np.matmul (p0, p0.T);  ## Auto-correlation matrix
eVal, eVec = np.linalg.eig (cMat);  ## Eigen values and vectors
i0  = np.argmax (eVal);   ## Find the index of the *maximum* eigenvalue...
e0  = eVec[:,i0];         ## ... and the corresponding eigenvector

print ("Center    : ", c1, "\nDirection : ", e0);

##................................... Plot the points and the center line:
fig2 = plt.figure();
ax2  = fig2.add_subplot (1,1,1, projection='3d')
lp1  = c1 + lLen * e0;
lp2  = c1 - lLen * e0;
plt.plot ([lp1[0], lp2[0]], [lp1[1], lp2[1]], [lp1[2], lp2[2]], '.-',color='red');
ax2.plot (p[0,:], p[1,:], p[2,:], '.', markersize=1)
plt.show ();
 
Top