Code cleanup for the neighbor_average() sculpt function.
authorNicholas Bishop <nicholasbishop@gmail.com>
Tue, 28 Feb 2012 23:08:40 +0000 (23:08 +0000)
committerNicholas Bishop <nicholasbishop@gmail.com>
Tue, 28 Feb 2012 23:08:40 +0000 (23:08 +0000)
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.

source/blender/blenkernel/BKE_mesh.h
source/blender/blenkernel/intern/mesh.c
source/blender/editors/sculpt_paint/sculpt.c

index 016cef6..e06dee2 100644 (file)
@@ -99,6 +99,18 @@ void mesh_calc_poly_center(struct MPoly *mpoly, struct MLoop *loopstart,
 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);
index ef80c22..c9b7337 100644 (file)
@@ -2833,6 +2833,42 @@ float mesh_calc_poly_area(MPoly *mpoly, MLoop *loopstart,
        }
 }
 
+/* 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])
 {
index 6a0887c..dc25f29 100644 (file)
@@ -904,62 +904,42 @@ static void calc_sculpt_normal(Sculpt *sd, Object *ob, float an[3], PBVHNode **n
    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;
-       }
-
-       while(node){
-               f= &ss->mpoly[node->index];
-
-               /* 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(node = ss->pmap[vert].first; node; node = node->next) {
+                       const MPoly *p= &ss->mpoly[node->index];
+                       unsigned f_adj_v[3];
 
+                       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);
 
-                       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)