[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [58852] branches/ soc-2013-meshdata_transfer/source/blender/bmesh/tools/bmesh_data_transfer.c : Vertex weights transfer through projection: fixing the memory leaks and applying the same optimization found in Vertex colors transfer , Vertex colors transfer through projection: extra memory reallocation reduction

Walid Shouman eng.walidshouman at gmail.com
Sat Aug 3 12:41:36 CEST 2013


Revision: 58852
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=58852
Author:   walid
Date:     2013-08-03 10:41:36 +0000 (Sat, 03 Aug 2013)
Log Message:
-----------
Vertex weights transfer through projection: fixing the memory leaks and applying the same optimization found in Vertex colors transfer, Vertex colors transfer through projection: extra memory reallocation reduction

Modified Paths:
--------------
    branches/soc-2013-meshdata_transfer/source/blender/bmesh/tools/bmesh_data_transfer.c

Modified: branches/soc-2013-meshdata_transfer/source/blender/bmesh/tools/bmesh_data_transfer.c
===================================================================
--- branches/soc-2013-meshdata_transfer/source/blender/bmesh/tools/bmesh_data_transfer.c	2013-08-03 10:30:39 UTC (rev 58851)
+++ branches/soc-2013-meshdata_transfer/source/blender/bmesh/tools/bmesh_data_transfer.c	2013-08-03 10:41:36 UTC (rev 58852)
@@ -2024,6 +2024,8 @@
 
 	float weight_accu;
 	int i;
+	const int exp_vert_per_face = 10;
+	int v_src_max_count, v_dst_max_count;
 	//====algorithm definitions end
 
 	int CD_src, CD_dst;
@@ -2056,8 +2058,8 @@
 	//get the faces tree
 	bmtree_src = BKE_bmbvh_new(em_src, 0, NULL, false);
 
-	v_co_list_dst = MEM_mallocN(sizeof(*v_co_list_dst), "v_co_list_dst bmesh_data_transfer.c");
-	v_co_list_src = MEM_mallocN(sizeof(*v_co_list_src), "v_co_list bmesh_data_transfer.c");
+	v_co_list_dst = MEM_mallocN(sizeof(*v_co_list_dst) * exp_vert_per_face, "v_co_list_dst bmesh_data_transfer.c");
+	v_co_list_src = MEM_mallocN(sizeof(*v_co_list_src) * exp_vert_per_face, "v_co_list_src bmesh_data_transfer.c");
 
 	weights_grp = MEM_mallocN(sizeof(*weights_grp) * bm_dst->totvert, "weights_grp bmesh_data_transfer.c");
 	BM_ITER_MESH (v, &iter, bm_dst, BM_VERTS_OF_MESH) {
@@ -2069,6 +2071,7 @@
 		weights_grp[v->head.index].count = 0;	//if that wasn't fast enf we may use calloc for the offsets_grp
 	}
 
+	tmp_weight = MEM_mallocN(sizeof(*tmp_weight) * exp_vert_per_face, "tmp_weight bmesh_data_transfer.c");
 	if (replace_mode == ST_APPEND_SHAPEKEY_GROUPS) {
 		//add 1 to skip the basis
 		src_lay_start = 0;
@@ -2105,7 +2108,13 @@
 			//get a coordinate list of the f_dst verts
 			//used to get the the f_mid_dst for mid_poly_v3
 			BM_ITER_ELEM_INDEX (v, &fiter, f_dst, BM_VERTS_OF_FACE, v_dst_count) {
-				v_co_list_dst = MEM_reallocN(v_co_list_dst, sizeof(*v_co_list_dst) * (v_dst_count + 1));
+				if (v_dst_count > exp_vert_per_face) {
+					if (v_dst_count > v_dst_max_count) {
+						v_co_list_dst = MEM_reallocN(v_co_list_dst, sizeof(*v_co_list_dst) * (v_dst_count + 1));
+						v_dst_max_count = v_dst_count;
+					}
+				}
+
 				copy_v3_v3(v_co_list_dst[v_dst_count], v->co);
 			}
 
@@ -2123,9 +2132,22 @@
 
 			//get a coordinate list of the f verts
 			BM_ITER_ELEM_INDEX (v2, &fiter, f_src, BM_VERTS_OF_FACE, v_src_count) {
-				v_co_list_src = MEM_reallocN(v_co_list_src, sizeof(*v_co_list_src) * (v_src_count + 1));
+				//reallocate if the verts/faces were more than expected
+				if (v_src_count > exp_vert_per_face) {
+					//and according to the previous records only allocate if that more than max already allocated
+					if (v_src_count > v_src_max_count) {
+						v_co_list_src = MEM_reallocN(v_co_list_src, sizeof(*v_co_list_src) * (v_src_count + 1));
+
+						// Prepare memory for later interpolation
+						tmp_weight = MEM_reallocN(tmp_weight, sizeof(*tmp_weight) * v_src_count);
+
+						v_src_max_count = v_src_count;
+					}
+				}
+
 				copy_v3_v3(v_co_list_src[v_src_count], v2->co);
 			}
+
 			zero_v3(f_mid_src);
 			mid_poly_v3(f_mid_src, v_co_list_src, v_src_count);	//get the mid point of the source face
 
@@ -2138,8 +2160,6 @@
 				project_v3_plane(tmp_co, f_src->no, f_mid_src);
 
 				// Interpolate weights over face.
-				//check that v_count will equal to n not n-1!!
-				tmp_weight = MEM_mallocN(sizeof(*tmp_weight) * v_src_count, "tmp_weight bmesh_data_transfer.c");
 
 				//spatially finding the weights from the face's vertices
 				interp_weights_poly_v3(tmp_weight, v_co_list_src, v_src_count, tmp_co);
@@ -2173,6 +2193,9 @@
 
 	}
 
+	MEM_freeN(v_co_list_dst);
+	MEM_freeN(v_co_list_src);
+	MEM_freeN(tmp_weight);
 	return true;
 }
 
@@ -2230,7 +2253,7 @@
 
 
 	v_co_list_dst = MEM_mallocN(sizeof(*v_co_list_dst) * exp_vert_per_face, "v_co_list_dst bmesh_data_transfer.c");
-	v_co_list_src = MEM_mallocN(sizeof(*v_co_list_src) * exp_vert_per_face, "v_co_list bmesh_data_transfer.c");
+	v_co_list_src = MEM_mallocN(sizeof(*v_co_list_src) * exp_vert_per_face, "v_co_list_src bmesh_data_transfer.c");
 
 	tot_layer_src = CustomData_number_of_layers(&bm_src->ldata, CD_MLOOPCOL);//to change the last one
 	tot_layer_dst = CustomData_number_of_layers(&bm_dst->ldata, CD_MLOOPCOL);	//get the number of Shapekey layers
@@ -2304,6 +2327,11 @@
 					//and according to the previous records only allocate if that more than max already allocated
 					if (v_src_count > v_src_max_count) {
 						v_co_list_src = MEM_reallocN(v_co_list_src, sizeof(*v_co_list_src) * (v_src_count + 1));
+
+						// Prepare memory for later interpolation
+						//appended to this loop to save time of adding an extra loop
+						tmp_weight = MEM_reallocN(tmp_weight, sizeof(*tmp_weight) * v_src_count);
+
 						v_src_max_count = v_src_count;
 					}
 				}
@@ -2313,12 +2341,6 @@
 			zero_v3(f_mid_src);
 			mid_poly_v3(f_mid_src, v_co_list_src, v_src_count);	//get the mid point of the source face
 
-			// Prepare memory for later interpolation
-			// it's unlikely that we've src_count more than 10!! so we'll reallocate only if our threshold was passed
-			if (v_src_count > exp_vert_per_face) {
-				tmp_weight = MEM_reallocN(tmp_weight, sizeof(*tmp_weight) * v_src_count);
-			}
-
 			BM_ITER_ELEM (l, &liter, f_dst, BM_LOOPS_OF_FACE) {
 
 				zero_v3(tmp_co);




More information about the Bf-blender-cvs mailing list