LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Find Rotational Matrix to align two 3D vectors

Solved!
Go to solution

Hi all,

 

I have two 3D vectors and i would like to find the rotational matrix for aligning them.  Does anyone have any experience with this?



-Matt
0 Kudos
Message 1 of 6
(10,188 Views)

Yes.  Conceptually, the easiest way to do this is to figure out the Rotation Matrix M1 that transforms some "reference point" (a common choice is the +X Axis) into your first vector, V1 (I trust you can figure out how to do this).  Do the same thing with V2, getting the Rotation Matrix M2 that takes X into V2.  So now to take V1 into V2, start by taking V1 into X (use the inverse of M1) and then take X into V2 (by using M2).

 

A simpler way to do this is to use Rotation Vectors instead of Rotation Matrices, but that's a little more advanced ...

 

Bob Schor

 

 

0 Kudos
Message 2 of 6
(10,158 Views)

So what im actually trying to do is find the rotational matrix of a plane with respect to the coordinate system.  I figured i could take the normal vector of the plane and use that to get the rotational matrix with comparasin to the positive z-axis.  I dont have much experience with matrices nor linear algebra in labview.  I havent even been able to find any good examples of rotational matrix calculation online either.

 

Im a little confused with your response as well.  Do you have an example or can you point me to one?

 

I appreciate any help.



-Matt
0 Kudos
Message 3 of 6
(10,148 Views)
Solution
Accepted by topic author Wolleee

@Wolleee wrote:

I dont have much experience with matrices nor linear algebra in labview. 


To be honest, I also don't have much experience with matrices in LabVIEW.  It has been a while since I wrote "The “Practical Mathematics” of Recording Three-Dimensional Eye Position Using Scleral Coils", and I was not yet acquainted with LabVIEW (and LabVIEW did not yet have Matrices as a Variable Type).

 

So let's learn together.  First, some definitions (just to keep things clear) -- when I say Vector", I mean a 3-D "column vector", and I'm using the Mathematician's "Operator" notation -- a Matrix "Operates" on a Vector (here by matrix multiplication) and does it "on the left", so Rotation Matrix M operating on (column) Vector V is written M*V.

 

I just tried playing with LabVIEW's Matrix representation, particularly to do Matrix Multiplication, and found that the operation I really wanted to do, namely multiply a Vector by a Matrix, is not (really) supported.  But never mind, you just write you own sub-VI and forget about the Matrix Data Type, sticking with 1D and 2D arrays.

 

Do you know how Matrix Multiplication is defined?  If not, look it up.  The operation we really want, Matrix x Vector, is (however) simpler.  If we write a Matrix as M11, M12, M13, M21, ... M33 (which means Row 1 Column 1, Row 1 Column 2, ... Row 3 Column 3) and the Vector as V1, V2, V3 (for Rows 1..3), then the Vector we get from M*V is M11*V1 + M12*V2 + M13*V3, M21*V1 + M22*V2 + M23*V3, M31*V1 + M32*V2 + M33*V3.  A little messy, which is why we bury this in a sub-VI called "M x V".

 

Now we have to talk about Roll (rotation about the X axis), Pitch (rotation about the Y axis), and Yaw (rotation about the Z axis).  Why?  Because you need to be able to come up with the Rotation Matrix that will transform the XY Plane into the "tilted" Plane you are using, and as you surmise, the simple way to do this is to figure out how to transform the Z axis into the normal of your Tilted Plane.  I'm going to do this "backwards" -- describe how to transform the Normal back to the Z axis (the inverse problem) and then describe how to do Roll, Pitch, and Yaw.

 

OK, so you have the coordinates of the unit Normal Vector (a Vector of length 1 normal to your Tilted Plane), with coordinates X, Y, and Z.  You want to transform this (by doing Roll, Pitch, and Yaw rotations) into the unit Z axis.  Start by looking down the Z axis at this vector -- you only "see" the X and Y components, and, in particular, you can express the angle this vector makes with the X axis, call it Theta.  Do a Yaw rotation (rotation about the Z axis) of -Theta, which will place the Vector into the XZ plane.  Now look the Y axis at the Vector.  It makes an angle, call it Phi, with the Z axis.  So now do a rotation about Y (or Pitch) of -Phi, and your 3D vector now points along the Z Axis.

 

So how to you get the Rotation Matrix that represents the original Normal?  Well, if Z is the (column) vector (0, 0, 1), Yaw(Theta) is the Rotation Matrix for a Yaw Rotation of Theta, and Pitch(Phi) is the Rotation Matrix for a Pitch Rotation of Phi, you've just found Theta and Phi such that if N is your unit Normal, Z = Pitch(-Phi) * Yaw(-Theta) * N, or inverting (and noting that "undoing" a rotation is simply rotating in the other direction), N = Yaw(Theta) * Pitch(Phi) * Z.  

 

[Do you know how to make a Right-Hand Coordinate System with your first two fingers and thumb?  Make your fingers at right angles to each other with your Index finger pointing straight ahead as X, your middle finger bent 90° as Y, and your thumb pointing up as Z.  Wiggle your hand around in Roll, Pitch, and Yaw and convince yourself that to "undo" a Yaw followed by a Pitch, you "undo" the Pitch first, as I've shown above].

 

So all that's left (Time for a joke -- "How Do You Get to Carnegie Hall?"  Answer -- "Practice, practice, practice") is to come up with the rotation matrices.  I'm going to do Yaw, you do Pitch and Roll.

 

OK, Yaw leaves Z untouched, and rotates the projection of the 3D vector in the XY Plane , so it affects only the X and Y components.  I assume you know how to rotate a 2D Vector in the XY plane by an angle Theta.  In any case, here's the Matrix, written as a 3 x 3 2D array:  [[cos(Theta), - sin(Theta), 0], [sin(Theta), cos(Theta), 0], [0, 0, 1]].  To convince yourself that this formula is correct, try yawing the X and Y axes by multiples of 90 degrees and see that the axes permute as they should.

 

If you have the patience to work through this yourself, you will have an understanding of rotation matrices when you come up for air.

 

Bob Schor

 

Message 4 of 6
(10,131 Views)

Hi Bob,

 

that was quite the answer and you appear to be quite knolwedgable on the subject.  I found a really simple solution for what i was trying to do, you commented on that Thread as well. Im sure ill come back to this thread one day, but not today. NI actually sells a robotics calculations add on. It does a lot of the math for you, but its quite expensive and i rarely have to use them.

 

As always i appreciate the help.



-Matt
0 Kudos
Message 5 of 6
(10,089 Views)

@Wolleee wrote:

Hi Bob,

 

that was quite the answer and you appear to be quite knolwedgable on the subject.

 


Well, I did write a "Methods" paper on the subject ...

 

BS

0 Kudos
Message 6 of 6
(10,078 Views)