1 | import math |
---|
2 | |
---|
3 | from .BaseInteractor import _BaseInteractor |
---|
4 | |
---|
5 | |
---|
6 | class RadiusInteractor(_BaseInteractor): |
---|
7 | """ |
---|
8 | Select an annulus through a 2D plot |
---|
9 | """ |
---|
10 | def __init__(self, base, axes, color='black', zorder=5, arc1=None, |
---|
11 | arc2=None, theta=math.pi / 8): |
---|
12 | """ |
---|
13 | """ |
---|
14 | _BaseInteractor.__init__(self, base, axes, color=color) |
---|
15 | self.markers = [] |
---|
16 | self.axes = axes |
---|
17 | self.r1 = arc1.get_radius() |
---|
18 | self.r2 = arc2.get_radius() |
---|
19 | self.theta = theta |
---|
20 | self.save_theta = theta |
---|
21 | self.move_stop = False |
---|
22 | self.theta_left = None |
---|
23 | self.theta_right = None |
---|
24 | self.arc1 = arc1 |
---|
25 | self.arc2 = arc2 |
---|
26 | x1 = self.r1 * math.cos(self.theta) |
---|
27 | y1 = self.r1 * math.sin(self.theta) |
---|
28 | x2 = self.r2 * math.cos(self.theta) |
---|
29 | y2 = self.r2 * math.sin(self.theta) |
---|
30 | self.line = self.axes.plot([x1, x2], [y1, y2], |
---|
31 | linestyle='-', marker='', |
---|
32 | color=self.color, |
---|
33 | visible=True)[0] |
---|
34 | self.phi = theta |
---|
35 | self.npts = 20 |
---|
36 | self.has_move = False |
---|
37 | self.connect_markers([self.line]) |
---|
38 | self.update() |
---|
39 | |
---|
40 | def set_layer(self, n): |
---|
41 | """ |
---|
42 | """ |
---|
43 | self.layernum = n |
---|
44 | self.update() |
---|
45 | |
---|
46 | def clear(self): |
---|
47 | """ |
---|
48 | """ |
---|
49 | self.clear_markers() |
---|
50 | try: |
---|
51 | self.line.remove() |
---|
52 | except: |
---|
53 | # Old version of matplotlib |
---|
54 | for item in range(len(self.axes.lines)): |
---|
55 | del self.axes.lines[0] |
---|
56 | |
---|
57 | def get_angle(self): |
---|
58 | """ |
---|
59 | """ |
---|
60 | return self.theta |
---|
61 | |
---|
62 | def update(self, r1=None, r2=None, theta=None): |
---|
63 | """ |
---|
64 | Draw the new roughness on the graph. |
---|
65 | """ |
---|
66 | if r1 is not None: |
---|
67 | self.r1 = r1 |
---|
68 | if r2 is not None: |
---|
69 | self.r2 = r2 |
---|
70 | if theta is not None: |
---|
71 | self.theta = theta |
---|
72 | x1 = self.r1 * math.cos(self.theta) |
---|
73 | y1 = self.r1 * math.sin(self.theta) |
---|
74 | x2 = self.r2 * math.cos(self.theta) |
---|
75 | y2 = self.r2 * math.sin(self.theta) |
---|
76 | self.line.set(xdata=[x1, x2], ydata=[y1, y2]) |
---|
77 | |
---|
78 | def save(self, ev): |
---|
79 | """ |
---|
80 | Remember the roughness for this layer and the next so that we |
---|
81 | can restore on Esc. |
---|
82 | """ |
---|
83 | self.save_theta = math.atan2(ev.y, ev.x) |
---|
84 | self.base.freeze_axes() |
---|
85 | |
---|
86 | def moveend(self, ev): |
---|
87 | """ |
---|
88 | """ |
---|
89 | self.has_move = False |
---|
90 | self.base.moveend(ev) |
---|
91 | |
---|
92 | def restore(self, ev): |
---|
93 | """ |
---|
94 | Restore the roughness for this layer. |
---|
95 | """ |
---|
96 | self.theta = self.save_theta |
---|
97 | |
---|
98 | def move(self, x, y, ev): |
---|
99 | """ |
---|
100 | Process move to a new position, making sure that the move is allowed. |
---|
101 | """ |
---|
102 | self.theta = math.atan2(y, x) |
---|
103 | self.has_move = True |
---|
104 | self.base.base.update() |
---|
105 | |
---|
106 | def set_cursor(self, r_min, r_max, theta): |
---|
107 | """ |
---|
108 | """ |
---|
109 | self.theta = theta |
---|
110 | self.r1 = r_min |
---|
111 | self.r2 = r_max |
---|
112 | self.update() |
---|
113 | |
---|
114 | def get_params(self): |
---|
115 | """ |
---|
116 | """ |
---|
117 | params = {} |
---|
118 | params["radius1"] = self.r1 |
---|
119 | params["radius2"] = self.r2 |
---|
120 | params["theta"] = self.theta |
---|
121 | return params |
---|
122 | |
---|
123 | def set_params(self, params): |
---|
124 | """ |
---|
125 | """ |
---|
126 | x1 = params["radius1"] |
---|
127 | x2 = params["radius2"] |
---|
128 | theta = params["theta"] |
---|
129 | self.set_cursor(x1, x2, theta) |
---|