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