forked from OSGeo/grass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtinf.c
409 lines (357 loc) · 7.43 KB
/
tinf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#include <grass/config.h>
/* #include <limits.h> */
#include <float.h>
#include <math.h>
#include <grass/gis.h>
#include <grass/raster.h>
#include "tinf.h"
int (*is_null) (void *);
void (*set_null_value) (void *, int);
int (*bpe) ();
void *(*get_max) (void *, void *);
void *(*get_min) (void *, void *);
void (*get_row) (int, void *, int);
void *(*get_buf) ();
void (*put_row) (int, void *);
double (*slope) (void *, void *, double);
void (*set_min) (void *);
void (*set_max) (void *);
void (*diff) (void *, void *);
void (*sum) (void *, void *);
void (*quot) (void *, void *);
void (*prod) (void *, void *);
/* To add a new multitype function, use the function below to initialize
* the function pointer to each of the three typed functions. The function
* pointers and the function prototypes are defined in a header file.
* The actual functions follow. */
void set_func_pointers(int in_type)
{
switch (in_type) {
case CELL_TYPE:
is_null = is_null_c;
bpe = bpe_c;
get_max = get_max_c;
get_min = get_min_c;
get_row = get_row_c;
get_buf = get_buf_c;
put_row = put_row_c;
slope = slope_c;
set_min = set_min_c;
set_max = set_max_c;
diff = diff_c;
sum = sum_c;
quot = quot_c;
prod = prod_c;
set_null_value = set_null_value_c;
break;
case FCELL_TYPE:
is_null = is_null_f;
bpe = bpe_f;
get_max = get_max_f;
get_min = get_min_f;
get_row = get_row_f;
get_buf = get_buf_f;
put_row = put_row_f;
slope = slope_f;
set_min = set_min_f;
set_max = set_max_f;
diff = diff_f;
sum = sum_f;
quot = quot_f;
prod = prod_f;
set_null_value = set_null_value_f;
break;
case DCELL_TYPE:
is_null = is_null_d;
bpe = bpe_d;
get_max = get_max_d;
get_min = get_min_d;
get_row = get_row_d;
get_buf = get_buf_d;
put_row = put_row_d;
slope = slope_d;
set_min = set_min_d;
set_max = set_max_d;
diff = diff_d;
sum = sum_d;
quot = quot_d;
prod = prod_d;
set_null_value = set_null_value_d;
}
return;
}
/* check for null values */
int is_null_c(void *value)
{
return Rast_is_c_null_value((CELL *) value);
}
int is_null_f(void *value)
{
return Rast_is_f_null_value((FCELL *) value);
}
int is_null_d(void *value)
{
return Rast_is_d_null_value((DCELL *) value);
}
/* set null values in buffer */
void set_null_value_c(void *value, int num)
{
Rast_set_c_null_value((CELL *) value, num);
}
void set_null_value_f(void *value, int num)
{
Rast_set_f_null_value((FCELL *) value, num);
}
void set_null_value_d(void *value, int num)
{
Rast_set_d_null_value((DCELL *) value, num);
}
/* return the size of the current type */
int bpe_c()
{
return sizeof(CELL);
}
int bpe_f()
{
return sizeof(FCELL);
}
int bpe_d()
{
return sizeof(DCELL);
}
/* return the pointer that points to the smaller of two value */
void *get_min_c(void *v1, void *v2)
{
void *rc;
rc = v2;
if (*(CELL *) v1 < *(CELL *) v2)
rc = v1;
return rc;
}
void *get_min_f(void *v1, void *v2)
{
void *rc;
rc = v2;
if (*(FCELL *) v1 < *(FCELL *) v2)
rc = v1;
return rc;
}
void *get_min_d(void *v1, void *v2)
{
void *rc;
rc = v2;
if (*(DCELL *) v1 < *(DCELL *) v2)
rc = v1;
return rc;
}
/* return the pointer that points to the larger value */
void *get_max_c(void *v1, void *v2)
{
void *rc;
rc = v2;
if (*(CELL *) v1 > *(CELL *) v2)
rc = v1;
return rc;
}
void *get_max_f(void *v1, void *v2)
{
void *rc;
rc = v2;
if (*(FCELL *) v1 > *(FCELL *) v2)
rc = v1;
return rc;
}
void *get_max_d(void *v1, void *v2)
{
void *rc;
rc = v2;
if (*(DCELL *) v1 > *(DCELL *) v2)
rc = v1;
return rc;
}
/* Read one line from a raster map */
void get_row_c(int fd, void *row, int n)
{
Rast_get_c_row(fd, (CELL *) row, n);
}
void get_row_f(int fd, void *row, int n)
{
Rast_get_f_row(fd, (FCELL *) row, n);
}
void get_row_d(int fd, void *row, int n)
{
Rast_get_d_row(fd, (DCELL *) row, n);
}
/* Write one row to a raster map */
void put_row_c(int fd, void *row)
{
Rast_put_c_row(fd, (CELL *) row);
}
void put_row_f(int fd, void *row)
{
Rast_put_f_row(fd, (FCELL *) row);
}
void put_row_d(int fd, void *row)
{
Rast_put_d_row(fd, (DCELL *) row);
}
/* Allocate memory for one line of data */
void *get_buf_c(void)
{
return (void *)Rast_allocate_c_buf();
}
void *get_buf_f(void)
{
return (void *)Rast_allocate_f_buf();
}
void *get_buf_d(void)
{
return (void *)Rast_allocate_d_buf();
}
/* initialize memory to a minimum value */
void set_min_c(void *v)
{
*(CELL *) v = INT_MIN;
}
void set_min_f(void *v)
{
*(FCELL *) v = FLT_MIN;
}
void set_min_d(void *v)
{
*(DCELL *) v = DBL_MIN;
}
/* initialize memory to a maximum value */
void set_max_c(void *v)
{
*(CELL *) v = INT_MAX;
}
void set_max_f(void *v)
{
*(FCELL *) v = FLT_MAX;
}
void set_max_d(void *v)
{
*(DCELL *) v = DBL_MAX;
}
/* get the difference between two values, returned in the first pointer */
void diff_c(void *v1, void *v2)
{
*(CELL *) v1 -= *(CELL *) v2;
}
void diff_f(void *v1, void *v2)
{
*(FCELL *) v1 -= *(FCELL *) v2;
}
void diff_d(void *v1, void *v2)
{
*(DCELL *) v1 -= *(DCELL *) v2;
}
/* get the sum of two values, returned in the first pointer */
void sum_c(void *v1, void *v2)
{
*(CELL *) v1 += *(CELL *) v2;
}
void sum_f(void *v1, void *v2)
{
*(FCELL *) v1 += *(FCELL *) v2;
}
void sum_d(void *v1, void *v2)
{
*(DCELL *) v1 += *(DCELL *) v2;
}
/* get the quotient of two values, returned in the first pointer */
void quot_c(void *v1, void *v2)
{
*(CELL *) v1 /= *(CELL *) v2;
}
void quot_f(void *v1, void *v2)
{
*(FCELL *) v1 /= *(FCELL *) v2;
}
void quot_d(void *v1, void *v2)
{
*(DCELL *) v1 /= *(DCELL *) v2;
}
/* get the product of two values, returned in the first pointer */
void prod_c(void *v1, void *v2)
{
*(CELL *) v1 *= *(CELL *) v2;
}
void prod_f(void *v1, void *v2)
{
*(FCELL *) v1 *= *(FCELL *) v2;
}
void prod_d(void *v1, void *v2)
{
*(DCELL *) v1 *= *(DCELL *) v2;
}
/* probably not a function of general interest */
/* calculate the slope between two cells, returned as a double */
double slope_c(void *line1, void *line2, double cnst)
{
double rc;
CELL *pedge;
rc = -HUGE_VAL;
pedge = (CELL *) line2;
if (!Rast_is_c_null_value(pedge)) {
rc = (*(CELL *) line1 - *pedge) / cnst;
}
return rc;
}
double slope_f(void *line1, void *line2, double cnst)
{
double rc;
FCELL *pedge;
rc = -HUGE_VAL;
pedge = (FCELL *) line2;
if (!Rast_is_f_null_value(pedge)) {
rc = (*(FCELL *) line1 - *pedge) / cnst;
}
return rc;
}
double slope_d(void *line1, void *line2, double cnst)
{
double rc;
DCELL *pedge;
rc = -HUGE_VAL;
pedge = (DCELL *) line2;
if (!Rast_is_d_null_value(pedge)) {
rc = (*(DCELL *) line1 - *pedge) / cnst;
}
return rc;
}
/* read a line and update a three-line buffer */
/* moving forward through a file */
int advance_band3(int fh, struct band3 *bnd)
{
int rc;
void *hold;
hold = bnd->b[0];
bnd->b[0] = bnd->b[1];
bnd->b[1] = bnd->b[2];
bnd->b[2] = hold;
if (fh == 0)
rc = 0;
else
rc = read(fh, bnd->b[2], bnd->sz);
return rc;
}
/* read a line and update a three-line buffer */
/* moving backward through a file */
int retreat_band3(int fh, struct band3 *bnd)
{
int rc;
void *hold;
hold = bnd->b[2];
bnd->b[2] = bnd->b[1];
bnd->b[1] = bnd->b[0];
bnd->b[0] = hold;
if (fh == 0)
rc = 0;
else {
rc = read(fh, bnd->b[0], bnd->sz);
lseek(fh, (off_t) - 2 * bnd->sz, SEEK_CUR);
}
return rc;
}