Cleanup: refactoring uvislands to prepare for python api

Add element_map->island_total_uvs.
Add element_map->island_total_unique_uvs.
Simplify callers based on new members.
Add comments.

Resolves: D15598
This commit is contained in:
Chris Blackbourn 2022-08-11 14:18:31 +12:00
parent d52d71b834
commit f35d671f46
7 changed files with 54 additions and 65 deletions

View File

@ -68,16 +68,20 @@ typedef struct UvElementMap {
/** Total number of unique UVs. */
int total_unique_uvs;
/* If Non-NULL, address UvElements by `BM_elem_index_get(BMVert*)`. */
/** If Non-NULL, address UvElements by `BM_elem_index_get(BMVert*)`. */
struct UvElement **vertex;
/* If Non-NULL, pointer to local head of each unique UV. */
/** If Non-NULL, pointer to local head of each unique UV. */
struct UvElement **head_table;
/* Number of Islands in the mesh */
int totalIslands;
/* Stores the starting index in buf where each island begins */
int *islandIndices;
/** Number of islands, or zero if not calculated. */
int total_islands;
/** Array of starting index in #storage where each island begins. */
int *island_indices;
/** Array of number of UVs in each island. */
int *island_total_uvs;
/** Array of number of unique UVs in each island. */
int *island_total_unique_uvs;
} UvElementMap;
/* Connectivity data */

View File

@ -820,28 +820,32 @@ static void bm_uv_build_islands(UvElementMap *element_map,
}
}
element_map->islandIndices = MEM_callocN(sizeof(*element_map->islandIndices) * nislands,
"UvElementMap_island_indices");
element_map->island_indices = MEM_callocN(sizeof(*element_map->island_indices) * nislands,
__func__);
element_map->island_total_uvs = MEM_callocN(sizeof(*element_map->island_total_uvs) * nislands,
__func__);
element_map->island_total_unique_uvs = MEM_callocN(
sizeof(*element_map->island_total_unique_uvs) * nislands, __func__);
int j = 0;
for (int i = 0; i < totuv; i++) {
UvElement *element = element_map->storage[i].next;
if (element == NULL) {
islandbuf[map[i]].next = NULL;
}
else {
islandbuf[map[i]].next = &islandbuf[map[element - element_map->storage]];
}
UvElement *next = element_map->storage[i].next;
islandbuf[map[i]].next = next ? &islandbuf[map[next - element_map->storage]] : NULL;
if (islandbuf[i].island != j) {
j++;
element_map->islandIndices[j] = i;
element_map->island_indices[j] = i;
}
BLI_assert(islandbuf[i].island == j);
element_map->island_total_uvs[j]++;
if (islandbuf[i].separate) {
element_map->island_total_unique_uvs[j]++;
}
}
MEM_SAFE_FREE(element_map->storage);
element_map->storage = islandbuf;
islandbuf = NULL;
element_map->totalIslands = nislands;
element_map->total_islands = nislands;
MEM_SAFE_FREE(stack);
MEM_SAFE_FREE(map);
}
@ -1059,7 +1063,9 @@ void BM_uv_element_map_free(UvElementMap *element_map)
MEM_SAFE_FREE(element_map->storage);
MEM_SAFE_FREE(element_map->vertex);
MEM_SAFE_FREE(element_map->head_table);
MEM_SAFE_FREE(element_map->islandIndices);
MEM_SAFE_FREE(element_map->island_indices);
MEM_SAFE_FREE(element_map->island_total_uvs);
MEM_SAFE_FREE(element_map->island_total_unique_uvs);
MEM_SAFE_FREE(element_map);
}
}

View File

@ -518,13 +518,7 @@ static UvSculptData *uv_sculpt_stroke_init(bContext *C, wmOperator *op, const wm
/* Count 'unique' UV's */
int unique_uvs = data->elementMap->total_unique_uvs;
if (do_island_optimization) {
unique_uvs = 0;
for (int i = 0; i < data->elementMap->total_uvs; i++) {
if (data->elementMap->storage[i].separate &&
(data->elementMap->storage[i].island == island_index)) {
unique_uvs++;
}
}
unique_uvs = data->elementMap->island_total_unique_uvs[island_index];
}
/* Allocate the unique uv buffers */
@ -572,6 +566,7 @@ static UvSculptData *uv_sculpt_stroke_init(bContext *C, wmOperator *op, const wm
uniqueUv[element - data->elementMap->storage] = counter;
}
}
BLI_assert(counter + 1 == unique_uvs);
/* Now, on to generate our uv connectivity data */
counter = 0;

View File

@ -270,7 +270,7 @@ static void createTransUVs(bContext *C, TransInfo *t)
continue;
}
island_center = MEM_callocN(sizeof(*island_center) * elementmap->totalIslands, __func__);
island_center = MEM_callocN(sizeof(*island_center) * elementmap->total_islands, __func__);
}
BM_ITER_MESH (efa, &iter, em->bm, BM_FACES_OF_MESH) {
@ -315,9 +315,7 @@ static void createTransUVs(bContext *C, TransInfo *t)
}
if (is_island_center) {
int i;
for (i = 0; i < elementmap->totalIslands; i++) {
for (int i = 0; i < elementmap->total_islands; i++) {
mul_v2_fl(island_center[i].co, 1.0f / island_center[i].co_num);
mul_v2_v2(island_center[i].co, t->aspect);
}

View File

@ -541,14 +541,11 @@ static bool uvedit_uv_straighten(Scene *scene, BMesh *bm, eUVWeldAlign tool)
}
bool changed = false;
/* Loop backwards to simplify logic. */
int j1 = element_map->total_uvs;
for (int i = element_map->totalIslands - 1; i >= 0; --i) {
int j0 = element_map->islandIndices[i];
changed |= uvedit_uv_straighten_elements(
element_map->storage + j0, j1 - j0, cd_loop_uv_offset, tool);
j1 = j0;
for (int i = 0; i < element_map->total_islands; i++) {
changed |= uvedit_uv_straighten_elements(element_map->storage + element_map->island_indices[i],
element_map->island_total_uvs[i],
cd_loop_uv_offset,
tool);
}
BM_uv_element_map_free(element_map);

View File

@ -5395,7 +5395,7 @@ static void uv_isolate_selected_islands(const Scene *scene,
return;
}
int num_islands = elementmap->totalIslands;
int num_islands = elementmap->total_islands;
/* Boolean array that tells if island with index i is completely selected or not. */
bool *is_island_not_selected = MEM_callocN(sizeof(bool) * (num_islands), __func__);

View File

@ -287,14 +287,6 @@ static void stitch_update_header(StitchStateContainer *ssc, bContext *C)
}
}
static int getNumOfIslandUvs(UvElementMap *elementMap, int island)
{
if (island == elementMap->totalIslands - 1) {
return elementMap->total_uvs - elementMap->islandIndices[island];
}
return elementMap->islandIndices[island + 1] - elementMap->islandIndices[island];
}
static void stitch_uv_rotate(const float mat[2][2],
const float medianPoint[2],
float uv[2],
@ -419,10 +411,9 @@ static void stitch_calculate_island_snapping(StitchState *state,
int final)
{
BMesh *bm = state->em->bm;
int i;
UvElement *element;
for (i = 0; i < state->element_map->totalIslands; i++) {
for (int i = 0; i < state->element_map->total_islands; i++) {
if (island_stitch_data[i].addedForPreview) {
int numOfIslandUVs = 0, j;
int totelem = island_stitch_data[i].num_rot_elements_neg +
@ -464,8 +455,8 @@ static void stitch_calculate_island_snapping(StitchState *state,
}
angle_to_mat2(rotation_mat, rotation);
numOfIslandUVs = getNumOfIslandUvs(state->element_map, i);
element = &state->element_map->storage[state->element_map->islandIndices[i]];
numOfIslandUVs = state->element_map->island_total_uvs[i];
element = &state->element_map->storage[state->element_map->island_indices[i]];
for (j = 0; j < numOfIslandUVs; j++, element++) {
/* stitchable uvs have already been processed, don't process */
if (!(element->flag & STITCH_PROCESSED)) {
@ -984,7 +975,7 @@ static int stitch_process_data(StitchStateContainer *ssc,
preview_position[i].data_position = STITCH_NO_PREVIEW;
}
island_stitch_data = MEM_callocN(sizeof(*island_stitch_data) * state->element_map->totalIslands,
island_stitch_data = MEM_callocN(sizeof(*island_stitch_data) * state->element_map->total_islands,
"stitch_island_data");
if (!island_stitch_data) {
return 0;
@ -1009,7 +1000,7 @@ static int stitch_process_data(StitchStateContainer *ssc,
}
/* Remember stitchable candidates as places the 'I' button will stop at. */
for (int island_idx = 0; island_idx < state->element_map->totalIslands; island_idx++) {
for (int island_idx = 0; island_idx < state->element_map->total_islands; island_idx++) {
state->island_is_stitchable[island_idx] = island_stitch_data[island_idx].stitchableCandidate ?
true :
false;
@ -1017,10 +1008,10 @@ static int stitch_process_data(StitchStateContainer *ssc,
if (is_active_state) {
/* set static island to one that is added for preview */
ssc->static_island %= state->element_map->totalIslands;
ssc->static_island %= state->element_map->total_islands;
while (!(island_stitch_data[ssc->static_island].stitchableCandidate)) {
ssc->static_island++;
ssc->static_island %= state->element_map->totalIslands;
ssc->static_island %= state->element_map->total_islands;
/* this is entirely possible if for example limit stitching
* with no stitchable verts or no selection */
if (ssc->static_island == previous_island) {
@ -1141,13 +1132,11 @@ static int stitch_process_data(StitchStateContainer *ssc,
* Setup preview for stitchable islands *
****************************************/
if (ssc->snap_islands) {
for (i = 0; i < state->element_map->totalIslands; i++) {
for (i = 0; i < state->element_map->total_islands; i++) {
if (island_stitch_data[i].addedForPreview) {
int numOfIslandUVs = 0, j;
UvElement *element;
numOfIslandUVs = getNumOfIslandUvs(state->element_map, i);
element = &state->element_map->storage[state->element_map->islandIndices[i]];
for (j = 0; j < numOfIslandUVs; j++, element++) {
int numOfIslandUVs = state->element_map->island_total_uvs[i];
UvElement *element = &state->element_map->storage[state->element_map->island_indices[i]];
for (int j = 0; j < numOfIslandUVs; j++, element++) {
stitch_set_face_preview_buffer_position(element->l->f, preview, preview_position);
}
}
@ -2120,8 +2109,8 @@ static StitchState *stitch_init(bContext *C,
/***** initialize static island preview data *****/
state->tris_per_island = MEM_mallocN(
sizeof(*state->tris_per_island) * state->element_map->totalIslands, "stitch island tris");
for (i = 0; i < state->element_map->totalIslands; i++) {
sizeof(*state->tris_per_island) * state->element_map->total_islands, "stitch island tris");
for (i = 0; i < state->element_map->total_islands; i++) {
state->tris_per_island[i] = 0;
}
@ -2133,7 +2122,7 @@ static StitchState *stitch_init(bContext *C,
}
}
state->island_is_stitchable = MEM_callocN(sizeof(bool) * state->element_map->totalIslands,
state->island_is_stitchable = MEM_callocN(sizeof(bool) * state->element_map->total_islands,
"stitch I stops");
if (!state->island_is_stitchable) {
state_delete(state);
@ -2157,7 +2146,7 @@ static bool goto_next_island(StitchStateContainer *ssc)
do {
ssc->static_island++;
if (ssc->static_island >= active_state->element_map->totalIslands) {
if (ssc->static_island >= active_state->element_map->total_islands) {
/* go to next object */
ssc->active_object_index++;
ssc->active_object_index %= ssc->objects_len;
@ -2307,7 +2296,7 @@ static int stitch_init_all(bContext *C, wmOperator *op)
ssc->static_island = RNA_int_get(op->ptr, "static_island");
StitchState *state = ssc->states[ssc->active_object_index];
ssc->static_island %= state->element_map->totalIslands;
ssc->static_island %= state->element_map->total_islands;
/* If the initial active object doesn't have any stitchable islands
* then no active island will be seen in the UI.