[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [51237] trunk/blender/source/blender: refactor screen foreach functions to accept float[2] arguments rather then int pairs.

Campbell Barton ideasman42 at gmail.com
Wed Oct 10 03:22:22 CEST 2012


Revision: 51237
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=51237
Author:   campbellbarton
Date:     2012-10-10 01:22:19 +0000 (Wed, 10 Oct 2012)
Log Message:
-----------
refactor screen foreach functions to accept float[2] arguments rather then int pairs.
overall means less converting between float and int (and short in some cases).

Modified Paths:
--------------
    trunk/blender/source/blender/blenlib/BLI_math_vector.h
    trunk/blender/source/blender/blenlib/BLI_rect.h
    trunk/blender/source/blender/blenlib/intern/math_vector_inline.c
    trunk/blender/source/blender/blenlib/intern/rct.c
    trunk/blender/source/blender/editors/curve/editcurve.c
    trunk/blender/source/blender/editors/gpencil/gpencil_paint.c
    trunk/blender/source/blender/editors/include/ED_mesh.h
    trunk/blender/source/blender/editors/include/ED_view3d.h
    trunk/blender/source/blender/editors/mesh/editmesh_knife.c
    trunk/blender/source/blender/editors/mesh/editmesh_loopcut.c
    trunk/blender/source/blender/editors/mesh/editmesh_select.c
    trunk/blender/source/blender/editors/mesh/meshtools.c
    trunk/blender/source/blender/editors/object/object_lattice.c
    trunk/blender/source/blender/editors/space_view3d/drawobject.c
    trunk/blender/source/blender/editors/space_view3d/view3d_draw.c
    trunk/blender/source/blender/editors/space_view3d/view3d_select.c

Modified: trunk/blender/source/blender/blenlib/BLI_math_vector.h
===================================================================
--- trunk/blender/source/blender/blenlib/BLI_math_vector.h	2012-10-09 22:55:05 UTC (rev 51236)
+++ trunk/blender/source/blender/blenlib/BLI_math_vector.h	2012-10-10 01:22:19 UTC (rev 51237)
@@ -139,12 +139,16 @@
 
 MINLINE float len_squared_v2(const float v[2]);
 MINLINE float len_squared_v3(const float v[3]);
+MINLINE float len_manhattan_v2(const float v[2]);
+MINLINE float len_manhattan_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]);
+MINLINE float len_squared_v3v3(const float a[3], const float b[3]);
+MINLINE float len_manhattan_v2v2(const float a[2], const float b[2]);
+MINLINE float len_manhattan_v3v3(const float a[3], const float b[3]);
 MINLINE float len_v3(const float a[3]);
 MINLINE float len_v3v3(const float a[3], const float b[3]);
-MINLINE float len_squared_v3v3(const float a[3], const float b[3]);
 
 MINLINE float normalize_v2(float r[2]);
 MINLINE float normalize_v2_v2(float r[2], const float a[2]);

Modified: trunk/blender/source/blender/blenlib/BLI_rect.h
===================================================================
--- trunk/blender/source/blender/blenlib/BLI_rect.h	2012-10-09 22:55:05 UTC (rev 51236)
+++ trunk/blender/source/blender/blenlib/BLI_rect.h	2012-10-10 01:22:19 UTC (rev 51237)
@@ -66,9 +66,7 @@
 int  BLI_rctf_isect_pt(const struct rctf *rect, const float x, const float y);
 int  BLI_rctf_isect_pt_v(const struct rctf *rect, const float xy[2]);
 int  BLI_rcti_isect_segment(const struct rcti *rect, const int s1[2], const int s2[2]);
-#if 0 /* NOT NEEDED YET */
-int  BLI_rctf_isect_segment(struct rcti *rect, int s1[2], int s2[2]);
-#endif
+int  BLI_rctf_isect_segment(const struct rctf *rect, const float s1[2], const float s2[2]);
 void BLI_rctf_union(struct rctf *rctf1, const struct rctf *rctf2);
 void BLI_rcti_union(struct rcti *rcti1, const struct rcti *rcti2);
 void BLI_rcti_rctf_copy(struct rcti *dst, const struct rctf *src);

Modified: trunk/blender/source/blender/blenlib/intern/math_vector_inline.c
===================================================================
--- trunk/blender/source/blender/blenlib/intern/math_vector_inline.c	2012-10-09 22:55:05 UTC (rev 51236)
+++ trunk/blender/source/blender/blenlib/intern/math_vector_inline.c	2012-10-10 01:22:19 UTC (rev 51237)
@@ -561,6 +561,16 @@
 	return v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
 }
 
+MINLINE float len_manhattan_v2(const float v[2])
+{
+	return fabsf(v[0]) + fabsf(v[1]);
+}
+
+MINLINE float len_manhattan_v3(const float v[3])
+{
+	return fabsf(v[0]) + fabsf(v[1]) + fabsf(v[2]);
+}
+
 MINLINE float len_v2(const float v[2])
 {
 	return sqrtf(v[0] * v[0] + v[1] * v[1]);
@@ -588,22 +598,38 @@
 	return dot_v2v2(d, d);
 }
 
-MINLINE float len_v3v3(const float a[3], const float b[3])
+MINLINE float len_squared_v3v3(const float a[3], const float b[3])
 {
 	float d[3];
 
 	sub_v3_v3v3(d, b, a);
-	return len_v3(d);
+	return dot_v3v3(d, d);
 }
 
-MINLINE float len_squared_v3v3(const float a[3], const float b[3])
+MINLINE float len_manhattan_v2v2(const float a[2], const float b[2])
 {
+	float d[2];
+
+	sub_v2_v2v2(d, b, a);
+	return len_manhattan_v2(d);
+}
+
+MINLINE float len_manhattan_v3v3(const float a[3], const float b[3])
+{
 	float d[3];
 
 	sub_v3_v3v3(d, b, a);
-	return dot_v3v3(d, d);
+	return len_manhattan_v3(d);
 }
 
+MINLINE float len_v3v3(const float a[3], const float b[3])
+{
+	float d[3];
+
+	sub_v3_v3v3(d, b, a);
+	return len_v3(d);
+}
+
 MINLINE float normalize_v2_v2(float r[2], const float a[2])
 {
 	float d = dot_v2v2(a, a);

Modified: trunk/blender/source/blender/blenlib/intern/rct.c
===================================================================
--- trunk/blender/source/blender/blenlib/intern/rct.c	2012-10-09 22:55:05 UTC (rev 51236)
+++ trunk/blender/source/blender/blenlib/intern/rct.c	2012-10-10 01:22:19 UTC (rev 51237)
@@ -102,7 +102,7 @@
 }
 
 /* based closely on 'isect_line_line_v2_int', but in modified so corner cases are treated as intersections */
-static int isect_segments(const int v1[2], const int v2[2], const int v3[2], const int v4[2])
+static int isect_segments_i(const int v1[2], const int v2[2], const int v3[2], const int v4[2])
 {
 	const double div = (double)((v2[0] - v1[0]) * (v4[1] - v3[1]) - (v2[1] - v1[1]) * (v4[0] - v3[0]));
 	if (div == 0.0f) {
@@ -114,6 +114,18 @@
 		return (labda >= 0.0f && labda <= 1.0f && mu >= 0.0f && mu <= 1.0f);
 	}
 }
+static int isect_segments_fl(const float v1[2], const float v2[2], const float v3[2], const float v4[2])
+{
+	const double div = (double)((v2[0] - v1[0]) * (v4[1] - v3[1]) - (v2[1] - v1[1]) * (v4[0] - v3[0]));
+	if (div == 0.0f) {
+		return 1; /* co-linear */
+	}
+	else {
+		const double labda = (double)((v1[1] - v3[1]) * (v4[0] - v3[0]) - (v1[0] - v3[0]) * (v4[1] - v3[1])) / div;
+		const double mu    = (double)((v1[1] - v3[1]) * (v2[0] - v1[0]) - (v1[0] - v3[0]) * (v2[1] - v1[1])) / div;
+		return (labda >= 0.0f && labda <= 1.0f && mu >= 0.0f && mu <= 1.0f);
+	}
+}
 
 int BLI_rcti_isect_segment(const rcti *rect, const int s1[2], const int s2[2])
 {
@@ -134,14 +146,14 @@
 		/* diagonal: [/] */
 		tvec1[0] = rect->xmin; tvec1[1] = rect->ymin;
 		tvec2[0] = rect->xmin; tvec2[1] = rect->ymax;
-		if (isect_segments(s1, s2, tvec1, tvec2)) {
+		if (isect_segments_i(s1, s2, tvec1, tvec2)) {
 			return 1;
 		}
 
 		/* diagonal: [\] */
 		tvec1[0] = rect->xmin; tvec1[1] = rect->ymax;
 		tvec2[0] = rect->xmax; tvec2[1] = rect->ymin;
-		if (isect_segments(s1, s2, tvec1, tvec2)) {
+		if (isect_segments_i(s1, s2, tvec1, tvec2)) {
 			return 1;
 		}
 
@@ -150,6 +162,41 @@
 	}
 }
 
+int BLI_rctf_isect_segment(const rctf *rect, const float s1[2], const float s2[2])
+{
+	/* first do outside-bounds check for both points of the segment */
+	if (s1[0] < rect->xmin && s2[0] < rect->xmin) return 0;
+	if (s1[0] > rect->xmax && s2[0] > rect->xmax) return 0;
+	if (s1[1] < rect->ymin && s2[1] < rect->ymin) return 0;
+	if (s1[1] > rect->ymax && s2[1] > rect->ymax) return 0;
+
+	/* if either points intersect then we definetly intersect */
+	if (BLI_rctf_isect_pt_v(rect, s1) || BLI_rctf_isect_pt_v(rect, s2)) {
+		return 1;
+	}
+	else {
+		/* both points are outside but may insersect the rect */
+		float tvec1[2];
+		float tvec2[2];
+		/* diagonal: [/] */
+		tvec1[0] = rect->xmin; tvec1[1] = rect->ymin;
+		tvec2[0] = rect->xmin; tvec2[1] = rect->ymax;
+		if (isect_segments_fl(s1, s2, tvec1, tvec2)) {
+			return 1;
+		}
+
+		/* diagonal: [\] */
+		tvec1[0] = rect->xmin; tvec1[1] = rect->ymax;
+		tvec2[0] = rect->xmax; tvec2[1] = rect->ymin;
+		if (isect_segments_fl(s1, s2, tvec1, tvec2)) {
+			return 1;
+		}
+
+		/* no intersection */
+		return 0;
+	}
+}
+
 void BLI_rctf_union(rctf *rct1, const rctf *rct2)
 {
 	if (rct1->xmin > rct2->xmin) rct1->xmin = rct2->xmin;

Modified: trunk/blender/source/blender/editors/curve/editcurve.c
===================================================================
--- trunk/blender/source/blender/editors/curve/editcurve.c	2012-10-09 22:55:05 UTC (rev 51236)
+++ trunk/blender/source/blender/editors/curve/editcurve.c	2012-10-10 01:22:19 UTC (rev 51237)
@@ -3226,12 +3226,12 @@
 
 /******************** find nearest ************************/
 
-static void findnearestNurbvert__doClosest(void *userData, Nurb *nu, BPoint *bp, BezTriple *bezt, int beztindex, int x, int y)
+static void findnearestNurbvert__doClosest(void *userData, Nurb *nu, BPoint *bp, BezTriple *bezt, int beztindex, const float screen_co[2])
 {
-	struct { BPoint *bp; BezTriple *bezt; Nurb *nurb; int dist, hpoint, select, mval[2]; } *data = userData;
+	struct { BPoint *bp; BezTriple *bezt; Nurb *nurb; float dist; int hpoint, select; float mval_fl[2]; } *data = userData;
 
 	short flag;
-	short temp;
+	float dist_test;
 
 	if (bp) {
 		flag = bp->f1;
@@ -3248,12 +3248,12 @@
 		}
 	}
 
-	temp = abs(data->mval[0] - x) + abs(data->mval[1] - y);
-	if ((flag & 1) == data->select) temp += 5;
-	if (bezt && beztindex == 1) temp += 3;  /* middle points get a small disadvantage */
+	dist_test = len_manhattan_v2v2(data->mval_fl, screen_co);
+	if ((flag & SELECT) == data->select) dist_test += 5.0f;
+	if (bezt && beztindex == 1) dist_test += 3.0f;  /* middle points get a small disadvantage */
 
-	if (temp < data->dist) {
-		data->dist = temp;
+	if (dist_test < data->dist) {
+		data->dist = dist_test;
 
 		data->bp = bp;
 		data->bezt = bezt;
@@ -3267,13 +3267,13 @@
 	/* (sel == 1): selected gets a disadvantage */
 	/* in nurb and bezt or bp the nearest is written */
 	/* return 0 1 2: handlepunt */
-	struct { BPoint *bp; BezTriple *bezt; Nurb *nurb; int dist, hpoint, select, mval[2]; } data = {NULL};
+	struct { BPoint *bp; BezTriple *bezt; Nurb *nurb; float dist; int hpoint, select; float mval_fl[2]; } data = {NULL};
 
 	data.dist = 100;
 	data.hpoint = 0;
 	data.select = sel;
-	data.mval[0] = mval[0];
-	data.mval[1] = mval[1];
+	data.mval_fl[0] = mval[0];
+	data.mval_fl[1] = mval[1];
 
 	ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d);
 	nurbs_foreachScreenVert(vc, findnearestNurbvert__doClosest, &data);

Modified: trunk/blender/source/blender/editors/gpencil/gpencil_paint.c
===================================================================
--- trunk/blender/source/blender/editors/gpencil/gpencil_paint.c	2012-10-09 22:55:05 UTC (rev 51236)
+++ trunk/blender/source/blender/editors/gpencil/gpencil_paint.c	2012-10-10 01:22:19 UTC (rev 51237)
@@ -787,11 +787,16 @@
                                            int rad, int x0, int y0, int x1, int y1)
 {
 	/* simple within-radius check for now */
-	if (edge_inside_circle(mval[0], mval[1], rad, x0, y0, x1, y1))
-		return 1;
+	const float mval_fl[2]     = {mval[0], mval[1]};
+	const float screen_co_a[2] = {x0, y0};
+	const float screen_co_b[2] = {x1, y1};
+
+	if (edge_inside_circle(mval_fl, rad, screen_co_a, screen_co_b)) {
+		return TRUE;
+	}
 	
 	/* not inside */
-	return 0;
+	return FALSE;
 } 
 
 static void gp_point_to_xy(ARegion *ar, View2D *v2d, rctf *subrect, bGPDstroke *gps, bGPDspoint *pt,

Modified: trunk/blender/source/blender/editors/include/ED_mesh.h
===================================================================
--- trunk/blender/source/blender/editors/include/ED_mesh.h	2012-10-09 22:55:05 UTC (rev 51236)

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list