Euler 3D rotation between two vectors
181 views (last 30 days)
Show older comments
Hello everyone,
I would like to obtain the Euler angles needed to rotate a vector u = (0,0,1) to a vector v, defined between an arbitrary point (x,y,z) and the origin (0,0,0). So v = (x,y,z). I am currently working with a right-handed 3d coordinate system, where X is pointing to the right side of the current reader, Y is pointing upwards and therefore Z is pointing outside the screen of the computer.
I calculate the angles needed to rotate by taking as a reference axis Y -> X -> Z. I have tried to use Euler angles directly, but somehow the objects that I rotate end up in a different location that they should be. Searching on the internet, I have found loads of information about "rotation matrixes" and "quartenions", but I am lost about how to apply them to my case. Any help would be appreciated :)
EDIT: After some tinkering and searching on internet I managed to obtain an answer for a XYZ coordinated system. Still not useful for me, but maybe other people can profit from it.
a = [0 0 1].';
b = [0 5 0].';
% Method described in
% https://math.stackexchange.com/questions/180418/calculate-rotation-matrix-to-align-vector-a-to-vector-b-in-3d
a = a./norm(a);
b = b./norm(b);
v = cross(a,b);
vx = [0 -v(3) v(2) ; v(3) 0 -v(1); -v(2) v(1) 0 ];
c = dot(a,b);
I = eye(3);
R=I+vx+vx^2*(1/(1+c));
R = round(R,5);
% From Rot matrix to euler coordinates, follows XYZ, described in:
% http://www.gregslabaugh.net/publications/euler.pdf
if (R(3,1) ~=1) && (R(3,1) ~=-1)
theta_1 = -asin(R(3,1));
theta_2 = pi-theta_1;
chi_1 = atan2((R(3,2)/cos(theta_1)),(R(3,3)/cos(theta_1)));
chi_2 = atan2((R(3,2)/cos(theta_2)),(R(3,3)/cos(theta_2)));
phi_1 = atan2((R(2,1)/cos(theta_1)),(R(1,1)/cos(theta_1)));
phi_2 = atan2((R(2,1)/cos(theta_2)),(R(1,1)/cos(theta_2)));
theta = min(theta_1,theta_2);
chi = min(chi_1,chi_2);
phi = min(phi_1,phi_2);
else
phi = 0;
if R(3,1) == -1
theta = pi/2;
chi = phi+atan2(R(1,2),R(1,3));
else
theta = -pi/2;
chi = -phi+atan2(-R(1,2),-R(1,3));
end
end
theta = rad2deg(theta)
chi = rad2deg(chi)
phi = rad2deg(phi)
2 Comments
Accepted Answer
darova
on 29 Nov 2019
4 Comments
Chad
on 14 Apr 2022
Dear All,
Thank you for putting this example together. I am close to fully understanding this example but have some questions. say I start with a vector at:
theta1=0
phi1=0
I get the x,y,z from this theta_1,phi_1
s1 = cosd(theta_1)*sind(phi_1)
s2 = sind(theta_1)*sind(phi_1)
s3 = cosd(phi_1)
Now I want to rotate to a new theta_2,phi_2
theta2=90
phi2=135
t1 = cosd(theta_2)*sind(phi_2)
t2 = sind(theta_2)*sind(phi_2)
t3 = cosd(phi_2)
a = [s1 s2 s3];
b = [t1 t2 t3];
a = a./norm(a);
b = b./norm(b);
Now the process to generate the Rotation Matrix
th = acos(dot(a,b)); % angle between vectors
u = cross(a,b); % vector of rotation
u = u/norm(u);
K = [0 -u(3) u(2)
u(3) 0 -u(1)
-u(2) u(1) 0];
R = @(t) eye(3) + sin(t)*K + (1-cos(t))*K^2; % rotation matrix
Here is my question. How do I obtain the "data to rotate" below?
x = [-1 1 0 0]; % data to rotate
y = [0 0 -1 1];
z = x*0 + 1;
Is there a way to generate this from s1,s2,s3 above?
Now the x,y,z is rotated.
v = R(th)*[x;y;z]; % rotate data
[x1,y1,z1] = deal( v(1,:),v(2,:),v(3,:) );
My second question is how do I convert x1,y1,z1 to my theta_2,phi_2?
Any help is appreaciated.
CJ
More Answers (0)
See Also
Categories
Find more on Graphics Object Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!