[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [58998] branches/ soc-2013-meshdata_transfer/source/blender/bmesh/tools/bmesh_data_transfer.c : Vertex Color transfer through projection: code cleaning, initializing the return vectors inside mid_poly_v3 and mid_poly_v2
Walid Shouman
eng.walidshouman at gmail.com
Wed Aug 7 20:31:26 CEST 2013
Revision: 58998
http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=58998
Author: walid
Date: 2013-08-07 18:31:26 +0000 (Wed, 07 Aug 2013)
Log Message:
-----------
Vertex Color transfer through projection: code cleaning, initializing the return vectors inside mid_poly_v3 and mid_poly_v2
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-07 18:22:15 UTC (rev 58997)
+++ branches/soc-2013-meshdata_transfer/source/blender/bmesh/tools/bmesh_data_transfer.c 2013-08-07 18:31:26 UTC (rev 58998)
@@ -1075,7 +1075,10 @@
{
int i;
+ zero_v2(r);
+
for (i = 0; i < n; i++) {
+
r[0] += v[i][0] * (1.0f/n);
r[1] += v[i][1] * (1.0f/n);
}
@@ -1085,6 +1088,8 @@
{
int i;
+ zero_v3(r);
+
for (i = 0; i < n; i++) {
r[0] += v[i][0] * (1.0f/n);
r[1] += v[i][1] * (1.0f/n);
@@ -2080,27 +2085,23 @@
BMEditMesh *em_src; //tree variable
struct BMBVHTree *bmtree_src = NULL; //tree variable
float *tmp_weight = NULL;
- float tmp_co[3];
+ 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 *v2;
+ BMVert *v;
+ BMIter iter;
float (*v_co_list_src)[3];
- float (*v_co_list_dst)[3];
- int v_src_count = 0;
- int v_dst_count = 0;
- int v_src_max_count, v_dst_max_count;
+ int v_src_max_count;
- int a;//, b, c, d, g, h, i;
+ int a, b;
const int exp_vert_per_face = 10;
//====algorithm definitions end
int CD_src, CD_dst;
- //used for iterating the destination's verts
- BMVert *v; //iterated vert
- BMIter iter, iter2; //vertex iterator
int src_lay_iter, dst_lay_iter;
//replace mode variables
@@ -2118,7 +2119,6 @@
bmtree_src = BKE_bmbvh_new(em_src, 0, NULL, false);
- 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");
//its unlikely to have faces with more than a certain number of vertices ...
@@ -2139,83 +2139,63 @@
//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) {
- 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;
- }
- }
+ //get the dst face center
+ BM_face_calc_center_mean(f_dst, f_mid_dst);
- 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);
-
+ //supporting either to copy relative to the target or not
if (relative_to_target == true) {
// 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!!
+ 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 {
- // Node tree accelerated search for closest face.
- f_src = BKE_bmbvh_find_face_closest(bmtree_src, f_mid_dst, FLT_MAX); //would return null if the source didn't
- //have faces within the radius range!!
+ f_src = BKE_bmbvh_find_face_closest(bmtree_src, f_mid_dst, FLT_MAX);
}
- ///fork from here to map each vertex into the projection
+ ///if we removed the FLT_MAX we shall check for the null f_src here
- //get a coordinate list of the f verts
- BM_ITER_ELEM_INDEX (v2, &iter2, f_src, BM_VERTS_OF_FACE, v_src_count) {
- //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
- //appended to this loop to save time of adding an extra loop
- tmp_weight = MEM_reallocN(tmp_weight, sizeof(*tmp_weight) * v_src_count);
-
- v_src_max_count = v_src_count;
- }
+ //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;
}
+ }
- copy_v3_v3(v_co_list_src[v_src_count], v2->co);
+ BM_ITER_ELEM_INDEX (v, &iter, f_src, BM_VERTS_OF_FACE, b) {
+ copy_v3_v3(v_co_list_src[b], v->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
+ //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 = MEM_mallocN(sizeof(*lcol_out), "lcol_out bmesh_data_transfer.c");
if (relative_to_target == true) {
- zero_v3(tmp_co);
+ zero_v3(v_dst_co);
// Transform into target space.
- mul_v3_m4v3(tmp_co, tmp_mat, l->v->co);
+ mul_v3_m4v3(v_dst_co, tmp_mat, l->v->co);
}
else {
- copy_v3_v3(tmp_co, l->v->co);
+ copy_v3_v3(v_dst_co, l->v->co);
}
+
// Project each vertex onto face.
- project_v3_plane(tmp_co, f_src->no, f_mid_src);
+ 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, v_src_count, tmp_co);
+ 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;
@@ -2249,7 +2229,6 @@
BKE_bmbvh_free(bmtree_src);
- MEM_freeN(v_co_list_dst);
MEM_freeN(v_co_list_src);
MEM_freeN(tmp_weight);
return true;
More information about the Bf-blender-cvs
mailing list