count_decimation.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /****************************************************************************
  2. *
  3. * MODULE: v.decimate
  4. * AUTHOR(S): Vaclav Petras
  5. * PURPOSE: Reduce the number of points in a vector map
  6. * COPYRIGHT: (C) 2015 by the GRASS Development Team
  7. *
  8. * This program is free software under the GNU General Public
  9. * License (>=v2). Read the COPYING file that comes with GRASS
  10. * for details.
  11. *
  12. *****************************************************************************/
  13. /* TODO: change int */
  14. /* TODO: revise names */
  15. #include "count_decimation.h"
  16. #include <grass/gis.h>
  17. #include <stdlib.h>
  18. void count_decimation_init(struct CountDecimationControl *control,
  19. int *skip, int *preserve,
  20. int *offset, int *limit)
  21. {
  22. control->skip_every = 0;
  23. control->preserve_every = 0;
  24. /* counter used by both but that's ok, skip and preserve are exclusive */
  25. control->every_counter = 0;
  26. control->n_count_filtered = 0;
  27. control->offset_n = 0;
  28. control->offset_n_counter = 0;
  29. control->limit_n = 0;
  30. control->limit_n_counter = 0;
  31. if (skip)
  32. control->skip_every = *skip;
  33. if (preserve)
  34. control->preserve_every = *preserve;
  35. if (offset)
  36. control->offset_n = *offset;
  37. if (limit)
  38. control->limit_n = *limit;
  39. }
  40. int count_decimation_is_valid(struct CountDecimationControl *control)
  41. {
  42. if (control->skip_every == 1)
  43. return FALSE;
  44. if (control->skip_every && control->preserve_every > 1)
  45. return FALSE;
  46. return TRUE;
  47. }
  48. int count_decimation_is_noop(struct CountDecimationControl *control)
  49. {
  50. if (control->skip_every < 2 && control->preserve_every < 2
  51. && !control->offset_n && !control->limit_n)
  52. return TRUE;
  53. return FALSE;
  54. }
  55. void count_decimation_init_from_str(struct CountDecimationControl *control,
  56. const char *skip, const char *preserve,
  57. const char *offset, const char *limit)
  58. {
  59. control->skip_every = 0;
  60. control->preserve_every = 0;
  61. control->every_counter = 0;
  62. control->n_count_filtered = 0;
  63. control->offset_n = 0;
  64. control->offset_n_counter = 0;
  65. control->limit_n = 0;
  66. control->limit_n_counter = 0;
  67. /* TODO: atoi is probably not appropriate */
  68. if (skip)
  69. control->skip_every = atoi(skip);
  70. if (preserve)
  71. control->preserve_every = atoi(preserve);
  72. if (offset)
  73. control->offset_n = atoi(offset);
  74. if (limit)
  75. control->limit_n = atoi(limit);
  76. }
  77. /* TODO: eliminate noop cases */
  78. int count_decimation_is_out(struct CountDecimationControl *control)
  79. {
  80. if (control->offset_n) {
  81. if (control->offset_n_counter < control->offset_n) {
  82. control->offset_n_counter++;
  83. return TRUE;
  84. }
  85. else {
  86. control->offset_n = 0; /* disable offset check */
  87. }
  88. }
  89. if (control->skip_every) {
  90. control->every_counter++;
  91. if (control->every_counter == control->skip_every) {
  92. control->n_count_filtered++;
  93. control->every_counter = 0;
  94. return TRUE;
  95. }
  96. }
  97. else if (control->preserve_every) {
  98. control->every_counter++;
  99. if (control->every_counter == control->preserve_every) {
  100. control->every_counter = 0;
  101. }
  102. else {
  103. control->n_count_filtered++;
  104. return TRUE;
  105. }
  106. }
  107. return FALSE;
  108. }
  109. int count_decimation_is_end(struct CountDecimationControl *control)
  110. {
  111. if (control->limit_n) {
  112. control->limit_n_counter++;
  113. /* this matches the last successfully imported point */
  114. if (control->limit_n_counter == control->limit_n)
  115. return TRUE;
  116. }
  117. return FALSE;
  118. }