Revision: 59056
          
http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=59056
Author:   walid
Date:     2013-08-10 22:04:18 +0000 (Sat, 10 Aug 2013)
Log Message:
-----------
Transfer function: duplicating the color transfer to be a base for a generic 
function for the 4 transfer types

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-10 21:17:46 UTC (rev 59055)
+++ 
branches/soc-2013-meshdata_transfer/source/blender/bmesh/tools/bmesh_data_transfer.c
        2013-08-10 22:04:18 UTC (rev 59056)
@@ -2498,7 +2498,335 @@
        }
 }
 
+bool BM_mesh_copy(BMesh *bm_src, BMesh* bm_dst, const struct ReplaceLayerInfo 
replace_info, bool relative_to_target,
+                               float tmp_mat[4][4])
+{
+       //-----uv dependent variables
+       BMLoop *l, *l2;                                         //used for 
iterating the destination's loops
+       BMIter liter, liter2;
 
+       //-----algorithm definitions start
+       BMEditMesh *em_src;                                             //tree 
variable
+       struct BMBVHTree *bmtree_src = NULL;    //tree variable
+       float *tmp_weight = NULL;
+       float v_dst_co[3];
+       float f_mid_dst_proj[3];
+       float f_mid_src[3], f_mid_dst[3];
+
+       BMFace *f_src, *f_dst;
+       BMIter fiter;
+       BMVert *v;
+       BMIter iter;
+       float (*v_co_list_src)[3];
+       int v_src_max_count;
+
+       int a, b, c;
+       const int exp_vert_per_face = 10;
+       //====algorithm definitions end
+
+       int CD_src, CD_dst;
+
+//     int src_lay_iter, dst_lay_iter;
+
+       //replace mode variables
+       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
+       em_src = BKE_editmesh_create(bm_src, true);     //create editmesh data 
from bm WITH tess.
+                                                                               
                        //if it was false ... data other than
+                                                                               
                        //em->bm won't be copied
+
+       //get the faces tree
+       bmtree_src = BKE_bmbvh_new(em_src, 0, NULL, false);
+
+
+       v_co_list_src = MEM_mallocN(sizeof(*v_co_list_src) * exp_vert_per_face, 
"v_co_list_src bmesh_data_transfer.c");
+
+       //its unlikely to have faces with more than a certain number of 
vertices ...
+       //we'll later reallocate only if this threshold got exceeded
+       tmp_weight = MEM_mallocN(sizeof(*tmp_weight) * exp_vert_per_face, 
"tmp_weight bmesh_data_transfer.c");
+
+       src_lay_start = replace_info.src_lay_start;
+       src_lay_end = replace_info.src_lay_end;
+       dst_lay_start = replace_info.dst_lay_start;
+
+       if (src_lay_start < src_lay_end) {
+               //we've multiple layers: we shall optimise for multiple layers 
by storing the relationships between the src and
+               //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
+
+               //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");
+
+               //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;
+
+                               b++;
+                       }
+               }
+
+               b = 0;
+               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);
+
+                       //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;
+                               }
+                       }
+
+                       BM_ITER_ELEM_INDEX (v, &iter, f_src, BM_VERTS_OF_FACE, 
c) {
+                               copy_v3_v3(v_co_list_src[c], v->co);
+                       }
+
+                       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) {
+                                       l_weights[b].l_w = 
MEM_reallocN(l_weights->l_w, sizeof(*(l_weights->l_w)) * f_src->len);
+                               }
+
+                               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);
+                               }
+
+                               else {
+                                       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
+                               //rewritten in the interp_weights_poly_v3()
+                               interp_weights_poly_v3(tmp_weight, 
v_co_list_src, f_src->len, v_dst_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++;
+                       }
+               }
+//----- we shall remove this part into a separate function
+               //take the bm_src, bm_dst, layers, the type and the (void 
*weights)
+/*
+               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++) {
+
+                       //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);
+
+                       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;
+
+                                       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 * 
weight);
+                                               lcol_out.b += (lcol2->b * 
weight);
+                                               lcol_out.g += (lcol2->g * 
weight);
+                                               lcol_out.r += (lcol2->r * 
weight);
+                                       }
+
+                                       //shall we verify the indices!?
+                                       //there's no copy_v4_v4 for char!
+                                       lcol->a = lcol_out.a;
+                                       lcol->b = lcol_out.b;
+                                       lcol->g = lcol_out.g;
+                                       lcol->r = lcol_out.r;
+
+                                       //end of interpolation
+
+                                       b++;
+                               }
+                       }
+
+               }
+*/
+//====end of separated part
+               BM_mesh_multi_layer_copy(bm_src, bm_dst, replace_info, 
CD_MLOOPCOL, l_weights);
+
+               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;
+       }
+
+       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);
+
+               //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);
+
+                       //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);
+                                       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 {
+                                       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
+                               //rewritten in the interp_weights_poly_v3()
+                               interp_weights_poly_v3(tmp_weight, 
v_co_list_src, f_src->len, v_dst_co);
+
+                               // 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);
+
+                                       //there's no madd_v4_v4fl for char!
+                                       lcol_out.a += (lcol2->a * 
tmp_weight[a]);
+                                       lcol_out.b += (lcol2->b * 
tmp_weight[a]);

@@ Diff output truncated at 10240 characters. @@
_______________________________________________
Bf-blender-cvs mailing list
Bf-blender-cvs@blender.org
http://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to