# 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/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/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 cv
import pylab
import numpy as np
import timeit
import opennpy
from OpenGL.GL import *
from blockplayer.visuals.pointwindow import PointWindow
from blockplayer import dataset
from blockplayer import normals
from blockplayer import config
from blockplayer import preprocess
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.


if not 'FOR_REAL' in globals():
    FOR_REAL = False

global window
if not 'window' in globals():
    window = PointWindow(title='demo_normals', size=(640,480))
    window.Move((0,0))



def color_axis(normals,d=0.1):
    #n = np.log(np.power(normals,40))
    X,Y,Z = [normals[:,:,i] for i in range(3)]
    cc = Y*Y+Z*Z, Z*Z+X*X, X*X+Y*Y
    cx = [np.max((1.0-(c/d*c/d),0*c),0) for c in cc]
    return [c * 0.8 + 0.2 for c in cx]


def show_normals(n, w, name='normals'):
    im = cv.CreateImage((n.shape[1],n.shape[0]), 32, 3)
    cv.SetData(im, np.dstack(3*[w]) * n[:,:,::-1]/2+.5)
    cv.ShowImage(name, im)


def show_normals_sphere(n, w):
    global axes_rotation

# 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/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/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_normals.py
import cv
import pylab
import numpy as np
import timeit
import opennpy
from OpenGL.GL import *
from blockplayer.visuals.pointwindow import PointWindow
from blockplayer import dataset
from blockplayer import normals
from blockplayer import config
from blockplayer import preprocess

# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.


if not 'FOR_REAL' in globals():
    FOR_REAL = False

global window
if not 'window' in globals():
    window = PointWindow(title='demo_normals', size=(640,480))
    window.Move((0,0))



def color_axis(normals,d=0.1):
    #n = np.log(np.power(normals,40))
    X,Y,Z = [normals[:,:,i] for i in range(3)]
    cc = Y*Y+Z*Z, Z*Z+X*X, X*X+Y*Y
    cx = [np.max((1.0-(c/d*c/d),0*c),0) for c in cc]
    return [c * 0.8 + 0.2 for c in cx]


def show_normals(n, w, name='normals'):
    im = cv.CreateImage((n.shape[1],n.shape[0]), 32, 3)
    cv.SetData(im, np.dstack(3*[w]) * n[:,:,::-1]/2+.5)
    cv.ShowImage(name, im)


def show_normals_sphere(n, w):
    global axes_rotation

    axes_rotation = np.eye(4)