diff options
author | Campbell Barton <ideasman42@gmail.com> | 2010-01-01 18:57:17 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2010-01-01 18:57:17 +0300 |
commit | 11e529f4f77e6eae519f7b2b91055cf8b2d459ba (patch) | |
tree | 4eb9e112af5c04c9ae0ee4791f5a34a8199957b3 /source | |
parent | 4ab4d2dd20791101215b39609bf174762e42e58d (diff) |
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)
Diffstat (limited to 'source')
-rw-r--r-- | source/blender/blenlib/BLI_math_geom.h | 2 | ||||
-rw-r--r-- | source/blender/blenlib/intern/math_geom.c | 80 | ||||
-rw-r--r-- | source/blender/editors/gpencil/gpencil_paint.c | 54 |
3 files changed, 83 insertions, 53 deletions
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 */ diff --git a/source/blender/editors/gpencil/gpencil_paint.c b/source/blender/editors/gpencil/gpencil_paint.c index 86ca69f6cf6..905f8c482e9 100644 --- a/source/blender/editors/gpencil/gpencil_paint.c +++ b/source/blender/editors/gpencil/gpencil_paint.c @@ -537,59 +537,7 @@ static void gp_stroke_newfrombuffer (tGPsdata *p) depth_arr[i] = 0.9999f; } else if(interp_depth) { - /* found invalid depths, interpolate */ - float valid_last= FLT_MAX; - int valid_ofs= 0; - - float *depth_arr_up= MEM_callocN(sizeof(float) * gpd->sbuffer_size, "depth_points_up"); - float *depth_arr_down= MEM_callocN(sizeof(float) * gpd->sbuffer_size, "depth_points_down"); - - int *depth_tot_up= MEM_callocN(sizeof(int) * gpd->sbuffer_size, "depth_tot_up"); - int *depth_tot_down= MEM_callocN(sizeof(int) * gpd->sbuffer_size, "depth_tot_down"); - - for (i=0; i < gpd->sbuffer_size; i++) { - if(depth_arr[i] == FLT_MAX) { - depth_arr_up[i]= valid_last; - depth_tot_up[i]= ++valid_ofs; - } - else { - valid_last= depth_arr[i]; - valid_ofs= 0; - } - } - - valid_last= FLT_MAX; - valid_ofs= 0; - - for (i=gpd->sbuffer_size-1; i >= 0; i--) { - if(depth_arr[i] == FLT_MAX) { - depth_arr_down[i]= valid_last; - depth_tot_down[i]= ++valid_ofs; - } - else { - valid_last= depth_arr[i]; - valid_ofs= 0; - } - } - - /* now blend */ - for (i=0; i < gpd->sbuffer_size; i++) { - if(depth_arr[i] == FLT_MAX) { - if(depth_arr_up[i] != FLT_MAX && depth_arr_down[i] != FLT_MAX) { - depth_arr[i]= ((depth_arr_up[i] * depth_tot_down[i]) + (depth_arr_down[i] * depth_tot_up[i])) / (float)(depth_tot_down[i] + depth_tot_up[i]); - } else if (depth_arr_up[i] != FLT_MAX) { - depth_arr[i]= depth_arr_up[i]; - } else if (depth_arr_down[i] != FLT_MAX) { - depth_arr[i]= depth_arr_down[i]; - } - } - } - - MEM_freeN(depth_arr_up); - MEM_freeN(depth_arr_down); - - MEM_freeN(depth_tot_up); - MEM_freeN(depth_tot_down); + interp_sparse_array(depth_arr, gpd->sbuffer_size, FLT_MAX); } } |