Skip to content

Commit

Permalink
Merge pull request #1682 from os-gabe/panzoom-fix
Browse files Browse the repository at this point in the history
Fixes #1681 by keeping a MatrixTransformation that gets modified on e…
  • Loading branch information
djhoese committed Aug 26, 2019
2 parents 8273e1e + 96a42ae commit 75d8bfb
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions vispy/scene/cameras/panzoom.py
Expand Up @@ -49,6 +49,7 @@ def __init__(self, rect=(0, 0, 1, 1), aspect=None, **kwargs):
super(PanZoomCamera, self).__init__(**kwargs)

self.transform = STTransform()
self.tf_mat = MatrixTransform()

# Set camera attributes
self.aspect = aspect
Expand Down Expand Up @@ -151,7 +152,8 @@ def rect(self, args):
@property
def center(self):
rect = self._rect
return 0.5 * (rect.left+rect.right), 0.5 * (rect.top+rect.bottom), 0
return 0.5 * (rect.left + rect.right), 0.5 * (rect.top +
rect.bottom), 0

@center.setter
def center(self, center):
Expand Down Expand Up @@ -206,7 +208,7 @@ def viewbox_mouse_event(self, event):

if event.type == 'mouse_wheel':
center = self._scene_transform.imap(event.pos)
self.zoom((1 + self.zoom_factor) ** (-event.delta[1] * 30), center)
self.zoom((1 + self.zoom_factor)**(-event.delta[1] * 30), center)
event.handled = True

elif event.type == 'mouse_move':
Expand All @@ -223,14 +225,14 @@ def viewbox_mouse_event(self, event):
p2 = np.array(event.pos)[:2]
p1s = self._transform.imap(p1)
p2s = self._transform.imap(p2)
self.pan(p1s-p2s)
self.pan(p1s - p2s)
event.handled = True
elif 2 in event.buttons and not modifiers:
# Zoom
p1c = np.array(event.last_event.pos)[:2]
p2c = np.array(event.pos)[:2]
scale = ((1 + self.zoom_factor) **
((p1c-p2c) * np.array([1, -1])))
scale = ((1 + self.zoom_factor)**((p1c - p2c) *
np.array([1, -1])))
center = self._transform.imap(event.press_event.pos[:2])
self.zoom(scale, center)
event.handled = True
Expand All @@ -252,8 +254,8 @@ def _update_transform(self):
# apply scale ratio constraint
if self._aspect is not None:
# Aspect ratio of the requested range
requested_aspect = (rect.width / rect.height
if rect.height != 0 else 1)
requested_aspect = (rect.width /
rect.height if rect.height != 0 else 1)
# Aspect ratio of the viewbox
view_aspect = vbr.width / vbr.height if vbr.height != 0 else 1
# View aspect ratio needed to obey the scale constraint
Expand All @@ -273,20 +275,18 @@ def _update_transform(self):
# Apply mapping between viewbox and cam view
self.transform.set_mapping(self._real_rect, vbr, update=False)
# Scale z, so that the clipping planes are between -alot and +alot
self.transform.zoom((1, 1, 1/d))
self.transform.zoom((1, 1, 1 / d))

# We've now set self.transform, which represents our 2D
# transform When up is +z this is all. In other cases,
# self.transform is now set up correctly to allow pan/zoom, but
# for the scene we need a different (3D) mapping. When there
# is a minus in up, we simply look at the scene from the other
# side (as if z was flipped).

if self.up == '+z':
thetransform = self.transform
self.tf_mat.matrix = self.transform.as_matrix().matrix
else:
rr = self._real_rect
tr = MatrixTransform()
d = d if (self.up[0] == '+') else -d
pp1 = [(vbr.left, vbr.bottom, 0), (vbr.left, vbr.top, 0),
(vbr.right, vbr.bottom, 0), (vbr.left, vbr.bottom, 1)]
Expand All @@ -300,9 +300,9 @@ def _update_transform(self):
elif self.up[1] == 'x':
pp2 = [(0, rr.left, rr.bottom), (0, rr.left, rr.top),
(0, rr.right, rr.bottom), (d, rr.left, rr.bottom)]

# Apply
tr.set_mapping(np.array(pp2), np.array(pp1))
thetransform = tr
self.tf_mat.set_mapping(np.array(pp2), np.array(pp1))

# Set on viewbox
self._set_scene_transform(thetransform)
self._set_scene_transform(self.tf_mat)

0 comments on commit 75d8bfb

Please sign in to comment.