Skip to content

Commit 37789f0

Browse files
author
Jonathan Rocher
committed
Adding some 3D plotting demoes with Mayavi.
1 parent be3ef0b commit 37789f0

File tree

8 files changed

+365
-0
lines changed

8 files changed

+365
-0
lines changed

mayavi/animated_mayavi_traits.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
""" Application of embedding a Mayavi scene in a TraitsUI: control the data
2+
that is displayed.
3+
4+
Derived from Gael Varoquaux's demo:
5+
http://docs.enthought.com/mayavi/mayavi/auto/example_mlab_interactive_dialog.html#example-mlab-interactive-dialog
6+
"""
7+
# Computation imports
8+
from numpy.random import randn
9+
10+
# Traits and TraitsUI imports
11+
#############################
12+
from traits.api import HasTraits, Any, Button, Instance, on_trait_change, \
13+
Array
14+
from traitsui.api import View, Item
15+
16+
# Mayavi imports
17+
################
18+
# Mayavi model
19+
from mayavi.tools.mlab_scene_model import MlabSceneModel
20+
# TraitsUI editor for a MlabSceneModel
21+
from tvtk.pyface.scene_editor import SceneEditor
22+
# Style of the scene editor
23+
from mayavi.core.ui.mayavi_scene import MayaviScene
24+
# Storage of a module (renderer)
25+
from mayavi.core.api import PipelineBase
26+
27+
# Animation part
28+
from pyface.timer.api import Timer
29+
30+
def compute_positions(N=3):
31+
""" Data generation: make random jumps from the present position
32+
"""
33+
print "computing the positions"
34+
x = randn(N)
35+
y = randn(N)
36+
z = randn(N)
37+
return x, y, z
38+
39+
class BrownianMotionVisualization(HasTraits):
40+
41+
scene = Instance(MlabSceneModel, ())
42+
plot = Instance(PipelineBase)
43+
x = Array()
44+
y = Array()
45+
z = Array()
46+
47+
timer = Any()
48+
timer_button = Button()
49+
50+
@on_trait_change('scene.activated')
51+
def create_plot(self):
52+
""" Wait before the scene is activated before trying to add data,
53+
modules, filers into it.
54+
"""
55+
x, y, z = compute_positions()
56+
self.plot = self.scene.mlab.points3d(x, y, z)
57+
58+
def _timer_button_fired(self):
59+
if self.timer is None:
60+
self.timer = Timer(100, self.update_plot)
61+
else:
62+
self.timer.stop()
63+
self.timer = None
64+
65+
def update_plot(self):
66+
""" Recompute the ball positions and redraw
67+
"""
68+
x, y, z = compute_positions()
69+
self.plot.mlab_source.set(x=x, y=y, z=z)
70+
71+
72+
# the layout of the dialog created
73+
view = View(Item('scene', editor=SceneEditor(scene_class=MayaviScene),
74+
height=250, width=300, show_label=False),
75+
Item('timer_button', label = "Start/Stop animation",
76+
show_label = False),
77+
)
78+
79+
if __name__ == "__main__":
80+
vis = BrownianMotionVisualization()
81+
vis.configure_traits()

mayavi/demo_pipeline.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
""" Demo looking at a 3D scalar field
2+
"""
3+
import numpy as np
4+
from mayavi import mlab
5+
6+
x, y, z = np.ogrid[-10:10:20j, -10:10:20j, -10:10:20j]
7+
s = np.sin(x*y*z)/(x*y*z)
8+
mlab.contour3d(s)
9+
mlab.clf()
10+
scalar_field = mlab.pipeline.scalar_field(s)
11+
vol = mlab.pipeline.volume(scalar_field, vmin=0, vmax=0.8)
12+
mlab.colorbar(orientation="vertical")
13+
ipw_x = mlab.pipeline.image_plane_widget(mlab.pipeline.scalar_field(s),
14+
plane_orientation='x_axes',
15+
slice_index=10,
16+
)
17+
ipw_y = mlab.pipeline.image_plane_widget(mlab.pipeline.scalar_field(s),
18+
plane_orientation='y_axes',
19+
slice_index=10,
20+
)
21+
mlab.outline()
22+
mlab.axes()
23+
mlab.show()

mayavi/demo_pipeline2.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
""" Demo looking at a 3D scalar field
2+
"""
3+
import numpy as np
4+
from mayavi import mlab
5+
6+
x, y, z = np.ogrid[-10:10:20j,
7+
-10:10:20j,
8+
-10:10:20j]
9+
s = np.sin(x*y*z)/(x*y*z)
10+
scalar_field = mlab.pipeline.scalar_field(s)
11+
mlab.pipeline.volume(scalar_field)
12+
mlab.show()

mayavi/first_mlab_script.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
""" Simple mayavi.mlab script to test mayavi. It also defines a few objects so
2+
that one can explore their content (fig, mesh_obj).
3+
"""
4+
5+
from numpy import pi, sin, cos, mgrid
6+
dphi, dtheta = pi/250.0, pi/250.0
7+
[phi,theta] = mgrid[0:pi+dphi*1.5:dphi,0:2*pi+dtheta*1.5:dtheta]
8+
m0 = 4; m1 = 3; m2 = 2; m3 = 3; m4 = 6; m5 = 2; m6 = 6; m7 = 4;
9+
r = sin(m0*phi)**m1 + cos(m2*phi)**m3 + sin(m4*theta)**m5 + cos(m6*theta)**m7
10+
x = r*sin(phi)*cos(theta)
11+
y = r*cos(phi)
12+
z = r*sin(phi)*sin(theta)
13+
14+
# View it.
15+
from mayavi import mlab
16+
mesh_obj = mlab.mesh(x, y, z)
17+
fig = mlab.gcf()
18+
mlab.show()

mayavi/mayavi_globe.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
""" Simulating a globe with the continents drawn.
2+
"""
3+
4+
from mayavi import mlab
5+
from mayavi.sources.builtin_surface import BuiltinSurface
6+
7+
ocean_blue = (0.4, 0.5, 1.0)
8+
r = 6371 # km
9+
10+
sphere = mlab.points3d(0, 0, 0, name='Globe',
11+
scale_mode='none', scale_factor=r * 2.0,
12+
color=ocean_blue, resolution=50)
13+
14+
sphere.actor.property.specular = 0.20
15+
sphere.actor.property.specular_power = 10
16+
17+
continents_src = BuiltinSurface(source='earth', name='Continents')
18+
continents_src.data_source.on_ratio = 1 # detail level
19+
continents_src.data_source.radius = r
20+
continents = mlab.pipeline.surface(continents_src, color=(0, 0, 0))
21+
mlab.show()

mayavi/mayavi_traits_app0.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
""" Simplest possible Traits application containing a Mayavi scene.
2+
3+
Uncomment the additional imports below to experiment with different available
4+
editors (only the toolbar differs).
5+
"""
6+
# Standard imports.
7+
from numpy import sqrt, sin, mgrid
8+
9+
# Enthought imports.
10+
from traits.api import HasTraits, Instance
11+
from traitsui.api import View, Item
12+
13+
from mayavi import mlab
14+
from tvtk.pyface.scene_editor import SceneEditor
15+
from mayavi.tools.mlab_scene_model import MlabSceneModel
16+
17+
# Optional scene classes to use in the SceneEditor (see below)
18+
# This one below is the most complete one with the Mayavi:
19+
#from mayavi.core.ui.mayavi_scene import MayaviScene
20+
# This one below is the most bare, just a vtk view:
21+
#from tvtk.pyface.api import Scene
22+
23+
24+
######################################################################
25+
class MayaviTraitsApp(HasTraits):
26+
27+
# The scene model.
28+
scene = Instance(MlabSceneModel, ())
29+
30+
######################
31+
view = View(Item(name='scene',
32+
editor=SceneEditor(),
33+
#editor=SceneEditor(scene_class=MayaviScene),
34+
#editor=SceneEditor(scene_class=Scene),
35+
show_label=False,
36+
),
37+
resizable=True,
38+
scrollable=True
39+
)
40+
41+
def __init__(self, **traits):
42+
# Has to be done since it's a Traits class
43+
super(MayaviTraitsApp, self).__init__(**traits)
44+
45+
x, y = mgrid[-5:5:100j, -5:5:100j]
46+
r = sqrt(x**2 + y**2)
47+
z = 5*sin(r)/r
48+
49+
# Build the visualization of the data: specifying which Mayavi scene to
50+
# use is necessary
51+
mlab.surf(x, y, z, figure=self.scene.mayavi_scene)
52+
53+
54+
if __name__ == '__main__':
55+
m = MayaviTraitsApp()
56+
m.configure_traits()

mayavi/mayavi_traits_app1.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
""" Next to the simplest Traits application containing a Mayavi scene where we
2+
now offer a view of its engine in addition to the
3+
"""
4+
# Authors: Prabhu Ramachandran <prabhu [at] aero.iitb.ac.in>
5+
# Copyright (c) 2007, Enthought, Inc.
6+
# License: BSD Style.
7+
8+
# Standard imports.
9+
from numpy import sqrt, sin, mgrid
10+
11+
# Enthought imports.
12+
from traits.api import HasTraits, Instance, Property
13+
from traitsui.api import View, Item, HSplit, VSplit, InstanceEditor
14+
from tvtk.pyface.scene_editor import SceneEditor
15+
from mayavi.core.ui.engine_view import EngineView
16+
from mayavi.tools.mlab_scene_model import MlabSceneModel
17+
18+
19+
######################################################################
20+
class MayaviTraitsApp2(HasTraits):
21+
22+
# The scene model.
23+
scene = Instance(MlabSceneModel, ())
24+
25+
# The mayavi engine view.
26+
engine_view = Instance(EngineView)
27+
28+
# The current selection in the engine tree view. This will be a pointing to
29+
# all kinds of objects inside the Mayavi pipeline and displaying its editor
30+
current_selection = Property
31+
32+
######################
33+
view = View(HSplit(VSplit(Item(name='engine_view',
34+
style='custom',
35+
resizable=True,
36+
show_label=False
37+
),
38+
Item(name='current_selection',
39+
editor=InstanceEditor(),
40+
enabled_when='current_selection is not None',
41+
style='custom',
42+
springy=True,
43+
show_label=False),
44+
),
45+
Item(name='scene',
46+
editor=SceneEditor(),
47+
show_label=False,
48+
resizable=True,
49+
height=500,
50+
width=500),
51+
),
52+
resizable=True,
53+
scrollable=True
54+
)
55+
56+
def __init__(self, **traits):
57+
# Has to be done since it's a Traits class
58+
super(MayaviTraitsApp2, self).__init__(**traits)
59+
60+
# Hook the engine view to the engine of the scene
61+
self.engine_view = EngineView(engine=self.scene.engine)
62+
63+
# Hook up the current_selection to change when the one in the engine
64+
# changes. This is probably unnecessary in Traits3 since you can show
65+
# the UI of a sub-object in T3.
66+
self.scene.engine.on_trait_change(self._selection_change,
67+
'current_selection')
68+
69+
self.generate_data_mayavi()
70+
71+
def generate_data_mayavi(self):
72+
from mayavi import mlab
73+
x, y = mgrid[-5:5:100j, -5:5:100j]
74+
r = sqrt(x**2 + y**2)
75+
z = 5*sin(r)/r
76+
mlab.surf(x, y, z)
77+
78+
def _selection_change(self, old, new):
79+
self.trait_property_changed('current_selection', old, new)
80+
81+
def _get_current_selection(self):
82+
return self.scene.engine.current_selection
83+
84+
85+
if __name__ == '__main__':
86+
m = MayaviTraitsApp2()
87+
m.configure_traits()

mayavi/mayavi_traits_app2.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
""" Application of embedding a Mayavi scene in a TraitsUI: control the data
2+
that is displayed.
3+
4+
Derived from Gael Varoquaux's demo:
5+
http://docs.enthought.com/mayavi/mayavi/auto/example_mlab_interactive_dialog.html#example-mlab-interactive-dialog
6+
"""
7+
# Computation imports
8+
from numpy import linspace, pi, cos, sin
9+
10+
# Traits and TraitsUI imports
11+
#############################
12+
from traits.api import HasTraits, Range, Instance, on_trait_change
13+
from traitsui.api import View, Item, HGroup
14+
15+
# Mayavi imports
16+
################
17+
# Mayavi model
18+
from mayavi.tools.mlab_scene_model import MlabSceneModel
19+
# TraitsUI editor for a MlabSceneModel
20+
from tvtk.pyface.scene_editor import SceneEditor
21+
# Style of the scene editor
22+
from mayavi.core.ui.mayavi_scene import MayaviScene
23+
# Storage of a module (renderer)
24+
from mayavi.core.api import PipelineBase
25+
26+
def curve(n_mer, n_long):
27+
""" Data generation
28+
"""
29+
phi = linspace(0, 2*pi, 2000)
30+
return [ cos(phi*n_mer) * (1 + 0.5*cos(n_long*phi)),
31+
sin(phi*n_mer) * (1 + 0.5*cos(n_long*phi)),
32+
0.5*sin(n_long*phi),
33+
sin(phi*n_mer)]
34+
35+
class Visualization(HasTraits):
36+
meridional = Range(1, 30, 6)
37+
transverse = Range(0, 30, 11)
38+
scene = Instance(MlabSceneModel, ())
39+
plot = Instance(PipelineBase)
40+
41+
@on_trait_change('scene.activated')
42+
def create_plot(self):
43+
""" Wait before the scene is activated before trying to add data,
44+
modules, filers into it.
45+
"""
46+
x, y, z, t = curve(self.meridional, self.transverse)
47+
self.plot = self.scene.mlab.plot3d(x, y, z, t, colormap='Spectral')
48+
49+
@on_trait_change('meridional,transverse')
50+
def update_plot(self):
51+
""" Recompute the data
52+
"""
53+
x, y, z, t = curve(self.meridional, self.transverse)
54+
self.plot.mlab_source.set(x=x, y=y, z=z, scalars=t)
55+
56+
57+
# the layout of the dialog created
58+
view = View(Item('scene', editor=SceneEditor(scene_class=MayaviScene),
59+
height=250, width=300, show_label=False),
60+
HGroup(
61+
'_', 'meridional', 'transverse',
62+
),
63+
)
64+
65+
if __name__ == "__main__":
66+
visualization = Visualization()
67+
visualization.configure_traits()

0 commit comments

Comments
 (0)