-
Notifications
You must be signed in to change notification settings - Fork 0
/
scatter.py
130 lines (109 loc) · 3.85 KB
/
scatter.py
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
from vispy import gloo
from vispy import app
import numpy as np
from math import exp
n = 10000
v_position = 0.25 * np.random.randn(n, 2).astype(np.float32)
v_color = np.random.uniform(0, 1, (n, 3)).astype(np.float32)
v_size = np.random.uniform(2, 12, (n, 1)).astype(np.float32)
VERT_SHADER = """
#version 120
attribute vec2 a_position;
attribute vec3 a_color;
attribute float a_size;
uniform vec2 u_pan;
uniform vec2 u_scale;
varying vec4 v_fg_color;
varying vec4 v_bg_color;
varying float v_radius;
varying float v_linewidth;
varying float v_antialias;
void main (void) {
v_radius = a_size;
v_linewidth = 1.0;
v_antialias = 1.0;
v_fg_color = vec4(0.0,0.0,0.0,0.5);
v_bg_color = vec4(a_color, 1.0);
vec2 position_tr = u_scale * (a_position + u_pan);
gl_Position = vec4(position_tr, 0.0, 1.0);
gl_PointSize = 2.0*(v_radius + v_linewidth + 1.5*v_antialias);
}
"""
FRAG_SHADER = """
#version 120
varying vec4 v_fg_color;
varying vec4 v_bg_color;
varying float v_radius;
varying float v_linewidth;
varying float v_antialias;
void main()
{
float size = 2.0*(v_radius + v_linewidth + 1.5*v_antialias);
float t = v_linewidth/2.0-v_antialias;
float r = length((gl_PointCoord.xy - vec2(0.5,0.5))*size);
float d = abs(r - v_radius) - t;
if( d < 0.0 )
gl_FragColor = v_fg_color;
else
{
float alpha = d/v_antialias;
alpha = exp(-alpha*alpha);
if (r > v_radius)
gl_FragColor = vec4(v_fg_color.rgb, alpha*v_fg_color.a);
else
gl_FragColor = mix(v_bg_color, v_fg_color, alpha);
}
}
"""
class Canvas(app.Canvas):
def __init__(self):
app.Canvas.__init__(self, close_keys='escape')
self.program = gloo.Program(VERT_SHADER, FRAG_SHADER)
self.program['a_color'] = gloo.VertexBuffer(v_color)
self.program['a_position'] = gloo.VertexBuffer(v_position)
self.program['a_size'] = gloo.VertexBuffer(v_size)
self.program['u_pan'] = (0., 0.)
self.program['u_scale'] = (1., 1.)
def on_initialize(self, event):
gloo.set_state(clear_color=(1, 1, 1, 1), blend=True,
blend_func=('src_alpha', 'one_minus_src_alpha'))
def _normalize(self, (x, y)):
w, h = float(self.width), float(self.height)
return x/(w/2.)-1., y/(h/2.)-1.
def pan(self, (dx, dy)):
pan_x, pan_y = self.program['u_pan']
scale_x, scale_y = self.program['u_scale']
self.program['u_pan'] = (pan_x+dx/scale_x, pan_y+dy/scale_y)
self.update()
def zoom(self, (dx, dy), (x0, y0)=(0,0)):
pan_x, pan_y = self.program['u_pan']
scale_x, scale_y = self.program['u_scale']
scale_x_new, scale_y_new = scale_x * exp(2.5*dx), scale_y * exp(2.5*dy)
self.program['u_scale'] = (scale_x_new, scale_y_new)
self.program['u_pan'] = (pan_x - x0 * (1./scale_x - 1./scale_x_new),
pan_y + y0 * (1./scale_y - 1./scale_y_new))
self.update()
def on_mouse_move(self, event):
if event.is_dragging:
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
if button == 1:
self.pan((dx, dy))
elif button == 2:
self.zoom((dx, dy), (x0, y0))
def on_mouse_wheel(self, event):
d = event.delta[1] * .1
self.zoom((d, d))
def on_resize(self, event):
self.width, self.height = event.size
gloo.set_viewport(0, 0, self.width, self.height)
def on_draw(self, event):
gloo.clear()
self.program.draw('points')
if __name__ == '__main__':
c = Canvas()
c.show()
app.run()