1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4'''
5=========================================================================
6
7  Program:   Visualization Toolkit
8  Module:    TestNamedColorsIntegration.py
9
10  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
11  All rights reserved.
12  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
13
14     This software is distributed WITHOUT ANY WARRANTY; without even
15     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16     PURPOSE.  See the above copyright notice for more information.
17
18=========================================================================
19'''
20
21import vtk
22import vtk.test.Testing
23from vtk.util.misc import vtkGetDataRoot
24VTK_DATA_ROOT = vtkGetDataRoot()
25
26class TestPlatonicSolids(vtk.test.Testing.vtkTest):
27
28    class Colors(object):
29        '''
30            Provides some wrappers for using color names.
31        '''
32        def __init__(self):
33            '''
34                Define a single instance of the NamedColors class here.
35            '''
36            self.namedColors = vtk.vtkNamedColors()
37
38        def GetRGBColor(self, colorName):
39            '''
40                Return the red, green and blue components for a
41                color as doubles.
42            '''
43            rgb = [0.0, 0.0, 0.0] # black
44            self.namedColors.GetColorRGB(colorName, rgb)
45            return rgb
46
47        def GetRGBAColor(self, colorName):
48            '''
49                Return the red, green, blue and alpha
50                components for a color as doubles.
51            '''
52            rgba = [0.0, 0.0, 0.0, 1.0] # black
53            self.namedColors.GetColor(colorName, rgba)
54            return rgba
55
56    def testPlatonicSolids(self):
57
58        # Create five instances of vtkPlatonicSolidSource
59        # corresponding to each of the five Platonic solids.
60        #
61        tet = vtk.vtkPlatonicSolidSource()
62        tet.SetSolidTypeToTetrahedron()
63        tetMapper = vtk.vtkPolyDataMapper()
64        tetMapper.SetInputConnection(tet.GetOutputPort())
65        tetActor = vtk.vtkActor()
66        tetActor.SetMapper(tetMapper)
67
68        cube = vtk.vtkPlatonicSolidSource()
69        cube.SetSolidTypeToCube()
70        cubeMapper = vtk.vtkPolyDataMapper()
71        cubeMapper.SetInputConnection(cube.GetOutputPort())
72        cubeActor = vtk.vtkActor()
73        cubeActor.SetMapper(cubeMapper)
74        cubeActor.AddPosition(2.0, 0, 0)
75
76        oct = vtk.vtkPlatonicSolidSource()
77        oct.SetSolidTypeToOctahedron()
78        octMapper = vtk.vtkPolyDataMapper()
79        octMapper.SetInputConnection(oct.GetOutputPort())
80        octActor = vtk.vtkActor()
81        octActor.SetMapper(octMapper)
82        octActor.AddPosition(4.0, 0, 0)
83
84        icosa = vtk.vtkPlatonicSolidSource()
85        icosa.SetSolidTypeToIcosahedron()
86        icosaMapper = vtk.vtkPolyDataMapper()
87        icosaMapper.SetInputConnection(icosa.GetOutputPort())
88        icosaActor = vtk.vtkActor()
89        icosaActor.SetMapper(icosaMapper)
90        icosaActor.AddPosition(6.0, 0, 0)
91
92        dode = vtk.vtkPlatonicSolidSource()
93        dode.SetSolidTypeToDodecahedron()
94        dodeMapper = vtk.vtkPolyDataMapper()
95        dodeMapper.SetInputConnection(dode.GetOutputPort())
96        dodeActor = vtk.vtkActor()
97        dodeActor.SetMapper(dodeMapper)
98        dodeActor.AddPosition(8.0, 0, 0)
99
100        # Create rendering stuff
101        #
102        ren = vtk.vtkRenderer()
103        renWin = vtk.vtkRenderWindow()
104        renWin.AddRenderer(ren)
105
106        # Add the actors to the renderer, set the background and size
107        #
108        ren.AddActor(tetActor)
109        ren.AddActor(cubeActor)
110        ren.AddActor(octActor)
111        ren.AddActor(icosaActor)
112        ren.AddActor(dodeActor)
113
114        colors = self.Colors()
115
116        # Create a lookup table with colors for each face
117        #
118        math = vtk.vtkMath()
119        lut = vtk.vtkLookupTable()
120        lut.SetNumberOfColors(20)
121        lut.Build()
122        lut.SetTableValue(0, colors.GetRGBAColor("red"))
123        lut.SetTableValue(1, colors.GetRGBAColor("lime"))
124        lut.SetTableValue(2, colors.GetRGBAColor("yellow"))
125        lut.SetTableValue(3, colors.GetRGBAColor("blue"))
126        lut.SetTableValue(4, colors.GetRGBAColor("magenta"))
127        lut.SetTableValue(5, colors.GetRGBAColor("cyan"))
128        lut.SetTableValue(6, colors.GetRGBAColor("spring_green"))
129        lut.SetTableValue(7, colors.GetRGBAColor("lavender"))
130        lut.SetTableValue(8, colors.GetRGBAColor("mint_cream"))
131        lut.SetTableValue(9, colors.GetRGBAColor("violet"))
132        lut.SetTableValue(10, colors.GetRGBAColor("ivory_black"))
133        lut.SetTableValue(11, colors.GetRGBAColor("coral"))
134        lut.SetTableValue(12, colors.GetRGBAColor("pink"))
135        lut.SetTableValue(13, colors.GetRGBAColor("salmon"))
136        lut.SetTableValue(14, colors.GetRGBAColor("sepia"))
137        lut.SetTableValue(15, colors.GetRGBAColor("carrot"))
138        lut.SetTableValue(16, colors.GetRGBAColor("gold"))
139        lut.SetTableValue(17, colors.GetRGBAColor("forest_green"))
140        lut.SetTableValue(18, colors.GetRGBAColor("turquoise"))
141        lut.SetTableValue(19, colors.GetRGBAColor("plum"))
142
143        lut.SetTableRange(0, 19)
144        tetMapper.SetLookupTable(lut)
145        tetMapper.SetScalarRange(0, 19)
146        cubeMapper.SetLookupTable(lut)
147        cubeMapper.SetScalarRange(0, 19)
148        octMapper.SetLookupTable(lut)
149        octMapper.SetScalarRange(0, 19)
150        icosaMapper.SetLookupTable(lut)
151        icosaMapper.SetScalarRange(0, 19)
152        dodeMapper.SetLookupTable(lut)
153        dodeMapper.SetScalarRange(0, 19)
154
155        cam = ren.GetActiveCamera()
156        cam.SetPosition(3.89696, 7.20771, 1.44123)
157        cam.SetFocalPoint(3.96132, 0, 0)
158        cam.SetViewUp(-0.0079335, 0.196002, -0.980571)
159        cam.SetClippingRange(5.42814, 9.78848)
160
161        ren.SetBackground(colors.GetRGBColor("black"))
162        renWin.SetSize(400, 150)
163
164        # render and interact with data
165
166        iRen = vtk.vtkRenderWindowInteractor()
167        iRen.SetRenderWindow(renWin);
168        renWin.Render()
169
170        img_file = "TestPlatonicSolids.png"
171        vtk.test.Testing.compareImage(iRen.GetRenderWindow(), vtk.test.Testing.getAbsImagePath(img_file), threshold=25)
172        vtk.test.Testing.interact()
173
174if __name__ == "__main__":
175     vtk.test.Testing.main([(TestPlatonicSolids, 'test')])
176