import numpy as np

def rotate_2d(img,angels_deg):
    
    angels_deg=np.array(angels_deg)
    
    T=[[0,0,0,1,2,3,0,0,0,0,0,0],[0,0,90,2,1,3,0,1,0,0,2,1],[0,0,180,1,2,3,1,1,0,2,2,2],[0,0,270,2,1,3,1,0,0,2,0,3],
       [0,90,0,3,2,1,1,0,0,1,1,1],[0,90,90,2,3,1,0,0,0,1,1,2],[0,90,180,3,2,1,0,1,0,1,1,3],[0,90,270,2,3,1,1,1,0,1,1,0],
       [0,180,0,1,2,3,1,0,1,0,2,2],[0,180,90,2,1,3,0,0,1,2,2,3],[0,180,180,1,2,3,0,1,1,2,0,0],[0,180,270,2,1,3,1,1,1,0,0,1],
       [0,270,0,3,2,1,0,0,1,3,3,1],[0,270,90,2,3,1,0,1,1,3,3,2],[0,270,180,3,2,1,1,1,1,3,3,3],[0,270,270,2,3,1,1,0,1,3,3,0],
       [90,0,0,1,3,2,0,0,1,1,0,0],[90,0,90,3,1,2,0,1,1,0,1,1],[90,0,180,1,3,2,1,1,1,1,2,2],[90,0,270,3,1,2,1,0,1,2,1,3],
       [90,90,0,2,3,1,0,0,0,1,1,2],[90,90,90,3,2,1,0,1,0,1,1,3],[90,90,180,2,3,1,1,1,0,1,1,0],[90,90,270,3,2,1,1,0,0,1,1,1],
       [90,180,0,1,3,2,1,0,0,3,2,2],[90,180,90,3,1,2,0,0,0,2,3,3],[90,180,180,1,3,2,0,1,0,3,0,0],[90,180,270,3,1,2,1,1,0,0,3,1],
       [90,270,0,2,3,1,1,0,1,3,3,0],[90,270,90,3,2,1,0,0,1,3,3,1],[90,270,180,2,3,1,0,1,1,3,3,2],[90,270,270,3,2,1,1,1,1,3,3,3],
       [180,0,0,1,2,3,0,1,1,2,0,0],[180,0,90,2,1,3,1,1,1,0,0,1],[180,0,180,1,2,3,1,0,1,0,2,2],[180,0,270,2,1,3,0,0,1,2,2,3],
       [180,90,0,3,2,1,0,1,0,1,1,3],[180,90,90,2,3,1,1,1,0,1,1,0],[180,90,180,3,2,1,1,0,0,1,1,1],[180,90,270,2,3,1,0,0,0,1,1,2],
       [180,180,0,1,2,3,1,1,0,2,2,2],[180,180,90,2,1,3,1,0,0,2,0,3],[180,180,180,1,2,3,0,0,0,0,0,0],[180,180,270,2,1,3,0,1,0,0,2,1],
       [180,270,0,3,2,1,1,1,1,3,3,3],[180,270,90,2,3,1,1,0,1,3,3,0],[180,270,180,3,2,1,0,0,1,3,3,1],[180,270,270,2,3,1,0,1,1,3,3,2],
       [270,0,0,1,3,2,0,1,0,3,0,0],[270,0,90,3,1,2,1,1,0,0,3,1],[270,0,180,1,3,2,1,0,0,3,2,2],[270,0,270,3,1,2,0,0,0,2,3,3],
       [270,90,0,2,3,1,1,1,0,1,1,0],[270,90,90,3,2,1,1,0,0,1,1,1],[270,90,180,2,3,1,0,0,0,1,1,2],[270,90,270,3,2,1,0,1,0,1,1,3],
       [270,180,0,1,3,2,1,1,1,1,2,2],[270,180,90,3,1,2,1,0,1,2,1,3],[270,180,180,1,3,2,0,0,1,1,0,0],[270,180,270,3,1,2,0,1,1,0,1,1],
       [270,270,0,2,3,1,0,1,1,3,3,2],[270,270,90,3,2,1,1,1,1,3,3,3],[270,270,180,2,3,1,1,0,1,3,3,0],[270,270,270,3,2,1,0,0,1,3,3,1]]

    T=np.array(T)
    rots_3d=T[:,0:3]

    for k in range(rots_3d.shape[0]):
        if all(rots_3d[k,:]==angels_deg):
            ind=k
            break

    rot_3d=rots_3d[ind,:]

    zrc1=T[ind,6]
    zrc2=T[ind,7]
    zrc3=T[ind,8]
    
    rot1=T[ind,9]
    rot2=T[ind,10]
    rot3=T[ind,11]
    
    
    p=T[ind,3:6]-1
    
    img=img[:,:,p]

    if zrc1==1:
        img[:,:,0]=np.fliplr(img[:,:,0]); 
    if zrc2==1:
        img[:,:,1]=np.fliplr(img[:,:,1]);
    if zrc3==1:
        img[:,:,2]=np.fliplr(img[:,:,2]);

    
    
    img[:,:,0]=np.rot90(img[:,:,0],rot1)
    img[:,:,1]=np.rot90(img[:,:,1],rot2)
    img[:,:,2]=np.rot90(img[:,:,2],rot3)
    
    return img



def rotate_3d(img,angels_deg):
    
    a,b,c = angels_deg
    
    img=np.rot90(img,int(a/90),axes=(1,2))
    img=np.rot90(img,int(b/90),axes=(0,2))
    img=np.rot90(img,int(c/90),axes=(0,1))
    
    return img
    


def rotate_3d_inverse(img,angels_deg):
    
    a,b,c = angels_deg
    a,b,c = -int(a/90),-int(b/90),-int(c/90)
    a,b,c=a%4,b%4,c%4
    
    
    img=np.rot90(img,c,axes=(0,1))
    img=np.rot90(img,b,axes=(0,2))
    img=np.rot90(img,a,axes=(1,2))
    

    
    return img




def flip_2d(img):
    
    
    img[:,:,0]=np.flipud(img[:,:,0])
    img[:,:,2]=np.fliplr(img[:,:,2])
    
    return img