int ssab_curve( float x, 
		float *y, 
		float *table,
		int size)
{
	int	x_idx;
	int	x_found;
	float	*ptr;
	float	x1, x2, y1, y2;

#define TABLE_Y(ix)   (*(table + 2 * (ix) - 1))
#define TABLE_X(ix)   (*(table + 2 * (ix) - 2))

	/* Get the x point */
	ptr = table;
	x_found = 0;
	for ( x_idx = 0; x_idx < size; x_idx++)
	{
	  if ( *ptr > x)
	  {
	    x_found = 1;
	    break;
	  }
	  ptr += 2;
	}

	/* Interpolate */
	if ( !x_found )
	{
	  /* Take the very last point */
	  *y = TABLE_Y( size);
	}
	else if ( x_idx == 0 )
	{
	  /* Take the first point */
	  *y = TABLE_Y( 1);
	}
	else
	{
	  y1 = TABLE_Y( x_idx);
	  y2 = TABLE_Y( x_idx + 1);
	  x1 = TABLE_X( x_idx);
	  x2 = TABLE_X( x_idx + 1);
	  *y = y1 + ( y2 - y1) * (x - x1)/(x2 - x1);
	}
	return SUTL__SUCCESS;
}
Ejemplo n.º 2
0
static int
default_wcsmap_interpolate(struct wcsmap_param_t* m,
                           const double xd, const double yd,
                           const integer_t n,
                           double* xin /*[n]*/, double* yin /*[n]*/,
                           /* Output parameters */
                           double* xout, double* yout,
                           struct driz_error_t* error) {

  int     i;
  int     result = 1;
  double *xiptr;
  double *yiptr;
  double *xoptr;
  double *yoptr;
  double *table;
  double  x, y;
  int     xi, yi;
  double  xf, yf, ixf, iyf;
  double  tabx00, tabx01, tabx10, tabx11;

  /* do the bilinear interpolation */
  xiptr = xin;
  yiptr = yin;
  xoptr = xout;
  yoptr = yout;
  table = m->table;

#define TABLE_X(x, y) (table[((y)*m->snx + (x))*2])
#define TABLE_Y(x, y) (table[((y)*m->snx + (x))*2 + 1])

  for (i = 0; i < n; ++i) {
    x = *xiptr++ / m->factor;
    y = *yiptr++ / m->factor;
    xi = (int)floor(x);
    yi = (int)floor(y);
    xf = x - (double)xi;
    yf = y - (double)yi;
    ixf = 1.0 - xf;
    iyf = 1.0 - yf;

    tabx00 = TABLE_X(xi, yi);
    tabx10 = TABLE_X(xi+1, yi);
    tabx01 = TABLE_X(xi, yi+1);
    tabx11 = TABLE_X(xi+1, yi+1);

    /* Account for interpolating across 360-0 boundary */
    if ((tabx00 - tabx10) > 359) {
      tabx00 -= 360.0;
      tabx01 -= 360.0;
    } else if ((tabx00 - tabx10) < -359) {
      tabx10 -= 360.0;
      tabx11 -= 360.0;
    }

    *xoptr++ =
      tabx00 * ixf * iyf +
      tabx10 * xf * iyf +
      tabx01 * ixf * yf +
      tabx11 * xf * yf;

    *yoptr++ =
      TABLE_Y(xi, yi)     * ixf * iyf +
      TABLE_Y(xi+1, yi)   * xf * iyf +
      TABLE_Y(xi, yi+1)   * ixf * yf +
      TABLE_Y(xi+1, yi+1) * xf * yf;
  }

#undef TABLE_X
#undef TABLE_Y

  result = 0;

 exit:

  return result;
}