1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4import os
5import vtk
6import vtk.test.Testing
7import math
8
9class TestParallelCoordinatesColors(vtk.test.Testing.vtkTest):
10    def testLinePlot(self):
11        "Test if colored parallel coordinates plots can be built with python"
12
13        # Set up a 2D scene, add a PC chart to it
14        view = vtk.vtkContextView()
15        view.GetRenderer().SetBackground(1.0, 1.0, 1.0)
16        view.GetRenderWindow().SetSize(600,300)
17
18        chart = vtk.vtkChartParallelCoordinates()
19        view.GetScene().AddItem(chart)
20
21        # Create a table with some points in it
22        arrX = vtk.vtkFloatArray()
23        arrX.SetName("XAxis")
24
25        arrC = vtk.vtkFloatArray()
26        arrC.SetName("Cosine")
27
28        arrS = vtk.vtkFloatArray()
29        arrS.SetName("Sine")
30
31        arrS2 = vtk.vtkFloatArray()
32        arrS2.SetName("Tan")
33
34        numPoints = 200
35        inc = 7.5 / (numPoints-1)
36
37        for i in range(numPoints):
38            arrX.InsertNextValue(i * inc)
39            arrC.InsertNextValue(math.cos(i * inc) + 0.0)
40            arrS.InsertNextValue(math.sin(i * inc) + 0.0)
41            arrS2.InsertNextValue(math.tan(i * inc) + 0.5)
42
43        table = vtk.vtkTable()
44        table.AddColumn(arrX)
45        table.AddColumn(arrC)
46        table.AddColumn(arrS)
47        table.AddColumn(arrS2)
48
49        # Create blue to gray to red lookup table
50        lut = vtk.vtkLookupTable()
51        lutNum = 256
52        lut.SetNumberOfTableValues(lutNum)
53        lut.Build()
54
55        ctf = vtk.vtkColorTransferFunction()
56        ctf.SetColorSpaceToDiverging()
57        cl = []
58        # Variant of Colorbrewer RdBu 5
59        cl.append([float(cc)/255.0 for cc in [202, 0, 32]])
60        cl.append([float(cc)/255.0 for cc in [244, 165, 130]])
61        cl.append([float(cc)/255.0 for cc in [140, 140, 140]])
62        cl.append([float(cc)/255.0 for cc in [146, 197, 222]])
63        cl.append([float(cc)/255.0 for cc in [5, 113, 176]])
64        vv = [float(xx)/float(len(cl)-1) for xx in range(len(cl))]
65        vv.reverse()
66        for pt,color in zip(vv,cl):
67            ctf.AddRGBPoint(pt, color[0], color[1], color[2])
68
69        for ii,ss in enumerate([float(xx)/float(lutNum) for xx in range(lutNum)]):
70            cc = ctf.GetColor(ss)
71            lut.SetTableValue(ii,cc[0],cc[1],cc[2],1.0)
72
73        lut.SetAlpha(0.25)
74        lut.SetRange(-1, 1)
75
76        chart.GetPlot(0).SetInputData(table)
77        chart.GetPlot(0).SetScalarVisibility(1)
78        chart.GetPlot(0).SetLookupTable(lut)
79        chart.GetPlot(0).SelectColorArray("Cosine")
80
81        view.GetRenderWindow().SetMultiSamples(0)
82        view.GetRenderWindow().Render()
83
84        img_file = "TestParallelCoordinatesColors.png"
85        vtk.test.Testing.compareImage(view.GetRenderWindow(),vtk.test.Testing.getAbsImagePath(img_file),threshold=25)
86        vtk.test.Testing.interact()
87
88if __name__ == "__main__":
89    vtk.test.Testing.main([(TestParallelCoordinatesColors, 'test')])
90