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