[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [55289] trunk/blender/source/blender/ blenlib: move polygon intersection out of BLI_lasso into BLI_math_geom since its a generally useful function .

Campbell Barton ideasman42 at gmail.com
Thu Mar 14 22:44:17 CET 2013


Revision: 55289
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=55289
Author:   campbellbarton
Date:     2013-03-14 21:44:16 +0000 (Thu, 14 Mar 2013)
Log Message:
-----------
move polygon intersection out of BLI_lasso into BLI_math_geom since its a generally useful function.

adds:
- isect_point_poly_v2()
- isect_point_poly_v2_int()

Modified Paths:
--------------
    trunk/blender/source/blender/blenlib/BLI_math_geom.h
    trunk/blender/source/blender/blenlib/intern/lasso.c
    trunk/blender/source/blender/blenlib/intern/math_geom.c

Modified: trunk/blender/source/blender/blenlib/BLI_math_geom.h
===================================================================
--- trunk/blender/source/blender/blenlib/BLI_math_geom.h	2013-03-14 19:40:42 UTC (rev 55288)
+++ trunk/blender/source/blender/blenlib/BLI_math_geom.h	2013-03-14 21:44:16 UTC (rev 55289)
@@ -135,13 +135,15 @@
                              const float v0[3], const float v1[3], const float v2[3], float *r_lambda, float r_uv[2], const float epsilon);
 
 /* point in polygon */
+bool isect_point_poly_v2(const float pt[2], const float verts[][2], const int nr);
+bool isect_point_poly_v2_int(const int pt[2], const int verts[][2], const int nr);
+
 int isect_point_quad_v2(const float p[2], const float a[2], const float b[2], const float c[2], const float d[2]);
 
 int isect_point_tri_v2(const float pt[2], const float v1[2], const float v2[2], const float v3[2]);
 int isect_point_tri_v2_cw(const float pt[2], const float v1[2], const float v2[2], const float v3[2]);
 int isect_point_tri_v2_int(const int x1, const int y1, const int x2, const int y2, const int a, const int b);
 int isect_point_tri_prism_v3(const float p[3], const float v1[3], const float v2[3], const float v3[3]);
-
 void isect_point_quad_uv_v2(const float v0[2], const float v1[2], const float v2[2], const float v3[2],
                             const float pt[2], float r_uv[2]);
 void isect_point_face_uv_v2(const int isquad, const float v0[2], const float v1[2], const float v2[2],

Modified: trunk/blender/source/blender/blenlib/intern/lasso.c
===================================================================
--- trunk/blender/source/blender/blenlib/intern/lasso.c	2013-03-14 19:40:42 UTC (rev 55288)
+++ trunk/blender/source/blender/blenlib/intern/lasso.c	2013-03-14 21:44:16 UTC (rev 55289)
@@ -57,46 +57,13 @@
                                const int sx, const int sy,
                                const int error_value)
 {
-	/* we do the angle rule, define that all added angles should be about zero or (2 * PI) */
-	float angletot = 0.0, dot, ang, cross, fp1[2], fp2[2];
-	int a;
-	const int *p1, *p2;
-
 	if (sx == error_value) {
 		return false;
 	}
-
-	p1 = mcords[moves - 1];
-	p2 = mcords[0];
-
-	/* first vector */
-	fp1[0] = (float)(p1[0] - sx);
-	fp1[1] = (float)(p1[1] - sy);
-	normalize_v2(fp1);
-
-	for (a = 0; a < moves; a++) {
-		/* second vector */
-		fp2[0] = (float)(p2[0] - sx);
-		fp2[1] = (float)(p2[1] - sy);
-		normalize_v2(fp2);
-
-		/* dot and angle and cross */
-		dot = fp1[0] * fp2[0] + fp1[1] * fp2[1];
-		ang = fabs(saacos(dot));
-
-		cross = (float)((p1[1] - p2[1]) * (p1[0] - sx) + (p2[0] - p1[0]) * (p1[1] - sy));
-
-		if (cross < 0.0f) angletot -= ang;
-		else angletot += ang;
-
-		/* circulate */
-		fp1[0] = fp2[0]; fp1[1] = fp2[1];
-		p1 = p2;
-		p2 = mcords[a + 1];
+	else {
+		int pt[2] = {sx, sy};
+		return isect_point_poly_v2_int(pt, mcords, moves);
 	}
-
-	if (fabsf(angletot) > 4.0f) return true;
-	return false;
 }
 
 /* edge version for lasso select. we assume boundbox check was done */

Modified: trunk/blender/source/blender/blenlib/intern/math_geom.c
===================================================================
--- trunk/blender/source/blender/blenlib/intern/math_geom.c	2013-03-14 19:40:42 UTC (rev 55288)
+++ trunk/blender/source/blender/blenlib/intern/math_geom.c	2013-03-14 21:44:16 UTC (rev 55289)
@@ -728,6 +728,86 @@
 	return 1;
 }
 
+/* point in polygon (keep float and int versions in sync) */
+bool isect_point_poly_v2(const float pt[2], const float verts[][2], const int nr)
+{
+	/* we do the angle rule, define that all added angles should be about zero or (2 * PI) */
+	float angletot = 0.0;
+	float fp1[2], fp2[2];
+	int i;
+	const float *p1, *p2;
+
+	p1 = verts[nr - 1];
+	p2 = verts[0];
+
+	/* first vector */
+	fp1[0] = (float)(p1[0] - pt[0]);
+	fp1[1] = (float)(p1[1] - pt[1]);
+	normalize_v2(fp1);
+
+	for (i = 0; i < nr; i++) {
+		float dot, ang, cross;
+		/* second vector */
+		fp2[0] = (float)(p2[0] - pt[0]);
+		fp2[1] = (float)(p2[1] - pt[1]);
+		normalize_v2(fp2);
+
+		/* dot and angle and cross */
+		dot = dot_v2v2(fp1, fp2);
+		ang = fabsf(saacos(dot));
+		cross = (float)((p1[1] - p2[1]) * (p1[0] - pt[0]) + (p2[0] - p1[0]) * (p1[1] - pt[1]));
+
+		if (cross < 0.0f) angletot -= ang;
+		else              angletot += ang;
+
+		/* circulate */
+		copy_v2_v2(fp1, fp2);
+		p1 = p2;
+		p2 = verts[i + 1];
+	}
+
+	return (fabsf(angletot) > 4.0f);
+}
+bool isect_point_poly_v2_int(const int pt[2], const int verts[][2], const int nr)
+{
+	/* we do the angle rule, define that all added angles should be about zero or (2 * PI) */
+	float angletot = 0.0;
+	float fp1[2], fp2[2];
+	int i;
+	const int *p1, *p2;
+
+	p1 = verts[nr - 1];
+	p2 = verts[0];
+
+	/* first vector */
+	fp1[0] = (float)(p1[0] - pt[0]);
+	fp1[1] = (float)(p1[1] - pt[1]);
+	normalize_v2(fp1);
+
+	for (i = 0; i < nr; i++) {
+		float dot, ang, cross;
+		/* second vector */
+		fp2[0] = (float)(p2[0] - pt[0]);
+		fp2[1] = (float)(p2[1] - pt[1]);
+		normalize_v2(fp2);
+
+		/* dot and angle and cross */
+		dot = dot_v2v2(fp1, fp2);
+		ang = fabsf(saacos(dot));
+		cross = (float)((p1[1] - p2[1]) * (p1[0] - pt[0]) + (p2[0] - p1[0]) * (p1[1] - pt[1]));
+
+		if (cross < 0.0f) angletot -= ang;
+		else              angletot += ang;
+
+		/* circulate */
+		copy_v2_v2(fp1, fp2);
+		p1 = p2;
+		p2 = verts[i + 1];
+	}
+
+	return (fabsf(angletot) > 4.0f);
+}
+
 /* point in tri */
 
 /* only single direction */




More information about the Bf-blender-cvs mailing list