# Path: blockplayer/calibkinect.py
# def recip_depth_openni(depth):
# def projection():
# def convertOpenNI2Real_weave(depth, u=None, v=None,
#                        mat=np.ascontiguousarray(
#                                 np.linalg.inv(projection()))):
# def convertOpenNI2Real_numpy(depth, u=None, v=None,
#                        mat=np.linalg.inv(projection())):
# def convertReal2OpenNI(X, Y, Z, mat=projection()):
#     N = np.prod(depth.shape)
#     X,Y = u,v
#     N = np.prod(depth.shape)
#     X,Y,Z = u,v, recip_depth_openni(depth)
# 
# Path: blockplayer/config.py
# def load(dir_path):
# def save(dir_path):
# LH = 0.0192
# LW = 0.016
# GRIDRAD = 18
# 
# Path: blockplayer/preprocess.py
# def threshold_and_mask(depth,bg):
#     def m_():
#     def m2_():
#     def m3_():


import numpy as np
import pylab
import opennpy
from blockplayer import calibkinect
from blockplayer import config
from blockplayer import preprocess
from blockplayer.visuals.pointwindow import PointWindow

mat = np.linalg.inv(calibkinect.aligned_projection())
mat = np.ascontiguousarray(mat)

PREPROCESS = True


def once():
    global depth
    global rgb
    opennpy.sync_update()
    depth,_ = opennpy.sync_get_depth()
    rgb,_ = opennpy.sync_get_video()

    global x,y,z,xyz

    if PREPROCESS:
        global mask, rect
        mask, rect = preprocess.threshold_and_mask(depth, config.bg)

        (l,t),(r,b) = rect
        depth_ = np.ascontiguousarray(depth[t:b,l:r])
        v,u = np.mgrid[t:b,l:r].astype('f')
        x,y,z = calibkinect.convertOpenNI2Real(depth_,u,v)
        x=x[mask[t:b,l:r]]
        y=y[mask[t:b,l:r]]
        z=z[mask[t:b,l:r]]

        rgb_ = rgb[t:b,l:r,:][mask[t:b,l:r],:]
    else:

# Path: blockplayer/calibkinect.py
# def recip_depth_openni(depth):
# def projection():
# def convertOpenNI2Real_weave(depth, u=None, v=None,
#                        mat=np.ascontiguousarray(
#                                 np.linalg.inv(projection()))):
# def convertOpenNI2Real_numpy(depth, u=None, v=None,
#                        mat=np.linalg.inv(projection())):
# def convertReal2OpenNI(X, Y, Z, mat=projection()):
#     N = np.prod(depth.shape)
#     X,Y = u,v
#     N = np.prod(depth.shape)
#     X,Y,Z = u,v, recip_depth_openni(depth)
# 
# Path: blockplayer/config.py
# def load(dir_path):
# def save(dir_path):
# LH = 0.0192
# LW = 0.016
# GRIDRAD = 18
# 
# Path: blockplayer/preprocess.py
# def threshold_and_mask(depth,bg):
#     def m_():
#     def m2_():
#     def m3_():


# Path: demos/demo_calib.py
import numpy as np
import pylab
import opennpy
from blockplayer import calibkinect
from blockplayer import config
from blockplayer import preprocess
from blockplayer.visuals.pointwindow import PointWindow


mat = np.linalg.inv(calibkinect.aligned_projection())
mat = np.ascontiguousarray(mat)

PREPROCESS = True


def once():
    global depth
    global rgb
    opennpy.sync_update()
    depth,_ = opennpy.sync_get_depth()
    rgb,_ = opennpy.sync_get_video()

    global x,y,z,xyz

    if PREPROCESS:
        global mask, rect
        mask, rect = preprocess.threshold_and_mask(depth, config.bg)

        (l,t),(r,b) = rect
        depth_ = np.ascontiguousarray(depth[t:b,l:r])
        v,u = np.mgrid[t:b,l:r].astype('f')
        x,y,z = calibkinect.convertOpenNI2Real(depth_,u,v)
        x=x[mask[t:b,l:r]]
        y=y[mask[t:b,l:r]]
        z=z[mask[t:b,l:r]]

        rgb_ = rgb[t:b,l:r,:][mask[t:b,l:r],:]
    else:

        x,y,z = calibkinect.convertOpenNI2Real(depth)