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