# Path: blockplayer/main.py
# def initialize():
# def update_frame(depth, rgb=None):
#     def from_rect(m,rect):
#     def matrix_slerp(matA, matB, alpha=0.6):
# 
# Path: blockplayer/stencil.py
# def color_dict():
# def print_colors():    
# def setup():
# def render_blocks(occ_grid, modelmat, rect=((0,0),(640,480))):
# def stencil_carve(depth, modelmat, occ_grid, rgb=None, rect=((0,0),(640,480))):
#         F = np.array([[[t, 255, 255]]], dtype='u1')
#         F = cv.fromarray(F)
#         F = np.asarray(F)
#     (L,T),(R,B) = rect
#     (L,T),(R,B) = rect
#     L,T,R,B = map(int, (L,T,R,B))
#     RGB = np.zeros((b_total.shape[0],
#                     b_total.shape[1],
#                     b_total.shape[2], 3),'u1')
#     HSV = np.zeros((b_total.shape[0],
#                     b_total.shape[1],
#                     b_total.shape[2], 3),'u1')
#         HSV = np.asarray(HSVm).reshape(HSV.shape)
#         RGB = np.asarray(RGBm).reshape(RGB.shape)
#         RGB = (RGB.astype('i')*4).clip(0,255)
# 
# Path: blockplayer/config.py
# def load(dir_path):
# def save(dir_path):
# LH = 0.0192
# LW = 0.016
# GRIDRAD = 18
# 
# Path: blockplayer/blockcraft.py
# def centered_rotated(R_correct, occ):
# def translated_rotated(R_correct, occ):
# 
# Path: blockplayer/blockdraw.py
# def clear():
# def show_grid(name, grid,
#               color=np.array([1,0,0,1]), opacity=1.0,
#               line_color=np.array([1,1,1,1])):
# def draw_block(blocks):
# def grid_vertices(grid, color=None):
# def grid_vertices_numpy(grid, color=None):
# def draw():
# 
# Path: blockplayer/dataset.py
# def advance(skip=1):
# def setup_opencl():
# def load_dataset(pathname):
# def load_gt():
# def load_random_dataset():
# def download():


import struct
import random
import os.path
import signal
import zmq
import numpy as np
import glxcontext
import opennpy
import loadgif
    import sys
from math import ceil
from glob import glob
from threading import Timer, Thread, Lock
from blockplayer import main
from blockplayer import stencil
from blockplayer import config
from blockplayer import blockcraft
from blockplayer import blockdraw
from blockplayer import dataset
        glxcontext.makecurrent()
        main.initialize()
        
        config.load('data/newest_calibration')
        opennpy.align_depth_to_rgb()
        dataset.setup_opencl()
        
        self.blocks = None
        self.block_loop_quit = False
        self.block_initialized = True
        self.block_loop()
    
    def block_slice(self, blocks):
        """Slices `blocks` to fit the playing area."""
        l,h,w = blocks.shape
        return (blocks[l/2-int(ceil(float(Board.PLAY_WIDTH)/2)):l/2+Board.PLAY_WIDTH/2,
            :Board.PLAY_HEIGHT,w/2-int(ceil(float(Board.PLAY_WIDTH)/2)):w/2+Board.PLAY_WIDTH/2])
    
    def block_once(self):
        """Process the next blockplayer frame"""
        opennpy.sync_update()
        depth,_ = opennpy.sync_get_depth()
        rgb,_ = opennpy.sync_get_video()
        
        main.update_frame(depth, rgb)
        
        # Update the blocks in the playing area
        if self.state < Game.STATE_CHECKWALL and self.update_blocks:
            self.blocks = main.grid.occ
            if hasattr(main, 'R_correct'):

# Path: blockplayer/main.py
# def initialize():
# def update_frame(depth, rgb=None):
#     def from_rect(m,rect):
#     def matrix_slerp(matA, matB, alpha=0.6):
# 
# Path: blockplayer/stencil.py
# def color_dict():
# def print_colors():    
# def setup():
# def render_blocks(occ_grid, modelmat, rect=((0,0),(640,480))):
# def stencil_carve(depth, modelmat, occ_grid, rgb=None, rect=((0,0),(640,480))):
#         F = np.array([[[t, 255, 255]]], dtype='u1')
#         F = cv.fromarray(F)
#         F = np.asarray(F)
#     (L,T),(R,B) = rect
#     (L,T),(R,B) = rect
#     L,T,R,B = map(int, (L,T,R,B))
#     RGB = np.zeros((b_total.shape[0],
#                     b_total.shape[1],
#                     b_total.shape[2], 3),'u1')
#     HSV = np.zeros((b_total.shape[0],
#                     b_total.shape[1],
#                     b_total.shape[2], 3),'u1')
#         HSV = np.asarray(HSVm).reshape(HSV.shape)
#         RGB = np.asarray(RGBm).reshape(RGB.shape)
#         RGB = (RGB.astype('i')*4).clip(0,255)
# 
# Path: blockplayer/config.py
# def load(dir_path):
# def save(dir_path):
# LH = 0.0192
# LW = 0.016
# GRIDRAD = 18
# 
# Path: blockplayer/blockcraft.py
# def centered_rotated(R_correct, occ):
# def translated_rotated(R_correct, occ):
# 
# Path: blockplayer/blockdraw.py
# def clear():
# def show_grid(name, grid,
#               color=np.array([1,0,0,1]), opacity=1.0,
#               line_color=np.array([1,1,1,1])):
# def draw_block(blocks):
# def grid_vertices(grid, color=None):
# def grid_vertices_numpy(grid, color=None):
# def draw():
# 
# 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: demos/demo_hitb.py
import struct
import random
import os.path
import signal
import zmq
import numpy as np
import glxcontext
import opennpy
import loadgif
    import sys
from math import ceil
from glob import glob
from threading import Timer, Thread, Lock
from blockplayer import main
from blockplayer import stencil
from blockplayer import config
from blockplayer import blockcraft
from blockplayer import blockdraw
from blockplayer import dataset

        glxcontext.makecurrent()
        main.initialize()
        
        config.load('data/newest_calibration')
        opennpy.align_depth_to_rgb()
        dataset.setup_opencl()
        
        self.blocks = None
        self.block_loop_quit = False
        self.block_initialized = True
        self.block_loop()
    
    def block_slice(self, blocks):
        """Slices `blocks` to fit the playing area."""
        l,h,w = blocks.shape
        return (blocks[l/2-int(ceil(float(Board.PLAY_WIDTH)/2)):l/2+Board.PLAY_WIDTH/2,
            :Board.PLAY_HEIGHT,w/2-int(ceil(float(Board.PLAY_WIDTH)/2)):w/2+Board.PLAY_WIDTH/2])
    
    def block_once(self):
        """Process the next blockplayer frame"""
        opennpy.sync_update()
        depth,_ = opennpy.sync_get_depth()
        rgb,_ = opennpy.sync_get_video()
        
        main.update_frame(depth, rgb)
        
        # Update the blocks in the playing area
        if self.state < Game.STATE_CHECKWALL and self.update_blocks:
            self.blocks = main.grid.occ
            if hasattr(main, 'R_correct'):

                self.blocks = blockcraft.translated_rotated(main.R_correct, self.blocks)