[Bf-blender-cvs] [7e9f47c06c5] master: Cleanup: Reduce scope of variable declarations in bmesh_bevel.c

Hans Goudey noreply at git.blender.org
Tue Aug 11 21:08:48 CEST 2020


Commit: 7e9f47c06c5f880a3958e19c9d4ed3265585454b
Author: Hans Goudey
Date:   Tue Aug 11 15:08:04 2020 -0400
Branches: master
https://developer.blender.org/rB7e9f47c06c5f880a3958e19c9d4ed3265585454b

Cleanup: Reduce scope of variable declarations in bmesh_bevel.c

This commit generally moves variable declarations to the smallest scope
the variables are used in. This makes the code more readable by
making it clearer when variables are used and by removing the block
of variable declarations at the top of each function.

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

M	source/blender/bmesh/tools/bmesh_bevel.c

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

diff --git a/source/blender/bmesh/tools/bmesh_bevel.c b/source/blender/bmesh/tools/bmesh_bevel.c
index ec3ed05375c..c21854b0154 100644
--- a/source/blender/bmesh/tools/bmesh_bevel.c
+++ b/source/blender/bmesh/tools/bmesh_bevel.c
@@ -427,9 +427,8 @@ static FKind get_face_kind(BevelParams *bp, BMFace *f)
 /* Are d1 and d2 parallel or nearly so? */
 static bool nearly_parallel(const float d1[3], const float d2[3])
 {
-  float ang;
+  float ang = angle_v3v3(d1, d2);
 
-  ang = angle_v3v3(d1, d2);
   return (fabsf(ang) < BEVEL_EPSILON_ANG) || (fabsf(ang - (float)M_PI) < BEVEL_EPSILON_ANG);
 }
 
@@ -504,9 +503,7 @@ static void copy_mesh_vert(VMesh *vm, int ito, int jto, int kto, int ifrom, int
 /* Find the EdgeHalf in bv's array that has edge bme. */
 static EdgeHalf *find_edge_half(BevVert *bv, BMEdge *bme)
 {
-  int i;
-
-  for (i = 0; i < bv->edgecount; i++) {
+  for (int i = 0; i < bv->edgecount; i++) {
     if (bv->edges[i].e == bme) {
       return &bv->edges[i];
     }
@@ -527,15 +524,12 @@ static BevVert *find_bevvert(BevelParams *bp, BMVert *bmv)
  */
 static EdgeHalf *find_other_end_edge_half(BevelParams *bp, EdgeHalf *e, BevVert **r_bvother)
 {
-  BevVert *bvo;
-  EdgeHalf *eother;
-
-  bvo = find_bevvert(bp, e->is_rev ? e->e->v1 : e->e->v2);
+  BevVert *bvo = find_bevvert(bp, e->is_rev ? e->e->v1 : e->e->v2);
   if (bvo) {
     if (r_bvother) {
       *r_bvother = bvo;
     }
-    eother = find_edge_half(bvo, e->e);
+    EdgeHalf *eother = find_edge_half(bvo, e->e);
     BLI_assert(eother != NULL);
     return eother;
   }
@@ -549,12 +543,10 @@ static EdgeHalf *find_other_end_edge_half(BevelParams *bp, EdgeHalf *e, BevVert
  * If from_e is NULL, find the first beveled edge. */
 static EdgeHalf *next_bev(BevVert *bv, EdgeHalf *from_e)
 {
-  EdgeHalf *e;
-
   if (from_e == NULL) {
     from_e = &bv->edges[bv->edgecount - 1];
   }
-  e = from_e;
+  EdgeHalf *e = from_e;
   do {
     if (e->is_bev) {
       return e;
@@ -584,9 +576,8 @@ static int count_ccw_edges_between(EdgeHalf *e1, EdgeHalf *e2)
  * where the next or previous edge in the face must be bme2. */
 static bool edges_face_connected_at_vert(BMEdge *bme1, BMEdge *bme2)
 {
-  BMLoop *l;
   BMIter iter;
-
+  BMLoop *l;
   BM_ITER_ELEM (l, &iter, bme1, BM_LOOPS_OF_EDGE) {
     if (l->prev->e == bme2 || l->next->e == bme2) {
       return true;
@@ -604,9 +595,9 @@ static bool edges_face_connected_at_vert(BMEdge *bme1, BMEdge *bme2)
  */
 static BMFace *boundvert_rep_face(BoundVert *v, BMFace **r_fother)
 {
-  BMFace *frep, *frep2;
+  BMFace *frep;
 
-  frep2 = NULL;
+  BMFace *frep2 = NULL;
   if (v->ebev) {
     frep = v->ebev->fprev;
     if (v->efirst->fprev != frep) {
@@ -675,20 +666,16 @@ static BMFace *bev_create_ngon(BMesh *bm,
                                int mat_nr,
                                bool do_interp)
 {
-  BMIter iter;
-  BMLoop *l;
-  BMFace *f, *interp_f;
-  BMEdge *bme;
-  float save_co[3];
-  int i;
-
-  f = BM_face_create_verts(bm, vert_arr, totv, facerep, BM_CREATE_NOP, true);
+  BMFace *f = BM_face_create_verts(bm, vert_arr, totv, facerep, BM_CREATE_NOP, true);
 
   if ((facerep || (face_arr && face_arr[0])) && f) {
     BM_elem_attrs_copy(bm, bm, facerep ? facerep : face_arr[0], f);
     if (do_interp) {
-      i = 0;
+      int i = 0;
+      BMIter iter;
+      BMLoop *l;
       BM_ITER_ELEM (l, &iter, f, BM_LOOPS_OF_FACE) {
+        BMFace *interp_f;
         if (face_arr) {
           /* Assume loops of created face are in same order as verts. */
           BLI_assert(l->v == vert_arr[i]);
@@ -698,10 +685,11 @@ static BMFace *bev_create_ngon(BMesh *bm,
           interp_f = facerep;
         }
         if (interp_f) {
-          bme = NULL;
+          BMEdge *bme = NULL;
           if (edge_arr) {
             bme = edge_arr[i];
           }
+          float save_co[3];
           if (bme) {
             copy_v3_v3(save_co, l->v->co);
             closest_to_line_segment_v3(l->v->co, save_co, bme->v1->co, bme->v2->co);
@@ -720,6 +708,8 @@ static BMFace *bev_create_ngon(BMesh *bm,
    * this is done so the operator can select newly created geometry. */
   if (f) {
     BM_elem_flag_enable(f, BM_ELEM_TAG);
+    BMIter iter;
+    BMEdge *bme;
     BM_ITER_ELEM (bme, &iter, f, BM_EDGES_OF_FACE) {
       flag_out_edge(bm, bme);
     }
@@ -784,16 +774,11 @@ static bool contig_ldata_across_loops(BMesh *bm, BMLoop *l1, BMLoop *l2, int lay
  */
 static bool contig_ldata_across_edge(BMesh *bm, BMEdge *e, BMFace *f1, BMFace *f2)
 {
-  BMLoop *lef1, *lef2;
-  BMLoop *lv1f1, *lv1f2, *lv2f1, *lv2f2;
-  BMVert *v1, *v2;
-  UNUSED_VARS_NDEBUG(v1, v2);
-  int i;
-
   if (bm->ldata.totlayer == 0) {
     return true;
   }
 
+  BMLoop *lef1, *lef2;
   if (!BM_edge_loop_pair(e, &lef1, &lef2)) {
     return false;
   }
@@ -806,16 +791,17 @@ static bool contig_ldata_across_edge(BMesh *bm, BMEdge *e, BMFace *f1, BMFace *f
   if (lef1->f != f1 || lef2->f != f2) {
     return false;
   }
-  v1 = lef1->v;
-  v2 = lef2->v;
+  BMVert *v1 = lef1->v;
+  BMVert *v2 = lef2->v;
   BLI_assert((v1 == e->v1 && v2 == e->v2) || (v1 == e->v2 && v2 == e->v1));
-  lv1f1 = lef1;
-  lv2f1 = lef1->next;
-  lv1f2 = lef2->next;
-  lv2f2 = lef2;
+  UNUSED_VARS_NDEBUG(v1, v2);
+  BMLoop *lv1f1 = lef1;
+  BMLoop *lv2f1 = lef1->next;
+  BMLoop *lv1f2 = lef2->next;
+  BMLoop *lv2f2 = lef2;
   BLI_assert(lv1f1->v == v1 && lv1f1->f == f1 && lv2f1->v == v2 && lv2f1->f == f1 &&
              lv1f2->v == v1 && lv1f2->f == f2 && lv2f2->v == v2 && lv2f2->f == f2);
-  for (i = 0; i < bm->ldata.totlayer; i++) {
+  for (int i = 0; i < bm->ldata.totlayer; i++) {
     if (CustomData_layer_has_math(&bm->ldata, i)) {
       if (!contig_ldata_across_loops(bm, lv1f1, lv1f2, i) ||
           !contig_ldata_across_loops(bm, lv2f1, lv2f2, i)) {
@@ -834,18 +820,9 @@ static bool contig_ldata_across_edge(BMesh *bm, BMEdge *e, BMFace *f1, BMFace *f
  */
 static void math_layer_info_init(BevelParams *bp, BMesh *bm)
 {
-  int i, f, stack_top, totface, current_component;
-  int bmf_index, bmf_other_index;
-  int *face_component;
-  BMFace *bmf, *bmf_other;
-  BMEdge *bme;
-  BMFace **stack;
-  bool *in_stack;
-  BMIter eiter, fiter;
-
   bp->math_layer_info.has_math_layers = false;
   bp->math_layer_info.face_component = NULL;
-  for (i = 0; i < bm->ldata.totlayer; i++) {
+  for (int i = 0; i < bm->ldata.totlayer; i++) {
     if (CustomData_has_layer(&bm->ldata, CD_MLOOPUV)) {
       bp->math_layer_info.has_math_layers = true;
       break;
@@ -857,32 +834,32 @@ static void math_layer_info_init(BevelParams *bp, BMesh *bm)
 
   BM_mesh_elem_index_ensure(bm, BM_FACE);
   BM_mesh_elem_table_ensure(bm, BM_FACE);
-  totface = bm->totface;
-  face_component = BLI_memarena_alloc(bp->mem_arena, totface * sizeof(int));
+  int totface = bm->totface;
+  int *face_component = BLI_memarena_alloc(bp->mem_arena, totface * sizeof(int));
   bp->math_layer_info.face_component = face_component;
 
   /* Use an array as a stack. Stack size can't exceed total faces if keep track of what is in
    * stack. */
-  stack = MEM_malloc_arrayN(totface, sizeof(BMFace *), __func__);
-  in_stack = MEM_malloc_arrayN(totface, sizeof(bool), __func__);
+  BMFace **stack = MEM_malloc_arrayN(totface, sizeof(BMFace *), __func__);
+  bool *in_stack = MEM_malloc_arrayN(totface, sizeof(bool), __func__);
 
   /* Set all component ids by DFS from faces with unassigned components. */
-  for (f = 0; f < totface; f++) {
+  for (int f = 0; f < totface; f++) {
     face_component[f] = -1;
     in_stack[f] = false;
   }
-  current_component = -1;
-  for (f = 0; f < totface; f++) {
+  int current_component = -1;
+  for (int f = 0; f < totface; f++) {
     if (face_component[f] == -1 && !in_stack[f]) {
-      stack_top = 0;
+      int stack_top = 0;
       current_component++;
       BLI_assert(stack_top < totface);
       stack[stack_top] = BM_face_at_index(bm, f);
       in_stack[f] = true;
       while (stack_top >= 0) {
-        bmf = stack[stack_top];
+        BMFace *bmf = stack[stack_top];
         stack_top--;
-        bmf_index = BM_elem_index_get(bmf);
+        int bmf_index = BM_elem_index_get(bmf);
         in_stack[bmf_index] = false;
         if (face_component[bmf_index] != -1) {
           continue;
@@ -892,10 +869,14 @@ static void math_layer_info_init(BevelParams *bp, BMesh *bm)
          * are where contig_ldata_across_edge(...) is true for the
          * shared edge and two faces.
          */
+        BMIter eiter;
+        BMEdge *bme;
         BM_ITER_ELEM (bme, &eiter, bmf, BM_EDGES_OF_FACE) {
+          BMIter fiter;
+          BMFace *bmf_other;
           BM_ITER_ELEM (bmf_other, &fiter, bme, BM_FACES_OF_EDGE) {
             if (bmf_other != bmf) {
-              bmf_other_index = BM_elem_index_get(bmf_other);
+              int bmf_other_index = BM_elem_index_get(bmf_other);
               if (face_component[bmf_other_index] != -1 || in_stack[bmf_other_index]) {
                 continue;
               }
@@ -929,26 +910,22 @@ static void math_layer_info_init(BevelParams *bp, BMesh *bm)
  */
 static BMFace *choose_rep_face(BevelParams *bp, BMFace **face, int nfaces)
 {
-  int bmf_index, value_index, best_f, i;
-  BMFace *bmf;
-  float cent[3];
 #define VEC_VALUE_LEN 6
   float(*value_vecs)[VEC_VALUE_LEN] = NULL;
-  bool *still_viable = NULL;
   int num_viable = 0;
 
   value_vecs = BLI_array_alloca(value_vecs, nfaces);
-  still_viable = BLI_array_alloca(still_viable, nfaces);
+  bool *still_viable = BLI_array_alloca(still_viable, nfaces);
   for (int f = 0; f < nfaces; f++) {
-    bmf = face[f];
+    BMFace *bmf = face[f];
     if (bmf == NULL) {
       still_viable[f] = false;
       continue;
     }
     still_viable[f] = true;
     num_viable++;
-    bmf_index = BM_elem_index_get(bmf);
-    value_index = 0;
+    int bmf_index = BM_elem_index_get(bmf);
+    int value_index = 0;
     /* First tie-breaker: lower math-layer connected component id. */
     value_vecs[f][value_index++] = bp->math_layer_info.face_component ?
                                        (float)bp->math_layer_info.face_component[bmf_index] :
@@ -958,6 +935,7 @@ static BMFace *choose_rep_face(BevelParams *bp, BMFace **face, int nfaces)
     /* Next tie-breaker: lower material i

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list