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