xif.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. #include <grass/gis.h>
  2. #include <grass/raster.h>
  3. #include <grass/calc.h>
  4. /********************************************************************
  5. if(a) 1,0,1 1 if a is non zero, 0 otherwise
  6. if(a,b) b,0,b b if a is non zero, 0 otherwise
  7. if(a,b,c) b,c,b b if a is non zero, c otherwise
  8. if(a,b,c,d) d,c,b b if a is positive, c if a is zero, d if a is negative
  9. ********************************************************************/
  10. static int f_if_i(int argc, const int *argt, void **args)
  11. {
  12. CELL *res = args[0];
  13. DCELL *arg1 = args[1];
  14. CELL *arg2 = (argc >= 2) ? args[2] : NULL;
  15. CELL *arg3 = (argc >= 3) ? args[3] : NULL;
  16. CELL *arg4 = (argc >= 4) ? args[4] : NULL;
  17. int i;
  18. switch (argc) {
  19. case 0:
  20. return E_ARG_LO;
  21. case 1:
  22. for (i = 0; i < columns; i++)
  23. if (IS_NULL_D(&arg1[i]))
  24. SET_NULL_C(&res[i]);
  25. else
  26. res[i] = arg1[i] != 0.0 ? 1 : 0;
  27. break;
  28. case 2:
  29. for (i = 0; i < columns; i++)
  30. if (IS_NULL_D(&arg1[i]))
  31. SET_NULL_C(&res[i]);
  32. else if (arg1[i] == 0.0)
  33. res[i] = 0;
  34. else {
  35. if (IS_NULL_C(&arg2[i]))
  36. SET_NULL_C(&res[i]);
  37. else
  38. res[i] = arg2[i];
  39. }
  40. break;
  41. case 3:
  42. for (i = 0; i < columns; i++)
  43. if (IS_NULL_D(&arg1[i]))
  44. SET_NULL_C(&res[i]);
  45. else if (arg1[i] == 0.0) {
  46. if (IS_NULL_C(&arg3[i]))
  47. SET_NULL_C(&res[i]);
  48. else
  49. res[i] = arg3[i];
  50. }
  51. else {
  52. if (IS_NULL_C(&arg2[i]))
  53. SET_NULL_C(&res[i]);
  54. else
  55. res[i] = arg2[i];
  56. }
  57. break;
  58. case 4:
  59. for (i = 0; i < columns; i++)
  60. if (IS_NULL_D(&arg1[i]))
  61. SET_NULL_C(&res[i]);
  62. else if (arg1[i] == 0.0) {
  63. if (IS_NULL_C(&arg3[i]))
  64. SET_NULL_C(&res[i]);
  65. else
  66. res[i] = arg3[i];
  67. }
  68. else if (arg1[i] > 0.0) {
  69. if (IS_NULL_C(&arg2[i]))
  70. SET_NULL_C(&res[i]);
  71. else
  72. res[i] = arg2[i];
  73. }
  74. else { /* (arg1[i] < 0.0) */
  75. if (IS_NULL_C(&arg4[i]))
  76. SET_NULL_C(&res[i]);
  77. else
  78. res[i] = arg4[i];
  79. }
  80. break;
  81. default:
  82. return E_ARG_HI;
  83. }
  84. return 0;
  85. }
  86. static int f_if_f(int argc, const int *argt, void **args)
  87. {
  88. FCELL *res = args[0];
  89. DCELL *arg1 = args[1];
  90. FCELL *arg2 = (argc >= 2) ? args[2] : NULL;
  91. FCELL *arg3 = (argc >= 3) ? args[3] : NULL;
  92. FCELL *arg4 = (argc >= 4) ? args[4] : NULL;
  93. int i;
  94. switch (argc) {
  95. case 0:
  96. return E_ARG_LO;
  97. case 1:
  98. return E_ARG_TYPE;
  99. case 2:
  100. for (i = 0; i < columns; i++)
  101. if (IS_NULL_D(&arg1[i]))
  102. SET_NULL_F(&res[i]);
  103. else if (arg1[i] == 0.0)
  104. res[i] = 0.0;
  105. else {
  106. if (IS_NULL_F(&arg2[i]))
  107. SET_NULL_F(&res[i]);
  108. else
  109. res[i] = arg2[i];
  110. }
  111. break;
  112. case 3:
  113. for (i = 0; i < columns; i++)
  114. if (IS_NULL_D(&arg1[i]))
  115. SET_NULL_F(&res[i]);
  116. else if (arg1[i] == 0.0) {
  117. if (IS_NULL_F(&arg3[i]))
  118. SET_NULL_F(&res[i]);
  119. else
  120. res[i] = arg3[i];
  121. }
  122. else {
  123. if (IS_NULL_F(&arg2[i]))
  124. SET_NULL_F(&res[i]);
  125. else
  126. res[i] = arg2[i];
  127. }
  128. break;
  129. case 4:
  130. for (i = 0; i < columns; i++)
  131. if (IS_NULL_D(&arg1[i]))
  132. SET_NULL_F(&res[i]);
  133. else if (arg1[i] == 0.0) {
  134. if (IS_NULL_F(&arg3[i]))
  135. SET_NULL_F(&res[i]);
  136. else
  137. res[i] = arg3[i];
  138. }
  139. else if (arg1[i] > 0.0) {
  140. if (IS_NULL_F(&arg2[i]))
  141. SET_NULL_F(&res[i]);
  142. else
  143. res[i] = arg2[i];
  144. }
  145. else { /* (arg1[i] < 0.0) */
  146. if (IS_NULL_F(&arg4[i]))
  147. SET_NULL_F(&res[i]);
  148. else
  149. res[i] = arg4[i];
  150. }
  151. break;
  152. default:
  153. return E_ARG_HI;
  154. }
  155. return 0;
  156. }
  157. static int f_if_d(int argc, const int *argt, void **args)
  158. {
  159. DCELL *res = args[0];
  160. DCELL *arg1 = args[1];
  161. DCELL *arg2 = (argc >= 2) ? args[2] : NULL;
  162. DCELL *arg3 = (argc >= 3) ? args[3] : NULL;
  163. DCELL *arg4 = (argc >= 4) ? args[4] : NULL;
  164. int i;
  165. switch (argc) {
  166. case 0:
  167. return E_ARG_LO;
  168. case 1:
  169. return E_ARG_TYPE;
  170. case 2:
  171. for (i = 0; i < columns; i++)
  172. if (IS_NULL_D(&arg1[i]))
  173. SET_NULL_D(&res[i]);
  174. else if (arg1[i] == 0.0)
  175. res[i] = 0.0;
  176. else {
  177. if (IS_NULL_D(&arg2[i]))
  178. SET_NULL_D(&res[i]);
  179. else
  180. res[i] = arg2[i];
  181. }
  182. break;
  183. case 3:
  184. for (i = 0; i < columns; i++)
  185. if (IS_NULL_D(&arg1[i]))
  186. SET_NULL_D(&res[i]);
  187. else if (arg1[i] == 0.0) {
  188. if (IS_NULL_D(&arg3[i]))
  189. SET_NULL_D(&res[i]);
  190. else
  191. res[i] = arg3[i];
  192. }
  193. else {
  194. if (IS_NULL_D(&arg2[i]))
  195. SET_NULL_D(&res[i]);
  196. else
  197. res[i] = arg2[i];
  198. }
  199. break;
  200. case 4:
  201. for (i = 0; i < columns; i++)
  202. if (IS_NULL_D(&arg1[i]))
  203. SET_NULL_D(&res[i]);
  204. else if (arg1[i] == 0.0) {
  205. if (IS_NULL_D(&arg3[i]))
  206. SET_NULL_D(&res[i]);
  207. else
  208. res[i] = arg3[i];
  209. }
  210. else if (arg1[i] > 0.0) {
  211. if (IS_NULL_D(&arg2[i]))
  212. SET_NULL_D(&res[i]);
  213. else
  214. res[i] = arg2[i];
  215. }
  216. else { /* (arg1[i] < 0.0) */
  217. if (IS_NULL_D(&arg4[i]))
  218. SET_NULL_D(&res[i]);
  219. else
  220. res[i] = arg4[i];
  221. }
  222. break;
  223. default:
  224. return E_ARG_HI;
  225. }
  226. return 0;
  227. }
  228. int f_if(int argc, const int *argt, void **args)
  229. {
  230. if (argc < 1)
  231. return E_ARG_LO;
  232. if (argc > 4)
  233. return E_ARG_HI;
  234. if (argt[1] != DCELL_TYPE)
  235. return E_ARG_TYPE;
  236. if (argc >= 2 && argt[2] != argt[0])
  237. return E_ARG_TYPE;
  238. if (argc >= 3 && argt[3] != argt[0])
  239. return E_ARG_TYPE;
  240. if (argc >= 4 && argt[4] != argt[0])
  241. return E_ARG_TYPE;
  242. switch (argt[0]) {
  243. case CELL_TYPE:
  244. return f_if_i(argc, argt, args);
  245. case FCELL_TYPE:
  246. return f_if_f(argc, argt, args);
  247. case DCELL_TYPE:
  248. return f_if_d(argc, argt, args);
  249. default:
  250. return E_INV_TYPE;
  251. }
  252. }
  253. int c_if(int argc, int *argt)
  254. {
  255. if (argc < 1)
  256. return E_ARG_LO;
  257. if (argc > 4)
  258. return E_ARG_HI;
  259. argt[0] = CELL_TYPE;
  260. if (argc >= 2 && argt[2] == FCELL_TYPE)
  261. argt[0] = FCELL_TYPE;
  262. if (argc >= 3 && argt[3] == FCELL_TYPE)
  263. argt[0] = FCELL_TYPE;
  264. if (argc >= 4 && argt[4] == FCELL_TYPE)
  265. argt[0] = FCELL_TYPE;
  266. if (argc >= 2 && argt[2] == DCELL_TYPE)
  267. argt[0] = DCELL_TYPE;
  268. if (argc >= 3 && argt[3] == DCELL_TYPE)
  269. argt[0] = DCELL_TYPE;
  270. if (argc >= 4 && argt[4] == DCELL_TYPE)
  271. argt[0] = DCELL_TYPE;
  272. argt[1] = DCELL_TYPE;
  273. if (argc >= 2)
  274. argt[2] = argt[0];
  275. if (argc >= 3)
  276. argt[3] = argt[0];
  277. if (argc >= 4)
  278. argt[4] = argt[0];
  279. return 0;
  280. }