[Bf-blender-cvs] [c70d1a0] mesh-transfer-data: Mesh2Mesh mapping: switch to MemArena mem handling.

Bastien Montagne noreply at git.blender.org
Wed Oct 15 13:11:18 CEST 2014


Commit: c70d1a06d0d56b1ce70364262fa4924ee13f7107
Author: Bastien Montagne
Date:   Tue Oct 14 10:58:44 2014 +0200
Branches: mesh-transfer-data
https://developer.blender.org/rBc70d1a06d0d56b1ce70364262fa4924ee13f7107

Mesh2Mesh mapping: switch to MemArena mem handling.

===================================================================

M	source/blender/blenkernel/BKE_mesh_mapping.h
M	source/blender/blenkernel/intern/mesh_mapping.c

===================================================================

diff --git a/source/blender/blenkernel/BKE_mesh_mapping.h b/source/blender/blenkernel/BKE_mesh_mapping.h
index e60e420..b3410b6 100644
--- a/source/blender/blenkernel/BKE_mesh_mapping.h
+++ b/source/blender/blenkernel/BKE_mesh_mapping.h
@@ -182,6 +182,7 @@ typedef struct Mesh2MeshMapping {
 } Mesh2MeshMapping;
 
 /* Helpers! */
+void BKE_mesh2mesh_mapping_init(Mesh2MeshMapping *map, const int num_items);
 void BKE_mesh2mesh_mapping_free(Mesh2MeshMapping *map);
 
 /* TODO:
diff --git a/source/blender/blenkernel/intern/mesh_mapping.c b/source/blender/blenkernel/intern/mesh_mapping.c
index 0111740..409dad0 100644
--- a/source/blender/blenkernel/intern/mesh_mapping.c
+++ b/source/blender/blenkernel/intern/mesh_mapping.c
@@ -36,6 +36,7 @@
 #include "BLI_utildefines.h"
 #include "BLI_bitmap.h"
 #include "BLI_math.h"
+#include "BLI_memarena.h"
 
 #include "BKE_bvhutils.h"
 #include "BKE_customdata.h"
@@ -699,24 +700,23 @@ bool BKE_loop_island_compute_uv(struct DerivedMesh *dm, MeshIslands *r_islands)
 /** \name Mesh to mesh mapping
  * \{ */
 
-void BKE_mesh2mesh_mapping_free(Mesh2MeshMapping *map)
+void BKE_mesh2mesh_mapping_init(Mesh2MeshMapping *map, const int num_items)
 {
-	/* For now, we use mere MEM_mallocN, later we'll probably switch to memarena! */
-	int i = map->nbr_items;
+	MemArena *mem = BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE, __func__);
 
-	if (!i) {
-		return;
-	}
+	BKE_mesh2mesh_mapping_free(map);
 
-	while (i--) {
-		Mesh2MeshMappingItem *it = &map->items[i];
-		if (it->nbr_sources) {
-			MEM_freeN(it->indices_src);
-			MEM_freeN(it->weights_src);
-		}
-	}
+	map->items = BLI_memarena_alloc(mem, sizeof(*map->items) * (size_t)num_items);
+	map->nbr_items = num_items;
 
-	MEM_freeN(map->items);
+	map->mem = mem;
+}
+
+void BKE_mesh2mesh_mapping_free(Mesh2MeshMapping *map)
+{
+	if (map->mem) {
+		BLI_memarena_free((MemArena *)map->mem);
+	}
 
 	map->nbr_items = 0;
 	map->items = NULL;
@@ -724,14 +724,17 @@ void BKE_mesh2mesh_mapping_free(Mesh2MeshMapping *map)
 }
 
 static void bke_mesh2mesh_mapping_item_define(
-        Mesh2MeshMappingItem *mapit, const float hit_distance, const int island,
+        Mesh2MeshMapping *map, const int idx, const float hit_distance, const int island,
         const int nbr_sources, const int *indices_src, const float *weights_src)
 {
+	Mesh2MeshMappingItem *mapit = &map->items[idx];
+	MemArena *mem = map->mem;
+
 	if (nbr_sources) {
 		mapit->nbr_sources = nbr_sources;
-		mapit->indices_src = MEM_mallocN(sizeof(*mapit->indices_src) * (size_t)nbr_sources, __func__);
+		mapit->indices_src = BLI_memarena_alloc(mem, sizeof(*mapit->indices_src) * (size_t)nbr_sources);
 		memcpy(mapit->indices_src, indices_src, sizeof(*mapit->indices_src) * (size_t)nbr_sources);
-		mapit->weights_src = MEM_mallocN(sizeof(*mapit->weights_src) * (size_t)nbr_sources, __func__);
+		mapit->weights_src = BLI_memarena_alloc(mem, sizeof(*mapit->weights_src) * (size_t)nbr_sources);
 		memcpy(mapit->weights_src, weights_src, sizeof(*mapit->weights_src) * (size_t)nbr_sources);
 	}
 	else {
@@ -841,13 +844,12 @@ void BKE_dm2mesh_mapping_verts_compute(
 
 	BLI_assert(mode & M2MMAP_MODE_VERT);
 
-	r_map->items = MEM_mallocN(sizeof(*r_map->items) * (size_t)numverts_dst, __func__);
-	r_map->nbr_items = numverts_dst;
+	BKE_mesh2mesh_mapping_init(r_map, numverts_dst);
 
 	if (mode == M2MMAP_MODE_TOPOLOGY) {
 		BLI_assert(numverts_dst == dm_src->getNumVerts(dm_src));
 		for (i = 0; i < numverts_dst; i++) {
-			bke_mesh2mesh_mapping_item_define(&r_map->items[i], FLT_MAX, 0, 1, &i, &full_weight);
+			bke_mesh2mesh_mapping_item_define(r_map, i, FLT_MAX, 0, 1, &i, &full_weight);
 		}
 	}
 	else {
@@ -869,11 +871,11 @@ void BKE_dm2mesh_mapping_verts_compute(
 				                                              tmp_co, max_dist_sq);
 
 				if (nearest.index >= 0) {
-					bke_mesh2mesh_mapping_item_define(&r_map->items[i], hitdist, 0, 1, &nearest.index, &full_weight);
+					bke_mesh2mesh_mapping_item_define(r_map, i, hitdist, 0, 1, &nearest.index, &full_weight);
 				}
 				else {
 					/* No source for this dest vertex! */
-					bke_mesh2mesh_mapping_item_define(&r_map->items[i], FLT_MAX, 0, 0, NULL, NULL);
+					bke_mesh2mesh_mapping_item_define(r_map, i, FLT_MAX, 0, 0, NULL, NULL);
 				}
 			}
 		}
@@ -902,7 +904,7 @@ void BKE_dm2mesh_mapping_verts_compute(
 						const float dist_v1 = len_squared_v3v3(tmp_co, *v1cos);
 						const float dist_v2 = len_squared_v3v3(tmp_co, *v2cos);
 						const int index = (int)((dist_v1 > dist_v2) ? me->v2 : me->v1);
-						bke_mesh2mesh_mapping_item_define(&r_map->items[i], hitdist, 0, 1, &index, &full_weight);
+						bke_mesh2mesh_mapping_item_define(r_map, i, hitdist, 0, 1, &index, &full_weight);
 					}
 					else if (mode == M2MMAP_MODE_VERT_EDGEINTERP_NEAREST) {
 						int indices[2];
@@ -916,12 +918,12 @@ void BKE_dm2mesh_mapping_verts_compute(
 						CLAMP(weights[0], 0.0f, 1.0f);
 						weights[1] = 1.0f - weights[0];
 
-						bke_mesh2mesh_mapping_item_define(&r_map->items[i], hitdist, 0, 2, indices, weights);
+						bke_mesh2mesh_mapping_item_define(r_map, i, hitdist, 0, 2, indices, weights);
 					}
 				}
 				else {
 					/* No source for this dest vertex! */
-					bke_mesh2mesh_mapping_item_define(&r_map->items[i], FLT_MAX, 0, 0, NULL, NULL);
+					bke_mesh2mesh_mapping_item_define(r_map, i, FLT_MAX, 0, 0, NULL, NULL);
 				}
 			}
 
@@ -962,12 +964,11 @@ void BKE_dm2mesh_mapping_verts_compute(
 						                                mp_src, loops_src, (const float (*)[3])vcos_src, rayhit.co,
 						                                &tmp_buff_size, &vcos, false, &indices, &weights, true, NULL);
 
-						bke_mesh2mesh_mapping_item_define(&r_map->items[i], rayhit.dist, 0,
-						                                  nbr_sources, indices, weights);
+						bke_mesh2mesh_mapping_item_define(r_map, i, rayhit.dist, 0, nbr_sources, indices, weights);
 					}
 					else {
 						/* No source for this dest vertex! */
-						bke_mesh2mesh_mapping_item_define(&r_map->items[i], FLT_MAX, 0, 0, NULL, NULL);
+						bke_mesh2mesh_mapping_item_define(r_map, i, FLT_MAX, 0, 0, NULL, NULL);
 					}
 				}
 			}
@@ -993,7 +994,7 @@ void BKE_dm2mesh_mapping_verts_compute(
 							                                &tmp_buff_size, &vcos, false, &indices, &weights, false,
 							                                &index);
 
-							bke_mesh2mesh_mapping_item_define(&r_map->items[i], hitdist, 0, 1, &index, &full_weight);
+							bke_mesh2mesh_mapping_item_define(r_map, i, hitdist, 0, 1, &index, &full_weight);
 						}
 						else if (mode == M2MMAP_MODE_VERT_POLYINTERP_NEAREST) {
 							const int nbr_sources = bke_mesh2mesh_mapping_get_interp_poly_data(
@@ -1001,13 +1002,12 @@ void BKE_dm2mesh_mapping_verts_compute(
 							                                &tmp_buff_size, &vcos, false, &indices, &weights, true,
 							                                NULL);
 
-							bke_mesh2mesh_mapping_item_define(&r_map->items[i], hitdist, 0,
-							                                  nbr_sources, indices, weights);
+							bke_mesh2mesh_mapping_item_define(r_map, i, hitdist, 0, nbr_sources, indices, weights);
 						}
 					}
 					else {
 						/* No source for this dest vertex! */
-						bke_mesh2mesh_mapping_item_define(&r_map->items[i], FLT_MAX, 0, 0, NULL, NULL);
+						bke_mesh2mesh_mapping_item_define(r_map, i, FLT_MAX, 0, 0, NULL, NULL);
 					}
 				}
 			}
@@ -1038,13 +1038,12 @@ void BKE_dm2mesh_mapping_edges_compute(
 
 	BLI_assert(mode & M2MMAP_MODE_EDGE);
 
-	r_map->items = MEM_mallocN(sizeof(*r_map->items) * (size_t)numedges_dst, __func__);
-	r_map->nbr_items = numedges_dst;
+	BKE_mesh2mesh_mapping_init(r_map, numedges_dst);
 
 	if (mode == M2MMAP_MODE_TOPOLOGY) {
 		BLI_assert(numedges_dst == dm_src->getNumEdges(dm_src));
 		for (i = 0; i < numedges_dst; i++) {
-			bke_mesh2mesh_mapping_item_define(&r_map->items[i], FLT_MAX, 0, 1, &i, &full_weight);
+			bke_mesh2mesh_mapping_item_define(r_map, i, FLT_MAX, 0, 1, &i, &full_weight);
 		}
 	}
 	else {
@@ -1155,11 +1154,11 @@ void BKE_dm2mesh_mapping_edges_compute(
 						}
 					}
 					hitdist = len_v3v3(co_dst, co_src);
-					bke_mesh2mesh_mapping_item_define(&r_map->items[i], hitdist, 0, 1, &best_eidx_src, &full_weight);
+					bke_mesh2mesh_mapping_item_define(r_map, i, hitdist, 0, 1, &best_eidx_src, &full_weight);
 				}
 				else {
 					/* No source for this dest edge! */
-					bke_mesh2mesh_mapping_item_define(&r_map->items[i], FLT_MAX, 0, 0, NULL, NULL);
+					bke_mesh2mesh_mapping_item_define(r_map, i, FLT_MAX, 0, 0, NULL, NULL);
 				}
 			}
 
@@ -1181,11 +1180,11 @@ void BKE_dm2mesh_mapping_edges_compute(
 				                                              tmp_co, max_dist_sq);
 
 				if (nearest.index >= 0) {
-					bke_mesh2mesh_mapping_item_define(&r_map->items[i], hitdist, 0, 1, &nearest.index, &full_weight);
+					bke_mesh2mesh_mapping_item_define(r_map, i, hitdist, 0, 1, &nearest.index, &full_weight);
 				}
 				else {
 					/* No source for this dest edge! */
-					bke_mesh2mesh_mapping_item_define(&r_map->items[i], FLT_MAX, 0, 0, NULL, NULL);
+					bke_mesh2mesh_mapping_item_define(r_map, i, FLT_MAX, 0, 0, NULL, NULL);
 				}
 			}
 		}
@@ -1231,12 +1230,12 @@ void BKE_dm2mesh_mapping_edges_compute(
 						}
 					}
 					if (best_eidx_src >= 0) {
-						bke_mesh2mesh_mapping_item_define(&r_map->items[i], hitdist, 0, 1, &best_eidx_src, &full_weight);
+						bke_mesh2mesh_mapping_item_define(r_map, i, hitdist, 0, 1, &best_eidx_src, &full_weight);
 					}
 				}
 				else {
 					/* No source for this dest edge! */
-					bke_mesh2mesh_mapping_item_define(&r_map->items[i], FLT_MAX, 0, 0, NULL, NULL);
+					bke_mesh2mesh_mapping_item_define(r_map, i, FLT_MAX, 0, 0, NULL, NULL);
 				}
 			}
 
@@ -1275,13 +1274,12 @@ void BKE_dm2mesh_mapping_polys_compute(
 		}
 	}
 
-	r_map->items = MEM_mallocN(sizeof(*r_map->items) * (size_t)numpolys_dst, __func__);
-	r_map->nbr_items = numpolys_dst;
+	BKE_mesh2mesh_mapping_init(r_map, numpolys_dst);
 
 	if (mode == M2MMAP_MODE_TOPOLOGY) {
 		BLI_assert(numpolys_dst == dm_src->getNumPolys(dm_src));
 		for (i = 0; i < numpolys_dst; i++) {
-			bke_mesh2mesh_mapping_item_define(&r_map->items[i], FLT_MAX, 0, 1, &i, &full_weight);
+			bke_mesh2me

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list