Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Rotate 3D object to align with x-y plane

Subject: Rotate 3D object to align with x-y plane

From: Greg

Date: 26 Apr, 2011 20:36:05

Message: 1 of 9

Hi - I realize this is an old thread but I have a perhaps more explicit question. I have three points given in xyz coordinates. Obviously they are co-planar. Assume the origin is at the center of gravity of the three points. I wish to rotate the points such that the rotated points are in a plane parallel to the xy plane, ie z=0 plane. I seek a rotation operator that will apply this rotation to any set of points, and the inverse operator, to rotate back. I am not concerned with rotation about the z axis - ie, any rotation that puts the three points in a plane parallel to xy, and with the same distances between the pairs of points, will do.

Thanks in advance -
Greg

Subject: Rotate 3D object to align with x-y plane

From: Doug Schwarz

Date: 26 Apr, 2011 21:27:20

Message: 2 of 9

On 4/26/2011 4:36 PM, Greg wrote:
> Hi - I realize this is an old thread but I have a perhaps more explicit
> question. I have three points given in xyz coordinates. Obviously they
> are co-planar. Assume the origin is at the center of gravity of the
> three points. I wish to rotate the points such that the rotated points
> are in a plane parallel to the xy plane, ie z=0 plane. I seek a rotation
> operator that will apply this rotation to any set of points, and the
> inverse operator, to rotate back. I am not concerned with rotation about
> the z axis - ie, any rotation that puts the three points in a plane
> parallel to xy, and with the same distances between the pairs of points,
> will do.
>
> 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.

Subject: Rotate 3D object to align with x-y plane

From: Roger Stafford

Date: 27 Apr, 2011 15:34:04

Message: 3 of 9

"Greg " <gdgibbons@aol.com> wrote in message <ip7abl$p2g$1@fred.mathworks.com>...
> Hi - I realize this is an old thread but I have a perhaps more explicit question. I have three points given in xyz coordinates. Obviously they are co-planar. Assume the origin is at the center of gravity of the three points. I wish to rotate the points such that the rotated points are in a plane parallel to the xy plane, ie z=0 plane. I seek a rotation operator that will apply this rotation to any set of points, and the inverse operator, to rotate back. I am not concerned with rotation about the z axis - ie, any rotation that puts the three points in a plane parallel to xy, and with the same distances between the pairs of points, will do.
>
> Thanks in advance -
> Greg
- - - - - - - - - -
  In case you don't have the Statistics Toolbox, it isn't necessary to use the principal components of the triangle. Any set of unit mutually orthogonal vectors will suffice in which one of them is orthogonal to the triangle and rotates into the z-axis. Also the origin need not be at the triangle's centroid - it only needs to lie somewhere in the triangle's plane. The rotation axis below will be about some line in the x-y plane though the origin, depending on what 'null' chooses to select.

  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

Subject: Rotate 3D object to align with x-y plane

From: Roger Stafford

Date: 27 Apr, 2011 17:19:09

Message: 4 of 9

"Roger Stafford" wrote in message <ip9d1c$29t$1@fred.mathworks.com>...
> ..... The rotation axis below will be about some line in the x-y plane though the origin, depending on what 'null' chooses to select.
> .....
- - - - - - - - -
  Correction: The rotation axis will be 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, depending on the choice made by 'null'.

Roger Stafford

Subject: Rotate 3D object to align with x-y plane

From: Saurabh Mahapatra

Date: 28 Apr, 2011 00:55:21

Message: 5 of 9

Something to ponder-rigid body math tells me that if you give me any two snapshots or configurations of a rigid body(the way you have defined your norm constraints), then there are infinitely many "permutations" of rotations that can do the job of aligning with the xy plane (and some troubling consequence that the pairs of (x,y)s you will generate will be non-unique. So, you will have to define another constraint that limits these possibilities.

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:)

Subject: Rotate 3D object to align with x-y plane

From: Matt J

Date: 11 May, 2011 15:57:08

Message: 6 of 9

"Roger Stafford" wrote in message <ip9j6d$kmc$1@fred.mathworks.com>...
> "Roger Stafford" wrote in message <ip9d1c$29t$1@fred.mathworks.com>...
> > ..... The rotation axis below will be about some line in the x-y plane though the origin, depending on what 'null' chooses to select.
> > .....
> - - - - - - - - -
> Correction: The rotation axis will be 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, depending on the choice made by 'null'.
====================

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

Subject: Rotate 3D object to align with x-y plane

From: Roger Stafford

Date: 11 May, 2011 18:27:02

Message: 7 of 9

"Matt J" wrote in message <iqebkk$d1c$1@newscl01ah.mathworks.com>...
> .......
> And therefore your approach needs to be modified from a pure rotation to an affine transform, no? ........
- - - - - - - - -
  No, Matt, that isn't true. Remember that an assumption was made by Greg that "the origin is at the center of gravity of the three points" and weakened by me to simply that the origin is "to lie somewhere in the triangle's plane". With either of these assumptions, the pure rotation R will bring the three points into the xy-plane.

  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

Subject: Rotate 3D object to align with x-y plane

From: Matt J

Date: 11 May, 2011 19:52:04

Message: 8 of 9

"Roger Stafford" wrote in message <iqekdm$8hq$1@newscl01ah.mathworks.com>...
> "Matt J" wrote in message <iqebkk$d1c$1@newscl01ah.mathworks.com>...
> > .......
> > And therefore your approach needs to be modified from a pure rotation to an affine transform, no? ........
> - - - - - - - - -
> No, Matt, that isn't true. Remember that an assumption was made by Greg that "the origin is at the center of gravity of the three points" and weakened by me to simply that the origin is "to lie somewhere in the triangle's plane". With either of these assumptions, the pure rotation R will bring the three points into the xy-plane.
=================

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').',

Subject: Rotate 3D object to align with x-y plane

From: Roger Stafford

Date: 11 May, 2011 20:57:04

Message: 9 of 9

"Saurabh Mahapatra" wrote in message <ipadtp$gc8$1@fred.mathworks.com>...
> Something to ponder-rigid body math tells me that if you give me any two snapshots or configurations of a rigid body(the way you have defined your norm constraints), then there are infinitely many "permutations" of rotations that can do the job of aligning with the xy plane (and some troubling consequence that the pairs of (x,y)s you will generate will be non-unique. So, you will have to define another constraint that limits these possibilities.
>
> 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:)
- - - - - - - - - - -
  Hello Saurabh. My own recommendation for a "canonical" solution would be to simply rotate about the line of intersection between the triangle's plane and the xy-plane. That requires the smallest possible angle of rotation about its axis. (The matlab code for doing this is very simple, and I should probably have used that as a solution to Greg's original query, rather than depending on the 'null' function's arbitrary choice.)

  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

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us