types.h 4.0 KB

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