Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
labeling/value.hh
1 // Copyright (C) 2007, 2008, 2009, 2010 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_VALUE_HH
28 # define MLN_LABELING_VALUE_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 # include <mln/data/fill.hh>
38 
39 
40 
41 namespace mln
42 {
43 
44  namespace labeling
45  {
46 
55  //
56  template <typename I, typename N, typename L>
57  mln_ch_value(I, L)
58  value(const Image<I>& input, const mln_value(I)& val,
59  const Neighborhood<N>& nbh, L& nlabels);
60 
61 
62 # ifndef MLN_INCLUDE_ONLY
63 
64 
65  // Tests.
66 
67  namespace internal
68  {
69 
70  template <typename I, typename N, typename L>
71  void
72  value_tests(const Image<I>& input, const mln_value(I)& val, const Neighborhood<N>& nbh,
73  L& nlabels)
74  {
75  mln_precondition(exact(input).is_valid());
76  mln_precondition(exact(nbh).is_valid());
77 
78  (void) input;
79  (void) val;
80  (void) nbh;
81  (void) nlabels;
82  }
83 
84  } // end of namespace mln::labeling::internal
85 
86 
87 
88  namespace impl
89  {
90 
91  // Generic functor.
92 
93  template <typename I, typename L>
94  struct value_functor
95  {
96  typedef mln_psite(I) P;
97 
98  const I& input;
99  const mln_value(I)& val;
100 
101  // Requirements from mln::canvas::labeling.
102 
103  typedef mln_domain(I) S;
104 
105  // Generic implementation
106 
107  void init() {}
108  bool handles(const P& p) const { return input(p) == val; }
109  bool equiv(const P& n, const P&) const { return input(n) == val; }
110  bool labels(const P&) const { return true; }
111  void do_no_union(const P&, const P&) {}
112  void init_attr(const P&) {}
113  void merge_attr(const P&, const P&) {}
114  void set_new_label(const P&, const L&) {}
115  void set_label(const P&, const L&) {}
116  void finalize() {}
117 
118  // Fastest implementation
119 
120  void init_() {}
121  bool handles_(unsigned p) const { return input.element(p) == val; }
122  bool equiv_(unsigned n, unsigned) const { return input.element(n) == val; }
123  bool labels_(unsigned) const { return true; }
124  void do_no_union_(unsigned, unsigned) {}
125  void init_attr_(unsigned) {}
126  void merge_attr_(unsigned, unsigned) {}
127  void set_new_label_(unsigned, const L&) {}
128  void set_label_(unsigned, const L&) {}
129  void finalize_() {}
130 
131  // end of Requirements.
132 
133  value_functor(const Image<I>& input_, const mln_value(I)& val)
134  : input(exact(input_)),
135  val(val)
136  {
137  }
138  };
139 
140  } // end of namespace mln::labeling::impl
141 
142 
143 
144 
145  // Facade.
146 
147  template <typename I, typename N, typename L>
148  mln_ch_value(I, L)
149  value(const Image<I>& input, const mln_value(I)& val,
150  const Neighborhood<N>& nbh, L& nlabels)
151  {
152  trace::entering("labeling::value");
153 
154  internal::value_tests(input, val, nbh, nlabels);
155 
156  mln_ch_value(I, L) output;
157  impl::value_functor<I,L> f(input, val);
158  output = canvas::labeling::video(input, nbh, nlabels, f);
159 
160  trace::exiting("labeling::value");
161  return output;
162  }
163 
164 # endif // ! MLN_INCLUDE_ONLY
165 
166  } // end of namespace mln::labeling
167 
168 } // end of namespace mln
169 
170 
171 #endif // ! MLN_LABELING_VALUE_HH