[Bf-blender-cvs] [8a91673562e] master: Cleanup: Move weld modifier to C++

Hans Goudey noreply at git.blender.org
Sat Dec 18 20:42:58 CET 2021


Commit: 8a91673562ebaa54844354d1e25dd7dc6e3c50d5
Author: Hans Goudey
Date:   Sat Dec 18 13:42:48 2021 -0600
Branches: master
https://developer.blender.org/rB8a91673562ebaa54844354d1e25dd7dc6e3c50d5

Cleanup: Move weld modifier to C++

This moves `MOD_weld.cc` to C++, fixing compiler warnings
coming from the change. It also goes a little bit further and converts
the code to use C++ data structures: `Span`, `Array`, and `Vector`.
This makes the code more shorter and easier to reason about, and
makes memory maneagement more automatic.

Differential Revision: https://developer.blender.org/D13618

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

M	source/blender/modifiers/CMakeLists.txt
R076	source/blender/modifiers/intern/MOD_weld.c	source/blender/modifiers/intern/MOD_weld.cc

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

diff --git a/source/blender/modifiers/CMakeLists.txt b/source/blender/modifiers/CMakeLists.txt
index c30beaf001a..e5c94c36a13 100644
--- a/source/blender/modifiers/CMakeLists.txt
+++ b/source/blender/modifiers/CMakeLists.txt
@@ -114,7 +114,7 @@ set(SRC
   intern/MOD_weightvgedit.c
   intern/MOD_weightvgmix.c
   intern/MOD_weightvgproximity.c
-  intern/MOD_weld.c
+  intern/MOD_weld.cc
   intern/MOD_wireframe.c
 
   MOD_modifiertypes.h
diff --git a/source/blender/modifiers/intern/MOD_weld.c b/source/blender/modifiers/intern/MOD_weld.cc
similarity index 76%
rename from source/blender/modifiers/intern/MOD_weld.c
rename to source/blender/modifiers/intern/MOD_weld.cc
index aa19e84b909..ac93f50edf4 100644
--- a/source/blender/modifiers/intern/MOD_weld.c
+++ b/source/blender/modifiers/intern/MOD_weld.cc
@@ -35,10 +35,12 @@
 
 #include "BLI_utildefines.h"
 
-#include "BLI_alloca.h"
+#include "BLI_array.hh"
 #include "BLI_bitmap.h"
 #include "BLI_kdtree.h"
 #include "BLI_math.h"
+#include "BLI_span.hh"
+#include "BLI_vector.hh"
 
 #include "BLT_translation.h"
 
@@ -69,6 +71,11 @@
 #include "MOD_modifiertypes.h"
 #include "MOD_ui_common.h"
 
+using blender::Array;
+using blender::MutableSpan;
+using blender::Span;
+using blender::Vector;
+
 /* Indicates when the element was not computed. */
 #define OUT_OF_CONTEXT (int)(-1)
 /* Indicates if the edge or face will be collapsed. */
@@ -89,13 +96,13 @@ struct WeldGroupEdge {
   int v2;
 };
 
-typedef struct WeldVert {
+struct WeldVert {
   /* Indexes relative to the original Mesh. */
   int vert_dest;
   int vert_orig;
-} WeldVert;
+};
 
-typedef struct WeldEdge {
+struct WeldEdge {
   union {
     int flag;
     struct {
@@ -106,9 +113,9 @@ typedef struct WeldEdge {
       int vert_b;
     };
   };
-} WeldEdge;
+};
 
-typedef struct WeldLoop {
+struct WeldLoop {
   union {
     int flag;
     struct {
@@ -119,9 +126,9 @@ typedef struct WeldLoop {
       int loop_skip_to;
     };
   };
-} WeldLoop;
+};
 
-typedef struct WeldPoly {
+struct WeldPoly {
   union {
     int flag;
     struct {
@@ -136,23 +143,23 @@ typedef struct WeldPoly {
       struct WeldGroup loops;
     };
   };
-} WeldPoly;
+};
 
-typedef struct WeldMesh {
+struct WeldMesh {
   /* Group of vertices to be merged. */
-  struct WeldGroup *vert_groups;
-  int *vert_groups_buffer;
+  Array<WeldGroup> vert_groups;
+  Array<int> vert_groups_buffer;
 
   /* Group of edges to be merged. */
-  struct WeldGroupEdge *edge_groups;
-  int *edge_groups_buffer;
+  Array<WeldGroupEdge> edge_groups;
+  Array<int> edge_groups_buffer;
   /* From the original index of the vertex, this indicates which group it is or is going to be
    * merged. */
-  int *edge_groups_map;
+  Array<int> edge_groups_map;
 
   /* References all polygons and loops that will be affected. */
-  WeldLoop *wloop;
-  WeldPoly *wpoly;
+  Vector<WeldLoop> wloop;
+  Vector<WeldPoly> wpoly;
   WeldPoly *wpoly_new;
   int wloop_len;
   int wpoly_len;
@@ -160,8 +167,8 @@ typedef struct WeldMesh {
 
   /* From the actual index of the element in the mesh, it indicates what is the index of the Weld
    * element above. */
-  int *loop_map;
-  int *poly_map;
+  Array<int> loop_map;
+  Array<int> poly_map;
 
   int vert_kill_len;
   int edge_kill_len;
@@ -170,14 +177,14 @@ typedef struct WeldMesh {
 
   /* Size of the affected polygon with more sides. */
   int max_poly_len;
-} WeldMesh;
+};
 
-typedef struct WeldLoopOfPolyIter {
+struct WeldLoopOfPolyIter {
   int loop_start;
   int loop_end;
-  const WeldLoop *wloop;
-  const MLoop *mloop;
-  const int *loop_map;
+  Span<WeldLoop> wloop;
+  Span<MLoop> mloop;
+  Span<int> loop_map;
   /* Weld group. */
   int *group;
 
@@ -189,7 +196,7 @@ typedef struct WeldLoopOfPolyIter {
   int v;
   int e;
   char type;
-} WeldLoopOfPolyIter;
+};
 
 /* -------------------------------------------------------------------- */
 /** \name Debug Utils
@@ -197,21 +204,19 @@ typedef struct WeldLoopOfPolyIter {
 
 #ifdef USE_WELD_DEBUG
 static bool weld_iter_loop_of_poly_begin(WeldLoopOfPolyIter *iter,
-                                         const WeldPoly *wp,
-                                         const WeldLoop *wloop,
-                                         const MLoop *mloop,
-                                         const int *loop_map,
+                                         const WeldPoly &wp,
+                                         Span<WeldLoop> wloop,
+                                         Span<MLoop> mloop,
+                                         Span<int> loop_map,
                                          int *group_buffer);
 
 static bool weld_iter_loop_of_poly_next(WeldLoopOfPolyIter *iter);
 
-static void weld_assert_edge_kill_len(const WeldEdge *wedge,
-                                      const int wedge_len,
-                                      const int supposed_kill_len)
+static void weld_assert_edge_kill_len(Span<WeldEdge> wedge, const int supposed_kill_len)
 {
   int kills = 0;
   const WeldEdge *we = &wedge[0];
-  for (int i = wedge_len; i--; we++) {
+  for (int i = wedge.size(); i--; we++) {
     int edge_dest = we->edge_dest;
     /* Magically includes collapsed edges. */
     if (edge_dest != OUT_OF_CONTEXT) {
@@ -221,28 +226,25 @@ static void weld_assert_edge_kill_len(const WeldEdge *wedge,
   BLI_assert(kills == supposed_kill_len);
 }
 
-static void weld_assert_poly_and_loop_kill_len(const WeldPoly *wpoly,
-                                               const WeldPoly *wpoly_new,
-                                               const int wpoly_new_len,
-                                               const WeldLoop *wloop,
-                                               const MLoop *mloop,
-                                               const int *loop_map,
-                                               const int *poly_map,
-                                               const MPoly *mpoly,
-                                               const int mpoly_len,
-                                               const int mloop_len,
+static void weld_assert_poly_and_loop_kill_len(Span<WeldPoly> wpoly,
+                                               Span<WeldPoly> wpoly_new,
+                                               Span<WeldLoop> wloop,
+                                               Span<MLoop> mloop,
+                                               Span<int> loop_map,
+                                               Span<int> poly_map,
+                                               Span<MPoly> mpoly,
                                                const int supposed_poly_kill_len,
                                                const int supposed_loop_kill_len)
 {
   int poly_kills = 0;
-  int loop_kills = mloop_len;
+  int loop_kills = mloop.size();
   const MPoly *mp = &mpoly[0];
-  for (int i = 0; i < mpoly_len; i++, mp++) {
+  for (int i = 0; i < mpoly.size(); i++, mp++) {
     int poly_ctx = poly_map[i];
     if (poly_ctx != OUT_OF_CONTEXT) {
       const WeldPoly *wp = &wpoly[poly_ctx];
       WeldLoopOfPolyIter iter;
-      if (!weld_iter_loop_of_poly_begin(&iter, wp, wloop, mloop, loop_map, NULL)) {
+      if (!weld_iter_loop_of_poly_begin(&iter, *wp, wloop, mloop, loop_map, nullptr)) {
         poly_kills++;
         continue;
       }
@@ -279,8 +281,8 @@ static void weld_assert_poly_and_loop_kill_len(const WeldPoly *wpoly,
     }
   }
 
-  const WeldPoly *wp = &wpoly_new[0];
-  for (int i = wpoly_new_len; i--; wp++) {
+  const WeldPoly *wp = wpoly_new.data();
+  for (int i = wpoly_new.size(); i--; wp++) {
     if (wp->poly_dst != OUT_OF_CONTEXT) {
       poly_kills++;
       continue;
@@ -312,15 +314,15 @@ static void weld_assert_poly_and_loop_kill_len(const WeldPoly *wpoly,
   BLI_assert(loop_kills == supposed_loop_kill_len);
 }
 
-static void weld_assert_poly_no_vert_repetition(const WeldPoly *wp,
-                                                const WeldLoop *wloop,
-                                                const MLoop *mloop,
-                                                const int *loop_map)
+static void weld_assert_poly_no_vert_repetition(const WeldPoly &wp,
+                                                Span<WeldLoop> wloop,
+                                                Span<MLoop> mloop,
+                                                Span<int> loop_map)
 {
-  const int len = wp->len;
-  int *verts = BLI_array_alloca(verts, len);
+  const int len = wp.len;
+  Array<int, 64> verts(len);
   WeldLoopOfPolyIter iter;
-  if (!weld_iter_loop_of_poly_begin(&iter, wp, wloop, mloop, loop_map, NULL)) {
+  if (!weld_iter_loop_of_poly_begin(&iter, wp, wloop, mloop, loop_map, nullptr)) {
     return;
   }
   else {
@@ -338,7 +340,7 @@ static void weld_assert_poly_no_vert_repetition(const WeldPoly *wp,
   }
 }
 
-static void weld_assert_poly_len(const WeldPoly *wp, const WeldLoop *wloop)
+static void weld_assert_poly_len(const WeldPoly *wp, const Span<WeldLoop> wloop)
 {
   if (wp->flag == ELEM_COLLAPSED) {
     return;
@@ -372,39 +374,27 @@ static void weld_assert_poly_len(const WeldPoly *wp, const WeldLoop *wloop)
 /** \name Weld Vert API
  * \{ */
 
-static void weld_vert_ctx_alloc_and_setup(const int mvert_len,
-                                          int *r_vert_dest_map,
-                                          WeldVert **r_wvert,
-                                          int *r_wvert_len)
+static Vector<WeldVert> weld_vert_ctx_alloc_and_setup(const int mvert_len,
+                                                      Span<int> vert_dest_map)
 {
-  /* Vert Context. */
-  int wvert_len = 0;
+  Vector<WeldVert> wvert;
+  wvert.reserve(mvert_len);
 
-  WeldVert *wvert, *wv;
-  wvert = MEM_mallocN(sizeof(*wvert) * mvert_len, __func__);
-  wv = &wvert[0];
-
-  int *v_dest_iter = &r_vert_dest_map[0];
+  const int *v_dest_iter = &vert_dest_map[0];
   for (int i = 0; i < mvert_len; i++, v_dest_iter++) {
     if (*v_dest_iter != OUT_OF_CONTEXT) {
-      wv->vert_dest = *v_dest_iter;
-      wv->vert_orig = i;
-      wv++;
-      wvert_len++;
+      wvert.append({*v_dest_iter, i});
     }
   }
-
-  *r_wvert = MEM_reallocN(wvert, sizeof(*wvert) * wvert_len);
-  *r_wvert_len = wvert_len;
+  return wvert

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list