[Bf-blender-cvs] [14a023a] fracture_modifier: refactoring of FractureModifier: split very long functions into more, shorter ones

Martin Felke noreply at git.blender.org
Tue Mar 17 20:48:32 CET 2015


Commit: 14a023abddcd27f8084945deb0e7609bebf54d7e
Author: Martin Felke
Date:   Tue Mar 17 20:48:03 2015 +0100
Branches: fracture_modifier
https://developer.blender.org/rB14a023abddcd27f8084945deb0e7609bebf54d7e

refactoring of FractureModifier: split very long functions into more, shorter ones

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

M	source/blender/blenkernel/intern/fracture.c
M	source/blender/blenkernel/intern/fracture_util.c
M	source/blender/blenkernel/intern/rigidbody.c
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 50159b9..b5c022a 100644
--- a/source/blender/blenkernel/intern/fracture.c
+++ b/source/blender/blenkernel/intern/fracture.c
@@ -144,17 +144,6 @@ static void shard_boundbox(Shard *s, float r_loc[3], float r_size[3])
 	r_size[2] = (max[2] - min[2]) / 2.0f;
 }
 
-#if 0
-static float shard_size(Shard* s)
-{
-	float size[3], loc[3];
-	shard_boundbox(s, loc, size);
-
-	return size[0] * size[1] * size[2];
-}
-#endif
-
-
 static int shard_sortsize(const void *s1, const void *s2, void* UNUSED(context))
 {
 	Shard **sh1 = (Shard **)s1;
@@ -189,20 +178,6 @@ Shard *BKE_custom_data_to_shard(Shard *s, DerivedMesh *dm)
 	CustomData_add_layer(&s->loopData, CD_MLOOPUV, CD_DUPLICATE, CustomData_get_layer(&dm->loopData, CD_MLOOPUV), s->totloop);
 	CustomData_add_layer(&s->polyData, CD_MTEXPOLY, CD_DUPLICATE, CustomData_get_layer(&dm->polyData, CD_MTEXPOLY), s->totpoly);
 
-	/*XXX TODO how do i use customdata PROPERLY ? */
-
-	/*CustomData_copy(&dm->vertData, &s->vertData, CD_MASK_MDEFORMVERT, CD_CALLOC, s->totvert);
-	   CustomData_copy_data(&dm->vertData, &s->vertData,
-	                     0, 0, s->totvert);
-
-	   CustomData_copy(&dm->loopData, &s->loopData, CD_MASK_MLOOPUV, CD_CALLOC, s->totloop);
-	   CustomData_copy_data(&dm->loopData, &s->loopData,
-	                     0, 0, s->totloop);
-
-	   CustomData_copy(&dm->polyData, &s->polyData, CD_MASK_MTEXPOLY, CD_CALLOC, s->totpoly);
-	   CustomData_copy_data(&dm->polyData, &s->polyData,
-	                     0, 0, s->totpoly);*/
-
 	return s;
 }
 
@@ -382,284 +357,298 @@ FracMesh *BKE_create_fracture_container(void)
 	return fmesh;
 }
 
-
-/* parse the voro++ cell data */
-static void parse_cells(cell *cells, int expected_shards, ShardID parent_id, FracMesh *fm, int algorithm, Object *obj, DerivedMesh *dm, short inner_material_index, float mat[4][4], int num_cuts, float fractal, bool smooth, int num_levels)
+static void handle_fast_bisect(FracMesh *fm, int expected_shards, int algorithm, BMesh* bm_parent, float obmat[4][4],
+                               float centroid[3], short inner_material_index, int parent_id, Shard **tempshards, Shard ***tempresults)
 {
-	/*Parse voronoi raw data*/
 	int i = 0;
-	Shard *s = NULL, *p = BKE_shard_by_id(fm, parent_id, dm);
-	float obmat[4][4]; /* use unit matrix for now */
-	float centroid[3];
-	BMesh *bm_parent = NULL;
-	DerivedMesh *dm_parent = NULL;
-	DerivedMesh *dm_p = NULL;
-	Shard **tempshards;
-	Shard **tempresults;
-	int max_retries = 20;
 
-	tempshards = MEM_mallocN(sizeof(Shard *) * expected_shards, "tempshards");
-	tempresults = MEM_mallocN(sizeof(Shard *) * expected_shards, "tempresults");
+	for (i = 0; i < expected_shards; i++) {
+		Shard *s = NULL;
+		Shard *s2 = NULL;
+		Shard *t;
+		int index = 0;
 
-	p->flag = 0;
-	p->flag |= SHARD_FRACTURED;
-	unit_m4(obmat);
+		if (fm->cancel == 1) {
+			break;
+		}
 
-	if ((algorithm == MOD_FRACTURE_BOOLEAN) || (algorithm == MOD_FRACTURE_BOOLEAN_FRACTAL)) {
-		MPoly *mpoly, *mp;
-		int totpoly, i;
+		printf("Processing shard: %d\n", i);
+		t = tempshards[i];
 
-		dm_parent = BKE_shard_create_dm(p, true);
-		mpoly = dm_parent->getPolyArray(dm_parent);
-		totpoly = dm_parent->getNumPolys(dm_parent);
-		for (i = 0, mp = mpoly; i < totpoly; i++, mp++) {
-			mp->flag &= ~ME_FACE_SEL;
+		if (t != NULL) {
+			t->parent_id = parent_id;
+			t->flag = SHARD_INTACT;
 		}
-	}
-	else if (algorithm == MOD_FRACTURE_BISECT || algorithm == MOD_FRACTURE_BISECT_FILL ||
-	         algorithm == MOD_FRACTURE_BISECT_FAST || algorithm == MOD_FRACTURE_BISECT_FAST_FILL)
-	{
-		bm_parent = shard_to_bmesh(p);
-		copy_v3_v3(centroid, p->centroid);
-	}
 
-	for (i = 0; i < expected_shards; i++) {
-		if (fm->cancel == 1) {
+		if (t == NULL || t->totvert == 0 || t->totloop == 0 || t->totpoly == 0) {
+			/* invalid shard, stop parsing*/
 			break;
 		}
 
-		printf("Parsing shard: %d\n", i);
-		s = parse_cell(cells[i]);
-		tempshards[i] = s;
-		tempresults[i] = NULL;
-		fm->progress_counter++;
-	}
+		index = (int)(BLI_frand() * (t->totpoly - 1));
+		if (index == 0) {
+			index = 1;
+		}
 
-	if (algorithm != MOD_FRACTURE_BISECT_FAST && algorithm != MOD_FRACTURE_BISECT_FAST_FILL) {
-		for (i = 0; i < expected_shards; i++) {
-			Shard *t;
-			if (fm->cancel == 1)
-				break;
+		printf("Bisecting cell %d...\n", i);
+		printf("Bisecting cell %d...\n", i + 1);
 
-			printf("Processing shard: %d\n", i);
-			t = tempshards[i];
+		s = BKE_fracture_shard_bisect(bm_parent, t, obmat, algorithm == MOD_FRACTURE_BISECT_FAST_FILL, false, true, index, centroid, inner_material_index);
+		s2 = BKE_fracture_shard_bisect(bm_parent, t, obmat, algorithm == MOD_FRACTURE_BISECT_FAST_FILL, true, false, index, centroid, inner_material_index);
 
-			if (t != NULL) {
-				t->parent_id = parent_id;
-				t->flag = SHARD_INTACT;
-			}
+		if (s != NULL && s2 != NULL && tempresults != NULL) {
+			int j = 0;
 
-			if (t == NULL || t->totvert == 0 || t->totloop == 0 || t->totpoly == 0) {
-				/* invalid shard, stop parsing */
-				break;
-			}
+			fm->progress_counter++;
+
+			s->parent_id = parent_id;
+			s->flag = SHARD_INTACT;
 
-			/* XXX TODO, need object for material as well, or atleast a material index... */
-			if (algorithm == MOD_FRACTURE_BOOLEAN) {
-				s = BKE_fracture_shard_boolean(obj, dm_parent, t, inner_material_index, 0, 0.0f, NULL, NULL, 0.0f, false, 0);
+			s2->parent_id = parent_id;
+			s2->flag = SHARD_INTACT;
+
+			if (bm_parent != NULL) {
+				BM_mesh_free(bm_parent);
+				bm_parent = NULL;
 			}
-			else if (algorithm == MOD_FRACTURE_BOOLEAN_FRACTAL) {
-				/* physics shard and fractalized shard, so we need to booleanize twice */
-				/* and we need both halves, so twice again */
-				Shard *s2 = NULL;
-				int index = 0;
-
-				/*continue with "halves", randomly*/
-				if (i == 0) {
-					dm_p = dm_parent;
-				}
 
-				while (s == NULL || s2 == NULL) {
+			(*tempresults)[i] = s;
+			(*tempresults)[i + 1] = s2;
 
-					float radius;
-					float size[3];
-					float eul[3];
-					float loc[3];
-					float one[3] = {1.0f, 1.0f, 1.0f};
-					float matrix[4][4];
+			BLI_qsort_r(*tempresults, i + 1, sizeof(Shard *), shard_sortsize, &i);
 
-					/*make a plane as cutter*/
-					BKE_object_dimensions_get(obj, size);
-					radius = MAX3(size[0], size[1], size[2]);
+			while ((*tempresults)[j] == NULL && j < (i + 1)) {
+				/* ignore invalid shards */
+				j++;
+			}
 
-					loc[0] = (BLI_frand() - 0.5f) * size[0];
-					loc[1] = (BLI_frand() - 0.5f) * size[1];
-					loc[2] = (BLI_frand() - 0.5f) * size[2];
+			/* continue splitting if not all expected shards exist yet */
+			if ((i + 2) < expected_shards) {
+				bm_parent = shard_to_bmesh((*tempresults)[j]);
+				copy_v3_v3(centroid, (*tempresults)[j]->centroid);
 
-					eul[0] = BLI_frand() * M_PI;
-					eul[1] = BLI_frand() * M_PI;
-					eul[2] = BLI_frand() * M_PI;
+				BKE_shard_free((*tempresults)[j], true);
+				(*tempresults)[j] = NULL;
+			}
+			i++;
+		}
+	}
+}
 
-					//printf("(%f %f %f) (%f %f %f) \n", loc[0], loc[1], loc[2], eul[0], eul[1], eul[2]);
+static void handle_boolean_fractal(Shard* s, Shard* t, int expected_shards, DerivedMesh* dm_parent, Object *obj, short inner_material_index,
+                                   int num_cuts, float fractal, int num_levels, bool smooth,int parent_id, int* i, Shard ***tempresults)
+{
+	/* physics shard and fractalized shard, so we need to booleanize twice */
+	/* and we need both halves, so twice again */
+	Shard *s2 = NULL;
+	int index = 0;
+	int max_retries = 20;
+	DerivedMesh *dm_p = NULL;
 
-					loc_eul_size_to_mat4(matrix, loc, eul, one);
+	/*continue with "halves", randomly*/
+	if ((*i) == 0) {
+		dm_p = dm_parent;
+	}
 
-					/*visual shards next, fractalized cuts */
-					s = BKE_fracture_shard_boolean(obj, dm_p, t, inner_material_index, num_cuts,fractal, &s2, matrix, radius, smooth, num_levels);
+	while (s == NULL || s2 == NULL) {
 
-					if (index < max_retries)
-					{
-						printf("Retrying...%d\n", index);
-						index++;
-					}
-					else if (s == NULL || s2 == NULL)
-					{
-						i++;
-						break;
-					}
-				}
+		float radius;
+		float size[3];
+		float eul[3];
+		float loc[3];
+		float one[3] = {1.0f, 1.0f, 1.0f};
+		float matrix[4][4];
 
-				if ((s != NULL) && (s2 != NULL)) {
-					int j = 0; //, k = 0;
-					//float size_max = 0;
+		/*make a plane as cutter*/
+		BKE_object_dimensions_get(obj, size);
+		radius = MAX3(size[0], size[1], size[2]);
 
-					s->parent_id = parent_id;
-					s->flag = SHARD_INTACT;
-					tempresults[i+1] = s;
+		loc[0] = (BLI_frand() - 0.5f) * size[0];
+		loc[1] = (BLI_frand() - 0.5f) * size[1];
+		loc[2] = (BLI_frand() - 0.5f) * size[2];
 
-					s2->parent_id = parent_id;
-					s2->flag = SHARD_INTACT;
-					tempresults[i] = s2;
+		eul[0] = BLI_frand() * M_PI;
+		eul[1] = BLI_frand() * M_PI;
+		eul[2] = BLI_frand() * M_PI;
 
-					BLI_qsort_r(tempresults, i + 1, sizeof(Shard *), shard_sortsize, &i);
-					while (tempresults[j] == NULL && j < (i + 1)) {
-						/* ignore invalid shards */
-						j++;
-					}
-#if 0
-					/*search for biggest shard and keep aligned with fractalized one*/
-					for (k = 0; k < (i+2); k++)
-					{
-						float size;
-						if (tempresults[k] != NULL)
-						{
-							size = shard_size(tempresults[k]);
-							if (size > size_max)
-							{
-								j = k;
-								size_max = size;
-							}
-						}
-					}
-#endif
-					/* continue splitting if not all expected shards exist yet */
-					if ((i + 2) < expected_shards) {
+		//printf("(%f %f %f) (%f %f %f) \n", loc[0], loc[1], loc[2], eul[0], eul[1], eul[2]);
 
-						Shard *p = tempresults[j];
+		loc_eul_size_to_mat4(matrix, loc, eul, one);
 
-						if (dm_p != dm_parent && dm_p != NULL) {
-							dm_p->needsFree = 1;
-							dm_p->release(dm_p);
-						}
+		/*visual shards next, fractalized cuts */
+		s = BKE_fracture_shard_boolean(obj, dm_p, t, inner_material_index, num_cuts,fractal, &s2, matrix, radius, smooth, num_levels);
 
-						dm_p = BKE_shard_create_dm(p, true);
+		if (index < max_retries)
+		{
+			printf("Retrying...%d\n", index);
+			index++;
+		}
+		else if (s == NULL || s2 == NULL)
+		{
+			(*i)++;
+			break;
+	

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list