|
1 | 1 | from __future__ import division
|
2 | 2 | import math
|
3 | 3 | from matplotlib import rcParams
|
4 |
| -from numerix import array, arange, sin, cos, pi, Float, sqrt, matrixmultiply |
| 4 | +from numerix import array, arange, sin, cos, pi, Float, sqrt, \ |
| 5 | + matrixmultiply, sqrt, nonzero, equal, asarray, dot |
5 | 6 | from artist import Artist, setp
|
6 | 7 | from cbook import enumerate, popd
|
7 | 8 | from colors import colorConverter
|
| 9 | +from lines import Line2D |
8 | 10 | from transforms import bound_vertices
|
9 | 11 |
|
| 12 | +from numerix.mlab import amin |
| 13 | +from mlab import dist_point_to_segment |
| 14 | + |
| 15 | + |
10 | 16 | class Patch(Artist):
|
11 | 17 | """
|
12 | 18 | A patch is a 2D thingy with a face color and an edge color
|
@@ -418,6 +424,120 @@ def __init__(self, xy, radius=5,
|
418 | 424 | orientation=0,
|
419 | 425 | **kwargs)
|
420 | 426 |
|
| 427 | +class PolygonInteractor: |
| 428 | + """ |
| 429 | + An polygon editor. |
| 430 | +
|
| 431 | + Key-bindings |
| 432 | +
|
| 433 | + 't' toggle vertex markers on and off. When vertex markers are on, |
| 434 | + you can move them, delete them |
| 435 | +
|
| 436 | + 'd' delete the vertex under point |
| 437 | +
|
| 438 | + 'i' insert a vertex at point. You must be within epsilon of the |
| 439 | + line connecting two existing vertices |
| 440 | + |
| 441 | + """ |
| 442 | + |
| 443 | + showverts = True |
| 444 | + epsilon = 5 # max pixel distance to count as a vertex hit |
| 445 | + |
| 446 | + def __init__(self, poly): |
| 447 | + if poly.figure is None: |
| 448 | + raise RuntimeError('You must first add the polygon to a figure or canvas before defining the interactor') |
| 449 | + canvas = poly.figure.canvas |
| 450 | + self.poly = poly |
| 451 | + self.poly.verts = list(self.poly.verts) |
| 452 | + x, y = zip(*self.poly.verts) |
| 453 | + self.line = Line2D(x,y,marker='o', markerfacecolor='r') |
| 454 | + #self._update_line(poly) |
| 455 | + |
| 456 | + cid = self.poly.add_callback(self.poly_changed) |
| 457 | + self._ind = None # the active vert |
| 458 | + |
| 459 | + canvas.mpl_connect('button_press_event', self.button_press_callback) |
| 460 | + canvas.mpl_connect('key_press_event', self.key_press_callback) |
| 461 | + canvas.mpl_connect('button_release_event', self.button_release_callback) |
| 462 | + canvas.mpl_connect('motion_notify_event', self.motion_notify_callback) |
| 463 | + self.canvas = canvas |
| 464 | + |
| 465 | + |
| 466 | + def poly_changed(self, poly): |
| 467 | + 'this method is called whenever the polygon object is called' |
| 468 | + # only copy the artist props to the line (except visibility) |
| 469 | + vis = self.line.get_visible() |
| 470 | + Artist.update_from(self.line, poly) |
| 471 | + self.line.set_visible(vis) # don't use the poly visibility state |
| 472 | + |
| 473 | + |
| 474 | + def get_ind_under_point(self, event): |
| 475 | + 'get the index of the vertex under point if within epsilon tolerance' |
| 476 | + x, y = zip(*self.poly.verts) |
| 477 | + |
| 478 | + # display coords |
| 479 | + xt, yt = self.poly.get_transform().numerix_x_y(x, y) |
| 480 | + d = sqrt((xt-event.x)**2 + (yt-event.y)**2) |
| 481 | + indseq = nonzero(equal(d, amin(d))) |
| 482 | + ind = indseq[0] |
| 483 | + |
| 484 | + if d[ind]>=self.epsilon: |
| 485 | + ind = None |
| 486 | + |
| 487 | + return ind |
| 488 | + |
| 489 | + def button_press_callback(self, event): |
| 490 | + 'whenever a mouse button is pressed' |
| 491 | + if not self.showverts: return |
| 492 | + if event.inaxes==None: return |
| 493 | + if event.button != 1: return |
| 494 | + self._ind = self.get_ind_under_point(event) |
| 495 | + |
| 496 | + def button_release_callback(self, event): |
| 497 | + 'whenever a mouse button is released' |
| 498 | + if not self.showverts: return |
| 499 | + if event.button != 1: return |
| 500 | + self._ind = None |
| 501 | + |
| 502 | + def key_press_callback(self, event): |
| 503 | + 'whenever a key is pressed' |
| 504 | + if not event.inaxes: return |
| 505 | + if event.key=='t': |
| 506 | + self.showverts = not self.showverts |
| 507 | + self.line.set_visible(self.showverts) |
| 508 | + if not self.showverts: self._ind = None |
| 509 | + elif event.key=='d': |
| 510 | + ind = self.get_ind_under_point(event) |
| 511 | + if ind is not None: |
| 512 | + self.poly.verts = [tup for i,tup in enumerate(self.poly.verts) if i!=ind] |
| 513 | + self.line.set_data(zip(*self.poly.verts)) |
| 514 | + elif event.key=='i': |
| 515 | + xys = self.poly.get_transform().seq_xy_tups(self.poly.verts) |
| 516 | + p = event.x, event.y # display coords |
| 517 | + for i in range(len(xys)-1): |
| 518 | + s0 = xys[i] |
| 519 | + s1 = xys[i+1] |
| 520 | + d = dist_point_to_segment(p, s0, s1) |
| 521 | + if d<=self.epsilon: |
| 522 | + self.poly.verts.insert(i+1, (event.xdata, event.ydata)) |
| 523 | + self.line.set_data(zip(*self.poly.verts)) |
| 524 | + break |
| 525 | + |
| 526 | + |
| 527 | + self.canvas.draw() |
| 528 | + |
| 529 | + def motion_notify_callback(self, event): |
| 530 | + 'on mouse movement' |
| 531 | + if not self.showverts: return |
| 532 | + if self._ind is None: return |
| 533 | + if event.inaxes is None: return |
| 534 | + if event.button != 1: return |
| 535 | + x,y = event.xdata, event.ydata |
| 536 | + self.poly.verts[self._ind] = x,y |
| 537 | + self.line.set_data(zip(*self.poly.verts)) |
| 538 | + self.canvas.draw_idle() |
| 539 | + |
| 540 | + |
421 | 541 | def bbox_artist(artist, renderer, props=None, fill=True):
|
422 | 542 | """
|
423 | 543 | This is a debug function to draw a rectangle around the bounding
|
|
0 commit comments