[Bf-blender-cvs] [08c5d99e88e] master: Cleanup: add BKE_image_find_nearest_tile_with_offset

Campbell Barton noreply at git.blender.org
Fri Jul 22 05:08:06 CEST 2022


Commit: 08c5d99e88ee3e9f807dfe69c188660eae347f31
Author: Campbell Barton
Date:   Fri Jul 22 13:05:26 2022 +1000
Branches: master
https://developer.blender.org/rB08c5d99e88ee3e9f807dfe69c188660eae347f31

Cleanup: add BKE_image_find_nearest_tile_with_offset

Every caller BKE_image_find_nearest_tile was calculating the tile offset
so add a version of this function that returns the offset too.

===================================================================

M	source/blender/blenkernel/BKE_image.h
M	source/blender/blenkernel/intern/image.cc
M	source/blender/editors/transform/transform_mode_resize.c
M	source/blender/editors/transform/transform_mode_translate.c
M	source/blender/editors/uvedit/uvedit_islands.c

===================================================================

diff --git a/source/blender/blenkernel/BKE_image.h b/source/blender/blenkernel/BKE_image.h
index 4e622a5708f..e3c249e56f9 100644
--- a/source/blender/blenkernel/BKE_image.h
+++ b/source/blender/blenkernel/BKE_image.h
@@ -6,6 +6,7 @@
  * \ingroup bke
  */
 
+#include "BLI_compiler_attrs.h"
 #include "BLI_utildefines.h"
 
 #include "BLI_rect.h"
@@ -424,7 +425,11 @@ void BKE_image_get_tile_uv(const struct Image *ima, const int tile_number, float
 /**
  * Return the tile_number for the closest UDIM tile.
  */
-int BKE_image_find_nearest_tile(const struct Image *image, const float co[2]);
+int BKE_image_find_nearest_tile_with_offset(const struct Image *image,
+                                            const float co[2],
+                                            float r_uv_offset[2]) ATTR_NONNULL(1, 2, 3);
+int BKE_image_find_nearest_tile(const struct Image *image, const float co[2])
+    ATTR_NONNULL(1, 2) ATTR_WARN_UNUSED_RESULT;
 
 void BKE_image_get_size(struct Image *image, struct ImageUser *iuser, int *r_width, int *r_height);
 void BKE_image_get_size_fl(struct Image *image, struct ImageUser *iuser, float r_size[2]);
diff --git a/source/blender/blenkernel/intern/image.cc b/source/blender/blenkernel/intern/image.cc
index f8b2d841028..7fa3a5ad14d 100644
--- a/source/blender/blenkernel/intern/image.cc
+++ b/source/blender/blenkernel/intern/image.cc
@@ -863,11 +863,14 @@ void BKE_image_get_tile_uv(const Image *ima, const int tile_number, float r_uv[2
   }
 }
 
-int BKE_image_find_nearest_tile(const Image *image, const float co[2])
+int BKE_image_find_nearest_tile_with_offset(const Image *image,
+                                            const float co[2],
+                                            float r_uv_offset[2])
 {
   const float co_floor[2] = {floorf(co[0]), floorf(co[1])};
   /* Distance to the closest UDIM tile. */
   float dist_best_sq = FLT_MAX;
+  float uv_offset_best[2] = {0, 0};
   int tile_number_best = -1;
 
   LISTBASE_FOREACH (const ImageTile *, tile, &image->tiles) {
@@ -875,6 +878,7 @@ int BKE_image_find_nearest_tile(const Image *image, const float co[2])
     BKE_image_get_tile_uv(image, tile->tile_number, uv_offset);
 
     if (equals_v2v2(co_floor, uv_offset)) {
+      copy_v2_v2(r_uv_offset, uv_offset);
       return tile->tile_number;
     }
 
@@ -884,12 +888,21 @@ int BKE_image_find_nearest_tile(const Image *image, const float co[2])
     if (dist_sq < dist_best_sq) {
       dist_best_sq = dist_sq;
       tile_number_best = tile->tile_number;
+      copy_v2_v2(uv_offset_best, uv_offset);
     }
   }
-
+  if (tile_number_best != -1) {
+    copy_v2_v2(r_uv_offset, uv_offset_best);
+  }
   return tile_number_best;
 }
 
+int BKE_image_find_nearest_tile(const struct Image *image, const float co[2])
+{
+  float uv_offset_dummy[2];
+  return BKE_image_find_nearest_tile_with_offset(image, co, uv_offset_dummy);
+}
+
 static void image_init_color_management(Image *ima)
 {
   ImBuf *ibuf;
diff --git a/source/blender/editors/transform/transform_mode_resize.c b/source/blender/editors/transform/transform_mode_resize.c
index bbe1cfdf521..1ccda96fecb 100644
--- a/source/blender/editors/transform/transform_mode_resize.c
+++ b/source/blender/editors/transform/transform_mode_resize.c
@@ -137,13 +137,7 @@ static bool clip_uv_transform_resize(TransInfo *t, float vec[2])
 
   /* If tiled image then constrain to correct/closest UDIM tile, else 0-1 UV space. */
   if (is_tiled_image) {
-    int nearest_tile_index = BKE_image_find_nearest_tile(image, t->center_global);
-    if (nearest_tile_index != -1) {
-      nearest_tile_index -= 1001;
-      /* Getting coordinates of nearest tile from the tile index. */
-      base_offset[0] = nearest_tile_index % 10;
-      base_offset[1] = nearest_tile_index / 10;
-    }
+    BKE_image_find_nearest_tile_with_offset(image, t->center_global, base_offset);
   }
 
   /* Assume no change is required. */
diff --git a/source/blender/editors/transform/transform_mode_translate.c b/source/blender/editors/transform/transform_mode_translate.c
index 67bdeb3fed0..04a41814b53 100644
--- a/source/blender/editors/transform/transform_mode_translate.c
+++ b/source/blender/editors/transform/transform_mode_translate.c
@@ -448,13 +448,7 @@ static bool clip_uv_transform_translation(TransInfo *t, float vec[2])
 
   /* If tiled image then constrain to correct/closest UDIM tile, else 0-1 UV space. */
   if (is_tiled_image) {
-    int nearest_tile_index = BKE_image_find_nearest_tile(image, t->center_global);
-    if (nearest_tile_index != -1) {
-      nearest_tile_index -= 1001;
-      /* Getting coordinates of nearest tile from the tile index. */
-      base_offset[0] = nearest_tile_index % 10;
-      base_offset[1] = nearest_tile_index / 10;
-    }
+    BKE_image_find_nearest_tile_with_offset(image, t->center_global, base_offset);
   }
 
   float min[2], max[2];
diff --git a/source/blender/editors/uvedit/uvedit_islands.c b/source/blender/editors/uvedit/uvedit_islands.c
index e1752ae5a29..9a31fd6469d 100644
--- a/source/blender/editors/uvedit/uvedit_islands.c
+++ b/source/blender/editors/uvedit/uvedit_islands.c
@@ -256,16 +256,12 @@ bool uv_coords_isect_udim(const Image *image, const int udim_grid[2], const floa
  * Calculates distance to nearest UDIM image tile in UV space and its UDIM tile number.
  */
 static float uv_nearest_image_tile_distance(const Image *image,
-                                            float coords[2],
+                                            const float coords[2],
                                             float nearest_tile_co[2])
 {
-  int nearest_image_tile_index = BKE_image_find_nearest_tile(image, coords);
-  if (nearest_image_tile_index == -1) {
-    nearest_image_tile_index = 1001;
+  if (BKE_image_find_nearest_tile_with_offset(image, coords, nearest_tile_co) == -1) {
+    zero_v2(nearest_tile_co);
   }
-
-  nearest_tile_co[0] = (nearest_image_tile_index - 1001) % 10;
-  nearest_tile_co[1] = (nearest_image_tile_index - 1001) / 10;
   /* Add 0.5 to get tile center coordinates. */
   float nearest_tile_center_co[2] = {nearest_tile_co[0], nearest_tile_co[1]};
   add_v2_fl(nearest_tile_center_co, 0.5f);



More information about the Bf-blender-cvs mailing list