$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
transform_snake.hh
1 // Copyright (C) 2008, 2009 EPITA Research and Development Laboratory (LRDE)
2 //
3 // This file is part of Olena.
4 //
5 // Olena is free software: you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation, version 2 of the License.
8 //
9 // Olena is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // As a special exception, you may use this file as part of a free
18 // software project without restriction. Specifically, if other files
19 // instantiate templates or use macros or inline functions from this
20 // file, or you compile this file and link it with other files to produce
21 // an executable, this file does not by itself cause the resulting
22 // executable to be covered by the GNU General Public License. This
23 // exception does not however invalidate any other reasons why the
24 // executable file might be covered by the GNU General Public License.
25 
26 #ifndef MLN_ACCU_TRANSFORM_SNAKE_HH
27 # define MLN_ACCU_TRANSFORM_SNAKE_HH
28 
38 
39 # include <mln/core/concept/image.hh>
40 # include <mln/core/concept/meta_accumulator.hh>
41 # include <mln/core/alias/window2d.hh>
42 # include <mln/win/diff.hh>
43 # include <mln/win/shift.hh>
44 # include <mln/geom/delta.hh>
45 # include <mln/extension/adjust.hh>
46 
47 # include <mln/canvas/browsing/snake_fwd.hh>
48 # include <mln/canvas/browsing/snake_generic.hh>
49 
50 
51 
52 namespace mln
53 {
54 
55  namespace accu
56  {
57 
58 
59  template <typename A, typename I, typename W>
60  mln_ch_value(I, mln_result(A))
61  transform_snake(const Accumulator<A>& a, const Image<I>& input, const Window<W>& win);
62 
63 
64  template <typename A, typename I, typename W>
65  mln_ch_value(I, mln_meta_accu_result(A, mln_value(I)))
66  transform_snake(const Meta_Accumulator<A>& a, const Image<I>& input, const Window<W>& win);
67 
68 
69 
70 # ifndef MLN_INCLUDE_ONLY
71 
72  namespace internal
73  {
74 
75 
76  template <typename I, typename W>
77  void transform_snake_tests(const Image<I>& input_, const Window<W>& win_)
78  {
79  const I& input = exact(input_);
80  const W& win = exact(win_);
81 
82  mln_precondition(input.is_valid());
83  mln_precondition(win.is_valid());
84  mln_precondition(! win.is_empty());
85 
86  (void) input;
87  (void) win;
88  }
89 
90 
91  // Functor.
92 
93  template <typename I, typename W, typename A>
94  struct transform_snake_functor
95  {
96  typedef transform_snake_functor<I,W, A> self;
97  typedef void (self::*move_fun)();
98  typedef mln_deduce(I, psite, delta) dpsite;
99 
100  const I& input;
101  const W& win;
102  A accu;
103 
104  mln_ch_value(I, mln_result(A)) output;
105 
106  mln_psite(I) p;
107 
108  window2d
109  win_left_fwd,
110  win_right_fwd,
111  win_left_bkd,
112  win_right_bkd,
113  win_bot_up,
114  win_top_up,
115  win_bot_down,
116  win_top_down;
117 
118  mln_qiter(window2d)
119  q_l_fwd,
120  q_r_fwd,
121  q_l_bkd,
122  q_r_bkd,
123  q_top_up,
124  q_bot_up,
125  q_top_down,
126  q_bot_down;
127 
128  std::vector<move_fun> moves;
129  std::vector<dpsite> dps;
130 
131  transform_snake_functor(const Image<I>& input, const Window<W>& win, const Accumulator<A>& a)
132  : input(exact(input)),
133  win(exact(win)),
134  accu(exact(a)),
135 
136  win_left_fwd(win::shift(win, mln::left) - win),
137  win_right_fwd(win - win::shift(win, mln::left)),
138  win_left_bkd(win::shift(win_left_fwd, mln::right)),
139  win_right_bkd(win::shift(win_right_fwd, mln::right)),
140 
141  win_bot_up(win::shift(win, mln::down) - win),
142  win_top_up(win - win::shift(win, mln::down)),
143  win_bot_down(win::shift(win_bot_up, mln::up)),
144  win_top_down(win::shift(win_top_up, mln::up)),
145 
146  q_l_fwd(win_left_fwd, p),
147  q_r_fwd(win_right_fwd, p),
148  q_l_bkd(win_left_bkd, p),
149  q_r_bkd(win_right_bkd, p),
150 
151  q_top_up(win_top_up, p),
152  q_bot_up(win_bot_up, p),
153  q_top_down(win_top_down, p),
154  q_bot_down(win_bot_down, p),
155 
156  moves(3),
157  dps(3)
158  {
159  if (win_bot_up.size() + win_top_up.size() +
160  win_bot_down.size() + win_top_down.size() <
161  win_left_fwd.size() + win_right_fwd.size() +
162  win_left_bkd.size() + win_right_bkd.size())
163  {
164  // Vertical snake
165  dps[0] = mln::right;
166  dps[1] = mln::down;
167  dps[2] = mln::up;
168  moves[0] = &self::right;
169  moves[1] = &self::down;
170  moves[2] = &self::up;
171  }
172  else
173  {
174  // Horizontal snake
175  dps[0] = mln::down;
176  dps[1] = mln::right;
177  dps[2] = mln::left;
178  moves[0] = &self::down;
179  moves[1] = &self::right;
180  moves[2] = &self::left;
181  }
182  }
183 
184  void init()
185  {
186  initialize(output, input);
187  accu.init();
188  p = input.domain().pmin() - dps[0];
189  mln_qiter(W) q(win, p);
190  for_all(q)
191  if (input.has(q))
192  accu.take(input(q));
193  p = input.domain().pmin();
194  }
195 
196  void right()
197  {
198  for_all(q_l_fwd)
199  if (input.has(q_l_fwd))
200  accu.untake(input(q_l_fwd));
201  for_all(q_r_fwd)
202  if (input.has(q_r_fwd))
203  accu.take(input(q_r_fwd));
204  output(p) = accu;
205  }
206 
207  void left()
208  {
209  for_all(q_r_bkd)
210  if (input.has(q_r_bkd))
211  accu.untake(input(q_r_bkd));
212  for_all(q_l_bkd)
213  if (input.has(q_l_bkd))
214  accu.take(input(q_l_bkd));
215  output(p) = accu;
216  }
217 
218  void down()
219  {
220  for_all(q_top_down)
221  if (input.has(q_top_down))
222  accu.untake(input(q_top_down));
223  for_all(q_bot_down)
224  if (input.has(q_bot_down))
225  accu.take(input(q_bot_down));
226  output(p) = accu;
227  }
228 
229  void up()
230  {
231  for_all(q_bot_up)
232  if (input.has(q_bot_up))
233  accu.untake(input(q_bot_up));
234  for_all(q_top_up)
235  if (input.has(q_top_up))
236  accu.take(input(q_top_up));
237  output(p) = accu;
238  }
239 
240  };
241 
242 
243 
244  // Functor (fastest version).
245 
246  template <typename I, typename W, typename A>
247  struct transform_snake_fastest_functor
248  {
249  typedef transform_snake_fastest_functor<I,W,A> self;
250  typedef void (self::*move_fun)();
251  typedef mln_deduce(I, psite, delta) dpsite;
252 
253  const I& input;
254  const W& win;
255  A accu;
256 
257  mln_ch_value(I, mln_result(A)) output;
258 
259  mln_psite(I) p;
260 
261  window2d
262  win_left_fwd,
263  win_right_fwd,
264  win_left_bkd,
265  win_right_bkd,
266  win_bot_up,
267  win_top_up,
268  win_bot_down,
269  win_top_down;
270 
271  mln_qixter(const I, window2d)
272  q_l_fwd,
273  q_r_fwd,
274  q_l_bkd,
275  q_r_bkd,
276  q_top_up,
277  q_bot_up,
278  q_top_down,
279  q_bot_down;
280 
281 
282  std::vector<move_fun> moves;
283  std::vector<dpsite> dps;
284 
285  transform_snake_fastest_functor(const I& input, const W& win, const A& a)
286  : input(input),
287  win(win),
288  accu(a),
289 
290  win_left_fwd(win::shift(win, mln::left) - win),
291  win_right_fwd(win - win::shift(win, mln::left)),
292  win_left_bkd(win::shift(win_left_fwd, mln::right)),
293  win_right_bkd(win::shift(win_right_fwd, mln::right)),
294 
295  win_bot_up(win::shift(win, mln::down) - win),
296  win_top_up(win - win::shift(win, mln::down)),
297  win_bot_down(win::shift(win_bot_up, mln::up)),
298  win_top_down(win::shift(win_top_up, mln::up)),
299 
300  q_l_fwd(input, win_left_fwd, p),
301  q_r_fwd(input, win_right_fwd, p),
302  q_l_bkd(input, win_left_bkd, p),
303  q_r_bkd(input, win_right_bkd, p),
304 
305  q_top_up(input, win_top_up, p),
306  q_bot_up(input, win_bot_up, p),
307  q_top_down(input, win_top_down, p),
308  q_bot_down(input, win_bot_down, p),
309 
310  moves(3),
311  dps(3)
312  {
313  if (win_bot_up.size() + win_top_up.size() +
314  win_bot_down.size() + win_top_down.size() <
315  win_left_fwd.size() + win_right_fwd.size() +
316  win_left_bkd.size() + win_right_bkd.size())
317  {
318  // Vertical snake
319  dps[0] = mln::right;
320  dps[1] = mln::down;
321  dps[2] = mln::up;
322  moves[0] = &self::right;
323  moves[1] = &self::down;
324  moves[2] = &self::up;
325  }
326  else
327  {
328  // Horizontal snake
329  dps[0] = mln::down;
330  dps[1] = mln::right;
331  dps[2] = mln::left;
332  moves[0] = &self::down;
333  moves[1] = &self::right;
334  moves[2] = &self::left;
335  }
336  }
337 
338  void init()
339  {
340  // extension::adjust_fill is performed in the routine
341  // because it has to be done before the initialization of
342  // the fast iterators (q_*).
343  initialize(output, input);
344  accu.init();
345  p = input.domain().pmin() - dps[0];
346  mln_qixter(const I, W) q(input, win, p);
347  for_all(q)
348  accu.take(q.val());
349  p = input.domain().pmin();
350  }
351 
352  void right()
353  {
354  for_all(q_l_fwd)
355  accu.untake(q_l_fwd.val());
356  for_all(q_r_fwd)
357  accu.take(q_r_fwd.val());
358  output(p) = accu;
359  }
360 
361  void left()
362  {
363  for_all(q_r_bkd)
364  accu.untake(q_r_bkd.val());
365  for_all(q_l_bkd)
366  accu.take(q_l_bkd.val());
367  output(p) = accu;
368  }
369 
370  void down()
371  {
372  for_all(q_top_down)
373  accu.untake(q_top_down.val());
374  for_all(q_bot_down)
375  accu.take(q_bot_down.val());
376  output(p) = accu;
377  }
378 
379  void up()
380  {
381  for_all(q_bot_up)
382  accu.untake(q_bot_up.val());
383  for_all(q_top_up)
384  accu.take(q_top_up.val());
385  output(p) = accu;
386  }
387 
388  };
389 
390 
391  // Both dispatch and implementation (hum...)
392 
393  template <typename A, typename I, typename W>
394  inline
395  mln_ch_value(I, mln_result(A))
396  transform_snake_dispatch(metal::false_,
397  const Accumulator<A>& a,
398  const Image<I>& input, const Window<W>& win)
399  {
400  typedef transform_snake_functor<I, W, A> F;
401  F f(exact(input), exact(win), exact(a));
403  return f.output;
404  }
405 
406  template <typename A, typename I, typename W>
407  inline
408  mln_ch_value(I, mln_result(A))
409  transform_snake_dispatch(metal::true_,
410  const Accumulator<A>& a,
411  const Image<I>& input, const Window<W>& win)
412  {
413  typedef transform_snake_fastest_functor<I, W, A> F;
414  F f(exact(input), exact(win), exact(a));
416  return f.output;
417  }
418 
419  template <typename A, typename I, typename W>
420  inline
421  mln_ch_value(I, mln_result(A))
422  transform_snake_dispatch(const Accumulator<A>& a,
423  const Image<I>& input, const Window<W>& win)
424  {
425  return transform_snake_dispatch(mln_is_fastest_IW(I, W)(),
426  a, input, win);
427  }
428 
429  } // end of namespace mln::accu::internal
430 
431 
432 
433 
434  template <typename A, typename I, typename W>
435  inline
436  mln_ch_value(I, mln_result(A))
437  transform_snake(const Accumulator<A>& a,
438  const Image<I>& input, const Window<W>& win)
439  {
440  mln_trace("accu::transform_snake");
441 
442  internal::transform_snake_tests(input, win);
443 
444  extension::adjust(input, geom::delta(win) + 1);
445  mln_ch_value(I, mln_result(A)) output;
446  output = internal::transform_snake_dispatch(a, input, win);
447 
448  return output;
449  }
450 
451 
452  template <typename A, typename I, typename W>
453  inline
454  mln_ch_value(I, mln_meta_accu_result(A, mln_value(I)))
455  transform_snake(const Meta_Accumulator<A>& a,
456  const Image<I>& input, const Window<W>& win)
457  {
458  mln_trace("accu::transform_snake");
459 
460  internal::transform_snake_tests(input, win);
461 
462  typedef mln_accu_with(A, mln_value(I)) A_;
463  A_ a_ = accu::unmeta(exact(a), mln_value(I)());
464 
465  extension::adjust(input, geom::delta(win) + 1);
466  mln_ch_value(I, mln_result(A_)) output;
467  output = internal::transform_snake_dispatch(a_, input, win);
468 
469  return output;
470  }
471 
472 
473 # endif // ! MLN_INCLUDE_ONLY
474 
475  } // end of namespace mln::accu
476 
477 } // end of namespace mln
478 
479 
480 #endif // ! MLN_ACCU_TRANSFORM_SNAKE_HH