function r = vrrotmat2vec(R)
	% implementation of vrrotmat2vec(Q)
	% similar to the MATLAB Simulink 3D Animation toolbox
	%
	% r		= vrrotmat2vec(Q)
	% returns an axis-angle representation r of the rotation Q
	%
	% output:	r			axis-angle rotation vector
	%				r(1:3)	unit vector of rotation
	%				r(4)		angle in radians to rotate
	% input:		Q			rotation matrix
	
	% axis is the eigenvector of R with eigenvalue 1 (or closest to 1)
	[Q,D]		= eig(R);
	[~,idx]	= min(abs(diag(D) - 1));
	eu			= Q(:,idx);
	
	% angle is given by
	cost		= (trace(R) - 1) / 2;
	% care must be taken to match sign of angle to chosen axis
	usint		= [R(3,2) - R(2,3);
					R(1,3) - R(3,1);
					R(2,1) - R(1,2)]/2;
	sint		= eu(:)' * usint(:);
	theta		= atan2(sint, cost);
	r			= [eu(:); theta];
end
