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