mask_functions.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /***************************************************************************
  2. * MODULE: this structs/functions are used by r3.mask and r3.null
  3. *
  4. * AUTHOR(S): Roman Waupotitsch, Michael Shapiro, Helena Mitasova,
  5. * Bill Brown, Lubos Mitas, Jaro Hofierka
  6. *
  7. * COPYRIGHT: (C) 2005 by the GRASS Development Team
  8. *
  9. * This program is free software under the GNU General Public
  10. * License (>=v2). Read the file COPYING that comes with GRASS
  11. * for details.
  12. *
  13. *****************************************************************************/
  14. /*Helperfunctions */
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <grass/gis.h>
  19. #include <grass/glocale.h>
  20. #include "mask_functions.h"
  21. /*local prototypes */
  22. static void add_d_mask_rule(d_Mask * d_mask, double a, double b, int inf);
  23. static void parse_d_mask_rule(char *vallist, d_Mask * d_mask, char *where);
  24. static void init_d_mask_rules(d_Mask * d_mask);
  25. /*******************************************************************/
  26. static void init_d_mask_rules(d_Mask * d_mask)
  27. {
  28. d_mask->list = NULL;
  29. }
  30. /*******************************************************************/
  31. static void add_d_mask_rule(d_Mask * d_mask, double a, double b, int inf)
  32. {
  33. d_Interval *I;
  34. I = (d_Interval *) G_malloc(sizeof(d_Interval));
  35. I->low = a <= b ? a : b;
  36. I->high = a >= b ? a : b;
  37. I->inf = inf;
  38. I->next = d_mask->list;
  39. d_mask->list = I;
  40. }
  41. /*******************************************************************/
  42. int mask_d_select(DCELL * x, d_Mask * mask)
  43. {
  44. d_Interval *I;
  45. if (mask->list == NULL)
  46. return 0;
  47. for (I = mask->list; I; I = I->next) {
  48. if (mask_match_d_interval(*x, I))
  49. return 1;
  50. }
  51. return 0;
  52. }
  53. /*******************************************************************/
  54. extern DCELL mask_match_d_interval(DCELL x, d_Interval * I)
  55. {
  56. if (I->inf < 0)
  57. return x <= I->low;
  58. if (I->inf > 0)
  59. return x >= I->high;
  60. return x >= I->low && x <= I->high;
  61. }
  62. /*******************************************************************/
  63. static void parse_d_mask_rule(char *vallist, d_Mask * d_mask, char *where)
  64. {
  65. double a, b;
  66. char junk[128];
  67. /* #-# */
  68. if (sscanf(vallist, "%lf-%lf", &a, &b) == 2) {
  69. G_message(_("Adding rule: %lf - %lf"), a, b);
  70. add_d_mask_rule(d_mask, a, b, 0);
  71. }
  72. /* inf-# */
  73. else if (sscanf(vallist, "%[^ -\t]-%lf", junk, &a) == 2)
  74. add_d_mask_rule(d_mask, a, a, -1);
  75. /* #-inf */
  76. else if (sscanf(vallist, "%lf-%[^ \t]", &a, junk) == 2)
  77. add_d_mask_rule(d_mask, a, a, 1);
  78. /* # */
  79. else if (sscanf(vallist, "%lf", &a) == 1)
  80. add_d_mask_rule(d_mask, a, a, 0);
  81. else {
  82. if (where)
  83. G_message("%s: ", where);
  84. G_warning(_("%s: illegal value spec"), vallist);
  85. G_usage();
  86. exit(EXIT_FAILURE);
  87. }
  88. }
  89. /*******************************************************************/
  90. void parse_vallist(char **vallist, d_Mask ** d_mask)
  91. {
  92. char buf[1024];
  93. char x[2];
  94. FILE *fd;
  95. *d_mask = (d_Mask *) G_malloc(sizeof(d_Mask));
  96. init_d_mask_rules(*d_mask);
  97. if (vallist == NULL)
  98. return;
  99. for (; *vallist; vallist++) {
  100. if (*vallist[0] == '/') {
  101. fd = fopen(*vallist, "r");
  102. if (fd == NULL) {
  103. perror(*vallist);
  104. G_usage();
  105. exit(EXIT_FAILURE);
  106. }
  107. while (fgets(buf, sizeof buf, fd)) {
  108. if (sscanf(buf, "%1s", x) != 1 || *x == '#')
  109. continue;
  110. parse_d_mask_rule(buf, *d_mask, *vallist);
  111. }
  112. fclose(fd);
  113. }
  114. else
  115. parse_d_mask_rule(*vallist, *d_mask, (char *)NULL);
  116. }
  117. }
  118. /*******************************************************************/
  119. /*******************************************************************/
  120. /*******************************************************************/