[0d9dae8] | 1 | |
---|
[b06ef8c] | 2 | import math |
---|
[0d9dae8] | 3 | import wx |
---|
| 4 | from copy import deepcopy |
---|
[b06ef8c] | 5 | |
---|
[0d9dae8] | 6 | from BaseInteractor import _BaseInteractor |
---|
| 7 | from sans.guicomm.events import NewPlotEvent, StatusEvent,SlicerParameterEvent,EVT_SLICER_PARS |
---|
| 8 | |
---|
| 9 | |
---|
[b06ef8c] | 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 |
---|
[cd84dca] | 20 | self._mouse_x = r |
---|
| 21 | self._mouse_y = 0 |
---|
[b06ef8c] | 22 | |
---|
[cd84dca] | 23 | self._save_x = r |
---|
| 24 | self._save_y = 0 |
---|
[b06ef8c] | 25 | |
---|
| 26 | self.scale = 10.0 |
---|
| 27 | |
---|
| 28 | self.theta1=theta1 |
---|
| 29 | self.theta2=theta2 |
---|
[e8c96f5] | 30 | self.radius= r |
---|
[cd84dca] | 31 | [self.arc] = self.axes.plot([],[], |
---|
[b06ef8c] | 32 | linestyle='-', marker='', |
---|
| 33 | color=self.color) |
---|
| 34 | self.npts = 20 |
---|
[2d107b8] | 35 | self.has_move= False |
---|
[cd84dca] | 36 | self.connect_markers([self.arc]) |
---|
[b06ef8c] | 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: |
---|
[cd84dca] | 46 | self.marker.remove() |
---|
| 47 | self.arc.remove() |
---|
[b06ef8c] | 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): |
---|
[cd84dca] | 56 | radius =math.sqrt(math.pow(self._mouse_x, 2)+math.pow(self._mouse_y, 2)) |
---|
[b06ef8c] | 57 | return radius |
---|
| 58 | |
---|
[e8c96f5] | 59 | def update(self,theta1=None,theta2=None, nbins=None, r=None): |
---|
[b06ef8c] | 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 |
---|
[e8c96f5] | 69 | |
---|
[b98db8c] | 70 | print "ring update theta1 theta2", math.degrees(self.theta1), math.degrees(self.theta2) |
---|
[b06ef8c] | 71 | while self.theta2 < self.theta1: self.theta2 += 2*math.pi |
---|
[cd84dca] | 72 | while self.theta2 >= self.theta1+2*math.pi: self.theta2 -= 2*math.pi |
---|
| 73 | |
---|
| 74 | """ |
---|
[e8c96f5] | 75 | if nbins!=None: |
---|
| 76 | self.npts =nbins |
---|
| 77 | else: |
---|
[cd84dca] | 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 | |
---|
[b06ef8c] | 86 | for i in range(self.npts): |
---|
| 87 | |
---|
| 88 | phi =(self.theta2-self.theta1)/(self.npts-1)*i +self.theta1 |
---|
[e8c96f5] | 89 | |
---|
| 90 | xval = 1.0*self.radius*math.cos(phi) |
---|
| 91 | yval = 1.0*self.radius*math.sin(phi) |
---|
| 92 | |
---|
[b06ef8c] | 93 | x.append(xval) |
---|
| 94 | y.append(yval) |
---|
| 95 | |
---|
[cd84dca] | 96 | #self.marker.set(xdata=[self._mouse_x],ydata=[0]) |
---|
| 97 | self.arc.set_data(x, y) |
---|
[b06ef8c] | 98 | |
---|
[e8c96f5] | 99 | |
---|
[b06ef8c] | 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 | """ |
---|
[cd84dca] | 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 |
---|
[e8c96f5] | 109 | |
---|
[b06ef8c] | 110 | self.base.freeze_axes() |
---|
| 111 | |
---|
| 112 | def moveend(self, ev): |
---|
[2d107b8] | 113 | self.has_move= False |
---|
[e8c96f5] | 114 | |
---|
[0d9dae8] | 115 | event = SlicerParameterEvent() |
---|
[e8c96f5] | 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 | |
---|
[b06ef8c] | 121 | self.base.moveend(ev) |
---|
| 122 | |
---|
| 123 | def restore(self): |
---|
| 124 | """ |
---|
| 125 | Restore the roughness for this layer. |
---|
| 126 | """ |
---|
[cd84dca] | 127 | self._mouse_x = self._save_x |
---|
| 128 | self._mouse_y = self._save_y |
---|
[b06ef8c] | 129 | |
---|
| 130 | def move(self, x, y, ev): |
---|
| 131 | """ |
---|
| 132 | Process move to a new position, making sure that the move is allowed. |
---|
| 133 | """ |
---|
[e8c96f5] | 134 | #print "ring move x, y", x,y |
---|
[cd84dca] | 135 | self._mouse_x = x |
---|
| 136 | self._mouse_y = y |
---|
[2d107b8] | 137 | self.has_move= True |
---|
[b06ef8c] | 138 | self.base.base.update() |
---|
| 139 | |
---|
[e8c96f5] | 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) |
---|
[b06ef8c] | 145 | |
---|
| 146 | |
---|
| 147 | def get_params(self): |
---|
| 148 | params = {} |
---|
[e8c96f5] | 149 | params["radius"] = self.radius |
---|
| 150 | params["theta1"] = self.theta1 |
---|
| 151 | params["theta2"] = self.theta2 |
---|
[b06ef8c] | 152 | return params |
---|
| 153 | |
---|
| 154 | def set_params(self, params): |
---|
| 155 | |
---|
| 156 | x = params["radius"] |
---|
[cd84dca] | 157 | self.set_cursor(x, self._mouse_y) |
---|
[b06ef8c] | 158 | |
---|
| 159 | |
---|