#!/usr/bin/python
# -*- coding:Utf-8 -*-

#----------------------------------------------------------------------------------------------------
#	Visualization Project : 2D Spatial Data
#
# Author : Arthur COSTE
# Creation Date : November 1st, 2012
# Update Date :
# Version : 1
# Status : Working
#
# Purpose : IsoSurface extraction and rendering
#
#----------------------------------------------------------------------------------------------------

import vtk
import sys

# read input vtk file
reader = vtk.vtkGenericDataObjectReader()
reader.SetFileName(sys.argv[1])
reader.Update()
# Extraction of the two isovalues we are interested in
# Bone structure
contours = vtk.vtkContourFilter()
contours.SetInputConnection(reader.GetOutputPort())
contours.GenerateValues(1, 120,120)
contours.SetComputeNormals(1)
# Skin
contours2 = vtk.vtkContourFilter()
contours2.SetInputConnection(reader.GetOutputPort())
contours2.GenerateValues(1, 5,5)
contours2.SetComputeNormals(1)
 
# Bone look up table
lut = vtk.vtkLookupTable()
lut.SetNumberOfTableValues(1)
lut.SetTableValue(0,1,1,1,1.0)
lut.Build()

# Skin lookup table
lut2 = vtk.vtkLookupTable()
lut2.SetNumberOfTableValues(1)
lut2.SetTableValue(0,1.0,0.8,0.7,0.45)
lut2.Build()

# The contour lines are mapped to the graphics library.
# Bone mapper
contMapper = vtk.vtkPolyDataMapper()
contMapper.SetInputConnection(contours.GetOutputPort())
contMapper.SetScalarRange(lut.GetTableRange())
contMapper.SetLookupTable(lut)

# Skin mappper
contMapper2 = vtk.vtkPolyDataMapper()
contMapper2.SetInputConnection(contours2.GetOutputPort())
contMapper2.SetScalarRange(lut2.GetTableRange())
contMapper2.SetLookupTable(lut2)

# Bone actor
contActor = vtk.vtkActor()
contActor.SetMapper(contMapper)

#skin actor
contActor2 = vtk.vtkActor()
contActor2.SetMapper(contMapper2)

# Bounding box (debug)
outline = vtk.vtkOutlineFilter()
outline.SetInputConnection(contours.GetOutputPort())
outlineMapper = vtk.vtkPolyDataMapper()
outlineMapper.SetInputConnection(outline.GetOutputPort())
outlineActor = vtk.vtkActor()
outlineActor.SetMapper(outlineMapper)
outlineActor.GetProperty().SetColor(0, 0, 0)
outline2 = vtk.vtkOutlineFilter()
outline2.SetInputConnection(contours2.GetOutputPort())
outlineMapper2 = vtk.vtkPolyDataMapper()
outlineMapper2.SetInputConnection(outline2.GetOutputPort())
outlineActor2 = vtk.vtkActor()
outlineActor2.SetMapper(outlineMapper2)

# Create the renderer, render window, and interactor.
ren = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)

# Set the background color to white. Associate the actors with the
# renderer.
ren.SetBackground(0, 0, 0)
ren.AddActor(contActor)
#ren.AddActor(outlineActor)
ren.AddActor(contActor2)
#ren.AddActor(outlineActor2)
renWin.SetSize(500, 500)
 
# Zoom in a little bit.
ren.ResetCamera()
ren.GetActiveCamera().Zoom(1.1)

#Initialize and start the event loop.
iren.Initialize()
renWin.Render()
iren.Start()

