Package omero :: Package util :: Module ROI_utils
[hide private]
[frames] | no frames]

Source Code for Module omero.util.ROI_utils

  1  # 
  2  # 
  3  #------------------------------------------------------------------------------ 
  4  #  Copyright (C) 2006-2009 University of Dundee. All rights reserved. 
  5  # 
  6  # 
  7  #       This program is free software; you can redistribute it and/or modify 
  8  #  it under the terms of the GNU General Public License as published by 
  9  #  the Free Software Foundation; either version 2 of the License, or 
 10  #  (at your option) any later version. 
 11  #  This program is distributed in the hope that it will be useful, 
 12  #  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 13  #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 14  #  GNU General Public License for more details. 
 15  # 
 16  #  You should have received a copy of the GNU General Public License along 
 17  #  with this program; if not, write to the Free Software Foundation, Inc., 
 18  #  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 
 19  # 
 20  #------------------------------------------------------------------------------ 
 21   
 22  ### 
 23  # 
 24  # ROIUtils allows the mapping of omero.model.ROIDataTypesI to python types 
 25  # and to create ROIDataTypesI from ROIUtil types.  
 26  # These methods also implement the acceptVisitor method linking to the ROIDrawingCanvas. 
 27  # 
 28  # 
 29  # @author  Jean-Marie Burel      
 30  #       <a href="mailto:j.burel@dundee.ac.uk">j.burel@dundee.ac.uk</a> 
 31  # @author       Donald MacDonald &nbsp;&nbsp;&nbsp;&nbsp; 
 32  #       <a href="mailto:donald@lifesci.dundee.ac.uk">donald@lifesci.dundee.ac.uk</a> 
 33  # @version 3.0 
 34  # <small> 
 35  # (<b>Internal version:</b> $Revision: $Date: $) 
 36  # </small> 
 37  # @since 3.0-Beta4 
 38  #/ 
 39  import omero.clients 
 40  from omero.model import RoiI 
 41  from omero.model import EllipseI 
 42  from omero.model import LineI 
 43  from omero.model import RectI 
 44  from omero.model import PointI 
 45  from omero.model import TextI 
 46  from omero.model import PolylineI 
 47  from omero.model import PolygonI 
 48  from omero.model import PathI 
 49  from omero.rtypes import rdouble  
 50  from omero.rtypes import rstring  
 51  from omero.rtypes import rint  
 52  from omero.rtypes import rfloat  
 53   
 54   
 55  ## 
 56  # abstract, defines the method that call it as abstract. 
 57  # 
 58  # 
59 -def abstract():
60 import inspect 61 caller = inspect.getouterframes(inspect.currentframe())[1][3] 62 raise NotImplementedError(caller + ' must be implemented in subclass')
63 64 ## 65 # ShapeSettingsData contains all the display information about the ROI that aggregates it. 66 #
67 -class ShapeSettingsData:
68 69 ## 70 # Initialises the default values of the ShapeSettings. 71 # Stroke has default colour of darkGrey 72 # StrokeWidth defaults to 1 73 #
74 - def __init__(self):
75 self.WHITE = 16777215 76 self.BLACK = 0 77 self.GREY = 11184810 78 self.strokeColour = rint(GREY) 79 self.strokeWidth = rint(1) 80 self.strokeDashArray = rstring('') 81 self.strokeDashOffset = rint(0) 82 self.strokeLineCap = rstring('') 83 self.strokeLineJoin = rstring('') 84 self.strokeMiterLimit = rint(0) 85 self.fillColour = rint(GREY) 86 self.fillRule = rstring('')
87 88 ## 89 # Applies the settings in the ShapeSettingsData to the ROITypeI 90 # @param shape the omero.model.ROITypeI that these settings will be applied to 91 #
92 - def setROIShapeSettings(self, shape):
93 shape.setStrokeColor(self.strokeColour); 94 shape.setStrokeWidth(self.strokeWidth); 95 shape.setStrokeDashArray(self.strokeDashArray); 96 shape.setStrokeDashOffset(self.strokeDashOffset); 97 shape.setStrokeLineCap(self.strokeLineCap); 98 shape.setStrokeLineJoin(self.strokeLineJoin); 99 shape.setStrokeMiterLimit(self.strokeMiterLimit); 100 shape.setFillColor(self.fillColour); 101 shape.setFillRule(self.fillRule);
102 103 ## 104 # Set the Stroke settings of the ShapeSettings. 105 # @param colour The colour of the stroke. 106 # @param width The stroke width. 107 #
108 - def setStrokeSettings(self, colour, width = 1):
109 self.strokeColour = rint(colour); 110 self.strokeWidth = rint(width);
111 112 ### 113 # Set the Fill Settings for the ShapeSettings. 114 # @param colour The fill colour of the shape.
115 - def setFillSettings(self, colour):
116 self.fillColour = rsting(colour);
117 118 ## 119 # Get the stroke settings as the tuple (strokeColour, strokeWidth). 120 # @return See above. 121 #
122 - def getStrokeSettings(self):
123 return (self.strokeColour.getValue(), self.strokeWidth.getValue());
124 125 ## 126 # Get the fill setting as a tuple of (fillColour) 127 # @return See above. 128 #
129 - def getFillSettings(self):
130 return (self.fillColour.getValue());
131 132 ## 133 # Get the tuple ((stokeColor, strokeWidth), (fillColour)). 134 # @return see above. 135 #
136 - def getSettings(self):
137 return (self.getStrokeSettings(), self.getFillSettings());
138 139 ## 140 # Set the current shapeSettings from the ROI roi. 141 # @param roi see above. 142 #
143 - def getShapeSettingsFromROI(self, roi):
144 self.strokeColour = roi.getStrokeColor(); 145 self.strokeWidth = roi.getStrokeWidth(); 146 self.strokeDashArray = roi.getStrokeDashArray(); 147 self.strokeDashOffset = roi.getStrokeDashOffset(); 148 self.strokeLineCap = roi.getStrokeLineCap(); 149 self.strokeLineJoin = roi.getStrokeLineJoin(); 150 self.strokeMiterLimit = roi.getStrokeMiterLimit(); 151 self.fillColour = roi.getFillColor(); 152 self.fillRule = roi.getFillRule();
153 154 ## 155 # This class stores the ROI Coordinate (Z,T). 156 #
157 -class ROICoordinate:
158 159 ## 160 # Initialise the ROICoordinate. 161 # @param z The z-section. 162 # @param t The timepoint.
163 - def __init__(self, z = 0, t = 0):
164 self.theZ = rint(z); 165 self.theT = rint(t);
166 167 ## 168 # Set the (z, t) for the roi using the (z, t) of the ROICoordinate. 169 # @param roi The ROI to set the (z, t) on. 170 #
171 - def setROICoord(self, roi):
172 roi.setTheZ(self.theZ); 173 roi.setTheT(self.theT);
174 175 ## 176 # Get the (z, t) from the ROI. 177 # @param See above. 178 #
179 - def setCoordFromROI(self, roi):
180 self.theZ = roi.getTheZ(); 181 self.theT = roi.getTheT();
182 183 ## 184 # Interface to inherit for accepting ROIDrawing as a visitor. 185 # @param visitor The ROIDrawingCompoent. 186 #
187 -class ROIDrawingI:
188 - def acceptVisitor(self, visitor):
189 abstract();
190 191 ## 192 # The base class for all ROIShapeData objects. 193 #
194 -class ShapeData:
195 196 ## 197 # Constructor sets up the coord, shapeSettings and ROI objects. 198 #
199 - def __init__(self):
200 self.coord = ROICoordinate(); 201 self.shapeSettings = ShapeSettingsData(); 202 self.ROI = None;
203 204 ## 205 # Set the coord of the class to coord. 206 # @param See above. 207 #
208 - def setCoord(self, coord):
209 self.coord = coord;
210 211 ## 212 # Set the ROICoordinate of the roi. 213 # @param roi See above. 214 #
215 - def setROICoord(self, roi):
216 self.coord.setROICoord(roi);
217 218 ## 219 # Set the Geometry of the roi from the geometry in ShapeData. 220 # @param roi See above. 221 #
222 - def setROIGeometry(self, roi):
223 abstract();
224 225 ## 226 # Set the Settings of the ShapeDate form the settings object. 227 # @param settings See above. 228 #
229 - def setShapeSettings(self, settings):
230 self.shapeSettings = settings;
231 232 ## 233 # Set the Settings of the roi from the setting in ShapeData. 234 # @param roi See above. 235 #
236 - def setROIShapeSettings(self, roi):
237 self.shapeSettings.setROIShapeSettings(roi);
238 239 ## 240 # Accept visitor. 241 # @param visitor See above. 242 #
243 - def acceptVisitor(self, visitor):
244 abstract();
245 246 ## 247 # Create the base type of ROI for this shape. 248 #
249 - def createBaseType(self):
250 abstract();
251 252 ## 253 # Get the roi from the ShapeData. If the roi already exists return it. 254 # Otherwise create it from the ShapeData and return it. 255 # @return See above. 256 #
257 - def getROI(self):
258 if(self.roi != None): 259 return self.roi; 260 self.roi = self.createBaseType(); 261 self.setROICoord(roi); 262 self.setROIGeometry(roi); 263 self.setROIShapeSettings(roi); 264 return self.roi;
265 266 ## 267 # Set the shape settings object from the roi. 268 # @param roi see above. 269 #
270 - def getShapeSettingsFromROI(self, roi):
271 self.shapeSettings.getShapeSettingsFromROI(roi);
272 273 ## 274 # Set the ROICoordinate from the roi. 275 # @param roi See above. 276 #
277 - def getCoordFromROI(self, roi):
278 self.coord.setCoordFromROI(roi);
279 280 ## 281 # Set the Geometr from the roi. 282 # @param roi See above. 283 #
284 - def getGeometryFromROI(self , roi):
285 abstract();
286 287 ## 288 # Get all settings from the roi, Geomerty, Shapesettins, ROICoordinate. 289 # @param roi See above. 290 #
291 - def fromROI(self, roi):
292 self.roi = roi; 293 self.getShapeSettingsFromROI(roi); 294 self.getCoordFromROI(roi); 295 self.getGeometryFromROI(roi);
296 297 ## 298 # The EllispeData class contains all the manipulation and create of EllipseI 299 # types. 300 # It also accepts the ROIDrawingUtils visitor for drawing ellipses. 301 #
302 -class EllipseData(ShapeData, ROIDrawingI):
303 304 ## 305 # Constructor for EllipseData object. 306 # @param roicoord The ROICoordinate of the object (default: 0,0) 307 # @param cx The centre x coordinate of the ellipse. 308 # @param cy The centre y coordinate of the ellipse. 309 # @param rx The major axis of the ellipse. 310 # @param ry The minor axis of the ellipse.
311 - def __init__(self, roicoord = ROICoordinate(), cx = 0, cy = 0, rx = 0, ry = 0):
312 ShapeData.__init__(self); 313 self.cx = rdouble(cx); 314 self.cy = rdouble(cy); 315 self.rx = rdouble(rx); 316 self.ry = rdouble(ry); 317 self.setCoord(roicoord);
318 319 ## 320 # overridden, @See ShapeData#setROIGeometry 321 #
322 - def setROIGeometry(self, ellipse):
323 ellipse.setTheZ(self.coord.theZ); 324 ellipse.setTheT(self.coord.theZ); 325 ellipse.setCx(self.cx); 326 ellipse.setCy(self.cy); 327 ellipse.setRx(self.rx); 328 ellipse.setRy(self.ry);
329 330 ## 331 # overridden, @See ShapeData#getGeometryFromROI 332 #
333 - def getGeometryFromROI(self, roi):
334 self.cx = roi.getCx(); 335 self.cy = roi.getCy(); 336 self.rx = roi.getRx(); 337 self.ry = roi.getRy();
338 339 ## 340 # overridden, @See ShapeData#createBaseType 341 #
342 - def createBaseType(self):
343 return EllipseI();
344 345 ## 346 # overridden, @See ShapeData#acceptVisitor 347 #
348 - def acceptVisitor(self, visitor):
349 visitor.drawEllipse(self.cx.getValue(), self.cy.getValue(), self.rx.getValue(), self.ry.getValue(), self.shapeSettings.getSettings());
350 351 ## 352 # The RectangleData class contains all the manipulation and create of RectI 353 # types. 354 # It also accepts the ROIDrawingUtils visitor for drawing rectangles. 355 #
356 -class RectangleData(ShapeData, ROIDrawingI):
357 358 ## 359 # Constructor for RectangleData object. 360 # @param roicoord The ROICoordinate of the object (default: 0,0) 361 # @param x The top left x - coordinate of the shape. 362 # @param y The top left y - coordinate of the shape. 363 # @param width The width of the shape. 364 # @param height The height of the shape.
365 - def __init__(self, roicoord = ROICoordinate(), x = 0, y = 0, width = 0, height = 0):
366 ShapeData.__init__(self); 367 self.x = rdouble(x); 368 self.y = rdouble(y); 369 self.width = rdouble(width); 370 self.height = rdouble(height); 371 self.setCoord(roicoord);
372 373 ## 374 # overridden, @See ShapeData#setGeometry 375 #
376 - def setGeometry(self, rectangle):
377 rectangle.setTheZ(self.coord.theZ); 378 rectangle.setTheT(self.coord.theZ); 379 rectangle.setX(self.x); 380 rectangle.setY(self.y); 381 rectangle.setWidth(self.width); 382 rectangle.setHeight(self.height);
383 384 ## 385 # overridden, @See ShapeData#getGeometryFromROI 386 #
387 - def getGeometryFromROI(self, roi):
388 self.x = roi.getX(); 389 self.y = roi.getY(); 390 self.width = roi.getWidth(); 391 self.height = roi.getHeight();
392 393 ## 394 # overridden, @See ShapeData#createBaseType 395 #
396 - def createBaseType(self):
397 return RectI();
398 399 ## 400 # overridden, @See ShapeData#acceptVisitor 401 #
402 - def acceptVisitor(self, visitor):
403 visitor.drawRectangle(self.x, self.y, self.width, self.height, self.shapeSettings.getSettings());
404 ## 405 # The LineData class contains all the manipulation and create of LineI 406 # types. 407 # It also accepts the ROIDrawingUtils visitor for drawing lines. 408 #
409 -class LineData(ShapeData, ROIDrawingI):
410 411 ## 412 # Constructor for LineData object. 413 # @param roicoord The ROICoordinate of the object (default: 0,0) 414 # @param x1 The first x coordinate of the shape. 415 # @param y1 The first y coordinate of the shape. 416 # @param x2 The second x coordinate of the shape. 417 # @param y2 The second y coordinate of the shape.
418 - def __init__(self, roicoord = ROICoordinate(), x1 = 0, y1 = 0, x2 = 0, y2 = 0):
419 ShapeData.__init__(self); 420 self.x1 = rdouble(x1); 421 self.y1 = rdouble(y1); 422 self.x2 = rdouble(x2); 423 self.y2 = rdouble(y2); 424 self.setCoord(roicoord);
425 426 ## 427 # overridden, @See ShapeData#setGeometry 428 #
429 - def setGeometry(self, line):
430 line.setTheZ(self.coord.theZ); 431 line.setTheT(self.coord.theZ); 432 line.setX1(self.x1); 433 line.setY1(self.y1); 434 line.setX2(self.x2); 435 line.setY2(self.y2);
436 437 ## 438 # overridden, @See ShapeData#getGeometryFromROI 439 #
440 - def getGeometryFromROI(self, roi):
441 self.x1 = roi.getX1(); 442 self.y1 = roi.getY1(); 443 self.x2 = roi.getX2(); 444 self.y2 = roi.getY2();
445 446 ## 447 # overridden, @See ShapeData#createBaseType 448 #
449 - def createBaseType(self):
450 return LineI();
451 452 ## 453 # overridden, @See ShapeData#acceptVisitor 454 #
455 - def acceptVisitor(self, visitor):
456 visitor.drawLine(self.x1.getValue(), self.y1.getValue(), self.x2.getValue(), self.y2.getValue(), self.shapeSettings.getSettings());
457 458 ## 459 # The MaskData class contains all the manipulation and create of MaskI 460 # types. 461 # It also accepts the ROIDrawingUtils visitor for drawing masks. 462 #
463 -class MaskData(ShapeData, ROIDrawingI):
464 465 ## 466 # Constructor for MaskData object. 467 # @param roicoord The ROICoordinate of the object (default: 0,0) 468 # @param bytes The mask data. 469 # @param x The top left x - coordinate of the shape. 470 # @param y The top left y - coordinate of the shape. 471 # @param width The width of the shape. 472 # @param height The height of the shape.
473 - def __init__(self, roicoord = ROICoordinate(), bytes = None, x = 0, y = 0, width = 0, height = 0):
474 ShapeData.__init__(self); 475 self.x = rdouble(x); 476 self.y = rdouble(y); 477 self.width = rdouble(width); 478 self.height = rdouble(height); 479 self.bytesdata = bytes; 480 self.setCoord(roicoord);
481 482 ## 483 # overridden, @See ShapeData#setGeometry 484 #
485 - def setGeometry(self, mask):
486 mask.setTheZ(self.coord.theZ); 487 mask.setTheT(self.coord.theZ); 488 mask.setX(self.x); 489 mask.setY(self.y); 490 mask.setWidth(self.width); 491 mask.setHeight(self.height); 492 mask.setBytes(self.bytedata);
493 494 ## 495 # overridden, @See ShapeData#getGeometryFromROI 496 #
497 - def getGeometryFromROI(self, roi):
498 self.x = roi.getX(); 499 self.y = roi.getY(); 500 self.width = roi.getWidth(); 501 self.height = roi.getHeight(); 502 self.bytesdata = roi.getBytes();
503 504 ## 505 # overridden, @See ShapeData#createBaseType 506 #
507 - def createBaseType(self):
508 return MaskI();
509 510 ## 511 # overridden, @See ShapeData#acceptVisitor 512 #
513 - def acceptVisitor(self, visitor):
514 visitor.drawMask(self.x.getValue(), self.y.getValue(), self.width.getValue(), self.height.getValue(), self.bytesdata, self.shapeSettings.getSettings());
515 516 ## 517 # The PointData class contains all the manipulation and create of PointI 518 # types. 519 # It also accepts the ROIDrawingUtils visitor for drawing points. 520 #
521 -class PointData(ShapeData, ROIDrawingI):
522 523 ## 524 # Constructor for PointData object. 525 # @param roicoord The ROICoordinate of the object (default: 0,0) 526 # @param x The x coordinate of the shape. 527 # @param y The y coordinate of the shape.
528 - def __init__(self, roicoord = ROICoordinate(), x = 0, y = 0):
529 ShapeData.__init__(self); 530 self.x = rdouble(x); 531 self.y = rdouble(y); 532 self.setCoord(roicoord);
533 534 ## 535 # overridden, @See ShapeData#setGeometry 536 #
537 - def setGeometry(self, point):
538 point.setTheZ(self.coord.theZ); 539 point.setTheT(self.coord.theZ); 540 point.setX(self.x); 541 point.setY(self.y);
542 543 ## 544 # overridden, @See ShapeData#getGeometryFromROI 545 #
546 - def getGeometryFromROI(self, roi):
547 self.x = roi.getX(); 548 self.y = roi.getY();
549 550 ## 551 # overridden, @See ShapeData#createBaseType 552 #
553 - def createBaseType(self):
554 return PointI();
555 556 ## 557 # overridden, @See ShapeData#acceptVisitor 558 #
559 - def acceptVisitor(self, visitor):
560 visitor.drawEllipse(self.x.getValue(), self.y.getValue(), 3, 3, self.shapeSettings.getSettings());
561 562 ## 563 # The PolygonData class contains all the manipulation and create of PolygonI 564 # types. 565 # It also accepts the ROIDrawingUtils visitor for drawing polygons. 566 #
567 -class PolygonData(ShapeData, ROIDrawingI):
568 569 ## 570 # Constructor for PolygonData object. 571 # @param roicoord The ROICoordinate of the object (default: 0,0) 572 # @param pointList The list of points that make up the polygon, as pairs [x1, y1, x2, y2 ..].
573 - def __init__(self, roicoord = ROICoordinate(), pointsList = [0,0]):
574 ShapeData.__init__(self); 575 self.points = rstring(self.listToString(pointsList)); 576 self.setCoord(roicoord);
577 578 ## 579 # overridden, @See ShapeData#setGeometry 580 #
581 - def setGeometry(self, polygon):
582 polygon.setTheZ(self.coord.theZ); 583 polygon.setTheT(self.coord.theZ); 584 polygon.setPoints(self.points);
585 586 ## 587 # overridden, @See ShapeData#getGeometryFromROI 588 #
589 - def getGeometryFromROI(self, roi):
590 self.points = roi.getPoints();
591 592 ## 593 # Convert a pointsList[x1,y1,x2,y2..] to a string. 594 # @param pointsList The list of points to convert. 595 # @return The pointsList converted to a string.
596 - def listToString(self, pointsList):
597 string = ''; 598 cnt = 0; 599 for element in pointsList: 600 if(cnt!=0): 601 string = string + ','; 602 cnt += 1; 603 string = string + str(element); 604 return string;
605 606 ## 607 # Convert a string of points to a tuple list [(x1,y1),(x2,y2)..]. 608 # @param pointString The string to convert. 609 # @return The tuple list converted from a string.
610 - def stringToTupleList(self, pointString):
611 elements = []; 612 list = pointString.split(','); 613 numTokens = len(list); 614 for tokenPair in range(0,numTokens/2): 615 elements.append((int(list[tokenPair*2]), int(list[tokenPair*2+1]))); 616 return elements;
617 618 ## 619 # overridden, @See ShapeData#createBaseType 620 #
621 - def createBaseType(self):
622 return PolygonI();
623 624 ## 625 # overridden, @See ShapeData#acceptVisitor 626 #
627 - def acceptVisitor(self, visitor):
628 visitor.drawPolygon(self.stringToTupleList(self.points.getValue()), self.shapeSettings.getSettings());
629 630 ## 631 # The PolylineData class contains all the manipulation and create of PolylineI 632 # types. 633 # It also accepts the ROIDrawingUtils visitor for drawing polylines. 634 #
635 -class PolylineData(ShapeData, ROIDrawingI):
636 637 ## 638 # Constructor for PolylineData object. 639 # @param roicoord The ROICoordinate of the object (default: 0,0) 640 # @param pointList The list of points that make up the polygon, as pairs [x1, y1, x2, y2 ..].
641 - def __init__(self, roicoord = ROICoordinate(), pointsList = [0,0]):
642 ShapeData.__init__(self); 643 self.points = rstring(self.listToString(pointsList)); 644 self.setCoord(roicoord);
645 646 ## 647 # overridden, @See ShapeData#setGeometry 648 #
649 - def setGeometry(self, point):
650 point.setTheZ(self.coord.theZ); 651 point.setTheT(self.coord.theZ); 652 point.setPoints(self.points);
653 654 ## 655 # overridden, @See ShapeData#getGeometryFromROI 656 #
657 - def getGeometryFromROI(self, roi):
658 self.points = roi.getPoints();
659 660 ## 661 # Convert a pointsList[x1,y1,x2,y2..] to a string. 662 # @param pointsList The list of points to convert. 663 # @return The pointsList converted to a string.
664 - def listToString(self, pointsList):
665 string = ''; 666 cnt = 0; 667 for element in pointsList: 668 if(cnt > 0): 669 string = string + ','; 670 string = string + str(element); 671 cnt+=1; 672 return string;
673 674 ## 675 # Convert a string of points to a tuple list [(x1,y1),(x2,y2)..]. 676 # @param pointString The string to convert. 677 # @return The tuple list converted from a string.
678 - def stringToTupleList(self, pointString):
679 elements = []; 680 list = pointString.split(','); 681 numTokens = len(list); 682 for tokenPair in range(0,numTokens/2): 683 elements.append((int(list[tokenPair*2]), int(list[tokenPair*2+1]))); 684 return elements;
685 686 ## 687 # overridden, @See ShapeData#createBaseType 688 #
689 - def createBaseType(self):
690 return PolylineI();
691 692 ## 693 # overridden, @See ShapeData#acceptVisitor 694 #
695 - def acceptVisitor(self, visitor):
696 visitor.drawPolyline(self.stringToTupleList(self.points.getValue()), self.shapeSettings.getSettings());
697