Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
regional_minima.hh
1 // Copyright (C) 2007, 2008, 2009 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_LABELING_REGIONAL_MINIMA_HH
28 # define MLN_LABELING_REGIONAL_MINIMA_HH
29 
33 
34 # include <mln/core/concept/image.hh>
35 # include <mln/core/concept/neighborhood.hh>
36 # include <mln/canvas/labeling/sorted.hh>
37 # include <mln/data/fill.hh>
38 # include <mln/data/sort_psites.hh>
39 
40 
41 namespace mln
42 {
43 
44  namespace labeling
45  {
46 
56  template <typename I, typename N, typename L>
57  mln_ch_value(I, L)
58  regional_minima(const Image<I>& input, const Neighborhood<N>& nbh,
59  L& nlabels);
60 
61 
62 # ifndef MLN_INCLUDE_ONLY
63 
64  namespace impl
65  {
66 
67  // Generic functor.
68 
69  template <typename I>
70  struct regional_minima_functor
71  {
72  typedef mln_psite(I) P;
73 
74  // requirements from mln::canvas::labeling:
75 
76  const I& input;
77 
78  // Generic implementation
79 
80  void init() { data::fill(attr, true); }
81  bool handles(const P&) const { return true; }
82  bool labels(const P& p) const { return attr(p); }
83  bool equiv(const P& n, const P& p) const { return input(n) ==
84  input(p); }
85  void do_no_union(const P& n, const P& p)
86  {
87  // Avoid a warning about an undefined variable when NDEBUG
88  // is not defined.
89  (void)n;
90 
91  mln_invariant(input(n) < input(p));
92  attr(p) = false;
93  }
94 
95  void init_attr(const P&) {}
96  void merge_attr(const P& r, const P& p) { attr(p) = attr(p) &&
97  attr(r); }
98 
99  // Fastest implementation
100 
101  void init_() { data::fill(attr, true); }
102  bool handles_(unsigned) const { return true; }
103  bool labels_(unsigned p) const { return attr.element(p); }
104  bool equiv_(unsigned n, unsigned p) const { return input.element(n) ==
105  input.element(p); }
106  void do_no_union_(unsigned n, unsigned p)
107  {
108  // Avoid a warning about an undefined variable when NDEBUG
109  // is not defined.
110  (void)n;
111 
112  mln_invariant(input.element(n) < input.element(p));
113  attr.element(p) = false;
114  }
115 
116  void init_attr_(unsigned) {}
117  void merge_attr_(unsigned r, unsigned p) { attr.element(p) = attr.element(p) &&
118  attr.element(r); }
119 
120  // end of requirements
121 
122  mln_ch_value(I, bool) attr;
123 
124  regional_minima_functor(const I& input)
125  : input(input)
126  {
127  initialize(attr, input);
128  }
129  };
130 
131 
132  } // end of namespace mln::labeling::impl
133 
134 
135 
136  // Facade.
137 
138  template <typename I, typename N, typename L>
139  mln_ch_value(I, L)
140  regional_minima(const Image<I>& input_, const Neighborhood<N>& nbh_,
141  L& nlabels)
142  {
143  trace::entering("labeling::regional_minima");
144 
145  const I& input = exact(input_);
146  const N& nbh = exact(nbh_);
147  mln_precondition(input.is_valid());
148 
149  // FIXME: abort if L is not wide enough to encode the set of
150  // minima.
151 
152  typedef impl::regional_minima_functor<I> F;
153  F f(exact(input));
154  mln_ch_value(I, L)
155  output = canvas::labeling::sorted(input, nbh, nlabels, f, false);
156 
157  trace::exiting("labeling::regional_minima");
158  return output;
159  }
160 
161 # endif // ! MLN_INCLUDE_ONLY
162 
163  } // end of namespace mln::labeling
164 
165 } // end of namespace mln
166 
167 
168 #endif // ! MLN_LABELING_REGIONAL_MINIMA_HH