[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [49994] trunk/blender/source/blender/ blenkernel: code cleanup: use BLI math funcs for metaballs, also remove MB_POINT struct and just use float[3] instead.

Campbell Barton ideasman42 at gmail.com
Sat Aug 18 21:30:28 CEST 2012


Revision: 49994
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=49994
Author:   campbellbarton
Date:     2012-08-18 19:30:27 +0000 (Sat, 18 Aug 2012)
Log Message:
-----------
code cleanup: use BLI math funcs for metaballs, also remove MB_POINT struct and just use float[3] instead.

Modified Paths:
--------------
    trunk/blender/source/blender/blenkernel/BKE_mball.h
    trunk/blender/source/blender/blenkernel/intern/mball.c

Modified: trunk/blender/source/blender/blenkernel/BKE_mball.h
===================================================================
--- trunk/blender/source/blender/blenkernel/BKE_mball.h	2012-08-18 19:01:37 UTC (rev 49993)
+++ trunk/blender/source/blender/blenkernel/BKE_mball.h	2012-08-18 19:30:27 UTC (rev 49994)
@@ -57,8 +57,8 @@
 void BKE_mball_properties_copy(struct Scene *scene, struct Object *active_object);
 
 int BKE_mball_minmax(struct MetaBall *mb, float min[3], float max[3]);
-int BKE_mball_center_median(struct MetaBall *mb, float cent[3]);
-int BKE_mball_center_bounds(struct MetaBall *mb, float cent[3]);
+int BKE_mball_center_median(struct MetaBall *mb, float r_cent[3]);
+int BKE_mball_center_bounds(struct MetaBall *mb, float r_cent[3]);
 void BKE_mball_translate(struct MetaBall *mb, float offset[3]);
 
 struct MetaElem *BKE_mball_element_add(struct MetaBall *mb, const int type);

Modified: trunk/blender/source/blender/blenkernel/intern/mball.c
===================================================================
--- trunk/blender/source/blender/blenkernel/intern/mball.c	2012-08-18 19:01:37 UTC (rev 49993)
+++ trunk/blender/source/blender/blenkernel/intern/mball.c	2012-08-18 19:30:27 UTC (rev 49994)
@@ -67,12 +67,9 @@
 
 /* Data types */
 
-typedef struct point {          /* a three-dimensional point */
-	float x, y, z;              /* its coordinates */
-} MB_POINT;
-
 typedef struct vertex {         /* surface vertex */
-	MB_POINT position, normal;  /* position and surface normal */
+	float co[3];  /* position and surface normal */
+	float no[3];
 } VERTEX;
 
 typedef struct vertices {       /* list of vertices in polygonization */
@@ -82,7 +79,7 @@
 
 typedef struct corner {         /* corner of a cube */
 	int i, j, k;                /* (i, j, k) is index within lattice */
-	float x, y, z, value;       /* location and function value */
+	float co[3], value;       /* location and function value */
 	struct corner *next;
 } CORNER;
 
@@ -159,11 +156,11 @@
 };
 
 /* Forward declarations */
-static int vertid(CORNER *c1, CORNER *c2, PROCESS *p, MetaBall *mb);
+static int vertid(const CORNER *c1, const CORNER *c2, PROCESS *p, MetaBall *mb);
 static int setcenter(CENTERLIST *table[], int i, int j, int k);
 static CORNER *setcorner(PROCESS *p, int i, int j, int k);
-static void converge(MB_POINT *p1, MB_POINT *p2, float v1, float v2,
-                     float (*function)(float, float, float), MB_POINT *p, MetaBall *mb, int f);
+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 */
 
@@ -631,65 +628,59 @@
 
 static float densfunc(MetaElem *ball, float x, float y, float z)
 {
-	float dist2 = 0.0, dx, dy, dz;
-	float vec[3];
+	float dist2;
+	float dvec[3] = {x, y, z};
 
-	vec[0] = x;
-	vec[1] = y;
-	vec[2] = z;
-	mul_m4_v3((float (*)[4])ball->imat, vec);
-	dx = vec[0];
-	dy = vec[1];
-	dz = vec[2];
+	mul_m4_v3((float (*)[4])ball->imat, dvec);
 
 	if (ball->type == MB_BALL) {
 	}
 	else if (ball->type == MB_TUBEX) {
-		if (dx > ball->len) dx -= ball->len;
-		else if (dx < -ball->len) dx += ball->len;
-		else dx = 0.0;
+		if      (dvec[0] >  ball->len) dvec[0] -= ball->len;
+		else if (dvec[0] < -ball->len) dvec[0] += ball->len;
+		else                           dvec[0] = 0.0;
 	}
 	else if (ball->type == MB_TUBEY) {
-		if (dy > ball->len) dy -= ball->len;
-		else if (dy < -ball->len) dy += ball->len;
-		else dy = 0.0;
+		if      (dvec[1] >  ball->len) dvec[1] -= ball->len;
+		else if (dvec[1] < -ball->len) dvec[1] += ball->len;
+		else                           dvec[1] = 0.0;
 	}
 	else if (ball->type == MB_TUBEZ) {
-		if (dz > ball->len) dz -= ball->len;
-		else if (dz < -ball->len) dz += ball->len;
-		else dz = 0.0;
+		if      (dvec[2] >  ball->len) dvec[2] -= ball->len;
+		else if (dvec[2] < -ball->len) dvec[2] += ball->len;
+		else                           dvec[2] = 0.0;
 	}
 	else if (ball->type == MB_TUBE) {
-		if (dx > ball->expx) dx -= ball->expx;
-		else if (dx < -ball->expx) dx += ball->expx;
-		else dx = 0.0;
+		if      (dvec[0] >  ball->expx) dvec[0] -= ball->expx;
+		else if (dvec[0] < -ball->expx) dvec[0] += ball->expx;
+		else                            dvec[0] = 0.0;
 	}
 	else if (ball->type == MB_PLANE) {
-		if (dx > ball->expx) dx -= ball->expx;
-		else if (dx < -ball->expx) dx += ball->expx;
-		else dx = 0.0;
-		if (dy > ball->expy) dy -= ball->expy;
-		else if (dy < -ball->expy) dy += ball->expy;
-		else dy = 0.0;
+		if      (dvec[0] >  ball->expx) dvec[0] -= ball->expx;
+		else if (dvec[0] < -ball->expx) dvec[0] += ball->expx;
+		else                            dvec[0] = 0.0;
+		if      (dvec[1] >  ball->expy) dvec[1] -= ball->expy;
+		else if (dvec[1] < -ball->expy) dvec[1] += ball->expy;
+		else                            dvec[1] = 0.0;
 	}
 	else if (ball->type == MB_ELIPSOID) {
-		dx *= 1 / ball->expx;
-		dy *= 1 / ball->expy;
-		dz *= 1 / ball->expz;
+		dvec[0] *= 1 / ball->expx;
+		dvec[1] *= 1 / ball->expy;
+		dvec[2] *= 1 / ball->expz;
 	}
 	else if (ball->type == MB_CUBE) {
-		if (dx > ball->expx) dx -= ball->expx;
-		else if (dx < -ball->expx) dx += ball->expx;
-		else dx = 0.0;
-		if (dy > ball->expy) dy -= ball->expy;
-		else if (dy < -ball->expy) dy += ball->expy;
-		else dy = 0.0;
-		if (dz > ball->expz) dz -= ball->expz;
-		else if (dz < -ball->expz) dz += ball->expz;
-		else dz = 0.0;
+		if      (dvec[0] >  ball->expx) dvec[0] -= ball->expx;
+		else if (dvec[0] < -ball->expx) dvec[0] += ball->expx;
+		else                            dvec[0] = 0.0;
+		if      (dvec[1] >  ball->expy) dvec[1] -= ball->expy;
+		else if (dvec[1] < -ball->expy) dvec[1] += ball->expy;
+		else                            dvec[1] = 0.0;
+		if      (dvec[2] >  ball->expz) dvec[2] -= ball->expz;
+		else if (dvec[2] < -ball->expz) dvec[2] += ball->expz;
+		else                            dvec[2] = 0.0;
 	}
 
-	dist2 = (dx * dx + dy * dy + dz * dz);
+	dist2 = len_v3(dvec);
 
 	if (ball->flag & MB_NEGATIVE) {
 		dist2 = 1.0f - (dist2 / ball->rad2);
@@ -1074,12 +1065,12 @@
 	c = (CORNER *) new_pgn_element(sizeof(CORNER));
 
 	c->i = i; 
-	c->x = ((float)i - 0.5f) * p->size;
+	c->co[0] = ((float)i - 0.5f) * p->size;
 	c->j = j; 
-	c->y = ((float)j - 0.5f) * p->size;
+	c->co[1] = ((float)j - 0.5f) * p->size;
 	c->k = k; 
-	c->z = ((float)k - 0.5f) * p->size;
-	c->value = p->function(c->x, c->y, c->z);
+	c->co[2] = ((float)k - 0.5f) * p->size;
+	c->value = p->function(c->co[0], c->co[1], c->co[2]);
 	
 	c->next = p->corners[index];
 	p->corners[index] = c;
@@ -1204,7 +1195,7 @@
 /* setcenter: set (i, j, k) entry of table[]
  * return 1 if already set; otherwise, set and return 0 */
 
-static int setcenter(CENTERLIST *table[], int i, int j, int k)
+static int setcenter(CENTERLIST *table[], const int i, const int j, const int k)
 {
 	int index;
 	CENTERLIST *newc, *l, *q;
@@ -1324,72 +1315,46 @@
 
 /* vnormal: compute unit length surface normal at point */
 
-static void vnormal(MB_POINT *point, PROCESS *p, MB_POINT *v)
+static void vnormal(const float point[3], PROCESS *p, float r_no[3])
 {
 	float delta = 0.2f * p->delta;
-	float f = p->function(point->x, point->y, point->z);
+	float f = p->function(point[0], point[1], point[2]);
 
-	v->x = p->function(point->x + delta, point->y, point->z) - f;
-	v->y = p->function(point->x, point->y + delta, point->z) - f;
-	v->z = p->function(point->x, point->y, point->z + delta) - f;
-	f = sqrtf(v->x * v->x + v->y * v->y + v->z * v->z);
-
-	if (f != 0.0f) {
-		v->x /= f; 
-		v->y /= f; 
-		v->z /= f;
-	}
+	r_no[0] = p->function(point[0] + delta, point[1], point[2]) - f;
+	r_no[1] = p->function(point[0], point[1] + delta, point[2]) - f;
+	r_no[2] = p->function(point[0], point[1], point[2] + delta) - f;
+	f = normalize_v3(r_no);
 	
-	if (FALSE) {
-		MB_POINT temp;
+	if (0) {
+		float tvec[3];
 		
 		delta *= 2.0f;
 		
-		f = p->function(point->x, point->y, point->z);
+		f = p->function(point[0], point[1], point[2]);
 	
-		temp.x = p->function(point->x + delta, point->y, point->z) - f;
-		temp.y = p->function(point->x, point->y + delta, point->z) - f;
-		temp.z = p->function(point->x, point->y, point->z + delta) - f;
-		f = sqrtf(temp.x * temp.x + temp.y * temp.y + temp.z * temp.z);
+		tvec[0] = p->function(point[0] + delta, point[1], point[2]) - f;
+		tvec[1] = p->function(point[0], point[1] + delta, point[2]) - f;
+		tvec[2] = p->function(point[0], point[1], point[2] + delta) - f;
 	
-		if (f != 0.0f) {
-			temp.x /= f; 
-			temp.y /= f; 
-			temp.z /= f;
-			
-			v->x += temp.x;
-			v->y += temp.y;
-			v->z += temp.z;
-			
-			f = sqrtf(v->x * v->x + v->y * v->y + v->z * v->z);
-		
-			if (f != 0.0f) {
-				v->x /= f; 
-				v->y /= f; 
-				v->z /= f;
-			}
+		if (normalize_v3(tvec) != 0.0f) {
+			add_v3_v3(r_no, tvec);
+			normalize_v3(r_no);
 		}
 	}
-	
 }
 
 
-static int vertid(CORNER *c1, CORNER *c2, PROCESS *p, MetaBall *mb)
+static int vertid(const CORNER *c1, const CORNER *c2, PROCESS *p, MetaBall *mb)
 {
 	VERTEX v;
-	MB_POINT a, b;
 	int vid = getedge(p->edges, c1->i, c1->j, c1->k, c2->i, c2->j, c2->k);
 
-	if (vid != -1) return vid;               /* previously computed */
-	a.x = c1->x; 
-	a.y = c1->y; 
-	a.z = c1->z;
-	b.x = c2->x; 
-	b.y = c2->y; 
-	b.z = c2->z;
+	if (vid != -1) {
+		return vid;  /* previously computed */
+	}
 
-	converge(&a, &b, c1->value, c2->value, p->function, &v.position, mb, 1); /* position */
-	vnormal(&v.position, p, &v.normal);
+	converge(c1->co, c2->co, c1->value, c2->value, p->function, v.co, mb, 1); /* position */
+	vnormal(v.co, p, v.no);
 
 	addtovertices(&p->vertices, v);            /* save vertex */
 	vid = p->vertices.count - 1;
@@ -1403,101 +1368,95 @@
 
 /* converge: from two points of differing sign, converge to zero crossing */
 /* watch it: p1 and p2 are used to calculate */
-static void converge(MB_POINT *p1, MB_POINT *p2, float v1, float v2,
-                     float (*function)(float, float, float), MB_POINT *p, MetaBall *mb, int f)
+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)
 {
 	int i = 0;
-	MB_POINT pos, neg;
+	float pos[3], neg[3];
 	float positive = 0.0f, negative = 0.0f;
-	float dx = 0.0f, dy = 0.0f, dz = 0.0f;
+	float dvec[3];
 	
 	if (v1 < 0) {
-		pos = *p2;
-		neg = *p1;
+		copy_v3_v3(pos, p2);
+		copy_v3_v3(neg, p1);
 		positive = v2;
 		negative = v1;
 	}

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list