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)
This commit is contained in:
Campbell Barton
2010-01-01 15:57:17 +00:00
parent 4ab4d2dd20
commit 11e529f4f7
3 changed files with 83 additions and 53 deletions

View File

@@ -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]);

View File

@@ -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 */

View File

@@ -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);
}
}