filters.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * lidar-related filtering functions
  3. *
  4. * Authors:
  5. * Markus Metz (r.in.lidar)
  6. * Vaclav Petras (refactoring and various additions)
  7. *
  8. * Copyright 2011-2016 by Markus Metz, and The GRASS Development Team
  9. *
  10. * This program is free software licensed under the GPL (>=v2).
  11. * Read the COPYING file that comes with GRASS for details.
  12. *
  13. */
  14. #include "filters.h"
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <grass/gis.h>
  18. #include <grass/glocale.h>
  19. int range_filter_from_option(struct Option *option, double *min, double *max)
  20. {
  21. if (option->answer != NULL) {
  22. if (option->answers[0] == NULL || option->answers[1] == NULL)
  23. G_fatal_error(_("Invalid range <%s> for option %s"), option->answer, option->key);
  24. sscanf(option->answers[0], "%lf", min);
  25. sscanf(option->answers[1], "%lf", max);
  26. /* for convenience, switch order to make valid input */
  27. if (*min > *max) {
  28. double tmp = *max;
  29. *max = *min;
  30. *min = tmp;
  31. }
  32. return TRUE;
  33. }
  34. return FALSE;
  35. }
  36. int return_filter_create_from_string(struct ReturnFilter *return_filter,
  37. const char *name)
  38. {
  39. return_filter->filter = LAS_ALL;
  40. if (name) {
  41. if (strcmp(name, "first") == 0)
  42. return_filter->filter = LAS_FIRST;
  43. else if (strcmp(name, "last") == 0)
  44. return_filter->filter = LAS_LAST;
  45. else if (strcmp(name, "mid") == 0)
  46. return_filter->filter = LAS_MID;
  47. else
  48. G_fatal_error(_("Unknown return filter value <%s>"), name);
  49. }
  50. if (return_filter->filter == LAS_ALL)
  51. return FALSE;
  52. else
  53. return TRUE;
  54. }
  55. int return_filter_is_out(struct ReturnFilter *return_filter, int return_n,
  56. int n_returns)
  57. {
  58. if (return_filter->filter == LAS_ALL)
  59. return FALSE;
  60. int skipme = 1;
  61. switch (return_filter->filter) {
  62. case LAS_FIRST:
  63. if (return_n == 1)
  64. skipme = 0;
  65. break;
  66. case LAS_MID:
  67. if (return_n > 1 && return_n < n_returns)
  68. skipme = 0;
  69. break;
  70. case LAS_LAST:
  71. if (n_returns > 1 && return_n == n_returns)
  72. skipme = 0;
  73. break;
  74. }
  75. if (skipme)
  76. return TRUE;
  77. return FALSE;
  78. }
  79. int class_filter_create_from_strings(struct ClassFilter *class_filter,
  80. char **classes)
  81. {
  82. class_filter->str_classes = classes;
  83. if (classes)
  84. return TRUE;
  85. else
  86. return FALSE;
  87. }
  88. int class_filter_is_out(struct ClassFilter *class_filter, int class_n)
  89. {
  90. if (!class_filter->str_classes)
  91. return FALSE;
  92. int i = 0;
  93. int skipme = TRUE;
  94. while (class_filter->str_classes[i]) {
  95. if (class_n == atoi(class_filter->str_classes[i])) {
  96. skipme = FALSE;
  97. break;
  98. }
  99. i++;
  100. }
  101. if (skipme)
  102. return TRUE;
  103. return FALSE;
  104. }