mask.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include <stdlib.h>
  2. #include <grass/gis.h>
  3. #include <grass/raster.h>
  4. #include "mask.h"
  5. #include "local_proto.h"
  6. int init_mask_rules(Mask * mask)
  7. {
  8. mask->list = NULL;
  9. return 0;
  10. }
  11. int init_d_mask_rules(d_Mask * d_mask)
  12. {
  13. d_mask->list = NULL;
  14. return 0;
  15. }
  16. int add_mask_rule(Mask * mask, long a, long b, int inf)
  17. {
  18. Interval *I;
  19. I = (Interval *) G_malloc(sizeof(Interval));
  20. I->low = a <= b ? a : b;
  21. I->high = a >= b ? a : b;
  22. I->inf = inf;
  23. I->next = mask->list;
  24. mask->list = I;
  25. return 0;
  26. }
  27. int add_d_mask_rule(d_Mask * d_mask, double a, double b, int inf)
  28. {
  29. d_Interval *I;
  30. I = (d_Interval *) G_malloc(sizeof(d_Interval));
  31. I->low = a <= b ? a : b;
  32. I->high = a >= b ? a : b;
  33. I->inf = inf;
  34. I->next = d_mask->list;
  35. d_mask->list = I;
  36. return 0;
  37. }
  38. int mask_cell_array(CELL * cell, int ncols, Mask * mask, int invert)
  39. {
  40. long x;
  41. while (ncols-- > 0) {
  42. x = *cell;
  43. if (mask_select(&x, mask, invert))
  44. *cell++ = x;
  45. else
  46. Rast_set_c_null_value(cell++, 1);
  47. }
  48. return 0;
  49. }
  50. int mask_d_cell_array(DCELL * dcell, int ncols, d_Mask * mask, int invert)
  51. {
  52. DCELL x;
  53. while (ncols-- > 0) {
  54. x = *dcell;
  55. if (mask_d_select(&x, mask, invert))
  56. *dcell++ = x;
  57. else
  58. Rast_set_d_null_value(dcell++, 1);
  59. }
  60. return 0;
  61. }
  62. int mask_select(long *x, Mask * mask, int invert)
  63. {
  64. Interval *I;
  65. if (mask->list == NULL)
  66. return 1;
  67. for (I = mask->list; I; I = I->next) {
  68. if (mask_match_interval(*x, I)) {
  69. if (invert)
  70. return 0;
  71. return 1;
  72. }
  73. }
  74. return invert;
  75. }
  76. int mask_d_select(DCELL * x, d_Mask * mask, int invert)
  77. {
  78. d_Interval *I;
  79. if (mask->list == NULL)
  80. return 1;
  81. for (I = mask->list; I; I = I->next) {
  82. if (mask_match_d_interval(*x, I)) {
  83. if (invert)
  84. return 0;
  85. return 1;
  86. }
  87. }
  88. return invert;
  89. }
  90. int mask_match_interval(long x, Interval * I)
  91. {
  92. if (I->inf < 0)
  93. return x <= I->low;
  94. if (I->inf > 0)
  95. return x >= I->high;
  96. return x >= I->low && x <= I->high;
  97. }
  98. int mask_match_d_interval(DCELL x, d_Interval * I)
  99. {
  100. if (I->inf < 0)
  101. return x <= I->low;
  102. if (I->inf > 0)
  103. return x >= I->high;
  104. return x >= I->low && x <= I->high;
  105. }