import os
import sys
import glob
import math
import numpy as np
import pandas as pd
import SimpleITK as sitk


def npyBsplineResample(npyImage, inSpacing, outSpacing, interpolator='linear', order=3):
    
    itkImage = sitk.GetImageFromArray(npyImage)
    itkImage.SetSpacing(inSpacing)
    
    resampler = sitk.ResampleImageFilter()
    resampler.SetOutputSpacing(outSpacing)
    resampler.SetDefaultPixelValue(0)
    resampler.SetInterpolator(sitk.sitkBSpline)
    
    identity = sitk.Transform(itkImage.GetDimension(), sitk.sitkIdentity)
    resampler.SetTransform(identity)
    
    inSize = np.array(itkImage.GetSize(), dtype=np.int)
    inSpacing = itkImage.GetSpacing()
    outSize = inSize * (np.array(inSpacing) / np.array(outSpacing))
    outSize = outSize.astype(np.int) #  Image dimensions are in integers
    outSize = [int(s) for s in outSize]
    resampler.SetSize(outSize)

    outImage = resampler.Execute(itkImage)
    outImage = sitk.GetArrayFromImage(outImage)
    return outImage

def NearestResample(itk_image, outputspacing,outsize=None):
    outputspacing = [float(val) for val in outputspacing]
    image = itk_image
    #image = image.astype(dtype=float)
    size = image.GetSize()
    #print(size)
    spacing = image.GetSpacing()
    transform = sitk.Transform()
    transform.SetIdentity()
    resampler = sitk.ResampleImageFilter()
    resampler.SetInterpolator(sitk.sitkNearestNeighbor)
    resampler.SetDefaultPixelValue(0)
    resampler.SetTransform(transform)
    resampler.SetOutputSpacing(outputspacing)
    resampler.SetOutputDirection(image.GetDirection())
    resampler.SetOutputOrigin(image.GetOrigin())
    if outsize is None:
        outsize = (int(math.ceil(size[0]*spacing[0]/outputspacing[0])),\
                          int(math.ceil(size[1]*spacing[1]/outputspacing[1])),\
                         int(math.ceil(size[2]*spacing[2]/outputspacing[2])))
    #print(outsize)
    resampler.SetSize(outsize)
    outimgsitk = resampler.Execute(image)
    return outimgsitk#,outsize,ori,dirction

def LinearResample(image, outputspacing):
    #image = image.astype(dtype=float)
    # print ('enter',type(image))
    OriginalSize = image.GetSize()
    # print(OriginalSize)
    OriginalSpacing = image.GetSpacing()
    # print(OriginalSpacing)
    #ori = image.GetOrigin()
    #dirction = image.GetDirection()
    transform = sitk.Transform()
    transform.SetIdentity()
    resampler = sitk.ResampleImageFilter()
    resampler.SetInterpolator(sitk.sitkLinear)
    resampler.SetDefaultPixelValue(0)
    resampler.SetTransform(transform)
    resampler.SetOutputSpacing(outputspacing)
    resampler.SetOutputDirection(image.GetDirection())
    resampler.SetOutputOrigin(image.GetOrigin())
    outsize = (int(math.ceil(OriginalSize[0]*OriginalSpacing[0]/outputspacing[0])),\
                      int(math.ceil(OriginalSize[1]*OriginalSpacing[1]/outputspacing[1])),\
                     int(math.ceil(OriginalSize[2]*OriginalSpacing[2]/outputspacing[2])))
    resampler.SetSize(outsize)
    outimgsitk = resampler.Execute(image)
    #writer = sitk.ImageFileWriter()
    #writer.SetFileName("D:/Data/LUNG/fissure/chenbotian/Resample.nii")
    #writer.Execute(outimgsitk)
    return outimgsitk,OriginalSpacing, OriginalSize#, ori, dirction