[0f6d05f8] | 1 | #TODO: the line slicer should listen to all 2DREFRESH events, get the data and slice it |
---|
| 2 | # before pushing a new 1D data update. |
---|
| 3 | |
---|
| 4 | # |
---|
| 5 | #TODO: NEED MAJOR REFACTOR |
---|
| 6 | # |
---|
| 7 | |
---|
| 8 | |
---|
| 9 | # Debug printout |
---|
| 10 | #from config import printEVT |
---|
| 11 | from BaseInteractor import _BaseInteractor |
---|
| 12 | from copy import deepcopy |
---|
| 13 | import math |
---|
| 14 | |
---|
| 15 | #from Plotter1D import AddPlotEvent |
---|
| 16 | import SlicerParameters |
---|
| 17 | import wx |
---|
| 18 | |
---|
| 19 | def find_intersection(a1= 2, a2= -0.5,b1= 1,b2= 1 ): |
---|
| 20 | """ @ return x, y coordinates of an intersection between 2 lines |
---|
| 21 | @param a1: the slope of the first line |
---|
| 22 | @param a2 : the slope of the 2nd line |
---|
| 23 | @param b1 : line intercept of the 1 st line |
---|
| 24 | @param b2 : line intercept of the 2 nd ligne |
---|
| 25 | @note 1st line equation is y= a1*x +b1 ; 2nd line equation is y= a2*x +b2 |
---|
| 26 | """ |
---|
| 27 | x= ( b2- b1) /(a1- a2) |
---|
| 28 | y= ( -a2*b1 + a1*b2 )/(a1 -a2) |
---|
| 29 | return x, y |
---|
| 30 | class BoxInteractor(_BaseInteractor): |
---|
| 31 | """ |
---|
| 32 | Select an annulus through a 2D plot |
---|
| 33 | """ |
---|
| 34 | def __init__(self,base,axes,color='black', zorder=3, |
---|
| 35 | x_min=0.0025, x_max=0.0025, y_min=0.0025, y_max=0.0025): |
---|
| 36 | |
---|
| 37 | _BaseInteractor.__init__(self, base, axes, color=color) |
---|
| 38 | self.markers = [] |
---|
| 39 | self.axes = axes |
---|
| 40 | self.qmax = self.base.qmax |
---|
| 41 | self.connect = self.base.connect |
---|
| 42 | self.xmin= -1* x_min |
---|
| 43 | self.ymin= -1* y_min |
---|
| 44 | |
---|
| 45 | self.xmax= x_max |
---|
| 46 | self.ymax= y_max |
---|
| 47 | |
---|
| 48 | self.theta2= math.pi/3 |
---|
| 49 | ## Number of points on the plot |
---|
| 50 | self.nbins = 20 |
---|
| 51 | self.count=0 |
---|
| 52 | self.error=0 |
---|
| 53 | self.main_line = LineInteractor(self, self.base.subplot,color='orange', |
---|
| 54 | zorder=zorder, ymin=y_min ,ymax=y_max, |
---|
| 55 | theta= self.theta2) |
---|
| 56 | self.main_line.qmax = self.base.qmax |
---|
| 57 | self.left_line = VerticalLine(self, self.base.subplot,color='blue', |
---|
| 58 | zorder=zorder, |
---|
| 59 | mline= self.main_line, |
---|
| 60 | ymin= self.ymin , |
---|
| 61 | ymax= self.ymax , |
---|
| 62 | xmin=self.xmin, |
---|
| 63 | xmax=self.xmin, |
---|
| 64 | theta2= self.theta2) |
---|
| 65 | self.left_line.qmax = self.base.qmax |
---|
| 66 | |
---|
| 67 | self.right_line= VerticalLine(self, self.base.subplot,color='black', |
---|
| 68 | zorder=zorder, |
---|
| 69 | mline= self.main_line, |
---|
| 70 | ymin= self.ymin , |
---|
| 71 | ymax= self.ymax, |
---|
| 72 | xmin= self.xmax, |
---|
| 73 | xmax= self.xmax, |
---|
| 74 | theta2= self.theta2) |
---|
| 75 | self.right_line.qmax = self.base.qmax |
---|
| 76 | |
---|
| 77 | self.top_line= HorizontalLine(self, self.base.subplot,color='green', |
---|
| 78 | zorder=zorder, |
---|
| 79 | mline= self.main_line, |
---|
| 80 | xmin=self.right_line.x1, |
---|
| 81 | xmax=self.left_line.x1, |
---|
| 82 | ymin=self.right_line.y1, |
---|
| 83 | ymax=self.left_line.y1) |
---|
| 84 | self.top_line.qmax= self.base.qmax |
---|
| 85 | |
---|
| 86 | self.bottom_line= HorizontalLine(self, self.base.subplot,color='gray', |
---|
| 87 | zorder=zorder, |
---|
| 88 | mline= self.main_line, |
---|
| 89 | xmin=self.right_line.x2, |
---|
| 90 | xmax=self.left_line.x2, |
---|
| 91 | ymin=self.right_line.y2, |
---|
| 92 | ymax=self.left_line.y2) |
---|
| 93 | self.bottom_line.qmax= self.base.qmax |
---|
| 94 | |
---|
| 95 | self.update() |
---|
| 96 | #self._post_data() |
---|
| 97 | |
---|
| 98 | # Bind to slice parameter events |
---|
| 99 | self.base.parent.Bind(SlicerParameters.EVT_SLICER_PARS, self._onEVT_SLICER_PARS) |
---|
| 100 | |
---|
| 101 | |
---|
| 102 | def _onEVT_SLICER_PARS(self, event): |
---|
| 103 | #printEVT("AnnulusSlicer._onEVT_SLICER_PARS") |
---|
| 104 | event.Skip() |
---|
| 105 | if event.type == self.__class__.__name__: |
---|
| 106 | #self.set_params(event.params) |
---|
| 107 | self.base.update() |
---|
| 108 | |
---|
| 109 | def update_and_post(self): |
---|
| 110 | self.update() |
---|
| 111 | self._post_data() |
---|
| 112 | |
---|
| 113 | def save_data(self, path, image, x, y): |
---|
| 114 | output = open(path, 'w') |
---|
| 115 | |
---|
| 116 | data_x, data_y = self.get_data(image, x, y) |
---|
| 117 | |
---|
| 118 | output.write("<phi> <average>\n") |
---|
| 119 | for i in range(len(data_x)): |
---|
| 120 | output.write("%g %g\n" % (data_x[i], data_y[i])) |
---|
| 121 | output.close() |
---|
| 122 | |
---|
| 123 | def set_layer(self, n): |
---|
| 124 | self.layernum = n |
---|
| 125 | self.update() |
---|
| 126 | |
---|
| 127 | def clear(self): |
---|
| 128 | self.clear_markers() |
---|
| 129 | |
---|
| 130 | |
---|
| 131 | self.main_line.clear() |
---|
| 132 | #self.base.connect.disconnect() |
---|
| 133 | self.base.parent.Unbind(SlicerParameters.EVT_SLICER_PARS) |
---|
| 134 | |
---|
| 135 | def update(self): |
---|
| 136 | """ |
---|
| 137 | Respond to changes in the model by recalculating the profiles and |
---|
| 138 | resetting the widgets. |
---|
| 139 | """ |
---|
| 140 | |
---|
| 141 | if self.main_line.has_move: |
---|
| 142 | |
---|
| 143 | self.main_line.update() |
---|
| 144 | self.left_line.update( |
---|
| 145 | xmin= self.xmin, |
---|
| 146 | xmax= self.xmin, |
---|
| 147 | ymin= self.ymin, |
---|
| 148 | ymax=self.ymax |
---|
| 149 | ) |
---|
| 150 | self.right_line.update( |
---|
| 151 | xmin= self.xmax, |
---|
| 152 | xmax= self.xmax, |
---|
| 153 | ymin= self.ymin, |
---|
| 154 | ymax=self.ymax) |
---|
| 155 | self.top_line.update(xmin= self.right_line.x1, |
---|
| 156 | xmax= self.left_line.x1, |
---|
| 157 | ymin= self.right_line.y1, |
---|
| 158 | ymax= self.left_line.y1) |
---|
| 159 | self.bottom_line.update(xmin= self.right_line.x2, |
---|
| 160 | xmax= self.left_line.x2, |
---|
| 161 | ymin= self.right_line.y2, |
---|
| 162 | ymax= self.left_line.y2) |
---|
| 163 | if self.top_line.has_move: |
---|
| 164 | print "top has moved",self.left_line.slope, self.top_line.slope |
---|
| 165 | x2, y2= find_intersection(a1= self.left_line.slope, |
---|
| 166 | a2= self.top_line.slope, |
---|
| 167 | b1= self.left_line.b, |
---|
| 168 | b2= self.top_line.b ) |
---|
| 169 | print "x, y max: ",x2,y2 |
---|
| 170 | x1, y1= find_intersection(a1= self.right_line.slope, |
---|
| 171 | a2= self.top_line.slope, |
---|
| 172 | b1= self.right_line.b, |
---|
| 173 | b2= self.top_line.b ) |
---|
| 174 | print "x, y min: ",x1 ,y1 |
---|
| 175 | self.top_line.update(xmin= x2, |
---|
| 176 | ymin= y2, |
---|
| 177 | xmax= x1, |
---|
| 178 | ymax= y1) |
---|
| 179 | |
---|
| 180 | self.bottom_line.update(xmin= -x2, |
---|
| 181 | ymin= -y2, |
---|
| 182 | xmax= -x1, |
---|
| 183 | ymax= -y1) |
---|
| 184 | self.left_line.update(xmin= -x1, |
---|
| 185 | ymin= -y1, |
---|
| 186 | xmax= x2, |
---|
| 187 | ymax= y2, |
---|
| 188 | translation= True) |
---|
| 189 | self.right_line.update( |
---|
| 190 | xmin= -x2, |
---|
| 191 | ymin= -y2, |
---|
| 192 | xmax= x1, |
---|
| 193 | ymax= y1, |
---|
| 194 | translation= True) |
---|
| 195 | print "top has moved",self.left_line.slope, self.top_line.slope |
---|
| 196 | x2, y2= find_intersection(a1= self.main_line.slope, |
---|
| 197 | a2= self.top_line.slope, |
---|
| 198 | b1= self.main_line.b, |
---|
| 199 | b2= self.top_line.b ) |
---|
| 200 | print "main x, y max: ",x2,y2 |
---|
| 201 | x1, y1= find_intersection(a1= self.main_line.slope, |
---|
| 202 | a2= self.bottom_line.slope, |
---|
| 203 | b1= self.main_line.b, |
---|
| 204 | b2= self.bottom_line.b ) |
---|
| 205 | print "main x, y min: ",x1,y1 |
---|
| 206 | self.main_line.update(x1= -x2, |
---|
| 207 | y1= -y2, |
---|
| 208 | x2= x2, |
---|
| 209 | y2= y2, |
---|
| 210 | translation= True) |
---|
| 211 | if self.bottom_line.has_move: |
---|
| 212 | |
---|
| 213 | print "bottom has moved",self.left_line.slope, self.bottom_line.slope |
---|
| 214 | x2, y2= find_intersection(a1= self.left_line.slope, |
---|
| 215 | a2= self.bottom_line.slope, |
---|
| 216 | b1= self.left_line.b, |
---|
| 217 | b2= self.bottom_line.b ) |
---|
| 218 | print "x, y max: ",x2,y2 |
---|
| 219 | x1, y1= find_intersection(a1= self.right_line.slope, |
---|
| 220 | a2= self.bottom_line.slope, |
---|
| 221 | b1= self.right_line.b, |
---|
| 222 | b2= self.bottom_line.b ) |
---|
| 223 | print "x, y min: ",x1 ,y1 |
---|
| 224 | self.bottom_line.update(xmin= x2, |
---|
| 225 | ymin= y2, |
---|
| 226 | xmax= x1, |
---|
| 227 | ymax= y1) |
---|
| 228 | |
---|
| 229 | self.top_line.update(xmin= -x2, |
---|
| 230 | ymin= -y2, |
---|
| 231 | xmax= -x1, |
---|
| 232 | ymax= -y1) |
---|
| 233 | self.left_line.update(xmin= -x1, |
---|
| 234 | ymin= -y1, |
---|
| 235 | xmax= x2, |
---|
| 236 | ymax= y2, |
---|
| 237 | translation= True) |
---|
| 238 | self.right_line.update( |
---|
| 239 | xmin= -x2, |
---|
| 240 | ymin= -y2, |
---|
| 241 | xmax= x1, |
---|
| 242 | ymax= y1, |
---|
| 243 | translation= True) |
---|
| 244 | print "bottom has moved",self.left_line.slope, self.bottom_line.slope |
---|
| 245 | x2, y2= find_intersection(a1= self.main_line.slope, |
---|
| 246 | a2= self.bottom_line.slope, |
---|
| 247 | b1= self.main_line.b, |
---|
| 248 | b2= self.bottom_line.b ) |
---|
| 249 | print "main x, y max: ",x2,y2 |
---|
| 250 | x1, y1= find_intersection(a1= self.main_line.slope, |
---|
| 251 | a2= self.top_line.slope, |
---|
| 252 | b1= self.main_line.b, |
---|
| 253 | b2= self.top_line.b ) |
---|
| 254 | print "main x, y min: ",x1,y1 |
---|
| 255 | self.main_line.update(x1= -x2, |
---|
| 256 | y1= -y2, |
---|
| 257 | x2= x2, |
---|
| 258 | y2= y2, |
---|
| 259 | translation= True) |
---|
| 260 | if self.left_line.has_move: |
---|
| 261 | print "left_line has moved",self.left_line.slope, self.top_line.slope |
---|
| 262 | x2, y2= find_intersection(a1= self.left_line.slope, |
---|
| 263 | a2= self.top_line.slope, |
---|
| 264 | b1= self.left_line.b, |
---|
| 265 | b2= self.top_line.b ) |
---|
| 266 | print "main x, y max: ",x2,y2 |
---|
| 267 | x1, y1= find_intersection(a1= self.left_line.slope, |
---|
| 268 | a2= self.bottom_line.slope, |
---|
| 269 | b1= self.left_line.b, |
---|
| 270 | b2= self.bottom_line.b ) |
---|
| 271 | self.left_line.update(xmin = x1, |
---|
| 272 | xmax = x2, |
---|
| 273 | ymin= y1, |
---|
| 274 | ymax= y2, |
---|
| 275 | translation=True) |
---|
| 276 | |
---|
| 277 | self.right_line.update(xmin = -x1, |
---|
| 278 | xmax = -x2, |
---|
| 279 | ymin= -y1, |
---|
| 280 | ymax= -y2, |
---|
| 281 | translation=True) |
---|
| 282 | |
---|
| 283 | self.bottom_line.update(xmin= x1, |
---|
| 284 | ymin= y1, |
---|
| 285 | xmax= -x2, |
---|
| 286 | ymax= -y2) |
---|
| 287 | |
---|
| 288 | self.top_line.update(xmin= x2, |
---|
| 289 | ymin= y2, |
---|
| 290 | xmax= -x1, |
---|
| 291 | ymax= -y1) |
---|
| 292 | print "initial xmin", self.xmin |
---|
| 293 | self.xmin= math.sqrt(math.pow((self.main_line.x2 - self.left_line.x2),2)\ |
---|
| 294 | +math.pow((self.main_line.y2 - self.left_line.y2),2)) |
---|
| 295 | |
---|
| 296 | print "new xmin ", self.xmin, self.main_line.x2 , self.left_line.x2 |
---|
| 297 | if self.right_line.has_move: |
---|
| 298 | print "right_line has moved",self.right_line.slope, self.top_line.slope |
---|
| 299 | x2, y2= find_intersection(a1= self.right_line.slope, |
---|
| 300 | a2= self.top_line.slope, |
---|
| 301 | b1= self.right_line.b, |
---|
| 302 | b2= self.top_line.b ) |
---|
| 303 | print "main x, y max: ",x2,y2 |
---|
| 304 | x1, y1= find_intersection(a1= self.right_line.slope, |
---|
| 305 | a2= self.bottom_line.slope, |
---|
| 306 | b1= self.right_line.b, |
---|
| 307 | b2= self.bottom_line.b ) |
---|
| 308 | self.right_line.update(xmin = x1, |
---|
| 309 | xmax = x2, |
---|
| 310 | ymin= y1, |
---|
| 311 | ymax= y2, |
---|
| 312 | translation=True) |
---|
| 313 | |
---|
| 314 | self.left_line.update(xmin = -x1, |
---|
| 315 | xmax = -x2, |
---|
| 316 | ymin= -y1, |
---|
| 317 | ymax= -y2, |
---|
| 318 | translation=True) |
---|
| 319 | |
---|
| 320 | self.bottom_line.update(xmin= x1, |
---|
| 321 | ymin= y1, |
---|
| 322 | xmax= -x2, |
---|
| 323 | ymax= -y2) |
---|
| 324 | |
---|
| 325 | self.top_line.update(xmin= x2, |
---|
| 326 | ymin= y2, |
---|
| 327 | xmax= -x1, |
---|
| 328 | ymax= -y1) |
---|
| 329 | |
---|
| 330 | print "initial xmax", self.xmax |
---|
| 331 | self.xmax= math.sqrt(math.pow((self.main_line.x2 - self.right_line.x2),2)\ |
---|
| 332 | +math.pow((self.main_line.y2 - self.right_line.y2),2)) |
---|
| 333 | print "new xmax",self.xmax |
---|
| 334 | def save(self, ev): |
---|
| 335 | """ |
---|
| 336 | Remember the roughness for this layer and the next so that we |
---|
| 337 | can restore on Esc. |
---|
| 338 | """ |
---|
| 339 | self.base.freeze_axes() |
---|
| 340 | self.inner_circle.save(ev) |
---|
| 341 | self.outer_circle.save(ev) |
---|
| 342 | |
---|
| 343 | def _post_data(self): |
---|
| 344 | # Compute data |
---|
| 345 | #data = self.base.data2D |
---|
| 346 | #from DataLoader.manipulations import Boxavg |
---|
| 347 | #radius = math.sqrt(math.pow(self.qmax,2)+math.pow(self.qmax,2)) |
---|
| 348 | #x_min= self.left_line.xmin |
---|
| 349 | #x_max= self.right_line.xmax |
---|
| 350 | #y_min= self.bottom_line.y |
---|
| 351 | #y_max= self.top_line.y |
---|
| 352 | #box = Boxavg (x_min=x_min, x_max=x_max, y_min=y_min, y_max=y_max) |
---|
| 353 | |
---|
| 354 | #self.count, self.error= box(self.base.data2D) |
---|
| 355 | |
---|
| 356 | print "post data" |
---|
| 357 | |
---|
| 358 | |
---|
| 359 | def moveend(self, ev): |
---|
| 360 | self.base.thaw_axes() |
---|
| 361 | |
---|
| 362 | # Post paramters |
---|
| 363 | event = SlicerParameters.SlicerParameterEvent() |
---|
| 364 | event.type = self.__class__.__name__ |
---|
| 365 | #event.params = self.get_params() |
---|
| 366 | wx.PostEvent(self.base.parent, event) |
---|
| 367 | |
---|
| 368 | self._post_data() |
---|
| 369 | |
---|
| 370 | def restore(self): |
---|
| 371 | """ |
---|
| 372 | Restore the roughness for this layer. |
---|
| 373 | """ |
---|
| 374 | self.inner_circle.restore() |
---|
| 375 | self.outer_circle.restore() |
---|
| 376 | |
---|
| 377 | def move(self, x, y, ev): |
---|
| 378 | """ |
---|
| 379 | Process move to a new position, making sure that the move is allowed. |
---|
| 380 | """ |
---|
| 381 | pass |
---|
| 382 | |
---|
| 383 | def set_cursor(self, x, y): |
---|
| 384 | pass |
---|
| 385 | |
---|
| 386 | def get_params(self): |
---|
| 387 | params = {} |
---|
| 388 | params["x1"]= self.xmax |
---|
| 389 | params["y1"]= self.ymin |
---|
| 390 | params["x2"]= self.xmin |
---|
| 391 | params["y2"]= self.ymax |
---|
| 392 | params["phi"] = self.main_line.theta |
---|
| 393 | return params |
---|
| 394 | |
---|
| 395 | def set_params(self, params): |
---|
| 396 | self.xmax = params["x1"] |
---|
| 397 | self.ymin = params["y1"] |
---|
| 398 | self.xmin = params["x2"] |
---|
| 399 | self.ymax = params["y2"] |
---|
| 400 | theta = params["theta"] |
---|
| 401 | print "theta setparams",theta |
---|
| 402 | self.main_line.update(radius1= math.fabs(self.ymax), radius2= math.fabs(self.ymin), theta= theta) |
---|
| 403 | self.left_line.update(xmin= -1*self.xmin) |
---|
| 404 | self.right_line.update(xmin= self.xmax) |
---|
| 405 | |
---|
| 406 | self.top_line.update(xmin= self.right_line.x1, |
---|
| 407 | xmax= self.left_line.x1, |
---|
| 408 | ymin= self.right_line.y1, |
---|
| 409 | ymax= self.left_line.y1) |
---|
| 410 | self.bottom_line.update(xmin= self.right_line.x2, |
---|
| 411 | xmax= self.left_line.x2, |
---|
| 412 | ymin= self.right_line.y2, |
---|
| 413 | ymax= self.left_line.y2) |
---|
| 414 | self._post_data() |
---|
| 415 | def freeze_axes(self): |
---|
| 416 | self.base.freeze_axes() |
---|
| 417 | |
---|
| 418 | def thaw_axes(self): |
---|
| 419 | self.base.thaw_axes() |
---|
| 420 | |
---|
| 421 | def draw(self): |
---|
| 422 | self.base.draw() |
---|
| 423 | |
---|
| 424 | class HorizontalLine(_BaseInteractor): |
---|
| 425 | """ |
---|
| 426 | Select an annulus through a 2D plot |
---|
| 427 | """ |
---|
| 428 | def __init__(self,base,axes,color='black', zorder=5,mline=None,ymin=None, ymax=None, y=0.5, |
---|
| 429 | xmin=0.0,xmax=0.5, |
---|
| 430 | theta2= math.pi/3 ): |
---|
| 431 | |
---|
| 432 | _BaseInteractor.__init__(self, base, axes, color=color) |
---|
| 433 | self.markers = [] |
---|
| 434 | self.axes = axes |
---|
| 435 | self.x1= xmax |
---|
| 436 | self.save_x1= xmax |
---|
| 437 | |
---|
| 438 | self.x2= xmin |
---|
| 439 | self.save_x2= xmin |
---|
| 440 | |
---|
| 441 | self.y1= ymax |
---|
| 442 | self.save_y1= ymax |
---|
| 443 | |
---|
| 444 | self.y2= ymin |
---|
| 445 | self.save_y2= ymin |
---|
| 446 | self.mline= mline |
---|
| 447 | self.line = self.axes.plot([self.x1,self.x2], |
---|
| 448 | [self.y1,self.y2], |
---|
| 449 | linestyle='-', marker='', |
---|
| 450 | color=self.color, |
---|
| 451 | visible=True)[0] |
---|
| 452 | |
---|
| 453 | self.slope= -1/math.tan(self.mline.theta) |
---|
| 454 | self.b= self.y1- self.slope* self.x1 |
---|
| 455 | self.save_b= self.b |
---|
| 456 | print "slope from point",(self.y2- self.y1)/(self.x2- self.x1) |
---|
| 457 | print "my slope horizontal", self.slope |
---|
| 458 | print "b from point ", self.y2- self.slope*self.x2, self.b |
---|
| 459 | self.npts = 20 |
---|
| 460 | self.has_move=False |
---|
| 461 | self.connect_markers([self.line]) |
---|
| 462 | self.update() |
---|
| 463 | |
---|
| 464 | def set_layer(self, n): |
---|
| 465 | self.layernum = n |
---|
| 466 | self.update() |
---|
| 467 | |
---|
| 468 | def clear(self): |
---|
| 469 | self.clear_markers() |
---|
| 470 | try: |
---|
| 471 | |
---|
| 472 | self.line.remove() |
---|
| 473 | except: |
---|
| 474 | # Old version of matplotlib |
---|
| 475 | for item in range(len(self.axes.lines)): |
---|
| 476 | del self.axes.lines[0] |
---|
| 477 | |
---|
| 478 | def get_radius(self): |
---|
| 479 | |
---|
| 480 | return 0 |
---|
| 481 | |
---|
| 482 | def update(self,xmin=None, xmax=None,ymin=None,ymax=None, mline=None,translation=False): |
---|
| 483 | """ |
---|
| 484 | Draw the new roughness on the graph. |
---|
| 485 | """ |
---|
| 486 | self.slope= -1/math.tan(self.mline.theta) |
---|
| 487 | if xmin !=None: |
---|
| 488 | self.x2 = xmin |
---|
| 489 | if xmax !=None: |
---|
| 490 | self.x1 = xmax |
---|
| 491 | if ymin !=None: |
---|
| 492 | self.y2 = ymin |
---|
| 493 | if ymax != None: |
---|
| 494 | self.y1 = ymax |
---|
| 495 | self.b= self.y1- self.slope * self.x1 |
---|
| 496 | self.line.set(xdata=[self.x1,self.x2], |
---|
| 497 | ydata=[self.y1,self.y2]) |
---|
| 498 | |
---|
| 499 | |
---|
| 500 | |
---|
| 501 | def save(self, ev): |
---|
| 502 | """ |
---|
| 503 | Remember the roughness for this layer and the next so that we |
---|
| 504 | can restore on Esc. |
---|
| 505 | """ |
---|
| 506 | self.save_x1= self.x1 |
---|
| 507 | self.save_x2= self.x2 |
---|
| 508 | |
---|
| 509 | self.save_y1= self.y1 |
---|
| 510 | self.save_y2= self.y2 |
---|
| 511 | self.save_b= self.b |
---|
| 512 | |
---|
| 513 | self.base.freeze_axes() |
---|
| 514 | |
---|
| 515 | def moveend(self, ev): |
---|
| 516 | |
---|
| 517 | self.has_move=False |
---|
| 518 | self.base.moveend(ev) |
---|
| 519 | |
---|
| 520 | def restore(self): |
---|
| 521 | """ |
---|
| 522 | Restore the roughness for this layer. |
---|
| 523 | """ |
---|
| 524 | self.x1 = self.save_x1 |
---|
| 525 | self.x2 = self.save_x2 |
---|
| 526 | self.y1 = self.save_y1 |
---|
| 527 | self.y2 = self.save_y2 |
---|
| 528 | self.b = self.save_b |
---|
| 529 | |
---|
| 530 | def move(self, x, y, ev): |
---|
| 531 | """ |
---|
| 532 | Process move to a new position, making sure that the move is allowed. |
---|
| 533 | """ |
---|
| 534 | print "horizontal move x y ", x, y |
---|
| 535 | self.b = y - (-1/self.mline.slope) *x |
---|
| 536 | self.has_move=True |
---|
| 537 | self.base.base.update() |
---|
| 538 | |
---|
| 539 | def set_cursor(self, x, y): |
---|
| 540 | self.move(x, y, None) |
---|
| 541 | self.update() |
---|
| 542 | |
---|
| 543 | |
---|
| 544 | def get_params(self): |
---|
| 545 | params = {} |
---|
| 546 | params["radius"] = self.x1 |
---|
| 547 | #params["theta"] = self.xmax |
---|
| 548 | return params |
---|
| 549 | |
---|
| 550 | def set_params(self, params): |
---|
| 551 | |
---|
| 552 | x = params["radius"] |
---|
| 553 | self.set_cursor(x, self._inner_mouse_y) |
---|
| 554 | |
---|
| 555 | |
---|
| 556 | |
---|
| 557 | |
---|
| 558 | class VerticalLine(_BaseInteractor): |
---|
| 559 | """ |
---|
| 560 | Select an annulus through a 2D plot |
---|
| 561 | """ |
---|
| 562 | def __init__(self,base,axes,color='black', zorder=5, mline=None, ymin=0.0, |
---|
| 563 | ymax=0.5,xmin=-0.5,xmax=0.5, |
---|
| 564 | theta2= math.pi/3 ): |
---|
| 565 | |
---|
| 566 | _BaseInteractor.__init__(self, base, axes, color=color) |
---|
| 567 | self.markers = [] |
---|
| 568 | self.axes = axes |
---|
| 569 | |
---|
| 570 | self.theta2 = mline.theta |
---|
| 571 | self.mline =mline |
---|
| 572 | self.xmin= xmin |
---|
| 573 | self.x1= mline.x1 + xmin*math.cos(math.pi/2 - self.theta2) |
---|
| 574 | self.x2= mline.x2 + xmin*math.cos(math.pi/2 - self.theta2) |
---|
| 575 | self.y1= mline.y1 - xmin*math.sin(math.pi/2 - self.theta2) |
---|
| 576 | self.y2= mline.y2 - xmin*math.sin(math.pi/2 - self.theta2) |
---|
| 577 | self.line = self.axes.plot([self.x1,self.x2],[self.y1,self.y2], |
---|
| 578 | linestyle='-', marker='', |
---|
| 579 | color=self.color, |
---|
| 580 | visible=True)[0] |
---|
| 581 | |
---|
| 582 | |
---|
| 583 | self.slope= math.tan(self.mline.theta) |
---|
| 584 | print "vertical line intercetp ",self.y2- self.slope*self.x2, self.y1- self.slope* self.x1 |
---|
| 585 | |
---|
| 586 | self.b = self.y1- self.slope* self.x1 |
---|
| 587 | self.has_move=False |
---|
| 588 | self.connect_markers([self.line]) |
---|
| 589 | self.update() |
---|
| 590 | |
---|
| 591 | def set_layer(self, n): |
---|
| 592 | self.layernum = n |
---|
| 593 | self.update() |
---|
| 594 | |
---|
| 595 | def clear(self): |
---|
| 596 | self.clear_markers() |
---|
| 597 | try: |
---|
| 598 | |
---|
| 599 | self.line.remove() |
---|
| 600 | except: |
---|
| 601 | # Old version of matplotlib |
---|
| 602 | for item in range(len(self.axes.lines)): |
---|
| 603 | del self.axes.lines[0] |
---|
| 604 | |
---|
| 605 | |
---|
| 606 | def get_radius(self): |
---|
| 607 | return 0 |
---|
| 608 | |
---|
| 609 | def update(self,xmin=None,xmax=None,ymin=None, ymax=None, opline=None,translation=False): |
---|
| 610 | """ |
---|
| 611 | Draw the new roughness on the graph. |
---|
| 612 | """ |
---|
| 613 | |
---|
| 614 | self.slope= math.tan(self.mline.theta) |
---|
| 615 | if translation: |
---|
| 616 | if xmin!=None: |
---|
| 617 | self.x2=xmin |
---|
| 618 | self.xmin= xmin |
---|
| 619 | if xmax!=None: |
---|
| 620 | self.x1=xmax |
---|
| 621 | if ymin!=None: |
---|
| 622 | self.y2=ymin |
---|
| 623 | if ymax!=None: |
---|
| 624 | self.y1=ymax |
---|
| 625 | self.line.set(xdata=[self.x1,self.x2], |
---|
| 626 | ydata=[self.y1,self.y2]) |
---|
| 627 | self.b= self.y1- self.slope * self.x1 |
---|
| 628 | return |
---|
| 629 | |
---|
| 630 | self.x1= self.mline.x1 + self.xmin*math.cos(math.pi/2 - self.mline.theta) |
---|
| 631 | self.x2= self.mline.x2 + self.xmin*math.cos(math.pi/2 - self.mline.theta) |
---|
| 632 | self.y1= self.mline.y1 - self.xmin*math.sin(math.pi/2 - self.mline.theta) |
---|
| 633 | self.y2= self.mline.y2 - self.xmin*math.sin(math.pi/2 - self.mline.theta) |
---|
| 634 | self.line.set(xdata=[self.x1,self.x2], |
---|
| 635 | ydata=[self.y1,self.y2]) |
---|
| 636 | print "update slope ", (self.y2-self.y1)/(self.x2- self.x1) |
---|
| 637 | #print "main slope", math.tan(self.mline.theta) |
---|
| 638 | |
---|
| 639 | def save(self, ev): |
---|
| 640 | """ |
---|
| 641 | Remember the roughness for this layer and the next so that we |
---|
| 642 | can restore on Esc. |
---|
| 643 | """ |
---|
| 644 | self.save_x1= self.x1 |
---|
| 645 | self.save_x2= self.x2 |
---|
| 646 | self.save_y1= self.y1 |
---|
| 647 | self.save_y2= self.y2 |
---|
| 648 | |
---|
| 649 | self.base.freeze_axes() |
---|
| 650 | |
---|
| 651 | def moveend(self, ev): |
---|
| 652 | |
---|
| 653 | self.has_move=False |
---|
| 654 | self.base.moveend(ev) |
---|
| 655 | |
---|
| 656 | def restore(self): |
---|
| 657 | """ |
---|
| 658 | Restore the roughness for this layer. |
---|
| 659 | """ |
---|
| 660 | self.x1 = self.save_x1 |
---|
| 661 | self.x2 = self.save_x2 |
---|
| 662 | self.y1 = self.save_y1 |
---|
| 663 | self.y2= self.save_y2 |
---|
| 664 | |
---|
| 665 | |
---|
| 666 | def move(self, x, y, ev): |
---|
| 667 | """ |
---|
| 668 | Process move to a new position, making sure that the move is allowed. |
---|
| 669 | """ |
---|
| 670 | self.has_move=True |
---|
| 671 | |
---|
| 672 | # compute the b intercept of the vertical line |
---|
| 673 | self.b=y - self.mline.slope * x |
---|
| 674 | |
---|
| 675 | |
---|
| 676 | self.base.base.update() |
---|
| 677 | |
---|
| 678 | |
---|
| 679 | def set_cursor(self, x, y): |
---|
| 680 | self.move(x, y, None) |
---|
| 681 | self.update() |
---|
| 682 | |
---|
| 683 | |
---|
| 684 | def get_params(self): |
---|
| 685 | params = {} |
---|
| 686 | params["x"] = self.xmin |
---|
| 687 | params["ymin"] = self.ymin |
---|
| 688 | params["ymax"] = self.ymax |
---|
| 689 | return params |
---|
| 690 | |
---|
| 691 | def set_params(self, params): |
---|
| 692 | """ |
---|
| 693 | Draw a vertical line given some value of params |
---|
| 694 | @param params: a dictionary containing value for x, ymin , ymax to draw |
---|
| 695 | a vertical line |
---|
| 696 | """ |
---|
| 697 | x = params["x"] |
---|
| 698 | ymin = params["ymin"] |
---|
| 699 | ymax = params["ymax"] |
---|
| 700 | #self.set_cursor(x, self._inner_mouse_y) |
---|
| 701 | self.update(self,x =x,ymin =ymin, ymax =ymax) |
---|
| 702 | |
---|
| 703 | |
---|
| 704 | |
---|
| 705 | class LineInteractor(_BaseInteractor): |
---|
| 706 | """ |
---|
| 707 | Select an annulus through a 2D plot |
---|
| 708 | """ |
---|
| 709 | def __init__(self,base,axes,color='black', zorder=5, ymin=1.0,ymax=1.0,theta=math.pi/4): |
---|
| 710 | |
---|
| 711 | _BaseInteractor.__init__(self, base, axes, color=color) |
---|
| 712 | self.markers = [] |
---|
| 713 | self.axes = axes |
---|
| 714 | |
---|
| 715 | self.save_theta = theta |
---|
| 716 | self.theta= theta |
---|
| 717 | |
---|
| 718 | self.radius1 = math.fabs(ymax) |
---|
| 719 | self.radius2 = math.fabs(ymin) |
---|
| 720 | self.scale = 10.0 |
---|
| 721 | |
---|
| 722 | # Inner circle |
---|
| 723 | self.x1= self.radius1*math.cos(self.theta) |
---|
| 724 | self.y1= self.radius1*math.sin(self.theta) |
---|
| 725 | self.x2= -1*self.radius2*math.cos(self.theta) |
---|
| 726 | self.y2= -1*self.radius2*math.sin(self.theta) |
---|
| 727 | |
---|
| 728 | self.line = self.axes.plot([self.x1,self.x2],[self.y1,self.y2], |
---|
| 729 | linestyle='-', marker='', |
---|
| 730 | color=self.color, |
---|
| 731 | visible=True)[0] |
---|
| 732 | self.slope= math.tan(self.theta) |
---|
| 733 | self.b= math.fabs(self.y1- self.slope * self.x1) |
---|
| 734 | print "intercept main",math.fabs(self.y2- self.slope * self.x2),math.fabs(self.y1- self.slope * self.x1) |
---|
| 735 | self.npts = 20 |
---|
| 736 | self.has_move=False |
---|
| 737 | self.connect_markers([self.line]) |
---|
| 738 | self.update() |
---|
| 739 | |
---|
| 740 | def set_layer(self, n): |
---|
| 741 | |
---|
| 742 | self.layernum = n |
---|
| 743 | self.update() |
---|
| 744 | |
---|
| 745 | def clear(self): |
---|
| 746 | """ |
---|
| 747 | Remove the line of the plot |
---|
| 748 | """ |
---|
| 749 | self.clear_markers() |
---|
| 750 | try: |
---|
| 751 | self.line.remove() |
---|
| 752 | except: |
---|
| 753 | # Old version of matplotlib |
---|
| 754 | for item in range(len(self.axes.lines)): |
---|
| 755 | del self.axes.lines[0] |
---|
| 756 | |
---|
| 757 | |
---|
| 758 | |
---|
| 759 | def get_delta_angle(self): |
---|
| 760 | """ |
---|
| 761 | return difference between initial angle and the final angle during |
---|
| 762 | rotation |
---|
| 763 | """ |
---|
| 764 | return self.theta - self.save_theta |
---|
| 765 | |
---|
| 766 | def update(self,x1=None, |
---|
| 767 | y1=None, |
---|
| 768 | x2=None, |
---|
| 769 | y2=None, |
---|
| 770 | translation=False, |
---|
| 771 | xmin=None,vline=None, theta=None,radius1=None,radius2=None): |
---|
| 772 | """ |
---|
| 773 | Draw a line given and angle relative to the x-axis and a radius |
---|
| 774 | @param theta: the angle realtive to the x-axis |
---|
| 775 | @param radius: the distance between the center and one end of the line |
---|
| 776 | """ |
---|
| 777 | if translation: |
---|
| 778 | if x1 !=None: |
---|
| 779 | self.x1= x1 |
---|
| 780 | if x2 !=None: |
---|
| 781 | self.x2= x2 |
---|
| 782 | if y1 !=None: |
---|
| 783 | self.y1= y1 |
---|
| 784 | if y2 !=None: |
---|
| 785 | self.y2= y2 |
---|
| 786 | |
---|
| 787 | self.line.set(xdata=[self.x1,self.x2], ydata=[self.y1,self.y2]) |
---|
| 788 | self.radius1= math.fabs(self.x1/math.cos(self.theta)) |
---|
| 789 | self.radius2= math.fabs(self.x2/math.cos(self.theta)) |
---|
| 790 | print "radius 1, radius2", self.radius1, self.radius2 |
---|
| 791 | return |
---|
| 792 | |
---|
| 793 | if theta !=None: |
---|
| 794 | self.theta= theta |
---|
| 795 | if radius1 !=None: |
---|
| 796 | self.radius1 =radius1 |
---|
| 797 | if radius2 !=None: |
---|
| 798 | self.radius2 =radius2 |
---|
| 799 | #print "update main line", math.degrees(self.theta),self.radius1, self.radius2 |
---|
| 800 | #print "smain radius 1 2", self.radius1, self.radius2 |
---|
| 801 | self.x1= self.radius1*math.cos(self.theta) |
---|
| 802 | self.y1= self.radius1*math.sin(self.theta) |
---|
| 803 | self.x2= -1*self.radius2*math.cos(self.theta) |
---|
| 804 | self.y2= -1*self.radius2*math.sin(self.theta) |
---|
| 805 | print "init mainline sintercept ", self.y1 - math.tan(self.theta)*self.x1,\ |
---|
| 806 | self.y2 - math.tan(self.theta)*self.x2 |
---|
| 807 | #print "main line slope ", (self.y2- self.y1)/(self.x2- self.x1) |
---|
| 808 | #print "theta", math.tan(self.theta) |
---|
| 809 | self.line.set(xdata=[self.x1,self.x2], ydata=[self.y1,self.y2]) |
---|
| 810 | |
---|
| 811 | |
---|
| 812 | |
---|
| 813 | def save(self, ev): |
---|
| 814 | """ |
---|
| 815 | Remember the roughness for this layer and the next so that we |
---|
| 816 | can restore on Esc. |
---|
| 817 | """ |
---|
| 818 | self.save_theta= self.theta |
---|
| 819 | self.base.freeze_axes() |
---|
| 820 | |
---|
| 821 | def moveend(self, ev): |
---|
| 822 | |
---|
| 823 | self.has_move=False |
---|
| 824 | self.base.moveend(ev) |
---|
| 825 | |
---|
| 826 | def restore(self): |
---|
| 827 | """ |
---|
| 828 | Restore the roughness for this layer. |
---|
| 829 | """ |
---|
| 830 | self.theta = self.save_theta |
---|
| 831 | |
---|
| 832 | def move(self, x, y, ev): |
---|
| 833 | """ |
---|
| 834 | Process move to a new position, making sure that the move is allowed. |
---|
| 835 | """ |
---|
| 836 | self.theta= math.atan2(y,x) |
---|
| 837 | self.slope= math.tan(self.theta) |
---|
| 838 | self.b= y - self.slope *x |
---|
| 839 | |
---|
| 840 | print "main move slope , theta, b", self.slope, self.theta, self.b |
---|
| 841 | |
---|
| 842 | #print "main_line previous theta --- next theta ",math.degrees(self.save_theta),math.degrees(self.theta) |
---|
| 843 | self.has_move=True |
---|
| 844 | self.base.base.update() |
---|
| 845 | |
---|
| 846 | |
---|
| 847 | def set_cursor(self, x, y): |
---|
| 848 | |
---|
| 849 | self.move(x, y, None) |
---|
| 850 | self.update() |
---|
| 851 | |
---|
| 852 | |
---|
| 853 | def get_params(self): |
---|
| 854 | """ |
---|
| 855 | return params a dictionary containing values of paramters necessary to draw |
---|
| 856 | this line |
---|
| 857 | """ |
---|
| 858 | params = {} |
---|
| 859 | params["ymax"] = self.radius1 |
---|
| 860 | params["ymin"] = self.radius2 |
---|
| 861 | params["theta"] = self.theta |
---|
| 862 | return params |
---|
| 863 | |
---|
| 864 | def set_params(self, params): |
---|
| 865 | """ |
---|
| 866 | Draw the line given value contains by params |
---|
| 867 | @param params: dictionary containing name of parameters and their values |
---|
| 868 | """ |
---|
| 869 | radius1 = params["ymax"] |
---|
| 870 | radius2 = params["ymin"] |
---|
| 871 | theta = params["theta"] |
---|
| 872 | self.update(x, theta= theta , radius1 = radius1 ,radius2 = radius2) |
---|
| 873 | |
---|
| 874 | |
---|
| 875 | |
---|
| 876 | |
---|
| 877 | |
---|