1function rot = makeRotation( varargin )
2% makeRotation Create a rotation extension object
3% makeRotation( phi ) - rotation about Z (in plane), angle in rad
4% makeRotation( phi, theta ) - rotation on a sphere, angles in rad
5% makeRotation( axis, angle ) - rotation about a given 3D vector by a given angle in rad
6% makeRotation( quaternion ) - rotation defined by a unit quaternion
7% makeRotation( rot_mat ) - rotation defined by a 3x3 rotation matrix
8if nargin<1
9 error('makeRotation:invalidArguments','makeRotation - invalid arguments: must supply rotation parameter(s)');
10end
11switch numel(varargin{1})
12 case 1
13 phi = varargin{1};
14 if nargin<2
15 theta = 0.0;
16 else
17 theta = varargin{2};
18 end
19 assert( ( phi >= -pi ) && ( phi < 2*pi) , 'makeRotation:invalidTheta',...
20 'rotation angle phi (%.2f) is invalid. should be within [-pi,2*pi] radians',phi);
21 assert( ( theta >= -pi ) && ( theta <= pi) , 'makeRotation:invalidPhi',...
22 'rotation angle theta (%.2f) is invalid. should be within [-pi,pi] radians',theta);
23 q1=[cos(theta/2) 0 sin(theta/2) 0]; % y axis
24 q2=[cos(phi/2) 0 0 sin(phi/2)]; % z axis
25 rot.rotQuaternion=mr.aux.quat.multiply(q2,q1); % ok, looks like the order is right
26 case 3
27 v=varargin{1};
28 v=v./sqrt(sum(v.^2));
29 assert(nargin>1);
30 phi = varargin{2};
31 assert( ( abs(phi) >= 0 ) & ( abs(phi) <= pi) , 'makeRotation:invalidPhi',...
32 'rotation angle phi (%.2f) is invalid. should be within [0,pi] radians',phi);
33 rot.rotQuaternion=[cos(phi/2) sin(phi/2)*v];
34 case 4
35 rot.rotQuaternion=mr.aux.quat.normalize(varargin{1});
36 case 9
37 assert(all(size(varargin{1})==[3 3]));
38 rot.rotQuaternion=mr.aux.quat.fromRotMat(varargin{1});
39 otherwise
40 error('unexpected input to makeRotation');
41end
43rot.type = 'rot3D';
45end