1
"""Simple test for the Optional and Collection filters.
3
# Author: Prabhu Ramachandran <prabhu [at] aero . iitb . ac . in>
4
# Copyright (c) 2008, Prabhu Ramachandran
7
# Standard library imports.
8
from os.path import abspath
9
from StringIO import StringIO
13
from common import TestCase, get_example_data
16
class TestOptionalCollection(TestCase):
19
############################################################
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
29
############################################################
30
# Create a new scene and set up the visualization.
33
# Read a VTK (old style) data file.
35
r.initialize(get_example_data('heart.vtk'))
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)
47
########################################
50
"""Check if test status is OK given the collection."""
54
assert coll.outputs[0].point_data.scalars.range == (127.5, 127.5)
55
# Adding a contour should create the appropriate output in
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.
63
assert 'disabled' in o.name
64
assert coll.outputs[0] is c.outputs[0]
65
# Set back everything to original state.
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
74
############################################################
75
# Test if saving a visualization and restoring it works.
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.
83
# Remove existing scene.
84
engine = script.engine
88
script.load_visualization(f)
89
s = engine.current_scene
92
coll = s.children[0].children[0]
95
############################################################
96
# Test if the Mayavi2 visualization can be deep-copied.
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)
103
coll = s.children[0].children[0]
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
113
coll = s.children[0].children[0]
116
# If we have come this far, we are golden!
118
if __name__ == "__main__":
119
t = TestOptionalCollection()