Home > atmlab > math > rotationmat3D.m

rotationmat3D

PURPOSE ^

function R= rotationmat3D(radians,Axis)

SYNOPSIS ^

function R= rotationmat3D(r,Axis)

DESCRIPTION ^

function R= rotationmat3D(radians,Axis)

 creates a rotation matrix such that R * x 
 operates on x by rotating x around the origin r radians around line
 connecting the origin to the point "Axis"

 example:
 rotate around a random direction a random amount and then back
 the result should be an Identity matrix

r = rand(4,1);
rotationmat3D(r(1),[r(2),r(3),r(4)]) * rotationmat3D(-r(1),[r(2),r(3),r(4)])

 example2: 
 rotate around z axis 45 degrees
 Rtest = rotationmat3D(pi/4,[0 0 1])

Bileschi 2009

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

rotationmat3D.m

SOURCE CODE ^

0001 function R= rotationmat3D(r,Axis)
0002 %function R= rotationmat3D(radians,Axis)
0003 %
0004 % creates a rotation matrix such that R * x
0005 % operates on x by rotating x around the origin r radians around line
0006 % connecting the origin to the point "Axis"
0007 %
0008 % example:
0009 % rotate around a random direction a random amount and then back
0010 % the result should be an Identity matrix
0011 %
0012 %r = rand(4,1);
0013 %rotationmat3D(r(1),[r(2),r(3),r(4)]) * rotationmat3D(-r(1),[r(2),r(3),r(4)])
0014 %
0015 % example2:
0016 % rotate around z axis 45 degrees
0017 % Rtest = rotationmat3D(pi/4,[0 0 1])
0018 %
0019 %Bileschi 2009
0020 
0021 if nargin == 1
0022    if(length(rotX) == 3)
0023       rotY = rotX(2);
0024       rotZ = rotZ(3);
0025       rotX = rotX(1);
0026    end
0027 end
0028 
0029 % useful intermediates
0030 L = norm(Axis);
0031 if (L < eps)
0032    error('axis direction must be non-zero vector');
0033 end
0034 Axis = Axis / L;
0035 L = 1;
0036 u = Axis(1);
0037 v = Axis(2);
0038 w = Axis(3);
0039 u2 = u^2;
0040 v2 = v^2;
0041 w2 = w^2;
0042 c = cos(r);
0043 s = sin(r);
0044 %storage
0045 R = nan(3);
0046 %fill
0047 R(1,1) =  u2 + (v2 + w2)*c;
0048 R(1,2) = u*v*(1-c) - w*s;
0049 R(1,3) = u*w*(1-c) + v*s;
0050 R(2,1) = u*v*(1-c) + w*s;
0051 R(2,2) = v2 + (u2+w2)*c;
0052 R(2,3) = v*w*(1-c) - u*s;
0053 R(3,1) = u*w*(1-c) - v*s;
0054 R(3,2) = v*w*(1-c)+u*s;
0055 R(3,3) = w2 + (u2+v2)*c;

Generated on Mon 15-Sep-2014 13:31:28 by m2html © 2005