[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [45141] trunk/blender/source/blender: Fix #30638 and part of #30646.

Antony Riakiotakis kalast at gmail.com
Sun Mar 25 21:02:37 CEST 2012


Revision: 45141
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=45141
Author:   psy-fi
Date:     2012-03-25 19:02:28 +0000 (Sun, 25 Mar 2012)
Log Message:
-----------
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.

Modified Paths:
--------------
    trunk/blender/source/blender/blenlib/BLI_math_vector.h
    trunk/blender/source/blender/blenlib/intern/math_vector.c
    trunk/blender/source/blender/blenlib/intern/math_vector_inline.c
    trunk/blender/source/blender/bmesh/intern/bmesh_polygon.c
    trunk/blender/source/blender/editors/uvedit/uvedit_draw.c

Modified: trunk/blender/source/blender/blenlib/BLI_math_vector.h
===================================================================
--- trunk/blender/source/blender/blenlib/BLI_math_vector.h	2012-03-25 18:19:40 UTC (rev 45140)
+++ trunk/blender/source/blender/blenlib/BLI_math_vector.h	2012-03-25 19:02:28 UTC (rev 45141)
@@ -129,6 +129,7 @@
 /*********************************** Length **********************************/
 
 MINLINE float len_squared_v2(const float v[2]);
+MINLINE float len_squared_v3(const float v[3]);
 MINLINE float len_v2(const float a[2]);
 MINLINE float len_v2v2(const float a[2], const float b[2]);
 MINLINE float len_squared_v2v2(const float a[2], const float b[2]);
@@ -190,6 +191,7 @@
 
 void project_v2_v2v2(float c[2], const float v1[2], const float v2[2]);
 void project_v3_v3v3(float r[3], const float p[3], const float n[3]);
+void project_v3_plane(float v[3], const float n[3], const float p[3]);
 void reflect_v3_v3v3(float r[3], const float v[3], const float n[3]);
 void ortho_basis_v3v3_v3(float r1[3], float r2[3], const float a[3]);
 void bisect_v3_v3v3v3(float r[3], const float a[3], const float b[3], const float c[3]);

Modified: trunk/blender/source/blender/blenlib/intern/math_vector.c
===================================================================
--- trunk/blender/source/blender/blenlib/intern/math_vector.c	2012-03-25 18:19:40 UTC (rev 45140)
+++ trunk/blender/source/blender/blenlib/intern/math_vector.c	2012-03-25 19:02:28 UTC (rev 45141)
@@ -286,6 +286,20 @@
 	c[2] = mul * v2[2];
 }
 
+/* project a vector on a plane defined by normal and a plane point p */
+void project_v3_plane(float v[3], const float n[3], const float p[3])
+{
+	float vector[3];
+	float mul;
+
+	sub_v3_v3v3(vector, v, p);
+	mul = dot_v3v3(vector, n)/len_squared_v3(n);
+
+	mul_v3_v3fl(vector, n, mul);
+
+	sub_v3_v3(v, vector);
+}
+
 /* Returns a vector bisecting the angle at v2 formed by v1, v2 and v3 */
 void bisect_v3_v3v3v3(float out[3], const float v1[3], const float v2[3], const float v3[3])
 {

Modified: trunk/blender/source/blender/blenlib/intern/math_vector_inline.c
===================================================================
--- trunk/blender/source/blender/blenlib/intern/math_vector_inline.c	2012-03-25 18:19:40 UTC (rev 45140)
+++ trunk/blender/source/blender/blenlib/intern/math_vector_inline.c	2012-03-25 19:02:28 UTC (rev 45141)
@@ -498,6 +498,11 @@
 	return v[0] * v[0] + v[1] * v[1];
 }
 
+MINLINE float len_squared_v3(const float v[3])
+{
+	return v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
+}
+
 MINLINE float len_v2(const float v[2])
 {
 	return (float)sqrtf(v[0] * v[0] + v[1] * v[1]);

Modified: trunk/blender/source/blender/bmesh/intern/bmesh_polygon.c
===================================================================
--- trunk/blender/source/blender/bmesh/intern/bmesh_polygon.c	2012-03-25 18:19:40 UTC (rev 45140)
+++ trunk/blender/source/blender/bmesh/intern/bmesh_polygon.c	2012-03-25 19:02:28 UTC (rev 45141)
@@ -160,40 +160,62 @@
  * \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 @@
 
 	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);
 

Modified: trunk/blender/source/blender/editors/uvedit/uvedit_draw.c
===================================================================
--- trunk/blender/source/blender/editors/uvedit/uvedit_draw.c	2012-03-25 18:19:40 UTC (rev 45140)
+++ trunk/blender/source/blender/editors/uvedit/uvedit_draw.c	2012-03-25 19:02:28 UTC (rev 45141)
@@ -422,6 +422,9 @@
 #endif
 		}
 	}
+
+	BLI_array_free(tf_uv);
+	BLI_array_free(tf_uvorig);
 }
 
 static void draw_uvs_other(Scene *scene, Object *obedit, Image *curimage)




More information about the Bf-blender-cvs mailing list