Skip to content

Trim a detection's polygon when its bounding box is shrunk over it#1783

Open
mattdawkins wants to merge 1 commit into
mainfrom
fix/clip-polygon-to-resized-box
Open

Trim a detection's polygon when its bounding box is shrunk over it#1783
mattdawkins wants to merge 1 commit into
mainfrom
fix/clip-polygon-to-resized-box

Conversation

@mattdawkins

Copy link
Copy Markdown
Member

Problem

Resizing a detection's bounding box leaves its polygon untouched, so shrinking the box past the polygon leaves polygon geometry sticking out beyond the box.

Fix

Box edits now clip any polygon on the detection to the new bounds using a small Sutherland–Hodgman implementation (clipPolygonToBounds in src/utils.ts, no new dependency):

  • Polygons crossing the new box edge are trimmed to fit.
  • A polygon falling entirely outside the new box is removed.
  • Holes are clipped by the same rule; holes left with fewer than 3 points are dropped.
  • Clipping happens against the rounded bounds that setFeature stores, so the trimmed polygon is consistent with the persisted box.
  • Rotated boxes are exempt: their stored bounds are the unrotated envelope, so an axis-aligned clip would cut the wrong region.

Unit tests cover containment, single/multi-edge clipping, slanted-edge intersection points, full removal, and hole handling.

🤖 Generated with Claude Code

Resizing a bounding box previously left the detection's polygon
untouched, so shrinking the box could leave polygon geometry sticking
out beyond it. Box edits now clip any polygon on the detection to the
new bounds (Sutherland-Hodgman against the axis-aligned box), dropping
a polygon entirely when it falls completely outside. Holes are clipped
with the same rule. Rotated boxes are exempt since their stored bounds
are the unrotated envelope.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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