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

« back to all changes in this revision

Viewing changes to enthought.mayavi/tests/test_set_active_attribute.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
# Author: Prabhu Ramachandran <prabhu [at] aero . iitb . ac . in>
 
2
# Copyright (c) 2008,  Enthought, Inc.
 
3
# License: BSD Style.
 
4
 
 
5
# Standard library imports.
 
6
from os.path import abspath
 
7
from StringIO import StringIO
 
8
import copy
 
9
 
 
10
# Local imports.
 
11
from common import TestCase, get_example_data
 
12
 
 
13
 
 
14
class TestSetActiveAttribute(TestCase):
 
15
    def check(self):
 
16
        """Check if the visualization is OK.  Note that this is not an
 
17
        image based check, which is very convenient.
 
18
        """
 
19
        e = self.script.engine
 
20
        scene = e.current_scene
 
21
        src = scene.children[0]
 
22
        assert src.point_scalars_name == 'u'
 
23
        c = src.children[1]
 
24
        sc = c.outputs[0].point_data.scalars
 
25
        assert sc.name == 'u'
 
26
        # It is an iso-contour!
 
27
        assert sc.range[0] == sc.range[1] 
 
28
        aa = c.children[0].children[0]
 
29
        assert aa.point_scalars_name == 't'
 
30
        sc = aa.outputs[0].point_data.scalars
 
31
        assert sc.name == 't'
 
32
        assert abs(sc.range[0] - 308) < 1.0
 
33
        assert abs(sc.range[1] - 631) < 1.0
 
34
        s = aa.children[0].children[0]
 
35
 
 
36
    def test(self):
 
37
        """Test for the SetActiveAttribute filter.
 
38
        """
 
39
        from enthought.mayavi.sources.api import VTKXMLFileReader
 
40
        from enthought.mayavi.filters.contour import Contour
 
41
        from enthought.mayavi.filters.api import PolyDataNormals
 
42
        from enthought.mayavi.filters.set_active_attribute import SetActiveAttribute
 
43
        from enthought.mayavi.modules.api import Surface, Outline
 
44
 
 
45
        mayavi = script = self.script
 
46
 
 
47
        scene = mayavi.new_scene()
 
48
        r = VTKXMLFileReader()
 
49
        r.initialize(get_example_data('fire_ug.vtu'))
 
50
        mayavi.add_source(r)
 
51
        r.point_scalars_name = 'u'
 
52
        o = Outline()
 
53
        mayavi.add_module(o)
 
54
        c = Contour()
 
55
        mayavi.add_filter(c)
 
56
        n = PolyDataNormals()
 
57
        mayavi.add_filter(n)
 
58
        aa = SetActiveAttribute()
 
59
        mayavi.add_filter(aa)
 
60
        aa.point_scalars_name = 't'
 
61
        s = Surface()
 
62
        mayavi.add_module(s)
 
63
 
 
64
        scene.scene.isometric_view()
 
65
        # Check if things are OK.
 
66
        self.check()
 
67
 
 
68
        ############################################################
 
69
        # Test if saving a visualization and restoring it works.
 
70
 
 
71
        # Save visualization.
 
72
        f = StringIO()
 
73
        f.name = abspath('test.mv2') # We simulate a file.
 
74
        mayavi.save_visualization(f)
 
75
        f.seek(0) # So we can read this saved data.
 
76
 
 
77
        # Remove existing scene.
 
78
        engine = mayavi.engine
 
79
        engine.close_scene(s)
 
80
 
 
81
        # Load visualization
 
82
        mayavi.load_visualization(f)
 
83
        s = engine.current_scene
 
84
 
 
85
        # Now do the check.
 
86
        s.scene.isometric_view()
 
87
        self.check()
 
88
 
 
89
        ############################################################
 
90
        # Test if the Mayavi2 visualization can be deep-copied.
 
91
 
 
92
        # Pop the source object.
 
93
        source = s.children.pop()
 
94
        # Add it back to see if that works without error.
 
95
        s.children.append(source)
 
96
        # Now do the check.
 
97
        self.check()
 
98
 
 
99
        # Now deepcopy the source and replace the existing one with
 
100
        # the copy.  This basically simulates cutting/copying the
 
101
        # object from the UI via the right-click menu on the tree
 
102
        # view, and pasting the copy back.
 
103
        source1 = copy.deepcopy(source)
 
104
        s.children[0] = source1
 
105
        # Now do the check.
 
106
        s.scene.isometric_view()
 
107
        self.check()
 
108
        
 
109
        # If we have come this far, we are golden!
 
110
 
 
111
if __name__ == '__main__':
 
112
    t = TestSetActiveAttribute()
 
113
    t.run()