[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [48957] trunk/blender/source/blender/ blenlib: add 2d version of interp_weights_poly_v3

Campbell Barton ideasman42 at gmail.com
Mon Jul 16 12:36:40 CEST 2012


Revision: 48957
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=48957
Author:   campbellbarton
Date:     2012-07-16 10:36:40 +0000 (Mon, 16 Jul 2012)
Log Message:
-----------
add 2d version of interp_weights_poly_v3

Modified Paths:
--------------
    trunk/blender/source/blender/blenlib/BLI_math_geom.h
    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	2012-07-16 09:41:38 UTC (rev 48956)
+++ trunk/blender/source/blender/blenlib/BLI_math_geom.h	2012-07-16 10:36:40 UTC (rev 48957)
@@ -191,7 +191,8 @@
 /* tri or quad, d can be NULL */
 void interp_weights_face_v3(float w[4],
                             const float a[3], const float b[3], const float c[3], const float d[3], const float p[3]);
-void interp_weights_poly_v3(float w[], float v[][3], const int n, const float p[3]);
+void interp_weights_poly_v3(float w[], float v[][3], const int n, const float co[3]);
+void interp_weights_poly_v2(float w[], float v[][2], const int n, const float co[2]);
 
 void interp_cubic_v3(float x[3], float v[3],
                      const float x1[3], const float v1[3], const float x2[3], const float v2[3], const float t);

Modified: trunk/blender/source/blender/blenlib/intern/math_geom.c
===================================================================
--- trunk/blender/source/blender/blenlib/intern/math_geom.c	2012-07-16 09:41:38 UTC (rev 48956)
+++ trunk/blender/source/blender/blenlib/intern/math_geom.c	2012-07-16 10:36:40 UTC (rev 48957)
@@ -2148,7 +2148,7 @@
 
 /* Mean value weights - smooth interpolation weights for polygons with
  * more than 3 vertices */
-static float mean_value_half_tan(const float v1[3], const float v2[3], const float v3[3])
+static float mean_value_half_tan_v3(const float v1[3], const float v2[3], const float v3[3])
 {
 	float d2[3], d3[3], cross[3], area, dot, len;
 
@@ -2160,10 +2160,32 @@
 	dot = dot_v3v3(d2, d3);
 	len = len_v3(d2) * len_v3(d3);
 
-	if (area == 0.0f)
+	if (LIKELY(area != 0.0f)) {
+		return (len - dot) / area;
+	}
+	else {
 		return 0.0f;
-	else
+	}
+}
+static float mean_value_half_tan_v2(const float v1[2], const float v2[2], const float v3[2])
+{
+	float d2[2], d3[2], area, dot, len;
+
+	sub_v2_v2v2(d2, v2, v1);
+	sub_v2_v2v2(d3, v3, v1);
+
+	/* different from the 3d version but still correct */
+	area = cross_v2v2(d2, d3);
+
+	dot = dot_v2v2(d2, d3);
+	len = len_v2(d2) * len_v2(d3);
+
+	if (LIKELY(area != 0.0f)) {
 		return (len - dot) / area;
+	}
+	else {
+		return 0.0f;
+	}
 }
 
 void interp_weights_poly_v3(float *w, float v[][3], const int n, const float co[3])
@@ -2178,19 +2200,48 @@
 		vprev = (i == 0) ? v[n - 1] : v[i - 1];
 		vnext = (i == n - 1) ? v[0] : v[i + 1];
 
-		t1 = mean_value_half_tan(co, vprev, vmid);
-		t2 = mean_value_half_tan(co, vmid, vnext);
+		t1 = mean_value_half_tan_v3(co, vprev, vmid);
+		t2 = mean_value_half_tan_v3(co, vmid, vnext);
 
 		len = len_v3v3(co, vmid);
 		w[i] = (t1 + t2) / len;
 		totweight += w[i];
 	}
 
-	if (totweight != 0.0f)
-		for (i = 0; i < n; i++)
+	if (totweight != 0.0f) {
+		for (i = 0; i < n; i++) {
 			w[i] /= totweight;
+		}
+	}
 }
 
+void interp_weights_poly_v2(float *w, float v[][2], const int n, const float co[2])
+{
+	float totweight, t1, t2, len, *vmid, *vprev, *vnext;
+	int i;
+
+	totweight = 0.0f;
+
+	for (i = 0; i < n; i++) {
+		vmid = v[i];
+		vprev = (i == 0) ? v[n - 1] : v[i - 1];
+		vnext = (i == n - 1) ? v[0] : v[i + 1];
+
+		t1 = mean_value_half_tan_v2(co, vprev, vmid);
+		t2 = mean_value_half_tan_v2(co, vmid, vnext);
+
+		len = len_v2v2(co, vmid);
+		w[i] = (t1 + t2) / len;
+		totweight += w[i];
+	}
+
+	if (totweight != 0.0f) {
+		for (i = 0; i < n; i++) {
+			w[i] /= totweight;
+		}
+	}
+}
+
 /* (x1,v1)(t1=0)------(x2,v2)(t2=1), 0<t<1 --> (x,v)(t) */
 void interp_cubic_v3(float x[3], float v[3], const float x1[3], const float v1[3], const float x2[3], const float v2[3], const float t)
 {




More information about the Bf-blender-cvs mailing list