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 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
|
# -*- coding: utf-8 -*-
# vispy: testskip
# -----------------------------------------------------------------------------
# Copyright (c) Vispy Development Team. All Rights Reserved.
# Distributed under the (new) BSD License. See LICENSE.txt for more info.
# -----------------------------------------------------------------------------
"""
Compare an optimal pan/zoom implementation to the same functionality
provided by scenegraph.
Use --vispy-cprofile to see an overview of time spent in all functions.
Use util.profiler and --vispy-profile=ClassName.method_name for more directed
profiling measurements.
"""
import numpy as np
import math
from vispy import gloo, app, scene
from vispy.visuals import Visual
from vispy.visuals.shaders import Function, Variable
from vispy.visuals.transforms import TransformSystem, BaseTransform
from vispy.util.profiler import Profiler
class PanZoomTransform(BaseTransform):
glsl_map = """
vec4 pz_transform_map(vec4 pos) {
return vec4($zoom * (pos.xy + $pan), 0., 1.);
}
"""
glsl_imap = """
vec4 pz_transform_imap(vec2 pos) {
return vec4(pos / $zoom - $pan, 0, 1);
}
"""
Linear = True
Orthogonal = True
NonScaling = False
Isometric = False
def __init__(self):
super(PanZoomTransform, self).__init__()
self._pan = None
self._zoom = None
@property
def pan(self):
if isinstance(self._pan, Variable):
return np.array(self._pan.value, dtype=np.float32)
else:
raise NotImplementedError()
@pan.setter
def pan(self, value):
if isinstance(value, Variable):
self._pan = value
self._shader_map['pan'] = self._pan
elif isinstance(self._pan, Variable):
self._pan.value = value
else:
raise NotImplementedError()
@property
def zoom(self):
if isinstance(self._zoom, Variable):
return np.array(self._zoom.value, dtype=np.float32)
else:
raise NotImplementedError()
@zoom.setter
def zoom(self, value):
if isinstance(value, Variable):
self._zoom = value
self._shader_map['zoom'] = self._zoom
elif isinstance(self._zoom, Variable):
self._zoom.value = value
else:
raise NotImplementedError()
def map(self, coords):
if not isinstance(coords, np.ndarray):
coords = np.array(coords)
return self.zoom[None, :] * (coords + self.pan[None, :])
def imap(self, coords):
if not isinstance(coords, np.ndarray):
coords = np.array(coords)
return (coords / self.zoom[None, :]) - self.pan[None, :]
class PanZoomCanvas(app.Canvas):
def __init__(self, **kwargs):
super(PanZoomCanvas, self).__init__(keys='interactive', **kwargs)
self._visuals = []
self._pz = PanZoomTransform()
self._pz.pan = Variable('uniform vec2 u_pan', (0, 0))
self._pz.zoom = Variable('uniform vec2 u_zoom', (1, 1))
self.width, self.height = self.size
self.context.set_viewport(0, 0, self.physical_size[0],
self.physical_size[1])
self.context.set_state(clear_color='black', blend=True,
blend_func=('src_alpha', 'one_minus_src_alpha'))
self._tr = TransformSystem(self)
self.show()
def on_resize(self, event):
self.width, self.height = event.size
self.context.set_viewport(0, 0, event.physical_size[0],
event.physical_size[1])
def _normalize(self, x_y):
x, y = x_y
w, h = float(self.width), float(self.height)
return x/(w/2.)-1., y/(h/2.)-1.
def bounds(self):
pan_x, pan_y = self._pz.pan
zoom_x, zoom_y = self._pz.zoom
xmin = -1 / zoom_x - pan_x
xmax = +1 / zoom_x - pan_x
ymin = -1 / zoom_y - pan_y
ymax = +1 / zoom_y - pan_y
return (xmin, ymin, xmax, ymax)
def on_mouse_move(self, event):
if event.is_dragging and not event.modifiers:
x0, y0 = self._normalize(event.press_event.pos)
x1, y1 = self._normalize(event.last_event.pos)
x, y = self._normalize(event.pos)
dx, dy = x - x1, -(y - y1)
button = event.press_event.button
pan_x, pan_y = self._pz.pan
zoom_x, zoom_y = self._pz.zoom
if button == 1:
self._pz.pan = (pan_x + dx/zoom_x,
pan_y + dy/zoom_y)
elif button == 2:
zoom_x_new, zoom_y_new = (zoom_x * math.exp(2.5 * dx),
zoom_y * math.exp(2.5 * dy))
self._pz.zoom = (zoom_x_new, zoom_y_new)
self._pz.pan = (pan_x - x0 * (1./zoom_x - 1./zoom_x_new),
pan_y + y0 * (1./zoom_y - 1./zoom_y_new))
self.update()
def on_mouse_wheel(self, event):
prof = Profiler() # noqa
if not event.modifiers:
dx = np.sign(event.delta[1])*.05
x0, y0 = self._normalize(event.pos)
pan_x, pan_y = self._pz.pan
zoom_x, zoom_y = self._pz.zoom
zoom_x_new, zoom_y_new = (zoom_x * math.exp(2.5 * dx),
zoom_y * math.exp(2.5 * dx))
self._pz.zoom = (zoom_x_new, zoom_y_new)
self._pz.pan = (pan_x - x0 * (1./zoom_x - 1./zoom_x_new),
pan_y + y0 * (1./zoom_y - 1./zoom_y_new))
self.update()
def on_key_press(self, event):
if event.key == 'R':
self._pz.zoom = (1., 1.)
self._pz.pan = (0., 0.)
self.update()
def add_visual(self, name, value):
value.shared_program.vert['transform'] = self._pz
value.events.update.connect(self.update)
self._visuals.append(value)
def __setattr__(self, name, value):
if isinstance(value, Visual):
self.add_visual(name, value)
super(PanZoomCanvas, self).__setattr__(name, value)
@property
def visuals(self):
return self._visuals
def on_draw(self, event):
prof = Profiler()
self.context.clear()
for visual in self.visuals:
visual.draw()
prof('draw visual')
X_TRANSFORM = """
float get_x(float x_index) {
// 'x_index' is between 0 and nsamples.
return -1. + 2. * x_index / (float($nsamples) - 1.);
}
"""
Y_TRANSFORM = """
float get_y(float y_index, float sample) {
// 'y_index' is between 0 and nsignals.
float a = float($scale) / float($nsignals);
float b = -1. + 2. * (y_index + .5) / float($nsignals);
return a * sample + b;
}
"""
DISCRETE_CMAP = """
vec3 get_color(float index) {
float x = (index + .5) / float($ncolors);
return texture2D($colormap, vec2(x, .5)).rgb;
}
"""
class SignalsVisual(Visual):
VERTEX_SHADER = """
attribute float a_position;
attribute vec2 a_index;
varying vec2 v_index;
uniform float u_nsignals;
uniform float u_nsamples;
void main() {
vec4 position = vec4($get_x(a_index.y),
$get_y(a_index.x, a_position), 0., 1.);
gl_Position = $transform(position);
v_index = a_index;
}
"""
FRAGMENT_SHADER = """
varying vec2 v_index;
void main() {
gl_FragColor = vec4($get_color(v_index.x), 1.);
// Discard vertices between two signals.
if ((fract(v_index.x) > 0.))
discard;
}
"""
def __init__(self, data):
Visual.__init__(self, self.VERTEX_SHADER, self.FRAGMENT_SHADER)
nsignals, nsamples = data.shape
# nsamples, nsignals = data.shape
self._data = data
a_index = np.c_[np.repeat(np.arange(nsignals), nsamples),
np.tile(np.arange(nsamples), nsignals)
].astype(np.float32)
# Doesn't seem to work nor to be very efficient.
# indices = nsignals * np.arange(nsamples)
# indices = indices[None, :] + np.arange(nsignals)[:, None]
# indices = indices.flatten().astype(np.uint32)
# self._ibuffer = gloo.IndexBuffer(indices)
self._buffer = gloo.VertexBuffer(data.reshape(-1, 1))
self.shared_program['a_position'] = self._buffer
self.shared_program['a_index'] = a_index
x_transform = Function(X_TRANSFORM)
x_transform['nsamples'] = nsamples
self.shared_program.vert['get_x'] = x_transform
y_transform = Function(Y_TRANSFORM)
y_transform['scale'] = Variable('uniform float u_signal_scale', 1.)
y_transform['nsignals'] = nsignals
self.shared_program.vert['get_y'] = y_transform
self._y_transform = y_transform
colormap = Function(DISCRETE_CMAP)
rng = np.random.RandomState(0)
cmap = rng.uniform(size=(1, nsignals, 3),
low=.5, high=.9).astype(np.float32)
tex = gloo.Texture2D((cmap * 255).astype(np.uint8))
colormap['colormap'] = Variable('uniform sampler2D u_colormap', tex)
colormap['ncolors'] = nsignals
self.shared_program.frag['get_color'] = colormap
self._draw_mode = 'line_strip'
self.set_gl_state('translucent', depth_test=False)
@property
def data(self):
return self._data
@data.setter
def data(self, value):
self._data = value
self._buffer.set_subdata(value.reshape(-1, 1))
self.update()
@property
def signal_scale(self):
return self._y_transform['scale'].value
@signal_scale.setter
def signal_scale(self, value):
self._y_transform['scale'].value = value
self.update()
def _prepare_draw(self, view=None):
"""This method is called immediately before each draw.
The *view* argument indicates which view is about to be drawn.
"""
pass
Signals = scene.visuals.create_visual_node(SignalsVisual)
if __name__ == '__main__':
data = np.random.normal(size=(128, 1000)).astype(np.float32)
pzcanvas = PanZoomCanvas(position=(400, 300), size=(800, 600),
title="PanZoomCanvas", vsync=False)
visual = SignalsVisual(data)
pzcanvas.add_visual('signal', visual)
scanvas = scene.SceneCanvas(show=True, keys='interactive',
title="SceneCanvas", vsync=False)
view = scanvas.central_widget.add_view('panzoom')
svisual = Signals(data, parent=view.scene)
view.camera.set_range([-0.9, 0.9], [-0.9, 0.9])
import sys
if sys.flags.interactive != 1:
app.run()
|