[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [48885] trunk/blender/source/blender/ blenlib: new function barycentric_weights_v2_quad(), like barycentric_weights_v2 but for quads.

Campbell Barton ideasman42 at gmail.com
Fri Jul 13 11:19:05 CEST 2012


Revision: 48885
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=48885
Author:   campbellbarton
Date:     2012-07-13 09:19:05 +0000 (Fri, 13 Jul 2012)
Log Message:
-----------
new function barycentric_weights_v2_quad(), like barycentric_weights_v2 but for quads.

takes vecs and a point and assigns 4 weights, needed for nice quad interpolation for mask feathering.

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-13 07:13:27 UTC (rev 48884)
+++ trunk/blender/source/blender/blenlib/BLI_math_geom.h	2012-07-13 09:19:05 UTC (rev 48885)
@@ -200,6 +200,8 @@
 
 void barycentric_weights_v2(const float v1[2], const float v2[2], const float v3[2],
                             const float co[2], float w[3]);
+void barycentric_weights_v2_quad(const float v1[2], const float v2[2], const float v3[2], const float v4[2],
+                                 const float co[2], float w[4]);
 
 int barycentric_coords_v2(const float v1[2], const float v2[2], const float v3[2], const float co[2], float w[3]);
 int barycentric_inside_triangle_v2(const float w[3]);

Modified: trunk/blender/source/blender/blenlib/intern/math_geom.c
===================================================================
--- trunk/blender/source/blender/blenlib/intern/math_geom.c	2012-07-13 07:13:27 UTC (rev 48884)
+++ trunk/blender/source/blender/blenlib/intern/math_geom.c	2012-07-13 09:19:05 UTC (rev 48885)
@@ -1942,6 +1942,46 @@
 	}
 }
 
+/* same as #barycentric_weights_v2 but works with a quad,
+ * note: untested for values outside the quad's bounds.
+ * note: there may be a more efficient method to do this, just figured it out - campbell */
+void barycentric_weights_v2_quad(const float v1[2], const float v2[2], const float v3[2], const float v4[2],
+                                 const float co[2], float w[4])
+{
+	float wtot;
+
+	const float areas_co[4] = {
+	    area_tri_signed_v2(v1, v2, co),
+	    area_tri_signed_v2(v2, v3, co),
+	    area_tri_signed_v2(v3, v4, co),
+	    area_tri_signed_v2(v4, v1, co),
+	};
+
+	const float areas_diag[4] = {
+	    area_tri_signed_v2(v4, v1, v2),
+	    area_tri_signed_v2(v1, v2, v3),
+	    area_tri_signed_v2(v2, v3, v4),
+	    area_tri_signed_v2(v3, v4, v1),
+	};
+
+	const float u = areas_co[3] / (areas_co[1] + areas_co[3]);
+	const float v = areas_co[0] / (areas_co[0] + areas_co[2]);
+
+	w[0] = ((1.0f - u) * (1.0f - v)) * sqrtf(areas_diag[0] / areas_diag[2]);
+	w[1] = ((       u) * (1.0f - v)) * sqrtf(areas_diag[1] / areas_diag[3]);
+	w[2] = ((       u) * (       v)) * sqrtf(areas_diag[2] / areas_diag[0]);
+	w[3] = ((1.0f - u) * (       v)) * sqrtf(areas_diag[3] / areas_diag[1]);
+
+	wtot = w[0] + w[1] + w[2] + w[3];
+
+	if (wtot != 0.0f) {
+		mul_v4_fl(w, 1.0f / wtot);
+	}
+	else { /* dummy values for zero area face */
+		copy_v4_fl(w, 1.0f / 4.0f);
+	}
+}
+
 /* given 2 triangles in 3D space, and a point in relation to the first triangle.
  * calculate the location of a point in relation to the second triangle.
  * Useful for finding relative positions with geometry */




More information about the Bf-blender-cvs mailing list