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