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