[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [59041] branches/ soc-2013-meshdata_transfer/source/blender/bmesh/tools/bmesh_data_transfer.c : Vertex Color Transfer Through projection: performance optimising the transfer speed to skip recalculating the spatially found weights for every layer

Walid Shouman eng.walidshouman at gmail.com
Sat Aug 10 00:26:59 CEST 2013


Revision: 59041
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=59041
Author:   walid
Date:     2013-08-09 22:26:58 +0000 (Fri, 09 Aug 2013)
Log Message:
-----------
Vertex Color Transfer Through projection: performance optimising the transfer speed to skip recalculating the spatially found weights for every layer

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-09 21:14:19 UTC (rev 59040)
+++ branches/soc-2013-meshdata_transfer/source/blender/bmesh/tools/bmesh_data_transfer.c	2013-08-09 22:26:58 UTC (rev 59041)
@@ -2074,6 +2074,16 @@
 	int count;
 } float_pool;
 
+typedef struct loop_weight {
+	BMLoop *l;
+	float weight;
+} loop_weight;
+
+typedef struct weighed_loop_pool {
+	loop_weight *l_w;
+	int count;
+} weighed_loop_pool;
+
 bool BM_mesh_vertex_color_copy(BMesh *bm_src, BMesh* bm_dst, const struct ReplaceLayerInfo replace_info, bool relative_to_target,
                                float tmp_mat[4][4])
 {
@@ -2096,7 +2106,7 @@
 	float (*v_co_list_src)[3];
 	int v_src_max_count;
 
-	int a, b;
+	int a, b, c;
 	const int exp_vert_per_face = 10;
 	//====algorithm definitions end
 
@@ -2108,6 +2118,12 @@
 	int src_lay_start, src_lay_end;
 	int dst_lay_start;
 
+	//----multi layer optimisation variables start
+	weighed_loop_pool *l_weights = NULL;
+
+
+	//====multi layer optimisation variables end
+
 	//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
@@ -2134,89 +2150,123 @@
 		//dst before getting into the transfer ... that would consume more memory; thus the otherway is left in case
 		//that the transfer is for a single layer
 
-		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++) {
+		//totloop or totvert according to the transferred-data type
+		l_weights = MEM_mallocN(sizeof(*l_weights) * bm_dst->totloop, "l_weights bmesh_data_transfer.c");
 
-			//fix the layer index of the source & dest
-			CD_src = CustomData_get_n_offset(&bm_src->ldata, CD_MLOOPCOL, src_lay_iter);
-			CD_dst = CustomData_get_n_offset(&bm_dst->ldata, CD_MLOOPCOL, dst_lay_iter);
+		//note that we rely on that the loops/face and faces/mesh are iterated in order everytime ... otherwise we'd
+		//combine the malloc-ation loop with the function loop
+		b = 0;
+		BM_ITER_MESH (f_dst, &fiter, bm_dst, BM_FACES_OF_MESH) {
+			BM_ITER_ELEM (l, &liter, f_dst, BM_LOOPS_OF_FACE) {
+				l_weights[b].l_w = MEM_mallocN(sizeof(*(l_weights->l_w)) * exp_vert_per_face,
+				                             "l_weights->l_w bmesh_data_transfer.c");
+				l_weights[b].count = 0;
 
-			//the way we do it is by looping over each face!!
-			BM_ITER_MESH (f_dst, &fiter, bm_dst, BM_FACES_OF_MESH) {
+				b++;
+			}
+		}
 
-				//get the dst face center
-				BM_face_calc_center_mean(f_dst, f_mid_dst);
+		b = 0;
+		BM_ITER_MESH (f_dst, &fiter, bm_dst, BM_FACES_OF_MESH) {
 
-				//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);
-				}
+			//get the dst face center
+			BM_face_calc_center_mean(f_dst, f_mid_dst);
 
-				else {
-					f_src = BKE_bmbvh_find_face_closest(bmtree_src, f_mid_dst, 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);
+			}
+
+			///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);
+					v_src_max_count = f_src->len;
 				}
+			}
 
-				///if we removed the FLT_MAX we shall check for the null f_src here
+			BM_ITER_ELEM_INDEX (v, &iter, f_src, BM_VERTS_OF_FACE, c) {
+				copy_v3_v3(v_co_list_src[c], v->co);
+			}
 
-				//we should be so cautious about reallocating extra memory in loops!!
+			BM_ITER_ELEM (l, &liter, f_dst, BM_LOOPS_OF_FACE) {
+
+				//this reallocation is dragged into this loop as its required for each loop rather than for each face!
+				//we didn't use the max count here cause everytime its a totally different variable
 				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;
-					}
+					l_weights[b].l_w = MEM_reallocN(l_weights->l_w, sizeof(*(l_weights->l_w)) * 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);
+				l_weights[b].count = f_src->len;
+
+				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);
 				}
 
-				//get the face center
-				BM_face_calc_center_mean(f_src, f_mid_src);
+				else {
+					copy_v3_v3(v_dst_co, l->v->co);
+				}
 
-				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);
+				// Project each vertex onto face.
+				project_v3_plane(v_dst_co, f_src->no, f_mid_src);
+				// Interpolate weights over face.
 
-						// Transform into target space.
-						mul_v3_m4v3(v_dst_co, tmp_mat, l->v->co);
-					}
+				//spatially finding the weights from the face's vertices (no need to reset the weights/ it already gets
+				//rewritten in the interp_weights_poly_v3()
+				interp_weights_poly_v3(tmp_weight, v_co_list_src, f_src->len, v_dst_co);
 
-					else {
-						copy_v3_v3(v_dst_co, l->v->co);
-					}
+				BM_ITER_ELEM_INDEX (l2, &liter2, f_src, BM_LOOPS_OF_FACE, a) {
+					l_weights[b].l_w[a].l = l2;
+					l_weights[b].l_w[a].weight = tmp_weight[a];
+				}
 
+				b++;
+			}
+		}
 
-					// Project each vertex onto face.
-					project_v3_plane(v_dst_co, f_src->no, f_mid_src);
+		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++) {
 
-					// Interpolate weights over face.
+			//fix the layer index of the source & dest
+			CD_src = CustomData_get_n_offset(&bm_src->ldata, CD_MLOOPCOL, src_lay_iter);
+			CD_dst = CustomData_get_n_offset(&bm_dst->ldata, CD_MLOOPCOL, dst_lay_iter);
 
-					//spatially finding the weights from the face's vertices (no need to reset the weights/ it already gets
-					//rewritten in the interp_weights_poly_v3()
-					interp_weights_poly_v3(tmp_weight, v_co_list_src, f_src->len, v_dst_co);
+			b = 0;
+			//the way we do it is by looping over each face!!
+			BM_ITER_MESH (f_dst, &fiter, bm_dst, BM_FACES_OF_MESH) {
 
+				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;
+
 					// Interpolating according to the spatially found weights
 					lcol_out.a = 0;
 					lcol_out.b = 0;
 					lcol_out.g = 0;
 					lcol_out.r = 0;
 
-					BM_ITER_ELEM_INDEX (l2, &liter2, f_src, BM_LOOPS_OF_FACE, a) {
-						MLoopCol *lcol2 = BM_ELEM_CD_GET_VOID_P(l2, CD_src);
+					for (a = 0; a < l_weights[b].count; a++) {
+						MLoopCol *lcol2 = BM_ELEM_CD_GET_VOID_P(l_weights[b].l_w->l, CD_src);
+						float weight = l_weights[b].l_w[a].weight;
 
 						//there's no madd_v4_v4fl for char!
-						lcol_out.a += (lcol2->a * tmp_weight[a]);
-						lcol_out.b += (lcol2->b * tmp_weight[a]);
-						lcol_out.g += (lcol2->g * tmp_weight[a]);
-						lcol_out.r += (lcol2->r * tmp_weight[a]);
-
+						lcol_out.a += (lcol2->a * weight);
+						lcol_out.b += (lcol2->b * weight);
+						lcol_out.g += (lcol2->g * weight);
+						lcol_out.r += (lcol2->r * weight);
 					}
 
 					//shall we verify the indices!?
@@ -2227,6 +2277,8 @@
 					lcol->r = lcol_out.r;
 
 					//end of interpolation
+
+					b++;
 				}
 			}
 
@@ -2234,6 +2286,16 @@
 
 		BKE_bmbvh_free(bmtree_src);
 
+		//freeing what we've allocated in loops
+		b = 0;
+		BM_ITER_MESH (f_dst, &fiter, bm_dst, BM_FACES_OF_MESH) {
+			BM_ITER_ELEM (l, &liter, f_dst, BM_LOOPS_OF_FACE) {
+				MEM_freeN(l_weights[b].l_w);
+				b++;
+			}
+		}
+		MEM_freeN(l_weights);
+
 		MEM_freeN(v_co_list_src);
 		MEM_freeN(tmp_weight);
 		return true;




More information about the Bf-blender-cvs mailing list