[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [60915] branches/ soc-2013-meshdata_transfer/source/blender/bmesh/tools/bmesh_data_transfer.c : Code Rewrite:

Walid Shouman eng.walidshouman at gmail.com
Thu Oct 24 23:38:48 CEST 2013


Revision: 60915
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=60915
Author:   walid
Date:     2013-10-24 21:38:47 +0000 (Thu, 24 Oct 2013)
Log Message:
-----------
Code Rewrite: 
-adding assertions to avoid closing the application silently 
-removing unused datatype
-adding a function that returns a loop array
-replacing ele->index.head with BM_elem_index_get and BM_elem_index_set
-eliminating the usage BM_vert_at_index per vertex by the usage of a
lookup
-declaring the functions as static
-adding the GPL header

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-10-24 20:17:30 UTC (rev 60914)
+++ branches/soc-2013-meshdata_transfer/source/blender/bmesh/tools/bmesh_data_transfer.c	2013-10-24 21:38:47 UTC (rev 60915)
@@ -1,3 +1,27 @@
+/*
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * The Original Code is: all of this file.
+ *
+ * Contributor(s): Walid Shouman
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
 #include "MEM_guardedalloc.h"
 
 #include "BLI_utildefines.h"
@@ -37,13 +61,6 @@
 //----------------------*
 //******************
 
-typedef struct BMFace_match {
-	BMFace *f_src;
-	BMFace *f_dst;
-	BMLoop **l_src;				//src loops are mapped to dst loops according to the order of both
-	BMLoop **l_dst;
-} BMFace_match;
-
 typedef struct BMVert_match {
 	BMVert *v_src;
 	BMVert *v_dst;
@@ -52,14 +69,15 @@
 //---------------Declarations------------------------------
 
 //---------------helping functions declarations -----------
-BMLoop* BM_vert_find_best_tan_match_loop(BMVert *v_src, BMLoop *l_dst);
+static BMLoop* BM_vert_find_best_tan_match_loop(BMVert *v_src, BMLoop *l_dst);
+static int BM_iter_loops_as_array(BMesh *bm, BMElem **array, int len);
 #if 0
-BMLoop* BM_face_find_best_tan_match_loop(BMFace *f_src, BMLoop *l_dst);
+static BMLoop* BM_face_find_best_tan_match_loop(BMFace *f_src, BMLoop *l_dst);
 #endif
 
-int* BM_transform_index_mapping(BMesh *bm_src, BMElem **array_dst, int array_dst_count, int *index_mapping_in,
+static int *BM_transform_index_mapping(BMesh *bm_src, BMElem **array_dst, int array_dst_count, int *index_mapping_in,
                                 int htype_from, int htype_to);
-void *BM_mesh_mapping(BMesh *bm_src, BMesh *bm_dst, const char htype);
+static void *BM_mesh_mapping(BMesh *bm_src, BMesh *bm_dst, const char htype);
 
 //--------------index transfer declarations ----------
 static void BM_mesh_cd_transfer_array(CustomData *cd_src, BMElem **array_src, int array_src_count,
@@ -70,15 +88,15 @@
                                      const struct ReplaceLayerInfo *replace_info);
 
 //--------------topology transfer declarations ------
-void BM_mesh_cd_transfer_mapped(CustomData *cd_src, BMElem **array_src, int array_src_count,
-                                CustomData *cd_dst, BMElem **array_dst, int array_dst_count,
-                                const int layer_type, const struct ReplaceLayerInfo *replace_info,
-                                int *index_mapping);
+static void BM_mesh_cd_transfer_mapped(CustomData *cd_src, BMElem **array_src, int array_src_count,
+                                       CustomData *cd_dst, BMElem **array_dst, int array_dst_count,
+                                       const int layer_type, const struct ReplaceLayerInfo *replace_info,
+                                       int *index_mapping);
 
 static void BM_mesh_transfer_mapped(BMesh *bm_src, BMesh *bm_dst, const char htype, const int layer_type,
                                     const struct ReplaceLayerInfo *replace_info);
 
-void set_loop_indices(BMesh *bm);
+static void set_loop_indices(BMesh *bm);
 
 //---------------Definitions---------------------------
 
@@ -241,11 +259,6 @@
 	CustomData *cd_dst;
 	CustomData *cd_src;
 
-	int b;					//counter
-	BMFace *f;
-	BMLoop *l;
-	BMIter fiter, liter;
-
 	switch (htype) {
 		case BM_VERT:
 			array_src = BM_iter_as_arrayN(bm_src, BM_VERTS_OF_MESH, NULL, &array_src_len, NULL, 0);
@@ -253,25 +266,11 @@
 			break;
 
 		case BM_LOOP:
-			array_src_len = bm_src->totloop;
-			array_dst_len = bm_dst->totloop;
-			array_src = MEM_mallocN(sizeof(*array_src) * array_src_len, "array_src bmesh_data_transfer.c");
-            array_dst = MEM_mallocN(sizeof(*array_dst) * array_dst_len, "array_dst bmesh_data_transfer.c");
-			b = 0;
-			BM_ITER_MESH (f, &fiter, bm_src, BM_FACES_OF_MESH) {
-				BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
-					array_src[b] = (BMElem*) l;
-					b++;
-				}
-			}
+			array_src = MEM_mallocN(sizeof(*array_src) * bm_src->totloop, "array_src bmesh_data_transfer.c");
+            array_dst = MEM_mallocN(sizeof(*array_dst) * bm_dst->totloop, "array_dst bmesh_data_transfer.c");
 
-			b = 0;
-			BM_ITER_MESH (f, &fiter, bm_dst, BM_FACES_OF_MESH) {
-				BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
-					array_dst[b] = (BMElem*) l;
-					b++;
-				}
-			}
+			array_src_len = BM_iter_loops_as_array(bm_src, array_src, bm_src->totloop);
+			array_dst_len = BM_iter_loops_as_array(bm_src, array_dst, bm_dst->totloop);
 
 			break;
 	}
@@ -289,13 +288,16 @@
 			cd_src = &bm_src->edata;
 			cd_dst = &bm_dst->edata;
 			break;
+
+		case BM_FACE:
+			cd_src = &bm_src->pdata;
+			cd_dst = &bm_dst->pdata;
+			break;
 #endif
 		case BM_LOOP:
 			cd_src = &bm_src->ldata;
 			cd_dst = &bm_dst->ldata;
 			break;
-
-			///should the pdata be associated to the FACES_OF_MESH?
 	}
 
 	BM_mesh_cd_transfer_array(cd_src, array_src, array_src_len, cd_dst, array_dst, array_dst_len, layer_type, replace_info);
@@ -303,7 +305,7 @@
 
 //--------------topology transfer definitions---------
 
-void BM_mesh_cd_transfer_mapped(CustomData *cd_src, BMElem **array_src, int array_src_count,
+static void BM_mesh_cd_transfer_mapped(CustomData *cd_src, BMElem **array_src, int array_src_count,
                                 CustomData *cd_dst, BMElem **array_dst, int array_dst_count,
                                 const int layer_type, const struct ReplaceLayerInfo *replace_info,
                                 int *index_mapping)
@@ -345,6 +347,10 @@
 			}
 		}
 	}
+
+	else {
+		printf("%s: %d != %d\n", __func__, array_src_count, array_dst_count);
+	}
 }
 
 static void BM_mesh_transfer_mapped(BMesh *bm_src, BMesh *bm_dst, const char htype, const int layer_type,
@@ -382,13 +388,13 @@
 
 			BM_ITER_MESH (f, &fiter, bm_src, BM_FACES_OF_MESH) {
 				BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
-					array_src[l->head.index] = (BMElem*) l;
+					array_src[BM_elem_index_get(l)] = (BMElem*) l;
 				}
 			}
 
 			BM_ITER_MESH (f, &fiter, bm_dst, BM_FACES_OF_MESH) {
 				BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
-					array_dst[l->head.index] = (BMElem*) l;
+					array_dst[BM_elem_index_get(l)] = (BMElem*) l;
 				}
 			}
 			index_mapping = BM_mesh_mapping(bm_src, bm_dst, BM_VERT);
@@ -397,6 +403,7 @@
 			break;
 
 		default:
+			BLI_assert(0);
 			break;
 	}
 
@@ -412,6 +419,10 @@
 			cd_src = &bm_src->edata;
 			cd_dst = &bm_dst->edata;
 			break;
+		case BM_FACE:
+			cd_src = &bm_src->pdata;
+			cd_dst = &bm_dst->pdata;
+			break;
 #endif
 
 		case BM_LOOP:
@@ -420,9 +431,8 @@
 			break;
 
 		default:
+			BLI_assert(0);
 			break;
-
-			///should the pdata be associated to the FACES_OF_MESH?
 	}
 
 	BM_mesh_cd_transfer_mapped(cd_src, array_src, array_src_len, cd_dst, array_dst, array_dst_len, layer_type, replace_info,
@@ -435,7 +445,7 @@
 
 //---------------helping functions definitions-----------
 
-void *BM_mesh_mapping(BMesh *bm_src, BMesh *bm_dst, const char htype)
+static void *BM_mesh_mapping(BMesh *bm_src, BMesh *bm_dst, const char htype)
 {
 	BMVert *v_src, *v_dst;
 	BMIter iter;
@@ -464,7 +474,7 @@
 
 				v_src = BKE_bmbvh_find_vert_closest(bmtree_src, v_dst->co, FLT_MAX);
 				if (v_src != NULL) {
-					index_mapping[a] = v_src->head.index;
+					index_mapping[a] = BM_elem_index_get(v_src);
 				}
 				else {
 					index_mapping[a] = -1;
@@ -487,13 +497,13 @@
 					BM_ITER_ELEM (l_dst, &liter, f_dst, BM_LOOPS_OF_FACE) {
 						l_src = BM_face_find_best_tan_match_loop(f_src, l_dst);
 						if (l_src != NULL) {
-							index_mapping[l_dst->head.index] = l_src->head.index;
+							index_mapping[BM_elem_index_get(l_dst)] = BM_elem_index_get(l_src);
 						}
 					}
 				}
 				else {
 					BM_ITER_ELEM (l_dst, &liter, f_dst, BM_LOOPS_OF_FACE) {
-						index_mapping[l_dst->head.index] = -1;
+						index_mapping[BM_elem_index_get(l_dst)] = -1;
 					}
 				}
 			}
@@ -510,6 +520,7 @@
 		default:
 			BKE_bmbvh_free(bmtree_src);
 			MEM_freeN(em_src);
+			BLI_assert(0);
 			return false;
 			//break;
 	}
@@ -521,7 +532,7 @@
 	return index_mapping;
 }
 
-void set_loop_indices(BMesh *bm)
+static void set_loop_indices(BMesh *bm)
 {
 	BMLoop *l;
 	BMFace *f;
@@ -532,23 +543,31 @@
 	b = 0;
 	BM_ITER_MESH (f, &fiter, bm, BM_FACES_OF_MESH) {
 		BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
-			l->head.index = b;
+			BM_elem_index_set(l, b);
 			b++;
 		}
 	}
 }
 
-int* BM_transform_index_mapping(BMesh *bm_src,BMElem **array_dst, int array_dst_count, int *index_mapping_in,
-                                int htype_from, int htype_to)
+static int *BM_transform_index_mapping(BMesh *bm_src, BMElem **array_dst, int array_dst_count, int *index_mapping_in,
+                                       int htype_from, int htype_to)
 {
 	int i;
 	BMElem *ele_dst;
 	BMElem *ele_src;
 	BMLoop *l_dst;
 	BMVert *v_src;
+
+	int v_src_count = bm_src->totvert;
+	BMVert **v_array_src = MEM_mallocN(v_src_count * sizeof(*v_array_src),
+	                                   "v_array_src bmesh_data_transfer.c");	//lookup array for the vertices to avoid
+																				//using the BM_vert_at_index per element
+
 	int *index_mapping_out = MEM_mallocN(array_dst_count * sizeof(*index_mapping_out),
 	                                     "index_mapping_out bmesh_data_transfer.c");
 
+	BM_iter_as_array(bm_src, BM_VERTS_OF_MESH, NULL, (void **)v_array_src, bm_src->totvert);
+
 	if (htype_from == BM_VERT && htype_to == BM_LOOP) {
 		for (i = 0; i < array_dst_count; i++) {	//for each destination loop
 			int v_src_index;
@@ -566,7 +585,8 @@
 

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list