diff options
author | Antony Riakiotakis <kalast@gmail.com> | 2012-03-25 23:02:28 +0400 |
---|---|---|
committer | Antony Riakiotakis <kalast@gmail.com> | 2012-03-25 23:02:28 +0400 |
commit | eb104023650f3e67c80f889df61060acc2c09123 (patch) | |
tree | 5685dbc46114fe8ec7756ea650f9324e58b2bc05 /source/blender/bmesh | |
parent | 337be23cea3aac29a41c571fa345380c8a0fa404 (diff) |
Fix #30638 and part of #30646.
Problem was that area calculation of polygons was done relative to the xy plane, and with a very obscure (to me at least) algorithm. That meant that vertical ngons would get 0 area.
Commented initial code in case this is a strange optimization case that someone wants to use and used a cleaner algorithm: first project vertices to the ngon plane, defined by the normal of the ngon and the center (mean) of the ngon vertices. This will only be exact for convex and mostly planar ngons, still it is much better than the previous code.
Also fixed memory leak when stretch display was on.
Diffstat (limited to 'source/blender/bmesh')
-rw-r--r-- | source/blender/bmesh/intern/bmesh_polygon.c | 46 |
1 files changed, 33 insertions, 13 deletions
diff --git a/source/blender/bmesh/intern/bmesh_polygon.c b/source/blender/bmesh/intern/bmesh_polygon.c index bdecdfee850..34a6ac8ca69 100644 --- a/source/blender/bmesh/intern/bmesh_polygon.c +++ b/source/blender/bmesh/intern/bmesh_polygon.c @@ -160,40 +160,62 @@ static void bm_face_compute_poly_normal_vertex_cos(BMFace *f, float n[3], * \brief COMPUTE POLY CENTER * * Computes the centroid and - * area of a polygon in the X/Y - * plane. + * area of a polygon in the normal + * plane. This is not an exact method as we can't access the tesselated data. */ -static int compute_poly_center(float center[3], float *r_area, float (* const verts)[3], int nverts) +static void compute_poly_center(float center[3], float *r_area, float (* const verts)[3], int nverts, float normal[3]) { int i, j; - float atmp = 0.0f, xtmp = 0.0f, ytmp = 0.0f, ai; + float area = 0.0; zero_v3(center); if (nverts < 3) - return FALSE; + return; i = nverts - 1; - j = 0; - while (j < nverts) { + /* calculate face center */ + for (j = 0; j < nverts; j++){ + add_v3_v3(center, verts[j]); + } + mul_v3_fl(center, 1.0/nverts); + + /* project vertices to the normal plane */ + for (j = 0; j < nverts; j++){ + project_v3_plane(verts[j], normal, center); + } + + /* add triangle area of triangles formed by polygon edges and center. + * For each triangle Sum(Base * Height / 2) + * The expression for closed polygons is simplified */ + for (j = 0; j < nverts; j++) { + area += area_tri_v3(verts[j], verts[i], center); + i = j; + } + + if (r_area) + *r_area = area; + + /* + for (j = 0; j < nverts; j++) { ai = verts[i][0] * verts[j][1] - verts[j][0] * verts[i][1]; atmp += ai; xtmp += (verts[j][0] + verts[i][0]) * ai; ytmp += (verts[j][1] + verts[i][1]) * ai; i = j; - j += 1; } if (r_area) *r_area = atmp / 2.0f; - + if (atmp != 0) { center[0] = xtmp / (3.0f * atmp); center[1] = xtmp / (3.0f * atmp); return TRUE; } return FALSE; + */ } /** @@ -210,13 +232,11 @@ float BM_face_area_calc(BMesh *bm, BMFace *f) BLI_array_fixedstack_declare(verts, BM_NGON_STACK_SIZE, f->len, __func__); - i = 0; - BM_ITER(l, &iter, bm, BM_LOOPS_OF_FACE, f) { + BM_ITER_INDEX(l, &iter, bm, BM_LOOPS_OF_FACE, f, i) { copy_v3_v3(verts[i], l->v->co); - i++; } - compute_poly_center(center, &area, verts, f->len); + compute_poly_center(center, &area, verts, f->len, f->no); BLI_array_fixedstack_free(verts); |