123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304 |
- #include <grass/gis.h>
- #include <grass/raster.h>
- #include <grass/calc.h>
- /********************************************************************
- if(a) 1,0,1 1 if a is non zero, 0 otherwise
- if(a,b) b,0,b b if a is non zero, 0 otherwise
- if(a,b,c) b,c,b b if a is non zero, c otherwise
- if(a,b,c,d) d,c,b b if a is positive, c if a is zero, d if a is negative
- ********************************************************************/
- static int f_if_i(int argc, const int *argt, void **args)
- {
- CELL *res = args[0];
- DCELL *arg1 = args[1];
- CELL *arg2 = (argc >= 2) ? args[2] : NULL;
- CELL *arg3 = (argc >= 3) ? args[3] : NULL;
- CELL *arg4 = (argc >= 4) ? args[4] : NULL;
- int i;
- switch (argc) {
- case 0:
- return E_ARG_LO;
- case 1:
- for (i = 0; i < columns; i++)
- if (IS_NULL_D(&arg1[i]))
- SET_NULL_C(&res[i]);
- else
- res[i] = arg1[i] != 0.0 ? 1 : 0;
- break;
- case 2:
- for (i = 0; i < columns; i++)
- if (IS_NULL_D(&arg1[i]))
- SET_NULL_C(&res[i]);
- else if (arg1[i] == 0.0)
- res[i] = 0;
- else {
- if (IS_NULL_C(&arg2[i]))
- SET_NULL_C(&res[i]);
- else
- res[i] = arg2[i];
- }
- break;
- case 3:
- for (i = 0; i < columns; i++)
- if (IS_NULL_D(&arg1[i]))
- SET_NULL_C(&res[i]);
- else if (arg1[i] == 0.0) {
- if (IS_NULL_C(&arg3[i]))
- SET_NULL_C(&res[i]);
- else
- res[i] = arg3[i];
- }
- else {
- if (IS_NULL_C(&arg2[i]))
- SET_NULL_C(&res[i]);
- else
- res[i] = arg2[i];
- }
- break;
- case 4:
- for (i = 0; i < columns; i++)
- if (IS_NULL_D(&arg1[i]))
- SET_NULL_C(&res[i]);
- else if (arg1[i] == 0.0) {
- if (IS_NULL_C(&arg3[i]))
- SET_NULL_C(&res[i]);
- else
- res[i] = arg3[i];
- }
- else if (arg1[i] > 0.0) {
- if (IS_NULL_C(&arg2[i]))
- SET_NULL_C(&res[i]);
- else
- res[i] = arg2[i];
- }
- else { /* (arg1[i] < 0.0) */
- if (IS_NULL_C(&arg4[i]))
- SET_NULL_C(&res[i]);
- else
- res[i] = arg4[i];
- }
- break;
- default:
- return E_ARG_HI;
- }
- return 0;
- }
- static int f_if_f(int argc, const int *argt, void **args)
- {
- FCELL *res = args[0];
- DCELL *arg1 = args[1];
- FCELL *arg2 = (argc >= 2) ? args[2] : NULL;
- FCELL *arg3 = (argc >= 3) ? args[3] : NULL;
- FCELL *arg4 = (argc >= 4) ? args[4] : NULL;
- int i;
- switch (argc) {
- case 0:
- return E_ARG_LO;
- case 1:
- return E_ARG_TYPE;
- case 2:
- for (i = 0; i < columns; i++)
- if (IS_NULL_D(&arg1[i]))
- SET_NULL_F(&res[i]);
- else if (arg1[i] == 0.0)
- res[i] = 0.0;
- else {
- if (IS_NULL_F(&arg2[i]))
- SET_NULL_F(&res[i]);
- else
- res[i] = arg2[i];
- }
- break;
- case 3:
- for (i = 0; i < columns; i++)
- if (IS_NULL_D(&arg1[i]))
- SET_NULL_F(&res[i]);
- else if (arg1[i] == 0.0) {
- if (IS_NULL_F(&arg3[i]))
- SET_NULL_F(&res[i]);
- else
- res[i] = arg3[i];
- }
- else {
- if (IS_NULL_F(&arg2[i]))
- SET_NULL_F(&res[i]);
- else
- res[i] = arg2[i];
- }
- break;
- case 4:
- for (i = 0; i < columns; i++)
- if (IS_NULL_D(&arg1[i]))
- SET_NULL_F(&res[i]);
- else if (arg1[i] == 0.0) {
- if (IS_NULL_F(&arg3[i]))
- SET_NULL_F(&res[i]);
- else
- res[i] = arg3[i];
- }
- else if (arg1[i] > 0.0) {
- if (IS_NULL_F(&arg2[i]))
- SET_NULL_F(&res[i]);
- else
- res[i] = arg2[i];
- }
- else { /* (arg1[i] < 0.0) */
- if (IS_NULL_F(&arg4[i]))
- SET_NULL_F(&res[i]);
- else
- res[i] = arg4[i];
- }
- break;
- default:
- return E_ARG_HI;
- }
- return 0;
- }
- static int f_if_d(int argc, const int *argt, void **args)
- {
- DCELL *res = args[0];
- DCELL *arg1 = args[1];
- DCELL *arg2 = (argc >= 2) ? args[2] : NULL;
- DCELL *arg3 = (argc >= 3) ? args[3] : NULL;
- DCELL *arg4 = (argc >= 4) ? args[4] : NULL;
- int i;
- switch (argc) {
- case 0:
- return E_ARG_LO;
- case 1:
- return E_ARG_TYPE;
- case 2:
- for (i = 0; i < columns; i++)
- if (IS_NULL_D(&arg1[i]))
- SET_NULL_D(&res[i]);
- else if (arg1[i] == 0.0)
- res[i] = 0.0;
- else {
- if (IS_NULL_D(&arg2[i]))
- SET_NULL_D(&res[i]);
- else
- res[i] = arg2[i];
- }
- break;
- case 3:
- for (i = 0; i < columns; i++)
- if (IS_NULL_D(&arg1[i]))
- SET_NULL_D(&res[i]);
- else if (arg1[i] == 0.0) {
- if (IS_NULL_D(&arg3[i]))
- SET_NULL_D(&res[i]);
- else
- res[i] = arg3[i];
- }
- else {
- if (IS_NULL_D(&arg2[i]))
- SET_NULL_D(&res[i]);
- else
- res[i] = arg2[i];
- }
- break;
- case 4:
- for (i = 0; i < columns; i++)
- if (IS_NULL_D(&arg1[i]))
- SET_NULL_D(&res[i]);
- else if (arg1[i] == 0.0) {
- if (IS_NULL_D(&arg3[i]))
- SET_NULL_D(&res[i]);
- else
- res[i] = arg3[i];
- }
- else if (arg1[i] > 0.0) {
- if (IS_NULL_D(&arg2[i]))
- SET_NULL_D(&res[i]);
- else
- res[i] = arg2[i];
- }
- else { /* (arg1[i] < 0.0) */
- if (IS_NULL_D(&arg4[i]))
- SET_NULL_D(&res[i]);
- else
- res[i] = arg4[i];
- }
- break;
- default:
- return E_ARG_HI;
- }
- return 0;
- }
- int f_if(int argc, const int *argt, void **args)
- {
- if (argc < 1)
- return E_ARG_LO;
- if (argc > 4)
- return E_ARG_HI;
- if (argt[1] != DCELL_TYPE)
- return E_ARG_TYPE;
- if (argc >= 2 && argt[2] != argt[0])
- return E_ARG_TYPE;
- if (argc >= 3 && argt[3] != argt[0])
- return E_ARG_TYPE;
- if (argc >= 4 && argt[4] != argt[0])
- return E_ARG_TYPE;
- switch (argt[0]) {
- case CELL_TYPE:
- return f_if_i(argc, argt, args);
- case FCELL_TYPE:
- return f_if_f(argc, argt, args);
- case DCELL_TYPE:
- return f_if_d(argc, argt, args);
- default:
- return E_INV_TYPE;
- }
- }
- int c_if(int argc, int *argt)
- {
- if (argc < 1)
- return E_ARG_LO;
- if (argc > 4)
- return E_ARG_HI;
- argt[0] = CELL_TYPE;
- if (argc >= 2 && argt[2] == FCELL_TYPE)
- argt[0] = FCELL_TYPE;
- if (argc >= 3 && argt[3] == FCELL_TYPE)
- argt[0] = FCELL_TYPE;
- if (argc >= 4 && argt[4] == FCELL_TYPE)
- argt[0] = FCELL_TYPE;
- if (argc >= 2 && argt[2] == DCELL_TYPE)
- argt[0] = DCELL_TYPE;
- if (argc >= 3 && argt[3] == DCELL_TYPE)
- argt[0] = DCELL_TYPE;
- if (argc >= 4 && argt[4] == DCELL_TYPE)
- argt[0] = DCELL_TYPE;
- argt[1] = DCELL_TYPE;
- if (argc >= 2)
- argt[2] = argt[0];
- if (argc >= 3)
- argt[3] = argt[0];
- if (argc >= 4)
- argt[4] = argt[0];
- return 0;
- }
|