#!/usr/bin/env python

"""This test ensures that the mapper does not change the LookupTable
that may be shared among different mappers.  Specifically, the vtkMapper
and vtkScalarsToColorsPainter classes use the LookupTable to map scalars
to colors.  When they do this they set the LookupTable's Alpha ivar.
They should do it in a manner that the LUT's original value is reset.
If this is not done strange errors show up.  For example if you use an
ImagePlaneWidget and share its LUT with another mapper that has either a
different opacity, then the image plane widget will pick up the wrong
opacity leading to subtle errors.  This test ensures that this does not
happen by testing MapScalars explicitly and also by creating an
ImagePlaneWidget.  """

import os
import os.path
from vtkmodules.vtkCommonCore import (
    vtkFloatArray,
    vtkLookupTable,
    vtkPoints,
)
from vtkmodules.vtkCommonDataModel import vtkPolyData
from vtkmodules.vtkFiltersModeling import vtkOutlineFilter
from vtkmodules.vtkIOImage import vtkVolume16Reader
from vtkmodules.vtkInteractionWidgets import vtkImagePlaneWidget
from vtkmodules.vtkRenderingCore import (
    vtkActor,
    vtkCellPicker,
    vtkPolyDataMapper,
    vtkRenderWindow,
    vtkRenderWindowInteractor,
    vtkRenderer,
)
import vtkmodules.vtkInteractionStyle
import vtkmodules.vtkRenderingFreeType
import vtkmodules.vtkRenderingOpenGL2
from vtkmodules.test import Testing
from vtkmodules.util.misc import vtkGetDataRoot
VTK_DATA_ROOT = vtkGetDataRoot()

class TestMapperLUT(Testing.vtkTest):
    def testMapScalars(self):
        """Test if the mapper sets the Alpha of the LUT."""
        # Create dummy data.
        p = vtkPolyData()
        pts = vtkPoints()
        pts.InsertNextPoint((0,0,0))
        sc = vtkFloatArray()
        sc.InsertNextValue(10.0)
        p.SetPoints(pts)
        p.GetPointData().SetScalars(sc)
        l = vtkLookupTable()
        m = vtkPolyDataMapper()
        m.SetInputData(p)
        m.SetScalarRange(0.0, 10.0)
        m.SetLookupTable(l)
        ret = m.MapScalars(0.5)
        self.assertEqual(l.GetAlpha(), 1.0)

    def testAboveAndBelowRange(self):
        """Test if the mapper handles values above and below the scalar range correctly."""

        # Known failure case of 10 colors in the lookup table and data range [0, 10]
        number_of_table_colors = 10
        range_minimum = 0.0
        range_maximum = 10.0
        p = vtkPolyData()
        pts = vtkPoints()
        pts.InsertNextPoint((0,0,0))
        sc = vtkFloatArray()
        sc.SetName("Scalars")
        sc.InsertNextValue(0.0)
        p.SetPoints(pts)
        p.GetPointData().SetScalars(sc)
        l = vtkLookupTable()
        l.SetNumberOfTableValues(number_of_table_colors)
        l.SetRange(range_minimum, range_maximum)
        l.Build()
        for i in range(number_of_table_colors):
            l.SetTableValue(i, 0, 0, 0, 1.0)

        l.SetBelowRangeColor(0.3, 0.3, 0.3, 1)
        l.UseBelowRangeColorOn()
        l.SetAboveRangeColor(0.7, 0.7, 0.7, 1)
        l.UseAboveRangeColorOn()

        m = vtkPolyDataMapper()
        m.SetInputData(p)
        m.UseLookupTableScalarRangeOn()
        m.SetScalarRange(0.0, 10.0)
        m.SetLookupTable(l)
        m.InterpolateScalarsBeforeMappingOn()
        m.SetInputArrayToProcess(0, 0, 0, 0, "Scalars")
        alpha = 1.0
        m.MapScalars(alpha)

        # Check value of the texel coordinate for the scalar at the lower end of the range
        texels = m.GetColorCoordinates()
        texel_width = 1.0 / (number_of_table_colors + 2); # +2 for below/above range
        # Value should be at least equal to the width of one texel but not too much above it
        self.assertTrue(texels.GetValue(0) >= texel_width)
        self.assertTrue(abs(texels.GetValue(0) - texel_width) < 1e-5)

        # Check value of the texel coordinate for the scalar at the upper end of the range
        sc.SetValue(0, 10.0)
        p.Modified()
        m.MapScalars(alpha)
        texels = m.GetColorCoordinates()
        self.assertTrue(texels.GetValue(0) <= 1.0 - texel_width)
        self.assertTrue(abs(texels.GetValue(0) - (1.0 - texel_width)) < 1e-5)

    def testImagePlaneWidget(self):
        "A more rigorous test using the image plane widget."
        # This test is largely copied from
        # Widgets/Python/TestImagePlaneWidget.py

        # Load some data.
        v16 = vtkVolume16Reader()
        v16.SetDataDimensions(64, 64)
        v16.SetDataByteOrderToLittleEndian()
        v16.SetFilePrefix(os.path.join(VTK_DATA_ROOT,
                                       "Data", "headsq", "quarter"))
        v16.SetImageRange(1, 93)
        v16.SetDataSpacing(3.2, 3.2, 1.5)
        v16.Update()

        xMin, xMax, yMin, yMax, zMin, zMax = v16.GetExecutive().GetWholeExtent(v16.GetOutputInformation(0))
        img_data = v16.GetOutput()
        spacing = img_data.GetSpacing()
        sx, sy, sz = spacing

        origin = img_data.GetOrigin()
        ox, oy, oz = origin

        # An outline is shown for context.
        outline = vtkOutlineFilter()
        outline.SetInputData(img_data)

        outlineMapper = vtkPolyDataMapper()
        outlineMapper.SetInputConnection(outline.GetOutputPort())

        outlineActor = vtkActor()
        outlineActor.SetMapper(outlineMapper)

        # The shared picker enables us to use 3 planes at one time
        # and gets the picking order right
        picker = vtkCellPicker()
        picker.SetTolerance(0.005)

        # The 3 image plane widgets are used to probe the dataset.
        planeWidgetX = vtkImagePlaneWidget()
        planeWidgetX.DisplayTextOn()
        planeWidgetX.SetInputData(img_data)
        planeWidgetX.SetPlaneOrientationToXAxes()
        planeWidgetX.SetSliceIndex(32)
        planeWidgetX.SetPicker(picker)
        planeWidgetX.SetKeyPressActivationValue("x")
        prop1 = planeWidgetX.GetPlaneProperty()
        prop1.SetColor(1, 0, 0)

        planeWidgetY = vtkImagePlaneWidget()
        planeWidgetY.DisplayTextOn()
        planeWidgetY.SetInputData(img_data)
        planeWidgetY.SetPlaneOrientationToYAxes()
        planeWidgetY.SetSliceIndex(32)
        planeWidgetY.SetPicker(picker)
        planeWidgetY.SetKeyPressActivationValue("y")
        prop2 = planeWidgetY.GetPlaneProperty()
        prop2.SetColor(1, 1, 0)
        planeWidgetY.SetLookupTable(planeWidgetX.GetLookupTable())

        # for the z-slice, turn off texture interpolation:
        # interpolation is now nearest neighbour, to demonstrate
        # cross-hair cursor snapping to pixel centers
        planeWidgetZ = vtkImagePlaneWidget()
        planeWidgetZ.DisplayTextOn()
        planeWidgetZ.SetInputData(img_data)
        planeWidgetZ.SetPlaneOrientationToZAxes()
        planeWidgetZ.SetSliceIndex(46)
        planeWidgetZ.SetPicker(picker)
        planeWidgetZ.SetKeyPressActivationValue("z")
        prop3 = planeWidgetZ.GetPlaneProperty()
        prop3.SetColor(0, 0, 1)
        planeWidgetZ.SetLookupTable(planeWidgetX.GetLookupTable())

        # Now create another actor with an opacity < 1 and with some
        # scalars.
        p = vtkPolyData()
        pts = vtkPoints()
        pts.InsertNextPoint((0,0,0))
        sc = vtkFloatArray()
        sc.InsertNextValue(1.0)
        p.SetPoints(pts)
        p.GetPointData().SetScalars(sc)
        m = vtkPolyDataMapper()
        m.SetInputData(p)
        # Share the lookup table of the widgets.
        m.SetLookupTable(planeWidgetX.GetLookupTable())
        m.UseLookupTableScalarRangeOn()
        dummyActor = vtkActor()
        dummyActor.SetMapper(m)
        dummyActor.GetProperty().SetOpacity(0.0)

        # Create the RenderWindow and Renderer
        ren = vtkRenderer()
        renWin = vtkRenderWindow()
        renWin.SetMultiSamples(0)
        renWin.AddRenderer(ren)

        # Add the dummy actor.
        ren.AddActor(dummyActor)
        # Add the outline actor to the renderer, set the background
        # color and size
        ren.AddActor(outlineActor)
        renWin.SetSize(600, 600)
        ren.SetBackground(0.1, 0.1, 0.2)

        current_widget = planeWidgetZ
        mode_widget = planeWidgetZ

        # Set the interactor for the widgets
        iact = vtkRenderWindowInteractor()
        iact.SetRenderWindow(renWin)
        planeWidgetX.SetInteractor(iact)
        planeWidgetX.On()
        planeWidgetY.SetInteractor(iact)
        planeWidgetY.On()
        planeWidgetZ.SetInteractor(iact)
        planeWidgetZ.On()

        # Create an initial interesting view
        ren.ResetCamera();
        cam1 = ren.GetActiveCamera()
        cam1.Elevation(110)
        cam1.SetViewUp(0, 0, -1)
        cam1.Azimuth(45)
        ren.ResetCameraClippingRange()

        iact.Initialize()
        renWin.Render()

if __name__ == "__main__":
    Testing.main([(TestMapperLUT, 'test')])

