1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
|
# -*- coding: utf-8 -*-
# Copyright (c) Vispy Development Team. All Rights Reserved.
# Distributed under the (new) BSD License. See LICENSE.txt for more info.
from __future__ import division
import numpy as np
from .base_camera import BaseCamera
from ...geometry import Rect
from ...visuals.transforms import STTransform, MatrixTransform
DEFAULT_RECT_TUPLE = (0, 0, 1, 1)
class PanZoomCamera(BaseCamera):
"""Camera implementing 2D pan/zoom mouse interaction.
For this camera, the ``scale_factor`` indicates the zoom level, and
the ``center`` indicates the center position of the view.
By default, this camera inverts the y axis of the scene. This usually
results in the scene +y axis pointing upward because widgets (including
ViewBox) have their +y axis pointing downward.
Parameters
----------
rect : Rect
A Rect object or 4-element tuple that specifies the rectangular
area to show.
aspect : float | None
The aspect ratio (i.e. scaling) between x and y dimension of
the scene. E.g. to show a square image as square, the aspect
should be 1. If None (default) the x and y dimensions are scaled
independently.
**kwargs : dict
Keyword arguments to pass to `BaseCamera`.
Notes
-----
Interaction:
* LMB: pan the view
* RMB or scroll: zooms the view
"""
_state_props = BaseCamera._state_props + ('rect', )
def __init__(self, rect=DEFAULT_RECT_TUPLE, aspect=None, **kwargs):
super(PanZoomCamera, self).__init__(**kwargs)
self.transform = STTransform()
self.tf_mat = MatrixTransform()
# Set camera attributes
self.aspect = aspect
self._rect = None
self.rect = rect
@property
def aspect(self):
"""The ratio between the x and y dimension. E.g. to show a
square image as square, the aspect should be 1. If None, the
dimensions are scaled automatically, dependening on the
available space. Otherwise the ratio between the dimensions
is fixed.
"""
return self._aspect
@aspect.setter
def aspect(self, value):
if value is None:
self._aspect = None
else:
self._aspect = float(value)
self.view_changed()
def zoom(self, factor, center=None):
"""Zoom in (or out) at the given center
Parameters
----------
factor : float or tuple
Fraction by which the scene should be zoomed (e.g. a factor of 2
causes the scene to appear twice as large).
center : tuple of 2-4 elements
The center of the view. If not given or None, use the
current center.
"""
# Init some variables
center = center if (center is not None) else self.center
assert len(center) in (2, 3, 4)
# Get scale factor, take scale ratio into account
if np.isscalar(factor):
scale = [factor, factor]
else:
if len(factor) != 2:
raise TypeError("factor must be scalar or length-2 sequence.")
scale = list(factor)
if self.aspect is not None:
scale[0] = scale[1]
# Make a new object (copy), so that allocation will
# trigger view_changed:
rect = Rect(self.rect)
# Get space from given center to edges
left_space = center[0] - rect.left
right_space = rect.right - center[0]
bottom_space = center[1] - rect.bottom
top_space = rect.top - center[1]
# Scale these spaces
rect.left = center[0] - left_space * scale[0]
rect.right = center[0] + right_space * scale[0]
rect.bottom = center[1] - bottom_space * scale[1]
rect.top = center[1] + top_space * scale[1]
self.rect = rect
def pan(self, *pan):
"""Pan the view.
Parameters
----------
*pan : length-2 sequence
The distance to pan the view, in the coordinate system of the
scene.
"""
if len(pan) == 1:
pan = pan[0]
self.rect = self.rect + pan
@property
def rect(self):
"""The rectangular border of the ViewBox visible area.
This is expressed in the coordinate system of the scene.
See :class:`~vispy.geometry.rect.Rect` for different ways this can
be specified.
Note that the rectangle can have negative width or height, in
which case the corresponding dimension is flipped (this flipping
is independent from the camera's ``flip`` property).
"""
return self._rect
@rect.setter
def rect(self, args):
if isinstance(args, tuple):
rect = Rect(*args)
else:
rect = Rect(args)
if self._rect != rect:
self._rect = rect
self.view_changed()
@property
def center(self):
rect = self._rect
return (*rect.center, 0)
@center.setter
def center(self, center):
if not (isinstance(center, (tuple, list)) and len(center) in (2, 3)):
raise ValueError('center must be a 2 or 3 element tuple')
rect = Rect(self.rect) or Rect(*DEFAULT_RECT_TUPLE) # make a copy of self.rect
rect.center = center[:2]
self.rect = rect
def _set_range(self, init):
if init and self._rect is not None:
return
# Convert limits to rect
w = self._xlim[1] - self._xlim[0]
h = self._ylim[1] - self._ylim[0]
self.rect = self._xlim[0], self._ylim[0], w, h
def viewbox_resize_event(self, event):
"""Modify the data aspect and scale factor, to adjust to
the new window size.
Parameters
----------
event : instance of Event
The event.
"""
self.view_changed()
def viewbox_mouse_event(self, event):
"""
The SubScene received a mouse event; update transform
accordingly.
Parameters
----------
event : instance of Event
The event.
"""
if event.handled or not self.interactive:
return
# Scrolling
BaseCamera.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)
event.handled = True
elif event.type == 'gesture_zoom':
center = self._scene_transform.imap(event.pos)
self.zoom(1 - event.scale, center)
event.handled = True
elif event.type == 'mouse_move':
if event.press_event is None:
return
modifiers = event.mouse_event.modifiers
p1 = event.mouse_event.press_event.pos
p2 = event.mouse_event.pos
if 1 in event.buttons and not modifiers:
# Translate
p1 = np.array(event.last_event.pos)[:2]
p2 = np.array(event.pos)[:2]
p1s = self._transform.imap(p1)
p2s = self._transform.imap(p2)
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])))
center = self._transform.imap(event.press_event.pos[:2])
self.zoom(scale, center)
event.handled = True
else:
event.handled = False
elif event.type == 'mouse_press':
# accept the event if it is button 1 or 2.
# This is required in order to receive future events
event.handled = event.button in [1, 2]
else:
event.handled = False
def _update_transform(self):
if self._resetting: # base camera linking operation
return
rect = self.rect
self._real_rect = Rect(rect)
vbr = self._viewbox.rect.flipped(x=self.flip[0], y=(not self.flip[1]))
d = self.depth_value
# 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)
# 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
constrained_aspect = abs(view_aspect / self._aspect)
if requested_aspect > constrained_aspect:
# view range needs to be taller than requested
dy = 0.5 * (rect.width / constrained_aspect - rect.height)
self._real_rect.top += dy
self._real_rect.bottom -= dy
else:
# view range needs to be wider than requested
dx = 0.5 * (rect.height * constrained_aspect - rect.width)
self._real_rect.left -= dx
self._real_rect.right += dx
# 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))
# 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':
self.tf_mat.matrix = self.transform.as_matrix().matrix
else:
rr = self._real_rect
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)]
# Get Mapping
if self.up[1] == 'z':
pp2 = [(rr.left, rr.bottom, 0), (rr.left, rr.top, 0),
(rr.right, rr.bottom, 0), (rr.left, rr.bottom, d)]
elif self.up[1] == 'y':
pp2 = [(rr.left, 0, rr.bottom), (rr.left, 0, rr.top),
(rr.right, 0, rr.bottom), (rr.left, d, rr.bottom)]
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
self.tf_mat.set_mapping(np.array(pp2), np.array(pp1))
# Set on viewbox
self._set_scene_transform(self.tf_mat)
|