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.
This commit is contained in:
Campbell Barton 2022-07-22 13:05:26 +10:00
parent 72e249974a
commit 08c5d99e88
5 changed files with 26 additions and 24 deletions

View File

@ -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]);

View File

@ -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;

View File

@ -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. */

View File

@ -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];

View File

@ -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);