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 | self.layernum = n |
---|
41 | self.update() |
---|
42 | |
---|
43 | def clear(self): |
---|
44 | self.clear_markers() |
---|
45 | try: |
---|
46 | self.marker.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 | |
---|
54 | |
---|
55 | def get_radius(self): |
---|
56 | radius =math.sqrt(math.pow(self._mouse_x, 2)+math.pow(self._mouse_y, 2)) |
---|
57 | return radius |
---|
58 | |
---|
59 | def update(self,theta1=None,theta2=None, nbins=None, r=None): |
---|
60 | """ |
---|
61 | """ |
---|
62 | # Plot inner circle |
---|
63 | x = [] |
---|
64 | y = [] |
---|
65 | if theta1 !=None: |
---|
66 | self.theta1= theta1 |
---|
67 | if theta2 !=None: |
---|
68 | self.theta2= theta2 |
---|
69 | |
---|
70 | print "ring update theta1 theta2", math.degrees(self.theta1), math.degrees(self.theta2) |
---|
71 | while self.theta2 < self.theta1: self.theta2 += 2*math.pi |
---|
72 | while self.theta2 >= self.theta1+2*math.pi: self.theta2 -= 2*math.pi |
---|
73 | |
---|
74 | """ |
---|
75 | if nbins!=None: |
---|
76 | self.npts =nbins |
---|
77 | else: |
---|
78 | """ |
---|
79 | npts = int((self.theta2 - self.theta1)/(math.pi/120)) |
---|
80 | |
---|
81 | if r ==None: |
---|
82 | self.radius= math.sqrt(math.pow(self._mouse_x, 2)+math.pow(self._mouse_y, 2)) |
---|
83 | else: |
---|
84 | self.radius= r |
---|
85 | |
---|
86 | for i in range(self.npts): |
---|
87 | |
---|
88 | phi =(self.theta2-self.theta1)/(self.npts-1)*i +self.theta1 |
---|
89 | |
---|
90 | xval = 1.0*self.radius*math.cos(phi) |
---|
91 | yval = 1.0*self.radius*math.sin(phi) |
---|
92 | |
---|
93 | x.append(xval) |
---|
94 | y.append(yval) |
---|
95 | |
---|
96 | #self.marker.set(xdata=[self._mouse_x],ydata=[0]) |
---|
97 | self.arc.set_data(x, y) |
---|
98 | |
---|
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 | |
---|
110 | self.base.freeze_axes() |
---|
111 | |
---|
112 | def moveend(self, ev): |
---|
113 | self.has_move= False |
---|
114 | |
---|
115 | event = SlicerParameterEvent() |
---|
116 | event.type = self.__class__.__name__ |
---|
117 | event.params = self.get_params() |
---|
118 | print "in arc moveend params",self.get_params() |
---|
119 | #wx.PostEvent(self.base.base.parent, event) |
---|
120 | |
---|
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 | self.theta1= phi_min |
---|
143 | self.theta2= phi_max |
---|
144 | self.update(nbins=nbins, r=radius) |
---|
145 | |
---|
146 | |
---|
147 | def get_params(self): |
---|
148 | params = {} |
---|
149 | params["radius"] = self.radius |
---|
150 | params["theta1"] = self.theta1 |
---|
151 | params["theta2"] = self.theta2 |
---|
152 | return params |
---|
153 | |
---|
154 | def set_params(self, params): |
---|
155 | |
---|
156 | x = params["radius"] |
---|
157 | self.set_cursor(x, self._mouse_y) |
---|
158 | |
---|
159 | |
---|