1 | """ |
---|
2 | Arc slicer for 2D data |
---|
3 | """ |
---|
4 | import math |
---|
5 | |
---|
6 | from BaseInteractor import _BaseInteractor |
---|
7 | from sans.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 | |
---|
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 | |
---|
22 | self._save_x = r |
---|
23 | self._save_y = 0 |
---|
24 | |
---|
25 | self.scale = 10.0 |
---|
26 | |
---|
27 | self.theta1 = theta1 |
---|
28 | self.theta2 = theta2 |
---|
29 | self.radius = r |
---|
30 | [self.arc] = self.axes.plot([], [], |
---|
31 | linestyle='-', marker='', |
---|
32 | color=self.color) |
---|
33 | self.npts = 20 |
---|
34 | self.has_move = False |
---|
35 | self.connect_markers([self.arc]) |
---|
36 | self.update() |
---|
37 | |
---|
38 | def set_layer(self, n): |
---|
39 | """ |
---|
40 | Allow adding plot to the same panel |
---|
41 | :param n: the number of layer |
---|
42 | """ |
---|
43 | self.layernum = n |
---|
44 | self.update() |
---|
45 | |
---|
46 | def clear(self): |
---|
47 | """ |
---|
48 | Clear this slicer and its markers |
---|
49 | """ |
---|
50 | self.clear_markers() |
---|
51 | try: |
---|
52 | for item in self.markers: |
---|
53 | item.remove() |
---|
54 | self.arc.remove() |
---|
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): |
---|
61 | """ |
---|
62 | Return arc radius |
---|
63 | """ |
---|
64 | radius = math.sqrt(math.pow(self._mouse_x, 2) + \ |
---|
65 | math.pow(self._mouse_y, 2)) |
---|
66 | return radius |
---|
67 | |
---|
68 | def update(self, theta1=None, theta2=None, nbins=None, r=None): |
---|
69 | """ |
---|
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 |
---|
75 | """ |
---|
76 | # Plot inner circle |
---|
77 | x = [] |
---|
78 | y = [] |
---|
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) |
---|
87 | npts = int((self.theta2 - self.theta1)/(math.pi/120)) |
---|
88 | |
---|
89 | if r == None: |
---|
90 | self.radius = math.sqrt(math.pow(self._mouse_x, 2) + \ |
---|
91 | math.pow(self._mouse_y, 2)) |
---|
92 | else: |
---|
93 | self.radius = r |
---|
94 | for i in range(self.npts): |
---|
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) |
---|
98 | |
---|
99 | x.append(xval) |
---|
100 | y.append(yval) |
---|
101 | #self.marker.set(xdata=[self._mouse_x],ydata=[0]) |
---|
102 | self.arc.set_data(x, y) |
---|
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 | """ |
---|
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 |
---|
113 | self.base.freeze_axes() |
---|
114 | |
---|
115 | def moveend(self, ev): |
---|
116 | """ |
---|
117 | After a dragging motion reset the flag self.has_move to False |
---|
118 | :param ev: event |
---|
119 | """ |
---|
120 | self.has_move = False |
---|
121 | |
---|
122 | event = SlicerParameterEvent() |
---|
123 | event.type = self.__class__.__name__ |
---|
124 | event.params = self.get_params() |
---|
125 | self.base.moveend(ev) |
---|
126 | |
---|
127 | def restore(self): |
---|
128 | """ |
---|
129 | Restore the roughness for this layer. |
---|
130 | """ |
---|
131 | self._mouse_x = self._save_x |
---|
132 | self._mouse_y = self._save_y |
---|
133 | |
---|
134 | def move(self, x, y, ev): |
---|
135 | """ |
---|
136 | Process move to a new position, making sure that the move is allowed. |
---|
137 | """ |
---|
138 | #print "ring move x, y", x,y |
---|
139 | self._mouse_x = x |
---|
140 | self._mouse_y = y |
---|
141 | self.has_move = True |
---|
142 | self.base.base.update() |
---|
143 | |
---|
144 | def set_cursor(self, radius, phi_min, phi_max, nbins): |
---|
145 | """ |
---|
146 | """ |
---|
147 | self.theta1 = phi_min |
---|
148 | self.theta2 = phi_max |
---|
149 | self.update(nbins=nbins, r=radius) |
---|
150 | |
---|
151 | def get_params(self): |
---|
152 | """ |
---|
153 | """ |
---|
154 | params = {} |
---|
155 | params["radius"] = self.radius |
---|
156 | params["theta1"] = self.theta1 |
---|
157 | params["theta2"] = self.theta2 |
---|
158 | return params |
---|
159 | |
---|
160 | def set_params(self, params): |
---|
161 | """ |
---|
162 | """ |
---|
163 | x = params["radius"] |
---|
164 | phi_max = self.theta2 |
---|
165 | nbins = self.npts |
---|
166 | self.set_cursor(x, self._mouse_y, phi_max, nbins) |
---|
167 | |
---|
168 | |
---|