Cleanup: simplify sorting during uv packing

Prevent double-sorting during uv packing with bounding-box packing.

Slight speed improvement, slight packing efficiency improvement.
This commit is contained in:
Chris Blackbourn 2023-03-30 11:30:49 +13:00
parent a62f6c8290
commit 9ea6771d10
4 changed files with 14 additions and 7 deletions

View File

@ -34,15 +34,17 @@ typedef struct BoxPack {
* There is no limit to the space boxes may take, only that they will be packed
* tightly into the lower left hand corner (0,0)
*
* \param boxarray: a pre-allocated array of boxes.
* \param box_array: a pre-allocated array of boxes.
* only the 'box->x' and 'box->y' are set, 'box->w' and 'box->h' are used,
* 'box->index' is not used at all, the only reason its there
* is that the box array is sorted by area and programs need to be able
* to have some way of writing the boxes back to the original data.
* \param len: the number of boxes in the array.
* \param sort_boxes: Sort `box_array` before packing.
* \param r_tot_x, r_tot_y: set so you can normalize the data.
*/
void BLI_box_pack_2d(BoxPack *boxarray, unsigned int len, float *r_tot_x, float *r_tot_y);
void BLI_box_pack_2d(
BoxPack *box_array, unsigned int len, bool sort_boxes, float *r_tot_x, float *r_tot_y);
typedef struct FixedSizeBoxPack {
struct FixedSizeBoxPack *next, *prev;

View File

@ -266,7 +266,8 @@ static int vertex_sort(const void *p1, const void *p2, void *vs_ctx_p)
/** \} */
void BLI_box_pack_2d(BoxPack *boxarray, const uint len, float *r_tot_x, float *r_tot_y)
void BLI_box_pack_2d(
BoxPack *boxarray, const uint len, const bool sort_boxes, float *r_tot_x, float *r_tot_y)
{
uint box_index, verts_pack_len, i, j, k;
uint *vertex_pack_indices; /* an array of indices used for sorting verts */
@ -284,8 +285,11 @@ void BLI_box_pack_2d(BoxPack *boxarray, const uint len, float *r_tot_x, float *r
return;
}
/* Sort boxes, biggest first */
qsort(boxarray, (size_t)len, sizeof(BoxPack), box_areasort);
if (sort_boxes) {
/* Sort boxes, biggest first.
* Be careful, qsort is not deterministic! */
qsort(boxarray, (size_t)len, sizeof(BoxPack), box_areasort);
}
/* Add verts to the boxes, these are only used internally. */
vert = MEM_mallocN(sizeof(BoxVert[4]) * (size_t)len, "BoxPack Verts");

View File

@ -821,7 +821,7 @@ static float pack_islands_scale_margin(const Span<PackIsland *> islands,
&max_v);
break;
default:
BLI_box_pack_2d(box_array, int(max_box_pack), &max_u, &max_v);
BLI_box_pack_2d(box_array, int(max_box_pack), false, &max_u, &max_v);
break;
}

View File

@ -1404,8 +1404,9 @@ static PyObject *M_Geometry_box_pack_2d(PyObject *UNUSED(self), PyObject *boxlis
return NULL; /* exception set */
}
const bool sort_boxes = true; /* Caution: BLI_box_pack_2d sorting is non-deterministic. */
/* Non Python function */
BLI_box_pack_2d(boxarray, len, &tot_width, &tot_height);
BLI_box_pack_2d(boxarray, len, sort_boxes, &tot_width, &tot_height);
boxPack_ToPyObject(boxlist, boxarray);
MEM_freeN(boxarray);