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