xif.c 6.2 KB

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