[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [58860] branches/ soc-2013-meshdata_transfer/source/blender/bmesh/tools/bmesh_data_transfer.c : UV transfer through projection: fixing the memory leaks and reducing the reallocation allover the function to reallocate only when not expected number of elem /elem (ie: loops/vert or loops/face) was found
Walid Shouman
eng.walidshouman at gmail.com
Sat Aug 3 16:04:21 CEST 2013
Revision: 58860
http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=58860
Author: walid
Date: 2013-08-03 14:04:21 +0000 (Sat, 03 Aug 2013)
Log Message:
-----------
UV transfer through projection: fixing the memory leaks and reducing the reallocation allover the function to reallocate only when not expected number of elem/elem (ie: loops/vert or loops/face) was found
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 13:13:53 UTC (rev 58859)
+++ branches/soc-2013-meshdata_transfer/source/blender/bmesh/tools/bmesh_data_transfer.c 2013-08-03 14:04:21 UTC (rev 58860)
@@ -1362,7 +1362,23 @@
int v_src_count;
int v_dst_count;
- int a, b, c, d, g, h, i;
+ int a, b, c, d, g, h, i, l_dst_iter;
+ const int exp_vert_per_face = 10;
+ int v_src_max_count, v_dst_max_count;
+
+ int const exp_loop_per_face = 4;
+ int const exp_tolerance = 2; //expectation tolerance of 2 would allocate double the ideal memory for each face
+ //totface should be replaced by the faces for interpolation if we're transfering (ie: between selected faces)
+ int const dst_src_faces_ratio = ceil((float)bm_dst->totface /(float)bm_src->totface); //we should ensure that
+ //bm_src->totface != 0
+ int const exp_dst_loops_per_src_face = dst_src_faces_ratio * exp_loop_per_face * exp_tolerance;
+
+ //we use double the loop/vert as each loop could be sawed to 2 other loops in case of manifolds of 2 ..
+ //the generic case of n manifolds may require n * loop/vert to fulfill the expectation
+ int const exp_loop_per_vert_double = 10; //it should be 8 for a vertex surrounded by 4 faces (common scenario)
+ //as the time penalty in realloc is considered severe, we're adding a
+ //tolerence of 5 faces per vertex ... more than that we would realloc
+ int l_grp_max_count = 0;
//====algorithm definitions end
int CD_src, CD_dst;
@@ -1381,21 +1397,22 @@
//computation for an island based algorithm
//using a table for a faster access when we check whether the face was already added or not
//calloc cause unmapped faces should be nulled!!
+ //access is made by the source face index
BM_loops_per_face_mapping *fl_table = MEM_callocN(sizeof(*fl_table) * bm_src->totface, "fl_table bmesh_data_transfer.c");
//used to hold the source faces that we inherited from only, looping over them would be faster than looping over
//all the faces with the index ... specially if we inherited from a small portion of the source faces
- //note: the effective size should be f_src_count
+ //note: the effective size should be f_src_count/
+ //access is random (fcfs) and its used to later loop only over the effective src faces
BMFace **f_src_table = MEM_mallocN(sizeof(*f_src_table) * bm_src->totface, "f_src_table bmesh_data_transfer.c");
- int f_src_count = 0;
+ int f_src_count; //count of actual faces stored in the f_src_table
BMFace *fa, *fb, *f_n;
BMEdge *e;
BMIter eiter;
BM_UV_per_face_mapping *fuv_table = MEM_mallocN(sizeof(*fuv_table) * bm_dst->totface, "fuv_table bmesh_data_transfer.c");
BM_loop_pool *l_grp = MEM_mallocN(sizeof(*l_grp) * bm_dst->totloop, "l_grp bmesh_data_transfer.c");
- int l_grp_count;
- float (*uv_buffer)[2];
- int uv_buffer_alloc_size;
+ int l_grp_count; //count of actual loop groups
+ float (*uv_buffer)[2] = MEM_mallocN(sizeof(*uv_buffer) * exp_loop_per_vert_double, "uv_buffer bmesh_data_transfer.c");
float mid_uv[2];
//======end of loops connnecting definitions
@@ -1565,19 +1582,30 @@
//============end of getting the source vertices representing each of the source paths
*/
- 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");
tot_layer_src = CustomData_number_of_layers(&bm_src->ldata, CD_MLOOPUV);//to change the last one
tot_layer_dst = CustomData_number_of_layers(&bm_dst->ldata, CD_MLOOPUV); //get the number of Shapekey layers
//within the target
- //preserving space for the buffer
- BM_ITER_MESH_INDEX (f_dst, &fiter, bm_dst, BM_FACES_OF_MESH, b) {
+ l_dst_iter = 0;
+ BM_ITER_MESH_INDEX (f_dst, &iter, bm_dst, BM_FACES_OF_MESH, b) {
fuv_table[b].uv = MEM_mallocN(sizeof(*(fuv_table->uv)) * (f_dst->len), "fuv_table->uv bmesh_data_transfer.c");
fuv_table[b].f = f_dst;
+
+ BM_ITER_ELEM (l, &fiter, f_dst, BM_LOOPS_OF_FACE) {
+ l_grp[l_dst_iter].l = MEM_mallocN(sizeof(*(l_grp->l)) * exp_loop_per_vert_double, "l_grp[].l bmesh_data_transfer");
+
+ l_dst_iter++;
+ }
}
+ BM_ITER_MESH_INDEX (f_src, &iter, bm_src, BM_FACES_OF_MESH, b) {
+ fl_table[b].l = MEM_mallocN(sizeof(*(fl_table[b].l)) * exp_dst_loops_per_src_face, "fl_table.l bmesh_data_transfer.c");
+ }
+
+ 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;
@@ -1609,13 +1637,20 @@
CD_dst = CustomData_get_n_offset(&bm_dst->ldata, CD_MLOOPUV, dst_lay_iter); //lay_iter(th)CD_SHAPEKEY layer
l_grp_count = 0;
+ f_src_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) {
//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, &iter, 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);
}
@@ -1633,12 +1668,19 @@
if (fl_table[f_src->head.index].f == NULL) { //if the face source reperesnts a new entry
f_src_table[f_src_count] = f_src;
f_src_count++;
- fl_table[f_src->head.index].l = MEM_mallocN(sizeof(*(fl_table[f_src->head.index].l)) * (f_dst->len), "fl_table.l bmesh_data_transfer.c");
+
+ if (f_dst->len > exp_dst_loops_per_src_face)
+ {
+ fl_table[f_src->head.index].l = MEM_reallocN(fl_table[f_src->head.index].l, sizeof(*(fl_table[f_src->head.index].l)) * (f_dst->len));
+ }
}
else {
- fl_table[f_src->head.index].l = MEM_reallocN(fl_table[f_src->head.index].l,
- sizeof(*(fl_table[f_src->head.index].l)) * (fl_table[f_src->head.index].count + f_dst->len));
+ if ((fl_table[f_src->head.index].count + f_dst->len) > exp_dst_loops_per_src_face)
+ {
+ fl_table[f_src->head.index].l = MEM_reallocN(fl_table[f_src->head.index].l,
+ sizeof(*(fl_table[f_src->head.index].l)) * (fl_table[f_src->head.index].count + f_dst->len));
+ }
}
fl_table[f_src->head.index].f = f_src;
@@ -1646,9 +1688,22 @@
//get a coordinate list of the f verts
BM_ITER_ELEM_INDEX (v2, &iter2, 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
@@ -1665,8 +1720,6 @@
//project_v3_plane(tmp_co, f->no, f->l_first->v->co);//not sure, do we really have to use an actual vertex ?
// 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);
@@ -1712,18 +1765,20 @@
//we now shall average them into a buffer!
//we've got 2 loops ... search for them in the loop groups ... if found
- //add a new entry to l_grp and eincrement l_grp_count
+ //add a new entry to l_grp and increment l_grp_count
//else append the other loop
for (h = 0; h < l_grp_count; h++) {
- if (BM_loop_in_loops(l_grp[h].l,l_grp[h].count, fl_table[f_n->head.index].l[g])) {
+ if (BM_loop_in_loops(l_grp[h].l, l_grp[h].count, fl_table[f_n->head.index].l[g])) {
//found the neighboring face's loop in the group
- if (!BM_loop_in_loops(l_grp[h].l,l_grp[h].count, fl_table[f_src->head.index].l[d])) {
+ if (!BM_loop_in_loops(l_grp[h].l, l_grp[h].count, fl_table[f_src->head.index].l[d])) {
//we ensured its the first time for this loop, not to add loops twice when we search from
//the neighboring face
(l_grp[h].count)++;
- l_grp[h].l = MEM_reallocN(l_grp[h].l, sizeof(*(l_grp->l)) * (l_grp[h].count));
+ if (l_grp[h].count < exp_loop_per_vert_double) {
+ l_grp[h].l = MEM_reallocN(l_grp[h].l, sizeof(*(l_grp->l)) * (l_grp[h].count));
+ }
//and append it
l_grp[h].l[l_grp[h].count - 1] = fl_table[f_n->head.index].l[g];
@@ -1738,7 +1793,9 @@
//now reallocate memory for a new loop
(l_grp[h].count)++;
- l_grp[h].l = MEM_reallocN(l_grp[h].l, sizeof(*(l_grp->l)) * (l_grp[h].count));
+ if (l_grp[h].count < exp_loop_per_vert_double) {
+ l_grp[h].l = MEM_reallocN(l_grp[h].l, sizeof(*(l_grp->l)) * (l_grp[h].count));
+ }
//and append it
l_grp[h].l[l_grp[h].count - 1] = fl_table[f_n->head.index].l[g];
}
@@ -1752,7 +1809,7 @@
//make a new group entry and append it
l_grp[l_grp_count].count = 2;
//adding a place for 2 loops
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-blender-cvs
mailing list