#!/usr/bin/env python

import vtk


def main():
    colors = vtk.vtkNamedColors()

    # Set the background color.
    colors.SetColor("BkgColor", [51, 77, 102, 255])

    titles = list()
    textMappers = list()
    textActors = list()

    uGrids = list()
    mappers = list()
    actors = list()
    renderers = list()

    uGrids.append(MakeHexagonalPrism())
    titles.append('Hexagonal Prism')
    uGrids.append(MakeHexahedron())
    titles.append('Hexahedron')
    uGrids.append(MakePentagonalPrism())
    titles.append('Pentagonal Prism')

    uGrids.append(MakePolyhedron())
    titles.append('Polyhedron')
    uGrids.append(MakePyramid())
    titles.append('Pyramid')
    uGrids.append(MakeTetrahedron())
    titles.append('Tetrahedron')

    uGrids.append(MakeVoxel())
    titles.append('Voxel')
    uGrids.append(MakeWedge())
    titles.append('Wedge')

    renWin = vtk.vtkRenderWindow()
    renWin.SetWindowName('Cell3D Demonstration')

    iRen = vtk.vtkRenderWindowInteractor()
    iRen.SetRenderWindow(renWin)

    # Create one text property for all
    textProperty = vtk.vtkTextProperty()
    textProperty.SetFontSize(16)
    textProperty.SetJustificationToCentered()

    # Create and link the mappers actors and renderers together.
    for i in range(0, len(uGrids)):
        textMappers.append(vtk.vtkTextMapper())
        textActors.append(vtk.vtkActor2D())

        mappers.append(vtk.vtkDataSetMapper())
        actors.append(vtk.vtkActor())
        renderers.append(vtk.vtkRenderer())

        mappers[i].SetInputData(uGrids[i])
        actors[i].SetMapper(mappers[i])
        actors[i].GetProperty().SetColor(
            colors.GetColor3d("Seashell"))
        renderers[i].AddViewProp(actors[i])

        textMappers[i].SetInput(titles[i])
        textMappers[i].SetTextProperty(textProperty)

        textActors[i].SetMapper(textMappers[i])
        textActors[i].SetPosition(120, 16)
        renderers[i].AddViewProp(textActors[i])

        renWin.AddRenderer(renderers[i])

    gridDimensions = 3
    rendererSize = 300

    renWin.SetSize(rendererSize * gridDimensions,
                   rendererSize * gridDimensions)

    for row in range(0, gridDimensions):
        for col in range(0, gridDimensions):
            index = row * gridDimensions + col

            # (xmin, ymin, xmax, ymax)
            viewport = [
                float(col) * rendererSize /
                (gridDimensions * rendererSize),
                float(gridDimensions - (row + 1)) * rendererSize /
                (gridDimensions * rendererSize),
                float(col + 1) * rendererSize /
                (gridDimensions * rendererSize),
                float(gridDimensions - row) * rendererSize /
                (gridDimensions * rendererSize)]

            if index > len(actors) - 1:
                # Add a renderer even if there is no actor.
                # This makes the render window background all the same color.
                ren = vtk.vtkRenderer()
                ren.SetBackground(colors.GetColor3d("BkgColor"))
                ren.SetViewport(viewport)
                renWin.AddRenderer(ren)
                continue

            renderers[index].SetViewport(viewport)
            renderers[index].SetBackground(colors.GetColor3d("BkgColor"))
            renderers[index].ResetCamera()
            renderers[index].GetActiveCamera().Azimuth(30)
            renderers[index].GetActiveCamera().Elevation(-30)
            renderers[index].GetActiveCamera().Zoom(0.85)
            renderers[index].ResetCameraClippingRange()

    iRen.Initialize()
    renWin.Render()
    iRen.Start()


def MakeHexagonalPrism():
    """
      3D: hexagonal prism: a wedge with an hexagonal base.
      Be careful, the base face ordering is different from wedge.
    """

    numberOfVertices = 12

    points = vtk.vtkPoints()

    points.InsertNextPoint(0.0, 0.0, 1.0)
    points.InsertNextPoint(1.0, 0.0, 1.0)
    points.InsertNextPoint(1.5, 0.5, 1.0)
    points.InsertNextPoint(1.0, 1.0, 1.0)
    points.InsertNextPoint(0.0, 1.0, 1.0)
    points.InsertNextPoint(-0.5, 0.5, 1.0)

    points.InsertNextPoint(0.0, 0.0, 0.0)
    points.InsertNextPoint(1.0, 0.0, 0.0)
    points.InsertNextPoint(1.5, 0.5, 0.0)
    points.InsertNextPoint(1.0, 1.0, 0.0)
    points.InsertNextPoint(0.0, 1.0, 0.0)
    points.InsertNextPoint(-0.5, 0.5, 0.0)

    hexagonalPrism = vtk.vtkHexagonalPrism()
    for i in range(0, numberOfVertices):
        hexagonalPrism.GetPointIds().SetId(i, i)

    ug = vtk.vtkUnstructuredGrid()
    ug.InsertNextCell(hexagonalPrism.GetCellType(),
                      hexagonalPrism.GetPointIds())
    ug.SetPoints(points)

    return ug


def MakeHexahedron():
    """
      A regular hexagon (cube) with all faces square and three squares around
       each vertex is created below.

      Setup the coordinates of eight points
       (the two faces must be in counter clockwise
       order as viewed from the outside).

      As an exercise you can modify the coordinates of the points to create
       seven topologically distinct convex hexahedras.
    """
    numberOfVertices = 8

    # Create the points
    points = vtk.vtkPoints()
    points.InsertNextPoint(0.0, 0.0, 0.0)
    points.InsertNextPoint(1.0, 0.0, 0.0)
    points.InsertNextPoint(1.0, 1.0, 0.0)
    points.InsertNextPoint(0.0, 1.0, 0.0)
    points.InsertNextPoint(0.0, 0.0, 1.0)
    points.InsertNextPoint(1.0, 0.0, 1.0)
    points.InsertNextPoint(1.0, 1.0, 1.0)