from __future__ import division

import logging

import SimpleITK as sitk
import numpy as np


def sitk_get_image_parameters(volume):
    return (np.array(volume.GetSpacing()), 
            np.array(volume.GetSize()), 
            np.array(volume.GetOrigin()))


def sitk_get_center(volume):
    _, size, _ = sitk_get_image_parameters(volume)
    return (size - 1) / 2


def sitk_get_size_parity(volume):
    _, size, _ = sitk_get_image_parameters(volume)
    return np.mod(size, 2) 


def sitk_get_diagonal_length(volume):
    _, size, _ = sitk_get_image_parameters(volume)
    return np.linalg.norm(size)


def sitk_paste_into_center(smaller, larger):
  
    smaller_parities = sitk_get_size_parity(smaller)
    larger_parities = sitk_get_size_parity(larger)
    if not np.allclose(smaller_parities, larger_parities):
        logging.warn('parities differ, result will not be centered : {0}, {1}'.format(smaller_parities, larger_parities))

    smaller_center = sitk_get_center(smaller)
    larger_center = sitk_get_center(larger)

    offset = np.around(larger_center - smaller_center).astype(int).tolist()
    return sitk.Paste(larger, smaller, smaller.GetSize(), [0, 0, 0,], offset)
