[Bf-blender-cvs] [cdb3cbd6440] master: Cleanup: Simplify three functions in displist.c

Hans Goudey noreply at git.blender.org
Fri Feb 12 23:06:26 CET 2021


Commit: cdb3cbd644013e466943f623f9080cfdb9ca6f12
Author: Hans Goudey
Date:   Fri Feb 12 16:06:17 2021 -0600
Branches: master
https://developer.blender.org/rBcdb3cbd644013e466943f623f9080cfdb9ca6f12

Cleanup: Simplify three functions in displist.c

A few related improvements to the three functions:
 - Reduce variable scope
 - Use for loops instead of while loops
 - Use const, bool instead of int
 - Generally make logic easier to read

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

M	source/blender/blenkernel/BKE_displist.h
M	source/blender/blenkernel/intern/displist.c

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

diff --git a/source/blender/blenkernel/BKE_displist.h b/source/blender/blenkernel/BKE_displist.h
index b2502031029..86145fdfb41 100644
--- a/source/blender/blenkernel/BKE_displist.h
+++ b/source/blender/blenkernel/BKE_displist.h
@@ -113,7 +113,7 @@ void BKE_displist_make_mball_forRender(struct Depsgraph *depsgraph,
                                        struct ListBase *dispbase);
 
 bool BKE_displist_surfindex_get(DispList *dl, int a, int *b, int *p1, int *p2, int *p3, int *p4);
-void BKE_displist_fill(struct ListBase *dispbase,
+void BKE_displist_fill(const struct ListBase *dispbase,
                        struct ListBase *to,
                        const float normal_proj[3],
                        const bool flipnormal);
diff --git a/source/blender/blenkernel/intern/displist.c b/source/blender/blenkernel/intern/displist.c
index 58c050493c9..dc274e25823 100644
--- a/source/blender/blenkernel/intern/displist.c
+++ b/source/blender/blenkernel/intern/displist.c
@@ -292,164 +292,137 @@ bool BKE_displist_surfindex_get(DispList *dl, int a, int *b, int *p1, int *p2, i
 /* ICC with the optimization -02 causes crashes. */
 #  pragma intel optimization_level 1
 #endif
-static void curve_to_displist(Curve *cu,
-                              ListBase *nubase,
-                              ListBase *dispbase,
-                              const bool for_render)
+static void curve_to_displist(const Curve *cu,
+                              const ListBase *nubase,
+                              const bool for_render,
+                              ListBase *r_dispbase)
 {
-  DispList *dl;
-  BezTriple *bezt, *prevbezt;
-  BPoint *bp;
-  float *data;
-  int a, len, resolu;
   const bool editmode = (!for_render && (cu->editnurb || cu->editfont));
 
   LISTBASE_FOREACH (Nurb *, nu, nubase) {
     if (nu->hide != 0 && editmode) {
       continue;
     }
-
-    if (for_render && cu->resolu_ren != 0) {
-      resolu = cu->resolu_ren;
-    }
-    else {
-      resolu = nu->resolu;
-    }
-
     if (!BKE_nurb_check_valid_u(nu)) {
-      /* pass */
+      continue;
     }
-    else if (nu->type == CU_BEZIER) {
-      /* count */
-      len = 0;
-      a = nu->pntsu - 1;
-      if (nu->flagu & CU_NURB_CYCLIC) {
-        a++;
-      }
 
-      prevbezt = nu->bezt;
-      bezt = prevbezt + 1;
-      while (a--) {
-        if (a == 0 && (nu->flagu & CU_NURB_CYCLIC)) {
-          bezt = nu->bezt;
-        }
+    const int resolution = (for_render && cu->resolu_ren != 0) ? cu->resolu_ren : nu->resolu;
+    const bool is_cyclic = nu->flagu & CU_NURB_CYCLIC;
+    const BezTriple *bezt_first = &nu->bezt[0];
+    const BezTriple *bezt_last = &nu->bezt[nu->pntsu - 1];
 
+    if (nu->type == CU_BEZIER) {
+      int samples_len = 0;
+      for (int i = 1; i < nu->pntsu; i++) {
+        const BezTriple *prevbezt = &nu->bezt[i - 1];
+        const BezTriple *bezt = &nu->bezt[i];
         if (prevbezt->h2 == HD_VECT && bezt->h1 == HD_VECT) {
-          len++;
+          samples_len++;
         }
         else {
-          len += resolu;
+          samples_len += resolution;
         }
-
-        if (a == 0 && (nu->flagu & CU_NURB_CYCLIC) == 0) {
-          len++;
+      }
+      if (is_cyclic) {
+        /* If the curve is cyclic, sample the last edge between the last and first points. */
+        if (bezt_first->h1 == HD_VECT && bezt_last->h2 == HD_VECT) {
+          samples_len++;
         }
-
-        prevbezt = bezt;
-        bezt++;
+        else {
+          samples_len += resolution;
+        }
+      }
+      else {
+        /* Otherwise, we only need one additional sample to complete the last edge. */
+        samples_len++;
       }
 
-      dl = MEM_callocN(sizeof(DispList), "makeDispListbez");
-      /* len+1 because of 'forward_diff_bezier' function */
-      dl->verts = MEM_mallocN((len + 1) * sizeof(float[3]), "dlverts");
-      BLI_addtail(dispbase, dl);
+      /* Check that there are more than two points so the curve doesn't loop back on itself. This
+       * needs to be separate from `is_cyclic` because cyclic sampling can work with two points
+       * and resolution > 1.  */
+      const bool use_cyclic_sample = is_cyclic && (samples_len != 2);
+
+      DispList *dl = MEM_callocN(sizeof(DispList), __func__);
+      /* Add one to the length because of 'BKE_curve_forward_diff_bezier'. */
+      dl->verts = MEM_mallocN(sizeof(float[3]) * (samples_len + 1), "dlverts");
+      BLI_addtail(r_dispbase, dl);
       dl->parts = 1;
-      dl->nr = len;
+      dl->nr = samples_len;
       dl->col = nu->mat_nr;
       dl->charidx = nu->charidx;
 
-      data = dl->verts;
+      dl->type = use_cyclic_sample ? DL_POLY : DL_SEGM;
 
-      /* check that (len != 2) so we don't immediately loop back on ourselves */
-      if (nu->flagu & CU_NURB_CYCLIC && (dl->nr != 2)) {
-        dl->type = DL_POLY;
-        a = nu->pntsu;
-      }
-      else {
-        dl->type = DL_SEGM;
-        a = nu->pntsu - 1;
-      }
-
-      prevbezt = nu->bezt;
-      bezt = prevbezt + 1;
-
-      while (a--) {
-        if (a == 0 && dl->type == DL_POLY) {
-          bezt = nu->bezt;
-        }
+      float *data = dl->verts;
+      for (int i = 1; i < nu->pntsu; i++) {
+        const BezTriple *prevbezt = &nu->bezt[i - 1];
+        const BezTriple *bezt = &nu->bezt[i];
 
         if (prevbezt->h2 == HD_VECT && bezt->h1 == HD_VECT) {
           copy_v3_v3(data, prevbezt->vec[1]);
           data += 3;
         }
         else {
-          int j;
-          for (j = 0; j < 3; j++) {
+          for (int j = 0; j < 3; j++) {
             BKE_curve_forward_diff_bezier(prevbezt->vec[1][j],
                                           prevbezt->vec[2][j],
                                           bezt->vec[0][j],
                                           bezt->vec[1][j],
                                           data + j,
-                                          resolu,
+                                          resolution,
                                           sizeof(float[3]));
           }
-
-          data += 3 * resolu;
+          data += 3 * resolution;
         }
-
-        if (a == 0 && dl->type == DL_SEGM) {
-          copy_v3_v3(data, bezt->vec[1]);
+      }
+      if (is_cyclic) {
+        if (bezt_first->h1 == HD_VECT && bezt_last->h2 == HD_VECT) {
+          copy_v3_v3(data, bezt_last->vec[1]);
+        }
+        else {
+          for (int j = 0; j < 3; j++) {
+            BKE_curve_forward_diff_bezier(bezt_last->vec[1][j],
+                                          bezt_last->vec[2][j],
+                                          bezt_first->vec[0][j],
+                                          bezt_first->vec[1][j],
+                                          data + j,
+                                          resolution,
+                                          sizeof(float[3]));
+          }
         }
-
-        prevbezt = bezt;
-        bezt++;
+      }
+      else {
+        copy_v3_v3(data, bezt_last->vec[1]);
       }
     }
     else if (nu->type == CU_NURBS) {
-      len = (resolu * SEGMENTSU(nu));
-
-      dl = MEM_callocN(sizeof(DispList), "makeDispListsurf");
+      const int len = (resolution * SEGMENTSU(nu));
+      DispList *dl = MEM_callocN(sizeof(DispList), __func__);
       dl->verts = MEM_mallocN(len * sizeof(float[3]), "dlverts");
-      BLI_addtail(dispbase, dl);
+      BLI_addtail(r_dispbase, dl);
       dl->parts = 1;
-
       dl->nr = len;
       dl->col = nu->mat_nr;
       dl->charidx = nu->charidx;
+      dl->type = is_cyclic ? DL_POLY : DL_SEGM;
 
-      data = dl->verts;
-      if (nu->flagu & CU_NURB_CYCLIC) {
-        dl->type = DL_POLY;
-      }
-      else {
-        dl->type = DL_SEGM;
-      }
-      BKE_nurb_makeCurve(nu, data, NULL, NULL, NULL, resolu, sizeof(float[3]));
+      BKE_nurb_makeCurve(nu, dl->verts, NULL, NULL, NULL, resolution, sizeof(float[3]));
     }
     else if (nu->type == CU_POLY) {
-      len = nu->pntsu;
-      dl = MEM_callocN(sizeof(DispList), "makeDispListpoly");
+      const int len = nu->pntsu;
+      DispList *dl = MEM_callocN(sizeof(DispList), __func__);
       dl->verts = MEM_mallocN(len * sizeof(float[3]), "dlverts");
-      BLI_addtail(dispbase, dl);
+      BLI_addtail(r_dispbase, dl);
       dl->parts = 1;
       dl->nr = len;
       dl->col = nu->mat_nr;
       dl->charidx = nu->charidx;
+      dl->type = (is_cyclic && (dl->nr != 2)) ? DL_POLY : DL_SEGM;
 
-      data = dl->verts;
-      if ((nu->flagu & CU_NURB_CYCLIC) && (dl->nr != 2)) {
-        dl->type = DL_POLY;
-      }
-      else {
-        dl->type = DL_SEGM;
-      }
-
-      a = len;
-      bp = nu->bp;
-      while (a--) {
-        copy_v3_v3(data, bp->vec);
-        bp++;
-        data += 3;
+      for (int i = 0; i < len; i++) {
+        const BPoint *bp = &nu->bp[i];
+        copy_v3_v3(&dl->verts[i], bp->vec);
       }
     }
   }
@@ -461,22 +434,11 @@ static void curve_to_displist(Curve *cu,
  * This is also used to initialize #DispList.nors (one normal per display list).
  * \param flipnormal: Flip the normal (same as passing \a normal_proj negated)
  */
-void BKE_displist_fill(ListBase *dispbase,
+void BKE_displist_fill(const ListBase *dispbase,
                        ListBase *to,
                        const float normal_proj[3],
-                       const bool flipnormal)
+                       const bool flip_normal)
 {
-  ScanFillContext sf_ctx;
-  ScanFillVert *sf_vert, *sf_vert_new, *sf_vert_last;
-  ScanFillFace *sf_tri;
-  MemArena *sf_arena;
-  DispList *dlnew = NULL;
-  float *f1;
-  int colnr = 0, charidx = 0, cont = 1, tot, a, *index, nextcol = 0;
-  int totvert;
-  const int scanfill_flag = BLI_SCANFILL_CALC_REMOVE_DOUBLES | BLI_SCANFILL_CALC_POLYS |
-                            BLI_SCANFILL_CALC_HOLES;
-
   if (dispbase == NULL) {
     return;
   }
@@ -484,45 +446,48 @@ void BKE_displist_fill(ListBase *dispbase,
     return;
   }
 
-  sf_arena = BLI_memarena_new(BLI_SCANFILL_ARENA_SIZE, __func__);
+  const int scanfill_flag = BLI_SCANFILL_CALC_REMOVE_DOUBLES | BLI_SCANFILL_CALC_POLYS |
+                            BLI_SCANFILL_CALC_HOLES;
+
+  MemArena *sf_arena = BLI_memarena_new(BLI_SCANFILL_ARENA_SIZE, __func__

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list