[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [58838] branches/ soc-2013-meshdata_transfer/source/blender/bmesh/tools: Vertex Weights transfer through projection: initial implementation for the vertex weights transfer in a similar manner to the previous transfer for UVs and Shapekeys
Walid Shouman
eng.walidshouman at gmail.com
Sat Aug 3 01:41:10 CEST 2013
Revision: 58838
http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=58838
Author: walid
Date: 2013-08-02 23:41:09 +0000 (Fri, 02 Aug 2013)
Log Message:
-----------
Vertex Weights transfer through projection: initial implementation for the vertex weights transfer in a similar manner to the previous transfer for UVs and Shapekeys
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-02 22:23:42 UTC (rev 58837)
+++ branches/soc-2013-meshdata_transfer/source/blender/bmesh/tools/bmesh_data_transfer.c 2013-08-02 23:41:09 UTC (rev 58838)
@@ -1997,3 +1997,183 @@
return true;
}
+
+typedef struct float_pool {
+ float *fl;
+ int count;
+} float_pool;
+
+bool BM_mesh_vertex_group_copy(BMesh *bm_src, BMesh* bm_dst,ST_ShapekeyGroupMode replace_mode, int *act_shapekey_lay,
+ float tmp_mat[4][4])
+{
+ //-----algorithm definitions start
+ struct BMBVHTree *bmtree_src = NULL;
+ float *tmp_weight = NULL;
+ float tmp_co[3];
+ BMIter fiter, fiter2;
+ BMVert *v2;
+
+ int a;
+
+ BMIter viter;
+ BMFace *f_dst, *f_src;
+ int v_dst_count, v_src_count;
+ float (*v_co_list_dst)[3], (*v_co_list_src)[3];
+ float f_mid_dst[3], f_mid_src[3];
+ float_pool *weights_grp; //stores all the gathered offsets/vert to be averaged at the end of interpolation
+
+ float weight_accu;
+ int i;
+ //====algorithm definitions end
+
+ int CD_src, CD_dst;
+
+ //used for iterating the destination's verts
+ BMVert *v;
+ //iter => vertex iterator
+ BMIter iter;
+ int tot_layer_src,tot_layer_dst;
+ int src_lay_iter, dst_lay_iter;
+
+ //tree variables
+ BMEditMesh *em_src;
+
+ //replace mode variables
+ int src_lay_start, src_lay_end;
+ int dst_lay_start, dst_lay_end; //dst_lay_end currently isn't being used
+
+// float v_co[3];
+
+ //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
+
+ tot_layer_src = CustomData_number_of_layers(&bm_src->vdata, CD_MDEFORMVERT);//to change the last one
+ tot_layer_dst = CustomData_number_of_layers(&bm_dst->vdata, CD_MDEFORMVERT); //get the number of Shapekey layers
+ //within the target
+
+ //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");
+
+ weights_grp = MEM_mallocN(sizeof(*weights_grp) * bm_dst->totvert, "weights_grp bmesh_data_transfer.c");
+ BM_ITER_MESH (v, &iter, bm_dst, BM_VERTS_OF_MESH) {
+
+ BM_ITER_ELEM_INDEX (f_dst, &viter, v, BM_FACES_OF_VERT, a) {}
+ weights_grp[v->head.index].fl = MEM_mallocN(sizeof(*(weights_grp[v->head.index].fl)) * a,
+ "weights_grp[v->head.index].fl bmesh_data_transfer.c");
+
+ weights_grp[v->head.index].count = 0; //if that wasn't fast enf we may use calloc for the offsets_grp
+ }
+
+ if (replace_mode == ST_APPEND_SHAPEKEY_GROUPS) {
+ //add 1 to skip the basis
+ src_lay_start = 0;
+ src_lay_end = tot_layer_src;
+ dst_lay_start = tot_layer_dst - tot_layer_src + 1;
+ dst_lay_end = tot_layer_dst;
+ }
+
+ else if ((replace_mode == ST_REPLACE_ENOUGH_SHAPEKEY_GROUPS) || (replace_mode == ST_REPLACE_ALL_SHAPEKEY_GROUPS)) {
+ src_lay_start = 0;
+ src_lay_end = tot_layer_src;
+ dst_lay_start = 0;
+ dst_lay_end = tot_layer_src;
+ }
+
+ else if (replace_mode == ST_REPLACE_ACTIVE_SHAPEKEY_GROUP) {
+ //passed shapekey reperesents the # of shapekeys (starts from one), however lay_start uses it as an index
+ src_lay_start = act_shapekey_lay[0] - 1;
+ src_lay_end = act_shapekey_lay[0];
+ dst_lay_start = act_shapekey_lay[1] - 1;
+ dst_lay_end = act_shapekey_lay[1];
+ }
+
+ 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->vdata, CD_MDEFORMVERT, src_lay_iter); //get the offset of the
+ CD_dst = CustomData_get_n_offset(&bm_dst->vdata, CD_MDEFORMVERT, dst_lay_iter); //lay_iter(th)CD_SHAPEKEY layer
+
+ //the way we do it is by looping over each face!!
+ BM_ITER_MESH (f_dst, &iter, 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, &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));
+ copy_v3_v3(v_co_list_dst[v_dst_count], v->co);
+ }
+
+ zero_v3(f_mid_dst);
+ mid_poly_v3(f_mid_dst, v_co_list_dst, v_dst_count);
+
+ // Transform into target space.
+ mul_v3_m4v3(tmp_co, tmp_mat, f_mid_dst); //to start searching for a match
+
+ // Node tree accelerated search for closest face.
+ f_src = BKE_bmbvh_find_face_closest(bmtree_src, tmp_co, FLT_MAX); //would return null if the source didn't
+ //have faces within the radius range!!
+ //deprecated comment
+ ///fork from here to map each vertex into the projection
+
+ //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));
+ 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) {
+ zero_v3(tmp_co);
+ // Transform into target space.
+ mul_v3_m4v3(tmp_co, tmp_mat, v->co);
+
+ // Project each vertex onto face.
+ project_v3_plane(tmp_co, f_src->no, f_mid_src);
+
+ // 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);
+
+ // Interpolating according to the spatially found weights
+ // Get weights from face.
+ weight_accu = 0;
+ BM_ITER_ELEM_INDEX (v2, &fiter2, f_src, BM_VERTS_OF_FACE, a) {
+ weight_accu = BM_ELEM_CD_GET_FLOAT(v2, CD_src) * tmp_weight[a];
+ }
+
+ weights_grp[v->head.index].fl[weights_grp[v->head.index].count] = weight_accu;
+ (weights_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) {
+ weight_accu = 0;
+
+ for (i = 0; i < weights_grp[v->head.index].count; i++) {
+ weight_accu += (weights_grp[v->head.index].fl[i]) / (float)(weights_grp[v->head.index].count);
+ }
+
+ ((float*)BM_ELEM_CD_GET_VOID_P(v, CD_dst))[0] = weight_accu;
+ weights_grp[v->head.index].count = 0;
+ }
+
+ }
+
+ return true;
+}
Modified: branches/soc-2013-meshdata_transfer/source/blender/bmesh/tools/bmesh_data_transfer.h
===================================================================
--- branches/soc-2013-meshdata_transfer/source/blender/bmesh/tools/bmesh_data_transfer.h 2013-08-02 22:23:42 UTC (rev 58837)
+++ branches/soc-2013-meshdata_transfer/source/blender/bmesh/tools/bmesh_data_transfer.h 2013-08-02 23:41:09 UTC (rev 58838)
@@ -61,6 +61,7 @@
int UNUSED_interp_pow, int UNUSED_no_pow, bool UNUSED_USE_NORMALS, ST_ShapekeyGroupMode replace_mode,
int *act_shapekey_lay, float tmp_mat[4][4]);
void BM_mesh_shapekey_copy_index(BMesh *bm_src, BMesh *bm_dst);
-
+bool BM_mesh_vertex_group_copy(BMesh *bm_src, BMesh* bm_dst,ST_ShapekeyGroupMode replace_mode, int *act_shapekey_lay,
+ float tmp_mat[4][4]);
#endif /* __BMESH_DATA_TRANSFER_H__ */
More information about the Bf-blender-cvs
mailing list