Skip to content

Commit

Permalink
[ENH] More natural zoom (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexrockhill authored Nov 12, 2023
1 parent 290e8e0 commit c245654
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
23 changes: 18 additions & 5 deletions mne_gui_addons/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

_IMG_LABELS = [["I", "P"], ["I", "L"], ["P", "L"]]
_ZOOM_STEP_SIZE = 5
_ZOOM_BORDER = 1 / 5


@verbose
Expand Down Expand Up @@ -422,11 +423,22 @@ def _zoom(self, sign=1, draw=False):
ymin, ymax = fig.axes[0].get_ylim()
xmid = (xmin + xmax) / 2
ymid = (ymin + ymax) / 2
if sign == 1: # may need to shift if zooming in
if abs(xmid - xcur) > delta / 2 * rx:
xmid += delta * np.sign(xcur - xmid) * rx
if abs(ymid - ycur) > delta / 2 * ry:
ymid += delta * np.sign(ycur - ymid) * ry
if sign >= 0: # may need to shift if zooming in or clicking
xedge = min([xmax - xcur, xcur - xmin])
if xedge < (xmax - xmin) * _ZOOM_BORDER:
xmid += np.sign(xcur - xmid) * (
(xmax - xmin) * _ZOOM_BORDER - xedge
)
if xcur < xmin or xcur > xmax: # out of view, reset
xmid = xcur
yedge = min([ymax - ycur, ycur - ymin])
if yedge < (ymax - ymin) * _ZOOM_BORDER:
ymid += np.sign(ycur - ymid) * (
(ymax - ymin) * _ZOOM_BORDER - yedge
)
if ycur < ymin or ycur > ymax: # out of view, reset
ymid = ycur

xwidth = (xmax - xmin) / 2 - delta * rx
ywidth = (ymax - ymin) / 2 - delta * ry
if xwidth <= 0 or ywidth <= 0:
Expand Down Expand Up @@ -620,6 +632,7 @@ def _on_click(self, event, axis):
logger.debug(f"Using voxel {list(xyz)}")
ras = apply_trans(self._vox_ras_t, xyz)
self._set_ras(ras)
self._zoom(sign=0, draw=True)

def _update_moved(self):
"""Update when cursor position changes."""
Expand Down
2 changes: 1 addition & 1 deletion mne_gui_addons/_ieeg_locate.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,8 +553,8 @@ def _update_ch_selection(self):
self._update_group()
if not np.isnan(self._chs[name]).any():
self._set_ras(self._chs[name])
self._zoom(sign=0, draw=True)
self._update_camera(render=True)
self._draw()

def _go_to_ch(self, index):
"""Change current channel to the item selected."""
Expand Down

0 comments on commit c245654

Please sign in to comment.