From 1e9c8487ba6a7aa19c3a591b2f2a2a7a619e1203 Mon Sep 17 00:00:00 2001 From: Emmanouil Papadeas <35376950+OverloadedOrama@users.noreply.github.com> Date: Thu, 5 Sep 2024 03:34:30 +0300 Subject: [PATCH] Optimize the lasso & polygon select tools by making them check fewer pixels The time they take to complete now depends on the size of the selection, rather than checking all of the pixels of the entire canvas. --- src/Tools/SelectionTools/Lasso.gd | 12 ++++++------ src/Tools/SelectionTools/PolygonSelect.gd | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/Tools/SelectionTools/Lasso.gd b/src/Tools/SelectionTools/Lasso.gd index 32132a0de..a03dd4c72 100644 --- a/src/Tools/SelectionTools/Lasso.gd +++ b/src/Tools/SelectionTools/Lasso.gd @@ -111,11 +111,12 @@ func apply_selection(_position) -> void: func lasso_selection( selection_map: SelectionMap, previous_selection_map: SelectionMap, points: Array[Vector2i] ) -> void: - var project := Global.current_project var selection_size := selection_map.get_size() + var bounding_rect := Rect2i(points[0], Vector2i.ZERO) for point in points: if point.x < 0 or point.y < 0 or point.x >= selection_size.x or point.y >= selection_size.y: continue + bounding_rect = bounding_rect.expand(point) if _intersect: if previous_selection_map.is_pixel_selected(point): selection_map.select_pixel(point, true) @@ -123,11 +124,10 @@ func lasso_selection( selection_map.select_pixel(point, !_subtract) var v := Vector2i() - var image_size := project.size - for x in image_size.x: - v.x = x - for y in image_size.y: - v.y = y + for x in bounding_rect.size.x: + v.x = x + bounding_rect.position.x + for y in bounding_rect.size.y: + v.y = y + bounding_rect.position.y if Geometry2D.is_point_in_polygon(v, points): if _intersect: if previous_selection_map.is_pixel_selected(v): diff --git a/src/Tools/SelectionTools/PolygonSelect.gd b/src/Tools/SelectionTools/PolygonSelect.gd index fda95d42e..291a8cfd9 100644 --- a/src/Tools/SelectionTools/PolygonSelect.gd +++ b/src/Tools/SelectionTools/PolygonSelect.gd @@ -154,11 +154,12 @@ func _clear() -> void: func lasso_selection( selection_map: SelectionMap, previous_selection_map: SelectionMap, points: Array[Vector2i] ) -> void: - var project := Global.current_project var selection_size := selection_map.get_size() + var bounding_rect := Rect2i(points[0], Vector2i.ZERO) for point in points: if point.x < 0 or point.y < 0 or point.x >= selection_size.x or point.y >= selection_size.y: continue + bounding_rect = bounding_rect.expand(point) if _intersect: if previous_selection_map.is_pixel_selected(point): selection_map.select_pixel(point, true) @@ -166,11 +167,10 @@ func lasso_selection( selection_map.select_pixel(point, !_subtract) var v := Vector2i() - var image_size := project.size - for x in image_size.x: - v.x = x - for y in image_size.y: - v.y = y + for x in bounding_rect.size.x: + v.x = x + bounding_rect.position.x + for y in bounding_rect.size.y: + v.y = y + bounding_rect.position.y if Geometry2D.is_point_in_polygon(v, points): if _intersect: if previous_selection_map.is_pixel_selected(v):