function f = EulerToMatrix

%  This program for MatLab was originally written by Casper Thorning at the Riso Lab (Denmark)
%  please acknowledge him in any public use
%  modified by AD Rollett to perform conversion from Eulers to matrix.

%Initial Euler angles
%global InitEuler = zeros(1,3);
    disp('This program calculates the orientation matrix from input as Euler angles.');

InitEuler = input('Specify initial Euler angles [phi1,PHI,phi2] in degrees: ');
InitEuler

%initial orientation matrix 
InitOrient = EuToG(InitEuler(1,1)*pi/180, InitEuler(1,2)*pi/180, InitEuler(1,3)*pi/180);  

% Symmetry operation matrix 
global e; 
e(:,:,1)=[1 0 0; 0 1 0; 0 0 1]; e(:,:,2)=[1 0 0;0 -1 0;0 0 -1]; e(:,:,3)=[1 0 0;0 0 -1;0 1 0]; e(:,:,4)=[1 0 0;0 0 1; 0 -1 0];
e(:,:,5)=[-1 0 0;0 1 0;0 0 -1]; e(:,:,6)=[-1 0 0;0 -1 0;0 0 1]; e(:,:,7)=[-1 0 0;0 0 -1;0 -1 0]; e(:,:,8)=[-1 0 0;0 0 1;0 1 0];
e(:,:,9)=[0 1 0;-1 0 0;0 0 1]; e(:,:,10)=[0 1 0;0 0 -1;-1 0 0]; e(:,:,11)=[0 1 0;1 0 0;0 0 -1]; e(:,:,12)=[0 1 0;0 0 1;1 0 0];
e(:,:,13)=[0 -1 0;1 0 0;0 0 1]; e(:,:,14)=[0 -1 0;0 0 -1;1 0 0]; e(:,:,15)=[0 -1 0;-1 0 0;0 0 -1]; e(:,:,16)=[0 -1 0;0 0 1;-1 0 0];
e(:,:,17)=[0 0 1;0 1 0;-1 0 0]; e(:,:,18)=[0 0 1;1 0 0;0 1 0]; e(:,:,19)=[0 0 1;0 -1 0;1 0 0]; e(:,:,20)=[0 0 1;-1 0 0;0 -1 0];
e(:,:,21)=[0 0 -1;0 1 0;1 0 0]; e(:,:,22)=[0 0 -1;-1 0 0;0 1 0]; e(:,:,23)=[0 0 -1;0 -1 0;-1 0 0]; e(:,:,24)=[0 0 -1;1 0 0;0 -1 0];

fprintf( '%.4g %.4g %.4g \n',InitOrient(1,:)'); 
fprintf( '%.4g %.4g %.4g \n',InitOrient(2,:)'); 
fprintf( '%.4g %.4g %.4g \n\n',InitOrient(3,:)'); 

printmat(InitOrient, 'Orientation Matrix');

vector = input('Enter the vector to be transformed as [x,y,z]: ') ;

   NewVector = MatVector( InitOrient , vector ) ;
   fprintf('New, transformed vector = %f, %f, %f \n',NewVector) ;

   
end

function g = EuToG(phi1,phi,phi2)
% Transform a orientation from [phi1, phi, phi2] (in radians) to a matrix

%u=g(1,1);r=g(1,2);h=g(1,3);
%v=g(2,1);s=g(2,2);k=g(2,3);
%w=g(3,1);t=g(3,2);l=g(3,3);
g = [cos(phi1)*cos(phi2)-sin(phi1)*sin(phi2)*cos(phi), sin(phi1)*cos(phi2)+cos(phi1)*sin(phi2)*cos(phi), sin(phi2)*sin(phi);
    -cos(phi1)*sin(phi2)-sin(phi1)*cos(phi2)*cos(phi), -sin(phi1)*sin(phi2)+cos(phi1)*cos(phi2)*cos(phi),cos(phi2)*sin(phi);
    sin(phi1)*sin(phi), -cos(phi1)*sin(phi), cos(phi)];

end

function newV = MatVector(matrix , vector)

newV = [ matrix(1,1)*vector(1)+matrix(1,2)*vector(2)+matrix(1,3)*vector(3),
    matrix(2,1)*vector(1)+matrix(2,2)*vector(2)+matrix(2,3)*vector(3),
    matrix(3,1)*vector(1)+matrix(3,2)*vector(2)+matrix(3,3)*vector(3) ] ;
end


