[749b715] | 1 | """ |
---|
| 2 | Arc slicer for 2D data |
---|
| 3 | """ |
---|
[e20870bc] | 4 | import numpy as np |
---|
[749b715] | 5 | |
---|
[e20870bc] | 6 | from sas.qtgui.Plotting.Slicers.BaseInteractor import BaseInteractor |
---|
[749b715] | 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, |
---|
[e20870bc] | 13 | theta1=np.pi / 8, theta2=np.pi / 4): |
---|
[749b715] | 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 | """ |
---|
[05fa132] | 33 | Allow adding plot to the same panel |
---|
| 34 | :param n: the number of layer |
---|
[749b715] | 35 | """ |
---|
| 36 | self.layernum = n |
---|
| 37 | self.update() |
---|
| 38 | |
---|
| 39 | def clear(self): |
---|
| 40 | """ |
---|
[05fa132] | 41 | Clear this slicer and its markers |
---|
[749b715] | 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 | """ |
---|
[05fa132] | 55 | Return arc radius |
---|
[749b715] | 56 | """ |
---|
[e20870bc] | 57 | radius = np.sqrt(np.power(self._mouse_x, 2) + \ |
---|
| 58 | np.power(self._mouse_y, 2)) |
---|
[749b715] | 59 | return radius |
---|
| 60 | |
---|
| 61 | def update(self, theta1=None, theta2=None, nbins=None, r=None): |
---|
| 62 | """ |
---|
[05fa132] | 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 |
---|
[749b715] | 68 | """ |
---|
| 69 | # Plot inner circle |
---|
| 70 | x = [] |
---|
| 71 | y = [] |
---|
[cee5c78] | 72 | if theta1 is not None: |
---|
[749b715] | 73 | self.theta1 = theta1 |
---|
[cee5c78] | 74 | if theta2 is not None: |
---|
[749b715] | 75 | self.theta2 = theta2 |
---|
| 76 | while self.theta2 < self.theta1: |
---|
[e20870bc] | 77 | self.theta2 += (2 * np.pi) |
---|
| 78 | while self.theta2 >= (self.theta1 + 2 * np.pi): |
---|
| 79 | self.theta2 -= (2 * np.pi) |
---|
[05fa132] | 80 | self.npts = int((self.theta2 - self.theta1) / (np.pi / 120)) |
---|
[749b715] | 81 | |
---|
[cee5c78] | 82 | if r is None: |
---|
[e20870bc] | 83 | self.radius = np.sqrt(np.power(self._mouse_x, 2) + \ |
---|
| 84 | np.power(self._mouse_y, 2)) |
---|
[749b715] | 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 |
---|
[e20870bc] | 89 | xval = 1.0 * self.radius * np.cos(phi) |
---|
| 90 | yval = 1.0 * self.radius * np.sin(phi) |
---|
[749b715] | 91 | |
---|
| 92 | x.append(xval) |
---|
| 93 | y.append(yval) |
---|
| 94 | self.arc.set_data(x, y) |
---|
| 95 | |
---|
| 96 | def save(self, ev): |
---|
| 97 | """ |
---|
| 98 | Remember the roughness for this layer and the next so that we |
---|
| 99 | can restore on Esc. |
---|
| 100 | """ |
---|
| 101 | self._save_x = self._mouse_x |
---|
| 102 | self._save_y = self._mouse_y |
---|
| 103 | self.base.freeze_axes() |
---|
| 104 | |
---|
| 105 | def moveend(self, ev): |
---|
| 106 | """ |
---|
[05fa132] | 107 | After a dragging motion reset the flag self.has_move to False |
---|
| 108 | :param ev: event |
---|
[749b715] | 109 | """ |
---|
| 110 | self.has_move = False |
---|
| 111 | |
---|
| 112 | self.base.moveend(ev) |
---|
| 113 | |
---|
| 114 | def restore(self): |
---|
| 115 | """ |
---|
| 116 | Restore the roughness for this layer. |
---|
| 117 | """ |
---|
| 118 | self._mouse_x = self._save_x |
---|
| 119 | self._mouse_y = self._save_y |
---|
| 120 | |
---|
| 121 | def move(self, x, y, ev): |
---|
| 122 | """ |
---|
| 123 | Process move to a new position, making sure that the move is allowed. |
---|
| 124 | """ |
---|
| 125 | self._mouse_x = x |
---|
| 126 | self._mouse_y = y |
---|
| 127 | self.has_move = True |
---|
| 128 | self.base.base.update() |
---|
| 129 | |
---|
| 130 | def set_cursor(self, radius, phi_min, phi_max, nbins): |
---|
| 131 | """ |
---|
| 132 | """ |
---|
| 133 | self.theta1 = phi_min |
---|
| 134 | self.theta2 = phi_max |
---|
| 135 | self.update(nbins=nbins, r=radius) |
---|
| 136 | |
---|
| 137 | def get_params(self): |
---|
| 138 | """ |
---|
| 139 | """ |
---|
| 140 | params = {} |
---|
| 141 | params["radius"] = self.radius |
---|
| 142 | params["theta1"] = self.theta1 |
---|
| 143 | params["theta2"] = self.theta2 |
---|
| 144 | return params |
---|
| 145 | |
---|
| 146 | def set_params(self, params): |
---|
| 147 | """ |
---|
| 148 | """ |
---|
| 149 | x = params["radius"] |
---|
| 150 | phi_max = self.theta2 |
---|
| 151 | nbins = self.npts |
---|
| 152 | self.set_cursor(x, self._mouse_y, phi_max, nbins) |
---|
| 153 | |
---|