[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [59074] branches/ soc-2013-meshdata_transfer/source/blender/bmesh/tools: Vertex Weight Transfer: memory leak fix, appended the vertex weights to the BM_mesh_data_copy(), used the projection project_v3_plane() in some functions as they were mistakenly not copied/removed

Walid Shouman eng.walidshouman at gmail.com
Sun Aug 11 22:50:18 CEST 2013


Revision: 59074
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=59074
Author:   walid
Date:     2013-08-11 20:50:17 +0000 (Sun, 11 Aug 2013)
Log Message:
-----------
Vertex Weight Transfer: memory leak fix, appended the vertex weights to the BM_mesh_data_copy(), used the projection project_v3_plane() in some functions as they were mistakenly not copied/removed

Modified Paths:
--------------
    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.h

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-11 19:47:43 UTC (rev 59073)
+++ branches/soc-2013-meshdata_transfer/source/blender/bmesh/tools/bmesh_data_transfer.c	2013-08-11 20:50:17 UTC (rev 59074)
@@ -1878,15 +1878,15 @@
 					zero_v3(tmp_co);
 					// Transform into target space.
 					mul_v3_m4v3(tmp_co, tmp_mat, l->v->co);
-
-					// Project each vertex onto face.
-					project_v3_plane(tmp_co, f_src->no, f_mid_src);
 				}
 
 				else {
 					copy_v3_v3(tmp_co, f_mid_src);
 				}
 
+				// Project each vertex onto face.
+				project_v3_plane(tmp_co, f_src->no, f_mid_src);
+
 				// Interpolate weights over face.
 
 				//spatially finding the weights from the face's vertices
@@ -2084,6 +2084,16 @@
 	int count;
 } weighed_loop_pool;
 
+typedef struct vert_weight {
+	BMVert *v;
+	float weight;
+} vert_weight;
+
+typedef struct weighed_vert_pool {
+	vert_weight *v_w;
+	int count;
+} weighed_vert_pool;
+
 bool BM_mesh_multi_layer_copy(BMesh* bm_src, BMesh *bm_dst, const struct ReplaceLayerInfo replace_info, int type, void *weights)
 {
 	int CD_src, CD_dst;
@@ -2098,6 +2108,16 @@
 	BMLoop *l;
 	BMFace *f_dst;
 
+	BMIter iter;
+	BMVert *v;
+	float_pool *weights_grp;				//stores all the gathered offsets/vert to be averaged at the end of interpolation
+	float weight_accu;
+
+	///we shall use that later to optimise for the speed of processing for multi layers as each vertex will directly be
+	///weighed by other normalized source vertices and accordingly there won't be a need for the normalising weights loop
+	///not loops for initialsing
+//	weighed_vert_pool *v_weights = (weighed_vert_pool*) weights;
+
 	switch (type) {
 	    case CD_MLOOPCOL:
 		{
@@ -2153,7 +2173,81 @@
 
 		case CD_MDEFORMVERT:
 		{
-			return false;
+			MDeformVert *dv_dst, *dv_src;
+			MDeformWeight *dw_dst, *dw_src;
+
+			const int cd_dvert_dst_offset = CustomData_get_offset(&bm_dst->vdata, CD_MDEFORMVERT);
+			const int cd_dvert_src_offset = CustomData_get_offset(&bm_src->vdata, CD_MDEFORMVERT);
+
+			//weight_grp init
+			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) {
+
+				BM_ITER_ELEM_INDEX (f_dst, &fiter, v, BM_FACES_OF_VERT, a) {}
+				weights_grp[v->head.index].fl = MEM_mallocN(sizeof(*(weights_grp[v->head.index].fl)) * a,
+				        "weights_grp[v->head.index].fl  bmesh_data_transfer.c");
+
+				weights_grp[v->head.index].count = 0;	//if that wasn't fast enf we may use calloc for the offsets_grp
+			}
+
+			for (src_lay_iter = src_lay_start, dst_lay_iter = dst_lay_start; src_lay_iter <= src_lay_end;
+				src_lay_iter++, dst_lay_iter++) {
+				int src_grp_ind = src_lay_iter;
+				int dst_grp_ind = dst_lay_start + dst_lay_iter;
+
+				b = 0;
+				BM_ITER_MESH (f_dst, &fiter, bm_dst, BM_FACES_OF_MESH) {
+
+					BM_ITER_ELEM (l, &liter, f_dst, BM_VERTS_OF_FACE) {
+						float weight_accu;
+						//getting the weight holder (MDeformWeight)
+						dv_dst = BM_ELEM_CD_GET_VOID_P(v, cd_dvert_dst_offset);
+						dw_dst = defvert_verify_index(dv_dst, dst_grp_ind);	//use this to have a weight for the group assuming
+																			//the vert may haven't been assigned in advance
+						weight_accu = 0;
+//						BM_ITER_ELEM_INDEX (v2, &fiter2, f_src, BM_VERTS_OF_FACE, a) {
+						for (a = 0; a < l_weights[b].count; a++) {
+							float weight = l_weights[b].l_w[a].weight;
+
+							dv_src = BM_ELEM_CD_GET_VOID_P(l_weights[b].l_w[a].l->v, cd_dvert_src_offset);
+							dw_src = defvert_verify_index(dv_src, src_grp_ind);
+							weight_accu += dw_src->weight * weight;
+						}
+
+						weights_grp[l->v->head.index].fl[weights_grp[l->v->head.index].count] = weight_accu;
+						(weights_grp[l->v->head.index].count)++;
+
+						b++;
+					}
+				}
+
+				//normalising weights loop
+				BM_ITER_MESH (v, &iter, bm_dst, BM_VERTS_OF_MESH) {
+					int i;
+					weight_accu = 0;
+
+					for (i = 0; i < weights_grp[v->head.index].count; i++) {
+						weight_accu += (weights_grp[v->head.index].fl[i]) / (float)(weights_grp[v->head.index].count);
+					}
+
+					dv_dst = BM_ELEM_CD_GET_VOID_P(v, cd_dvert_dst_offset);
+					dw_dst = defvert_verify_index(dv_dst, dst_grp_ind);
+
+					if (dw_dst != NULL) {	//this check isnt useful till we support/unsupport copying vertices to unassigned v
+						dw_dst->weight = weight_accu;
+					}
+					weights_grp[v->head.index].count = 0;
+				}
+			}
+
+			//Freeing allocated memory blocks
+			BM_ITER_MESH (v, &iter, bm_dst, BM_VERTS_OF_MESH) {
+				MEM_freeN(weights_grp[v->head.index].fl);
+			}
+
+			MEM_freeN(weights_grp);
+
+			return true;
 		}
 
 		case CD_SHAPEKEY:
@@ -2498,7 +2592,7 @@
 	}
 }
 
-bool BM_mesh_data_copy(BMesh *bm_src, BMesh* bm_dst, const struct ReplaceLayerInfo replace_info, bool relative_to_target,
+bool BM_mesh_data_copy(BMesh *bm_src, BMesh* bm_dst, int type, const struct ReplaceLayerInfo replace_info, bool relative_to_target,
                                float tmp_mat[4][4])
 {
 	//-----uv dependent variables
@@ -2538,6 +2632,22 @@
 
 	//====multi layer optimisation variables end
 
+	//---extra variables for the vertex group copy
+	float_pool *weights_grp;				//stores all the gathered offsets/vert to be averaged at the end of interpolation
+	BMIter iter2;
+	BMVert *v2;
+	int i;
+
+	MDeformVert *dv_dst, *dv_src;
+	MDeformWeight *dw_dst, *dw_src;
+
+	const int cd_dvert_dst_offset = CustomData_get_offset(&bm_dst->vdata, CD_MDEFORMVERT);
+	const int cd_dvert_src_offset = CustomData_get_offset(&bm_src->vdata, CD_MDEFORMVERT);
+
+	int src_grp_ind, dst_grp_ind;
+
+	//===end of extra appended variables for the vgroup copy
+
 	//Is that good to support edit mesh mode at the cost of receiving me_src too ?
 	//if (me_src->edit_btmesh != NULL) em_src = me_src->edit_btmesh;	//edit mesh mode
 	//else
@@ -2633,9 +2743,9 @@
 					copy_v3_v3(v_dst_co, l->v->co);
 				}
 
-
 				// Project each vertex onto face.
 				project_v3_plane(v_dst_co, f_src->no, f_mid_src);
+
 				// Interpolate weights over face.
 
 				//spatially finding the weights from the face's vertices (no need to reset the weights/ it already gets
@@ -2701,8 +2811,32 @@
 		}
 */
 //====end of separated part
-		BM_mesh_multi_layer_copy(bm_src, bm_dst, replace_info, CD_MLOOPCOL, l_weights);
+		switch (type) {
+			case CD_MLOOPCOL:
+			{
+				BM_mesh_multi_layer_copy(bm_src, bm_dst, replace_info, CD_MLOOPCOL, l_weights);
+				break;
+			}
+			case CD_MDEFORMVERT:
+			{
+				BM_mesh_multi_layer_copy(bm_src, bm_dst, replace_info, CD_MDEFORMVERT, l_weights);
+				break;
+			}
+			case CD_SHAPEKEY:
+			{
+//				BM_mesh_multi_layer_copy(bm_src, bm_dst, replace_info, CD_SHAPEKEY, l_weights);
+				break;
+			}
+			case CD_MLOOPUV:
+			{
+//				BM_mesh_multi_layer_copy(bm_src, bm_dst, replace_info, CD_MLOOPUV, l_weights);
+				break;
+			}
+		}
 
+
+		//pure garpage collection part
+
 		BKE_bmbvh_free(bmtree_src);
 
 		//freeing what we've allocated in loops
@@ -2722,97 +2856,260 @@
 
 	else if (src_lay_start == src_lay_end) {
 
-		//fix the layer index of the source & dest
-		CD_src = CustomData_get_n_offset(&bm_src->ldata, CD_MLOOPCOL, src_lay_start);
-		CD_dst = CustomData_get_n_offset(&bm_dst->ldata, CD_MLOOPCOL, dst_lay_start);
+		switch (type) {
+			case CD_MLOOPCOL:
+			{
+				//fix the layer index of the source & dest
+				CD_src = CustomData_get_n_offset(&bm_src->ldata, CD_MLOOPCOL, src_lay_start);
+				CD_dst = CustomData_get_n_offset(&bm_dst->ldata, CD_MLOOPCOL, dst_lay_start);
 
-		//the way we do it is by looping over each face!!
-		BM_ITER_MESH (f_dst, &fiter, bm_dst, BM_FACES_OF_MESH) {
+				//the way we do it is by looping over each face!!
+				BM_ITER_MESH (f_dst, &fiter, bm_dst, BM_FACES_OF_MESH) {
 
-			//get the dst face center
-			BM_face_calc_center_mean(f_dst, f_mid_dst);
+					//get the dst face center
+					BM_face_calc_center_mean(f_dst, f_mid_dst);
 
-			//supporting either to copy relative to the target or not
-			if (relative_to_target == true) {
-				// Transform into target space.
-				mul_v3_m4v3(f_mid_dst_proj, tmp_mat, f_mid_dst);	//to start searching for a match
-				///the radius could be used to avoid overwriting data at at certain distance
-				f_src = BKE_bmbvh_find_face_closest(bmtree_src, f_mid_dst_proj, FLT_MAX);
-			}
+					//supporting either to copy relative to the target or not
+					if (relative_to_target == true) {
+						// Transform into target space.
+						mul_v3_m4v3(f_mid_dst_proj, tmp_mat, f_mid_dst);	//to start searching for a match
+						///the radius could be used to avoid overwriting data at at certain distance
+						f_src = BKE_bmbvh_find_face_closest(bmtree_src, f_mid_dst_proj, FLT_MAX);
+					}
 
-			else {
-				f_src = BKE_bmbvh_find_face_closest(bmtree_src, f_mid_dst, FLT_MAX);
-			}
+					else {
+						f_src = BKE_bmbvh_find_face_closest(bmtree_src, f_mid_dst, FLT_MAX);
+					}
 
-			///if we removed the FLT_MAX we shall check for the null f_src here
+					///if we removed the FLT_MAX we shall check for the null f_src here
 
-			//we should be so cautious about reallocating extra memory in loops!!
-			if (f_src->len > exp_vert_per_face) {
-				if (f_src->len > v_src_max_count) {
-					v_co_list_src = MEM_reallocN(v_co_list_src, sizeof(*v_co_list_src) * f_src->len);
-					tmp_weight = MEM_reallocN(tmp_weight, sizeof(*tmp_weight) * f_src->len);
-					v_src_max_count = f_src->len;
+					//we should be so cautious about reallocating extra memory in loops!!
+					if (f_src->len > exp_vert_per_face) {
+						if (f_src->len > v_src_max_count) {
+							v_co_list_src = MEM_reallocN(v_co_list_src, sizeof(*v_co_list_src) * f_src->len);
+							tmp_weight = MEM_reallocN(tmp_weight, sizeof(*tmp_weight) * f_src->len);
+							v_src_max_count = f_src->len;
+						}
+					}
+
+					BM_ITER_ELEM_INDEX (v, &iter, f_src, BM_VERTS_OF_FACE, b) {
+						copy_v3_v3(v_co_list_src[b], v->co);
+					}
+
+					//get the face center
+					BM_face_calc_center_mean(f_src, f_mid_src);
+
+					BM_ITER_ELEM (l, &liter, f_dst, BM_LOOPS_OF_FACE) {
+						MLoopCol *lcol = BM_ELEM_CD_GET_VOID_P(l, CD_dst);
+						MLoopCol lcol_out;
+
+						if (relative_to_target == true) {
+							zero_v3(v_dst_co);
+
+							// Transform into target space.
+							mul_v3_m4v3(v_dst_co, tmp_mat, l->v->co);
+						}
+
+						else {

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list