Loading...
Searching...
No Matches
SuperProber.h
1/*
2
3MIT License
4
5Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25 */
26/*
27Part of Rack development has been done in the BALTRAD projects part-financed
28by the European Union (European Regional Development Fund and European
29Neighbourhood Partnership Instrument, Baltic Sea Region Programme 2007-2013)
30 */
31#ifndef SUPER_PROBER_H
32#define SUPER_PROBER_H
33
34
35#include <drain/image/EdgeTracker.h>
36
37#include "Direction.h"
38
39#include "SegmentProber.h"
40#include "TreeSVG.h"
41
42
43namespace drain
44{
45namespace image
46{
47
48
50
59
60 Range<int> anchor;
61
62 //int visitedMarker = 255;
63
64};
65
66
67
69
78class SuperProber : public SegmentProber<int,int,SuperProberConf> {
79 // SegmentProber<int,int,SegmentProberConf<int, int> > {
80
81public:
82
83 size_t size = 0;
84
85
86 //CoordinateHandler2D handler;
87
88 // SuperProber(const Channel &s, Channel &d) : SegmentProber<int,int,SegmentProberConf<int, int> >(s, d), size(0) {};
90 //control.markerImage.set
91 };
92
93
94 // Document the base?
95 virtual inline
96 void clear(){
97 size = 0;
98 };
99
101 virtual inline
102 void update(const Position & pos){
103 ++size;
104 }
105
106 virtual inline
107 void visit2(const Position & pos) {
108 update(pos);
109 control.markVisited(pos);
110 }
111
112 /*
113 virtual inline
114 void markVisited(const Position & pos) const {
115 return dst->put(pos.i, pos.j, conf.visitedMarker);
116 }
117 */
118
119 // DEPRECATING
120 virtual // place holder
121 bool isValidSegment(int i, int j) const {
122 return true;
123 };
124
126
132 virtual inline
133 bool isNewSegment(const Position & pos, Direction::value_t dir) const {
134 return this->conf.anchor.contains(src.get<src_t>(pos.i, pos.j));
135 //return (dst->get<dst_t>(pos.i, pos.j) == conf.visitedMarker);
136 }
137
138
139 bool checkNext(Position & pos, Direction::value_t dir, TreeSVG & tree){
140
141 drain::Logger mout(__FILE__, __FUNCTION__, __LINE__);
142
143 /* No use, offset + handler is not much more expensive
144 if (DIR == Direction::NONE){
145 }
146 */
147 //mout.warn("check", pos, " -> ", dir);
148 mout.warn(); // "check", pos, " -> ", dir);
149 // , Direction::dict.getValue(dir), ' ', '<', dir , '>'
150 mout << "check" << pos << ' ' << Direction::arrows.getValue(dir) << " <" << dir<< ">";
151
152 // Position pos2(pos);
153 switch (control.move(pos, dir)){
154 case ProberControl::MOVE_ACCEPTED:
155 if (!isNewSegment(pos,dir)){
156 // Default = continue probing...
157 mout << " mark" << pos;
158 //visit2(pos);
159 control.markVisited(pos);
160 update(pos);
161 // dst->put(pos.i, pos.j, dir);
162 // update(pos);
163 mout << mout.endl;
164 return true;
165 }
166 else {
167
168 if (dir != Direction::NONE){
169 // New segment!
170 mout << " NEW SEG! " << mout.endl;
171 EdgeTracker<int,int> edgeTracker(src, control);
172 edgeTracker.track(pos, dir);
173 if (edgeTracker.contour.size() > 0){
174 TreeSVG & contour = tree[drain::StringBuilder<'-'>("cont", pos.i, pos.j).str()](svg::POLYGON);
175 FlexibleVariable & c = contour->get("points");
176 c.setType(typeid(std::string));
177 c.setSeparator(0);
178 for (const Position & p: edgeTracker.contour){
179 c << p.i << ',' << p.j << ' ';
180 }
181 // contour->set("points", drain::sprinter(edgeTracker.contour, drain::Sprinter::plainLayout));
182 /*
183 TreeSVG & segment = tree[drain::StringBuilder<'-'>("seg",pos.i,pos.j)](NodeSVG::CIRCLE); // (NodeSVG::POLYGON);
184 segment->set("cx", pos.i);
185 segment->set("cy", pos.j);
186 segment->set("r", 5);
187 */
188 // <polygon points="100,10 150,190 50,190" style="fill:lime;stroke:purple;stroke-width:3" />
189 }
190 dst->put(pos.i, pos.j, 192);
191 }
192 else {
193 mout << " {none} ";
194 dst->put(pos.i, pos.j, 85);
195 }
196 // Scan EDGE!
197 //return true;
198 // mout << mout.endl;
199 //return false;
200 }
201 break;
202 case ProberControl::COORD_ERROR:
203 mout << " COORD_ERROR ";
204 // return false;
205 break;
206 case ProberControl::DIR_ERROR:
207 mout << " DIR_ERROR ";
208 // return false;
209 break;
210
211 }
212
213 mout << mout.endl;
214
215 /*
216 const Position & offset = Direction::offset.find(dir)->second;
217 pos.add(offset);
218
219 mout.debug() << "checkNext: " << pos;
220
221 if (!control.handler.handle(pos)){
222 mout << " (OK)";
223 if (!control.isVisited(pos)){
224 }
225 else {
226 mout << " (already visited)" << pos.i << ',' << pos.j;
227 }
228 }
229 else {
230 mout << " (OUT)";
231 }
232 mout << mout.endl;
233 */
234
235 return false;
236 }
237
238
239 // void probe2(const Position & pos, Direction::value_t DIR, TreeSVG & tree){
240 void probe2(Position pos, Direction::value_t dir, TreeSVG & tree){
241
242 // TODO: use rotating direction DIR (old idea)
243
244 // Position pos2(pos);
245 Position p;
246
247 while (checkNext(pos, dir, tree)){
248
249 // tree->get("points") << " " << pos2.i << ',' << pos2.j;
250 if (dir == Direction::NONE){
251 dir = Direction::RIGHT;
252 continue;
253 }
254
255 //Position
256 p = pos;
257 //dst->put(posSide.i, posSide.j, DIR);
258
259 if (checkNext(p, DIR_TURN_DEG(dir, 90), tree)){
260 //dst->put(posSide.i, posSide.j, 64);
261 std::cout << "090" << " = " << p.i << ',' << p.j << '\n';
262 probe2(p, dir, tree); //["+90fwd"](NodeSVG::POLYGON)); // recursion
263 probe2(p, DIR_TURN_DEG(dir, 180), tree); //["+90inv"](NodeSVG::POLYGON));
264 }
265
266 p = pos;
267 if (checkNext(p, DIR_TURN_DEG(dir, 270), tree)){
268 //dst->put(posSide.i, posSide.j, 192);
269 std::cout << "270" << " = " << p.i << ',' << p.j << '\n';
270 probe2(p, dir, tree); // ["-90pos"](NodeSVG::POLYGON)); // recursion
271 probe2(p, DIR_TURN_DEG(dir, 180), tree); // ["-90inv"](NodeSVG::POLYGON));
272 }
273
274 }
275
276 //return false;
277 };
278
279
280};
281
282
283} // image::
284
285} // drain::
286
287#endif /* SUPER_PROBER_H_ */
288
289
290
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition Log.h:313
Definition Range.h:52
Definition StringBuilder.h:58
VariableT is a final class applied through typedefs Variable, Reference and FlexibleVariable.
Definition VariableT.h:87
Image with static geometry.
Definition ImageChannel.h:58
Container for parameters of SegmentProber.
Definition EdgeTracker.h:72
void put(size_t i, T x)
Sets the intensity in location i to x. See \address.
Definition ImageFrame.h:187
T get(size_t i) const
Gets the intensity at location i. See address().
Definition ImageFrame.h:249
A recursive method for visiting pixels of a segment in an image.
Definition SegmentProber.h:98
A DIRECTED recursive method for visiting pixels of a segment in an image.
Definition SuperProber.h:78
void probe2(Position pos, Direction::value_t dir, TreeSVG &tree)
Definition SuperProber.h:240
virtual bool isValidSegment(int i, int j) const
Application dependent, to be redefined. Assumes checked coordinates.
Definition SuperProber.h:121
virtual void update(const Position &pos)
Application dependent operation performed in each segment location (i,j).
Definition SuperProber.h:102
virtual void clear()
Called before processing each segment. Compare with init(), which is called once for each image.
Definition SuperProber.h:96
virtual bool isNewSegment(const Position &pos, Direction::value_t dir) const
Application dependent. Assumes checked coordinates.
Definition SuperProber.h:133
Definition DataSelector.cpp:1277
Definition Direction.h:46
Container for parameters of SegmentProber.
Definition SuperProber.h:58