[Bf-blender-cvs] [3fbbb27] fracture_modifier: replaced Shard pointer to arraypointer by ListBase for FracMesh->shard_map in DNA, for fracture modifier

Martin Felke noreply at git.blender.org
Fri Oct 10 16:30:12 CEST 2014


Commit: 3fbbb2752ceb0af654be6617d564c73baf056aa0
Author: Martin Felke
Date:   Fri Oct 10 15:55:50 2014 +0200
Branches: fracture_modifier
https://developer.blender.org/rB3fbbb2752ceb0af654be6617d564c73baf056aa0

replaced Shard pointer to arraypointer by ListBase for FracMesh->shard_map in DNA, for fracture modifier

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

M	source/blender/blenkernel/intern/fracture.c
M	source/blender/blenloader/intern/readfile.c
M	source/blender/blenloader/intern/writefile.c
M	source/blender/makesdna/DNA_fracture_types.h
M	source/blender/modifiers/intern/MOD_fracture.c

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

diff --git a/source/blender/blenkernel/intern/fracture.c b/source/blender/blenkernel/intern/fracture.c
index 5b57ef7..170707a 100644
--- a/source/blender/blenkernel/intern/fracture.c
+++ b/source/blender/blenkernel/intern/fracture.c
@@ -81,8 +81,7 @@ static void parse_cell_neighbors(cell c, int *neighbors, int totpoly);
 
 static void add_shard(FracMesh *fm, Shard *s)
 {
-	fm->shard_map = MEM_reallocN(fm->shard_map, sizeof(Shard *) * (fm->shard_count + 1));
-	fm->shard_map[fm->shard_count] = s;
+	BLI_addtail(&fm->shard_map, s);
 	s->shard_id = fm->shard_count;
 	fm->shard_count++;
 }
@@ -134,7 +133,7 @@ static void shard_boundbox(Shard *s, float r_loc[3], float r_size[3])
 }
 
 
-static int shard_sortsize(const void *s1, const void *s2, void *context)
+static int shard_sortsize(const void *s1, const void *s2, void* UNUSED(context))
 {
 	Shard **sh1 = (Shard **)s1;
 	Shard **sh2 = (Shard **)s2;
@@ -282,7 +281,8 @@ float BKE_shard_calc_minmax(Shard *shard)
 /*access shard directly by index / id*/
 Shard *BKE_shard_by_id(FracMesh *mesh, ShardID id, DerivedMesh *dm) {
 	if ((id < mesh->shard_count) && (id >= 0)) {
-		return mesh->shard_map[id];
+		//return mesh->shard_map[id];
+		return (Shard *)BLI_findlink(&mesh->shard_map, id);
 	}
 	else if (id == -1)
 	{
@@ -351,7 +351,7 @@ FracMesh *BKE_create_fracture_container(void)
 	
 	fmesh = MEM_mallocN(sizeof(FracMesh), __func__);
 	
-	fmesh->shard_map = MEM_mallocN(sizeof(Shard *), __func__); /* allocate in chunks ?, better use proper blender functions for this*/
+//	fmesh->shard_map = MEM_mallocN(sizeof(Shard *), __func__); /* allocate in chunks ?, better use proper blender functions for this*/
 	fmesh->shard_count = 0;
 	fmesh->cancel = 0;
 	fmesh->running = 0;
@@ -513,7 +513,7 @@ static void parse_cells(cell *cells, int expected_shards, ShardID parent_id, Fra
 				tempresults[i] = s;
 				tempresults[i + 1] = s2;
 
-				BLI_qsort_r(tempresults, i + 1, sizeof(Shard *), shard_sortsize, i);
+				BLI_qsort_r(tempresults, i + 1, sizeof(Shard *), shard_sortsize, &i);
 
 				while (tempresults[j] == NULL && j < (i + 1)) {
 					/* ignore invalid shards */
@@ -583,9 +583,9 @@ static Shard *parse_cell(cell c)
 	int *neighbors = NULL;
 	int totpoly = 0, totloop = 0, totvert = 0;
 	float centr[3];
-	int shard_id;
+//	int shard_id;
 
-	shard_id = c.index;
+//	shard_id = c.index;
 
 	totvert = c.totvert;
 	if (totvert > 0) {
@@ -754,51 +754,24 @@ void BKE_fracture_shard_by_points(FracMesh *fmesh, ShardID id, FracPointCloud *p
 
 void BKE_fracmesh_free(FracMesh *fm, bool doCustomData)
 {
-	int i = 0, count;
-
 	if (fm == NULL) {
 		return;
 	}
 
-	if (fm->shard_map == NULL) {
-		fm->shard_count = 0;
-		return;
-	}
-
-	count = fm->shard_count;
-
-	for (i = 0; i < count; i++) {
-		Shard *s = fm->shard_map[i];
-		if (s != NULL) {
-			BKE_shard_free(s, doCustomData);
-		}
-		fm->shard_count--;
-	}
-
-	if ((fm->shard_map != NULL)) {
-		MEM_freeN(fm->shard_map);
-		fm->shard_map = NULL;
+	while (fm->shard_map.first) {
+		Shard* s = (Shard*)fm->shard_map.first;
+		BLI_remlink_safe(&fm->shard_map, s);
+		BKE_shard_free(s, doCustomData);
 	}
 }
 
 
 /* DerivedMesh */
-
-void BKE_fracture_release_dm(FractureModifierData *fmd)
-{
-	if (fmd->dm) {
-		fmd->dm->needsFree = 1;
-		fmd->dm->release(fmd->dm);
-		fmd->dm = NULL;
-	}
-}
-
 static DerivedMesh *create_dm(FractureModifierData *fmd, bool doCustomData)
 {
 	int shard_count = fmd->shards_to_islands ? BLI_countlist(&fmd->islandShards) : fmd->frac_mesh->shard_count;
-	Shard **shard_map = fmd->frac_mesh->shard_map;
-	Shard *shard;
-	int s;
+	ListBase *shardlist;
+	Shard *shard, *s;
 	
 	int num_verts, num_polys, num_loops;
 	int vertstart, polystart, loopstart;
@@ -810,20 +783,17 @@ static DerivedMesh *create_dm(FractureModifierData *fmd, bool doCustomData)
 	num_verts = num_polys = num_loops = 0;
 
 	if (fmd->shards_to_islands) {
-		for (s = 0; s < shard_count; ++s) {
-			shard = BLI_findlink(&fmd->islandShards, s);
-			num_verts += shard->totvert;
-			num_polys += shard->totpoly;
-			num_loops += shard->totloop;
+		for (s = fmd->islandShards.first; s; s = s->next) {
+			num_verts += s->totvert;
+			num_polys += s->totpoly;
+			num_loops += s->totloop;
 		}
 	}
 	else {
-		for (s = 0; s < shard_count; ++s) {
-			shard = shard_map[s];
-
-			num_verts += shard->totvert;
-			num_polys += shard->totpoly;
-			num_loops += shard->totloop;
+		for (s = fmd->frac_mesh->shard_map.first; s; s = s->next) {
+			num_verts += s->totvert;
+			num_polys += s->totpoly;
+			num_loops += s->totloop;
 		}
 	}
 	
@@ -838,27 +808,28 @@ static DerivedMesh *create_dm(FractureModifierData *fmd, bool doCustomData)
 			s = BLI_findlink(&fmd->islandShards, 0);
 		}
 		else {
-			s = shard_map[0];
+			s = BLI_findlink(&fmd->frac_mesh->shard_map, 0);
 		}
 
 		CustomData_merge(&s->vertData, &result->vertData, CD_MASK_MDEFORMVERT, CD_CALLOC, num_verts);
 		CustomData_merge(&s->polyData, &result->polyData, CD_MASK_MTEXPOLY, CD_CALLOC, num_polys);
 		CustomData_merge(&s->loopData, &result->loopData, CD_MASK_MLOOPUV, CD_CALLOC, num_loops);
 	}
-	
+
 	vertstart = polystart = loopstart = 0;
-	for (s = 0; s < shard_count; ++s) {
+	if (fmd->shards_to_islands) {
+		shardlist = &fmd->islandShards;
+	}
+	else
+	{
+		shardlist = &fmd->frac_mesh->shard_map;
+	}
+
+	for (shard = shardlist->first; shard; shard = shard->next)
+	{
 		MPoly *mp;
 		MLoop *ml;
 		int i;
-
-		if (fmd->shards_to_islands) {
-			/* may be inefficent... but else would need to do another loop */
-			shard = BLI_findlink(&fmd->islandShards, s);
-		}
-		else {
-			shard = shard_map[s];
-		}
 		
 		memcpy(mverts + vertstart, shard->mvert, shard->totvert * sizeof(MVert));
 		memcpy(mpolys + polystart, shard->mpoly, shard->totpoly * sizeof(MPoly));
diff --git a/source/blender/blenloader/intern/readfile.c b/source/blender/blenloader/intern/readfile.c
index b1e6e74..3dca493 100644
--- a/source/blender/blenloader/intern/readfile.c
+++ b/source/blender/blenloader/intern/readfile.c
@@ -4995,13 +4995,11 @@ static void direct_link_modifiers(FileData *fd, ListBase *lb)
 				Shard *s;
 				int count = 0;
 
-				fm->shard_map = newdataadr(fd, fm->shard_map);
-				for (i = 0; i < fm->shard_count; i++) {
-					fm->shard_map[i] = newdataadr(fd, fm->shard_map[i]);
-					read_shard(fd, &(fm->shard_map[i]));
+				link_list(fd, &fm->shard_map);
+				for (s = fm->shard_map.first; s; s = s->next) {
+					read_shard(fd, &s);
 				}
 
-				fmd->frac_mesh = fm;
 				fmd->dm = NULL;
 				fmd->visible_mesh = NULL;
 
diff --git a/source/blender/blenloader/intern/writefile.c b/source/blender/blenloader/intern/writefile.c
index 8202b5d..54292a7 100644
--- a/source/blender/blenloader/intern/writefile.c
+++ b/source/blender/blenloader/intern/writefile.c
@@ -1639,7 +1639,6 @@ static void write_modifiers(WriteData *wd, ListBase *modbase)
 		}
 
 		else if (md->type==eModifierType_Fracture) {
-			int i = 0;
 			FractureModifierData *fmd = (FractureModifierData*)md;
 			FracMesh* fm = fmd->frac_mesh;
 
@@ -1651,12 +1650,8 @@ static void write_modifiers(WriteData *wd, ListBase *modbase)
 				if (fm->running == 0)
 				{
 					writestruct(wd, DATA, "FracMesh", 1, fm);
-					writedata(wd, DATA, sizeof(Shard*) * fm->shard_count, fm->shard_map);
-					for (i = 0; i < fm->shard_count; i++) {
-						Shard *s = fm->shard_map[i];
-						/* next, prev not necessary in this case, because we use an array here */
-						s->next = NULL;
-						s->prev = NULL;
+
+					for (s = fm->shard_map.first; s; s = s->next) {
 						write_shard(wd, s);
 					}
 
diff --git a/source/blender/makesdna/DNA_fracture_types.h b/source/blender/makesdna/DNA_fracture_types.h
index 190f450..2771a06 100644
--- a/source/blender/makesdna/DNA_fracture_types.h
+++ b/source/blender/makesdna/DNA_fracture_types.h
@@ -72,7 +72,7 @@ typedef struct Shard {
 } Shard;
 
 typedef struct FracMesh {
-	Shard **shard_map;      /* groups mesh elements to islands, generated by fracture itself */
+	ListBase shard_map;     /* groups mesh elements to islands, generated by fracture itself */
 	int shard_count;        /* how many islands we have */
 	short cancel;           /* whether the process is cancelled (from the job, ugly, but this way we dont need the entire modifier) */
 	short running;          /* whether the process is currently in progress, so the modifier wont be touched from the main thread */
diff --git a/source/blender/modifiers/intern/MOD_fracture.c b/source/blender/modifiers/intern/MOD_fracture.c
index 3a6200d..a12f71a 100644
--- a/source/blender/modifiers/intern/MOD_fracture.c
+++ b/source/blender/modifiers/intern/MOD_fracture.c
@@ -291,7 +291,7 @@ static void growCluster(FractureModifierData *fmd, Shard *seed, int sindex, List
 	for (i = 0; i < count; i++) {
 		Shard *neighbor;
 		int index = nearest[i].index;
-		neighbor = fmd->frac_mesh->shard_map[index];
+		neighbor = (Shard *)BLI_findlink(&fmd->frac_mesh->shard_map, index);
 		if (neighbor->cluster_colors[0] == -1) {
 			neighbor->cluster_colors[0] = sindex;
 		}
@@ -315,11 +315,10 @@ static void doClusters(FractureModifierData *fmd, int levels)
 	lbVisit.last = NULL;
 
 	for (j = 0; j < levels; j++) {
-		Shard **seeds;
+		Shard **seeds, *s;
 		int seed_count;
 		/*prepare shard as list*/
-		for (i = 0; i < fmd->frac_mesh->shard_count; i++) {
-			Shard *s = fmd->frac_mesh->shard_map[i];
+		for (s = fmd->frac_mesh->shard_map.first; s; s = s->next) {
 			s->cluster_colors = MEM_mallocN(sizeof(int) * levels, "cluster_colors");
 			for (k = 0; k < levels; k++)
 			{
@@ -342,7 +341,7 @@ static void doClusters(FractureModifierData *fmd, int levels)
 		for (k = 0; k < seed_count; k++) {
 			int color = k;
 			int which_index = k * (int)(fmd->frac_mesh->shard_count / seed_count);
-			Shard *which = fmd->frac_mesh->shard_map[which_index];
+			Shard *which = (Shard *)BLI_findlink(&fmd->frac_mesh->shard_map, which_index);
 			which->cluster_colors[j] = color;
 			BLI_addtail(&lbVisit, which);
 			seeds[k] = which;
@@ -1344,8 +1343,7 @@ static void bm_mesh_hflag_flush_vert(BMesh *bm, const char hflag)
 	}
 }
 
-void mesh_separate_loose_partition(FractureModifierData *rmd, Object *ob, BMesh *bm_work, BMVert **orig_work, DerivedMesh *dm);
-void mesh_separate_loose_partition(Fracture

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list