Skip to content

_overlaps_common

Functions to identify overlaps between regions, not related to table specs.

_is_overlapping_1D_int(line1, line2)

Given two integer intervals, find whether they overlap

This is the same as is_overlapping_1D (based on https://stackoverflow.com/a/70023212/19085332), for integer-valued intervals.

PARAMETER DESCRIPTION
line1

The boundaries of the first interval , written as [x_min, x_max].

TYPE: Sequence[int]

line2

The boundaries of the second interval , written as [x_min, x_max].

TYPE: Sequence[int]

Source code in fractal_tasks_core/roi/_overlaps_common.py
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
def _is_overlapping_1D_int(
    line1: Sequence[int],
    line2: Sequence[int],
) -> bool:
    """
    Given two integer intervals, find whether they overlap

    This is the same as `is_overlapping_1D` (based on
    https://stackoverflow.com/a/70023212/19085332), for integer-valued
    intervals.

    Args:
        line1: The boundaries of the first interval , written as
            `[x_min, x_max]`.
        line2: The boundaries of the second interval , written as
            `[x_min, x_max]`.
    """
    return line1[0] < line2[1] and line2[0] < line1[1]

_is_overlapping_3D_int(box1, box2)

Given two three-dimensional integer boxes, find whether they overlap.

This is the same as is_overlapping_3D (based on https://stackoverflow.com/a/70023212/19085332), for integer-valued boxes.

PARAMETER DESCRIPTION
box1

The boundaries of the first box, written as [x_min, y_min, z_min, x_max, y_max, z_max].

TYPE: list[int]

box2

The boundaries of the second box, written as [x_min, y_min, z_min, x_max, y_max, z_max].

TYPE: list[int]

Source code in fractal_tasks_core/roi/_overlaps_common.py
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
def _is_overlapping_3D_int(box1: list[int], box2: list[int]) -> bool:
    """
    Given two three-dimensional integer boxes, find whether they overlap.

    This is the same as is_overlapping_3D (based on
    https://stackoverflow.com/a/70023212/19085332), for integer-valued
    boxes.

    Args:
        box1: The boundaries of the first box, written as
            `[x_min, y_min, z_min, x_max, y_max, z_max]`.
        box2: The boundaries of the second box, written as
            `[x_min, y_min, z_min, x_max, y_max, z_max]`.
    """
    overlap_x = _is_overlapping_1D_int([box1[0], box1[3]], [box2[0], box2[3]])
    overlap_y = _is_overlapping_1D_int([box1[1], box1[4]], [box2[1], box2[4]])
    overlap_z = _is_overlapping_1D_int([box1[2], box1[5]], [box2[2], box2[5]])
    return overlap_x and overlap_y and overlap_z

is_overlapping_1D(line1, line2, tol=1e-10)

Given two intervals, finds whether they overlap.

This is based on https://stackoverflow.com/a/70023212/19085332, and we additionally use a finite tolerance for floating-point comparisons.

PARAMETER DESCRIPTION
line1

The boundaries of the first interval, written as [x_min, x_max].

TYPE: Sequence[float]

line2

The boundaries of the second interval, written as [x_min, x_max].

TYPE: Sequence[float]

tol

Finite tolerance for floating-point comparisons.

TYPE: float DEFAULT: 1e-10

Source code in fractal_tasks_core/roi/_overlaps_common.py
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
def is_overlapping_1D(
    line1: Sequence[float], line2: Sequence[float], tol: float = 1e-10
) -> bool:
    """
    Given two intervals, finds whether they overlap.

    This is based on https://stackoverflow.com/a/70023212/19085332, and we
    additionally use a finite tolerance for floating-point comparisons.

    Args:
        line1: The boundaries of the first interval, written as
            `[x_min, x_max]`.
        line2: The boundaries of the second interval, written as
            `[x_min, x_max]`.
        tol: Finite tolerance for floating-point comparisons.
    """
    return line1[0] <= line2[1] - tol and line2[0] <= line1[1] - tol

is_overlapping_2D(box1, box2, tol=1e-10)

Given two rectangular boxes, finds whether they overlap.

This is based on https://stackoverflow.com/a/70023212/19085332, and we additionally use a finite tolerance for floating-point comparisons.

PARAMETER DESCRIPTION
box1

The boundaries of the first rectangle, written as [x_min, y_min, x_max, y_max].

TYPE: Sequence[float]

box2

The boundaries of the second rectangle, written as [x_min, y_min, x_max, y_max].

TYPE: Sequence[float]

tol

Finite tolerance for floating-point comparisons.

TYPE: float DEFAULT: 1e-10

Source code in fractal_tasks_core/roi/_overlaps_common.py
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
def is_overlapping_2D(
    box1: Sequence[float], box2: Sequence[float], tol: float = 1e-10
) -> bool:
    """
    Given two rectangular boxes, finds whether they overlap.

    This is based on https://stackoverflow.com/a/70023212/19085332, and we
    additionally use a finite tolerance for floating-point comparisons.

    Args:
        box1: The boundaries of the first rectangle, written as
            `[x_min, y_min, x_max, y_max]`.
        box2: The boundaries of the second rectangle, written as
            `[x_min, y_min, x_max, y_max]`.
        tol: Finite tolerance for floating-point comparisons.
    """
    overlap_x = is_overlapping_1D(
        [box1[0], box1[2]], [box2[0], box2[2]], tol=tol
    )
    overlap_y = is_overlapping_1D(
        [box1[1], box1[3]], [box2[1], box2[3]], tol=tol
    )
    return overlap_x and overlap_y

is_overlapping_3D(box1, box2, tol=1e-10)

Given two three-dimensional boxes, finds whether they overlap.

This is based on https://stackoverflow.com/a/70023212/19085332, and we additionally use a finite tolerance for floating-point comparisons.

PARAMETER DESCRIPTION
box1

The boundaries of the first box, written as [x_min, y_min, z_min, x_max, y_max, z_max].

TYPE: Sequence[float]

box2

The boundaries of the second box, written as [x_min, y_min, z_min, x_max, y_max, z_max].

TYPE: Sequence[float]

tol

Finite tolerance for floating-point comparisons.

TYPE: float DEFAULT: 1e-10

Source code in fractal_tasks_core/roi/_overlaps_common.py
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
def is_overlapping_3D(
    box1: Sequence[float], box2: Sequence[float], tol: float = 1e-10
) -> bool:
    """
    Given two three-dimensional boxes, finds whether they overlap.

    This is based on https://stackoverflow.com/a/70023212/19085332, and we
    additionally use a finite tolerance for floating-point comparisons.

    Args:
        box1: The boundaries of the first box, written as
            `[x_min, y_min, z_min, x_max, y_max, z_max]`.
        box2: The boundaries of the second box, written as
            `[x_min, y_min, z_min, x_max, y_max, z_max]`.
        tol: Finite tolerance for floating-point comparisons.
    """

    overlap_x = is_overlapping_1D(
        [box1[0], box1[3]], [box2[0], box2[3]], tol=tol
    )
    overlap_y = is_overlapping_1D(
        [box1[1], box1[4]], [box2[1], box2[4]], tol=tol
    )
    overlap_z = is_overlapping_1D(
        [box1[2], box1[5]], [box2[2], box2[5]], tol=tol
    )
    return overlap_x and overlap_y and overlap_z