It's a matter of simple vector analysis. Let P0 = [x0,y0,z0] be some base point on your principal axis and let n = [nx,ny,nz] be a vector pointing along this axis from P0. Now let P1 = [x1,y1,z1] be some point in your cloud. You wish to find the point P2 = [x2,y2,z2] at which the principal axis intersects the plane which is orthogonal to the axis and contains P1, and you want to find the squared distance between P1 and P2. Then we know that
1) P2-P1 is orthogonal to n
and
2) P2-P0 is equal to some scalar multiple of n
Combining these two facts we can arrive at a solution for P2:
P2 = P0 + dot(P1-P0,n)/dot(n,n)*n;
Then the squared distance between P1 and P2 would be dot(P1-P2,P1-P2).
I should add that it is easy to vectorize the expressions for all the P2 points and the squared distances, making use of the bsxfun function.
Best Answer