[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [50519] trunk/blender/source/blender/bmesh /intern: fix [#31738] BM_vert_splice modifies loops during iteration

Campbell Barton ideasman42 at gmail.com
Tue Sep 11 08:12:49 CEST 2012


Revision: 50519
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=50519
Author:   campbellbarton
Date:     2012-09-11 06:12:48 +0000 (Tue, 11 Sep 2012)
Log Message:
-----------
fix [#31738] BM_vert_splice modifies loops during iteration

patch by Nicholas Bishop, modified to avoid looping over vert-loops one extra time.

added BM_iter_as_arrayN(), returns an iterator as an array without knowing the length before calling.

Modified Paths:
--------------
    trunk/blender/source/blender/bmesh/intern/bmesh_core.c
    trunk/blender/source/blender/bmesh/intern/bmesh_iterators.c
    trunk/blender/source/blender/bmesh/intern/bmesh_iterators.h

Modified: trunk/blender/source/blender/bmesh/intern/bmesh_core.c
===================================================================
--- trunk/blender/source/blender/bmesh/intern/bmesh_core.c	2012-09-11 05:27:15 UTC (rev 50518)
+++ trunk/blender/source/blender/bmesh/intern/bmesh_core.c	2012-09-11 06:12:48 UTC (rev 50519)
@@ -1798,18 +1798,21 @@
 int BM_vert_splice(BMesh *bm, BMVert *v, BMVert *vtarget)
 {
 	BMEdge *e;
-	BMLoop *l;
-	BMIter liter;
 
+	BMLoop **loops;
+	int i, loops_tot;
+
 	/* verts already spliced */
 	if (v == vtarget) {
 		return FALSE;
 	}
 
-	/* retarget all the loops of v to vtarget */
-	BM_ITER_ELEM (l, &liter, v, BM_LOOPS_OF_VERT) {
-		l->v = vtarget;
+	/* we can't modify the vert while iterating so first allocate an array of loops */
+	loops = BM_iter_as_arrayN(bm, BM_LOOPS_OF_VERT, v, &loops_tot);
+	for (i = 0; i < loops_tot; i++) {
+		loops[i]->v = vtarget;
 	}
+	MEM_freeN(loops);
 
 	/* move all the edges from v's disk to vtarget's disk */
 	while ((e = v->e)) {

Modified: trunk/blender/source/blender/bmesh/intern/bmesh_iterators.c
===================================================================
--- trunk/blender/source/blender/bmesh/intern/bmesh_iterators.c	2012-09-11 05:27:15 UTC (rev 50518)
+++ trunk/blender/source/blender/bmesh/intern/bmesh_iterators.c	2012-09-11 06:12:48 UTC (rev 50519)
@@ -28,6 +28,8 @@
  * See: bmesh_iterators_inlin.c too, some functions are here for speed reasons.
  */
 
+#include "MEM_guardedalloc.h"
+
 #include "BLI_utildefines.h"
 
 #include "bmesh.h"
@@ -105,6 +107,38 @@
 }
 
 /**
+ * \brief Iterator as Array
+ *
+ * Allocates a new array, has the advantage that you dont need to know the size ahead of time.
+ *
+ * Takes advantage of less common iterator usage to avoid counting twice,
+ * which you might end up doing when #BM_iter_as_array is used.
+ *
+ * Caller needs to free the array.
+ */
+void *BM_iter_as_arrayN(BMesh *bm, const char itype, void *data, int *r_len)
+{
+	BMIter iter;
+
+	if (BM_iter_init(&iter, bm, itype, data) && iter.count > 0) {
+		BMElem *ele;
+		BMElem **array = MEM_mallocN(sizeof(ele) * iter.count, __func__);
+		int i;
+
+		*r_len = iter.count;  /* set before iterating */
+
+		for (ele = BM_iter_step(&iter), i = 0; ele; ele = BM_iter_step(&iter), i++) {
+			array[i] = ele;
+		}
+		return array;
+	}
+	else {
+		*r_len = 0;
+		return NULL;
+	}
+}
+
+/**
  * \brief Elem Iter Flag Count
  *
  * Counts how many flagged / unflagged items are found in this element.

Modified: trunk/blender/source/blender/bmesh/intern/bmesh_iterators.h
===================================================================
--- trunk/blender/source/blender/bmesh/intern/bmesh_iterators.h	2012-09-11 05:27:15 UTC (rev 50518)
+++ trunk/blender/source/blender/bmesh/intern/bmesh_iterators.h	2012-09-11 06:12:48 UTC (rev 50519)
@@ -115,10 +115,19 @@
 	char itype;
 } BMIter;
 
-void *BM_iter_at_index(BMesh *bm, const char itype, void *data, int index);
-int   BM_iter_as_array(BMesh *bm, const char itype, void *data, void **array, const int len);
-int   BM_iter_elem_count_flag(const char itype, void *data, const char hflag, const short value);
-int   BM_iter_mesh_count_flag(const char itype, BMesh *bm, const char hflag, const short value);
+void   *BM_iter_at_index(BMesh *bm, const char itype, void *data, int index)
+#ifdef __GNUC__
+__attribute__((warn_unused_result))
+#endif
+;
+int     BM_iter_as_array(BMesh *bm, const char itype, void *data, void **array, const int len);
+void   *BM_iter_as_arrayN(BMesh *bm, const char itype, void *data, int *r_len)
+#ifdef __GNUC__
+__attribute__((warn_unused_result))
+#endif
+;
+int     BM_iter_elem_count_flag(const char itype, void *data, const char hflag, const short value);
+int     BM_iter_mesh_count_flag(const char itype, BMesh *bm, const char hflag, const short value);
 
 /* private for bmesh_iterators_inline.c */
 void  bmiter__vert_of_mesh_begin(struct BMIter *iter);




More information about the Bf-blender-cvs mailing list