1 /*
2  *      nearest.c - returns the nearest neighbor to a given
3  *                  x,y position
4  */
5 
6 #include <math.h>
7 #include "global.h"
8 
p_nearest(struct cache * ibuffer,void * obufptr,int cell_type,double * row_idx,double * col_idx,struct Cell_head * cellhd)9 void p_nearest(struct cache *ibuffer,	 /* input buffer                  */
10 	       void *obufptr,	         /* ptr in output buffer          */
11 	       int cell_type,	         /* raster map type of obufptr    */
12 	       double *row_idx,	         /* row index in input matrix     */
13 	       double *col_idx,	         /* column index in input matrix  */
14 	       struct Cell_head *cellhd	 /* cell header of input layer    */
15     )
16 {
17     int row, col;		/* row/col of nearest neighbor   */
18     DCELL *cellp;
19 
20     /* cut indices to integer and get nearest cell */
21     /* the row_idx, col_idx correction for bilinear/bicubic does not apply here */
22     row = (int)floor(*row_idx);
23     col = (int)floor(*col_idx);
24 
25     /* check for out of bounds - if out of bounds set NULL value     */
26     if (row < 0 || row >= cellhd->rows || col < 0 || col >= cellhd->cols) {
27 	Rast_set_null_value(obufptr, 1, cell_type);
28 	return;
29     }
30 
31     cellp = CPTR(ibuffer, row, col);
32 
33     if (Rast_is_d_null_value(cellp)) {
34 	Rast_set_null_value(obufptr, 1, cell_type);
35 	return;
36     }
37 
38     Rast_set_d_value(obufptr, *cellp, cell_type);
39 }
40