$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
generic.hh
1 // Copyright (C) 2009, 2012, 2013 EPITA Research and Development
2 // 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_LABELING_GENERIC_HH
28 # define MLN_CANVAS_LABELING_GENERIC_HH
29 
34 
35 # include <mln/core/concept/image.hh>
36 # include <mln/core/concept/neighborhood.hh>
37 # include <mln/core/concept/site_set.hh>
38 
39 # include <mln/data/fill.hh>
40 
41 namespace mln
42 {
43 
44  namespace canvas
45  {
46 
47  namespace labeling
48  {
49 
50  namespace impl
51  {
52 
53  namespace generic
54  {
55 
61  template <typename I, typename N, typename L,
62  typename S, typename F>
63  mln_ch_value(I, L)
64  labeling(const Image<I>& input_, const Neighborhood<N>& nbh_,
65  L& nlabels, const Site_Set<S>& s_, F& f);
66 
67 
68 # ifndef MLN_INCLUDE_ONLY
69 
70  template <typename I>
71  static inline
72  mln_psite(I)
73  find_root(I& parent, const mln_psite(I)& x)
74  {
75  if (parent(x) == x)
76  return x;
77  else
78  return parent(x) = find_root(parent, parent(x));
79  }
80 
81 
82 
83  template <typename I, typename N, typename L,
84  typename S, typename F>
85  mln_ch_value(I, L)
86  labeling(const Image<I>& input_, const Neighborhood<N>& nbh_,
87  L& nlabels, const Site_Set<S>& s_, F& f)
88  {
89  mln_trace("canvas::labeling::impl::generic::labeling");
90 
91  // FIXME: Test?!
92 
93  const I& input = exact(input_);
94  const N& nbh = exact(nbh_);
95  const S& s = exact(s_);
96 
97  // Local type.
98  typedef mln_psite(I) P;
99 
100  // Auxiliary data.
101  mln_ch_value(I, bool) deja_vu;
102  mln_ch_value(I, P) parent;
103 
104  // Output.
105  mln_ch_value(I, L) output;
106 
107  // Initialization.
108  {
109  initialize(deja_vu, input);
110  mln::data::fill(deja_vu, false);
111 
112  initialize(parent, input);
113 
114  initialize(output, input);
115  mln::data::fill(output, L(literal::zero));
116  nlabels = 0;
117 
118  f.init(); // <-- f.init() - Client initialization.
119  }
120 
121  // First Pass.
122  {
123  mln_bkd_piter(S) p(s); // Backward.
124  mln_niter(N) n(nbh, p);
125  for_all(p) if (f.handles(p)) // <-- f.handles()
126  {
127  // Make-Set.
128  parent(p) = p;
129  f.init_attr(p);
130 
131  for_all(n)
132  if (input.domain().has(n) && deja_vu(n))
133  {
134  if (f.equiv(n, p)) // <-- f.equiv()
135  {
136  // Do-Union.
137  P r = find_root(parent, n);
138  if (r != p)
139  {
140  parent(r) = p;
141  f.merge_attr(r, p); // <-- f.merge_attr()
142  }
143  }
144  else
145  f.do_no_union(n, p); // <-- f.do_no_union()
146  }
147  deja_vu(p) = true;
148  }
149  }
150 
151  // Second Pass.
152  {
153  mln_fwd_piter(S) p(s); // Forward.
154  for_all(p) if (f.handles(p)) // <-- f.handles()
155  {
156  if (parent(p) == p) // if p is root
157  {
158  if (f.labels(p)) // <-- f.labels()
159  {
160  if (nlabels == mln_max(L))
161  {
162  mln_trace_warning("labeling aborted! Too many labels \
163  for this label type: nlabels > \
164  max(label_type).");
165 
166  return output;
167  }
168  output(p) = ++nlabels;
169  }
170  }
171  else
172  output(p) = output(parent(p));
173  }
174  }
175 
176  return output;
177  }
178 
179 # endif // ! MLN_INCLUDE_ONLY
180 
181  } // end of namespace mln::canvas::labeling::impl::generic
182 
183  } // end of namespace mln::canvas::labeling::impl
184 
185  } // end of namespace mln::canvas::labeling
186 
187  } // end of namespace mln::canvas
188 
189 } // end of namespace mln
190 
191 
192 #endif // ! MLN_CANVAS_LABELING_GENERIC_HH