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