ProbingControl.h
1 /*
2 
3 MIT License
4 
5 Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13 
14 The above copyright notice and this permission notice shall be included in all
15 copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 SOFTWARE.
24 
25 */
26 /*
27 Part of Rack development has been done in the BALTRAD projects part-financed
28 by the European Union (European Regional Development Fund and European
29 Neighbourhood Partnership Instrument, Baltic Sea Region Programme 2007-2013)
30 */
31 #ifndef PROBING_CRITERIA_H
32 #define PROBING_CRITERIA_H
33 
34 #include <list>
35 
36 #include "Direction.h"
37 #include "ImageChannel.h"
38 #include "ImageT.h"
39 #include "CoordinateHandler.h"
40 // #include "FilePng.h"
41 
42 namespace drain
43 {
44 
45 namespace image
46 {
47 
48 
50 
58 //typedef std::list<drain::Point2D<int> > Contour;
59 
60 struct ProberControl {
61 
62  typedef unsigned char marker_t;
63 
64  virtual inline
65  ~ProberControl(){};
66 
68 
70  virtual inline
71  bool isVisited(const Position & pos) const {
72  //return (markerImage.get<int>(pos.i, pos.j) > 0); //== visitedMarker); // > 0
73  return (markerImage.at(pos.i, pos.j) > 0); //== visitedMarker); // > 0
74  }
75 
76  virtual inline
77  void markVisited(const Position & pos){
78  //markerImage.put(pos.i, pos.j, 255);
79  markerImage.at(pos.i, pos.j) = visitedMarker;
80  }
81 
82  /*
83  virtual inline
84  void mark(const Position & pos, marker_t m){
85  markerImage.at(pos.i, pos.j) |= m;
86  //markerImage.put(pos.i, pos.j, markerImage.get<int>(pos.i, pos.j) | m);
87  }
88  */
89 
91 
98  void markBlockedIn(const Position & pos, Direction::value_t dir){
99  markerImage.at(pos.i, pos.j) |= dir;
100  }
101 
103 
111  bool markBlockedOut(Position pos, Direction::value_t dir){
112  pos.add(Direction::offset.find(dir)->second);
113  if (handler.handle(pos.i, pos.j)){
114  return false;
115  }
116  else {
117  markerImage.at(pos.i, pos.j) |= DIR_TURN_DEG(dir, 180);
118  return true;
119  }
120  }
121 
123 
126  virtual inline
127  bool isValidDir(const Position & pos, Direction::value_t dir) const {
128  return (dir & markerImage.at(pos.i, pos.j)) == 0;
129  //return (markerImage.get<int>(pos.i, pos.j) | dir) != 0;
130  }
131 
132  virtual
133  bool isValidPixel(const Channel & src, const Position & pos) const = 0;
134 
135 
136  typedef enum {MOVE_ACCEPTED=0, COORD_ERROR=1, DIR_ERROR=2} move_status;
137 
138 
139  virtual inline
140  move_status move(Position & pos, Direction::value_t dir) const {
141 
142  Position pos2(pos);
143  pos2.add(Direction::offset.find(dir)->second);
144 
145  if (!handler.handle(pos2)){ // Warning: in POLAR/WRAP coordinates, also dir should change.
146  if (isValidDir(pos2, dir)){ // includes visited = invalid from all directions
147  pos = pos2;
148  return MOVE_ACCEPTED;
149  }
150  else {
151  return DIR_ERROR;
152  }
153  }
154  else {
155  return COORD_ERROR;
156  }
157 
158  }
159 
160  /*
161  virtual inline
162  bool move(Position & pos, Direction::value_t dir) const {
163 
164  Position posNext(pos);
165  posNext.add(Direction::offset.find(dir)->second);
166 
167  if (!handler.handle(posNext)){
168  if (isValidDir(posNext, dir)){
169  pos = posNext;
170  return true; //OK;
171  }
172  else {
173  return false; // DIR_ERROR;
174  }
175  }
176  else {
177  return false; // COORD_ERROR;
178  }
179 
180  }
181  */
182 
183 
184  // This could be in proberCriteria, but is in (Super)Prober, inherited from SegmentProber
185  CoordinateHandler2D handler;
186 
187 
188  ImageT<marker_t> markerImage;
189  //Image markerImage;
190 
191  marker_t visitedMarker = 0xff;
192 
193 };
194 
196 
197  int threshold = 128;
198 
199  virtual
200  bool isValidPixel(const Channel & src, const Position & pos) const {
201  return src.get<int>(pos.i, pos.j) > threshold;
202  };
203 
204  // int threshold = 128;
205 
207  /*
208  virtual inline
209  bool isValidPixel(int i) const override {
210  return i > threshold;
211  };
212 
214  virtual inline
215  bool isVisited(int i) const override {
216  return (i == visitedMarker);
217  };
218  */
219 
220  /*
221  virtual inline
222  bool isVisited(const Position & pos) const {
223  return controlImage.at(pos.i, pos.j) != visitedMarker;
224  };
225  */
226 
227 
228 };
229 
230 
231 /*
232 template <class S, class D>
233 std::ostream & operator<<(std::ostream & ostr, const EdgeTracker<S,D> & prober){
234  return ostr;
235 }
236 */
237 
238 
239 } // image::
240 
241 } // drain::
242 
243 #endif /* PROBER_CRITERIA_H_ */
244 
245 
246 
Image with static geometry.
Definition: ImageChannel.h:60
virtual coord_overflow_t handle(int &x, int &y) const
Ensures the validity of the coordinates. If inside limits, arguments (x,y) remain intact and 0 is ret...
Definition: CoordinateHandler.h:190
T get(size_t i) const
Gets the intensity at location i. See address().
Definition: ImageFrame.h:254
Definition: DataSelector.cpp:1277
static const std::map< value_t, Position > offset
Opposite direction.
Definition: Direction.h:173
Definition: Direction.h:46
Container for parameters of SegmentProber.
Definition: ProbingControl.h:60
virtual bool isValidDir(const Position &pos, Direction::value_t dir) const
Direction NONE is always valid,.
Definition: ProbingControl.h:127
bool markBlockedOut(Position pos, Direction::value_t dir)
Disable entry to position pos from direction dir inverted .
Definition: ProbingControl.h:111
virtual bool isVisited(const Position &pos) const
NEW.
Definition: ProbingControl.h:71
void markBlockedIn(const Position &pos, Direction::value_t dir)
Disable exit from position pos towards direction dir.
Definition: ProbingControl.h:98
Definition: ProbingControl.h:195