[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [58564] branches/ soc-2013-meshdata_transfer/source/blender: Supporting the shapekey transfer by nearest face

Walid Shouman eng.walidshouman at gmail.com
Wed Jul 24 07:39:39 CEST 2013


Revision: 58564
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=58564
Author:   walid
Date:     2013-07-24 05:39:38 +0000 (Wed, 24 Jul 2013)
Log Message:
-----------
Supporting the shapekey transfer by nearest face

Modified Paths:
--------------
    branches/soc-2013-meshdata_transfer/source/blender/blenkernel/BKE_editmesh_bvh.h
    branches/soc-2013-meshdata_transfer/source/blender/blenkernel/intern/editmesh_bvh.c
    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
    branches/soc-2013-meshdata_transfer/source/blender/editors/object/object_shapekey.c

Modified: branches/soc-2013-meshdata_transfer/source/blender/blenkernel/BKE_editmesh_bvh.h
===================================================================
--- branches/soc-2013-meshdata_transfer/source/blender/blenkernel/BKE_editmesh_bvh.h	2013-07-24 05:12:51 UTC (rev 58563)
+++ branches/soc-2013-meshdata_transfer/source/blender/blenkernel/BKE_editmesh_bvh.h	2013-07-24 05:39:38 UTC (rev 58564)
@@ -49,8 +49,9 @@
 /* find a face intersecting a segment (but not apart of the segment) */
 struct BMFace  *BKE_bmbvh_find_face_segment(BMBVHTree *tree, const float co_a[3], const float co_b[3],
                                             float *r_fac, float r_hitout[3], float r_cagehit[3]);
-/* find a vert closest to co in a sphere of radius maxdist */
+/* find a vert/face closest to co in a sphere of radius maxdist */
 struct BMVert  *BKE_bmbvh_find_vert_closest(BMBVHTree *tree, const float co[3], const float maxdist);
+struct BMFace  *BKE_bmbvh_find_face_closest(BMBVHTree *tree, const float co[3], const float maxdist);
 
 /* BKE_bmbvh_new flag parameter */
 enum {

Modified: branches/soc-2013-meshdata_transfer/source/blender/blenkernel/intern/editmesh_bvh.c
===================================================================
--- branches/soc-2013-meshdata_transfer/source/blender/blenkernel/intern/editmesh_bvh.c	2013-07-24 05:12:51 UTC (rev 58563)
+++ branches/soc-2013-meshdata_transfer/source/blender/blenkernel/intern/editmesh_bvh.c	2013-07-24 05:39:38 UTC (rev 58564)
@@ -417,3 +417,71 @@
 
 	return NULL;
 }
+
+/* BKE_bmbvh_find_face_closest */
+
+struct FaceSearchUserData {
+	/* from the bmtree */
+	const BMLoop *(*looptris)[3];
+	const float (*cos_cage)[3];
+
+	/* from the hit */
+	float maxdist;
+};
+
+static void bmbvh_find_face_closest_cb(void *userdata, int index, const float co[3], BVHTreeNearest *hit)
+{
+	struct FaceSearchUserData *bmcb_data = userdata;	//get the bmcb data from the given userdata
+	const BMLoop **ltri = bmcb_data->looptris[index];	//the triangle should point at the gvn index of the gvn data
+	const float maxdist = bmcb_data->maxdist;			//get the maxdist from the given data
+	float dist;
+
+	const float *tri_cos[3];
+
+	float ltri_mid[3];
+
+	bmbvh_tri_from_face(tri_cos, ltri, bmcb_data->cos_cage);	//get the coordinates of each triangle
+
+	mid_v3_v3v3v3(ltri_mid, ltri[0]->v->co, ltri[1]->v->co, ltri[2]->v->co);
+	dist = len_squared_v3v3(co, ltri_mid);
+
+	if (dist < hit->dist && dist < maxdist) {		//each time the hit is updated so we're sure that only the best of the 3 will be selected
+		copy_v3_v3(hit->co, ltri_mid);
+		/* XXX, normal ignores cage */
+		copy_v3_v3(hit->no, ltri[0]->f->no);		//any of the three loops shall work
+		hit->dist = dist;
+		hit->index = index;
+	}
+}
+
+BMFace *BKE_bmbvh_find_face_closest(BMBVHTree *bmtree, const float co[3], const float maxdist)
+{
+	BVHTreeNearest hit;
+	struct FaceSearchUserData bmcb_data;
+	float maxdist_sq = maxdist * maxdist;
+
+	//avoid overlap
+	if ((maxdist > 0) && (maxdist_sq < maxdist))
+		maxdist_sq = FLT_MAX;
+
+	else if ((maxdist < 0) && (maxdist_sq < (-1 * maxdist)))
+		maxdist_sq = FLT_MAX;
+
+	//is that ok?
+	if (bmtree->cos_cage) BLI_assert(!(bmtree->em->bm->elem_index_dirty & BM_FACE));
+
+	hit.dist = maxdist_sq;
+	hit.index = -1;
+
+	bmcb_data.looptris = (const BMLoop *(*)[3])bmtree->em->looptris;
+	bmcb_data.cos_cage = (const float (*)[3])bmtree->cos_cage;
+	bmcb_data.maxdist = maxdist_sq;
+
+	BLI_bvhtree_find_nearest(bmtree->tree, co, &hit, bmbvh_find_face_closest_cb, &bmcb_data);
+	if (hit.index != -1) {
+		BMLoop **ltri = bmtree->em->looptris[hit.index];
+		return ltri[0]->f;								//any loop would be ok
+	}
+
+	return NULL;
+}

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-07-24 05:12:51 UTC (rev 58563)
+++ branches/soc-2013-meshdata_transfer/source/blender/bmesh/tools/bmesh_data_transfer.c	2013-07-24 05:39:38 UTC (rev 58564)
@@ -14,6 +14,9 @@
 #include "DNA_key_types.h"			//used for dealing with keyblocks
 #include "BLI_math_vector.h"		//for copying vectors
 
+#include "DNA_object_types.h"			//using the bDeformGroup
+#include "BKE_bvhutils.h"				//using the bvhutils.h
+
 //should be replaced by the min_float and its inverse
 #ifndef MY_MIN_FLOAT
 #define MY_MIN_FLOAT .001
@@ -539,7 +542,7 @@
 		//the lookup table's variables:
 		//using the effective vertices' indices directly
 		int *inherit_vert_ind = MEM_mallocN(sizeof(*inherit_vert_ind), "inherit_vert_ind bmesh_data_transfer.c");
-		int *interp_vert_ind = MEM_mallocN(sizeof(*interp_vert_ind) * interp_vert, "interp_vert_ind bmesh_data_transfer.c");
+		int *interp_vert_ind = MEM_mallocN(sizeof(*interp_vert_ind), "interp_vert_ind bmesh_data_transfer.c");
 
 		//weights carry the weights of the vertices organized by their index
 		//act as a link between the offsets of the barycentric transformation and the vertices' data
@@ -850,3 +853,144 @@
 	return true;
 }
 
+void mid_poly_v3(float r[3], float v[][3], const int n)
+{
+	int i;
+
+	for (i = 0; i < n; i++) {
+		r[0] += v[i][0] * (1.0f/n);
+		r[1] += v[i][1] * (1.0f/n);
+		r[2] += v[i][2] * (1.0f/n);
+	}
+}
+
+/*bool BM_mesh_shapekey_copy2(BMesh *bm_src, BMesh *bm_dst, float tolerance, float radius_interp, int dist_pow, int no_pow,
+                            bool USE_NORMALS, ST_ShapekeyGroupMode replace_mode, int *act_shapekey_lay,
+                            float tmp_mat[4])*/
+bool BM_mesh_shapekey_copy2(BMesh *bm_src, BMesh *bm_dst, float UNUSED(tolerance), float UNUSED(radius_interp),
+                            int UNUSED(dist_pow), int UNUSED(no_pow), bool UNUSED(USE_NORMALS),
+                            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];
+	BMFace *f;
+	BMIter fiter;
+	BMVert *v2;
+	float (*v_co_list)[3];
+	int v_count;
+
+	int a;
+	float v_src_offset[3] = {0, 0, 0};
+	float v_dst_offset[3] = {0, 0, 0};
+	//====algorithm definitions end
+
+	int CD_offset_src, CD_offset_dst;
+	int CD_basis_src, CD_basis_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
+
+	//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_SHAPEKEY);//to change the last one
+	tot_layer_dst = CustomData_number_of_layers(&bm_dst->vdata, CD_SHAPEKEY);	//get the number of Shapekey layers
+																				//within the target
+	CD_basis_src = CustomData_get_n_offset(&bm_src->vdata, CD_SHAPEKEY, 0);	//get the offset of the basis
+	CD_basis_dst = CustomData_get_n_offset(&bm_dst->vdata, CD_SHAPEKEY, 0);
+
+	//get the faces tree
+	bmtree_src = BKE_bmbvh_new(em_src, 0, NULL, false);
+
+	if (replace_mode == ST_APPEND_SHAPEKEY_GROUPS) {
+		//add 1 to skip the basis
+		src_lay_start = 1;
+		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 = 1;
+		src_lay_end = tot_layer_src;
+		dst_lay_start = 1;
+		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_offset_src = CustomData_get_n_offset(&bm_src->vdata, CD_SHAPEKEY,src_lay_iter);	//get the offset of the
+		CD_offset_dst = CustomData_get_n_offset(&bm_dst->vdata, CD_SHAPEKEY,dst_lay_iter);	//lay_iter(th)CD_SHAPEKEY layer
+
+		//the way we do it is by looping over each vertex!!
+		for (v = BM_iter_new(&iter, bm_dst, BM_VERTS_OF_MESH, NULL); v; v = BM_iter_step(&iter)) {
+
+			// Transform into target space.
+			mul_v3_m4v3(tmp_co, tmp_mat, v->co);
+
+			// Node tree accelerated search for closest face.
+			f = BKE_bmbvh_find_face_closest(bmtree_src, tmp_co, FLT_MAX);
+
+			//get a coordinate list of the f verts
+			BM_ITER_ELEM_INDEX (v2, &fiter, f, BM_VERTS_OF_FACE, v_count) {
+				v_co_list = MEM_reallocN(v_co_list, sizeof(*v_co_list) * (v_count + 1));
+				copy_v3_v3(v_co_list[v_count], v2->co);
+			}
+
+			// Project onto face.
+			//use a single coodrinate to get the tmp_co
+			//we may need to use this function instead of using the first vert's coords
+			//mid_poly_v3(v_co, v_co_list, v_count);
+			project_v3_plane(tmp_co, f->no, f->l_first->v->co);
+
+			// Interpolate weights over face.
+			//check that v_count will equal to n not n-1!!
+			tmp_weight = MEM_mallocN(sizeof(*tmp_weight) * v_count, "tmp_weight bmesh_data_transfer.c");
+
+			interp_weights_poly_v3(tmp_weight, v_co_list, v_count, tmp_co);
+
+			// Get weights from face.
+			zero_v3(v_dst_offset);
+			BM_ITER_ELEM_INDEX (v2, &fiter, f, BM_VERTS_OF_FACE, a) {
+				sub_v3_v3v3(v_src_offset, BM_ELEM_CD_GET_VOID_P(v2, CD_offset_src),
+				        BM_ELEM_CD_GET_VOID_P(v2, CD_basis_src));
+				madd_v3_v3fl(v_dst_offset, v_src_offset, tmp_weight[a]);
+			}
+
+			//shall we verify the indices!?
+			//we interpolate vertix weights instead
+			add_v3_v3v3(BM_ELEM_CD_GET_VOID_P(v, CD_offset_dst), BM_ELEM_CD_GET_VOID_P(v, CD_basis_dst),
+			            v_dst_offset);
+		}
+	}
+
+	return true;
+}
+

Modified: branches/soc-2013-meshdata_transfer/source/blender/bmesh/tools/bmesh_data_transfer.h

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list