tensorbay.geometry.polygon

Polygon.

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

class tensorbay.geometry.polygon.Polygon(points: Optional[Iterable[Iterable[float]]] = 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: List[Dict[str, float]]) tensorbay.geometry.polygon._P[source]

Loads the information of Polygon.

Parameters

contents – A list of dictionary lists containing the coordinates of the vertexes of the polygon.

Returns

The loaded Polygon object.

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() float[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.

Examples

>>> polygon = Polygon([[1, 2], [2, 2], [2, 3]])
>>> polygon.area()
0.5
class tensorbay.geometry.polygon.MultiPolygon(polygons: Optional[Iterable[Iterable[Iterable[float]]]])[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: List[List[Dict[str, float]]]) tensorbay.geometry.polygon._P[source]

Loads a MultiPolygon from the given contents.

Parameters

contents – A list of dict lists containing the coordinates of the vertices of the polygon list.

Returns

The loaded MultiPolyline2D object.

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() List[List[Dict[str, float]]][source]

Dumps a MultiPolygon into a polygon list.

Returns

All the information of the MultiPolygon.

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: Optional[Iterable[int]])[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: List[int]) tensorbay.geometry.polygon.RLE[source]

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

Parameters

contents – One rle mask.

Returns

The loaded RLE object.

Examples

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

Dumps a RLE into one rle mask.

Returns

All the information of the RLE.

Examples

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