tensorbay.geometry.polygon

The implementation of the TensorBay polygon.

class tensorbay.geometry.polygon.Polygon(points=None)[source]

Bases: tensorbay.geometry.point_list.PointList2D[tensorbay.geometry.vector.Vector2D]

This class defines the concept of Polygon.

Polygon contains the coordinates of the vertexes of the polygon and provides Polygon.area() to calculate the area of the polygon.

Examples

>>> Polygon([[1, 2], [2, 3], [2, 2]])
Polygon [
  Vector2D(1, 2),
  Vector2D(2, 3),
  Vector2D(2, 2)
]
classmethod loads(contents)[source]

Loads the information of Polygon.

Parameters

contents (Sequence[Mapping[str, float]]) – A list of dictionary lists containing the coordinates of the vertexes of the polygon.

Returns

The loaded Polygon object.

Return type

tensorbay.geometry.polygon._P

Examples

>>> contents = [{"x": 1.0, "y": 1.0}, {"x": 2.0, "y": 2.0}, {"x": 2.0, "y": 3.0}]
>>> Polygon.loads(contents)
Polygon [
  Vector2D(1.0, 1.0),
  Vector2D(2.0, 2.0),
  Vector2D(2.0, 3.0)
]
area()[source]

Return the area of the polygon.

The area is positive if the rotating direction of the points is counterclockwise, and negative if clockwise.

Returns

The area of the polygon.

Return type

float

Examples

>>> polygon = Polygon([[1, 2], [2, 2], [2, 3]])
>>> polygon.area()
0.5
class tensorbay.geometry.polygon.MultiPolygon(polygons)[source]

Bases: tensorbay.geometry.point_list.MultiPointList2D[tensorbay.geometry.polygon.Polygon]

This class defines the concept of MultiPolygon.

MultiPolygon contains a list of polygons.

Parameters

polygons – A list of polygons.

Examples

>>> MultiPolygon([[[1.0, 4.0], [2.0, 3.7], [7.0, 4.0]],
...               [[5.0, 7.0], [6.0, 7.0], [9.0, 8.0]]])
MultiPolygon [
    Polygon [...]
    Polygon [...]
    ...
]
classmethod loads(contents)[source]

Loads a MultiPolygon from the given contents.

Parameters

contents (Sequence[Sequence[Mapping[str, float]]]) – A list of dict lists containing the coordinates of the vertices of the polygon list.

Returns

The loaded MultiPolyline2D object.

Return type

tensorbay.geometry.polygon._P

Examples

>>> contents = [[{'x': 1.0, 'y': 4.0}, {'x': 2.0, 'y': 3.7}, {'x': 7.0, 'y': 4.0}],
...             [{'x': 5.0, 'y': 7.0}, {'x': 6.0, 'y': 7.0}, {'x': 9.0, 'y': 8.0}]]
>>> multipolygon = MultiPolygon.loads(contents)
>>> multipolygon
MultiPolygon [
    Polygon [...]
    Polygon [...]
    ...
]
dumps()[source]

Dumps a MultiPolygon into a polygon list.

Returns

All the information of the MultiPolygon.

Return type

List[List[Dict[str, float]]]

Examples

>>> multipolygon = MultiPolygon([[[1.0, 4.0], [2.0, 3.7], [7.0, 4.0]],
...                             [[5.0, 7.0], [6.0, 7.0], [9.0, 8.0]]])
>>> multipolygon.dumps()
[
    [{'x': 1.0, 'y': 4.0}, {'x': 2.0, 'y': 3.7}, {'x': 7.0, 'y': 4.0}],
    [{'x': 5,0, 'y': 7.0}, {'x': 6.0, 'y': 7.0}, {'x': 9.0, 'y': 8.0}]
]
class tensorbay.geometry.polygon.RLE(rle=None)[source]

Bases: tensorbay.utility.user.UserMutableSequence[int]

This class defines the concept of RLE.

RLE contains an rle format mask.

Parameters

rle – A rle format mask.

Examples

>>> RLE([272, 2, 4, 4, 2, 9])
RLE [
  272,
  2,
  ...
]
classmethod loads(contents)[source]

Loads a :class:RLE` from the given contents.

Parameters

contents (List[int]) – One rle mask.

Returns

The loaded RLE object.

Return type

tensorbay.geometry.polygon.RLE

Examples

>>> contents = [272, 2, 4, 4, 2, 9]
>>> rle = RLE.loads(contents)
>>> rle
RLE [
  272,
  2,
  ...
]
dumps()[source]

Dumps a RLE into one rle mask.

Returns

All the information of the RLE.

Return type

List[int]

Examples

>>> rle = RLE([272, 2, 4, 4, 2, 9])
>>> rle.dumps()
[272, 2, 4, 4, 2, 9]