Thanks in advance -
Greg
Principal Components Analysis. Might be overkill, but it's easy to do.
xyz = rand(3,3); % 3 random points, each row is x,y,z coords of a pt.
xyz = bsxfun(@minus,xyz,mean(xyz)); % make the mean = (0,0,0)
[c,xyz2] = princomp(xyz); % xyz2 will be what you want.
xyz_hat = xyz2*c'; % xyz_hat = xyz
You'll need the Statistics Toolbox for princomp. Let me know if you
don't have it.
--
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.
Using Doug's 'xyz' notation with points as rows, do this:
w = cross(xyz(2,:)-xyz(1,:),xys(3,:)-xyz(1,:));
w = w/norm(w);
R = [null(w),w.'];
if det(R)<0, R(:,1:2) = R(:,2:-1:1); end
xyz2 = xyz*R;
Actually the determinant of R need not be forced to be positive in the 'if' line to obtain just the rotated triangle, since a triangle in three dimensions has no inherent right-hand orientation, but if you need to rotate other points outside the triangle's plane along with it, then R should be a valid rotation.
Roger Stafford
Roger Stafford
In fact, if I project point 1 directly to the XY-plane, then choose the other two vertices based on the rigid body constraints-it is also a viable configuration obtained by some rotation permutation. So the rule is up for grabs.
If I were you, this is what I would do:
1. Order the pairs A, B, C. Define a coordinate frame with AB as the X -axis, Cross(AB,BC) as the z-axis and then obtaining the y-axis by the right hand rule. Normalize the vectors
2. The rotation matrix that will take the original X-Y-Z frame to this configuration can be obtained quite simply the way this instructor shows:
http://www.stanford.edu/class/engr14/Documents/RotationMatrixInstructor.pdf
The fact that you defined these rules means that you are now treating your data points democratically and of course, you can account for any ordering bias.
My 2 cents:)
And therefore your approach needs to be modified from a pure rotation to an affine transform, no? Below is my proposed modification and uses my AxelRot tool
http://www.mathworks.com/matlabcentral/fileexchange/30864-3d-rotation-about-shifted-axis
xyz=eye(3); %fake data, points on the unit simplex
centroid=mean(xyz,1),
w = cross(xyz(2,:)-xyz(1,:),xyz(3,:)-xyz(1,:)); %normal to plane
rotAngle = acosd(w(3)/norm(w)); %rotation angle
rotAxis= cross(w,[0,0,1]); %rotation axis
M=AxelRot(rotAngle,rotAxis,centroid);
xyz_new_homogeneous=([xyz,[1; 1; 1]])*M.';
xyz_new=xyz_new_homogeneous(:,1:3),
%Check
centroid_new=mean(xyz_new), %same as old centroid
Here's an example. It is easy to show that the three points of the rows of xyz are coplanar with the origin - the volume of the tetrahedron they form with the origin is zero.
xyz =
1.03072751719372 1.03710039288023 -0.47172543062421
-1.28711731083625 -0.84785872257114 -0.24727209937913
-0.54211111717983 0.36846849121402 -1.46103428529782
The resulting R and xyz2 from my code are:
R =
0 -0.83020044023651 0.55746500251685
-0.47154841677308 -0.49159503423629 -0.73210409980618
0.88184017295585 -0.26287173934322 -0.39147970319783
xyz2 =
-0.90502948362674 -1.24154055724192 0.00000000000000
0.18175196739202 1.55036934269703 -0.00000000000000
-1.46214946050419 0.65298823140276 -0.00000000000000
The determinant of R is +1, so it is a pure rotation and it has brought the three points into the x-y plane. In fact it is, of necessity, a rotation, as I pointed out, about "some line through the origin and lying in the plane bisecting the dihedral angle between the triangle's plane and the x-y plane."
(Note that there was a typo in my original code with 'xys' being in place of 'xyz' at one point.)
Roger Stafford
OK. Well, under this assumption, I'll just note here that the approach with AxelRot would simplify to:
w = cross(xyz(2,:)-xyz(1,:),xyz(3,:)-xyz(1,:)); %normal to plane
rotAngle = acosd(w(3)/norm(w)); %rotation angle
rotAxis= cross(w,[0,0,1]); %rotation axis
xyz_new=xyz*AxelRot(rotAngle,rotAxis,'R').',
Another strange pair of semi-unique possibilities would be to rotate about either line through the origin lying in one of the two dihedral bisectors of the triangle plane and the xy-plane and which are orthogonal to their intersection. These would also seem to qualify as being canonical rotations of a kind, though their rotation angles would always have to be of a maximum pi radians. If the original triangle already lay in the xy-plane, the rotation would change it to a mirror image. I think I prefer the canonical definition in the previous paragraph.
Roger Stafford