Skip to content

fix(tables): make calc_bbox_intersection safety_margin actually apply - #99

Open
eeshsaxena wants to merge 1 commit into
Filimoa:mainfrom
eeshsaxena:fix/bbox-intersection-safety-margin
Open

fix(tables): make calc_bbox_intersection safety_margin actually apply#99
eeshsaxena wants to merge 1 commit into
Filimoa:mainfrom
eeshsaxena:fix/bbox-intersection-safety-margin

Conversation

@eeshsaxena

Copy link
Copy Markdown

Problem

calc_bbox_intersection(bbox1, bbox2, safety_margin=5.0) is meant to treat two boxes within safety_margin of each other as intersecting. But the "expanded boxes intersect" guard can never be true, so safety_margin has no effect:

x2_expanded_max = max(bbox1[2], bbox2[2]) + safety_margin
if x2_expanded_max <= max(bbox1[0], bbox2[0]) or ...:   # always False
    return None

max(right edges) + margin <= max(left edges) never holds, because every box's right edge exceeds its left edge (so max(rights) >= max(lefts) and the margin only widens the gap). The other three disjuncts are false for the same reason. The function then falls through and returns the intersection of the un-expanded boxes, so it only ever returns non-None when the original boxes strictly overlap — regardless of safety_margin.

Impact: ml.py builds table cells via calc_bbox_intersection(row.bbox, col.bbox, safety_margin=5). A row/column pair that is within 5px but not strictly overlapping (detection noise at cell edges) returns None, dropping the cell.

The existing test even documents this — two cases were "adjusted to expect None" to fit the broken behavior:

# Test case 2: Intersecting with margin - adjusted to expect None for a point intersection
((10, 10, 20, 20), (20, 20, 30, 30), 5, None),  # Adjusted expectation

Fix

Expand each box by safety_margin, then intersect the expanded boxes — the same tolerance pattern used by Element.overlaps() elsewhere in the codebase. This makes safety_margin take effect while leaving the margin=0 and strictly-overlapping cases unchanged.

The two "adjusted" test cases now assert the correct margin-expanded intersections ((15,15,25,25) and (21,21,25,25)), and I added a far-apart case that stays None to pin the tolerance boundary.

The 'expanded boxes intersect' guard in calc_bbox_intersection could never
be true: e.g. max(right edges) + margin <= max(left edges) never holds
because every box's right edge exceeds its left. So the safety_margin branch
was dead and the function only returned the intersection of the un-expanded
boxes; safety_margin (passed as 5 when building table cells in ml.py) had no
effect, and boxes within the margin but not strictly overlapping returned
None.

Expand each box by safety_margin and intersect the expanded boxes, matching
the tolerance used by Element.overlaps(). Two existing test cases whose
expectations were 'adjusted' to None to fit the broken behavior now assert
the correct margin-expanded intersections, plus a far-apart case that stays
None.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant