Cuboid

class Cuboid(points, faces, sly_id=None, class_id=None, labeler_login=None, updated_at=None, created_at=None)[source]

Bases: Geometry

3D cuboid defined by eight corner points and six faces. Immutable.

Cuboid geometry for a single Label. Cuboid class object is immutable.

Parameters:
points

List or tuple of PointLocation objects.

faces

List or tuple of CuboidFace objects.

sly_id : int, optional

Cuboid ID in Supervisely server.

class_id : int, optional

ID for ObjClass to which belongs Cuboid.

labeler_login : str, optional

Login of the user who created Cuboid.

updated_at : str, optional

Date and Time when Cuboid was modified last. Date Format: Year:Month:Day:Hour:Minute:Seconds. Example: ‘2021-01-22T19:37:50.158Z’.

created_at : str, optional

Date and Time when Cuboid was created. Date Format is the same as in “updated_at” parameter.

Raises:
  • ValueError – if len(CuboidFace objects) != 3. A cuboid must have exactly 3 faces. Instead got {len(faces)} faces.

  • ValueError – if point index is out of bounds for cuboid face. Got {len(points)!r} points, but the index is {point_idx!r}.

Usage Example:
import supervisely as sly
from supervisely.geometry.cuboid import CuboidFace

nodes = [[277, 273], [840, 273], [840, 690], [277, 690], [688, 168], [1200, 168], [1200, 522]]
edges = [CuboidFace(0, 1, 2, 3), CuboidFace(0, 4, 5, 1), CuboidFace(1, 5, 6, 2)]
pl_nodes = (sly.PointLocation(node[0], node[1]) for node in nodes)
figure = sly.Cuboid(pl_nodes, edges)

Methods

allowed_transforms

Returns the allowed transforms for the Geometry.

clone

Clone from GEOMETRYYY

config_from_json

Convert geometry config from json format

config_to_json

Convert geometry config to json format

convert

Convert geometry to another geometry shape.

crop

Crops current Cuboid.

draw

draw_contour

Draws the figure contour on a given bitmap canvas :param bitmap: np.ndarray :param color: [R, G, B] :param thickness: (int) :param config: drawing config specific to a concrete subclass, e.g. per edge colors.

fliplr

Flips current Cuboid in horizontal.

flipud

Flips current Cuboid in vertical.

from_json

Convert a json dict to Cuboid.

geometry_name

Returns the name of the geometry.

get_mask

Returns 2D boolean mask of the geometry.

name

Get the name of the geometry.

relative_crop

Crops object like "crop" method, but return results with coordinates relative to rect :param rect: Rectangle for crop.

resize

Resize current Cuboid.

rotate

Rotates current Cuboid.

scale

Scale current Cuboid.

to_bbox

Create Rectangle object from current Cuboid.

to_json

Convert the Cuboid to a json dict.

translate

Translate current Cuboid.

validate

Validate geometry.

Attributes

area

Cuboid area.

faces

List of CuboidFace objects.

points

List of PointLocation objects.

classmethod allowed_transforms()

Returns the allowed transforms for the Geometry.

classmethod from_json(data)[source]

Convert a json dict to Cuboid. Read more about Supervisely format.

Parameters:
data : Dict[str, List[int]]

Cuboid in json format as a dict.

Returns:

Cuboid from json.

Return type:

Cuboid

Raises:

ValueError – if json format is not correct

Usage Example:
import supervisely as sly

data = {
    "faces": [
                [0, 1, 2, 3],
                [0, 4, 5, 1],
                [1, 5, 6, 2]
    ],
    "points": [
                [273, 277],
                [273, 840],
                [690, 840],
                [690, 277],
                [168, 688],
                [168, 1200],
                [522, 1200]
    ]
}

figure = sly.Cuboid.from_json(data)
classmethod name()

Get the name of the geometry.

Same as geometry_name(), but shorter. In order to make the code more concise.

Returns:

string with name of geometry

static config_from_json(config)

Convert geometry config from json format

Parameters:
config : dict

dictionary(geometry config) in json format

Returns:

dictionary(geometry config) in json format

Return type:

dict

static config_to_json(config)

Convert geometry config to json format

Parameters:
config : dict

dictionary(geometry config)

Returns:

dictionary(geometry config) in json format

Return type:

dict

static geometry_name()[source]

Returns the name of the geometry.

Returns:

name of the geometry

Return type:

str

clone()

Clone from GEOMETRYYY

convert(new_geometry, contour_radius=0, approx_epsilon=None)

Convert geometry to another geometry shape.

Parameters:
new_geometry

New geometry shape.

contour_radius : int

Radius of the contour.

approx_epsilon : float

Approximation epsilon.

Returns:

List of geometries.

Return type:

List[Geometry]

crop(rect)[source]

Crops current Cuboid.

Parameters:
rect

Rectangle for crop.

Returns:

List of Cuboids after crop.

Return type:

List[Cuboid]

Usage Example:
import supervisely as sly
from supervisely.geometry.cuboid import CuboidFace

nodes = [[277, 273], [840, 273], [840, 690], [277, 690], [688, 168], [1200, 168], [1200, 522]]
edges = [CuboidFace(0, 1, 2, 3), CuboidFace(0, 4, 5, 1), CuboidFace(1, 5, 6, 2)]
pl_nodes = (sly.PointLocation(node[0], node[1]) for node in nodes)
figure = sly.Cuboid(pl_nodes, edges)
crop_figures = figure.crop(sly.Rectangle(1, 1, 1500, 1550))
draw(bitmap, color, thickness=1, config=None)
Parameters:
bitmap

np.ndarray

color

Color [R, G, B]

thickness=1

used only in Polyline and Point

config : dict

Drawing config specific to a concrete subclass, e.g. per edge colors

Raises:

NotImplementedError – if method is not implemented in subclass

draw_contour(bitmap, color, thickness=1, config=None)

Draws the figure contour on a given bitmap canvas :param bitmap: np.ndarray :param color: [R, G, B] :param thickness: (int) :param config: drawing config specific to a concrete subclass, e.g. per edge colors

fliplr(img_size)[source]

Flips current Cuboid in horizontal.

Parameters:
img_size : Tuple[int, int]

Image size (height, width) to which belongs Cuboid object.

Returns:

Cuboid after flip in horizontal.

Return type:

Cuboid

Usage Example:
import supervisely as sly
from supervisely.geometry.cuboid import CuboidFace

nodes = [[277, 273], [840, 273], [840, 690], [277, 690], [688, 168], [1200, 168], [1200, 522]]
edges = [CuboidFace(0, 1, 2, 3), CuboidFace(0, 4, 5, 1), CuboidFace(1, 5, 6, 2)]
pl_nodes = (sly.PointLocation(node[0], node[1]) for node in nodes)
figure = sly.Cuboid(pl_nodes, edges)

# Remember that Cuboid class object is immutable, and we need to assign new instance of Cuboid to a new variable
height, width = 1250, 1400
fliplr_figure = figure.fliplr((height, width))
flipud(img_size)[source]

Flips current Cuboid in vertical.

Parameters:
img_size : Tuple[int, int]

Image size (height, width) to which belongs Cuboid object.

Returns:

Cuboid after flip in vertical.

Return type:

Cuboid

Usage Example:
import supervisely as sly
from supervisely.geometry.cuboid import CuboidFace

nodes = [[277, 273], [840, 273], [840, 690], [277, 690], [688, 168], [1200, 168], [1200, 522]]
edges = [CuboidFace(0, 1, 2, 3), CuboidFace(0, 4, 5, 1), CuboidFace(1, 5, 6, 2)]
pl_nodes = (sly.PointLocation(node[0], node[1]) for node in nodes)
figure = sly.Cuboid(pl_nodes, edges)

# Remember that Cuboid class object is immutable, and we need to assign new instance of Cuboid to a new variable
height, width = 1250, 1400
flipud_figure = figure.flipud((height, width))
get_mask(img_size)

Returns 2D boolean mask of the geometry. With shape as img_size (height, width) and filled with True values inside the geometry and False values outside. dtype = np.bool shape = img_size

Parameters:
img_size : Tuple[int, int]

size of the image (height, width)

Returns:

2D boolean mask of the geometry

Return type:

np.ndarray

relative_crop(rect)

Crops object like “crop” method, but return results with coordinates relative to rect :param rect: Rectangle for crop. :type rect: Rectangle :returns: List of Geometry after relative crop. :rtype: List[Geometry]` :raises NotImplementedError: if method is not implemented in subclass

resize(in_size, out_size)[source]

Resize current Cuboid.

Parameters:
in_size : Tuple[int, int]

Input image size (height, width) to which belongs Cuboid object.

out_size : Tuple[int, int]

Desired output image size (height, width) to which belongs Cuboid object.

Returns:

Cuboid after resize.

Return type:

Cuboid

Usage Example:
import supervisely as sly
from supervisely.geometry.cuboid import CuboidFace

nodes = [[277, 273], [840, 273], [840, 690], [277, 690], [688, 168], [1200, 168], [1200, 522]]
edges = [CuboidFace(0, 1, 2, 3), CuboidFace(0, 4, 5, 1), CuboidFace(1, 5, 6, 2)]
pl_nodes = (sly.PointLocation(node[0], node[1]) for node in nodes)
figure = sly.Cuboid(pl_nodes, edges)

# Remember that Cuboid class object is immutable, and we need to assign new instance of Cuboid to a new variable
in_height, in_width = 1250, 1400
out_height, out_width = 600, 800
resize_figure = figure.resize((in_height, in_width), (out_height, out_width))
rotate(rotator)[source]

Rotates current Cuboid.

Parameters:
rotator

Class for image rotation.

Returns:

Cuboid after rotation.

Return type:

Cuboid

Usage Example:
import supervisely as sly
from supervisely.geometry.cuboid import CuboidFace
from supervisely.geometry.image_rotator import ImageRotator

nodes = [[277, 273], [840, 273], [840, 690], [277, 690], [688, 168], [1200, 168], [1200, 522]]
edges = [CuboidFace(0, 1, 2, 3), CuboidFace(0, 4, 5, 1), CuboidFace(1, 5, 6, 2)]
pl_nodes = (sly.PointLocation(node[0], node[1]) for node in nodes)
figure = sly.Cuboid(pl_nodes, edges)

# Remember that Cuboid class object is immutable, and we need to assign new instance of Cuboid to a new variable
rotator = ImageRotator((300, 400), 25)
rotate_fugure= figure.rotate(rotator)
scale(factor)[source]

Scale current Cuboid.

Parameters:
factor : float

Scale parameter.

Returns:

Cuboid after scale.

Return type:

Cuboid

Usage Example:
import supervisely as sly
from supervisely.geometry.cuboid import CuboidFace

nodes = [[277, 273], [840, 273], [840, 690], [277, 690], [688, 168], [1200, 168], [1200, 522]]
edges = [CuboidFace(0, 1, 2, 3), CuboidFace(0, 4, 5, 1), CuboidFace(1, 5, 6, 2)]
pl_nodes = (sly.PointLocation(node[0], node[1]) for node in nodes)
figure = sly.Cuboid(pl_nodes, edges)

# Remember that Cuboid class object is immutable, and we need to assign new instance of Cuboid to a new variable
scale_figure = figure.scale(1.75)
to_bbox()[source]

Create Rectangle object from current Cuboid.

Returns:

Rectangle from cuboid.

Return type:

Rectangle

Usage Example:
import supervisely as sly
from supervisely.geometry.cuboid import CuboidFace

nodes = [[277, 273], [840, 273], [840, 690], [277, 690], [688, 168], [1200, 168], [1200, 522]]
edges = [CuboidFace(0, 1, 2, 3), CuboidFace(0, 4, 5, 1), CuboidFace(1, 5, 6, 2)]
pl_nodes = (sly.PointLocation(node[0], node[1]) for node in nodes)
figure = sly.Cuboid(pl_nodes, edges)

rectangle = figure.to_bbox()
to_json()[source]

Convert the Cuboid to a json dict. Read more about Supervisely format.

Returns:

Json format as a dict

Return type:

dict

Usage Example:
import supervisely as sly
from supervisely.geometry.cuboid import CuboidFace

nodes = [[277, 273], [840, 273], [840, 690], [277, 690], [688, 168], [1200, 168], [1200, 522]]
edges = [CuboidFace(0, 1, 2, 3), CuboidFace(0, 4, 5, 1), CuboidFace(1, 5, 6, 2)]
pl_nodes = (sly.PointLocation(node[0], node[1]) for node in nodes)
figure = sly.Cuboid(pl_nodes, edges)

figure_json = figure.to_json()
print(figure_json)
# Output: {
#    "faces": [
#        [0, 1, 2, 3],
#        [0, 4, 5, 1],
#        [1, 5, 6, 2]
#    ],
#    "points": [
#        [273, 277],
#        [273, 840],
#        [690, 840],
#        [690, 277],
#        [168, 688],
#        [168, 1200],
#        [522, 1200]
#    ]
# }
translate(drow, dcol)[source]

Translate current Cuboid.

Parameters:
drow : int

Horizontal shift.

dcol : int

Vertical shift.

Returns:

Cuboid after translate.

Return type:

Cuboid

Usage Example:
import supervisely as sly
from supervisely.geometry.cuboid import CuboidFace

nodes = [[277, 273], [840, 273], [840, 690], [277, 690], [688, 168], [1200, 168], [1200, 522]]
edges = [CuboidFace(0, 1, 2, 3), CuboidFace(0, 4, 5, 1), CuboidFace(1, 5, 6, 2)]
pl_nodes = (sly.PointLocation(node[0], node[1]) for node in nodes)
figure = sly.Cuboid(pl_nodes, edges)

# Remember that Cuboid class object is immutable, and we need to assign new instance of Cuboid to a new variable
translate_figure = figure.translate(150, 350)
validate(obj_class_shape, settings)

Validate geometry.

Parameters:
obj_class_shape : str

Object class shape.

settings : dict

Settings.

Raises:

ValueError – if geometry validation error

property area : float

Cuboid area.

Returns:

Area of current Cuboid.

Return type:

float

Usage Example:
import supervisely as sly
from supervisely.geometry.cuboid import CuboidFace

nodes = [[277, 273], [840, 273], [840, 690], [277, 690], [688, 168], [1200, 168], [1200, 522]]
edges = [CuboidFace(0, 1, 2, 3), CuboidFace(0, 4, 5, 1), CuboidFace(1, 5, 6, 2)]
pl_nodes = (sly.PointLocation(node[0], node[1]) for node in nodes)
figure = sly.Cuboid(pl_nodes, edges)

print(figure.area)
# Output: 5146.0
property faces : list[supervisely.geometry.cuboid.CuboidFace]

List of CuboidFace objects.

Returns:

Cuboid edges

Return type:

List[CuboidFace]

property points : list[supervisely.geometry.point_location.PointLocation]

List of PointLocation objects.

Returns:

Cuboid nodes

Return type:

List[PointLocation]