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

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:41:36 UTC (rev 58852)
+++ 
branches/soc-2013-meshdata_transfer/source/blender/bmesh/tools/bmesh_data_transfer.c
        2013-08-03 11:03:52 UTC (rev 58853)
@@ -1836,6 +1836,8 @@
        float (*v_co_list_dst)[3], (*v_co_list_src)[3];
        float f_mid_dst[3], f_mid_src[3];
        coord_pool *offsets_grp;                                //stores all 
the gathered offsets/vert to be averaged at the end of interpolation
+       const int exp_vert_per_face = 10;
+       int v_src_max_count, v_dst_max_count;
        //====algorithm definitions end
 
        int CD_offset_src, CD_offset_dst;
@@ -1873,8 +1875,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");
 
        offsets_grp = MEM_mallocN(sizeof(*offsets_grp) * bm_dst->totvert, 
"offsets_grp bmesh_data_transfer.c");
        BM_ITER_MESH (v, &iter, bm_dst, BM_VERTS_OF_MESH) {
@@ -1886,6 +1888,7 @@
                offsets_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 = 1;
@@ -1922,7 +1925,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);
                        }
 
@@ -1940,14 +1949,26 @@
 
                        //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
 
                        BM_ITER_ELEM (v, &fiter, f_dst, BM_VERTS_OF_FACE) {
-                               float tmp[3] = {0, 0, 0};
                                zero_v3(tmp_co);
                                // Transform into target space.
                                mul_v3_m4v3(tmp_co, tmp_mat, v->co);
@@ -1957,8 +1978,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);
@@ -1973,18 +1992,14 @@
                                }
 
                                
copy_v3_v3(offsets_grp[v->head.index].coord[offsets_grp[v->head.index].count], 
v_dst_offset);
-                               copy_v3_v3(tmp, v_dst_offset);
-                               copy_v3_v3(tmp, 
offsets_grp[v->head.index].coord[offsets_grp[v->head.index].count]);
                                (offsets_grp[v->head.index].count)++;
 
                                //shall we verify the indices!?
                                //we interpolate vertix weights instead
                        }
-
                }
 
                BM_ITER_MESH (v, &iter, bm_dst, BM_VERTS_OF_MESH) {
-                       ///should be prepended with a copy_v3_v3
                        zero_v3(v_dst_offset);
                        mid_poly_v3(v_dst_offset, 
offsets_grp[v->head.index].coord, offsets_grp[v->head.index].count);
 
@@ -1995,6 +2010,15 @@
 
        }
 
+       BM_ITER_MESH (v, &iter, bm_dst, BM_VERTS_OF_MESH) {
+               MEM_freeN(offsets_grp[v->head.index].coord);
+       }
+
+       MEM_freeN(offsets_grp);
+
+       MEM_freeN(v_co_list_dst);
+       MEM_freeN(v_co_list_src);
+       MEM_freeN(tmp_weight);
        return true;
 }
 

_______________________________________________
Bf-blender-cvs mailing list
[email protected]
http://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to