~ubuntu-branches/ubuntu/hardy/mayavi2/hardy-backports

« back to all changes in this revision

Viewing changes to enthought.mayavi/tests/test_optional_collection.py

  • Committer: Bazaar Package Importer
  • Author(s): Scott Kitterman
  • Date: 2008-07-25 09:03:34 UTC
  • mfrom: (2.2.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080725090334-1hbb9fn8b3as5qy0
Tags: 2.2.0-1~hardy1
Automated backport upload; no source changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""Simple test for the Optional and Collection filters.
 
2
"""
 
3
# Author: Prabhu Ramachandran <prabhu [at] aero . iitb . ac . in>
 
4
# Copyright (c) 2008,  Prabhu Ramachandran
 
5
# License: BSD Style.
 
6
 
 
7
# Standard library imports.
 
8
from os.path import abspath
 
9
from StringIO import StringIO
 
10
import copy
 
11
 
 
12
# Local imports.
 
13
from common import TestCase, get_example_data
 
14
 
 
15
 
 
16
class TestOptionalCollection(TestCase):
 
17
 
 
18
    def test(self):
 
19
        ############################################################
 
20
        # Imports.
 
21
        script = self.script
 
22
        from enthought.mayavi.sources.vtk_file_reader import VTKFileReader
 
23
        from enthought.mayavi.filters.contour import Contour
 
24
        from enthought.mayavi.filters.optional import Optional
 
25
        from enthought.mayavi.filters.collection import Collection 
 
26
        from enthought.mayavi.filters.api import PolyDataNormals
 
27
        from enthought.mayavi.modules.api import Surface
 
28
 
 
29
        ############################################################
 
30
        # Create a new scene and set up the visualization.
 
31
        s = self.new_scene()
 
32
 
 
33
        # Read a VTK (old style) data file.
 
34
        r = VTKFileReader()
 
35
        r.initialize(get_example_data('heart.vtk'))
 
36
        script.add_source(r)
 
37
 
 
38
        c = Contour() 
 
39
        # `name` is used for the notebook tabs.
 
40
        n = PolyDataNormals(name='Normals') 
 
41
        o = Optional(filter=n, label_text='Compute normals')
 
42
        coll = Collection(filters=[c, o], name='IsoSurface')
 
43
        mayavi.add_filter(coll)
 
44
        s = Surface()
 
45
        mayavi.add_module(s)
 
46
 
 
47
        ########################################
 
48
        # do the testing.
 
49
        def check(coll):
 
50
            """Check if test status is OK given the collection."""
 
51
            c, o = coll.filters
 
52
            c = c.filter
 
53
            n = o.filter
 
54
            assert coll.outputs[0].point_data.scalars.range == (127.5, 127.5)
 
55
            # Adding a contour should create the appropriate output in
 
56
            # the collection.
 
57
            c.contours.append(200)
 
58
            assert coll.outputs[0].point_data.scalars.range == (127.5, 200.0)
 
59
            # the collection's output should be that of the normals.
 
60
            assert coll.outputs[0] is n.outputs[0]
 
61
            # disable the optional filter and check.
 
62
            o.enabled = False
 
63
            assert 'disabled' in o.name
 
64
            assert coll.outputs[0] is c.outputs[0]
 
65
            # Set back everything to original state.
 
66
            c.contours.pop()
 
67
            o.enabled = True
 
68
            assert coll.outputs[0].point_data.scalars.range == (127.5, 127.5)
 
69
            assert coll.outputs[0] is n.outputs[0]
 
70
            assert 'disabled' not in o.name
 
71
 
 
72
        check(coll)
 
73
 
 
74
        ############################################################
 
75
        # Test if saving a visualization and restoring it works.
 
76
 
 
77
        # Save visualization.
 
78
        f = StringIO()
 
79
        f.name = abspath('test.mv2') # We simulate a file.
 
80
        script.save_visualization(f)
 
81
        f.seek(0) # So we can read this saved data.
 
82
 
 
83
        # Remove existing scene.
 
84
        engine = script.engine
 
85
        engine.close_scene(s)
 
86
 
 
87
        # Load visualization
 
88
        script.load_visualization(f)
 
89
        s = engine.current_scene
 
90
 
 
91
        # Now do the check.
 
92
        coll = s.children[0].children[0]
 
93
        check(coll)
 
94
 
 
95
        ############################################################
 
96
        # Test if the Mayavi2 visualization can be deep-copied.
 
97
 
 
98
        # Pop the source object.
 
99
        source = s.children.pop()
 
100
        # Add it back to see if that works without error.
 
101
        s.children.append(source)
 
102
        # Now do the check.
 
103
        coll = s.children[0].children[0]
 
104
        check(coll)
 
105
 
 
106
        # Now deepcopy the source and replace the existing one with
 
107
        # the copy.  This basically simulates cutting/copying the
 
108
        # object from the UI via the right-click menu on the tree
 
109
        # view, and pasting the copy back.
 
110
        source1 = copy.deepcopy(source)
 
111
        s.children[0] = source1
 
112
        # Now do the check.
 
113
        coll = s.children[0].children[0]
 
114
        check(coll)
 
115
        
 
116
        # If we have come this far, we are golden!
 
117
 
 
118
if __name__ == "__main__":
 
119
    t = TestOptionalCollection()
 
120
    t.run()
 
121