Snap - Fix creation of 2D bounding boxes

Previously, objects with 2D bounding boxes outside the view were still being added. This issue is now resolved.
Additionally, implement simple AABB detection to check if the bounding box is within the viewport.
This commit is contained in:
Bruno Perdigão
2025-04-04 20:19:25 -03:00
parent 47e8b978af
commit bb91416c4e
2 changed files with 18 additions and 10 deletions
@@ -899,7 +899,8 @@ class PolylineOperator:
if self.mousemove_count == 2:
self.objs_2d_bbox = []
for obj in self.visible_objs:
self.objs_2d_bbox.append(tool.Raycast.get_on_screen_2d_bounding_boxes(context, obj))
if bbox_2d := tool.Raycast.get_on_screen_2d_bounding_boxes(context, obj):
self.objs_2d_bbox.append(bbox_2d)
if self.mousemove_count > 3:
detected_snaps = tool.Snap.detect_snapping_points(context, event, self.objs_2d_bbox, self.tool_state)
@@ -988,7 +989,8 @@ class PolylineOperator:
self.tool_state.mode = "Mouse"
self.visible_objs = tool.Raycast.get_visible_objects(context)
for obj in self.visible_objs:
self.objs_2d_bbox.append(tool.Raycast.get_on_screen_2d_bounding_boxes(context, obj))
if bbox_2d := tool.Raycast.get_on_screen_2d_bounding_boxes(context, obj):
self.objs_2d_bbox.append(bbox_2d)
detected_snaps = tool.Snap.detect_snapping_points(context, event, self.objs_2d_bbox, self.tool_state)
self.snapping_points = tool.Snap.select_snapping_points(context, event, self.tool_state, detected_snaps)
tool.Polyline.calculate_distance_and_angle(context, self.input_ui, self.tool_state)
+14 -8
View File
@@ -75,18 +75,24 @@ class Raycast(bonsai.core.tool.Raycast):
transposed_bbox.append(coord_2d)
region = context.region
borders = (region.width, region.height)
borders = (0, region.width, 0, region.height)
for i, axis in enumerate(zip(*transposed_bbox)):
min_point = min(axis)
max_point = max(axis)
if min_point == max_point:
min_point = 0
if min_point < borders[i] and max_point > 0:
bbox_2d.extend([min_point, max_point])
else:
return (obj, None)
bbox_2d.extend([min_point, max_point])
if len(bbox_2d) == 0:
return None
# AABB
if (
bbox_2d[0] <= borders[1]
and bbox_2d[1] >= borders[0]
and bbox_2d[2] <= borders[3]
and bbox_2d[3] >= borders[2]
):
return (obj, bbox_2d)
return None
return (obj, bbox_2d)
@classmethod
def intersect_mouse_2d_bounding_box(cls, mouse_pos: tuple[int, int], bbox: list[float, float, float, float]):