1 | #from config import printEVT |
---|
2 | from BaseInteractor import _BaseInteractor |
---|
3 | from copy import deepcopy |
---|
4 | import math |
---|
5 | |
---|
6 | #from Plotter1D import AddPlotEvent |
---|
7 | import SlicerParameters |
---|
8 | import wx |
---|
9 | #import radii |
---|
10 | #(FunctionRadiusEvent, EVT_FUNC_RAD) = wx.lib.newevent.NewEvent() |
---|
11 | class ArcInteractor(_BaseInteractor): |
---|
12 | """ |
---|
13 | Select an annulus through a 2D plot |
---|
14 | """ |
---|
15 | def __init__(self,base,axes,color='black', zorder=5, r=1.0,theta1=math.pi/8, |
---|
16 | theta2=math.pi/4): |
---|
17 | |
---|
18 | _BaseInteractor.__init__(self, base, axes, color=color) |
---|
19 | self.markers = [] |
---|
20 | self.axes = axes |
---|
21 | self._mouse_x = r |
---|
22 | self._mouse_y = 0 |
---|
23 | |
---|
24 | self._save_x = r |
---|
25 | self._save_y = 0 |
---|
26 | |
---|
27 | self.scale = 10.0 |
---|
28 | |
---|
29 | self.theta1=theta1 |
---|
30 | self.theta2=theta2 |
---|
31 | self.radius= r |
---|
32 | [self.arc] = self.axes.plot([],[], |
---|
33 | linestyle='-', marker='', |
---|
34 | color=self.color) |
---|
35 | self.npts = 20 |
---|
36 | self.has_move= False |
---|
37 | self.connect_markers([self.arc]) |
---|
38 | self.update() |
---|
39 | |
---|
40 | def set_layer(self, n): |
---|
41 | self.layernum = n |
---|
42 | self.update() |
---|
43 | |
---|
44 | def clear(self): |
---|
45 | self.clear_markers() |
---|
46 | try: |
---|
47 | self.marker.remove() |
---|
48 | self.arc.remove() |
---|
49 | except: |
---|
50 | # Old version of matplotlib |
---|
51 | for item in range(len(self.axes.lines)): |
---|
52 | del self.axes.lines[0] |
---|
53 | |
---|
54 | |
---|
55 | |
---|
56 | def get_radius(self): |
---|
57 | radius =math.sqrt(math.pow(self._mouse_x, 2)+math.pow(self._mouse_y, 2)) |
---|
58 | return radius |
---|
59 | |
---|
60 | def update(self,theta1=None,theta2=None, nbins=None, r=None): |
---|
61 | """ |
---|
62 | """ |
---|
63 | # Plot inner circle |
---|
64 | x = [] |
---|
65 | y = [] |
---|
66 | if theta1 !=None: |
---|
67 | self.theta1= theta1 |
---|
68 | if theta2 !=None: |
---|
69 | self.theta2= theta2 |
---|
70 | |
---|
71 | print "ring update theta1 theta2", math.degrees(self.theta1), math.degrees(self.theta2) |
---|
72 | while self.theta2 < self.theta1: self.theta2 += 2*math.pi |
---|
73 | while self.theta2 >= self.theta1+2*math.pi: self.theta2 -= 2*math.pi |
---|
74 | |
---|
75 | """ |
---|
76 | if nbins!=None: |
---|
77 | self.npts =nbins |
---|
78 | else: |
---|
79 | """ |
---|
80 | npts = int((self.theta2 - self.theta1)/(math.pi/120)) |
---|
81 | |
---|
82 | if r ==None: |
---|
83 | self.radius= math.sqrt(math.pow(self._mouse_x, 2)+math.pow(self._mouse_y, 2)) |
---|
84 | else: |
---|
85 | self.radius= r |
---|
86 | |
---|
87 | for i in range(self.npts): |
---|
88 | |
---|
89 | phi =(self.theta2-self.theta1)/(self.npts-1)*i +self.theta1 |
---|
90 | |
---|
91 | xval = 1.0*self.radius*math.cos(phi) |
---|
92 | yval = 1.0*self.radius*math.sin(phi) |
---|
93 | |
---|
94 | x.append(xval) |
---|
95 | y.append(yval) |
---|
96 | |
---|
97 | #self.marker.set(xdata=[self._mouse_x],ydata=[0]) |
---|
98 | self.arc.set_data(x, y) |
---|
99 | |
---|
100 | |
---|
101 | def save(self, ev): |
---|
102 | """ |
---|
103 | Remember the roughness for this layer and the next so that we |
---|
104 | can restore on Esc. |
---|
105 | """ |
---|
106 | self._save_x = self._mouse_x |
---|
107 | self._save_y = self._mouse_y |
---|
108 | #self._save_x = ev.xdata |
---|
109 | #self._save_y = ev.ydata |
---|
110 | |
---|
111 | self.base.freeze_axes() |
---|
112 | |
---|
113 | def moveend(self, ev): |
---|
114 | self.has_move= False |
---|
115 | |
---|
116 | event = SlicerParameters.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 | self.theta1= phi_min |
---|
144 | self.theta2= phi_max |
---|
145 | self.update(nbins=nbins, r=radius) |
---|
146 | |
---|
147 | |
---|
148 | def get_params(self): |
---|
149 | params = {} |
---|
150 | params["radius"] = self.radius |
---|
151 | params["theta1"] = self.theta1 |
---|
152 | params["theta2"] = self.theta2 |
---|
153 | return params |
---|
154 | |
---|
155 | def set_params(self, params): |
---|
156 | |
---|
157 | x = params["radius"] |
---|
158 | self.set_cursor(x, self._mouse_y) |
---|
159 | |
---|
160 | |
---|