# PyVision License
#
# Copyright (c) 2009 David S. Bolme
# All rights reserved.
#
# 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 name of copyright holders 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 REGENTS 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.


import gzip
import numpy as np
import time
import pyvision as pv
import scipy.interpolate as it
import scipy.ndimage as nd

class RangeImage:
    '''
    This class is used to handle range images. Originally written to handle
    output from the Minolta Vivid sensors distributed with the Face Recognition
    Grand Challenge 2004
    
    This implementation currently can parse range images in ".abs" or ".abs.gz" format.
    
    Very little type checking is done during parsing so unexpected exception or
    unusual behavior may occur if the file is not formated properly.
    
    This is a sample for the .abs file format:
    480 rows
    640 columns
    pixels (flag X Y Z):
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0  ...
    -999999.000000 -999999.000000 -999999.000000 -9999 ...
    -999999.000000 -999999.000000 -999999.000000 -9999 ...
    -999999.000000 -999999.000000 -999999.000000 -9999 ...
    
    Author: David S. Bolme 2009
    '''
    
    def __init__(self,filename):
        '''
        Reads a file containing range data.
        '''

        if filename[-7:] == '.abs.gz':
            # Assume the date is a zlib compressed abs file 
            f = gzip.open(filename)
            
        if filename[-4:] == '.abs':
            # Assume uncompressed
            f = open(filename)
        
        #print buffer[:100]
        
        #lines = buffer.split(EOL)
        rows = int(f.next().split()[0])
        cols = int(f.next().split()[0])
        format = f.next()
        
        self.width = cols
        self.height = rows
        
        self.flags = np.array([int(v) for v in f.next().split()]).reshape(rows,cols).transpose()
        self.x = np.array([float(v) for v in f.next().split()]).reshape(rows,cols).transpose()
        self.y = np.array([float(v) for v in f.next().split()]).reshape(rows,cols).transpose()
        self.z = np.array([float(v) for v in f.next().split()]).reshape(rows,cols).transpose()
        
        
    def getRange(self):
        '''
        @returns: xmin,xmax,ymin,ymax,zmin,zmax
        '''
        
        flags = np.array(self.flags.flatten(),dtype=np.bool)

        X = self.x.flatten()[flags]
        Y = self.y.flatten()[flags]
        Z = self.z.flatten()[flags]
        return min(X),max(X),min(Y),max(Y),min(Z),max(Z)    
    
    
    def getXImage(self):
        '''
        @returns: the x coordinates.
        '''
        xmin,xmax,ymin,ymax,zmin,zmax = self.getRange()
        
        r,c = self.x.shape 