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