#!/usr/bin/python
#dv3d_io_autoScript.py
#Automatic processing of i/o for images / movies
#DV3D refactor 2
#AG 22/01/2013

''' 
Form the main script, type holders are:

    #the main data type holders
    tpw.Planes = []
    tpw.Volumes = []
    tpw.Surfaces = []
    tpw.SurfaceGroups = []
    tpw.Markers = []
    tpw.Lights = []

'''


import dv3d_gui_Callbacks as cb
import time
import vtk

from dv3d_vtkTools_generateVTKSurface import *
from numpy import *


def writeImage(imageCounter, renderWindow):
    #write an image out to file
    windowToImageFilter = vtk.vtkWindowToImageFilter()
    windowToImageFilter.SetInput(renderWindow)
    windowToImageFilter.SetMagnification(1) #set the resolution of the output image (1x times the current resolution of vtk render window)
    windowToImageFilter.SetInputBufferTypeToRGBA() #also record the alpha (transparency) channel
    windowToImageFilter.Update()

    writer = vtk.vtkTIFFWriter()
    writer.SetCompressionToNoCompression()
    writer.SetFileName('/tmp/frame%05d.tiff' %imageCounter)
    writer.SetInputConnection(windowToImageFilter.GetOutputPort())
    writer.Write()


def runAutoScripts(the_parent_window):
    #pass # for now
    print 'executing auto script'

    tpw = the_parent_window #for brevity

    # change background colour
    tpw.ren3d.SetBackground(1,1,1)

    # load base fmri Volume
    cb.cb_LoadDataForPlanes(tpw, "/scratch/home/andre/4_code/kinect2/code/pyosceleton/anatomy/MNI_structural.nii.gz")
    planes1 = tpw.Planes[-1]    

    # load base fmri Volume
    cb.cb_LoadDataForPlanes(tpw, "/groups/Projects/P1062/Andre/Overlay1_midres.nii.gz")
    planes1 = tpw.Planes[-1]    

    # load base fmri Volume
    cb.cb_LoadDataForPlanes(tpw, "/groups/Projects/P1062/Andre/Overlay2_midres.nii.gz")
    planes1 = tpw.Planes[-1]    

    # load cortex
    #cb.cb_LoadDataForSurface(tpw, "/scratch/home/andre/4_code/kinect2/code/pyosceleton/anatomy/Cortex_3mm.vtk")
    cb.cb_LoadDataForSurface(tpw, "/home/andre/andre_svn/trunk/DV3D_BetaDist/DV3D_v0.551/DV3D_essentials/mni_surface_145_05mm_x3.vtk")
    cortex = tpw.Surfaces[-1]
    #change appearance
    #cortex.GetProperty().SetRepresentationToWireframe()
    cortex.GetProperty().SetOpacity(1.0)
    cortex.GetProperty().SetColor(0.85,0.85,0.85)    

    # load scalp
    #cb.cb_LoadDataForSurface(tpw, "/scratch/home/andre/4_code/kinect2/code/pyosceleton/anatomy/Cortex_3mm.vtk")
    cb.cb_LoadDataForSurface(tpw, "/scratch/home/andre/4_code/kinect2/code/pyosceleton/anatomy/Scalp.vtk")
    scalp = tpw.Surfaces[-1]
    #change appearance
    #cortex.GetProperty().SetRepresentationToWireframe()
    scalp.GetProperty().SetOpacity(1.0)
    scalp.GetProperty().SetColor(1,0.78,0.62)
    scalp.GetProperty().SetBackfaceCulling(1)




    #stat1 form overlay 1
    #generateGroupSurfaces(tpw, 1, extractionVals, opacityVals, colHueVals, surfacesToGetIndex, smoothingIterations, decimationFactor, samplingFrequency)
    generateGroupSurfaces(tpw, 1, linspace(2.48,2.59,6), [0.18,0.18,0.18,0.18,0.18,1.0], linspace(0,0.166666666,6), 0, 100, 0, 1)
    stat1 = tpw.SurfaceGroups[-1]

    #inverse stat1 form overlay 1
    generateGroupSurfaces(tpw, 1, linspace(-2.48,-2.59,6), [0.18,0.18,0.18,0.18,0.18,1.0], linspace(0.6666666,0.33333,6), 0, 100, 0, 1)
    stat2 = tpw.SurfaceGroups[-1]

    #stat2 form overlay 2
    #generateGroupSurfaces(tpw, 1, extractionVals, opacityVals, colHueVals, surfacesToGetIndex, smoothingIterations, decimationFactor, samplingFrequency)
    #generateGroupSurfaces(tpw, 2, linspace(2.48,2.59,6), [0.18,0.18,0.18,0.18,0.18,1.0], linspace(0,0.166666666,6), 0, 100, 0, 1)
    #stat1 = tpw.SurfaceGroups[-1]

    #inverse stat2 form overlay 2
    #generateGroupSurfaces(tpw, 2, linspace(-5.48,-13,6), [0.18,0.18,0.18,0.18,0.18,1.0], linspace(0.6666666,0.33333,6), 0, 100, 0, 1)
    #stat2 = tpw.SurfaceGroups[-1]


    camera = tpw.ren3d.GetActiveCamera()
    renWin = tpw.ren3d.GetRenderWindow()

    #Everything loaded, now lets start some interacting
    imageCounter = 0 #for naming output frames

    ##planes are off initially
    for thisPlaneGroup in tpw.Planes:
        thisPlaneGroup.planeWidget_3dX.Off()
        thisPlaneGroup.planeWidget_3dY.Off()
        thisPlaneGroup.planeWidget_3dZ.Off()

    #set up the correct camera position
    camera.SetFocalPoint(cortex.GetCenter())
    camera.Elevation(300)
    camera.Zoom(1.5)
    camera.SetViewUp(0,0,1)
    camera.Azimuth(-90)

    tpw.iwidget_3d.interactor.GetRenderWindow().Render()
    time.sleep(1.0)

    
    camera.Azimuth(180)


    for i in range(30):
        writeImage(imageCounter, renWin)
        imageCounter += 1


    #first process is to rotate the cortex, fading it out
    #  to reveal the thalamus below
    for i in range(180):
        camera.Azimuth(1)
        scalp.GetProperty().SetOpacity((360-i*1.93)/360.0)
        tpw.iwidget_3d.interactor.GetRenderWindow().Render()
        #time.sleep(0.01)
        writeImage(imageCounter, renWin)
        imageCounter += 1

    #first process is to rotate the cortex, fading it out
    #  to reveal the thalamus below
    for i in range(180):
        camera.Azimuth(2)
        cortex.GetProperty().SetOpacity((360-i*1.93)/360.0)
        tpw.iwidget_3d.interactor.GetRenderWindow().Render()
        #time.sleep(0.01)
        writeImage(imageCounter, renWin)
        imageCounter += 1

    for i in range(45):
        camera.Azimuth(2)
        camera.Elevation(60/45.1)
        #cortex.GetProperty().SetOpacity((360-i*1.93)/360.0)
        tpw.iwidget_3d.interactor.GetRenderWindow().Render()
        #time.sleep(0.01)
        writeImage(imageCounter, renWin)
        imageCounter += 1




    #for i in range(30):
    #    camera.Elevation(-2)
    #    camera.Zoom(1.015)
    #    tpw.iwidget_3d.interactor.GetRenderWindow().Render()       
    #    #writeImage(imageCounter, renWin)
    #    #imageCounter += 1




'''
then run:
ffmpeg -r 25 -i /tmp/frame%05d.tiff -vb 10000k /tmp/video.mpg
'''


   

