################################################################################
#    COPYRIGHT Fosun Proxima, 2017
#    All rights are reserved. Reproduction in whole or in part is   
#    prohibited without the written consent of the copyright owner. 
#
#    primary author(s):
#        Dr. LIU Weiping        Liuwp@fosun.com
#
#    file history:
#        7st Mar, 2018 generated by LIU Weiping
#        24st Jan, 2019 revised by LIU Weiping
#    affiliation(s):
#        Fosun Proxima
#
#    introduction:
#        volume Resample
################################################################################
import SimpleITK as sitk
import math

def LinearResample(image, outputspacing,pad_value=-1024):
    #image = image.astype(dtype=float)
    OriginalSize = image.GetSize()
    OriginalSpacing = image.GetSpacing()

    transform = sitk.Transform()
    transform.SetIdentity()
    resampler = sitk.ResampleImageFilter()
    resampler.SetInterpolator(sitk.sitkLinear)
    resampler.SetDefaultPixelValue(pad_value)
    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)
    return outimgsitk,OriginalSpacing, OriginalSize#, ori, dirction
    
def NearestResample(itk_image, outputspacing,target_shape=None):
    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 target_shape is not None:
        outsize = target_shape
    else:
        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])))
    resampler.SetSize(outsize)
    outimgsitk = resampler.Execute(image)
    return outimgsitk#,outsize,ori,dirction
    
##
def LinearResample4DiffDicomLoad(foldername, maskSpacing, outputspacing):
    reader = sitk.ImageSeriesReader()
    dicom_names = reader.GetGDCMSeriesFileNames( foldername )
    reader.SetFileNames(dicom_names)
    image = reader.Execute()
    #image = image.astype(dtype=float)
    maskspacingreverse = (maskSpacing[2],maskSpacing[1],maskSpacing[0])
    image.SetSpacing(maskspacingreverse)
    OriginalSize = image.GetSize()
    #print(size)
    OriginalSpacing = image.GetSpacing()
    #print(spacing)
    #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
