Skip to content

Commit 075de95

Browse files
larsoneragramfort
authored andcommitted
ENH: Add terrain interaction (#178)
1 parent ddba0e1 commit 075de95

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

examples/plot_meg_inverse_solution.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
print(__doc__)
1515

16+
path = os.path.dirname(__file__)
17+
1618
"""
1719
define subject, surface and hemisphere(s) to plot
1820
"""
@@ -22,7 +24,8 @@
2224
"""
2325
create Brain object for visualization
2426
"""
25-
brain = Brain(subject_id, hemi, surface, size=(800, 400))
27+
brain = Brain(subject_id, hemi, surface, size=(800, 400),
28+
interaction='terrain')
2629

2730
"""
2831
label for time annotation in milliseconds
@@ -37,7 +40,7 @@ def time_label(t):
3740
read MNE dSPM inverse solution
3841
"""
3942
for hemi in ['lh', 'rh']:
40-
stc_fname = os.path.join('example_data/meg_source_estimate-' +
43+
stc_fname = os.path.join(path, 'example_data', 'meg_source_estimate-' +
4144
hemi + '.stc')
4245
stc = read_stc(stc_fname)
4346

surfer/tests/test_viz.py

+5
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,13 @@ def test_brains():
101101
alpha=alpha, size=s, background=bg, foreground=fg,
102102
figure=fig, subjects_dir=sd)
103103
brain.close()
104+
brain = Brain(subject_id, hemi, surf, subjects_dir=sd,
105+
interaction='terrain')
106+
brain.close()
104107
assert_raises(ValueError, Brain, subject_id, 'lh', 'inflated',
105108
subjects_dir='')
109+
assert_raises(ValueError, Brain, subject_id, 'lh', 'inflated',
110+
interaction='foo', subjects_dir=sd)
106111

107112

108113
@requires_fsaverage

surfer/viz.py

+19-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from mayavi.core.ui.mayavi_scene import MayaviScene
2020
from traits.api import (HasTraits, Range, Int, Float,
2121
Bool, Enum, on_trait_change, Instance)
22+
from tvtk.api import tvtk
2223

2324
from . import utils, io
2425
from .utils import (Surface, verbose, create_color_lut, _get_subjects_dir,
@@ -189,7 +190,8 @@ def _force_render(figures, backend):
189190
_gui.process_events()
190191

191192

192-
def _make_viewer(figure, n_row, n_col, title, scene_size, offscreen):
193+
def _make_viewer(figure, n_row, n_col, title, scene_size, offscreen,
194+
interaction='trackball'):
193195
"""Triage viewer creation
194196
195197
If n_row == n_col == 1, then we can use a Mayavi figure, which
@@ -219,6 +221,11 @@ def _make_viewer(figure, n_row, n_col, title, scene_size, offscreen):
219221
else:
220222
window = _MlabGenerator(n_row, n_col, w, h, title)
221223
figures, _v = window._get_figs_view()
224+
if interaction == 'terrain': # "trackball" is default
225+
for figure in figures:
226+
for f in figure:
227+
f.scene.interactor.interactor_style = \
228+
tvtk.InteractorStyleTerrain()
222229
else:
223230
if isinstance(figure, int): # use figure with specified id
224231
figure = [mlab.figure(figure, size=scene_size)]
@@ -351,6 +358,9 @@ class Brain(object):
351358
If True, rendering will be done offscreen (not shown). Useful
352359
mostly for generating images or screenshots, but can be buggy.
353360
Use at your own risk.
361+
interaction : str
362+
Can be "trackball" (default) or "terrain", i.e. a turntable-style
363+
camera.
354364
355365
Attributes
356366
----------
@@ -362,7 +372,8 @@ def __init__(self, subject_id, hemi, surf, title=None,
362372
cortex="classic", alpha=1.0, size=800, background="black",
363373
foreground="white", figure=None, subjects_dir=None,
364374
views=['lat'], offset=True, show_toolbar=False,
365-
offscreen=False, config_opts=None, curv=None):
375+
offscreen=False, interaction='trackball',
376+
config_opts=None, curv=None):
366377

367378
# Keep backwards compatability
368379
if config_opts is not None:
@@ -392,6 +403,10 @@ def __init__(self, subject_id, hemi, surf, title=None,
392403
if not curv:
393404
cortex = None
394405

406+
if not isinstance(interaction, string_types) or \
407+
interaction not in ('trackball', 'terrain'):
408+
raise ValueError('interaction must be "trackball" or "terrain", '
409+
'got "%s"' % (interaction,))
395410
col_dict = dict(lh=1, rh=1, both=1, split=2)
396411
n_col = col_dict[hemi]
397412
if hemi not in col_dict.keys():
@@ -433,7 +448,8 @@ def __init__(self, subject_id, hemi, surf, title=None,
433448
# deal with making figures
434449
self._set_window_properties(size, background, foreground)
435450
figures, _v = _make_viewer(figure, n_row, n_col, title,
436-
self._scene_size, offscreen)
451+
self._scene_size, offscreen,
452+
interaction)
437453
self._figures = figures
438454
self._v = _v
439455
self._window_backend = 'Mayavi' if self._v is None else 'TraitsUI'

0 commit comments

Comments
 (0)