# Copyright 2018 Diamond Light Source Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
.. module:: visual_hulls_recon
   :platform: Unix
   :synopsis: simple visual hulls reconstruction

.. moduleauthor:: Mark Basham <scientificsoftware@diamond.ac.uk>

"""

import logging
import numpy as np

from scipy.ndimage.filters import median_filter
from savu.plugins.reconstructions.base_recon import BaseRecon
from savu.data.plugin_list import CitationInformation
from savu.plugins.driver.cpu_plugin import CpuPlugin
from savu.plugins.utils import register_plugin


@register_plugin
class VisualHullsRecon(BaseRecon, CpuPlugin):
    """
    A Plugin to reconstruct an image by filter back projection
    using the inverse radon transform from scikit-image.

    :param threshold: threshold to binarize the input sinogram. Default: 0.5.

    :~param outer_pad: Not required. Default: False.
    :~param centre_pad: Not required. Default: False.
    """

    def __init__(self):
        logging.debug("initialising Scikitimage Filter Back Projection")
        logging.debug("Calling super to make sure that all superclasses are " +
                      " initialised")
        super(VisualHullsRecon,
              self).__init__("VisualHullsRecon")

    def _mapping_array(self, array_shape, center, theta):
        x, y = np.meshgrid(np.arange(-center, array_shape[0] - center),
                           np.arange(-center, array_shape[1] - center))
        return x*np.cos(theta) - y*np.sin(theta)

    def _recon_hull(self, sino, centre, angles):
        data_shape = (sino.shape[1], sino.shape[1])
        full = np.ones(data_shape)
        for i in range(len(angles)):
            mapping_array = self._mapping_array(data_shape, centre, np.deg2rad(angles[i]))
            mapping_array = np.clip(mapping_array.astype('int')+centre, 0,
                                    sino.shape[1]-1).astype('int')
            mask = sino[i, :][mapping_array]
            full -= 1-mask
        data_range = full.max() - full.min()
        full += data_range // 4
        full[full < 0.5] = 0
        return full

    def _binarize_sinogram(self, input_sinogram, threshold):
        sino = np.zeros_like(input_sinogram)
        sino[input_sinogram > threshold] = 1
        # as this is a simple routine, do a quick median filter to
        # get rid of any stray pixels in the binarization.
        sino = median_filter(sino, size=3)
        return sino

    def process_frames(self, data):
        sinogram = self._binarize_sinogram(data[0],
                                           self.parameters['threshold'])
        centre_of_rotations, angles, vol_shape, init = \
            self.get_frame_params()
        recon = self._recon_hull(sinogram, centre_of_rotations, angles)
        return recon

    def get_max_frames(self):
        return 'single'

    def get_citation_information(self):
        cite_info = CitationInformation()
        cite_info.description = \
            ("The reconstruction performed in this processing " +
             "chain is derived from this work.")
        cite_info.bibtex = \
            ("@article{laurentini1994visual," +
             "title={The visual hull concept for silhouette-based image understanding}," +
             "author={Laurentini, Aldo}," +
             "journal={IEEE Transactions on pattern analysis and machine intelligence}," +
             "volume={16}," +
             "number={2}," +
             "pages={150--162}," +
             "year={1994}," +
             "publisher={IEEE}" +
             "}")
        cite_info.endnote = \
            ("%0 Journal Article" +
             "%T The visual hull concept for silhouette-based image understanding" +
             "%A Laurentini, Aldo" +
             "%J IEEE Transactions on pattern analysis and machine intelligence" +
             "%V 16" +
             "%N 2" +
             "%P 150-162" +
             "%@ 0162-8828" +
             "%D 1994" +
             "%I IEEE")
        cite_info.doi = "http://dx.doi.org/10.1109/34.273735"
        return cite_info
