$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
snake_generic.hh
1 // Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012 EPITA Research and
2 // Development Laboratory (LRDE)
3 //
4 // This file is part of Olena.
5 //
6 // Olena is free software: you can redistribute it and/or modify it under
7 // the terms of the GNU General Public License as published by the Free
8 // Software Foundation, version 2 of the License.
9 //
10 // Olena is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // As a special exception, you may use this file as part of a free
19 // software project without restriction. Specifically, if other files
20 // instantiate templates or use macros or inline functions from this
21 // file, or you compile this file and link it with other files to produce
22 // an executable, this file does not by itself cause the resulting
23 // executable to be covered by the GNU General Public License. This
24 // exception does not however invalidate any other reasons why the
25 // executable file might be covered by the GNU General Public License.
26 
27 #ifndef MLN_CANVAS_BROWSING_SNAKE_GENERIC_HH
28 # define MLN_CANVAS_BROWSING_SNAKE_GENERIC_HH
29 
33 
34 # include <vector>
35 # include <mln/core/concept/browsing.hh>
36 
37 
38 namespace mln
39 {
40 
41  namespace canvas
42  {
43 
44  namespace browsing
45  {
46 
76  struct snake_generic_t : public Browsing< snake_generic_t >
77  {
78 
79  // This default constructor is needed for compilation with gcc
80  // 4.6.0, gcc 4.6.1 and Clang.
82 
83  template <typename F>
84  void operator()(F& f) const;
85 
86  };
87 
88  extern const snake_generic_t snake_generic;
89 
90 # ifndef MLN_INCLUDE_ONLY
91 
92 # ifndef MLN_WO_GLOBAL_VARS
93 
95 
96 # endif // ! MLN_WO_GLOBAL_VARS
97 
98  inline
100  {
101  }
102 
103  template <typename F>
104  inline
105  void
106  snake_generic_t::operator()(F& f) const
107  {
108  mln_trace("canvas::browsing::snake_generic");
109  mln_precondition(f.input.is_valid());
110 
111  // p init
112  f.p = f.input.bbox().pmin();// - f.dps[0];
113 
114  std::vector< int > directions(f.moves.size(), 0);
115  unsigned deph = 0;
116  unsigned total_deph = f.moves.size() / 2 + 1;
117 
118  // initialization
119  f.init();
120 
121  bool first = true;
122  directions[deph] = 1;
123  deph = total_deph - 1;
124 
125  // Call the move function (for the first point)
126  (f.*(f.moves[(deph - 1) * 2 - 1 + directions[deph - 1]])) ();
127  while (deph > 0) // If direction is empty, break
128  {
129  mln_assertion(deph <= total_deph);
130  mln_assertion(deph > 0);
131  // If f.p is near the border (we ended a direction) -> next child
132  if (!f.input.domain().has(f.p +
133  f.dps[(deph - 1) * 2 - 1 + directions[deph - 1]]))
134  {
135  // Go up the tree
136  deph--;
137  if (deph >= 1)
138  // Change directions
139  directions[deph] = directions[deph] == 1 ? 0 : 1;
140  continue;
141  }
142 
143  if (!first)
144  {
145  // Move f.p
146  f.p += f.dps[(deph - 1) * 2 - 1 + directions[deph - 1]];
147  // Call the move function
148  (f.*(f.moves[(deph - 1) * 2 - 1 + directions[deph - 1]])) ();
149  }
150  else
151  first = false;
152 
153  if (deph != total_deph)
154  {
155  // Go down the tree
156  deph++;
157  first = true;
158  }
159  }
160 
161  }
162 
163 # endif // ! MLN_INCLUDE_ONLY
164 
165  } // end of namespace mln::canvas::browsing
166 
167  } // end of namespace mln::canvas
168 
169 } // end of namespace mln
170 
171 
172 #endif // ! MLN_CANVAS_BROWSING_SNAKE_GENERIC_HH