code cleanup: use much simpler (and likely faster) polygon normal calculation in...
authorCampbell Barton <ideasman42@gmail.com>
Fri, 4 May 2012 13:13:45 +0000 (13:13 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Fri, 4 May 2012 13:13:45 +0000 (13:13 +0000)
source/blender/blenkernel/intern/mesh.c
source/blender/editors/space_view3d/drawobject.c
source/blender/render/intern/source/strand.c

index 40c04a170ca8ecceb4355691e8f1ea68e5907ce2..6dd5923a80339587e7f10b69e41bb720eb84b303 100644 (file)
@@ -2766,64 +2766,20 @@ int mesh_mpoly_to_mface(struct CustomData *fdata, struct CustomData *ldata,
 static void mesh_calc_ngon_normal(MPoly *mpoly, MLoop *loopstart, 
                                   MVert *mvert, float normal[3])
 {
-
-       MVert *v1, *v2, *v3;
-       double u[3], v[3], w[3];
-       double n[3] = {0.0, 0.0, 0.0}, l;
+       const int nverts = mpoly->totloop;
+       float const *v_prev = mvert[loopstart[nverts - 1].v].co;
+       float const *v_curr = mvert[loopstart->v].co;
+       float n[3] = {0.0f};
        int i;
 
-       for (i = 0; i < mpoly->totloop; i++) {
-               v1 = mvert + loopstart[i].v;
-               v2 = mvert + loopstart[(i+1)%mpoly->totloop].v;
-               v3 = mvert + loopstart[(i+2)%mpoly->totloop].v;
-               
-               copy_v3db_v3fl(u, v1->co);
-               copy_v3db_v3fl(v, v2->co);
-               copy_v3db_v3fl(w, v3->co);
-
-               /*this fixes some weird numerical error*/
-               if (i==0) {
-                       u[0] += 0.0001f;
-                       u[1] += 0.0001f;
-                       u[2] += 0.0001f;
-               }
-               
-               /* newell's method
-                * 
-                * so thats?:
-                * (a[1] - b[1]) * (a[2] + b[2]);
-                * a[1]*b[2] - b[1]*a[2] - b[1]*b[2] + a[1]*a[2]
-                * 
-                * odd.  half of that is the cross product. . .what's the
-                * other half?
-                * 
-                * also could be like a[1]*(b[2] + a[2]) - b[1]*(a[2] - b[2])
-                */
-
-               n[0] += (u[1] - v[1]) * (u[2] + v[2]);
-               n[1] += (u[2] - v[2]) * (u[0] + v[0]);
-               n[2] += (u[0] - v[0]) * (u[1] + v[1]);
+       /* Newell's Method */
+       for (i = 0; i < nverts; v_prev = v_curr, v_curr = mvert[loopstart[++i].v].co) {
+               add_newell_cross_v3_v3v3(n, v_prev, v_curr);
        }
-       
-       l = n[0]*n[0]+n[1]*n[1]+n[2]*n[2];
-       l = sqrt(l);
-
-       if (l == 0.0) {
-               normal[0] = 0.0f;
-               normal[1] = 0.0f;
-               normal[2] = 1.0f;
 
-               return;
+       if (UNLIKELY(normalize_v3_v3(normal, n) == 0.0f)) {
+               normal[2] = 1.0f; /* other axis set to 0.0 */
        }
-       else l = 1.0f / l;
-
-       n[0] *= l;
-       n[1] *= l;
-       n[2] *= l;
-       
-       normal[0] = (float) n[0];
-       normal[1] = (float) n[1];
-       normal[2] = (float) n[2];
 }
 
 void mesh_calc_poly_normal(MPoly *mpoly, MLoop *loopstart, 
@@ -2857,54 +2813,20 @@ void mesh_calc_poly_normal(MPoly *mpoly, MLoop *loopstart,
 static void mesh_calc_ngon_normal_coords(MPoly *mpoly, MLoop *loopstart,
                                          const float (*vertex_coords)[3], float normal[3])
 {
-
-       const float *v1, *v2, *v3;
-       double u[3], v[3], w[3];
-       double n[3] = {0.0, 0.0, 0.0}, l;
+       const int nverts = mpoly->totloop;
+       float const *v_prev = vertex_coords[loopstart[nverts - 1].v];
+       float const *v_curr = vertex_coords[loopstart->v];
+       float n[3] = {0.0f};
        int i;
 
-       for (i = 0; i < mpoly->totloop; i++) {
-               v1 = (const float *)(vertex_coords + loopstart[i].v);
-               v2 = (const float *)(vertex_coords + loopstart[(i+1)%mpoly->totloop].v);
-               v3 = (const float *)(vertex_coords + loopstart[(i+2)%mpoly->totloop].v);
-
-               copy_v3db_v3fl(u, v1);
-               copy_v3db_v3fl(v, v2);
-               copy_v3db_v3fl(w, v3);
-
-               /*this fixes some weird numerical error*/
-               if (i==0) {
-                       u[0] += 0.0001f;
-                       u[1] += 0.0001f;
-                       u[2] += 0.0001f;
-               }
-
-               n[0] += (u[1] - v[1]) * (u[2] + v[2]);
-               n[1] += (u[2] - v[2]) * (u[0] + v[0]);
-               n[2] += (u[0] - v[0]) * (u[1] + v[1]);
+       /* Newell's Method */
+       for (i = 0; i < nverts; v_prev = v_curr, v_curr = vertex_coords[loopstart[++i].v]) {
+               add_newell_cross_v3_v3v3(n, v_prev, v_curr);
        }
 
-       l = n[0]*n[0]+n[1]*n[1]+n[2]*n[2];
-       l = sqrt(l);
-
-       if (l == 0.0) {
-               normal[0] = 0.0f;
-               normal[1] = 0.0f;
-               normal[2] = 1.0f;
-
-               return;
-       }
-       else {
-               l = 1.0f / l;
+       if (UNLIKELY(normalize_v3_v3(normal, n) == 0.0f)) {
+               normal[2] = 1.0f; /* other axis set to 0.0 */
        }
-
-       n[0] *= l;
-       n[1] *= l;
-       n[2] *= l;
-
-       normal[0] = (float) n[0];
-       normal[1] = (float) n[1];
-       normal[2] = (float) n[2];
 }
 
 void mesh_calc_poly_normal_coords(MPoly *mpoly, MLoop *loopstart,
index 36b11b2c28bb253db631264dd118f90888cdebc6..c8754681e41a4fa69cb37f771b4aedca1605fd81 100644 (file)
@@ -2902,9 +2902,10 @@ static void draw_em_measure_stats(View3D *v3d, Object *ob, BMEditMesh *em, UnitS
 
 #define DRAW_EM_MEASURE_STATS_FACEAREA()                                      \
        if (BM_elem_flag_test(f, BM_ELEM_SELECT)) {                               \
-               mul_v3_fl(vmid, 1.0 / n);                                             \
+               mul_v3_fl(vmid, 1.0f / (float)n);                                     \
                if (unit->system)                                                     \
-                       bUnit_AsString(numstr, sizeof(numstr), area * unit->scale_length, \
+                       bUnit_AsString(numstr, sizeof(numstr),                            \
+                                  (double)(area * unit->scale_length),               \
                                       3, unit->system, B_UNIT_LENGTH, do_split, FALSE);  \
                else                                                                  \
                        BLI_snprintf(numstr, sizeof(numstr), conv_float, area);           \
index 29931e160564954d982fbebcc39eaa76c3b1de4d..f986be5eaeb4b5b68df86deb70e76ddec9643163 100644 (file)
@@ -401,7 +401,7 @@ void strand_shade_segment(Render *re, StrandShadeCache *cache, StrandSegment *ss
 
        /* apply alpha along width */
        if (sseg->buffer->widthfade != 0.0f) {
-               s = 1.0f - pow(fabs(s), sseg->buffer->widthfade);
+               s = 1.0f - powf(fabsf(s), sseg->buffer->widthfade);
 
                strand_apply_shaderesult_alpha(ssamp->shr, s);
        }