| 1 |
/* functions that support interpolation in latitude-longitude projections */ |
| 2 |
|
| 3 |
#include <stdlib.h> |
| 4 |
#include <grass/gis.h> |
| 5 |
#include "main.h" |
| 6 |
|
| 7 |
/************************************************************************/ |
| 8 |
/* This function initializes the search for nearest neighbors */ |
| 9 |
/* by locating the two data closest to the specified column in */ |
| 10 |
/* a linked list of row data */ |
| 11 |
|
| 12 |
int first_west_LL(EW *ewptr, int col) |
| 13 |
{ |
| 14 |
if (ewptr->start == NULL) /* no data in this row */ |
| 15 |
ewptr->walive = ewptr->ealive = FALSE; |
| 16 |
else if (ewptr->start == ewptr->start->prior) { /* single datum */ |
| 17 |
ewptr->west = ewptr->east = ewptr->start; |
| 18 |
ewptr->walive = FALSE; |
| 19 |
ewptr->ealive = TRUE; |
| 20 |
} |
| 21 |
else { /* two or more data in this row */ |
| 22 |
while (col > ewptr->start->x && |
| 23 |
ewptr->start->x < |
| 24 |
ewptr->start->next->x) /* start is west of col */ |
| 25 |
ewptr->start = ewptr->start->next; |
| 26 |
ewptr->east = ewptr->start; |
| 27 |
ewptr->west = ewptr->start->prior; |
| 28 |
ewptr->walive = ewptr->ealive = TRUE; |
| 29 |
} |
| 30 |
|
| 31 |
return 0; |
| 32 |
} |
| 33 |
|
| 34 |
double offset_distance_LL(int offset) |
| 35 |
{ |
| 36 |
extern double *lat_diff; |
| 37 |
|
| 38 |
return (*(lat_diff + abs((int)offset))); |
| 39 |
} |
| 40 |
|
| 41 |
/************************************************************************/ |
| 42 |
/* Return TRUE if search is exhausted both west and east in a row */ |
| 43 |
|
| 44 |
int completed_row_LL(EW *ewptr) |
| 45 |
{ |
| 46 |
return (!ewptr->walive && !ewptr->ealive); |
| 47 |
} |
| 48 |
|
| 49 |
int find_neighbors_LL(EW *ewptr, NEIGHBOR *nbr_head, int row, int col, |
| 50 |
int npoints, int *neighbors) |
| 51 |
{ |
| 52 |
MELEMENT **Mptr; /* double indirection !! */ |
| 53 |
int westward = 1; /* 1 if west of interpolation point */ |
| 54 |
double distance; |
| 55 |
int *active; /* TRUE if active search in this direction */ |
| 56 |
|
| 57 |
active = &ewptr->walive; /* TRUE if searching west in this row */ |
| 58 |
Mptr = &ewptr->west; /* process search west first, then east */ |
| 59 |
do { |
| 60 |
if (*active) { |
| 61 |
distance = distance_LL(row, col, *Mptr); |
| 62 |
|
| 63 |
if (*neighbors < npoints) |
| 64 |
add_neighbor(Mptr, nbr_head, distance, ++(*neighbors)); |
| 65 |
else if (!replace_neighbor(Mptr, nbr_head, distance)) |
| 66 |
*active = FALSE; /* curtail search in this direction */ |
| 67 |
|
| 68 |
if (*active) { |
| 69 |
if (westward) |
| 70 |
extend_west(ewptr); |
| 71 |
else |
| 72 |
extend_east(ewptr); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
active = &ewptr->ealive; |
| 77 |
Mptr = &ewptr->east; |
| 78 |
} while (westward--); /* repeat loop for east and quit */ |
| 79 |
|
| 80 |
return 0; |
| 81 |
} |
| 82 |
|
| 83 |
/************************************************************************/ |
| 84 |
/* This function exhausts all possible nearest neighhbors */ |
| 85 |
/* within the row indexed by the ew search pointer */ |
| 86 |
|
| 87 |
int exhaust_search_LL(EW *ewptr, NEIGHBOR *nbr_head, int row, int col) |
| 88 |
{ |
| 89 |
double distance; |
| 90 |
|
| 91 |
while (ewptr->walive) { /* search active */ |
| 92 |
distance = distance_LL(row, col, ewptr->west); |
| 93 |
|
| 94 |
if (!replace_neighbor(&ewptr->west, nbr_head, distance)) |
| 95 |
break; /* curtail search in this direction */ |
| 96 |
else |
| 97 |
extend_west(ewptr); |
| 98 |
} |
| 99 |
|
| 100 |
while (ewptr->ealive) { /* search active */ |
| 101 |
distance = distance_LL(row, col, ewptr->east); |
| 102 |
|
| 103 |
if (!replace_neighbor(&ewptr->east, nbr_head, distance)) |
| 104 |
break; /* curtail search in this direction */ |
| 105 |
else |
| 106 |
extend_east(ewptr); |
| 107 |
} |
| 108 |
|
| 109 |
return 0; |
| 110 |
} |
| 111 |
|
| 112 |
int extend_west(EW *ewptr) |
| 113 |
{ |
| 114 |
if (ewptr->west->prior != ewptr->east) |
| 115 |
ewptr->west = ewptr->west->prior; |
| 116 |
else |
| 117 |
ewptr->walive = FALSE; |
| 118 |
|
| 119 |
return 0; |
| 120 |
} |
| 121 |
|
| 122 |
int extend_east(EW *ewptr) |
| 123 |
{ |
| 124 |
if (ewptr->east->next != ewptr->west) |
| 125 |
ewptr->east = ewptr->east->next; |
| 126 |
else |
| 127 |
ewptr->ealive = FALSE; |
| 128 |
|
| 129 |
return 0; |
| 130 |
} |
| 131 |
|
| 132 |
double distance_LL(int row, int col, MELEMENT *Mptr) |
| 133 |
{ |
| 134 |
extern double *rowlook, *collook; |
| 135 |
|
| 136 |
/* use lookup tables to increase distance calculation efficiency */ |
| 137 |
LL_set_geodesic_distance(rowlook, row, Mptr->y); |
| 138 |
return (LL_geodesic_distance(*(collook + abs((int)(col - Mptr->x))))); |
| 139 |
} |
| 140 |
|
| 141 |
/************************************************************************/ |
| 142 |
/* Lookup tables storing pre-processed latitude and longitude data */ |
| 143 |
/* are created for later use in selecting nearest neighbors */ |
| 144 |
|
| 145 |
int LL_lookup_tables(int nrows, int ncols) |
| 146 |
{ |
| 147 |
extern double *rowlook, *collook, *lat_diff; |
| 148 |
extern struct Cell_head window; |
| 149 |
double *nextrow, *nextcol, *next_diff, |
| 150 |
lon = 0., lat = window.north - (0.5 * window.ns_res); |
| 151 |
int i; |
| 152 |
|
| 153 |
nextrow = rowlook = (double *)G_calloc(nrows, sizeof(double)); |
| 154 |
for (i = 0; i < nrows; i++, nextrow++, lat -= window.ns_res) |
| 155 |
*nextrow = LL_set_geodesic_distance_lat(lat); |
| 156 |
|
| 157 |
nextcol = collook = (double *)G_calloc(ncols, sizeof(double)); |
| 158 |
for (i = 0; i < ncols; i++, nextcol++, lon += window.ew_res) |
| 159 |
*nextcol = set_sdlmr(lon); |
| 160 |
|
| 161 |
/* compute distance between latitudes at same longitude */ |
| 162 |
next_diff = lat_diff = (double *)G_calloc(nrows, sizeof(double)); |
| 163 |
for (i = 0; i < nrows; i++, next_diff++) { |
| 164 |
LL_set_geodesic_distance(rowlook, 0, i); |
| 165 |
*next_diff = LL_geodesic_distance(0.); |
| 166 |
} |
| 167 |
|
| 168 |
return 0; |
| 169 |
} |