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