Skip to content

Box

Bases: Polygon

A Box is a polygon that is constrained to 4 unique points.

Note that this does not need to be axis-aligned.

Parameters:

Name Type Description Default
value List[List[Tuple[float, float]]]

An polygon value representing a box.

required

Attributes:

Name Type Description
area
polygon
boundary
holes
xmin
xmax
ymin
ymax

Examples:

>>> Box([[(0,0), (0,1), (1,1), (1,0), (0,0)]])

Create a Box using extrema.

>>> Box.from_extrema(
...     xmin=0, xmax=1,
...     ymin=0, ymax=1,
... )
Source code in valor/schemas/symbolic/types.py
class Box(Polygon):
    """
    A Box is a polygon that is constrained to 4 unique points.

    Note that this does not need to be axis-aligned.

    Parameters
    ----------
    value : List[List[Tuple[float, float]]], optional
        An polygon value representing a box.

    Attributes
    ----------
    area
    polygon
    boundary
    holes
    xmin
    xmax
    ymin
    ymax

    Examples
    --------
    >>> Box([[(0,0), (0,1), (1,1), (1,0), (0,0)]])

    Create a Box using extrema.
    >>> Box.from_extrema(
    ...     xmin=0, xmax=1,
    ...     ymin=0, ymax=1,
    ... )
    """

    def __init__(
        self, value: typing.List[typing.List[typing.Tuple[float, float]]]
    ):
        super().__init__(value=value)

    @classmethod
    def __validate__(cls, value: typing.Any):
        """
        Validates typing.

        Parameters
        ----------
        value : typing.Any
            The value to validate.

        Raises
        ------
        TypeError
            If the value type is not supported.
        """
        Polygon.__validate__(value)
        if len(value) != 1:
            raise ValueError("Box should not contain holes.")
        elif len(value[0]) != 5:
            raise ValueError("Box should consist of four unique points.")

    @classmethod
    def decode_value(
        cls,
        value: typing.Optional[typing.List[typing.List[typing.List[float]]]],
    ):
        """Decode object from JSON compatible dictionary."""
        if value is None:
            return None
        return super().decode_value(value)

    @classmethod
    def from_extrema(
        cls,
        xmin: float,
        xmax: float,
        ymin: float,
        ymax: float,
    ):
        """
        Create a Box from extrema values.

        Parameters
        ----------
        xmin : float
            Minimum x-coordinate of the bounding box.
        xmax : float
            Maximum x-coordinate of the bounding box.
        ymin : float
            Minimum y-coordinate of the bounding box.
        ymax : float
            Maximum y-coordinate of the bounding box.

        Returns
        -------
        Box
            A Box created from the provided extrema values.
        """
        points = [
            [
                (xmin, ymin),
                (xmax, ymin),
                (xmax, ymax),
                (xmin, ymax),
                (xmin, ymin),
            ]
        ]
        return cls(value=points)

    def to_polygon(self) -> Polygon:
        """
        Converts box to a generic polygon.

        Returns
        -------
        Polygon
            The box as a Polygon.
        """
        return Polygon(self.get_value())

Functions

valor.schemas.Box.__validate__(value) classmethod

Validates typing.

Parameters:

Name Type Description Default
value Any

The value to validate.

required

Raises:

Type Description
TypeError

If the value type is not supported.

Source code in valor/schemas/symbolic/types.py
@classmethod
def __validate__(cls, value: typing.Any):
    """
    Validates typing.

    Parameters
    ----------
    value : typing.Any
        The value to validate.

    Raises
    ------
    TypeError
        If the value type is not supported.
    """
    Polygon.__validate__(value)
    if len(value) != 1:
        raise ValueError("Box should not contain holes.")
    elif len(value[0]) != 5:
        raise ValueError("Box should consist of four unique points.")

valor.schemas.Box.decode_value(value) classmethod

Decode object from JSON compatible dictionary.

Source code in valor/schemas/symbolic/types.py
@classmethod
def decode_value(
    cls,
    value: typing.Optional[typing.List[typing.List[typing.List[float]]]],
):
    """Decode object from JSON compatible dictionary."""
    if value is None:
        return None
    return super().decode_value(value)

valor.schemas.Box.from_extrema(xmin, xmax, ymin, ymax) classmethod

Create a Box from extrema values.

Parameters:

Name Type Description Default
xmin float

Minimum x-coordinate of the bounding box.

required
xmax float

Maximum x-coordinate of the bounding box.

required
ymin float

Minimum y-coordinate of the bounding box.

required
ymax float

Maximum y-coordinate of the bounding box.

required

Returns:

Type Description
Box

A Box created from the provided extrema values.

Source code in valor/schemas/symbolic/types.py
@classmethod
def from_extrema(
    cls,
    xmin: float,
    xmax: float,
    ymin: float,
    ymax: float,
):
    """
    Create a Box from extrema values.

    Parameters
    ----------
    xmin : float
        Minimum x-coordinate of the bounding box.
    xmax : float
        Maximum x-coordinate of the bounding box.
    ymin : float
        Minimum y-coordinate of the bounding box.
    ymax : float
        Maximum y-coordinate of the bounding box.

    Returns
    -------
    Box
        A Box created from the provided extrema values.
    """
    points = [
        [
            (xmin, ymin),
            (xmax, ymin),
            (xmax, ymax),
            (xmin, ymax),
            (xmin, ymin),
        ]
    ]
    return cls(value=points)

valor.schemas.Box.to_polygon()

Converts box to a generic polygon.

Returns:

Type Description
Polygon

The box as a Polygon.

Source code in valor/schemas/symbolic/types.py
def to_polygon(self) -> Polygon:
    """
    Converts box to a generic polygon.

    Returns
    -------
    Polygon
        The box as a Polygon.
    """
    return Polygon(self.get_value())