[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [58616] branches/ soc-2013-meshdata_transfer/source/blender/bmesh/tools/bmesh_data_transfer.c : Using mallocs instead of nested reallocs within the transfer shapekey function

Walid Shouman eng.walidshouman at gmail.com
Fri Jul 26 12:02:55 CEST 2013


Revision: 58616
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=58616
Author:   walid
Date:     2013-07-26 10:02:54 +0000 (Fri, 26 Jul 2013)
Log Message:
-----------
Using mallocs instead of nested reallocs within the transfer shapekey function

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-07-26 06:12:49 UTC (rev 58615)
+++ branches/soc-2013-meshdata_transfer/source/blender/bmesh/tools/bmesh_data_transfer.c	2013-07-26 10:02:54 UTC (rev 58616)
@@ -490,8 +490,17 @@
 
 	int i, j, k, m;
 
-	print_shapekeys_info(bm_src,CD_SHAPEKEY, true, true, SHAPEKEY_VALUES_BY_LAYER);
+	int *inherit_vert_ind;
+	int *interp_vert_ind;
 
+	float ** weights;
+
+	//---for interpolation
+	float (*eff_vert)[3];				//the effective vertices' shapekeys
+	float* inv_len;
+
+	//print_shapekeys_info(bm_src,CD_SHAPEKEY, true, true, SHAPEKEY_VALUES_BY_LAYER);
+
 	//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
@@ -534,27 +543,19 @@
 	}
 
 
-	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 ident_vert = 0;
-		int interp_vert = 0;
+	//for a higher speed we allocate the pointers by the maximum size they may need (mesh_vert_num) in previous
+	//to avoid nested reallocations
+	if (src_lay_end >= src_lay_start) {
+		weights = MEM_mallocN(sizeof(*weights) * num_vert, "weights bmesh_data_transfer.c");
 
-		//the lookup table's variables:
-		//using the effective vertices' indices directly
-		int *inherit_vert_ind = MEM_mallocN(sizeof(*inherit_vert_ind), "inherit_vert_ind bmesh_data_transfer.c");
-		int *interp_vert_ind = MEM_mallocN(sizeof(*interp_vert_ind), "interp_vert_ind bmesh_data_transfer.c");
-
-		//weights carry the weights of the vertices organized by their index
-		//act as a link between the offsets of the barycentric transformation and the vertices' data
-		float **weights;
-
-		weights = MEM_callocN(sizeof(*weights) * num_vert, "weights bmesh_data_transfer.c");
-
+		//those ones' size should be the interp_vert
+		vertexCos_src_basis = MEM_mallocN(sizeof(*vertexCos_src_basis) * num_vert, "vertexCos_src_basis bmesh_data_transfer.c");
+		vertexCos_src_offset = MEM_mallocN(sizeof(*vertexCos_src_offset) * num_vert, "vertexCos_src_offset bmesh_data_transfer.c");
+		vertexCos_dst_basis = MEM_mallocN(sizeof(*vertexCos_dst_basis) * num_vert, "vertexCos_dst_basis bmesh_data_transfer.c");
 		vertexCos_dst_offset = MEM_mallocN(sizeof(*vertexCos_dst_offset) * num_vert, "vertexCos_dst_offset bmesh_data_transfer.c");
 
-		//fix the layer index of the source & dest
-		CD_offset_src = CustomData_get_n_offset(&bm_src->vdata, CD_SHAPEKEY,src_lay_iter);	//get the offset of the
-		CD_offset_dst = CustomData_get_n_offset(&bm_dst->vdata, CD_SHAPEKEY,dst_lay_iter);	//lay_iter(th)CD_SHAPEKEY layer
+		interp_vert_ind = MEM_mallocN(sizeof(*interp_vert_ind) * num_vert, "interp_vert_ind bmesh_data_transfer.c");
+		inherit_vert_ind = MEM_mallocN(sizeof(*inherit_vert_ind) * num_vert, "inherit_vert_ind bmesh_data_transfer.c");
 
 		//getting the cage coordinates is by using
 		//const float (*cagecos)[3];
@@ -564,6 +565,25 @@
 		//getting the bmtree representing the source (2nd,3rd &4th values should be checked)
 		bmtree = BKE_bmbvh_new(em_src, 0, NULL, false);
 
+		//----allocations for interpolation
+		//those should be reallocated num_effective_v times
+		inv_len = MEM_mallocN(sizeof(*inv_len) * num_vert, "inv_len bmesh_data_transfer.c");
+		//the effective vertices' shapekeys
+		eff_vert = MEM_mallocN(sizeof(*eff_vert) * num_vert, "eff_vert bmesh_data_transfer.c");
+	}
+
+	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 ident_vert = 0;
+		int interp_vert = 0;
+
+		//fix the layer index of the source & dest
+		CD_offset_src = CustomData_get_n_offset(&bm_src->vdata, CD_SHAPEKEY,src_lay_iter);	//get the offset of the
+		CD_offset_dst = CustomData_get_n_offset(&bm_dst->vdata, CD_SHAPEKEY,dst_lay_iter);	//lay_iter(th)CD_SHAPEKEY layer
+
+		//getting the bmtree representing the source (2nd,3rd &4th values should be checked)
+		bmtree = BKE_bmbvh_new(em_src, 0, NULL, false);
+
 		//the way we do it is by looping over each vertex!!
 		for (v = BM_iter_new(&iter, bm_dst, BM_VERTS_OF_MESH, NULL); v; v = BM_iter_step(&iter)) {
 			BMVert *v_match;
@@ -578,13 +598,6 @@
 				if (v_match != NULL)	{
 					ident_vert++;
 
-					//update the lookup table
-					inherit_vert_ind = MEM_reallocN(inherit_vert_ind, sizeof(*inherit_vert_ind) * ident_vert);
-
-					vertexCos_src_basis = MEM_reallocN(vertexCos_src_basis, sizeof(*vertexCos_src_basis) * ident_vert);
-					vertexCos_src_offset = MEM_reallocN(vertexCos_src_offset, sizeof(*vertexCos_src_offset) * ident_vert);
-					vertexCos_dst_basis = MEM_reallocN(vertexCos_dst_basis, sizeof(*vertexCos_dst_basis) * ident_vert);
-
 					copy_v3_v3(vertexCos_src_basis[ident_vert - 1], BM_ELEM_CD_GET_VOID_P(v_match, CD_basis_src));
 					copy_v3_v3(tmp1, BM_ELEM_CD_GET_VOID_P(v_match, CD_offset_src));
 					sub_v3_v3v3(vertexCos_src_offset[ident_vert - 1], tmp1, vertexCos_src_basis[ident_vert - 1]);
@@ -599,125 +612,117 @@
 				}
 				else {
 					interp_vert++;
-					interp_vert_ind = MEM_reallocN(interp_vert_ind, sizeof(*interp_vert_ind) * interp_vert);
 					interp_vert_ind[interp_vert - 1] = BM_elem_index_get(v);
 				}
 			}
 		}
 
-	if (!BKE_bmesh_calc_relative_deform(ident_vert, (const float(*)[3])vertexCos_src_basis,
-								   (const float(*)[3])vertexCos_dst_basis, (const float(*)[3])vertexCos_src_offset,
-								   (float(*)[3])vertexCos_dst_offset)) {
-		return false;
-	}
+		if (!BKE_bmesh_calc_relative_deform(ident_vert, (const float(*)[3])vertexCos_src_basis,
+									   (const float(*)[3])vertexCos_dst_basis, (const float(*)[3])vertexCos_src_offset,
+									   (float(*)[3])vertexCos_dst_offset)) {
+			return false;
+		}
 
-	//updating the main vertices in the destination
-	for (k = 0; k < ident_vert; k++) {
-		v = BM_vert_at_index(bm_dst, inherit_vert_ind[k]);
-		add_v3_v3v3(BM_ELEM_CD_GET_VOID_P(v,CD_offset_dst), BM_ELEM_CD_GET_VOID_P(v,CD_basis_dst),
-					(float*) weights[BM_elem_index_get(v)]);
-	}
+		//updating the main vertices in the destination
+		for (k = 0; k < ident_vert; k++) {
+			v = BM_vert_at_index(bm_dst, inherit_vert_ind[k]);
+			add_v3_v3v3(BM_ELEM_CD_GET_VOID_P(v,CD_offset_dst), BM_ELEM_CD_GET_VOID_P(v,CD_basis_dst),
+						(float*) weights[BM_elem_index_get(v)]);
+		}
 
-	//updating the rest of vertices in the destination
-	for (m = 0; m < interp_vert; m++) {
-		BMVert *v2;
+		//updating the rest of vertices in the destination
+		for (m = 0; m < interp_vert; m++) {
+			BMVert *v2;
 
-		//to be re-allocated when we figure out num_effective_v
-		float* inv_len = MEM_mallocN(sizeof(*inv_len), "inv_len bmesh_data_transfer.c");
-		float* eff_v_weights;
+			//to be re-allocated when we figure out num_effective_v
+			float* eff_v_weights;
 
-		float len;
+			float len;
 
-		//counter of the effective vertices
-		int num_effective_v = 0;
-		float sum_of_inv_len = 0;
+			//counter of the effective vertices
+			int num_effective_v = 0;
+			float sum_of_inv_len = 0;
 
-		//the effective vertices' shapekeys
-		float (*eff_vert)[3] = MEM_callocN(sizeof(*eff_vert), "eff_vert bmesh_data_transfer.c");
+			float weight_accu[3];
+			zero_v3(weight_accu);
 
-		float weight_accu[3];
-		zero_v3(weight_accu);
+			v = BM_vert_at_index(bm_dst, interp_vert_ind[m]);
 
-		v = BM_vert_at_index(bm_dst, interp_vert_ind[m]);
+			j = 0;
+			//get the effective vertices
+			for(k = 0; k < ident_vert; k++)
+			{
+				v2 = BM_vert_at_index(bm_dst, inherit_vert_ind[k]);
+				len = len_v3v3(v2->co, v->co);
+				if (len <= radius_interp) {
+					num_effective_v++;
 
-		j = 0;
-		//get the effective vertices
-		for(k = 0; k < ident_vert; k++)
-		{
-			v2 = BM_vert_at_index(bm_dst, inherit_vert_ind[k]);
-			len = len_v3v3(v2->co, v->co);
-			if (len <= radius_interp) {
-				num_effective_v++;
+					//get the offset of this vertex
+					sub_v3_v3v3(eff_vert[j], BM_ELEM_CD_GET_VOID_P(v2, CD_offset_dst),
+								BM_ELEM_CD_GET_VOID_P(v2, CD_basis_dst));
 
-				//getting the vert's shapekey
-				eff_vert = MEM_reallocN(eff_vert, sizeof(*eff_vert) * num_effective_v);
+					//getting the lengths
+					inv_len[j] = len > MY_MIN_FLOAT ? (1.0f / len) : MY_MAX_FLOAT;
 
-				//get the offset of this vertex
-				sub_v3_v3v3(eff_vert[j], BM_ELEM_CD_GET_VOID_P(v2, CD_offset_dst),
-							BM_ELEM_CD_GET_VOID_P(v2, CD_basis_dst));
-
-				//getting the lengths
-				inv_len = MEM_reallocN(inv_len, sizeof(*inv_len) * num_effective_v);
-				inv_len[j] = len > MY_MIN_FLOAT ? (1.0f / len) : MY_MAX_FLOAT;
-
-				if (USE_NORMALS) {
-					for (i = 0; i < no_pow; i++) {
-						inv_len[j] *= (1 + dot_v3v3(v2->no, v->no) );
+					if (USE_NORMALS) {
+						for (i = 0; i < no_pow; i++) {
+							inv_len[j] *= (1 + dot_v3v3(v2->no, v->no) );
+						}
 					}
+					j++;
 				}
-				j++;
 			}
-		}
 
-		//now we know exactly how many vertices we'll interpolate from
-		eff_v_weights = MEM_callocN(sizeof(*eff_v_weights) * num_effective_v, "eff_v_weights bmesh_data_transfer.c");
+			//now we know exactly how many vertices we'll interpolate from
+			eff_v_weights = MEM_mallocN(sizeof(*eff_v_weights) * num_effective_v, "eff_v_weights bmesh_data_transfer.c");
 
-		//normalized effect of the inv_edge_len:
-		//getting the sum
-		for (j = 0; j < num_effective_v; j++) {
-			for (i = 0; i < dist_pow; i++) {
-				inv_len[j] *= inv_len[j];
+			//normalized effect of the inv_edge_len:
+			//getting the sum
+			for (j = 0; j < num_effective_v; j++) {
+				for (i = 0; i < dist_pow; i++) {
+					inv_len[j] *= inv_len[j];
+				}
+
+				//ensure that the inv_len won't approach infinity
+				inv_len[j] = inv_len[j] < MY_MAX_FLOAT ? inv_len[j] : MY_MAX_FLOAT;
+
+				sum_of_inv_len += inv_len[j];
 			}
 
-			//ensure that the inv_len won't approach infinity
-			inv_len[j] = inv_len[j] < MY_MAX_FLOAT ? inv_len[j] : MY_MAX_FLOAT;
+			//getting the weights
+			for (j = 0; j < num_effective_v; j++) {
+				eff_v_weights[j] = inv_len[j] / sum_of_inv_len;
 
-			sum_of_inv_len += inv_len[j];
-		}
+			}
 
-		//getting the weights
-		for (j = 0; j < num_effective_v; j++) {
-			eff_v_weights[j] = inv_len[j] / sum_of_inv_len;
+			//start interpolating given the vertices' shapekeys and their weights

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list