1 | """ |
---|
2 | Module that draws multiple arrows in 3D coordinates |
---|
3 | """ |
---|
4 | |
---|
5 | from matplotlib.patches import FancyArrowPatch |
---|
6 | from mpl_toolkits.mplot3d import proj3d |
---|
7 | import time |
---|
8 | #from matplotlib.artist import allow_rasterization |
---|
9 | class Arrow3D(FancyArrowPatch): |
---|
10 | """ |
---|
11 | Draw 3D arrow |
---|
12 | """ |
---|
13 | |
---|
14 | def __init__(self, base, xs, ys, zs, colors, *args, **kwargs): |
---|
15 | """ |
---|
16 | Init |
---|
17 | |
---|
18 | :Params xs: [[x0, x0+dx0], [x1, x1+dx1], ...] |
---|
19 | :Params ys: [[y0, y0+dy0], [y1, y1+dy1], ...] |
---|
20 | :Params zs: [[z0, z0+dz0], [z1, z1+dz1], ...] |
---|
21 | :Params colors: [[R0, G0, B0], [R1, G1, B1], ...] |
---|
22 | where R, G, B ranges (0,1) |
---|
23 | """ |
---|
24 | FancyArrowPatch.__init__(self, (0,0), (0,0), *args, **kwargs) |
---|
25 | self.leftdown = False |
---|
26 | self.t_click = 0 |
---|
27 | self._verts3d = xs, ys, zs |
---|
28 | self.colors = colors |
---|
29 | self.base = base |
---|
30 | |
---|
31 | if base != None: |
---|
32 | # To turn the updating off during dragging |
---|
33 | base.canvas.mpl_connect('button_press_event', self.onLeftDown) |
---|
34 | base.canvas.mpl_connect('button_release_event', self.onLeftUp) |
---|
35 | |
---|
36 | def onLeftDown(self, event): |
---|
37 | """ |
---|
38 | """ |
---|
39 | self.leftdown = True |
---|
40 | self.t_click = time.time() |
---|
41 | |
---|
42 | def onLeftUp(self, event): |
---|
43 | """ |
---|
44 | """ |
---|
45 | t_up = time.time() - self.t_click |
---|
46 | # Avoid just clicking |
---|
47 | if t_up > 0.1: |
---|
48 | self.leftdown = False |
---|
49 | self.base.canvas.draw() |
---|
50 | |
---|
51 | def draw(self, renderer, rasterized=True): |
---|
52 | """ |
---|
53 | Drawing actually happens here |
---|
54 | """ |
---|
55 | # Draws only when the dragging finished |
---|
56 | if self.leftdown: |
---|
57 | return |
---|
58 | xs3d, ys3d, zs3d = self._verts3d |
---|
59 | for i in xrange(len(xs3d)): |
---|
60 | xs, ys, _ = proj3d.proj_transform(xs3d[i], ys3d[i], zs3d[i], |
---|
61 | renderer.M) |
---|
62 | self.set_positions((xs[0],ys[0]),(xs[1],ys[1])) |
---|
63 | self.set_color(self.colors[i]) |
---|
64 | FancyArrowPatch.draw(self, renderer) |
---|
65 | |
---|
66 | self.leftdown = False |
---|