plateau.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /****************************************************************************
  2. *
  3. * MODULE: r.terraflow
  4. *
  5. * COPYRIGHT (C) 2007 Laura Toma
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. *****************************************************************************/
  18. #ifndef PLATEAU_H
  19. #define PLATEAU_H
  20. #include <assert.h>
  21. #include <grass/iostream/ami.h>
  22. #include "types.h"
  23. #include "direction.h"
  24. #include "genericWindow.h"
  25. /* ---------------------------------------------------------------------- */
  26. class plateauType : public ijBaseType {
  27. public: /* struct, so members public */
  28. cclabel_type cclabel;
  29. direction_type dir;
  30. bool valid;
  31. public:
  32. plateauType(dimension_type gi, dimension_type gj, direction_type gdir,
  33. cclabel_type gcclabel=LABEL_UNDEF) :
  34. ijBaseType(gi,gj), cclabel(gcclabel), dir(gdir), valid(true) {};
  35. plateauType() : valid(false) {};
  36. ~plateauType() {}
  37. void invalidate() { valid = false; }
  38. static char *printLabel(const plateauType &p) {
  39. static char buf[8];
  40. sprintf(buf, CCLABEL_FMT, p.cclabel);
  41. return buf;
  42. }
  43. friend ostream& operator << (ostream& s, const plateauType &p) {
  44. if(p.valid) {
  45. return s << "[" << (ijBaseType)p
  46. << ": dir=" << p.dir
  47. << "; lbl=" << p.cclabel << "]";
  48. } else {
  49. return s << "[invalid]";
  50. }
  51. }
  52. };
  53. class ijCmpPlateauType {
  54. public:
  55. static int compare(const plateauType &a, const plateauType &b) {
  56. return ijBaseType::compare(a, b);
  57. }
  58. };
  59. class labelCmpPlateauType {
  60. public:
  61. static int compare(const plateauType &a, const plateauType &b) {
  62. if(a.cclabel < b.cclabel) return -1;
  63. if(a.cclabel > b.cclabel) return 1;
  64. return 0;
  65. }
  66. };
  67. /* ********************************************************************** */
  68. class plateauStats {
  69. public:
  70. dimension_type iMin, iMax, jMin, jMax;
  71. long size;
  72. cclabel_type label;
  73. bool hasSpill;
  74. public:
  75. plateauStats() : label(LABEL_UNDEF) {}
  76. plateauStats(cclabel_type l) :
  77. iMin(dimension_type_max), iMax(0),
  78. jMin(dimension_type_max), jMax(0),
  79. size(0), label(l), hasSpill(false) {};
  80. void add(plateauType &pt) {
  81. assert(pt.cclabel == label);
  82. if(pt.i < iMin) iMin = pt.i;
  83. if(pt.i > iMax) iMax = pt.i;
  84. if(pt.j < jMin) jMin = pt.j;
  85. if(pt.j > jMax) jMax = pt.j;
  86. if(pt.dir > 0) hasSpill = true;
  87. size++;
  88. }
  89. SHALLOW_OP_EQ(plateauStats);
  90. friend ostream& operator << (ostream& s, const plateauStats &p) {
  91. return s << "[" << p.label << ": "
  92. << "(" << p.iMin << "," << p.jMin << ")-"
  93. << "(" << p.iMax << "," << p.jMax << "); "
  94. << p.size << " "
  95. << (p.hasSpill?"S":".") << "]";
  96. }
  97. };
  98. /* ********************************************************************** */
  99. AMI_STREAM<plateauType> *
  100. findPlateaus(AMI_STREAM<elevation_type> *elstr,
  101. const dimension_type nrows, const dimension_type ncols,
  102. const elevation_type nodata_value,
  103. AMI_STREAM<ElevationWindow > *winstr,
  104. AMI_STREAM<direction_type> *dirStr,
  105. AMI_STREAM<plateauStats> *statStr);
  106. #endif