[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [47272] trunk/blender/source/blender/ blenlib: math lib changes from tomato

Campbell Barton ideasman42 at gmail.com
Thu May 31 13:57:09 CEST 2012


Revision: 47272
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=47272
Author:   campbellbarton
Date:     2012-05-31 11:57:09 +0000 (Thu, 31 May 2012)
Log Message:
-----------
math lib changes from tomato

Modified Paths:
--------------
    trunk/blender/source/blender/blenlib/BLI_math_base.h
    trunk/blender/source/blender/blenlib/BLI_math_color.h
    trunk/blender/source/blender/blenlib/BLI_math_geom.h
    trunk/blender/source/blender/blenlib/BLI_math_vector.h
    trunk/blender/source/blender/blenlib/intern/math_color.c
    trunk/blender/source/blender/blenlib/intern/math_geom.c
    trunk/blender/source/blender/blenlib/intern/math_vector.c

Modified: trunk/blender/source/blender/blenlib/BLI_math_base.h
===================================================================
--- trunk/blender/source/blender/blenlib/BLI_math_base.h	2012-05-31 11:55:05 UTC (rev 47271)
+++ trunk/blender/source/blender/blenlib/BLI_math_base.h	2012-05-31 11:57:09 UTC (rev 47272)
@@ -53,6 +53,9 @@
 #ifndef M_SQRT1_2
 #define M_SQRT1_2   0.70710678118654752440
 #endif
+#ifndef M_SQRT3
+#define M_SQRT3   1.7320508075688772
+#endif
 #ifndef M_1_PI
 #define M_1_PI      0.318309886183790671538
 #endif

Modified: trunk/blender/source/blender/blenlib/BLI_math_color.h
===================================================================
--- trunk/blender/source/blender/blenlib/BLI_math_color.h	2012-05-31 11:55:05 UTC (rev 47271)
+++ trunk/blender/source/blender/blenlib/BLI_math_color.h	2012-05-31 11:57:09 UTC (rev 47272)
@@ -69,6 +69,8 @@
 void rgb_to_hsv_v(const float rgb[3], float r_hsv[3]);
 void rgb_to_hsv_compat(float r, float g, float b, float *lh, float *ls, float *lv);
 void rgb_to_hsv_compat_v(const float rgb[3], float r_hsv[3]);
+void rgb_to_lab(float r, float g, float b, float *ll, float *la, float *lb);
+void rgb_to_xyz(float r, float g, float b, float *x, float *y, float *z);
 unsigned int rgb_to_cpack(float r, float g, float b);
 unsigned int hsv_to_cpack(float h, float s, float v);
 
@@ -112,6 +114,8 @@
 void rgb_float_to_uchar(unsigned char col_r[3], const float col_f[3]);
 void rgba_float_to_uchar(unsigned char col_r[4], const float col_f[4]);
 
+void xyz_to_lab(float x, float y, float z, float *l, float *a, float *b);
+
 /***************** lift/gamma/gain / ASC-CDL conversion *****************/
 
 void lift_gamma_gain_to_asc_cdl(float *lift, float *gamma, float *gain, float *offset, float *slope, float *power);

Modified: trunk/blender/source/blender/blenlib/BLI_math_geom.h
===================================================================
--- trunk/blender/source/blender/blenlib/BLI_math_geom.h	2012-05-31 11:55:05 UTC (rev 47271)
+++ trunk/blender/source/blender/blenlib/BLI_math_geom.h	2012-05-31 11:57:09 UTC (rev 47272)
@@ -191,6 +191,9 @@
 void barycentric_weights_v2(const float v1[2], const float v2[2], const float v3[2],
                             const float co[2], float w[3]);
 
+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]);
+
 void resolve_tri_uv(float r_uv[2], const float st[2], const float st0[2], const float st1[2], const float st2[2]);
 void resolve_quad_uv(float uv[2], const float st[2], const float st0[2], const float st1[2], const float st2[2], const float st3[2]);
 

Modified: trunk/blender/source/blender/blenlib/BLI_math_vector.h
===================================================================
--- trunk/blender/source/blender/blenlib/BLI_math_vector.h	2012-05-31 11:55:05 UTC (rev 47271)
+++ trunk/blender/source/blender/blenlib/BLI_math_vector.h	2012-05-31 11:57:09 UTC (rev 47272)
@@ -228,8 +228,12 @@
 void mul_vn_vn_fl(float *array_tar, const float *array_src, const int size, const float f);
 void add_vn_vn(float *array_tar, const float *array_src, const int size);
 void add_vn_vnvn(float *array_tar, const float *array_src_a, const float *array_src_b, const int size);
+void madd_vn_vn(float *array_tar, const float *array_src, const float f, const int size);
+void madd_vn_vnvn(float *array_tar, const float *array_src_a, const float *array_src_b, const float f, const int size);
 void sub_vn_vn(float *array_tar, const float *array_src, const int size);
 void sub_vn_vnvn(float *array_tar, const float *array_src_a, const float *array_src_b, const int size);
+void msub_vn_vn(float *array_tar, const float *array_src, const float f, const int size);
+void msub_vn_vnvn(float *array_tar, const float *array_src_a, const float *array_src_b, const float f, const int size);
 void fill_vn_i(int *array_tar, const int size, const int val);
 void fill_vn_ushort(unsigned short *array_tar, const int size, const unsigned short val);
 void fill_vn_fl(float *array_tar, const int size, const float val);

Modified: trunk/blender/source/blender/blenlib/intern/math_color.c
===================================================================
--- trunk/blender/source/blender/blenlib/intern/math_color.c	2012-05-31 11:55:05 UTC (rev 47271)
+++ trunk/blender/source/blender/blenlib/intern/math_color.c	2012-05-31 11:57:09 UTC (rev 47272)
@@ -264,6 +264,35 @@
 	rgb_to_hsv(rgb[0], rgb[1], rgb[2], &r_hsv[0], &r_hsv[1], &r_hsv[2]);
 }
 
+void rgb_to_hsl(float r, float g, float b, float *lh, float *ls, float *ll)
+{
+	float cmax = MAX3(r, g, b);
+	float cmin = MIN3(r, g, b);
+	float h, s, l = (cmax + cmin) / 2.0f;
+
+	if (cmax == cmin) {
+		h = s = 0.0f; // achromatic
+	}
+	else {
+		float d = cmax - cmin;
+		s = l > 0.5f ? d / (2.0f - cmax - cmin) : d / (cmax + cmin);
+		if (cmax == r) {
+			h = (g - b) / d + (g < b ? 6.0f : 0.0f);
+		}
+		else if (cmax == g) {
+			h = (b - r) / d + 2.0f;
+		}
+		else {
+			h = (r - g) / d + 4.0f;
+		}
+        }
+        h /= 6.0f;
+
+	*lh = h;
+	*ls = s;
+	*ll = l;
+}
+
 void rgb_to_hsv_compat(float r, float g, float b, float *lh, float *ls, float *lv)
 {
 	float orig_h = *lh;
@@ -603,3 +632,58 @@
 		BLI_color_to_srgb_table[i] = b * 0x100;
 	}
 }
+static float inverse_srgb_companding(float v)
+{
+    if (v > 0.04045f) {
+        return powf((v + 0.055f) / 1.055f, 2.4);
+    }
+    else {
+        return v / 12.92f;
+    }
+}
+
+void rgb_to_xyz(float r, float g, float b, float *x, float *y, float *z)
+{
+    r = inverse_srgb_companding(r) * 100.0f;
+    g = inverse_srgb_companding(g) * 100.0f;
+    b = inverse_srgb_companding(b) * 100.0f;
+
+    *x = r * 0.4124 + g * 0.3576 + b * 0.1805;
+    *y = r * 0.2126 + g * 0.7152 + b * 0.0722;
+    *z = r * 0.0193 + g * 0.1192 + b * 0.9505;
+}
+
+static float xyz_to_lab_component(float v)
+{
+	const float eps = 0.008856f;
+	const float k = 903.3f;
+
+	if (v > eps) {
+		return pow(v, 1.0f / 3.0f);
+	}
+	else {
+		return (k * v + 16.0f) / 116.0f;
+	}
+}
+
+void xyz_to_lab(float x, float y, float z, float *l, float *a, float *b)
+{
+	float xr = x / 95.047f;
+	float yr = y / 100.0f;
+	float zr = z / 108.883f;
+
+	float fx = xyz_to_lab_component(xr);
+	float fy = xyz_to_lab_component(yr);
+	float fz = xyz_to_lab_component(zr);
+
+	*l = 116.0f * fy - 16.0f;
+	*a = 500.0f * (fx - fy);
+	*b = 200.0f * (fy - fz);
+}
+
+void rgb_to_lab(float r, float g, float b, float *ll, float *la, float *lb)
+{
+	float x, y, z;
+	rgb_to_xyz(r, g, b, &x, &y, &z);
+	xyz_to_lab(x, y, z, ll, la, lb);
+}

Modified: trunk/blender/source/blender/blenlib/intern/math_geom.c
===================================================================
--- trunk/blender/source/blender/blenlib/intern/math_geom.c	2012-05-31 11:55:05 UTC (rev 47271)
+++ trunk/blender/source/blender/blenlib/intern/math_geom.c	2012-05-31 11:57:09 UTC (rev 47272)
@@ -1825,6 +1825,45 @@
 	}
 }
 
+/* return 1 of point is inside triangle, 2 if it's on the edge, 0 if point is outside of triangle */
+int barycentric_inside_triangle_v2(const float w[3])
+{
+	if (IN_RANGE(w[0], 0.0f, 1.0f) &&
+	    IN_RANGE(w[1], 0.0f, 1.0f) &&
+	    IN_RANGE(w[2], 0.0f, 1.0f))
+	{
+		return 1;
+	}
+	else if (IN_RANGE_INCL(w[0], 0.0f, 1.0f) &&
+	         IN_RANGE_INCL(w[1], 0.0f, 1.0f) &&
+	         IN_RANGE_INCL(w[2], 0.0f, 1.0f))
+	{
+		return 2;
+	}
+
+	return 0;
+}
+
+/* returns 0 for degenerated triangles */
+int barycentric_coords_v2(const float v1[2], const float v2[2], const float v3[2], const float co[2], float w[3])
+{
+	float x = co[0], y = co[1];
+	float x1 = v1[0], y1 = v1[1];
+	float x2 = v2[0], y2 = v2[1];
+	float x3 = v3[0], y3 = v3[1];
+	float det = (y2 - y3) * (x1 - x3) + (x3 - x2) * (y1 - y3);
+
+	if (fabsf(det) > FLT_EPSILON) {
+		w[0] = ((y2 - y3) * (x - x3) + (x3 - x2) * (y - y3)) / det;
+		w[1] = ((y3 - y1) * (x - x3) + (x1 - x3) * (y - y3)) / det;
+		w[2] = 1.0f - w[0] - w[1];
+
+	return 1;
+	}
+
+	return 0;
+}
+
 /* used by projection painting
  * note: using area_tri_signed_v2 means locations outside the triangle are correctly weighted */
 void barycentric_weights_v2(const float v1[2], const float v2[2], const float v3[2], const float co[2], float w[3])

Modified: trunk/blender/source/blender/blenlib/intern/math_vector.c
===================================================================
--- trunk/blender/source/blender/blenlib/intern/math_vector.c	2012-05-31 11:55:05 UTC (rev 47271)
+++ trunk/blender/source/blender/blenlib/intern/math_vector.c	2012-05-31 11:57:09 UTC (rev 47272)
@@ -550,6 +550,27 @@
 	}
 }
 
+void madd_vn_vn(float *array_tar, const float *array_src, const float f, const int size)
+{
+	float *tar = array_tar + (size - 1);
+	const float *src = array_src + (size - 1);
+	int i = size;
+	while (i--) {
+		*(tar--) += *(src--) * f;
+	}
+}
+
+void madd_vn_vnvn(float *array_tar, const float *array_src_a, const float *array_src_b, const float f, const int size)
+{
+	float *tar = array_tar + (size - 1);
+	const float *src_a = array_src_a + (size - 1);
+	const float *src_b = array_src_b + (size - 1);
+	int i = size;
+	while (i--) {
+		*(tar--) = *(src_a--) + (*(src_b--) * f);
+	}
+}
+
 void sub_vn_vn(float *array_tar, const float *array_src, const int size)
 {
 	float *tar = array_tar + (size - 1);
@@ -571,6 +592,27 @@
 	}
 }
 
+void msub_vn_vn(float *array_tar, const float *array_src, const float f, const int size)
+{
+	float *tar = array_tar + (size - 1);
+	const float *src = array_src + (size - 1);
+	int i = size;
+	while (i--) {
+		*(tar--) -= *(src--) * f;
+	}
+}
+
+void msub_vn_vnvn(float *array_tar, const float *array_src_a, const float *array_src_b, const float f, const int size)
+{
+	float *tar = array_tar + (size - 1);
+	const float *src_a = array_src_a + (size - 1);
+	const float *src_b = array_src_b + (size - 1);
+	int i = size;
+	while (i--) {
+		*(tar--) = *(src_a--) - (*(src_b--) * f);
+	}
+}
+
 void fill_vn_i(int *array_tar, const int size, const int val)
 {
 	int *tar = array_tar + (size - 1);




More information about the Bf-blender-cvs mailing list