9.15. shape

The class shape represents 2D geometric elements with floating (or subpixel) precision. Any shape is given by a set of base points and an optional transformation matrix. The following shapes are currently defined:

  • Point
  • Line (a direct connection between two points)
  • Rectangle (an rectangle that is mainly defined by the top-left and bottom-right corner point)
  • Square (similar to a rectangle, however the side lengths are equal. It is defined by the center point and the side lengths)
  • Ellipse (an ellipse that is defined by the top-left and bottom-right corner of its outer rectangle)
  • Circle (similar to an ellipse, however the side lengths are equal. It is defined by the center point and the side lengths)
  • Polygon (polygon with n points)

Examples for the creation of shapes are:

point = shape(shape.Point, (0,0))
line = shape(shape.Line, (0,0), (100,50))
rect = shape(shape.Rectangle, (20,20), (70,100)) #top-left, bottom-right
square = shape(shape.Square, (30,-50), 20) #center, side-length
ellipse = shape(shape.Ellipse, (-50,-70), (-20, 0)) #top-left, bottom-right
circle = shape(shape.Circle, (-30, 100), 40) #center, side-length

If the optional transformation matrix (2x3 float64 matrix) is set, the shape can be translated and/or rotated. Please consider, that any rotation is currently not supported in any plot. Rectangles, squares, ellipses or circles are always defined, such that their main axes are parallel to the x- and y-axis. Use the rotation to choose another principal orientation. The base points of the shape are never affected by any transformation matrix. Only the contour points can be requested with the applied coordinate transformation (if desired).

It is possible to obtain a region from any shape with a valid area (points and lines don’t have an area). The region is always a pixel-precise structure. Regions can be combined using union or intersection operators.

Furthermore, a mask dataObject can be obtained from any dataObject using the method createMask() if one or multiple shapes are given.

The demo script demoShapes.py show further examples about the usage of shape objects.

class itom.shape([type, param1, param2, index, name]) → creates a shape object of a specific type.

Bases: object

Depending on the type, the following parameters are allowed:

  • shape.Invalid: -
  • shape.Point: point
  • shape.Line: start-point, end-point
  • shape.Rectangle: top left point, bottom right point
  • shape.Square: center point, side-length
  • shape.Ellipse: top left point, bottom right point of bounding box
  • shape.Circle: center point, radius
  • shape.Polygon : 2xM float64 array with M points of polygon

parameters point, start-point, ... can be all array-like types (e.g. dataObject, list, tuple, np.array) that can be mapped to float64 and have two elements.

During construction, all shapes are aligned with respect to the x- and y-axis. Set a 2d transformation (attribute ‘transform’) to rotate and move it.

contour([applyTrafo = True, tol = -1.0]) → return contour points as a 2xNumPoints float64 dataObject

If a transformation matrix is set, the base points can be transformed if ‘applyTrafo’ is True. For point, line and rectangle based shapes, the contour is directly given. For ellipses and circles, a polygon is approximated to the form of the ellipse and returned as contour. The approximation is done by line segments. Use ‘tol’ to set the maximum distance between each line segment and the real shape. If ‘tol’ is -1.0, ‘tol’ is set to 1% of the smallest diameter.

normalized() -> return a normalized shape (this has only an impact on rectangles, squares, ellipses and circles)

The normalized shape guarantees that the bounding box of the shape never has a non-negative width or height. Therefore, the order or position of the two corner points (base points) is switched or changed, if necessary. Shapes different than rectangle, square, circle or ellipse are not affected by this and are returned as they are.

region() → Return a region object from this shape.

The region object only contains valid regions if the shape has an area > 0. A region object is an integer based object (pixel raster), therefore the shapes are rounded to the nearest fixed-point coordinates.

rotateDeg(array-like object) → Rotate shape by given angle in degree (counterclockwise).
rotateRad(array-like object) → Rotate shape by given angle in radians (counterclockwise).
translate(array-like object) → Translate shape by given (dx,dy) value.
angleDeg

Get/set angle of optional rotation matrix in degree (counter-clockwise).

angleRad

Get/set angle of optional rotation matrix in radians (counter-clockwise).

area

Get area of shape (points and lines have an empty area).

basePoints

Return base points of this shape: M base points of shape as 2xM float64 dataObject

The base points are untransformed points that describe the shape dependent on its type:

  • shape.Point: one point
  • shape.Line : start point, end point
  • shape.Rectangle, shape.Square : top left point, bottom right point
  • shape.Ellipse, shape.Circle : top left point, bottom right point of bounding box
  • shape.Polygon : points of polygon, the last and first point are connected, too.
center

Get/set center point

The center point is the point (type: point), or the center of the line (type: line) or bounding box of a rectangle, square, ellipse or circle.

flags

Get/set bitmask with flags for this shape: Shape.MoveLock, Shape.RotateLock, Shape.ResizeLock

height

Get/set height of shape.

A height can only be set for a square or for a rectangle and is defined with respect to the base coordinate system.

index

Get/set index of shape. The default is -1, however if the shape is a geometric shape of a plot, an auto-incremented index is assigned once the shape is drawn or set. If >= 0 it is possible to modify an existing shape with the same index.

name

Get/set name (label) of the shape.

point1

Get/set first point (of bounding box)

The first point is the first point (type: point or line) or the upper left point of the bounding box (type: rectangle, square, ellipse, circle). The point always considers a possible 2D coordinate transformation matrix.

point2

Get/set second point of bounding box

The second point is the bottom right point of the bounding box (type: rectangle, square, ellipse, circle). The point always considers a possible 2D coordinate transformation matrix.

radius

Get/set radius of shape.

A radius can only be set for a circle (float value) or for an ellipse (a, b) as half side-length in x- and y-direction of the base coordinate system.

transform

Get/set the affine, non scaled 2D transformation matrix (2x3, float64, [2x2 Rot, 2x1 trans])

type

Get type of shape, e.g. Shape.Line, Shape.Point, Shape.Rectangle, Shape.Ellipse, Shape.Circle, Shape.Square

valid

Return True if shape is a valid geometric shape, else False

width

Get/set width of shape.

A width can only be set for a square or for a rectangle and is defined with respect to the base coordinate system.