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 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
|
# -*- coding: utf-8 -*-
# -----------------------------------------------------------------------------
# Copyright (c) Vispy Development Team. All Rights Reserved.
# Distributed under the (new) BSD License. See LICENSE.txt for more info.
# -----------------------------------------------------------------------------
"""
Marker Visual and shader definitions.
"""
import numpy as np
from ..color import ColorArray
from ..gloo import VertexBuffer, _check_valid
from .shaders import Function, Variable
from .visual import Visual
vert = """
uniform float u_antialias;
uniform float u_px_scale;
uniform float u_scale;
attribute vec3 a_position;
attribute vec4 a_fg_color;
attribute vec4 a_bg_color;
attribute float a_edgewidth;
attribute float a_size;
varying vec4 v_fg_color;
varying vec4 v_bg_color;
varying float v_edgewidth;
varying float v_antialias;
void main (void) {
$v_size = a_size * u_px_scale * u_scale;
v_edgewidth = a_edgewidth * float(u_px_scale);
v_antialias = u_antialias;
v_fg_color = a_fg_color;
v_bg_color = a_bg_color;
gl_Position = $transform(vec4(a_position,1.0));
float edgewidth = max(v_edgewidth, 1.0);
gl_PointSize = ($v_size) + 4.*(edgewidth + 1.5*v_antialias);
}
"""
frag = """#version 120
varying vec4 v_fg_color;
varying vec4 v_bg_color;
varying float v_edgewidth;
varying float v_antialias;
void main()
{
// Discard plotting marker body and edge if zero-size
if ($v_size <= 0.)
discard;
float edgewidth = max(v_edgewidth, 1.0);
float edgealphafactor = min(v_edgewidth, 1.0);
float size = $v_size + 4.*(edgewidth + 1.5*v_antialias);
// factor 6 for acute edge angles that need room as for star marker
// The marker function needs to be linked with this shader
float r = $marker(gl_PointCoord, size);
// it takes into account an antialising layer
// of size v_antialias inside the edge
// r:
// [-e/2-a, -e/2+a] antialising face-edge
// [-e/2+a, e/2-a] core edge (center 0, diameter e-2a = 2t)
// [e/2-a, e/2+a] antialising edge-background
float t = 0.5*v_edgewidth - v_antialias;
float d = abs(r) - t;
vec4 edgecolor = vec4(v_fg_color.rgb, edgealphafactor*v_fg_color.a);
if (r > 0.5*v_edgewidth + v_antialias)
{
// out of the marker (beyond the outer edge of the edge
// including transition zone due to antialiasing)
discard;
}
else if (d < 0.0)
{
// inside the width of the edge
// (core, out of the transition zone for antialiasing)
gl_FragColor = edgecolor;
}
else
{
if (v_edgewidth == 0.)
{// no edge
if (r > -v_antialias)
{
float alpha = 1.0 + r/v_antialias;
alpha = exp(-alpha*alpha);
gl_FragColor = vec4(v_bg_color.rgb, alpha*v_bg_color.a);
}
else
{
gl_FragColor = v_bg_color;
}
}
else
{
float alpha = d/v_antialias;
alpha = exp(-alpha*alpha);
if (r > 0.)
{
// outer part of the edge: fade out into the background...
gl_FragColor = vec4(edgecolor.rgb, alpha*edgecolor.a);
}
else
{
gl_FragColor = mix(v_bg_color, edgecolor, alpha);
}
}
}
}
"""
disc = """
float disc(vec2 pointcoord, float size)
{
float r = length((pointcoord.xy - vec2(0.5,0.5))*size);
r -= $v_size/2.;
return r;
}
"""
arrow = """
const float sqrt2 = sqrt(2.);
float rect(vec2 pointcoord, float size)
{
float half_size = $v_size/2.;
float ady = abs(pointcoord.y -.5)*size;
float dx = (pointcoord.x -.5)*size;
float r1 = abs(dx) + ady - half_size;
float r2 = dx + 0.25*$v_size + ady - half_size;
float r = max(r1,-r2);
return r/sqrt2;//account for slanted edge and correct for width
}
"""
ring = """
float ring(vec2 pointcoord, float size)
{
float r1 = length((pointcoord.xy - vec2(0.5,0.5))*size) - $v_size/2.;
float r2 = length((pointcoord.xy - vec2(0.5,0.5))*size) - $v_size/4.;
float r = max(r1,-r2);
return r;
}
"""
clobber = """
const float sqrt3 = sqrt(3.);
float clobber(vec2 pointcoord, float size)
{
const float PI = 3.14159265358979323846264;
const float t1 = -PI/2;
float circle_radius = 0.32 * $v_size;
float center_shift = 0.36/sqrt3 * $v_size;
//total size (horizontal) = 2*circle_radius + sqrt3*center_shirt = $v_size
vec2 c1 = vec2(cos(t1),sin(t1))*center_shift;
const float t2 = t1+2*PI/3;
vec2 c2 = vec2(cos(t2),sin(t2))*center_shift;
const float t3 = t2+2*PI/3;
vec2 c3 = vec2(cos(t3),sin(t3))*center_shift;
//xy is shift to center marker vertically
vec2 xy = (pointcoord.xy-vec2(0.5,0.5))*size + vec2(0.,-0.25*center_shift);
float r1 = length(xy - c1) - circle_radius;
float r2 = length(xy - c2) - circle_radius;
float r3 = length(xy - c3) - circle_radius;
float r = min(min(r1,r2),r3);
return r;
}
"""
square = """
float square(vec2 pointcoord, float size)
{
float r = max(abs(pointcoord.x -.5)*size, abs(pointcoord.y -.5)*size);
r -= $v_size/2.;
return r;
}
"""
x_ = """
float x_(vec2 pointcoord, float size)
{
vec2 rotcoord = vec2((pointcoord.x + pointcoord.y - 1.) / sqrt(2.),
(pointcoord.y - pointcoord.x) / sqrt(2.));
//vbar
float r1 = abs(rotcoord.x)*size - $v_size/6.;
float r2 = abs(rotcoord.y)*size - $v_size/2.;
float vbar = max(r1,r2);
//hbar
float r3 = abs(rotcoord.y)*size - $v_size/6.;
float r4 = abs(rotcoord.x)*size - $v_size/2.;
float hbar = max(r3,r4);
return min(vbar, hbar);
}
"""
diamond = """
float diamond(vec2 pointcoord, float size)
{
float r = abs(pointcoord.x -.5)*size + abs(pointcoord.y -.5)*size;
r -= $v_size/2.;
return r / sqrt(2.);//account for slanted edge and correct for width
}
"""
vbar = """
float vbar(vec2 pointcoord, float size)
{
float r1 = abs(pointcoord.x - 0.5)*size - $v_size/6.;
float r3 = abs(pointcoord.y - 0.5)*size - $v_size/2.;
float r = max(r1,r3);
return r;
}
"""
hbar = """
float rect(vec2 pointcoord, float size)
{
float r2 = abs(pointcoord.y - 0.5)*size - $v_size/6.;
float r3 = abs(pointcoord.x - 0.5)*size - $v_size/2.;
float r = max(r2,r3);
return r;
}
"""
cross = """
float cross(vec2 pointcoord, float size)
{
//vbar
float r1 = abs(pointcoord.x - 0.5)*size - $v_size/6.;
float r2 = abs(pointcoord.y - 0.5)*size - $v_size/2.;
float vbar = max(r1,r2);
//hbar
float r3 = abs(pointcoord.y - 0.5)*size - $v_size/6.;
float r4 = abs(pointcoord.x - 0.5)*size - $v_size/2.;
float hbar = max(r3,r4);
return min(vbar, hbar);
}
"""
tailed_arrow = """
const float sqrt2 = sqrt(2.);
float rect(vec2 pointcoord, float size)
{
float half_size = $v_size/2.;
float ady = abs(pointcoord.y -.5)*size;
float dx = (pointcoord.x -.5)*size;
float r1 = abs(dx) + ady - half_size;
float r2 = dx + 0.25*$v_size + ady - half_size;
float arrow = max(r1,-r2);
//hbar
float upper_bottom_edges = ady - $v_size/8./sqrt2;
float left_edge = -dx - half_size;
float right_edge = dx + ady - half_size;
float hbar = max(upper_bottom_edges, left_edge);
float scale = 1.; //rescaling for slanted edge
if (right_edge >= hbar)
{
hbar = right_edge;
scale = sqrt2;
}
if (arrow <= hbar)
{
return arrow / sqrt2;//account for slanted edge and correct for width
}
else
{
return hbar / scale;
}
}
"""
triangle_up = """
float rect(vec2 pointcoord, float size)
{
float height = $v_size*sqrt(3.)/2.;
float bottom = ((pointcoord.y - 0.5)*size - height/2.);
float rotated_y = sqrt(3.)/2. * (pointcoord.x - 0.5) * size
- 0.5 * ((pointcoord.y - 0.5)*size - height/6.) + height/6.;
float right_edge = (rotated_y - height/2.);
float cc_rotated_y = -sqrt(3.)/2. * (pointcoord.x - 0.5)*size
- 0.5 * ((pointcoord.y - 0.5)*size - height/6.) + height/6.;
float left_edge = (cc_rotated_y - height/2.);
float slanted_edges = max(right_edge, left_edge);
return max(slanted_edges, bottom);
}
"""
triangle_down = """
float rect(vec2 pointcoord, float size)
{
float height = -$v_size*sqrt(3.)/2.;
float bottom = -((pointcoord.y - 0.5)*size - height/2.);
float rotated_y = sqrt(3.)/2. * (pointcoord.x - 0.5) * size
- 0.5 * ((pointcoord.y - 0.5)*size - height/6.) + height/6.;
float right_edge = -(rotated_y - height/2.);
float cc_rotated_y = -sqrt(3.)/2. * (pointcoord.x - 0.5)*size
- 0.5 * ((pointcoord.y - 0.5)*size - height/6.) + height/6.;
float left_edge = -(cc_rotated_y - height/2.);
float slanted_edges = max(right_edge, left_edge);
return max(slanted_edges, bottom);
}
"""
star = """
float rect(vec2 pointcoord, float size)
{
float star = -10000.;
const float PI2_5 = 3.141592653589*2./5.;
const float PI2_20 = 3.141592653589/10.; //PI*2/20
// downwards shift to that the marker center is halfway vertically
// between the top of the upward spike (y = -v_size/2.)
// and the bottom of one of two downward spikes
// (y = +v_size/2.*cos(2.*pi/10.) approx +v_size/2.*0.8)
// center is at -v_size/2.*0.1
float shift_y = -0.05*$v_size;
// first spike upwards,
// rotate spike by 72 deg four times to complete the star
for (int i = 0; i <= 4; i++)
{
//if not the first spike, rotate it upwards
float x = (pointcoord.x - 0.5)*size;
float y = (pointcoord.y - 0.5)*size;
float spike_rot_angle = float(i) * PI2_5;
float cosangle = cos(spike_rot_angle);
float sinangle = sin(spike_rot_angle);
float spike_x = x;
float spike_y = y + shift_y;
if (i > 0)
{
spike_x = cosangle * x - sinangle * (y + shift_y);
spike_y = sinangle * x + cosangle * (y + shift_y);
}
// in the frame where the spike is upwards:
// rotate 18 deg the zone x < 0 around the top of the star
// (point whose coords are -s/2, 0 where s is the size of the marker)
// compute y coordonates as well because
// we do a second rotation to put the spike at its final position
float rot_center_y = -$v_size/2.;
float rot18x = cos(PI2_20) * spike_x
- sin(PI2_20) * (spike_y - rot_center_y);
//rotate -18 deg the zone x > 0 arount the top of the star
float rot_18x = cos(PI2_20) * spike_x
+ sin(PI2_20) * (spike_y - rot_center_y);
float bottom = spike_y - $v_size/10.;
// max(left edge, right edge)
float spike = max(bottom, max(rot18x, -rot_18x));
if (i == 0)
{// first spike, skip the rotation
star = spike;
}
else // i > 0
{
star = min(star, spike);
}
}
return star;
}
"""
# the following two markers needs x and y sizes
rect = """
float rect(vec2 pointcoord, float size)
{
float x_boundaries = abs(pointcoord.x - 0.5)*size - $v_size.x/2.;
float y_boundaries = abs(pointcoord.y - 0.5)*size - $v_size.y/2.;
return max(x_boundaries, y_boundaries);
}
"""
ellipse = """
float rect(vec2 pointcoord, float size)
{
float x = (pointcoord.x - 0.5)*size;
float y = (pointcoord.y - 0.5)*size;
// normalise radial distance (for edge and antialising to remain isotropic)
// Scaling factor is the norm of the gradient of the function defining
// the surface taken at a well chosen point on the edge of the ellipse
// f(x, y) = (sqrt(x^2/a^2 + y^2/b^2) = 0.5 in this case
// where a = v_size.x and b = v_size.y)
// The well chosen point on the edge of the ellipse should be the point
// whose normal points towards the current point.
// Below we choose a different point whose computation
// is simple enough to fit here.
float f = length(vec2(x / $v_size.x, y / $v_size.y));
// We set the default value of the norm so that
// - near the axes (x=0 or y=0 +/- 1 pixel), the norm is correct
// (the computation below is unstable near the axes)
// - if the ellipse is a circle, the norm is correct
// - if we are deep in the interior of the ellipse the norm
// is set to an arbitrary value (but is not used)
float norm = abs(x) < 1. ? 1./$v_size.y : 1./$v_size.x;
if (f > 1e-3 && abs($v_size.x - $v_size.y) > 1e-3
&& abs(x) > 1. && abs(y) > 1.)
{
// Find the point x0, y0 on the ellipse which has the same hyperbola
// coordinate in the elliptic coordinate system linked to the ellipse
// (finding the right 'well chosen' point is too complicated)
// Visually it's nice, even at high eccentricities, where
// the approximation is visible but not ugly.
float a = $v_size.x/2.;
float b = $v_size.y/2.;
float C = max(a, b);
float c = min(a, b);
float focal_length = sqrt(C*C - c*c);
float fl2 = focal_length*focal_length;
float x2 = x*x;
float y2 = y*y;
float tmp = fl2 + x2 + y2;
float x0 = 0;
float y0 = 0;
if ($v_size.x > $v_size.y)
{
float cos2v = 0.5 * (tmp - sqrt(tmp*tmp - 4.*fl2*x2)) / fl2;
cos2v = fract(cos2v);
x0 = a * sqrt(cos2v);
// v_size.x = focal_length*cosh m where m is the ellipse coordinate
y0 = b * sqrt(1-cos2v);
// v_size.y = focal_length*sinh m
}
else // $v_size.x < $v_size.y
{//exchange x and y axis for elliptic coordinate
float cos2v = 0.5 * (tmp - sqrt(tmp*tmp - 4.*fl2*y2)) / fl2;
cos2v = fract(cos2v);
x0 = a * sqrt(1-cos2v);
// v_size.x = focal_length*sinh m where m is the ellipse coordinate
y0 = b * sqrt(cos2v);
// v_size.y = focal_length*cosh m
}
vec2 normal = vec2(2.*x0/v_size.x/v_size.x, 2.*y0/v_size.y/v_size.y);
norm = length(normal);
}
return (f - 0.5) / norm;
}
"""
_marker_dict = {
'disc': disc,
'arrow': arrow,
'ring': ring,
'clobber': clobber,
'square': square,
'diamond': diamond,
'vbar': vbar,
'hbar': hbar,
'cross': cross,
'tailed_arrow': tailed_arrow,
'x': x_,
'triangle_up': triangle_up,
'triangle_down': triangle_down,
'star': star,
# aliases
'o': disc,
'+': cross,
's': square,
'-': hbar,
'|': vbar,
'->': tailed_arrow,
'>': arrow,
'^': triangle_up,
'v': triangle_down,
'*': star,
}
marker_types = tuple(sorted(list(_marker_dict.keys())))
class MarkersVisual(Visual):
""" Visual displaying marker symbols.
"""
def __init__(self, **kwargs):
self._vbo = VertexBuffer()
self._v_size_var = Variable('varying float v_size')
self._symbol = None
self._marker_fun = None
self._data = None
self.antialias = 1
self.scaling = False
Visual.__init__(self, vcode=vert, fcode=frag)
self.shared_program.vert['v_size'] = self._v_size_var
self.shared_program.frag['v_size'] = self._v_size_var
self.set_gl_state(depth_test=True, blend=True,
blend_func=('src_alpha', 'one_minus_src_alpha'))
self._draw_mode = 'points'
if len(kwargs) > 0:
self.set_data(**kwargs)
self.freeze()
def set_data(self, pos=None, symbol='o', size=10., edge_width=1.,
edge_width_rel=None, edge_color='black', face_color='white',
scaling=False):
""" Set the data used to display this visual.
Parameters
----------
pos : array
The array of locations to display each symbol.
symbol : str
The style of symbol to draw (see Notes).
size : float or array
The symbol size in px.
edge_width : float | None
The width of the symbol outline in pixels.
edge_width_rel : float | None
The width as a fraction of marker size. Exactly one of
`edge_width` and `edge_width_rel` must be supplied.
edge_color : Color | ColorArray
The color used to draw each symbol outline.
face_color : Color | ColorArray
The color used to draw each symbol interior.
scaling : bool
If set to True, marker scales when rezooming.
Notes
-----
Allowed style strings are: disc, arrow, ring, clobber, square, diamond,
vbar, hbar, cross, tailed_arrow, x, triangle_up, triangle_down,
and star.
"""
if (edge_width is not None) + (edge_width_rel is not None) != 1:
raise ValueError('exactly one of edge_width and edge_width_rel '
'must be non-None')
if edge_width is not None:
if edge_width < 0:
raise ValueError('edge_width cannot be negative')
else:
if edge_width_rel < 0:
raise ValueError('edge_width_rel cannot be negative')
self.symbol = symbol
self.scaling = scaling
edge_color = ColorArray(edge_color).rgba
if len(edge_color) == 1:
edge_color = edge_color[0]
face_color = ColorArray(face_color).rgba
if len(face_color) == 1:
face_color = face_color[0]
if pos is not None:
assert (isinstance(pos, np.ndarray) and
pos.ndim == 2 and pos.shape[1] in (2, 3))
n = len(pos)
data = np.zeros(n, dtype=[('a_position', np.float32, 3),
('a_fg_color', np.float32, 4),
('a_bg_color', np.float32, 4),
('a_size', np.float32),
('a_edgewidth', np.float32)])
data['a_fg_color'] = edge_color
data['a_bg_color'] = face_color
if edge_width is not None:
data['a_edgewidth'] = edge_width
else:
data['a_edgewidth'] = size*edge_width_rel
data['a_position'][:, :pos.shape[1]] = pos
data['a_size'] = size
self.shared_program['u_antialias'] = self.antialias # XXX make prop
self._data = data
if self._symbol is not None:
# If we have no symbol set, we skip drawing (_prepare_draw
# returns False). This causes the GLIR queue to not flush,
# and thus the GLIR queue fills with VBO DATA commands, resulting
# in a "memory leak". Thus only set the VertexBuffer data if we
# are actually going to draw.
self._vbo.set_data(data)
self.shared_program.bind(self._vbo)
self.update()
@property
def symbol(self):
return self._symbol
@symbol.setter
def symbol(self, symbol):
if symbol == self._symbol:
return
if (symbol is not None and self._symbol is None and
self._data is not None):
# Allow user to configure symbol after a set_data call with
# symbol=None. This can break down if the user does a consecutive
# marker.symbol = 'disc'
# marker.symbol = None
# without drawing. At this point the memory leaking ensues
# but this case is unlikely/makes no sense.
self._vbo.set_data(self._data)
self.shared_program.bind(self._vbo)
self._symbol = symbol
if symbol is None:
self._marker_fun = None
else:
_check_valid('symbol', symbol, marker_types)
self._marker_fun = Function(_marker_dict[symbol])
self._marker_fun['v_size'] = self._v_size_var
self.shared_program.frag['marker'] = self._marker_fun
self.update()
def _prepare_transforms(self, view):
xform = view.transforms.get_transform()
view.view_program.vert['transform'] = xform
def _prepare_draw(self, view):
if self._symbol is None:
return False
view.view_program['u_px_scale'] = view.transforms.pixel_scale
if self.scaling:
tr = view.transforms.get_transform('visual', 'document').simplified
mat = tr.map(np.eye(3)) - tr.map(np.zeros((3, 3)))
scale = np.linalg.norm(mat[:, :3])
view.view_program['u_scale'] = scale
else:
view.view_program['u_scale'] = 1
def _compute_bounds(self, axis, view):
pos = self._data['a_position']
if pos is None:
return None
if pos.shape[1] > axis:
return (pos[:, axis].min(), pos[:, axis].max())
else:
return (0, 0)
|