# state file generated using paraview version 5.1.2

# ----------------------------------------------------------------
# setup views used in the visualization
# ----------------------------------------------------------------

#### import the simple module from the paraview
from paraview.simple import *
from time import sleep
from PIL import Image
import math
#### disable automatic camera reset on 'Show'
paraview.simple._DisableFirstRenderCameraReset()

# Create a new 'Render View'
renderView1 = CreateView('RenderView')
renderView1.ViewSize = [1108, 853]
renderView1.AxesGrid = 'GridAxes3DActor'
renderView1.CenterOfRotation = [83.0539321899414, 68.33893013000488, 81.69787979125977]
renderView1.StereoType = 0
renderView1.CameraPosition = [0, 0, 200]
renderView1.CameraFocalPoint = [80.28337733924155, 34.389806037434816, 87.3043883633431]
renderView1.CameraViewUp = [0.3784900662153272, -0.18080221906603647, -0.9077752075029998]
renderView1.CameraParallelScale = 29.414632087040744
renderView1.Background = [0.32, 0.34, 0.43]

# ----------------------------------------------------------------
# setup the data processing pipelines
# ----------------------------------------------------------------

# create a new 'XDMF Reader'
u150ref0xdmf = XDMFReader(FileNames=['C:\\Users\\NoobsDeSroobs\\Downloads\\data\\volumes\\u150-ref0\\u150-ref0.xdmf'])
u150ref0xdmf.PointArrayStatus = ['u150-ref0']
u150ref0xdmf.GridStatus = ['u150-ref0_0', 'u150-ref0_1', 'u150-ref0_2', 'u150-ref0_3', 'u150-ref0_4', 'u150-ref0_5', 'u150-ref0_6',
                           'u150-ref0_7', 'u150-ref0_8', 'u150-ref0_9', 'u150-ref0_10', 'u150-ref0_11', 'u150-ref0_12', 'u150-ref0_13', 'u150-ref0_14',
                           'u150-ref0_15', 'u150-ref0_16', 'u150-ref0_17']
u150ref0xdmf.UpdatePipeline()
data = u150ref0xdmf.GetDataInformation()
bounds = data.DataInformation.GetBounds()

# create a new 'Slice'
slice1 = Slice(Input=u150ref0xdmf)
slice1.SliceType = 'Plane'
slice1.SliceOffsetValues = [0.0]

numSlices = 100.0
numCols = math.ceil(math.sqrt(numSlices))
numRows = math.ceil(numSlices/numCols)

sliceDiff = (bounds[5] - bounds[4])/numSlices
volXSize = bounds[1] - bounds[0]
volYSize = bounds[3] - bounds[2]
volXCellSize = volXSize / numCols
volYCellSize = volYSize / numRows

for i in range(0, numSlices):
    image = Image.new("RGBA", (volXSize, volYSize));
    imageSize = image.size
    pixels = image.load();
    
    # init the 'Plane' selected for 'SliceType'
    slice1.SliceType.Origin = [83.0539321899414, 68.3389301300049, bounds[4] + (i*sliceDiff)]
    slice1.SliceType.Normal = [0.0, 0.0, 1.0]


    # ----------------------------------------------------------------
    # setup color maps and opacity mapes used in the visualization
    # note: the Get..() functions create a new object, if needed
    # ----------------------------------------------------------------

    # get color transfer function/color map for 'u150ref0'
    u150ref0LUT = GetColorTransferFunction('u150ref0')
    u150ref0LUT.RGBPoints = [0.0, 0.231373, 0.298039, 0.752941, 271.4392904696593, 0.865003, 0.865003, 0.865003,
                             542.8785809393186, 0.705882, 0.0156863, 0.14902]
    u150ref0LUT.ScalarRangeInitialized = 1.0

    # get opacity transfer function/opacity map for 'u150ref0'
    u150ref0PWF = GetOpacityTransferFunction('u150ref0')
    u150ref0PWF.Points = [0.0, 0.0, 0.5, 0.0, 542.8785809393186, 1.0, 0.5, 0.0]
    u150ref0PWF.ScalarRangeInitialized = 1

    # ----------------------------------------------------------------
    # setup the visualization in view 'renderView1'
    # ----------------------------------------------------------------

    # show data from slice1
    slice1Display = Show(slice1, renderView1)
    # trace defaults for the display properties.
    slice1Display.ColorArrayName = ['POINTS', 'u150-ref0']
    slice1Display.LookupTable = u150ref0LUT
    slice1Display.OSPRayScaleArray = 'u150-ref0'
    slice1Display.OSPRayScaleFunction = 'PiecewiseFunction'
    slice1Display.GlyphType = 'Arrow'
    slice1Display.SetScaleArray = [None, '']
    slice1Display.ScaleTransferFunction = 'PiecewiseFunction'
    slice1Display.OpacityArray = [None, '']
    slice1Display.OpacityTransferFunction = 'PiecewiseFunction'

    # show color legend
    slice1Display.SetScalarBarVisibility(renderView1, True)

    # setup the color legend parameters for each legend in this view

    # get color legend/bar for u150ref0LUT in view renderView1
    u150ref0LUTColorBar = GetScalarBar(u150ref0LUT, renderView1)
    u150ref0LUTColorBar.Title = 'u150-ref0'
    u150ref0LUTColorBar.ComponentTitle = 'Magnitude'

    # ----------------------------------------------------------------
    # finally, restore active source
    SetActiveSource(slice1)
    # ----------------------------------------------------------------


    #Save the slice to a PNG image
    sliceBounds = slice1.GetDataInformation().DataInformation.GetBounds()
    for y in range(0, volXSize):
        for x in range(0, volYSize):
            currCellX = sliceBounds[0] + x
            currCellY = sliceBounds[2] + y
            currCellZ = sliceBounds[4]
            #Get the RGBA value somehow....
            pointValue = slice1.PointData[currCellX, currCellY, currCellZ]
            pixels[x, y] = pointValue
            
    

    
    Render()
    sleep(1)
