# Copyright 2017 Bateared Collie
#
# Redistribution and use in source and binary forms, with or without modification, 
# are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this 
#   list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice, this 
#   list of conditions and the following disclaimer in the documentation and/or other
#   materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its contributors may 
#   be used to endorse or promote products derived from this software without specific
#   prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
# SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
# OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 
# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#


from vtk.util import numpy_support
from stk.DataTypes import vtkHyperCube

import numpy as np

def hyperCubeGenerate(array=np.zeros([100,1]),
            delta=[1.,1.],
            origin=[0.,0.],
            name="HyperCubeData",
            copy=True
            ):
    '''
    @summary: Generates a vtkHyperCube object from an input numpy array and axis.
                
    @param array: input data array
    @type array: numpy nd array 
    @param traceDictList: trace dictionary
    @type traceDictList: [dict]  
    @param delta: time sampling   
    @type delta: float
    @param origin: origin time for traces
    @type origin: float
    @param name: Name for data array
    @type name: string
    @param copy: perform a copy of the source array (turn this off at own risk)
    @type copy: boolean      
    
    ** Example Usage **
    
    >>> import stk.generators as gen
    >>> cube = gen.hyperCubeGenerate(array=np.zeros([10,20,5,6],dtype=np.float),
                          delta=[0.1,0.2,1.,2.],
                          origin=[-0.5,0.,-10.,3.],
                          )
                       
    .. warning::                    
 
         If you use copy=False then you should not try to access the 
            the trace data after processing.
    
    '''
    

    # Check the axis number
    nDim=len(array.shape)
    if len(delta)!=nDim:  
        raise Exception("Delta length does not match array dimensions")
    if len(origin)!=nDim: 
        raise Exception("Delta length does not match array dimensions")
    
    tpD = vtkHyperCube()
    tpD.SetNDimensions(nDim)
    tpD.SetDimensions(np.flip(array.shape,0))
    tpD.SetSpacing(delta)
    tpD.SetOrigin(origin)

    vtk_data = numpy_support.numpy_to_vtk(array.flatten(),deep=copy)
    
    vtk_data.SetName(name)
    tpD.GetPointData().SetScalars(vtk_data)
    
     
    return tpD