maskfn.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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/raster3d.h>
  20. #include <grass/glocale.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. void init_d_mask_rules(d_Mask * d_mask)
  26. {
  27. d_mask->list = NULL;
  28. }
  29. void add_d_mask_rule(d_Mask * d_mask, double a, double b, int inf)
  30. {
  31. d_Interval *I;
  32. I = (d_Interval *) G_malloc(sizeof(d_Interval));
  33. I->low = a <= b ? a : b;
  34. I->high = a >= b ? a : b;
  35. I->inf = inf;
  36. I->next = d_mask->list;
  37. d_mask->list = I;
  38. }
  39. int Rast3d_mask_d_select(DCELL * x, d_Mask * mask)
  40. {
  41. d_Interval *I;
  42. if (mask->list == NULL)
  43. return 0;
  44. for (I = mask->list; I; I = I->next) {
  45. if (Rast3d_mask_match_d_interval(*x, I))
  46. return 1;
  47. }
  48. return 0;
  49. }
  50. DCELL Rast3d_mask_match_d_interval(DCELL x, d_Interval * I)
  51. {
  52. if (I->inf < 0)
  53. return x <= I->low;
  54. if (I->inf > 0)
  55. return x >= I->high;
  56. return x >= I->low && x <= I->high;
  57. }
  58. void parse_d_mask_rule(char *vallist, d_Mask * d_mask, char *where)
  59. {
  60. double a, b;
  61. char junk[128];
  62. /* #-# */
  63. if (sscanf(vallist, "%lf-%lf", &a, &b) == 2) {
  64. G_message(_("Adding rule: %lf - %lf"), a, b);
  65. add_d_mask_rule(d_mask, a, b, 0);
  66. }
  67. /* inf-# */
  68. else if (sscanf(vallist, "%[^ -\t]-%lf", junk, &a) == 2)
  69. add_d_mask_rule(d_mask, a, a, -1);
  70. /* #-inf */
  71. else if (sscanf(vallist, "%lf-%[^ \t]", &a, junk) == 2)
  72. add_d_mask_rule(d_mask, a, a, 1);
  73. /* # */
  74. else if (sscanf(vallist, "%lf", &a) == 1)
  75. add_d_mask_rule(d_mask, a, a, 0);
  76. else {
  77. if (where)
  78. G_message("%s: ", where);
  79. G_warning(_("%s: illegal value spec"), vallist);
  80. G_usage();
  81. exit(EXIT_FAILURE);
  82. }
  83. }
  84. void Rast3d_parse_vallist(char **vallist, d_Mask ** d_mask)
  85. {
  86. char buf[1024];
  87. char x[2];
  88. FILE *fd;
  89. *d_mask = (d_Mask *) G_malloc(sizeof(d_Mask));
  90. init_d_mask_rules(*d_mask);
  91. if (vallist == NULL)
  92. return;
  93. for (; *vallist; vallist++) {
  94. if (*vallist[0] == '/') {
  95. fd = fopen(*vallist, "r");
  96. if (fd == NULL) {
  97. perror(*vallist);
  98. G_usage();
  99. exit(EXIT_FAILURE);
  100. }
  101. while (fgets(buf, sizeof buf, fd)) {
  102. if (sscanf(buf, "%1s", x) != 1 || *x == '#')
  103. continue;
  104. parse_d_mask_rule(buf, *d_mask, *vallist);
  105. }
  106. fclose(fd);
  107. }
  108. else
  109. parse_d_mask_rule(*vallist, *d_mask, (char *)NULL);
  110. }
  111. }