/maps/build/mayavi/integrationtests/mayavi/test_contour.py

https://github.com/fspaolo/code
Python | 183 lines | 104 code | 37 blank | 42 comment | 1 complexity | 2e558b96aae810e0b1025ed3ce116545 MD5 | raw file
  1. """Simple test for the contour related modules.
  2. """
  3. # Author: Prabhu Ramachandran <prabhu_r@users.sf.net>
  4. # Copyright (c) 2005-2008, Enthought, Inc.
  5. # License: BSD Style.
  6. # Standard library imports.
  7. from os.path import abspath
  8. from StringIO import StringIO
  9. import copy
  10. import numpy
  11. # Local imports.
  12. from common import TestCase, get_example_data
  13. class TestContour(TestCase):
  14. def check(self):
  15. """Do the actual testing."""
  16. script = self.script
  17. e = script.engine
  18. scene = e.current_scene
  19. src = scene.children[0]
  20. mm = src.children[0]
  21. cgp1 = mm.children[1]
  22. assert cgp1.grid_plane.position == 15
  23. cgp2 = mm.children[2]
  24. assert cgp2.contour.filled_contours == True
  25. assert cgp2.grid_plane.axis == 'y'
  26. assert cgp2.grid_plane.position == 15
  27. iso = mm.children[3]
  28. ctr = iso.contour.contours
  29. assert iso.compute_normals == True
  30. assert ctr == [200.0]
  31. rng = iso.actor.mapper.input.point_data.scalars.range
  32. assert rng[0] == 200.0
  33. assert rng[1] == 200.0
  34. cp = mm.children[4]
  35. ip = cp.implicit_plane
  36. assert abs(numpy.sum(ip.normal - (0,0,1))) < 1e-16
  37. assert abs(numpy.sum(ip.origin - (0,0,5))) < 1e-16
  38. assert ip.widget.enabled == False
  39. def test(self):
  40. self.main()
  41. def do(self):
  42. ############################################################
  43. # Imports.
  44. script = self.script
  45. from enthought.mayavi.sources.vtk_file_reader import VTKFileReader
  46. from enthought.mayavi.modules.outline import Outline
  47. from enthought.mayavi.modules.iso_surface import IsoSurface
  48. from enthought.mayavi.modules.contour_grid_plane \
  49. import ContourGridPlane
  50. from enthought.mayavi.modules.scalar_cut_plane import ScalarCutPlane
  51. ############################################################
  52. # Create a new scene and set up the visualization.
  53. s = self.new_scene()
  54. # Read a VTK (old style) data file.
  55. r = VTKFileReader()
  56. r.initialize(get_example_data('heart.vtk'))
  57. script.add_source(r)
  58. # Create an outline for the data.
  59. o = Outline()
  60. script.add_module(o)
  61. # Create one ContourGridPlane normal to the 'x' axis.
  62. cgp1 = ContourGridPlane()
  63. script.add_module(cgp1)
  64. # Set the position to the middle of the data.
  65. cgp1.grid_plane.position = 15
  66. # Another with filled contours normal to 'y' axis.
  67. cgp2 = ContourGridPlane()
  68. cgp2.contour.filled_contours = True
  69. # Set the axis and position to the middle of the data.
  70. cgp2.grid_plane.axis = 'y'
  71. cgp2.grid_plane.position = 15
  72. script.add_module(cgp2)
  73. # An isosurface module.
  74. iso = IsoSurface(compute_normals=True)
  75. script.add_module(iso)
  76. iso.contour.contours = [200.0]
  77. # An interactive scalar cut plane.
  78. cp = ScalarCutPlane()
  79. script.add_module(cp)
  80. ip = cp.implicit_plane
  81. ip.normal = 0,0,1
  82. ip.origin = 0,0,5
  83. ip.widget.enabled = False
  84. # Set the scene to an isometric view.
  85. s.scene.isometric_view()
  86. # Now test.
  87. self.check()
  88. ############################################################
  89. # Test if the modules respond correctly when the components
  90. # are changed.
  91. ctr = cgp2.contour
  92. cgp2.contour = ctr.__class__()
  93. cgp2.contour = ctr
  94. cgp2.actor = cgp2.actor.__class__()
  95. iso.contour = iso.contour.__class__()
  96. iso.contour.contours = [200.0]
  97. iso.actor = iso.actor.__class__()
  98. iso.normals = iso.normals.__class__()
  99. ip = cp.implicit_plane
  100. cp.implicit_plane = cp.implicit_plane.__class__()
  101. cp.implicit_plane = ip
  102. ip.widget.enabled = False
  103. cp.contour = cp.contour.__class__()
  104. cp.cutter = cp.cutter.__class__()
  105. cp.actor = cp.actor.__class__()
  106. s.render()
  107. # Now check.
  108. self.check()
  109. ############################################################
  110. # Test if saving a visualization and restoring it works.
  111. # Save visualization.
  112. f = StringIO()
  113. f.name = abspath('test.mv2') # We simulate a file.
  114. script.save_visualization(f)
  115. f.seek(0) # So we can read this saved data.
  116. # Remove existing scene.
  117. engine = script.engine
  118. engine.close_scene(s)
  119. # Load visualization
  120. script.load_visualization(f)
  121. s = engine.current_scene
  122. self.check()
  123. ############################################################
  124. # Test if the MayaVi2 visualization can be deep-copied.
  125. # Pop the source object.
  126. source = s.children.pop()
  127. # Add it back to see if that works without error.
  128. s.children.append(source)
  129. # Now set the enabled status of the widget, this is impossible
  130. # to get correctly.
  131. cp = source.children[0].children[-1]
  132. cp.implicit_plane.widget.enabled = False
  133. self.check()
  134. # Now deepcopy the source and replace the existing one with
  135. # the copy. This basically simulates cutting/copying the
  136. # object from the UI via the right-click menu on the tree
  137. # view, and pasting the copy back.
  138. source1 = copy.deepcopy(source)
  139. s.children[0] = source1
  140. cp = source1.children[0].children[-1]
  141. cp.implicit_plane.widget.enabled = False
  142. self.check()
  143. # If we have come this far, we are golden!
  144. if __name__ == "__main__":
  145. t = TestContour()
  146. t.test()