[Bf-blender-cvs] [e441e21d74f] master: Cleanup: refactoring uvislands to prepare for python api

Chris Blackbourn noreply at git.blender.org
Sun Aug 7 06:16:41 CEST 2022


Commit: e441e21d74f9b1d28a5384ac14386e1465035b35
Author: Chris Blackbourn
Date:   Sun Aug 7 16:11:47 2022 +1200
Branches: master
https://developer.blender.org/rBe441e21d74f9b1d28a5384ac14386e1465035b35

Cleanup: refactoring uvislands to prepare for python api

See also: D15598

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

M	source/blender/blenkernel/BKE_mesh_mapping.h
M	source/blender/editors/mesh/editmesh_utils.c
M	source/blender/editors/sculpt_paint/sculpt_uv.c
M	source/blender/editors/uvedit/uvedit_ops.c
M	source/blender/editors/uvedit/uvedit_smart_stitch.c

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

diff --git a/source/blender/blenkernel/BKE_mesh_mapping.h b/source/blender/blenkernel/BKE_mesh_mapping.h
index c58bcbea242..f9dda7f5737 100644
--- a/source/blender/blenkernel/BKE_mesh_mapping.h
+++ b/source/blender/blenkernel/BKE_mesh_mapping.h
@@ -52,17 +52,24 @@ typedef struct UvElement {
   unsigned int island;
 } UvElement;
 
-/* UvElementMap is a container for UvElements of a mesh. It stores some UvElements belonging to the
- * same uv island in sequence and the number of uvs per island so it is possible to access all uvs
- * belonging to an island directly by iterating through the buffer.
+/** UvElementMap is a container for UvElements of a BMesh.
+ *
+ * It simplifies access to UV information and ensures the
+ * different UV selection modes are respected.
+ *
+ * If islands are calculated, it also stores UvElements
+ * belonging to the same uv island in sequence and
+ * the number of uvs per island.
  */
 typedef struct UvElementMap {
   /* address UvElements by their vertex */
   struct UvElement **vert;
   /* UvElement Store */
   struct UvElement *buf;
-  /* Total number of UVs in the layer. Useful to know */
-  int totalUVs;
+  /** Total number of UVs. */
+  int total_uvs;
+  /** Total number of unique UVs. */
+  int total_unique_uvs;
   /* Number of Islands in the mesh */
   int totalIslands;
   /* Stores the starting index in buf where each island begins */
diff --git a/source/blender/editors/mesh/editmesh_utils.c b/source/blender/editors/mesh/editmesh_utils.c
index ac5530c8ea9..fb7120b19ae 100644
--- a/source/blender/editors/mesh/editmesh_utils.c
+++ b/source/blender/editors/mesh/editmesh_utils.c
@@ -620,11 +620,11 @@ static int bm_uv_edge_select_build_islands(UvElementMap *element_map,
                                            bool uv_selected,
                                            int cd_loop_uv_offset)
 {
-  int totuv = element_map->totalUVs;
+  int total_uvs = element_map->total_uvs;
 
   /* For each UvElement, locate the "separate" UvElement that precedes it in the linked list. */
-  UvElement **head_table = MEM_mallocN(sizeof(*head_table) * totuv, "uv_island_head_table");
-  for (int i = 0; i < totuv; i++) {
+  UvElement **head_table = MEM_mallocN(sizeof(*head_table) * total_uvs, "uv_island_head_table");
+  for (int i = 0; i < total_uvs; i++) {
     UvElement *head = element_map->buf + i;
     if (head->separate) {
       UvElement *element = head;
@@ -641,11 +641,11 @@ static int bm_uv_edge_select_build_islands(UvElementMap *element_map,
   /* Depth first search the graph, building islands as we go. */
   int nislands = 0;
   int islandbufsize = 0;
-  int stack_upper_bound = totuv;
+  int stack_upper_bound = total_uvs;
   UvElement **stack_uv = MEM_mallocN(sizeof(*stack_uv) * stack_upper_bound,
                                      "uv_island_element_stack");
   int stacksize_uv = 0;
-  for (int i = 0; i < totuv; i++) {
+  for (int i = 0; i < total_uvs; i++) {
     UvElement *element = element_map->buf + i;
     if (element->island != INVALID_ISLAND) {
       /* Unique UV (element and all it's children) are already part of an island. */
@@ -713,7 +713,7 @@ static int bm_uv_edge_select_build_islands(UvElementMap *element_map,
     }
     nislands++;
   }
-  BLI_assert(islandbufsize == totuv);
+  BLI_assert(islandbufsize == total_uvs);
 
   MEM_SAFE_FREE(stack_uv);
   MEM_SAFE_FREE(head_table);
@@ -778,7 +778,7 @@ UvElementMap *BM_uv_element_map_create(BMesh *bm,
   }
 
   element_map = (UvElementMap *)MEM_callocN(sizeof(*element_map), "UvElementMap");
-  element_map->totalUVs = totuv;
+  element_map->total_uvs = totuv;
   element_map->vert = (UvElement **)MEM_callocN(sizeof(*element_map->vert) * totverts,
                                                 "UvElementVerts");
   buf = element_map->buf = (UvElement *)MEM_callocN(sizeof(*element_map->buf) * totuv,
@@ -1006,6 +1006,13 @@ UvElementMap *BM_uv_element_map_create(BMesh *bm,
 
   BLI_buffer_free(&tf_uv_buf);
 
+  element_map->total_unique_uvs = 0;
+  for (int i = 0; i < element_map->total_uvs; i++) {
+    if (element_map->buf[i].separate) {
+      element_map->total_unique_uvs++;
+    }
+  }
+
   return element_map;
 }
 
diff --git a/source/blender/editors/sculpt_paint/sculpt_uv.c b/source/blender/editors/sculpt_paint/sculpt_uv.c
index 52b790ba1ea..8ff7965513b 100644
--- a/source/blender/editors/sculpt_paint/sculpt_uv.c
+++ b/source/blender/editors/sculpt_paint/sculpt_uv.c
@@ -414,9 +414,8 @@ static void uv_sculpt_stroke_exit(bContext *C, wmOperator *op)
   if (data->timer) {
     WM_event_remove_timer(CTX_wm_manager(C), CTX_wm_window(C), data->timer);
   }
-  if (data->elementMap) {
-    BM_uv_element_map_free(data->elementMap);
-  }
+  BM_uv_element_map_free(data->elementMap);
+  data->elementMap = NULL;
   MEM_SAFE_FREE(data->uv);
   MEM_SAFE_FREE(data->uvedges);
   if (data->initial_stroke) {
@@ -469,7 +468,6 @@ static UvSculptData *uv_sculpt_stroke_init(bContext *C, wmOperator *op, const wm
   BKE_curvemapping_init(ts->uvsculpt->paint.brush->curve);
 
   if (data) {
-    int counter = 0, i;
     ARegion *region = CTX_wm_region(C);
     float co[2];
     BMFace *efa;
@@ -518,20 +516,24 @@ static UvSculptData *uv_sculpt_stroke_init(bContext *C, wmOperator *op, const wm
     }
 
     /* Count 'unique' UV's */
-    for (i = 0; i < data->elementMap->totalUVs; i++) {
-      if (data->elementMap->buf[i].separate &&
-          (!do_island_optimization || data->elementMap->buf[i].island == island_index)) {
-        counter++;
+    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->buf[i].separate &&
+            (data->elementMap->buf[i].island == island_index)) {
+          unique_uvs++;
+        }
       }
     }
 
     /* Allocate the unique uv buffers */
-    data->uv = MEM_mallocN(sizeof(*data->uv) * counter, "uv_brush_unique_uvs");
-    uniqueUv = MEM_mallocN(sizeof(*uniqueUv) * data->elementMap->totalUVs,
+    data->uv = MEM_mallocN(sizeof(*data->uv) * unique_uvs, "uv_brush_unique_uvs");
+    uniqueUv = MEM_mallocN(sizeof(*uniqueUv) * data->elementMap->total_uvs,
                            "uv_brush_unique_uv_map");
     edgeHash = BLI_ghash_new(uv_edge_hash, uv_edge_compare, "uv_brush_edge_hash");
     /* we have at most totalUVs edges */
-    edges = MEM_mallocN(sizeof(*edges) * data->elementMap->totalUVs, "uv_brush_all_edges");
+    edges = MEM_mallocN(sizeof(*edges) * data->elementMap->total_uvs, "uv_brush_all_edges");
     if (!data->uv || !uniqueUv || !edgeHash || !edges) {
       MEM_SAFE_FREE(edges);
       MEM_SAFE_FREE(uniqueUv);
@@ -542,11 +544,11 @@ static UvSculptData *uv_sculpt_stroke_init(bContext *C, wmOperator *op, const wm
       return NULL;
     }
 
-    data->totalUniqueUvs = counter;
-    /* So that we can use this as index for the UvElements */
-    counter = -1;
+    data->totalUniqueUvs = unique_uvs;
+    /* Index for the UvElements. */
+    int counter = -1;
     /* initialize the unique UVs */
-    for (i = 0; i < bm->totvert; i++) {
+    for (int i = 0; i < bm->totvert; i++) {
       UvElement *element = data->elementMap->vert[i];
       for (; element; element = element->next) {
         if (element->separate) {
@@ -627,7 +629,7 @@ static UvSculptData *uv_sculpt_stroke_init(bContext *C, wmOperator *op, const wm
     }
 
     /* fill the edges with data */
-    i = 0;
+    int i = 0;
     GHASH_ITER (gh_iter, edgeHash) {
       data->uvedges[i++] = *((UvEdge *)BLI_ghashIterator_getKey(&gh_iter));
     }
@@ -639,7 +641,7 @@ static UvSculptData *uv_sculpt_stroke_init(bContext *C, wmOperator *op, const wm
 
     /* transfer boundary edge property to UV's */
     if (ts->uv_sculpt_settings & UV_SCULPT_LOCK_BORDERS) {
-      for (i = 0; i < data->totalUvEdges; i++) {
+      for (int i = 0; i < data->totalUvEdges; i++) {
         if (!data->uvedges[i].flag) {
           data->uv[data->uvedges[i].uv1].flag |= MARK_BOUNDARY;
           data->uv[data->uvedges[i].uv2].flag |= MARK_BOUNDARY;
@@ -686,7 +688,7 @@ static UvSculptData *uv_sculpt_stroke_init(bContext *C, wmOperator *op, const wm
 
       counter = 0;
 
-      for (i = 0; i < data->totalUniqueUvs; i++) {
+      for (int i = 0; i < data->totalUniqueUvs; i++) {
         float dist, diff[2];
         if (data->uv[i].flag & MARK_BOUNDARY) {
           continue;
diff --git a/source/blender/editors/uvedit/uvedit_ops.c b/source/blender/editors/uvedit/uvedit_ops.c
index 5ebdfcec294..67c80f5bdfd 100644
--- a/source/blender/editors/uvedit/uvedit_ops.c
+++ b/source/blender/editors/uvedit/uvedit_ops.c
@@ -543,7 +543,7 @@ static bool uvedit_uv_straighten(Scene *scene, BMesh *bm, eUVWeldAlign tool)
   bool changed = false;
 
   /* Loop backwards to simplify logic. */
-  int j1 = element_map->totalUVs;
+  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(
diff --git a/source/blender/editors/uvedit/uvedit_smart_stitch.c b/source/blender/editors/uvedit/uvedit_smart_stitch.c
index 579674930a6..fb552ffc805 100644
--- a/source/blender/editors/uvedit/uvedit_smart_stitch.c
+++ b/source/blender/editors/uvedit/uvedit_smart_stitch.c
@@ -290,7 +290,7 @@ static void stitch_update_header(StitchStateContainer *ssc, bContext *C)
 static int getNumOfIslandUvs(UvElementMap *elementMap, int island)
 {
   if (island == elementMap->totalIslands - 1) {
-    return elementMap->totalUVs - elementMap->islandIndices[island];
+    return elementMap->total_uvs - elementMap->islandIndices[island];
   }
   return elementMap->islandIndices[island + 1] - elementMap->islandIndices[island];
 }
@@ -653,9 +653,8 @@ static void state_delete(StitchState *state)
     if (state->edges) {
       MEM_freeN(state->edges);
     }
-    if (state->stitch_preview) {
-      stitch_preview_delete(state->stitch_preview);
-    }
+    stitch_preview_delete(state->stitch_preview);
+    state->stitch_preview = NULL;
     if (state->edge_hash) {
       BLI_ghash_free(state->edge_hash, NULL, NULL);
     }
@@ -1263,7 +1262,7 @@ static int stitch_process_data(StitchS

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list