[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [57516] branches/soc-2013-depsgraph_mt/ source/blender/blenkernel/intern/mball.c: Remove static variables from mball module

Sergey Sharybin sergey.vfx at gmail.com
Mon Jun 17 12:51:25 CEST 2013


Revision: 57516
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=57516
Author:   nazgul
Date:     2013-06-17 10:51:24 +0000 (Mon, 17 Jun 2013)
Log Message:
-----------
Remove static variables from mball module

Moved all the static variables into a PROCESS
structure which is now passing all over to where
static variables used to be used.

There's still one static variable which is
cubetable, but it's being initialized once
and then used read-only. Maybe we'll need
to move cubetable initialization to blender
startup, but that could wait a bit for now.

For users it means BI rendered viewport
wouldn't crash when using metaballs.

Modified Paths:
--------------
    branches/soc-2013-depsgraph_mt/source/blender/blenkernel/intern/mball.c

Modified: branches/soc-2013-depsgraph_mt/source/blender/blenkernel/intern/mball.c
===================================================================
--- branches/soc-2013-depsgraph_mt/source/blender/blenkernel/intern/mball.c	2013-06-17 10:01:35 UTC (rev 57515)
+++ branches/soc-2013-depsgraph_mt/source/blender/blenkernel/intern/mball.c	2013-06-17 10:51:24 UTC (rev 57516)
@@ -112,19 +112,6 @@
 	struct intlists *next;      /* remaining elements */
 } INTLISTS;
 
-typedef struct process {        /* parameters, function, storage */
-	/* what happens here? floats, I think. */
-	/*  float (*function)(void);	 */	/* implicit surface function */
-	float (*function)(float, float, float);
-	float size, delta;          /* cube size, normal delta */
-	int bounds;                 /* cube range within lattice */
-	CUBES *cubes;               /* active cubes */
-	VERTICES vertices;          /* surface vertices */
-	CENTERLIST **centers;       /* cube center hash table */
-	CORNER **corners;           /* corner value hash table */
-	EDGELIST **edges;           /* edge and vertex id hash table */
-} PROCESS;
-
 /* dividing scene using octal tree makes polygonisation faster */
 typedef struct ml_pointer {
 	struct ml_pointer *next, *prev;
@@ -153,21 +140,42 @@
 	char *data;
 };
 
-/* Forward declarations */
-static int vertid(const CORNER *c1, const CORNER *c2, PROCESS *p, MetaBall *mb);
-static int setcenter(CENTERLIST *table[], const int i, const int j, const int k);
-static CORNER *setcorner(PROCESS *p, int i, int j, int k);
-static void converge(const float p1[3], const float p2[3], float v1, float v2,
-                     float (*function)(float, float, float), float p[3], MetaBall *mb, int f);
-
-/* Global variables */
-static struct {
+typedef struct process {        /* parameters, function, storage */
+	/* ** old G_mb contents ** */
 	float thresh;
 	int totelem;
 	MetaElem **mainb;
 	octal_tree *metaball_tree;
-} G_mb = {0};
 
+	/* ** old process contents ** */
+
+	/* what happens here? floats, I think. */
+	/*  float (*function)(void);	 */	/* implicit surface function */
+	float (*function)(struct process*, float, float, float);
+	float size, delta;          /* cube size, normal delta */
+	int bounds;                 /* cube range within lattice */
+	CUBES *cubes;               /* active cubes */
+	VERTICES vertices;          /* surface vertices */
+	CENTERLIST **centers;       /* cube center hash table */
+	CORNER **corners;           /* corner value hash table */
+	EDGELIST **edges;           /* edge and vertex id hash table */
+
+	/* Runtime things */
+	int *indices;
+	int totindex, curindex;
+
+	int pgn_offset;
+	struct pgn_elements *pgn_current;
+	ListBase pgn_list;
+} PROCESS;
+
+/* Forward declarations */
+static int vertid(PROCESS *process, const CORNER *c1, const CORNER *c2, MetaBall *mb);
+static int setcenter(PROCESS *process, CENTERLIST *table[], const int i, const int j, const int k);
+static CORNER *setcorner(PROCESS *process, int i, int j, int k);
+static void converge(PROCESS *process, const float p1[3], const float p2[3], float v1, float v2,
+                     float p[3], MetaBall *mb, int f);
+
 /* Functions */
 
 void BKE_mball_unlink(MetaBall *mb)
@@ -519,43 +527,23 @@
 	Scene *sce_iter = scene;
 	Base *base;
 	Object *ob, *bob = basis;
-	MetaElem *ml = NULL;
 	int basisnr, obnr;
 	char basisname[MAX_ID_NAME], obname[MAX_ID_NAME];
 
 	BLI_split_name_num(basisname, &basisnr, basis->id.name + 2, '.');
-	G_mb.totelem = 0;
 
 	/* XXX recursion check, see scene.c, just too simple code this BKE_scene_base_iter_next() */
 	if (F_ERROR == BKE_scene_base_iter_next(&sce_iter, 0, NULL, NULL))
 		return NULL;
-	
+
 	while (BKE_scene_base_iter_next(&sce_iter, 1, &base, &ob)) {
-		
 		if (ob->type == OB_MBALL) {
-			if (ob == bob) {
-				MetaBall *mb = ob->data;
-				
-				/* if bob object is in edit mode, then dynamic list of all MetaElems
-				 * is stored in editelems */
-				if (mb->editelems) ml = mb->editelems->first;
-				/* if bob object is in object mode */
-				else ml = mb->elems.first;
-			}
-			else {
+			if (ob != bob) {
 				BLI_split_name_num(obname, &obnr, ob->id.name + 2, '.');
 
 				/* object ob has to be in same "group" ... it means, that it has to have
 				 * same base of its name */
 				if (strcmp(obname, basisname) == 0) {
-					MetaBall *mb = ob->data;
-					
-					/* if object is in edit mode, then dynamic list of all MetaElems
-					 * is stored in editelems */
-					if (mb->editelems) ml = mb->editelems->first;
-					/* if bob object is in object mode */
-					else ml = mb->elems.first;
-					
 					if (obnr < basisnr) {
 						if (!(ob->flag & OB_FROMDUPLI)) {
 							basis = ob;
@@ -564,12 +552,6 @@
 					}
 				}
 			}
-			
-			for ( ; ml; ml = ml->next) {
-				if (!(ml->flag & MB_HIDE)) {
-					G_mb.totelem++;
-				}
-			}
 		}
 	}
 
@@ -768,60 +750,57 @@
 	return node;
 }
 
-static float metaball(float x, float y, float z)
+static float metaball(PROCESS *process, float x, float y, float z)
 /*  float x, y, z; */
 {
+	octal_tree *metaball_tree = process->metaball_tree;
 	struct octal_node *node;
 	struct ml_pointer *ml_p;
 	float dens = 0;
 	int a;
 	
-	if (G_mb.totelem > 1) {
-		node = find_metaball_octal_node(G_mb.metaball_tree->first, x, y, z, G_mb.metaball_tree->depth);
+	if (process->totelem > 1) {
+		node = find_metaball_octal_node(metaball_tree->first, x, y, z, metaball_tree->depth);
 		if (node) {
 			for (ml_p = node->elems.first; ml_p; ml_p = ml_p->next) {
 				dens += densfunc(ml_p->ml, x, y, z);
 			}
 
-			dens += -0.5f * (G_mb.metaball_tree->pos - node->pos);
-			dens +=  0.5f * (G_mb.metaball_tree->neg - node->neg);
+			dens += -0.5f * (metaball_tree->pos - node->pos);
+			dens +=  0.5f * (metaball_tree->neg - node->neg);
 		}
 		else {
-			for (a = 0; a < G_mb.totelem; a++) {
-				dens += densfunc(G_mb.mainb[a], x, y, z);
+			for (a = 0; a < process->totelem; a++) {
+				dens += densfunc(process->mainb[a], x, y, z);
 			}
 		}
 	}
 	else {
-		dens += densfunc(G_mb.mainb[0], x, y, z);
+		dens += densfunc(process->mainb[0], x, y, z);
 	}
 
-	return G_mb.thresh - dens;
+	return process->thresh - dens;
 }
 
 /* ******************************************** */
 
-static int *indices = NULL;
-static int totindex, curindex;
-
-
-static void accum_mballfaces(int i1, int i2, int i3, int i4)
+static void accum_mballfaces(PROCESS *process, int i1, int i2, int i3, int i4)
 {
 	int *newi, *cur;
 	/* static int i = 0; I would like to delete altogether, but I don't dare to, yet */
 
-	if (totindex == curindex) {
-		totindex += 256;
-		newi = MEM_mallocN(4 * sizeof(int) * totindex, "vertindex");
+	if (process->totindex == process->curindex) {
+		process->totindex += 256;
+		newi = MEM_mallocN(4 * sizeof(int) * process->totindex, "vertindex");
 		
-		if (indices) {
-			memcpy(newi, indices, 4 * sizeof(int) * (totindex - 256));
-			MEM_freeN(indices);
+		if (process->indices) {
+			memcpy(newi, process->indices, 4 * sizeof(int) * (process->totindex - 256));
+			MEM_freeN(process->indices);
 		}
-		indices = newi;
+		process->indices = newi;
 	}
 	
-	cur = indices + 4 * curindex;
+	cur = process->indices + 4 * process->curindex;
 
 	/* displists now support array drawing, we treat tri's as fake quad */
 	
@@ -833,63 +812,62 @@
 	else 
 		cur[3] = i4;
 	
-	curindex++;
+	process->curindex++;
 
 }
 
 /* ******************* MEMORY MANAGEMENT *********************** */
-static void *new_pgn_element(int size)
+static void *new_pgn_element(PROCESS *process, int size)
 {
 	/* during polygonize 1000s of elements are allocated
 	 * and never freed in between. Freeing only done at the end.
 	 */
 	int blocksize = 16384;
-	static int offs = 0;     /* the current free address */
-	static struct pgn_elements *cur = NULL;
-	static ListBase lb = {NULL, NULL};
 	void *adr;
 	
 	if (size > 10000 || size == 0) {
 		printf("incorrect use of new_pgn_element\n");
 	}
 	else if (size == -1) {
-		cur = lb.first;
+		struct pgn_elements *cur = process->pgn_list.first;
 		while (cur) {
 			MEM_freeN(cur->data);
 			cur = cur->next;
 		}
-		BLI_freelistN(&lb);
+		BLI_freelistN(&process->pgn_list);
 		
 		return NULL;
 	}
 	
 	size = 4 * ( (size + 3) / 4);
 	
-	if (cur) {
-		if (size + offs < blocksize) {
-			adr = (void *) (cur->data + offs);
-			offs += size;
+	if (process->pgn_current) {
+		if (size + process->pgn_offset < blocksize) {
+			adr = (void *) (process->pgn_current->data + process->pgn_offset);
+			process->pgn_offset += size;
 			return adr;
 		}
 	}
 	
-	cur = MEM_callocN(sizeof(struct pgn_elements), "newpgn");
-	cur->data = MEM_callocN(blocksize, "newpgn");
-	BLI_addtail(&lb, cur);
+	process->pgn_current = MEM_callocN(sizeof(struct pgn_elements), "newpgn");
+	process->pgn_current->data = MEM_callocN(blocksize, "newpgn");
+	BLI_addtail(&process->pgn_list, process->pgn_current);
 	
-	offs = size;
-	return cur->data;
+	process->pgn_offset = size;
+	return process->pgn_current->data;
 }
 
-static void freepolygonize(PROCESS *p)
+static void freepolygonize(PROCESS *process)
 {
-	MEM_freeN(p->corners);
-	MEM_freeN(p->edges);
-	MEM_freeN(p->centers);
+	MEM_freeN(process->corners);
+	MEM_freeN(process->edges);
+	MEM_freeN(process->centers);
 
-	new_pgn_element(-1);
-	
-	if (p->vertices.ptr) MEM_freeN(p->vertices.ptr);
+	new_pgn_element(process, -1);
+
+	if (process->vertices.ptr) {
+		MEM_freeN(process->vertices.ptr);
+	}
 }
 
 /**** Cubical Polygonization (optional) ****/
@@ -928,7 +906,7 @@
 
 /* docube: triangulate the cube directly, without decomposition */
 
-static void docube(CUBE *cube, PROCESS *p, MetaBall *mb)
+static void docube(PROCESS *process, CUBE *cube, MetaBall *mb)
 {
 	INTLISTS *polys;
 	CORNER *c1, *c2;
@@ -945,45 +923,45 @@
 			c1 = cube->corners[corner1[edges->i]];
 			c2 = cube->corners[corner2[edges->i]];
 			
-			indexar[count] = vertid(c1, c2, p, mb);
+			indexar[count] = vertid(process, c1, c2, mb);
 			count++;
 		}
 		if (count > 2) {
 			switch (count) {
 				case 3:
-					accum_mballfaces(indexar[2], indexar[1], indexar[0], 0);
+					accum_mballfaces(process, indexar[2], indexar[1], indexar[0], 0);
 					break;
 				case 4:
-					if (indexar[0] == 0) accum_mballfaces(indexar[0], indexar[3], indexar[2], indexar[1]);
-					else accum_mballfaces(indexar[3], indexar[2], indexar[1], indexar[0]);
+					if (indexar[0] == 0) accum_mballfaces(process, indexar[0], indexar[3], indexar[2], indexar[1]);
+					else accum_mballfaces(process, indexar[3], indexar[2], indexar[1], indexar[0]);
 					break;
 				case 5:

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list