utility function for getting the center of a polygon, needed for up coming changes
authorCampbell Barton <ideasman42@gmail.com>
Mon, 28 Nov 2011 23:50:40 +0000 (23:50 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Mon, 28 Nov 2011 23:50:40 +0000 (23:50 +0000)
source/blender/blenkernel/BKE_mesh.h
source/blender/blenkernel/intern/mesh.c

index 054ce35b31c38eea87870da1c495fa583ffced91..e525ce13ade5dd24c832b0bc75fa0848d04a489b 100644 (file)
@@ -79,7 +79,10 @@ int mesh_recalcTesselation(struct CustomData *fdata, struct CustomData *ldata,
 
 /*calculates a face normal.*/
 void mesh_calc_poly_normal(struct MPoly *mpoly, struct MLoop *loopstart, 
-                           struct MVert *mvarray, float *no);
+                           struct MVert *mvarray, float no[3]);
+
+void mesh_calc_poly_center(struct MPoly *mpoly, struct MLoop *loopstart,
+                           struct MVert *mvarray, float cent[3]);
 
 void unlink_mesh(struct Mesh *me);
 void free_mesh(struct Mesh *me, int unlink);
index b48c750474b03db2f8151d47deb7996c6dbd8d47..67fb6775a556e9a0c2da0c7daa5341578ca5fb58 100644 (file)
@@ -2450,7 +2450,7 @@ int mesh_recalcTesselation(CustomData *fdata,
  *
 */
 static void mesh_calc_ngon_normal(MPoly *mpoly, MLoop *loopstart, 
-                                 MVert *mvert, float *normal)
+                                  MVert *mvert, float normal[3])
 {
 
        MVert *v1, *v2, *v3;
@@ -2512,32 +2512,66 @@ static void mesh_calc_ngon_normal(MPoly *mpoly, MLoop *loopstart,
 }
 
 void mesh_calc_poly_normal(MPoly *mpoly, MLoop *loopstart, 
-                           MVert *mvarray, float *no)
+                           MVert *mvarray, float no[3])
 {
-       if(mpoly->totloop > 4) {
+       if (mpoly->totloop > 4) {
                mesh_calc_ngon_normal(mpoly, loopstart, mvarray, no);
        }
-       else if(mpoly->totloop == 3){
-               MVert *v1, *v2, *v3;
+       else if (mpoly->totloop == 3){
+               normal_tri_v3(no,
+                             mvarray[loopstart[0].v].co,
+                             mvarray[loopstart[1].v].co,
+                             mvarray[loopstart[2].v].co
+                             );
+       }
+       else if (mpoly->totloop == 4) {
+               normal_quad_v3(no,
+                              mvarray[loopstart[0].v].co,
+                              mvarray[loopstart[1].v].co,
+                              mvarray[loopstart[2].v].co,
+                              mvarray[loopstart[3].v].co
+                              );
+       }
+       else { /* horrible, two sided face! */
+               no[0] = 0.0;
+               no[1] = 0.0;
+               no[2] = 1.0;
+       }
+}
+
+static void mesh_calc_ngon_center(MPoly *mpoly, MLoop *loopstart,
+                                  MVert *mvert, float cent[3])
+{
+       const float fac= 1.0f / (float)mpoly->totloop;
+       int i;
 
-               v1 = mvarray + (loopstart++)->v;
-               v2 = mvarray + (loopstart++)->v;
-               v3 = mvarray + loopstart->v;
-               normal_tri_v3( no,v1->co, v2->co, v3->co);
+       zero_v3(cent);
+
+       for (i = 0; i < mpoly->totloop; i++) {
+               madd_v3_v3fl(cent, mvert[(loopstart++)->v].co, fac);
        }
-       else if(mpoly->totloop == 4){
-               MVert *v1, *v2, *v3, *v4;
+}
 
-               v1 = mvarray + (loopstart++)->v;
-               v2 = mvarray + (loopstart++)->v;
-               v3 = mvarray + (loopstart++)->v;
-               v4 = mvarray + loopstart->v;
-               normal_quad_v3( no,v1->co, v2->co, v3->co, v4->co);
+void mesh_calc_poly_center(MPoly *mpoly, MLoop *loopstart,
+                           MVert *mvarray, float cent[3])
+{
+       if (mpoly->totloop == 3) {
+               cent_tri_v3(cent,
+                           mvarray[loopstart[0].v].co,
+                           mvarray[loopstart[1].v].co,
+                           mvarray[loopstart[2].v].co
+                           );
+       }
+       else if (mpoly->totloop == 4) {
+               cent_quad_v3(cent,
+                            mvarray[loopstart[0].v].co,
+                            mvarray[loopstart[1].v].co,
+                            mvarray[loopstart[2].v].co,
+                            mvarray[loopstart[3].v].co
+                            );
        }
-       else{ /*horrible, two sided face!*/
-               no[0] = 0.0;
-               no[1] = 0.0;
-               no[2] = 1.0;
+       else {
+               mesh_calc_ngon_center(mpoly, loopstart, mvarray, cent);
        }
 }