[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [44537] trunk/blender/source/blender: Code cleanup for the neighbor_average() sculpt function.

Nicholas Bishop nicholasbishop at gmail.com
Wed Feb 29 00:08:46 CET 2012


Revision: 44537
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=44537
Author:   nicholasbishop
Date:     2012-02-28 23:08:40 +0000 (Tue, 28 Feb 2012)
Log Message:
-----------
Code cleanup for the neighbor_average() sculpt function.

Moved some of the code into a couple new mesh functions for searching
in poly loops to simplify the function, the rest is just cosmetic
changes.

Modified Paths:
--------------
    trunk/blender/source/blender/blenkernel/BKE_mesh.h
    trunk/blender/source/blender/blenkernel/intern/mesh.c
    trunk/blender/source/blender/editors/sculpt_paint/sculpt.c

Modified: trunk/blender/source/blender/blenkernel/BKE_mesh.h
===================================================================
--- trunk/blender/source/blender/blenkernel/BKE_mesh.h	2012-02-28 22:54:09 UTC (rev 44536)
+++ trunk/blender/source/blender/blenkernel/BKE_mesh.h	2012-02-28 23:08:40 UTC (rev 44537)
@@ -99,6 +99,18 @@
 float mesh_calc_poly_area(struct MPoly *mpoly, struct MLoop *loopstart,
                           struct MVert *mvarray, float polynormal[3]);
 
+/* Find the index of the loop in 'poly' which references vertex,
+   returns -1 if not found */
+int poly_find_loop_from_vert(const struct MPoly *poly,
+							 const struct MLoop *loopstart,
+							 unsigned vert);
+
+/* Fill 'adj_r' with the loop indices in 'poly' adjacent to the
+   vertex. Returns the index of the loop matching vertex, or -1 if the
+   vertex is not in 'poly' */
+int poly_get_adj_loops_from_vert(unsigned adj_r[3], const struct MPoly *poly,
+								 const struct MLoop *mloop, unsigned vert);
+
 void unlink_mesh(struct Mesh *me);
 void free_mesh(struct Mesh *me, int unlink);
 struct Mesh *add_mesh(const char *name);

Modified: trunk/blender/source/blender/blenkernel/intern/mesh.c
===================================================================
--- trunk/blender/source/blender/blenkernel/intern/mesh.c	2012-02-28 22:54:09 UTC (rev 44536)
+++ trunk/blender/source/blender/blenkernel/intern/mesh.c	2012-02-28 23:08:40 UTC (rev 44537)
@@ -2833,6 +2833,42 @@
 	}
 }
 
+/* Find the index of the loop in 'poly' which references vertex,
+   returns -1 if not found */
+int poly_find_loop_from_vert(const MPoly *poly, const MLoop *loopstart,
+							 unsigned vert)
+{
+	int j;
+	for (j = 0; j < poly->totloop; j++, loopstart++) {
+		if (loopstart->v == vert)
+			return j;
+	}
+	
+	return -1;
+}
+
+/* Fill 'adj_r' with the loop indices in 'poly' adjacent to the
+   vertex. Returns the index of the loop matching vertex, or -1 if the
+   vertex is not in 'poly' */
+int poly_get_adj_loops_from_vert(unsigned adj_r[3], const MPoly *poly,
+								 const MLoop *mloop, unsigned vert)
+{
+	int corner = poly_find_loop_from_vert(poly,
+										  &mloop[poly->loopstart],
+										  vert);
+		
+	if(corner != -1) {
+		const MLoop *ml = &mloop[poly->loopstart + corner];
+
+		/* vertex was found */
+		adj_r[0] = ME_POLY_LOOP_PREV(mloop, poly, corner)->v;
+		adj_r[1] = ml->v;
+		adj_r[2] = ME_POLY_LOOP_NEXT(mloop, poly, corner)->v;
+	}
+
+	return corner;
+}
+
 /* basic vertex data functions */
 int minmax_mesh(Mesh *me, float min[3], float max[3])
 {

Modified: trunk/blender/source/blender/editors/sculpt_paint/sculpt.c
===================================================================
--- trunk/blender/source/blender/editors/sculpt_paint/sculpt.c	2012-02-28 22:54:09 UTC (rev 44536)
+++ trunk/blender/source/blender/editors/sculpt_paint/sculpt.c	2012-02-28 23:08:40 UTC (rev 44537)
@@ -904,62 +904,42 @@
    polygon.) */
 static void neighbor_average(SculptSession *ss, float avg[3], unsigned vert)
 {
-	int i, j, ok, total=0;
-	IndexNode *node= ss->pmap[vert].first;
-	char ncount= BLI_countlist(&ss->pmap[vert]);
-	MPoly *f;
-	MLoop *ml;
+	const int ncount = BLI_countlist(&ss->pmap[vert]);
+	const MVert *mvert = ss->mvert;
+	float (*deform_co)[3] = ss->deform_cos;
 
-	avg[0] = avg[1] = avg[2] = 0;
+	zero_v3(avg);
 		
 	/* Don't modify corner vertices */
-	if(ncount==1) {
-		if(ss->deform_cos) copy_v3_v3(avg, ss->deform_cos[vert]);
-		else copy_v3_v3(avg, ss->mvert[vert].co);
+	if(ncount != 1) {
+		IndexNode *node;
+		int total = 0;
 
-		return;
-	}
+		for(node = ss->pmap[vert].first; node; node = node->next) {
+			const MPoly *p= &ss->mpoly[node->index];
+			unsigned f_adj_v[3];
 
-	while(node){
-		f= &ss->mpoly[node->index];
+			if(poly_get_adj_loops_from_vert(f_adj_v, p, ss->mloop, vert) != -1) {
+				int i;
+			
+				for (i = 0; i < 3; i++) {
+					if (ncount != 2 || BLI_countlist(&ss->pmap[f_adj_v[i]]) <= 2) {
+						add_v3_v3(avg, deform_co ? deform_co[f_adj_v[i]] :
+												       mvert[f_adj_v[i]].co);
 
-		/* find the loop in the poly which references this vertex */
-		ok = FALSE;
-		ml = ss->mloop + f->loopstart;
-		for (j = 0; j < f->totloop; j++, ml++) {
-			if (ml->v == vert) {
-				ok = TRUE;
-				break;
-			}
-		}
-
-		if (ok) {
-			/* vertex was found */
-			unsigned int f_adj_v[3] = {
-			    ME_POLY_LOOP_PREV(ss->mloop, f, j)->v,
-			    ml->v,
-			    ME_POLY_LOOP_NEXT(ss->mloop, f, j)->v};
-
-
-			for (i=0; i<3; ++i) {
-				if (ncount!=2 || BLI_countlist(&ss->pmap[f_adj_v[i]]) <= 2) {
-					if(ss->deform_cos) add_v3_v3(avg, ss->deform_cos[f_adj_v[i]]);
-					else add_v3_v3(avg, ss->mvert[f_adj_v[i]].co);
-					++total;
+						total++;
+					}
 				}
 			}
-
 		}
 
-		node= node->next;
+		if(total > 0) {
+			mul_v3_fl(avg, 1.0f / total);
+			return;
+		}
 	}
 
-	if(total>0)
-		mul_v3_fl(avg, 1.0f / total);
-	else {
-		if(ss->deform_cos) copy_v3_v3(avg, ss->deform_cos[vert]);
-		else copy_v3_v3(avg, ss->mvert[vert].co);
-	}
+	copy_v3_v3(avg, deform_co ? deform_co[vert] : mvert[vert].co);
 }
 
 static void do_mesh_smooth_brush(Sculpt *sd, SculptSession *ss, PBVHNode *node, float bstrength)




More information about the Bf-blender-cvs mailing list