# Path: blockplayer/dataset.py
# def advance(skip=1):
# def setup_opencl():
# def load_dataset(pathname):
# def load_gt():
# def load_random_dataset():
# def download():
# 
# 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: blockplayer/normals.py
# def normals_opencl(depth, mask=None, rect=((0,0),(640,480)), win=6):
#     def from_rect(m,rect):
# def normal_show(nx,ny,nz):
# def normals_numpy(depth, rect=((0,0),(640,480)), win=7, mat=None):
# def normals_c(depth, rect=((0,0),(640,480)), win=7):
# def color_axis(normals,d=0.1):
#     X,Y,Z,W = -dx, -dy, 0*dy+1, -(-dx*u + -dy*v + depth).astype(np.float32)
#     X,Y,Z = [normals[:,:,i] for i in range(3)]
# 
# Path: blockplayer/opencl.py
# def print_info(obj, info_cls):
# def normal_maker(name, mat, matw, matr):
#   def tup(i):
#   def tupw(i):
#   def tupr(i):
# def setup_kernel(mats=None):
#     def workaround(self):
# def print_all():
# def set_rect(_rect):
# def load_mask(mask):
# def load_raw(depth):
# def load_filt(filt):
# def get_xyz():
# def get_normals():
# def get_flatrot():
# def get_modelxyz():
# def get_face_debug():
# def get_gridinds_debug():
# def get_gridinds():
# def compute_normals():
# def compute_flatrot(mat):
#   def f(m): return m.astype('f')
# def compute_lattice2(modelmat, modulo):
#   def f(m): return m.astype('f')
# def compute_gridinds(xfix, zfix, LW, LH, gridmin, gridmax):
# def reduce_flatrot():
# def reduce_lattice2():
#     KK, RT = mats
#   (L,T),(R,B) = rect;
#   (L,T),(R,B) = rect
#   (L,T),(R,B) = rect
#   (L,T),(R,B) = rect
#   (L,T),(R,B) = rect
#     (L,T),(R,B) = rect
#     (L,T),(R,B) = rect
#   (L,T),(R,B) = rect; bounds = np.array((L,T,R,B),'f')
# 
# Path: blockplayer/lattice.py
# def circular_mean(data, modulo):
# def color_axis(X,Y,Z,w,d=0.3):
# def project(X, Y, Z, mat):
# def orientation_opencl(noshow=None):
# def orientation_numpy(normals,weights):
# def translation_opencl(R_oriented, init_t=None):
#   def cmean(mxy,c):
# def is_valid_estimate():
# def translation_numpy(n,w,depth,mat,matxyz,rect,init_t=None):
#   X2,Y2,Z2 = X*X,Y*Y,Z*Z
#   X,Y,Z = project(u.astype('f'),v.astype('f'),depth[t:b,l:r].astype('f'),
#                   np.dot(modelmat, matxyz))
#     X,Y,Z = project(u.astype('f'),v.astype('f'),depth[t:b,l:r].astype('f'),
#                     np.dot(modelmat, matxyz))
#   XYZ = ((X,Y,Z))
# 
# Path: blockplayer/grid.py
# def initialize():
# def gt2grid(gtstr, chars='*rR'):
# def grid2gt(occ):
# def initialize_with_groundtruth(GT):
# def window_correction(occA, vacA, occB, vacB):
#     def error(t):
# def xcorr_correction(A, B):
#     def convolve(p1, p2):
#     def best(cc):
# def show_votegrid(vg, color=(1,0,0), opacity=1):
# def has_previous_estimate():
# def apply_correction(grid, bx,bz,rot):
# def center(R_aligned, occ_new, vac_new):
# def nearest(R_previous, R_aligned):
# def align_with_previous(R_aligned, occ_new, vac_new):
# def stencil_carve(depth, rect, R_correct, occ, vac, rgb=None):
# def merge_with_previous(occ_, vac_, occ_stencil, vac_stencil, color_=None):
# def update_previous_estimate(R_correct):
#     B = apply_correction(B, x, z, r)
#     B = apply_correction(B, bx, bz, br)
#     X,Y,Z = np.array(gridmax)-gridmin
#     M = [expmap.axis2rot(np.array([0,-i*np.pi/2,0])) for i in range(4)]


import numpy as np
import pylab
from OpenGL.GL import *
from blockplayer.visuals.pointwindow import PointWindow
from blockplayer import dataset
from blockplayer import config
from blockplayer import preprocess
from blockplayer import normals
from blockplayer import opencl
from blockplayer import lattice
from blockplayer import grid

global window
if not 'window' in globals():
    window = PointWindow(title='demo_lattice', size=(640,480))



def once():
    if not FOR_REAL:
        dataset.advance()
        global depth
        depth = dataset.depth
    else:
        opennpy.sync_update()
        depth,_ = opennpy.sync_get_depth()

    def from_rect(m,rect):
        (l,t),(r,b) = rect
        return m[t:b,l:r]

    global mask, rect

    (mask,rect) = preprocess.threshold_and_mask(depth,config.bg)

    # Compute the surface normals
    normals.normals_opencl(depth, mask, rect)

    # Find the lattice orientation and then translation
    global R_oriented, R_aligned, R_correct
    R_oriented = lattice.orientation_opencl()

# Path: blockplayer/dataset.py
# def advance(skip=1):
# def setup_opencl():
# def load_dataset(pathname):
# def load_gt():
# def load_random_dataset():
# def download():
# 
# 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: blockplayer/normals.py
# def normals_opencl(depth, mask=None, rect=((0,0),(640,480)), win=6):
#     def from_rect(m,rect):
# def normal_show(nx,ny,nz):
# def normals_numpy(depth, rect=((0,0),(640,480)), win=7, mat=None):
# def normals_c(depth, rect=((0,0),(640,480)), win=7):
# def color_axis(normals,d=0.1):
#     X,Y,Z,W = -dx, -dy, 0*dy+1, -(-dx*u + -dy*v + depth).astype(np.float32)
#     X,Y,Z = [normals[:,:,i] for i in range(3)]
# 
# Path: blockplayer/opencl.py
# def print_info(obj, info_cls):
# def normal_maker(name, mat, matw, matr):
#   def tup(i):
#   def tupw(i):
#   def tupr(i):
# def setup_kernel(mats=None):
#     def workaround(self):
# def print_all():
# def set_rect(_rect):
# def load_mask(mask):
# def load_raw(depth):
# def load_filt(filt):
# def get_xyz():
# def get_normals():
# def get_flatrot():
# def get_modelxyz():
# def get_face_debug():
# def get_gridinds_debug():
# def get_gridinds():
# def compute_normals():
# def compute_flatrot(mat):
#   def f(m): return m.astype('f')
# def compute_lattice2(modelmat, modulo):
#   def f(m): return m.astype('f')
# def compute_gridinds(xfix, zfix, LW, LH, gridmin, gridmax):
# def reduce_flatrot():
# def reduce_lattice2():
#     KK, RT = mats
#   (L,T),(R,B) = rect;
#   (L,T),(R,B) = rect
#   (L,T),(R,B) = rect
#   (L,T),(R,B) = rect
#   (L,T),(R,B) = rect
#     (L,T),(R,B) = rect
#     (L,T),(R,B) = rect
#   (L,T),(R,B) = rect; bounds = np.array((L,T,R,B),'f')
# 
# Path: blockplayer/lattice.py
# def circular_mean(data, modulo):
# def color_axis(X,Y,Z,w,d=0.3):
# def project(X, Y, Z, mat):
# def orientation_opencl(noshow=None):
# def orientation_numpy(normals,weights):
# def translation_opencl(R_oriented, init_t=None):
#   def cmean(mxy,c):
# def is_valid_estimate():
# def translation_numpy(n,w,depth,mat,matxyz,rect,init_t=None):
#   X2,Y2,Z2 = X*X,Y*Y,Z*Z
#   X,Y,Z = project(u.astype('f'),v.astype('f'),depth[t:b,l:r].astype('f'),
#                   np.dot(modelmat, matxyz))
#     X,Y,Z = project(u.astype('f'),v.astype('f'),depth[t:b,l:r].astype('f'),
#                     np.dot(modelmat, matxyz))
#   XYZ = ((X,Y,Z))
# 
# Path: blockplayer/grid.py
# def initialize():
# def gt2grid(gtstr, chars='*rR'):
# def grid2gt(occ):
# def initialize_with_groundtruth(GT):
# def window_correction(occA, vacA, occB, vacB):
#     def error(t):
# def xcorr_correction(A, B):
#     def convolve(p1, p2):
#     def best(cc):
# def show_votegrid(vg, color=(1,0,0), opacity=1):
# def has_previous_estimate():
# def apply_correction(grid, bx,bz,rot):
# def center(R_aligned, occ_new, vac_new):
# def nearest(R_previous, R_aligned):
# def align_with_previous(R_aligned, occ_new, vac_new):
# def stencil_carve(depth, rect, R_correct, occ, vac, rgb=None):
# def merge_with_previous(occ_, vac_, occ_stencil, vac_stencil, color_=None):
# def update_previous_estimate(R_correct):
#     B = apply_correction(B, x, z, r)
#     B = apply_correction(B, bx, bz, br)
#     X,Y,Z = np.array(gridmax)-gridmin
#     M = [expmap.axis2rot(np.array([0,-i*np.pi/2,0])) for i in range(4)]


# Path: demos/demo_lattice.py
import numpy as np
import pylab
from OpenGL.GL import *
from blockplayer.visuals.pointwindow import PointWindow
from blockplayer import dataset
from blockplayer import config
from blockplayer import preprocess
from blockplayer import normals
from blockplayer import opencl
from blockplayer import lattice
from blockplayer import grid


global window
if not 'window' in globals():
    window = PointWindow(title='demo_lattice', size=(640,480))



def once():
    if not FOR_REAL:
        dataset.advance()
        global depth
        depth = dataset.depth
    else:
        opennpy.sync_update()
        depth,_ = opennpy.sync_get_depth()

    def from_rect(m,rect):
        (l,t),(r,b) = rect
        return m[t:b,l:r]

    global mask, rect

    (mask,rect) = preprocess.threshold_and_mask(depth,config.bg)

    # Compute the surface normals
    normals.normals_opencl(depth, mask, rect)

    # Find the lattice orientation and then translation
    global R_oriented, R_aligned, R_correct
    R_oriented = lattice.orientation_opencl()

    R_aligned = lattice.translation_opencl(R_oriented)