File: TestGL2PSExporter.py

package info (click to toggle)
vtk 5.0.4-1.1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 51,084 kB
  • ctags: 70,426
  • sloc: cpp: 524,166; ansic: 220,276; tcl: 43,377; python: 14,037; perl: 3,102; java: 1,436; yacc: 1,033; sh: 339; lex: 248; makefile: 197; asm: 154
file content (168 lines) | stat: -rw-r--r-- 5,088 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env python
"""
Run this test like so:
$ vtkpython TestGL2PSExporter.py  -D $VTK_DATA_ROOT \
  -B $VTK_DATA_ROOT/Baseline/Rendering/

$ vtkpython TestGL2PSExporter.py --help
provides more details on other options.

Please note that this test requires that you have PIL (the Python
Imaging Library) module installed and also GL2PS support built into
VTK.

"""

import sys
import os
import os.path
import tempfile

import vtk
from vtk.test import Testing

# This requires that you have PIL installed.
try:
    import Image
except ImportError:
    print "Please install PIL (Python Imaging Library) to run this test."
    sys.exit(0)
    

class TestGL2PSExporter(Testing.vtkTest):
    # Create these as class attributes so that they are only created
    # once and not for every test.
    cs = vtk.vtkConeSource()
    mapper = vtk.vtkPolyDataMapper()
    mapper.SetInputConnection(cs.GetOutputPort())

    act = vtk.vtkActor()
    act.SetMapper(mapper)
    act.GetProperty().SetColor(0.5, 0.5, 1.0)

    axes = vtk.vtkCubeAxesActor2D()
    axes.SetInputConnection(cs.GetOutputPort())
    axes.SetFontFactor(2.0)
    axes.SetCornerOffset(0.0)
    axes.GetProperty().SetColor(0,0,0)

    txt = vtk.vtkTextActor()
    txt.SetDisplayPosition(45, 150)
    txt.SetInput("This is test\nmultiline\ninput\ndata.")
    tprop = txt.GetTextProperty()
    tprop.SetFontSize(18)
    tprop.SetFontFamilyToArial()
    tprop.SetJustificationToCentered()
    tprop.BoldOn()
    tprop.ItalicOn()
    tprop.ShadowOn()
    tprop.SetColor(0, 0, 1)

    ren = vtk.vtkRenderer()
    ren.AddActor(act)
    ren.AddActor(txt)
    axes.SetCamera(ren.GetActiveCamera())
    ren.AddActor(axes)
    ren.SetBackground(0.8, 0.8, 0.8)

    renWin = vtk.vtkRenderWindow()
    renWin.AddRenderer(ren)

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

    cam = ren.GetActiveCamera()
    cam.Azimuth(30)

    iren.Initialize()
    iren.Render()

    def _cleanup(self, files):
        """Remove the list of given files."""
        for f in files:
            if os.path.isfile(f):
                os.remove(f)
        
    def testVectorEPS(self):
        """Test vector EPS output."""
        # Get a temporary file name.  Set the extension to empty since
        # the exporter appends a suitable extension.
        tmp_eps = tempfile.mktemp('')
        # Write an EPS file.
        exp = vtk.vtkGL2PSExporter()
        exp.SetRenderWindow(self.renWin)
        exp.SetFilePrefix(tmp_eps)
        # Turn off compression so PIL can read file.
        exp.CompressOff() 
        exp.SetSortToBSP()
        exp.DrawBackgroundOn()
        exp.Write()
        # Now read the EPS file using PIL.
        tmp_eps += '.eps'
        im = Image.open(tmp_eps)
        # Get a temporary name for the PNG file.
        tmp_png = tempfile.mktemp('.png')
        im.save(tmp_png)

        # Now read the saved image and compare it for the test.
        png_r = vtk.vtkPNGReader()
        png_r.SetFileName(tmp_png)
        png_r.Update()
        img = png_r.GetOutput()

        # Cleanup.  Do this first because if the test fails, an
        # exception is raised and the temporary files won't be
        # removed.
        self._cleanup([tmp_eps, tmp_png])
        
        img_file = "TestGL2PSExporter.png"
        Testing.compareImageWithSavedImage(img,
                                           Testing.getAbsImagePath(img_file))
        # Interact if necessary.
        Testing.interact()

    def testRasterEPS(self):
        """Test EPS output when Write3DPropsAsRasterImage is on."""
        # Get a temporary file name.  Set the extension to empty since
        # the exporter appends a suitable extension.
        tmp_eps = tempfile.mktemp('')
        # Write an EPS file.
        exp = vtk.vtkGL2PSExporter()
        exp.SetRenderWindow(self.renWin)
        exp.SetFilePrefix(tmp_eps)
        # Turn off compression so PIL can read file.
        exp.CompressOff() 
        exp.SetSortToOff()
        exp.DrawBackgroundOn()
        exp.Write3DPropsAsRasterImageOn()
        exp.Write()
        # Now read the EPS file using PIL.
        tmp_eps += '.eps'
        im = Image.open(tmp_eps)
        # Get a temporary name for the PNG file.
        tmp_png = tempfile.mktemp('.png')
        im.save(tmp_png)

        # Now read the saved image and compare it for the test.
        png_r = vtk.vtkPNGReader()
        png_r.SetFileName(tmp_png)
        png_r.Update()
        img = png_r.GetOutput()

        # Cleanup.  Do this first because if the test fails, an
        # exception is raised and the temporary files won't be
        # removed.
        self._cleanup([tmp_eps, tmp_png])
        
        img_file = "TestGL2PSExporter.png"
        Testing.compareImageWithSavedImage(img,
                                           Testing.getAbsImagePath(img_file))
        # Interact if necessary.
        Testing.interact()


if __name__ == "__main__":
    cases = [(TestGL2PSExporter, 'test')]
    # This should prevent debug leaks messages.
    del TestGL2PSExporter
    Testing.main(cases)