;+ ; NAME: ; ROTMATRIX_TO_EULER ; ; PURPOSE: ; Takes a rotation matrix as input, and returns the associated Euler angles. ; ; CALLING SEQUENCE: ; euler = ROTMATRIX_TO_EULER( matrix[, /ALTERNATE, /DEGREE] ) ; ; INPUTS: ; MATRIX = A 3-dimensional, 3x3 rotation matrix. ; ; OPTIONAL INPUT KEYWORD: ; /ALTERNATE - There are generally two sets of Euler angles that can represent the same rotation matrix. Use this keyword to get the second set of Euler angles. ; /DEGREE - Use this keyword to get Euler angle in degrees (default is radians). ; ; OUTPUTS: ; EULER - A 3-elements array containing, respectively, the psi, theta and phi Euler angles (also called alpha, beta, gamma). ; ; NOTE: ; ; By default, the Euler angles are returned in radians. ; ; PROCEDURES USED: ; DETERM() ; ; MODIFICATION HISTORY: ; WRITTEN, Jonathan Gagne, August, 21 2013 ;- Function rotmatrix_to_euler, matrix, ALTERNATE=alternate, DEGREE=degree ;Output error message if matrix is not a rotation matrix if determ(matrix) ne 1 then message, ' This matrix has a non-unity determinant, and hence is not a rotation matrix !' if keyword_set(degree) then factor = 180./!dpi else $ factor = 1. ;If THETA is not ±90 degrees, there are two possible solutions if abs(matrix[0,2]) ne 1. then begin ;Find two possibilities for angle Theta t1 = - asin(matrix[0,2]);mat_rot_uvw t2 = !pi - t1 ;Find two corresponding possibilities for angle Psi ps1 = atan((matrix[1,2]/cos(t1))/(matrix[2,2]/cos(t1))) ps2 = atan((matrix[1,2]/cos(t2))/(matrix[2,2]/cos(t2))) ;Find two corresponding possibilities for angle Phi ph1 = atan(matrix[0,1]/cos(t1),matrix[0,0]/cos(t1)) ph2 = atan(matrix[0,1]/cos(t2),matrix[0,0]/cos(t2)) ;Choose the combination with smallest psi if ps2 le ps1 then begin ;Store alternate solution alternate = [ps2,t2,ph2]*factor;mat_rot_uvw ;Return chosen solution return, [ps1,t1,ph1]*factor endif else begin ;Store alternate solution alternate = [ps1,t1,ph1]*factor ;Return chosen solution return, [ps2,t2,ph2]*factor endelse endif else begin ;Phi can be set to anything here phi = 0. ;Treat separately the cases where theta is 90 or -90 degrees if matrix[0,2] eq -1. then begin theta = !pi/2. psi = phi + atan(matrix[1,0],matrix[2,0]) endif else begin theta = -!pi/2. psi = -phi + atan(-matrix[1,0],-matrix[2,0]) endelse ;Return computed values return, [psi,theta,phi]*factor endelse End