types.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 _types_H
  19. #define _types_H
  20. #include <limits.h>
  21. #include <iostream>
  22. using namespace std;
  23. /* input parameters type */
  24. /* ------------------------------------------------------------ */
  25. typedef short dimension_type; /* represent dimension of the grid */
  26. static const dimension_type dimension_type_max=SHRT_MAX;
  27. typedef short direction_type; /* represent the direction of a cell */
  28. static const direction_type DIRECTION_UNDEF=-1;
  29. /* one of these options must be defined at compile time */
  30. /* #define ELEV_SHORT */
  31. /* #define ELEV_FLOAT */
  32. #if (!defined ELEV_SHORT && !defined ELEV_FLOAT)
  33. #error Must define ELEVATION TYPE
  34. #endif
  35. #ifdef ELEV_SHORT
  36. typedef short elevation_type; /* represent the elevation of a cell */
  37. static const elevation_type elevation_type_max = SHRT_MAX;
  38. #endif
  39. #ifdef ELEV_FLOAT
  40. typedef float elevation_type; /* represent the elevation of a cell */
  41. static const elevation_type elevation_type_max = 1e+15;
  42. #endif
  43. /* static const elevation_type ELEVATION_UNDEF=SHRT_MAX;
  44. static const elevation_type ELEVATION_MIN=SHRT_MIN;
  45. also see nodata.H for ELEVATION_BOUNDARY and ELEVATION_NODATA
  46. */
  47. /* represent the topological rank of a cell */
  48. typedef int toporank_type;
  49. /* output parameter types */
  50. /* ------------------------------------------------------------ */
  51. typedef float flowaccumulation_type;
  52. static const flowaccumulation_type MAX_ACCU = 1e+15;
  53. typedef float tci_type;
  54. typedef int cclabel_type;
  55. #define CCLABEL_FMT "%3d"
  56. /* the following are not arbitrary. LABEL_UNDEF should be the
  57. * most-negative value */
  58. static const cclabel_type LABEL_UNDEF=-1;
  59. static const cclabel_type LABEL_BOUNDARY=0;
  60. static const cclabel_type LABEL_NODATA=1;
  61. static const cclabel_type LABEL_START=1; /* the next label will be used */
  62. typedef int bfs_depth_type;
  63. static const bfs_depth_type DEPTH_INITIAL=1;
  64. #define IS_BOUNDARY(i,j,nr,nc) (((i)==0) || ((i)==((nr)-1)) || \
  65. ((j)==0) || ((j)==((nc)-1)))
  66. /* ---------------------------------------------------------------------- */
  67. class labelFactory {
  68. protected:
  69. static cclabel_type label;
  70. public:
  71. static cclabel_type getNewLabel() { return ++label; }
  72. static cclabel_type getCurrentLabel() { return label; }
  73. static const cclabel_type getLabelInit() {
  74. return cclabel_type(LABEL_START);
  75. }
  76. static const cclabel_type getLabelCount() {
  77. return label+1;
  78. }
  79. static void setLabelCount(int n) {
  80. label = n-1;
  81. }
  82. static void reset() { label = getLabelInit(); }
  83. };
  84. /* ---------------------------------------------------------------------- */
  85. class ijBaseType {
  86. public:
  87. dimension_type i,j;
  88. ijBaseType() : i(-1), j(-1) {};
  89. ijBaseType(dimension_type gi, dimension_type gj) : i(gi), j(gj) {};
  90. friend int operator==(const ijBaseType &a, const ijBaseType &b) {
  91. return (compare(a,b) == 0);
  92. }
  93. friend int operator!=(const ijBaseType &a, const ijBaseType &b) {
  94. return (compare(a,b) != 0);
  95. }
  96. friend ostream& operator << (ostream& s, const ijBaseType &p);
  97. static int compare(const ijBaseType &a, const ijBaseType &b);
  98. };
  99. #define SHALLOW_OP_EQ(_cls) \
  100. friend int \
  101. operator==(const _cls &a, const _cls &b) { \
  102. const int n = sizeof(_cls); \
  103. const char *ap = (const char *)&a; \
  104. const char *bp = (const char *)&b; \
  105. for(int i=0; i<n; i++) { \
  106. if(ap[i] != bp[i]) return 0; \
  107. } \
  108. return 1; \
  109. }
  110. #endif