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 
19 
20 #ifndef PLATEAU_H
21 #define PLATEAU_H
22 
23 #include <assert.h>
24 
25 #include <grass/iostream/ami.h>
26 
27 #include "types.h"
28 #include "direction.h"
29 #include "genericWindow.h"
30 
31 /* ---------------------------------------------------------------------- */
32 class plateauType : public ijBaseType {
33 public: /* struct, so members public */
34   cclabel_type cclabel;
35   direction_type dir;
36   bool valid;
37 public:
38   plateauType(dimension_type gi, dimension_type gj, direction_type gdir,
39 	      cclabel_type gcclabel=LABEL_UNDEF) :
ijBaseType(gi,gj)40     ijBaseType(gi,gj), cclabel(gcclabel), dir(gdir), valid(true) {};
41 
plateauType()42   plateauType() : valid(false) {};
43 
~plateauType()44   ~plateauType() {}
45 
invalidate()46   void invalidate() { valid = false; }
47 
printLabel(const plateauType & p)48   static char *printLabel(const plateauType &p) {
49     static char buf[8];
50     sprintf(buf, CCLABEL_FMT, p.cclabel);
51     return buf;
52   }
53 
54   friend ostream& operator << (ostream& s, const plateauType &p) {
55     if(p.valid) {
56       return s << "[" << (ijBaseType)p
57 	       << ": dir=" << p.dir
58 	       << "; lbl=" << p.cclabel << "]";
59     } else {
60       return s << "[invalid]";
61     }
62   }
63 };
64 
65 class ijCmpPlateauType {
66 public:
compare(const plateauType & a,const plateauType & b)67   static int compare(const plateauType &a, const plateauType &b) {
68     return ijBaseType::compare(a, b);
69   }
70 };
71 
72 class labelCmpPlateauType {
73 public:
compare(const plateauType & a,const plateauType & b)74   static int compare(const plateauType &a, const plateauType &b) {
75 	if(a.cclabel < b.cclabel) return -1;
76 	if(a.cclabel > b.cclabel) return 1;
77 	return 0;
78   }
79 };
80 
81 
82 
83 
84 
85 /* ********************************************************************** */
86 class plateauStats {
87 public:
88   dimension_type iMin, iMax, jMin, jMax;
89   long size;
90   cclabel_type label;
91   bool hasSpill;
92 public:
93 
plateauStats()94   plateauStats() : label(LABEL_UNDEF) {}
95 
plateauStats(cclabel_type l)96   plateauStats(cclabel_type l) :
97     iMin(dimension_type_max), iMax(0),
98     jMin(dimension_type_max), jMax(0),
99     size(0), label(l), hasSpill(false) {};
100 
add(plateauType & pt)101   void add(plateauType &pt) {
102     assert(pt.cclabel == label);
103     if(pt.i < iMin) iMin = pt.i;
104     if(pt.i > iMax) iMax = pt.i;
105     if(pt.j < jMin) jMin = pt.j;
106     if(pt.j > jMax) jMax = pt.j;
107     if(pt.dir > 0) hasSpill = true;
108     size++;
109   }
110 
111   SHALLOW_OP_EQ(plateauStats);
112 
113   friend ostream& operator << (ostream& s, const plateauStats &p) {
114     return s << "[" << p.label << ": "
115 	     << "(" << p.iMin << "," << p.jMin << ")-"
116 	     << "(" << p.iMax << "," << p.jMax << "); "
117 	     << p.size << " "
118 	     << (p.hasSpill?"S":".") << "]";
119   }
120 
121 };
122 
123 
124 
125 /* ********************************************************************** */
126 
127 AMI_STREAM<plateauType> *
128 findPlateaus(AMI_STREAM<elevation_type> *elstr,
129 			 const dimension_type nrows, const dimension_type ncols,
130 			 const elevation_type nodata_value,
131 			 AMI_STREAM<ElevationWindow > *winstr,
132 			 AMI_STREAM<direction_type> *dirStr,
133 			 AMI_STREAM<plateauStats> *statStr);
134 
135 #endif
136 
137