From 11e529f4f77e6eae519f7b2b91055cf8b2d459ba Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Fri, 1 Jan 2010 15:57:17 +0000 Subject: made the array interpolation function from last commit into a generic function /* given an array with some invalid values this function interpolates valid values * replacing the invalid ones */ int interp_sparse_array(float *array, int list_size, float skipval) --- source/blender/blenlib/BLI_math_geom.h | 2 + source/blender/blenlib/intern/math_geom.c | 80 +++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) (limited to 'source/blender/blenlib') diff --git a/source/blender/blenlib/BLI_math_geom.h b/source/blender/blenlib/BLI_math_geom.h index e676001fba5..0bbe8fac976 100644 --- a/source/blender/blenlib/BLI_math_geom.h +++ b/source/blender/blenlib/BLI_math_geom.h @@ -121,6 +121,8 @@ void interp_weights_poly_v3(float w[], float v[][3], int n, float p[3]); void interp_cubic_v3(float x[3], float v[3], float x1[3], float v1[3], float x2[3], float v2[3], float t); +int interp_sparse_array(float *array, int list_size, float invalid); + void barycentric_transform(float pt_tar[3], float const pt_src[3], const float tri_tar_p1[3], const float tri_tar_p2[3], const float tri_tar_p3[3], const float tri_src_p1[3], const float tri_src_p2[3], const float tri_src_p3[3]); diff --git a/source/blender/blenlib/intern/math_geom.c b/source/blender/blenlib/intern/math_geom.c index 470e5d71a45..2b94227099c 100644 --- a/source/blender/blenlib/intern/math_geom.c +++ b/source/blender/blenlib/intern/math_geom.c @@ -32,6 +32,7 @@ #include "BLI_math.h" #include "BLI_memarena.h" +#include "MEM_guardedalloc.h" /********************************** Polygons *********************************/ @@ -1408,6 +1409,85 @@ void barycentric_transform(float pt_tar[3], float const pt_src[3], madd_v3_v3v3fl(pt_tar, pt_tar, no_tar, (z_ofs_src / area_src) * area_tar); } +/* given an array with some invalid values this function interpolates valid values + * replacing the invalid ones */ +int interp_sparse_array(float *array, int list_size, float skipval) +{ + int found_invalid = 0; + int found_valid = 0; + int i; + + for (i=0; i < list_size; i++) { + if(array[i] == skipval) + found_invalid= 1; + else + found_valid= 1; + } + + if(found_valid==0) { + return -1; + } + else if (found_invalid==0) { + return 0; + } + else { + /* found invalid depths, interpolate */ + float valid_last= skipval; + int valid_ofs= 0; + + float *array_up= MEM_callocN(sizeof(float) * list_size, "interp_sparse_array up"); + float *array_down= MEM_callocN(sizeof(float) * list_size, "interp_sparse_array up"); + + int *ofs_tot_up= MEM_callocN(sizeof(int) * list_size, "interp_sparse_array tup"); + int *ofs_tot_down= MEM_callocN(sizeof(int) * list_size, "interp_sparse_array tdown"); + + for (i=0; i < list_size; i++) { + if(array[i] == skipval) { + array_up[i]= valid_last; + ofs_tot_up[i]= ++valid_ofs; + } + else { + valid_last= array[i]; + valid_ofs= 0; + } + } + + valid_last= skipval; + valid_ofs= 0; + + for (i=list_size-1; i >= 0; i--) { + if(array[i] == skipval) { + array_down[i]= valid_last; + ofs_tot_down[i]= ++valid_ofs; + } + else { + valid_last= array[i]; + valid_ofs= 0; + } + } + + /* now blend */ + for (i=0; i < list_size; i++) { + if(array[i] == skipval) { + if(array_up[i] != skipval && array_down[i] != skipval) { + array[i]= ((array_up[i] * ofs_tot_down[i]) + (array_down[i] * ofs_tot_up[i])) / (float)(ofs_tot_down[i] + ofs_tot_up[i]); + } else if (array_up[i] != skipval) { + array[i]= array_up[i]; + } else if (array_down[i] != skipval) { + array[i]= array_down[i]; + } + } + } + + MEM_freeN(array_up); + MEM_freeN(array_down); + + MEM_freeN(ofs_tot_up); + MEM_freeN(ofs_tot_down); + } + + return 1; +} /* Mean value weights - smooth interpolation weights for polygons with * more than 3 vertices */ -- cgit v1.2.3