Calculate Rotation Matrix to align Vector A to Vector B in 3d?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
86
down vote

favorite
75












I have one triangle in 3d space that I am tracking in a simulation. Between time steps I have the the previous normal of the triangle and the current normal of the triangle along with both the current and previous 3d vertex positions of the triangles.



Using the normals of the triangular plane I would like to determine a rotation matrix that would align the normals of the triangles thereby setting the two triangles parallel to each other. I would then like to use a translation matrix to map the previous onto the current, however this is not my main concern right now.



I have found this website http://forums.cgsociety.org/archive/index.php/t-741227.html
that says I must



  • determine the cross product of these two vectors (to determine a rotation axis)

  • determine the dot product ( to find rotation angle)

  • build quaternion (not sure what this means)

  • the transformation matrix is the quaternion as a 3 by 3 ( not sure)

Any help on how I can solve this problem would be appreciated.







share|cite|improve this question
















  • 1




    This may be helpful: gamedev.stackexchange.com/questions/20097/…
    – process91
    Aug 8 '12 at 21:21







  • 6




    I know it's not your main concern right now, but I suspect it will become a concern later: There's no reason to expect that after applying an arbitrary rotation aligning the normals the triangles will be related by a translation -- you'd still have to rotate around the normal to align them. Stated differently, it's not clear that aligning the normals is a good first step, since you then have to perform two separate rotations. You can get the axis of the full rotation by taking the cross-product of the changes in the differences between two pairs of vertex positions.
    – joriki
    Aug 9 '12 at 5:13











  • That is a very good point I had not even though of that
    – user1084113
    Aug 9 '12 at 14:23






  • 2




    @user1084113: No, that would be the cross-product of the changes in two vertex positions; I was talking about the cross-product of the changes in the differences between two pairs of vertex positions, which would be $((A-B)-(A'-B'))times((B-C)times(B'-C'))$. This gives you the axis of rotation (except if it lies in the plane of the triangle) because the translation drops out due to the differences, so this is purely the change in two different vectors due to rotation. The translation wouldn't drop out in your version.
    – joriki
    Aug 10 '12 at 13:51






  • 2




    @user1084113: There's no canonical answer to that question. An orientation-preserving isometry of $mathbb R^3$ can be written as a composition of a rotation and a translation (in either order), but this decomposition is not unique -- you can shift the rotation axis and compensate by performing a different translation. Thus you can only determine the direction of the axis from the data, not its position. You can choose the position arbitrarily, e.g. through the origin, and choose the translation accordingly.
    – joriki
    Aug 10 '12 at 14:33















up vote
86
down vote

favorite
75












I have one triangle in 3d space that I am tracking in a simulation. Between time steps I have the the previous normal of the triangle and the current normal of the triangle along with both the current and previous 3d vertex positions of the triangles.



Using the normals of the triangular plane I would like to determine a rotation matrix that would align the normals of the triangles thereby setting the two triangles parallel to each other. I would then like to use a translation matrix to map the previous onto the current, however this is not my main concern right now.



I have found this website http://forums.cgsociety.org/archive/index.php/t-741227.html
that says I must



  • determine the cross product of these two vectors (to determine a rotation axis)

  • determine the dot product ( to find rotation angle)

  • build quaternion (not sure what this means)

  • the transformation matrix is the quaternion as a 3 by 3 ( not sure)

Any help on how I can solve this problem would be appreciated.







share|cite|improve this question
















  • 1




    This may be helpful: gamedev.stackexchange.com/questions/20097/…
    – process91
    Aug 8 '12 at 21:21







  • 6




    I know it's not your main concern right now, but I suspect it will become a concern later: There's no reason to expect that after applying an arbitrary rotation aligning the normals the triangles will be related by a translation -- you'd still have to rotate around the normal to align them. Stated differently, it's not clear that aligning the normals is a good first step, since you then have to perform two separate rotations. You can get the axis of the full rotation by taking the cross-product of the changes in the differences between two pairs of vertex positions.
    – joriki
    Aug 9 '12 at 5:13











  • That is a very good point I had not even though of that
    – user1084113
    Aug 9 '12 at 14:23






  • 2




    @user1084113: No, that would be the cross-product of the changes in two vertex positions; I was talking about the cross-product of the changes in the differences between two pairs of vertex positions, which would be $((A-B)-(A'-B'))times((B-C)times(B'-C'))$. This gives you the axis of rotation (except if it lies in the plane of the triangle) because the translation drops out due to the differences, so this is purely the change in two different vectors due to rotation. The translation wouldn't drop out in your version.
    – joriki
    Aug 10 '12 at 13:51






  • 2




    @user1084113: There's no canonical answer to that question. An orientation-preserving isometry of $mathbb R^3$ can be written as a composition of a rotation and a translation (in either order), but this decomposition is not unique -- you can shift the rotation axis and compensate by performing a different translation. Thus you can only determine the direction of the axis from the data, not its position. You can choose the position arbitrarily, e.g. through the origin, and choose the translation accordingly.
    – joriki
    Aug 10 '12 at 14:33













up vote
86
down vote

favorite
75









up vote
86
down vote

favorite
75






75





I have one triangle in 3d space that I am tracking in a simulation. Between time steps I have the the previous normal of the triangle and the current normal of the triangle along with both the current and previous 3d vertex positions of the triangles.



Using the normals of the triangular plane I would like to determine a rotation matrix that would align the normals of the triangles thereby setting the two triangles parallel to each other. I would then like to use a translation matrix to map the previous onto the current, however this is not my main concern right now.



I have found this website http://forums.cgsociety.org/archive/index.php/t-741227.html
that says I must



  • determine the cross product of these two vectors (to determine a rotation axis)

  • determine the dot product ( to find rotation angle)

  • build quaternion (not sure what this means)

  • the transformation matrix is the quaternion as a 3 by 3 ( not sure)

Any help on how I can solve this problem would be appreciated.







share|cite|improve this question












I have one triangle in 3d space that I am tracking in a simulation. Between time steps I have the the previous normal of the triangle and the current normal of the triangle along with both the current and previous 3d vertex positions of the triangles.



Using the normals of the triangular plane I would like to determine a rotation matrix that would align the normals of the triangles thereby setting the two triangles parallel to each other. I would then like to use a translation matrix to map the previous onto the current, however this is not my main concern right now.



I have found this website http://forums.cgsociety.org/archive/index.php/t-741227.html
that says I must



  • determine the cross product of these two vectors (to determine a rotation axis)

  • determine the dot product ( to find rotation angle)

  • build quaternion (not sure what this means)

  • the transformation matrix is the quaternion as a 3 by 3 ( not sure)

Any help on how I can solve this problem would be appreciated.









share|cite|improve this question











share|cite|improve this question




share|cite|improve this question










asked Aug 8 '12 at 21:03









user1084113

584169




584169







  • 1




    This may be helpful: gamedev.stackexchange.com/questions/20097/…
    – process91
    Aug 8 '12 at 21:21







  • 6




    I know it's not your main concern right now, but I suspect it will become a concern later: There's no reason to expect that after applying an arbitrary rotation aligning the normals the triangles will be related by a translation -- you'd still have to rotate around the normal to align them. Stated differently, it's not clear that aligning the normals is a good first step, since you then have to perform two separate rotations. You can get the axis of the full rotation by taking the cross-product of the changes in the differences between two pairs of vertex positions.
    – joriki
    Aug 9 '12 at 5:13











  • That is a very good point I had not even though of that
    – user1084113
    Aug 9 '12 at 14:23






  • 2




    @user1084113: No, that would be the cross-product of the changes in two vertex positions; I was talking about the cross-product of the changes in the differences between two pairs of vertex positions, which would be $((A-B)-(A'-B'))times((B-C)times(B'-C'))$. This gives you the axis of rotation (except if it lies in the plane of the triangle) because the translation drops out due to the differences, so this is purely the change in two different vectors due to rotation. The translation wouldn't drop out in your version.
    – joriki
    Aug 10 '12 at 13:51






  • 2




    @user1084113: There's no canonical answer to that question. An orientation-preserving isometry of $mathbb R^3$ can be written as a composition of a rotation and a translation (in either order), but this decomposition is not unique -- you can shift the rotation axis and compensate by performing a different translation. Thus you can only determine the direction of the axis from the data, not its position. You can choose the position arbitrarily, e.g. through the origin, and choose the translation accordingly.
    – joriki
    Aug 10 '12 at 14:33













  • 1




    This may be helpful: gamedev.stackexchange.com/questions/20097/…
    – process91
    Aug 8 '12 at 21:21







  • 6




    I know it's not your main concern right now, but I suspect it will become a concern later: There's no reason to expect that after applying an arbitrary rotation aligning the normals the triangles will be related by a translation -- you'd still have to rotate around the normal to align them. Stated differently, it's not clear that aligning the normals is a good first step, since you then have to perform two separate rotations. You can get the axis of the full rotation by taking the cross-product of the changes in the differences between two pairs of vertex positions.
    – joriki
    Aug 9 '12 at 5:13











  • That is a very good point I had not even though of that
    – user1084113
    Aug 9 '12 at 14:23






  • 2




    @user1084113: No, that would be the cross-product of the changes in two vertex positions; I was talking about the cross-product of the changes in the differences between two pairs of vertex positions, which would be $((A-B)-(A'-B'))times((B-C)times(B'-C'))$. This gives you the axis of rotation (except if it lies in the plane of the triangle) because the translation drops out due to the differences, so this is purely the change in two different vectors due to rotation. The translation wouldn't drop out in your version.
    – joriki
    Aug 10 '12 at 13:51






  • 2




    @user1084113: There's no canonical answer to that question. An orientation-preserving isometry of $mathbb R^3$ can be written as a composition of a rotation and a translation (in either order), but this decomposition is not unique -- you can shift the rotation axis and compensate by performing a different translation. Thus you can only determine the direction of the axis from the data, not its position. You can choose the position arbitrarily, e.g. through the origin, and choose the translation accordingly.
    – joriki
    Aug 10 '12 at 14:33








1




1




This may be helpful: gamedev.stackexchange.com/questions/20097/…
– process91
Aug 8 '12 at 21:21





This may be helpful: gamedev.stackexchange.com/questions/20097/…
– process91
Aug 8 '12 at 21:21





6




6




I know it's not your main concern right now, but I suspect it will become a concern later: There's no reason to expect that after applying an arbitrary rotation aligning the normals the triangles will be related by a translation -- you'd still have to rotate around the normal to align them. Stated differently, it's not clear that aligning the normals is a good first step, since you then have to perform two separate rotations. You can get the axis of the full rotation by taking the cross-product of the changes in the differences between two pairs of vertex positions.
– joriki
Aug 9 '12 at 5:13





I know it's not your main concern right now, but I suspect it will become a concern later: There's no reason to expect that after applying an arbitrary rotation aligning the normals the triangles will be related by a translation -- you'd still have to rotate around the normal to align them. Stated differently, it's not clear that aligning the normals is a good first step, since you then have to perform two separate rotations. You can get the axis of the full rotation by taking the cross-product of the changes in the differences between two pairs of vertex positions.
– joriki
Aug 9 '12 at 5:13













That is a very good point I had not even though of that
– user1084113
Aug 9 '12 at 14:23




That is a very good point I had not even though of that
– user1084113
Aug 9 '12 at 14:23




2




2




@user1084113: No, that would be the cross-product of the changes in two vertex positions; I was talking about the cross-product of the changes in the differences between two pairs of vertex positions, which would be $((A-B)-(A'-B'))times((B-C)times(B'-C'))$. This gives you the axis of rotation (except if it lies in the plane of the triangle) because the translation drops out due to the differences, so this is purely the change in two different vectors due to rotation. The translation wouldn't drop out in your version.
– joriki
Aug 10 '12 at 13:51




@user1084113: No, that would be the cross-product of the changes in two vertex positions; I was talking about the cross-product of the changes in the differences between two pairs of vertex positions, which would be $((A-B)-(A'-B'))times((B-C)times(B'-C'))$. This gives you the axis of rotation (except if it lies in the plane of the triangle) because the translation drops out due to the differences, so this is purely the change in two different vectors due to rotation. The translation wouldn't drop out in your version.
– joriki
Aug 10 '12 at 13:51




2




2




@user1084113: There's no canonical answer to that question. An orientation-preserving isometry of $mathbb R^3$ can be written as a composition of a rotation and a translation (in either order), but this decomposition is not unique -- you can shift the rotation axis and compensate by performing a different translation. Thus you can only determine the direction of the axis from the data, not its position. You can choose the position arbitrarily, e.g. through the origin, and choose the translation accordingly.
– joriki
Aug 10 '12 at 14:33





@user1084113: There's no canonical answer to that question. An orientation-preserving isometry of $mathbb R^3$ can be written as a composition of a rotation and a translation (in either order), but this decomposition is not unique -- you can shift the rotation axis and compensate by performing a different translation. Thus you can only determine the direction of the axis from the data, not its position. You can choose the position arbitrarily, e.g. through the origin, and choose the translation accordingly.
– joriki
Aug 10 '12 at 14:33











19 Answers
19






active

oldest

votes

















up vote
73
down vote



accepted










Suppose you want to find a rotation matrix $R$ that rotates unit vector $a$ onto unit vector $b$.



Proceed as follows:



Let $v = a times b$



Let $s = |v|$ (sine of angle)



Let $c = a cdot b$ (cosine of angle)



Then the rotation matrix R is given by:
$$R = I + [v]_times + [v]_times^2frac1-cs^2,$$



where $[v]_times$ is the skew-symmetric cross-product matrix of $v$,
$$[v]_times stackrelrm def= beginbmatrix
,,0 & !-v_3 & ,,,v_2\
,,,v_3 & 0 & !-v_1\
!-v_2 & ,,v_1 &,,0
endbmatrix.$$



The last part of the formula can be simplified to
$$
frac1-cs^2 = frac1-c1-c^2 = frac11+c,
$$
revealing that it is not applicable only for $cos(angle(a, b)) = -1$, i.e., if $a$ and $b$ point into exactly opposite directions.






share|cite|improve this answer


















  • 3




    I confirm that this works and gives answers identical to those in my answer.
    – Kuba Ober
    Aug 14 '14 at 22:46










  • I tried to implement this, however I am getting an issue when the cross products is 0.
    – user2970916
    Jan 14 '15 at 14:40










  • cross product can't be 0 because a and b are unit vectors
    – Julien__
    May 23 '15 at 22:45






  • 2




    @Julien__ what if a == b?
    – Jerem
    May 30 '15 at 12:22






  • 2




    Indeed... I forgot that case. In that case v = 0 and the formula gives R = I. So here is a better answer : when the cross product is 0, R = I
    – Julien__
    May 31 '15 at 13:08


















up vote
32
down vote













Using Kjetil's answer answer, with process91's comment, we arrive at the following procedure.



Derivation



We are given two unit column vectors, $A$ and $B$ ($|A|=1$ and $|B|=1$). The $|circ|$ denotes the L-2 norm of $circ$.



First, note that the rotation from $A$ to $B$ is just a 2D rotation on a plane with the normal $A times B$. A 2D rotation by an angle $theta$ is given by the following augmented matrix:
$$G=beginpmatrix
costheta & -sintheta & 0 \
sintheta & costheta & 0 \
0 & 0 & 1
endpmatrix.$$



Of course we don't want to actually compute any trig functions. Given our unit vectors, we note that $costheta=Acdot B$, and $sintheta=||Atimes B||$. Thus
$$G=beginpmatrix
Acdot B & -|Atimes B| & 0 \
|Atimes B| & Acdot B & 0 \
0 & 0 & 1endpmatrix.$$



This matrix represents the rotation from $A$ to $B$ in the base consisting of the following column vectors:



  1. normalized vector projection of $B$ onto $A$: $$u==A$$


  2. normalized vector rejection of $B$ onto $A$: $$v=$$


  3. the cross product of $B$ and $A$: $$w=B times A$$


Those vectors are all orthogonal and normal, and form an orthonormal basis. This is the detail that Kjetil had missed in his answer.



The basis change matrix for this basis is:
$$F=beginpmatrixu & v & w endpmatrix^-1=beginpmatrix A & & B times Aendpmatrix^-1$$



Thus, in the original base, the rotation from $A$ to $B$ can be expressed as right-multiplication of a vector by the following matrix: $$U=F^-1G F.$$



One can easily show that $U A = B$, and that $|U|_2=1$. Also, $U$ is the same as the $R$ matrix from Rik's answer.



2D Case



For the 2D case, given $A=left(x_1,y_1,0right)$ and $B=left(x_2,y_2,0right)$, the matrix $G$ is the forward transformation matrix itself, and we can simplify it further. We note
$$beginaligned
costheta &= Acdot B = x_1x_2+y_1y_2 \
sintheta &= | Atimes B| = x_1y_2-x_2y_1
endaligned$$



Finally,
$$Uequiv G=beginpmatrix
x_1x_2+y_1y_2 & -(x_1y_2-x_2y_1) \
x_1y_2-x_2y_1 & x_1x_2+y_1y_2
endpmatrix$$
and
$$U^-1equiv G^-1=beginpmatrix
x_1x_2+y_1y_2 & x_1y_2-x_2y_1 \
-(x_1y_2-x_2y_1) & x_1x_2+y_1y_2
endpmatrix$$



Octave/Matlab Implementation



The basic implementation is very simple. You could improve it by factoring out the common expressions of dot(A,B) and cross(B,A). Also note that $||Atimes B||=||Btimes A||$.



GG = @(A,B) [ dot(A,B) -norm(cross(A,B)) 0;
norm(cross(A,B)) dot(A,B) 0;
0 0 1];

FFi = @(A,B) [ A (B-dot(A,B)*A)/norm(B-dot(A,B)*A) cross(B,A) ];

UU = @(Fi,G) Fi*G*inv(Fi);


Testing:



> a=[1 0 0]'; b=[0 1 0]';
> U = UU(FFi(a,b), GG(a,b));
> norm(U) % is it length-preserving?
ans = 1
> norm(b-U*a) % does it rotate a onto b?
ans = 0
> U
U =

0 -1 0
1 0 0
0 0 1


Now with random vectors:



> vu = @(v) v/norm(v);
> ru = @() vu(rand(3,1));
> a = ru()
a =

0.043477
0.036412
0.998391
> b = ru()
b =

0.60958
0.73540
0.29597
> U = UU(FFi(a,b), GG(a,b));
> norm(U)
ans = 1
> norm(b-U*a)
ans = 2.2888e-16
> U
U =

0.73680 -0.32931 0.59049
-0.30976 0.61190 0.72776
-0.60098 -0.71912 0.34884


Implementation of Rik's Answer



It is computationally a bit more efficient to use Rik's answer. This is also an Octave/MatLab implementation.



ssc = @(v) [0 -v(3) v(2); v(3) 0 -v(1); -v(2) v(1) 0]
RU = @(A,B) eye(3) + ssc(cross(A,B)) +
ssc(cross(A,B))^2*(1-dot(A,B))/(norm(cross(A,B))^2)


The results produced are same as above, with slightly smaller numerical errors since there are less operations being done.






share|cite|improve this answer






















  • Matlab notation for Rik's Implementation: function R=fcn_RotationFromTwoVectors(A, B) v = cross(A,B); ssc = [0 -v(3) v(2); v(3) 0 -v(1); -v(2) v(1) 0]; R = eye(3) + ssc + ssc^2*(1-dot(A,B))/(norm(v))^2;
    – phyatt
    Aug 20 '15 at 21:55











  • @phyatt Doesn't the lambda syntax work in Matlab, too?
    – Kuba Ober
    Aug 20 '15 at 22:16










  • I've used lamdas in other languages, and I haven't seen it in use in the Matlab projects... and sure enough, its there. mathworks.com/help/matlab/matlab_prog/anonymous-functions.html Thanks @KuberOber.
    – phyatt
    Aug 20 '15 at 22:21










  • Rik's answer doesn't work if a == b, does your answer work in this case?
    – Ruts
    Nov 10 '15 at 8:53










  • @Ruts It can't work, since you can't normalize a zero-length vector a-dot(a,a)*a. You have to check for a==b first :)
    – Kuba Ober
    Nov 10 '15 at 17:06


















up vote
6
down vote













At the top of my head (do the checking yourself)
Let the given vectors in $R^3$ be $A$ and $B$, for simplicity assume they have norm 1, and assume they are not identical. Define $C$ as the cross product of $A$ and $B$. We want an orthogonal matrix $U$ such that $UA=B$ and $UC=C$.
First change bases, to the new base $(U_1,u_2,u_3)=(A,B,C)$. In this new basis the matrix doing the job is simply $G=left(beginsmallmatrix 0&1&0\1&0&0\0&0&1endsmallmatrixright)$. Then we need the basis shift matrix, to the new basis. Write the coordinates of the vectors in the old base as simply $A=(a_1,a_2,a_3), B=(b_1,b_2,b_3), C=(c_1,c_2,c_3)$. Then the basis shift matrix can be seen to be $left( beginsmallmatrix a_1&b_1&c_1\a_2&b_2&c_2\a_3&b_3&c_3 endsmallmatrixright)^-1$. The result is now simply $U=F^-1 G F$, which is an orthogonal matrix rotating $A$ into $B$.






share|cite|improve this answer




















  • Actually, I think there may be further trouble - your matrix $U$ is only guaranteed to be orthogonal if $F$ is, which is only the case if $A$ and $B$ are orthogonal. In other instances, the angles of various other vectors to which $U$ is applied may be stretched or compressed.
    – process91
    Aug 9 '12 at 19:21










  • Your thought process is a good one, though - don't give up on it yet! I think there is a solution this way if you consider different original basis vectors which still have an easily represented form of $A$ and $B$. You could, for instance, choose the projection of $A$ on $B$, the rejection of $A$ on $B$, and $A times B$.
    – process91
    Aug 9 '12 at 19:50







  • 2




    Just so that people aren't derailed: this answer doesn't work as-is, one has to take process91's comments into account. +1 since it provided valuable inspiration.
    – Kuba Ober
    Aug 14 '14 at 22:26


















up vote
4
down vote













The MATLAB code for any dimension greater than one is



u = a/norm(a); % a and b must be column vectors
v = b/norm(b); % of equal length
N = length(u);
S = reflection( eye(N), v+u ); % S*u = -v, S*v = -u
R = reflection( S, v ); % v = R*u


where



function v = reflection( u, n ) % Reflection of u on hyperplane n.
%
% u can be a matrix. u and v must have the same number of rows.

v = u - 2 * n * (n'*u) / (n'*n);
return


See this for background on how this works.






share|cite|improve this answer





























    up vote
    3
    down vote













    The quaternion is a 4-dimensional complex number:
    http://en.wikipedia.org/wiki/Quaternion
    used to describe rotations in space. A quaternion (like a complex number) has a polar representation involving the exponential of the arguments (rotations), and a magnetude multiplier. Building the quaternion comes from the cross product (the product of the complex components), which will give you the argument in those 3 dimensions, you'll then get a number from that in the form A+Bi+Cj+Dk, and write it out in the matrix form described in the article there.



    An easier way would be to simply fingure out what your original vectors are in the 4-space, and take the appropriate inverse operations to get your resultant quaternion (without going through the dot/cross product steps) but that requires a good foundation in hypercomplex algebra.






    share|cite|improve this answer
















    • 1




      Just to be clear: from the article, the dot product will be your scalar part, and the cross product the vector part, so with dot product: x and scalar product iy + jz + ka the quaternion q would be q = x +iy +jz +ka.
      – Vilid
      Aug 8 '12 at 21:51











    • Not exactly. If the dot product is a, and the cross product is (x, y, z), it would be cos(a/2) + (x * sin(a/2))i + (y * sin(a/2))j + ( z * sin(a/2))k. Rotation quaternions are unitary.
      – Jeff
      Jul 19 '13 at 2:33


















    up vote
    3
    down vote













    Use Rodrigues' rotation formula (See the section "Conversion to rotation matrix"). $costheta$ is the dot product of the normalised initial vectors and $sintheta$ can be determined from $sin^2theta + cos^2theta =1$






    share|cite|improve this answer






















    • I don't think $sin theta$ can be determined in that way. There is sign ambiguity. (If it weren't so, then we'd get the same rotation matrix for going from $a$ to $b$ and viceversa.)
      – leonbloy
      Aug 8 '17 at 23:59


















    up vote
    3
    down vote













    Rodrigues's rotation formula gives the result of a rotation of a vector $a$ around a rotation axis $k$ by the angle $theta$. We can make use of this by realizing that, to rotate a unit vector $a$ into $b$, we simply need to rotate $a$ by $pi$ around $(a+b)/2$. With this, one gets the beautiful
    $$
    R = 2 frac(a+b)(a+b)^T(a+b)^T(a+b) - I.
    $$






    share|cite|improve this answer






















    • I can confirm that this works. Checked using Wolfram Mathematica that $Rcdot a=b$ with the assumptions that $||a||=||b||=1$.
      – Ruslan
      Apr 28 at 19:30











    • It might be instructive to note that another way of thinking (that of course leads to a very similar result) are Householder reflections
      – Bort
      May 10 at 19:24

















    up vote
    2
    down vote













    Here is a Matlab function that can be used to calculated the rotation from one vector to another.



    Example 1:



    >> v1=[1 2 3]';
    >> v2=[4 5 6]';
    >> fcn_RotationFromTwoVectors(v1, v2)

    ans =

    0.9789 0.0829 0.1870
    -0.0998 0.9915 0.0829
    -0.1785 -0.0998 0.9789


    Example 2:



    >> v1=[1 2 0]';
    >> v2=[3 4 0]';
    >> fcn_RotationFromTwoVectors(v1, v2)

    ans =

    0.9839 0.1789 0
    -0.1789 0.9839 0
    0 0 1.0000


    Function:



    function R=fcn_RotationFromTwoVectors(v1, v2)
    % R*v1=v2
    % v1 and v2 should be column vectors and 3x1

    % 1. rotation vector
    w=cross(v1,v2);
    w=w/norm(w);
    w_hat=fcn_GetSkew(w);
    % 2. rotation angle
    cos_tht=v1'*v2/norm(v1)/norm(v2);
    tht=acos(cos_tht);
    % 3. rotation matrix, using Rodrigues' formula
    R=eye(size(v1,1))+w_hat*sin(tht)+w_hat^2*(1-cos(tht));

    function x_skew=fcn_GetSkew(x)
    x_skew=[0 -x(3) x(2);
    x(3) 0 -x(1);
    -x(2) x(1) 0];





    share|cite|improve this answer





























      up vote
      2
      down vote













      As you read from the thread: http://forums.cgsociety.org/archive/index.php/t-741227.html




      Note that this will not align all 3 axes of the teapots, only the Z axis. We have no knowledge of the full orientation of the teapots in space, only know where the Z are pointing at. theTM will be the value you are looking for.




      There is NO unique Matrix that could rotate one unit vector to another. Simply because the solution to 3 equations with 9 arguments does not unique.



      Since you have the plane (not only the normal vector), a way to find a unique rotation matrix between two coordinate system would be: do the non-unique rotation twice!



      That is



      1. Find a orthogonal vector in the same plane of interest with A and B respectively. Say $A_o bot A$ in the original plane and $B_o bot B$ in the target plane.

      2. use previous answers: This or this to find the rotation matrix within the $Atimes B$ plane. Assume matrix is $U_AB$

      3. Apply the matrix $U_AB$ to $A_o$ result in a new $B'_o=U_ABA_o$.

      4. This $B'_o$ can be used as one of the new inputs to the same rotation funtion from the previous answers together with $B_o$. Another rotation matrix $U_B'B$ is calculated in $B'times B$ plane.

      5. Since $B'times B$ plane is perpendicular to $A$, $U_B'B$ would not influence the transform of $Ato B$. i.e. $B=U_ABA=U_B'BU_ABA$. and the final coordinate transform matrix should be $U=U_B'BU_AB$

      6. consider yourself for corner cases. :)

      validate:



      function:



      % Implementation of Rik's Answer:
      ssc = @(v) [0 -v(3) v(2); v(3) 0 -v(1); -v(2) v(1) 0];
      RU = @(A,B) eye(3) + ssc(cross(A,B)) + ...
      ssc(cross(A,B))^2*(1-dot(A,B))/(norm(cross(A,B))^2);


      Test:





      > a=[1 0 0]'; b=[0 1 0]';
      > ao=[0 1 0]'; bo=[-sqrt(2)/2,0,sqrt(2)/2]';
      > Uab = RU(a,b);
      > Ucd = RU(Uab*ao,bo);
      > U = Ucd*Uab;
      > norm(b-U*a) % does Ucd influence a to b?
      ans = 0
      > norm(bo-U*ao) % does Ucd influence a-orthogonal and b-orthogonal?
      ans = 0
      > U
      U =

      0 -0.7071 0.7071
      1.0000 0 0
      0 0.7071 0.7071





      share|cite|improve this answer






















      • Please, use Latex.
        – Mithlesh Upadhyay
        Jul 15 '16 at 9:21










      • @MithleshUpadhyay I don't get it? I feel somewhat difficult when editing answers but I did use some LaTeX for equations. They looks nice on my screen. Is there anything I can do better? If there are tricks and tips I'm willing to hear from you.
        – Kangqiao Zhao
        Jul 19 '16 at 11:47

















      up vote
      1
      down vote













      You can easily do all this operation using the Vector3 library.



      The following four steps worked for me.



      Vector3 axis = Vector3.CrossProduct(v1, v2);

      if (axis.Magnitude != 0)

      axis.Normalize();
      Vector3D vAxis = new Vector3D(axis.X, axis.Y, axis.Z);
      Matrix3D m = Matrix3D.Identity;
      Quaternion q = new Quaternion(vAxis, AngleFromZaxis);

      m.RotateAt(q, centerPoint);
      MatrixTransform3D mT = new MatrixTransform3D(m);

      group.Children.Add(mT);

      myModel.Transform = group;






      share|cite|improve this answer


















      • 1




        You use an undeclared variable AngleFromZaxis in your example there. And group. Hmm this code is a bit of a mess unfortunately.
        – Andrew Hundt
        Apr 30 '15 at 21:39

















      up vote
      1
      down vote













      General solution for n dimensions in matlab / octave:



      %% Build input data
      n = 4;
      a = randn(n,1);
      b = randn(n,1);



      %% Compute Q = rotation matrix
      A = a*b';
      [V,D] = eig(A'+A);
      [~,idx] = min(diag(D));
      v = V(:,idx);
      Q = eye(n) - 2*(v*v');



      %% Validate Q is correct
      b_hat = Q'*a*norm(b)/norm(a);
      disp(['norm of error = ' num2str(norm(b_hat-b))])
      disp(['eigenvalues of Q = ' num2str(eig(Q)')])






      share|cite|improve this answer





























        up vote
        1
        down vote













        You could say you are looking for a transformation between two orthonormal bases:



        $$
        M*[veci,vecj,veck]=[veci',vecj',veck']
        $$
        where



        • $veci$ and $veci'$ are the two vectors in question ("from" and "to")

        and the missing parts can be picked in a way which suits the purpose:



        • $vecj=vecj'=fracvecitimesveci'$, the axis of rotation, which is left in place and is orthogonal to the $veci,veci'$ plane (and has to be normalized)

        • $veck=vecitimesvecj, veck'=veci'timesvecj'$, because you need a pair of 3rd vectors for completing the bases.

        Since $[veci,vecj,veck]$ is an orthogonal matrix, there is no need for 'real' inversion and the transformation is



        $$
        M=[veci',vecj',veck']*[veci,vecj,veck]^T
        $$
        Except when $veci,veci'$ do not stretch a plane (and thus $||vecitimesveci'||=0$). This calculation will not produce $I$, e.g. dies on both $veci=veci'$ and $veci=-veci'$



        Disclaimer: Sorry about the necro, and especially for the partial repeat: I see Kjetil's answer, but I simply do not understand what and why that skewed matrix is doing there, and while Kuba's answer says it builds on Kjetil's, it introduces trigonometry on top of that, slightly defeating the idea (of course I understand that the trigonometric part is expressed with dot/cross products at the end)



        Plus this was my first time with LaTeX, I feel $veci~veci'$ look a bit too similar, but $veci~veci'$ is just plain ugly. And writing that fraction properly is way above me.






        share|cite|improve this answer





























          up vote
          0
          down vote













          Here's how to find the transformation from one triangle to another.



          First triangle has vertices $a,b,c$ and normal $n$ and second triangle has vertices $a',b',c'$ and normal $n'$.



          First we will find transformation $f(x)=Mx+t$ from reference triangle to the first triangle and another transformation $g(x)=M'x+t'$ from reference triangle to the second triangle. Then the transformation mapping first triangle to the second is $$T(x) = g(f^-1(x)) = M'(M^-1(x-t)) + t' = Rx + s$$
          where $R = M'M^-1$ and $s = -M'M^-1t + t'$.



          As the reference triangle we will use triangle with vertices $(0,0,0),(1,0,0),(0,1,0)$ and normal $(0,0,1)$



          Then:
          $$M = [ b-a, c-a, n]$$
          $$t = a $$
          $$M' = [ b'-a', c'-a', n' ] $$
          $$t' = a' $$



          You have to be cautious and give vertices $a,b,c$ and $a',b',c'$ in right order. This has to satisfy $((b-a)times (c-a))cdot n > 0$ and the same for second triangle.




          If those two triangles are not the same then matrix $R$ will not be orthogonal. But we can find isometry which maps one triangle to the another as close as possible in some sense. For this you can use Kabsch algorithm which is well explained here.






          share|cite|improve this answer





























            up vote
            0
            down vote













            This is an interesting problem. It guarantees that you rotate the unit vector $a$ to coincide with $b$. Still it would not be enough to rotate a rigid body to make it coincide with another. I will post an example of this below but first let me point a few important references. @glennr correctly pointed out the Rodrigues rotation formula . Note that this formula is the same as the one shown by @Jur var den vec here. The scaling factors are due to normalization of the cross and dot products since the vector $v$ in the Wikipedia website (here $a$) is arbitrary and here $a$ is unit. The Wikipedia page only requires vector manipulations (need to be good at taking cross products). Here is an elegant proof that does not depend too much on geometrical constructions but it requires simple linear ordinary differential equations for matrices, in addition to the cross product representation as a matrix operator. The final reference is the most complete document that I have found on this topic. It is titled ROTATION: A review of useful theorems involving proer orthogonal matries referenced to three dimensional physical space.



            Now, for an example . This stack exchange link is about a question I posted on the rotation of a tetrahedron. I found and answer to my question by two consecutive rotations of matrices. The apex of the tetrahedron was at $(0,0,sqrt(3))$ and I wanted it to be at $(-1,-1,1)$.
            Of course I wanted to verify that all vertices were rotated to their correct place and the product of matrices:
            begineqnarray Y = left ( beginarrayccc cos alpha &
            -sin alpha & 0 \ sin alpha & cos alpha & 0 \
            0 & 0 & 1 endarray right )
            = left ( beginarrayccc fracsqrt22 & -fracsqrt22 & 0 \ fracsqrt22 & fracsqrt22 & 0 \
            0 & 0 & 1 endarray right )
            endeqnarray



            and
            begineqnarray P = left (
            beginarrayccc cos theta & 0 & -sin theta \ 0 & 1 & 0
            \ sin theta & 0 & cos theta endarray right )
            = left ( beginarrayccc fracsqrt33 & 0 & -fracsqrt2sqrt3 \
            0 & 1 & 0 \ fracsqrt2sqrt3 & 0 & fracsqrt33 \ endarray right )
            endeqnarray



            That is the matrix:
            begineqnarray*
            frac16 left (
            beginarrayccc
            sqrt6 & -3sqrt2 & - 2 sqrt3 \
            sqrt6 & 3 sqrt2 & -2 sqrt3 \
            2 sqrt6 & 0 & 2 sqrt3
            endarray
            right )
            endeqnarray*



            did the job.



            I thought that the Rodrigues rotation (the one being considered here)
            would do the job but it did not. I computed the Rodrigues rotation for this problem. Here is the result:



            begineqnarray*
            R =
            left (
            beginarrayccc
            fracsqrt3+12 sqrt3 & -fracsqrt3-12 sqrt3 & -frac1sqrt3 \
            -fracsqrt3-12 sqrt3 & fracsqrt3+12 sqrt3& -frac1sqrt3 \
            frac1sqrt3 & frac1sqrt3 & frac1sqrt3
            endarray
            right )
            endeqnarray*



            This matrix correctly maps the vector $(0,0,1)$ into the vector
            $frac1sqrt3 (-1,-1,1)$ as promised, but the other three vectors on the base of the pyramid (tetrahedron) are not correctly mapped into their positions.



            The meaning of this is that, to map rigid bodies through rotations, there is not a magic formula where only one matrix could be found and instead a multiplication of elementary (or non elmentary) matrices is required to do the job, unless we are lucky. Right?






            share|cite|improve this answer





























              up vote
              0
              down vote













              Kuba Ober and Leyu Wang's answer works great. Here, is a python implementation of the same algorithm.



              import numpy as np
              import math


              def rotation_matrix(A,B):
              # a and b are in the form of numpy array

              ax = A[0]
              ay = A[1]
              az = A[2]

              bx = B[0]
              by = B[1]
              bz = B[2]

              au = A/(np.sqrt(ax*ax + ay*ay + az*az))
              bu = B/(np.sqrt(bx*bx + by*by + bz*bz))

              R=np.array([[bu[0]*au[0], bu[0]*au[1], bu[0]*au[2]], [bu[1]*au[0], bu[1]*au[1], bu[1]*au[2]], [bu[2]*au[0], bu[2]*au[1], bu[2]*au[2]] ])


              return(R)





              share|cite|improve this answer



























                up vote
                0
                down vote













                One way to proceed is as following:



                Start by constructing one orthonormal basis for each of the vectors $vecn_1$ and $vecn_2$. This can be done by the trick given in an answer to another question [1]. This will result in two transformation matrices



                $$R_1=beginbmatrixvecu_1 & vecv_1 & vecw_1endbmatrix$$



                and
                $$R_2=beginbmatrixvecu_2 & vecv_2 & vecw_2endbmatrix$$



                Then the rotation matrix for aligning $vecn_1$ onto $vecn_2$ becomes



                $$R=R_2R_1^Tvecn_1$$



                [1] https://math.stackexchange.com/q/712065






                share|cite|improve this answer





























                  up vote
                  0
                  down vote













                  Given two unit vectors $hat a$ and $hat c$, reflecting a vector $x$ across the orthogonal complement of $hat a$ and then for $hat c$ will rotate the part of $x$ in the span of $hat a$ and $hat c$ by twice the angle from $hat a$ to $hat c$. Letting $hat c = frachat a + hat b$ be the unit vector that bisects $hat a$ and $hat b$, the composition of the two reflections $R_hat c circ R_hat a$ will rotate $hat a$ to $hat b$, and any vector $x$ by the angle from $hat a$ to $hat b$. Recall that the reflection transformation is $R_n(x) = x - 2 fracx cdot nn cdot n n$.






                  share|cite|improve this answer



























                    up vote
                    0
                    down vote













                    Sadly I don't have enough points to comment on the accepted answer but as others have noted, the formula doesn't work when a == -b.



                    To solve this edge case you have to create a normal vector of a by for example using the formula found here (a,b and c being the components of the vector):




                    function (a,b,c) 

                    return c<a ? (b,-a,0) : (0,-c,b)




                    then make the rotation matrix by rotating vector a around this normal by Pi.






                    share|cite|improve this answer





























                      up vote
                      -1
                      down vote













                      I have a simpler method comes from Erigen's "Mechanics of Continua". R is rotational matrix that rotate vector "a" align with vector "b" Matlab Code:



                      %%%%%% Rotate vector a align with vector b%%%%%%%%%% 

                      syms ax ay az bx by bz k real

                      a=[ax ay az]'
                      au=a./sqrt(ax^2+ay^2+az^2)

                      b=[bx by bz]'
                      bu=b./sqrt(bx^2+by^2+bz^2)

                      R=[bu(1)*au(1) bu(1)*au(2) bu(1)*au(3);

                      bu(2)*au(1) bu(2)*au(2) bu(2)*au(3);

                      bu(3)*au(1) bu(3)*au(2) bu(3)*au(3)]


                      To verify:



                      c=R*a
                      cu=c./sqrt(c(1)^2+c(2)^2+c(3)^2)
                      simple(bu-cu)


                      A zero result means that $c$ (rotated $a$) and $b$ are aligned with each other.



                      simple(sqrt(c(1)^2+c(2)^2+c(3)^2)-sqrt(c(1)^2+c(2)^2+c(3)^2))


                      A zero result means that $c$ (rotated $a$) and $a$ are of the same length.






                      share|cite|improve this answer






















                      • Here's a MathJax tutorial :)
                        – barto
                        Jul 25 '14 at 17:50










                      • Note that R = (au*bu')'. Or, simply, $R_ij = hatb_i hata_j$
                        – Kuba Ober
                        Aug 14 '14 at 18:49










                      • Of course this doesn't really work. Say let a=[1 0 0], b=[0 1 0], we should get a simple $90^circ$ rotation around the z axis, with R=[0 1 0; 1 0 0; 0 0 1].
                        – Kuba Ober
                        Aug 14 '14 at 18:54










                      • In all cases the $R$ squishes $a times b$ to zero.
                        – Kuba Ober
                        Aug 14 '14 at 19:36











                      Your Answer




                      StackExchange.ifUsing("editor", function ()
                      return StackExchange.using("mathjaxEditing", function ()
                      StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
                      StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
                      );
                      );
                      , "mathjax-editing");

                      StackExchange.ready(function()
                      var channelOptions =
                      tags: "".split(" "),
                      id: "69"
                      ;
                      initTagRenderer("".split(" "), "".split(" "), channelOptions);

                      StackExchange.using("externalEditor", function()
                      // Have to fire editor after snippets, if snippets enabled
                      if (StackExchange.settings.snippets.snippetsEnabled)
                      StackExchange.using("snippets", function()
                      createEditor();
                      );

                      else
                      createEditor();

                      );

                      function createEditor()
                      StackExchange.prepareEditor(
                      heartbeatType: 'answer',
                      convertImagesToLinks: true,
                      noModals: false,
                      showLowRepImageUploadWarning: true,
                      reputationToPostImages: 10,
                      bindNavPrevention: true,
                      postfix: "",
                      noCode: true, onDemand: true,
                      discardSelector: ".discard-answer"
                      ,immediatelyShowMarkdownHelp:true
                      );



                      );













                       

                      draft saved


                      draft discarded


















                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f180418%2fcalculate-rotation-matrix-to-align-vector-a-to-vector-b-in-3d%23new-answer', 'question_page');

                      );

                      Post as a guest






























                      19 Answers
                      19






                      active

                      oldest

                      votes








                      19 Answers
                      19






                      active

                      oldest

                      votes









                      active

                      oldest

                      votes






                      active

                      oldest

                      votes








                      up vote
                      73
                      down vote



                      accepted










                      Suppose you want to find a rotation matrix $R$ that rotates unit vector $a$ onto unit vector $b$.



                      Proceed as follows:



                      Let $v = a times b$



                      Let $s = |v|$ (sine of angle)



                      Let $c = a cdot b$ (cosine of angle)



                      Then the rotation matrix R is given by:
                      $$R = I + [v]_times + [v]_times^2frac1-cs^2,$$



                      where $[v]_times$ is the skew-symmetric cross-product matrix of $v$,
                      $$[v]_times stackrelrm def= beginbmatrix
                      ,,0 & !-v_3 & ,,,v_2\
                      ,,,v_3 & 0 & !-v_1\
                      !-v_2 & ,,v_1 &,,0
                      endbmatrix.$$



                      The last part of the formula can be simplified to
                      $$
                      frac1-cs^2 = frac1-c1-c^2 = frac11+c,
                      $$
                      revealing that it is not applicable only for $cos(angle(a, b)) = -1$, i.e., if $a$ and $b$ point into exactly opposite directions.






                      share|cite|improve this answer


















                      • 3




                        I confirm that this works and gives answers identical to those in my answer.
                        – Kuba Ober
                        Aug 14 '14 at 22:46










                      • I tried to implement this, however I am getting an issue when the cross products is 0.
                        – user2970916
                        Jan 14 '15 at 14:40










                      • cross product can't be 0 because a and b are unit vectors
                        – Julien__
                        May 23 '15 at 22:45






                      • 2




                        @Julien__ what if a == b?
                        – Jerem
                        May 30 '15 at 12:22






                      • 2




                        Indeed... I forgot that case. In that case v = 0 and the formula gives R = I. So here is a better answer : when the cross product is 0, R = I
                        – Julien__
                        May 31 '15 at 13:08















                      up vote
                      73
                      down vote



                      accepted










                      Suppose you want to find a rotation matrix $R$ that rotates unit vector $a$ onto unit vector $b$.



                      Proceed as follows:



                      Let $v = a times b$



                      Let $s = |v|$ (sine of angle)



                      Let $c = a cdot b$ (cosine of angle)



                      Then the rotation matrix R is given by:
                      $$R = I + [v]_times + [v]_times^2frac1-cs^2,$$



                      where $[v]_times$ is the skew-symmetric cross-product matrix of $v$,
                      $$[v]_times stackrelrm def= beginbmatrix
                      ,,0 & !-v_3 & ,,,v_2\
                      ,,,v_3 & 0 & !-v_1\
                      !-v_2 & ,,v_1 &,,0
                      endbmatrix.$$



                      The last part of the formula can be simplified to
                      $$
                      frac1-cs^2 = frac1-c1-c^2 = frac11+c,
                      $$
                      revealing that it is not applicable only for $cos(angle(a, b)) = -1$, i.e., if $a$ and $b$ point into exactly opposite directions.






                      share|cite|improve this answer


















                      • 3




                        I confirm that this works and gives answers identical to those in my answer.
                        – Kuba Ober
                        Aug 14 '14 at 22:46










                      • I tried to implement this, however I am getting an issue when the cross products is 0.
                        – user2970916
                        Jan 14 '15 at 14:40










                      • cross product can't be 0 because a and b are unit vectors
                        – Julien__
                        May 23 '15 at 22:45






                      • 2




                        @Julien__ what if a == b?
                        – Jerem
                        May 30 '15 at 12:22






                      • 2




                        Indeed... I forgot that case. In that case v = 0 and the formula gives R = I. So here is a better answer : when the cross product is 0, R = I
                        – Julien__
                        May 31 '15 at 13:08













                      up vote
                      73
                      down vote



                      accepted







                      up vote
                      73
                      down vote



                      accepted






                      Suppose you want to find a rotation matrix $R$ that rotates unit vector $a$ onto unit vector $b$.



                      Proceed as follows:



                      Let $v = a times b$



                      Let $s = |v|$ (sine of angle)



                      Let $c = a cdot b$ (cosine of angle)



                      Then the rotation matrix R is given by:
                      $$R = I + [v]_times + [v]_times^2frac1-cs^2,$$



                      where $[v]_times$ is the skew-symmetric cross-product matrix of $v$,
                      $$[v]_times stackrelrm def= beginbmatrix
                      ,,0 & !-v_3 & ,,,v_2\
                      ,,,v_3 & 0 & !-v_1\
                      !-v_2 & ,,v_1 &,,0
                      endbmatrix.$$



                      The last part of the formula can be simplified to
                      $$
                      frac1-cs^2 = frac1-c1-c^2 = frac11+c,
                      $$
                      revealing that it is not applicable only for $cos(angle(a, b)) = -1$, i.e., if $a$ and $b$ point into exactly opposite directions.






                      share|cite|improve this answer














                      Suppose you want to find a rotation matrix $R$ that rotates unit vector $a$ onto unit vector $b$.



                      Proceed as follows:



                      Let $v = a times b$



                      Let $s = |v|$ (sine of angle)



                      Let $c = a cdot b$ (cosine of angle)



                      Then the rotation matrix R is given by:
                      $$R = I + [v]_times + [v]_times^2frac1-cs^2,$$



                      where $[v]_times$ is the skew-symmetric cross-product matrix of $v$,
                      $$[v]_times stackrelrm def= beginbmatrix
                      ,,0 & !-v_3 & ,,,v_2\
                      ,,,v_3 & 0 & !-v_1\
                      !-v_2 & ,,v_1 &,,0
                      endbmatrix.$$



                      The last part of the formula can be simplified to
                      $$
                      frac1-cs^2 = frac1-c1-c^2 = frac11+c,
                      $$
                      revealing that it is not applicable only for $cos(angle(a, b)) = -1$, i.e., if $a$ and $b$ point into exactly opposite directions.







                      share|cite|improve this answer














                      share|cite|improve this answer



                      share|cite|improve this answer








                      edited Sep 1 '16 at 18:40









                      Nico Schlömer

                      398113




                      398113










                      answered Aug 26 '13 at 5:56









                      Jur van den Berg

                      74662




                      74662







                      • 3




                        I confirm that this works and gives answers identical to those in my answer.
                        – Kuba Ober
                        Aug 14 '14 at 22:46










                      • I tried to implement this, however I am getting an issue when the cross products is 0.
                        – user2970916
                        Jan 14 '15 at 14:40










                      • cross product can't be 0 because a and b are unit vectors
                        – Julien__
                        May 23 '15 at 22:45






                      • 2




                        @Julien__ what if a == b?
                        – Jerem
                        May 30 '15 at 12:22






                      • 2




                        Indeed... I forgot that case. In that case v = 0 and the formula gives R = I. So here is a better answer : when the cross product is 0, R = I
                        – Julien__
                        May 31 '15 at 13:08













                      • 3




                        I confirm that this works and gives answers identical to those in my answer.
                        – Kuba Ober
                        Aug 14 '14 at 22:46










                      • I tried to implement this, however I am getting an issue when the cross products is 0.
                        – user2970916
                        Jan 14 '15 at 14:40










                      • cross product can't be 0 because a and b are unit vectors
                        – Julien__
                        May 23 '15 at 22:45






                      • 2




                        @Julien__ what if a == b?
                        – Jerem
                        May 30 '15 at 12:22






                      • 2




                        Indeed... I forgot that case. In that case v = 0 and the formula gives R = I. So here is a better answer : when the cross product is 0, R = I
                        – Julien__
                        May 31 '15 at 13:08








                      3




                      3




                      I confirm that this works and gives answers identical to those in my answer.
                      – Kuba Ober
                      Aug 14 '14 at 22:46




                      I confirm that this works and gives answers identical to those in my answer.
                      – Kuba Ober
                      Aug 14 '14 at 22:46












                      I tried to implement this, however I am getting an issue when the cross products is 0.
                      – user2970916
                      Jan 14 '15 at 14:40




                      I tried to implement this, however I am getting an issue when the cross products is 0.
                      – user2970916
                      Jan 14 '15 at 14:40












                      cross product can't be 0 because a and b are unit vectors
                      – Julien__
                      May 23 '15 at 22:45




                      cross product can't be 0 because a and b are unit vectors
                      – Julien__
                      May 23 '15 at 22:45




                      2




                      2




                      @Julien__ what if a == b?
                      – Jerem
                      May 30 '15 at 12:22




                      @Julien__ what if a == b?
                      – Jerem
                      May 30 '15 at 12:22




                      2




                      2




                      Indeed... I forgot that case. In that case v = 0 and the formula gives R = I. So here is a better answer : when the cross product is 0, R = I
                      – Julien__
                      May 31 '15 at 13:08





                      Indeed... I forgot that case. In that case v = 0 and the formula gives R = I. So here is a better answer : when the cross product is 0, R = I
                      – Julien__
                      May 31 '15 at 13:08











                      up vote
                      32
                      down vote













                      Using Kjetil's answer answer, with process91's comment, we arrive at the following procedure.



                      Derivation



                      We are given two unit column vectors, $A$ and $B$ ($|A|=1$ and $|B|=1$). The $|circ|$ denotes the L-2 norm of $circ$.



                      First, note that the rotation from $A$ to $B$ is just a 2D rotation on a plane with the normal $A times B$. A 2D rotation by an angle $theta$ is given by the following augmented matrix:
                      $$G=beginpmatrix
                      costheta & -sintheta & 0 \
                      sintheta & costheta & 0 \
                      0 & 0 & 1
                      endpmatrix.$$



                      Of course we don't want to actually compute any trig functions. Given our unit vectors, we note that $costheta=Acdot B$, and $sintheta=||Atimes B||$. Thus
                      $$G=beginpmatrix
                      Acdot B & -|Atimes B| & 0 \
                      |Atimes B| & Acdot B & 0 \
                      0 & 0 & 1endpmatrix.$$



                      This matrix represents the rotation from $A$ to $B$ in the base consisting of the following column vectors:



                      1. normalized vector projection of $B$ onto $A$: $$u==A$$


                      2. normalized vector rejection of $B$ onto $A$: $$v=$$


                      3. the cross product of $B$ and $A$: $$w=B times A$$


                      Those vectors are all orthogonal and normal, and form an orthonormal basis. This is the detail that Kjetil had missed in his answer.



                      The basis change matrix for this basis is:
                      $$F=beginpmatrixu & v & w endpmatrix^-1=beginpmatrix A & & B times Aendpmatrix^-1$$



                      Thus, in the original base, the rotation from $A$ to $B$ can be expressed as right-multiplication of a vector by the following matrix: $$U=F^-1G F.$$



                      One can easily show that $U A = B$, and that $|U|_2=1$. Also, $U$ is the same as the $R$ matrix from Rik's answer.



                      2D Case



                      For the 2D case, given $A=left(x_1,y_1,0right)$ and $B=left(x_2,y_2,0right)$, the matrix $G$ is the forward transformation matrix itself, and we can simplify it further. We note
                      $$beginaligned
                      costheta &= Acdot B = x_1x_2+y_1y_2 \
                      sintheta &= | Atimes B| = x_1y_2-x_2y_1
                      endaligned$$



                      Finally,
                      $$Uequiv G=beginpmatrix
                      x_1x_2+y_1y_2 & -(x_1y_2-x_2y_1) \
                      x_1y_2-x_2y_1 & x_1x_2+y_1y_2
                      endpmatrix$$
                      and
                      $$U^-1equiv G^-1=beginpmatrix
                      x_1x_2+y_1y_2 & x_1y_2-x_2y_1 \
                      -(x_1y_2-x_2y_1) & x_1x_2+y_1y_2
                      endpmatrix$$



                      Octave/Matlab Implementation



                      The basic implementation is very simple. You could improve it by factoring out the common expressions of dot(A,B) and cross(B,A). Also note that $||Atimes B||=||Btimes A||$.



                      GG = @(A,B) [ dot(A,B) -norm(cross(A,B)) 0;
                      norm(cross(A,B)) dot(A,B) 0;
                      0 0 1];

                      FFi = @(A,B) [ A (B-dot(A,B)*A)/norm(B-dot(A,B)*A) cross(B,A) ];

                      UU = @(Fi,G) Fi*G*inv(Fi);


                      Testing:



                      > a=[1 0 0]'; b=[0 1 0]';
                      > U = UU(FFi(a,b), GG(a,b));
                      > norm(U) % is it length-preserving?
                      ans = 1
                      > norm(b-U*a) % does it rotate a onto b?
                      ans = 0
                      > U
                      U =

                      0 -1 0
                      1 0 0
                      0 0 1


                      Now with random vectors:



                      > vu = @(v) v/norm(v);
                      > ru = @() vu(rand(3,1));
                      > a = ru()
                      a =

                      0.043477
                      0.036412
                      0.998391
                      > b = ru()
                      b =

                      0.60958
                      0.73540
                      0.29597
                      > U = UU(FFi(a,b), GG(a,b));
                      > norm(U)
                      ans = 1
                      > norm(b-U*a)
                      ans = 2.2888e-16
                      > U
                      U =

                      0.73680 -0.32931 0.59049
                      -0.30976 0.61190 0.72776
                      -0.60098 -0.71912 0.34884


                      Implementation of Rik's Answer



                      It is computationally a bit more efficient to use Rik's answer. This is also an Octave/MatLab implementation.



                      ssc = @(v) [0 -v(3) v(2); v(3) 0 -v(1); -v(2) v(1) 0]
                      RU = @(A,B) eye(3) + ssc(cross(A,B)) +
                      ssc(cross(A,B))^2*(1-dot(A,B))/(norm(cross(A,B))^2)


                      The results produced are same as above, with slightly smaller numerical errors since there are less operations being done.






                      share|cite|improve this answer






















                      • Matlab notation for Rik's Implementation: function R=fcn_RotationFromTwoVectors(A, B) v = cross(A,B); ssc = [0 -v(3) v(2); v(3) 0 -v(1); -v(2) v(1) 0]; R = eye(3) + ssc + ssc^2*(1-dot(A,B))/(norm(v))^2;
                        – phyatt
                        Aug 20 '15 at 21:55











                      • @phyatt Doesn't the lambda syntax work in Matlab, too?
                        – Kuba Ober
                        Aug 20 '15 at 22:16










                      • I've used lamdas in other languages, and I haven't seen it in use in the Matlab projects... and sure enough, its there. mathworks.com/help/matlab/matlab_prog/anonymous-functions.html Thanks @KuberOber.
                        – phyatt
                        Aug 20 '15 at 22:21










                      • Rik's answer doesn't work if a == b, does your answer work in this case?
                        – Ruts
                        Nov 10 '15 at 8:53










                      • @Ruts It can't work, since you can't normalize a zero-length vector a-dot(a,a)*a. You have to check for a==b first :)
                        – Kuba Ober
                        Nov 10 '15 at 17:06















                      up vote
                      32
                      down vote













                      Using Kjetil's answer answer, with process91's comment, we arrive at the following procedure.



                      Derivation



                      We are given two unit column vectors, $A$ and $B$ ($|A|=1$ and $|B|=1$). The $|circ|$ denotes the L-2 norm of $circ$.



                      First, note that the rotation from $A$ to $B$ is just a 2D rotation on a plane with the normal $A times B$. A 2D rotation by an angle $theta$ is given by the following augmented matrix:
                      $$G=beginpmatrix
                      costheta & -sintheta & 0 \
                      sintheta & costheta & 0 \
                      0 & 0 & 1
                      endpmatrix.$$



                      Of course we don't want to actually compute any trig functions. Given our unit vectors, we note that $costheta=Acdot B$, and $sintheta=||Atimes B||$. Thus
                      $$G=beginpmatrix
                      Acdot B & -|Atimes B| & 0 \
                      |Atimes B| & Acdot B & 0 \
                      0 & 0 & 1endpmatrix.$$



                      This matrix represents the rotation from $A$ to $B$ in the base consisting of the following column vectors:



                      1. normalized vector projection of $B$ onto $A$: $$u==A$$


                      2. normalized vector rejection of $B$ onto $A$: $$v=$$


                      3. the cross product of $B$ and $A$: $$w=B times A$$


                      Those vectors are all orthogonal and normal, and form an orthonormal basis. This is the detail that Kjetil had missed in his answer.



                      The basis change matrix for this basis is:
                      $$F=beginpmatrixu & v & w endpmatrix^-1=beginpmatrix A & & B times Aendpmatrix^-1$$



                      Thus, in the original base, the rotation from $A$ to $B$ can be expressed as right-multiplication of a vector by the following matrix: $$U=F^-1G F.$$



                      One can easily show that $U A = B$, and that $|U|_2=1$. Also, $U$ is the same as the $R$ matrix from Rik's answer.



                      2D Case



                      For the 2D case, given $A=left(x_1,y_1,0right)$ and $B=left(x_2,y_2,0right)$, the matrix $G$ is the forward transformation matrix itself, and we can simplify it further. We note
                      $$beginaligned
                      costheta &= Acdot B = x_1x_2+y_1y_2 \
                      sintheta &= | Atimes B| = x_1y_2-x_2y_1
                      endaligned$$



                      Finally,
                      $$Uequiv G=beginpmatrix
                      x_1x_2+y_1y_2 & -(x_1y_2-x_2y_1) \
                      x_1y_2-x_2y_1 & x_1x_2+y_1y_2
                      endpmatrix$$
                      and
                      $$U^-1equiv G^-1=beginpmatrix
                      x_1x_2+y_1y_2 & x_1y_2-x_2y_1 \
                      -(x_1y_2-x_2y_1) & x_1x_2+y_1y_2
                      endpmatrix$$



                      Octave/Matlab Implementation



                      The basic implementation is very simple. You could improve it by factoring out the common expressions of dot(A,B) and cross(B,A). Also note that $||Atimes B||=||Btimes A||$.



                      GG = @(A,B) [ dot(A,B) -norm(cross(A,B)) 0;
                      norm(cross(A,B)) dot(A,B) 0;
                      0 0 1];

                      FFi = @(A,B) [ A (B-dot(A,B)*A)/norm(B-dot(A,B)*A) cross(B,A) ];

                      UU = @(Fi,G) Fi*G*inv(Fi);


                      Testing:



                      > a=[1 0 0]'; b=[0 1 0]';
                      > U = UU(FFi(a,b), GG(a,b));
                      > norm(U) % is it length-preserving?
                      ans = 1
                      > norm(b-U*a) % does it rotate a onto b?
                      ans = 0
                      > U
                      U =

                      0 -1 0
                      1 0 0
                      0 0 1


                      Now with random vectors:



                      > vu = @(v) v/norm(v);
                      > ru = @() vu(rand(3,1));
                      > a = ru()
                      a =

                      0.043477
                      0.036412
                      0.998391
                      > b = ru()
                      b =

                      0.60958
                      0.73540
                      0.29597
                      > U = UU(FFi(a,b), GG(a,b));
                      > norm(U)
                      ans = 1
                      > norm(b-U*a)
                      ans = 2.2888e-16
                      > U
                      U =

                      0.73680 -0.32931 0.59049
                      -0.30976 0.61190 0.72776
                      -0.60098 -0.71912 0.34884


                      Implementation of Rik's Answer



                      It is computationally a bit more efficient to use Rik's answer. This is also an Octave/MatLab implementation.



                      ssc = @(v) [0 -v(3) v(2); v(3) 0 -v(1); -v(2) v(1) 0]
                      RU = @(A,B) eye(3) + ssc(cross(A,B)) +
                      ssc(cross(A,B))^2*(1-dot(A,B))/(norm(cross(A,B))^2)


                      The results produced are same as above, with slightly smaller numerical errors since there are less operations being done.






                      share|cite|improve this answer






















                      • Matlab notation for Rik's Implementation: function R=fcn_RotationFromTwoVectors(A, B) v = cross(A,B); ssc = [0 -v(3) v(2); v(3) 0 -v(1); -v(2) v(1) 0]; R = eye(3) + ssc + ssc^2*(1-dot(A,B))/(norm(v))^2;
                        – phyatt
                        Aug 20 '15 at 21:55











                      • @phyatt Doesn't the lambda syntax work in Matlab, too?
                        – Kuba Ober
                        Aug 20 '15 at 22:16










                      • I've used lamdas in other languages, and I haven't seen it in use in the Matlab projects... and sure enough, its there. mathworks.com/help/matlab/matlab_prog/anonymous-functions.html Thanks @KuberOber.
                        – phyatt
                        Aug 20 '15 at 22:21










                      • Rik's answer doesn't work if a == b, does your answer work in this case?
                        – Ruts
                        Nov 10 '15 at 8:53










                      • @Ruts It can't work, since you can't normalize a zero-length vector a-dot(a,a)*a. You have to check for a==b first :)
                        – Kuba Ober
                        Nov 10 '15 at 17:06













                      up vote
                      32
                      down vote










                      up vote
                      32
                      down vote









                      Using Kjetil's answer answer, with process91's comment, we arrive at the following procedure.



                      Derivation



                      We are given two unit column vectors, $A$ and $B$ ($|A|=1$ and $|B|=1$). The $|circ|$ denotes the L-2 norm of $circ$.



                      First, note that the rotation from $A$ to $B$ is just a 2D rotation on a plane with the normal $A times B$. A 2D rotation by an angle $theta$ is given by the following augmented matrix:
                      $$G=beginpmatrix
                      costheta & -sintheta & 0 \
                      sintheta & costheta & 0 \
                      0 & 0 & 1
                      endpmatrix.$$



                      Of course we don't want to actually compute any trig functions. Given our unit vectors, we note that $costheta=Acdot B$, and $sintheta=||Atimes B||$. Thus
                      $$G=beginpmatrix
                      Acdot B & -|Atimes B| & 0 \
                      |Atimes B| & Acdot B & 0 \
                      0 & 0 & 1endpmatrix.$$



                      This matrix represents the rotation from $A$ to $B$ in the base consisting of the following column vectors:



                      1. normalized vector projection of $B$ onto $A$: $$u==A$$


                      2. normalized vector rejection of $B$ onto $A$: $$v=$$


                      3. the cross product of $B$ and $A$: $$w=B times A$$


                      Those vectors are all orthogonal and normal, and form an orthonormal basis. This is the detail that Kjetil had missed in his answer.



                      The basis change matrix for this basis is:
                      $$F=beginpmatrixu & v & w endpmatrix^-1=beginpmatrix A & & B times Aendpmatrix^-1$$



                      Thus, in the original base, the rotation from $A$ to $B$ can be expressed as right-multiplication of a vector by the following matrix: $$U=F^-1G F.$$



                      One can easily show that $U A = B$, and that $|U|_2=1$. Also, $U$ is the same as the $R$ matrix from Rik's answer.



                      2D Case



                      For the 2D case, given $A=left(x_1,y_1,0right)$ and $B=left(x_2,y_2,0right)$, the matrix $G$ is the forward transformation matrix itself, and we can simplify it further. We note
                      $$beginaligned
                      costheta &= Acdot B = x_1x_2+y_1y_2 \
                      sintheta &= | Atimes B| = x_1y_2-x_2y_1
                      endaligned$$



                      Finally,
                      $$Uequiv G=beginpmatrix
                      x_1x_2+y_1y_2 & -(x_1y_2-x_2y_1) \
                      x_1y_2-x_2y_1 & x_1x_2+y_1y_2
                      endpmatrix$$
                      and
                      $$U^-1equiv G^-1=beginpmatrix
                      x_1x_2+y_1y_2 & x_1y_2-x_2y_1 \
                      -(x_1y_2-x_2y_1) & x_1x_2+y_1y_2
                      endpmatrix$$



                      Octave/Matlab Implementation



                      The basic implementation is very simple. You could improve it by factoring out the common expressions of dot(A,B) and cross(B,A). Also note that $||Atimes B||=||Btimes A||$.



                      GG = @(A,B) [ dot(A,B) -norm(cross(A,B)) 0;
                      norm(cross(A,B)) dot(A,B) 0;
                      0 0 1];

                      FFi = @(A,B) [ A (B-dot(A,B)*A)/norm(B-dot(A,B)*A) cross(B,A) ];

                      UU = @(Fi,G) Fi*G*inv(Fi);


                      Testing:



                      > a=[1 0 0]'; b=[0 1 0]';
                      > U = UU(FFi(a,b), GG(a,b));
                      > norm(U) % is it length-preserving?
                      ans = 1
                      > norm(b-U*a) % does it rotate a onto b?
                      ans = 0
                      > U
                      U =

                      0 -1 0
                      1 0 0
                      0 0 1


                      Now with random vectors:



                      > vu = @(v) v/norm(v);
                      > ru = @() vu(rand(3,1));
                      > a = ru()
                      a =

                      0.043477
                      0.036412
                      0.998391
                      > b = ru()
                      b =

                      0.60958
                      0.73540
                      0.29597
                      > U = UU(FFi(a,b), GG(a,b));
                      > norm(U)
                      ans = 1
                      > norm(b-U*a)
                      ans = 2.2888e-16
                      > U
                      U =

                      0.73680 -0.32931 0.59049
                      -0.30976 0.61190 0.72776
                      -0.60098 -0.71912 0.34884


                      Implementation of Rik's Answer



                      It is computationally a bit more efficient to use Rik's answer. This is also an Octave/MatLab implementation.



                      ssc = @(v) [0 -v(3) v(2); v(3) 0 -v(1); -v(2) v(1) 0]
                      RU = @(A,B) eye(3) + ssc(cross(A,B)) +
                      ssc(cross(A,B))^2*(1-dot(A,B))/(norm(cross(A,B))^2)


                      The results produced are same as above, with slightly smaller numerical errors since there are less operations being done.






                      share|cite|improve this answer














                      Using Kjetil's answer answer, with process91's comment, we arrive at the following procedure.



                      Derivation



                      We are given two unit column vectors, $A$ and $B$ ($|A|=1$ and $|B|=1$). The $|circ|$ denotes the L-2 norm of $circ$.



                      First, note that the rotation from $A$ to $B$ is just a 2D rotation on a plane with the normal $A times B$. A 2D rotation by an angle $theta$ is given by the following augmented matrix:
                      $$G=beginpmatrix
                      costheta & -sintheta & 0 \
                      sintheta & costheta & 0 \
                      0 & 0 & 1
                      endpmatrix.$$



                      Of course we don't want to actually compute any trig functions. Given our unit vectors, we note that $costheta=Acdot B$, and $sintheta=||Atimes B||$. Thus
                      $$G=beginpmatrix
                      Acdot B & -|Atimes B| & 0 \
                      |Atimes B| & Acdot B & 0 \
                      0 & 0 & 1endpmatrix.$$



                      This matrix represents the rotation from $A$ to $B$ in the base consisting of the following column vectors:



                      1. normalized vector projection of $B$ onto $A$: $$u==A$$


                      2. normalized vector rejection of $B$ onto $A$: $$v=$$


                      3. the cross product of $B$ and $A$: $$w=B times A$$


                      Those vectors are all orthogonal and normal, and form an orthonormal basis. This is the detail that Kjetil had missed in his answer.



                      The basis change matrix for this basis is:
                      $$F=beginpmatrixu & v & w endpmatrix^-1=beginpmatrix A & & B times Aendpmatrix^-1$$



                      Thus, in the original base, the rotation from $A$ to $B$ can be expressed as right-multiplication of a vector by the following matrix: $$U=F^-1G F.$$



                      One can easily show that $U A = B$, and that $|U|_2=1$. Also, $U$ is the same as the $R$ matrix from Rik's answer.



                      2D Case



                      For the 2D case, given $A=left(x_1,y_1,0right)$ and $B=left(x_2,y_2,0right)$, the matrix $G$ is the forward transformation matrix itself, and we can simplify it further. We note
                      $$beginaligned
                      costheta &= Acdot B = x_1x_2+y_1y_2 \
                      sintheta &= | Atimes B| = x_1y_2-x_2y_1
                      endaligned$$



                      Finally,
                      $$Uequiv G=beginpmatrix
                      x_1x_2+y_1y_2 & -(x_1y_2-x_2y_1) \
                      x_1y_2-x_2y_1 & x_1x_2+y_1y_2
                      endpmatrix$$
                      and
                      $$U^-1equiv G^-1=beginpmatrix
                      x_1x_2+y_1y_2 & x_1y_2-x_2y_1 \
                      -(x_1y_2-x_2y_1) & x_1x_2+y_1y_2
                      endpmatrix$$



                      Octave/Matlab Implementation



                      The basic implementation is very simple. You could improve it by factoring out the common expressions of dot(A,B) and cross(B,A). Also note that $||Atimes B||=||Btimes A||$.



                      GG = @(A,B) [ dot(A,B) -norm(cross(A,B)) 0;
                      norm(cross(A,B)) dot(A,B) 0;
                      0 0 1];

                      FFi = @(A,B) [ A (B-dot(A,B)*A)/norm(B-dot(A,B)*A) cross(B,A) ];

                      UU = @(Fi,G) Fi*G*inv(Fi);


                      Testing:



                      > a=[1 0 0]'; b=[0 1 0]';
                      > U = UU(FFi(a,b), GG(a,b));
                      > norm(U) % is it length-preserving?
                      ans = 1
                      > norm(b-U*a) % does it rotate a onto b?
                      ans = 0
                      > U
                      U =

                      0 -1 0
                      1 0 0
                      0 0 1


                      Now with random vectors:



                      > vu = @(v) v/norm(v);
                      > ru = @() vu(rand(3,1));
                      > a = ru()
                      a =

                      0.043477
                      0.036412
                      0.998391
                      > b = ru()
                      b =

                      0.60958
                      0.73540
                      0.29597
                      > U = UU(FFi(a,b), GG(a,b));
                      > norm(U)
                      ans = 1
                      > norm(b-U*a)
                      ans = 2.2888e-16
                      > U
                      U =

                      0.73680 -0.32931 0.59049
                      -0.30976 0.61190 0.72776
                      -0.60098 -0.71912 0.34884


                      Implementation of Rik's Answer



                      It is computationally a bit more efficient to use Rik's answer. This is also an Octave/MatLab implementation.



                      ssc = @(v) [0 -v(3) v(2); v(3) 0 -v(1); -v(2) v(1) 0]
                      RU = @(A,B) eye(3) + ssc(cross(A,B)) +
                      ssc(cross(A,B))^2*(1-dot(A,B))/(norm(cross(A,B))^2)


                      The results produced are same as above, with slightly smaller numerical errors since there are less operations being done.







                      share|cite|improve this answer














                      share|cite|improve this answer



                      share|cite|improve this answer








                      edited Apr 13 '17 at 12:21









                      Community♦

                      1




                      1










                      answered Aug 14 '14 at 21:13









                      Kuba Ober

                      466413




                      466413











                      • Matlab notation for Rik's Implementation: function R=fcn_RotationFromTwoVectors(A, B) v = cross(A,B); ssc = [0 -v(3) v(2); v(3) 0 -v(1); -v(2) v(1) 0]; R = eye(3) + ssc + ssc^2*(1-dot(A,B))/(norm(v))^2;
                        – phyatt
                        Aug 20 '15 at 21:55











                      • @phyatt Doesn't the lambda syntax work in Matlab, too?
                        – Kuba Ober
                        Aug 20 '15 at 22:16










                      • I've used lamdas in other languages, and I haven't seen it in use in the Matlab projects... and sure enough, its there. mathworks.com/help/matlab/matlab_prog/anonymous-functions.html Thanks @KuberOber.
                        – phyatt
                        Aug 20 '15 at 22:21










                      • Rik's answer doesn't work if a == b, does your answer work in this case?
                        – Ruts
                        Nov 10 '15 at 8:53










                      • @Ruts It can't work, since you can't normalize a zero-length vector a-dot(a,a)*a. You have to check for a==b first :)
                        – Kuba Ober
                        Nov 10 '15 at 17:06

















                      • Matlab notation for Rik's Implementation: function R=fcn_RotationFromTwoVectors(A, B) v = cross(A,B); ssc = [0 -v(3) v(2); v(3) 0 -v(1); -v(2) v(1) 0]; R = eye(3) + ssc + ssc^2*(1-dot(A,B))/(norm(v))^2;
                        – phyatt
                        Aug 20 '15 at 21:55











                      • @phyatt Doesn't the lambda syntax work in Matlab, too?
                        – Kuba Ober
                        Aug 20 '15 at 22:16










                      • I've used lamdas in other languages, and I haven't seen it in use in the Matlab projects... and sure enough, its there. mathworks.com/help/matlab/matlab_prog/anonymous-functions.html Thanks @KuberOber.
                        – phyatt
                        Aug 20 '15 at 22:21










                      • Rik's answer doesn't work if a == b, does your answer work in this case?
                        – Ruts
                        Nov 10 '15 at 8:53










                      • @Ruts It can't work, since you can't normalize a zero-length vector a-dot(a,a)*a. You have to check for a==b first :)
                        – Kuba Ober
                        Nov 10 '15 at 17:06
















                      Matlab notation for Rik's Implementation: function R=fcn_RotationFromTwoVectors(A, B) v = cross(A,B); ssc = [0 -v(3) v(2); v(3) 0 -v(1); -v(2) v(1) 0]; R = eye(3) + ssc + ssc^2*(1-dot(A,B))/(norm(v))^2;
                      – phyatt
                      Aug 20 '15 at 21:55





                      Matlab notation for Rik's Implementation: function R=fcn_RotationFromTwoVectors(A, B) v = cross(A,B); ssc = [0 -v(3) v(2); v(3) 0 -v(1); -v(2) v(1) 0]; R = eye(3) + ssc + ssc^2*(1-dot(A,B))/(norm(v))^2;
                      – phyatt
                      Aug 20 '15 at 21:55













                      @phyatt Doesn't the lambda syntax work in Matlab, too?
                      – Kuba Ober
                      Aug 20 '15 at 22:16




                      @phyatt Doesn't the lambda syntax work in Matlab, too?
                      – Kuba Ober
                      Aug 20 '15 at 22:16












                      I've used lamdas in other languages, and I haven't seen it in use in the Matlab projects... and sure enough, its there. mathworks.com/help/matlab/matlab_prog/anonymous-functions.html Thanks @KuberOber.
                      – phyatt
                      Aug 20 '15 at 22:21




                      I've used lamdas in other languages, and I haven't seen it in use in the Matlab projects... and sure enough, its there. mathworks.com/help/matlab/matlab_prog/anonymous-functions.html Thanks @KuberOber.
                      – phyatt
                      Aug 20 '15 at 22:21












                      Rik's answer doesn't work if a == b, does your answer work in this case?
                      – Ruts
                      Nov 10 '15 at 8:53




                      Rik's answer doesn't work if a == b, does your answer work in this case?
                      – Ruts
                      Nov 10 '15 at 8:53












                      @Ruts It can't work, since you can't normalize a zero-length vector a-dot(a,a)*a. You have to check for a==b first :)
                      – Kuba Ober
                      Nov 10 '15 at 17:06





                      @Ruts It can't work, since you can't normalize a zero-length vector a-dot(a,a)*a. You have to check for a==b first :)
                      – Kuba Ober
                      Nov 10 '15 at 17:06











                      up vote
                      6
                      down vote













                      At the top of my head (do the checking yourself)
                      Let the given vectors in $R^3$ be $A$ and $B$, for simplicity assume they have norm 1, and assume they are not identical. Define $C$ as the cross product of $A$ and $B$. We want an orthogonal matrix $U$ such that $UA=B$ and $UC=C$.
                      First change bases, to the new base $(U_1,u_2,u_3)=(A,B,C)$. In this new basis the matrix doing the job is simply $G=left(beginsmallmatrix 0&1&0\1&0&0\0&0&1endsmallmatrixright)$. Then we need the basis shift matrix, to the new basis. Write the coordinates of the vectors in the old base as simply $A=(a_1,a_2,a_3), B=(b_1,b_2,b_3), C=(c_1,c_2,c_3)$. Then the basis shift matrix can be seen to be $left( beginsmallmatrix a_1&b_1&c_1\a_2&b_2&c_2\a_3&b_3&c_3 endsmallmatrixright)^-1$. The result is now simply $U=F^-1 G F$, which is an orthogonal matrix rotating $A$ into $B$.






                      share|cite|improve this answer




















                      • Actually, I think there may be further trouble - your matrix $U$ is only guaranteed to be orthogonal if $F$ is, which is only the case if $A$ and $B$ are orthogonal. In other instances, the angles of various other vectors to which $U$ is applied may be stretched or compressed.
                        – process91
                        Aug 9 '12 at 19:21










                      • Your thought process is a good one, though - don't give up on it yet! I think there is a solution this way if you consider different original basis vectors which still have an easily represented form of $A$ and $B$. You could, for instance, choose the projection of $A$ on $B$, the rejection of $A$ on $B$, and $A times B$.
                        – process91
                        Aug 9 '12 at 19:50







                      • 2




                        Just so that people aren't derailed: this answer doesn't work as-is, one has to take process91's comments into account. +1 since it provided valuable inspiration.
                        – Kuba Ober
                        Aug 14 '14 at 22:26















                      up vote
                      6
                      down vote













                      At the top of my head (do the checking yourself)
                      Let the given vectors in $R^3$ be $A$ and $B$, for simplicity assume they have norm 1, and assume they are not identical. Define $C$ as the cross product of $A$ and $B$. We want an orthogonal matrix $U$ such that $UA=B$ and $UC=C$.
                      First change bases, to the new base $(U_1,u_2,u_3)=(A,B,C)$. In this new basis the matrix doing the job is simply $G=left(beginsmallmatrix 0&1&0\1&0&0\0&0&1endsmallmatrixright)$. Then we need the basis shift matrix, to the new basis. Write the coordinates of the vectors in the old base as simply $A=(a_1,a_2,a_3), B=(b_1,b_2,b_3), C=(c_1,c_2,c_3)$. Then the basis shift matrix can be seen to be $left( beginsmallmatrix a_1&b_1&c_1\a_2&b_2&c_2\a_3&b_3&c_3 endsmallmatrixright)^-1$. The result is now simply $U=F^-1 G F$, which is an orthogonal matrix rotating $A$ into $B$.






                      share|cite|improve this answer




















                      • Actually, I think there may be further trouble - your matrix $U$ is only guaranteed to be orthogonal if $F$ is, which is only the case if $A$ and $B$ are orthogonal. In other instances, the angles of various other vectors to which $U$ is applied may be stretched or compressed.
                        – process91
                        Aug 9 '12 at 19:21










                      • Your thought process is a good one, though - don't give up on it yet! I think there is a solution this way if you consider different original basis vectors which still have an easily represented form of $A$ and $B$. You could, for instance, choose the projection of $A$ on $B$, the rejection of $A$ on $B$, and $A times B$.
                        – process91
                        Aug 9 '12 at 19:50







                      • 2




                        Just so that people aren't derailed: this answer doesn't work as-is, one has to take process91's comments into account. +1 since it provided valuable inspiration.
                        – Kuba Ober
                        Aug 14 '14 at 22:26













                      up vote
                      6
                      down vote










                      up vote
                      6
                      down vote









                      At the top of my head (do the checking yourself)
                      Let the given vectors in $R^3$ be $A$ and $B$, for simplicity assume they have norm 1, and assume they are not identical. Define $C$ as the cross product of $A$ and $B$. We want an orthogonal matrix $U$ such that $UA=B$ and $UC=C$.
                      First change bases, to the new base $(U_1,u_2,u_3)=(A,B,C)$. In this new basis the matrix doing the job is simply $G=left(beginsmallmatrix 0&1&0\1&0&0\0&0&1endsmallmatrixright)$. Then we need the basis shift matrix, to the new basis. Write the coordinates of the vectors in the old base as simply $A=(a_1,a_2,a_3), B=(b_1,b_2,b_3), C=(c_1,c_2,c_3)$. Then the basis shift matrix can be seen to be $left( beginsmallmatrix a_1&b_1&c_1\a_2&b_2&c_2\a_3&b_3&c_3 endsmallmatrixright)^-1$. The result is now simply $U=F^-1 G F$, which is an orthogonal matrix rotating $A$ into $B$.






                      share|cite|improve this answer












                      At the top of my head (do the checking yourself)
                      Let the given vectors in $R^3$ be $A$ and $B$, for simplicity assume they have norm 1, and assume they are not identical. Define $C$ as the cross product of $A$ and $B$. We want an orthogonal matrix $U$ such that $UA=B$ and $UC=C$.
                      First change bases, to the new base $(U_1,u_2,u_3)=(A,B,C)$. In this new basis the matrix doing the job is simply $G=left(beginsmallmatrix 0&1&0\1&0&0\0&0&1endsmallmatrixright)$. Then we need the basis shift matrix, to the new basis. Write the coordinates of the vectors in the old base as simply $A=(a_1,a_2,a_3), B=(b_1,b_2,b_3), C=(c_1,c_2,c_3)$. Then the basis shift matrix can be seen to be $left( beginsmallmatrix a_1&b_1&c_1\a_2&b_2&c_2\a_3&b_3&c_3 endsmallmatrixright)^-1$. The result is now simply $U=F^-1 G F$, which is an orthogonal matrix rotating $A$ into $B$.







                      share|cite|improve this answer












                      share|cite|improve this answer



                      share|cite|improve this answer










                      answered Aug 8 '12 at 22:04









                      kjetil b halvorsen

                      4,53942636




                      4,53942636











                      • Actually, I think there may be further trouble - your matrix $U$ is only guaranteed to be orthogonal if $F$ is, which is only the case if $A$ and $B$ are orthogonal. In other instances, the angles of various other vectors to which $U$ is applied may be stretched or compressed.
                        – process91
                        Aug 9 '12 at 19:21










                      • Your thought process is a good one, though - don't give up on it yet! I think there is a solution this way if you consider different original basis vectors which still have an easily represented form of $A$ and $B$. You could, for instance, choose the projection of $A$ on $B$, the rejection of $A$ on $B$, and $A times B$.
                        – process91
                        Aug 9 '12 at 19:50







                      • 2




                        Just so that people aren't derailed: this answer doesn't work as-is, one has to take process91's comments into account. +1 since it provided valuable inspiration.
                        – Kuba Ober
                        Aug 14 '14 at 22:26

















                      • Actually, I think there may be further trouble - your matrix $U$ is only guaranteed to be orthogonal if $F$ is, which is only the case if $A$ and $B$ are orthogonal. In other instances, the angles of various other vectors to which $U$ is applied may be stretched or compressed.
                        – process91
                        Aug 9 '12 at 19:21










                      • Your thought process is a good one, though - don't give up on it yet! I think there is a solution this way if you consider different original basis vectors which still have an easily represented form of $A$ and $B$. You could, for instance, choose the projection of $A$ on $B$, the rejection of $A$ on $B$, and $A times B$.
                        – process91
                        Aug 9 '12 at 19:50







                      • 2




                        Just so that people aren't derailed: this answer doesn't work as-is, one has to take process91's comments into account. +1 since it provided valuable inspiration.
                        – Kuba Ober
                        Aug 14 '14 at 22:26
















                      Actually, I think there may be further trouble - your matrix $U$ is only guaranteed to be orthogonal if $F$ is, which is only the case if $A$ and $B$ are orthogonal. In other instances, the angles of various other vectors to which $U$ is applied may be stretched or compressed.
                      – process91
                      Aug 9 '12 at 19:21




                      Actually, I think there may be further trouble - your matrix $U$ is only guaranteed to be orthogonal if $F$ is, which is only the case if $A$ and $B$ are orthogonal. In other instances, the angles of various other vectors to which $U$ is applied may be stretched or compressed.
                      – process91
                      Aug 9 '12 at 19:21












                      Your thought process is a good one, though - don't give up on it yet! I think there is a solution this way if you consider different original basis vectors which still have an easily represented form of $A$ and $B$. You could, for instance, choose the projection of $A$ on $B$, the rejection of $A$ on $B$, and $A times B$.
                      – process91
                      Aug 9 '12 at 19:50





                      Your thought process is a good one, though - don't give up on it yet! I think there is a solution this way if you consider different original basis vectors which still have an easily represented form of $A$ and $B$. You could, for instance, choose the projection of $A$ on $B$, the rejection of $A$ on $B$, and $A times B$.
                      – process91
                      Aug 9 '12 at 19:50





                      2




                      2




                      Just so that people aren't derailed: this answer doesn't work as-is, one has to take process91's comments into account. +1 since it provided valuable inspiration.
                      – Kuba Ober
                      Aug 14 '14 at 22:26





                      Just so that people aren't derailed: this answer doesn't work as-is, one has to take process91's comments into account. +1 since it provided valuable inspiration.
                      – Kuba Ober
                      Aug 14 '14 at 22:26











                      up vote
                      4
                      down vote













                      The MATLAB code for any dimension greater than one is



                      u = a/norm(a); % a and b must be column vectors
                      v = b/norm(b); % of equal length
                      N = length(u);
                      S = reflection( eye(N), v+u ); % S*u = -v, S*v = -u
                      R = reflection( S, v ); % v = R*u


                      where



                      function v = reflection( u, n ) % Reflection of u on hyperplane n.
                      %
                      % u can be a matrix. u and v must have the same number of rows.

                      v = u - 2 * n * (n'*u) / (n'*n);
                      return


                      See this for background on how this works.






                      share|cite|improve this answer


























                        up vote
                        4
                        down vote













                        The MATLAB code for any dimension greater than one is



                        u = a/norm(a); % a and b must be column vectors
                        v = b/norm(b); % of equal length
                        N = length(u);
                        S = reflection( eye(N), v+u ); % S*u = -v, S*v = -u
                        R = reflection( S, v ); % v = R*u


                        where



                        function v = reflection( u, n ) % Reflection of u on hyperplane n.
                        %
                        % u can be a matrix. u and v must have the same number of rows.

                        v = u - 2 * n * (n'*u) / (n'*n);
                        return


                        See this for background on how this works.






                        share|cite|improve this answer
























                          up vote
                          4
                          down vote










                          up vote
                          4
                          down vote









                          The MATLAB code for any dimension greater than one is



                          u = a/norm(a); % a and b must be column vectors
                          v = b/norm(b); % of equal length
                          N = length(u);
                          S = reflection( eye(N), v+u ); % S*u = -v, S*v = -u
                          R = reflection( S, v ); % v = R*u


                          where



                          function v = reflection( u, n ) % Reflection of u on hyperplane n.
                          %
                          % u can be a matrix. u and v must have the same number of rows.

                          v = u - 2 * n * (n'*u) / (n'*n);
                          return


                          See this for background on how this works.






                          share|cite|improve this answer














                          The MATLAB code for any dimension greater than one is



                          u = a/norm(a); % a and b must be column vectors
                          v = b/norm(b); % of equal length
                          N = length(u);
                          S = reflection( eye(N), v+u ); % S*u = -v, S*v = -u
                          R = reflection( S, v ); % v = R*u


                          where



                          function v = reflection( u, n ) % Reflection of u on hyperplane n.
                          %
                          % u can be a matrix. u and v must have the same number of rows.

                          v = u - 2 * n * (n'*u) / (n'*n);
                          return


                          See this for background on how this works.







                          share|cite|improve this answer














                          share|cite|improve this answer



                          share|cite|improve this answer








                          edited Apr 13 '17 at 12:21









                          Community♦

                          1




                          1










                          answered Feb 26 '17 at 1:17









                          T L Davis

                          24616




                          24616




















                              up vote
                              3
                              down vote













                              The quaternion is a 4-dimensional complex number:
                              http://en.wikipedia.org/wiki/Quaternion
                              used to describe rotations in space. A quaternion (like a complex number) has a polar representation involving the exponential of the arguments (rotations), and a magnetude multiplier. Building the quaternion comes from the cross product (the product of the complex components), which will give you the argument in those 3 dimensions, you'll then get a number from that in the form A+Bi+Cj+Dk, and write it out in the matrix form described in the article there.



                              An easier way would be to simply fingure out what your original vectors are in the 4-space, and take the appropriate inverse operations to get your resultant quaternion (without going through the dot/cross product steps) but that requires a good foundation in hypercomplex algebra.






                              share|cite|improve this answer
















                              • 1




                                Just to be clear: from the article, the dot product will be your scalar part, and the cross product the vector part, so with dot product: x and scalar product iy + jz + ka the quaternion q would be q = x +iy +jz +ka.
                                – Vilid
                                Aug 8 '12 at 21:51











                              • Not exactly. If the dot product is a, and the cross product is (x, y, z), it would be cos(a/2) + (x * sin(a/2))i + (y * sin(a/2))j + ( z * sin(a/2))k. Rotation quaternions are unitary.
                                – Jeff
                                Jul 19 '13 at 2:33















                              up vote
                              3
                              down vote













                              The quaternion is a 4-dimensional complex number:
                              http://en.wikipedia.org/wiki/Quaternion
                              used to describe rotations in space. A quaternion (like a complex number) has a polar representation involving the exponential of the arguments (rotations), and a magnetude multiplier. Building the quaternion comes from the cross product (the product of the complex components), which will give you the argument in those 3 dimensions, you'll then get a number from that in the form A+Bi+Cj+Dk, and write it out in the matrix form described in the article there.



                              An easier way would be to simply fingure out what your original vectors are in the 4-space, and take the appropriate inverse operations to get your resultant quaternion (without going through the dot/cross product steps) but that requires a good foundation in hypercomplex algebra.






                              share|cite|improve this answer
















                              • 1




                                Just to be clear: from the article, the dot product will be your scalar part, and the cross product the vector part, so with dot product: x and scalar product iy + jz + ka the quaternion q would be q = x +iy +jz +ka.
                                – Vilid
                                Aug 8 '12 at 21:51











                              • Not exactly. If the dot product is a, and the cross product is (x, y, z), it would be cos(a/2) + (x * sin(a/2))i + (y * sin(a/2))j + ( z * sin(a/2))k. Rotation quaternions are unitary.
                                – Jeff
                                Jul 19 '13 at 2:33













                              up vote
                              3
                              down vote










                              up vote
                              3
                              down vote









                              The quaternion is a 4-dimensional complex number:
                              http://en.wikipedia.org/wiki/Quaternion
                              used to describe rotations in space. A quaternion (like a complex number) has a polar representation involving the exponential of the arguments (rotations), and a magnetude multiplier. Building the quaternion comes from the cross product (the product of the complex components), which will give you the argument in those 3 dimensions, you'll then get a number from that in the form A+Bi+Cj+Dk, and write it out in the matrix form described in the article there.



                              An easier way would be to simply fingure out what your original vectors are in the 4-space, and take the appropriate inverse operations to get your resultant quaternion (without going through the dot/cross product steps) but that requires a good foundation in hypercomplex algebra.






                              share|cite|improve this answer












                              The quaternion is a 4-dimensional complex number:
                              http://en.wikipedia.org/wiki/Quaternion
                              used to describe rotations in space. A quaternion (like a complex number) has a polar representation involving the exponential of the arguments (rotations), and a magnetude multiplier. Building the quaternion comes from the cross product (the product of the complex components), which will give you the argument in those 3 dimensions, you'll then get a number from that in the form A+Bi+Cj+Dk, and write it out in the matrix form described in the article there.



                              An easier way would be to simply fingure out what your original vectors are in the 4-space, and take the appropriate inverse operations to get your resultant quaternion (without going through the dot/cross product steps) but that requires a good foundation in hypercomplex algebra.







                              share|cite|improve this answer












                              share|cite|improve this answer



                              share|cite|improve this answer










                              answered Aug 8 '12 at 21:50









                              Vilid

                              33617




                              33617







                              • 1




                                Just to be clear: from the article, the dot product will be your scalar part, and the cross product the vector part, so with dot product: x and scalar product iy + jz + ka the quaternion q would be q = x +iy +jz +ka.
                                – Vilid
                                Aug 8 '12 at 21:51











                              • Not exactly. If the dot product is a, and the cross product is (x, y, z), it would be cos(a/2) + (x * sin(a/2))i + (y * sin(a/2))j + ( z * sin(a/2))k. Rotation quaternions are unitary.
                                – Jeff
                                Jul 19 '13 at 2:33













                              • 1




                                Just to be clear: from the article, the dot product will be your scalar part, and the cross product the vector part, so with dot product: x and scalar product iy + jz + ka the quaternion q would be q = x +iy +jz +ka.
                                – Vilid
                                Aug 8 '12 at 21:51











                              • Not exactly. If the dot product is a, and the cross product is (x, y, z), it would be cos(a/2) + (x * sin(a/2))i + (y * sin(a/2))j + ( z * sin(a/2))k. Rotation quaternions are unitary.
                                – Jeff
                                Jul 19 '13 at 2:33








                              1




                              1




                              Just to be clear: from the article, the dot product will be your scalar part, and the cross product the vector part, so with dot product: x and scalar product iy + jz + ka the quaternion q would be q = x +iy +jz +ka.
                              – Vilid
                              Aug 8 '12 at 21:51





                              Just to be clear: from the article, the dot product will be your scalar part, and the cross product the vector part, so with dot product: x and scalar product iy + jz + ka the quaternion q would be q = x +iy +jz +ka.
                              – Vilid
                              Aug 8 '12 at 21:51













                              Not exactly. If the dot product is a, and the cross product is (x, y, z), it would be cos(a/2) + (x * sin(a/2))i + (y * sin(a/2))j + ( z * sin(a/2))k. Rotation quaternions are unitary.
                              – Jeff
                              Jul 19 '13 at 2:33





                              Not exactly. If the dot product is a, and the cross product is (x, y, z), it would be cos(a/2) + (x * sin(a/2))i + (y * sin(a/2))j + ( z * sin(a/2))k. Rotation quaternions are unitary.
                              – Jeff
                              Jul 19 '13 at 2:33











                              up vote
                              3
                              down vote













                              Use Rodrigues' rotation formula (See the section "Conversion to rotation matrix"). $costheta$ is the dot product of the normalised initial vectors and $sintheta$ can be determined from $sin^2theta + cos^2theta =1$






                              share|cite|improve this answer






















                              • I don't think $sin theta$ can be determined in that way. There is sign ambiguity. (If it weren't so, then we'd get the same rotation matrix for going from $a$ to $b$ and viceversa.)
                                – leonbloy
                                Aug 8 '17 at 23:59















                              up vote
                              3
                              down vote













                              Use Rodrigues' rotation formula (See the section "Conversion to rotation matrix"). $costheta$ is the dot product of the normalised initial vectors and $sintheta$ can be determined from $sin^2theta + cos^2theta =1$






                              share|cite|improve this answer






















                              • I don't think $sin theta$ can be determined in that way. There is sign ambiguity. (If it weren't so, then we'd get the same rotation matrix for going from $a$ to $b$ and viceversa.)
                                – leonbloy
                                Aug 8 '17 at 23:59













                              up vote
                              3
                              down vote










                              up vote
                              3
                              down vote









                              Use Rodrigues' rotation formula (See the section "Conversion to rotation matrix"). $costheta$ is the dot product of the normalised initial vectors and $sintheta$ can be determined from $sin^2theta + cos^2theta =1$






                              share|cite|improve this answer














                              Use Rodrigues' rotation formula (See the section "Conversion to rotation matrix"). $costheta$ is the dot product of the normalised initial vectors and $sintheta$ can be determined from $sin^2theta + cos^2theta =1$







                              share|cite|improve this answer














                              share|cite|improve this answer



                              share|cite|improve this answer








                              edited Aug 11 '13 at 19:38









                              Stefan Hansen

                              20.3k73459




                              20.3k73459










                              answered Aug 11 '13 at 19:18









                              glennr

                              1311




                              1311











                              • I don't think $sin theta$ can be determined in that way. There is sign ambiguity. (If it weren't so, then we'd get the same rotation matrix for going from $a$ to $b$ and viceversa.)
                                – leonbloy
                                Aug 8 '17 at 23:59

















                              • I don't think $sin theta$ can be determined in that way. There is sign ambiguity. (If it weren't so, then we'd get the same rotation matrix for going from $a$ to $b$ and viceversa.)
                                – leonbloy
                                Aug 8 '17 at 23:59
















                              I don't think $sin theta$ can be determined in that way. There is sign ambiguity. (If it weren't so, then we'd get the same rotation matrix for going from $a$ to $b$ and viceversa.)
                              – leonbloy
                              Aug 8 '17 at 23:59





                              I don't think $sin theta$ can be determined in that way. There is sign ambiguity. (If it weren't so, then we'd get the same rotation matrix for going from $a$ to $b$ and viceversa.)
                              – leonbloy
                              Aug 8 '17 at 23:59











                              up vote
                              3
                              down vote













                              Rodrigues's rotation formula gives the result of a rotation of a vector $a$ around a rotation axis $k$ by the angle $theta$. We can make use of this by realizing that, to rotate a unit vector $a$ into $b$, we simply need to rotate $a$ by $pi$ around $(a+b)/2$. With this, one gets the beautiful
                              $$
                              R = 2 frac(a+b)(a+b)^T(a+b)^T(a+b) - I.
                              $$






                              share|cite|improve this answer






















                              • I can confirm that this works. Checked using Wolfram Mathematica that $Rcdot a=b$ with the assumptions that $||a||=||b||=1$.
                                – Ruslan
                                Apr 28 at 19:30











                              • It might be instructive to note that another way of thinking (that of course leads to a very similar result) are Householder reflections
                                – Bort
                                May 10 at 19:24














                              up vote
                              3
                              down vote













                              Rodrigues's rotation formula gives the result of a rotation of a vector $a$ around a rotation axis $k$ by the angle $theta$. We can make use of this by realizing that, to rotate a unit vector $a$ into $b$, we simply need to rotate $a$ by $pi$ around $(a+b)/2$. With this, one gets the beautiful
                              $$
                              R = 2 frac(a+b)(a+b)^T(a+b)^T(a+b) - I.
                              $$






                              share|cite|improve this answer






















                              • I can confirm that this works. Checked using Wolfram Mathematica that $Rcdot a=b$ with the assumptions that $||a||=||b||=1$.
                                – Ruslan
                                Apr 28 at 19:30











                              • It might be instructive to note that another way of thinking (that of course leads to a very similar result) are Householder reflections
                                – Bort
                                May 10 at 19:24












                              up vote
                              3
                              down vote










                              up vote
                              3
                              down vote









                              Rodrigues's rotation formula gives the result of a rotation of a vector $a$ around a rotation axis $k$ by the angle $theta$. We can make use of this by realizing that, to rotate a unit vector $a$ into $b$, we simply need to rotate $a$ by $pi$ around $(a+b)/2$. With this, one gets the beautiful
                              $$
                              R = 2 frac(a+b)(a+b)^T(a+b)^T(a+b) - I.
                              $$






                              share|cite|improve this answer














                              Rodrigues's rotation formula gives the result of a rotation of a vector $a$ around a rotation axis $k$ by the angle $theta$. We can make use of this by realizing that, to rotate a unit vector $a$ into $b$, we simply need to rotate $a$ by $pi$ around $(a+b)/2$. With this, one gets the beautiful
                              $$
                              R = 2 frac(a+b)(a+b)^T(a+b)^T(a+b) - I.
                              $$







                              share|cite|improve this answer














                              share|cite|improve this answer



                              share|cite|improve this answer








                              edited Mar 1 at 20:51

























                              answered Mar 1 at 20:43









                              Nico Schlömer

                              398113




                              398113











                              • I can confirm that this works. Checked using Wolfram Mathematica that $Rcdot a=b$ with the assumptions that $||a||=||b||=1$.
                                – Ruslan
                                Apr 28 at 19:30











                              • It might be instructive to note that another way of thinking (that of course leads to a very similar result) are Householder reflections
                                – Bort
                                May 10 at 19:24
















                              • I can confirm that this works. Checked using Wolfram Mathematica that $Rcdot a=b$ with the assumptions that $||a||=||b||=1$.
                                – Ruslan
                                Apr 28 at 19:30











                              • It might be instructive to note that another way of thinking (that of course leads to a very similar result) are Householder reflections
                                – Bort
                                May 10 at 19:24















                              I can confirm that this works. Checked using Wolfram Mathematica that $Rcdot a=b$ with the assumptions that $||a||=||b||=1$.
                              – Ruslan
                              Apr 28 at 19:30





                              I can confirm that this works. Checked using Wolfram Mathematica that $Rcdot a=b$ with the assumptions that $||a||=||b||=1$.
                              – Ruslan
                              Apr 28 at 19:30













                              It might be instructive to note that another way of thinking (that of course leads to a very similar result) are Householder reflections
                              – Bort
                              May 10 at 19:24




                              It might be instructive to note that another way of thinking (that of course leads to a very similar result) are Householder reflections
                              – Bort
                              May 10 at 19:24










                              up vote
                              2
                              down vote













                              Here is a Matlab function that can be used to calculated the rotation from one vector to another.



                              Example 1:



                              >> v1=[1 2 3]';
                              >> v2=[4 5 6]';
                              >> fcn_RotationFromTwoVectors(v1, v2)

                              ans =

                              0.9789 0.0829 0.1870
                              -0.0998 0.9915 0.0829
                              -0.1785 -0.0998 0.9789


                              Example 2:



                              >> v1=[1 2 0]';
                              >> v2=[3 4 0]';
                              >> fcn_RotationFromTwoVectors(v1, v2)

                              ans =

                              0.9839 0.1789 0
                              -0.1789 0.9839 0
                              0 0 1.0000


                              Function:



                              function R=fcn_RotationFromTwoVectors(v1, v2)
                              % R*v1=v2
                              % v1 and v2 should be column vectors and 3x1

                              % 1. rotation vector
                              w=cross(v1,v2);
                              w=w/norm(w);
                              w_hat=fcn_GetSkew(w);
                              % 2. rotation angle
                              cos_tht=v1'*v2/norm(v1)/norm(v2);
                              tht=acos(cos_tht);
                              % 3. rotation matrix, using Rodrigues' formula
                              R=eye(size(v1,1))+w_hat*sin(tht)+w_hat^2*(1-cos(tht));

                              function x_skew=fcn_GetSkew(x)
                              x_skew=[0 -x(3) x(2);
                              x(3) 0 -x(1);
                              -x(2) x(1) 0];





                              share|cite|improve this answer


























                                up vote
                                2
                                down vote













                                Here is a Matlab function that can be used to calculated the rotation from one vector to another.



                                Example 1:



                                >> v1=[1 2 3]';
                                >> v2=[4 5 6]';
                                >> fcn_RotationFromTwoVectors(v1, v2)

                                ans =

                                0.9789 0.0829 0.1870
                                -0.0998 0.9915 0.0829
                                -0.1785 -0.0998 0.9789


                                Example 2:



                                >> v1=[1 2 0]';
                                >> v2=[3 4 0]';
                                >> fcn_RotationFromTwoVectors(v1, v2)

                                ans =

                                0.9839 0.1789 0
                                -0.1789 0.9839 0
                                0 0 1.0000


                                Function:



                                function R=fcn_RotationFromTwoVectors(v1, v2)
                                % R*v1=v2
                                % v1 and v2 should be column vectors and 3x1

                                % 1. rotation vector
                                w=cross(v1,v2);
                                w=w/norm(w);
                                w_hat=fcn_GetSkew(w);
                                % 2. rotation angle
                                cos_tht=v1'*v2/norm(v1)/norm(v2);
                                tht=acos(cos_tht);
                                % 3. rotation matrix, using Rodrigues' formula
                                R=eye(size(v1,1))+w_hat*sin(tht)+w_hat^2*(1-cos(tht));

                                function x_skew=fcn_GetSkew(x)
                                x_skew=[0 -x(3) x(2);
                                x(3) 0 -x(1);
                                -x(2) x(1) 0];





                                share|cite|improve this answer
























                                  up vote
                                  2
                                  down vote










                                  up vote
                                  2
                                  down vote









                                  Here is a Matlab function that can be used to calculated the rotation from one vector to another.



                                  Example 1:



                                  >> v1=[1 2 3]';
                                  >> v2=[4 5 6]';
                                  >> fcn_RotationFromTwoVectors(v1, v2)

                                  ans =

                                  0.9789 0.0829 0.1870
                                  -0.0998 0.9915 0.0829
                                  -0.1785 -0.0998 0.9789


                                  Example 2:



                                  >> v1=[1 2 0]';
                                  >> v2=[3 4 0]';
                                  >> fcn_RotationFromTwoVectors(v1, v2)

                                  ans =

                                  0.9839 0.1789 0
                                  -0.1789 0.9839 0
                                  0 0 1.0000


                                  Function:



                                  function R=fcn_RotationFromTwoVectors(v1, v2)
                                  % R*v1=v2
                                  % v1 and v2 should be column vectors and 3x1

                                  % 1. rotation vector
                                  w=cross(v1,v2);
                                  w=w/norm(w);
                                  w_hat=fcn_GetSkew(w);
                                  % 2. rotation angle
                                  cos_tht=v1'*v2/norm(v1)/norm(v2);
                                  tht=acos(cos_tht);
                                  % 3. rotation matrix, using Rodrigues' formula
                                  R=eye(size(v1,1))+w_hat*sin(tht)+w_hat^2*(1-cos(tht));

                                  function x_skew=fcn_GetSkew(x)
                                  x_skew=[0 -x(3) x(2);
                                  x(3) 0 -x(1);
                                  -x(2) x(1) 0];





                                  share|cite|improve this answer














                                  Here is a Matlab function that can be used to calculated the rotation from one vector to another.



                                  Example 1:



                                  >> v1=[1 2 3]';
                                  >> v2=[4 5 6]';
                                  >> fcn_RotationFromTwoVectors(v1, v2)

                                  ans =

                                  0.9789 0.0829 0.1870
                                  -0.0998 0.9915 0.0829
                                  -0.1785 -0.0998 0.9789


                                  Example 2:



                                  >> v1=[1 2 0]';
                                  >> v2=[3 4 0]';
                                  >> fcn_RotationFromTwoVectors(v1, v2)

                                  ans =

                                  0.9839 0.1789 0
                                  -0.1789 0.9839 0
                                  0 0 1.0000


                                  Function:



                                  function R=fcn_RotationFromTwoVectors(v1, v2)
                                  % R*v1=v2
                                  % v1 and v2 should be column vectors and 3x1

                                  % 1. rotation vector
                                  w=cross(v1,v2);
                                  w=w/norm(w);
                                  w_hat=fcn_GetSkew(w);
                                  % 2. rotation angle
                                  cos_tht=v1'*v2/norm(v1)/norm(v2);
                                  tht=acos(cos_tht);
                                  % 3. rotation matrix, using Rodrigues' formula
                                  R=eye(size(v1,1))+w_hat*sin(tht)+w_hat^2*(1-cos(tht));

                                  function x_skew=fcn_GetSkew(x)
                                  x_skew=[0 -x(3) x(2);
                                  x(3) 0 -x(1);
                                  -x(2) x(1) 0];






                                  share|cite|improve this answer














                                  share|cite|improve this answer



                                  share|cite|improve this answer








                                  edited May 8 '15 at 16:41









                                  Kuba Ober

                                  466413




                                  466413










                                  answered Jan 25 '15 at 19:34









                                  Shiyu

                                  2,53321830




                                  2,53321830




















                                      up vote
                                      2
                                      down vote













                                      As you read from the thread: http://forums.cgsociety.org/archive/index.php/t-741227.html




                                      Note that this will not align all 3 axes of the teapots, only the Z axis. We have no knowledge of the full orientation of the teapots in space, only know where the Z are pointing at. theTM will be the value you are looking for.




                                      There is NO unique Matrix that could rotate one unit vector to another. Simply because the solution to 3 equations with 9 arguments does not unique.



                                      Since you have the plane (not only the normal vector), a way to find a unique rotation matrix between two coordinate system would be: do the non-unique rotation twice!



                                      That is



                                      1. Find a orthogonal vector in the same plane of interest with A and B respectively. Say $A_o bot A$ in the original plane and $B_o bot B$ in the target plane.

                                      2. use previous answers: This or this to find the rotation matrix within the $Atimes B$ plane. Assume matrix is $U_AB$

                                      3. Apply the matrix $U_AB$ to $A_o$ result in a new $B'_o=U_ABA_o$.

                                      4. This $B'_o$ can be used as one of the new inputs to the same rotation funtion from the previous answers together with $B_o$. Another rotation matrix $U_B'B$ is calculated in $B'times B$ plane.

                                      5. Since $B'times B$ plane is perpendicular to $A$, $U_B'B$ would not influence the transform of $Ato B$. i.e. $B=U_ABA=U_B'BU_ABA$. and the final coordinate transform matrix should be $U=U_B'BU_AB$

                                      6. consider yourself for corner cases. :)

                                      validate:



                                      function:



                                      % Implementation of Rik's Answer:
                                      ssc = @(v) [0 -v(3) v(2); v(3) 0 -v(1); -v(2) v(1) 0];
                                      RU = @(A,B) eye(3) + ssc(cross(A,B)) + ...
                                      ssc(cross(A,B))^2*(1-dot(A,B))/(norm(cross(A,B))^2);


                                      Test:





                                      > a=[1 0 0]'; b=[0 1 0]';
                                      > ao=[0 1 0]'; bo=[-sqrt(2)/2,0,sqrt(2)/2]';
                                      > Uab = RU(a,b);
                                      > Ucd = RU(Uab*ao,bo);
                                      > U = Ucd*Uab;
                                      > norm(b-U*a) % does Ucd influence a to b?
                                      ans = 0
                                      > norm(bo-U*ao) % does Ucd influence a-orthogonal and b-orthogonal?
                                      ans = 0
                                      > U
                                      U =

                                      0 -0.7071 0.7071
                                      1.0000 0 0
                                      0 0.7071 0.7071





                                      share|cite|improve this answer






















                                      • Please, use Latex.
                                        – Mithlesh Upadhyay
                                        Jul 15 '16 at 9:21










                                      • @MithleshUpadhyay I don't get it? I feel somewhat difficult when editing answers but I did use some LaTeX for equations. They looks nice on my screen. Is there anything I can do better? If there are tricks and tips I'm willing to hear from you.
                                        – Kangqiao Zhao
                                        Jul 19 '16 at 11:47














                                      up vote
                                      2
                                      down vote













                                      As you read from the thread: http://forums.cgsociety.org/archive/index.php/t-741227.html




                                      Note that this will not align all 3 axes of the teapots, only the Z axis. We have no knowledge of the full orientation of the teapots in space, only know where the Z are pointing at. theTM will be the value you are looking for.




                                      There is NO unique Matrix that could rotate one unit vector to another. Simply because the solution to 3 equations with 9 arguments does not unique.



                                      Since you have the plane (not only the normal vector), a way to find a unique rotation matrix between two coordinate system would be: do the non-unique rotation twice!



                                      That is



                                      1. Find a orthogonal vector in the same plane of interest with A and B respectively. Say $A_o bot A$ in the original plane and $B_o bot B$ in the target plane.

                                      2. use previous answers: This or this to find the rotation matrix within the $Atimes B$ plane. Assume matrix is $U_AB$

                                      3. Apply the matrix $U_AB$ to $A_o$ result in a new $B'_o=U_ABA_o$.

                                      4. This $B'_o$ can be used as one of the new inputs to the same rotation funtion from the previous answers together with $B_o$. Another rotation matrix $U_B'B$ is calculated in $B'times B$ plane.

                                      5. Since $B'times B$ plane is perpendicular to $A$, $U_B'B$ would not influence the transform of $Ato B$. i.e. $B=U_ABA=U_B'BU_ABA$. and the final coordinate transform matrix should be $U=U_B'BU_AB$

                                      6. consider yourself for corner cases. :)

                                      validate:



                                      function:



                                      % Implementation of Rik's Answer:
                                      ssc = @(v) [0 -v(3) v(2); v(3) 0 -v(1); -v(2) v(1) 0];
                                      RU = @(A,B) eye(3) + ssc(cross(A,B)) + ...
                                      ssc(cross(A,B))^2*(1-dot(A,B))/(norm(cross(A,B))^2);


                                      Test:





                                      > a=[1 0 0]'; b=[0 1 0]';
                                      > ao=[0 1 0]'; bo=[-sqrt(2)/2,0,sqrt(2)/2]';
                                      > Uab = RU(a,b);
                                      > Ucd = RU(Uab*ao,bo);
                                      > U = Ucd*Uab;
                                      > norm(b-U*a) % does Ucd influence a to b?
                                      ans = 0
                                      > norm(bo-U*ao) % does Ucd influence a-orthogonal and b-orthogonal?
                                      ans = 0
                                      > U
                                      U =

                                      0 -0.7071 0.7071
                                      1.0000 0 0
                                      0 0.7071 0.7071





                                      share|cite|improve this answer






















                                      • Please, use Latex.
                                        – Mithlesh Upadhyay
                                        Jul 15 '16 at 9:21










                                      • @MithleshUpadhyay I don't get it? I feel somewhat difficult when editing answers but I did use some LaTeX for equations. They looks nice on my screen. Is there anything I can do better? If there are tricks and tips I'm willing to hear from you.
                                        – Kangqiao Zhao
                                        Jul 19 '16 at 11:47












                                      up vote
                                      2
                                      down vote










                                      up vote
                                      2
                                      down vote









                                      As you read from the thread: http://forums.cgsociety.org/archive/index.php/t-741227.html




                                      Note that this will not align all 3 axes of the teapots, only the Z axis. We have no knowledge of the full orientation of the teapots in space, only know where the Z are pointing at. theTM will be the value you are looking for.




                                      There is NO unique Matrix that could rotate one unit vector to another. Simply because the solution to 3 equations with 9 arguments does not unique.



                                      Since you have the plane (not only the normal vector), a way to find a unique rotation matrix between two coordinate system would be: do the non-unique rotation twice!



                                      That is



                                      1. Find a orthogonal vector in the same plane of interest with A and B respectively. Say $A_o bot A$ in the original plane and $B_o bot B$ in the target plane.

                                      2. use previous answers: This or this to find the rotation matrix within the $Atimes B$ plane. Assume matrix is $U_AB$

                                      3. Apply the matrix $U_AB$ to $A_o$ result in a new $B'_o=U_ABA_o$.

                                      4. This $B'_o$ can be used as one of the new inputs to the same rotation funtion from the previous answers together with $B_o$. Another rotation matrix $U_B'B$ is calculated in $B'times B$ plane.

                                      5. Since $B'times B$ plane is perpendicular to $A$, $U_B'B$ would not influence the transform of $Ato B$. i.e. $B=U_ABA=U_B'BU_ABA$. and the final coordinate transform matrix should be $U=U_B'BU_AB$

                                      6. consider yourself for corner cases. :)

                                      validate:



                                      function:



                                      % Implementation of Rik's Answer:
                                      ssc = @(v) [0 -v(3) v(2); v(3) 0 -v(1); -v(2) v(1) 0];
                                      RU = @(A,B) eye(3) + ssc(cross(A,B)) + ...
                                      ssc(cross(A,B))^2*(1-dot(A,B))/(norm(cross(A,B))^2);


                                      Test:





                                      > a=[1 0 0]'; b=[0 1 0]';
                                      > ao=[0 1 0]'; bo=[-sqrt(2)/2,0,sqrt(2)/2]';
                                      > Uab = RU(a,b);
                                      > Ucd = RU(Uab*ao,bo);
                                      > U = Ucd*Uab;
                                      > norm(b-U*a) % does Ucd influence a to b?
                                      ans = 0
                                      > norm(bo-U*ao) % does Ucd influence a-orthogonal and b-orthogonal?
                                      ans = 0
                                      > U
                                      U =

                                      0 -0.7071 0.7071
                                      1.0000 0 0
                                      0 0.7071 0.7071





                                      share|cite|improve this answer














                                      As you read from the thread: http://forums.cgsociety.org/archive/index.php/t-741227.html




                                      Note that this will not align all 3 axes of the teapots, only the Z axis. We have no knowledge of the full orientation of the teapots in space, only know where the Z are pointing at. theTM will be the value you are looking for.




                                      There is NO unique Matrix that could rotate one unit vector to another. Simply because the solution to 3 equations with 9 arguments does not unique.



                                      Since you have the plane (not only the normal vector), a way to find a unique rotation matrix between two coordinate system would be: do the non-unique rotation twice!



                                      That is



                                      1. Find a orthogonal vector in the same plane of interest with A and B respectively. Say $A_o bot A$ in the original plane and $B_o bot B$ in the target plane.

                                      2. use previous answers: This or this to find the rotation matrix within the $Atimes B$ plane. Assume matrix is $U_AB$

                                      3. Apply the matrix $U_AB$ to $A_o$ result in a new $B'_o=U_ABA_o$.

                                      4. This $B'_o$ can be used as one of the new inputs to the same rotation funtion from the previous answers together with $B_o$. Another rotation matrix $U_B'B$ is calculated in $B'times B$ plane.

                                      5. Since $B'times B$ plane is perpendicular to $A$, $U_B'B$ would not influence the transform of $Ato B$. i.e. $B=U_ABA=U_B'BU_ABA$. and the final coordinate transform matrix should be $U=U_B'BU_AB$

                                      6. consider yourself for corner cases. :)

                                      validate:



                                      function:



                                      % Implementation of Rik's Answer:
                                      ssc = @(v) [0 -v(3) v(2); v(3) 0 -v(1); -v(2) v(1) 0];
                                      RU = @(A,B) eye(3) + ssc(cross(A,B)) + ...
                                      ssc(cross(A,B))^2*(1-dot(A,B))/(norm(cross(A,B))^2);


                                      Test:





                                      > a=[1 0 0]'; b=[0 1 0]';
                                      > ao=[0 1 0]'; bo=[-sqrt(2)/2,0,sqrt(2)/2]';
                                      > Uab = RU(a,b);
                                      > Ucd = RU(Uab*ao,bo);
                                      > U = Ucd*Uab;
                                      > norm(b-U*a) % does Ucd influence a to b?
                                      ans = 0
                                      > norm(bo-U*ao) % does Ucd influence a-orthogonal and b-orthogonal?
                                      ans = 0
                                      > U
                                      U =

                                      0 -0.7071 0.7071
                                      1.0000 0 0
                                      0 0.7071 0.7071






                                      share|cite|improve this answer














                                      share|cite|improve this answer



                                      share|cite|improve this answer








                                      edited Apr 13 '17 at 12:20









                                      Community♦

                                      1




                                      1










                                      answered Jul 15 '16 at 8:57









                                      Kangqiao Zhao

                                      211




                                      211











                                      • Please, use Latex.
                                        – Mithlesh Upadhyay
                                        Jul 15 '16 at 9:21










                                      • @MithleshUpadhyay I don't get it? I feel somewhat difficult when editing answers but I did use some LaTeX for equations. They looks nice on my screen. Is there anything I can do better? If there are tricks and tips I'm willing to hear from you.
                                        – Kangqiao Zhao
                                        Jul 19 '16 at 11:47
















                                      • Please, use Latex.
                                        – Mithlesh Upadhyay
                                        Jul 15 '16 at 9:21










                                      • @MithleshUpadhyay I don't get it? I feel somewhat difficult when editing answers but I did use some LaTeX for equations. They looks nice on my screen. Is there anything I can do better? If there are tricks and tips I'm willing to hear from you.
                                        – Kangqiao Zhao
                                        Jul 19 '16 at 11:47















                                      Please, use Latex.
                                      – Mithlesh Upadhyay
                                      Jul 15 '16 at 9:21




                                      Please, use Latex.
                                      – Mithlesh Upadhyay
                                      Jul 15 '16 at 9:21












                                      @MithleshUpadhyay I don't get it? I feel somewhat difficult when editing answers but I did use some LaTeX for equations. They looks nice on my screen. Is there anything I can do better? If there are tricks and tips I'm willing to hear from you.
                                      – Kangqiao Zhao
                                      Jul 19 '16 at 11:47




                                      @MithleshUpadhyay I don't get it? I feel somewhat difficult when editing answers but I did use some LaTeX for equations. They looks nice on my screen. Is there anything I can do better? If there are tricks and tips I'm willing to hear from you.
                                      – Kangqiao Zhao
                                      Jul 19 '16 at 11:47










                                      up vote
                                      1
                                      down vote













                                      You can easily do all this operation using the Vector3 library.



                                      The following four steps worked for me.



                                      Vector3 axis = Vector3.CrossProduct(v1, v2);

                                      if (axis.Magnitude != 0)

                                      axis.Normalize();
                                      Vector3D vAxis = new Vector3D(axis.X, axis.Y, axis.Z);
                                      Matrix3D m = Matrix3D.Identity;
                                      Quaternion q = new Quaternion(vAxis, AngleFromZaxis);

                                      m.RotateAt(q, centerPoint);
                                      MatrixTransform3D mT = new MatrixTransform3D(m);

                                      group.Children.Add(mT);

                                      myModel.Transform = group;






                                      share|cite|improve this answer


















                                      • 1




                                        You use an undeclared variable AngleFromZaxis in your example there. And group. Hmm this code is a bit of a mess unfortunately.
                                        – Andrew Hundt
                                        Apr 30 '15 at 21:39














                                      up vote
                                      1
                                      down vote













                                      You can easily do all this operation using the Vector3 library.



                                      The following four steps worked for me.



                                      Vector3 axis = Vector3.CrossProduct(v1, v2);

                                      if (axis.Magnitude != 0)

                                      axis.Normalize();
                                      Vector3D vAxis = new Vector3D(axis.X, axis.Y, axis.Z);
                                      Matrix3D m = Matrix3D.Identity;
                                      Quaternion q = new Quaternion(vAxis, AngleFromZaxis);

                                      m.RotateAt(q, centerPoint);
                                      MatrixTransform3D mT = new MatrixTransform3D(m);

                                      group.Children.Add(mT);

                                      myModel.Transform = group;






                                      share|cite|improve this answer


















                                      • 1




                                        You use an undeclared variable AngleFromZaxis in your example there. And group. Hmm this code is a bit of a mess unfortunately.
                                        – Andrew Hundt
                                        Apr 30 '15 at 21:39












                                      up vote
                                      1
                                      down vote










                                      up vote
                                      1
                                      down vote









                                      You can easily do all this operation using the Vector3 library.



                                      The following four steps worked for me.



                                      Vector3 axis = Vector3.CrossProduct(v1, v2);

                                      if (axis.Magnitude != 0)

                                      axis.Normalize();
                                      Vector3D vAxis = new Vector3D(axis.X, axis.Y, axis.Z);
                                      Matrix3D m = Matrix3D.Identity;
                                      Quaternion q = new Quaternion(vAxis, AngleFromZaxis);

                                      m.RotateAt(q, centerPoint);
                                      MatrixTransform3D mT = new MatrixTransform3D(m);

                                      group.Children.Add(mT);

                                      myModel.Transform = group;






                                      share|cite|improve this answer














                                      You can easily do all this operation using the Vector3 library.



                                      The following four steps worked for me.



                                      Vector3 axis = Vector3.CrossProduct(v1, v2);

                                      if (axis.Magnitude != 0)

                                      axis.Normalize();
                                      Vector3D vAxis = new Vector3D(axis.X, axis.Y, axis.Z);
                                      Matrix3D m = Matrix3D.Identity;
                                      Quaternion q = new Quaternion(vAxis, AngleFromZaxis);

                                      m.RotateAt(q, centerPoint);
                                      MatrixTransform3D mT = new MatrixTransform3D(m);

                                      group.Children.Add(mT);

                                      myModel.Transform = group;







                                      share|cite|improve this answer














                                      share|cite|improve this answer



                                      share|cite|improve this answer








                                      edited Aug 6 '15 at 19:09









                                      Kuba Ober

                                      466413




                                      466413










                                      answered Feb 19 '13 at 0:04









                                      Shanmuga Sundaram

                                      111




                                      111







                                      • 1




                                        You use an undeclared variable AngleFromZaxis in your example there. And group. Hmm this code is a bit of a mess unfortunately.
                                        – Andrew Hundt
                                        Apr 30 '15 at 21:39












                                      • 1




                                        You use an undeclared variable AngleFromZaxis in your example there. And group. Hmm this code is a bit of a mess unfortunately.
                                        – Andrew Hundt
                                        Apr 30 '15 at 21:39







                                      1




                                      1




                                      You use an undeclared variable AngleFromZaxis in your example there. And group. Hmm this code is a bit of a mess unfortunately.
                                      – Andrew Hundt
                                      Apr 30 '15 at 21:39




                                      You use an undeclared variable AngleFromZaxis in your example there. And group. Hmm this code is a bit of a mess unfortunately.
                                      – Andrew Hundt
                                      Apr 30 '15 at 21:39










                                      up vote
                                      1
                                      down vote













                                      General solution for n dimensions in matlab / octave:



                                      %% Build input data
                                      n = 4;
                                      a = randn(n,1);
                                      b = randn(n,1);



                                      %% Compute Q = rotation matrix
                                      A = a*b';
                                      [V,D] = eig(A'+A);
                                      [~,idx] = min(diag(D));
                                      v = V(:,idx);
                                      Q = eye(n) - 2*(v*v');



                                      %% Validate Q is correct
                                      b_hat = Q'*a*norm(b)/norm(a);
                                      disp(['norm of error = ' num2str(norm(b_hat-b))])
                                      disp(['eigenvalues of Q = ' num2str(eig(Q)')])






                                      share|cite|improve this answer


























                                        up vote
                                        1
                                        down vote













                                        General solution for n dimensions in matlab / octave:



                                        %% Build input data
                                        n = 4;
                                        a = randn(n,1);
                                        b = randn(n,1);



                                        %% Compute Q = rotation matrix
                                        A = a*b';
                                        [V,D] = eig(A'+A);
                                        [~,idx] = min(diag(D));
                                        v = V(:,idx);
                                        Q = eye(n) - 2*(v*v');



                                        %% Validate Q is correct
                                        b_hat = Q'*a*norm(b)/norm(a);
                                        disp(['norm of error = ' num2str(norm(b_hat-b))])
                                        disp(['eigenvalues of Q = ' num2str(eig(Q)')])






                                        share|cite|improve this answer
























                                          up vote
                                          1
                                          down vote










                                          up vote
                                          1
                                          down vote









                                          General solution for n dimensions in matlab / octave:



                                          %% Build input data
                                          n = 4;
                                          a = randn(n,1);
                                          b = randn(n,1);



                                          %% Compute Q = rotation matrix
                                          A = a*b';
                                          [V,D] = eig(A'+A);
                                          [~,idx] = min(diag(D));
                                          v = V(:,idx);
                                          Q = eye(n) - 2*(v*v');



                                          %% Validate Q is correct
                                          b_hat = Q'*a*norm(b)/norm(a);
                                          disp(['norm of error = ' num2str(norm(b_hat-b))])
                                          disp(['eigenvalues of Q = ' num2str(eig(Q)')])






                                          share|cite|improve this answer














                                          General solution for n dimensions in matlab / octave:



                                          %% Build input data
                                          n = 4;
                                          a = randn(n,1);
                                          b = randn(n,1);



                                          %% Compute Q = rotation matrix
                                          A = a*b';
                                          [V,D] = eig(A'+A);
                                          [~,idx] = min(diag(D));
                                          v = V(:,idx);
                                          Q = eye(n) - 2*(v*v');



                                          %% Validate Q is correct
                                          b_hat = Q'*a*norm(b)/norm(a);
                                          disp(['norm of error = ' num2str(norm(b_hat-b))])
                                          disp(['eigenvalues of Q = ' num2str(eig(Q)')])







                                          share|cite|improve this answer














                                          share|cite|improve this answer



                                          share|cite|improve this answer








                                          edited May 13 '17 at 20:41









                                          NoseKnowsAll

                                          1,870920




                                          1,870920










                                          answered Sep 6 '16 at 21:24









                                          Orion

                                          112




                                          112




















                                              up vote
                                              1
                                              down vote













                                              You could say you are looking for a transformation between two orthonormal bases:



                                              $$
                                              M*[veci,vecj,veck]=[veci',vecj',veck']
                                              $$
                                              where



                                              • $veci$ and $veci'$ are the two vectors in question ("from" and "to")

                                              and the missing parts can be picked in a way which suits the purpose:



                                              • $vecj=vecj'=fracvecitimesveci'$, the axis of rotation, which is left in place and is orthogonal to the $veci,veci'$ plane (and has to be normalized)

                                              • $veck=vecitimesvecj, veck'=veci'timesvecj'$, because you need a pair of 3rd vectors for completing the bases.

                                              Since $[veci,vecj,veck]$ is an orthogonal matrix, there is no need for 'real' inversion and the transformation is



                                              $$
                                              M=[veci',vecj',veck']*[veci,vecj,veck]^T
                                              $$
                                              Except when $veci,veci'$ do not stretch a plane (and thus $||vecitimesveci'||=0$). This calculation will not produce $I$, e.g. dies on both $veci=veci'$ and $veci=-veci'$



                                              Disclaimer: Sorry about the necro, and especially for the partial repeat: I see Kjetil's answer, but I simply do not understand what and why that skewed matrix is doing there, and while Kuba's answer says it builds on Kjetil's, it introduces trigonometry on top of that, slightly defeating the idea (of course I understand that the trigonometric part is expressed with dot/cross products at the end)



                                              Plus this was my first time with LaTeX, I feel $veci~veci'$ look a bit too similar, but $veci~veci'$ is just plain ugly. And writing that fraction properly is way above me.






                                              share|cite|improve this answer


























                                                up vote
                                                1
                                                down vote













                                                You could say you are looking for a transformation between two orthonormal bases:



                                                $$
                                                M*[veci,vecj,veck]=[veci',vecj',veck']
                                                $$
                                                where



                                                • $veci$ and $veci'$ are the two vectors in question ("from" and "to")

                                                and the missing parts can be picked in a way which suits the purpose:



                                                • $vecj=vecj'=fracvecitimesveci'$, the axis of rotation, which is left in place and is orthogonal to the $veci,veci'$ plane (and has to be normalized)

                                                • $veck=vecitimesvecj, veck'=veci'timesvecj'$, because you need a pair of 3rd vectors for completing the bases.

                                                Since $[veci,vecj,veck]$ is an orthogonal matrix, there is no need for 'real' inversion and the transformation is



                                                $$
                                                M=[veci',vecj',veck']*[veci,vecj,veck]^T
                                                $$
                                                Except when $veci,veci'$ do not stretch a plane (and thus $||vecitimesveci'||=0$). This calculation will not produce $I$, e.g. dies on both $veci=veci'$ and $veci=-veci'$



                                                Disclaimer: Sorry about the necro, and especially for the partial repeat: I see Kjetil's answer, but I simply do not understand what and why that skewed matrix is doing there, and while Kuba's answer says it builds on Kjetil's, it introduces trigonometry on top of that, slightly defeating the idea (of course I understand that the trigonometric part is expressed with dot/cross products at the end)



                                                Plus this was my first time with LaTeX, I feel $veci~veci'$ look a bit too similar, but $veci~veci'$ is just plain ugly. And writing that fraction properly is way above me.






                                                share|cite|improve this answer
























                                                  up vote
                                                  1
                                                  down vote










                                                  up vote
                                                  1
                                                  down vote









                                                  You could say you are looking for a transformation between two orthonormal bases:



                                                  $$
                                                  M*[veci,vecj,veck]=[veci',vecj',veck']
                                                  $$
                                                  where



                                                  • $veci$ and $veci'$ are the two vectors in question ("from" and "to")

                                                  and the missing parts can be picked in a way which suits the purpose:



                                                  • $vecj=vecj'=fracvecitimesveci'$, the axis of rotation, which is left in place and is orthogonal to the $veci,veci'$ plane (and has to be normalized)

                                                  • $veck=vecitimesvecj, veck'=veci'timesvecj'$, because you need a pair of 3rd vectors for completing the bases.

                                                  Since $[veci,vecj,veck]$ is an orthogonal matrix, there is no need for 'real' inversion and the transformation is



                                                  $$
                                                  M=[veci',vecj',veck']*[veci,vecj,veck]^T
                                                  $$
                                                  Except when $veci,veci'$ do not stretch a plane (and thus $||vecitimesveci'||=0$). This calculation will not produce $I$, e.g. dies on both $veci=veci'$ and $veci=-veci'$



                                                  Disclaimer: Sorry about the necro, and especially for the partial repeat: I see Kjetil's answer, but I simply do not understand what and why that skewed matrix is doing there, and while Kuba's answer says it builds on Kjetil's, it introduces trigonometry on top of that, slightly defeating the idea (of course I understand that the trigonometric part is expressed with dot/cross products at the end)



                                                  Plus this was my first time with LaTeX, I feel $veci~veci'$ look a bit too similar, but $veci~veci'$ is just plain ugly. And writing that fraction properly is way above me.






                                                  share|cite|improve this answer














                                                  You could say you are looking for a transformation between two orthonormal bases:



                                                  $$
                                                  M*[veci,vecj,veck]=[veci',vecj',veck']
                                                  $$
                                                  where



                                                  • $veci$ and $veci'$ are the two vectors in question ("from" and "to")

                                                  and the missing parts can be picked in a way which suits the purpose:



                                                  • $vecj=vecj'=fracvecitimesveci'$, the axis of rotation, which is left in place and is orthogonal to the $veci,veci'$ plane (and has to be normalized)

                                                  • $veck=vecitimesvecj, veck'=veci'timesvecj'$, because you need a pair of 3rd vectors for completing the bases.

                                                  Since $[veci,vecj,veck]$ is an orthogonal matrix, there is no need for 'real' inversion and the transformation is



                                                  $$
                                                  M=[veci',vecj',veck']*[veci,vecj,veck]^T
                                                  $$
                                                  Except when $veci,veci'$ do not stretch a plane (and thus $||vecitimesveci'||=0$). This calculation will not produce $I$, e.g. dies on both $veci=veci'$ and $veci=-veci'$



                                                  Disclaimer: Sorry about the necro, and especially for the partial repeat: I see Kjetil's answer, but I simply do not understand what and why that skewed matrix is doing there, and while Kuba's answer says it builds on Kjetil's, it introduces trigonometry on top of that, slightly defeating the idea (of course I understand that the trigonometric part is expressed with dot/cross products at the end)



                                                  Plus this was my first time with LaTeX, I feel $veci~veci'$ look a bit too similar, but $veci~veci'$ is just plain ugly. And writing that fraction properly is way above me.







                                                  share|cite|improve this answer














                                                  share|cite|improve this answer



                                                  share|cite|improve this answer








                                                  edited Feb 27 at 19:43









                                                  Yves Daoust

                                                  113k665207




                                                  113k665207










                                                  answered Oct 13 '17 at 10:29









                                                  tevemadar

                                                  1964




                                                  1964




















                                                      up vote
                                                      0
                                                      down vote













                                                      Here's how to find the transformation from one triangle to another.



                                                      First triangle has vertices $a,b,c$ and normal $n$ and second triangle has vertices $a',b',c'$ and normal $n'$.



                                                      First we will find transformation $f(x)=Mx+t$ from reference triangle to the first triangle and another transformation $g(x)=M'x+t'$ from reference triangle to the second triangle. Then the transformation mapping first triangle to the second is $$T(x) = g(f^-1(x)) = M'(M^-1(x-t)) + t' = Rx + s$$
                                                      where $R = M'M^-1$ and $s = -M'M^-1t + t'$.



                                                      As the reference triangle we will use triangle with vertices $(0,0,0),(1,0,0),(0,1,0)$ and normal $(0,0,1)$



                                                      Then:
                                                      $$M = [ b-a, c-a, n]$$
                                                      $$t = a $$
                                                      $$M' = [ b'-a', c'-a', n' ] $$
                                                      $$t' = a' $$



                                                      You have to be cautious and give vertices $a,b,c$ and $a',b',c'$ in right order. This has to satisfy $((b-a)times (c-a))cdot n > 0$ and the same for second triangle.




                                                      If those two triangles are not the same then matrix $R$ will not be orthogonal. But we can find isometry which maps one triangle to the another as close as possible in some sense. For this you can use Kabsch algorithm which is well explained here.






                                                      share|cite|improve this answer


























                                                        up vote
                                                        0
                                                        down vote













                                                        Here's how to find the transformation from one triangle to another.



                                                        First triangle has vertices $a,b,c$ and normal $n$ and second triangle has vertices $a',b',c'$ and normal $n'$.



                                                        First we will find transformation $f(x)=Mx+t$ from reference triangle to the first triangle and another transformation $g(x)=M'x+t'$ from reference triangle to the second triangle. Then the transformation mapping first triangle to the second is $$T(x) = g(f^-1(x)) = M'(M^-1(x-t)) + t' = Rx + s$$
                                                        where $R = M'M^-1$ and $s = -M'M^-1t + t'$.



                                                        As the reference triangle we will use triangle with vertices $(0,0,0),(1,0,0),(0,1,0)$ and normal $(0,0,1)$



                                                        Then:
                                                        $$M = [ b-a, c-a, n]$$
                                                        $$t = a $$
                                                        $$M' = [ b'-a', c'-a', n' ] $$
                                                        $$t' = a' $$



                                                        You have to be cautious and give vertices $a,b,c$ and $a',b',c'$ in right order. This has to satisfy $((b-a)times (c-a))cdot n > 0$ and the same for second triangle.




                                                        If those two triangles are not the same then matrix $R$ will not be orthogonal. But we can find isometry which maps one triangle to the another as close as possible in some sense. For this you can use Kabsch algorithm which is well explained here.






                                                        share|cite|improve this answer
























                                                          up vote
                                                          0
                                                          down vote










                                                          up vote
                                                          0
                                                          down vote









                                                          Here's how to find the transformation from one triangle to another.



                                                          First triangle has vertices $a,b,c$ and normal $n$ and second triangle has vertices $a',b',c'$ and normal $n'$.



                                                          First we will find transformation $f(x)=Mx+t$ from reference triangle to the first triangle and another transformation $g(x)=M'x+t'$ from reference triangle to the second triangle. Then the transformation mapping first triangle to the second is $$T(x) = g(f^-1(x)) = M'(M^-1(x-t)) + t' = Rx + s$$
                                                          where $R = M'M^-1$ and $s = -M'M^-1t + t'$.



                                                          As the reference triangle we will use triangle with vertices $(0,0,0),(1,0,0),(0,1,0)$ and normal $(0,0,1)$



                                                          Then:
                                                          $$M = [ b-a, c-a, n]$$
                                                          $$t = a $$
                                                          $$M' = [ b'-a', c'-a', n' ] $$
                                                          $$t' = a' $$



                                                          You have to be cautious and give vertices $a,b,c$ and $a',b',c'$ in right order. This has to satisfy $((b-a)times (c-a))cdot n > 0$ and the same for second triangle.




                                                          If those two triangles are not the same then matrix $R$ will not be orthogonal. But we can find isometry which maps one triangle to the another as close as possible in some sense. For this you can use Kabsch algorithm which is well explained here.






                                                          share|cite|improve this answer














                                                          Here's how to find the transformation from one triangle to another.



                                                          First triangle has vertices $a,b,c$ and normal $n$ and second triangle has vertices $a',b',c'$ and normal $n'$.



                                                          First we will find transformation $f(x)=Mx+t$ from reference triangle to the first triangle and another transformation $g(x)=M'x+t'$ from reference triangle to the second triangle. Then the transformation mapping first triangle to the second is $$T(x) = g(f^-1(x)) = M'(M^-1(x-t)) + t' = Rx + s$$
                                                          where $R = M'M^-1$ and $s = -M'M^-1t + t'$.



                                                          As the reference triangle we will use triangle with vertices $(0,0,0),(1,0,0),(0,1,0)$ and normal $(0,0,1)$



                                                          Then:
                                                          $$M = [ b-a, c-a, n]$$
                                                          $$t = a $$
                                                          $$M' = [ b'-a', c'-a', n' ] $$
                                                          $$t' = a' $$



                                                          You have to be cautious and give vertices $a,b,c$ and $a',b',c'$ in right order. This has to satisfy $((b-a)times (c-a))cdot n > 0$ and the same for second triangle.




                                                          If those two triangles are not the same then matrix $R$ will not be orthogonal. But we can find isometry which maps one triangle to the another as close as possible in some sense. For this you can use Kabsch algorithm which is well explained here.







                                                          share|cite|improve this answer














                                                          share|cite|improve this answer



                                                          share|cite|improve this answer








                                                          edited Dec 16 '13 at 19:55









                                                          meetar

                                                          1125




                                                          1125










                                                          answered Aug 26 '13 at 6:33









                                                          tom

                                                          2,77511030




                                                          2,77511030




















                                                              up vote
                                                              0
                                                              down vote













                                                              This is an interesting problem. It guarantees that you rotate the unit vector $a$ to coincide with $b$. Still it would not be enough to rotate a rigid body to make it coincide with another. I will post an example of this below but first let me point a few important references. @glennr correctly pointed out the Rodrigues rotation formula . Note that this formula is the same as the one shown by @Jur var den vec here. The scaling factors are due to normalization of the cross and dot products since the vector $v$ in the Wikipedia website (here $a$) is arbitrary and here $a$ is unit. The Wikipedia page only requires vector manipulations (need to be good at taking cross products). Here is an elegant proof that does not depend too much on geometrical constructions but it requires simple linear ordinary differential equations for matrices, in addition to the cross product representation as a matrix operator. The final reference is the most complete document that I have found on this topic. It is titled ROTATION: A review of useful theorems involving proer orthogonal matries referenced to three dimensional physical space.



                                                              Now, for an example . This stack exchange link is about a question I posted on the rotation of a tetrahedron. I found and answer to my question by two consecutive rotations of matrices. The apex of the tetrahedron was at $(0,0,sqrt(3))$ and I wanted it to be at $(-1,-1,1)$.
                                                              Of course I wanted to verify that all vertices were rotated to their correct place and the product of matrices:
                                                              begineqnarray Y = left ( beginarrayccc cos alpha &
                                                              -sin alpha & 0 \ sin alpha & cos alpha & 0 \
                                                              0 & 0 & 1 endarray right )
                                                              = left ( beginarrayccc fracsqrt22 & -fracsqrt22 & 0 \ fracsqrt22 & fracsqrt22 & 0 \
                                                              0 & 0 & 1 endarray right )
                                                              endeqnarray



                                                              and
                                                              begineqnarray P = left (
                                                              beginarrayccc cos theta & 0 & -sin theta \ 0 & 1 & 0
                                                              \ sin theta & 0 & cos theta endarray right )
                                                              = left ( beginarrayccc fracsqrt33 & 0 & -fracsqrt2sqrt3 \
                                                              0 & 1 & 0 \ fracsqrt2sqrt3 & 0 & fracsqrt33 \ endarray right )
                                                              endeqnarray



                                                              That is the matrix:
                                                              begineqnarray*
                                                              frac16 left (
                                                              beginarrayccc
                                                              sqrt6 & -3sqrt2 & - 2 sqrt3 \
                                                              sqrt6 & 3 sqrt2 & -2 sqrt3 \
                                                              2 sqrt6 & 0 & 2 sqrt3
                                                              endarray
                                                              right )
                                                              endeqnarray*



                                                              did the job.



                                                              I thought that the Rodrigues rotation (the one being considered here)
                                                              would do the job but it did not. I computed the Rodrigues rotation for this problem. Here is the result:



                                                              begineqnarray*
                                                              R =
                                                              left (
                                                              beginarrayccc
                                                              fracsqrt3+12 sqrt3 & -fracsqrt3-12 sqrt3 & -frac1sqrt3 \
                                                              -fracsqrt3-12 sqrt3 & fracsqrt3+12 sqrt3& -frac1sqrt3 \
                                                              frac1sqrt3 & frac1sqrt3 & frac1sqrt3
                                                              endarray
                                                              right )
                                                              endeqnarray*



                                                              This matrix correctly maps the vector $(0,0,1)$ into the vector
                                                              $frac1sqrt3 (-1,-1,1)$ as promised, but the other three vectors on the base of the pyramid (tetrahedron) are not correctly mapped into their positions.



                                                              The meaning of this is that, to map rigid bodies through rotations, there is not a magic formula where only one matrix could be found and instead a multiplication of elementary (or non elmentary) matrices is required to do the job, unless we are lucky. Right?






                                                              share|cite|improve this answer


























                                                                up vote
                                                                0
                                                                down vote













                                                                This is an interesting problem. It guarantees that you rotate the unit vector $a$ to coincide with $b$. Still it would not be enough to rotate a rigid body to make it coincide with another. I will post an example of this below but first let me point a few important references. @glennr correctly pointed out the Rodrigues rotation formula . Note that this formula is the same as the one shown by @Jur var den vec here. The scaling factors are due to normalization of the cross and dot products since the vector $v$ in the Wikipedia website (here $a$) is arbitrary and here $a$ is unit. The Wikipedia page only requires vector manipulations (need to be good at taking cross products). Here is an elegant proof that does not depend too much on geometrical constructions but it requires simple linear ordinary differential equations for matrices, in addition to the cross product representation as a matrix operator. The final reference is the most complete document that I have found on this topic. It is titled ROTATION: A review of useful theorems involving proer orthogonal matries referenced to three dimensional physical space.



                                                                Now, for an example . This stack exchange link is about a question I posted on the rotation of a tetrahedron. I found and answer to my question by two consecutive rotations of matrices. The apex of the tetrahedron was at $(0,0,sqrt(3))$ and I wanted it to be at $(-1,-1,1)$.
                                                                Of course I wanted to verify that all vertices were rotated to their correct place and the product of matrices:
                                                                begineqnarray Y = left ( beginarrayccc cos alpha &
                                                                -sin alpha & 0 \ sin alpha & cos alpha & 0 \
                                                                0 & 0 & 1 endarray right )
                                                                = left ( beginarrayccc fracsqrt22 & -fracsqrt22 & 0 \ fracsqrt22 & fracsqrt22 & 0 \
                                                                0 & 0 & 1 endarray right )
                                                                endeqnarray



                                                                and
                                                                begineqnarray P = left (
                                                                beginarrayccc cos theta & 0 & -sin theta \ 0 & 1 & 0
                                                                \ sin theta & 0 & cos theta endarray right )
                                                                = left ( beginarrayccc fracsqrt33 & 0 & -fracsqrt2sqrt3 \
                                                                0 & 1 & 0 \ fracsqrt2sqrt3 & 0 & fracsqrt33 \ endarray right )
                                                                endeqnarray



                                                                That is the matrix:
                                                                begineqnarray*
                                                                frac16 left (
                                                                beginarrayccc
                                                                sqrt6 & -3sqrt2 & - 2 sqrt3 \
                                                                sqrt6 & 3 sqrt2 & -2 sqrt3 \
                                                                2 sqrt6 & 0 & 2 sqrt3
                                                                endarray
                                                                right )
                                                                endeqnarray*



                                                                did the job.



                                                                I thought that the Rodrigues rotation (the one being considered here)
                                                                would do the job but it did not. I computed the Rodrigues rotation for this problem. Here is the result:



                                                                begineqnarray*
                                                                R =
                                                                left (
                                                                beginarrayccc
                                                                fracsqrt3+12 sqrt3 & -fracsqrt3-12 sqrt3 & -frac1sqrt3 \
                                                                -fracsqrt3-12 sqrt3 & fracsqrt3+12 sqrt3& -frac1sqrt3 \
                                                                frac1sqrt3 & frac1sqrt3 & frac1sqrt3
                                                                endarray
                                                                right )
                                                                endeqnarray*



                                                                This matrix correctly maps the vector $(0,0,1)$ into the vector
                                                                $frac1sqrt3 (-1,-1,1)$ as promised, but the other three vectors on the base of the pyramid (tetrahedron) are not correctly mapped into their positions.



                                                                The meaning of this is that, to map rigid bodies through rotations, there is not a magic formula where only one matrix could be found and instead a multiplication of elementary (or non elmentary) matrices is required to do the job, unless we are lucky. Right?






                                                                share|cite|improve this answer
























                                                                  up vote
                                                                  0
                                                                  down vote










                                                                  up vote
                                                                  0
                                                                  down vote









                                                                  This is an interesting problem. It guarantees that you rotate the unit vector $a$ to coincide with $b$. Still it would not be enough to rotate a rigid body to make it coincide with another. I will post an example of this below but first let me point a few important references. @glennr correctly pointed out the Rodrigues rotation formula . Note that this formula is the same as the one shown by @Jur var den vec here. The scaling factors are due to normalization of the cross and dot products since the vector $v$ in the Wikipedia website (here $a$) is arbitrary and here $a$ is unit. The Wikipedia page only requires vector manipulations (need to be good at taking cross products). Here is an elegant proof that does not depend too much on geometrical constructions but it requires simple linear ordinary differential equations for matrices, in addition to the cross product representation as a matrix operator. The final reference is the most complete document that I have found on this topic. It is titled ROTATION: A review of useful theorems involving proer orthogonal matries referenced to three dimensional physical space.



                                                                  Now, for an example . This stack exchange link is about a question I posted on the rotation of a tetrahedron. I found and answer to my question by two consecutive rotations of matrices. The apex of the tetrahedron was at $(0,0,sqrt(3))$ and I wanted it to be at $(-1,-1,1)$.
                                                                  Of course I wanted to verify that all vertices were rotated to their correct place and the product of matrices:
                                                                  begineqnarray Y = left ( beginarrayccc cos alpha &
                                                                  -sin alpha & 0 \ sin alpha & cos alpha & 0 \
                                                                  0 & 0 & 1 endarray right )
                                                                  = left ( beginarrayccc fracsqrt22 & -fracsqrt22 & 0 \ fracsqrt22 & fracsqrt22 & 0 \
                                                                  0 & 0 & 1 endarray right )
                                                                  endeqnarray



                                                                  and
                                                                  begineqnarray P = left (
                                                                  beginarrayccc cos theta & 0 & -sin theta \ 0 & 1 & 0
                                                                  \ sin theta & 0 & cos theta endarray right )
                                                                  = left ( beginarrayccc fracsqrt33 & 0 & -fracsqrt2sqrt3 \
                                                                  0 & 1 & 0 \ fracsqrt2sqrt3 & 0 & fracsqrt33 \ endarray right )
                                                                  endeqnarray



                                                                  That is the matrix:
                                                                  begineqnarray*
                                                                  frac16 left (
                                                                  beginarrayccc
                                                                  sqrt6 & -3sqrt2 & - 2 sqrt3 \
                                                                  sqrt6 & 3 sqrt2 & -2 sqrt3 \
                                                                  2 sqrt6 & 0 & 2 sqrt3
                                                                  endarray
                                                                  right )
                                                                  endeqnarray*



                                                                  did the job.



                                                                  I thought that the Rodrigues rotation (the one being considered here)
                                                                  would do the job but it did not. I computed the Rodrigues rotation for this problem. Here is the result:



                                                                  begineqnarray*
                                                                  R =
                                                                  left (
                                                                  beginarrayccc
                                                                  fracsqrt3+12 sqrt3 & -fracsqrt3-12 sqrt3 & -frac1sqrt3 \
                                                                  -fracsqrt3-12 sqrt3 & fracsqrt3+12 sqrt3& -frac1sqrt3 \
                                                                  frac1sqrt3 & frac1sqrt3 & frac1sqrt3
                                                                  endarray
                                                                  right )
                                                                  endeqnarray*



                                                                  This matrix correctly maps the vector $(0,0,1)$ into the vector
                                                                  $frac1sqrt3 (-1,-1,1)$ as promised, but the other three vectors on the base of the pyramid (tetrahedron) are not correctly mapped into their positions.



                                                                  The meaning of this is that, to map rigid bodies through rotations, there is not a magic formula where only one matrix could be found and instead a multiplication of elementary (or non elmentary) matrices is required to do the job, unless we are lucky. Right?






                                                                  share|cite|improve this answer














                                                                  This is an interesting problem. It guarantees that you rotate the unit vector $a$ to coincide with $b$. Still it would not be enough to rotate a rigid body to make it coincide with another. I will post an example of this below but first let me point a few important references. @glennr correctly pointed out the Rodrigues rotation formula . Note that this formula is the same as the one shown by @Jur var den vec here. The scaling factors are due to normalization of the cross and dot products since the vector $v$ in the Wikipedia website (here $a$) is arbitrary and here $a$ is unit. The Wikipedia page only requires vector manipulations (need to be good at taking cross products). Here is an elegant proof that does not depend too much on geometrical constructions but it requires simple linear ordinary differential equations for matrices, in addition to the cross product representation as a matrix operator. The final reference is the most complete document that I have found on this topic. It is titled ROTATION: A review of useful theorems involving proer orthogonal matries referenced to three dimensional physical space.



                                                                  Now, for an example . This stack exchange link is about a question I posted on the rotation of a tetrahedron. I found and answer to my question by two consecutive rotations of matrices. The apex of the tetrahedron was at $(0,0,sqrt(3))$ and I wanted it to be at $(-1,-1,1)$.
                                                                  Of course I wanted to verify that all vertices were rotated to their correct place and the product of matrices:
                                                                  begineqnarray Y = left ( beginarrayccc cos alpha &
                                                                  -sin alpha & 0 \ sin alpha & cos alpha & 0 \
                                                                  0 & 0 & 1 endarray right )
                                                                  = left ( beginarrayccc fracsqrt22 & -fracsqrt22 & 0 \ fracsqrt22 & fracsqrt22 & 0 \
                                                                  0 & 0 & 1 endarray right )
                                                                  endeqnarray



                                                                  and
                                                                  begineqnarray P = left (
                                                                  beginarrayccc cos theta & 0 & -sin theta \ 0 & 1 & 0
                                                                  \ sin theta & 0 & cos theta endarray right )
                                                                  = left ( beginarrayccc fracsqrt33 & 0 & -fracsqrt2sqrt3 \
                                                                  0 & 1 & 0 \ fracsqrt2sqrt3 & 0 & fracsqrt33 \ endarray right )
                                                                  endeqnarray



                                                                  That is the matrix:
                                                                  begineqnarray*
                                                                  frac16 left (
                                                                  beginarrayccc
                                                                  sqrt6 & -3sqrt2 & - 2 sqrt3 \
                                                                  sqrt6 & 3 sqrt2 & -2 sqrt3 \
                                                                  2 sqrt6 & 0 & 2 sqrt3
                                                                  endarray
                                                                  right )
                                                                  endeqnarray*



                                                                  did the job.



                                                                  I thought that the Rodrigues rotation (the one being considered here)
                                                                  would do the job but it did not. I computed the Rodrigues rotation for this problem. Here is the result:



                                                                  begineqnarray*
                                                                  R =
                                                                  left (
                                                                  beginarrayccc
                                                                  fracsqrt3+12 sqrt3 & -fracsqrt3-12 sqrt3 & -frac1sqrt3 \
                                                                  -fracsqrt3-12 sqrt3 & fracsqrt3+12 sqrt3& -frac1sqrt3 \
                                                                  frac1sqrt3 & frac1sqrt3 & frac1sqrt3
                                                                  endarray
                                                                  right )
                                                                  endeqnarray*



                                                                  This matrix correctly maps the vector $(0,0,1)$ into the vector
                                                                  $frac1sqrt3 (-1,-1,1)$ as promised, but the other three vectors on the base of the pyramid (tetrahedron) are not correctly mapped into their positions.



                                                                  The meaning of this is that, to map rigid bodies through rotations, there is not a magic formula where only one matrix could be found and instead a multiplication of elementary (or non elmentary) matrices is required to do the job, unless we are lucky. Right?







                                                                  share|cite|improve this answer














                                                                  share|cite|improve this answer



                                                                  share|cite|improve this answer








                                                                  edited Apr 13 '17 at 12:20









                                                                  Community♦

                                                                  1




                                                                  1










                                                                  answered Dec 5 '15 at 20:35









                                                                  Herman Jaramillo

                                                                  1,174818




                                                                  1,174818




















                                                                      up vote
                                                                      0
                                                                      down vote













                                                                      Kuba Ober and Leyu Wang's answer works great. Here, is a python implementation of the same algorithm.



                                                                      import numpy as np
                                                                      import math


                                                                      def rotation_matrix(A,B):
                                                                      # a and b are in the form of numpy array

                                                                      ax = A[0]
                                                                      ay = A[1]
                                                                      az = A[2]

                                                                      bx = B[0]
                                                                      by = B[1]
                                                                      bz = B[2]

                                                                      au = A/(np.sqrt(ax*ax + ay*ay + az*az))
                                                                      bu = B/(np.sqrt(bx*bx + by*by + bz*bz))

                                                                      R=np.array([[bu[0]*au[0], bu[0]*au[1], bu[0]*au[2]], [bu[1]*au[0], bu[1]*au[1], bu[1]*au[2]], [bu[2]*au[0], bu[2]*au[1], bu[2]*au[2]] ])


                                                                      return(R)





                                                                      share|cite|improve this answer
























                                                                        up vote
                                                                        0
                                                                        down vote













                                                                        Kuba Ober and Leyu Wang's answer works great. Here, is a python implementation of the same algorithm.



                                                                        import numpy as np
                                                                        import math


                                                                        def rotation_matrix(A,B):
                                                                        # a and b are in the form of numpy array

                                                                        ax = A[0]
                                                                        ay = A[1]
                                                                        az = A[2]

                                                                        bx = B[0]
                                                                        by = B[1]
                                                                        bz = B[2]

                                                                        au = A/(np.sqrt(ax*ax + ay*ay + az*az))
                                                                        bu = B/(np.sqrt(bx*bx + by*by + bz*bz))

                                                                        R=np.array([[bu[0]*au[0], bu[0]*au[1], bu[0]*au[2]], [bu[1]*au[0], bu[1]*au[1], bu[1]*au[2]], [bu[2]*au[0], bu[2]*au[1], bu[2]*au[2]] ])


                                                                        return(R)





                                                                        share|cite|improve this answer






















                                                                          up vote
                                                                          0
                                                                          down vote










                                                                          up vote
                                                                          0
                                                                          down vote









                                                                          Kuba Ober and Leyu Wang's answer works great. Here, is a python implementation of the same algorithm.



                                                                          import numpy as np
                                                                          import math


                                                                          def rotation_matrix(A,B):
                                                                          # a and b are in the form of numpy array

                                                                          ax = A[0]
                                                                          ay = A[1]
                                                                          az = A[2]

                                                                          bx = B[0]
                                                                          by = B[1]
                                                                          bz = B[2]

                                                                          au = A/(np.sqrt(ax*ax + ay*ay + az*az))
                                                                          bu = B/(np.sqrt(bx*bx + by*by + bz*bz))

                                                                          R=np.array([[bu[0]*au[0], bu[0]*au[1], bu[0]*au[2]], [bu[1]*au[0], bu[1]*au[1], bu[1]*au[2]], [bu[2]*au[0], bu[2]*au[1], bu[2]*au[2]] ])


                                                                          return(R)





                                                                          share|cite|improve this answer












                                                                          Kuba Ober and Leyu Wang's answer works great. Here, is a python implementation of the same algorithm.



                                                                          import numpy as np
                                                                          import math


                                                                          def rotation_matrix(A,B):
                                                                          # a and b are in the form of numpy array

                                                                          ax = A[0]
                                                                          ay = A[1]
                                                                          az = A[2]

                                                                          bx = B[0]
                                                                          by = B[1]
                                                                          bz = B[2]

                                                                          au = A/(np.sqrt(ax*ax + ay*ay + az*az))
                                                                          bu = B/(np.sqrt(bx*bx + by*by + bz*bz))

                                                                          R=np.array([[bu[0]*au[0], bu[0]*au[1], bu[0]*au[2]], [bu[1]*au[0], bu[1]*au[1], bu[1]*au[2]], [bu[2]*au[0], bu[2]*au[1], bu[2]*au[2]] ])


                                                                          return(R)






                                                                          share|cite|improve this answer












                                                                          share|cite|improve this answer



                                                                          share|cite|improve this answer










                                                                          answered Apr 17 '16 at 10:16









                                                                          Soumya

                                                                          1




                                                                          1




















                                                                              up vote
                                                                              0
                                                                              down vote













                                                                              One way to proceed is as following:



                                                                              Start by constructing one orthonormal basis for each of the vectors $vecn_1$ and $vecn_2$. This can be done by the trick given in an answer to another question [1]. This will result in two transformation matrices



                                                                              $$R_1=beginbmatrixvecu_1 & vecv_1 & vecw_1endbmatrix$$



                                                                              and
                                                                              $$R_2=beginbmatrixvecu_2 & vecv_2 & vecw_2endbmatrix$$



                                                                              Then the rotation matrix for aligning $vecn_1$ onto $vecn_2$ becomes



                                                                              $$R=R_2R_1^Tvecn_1$$



                                                                              [1] https://math.stackexchange.com/q/712065






                                                                              share|cite|improve this answer


























                                                                                up vote
                                                                                0
                                                                                down vote













                                                                                One way to proceed is as following:



                                                                                Start by constructing one orthonormal basis for each of the vectors $vecn_1$ and $vecn_2$. This can be done by the trick given in an answer to another question [1]. This will result in two transformation matrices



                                                                                $$R_1=beginbmatrixvecu_1 & vecv_1 & vecw_1endbmatrix$$



                                                                                and
                                                                                $$R_2=beginbmatrixvecu_2 & vecv_2 & vecw_2endbmatrix$$



                                                                                Then the rotation matrix for aligning $vecn_1$ onto $vecn_2$ becomes



                                                                                $$R=R_2R_1^Tvecn_1$$



                                                                                [1] https://math.stackexchange.com/q/712065






                                                                                share|cite|improve this answer
























                                                                                  up vote
                                                                                  0
                                                                                  down vote










                                                                                  up vote
                                                                                  0
                                                                                  down vote









                                                                                  One way to proceed is as following:



                                                                                  Start by constructing one orthonormal basis for each of the vectors $vecn_1$ and $vecn_2$. This can be done by the trick given in an answer to another question [1]. This will result in two transformation matrices



                                                                                  $$R_1=beginbmatrixvecu_1 & vecv_1 & vecw_1endbmatrix$$



                                                                                  and
                                                                                  $$R_2=beginbmatrixvecu_2 & vecv_2 & vecw_2endbmatrix$$



                                                                                  Then the rotation matrix for aligning $vecn_1$ onto $vecn_2$ becomes



                                                                                  $$R=R_2R_1^Tvecn_1$$



                                                                                  [1] https://math.stackexchange.com/q/712065






                                                                                  share|cite|improve this answer














                                                                                  One way to proceed is as following:



                                                                                  Start by constructing one orthonormal basis for each of the vectors $vecn_1$ and $vecn_2$. This can be done by the trick given in an answer to another question [1]. This will result in two transformation matrices



                                                                                  $$R_1=beginbmatrixvecu_1 & vecv_1 & vecw_1endbmatrix$$



                                                                                  and
                                                                                  $$R_2=beginbmatrixvecu_2 & vecv_2 & vecw_2endbmatrix$$



                                                                                  Then the rotation matrix for aligning $vecn_1$ onto $vecn_2$ becomes



                                                                                  $$R=R_2R_1^Tvecn_1$$



                                                                                  [1] https://math.stackexchange.com/q/712065







                                                                                  share|cite|improve this answer














                                                                                  share|cite|improve this answer



                                                                                  share|cite|improve this answer








                                                                                  edited Apr 13 '17 at 12:21









                                                                                  Community♦

                                                                                  1




                                                                                  1










                                                                                  answered May 2 '16 at 19:14









                                                                                  user877329

                                                                                  1428




                                                                                  1428




















                                                                                      up vote
                                                                                      0
                                                                                      down vote













                                                                                      Given two unit vectors $hat a$ and $hat c$, reflecting a vector $x$ across the orthogonal complement of $hat a$ and then for $hat c$ will rotate the part of $x$ in the span of $hat a$ and $hat c$ by twice the angle from $hat a$ to $hat c$. Letting $hat c = frachat a + hat b$ be the unit vector that bisects $hat a$ and $hat b$, the composition of the two reflections $R_hat c circ R_hat a$ will rotate $hat a$ to $hat b$, and any vector $x$ by the angle from $hat a$ to $hat b$. Recall that the reflection transformation is $R_n(x) = x - 2 fracx cdot nn cdot n n$.






                                                                                      share|cite|improve this answer
























                                                                                        up vote
                                                                                        0
                                                                                        down vote













                                                                                        Given two unit vectors $hat a$ and $hat c$, reflecting a vector $x$ across the orthogonal complement of $hat a$ and then for $hat c$ will rotate the part of $x$ in the span of $hat a$ and $hat c$ by twice the angle from $hat a$ to $hat c$. Letting $hat c = frachat a + hat b$ be the unit vector that bisects $hat a$ and $hat b$, the composition of the two reflections $R_hat c circ R_hat a$ will rotate $hat a$ to $hat b$, and any vector $x$ by the angle from $hat a$ to $hat b$. Recall that the reflection transformation is $R_n(x) = x - 2 fracx cdot nn cdot n n$.






                                                                                        share|cite|improve this answer






















                                                                                          up vote
                                                                                          0
                                                                                          down vote










                                                                                          up vote
                                                                                          0
                                                                                          down vote









                                                                                          Given two unit vectors $hat a$ and $hat c$, reflecting a vector $x$ across the orthogonal complement of $hat a$ and then for $hat c$ will rotate the part of $x$ in the span of $hat a$ and $hat c$ by twice the angle from $hat a$ to $hat c$. Letting $hat c = frachat a + hat b$ be the unit vector that bisects $hat a$ and $hat b$, the composition of the two reflections $R_hat c circ R_hat a$ will rotate $hat a$ to $hat b$, and any vector $x$ by the angle from $hat a$ to $hat b$. Recall that the reflection transformation is $R_n(x) = x - 2 fracx cdot nn cdot n n$.






                                                                                          share|cite|improve this answer












                                                                                          Given two unit vectors $hat a$ and $hat c$, reflecting a vector $x$ across the orthogonal complement of $hat a$ and then for $hat c$ will rotate the part of $x$ in the span of $hat a$ and $hat c$ by twice the angle from $hat a$ to $hat c$. Letting $hat c = frachat a + hat b$ be the unit vector that bisects $hat a$ and $hat b$, the composition of the two reflections $R_hat c circ R_hat a$ will rotate $hat a$ to $hat b$, and any vector $x$ by the angle from $hat a$ to $hat b$. Recall that the reflection transformation is $R_n(x) = x - 2 fracx cdot nn cdot n n$.







                                                                                          share|cite|improve this answer












                                                                                          share|cite|improve this answer



                                                                                          share|cite|improve this answer










                                                                                          answered May 27 '16 at 16:46









                                                                                          Centrinia

                                                                                          211




                                                                                          211




















                                                                                              up vote
                                                                                              0
                                                                                              down vote













                                                                                              Sadly I don't have enough points to comment on the accepted answer but as others have noted, the formula doesn't work when a == -b.



                                                                                              To solve this edge case you have to create a normal vector of a by for example using the formula found here (a,b and c being the components of the vector):




                                                                                              function (a,b,c) 

                                                                                              return c<a ? (b,-a,0) : (0,-c,b)




                                                                                              then make the rotation matrix by rotating vector a around this normal by Pi.






                                                                                              share|cite|improve this answer


























                                                                                                up vote
                                                                                                0
                                                                                                down vote













                                                                                                Sadly I don't have enough points to comment on the accepted answer but as others have noted, the formula doesn't work when a == -b.



                                                                                                To solve this edge case you have to create a normal vector of a by for example using the formula found here (a,b and c being the components of the vector):




                                                                                                function (a,b,c) 

                                                                                                return c<a ? (b,-a,0) : (0,-c,b)




                                                                                                then make the rotation matrix by rotating vector a around this normal by Pi.






                                                                                                share|cite|improve this answer
























                                                                                                  up vote
                                                                                                  0
                                                                                                  down vote










                                                                                                  up vote
                                                                                                  0
                                                                                                  down vote









                                                                                                  Sadly I don't have enough points to comment on the accepted answer but as others have noted, the formula doesn't work when a == -b.



                                                                                                  To solve this edge case you have to create a normal vector of a by for example using the formula found here (a,b and c being the components of the vector):




                                                                                                  function (a,b,c) 

                                                                                                  return c<a ? (b,-a,0) : (0,-c,b)




                                                                                                  then make the rotation matrix by rotating vector a around this normal by Pi.






                                                                                                  share|cite|improve this answer














                                                                                                  Sadly I don't have enough points to comment on the accepted answer but as others have noted, the formula doesn't work when a == -b.



                                                                                                  To solve this edge case you have to create a normal vector of a by for example using the formula found here (a,b and c being the components of the vector):




                                                                                                  function (a,b,c) 

                                                                                                  return c<a ? (b,-a,0) : (0,-c,b)




                                                                                                  then make the rotation matrix by rotating vector a around this normal by Pi.







                                                                                                  share|cite|improve this answer














                                                                                                  share|cite|improve this answer



                                                                                                  share|cite|improve this answer








                                                                                                  edited May 23 '17 at 12:39









                                                                                                  Community♦

                                                                                                  1




                                                                                                  1










                                                                                                  answered Dec 12 '16 at 17:31









                                                                                                  jaga

                                                                                                  101




                                                                                                  101




















                                                                                                      up vote
                                                                                                      -1
                                                                                                      down vote













                                                                                                      I have a simpler method comes from Erigen's "Mechanics of Continua". R is rotational matrix that rotate vector "a" align with vector "b" Matlab Code:



                                                                                                      %%%%%% Rotate vector a align with vector b%%%%%%%%%% 

                                                                                                      syms ax ay az bx by bz k real

                                                                                                      a=[ax ay az]'
                                                                                                      au=a./sqrt(ax^2+ay^2+az^2)

                                                                                                      b=[bx by bz]'
                                                                                                      bu=b./sqrt(bx^2+by^2+bz^2)

                                                                                                      R=[bu(1)*au(1) bu(1)*au(2) bu(1)*au(3);

                                                                                                      bu(2)*au(1) bu(2)*au(2) bu(2)*au(3);

                                                                                                      bu(3)*au(1) bu(3)*au(2) bu(3)*au(3)]


                                                                                                      To verify:



                                                                                                      c=R*a
                                                                                                      cu=c./sqrt(c(1)^2+c(2)^2+c(3)^2)
                                                                                                      simple(bu-cu)


                                                                                                      A zero result means that $c$ (rotated $a$) and $b$ are aligned with each other.



                                                                                                      simple(sqrt(c(1)^2+c(2)^2+c(3)^2)-sqrt(c(1)^2+c(2)^2+c(3)^2))


                                                                                                      A zero result means that $c$ (rotated $a$) and $a$ are of the same length.






                                                                                                      share|cite|improve this answer






















                                                                                                      • Here's a MathJax tutorial :)
                                                                                                        – barto
                                                                                                        Jul 25 '14 at 17:50










                                                                                                      • Note that R = (au*bu')'. Or, simply, $R_ij = hatb_i hata_j$
                                                                                                        – Kuba Ober
                                                                                                        Aug 14 '14 at 18:49










                                                                                                      • Of course this doesn't really work. Say let a=[1 0 0], b=[0 1 0], we should get a simple $90^circ$ rotation around the z axis, with R=[0 1 0; 1 0 0; 0 0 1].
                                                                                                        – Kuba Ober
                                                                                                        Aug 14 '14 at 18:54










                                                                                                      • In all cases the $R$ squishes $a times b$ to zero.
                                                                                                        – Kuba Ober
                                                                                                        Aug 14 '14 at 19:36















                                                                                                      up vote
                                                                                                      -1
                                                                                                      down vote













                                                                                                      I have a simpler method comes from Erigen's "Mechanics of Continua". R is rotational matrix that rotate vector "a" align with vector "b" Matlab Code:



                                                                                                      %%%%%% Rotate vector a align with vector b%%%%%%%%%% 

                                                                                                      syms ax ay az bx by bz k real

                                                                                                      a=[ax ay az]'
                                                                                                      au=a./sqrt(ax^2+ay^2+az^2)

                                                                                                      b=[bx by bz]'
                                                                                                      bu=b./sqrt(bx^2+by^2+bz^2)

                                                                                                      R=[bu(1)*au(1) bu(1)*au(2) bu(1)*au(3);

                                                                                                      bu(2)*au(1) bu(2)*au(2) bu(2)*au(3);

                                                                                                      bu(3)*au(1) bu(3)*au(2) bu(3)*au(3)]


                                                                                                      To verify:



                                                                                                      c=R*a
                                                                                                      cu=c./sqrt(c(1)^2+c(2)^2+c(3)^2)
                                                                                                      simple(bu-cu)


                                                                                                      A zero result means that $c$ (rotated $a$) and $b$ are aligned with each other.



                                                                                                      simple(sqrt(c(1)^2+c(2)^2+c(3)^2)-sqrt(c(1)^2+c(2)^2+c(3)^2))


                                                                                                      A zero result means that $c$ (rotated $a$) and $a$ are of the same length.






                                                                                                      share|cite|improve this answer






















                                                                                                      • Here's a MathJax tutorial :)
                                                                                                        – barto
                                                                                                        Jul 25 '14 at 17:50










                                                                                                      • Note that R = (au*bu')'. Or, simply, $R_ij = hatb_i hata_j$
                                                                                                        – Kuba Ober
                                                                                                        Aug 14 '14 at 18:49










                                                                                                      • Of course this doesn't really work. Say let a=[1 0 0], b=[0 1 0], we should get a simple $90^circ$ rotation around the z axis, with R=[0 1 0; 1 0 0; 0 0 1].
                                                                                                        – Kuba Ober
                                                                                                        Aug 14 '14 at 18:54










                                                                                                      • In all cases the $R$ squishes $a times b$ to zero.
                                                                                                        – Kuba Ober
                                                                                                        Aug 14 '14 at 19:36













                                                                                                      up vote
                                                                                                      -1
                                                                                                      down vote










                                                                                                      up vote
                                                                                                      -1
                                                                                                      down vote









                                                                                                      I have a simpler method comes from Erigen's "Mechanics of Continua". R is rotational matrix that rotate vector "a" align with vector "b" Matlab Code:



                                                                                                      %%%%%% Rotate vector a align with vector b%%%%%%%%%% 

                                                                                                      syms ax ay az bx by bz k real

                                                                                                      a=[ax ay az]'
                                                                                                      au=a./sqrt(ax^2+ay^2+az^2)

                                                                                                      b=[bx by bz]'
                                                                                                      bu=b./sqrt(bx^2+by^2+bz^2)

                                                                                                      R=[bu(1)*au(1) bu(1)*au(2) bu(1)*au(3);

                                                                                                      bu(2)*au(1) bu(2)*au(2) bu(2)*au(3);

                                                                                                      bu(3)*au(1) bu(3)*au(2) bu(3)*au(3)]


                                                                                                      To verify:



                                                                                                      c=R*a
                                                                                                      cu=c./sqrt(c(1)^2+c(2)^2+c(3)^2)
                                                                                                      simple(bu-cu)


                                                                                                      A zero result means that $c$ (rotated $a$) and $b$ are aligned with each other.



                                                                                                      simple(sqrt(c(1)^2+c(2)^2+c(3)^2)-sqrt(c(1)^2+c(2)^2+c(3)^2))


                                                                                                      A zero result means that $c$ (rotated $a$) and $a$ are of the same length.






                                                                                                      share|cite|improve this answer














                                                                                                      I have a simpler method comes from Erigen's "Mechanics of Continua". R is rotational matrix that rotate vector "a" align with vector "b" Matlab Code:



                                                                                                      %%%%%% Rotate vector a align with vector b%%%%%%%%%% 

                                                                                                      syms ax ay az bx by bz k real

                                                                                                      a=[ax ay az]'
                                                                                                      au=a./sqrt(ax^2+ay^2+az^2)

                                                                                                      b=[bx by bz]'
                                                                                                      bu=b./sqrt(bx^2+by^2+bz^2)

                                                                                                      R=[bu(1)*au(1) bu(1)*au(2) bu(1)*au(3);

                                                                                                      bu(2)*au(1) bu(2)*au(2) bu(2)*au(3);

                                                                                                      bu(3)*au(1) bu(3)*au(2) bu(3)*au(3)]


                                                                                                      To verify:



                                                                                                      c=R*a
                                                                                                      cu=c./sqrt(c(1)^2+c(2)^2+c(3)^2)
                                                                                                      simple(bu-cu)


                                                                                                      A zero result means that $c$ (rotated $a$) and $b$ are aligned with each other.



                                                                                                      simple(sqrt(c(1)^2+c(2)^2+c(3)^2)-sqrt(c(1)^2+c(2)^2+c(3)^2))


                                                                                                      A zero result means that $c$ (rotated $a$) and $a$ are of the same length.







                                                                                                      share|cite|improve this answer














                                                                                                      share|cite|improve this answer



                                                                                                      share|cite|improve this answer








                                                                                                      edited Aug 14 '14 at 18:42









                                                                                                      Kuba Ober

                                                                                                      466413




                                                                                                      466413










                                                                                                      answered Jul 25 '14 at 17:25









                                                                                                      Leyu Wang

                                                                                                      71




                                                                                                      71











                                                                                                      • Here's a MathJax tutorial :)
                                                                                                        – barto
                                                                                                        Jul 25 '14 at 17:50










                                                                                                      • Note that R = (au*bu')'. Or, simply, $R_ij = hatb_i hata_j$
                                                                                                        – Kuba Ober
                                                                                                        Aug 14 '14 at 18:49










                                                                                                      • Of course this doesn't really work. Say let a=[1 0 0], b=[0 1 0], we should get a simple $90^circ$ rotation around the z axis, with R=[0 1 0; 1 0 0; 0 0 1].
                                                                                                        – Kuba Ober
                                                                                                        Aug 14 '14 at 18:54










                                                                                                      • In all cases the $R$ squishes $a times b$ to zero.
                                                                                                        – Kuba Ober
                                                                                                        Aug 14 '14 at 19:36

















                                                                                                      • Here's a MathJax tutorial :)
                                                                                                        – barto
                                                                                                        Jul 25 '14 at 17:50










                                                                                                      • Note that R = (au*bu')'. Or, simply, $R_ij = hatb_i hata_j$
                                                                                                        – Kuba Ober
                                                                                                        Aug 14 '14 at 18:49










                                                                                                      • Of course this doesn't really work. Say let a=[1 0 0], b=[0 1 0], we should get a simple $90^circ$ rotation around the z axis, with R=[0 1 0; 1 0 0; 0 0 1].
                                                                                                        – Kuba Ober
                                                                                                        Aug 14 '14 at 18:54










                                                                                                      • In all cases the $R$ squishes $a times b$ to zero.
                                                                                                        – Kuba Ober
                                                                                                        Aug 14 '14 at 19:36
















                                                                                                      Here's a MathJax tutorial :)
                                                                                                      – barto
                                                                                                      Jul 25 '14 at 17:50




                                                                                                      Here's a MathJax tutorial :)
                                                                                                      – barto
                                                                                                      Jul 25 '14 at 17:50












                                                                                                      Note that R = (au*bu')'. Or, simply, $R_ij = hatb_i hata_j$
                                                                                                      – Kuba Ober
                                                                                                      Aug 14 '14 at 18:49




                                                                                                      Note that R = (au*bu')'. Or, simply, $R_ij = hatb_i hata_j$
                                                                                                      – Kuba Ober
                                                                                                      Aug 14 '14 at 18:49












                                                                                                      Of course this doesn't really work. Say let a=[1 0 0], b=[0 1 0], we should get a simple $90^circ$ rotation around the z axis, with R=[0 1 0; 1 0 0; 0 0 1].
                                                                                                      – Kuba Ober
                                                                                                      Aug 14 '14 at 18:54




                                                                                                      Of course this doesn't really work. Say let a=[1 0 0], b=[0 1 0], we should get a simple $90^circ$ rotation around the z axis, with R=[0 1 0; 1 0 0; 0 0 1].
                                                                                                      – Kuba Ober
                                                                                                      Aug 14 '14 at 18:54












                                                                                                      In all cases the $R$ squishes $a times b$ to zero.
                                                                                                      – Kuba Ober
                                                                                                      Aug 14 '14 at 19:36





                                                                                                      In all cases the $R$ squishes $a times b$ to zero.
                                                                                                      – Kuba Ober
                                                                                                      Aug 14 '14 at 19:36


















                                                                                                       

                                                                                                      draft saved


                                                                                                      draft discarded















































                                                                                                       


                                                                                                      draft saved


                                                                                                      draft discarded














                                                                                                      StackExchange.ready(
                                                                                                      function ()
                                                                                                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f180418%2fcalculate-rotation-matrix-to-align-vector-a-to-vector-b-in-3d%23new-answer', 'question_page');

                                                                                                      );

                                                                                                      Post as a guest













































































                                                                                                      這個網誌中的熱門文章

                                                                                                      How to combine Bézier curves to a surface?

                                                                                                      Carbon dioxide

                                                                                                      Why am i infinitely getting the same tweet with the Twitter Search API?