Skip to content

Contour

SUMMARY

An ordered contour in 2D space.

python
from telekinesis import datatypes
contour = datatypes.Contour(points=[[118, 84], [134, 79], [150, 86], [139, 115]])
API Reference
Complete API documentation for Contour, including parameters, attributes, and methods.
View Reference →

Parameters

ParameterTypeDefaultDescription
pointslist[list[int]] | np.ndarrayRequiredOrdered contour vertices with shape (K, 2), with one [x, y] row per vertex. K may be 0.

Raises

ExceptionCondition
TypeErrorpoints can't be converted to an int64 array
ValueErrorpoints, after conversion, isn't 2-D or its second axis isn't length 2

Attributes

AttributeTypeDescription
pointsnp.ndarrayDefensive copy of the contour vertices, shape (K, 2), int64.

Methods

MethodTypeDescription
Contour.coerce(value)ContourConverts array-like point data into a Contour. Accepts a np.ndarray or list of (x, y) points. If value is already a Contour, it is returned unchanged.

Operators

OperationBehavior
contour == otherTrue only if other is a Contour with an equal points array. NotImplemented (so False) for any other type.
len(contour)The vertex count, K.
repr(contour)Contour(num_points=3) — verified empirically.

Visualization

python
import rerun as rr

# Your code block
# ....

rr.init("contour_example", spawn=True)
datatypes.visualize(contour, entity_path="/contour", label="Contour")

Example

python
"""Demonstrates the Telekinesis Contour datatype."""

import time

import numpy as np
import rerun as rr
from loguru import logger

from telekinesis import datatypes

def contour_example():
    """Demonstrate creation, inspection, operations, visualization, and serialization."""

    # ======================= Create ============================================
    contour = datatypes.Contour(
        points=np.array([[118, 84], [134, 79], [150, 86], [139, 115]], dtype=np.int64),
    )
    logger.info(f"Original Contour: {contour}")

    contour_from_list = datatypes.Contour.coerce([[0, 0], [10, 0], [10, 10], [0, 10]])
    logger.info(f"Contour coerced from list: {contour_from_list}")

    # ======================= Inspect ===========================================
    logger.info(f"points={contour.points}")

    # ======================= Operations =========================================
    logger.info(f"Number of points: {len(contour)}")

    shifted_points = contour.points + np.array([10, 10], dtype=np.int64)
    shifted_contour = datatypes.Contour(points=shifted_points)
    logger.info(f"Shifted Contour: {shifted_contour}")

    # ======================= Visualize =========================================
    rr.init("contour_example", spawn=True)
    datatypes.visualize(contour, entity_path="/contour/original")
    datatypes.visualize(shifted_contour, entity_path="/contour/shifted")

    # ======================= Serialize / Deserialize ===========================
    start = time.perf_counter()
    serialized = datatypes.serialize(shifted_contour)
    serialization_ms = (time.perf_counter() - start) * 1000

    start = time.perf_counter()
    deserialized = datatypes.deserialize(serialized)["param_0"]
    deserialization_ms = (time.perf_counter() - start) * 1000

    logger.info(f"Deserialized Contour: {deserialized}")
    logger.info(f"Round-trip successful: {deserialized == shifted_contour}")
    logger.info(f"Serialization time: {serialization_ms:.3f} ms")
    logger.info(f"Deserialization time: {deserialization_ms:.3f} ms")


if __name__ == "__main__":
    contour_example()