Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
regional_maxima.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_MAXIMA_HH
28 # define MLN_LABELING_REGIONAL_MAXIMA_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_maxima(const Image<I>& input, const Neighborhood<N>& nbh,
59  L& nlabels);
60 
61 
62 
63 # ifndef MLN_INCLUDE_ONLY
64 
65  namespace impl
66  {
67 
68  // Generic functor.
69 
70  template <typename I>
71  struct regional_maxima_functor
72  {
73  typedef mln_psite(I) P;
74 
75  // requirements from mln::canvas::labeling:
76 
77  const I& input;
78 
79  // Generic implementation
80 
81  void init() { data::fill(attr, true); }
82  bool handles(const P&) const { return true; }
83  bool labels(const P& p) const { return attr(p); }
84  bool equiv(const P& n, const P& p) const { return input(n) ==
85  input(p); }
86  void do_no_union(const P& n, const P& p) { mln_invariant(input(n) >
87  input(p));
88  attr(p) = false; (void)n; }
89  void init_attr(const P&) {}
90  void merge_attr(const P& r, const P& p) { attr(p) = attr(p) &&
91  attr(r); }
92 
93  // Fastest implementation
94 
95  void init_() { data::fill(attr, true); }
96  bool handles_(unsigned) const { return true; }
97  bool labels_(unsigned p) const { return attr.element(p); }
98  bool equiv_(unsigned n, unsigned p) const { return input.element(n) ==
99  input.element(p); }
100  void do_no_union_(unsigned n, unsigned p) { // mln_invariant(input.element(n) >
101  // input.element(p));
102  attr.element(p) = false;
103  (void) n;
104  }
105  void init_attr_(unsigned) {}
106  void merge_attr_(unsigned r, unsigned p) { attr.element(p) = attr.element(p) &&
107  attr.element(r); }
108 
109  // end of requirements
110 
111  mln_ch_value(I, bool) attr;
112 
113  regional_maxima_functor(const I& input)
114  : input(input)
115  {
116  initialize(attr, input);
117  }
118  };
119 
120 
121  } // end of namespace mln::labeling::impl
122 
123 
124 
125 
126  // Facade.
127 
128  template <typename I, typename N, typename L>
129  mln_ch_value(I, L)
130  regional_maxima(const Image<I>& input_, const Neighborhood<N>& nbh_,
131  L& nlabels)
132  {
133  trace::entering("labeling::regional_maxima");
134 
135  const I& input = exact(input_);
136  const N& nbh = exact(nbh_);
137  mln_precondition(input.is_valid());
138 
139  typedef impl::regional_maxima_functor<I> F;
140  F f(exact(input));
141  mln_ch_value(I, L)
142  output = canvas::labeling::sorted(input, nbh, nlabels, f, true);
143 
144  trace::exiting("labeling::regional_maxima");
145  return output;
146  }
147 
148 # endif // ! MLN_INCLUDE_ONLY
149 
150  } // end of namespace mln::labeling
151 
152 } // end of namespace mln
153 
154 
155 #endif // ! MLN_LABELING_REGIONAL_MAXIMA_HH