Friday, November 28, 2008

rotation matrix to do basis transformation

Hi, there is a small matlab code that help you do linear
transformation of basis in 3D. I share it with you.

% Rotation of basis in three-dimensional cartesian coordinate
function [y]=rot3d(x,lon,lat)

% x is a 3*1 vector in old cartesian coordinate: xyz
% y is new 3*1 vector in new coordinate
% the rotation of the new coordinate composite two rotations
% lon is the longitude of the pole and lat is the latitude of it.
% g is the first rotation of xy plane around z axis. xyz -> x'y'z
% b is the second rotation of xz plane around x' axis. x'y'z -> x'y''z'
% each ' denote a change of direction in old coordinate

% lat and lon is in degree
% option: plot the two coordinate systems in 3D
% output the rotation matrix: A 3*3
% y = A*x

% reference: http://mathworld.wolfram.com/RotationMatrix.html
% http://en.wikipedia.org/wiki/Rotation_matrix

% Xiaopeng, Nov 28 2008.

% This code can be used to do coordinate rotation in 3D cartesian.

b=lat-90;
g=lon+90;

Rx=[1 0 0; 0 cosd(b) -sind(b); 0 sind(b) cosd(b)] % around x conterclockwise facing toward x
% Ry=[cosd(lat) 0 -sind(lat); 0 1 0; sind(lat) 0 cosd(lat)]; % around y
% conterclockwise facing toward y
Rz=[cosd(g) sind(g) 0; -sind(g) cosd(g) 0; 0 0 1] % around z conterclockwise facing toward z

A=Rx*Rz

% A*A'

y=A*x


% as a test try to rotate a pole of vector: x=[cosd(32)*cosd(102) ...
% cosd(32)*sind(102) sind(32)] to [0 0 1]


% visualize it
vec = [cosd(lat)*cosd(lon) cosd(lat)*sind(lon) sind(lat)];

figure,quiver3(0,0,0,vec(1),vec(2),vec(3))

axis([-1 1 -1 1 -1 1])
xlabel('x') % greenwich longitude=0
ylabel('y') % longitude = 90E
zlabel('z') % north pole
title('pole of rotation')

view(60,60)

end

0 comments: