Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
flat_zones.hh
1 // Copyright (C) 2007, 2008, 2009, 2010, 2011 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_LABELING_FLAT_ZONES_HH
28 # define MLN_LABELING_FLAT_ZONES_HH
29 
33 
34 # include <mln/core/concept/image.hh>
35 # include <mln/core/concept/neighborhood.hh>
36 # include <mln/canvas/labeling/video.hh>
37 
38 
39 namespace mln
40 {
41 
42  namespace labeling
43  {
44 
52  template <typename I, typename N, typename L>
53  mln_ch_value(I, L)
54  flat_zones(const Image<I>& input, const Neighborhood<N>& nbh,
55  L& nlabels);
56 
57 
58 
59 # ifndef MLN_INCLUDE_ONLY
60 
61  namespace impl
62  {
63 
64  // Flat zone functor for the labeling canvas.
65 
66  template <typename I, typename L>
67  struct flat_zones_functor
68  {
69  const I& input;
70 
71  // Generic implementation.
72 
73  typedef mln_psite(I) P;
74 
75  void init() {}
76  bool handles(const P&) const { return true; }
77 
78  bool equiv(const P& n, const P& p) const
79  { return input(n) == input(p); }
80 
81  bool labels(const P&) const { return true; }
82  void do_no_union(const P&, const P&) {}
83  void init_attr(const P&) {}
84  void merge_attr(const P&, const P&) {}
85  void set_new_label(const P&, const L&){}
86  void set_label(const P&, const L&) {}
87  void finalize() {}
88 
89 
90  // Fastest implementation.
91 
92  void init_() {}
93 
94  bool handles_(unsigned) const
95  { return true; }
96 
97  bool equiv_(unsigned n, unsigned p) const
98  { return input.element(n) == input.element(p); }
99 
100  bool labels_(unsigned) const { return true; }
101  void do_no_union_(unsigned, unsigned) {}
102  void init_attr_(unsigned) {}
103  void merge_attr_(unsigned, unsigned) {}
104  void set_new_label_(unsigned, const L&) {}
105  void set_label_(unsigned, const L&) {}
106  void finalize_() {}
107 
108  // end of requirements.
109 
110  flat_zones_functor(const I& input)
111  : input(input)
112  {}
113  };
114 
115 
116  } // end of namespace mln::labeling::impl
117 
118 
119 
120  // Facade.
121 
122  template <typename I, typename N, typename L>
123  mln_ch_value(I, L)
124  flat_zones(const Image<I>& input_, const Neighborhood<N>& nbh_,
125  L& nlabels)
126  {
127  trace::entering("labeling::flat_zones");
128 
129  const I& input = exact(input_);
130  const N& nbh = exact(nbh_);
131  mln_precondition(input.is_valid());
132 
133  // Call the labeling canvas.
134  typedef impl::flat_zones_functor<I,L> F;
135  F f(input);
136  mln_ch_value(I, L)
137  output = canvas::labeling::video(input, nbh, nlabels, f);
138 
139  trace::exiting("labeling::flat_zones");
140  return output;
141  }
142 
143 # endif // ! MLN_INCLUDE_ONLY
144 
145  } // end of namespace mln::labeling
146 
147 } // end of namespace mln
148 
149 
150 #endif // ! MLN_LABELING_FLAT_ZONES_HH