[e7a0b2f] | 1 | """ |
---|
| 2 | Arc slicer for 2D data |
---|
| 3 | """ |
---|
| 4 | import math |
---|
| 5 | |
---|
| 6 | from BaseInteractor import BaseInteractor |
---|
| 7 | |
---|
| 8 | class ArcInteractor(BaseInteractor): |
---|
| 9 | """ |
---|
| 10 | Select an annulus through a 2D plot |
---|
| 11 | """ |
---|
| 12 | def __init__(self, base, axes, color='black', zorder=5, r=1.0, |
---|
| 13 | theta1=math.pi / 8, theta2=math.pi / 4): |
---|
| 14 | BaseInteractor.__init__(self, base, axes, color=color) |
---|
| 15 | self.markers = [] |
---|
| 16 | self.axes = axes |
---|
| 17 | self._mouse_x = r |
---|
| 18 | self._mouse_y = 0 |
---|
| 19 | self._save_x = r |
---|
| 20 | self._save_y = 0 |
---|
| 21 | self.scale = 10.0 |
---|
| 22 | self.theta1 = theta1 |
---|
| 23 | self.theta2 = theta2 |
---|
| 24 | self.radius = r |
---|
| 25 | [self.arc] = self.axes.plot([], [], linestyle='-', marker='', color=self.color) |
---|
| 26 | self.npts = 20 |
---|
| 27 | self.has_move = False |
---|
| 28 | self.connect_markers([self.arc]) |
---|
| 29 | self.update() |
---|
| 30 | |
---|
| 31 | def set_layer(self, n): |
---|
| 32 | """ |
---|
| 33 | Allow adding plot to the same panel |
---|
| 34 | :param n: the number of layer |
---|
| 35 | """ |
---|
| 36 | self.layernum = n |
---|
| 37 | self.update() |
---|
| 38 | |
---|
| 39 | def clear(self): |
---|
| 40 | """ |
---|
| 41 | Clear this slicer and its markers |
---|
| 42 | """ |
---|
| 43 | self.clear_markers() |
---|
| 44 | try: |
---|
| 45 | for item in self.markers: |
---|
| 46 | item.remove() |
---|
| 47 | self.arc.remove() |
---|
| 48 | except: |
---|
| 49 | # Old version of matplotlib |
---|
| 50 | for item in range(len(self.axes.lines)): |
---|
| 51 | del self.axes.lines[0] |
---|
| 52 | |
---|
| 53 | def get_radius(self): |
---|
| 54 | """ |
---|
| 55 | Return arc radius |
---|
| 56 | """ |
---|
| 57 | radius = math.sqrt(math.pow(self._mouse_x, 2) + \ |
---|
| 58 | math.pow(self._mouse_y, 2)) |
---|
| 59 | return radius |
---|
| 60 | |
---|
| 61 | def update(self, theta1=None, theta2=None, nbins=None, r=None): |
---|
| 62 | """ |
---|
| 63 | Update the plotted arc |
---|
| 64 | :param theta1: starting angle of the arc |
---|
| 65 | :param theta2: ending angle of the arc |
---|
| 66 | :param nbins: number of points along the arc |
---|
| 67 | :param r: radius of the arc |
---|
| 68 | """ |
---|
| 69 | # Plot inner circle |
---|
| 70 | x = [] |
---|
| 71 | y = [] |
---|
| 72 | if theta1 != None: |
---|
| 73 | self.theta1 = theta1 |
---|
| 74 | if theta2 != None: |
---|
| 75 | self.theta2 = theta2 |
---|
| 76 | while self.theta2 < self.theta1: |
---|
| 77 | self.theta2 += (2 * math.pi) |
---|
| 78 | while self.theta2 >= (self.theta1 + 2 * math.pi): |
---|
| 79 | self.theta2 -= (2 * math.pi) |
---|
| 80 | npts = int((self.theta2 - self.theta1) / (math.pi / 120)) |
---|
| 81 | |
---|
| 82 | if r == None: |
---|
| 83 | self.radius = math.sqrt(math.pow(self._mouse_x, 2) + \ |
---|
| 84 | math.pow(self._mouse_y, 2)) |
---|
| 85 | else: |
---|
| 86 | self.radius = r |
---|
| 87 | for i in range(self.npts): |
---|
| 88 | phi = (self.theta2 - self.theta1) / (self.npts - 1) * i + self.theta1 |
---|
| 89 | xval = 1.0 * self.radius * math.cos(phi) |
---|
| 90 | yval = 1.0 * self.radius * math.sin(phi) |
---|
| 91 | |
---|
| 92 | x.append(xval) |
---|
| 93 | y.append(yval) |
---|
| 94 | # self.marker.set(xdata=[self._mouse_x],ydata=[0]) |
---|
| 95 | self.arc.set_data(x, y) |
---|
| 96 | |
---|
| 97 | def save(self, ev): |
---|
| 98 | """ |
---|
| 99 | Remember the roughness for this layer and the next so that we |
---|
| 100 | can restore on Esc. |
---|
| 101 | """ |
---|
| 102 | self._save_x = self._mouse_x |
---|
| 103 | self._save_y = self._mouse_y |
---|
| 104 | # self._save_x = ev.xdata |
---|
| 105 | # self._save_y = ev.ydata |
---|
| 106 | self.base.freeze_axes() |
---|
| 107 | |
---|
| 108 | def moveend(self, ev): |
---|
| 109 | """ |
---|
| 110 | After a dragging motion reset the flag self.has_move to False |
---|
| 111 | :param ev: event |
---|
| 112 | """ |
---|
| 113 | self.has_move = False |
---|
| 114 | |
---|
| 115 | self.base.moveend(ev) |
---|
| 116 | |
---|
| 117 | def restore(self): |
---|
| 118 | """ |
---|
| 119 | Restore the roughness for this layer. |
---|
| 120 | """ |
---|
| 121 | self._mouse_x = self._save_x |
---|
| 122 | self._mouse_y = self._save_y |
---|
| 123 | |
---|
| 124 | def move(self, x, y, ev): |
---|
| 125 | """ |
---|
| 126 | Process move to a new position, making sure that the move is allowed. |
---|
| 127 | """ |
---|
| 128 | # print "ring move x, y", x,y |
---|
| 129 | self._mouse_x = x |
---|
| 130 | self._mouse_y = y |
---|
| 131 | self.has_move = True |
---|
| 132 | self.base.base.update() |
---|
| 133 | |
---|
| 134 | def set_cursor(self, radius, phi_min, phi_max, nbins): |
---|
| 135 | """ |
---|
| 136 | """ |
---|
| 137 | self.theta1 = phi_min |
---|
| 138 | self.theta2 = phi_max |
---|
| 139 | self.update(nbins=nbins, r=radius) |
---|
| 140 | |
---|
| 141 | def get_params(self): |
---|
| 142 | """ |
---|
| 143 | """ |
---|
| 144 | params = {} |
---|
| 145 | params["radius"] = self.radius |
---|
| 146 | params["theta1"] = self.theta1 |
---|
| 147 | params["theta2"] = self.theta2 |
---|
| 148 | return params |
---|
| 149 | |
---|
| 150 | def set_params(self, params): |
---|
| 151 | """ |
---|
| 152 | """ |
---|
| 153 | x = params["radius"] |
---|
| 154 | phi_max = self.theta2 |
---|
| 155 | nbins = self.npts |
---|
| 156 | self.set_cursor(x, self._mouse_y, phi_max, nbins) |
---|
| 157 | |
---|