Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
value_and_compute.hh
1 // Copyright (C) 2010 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_LABELING_VALUE_AND_COMPUTE_HH
27 # define MLN_LABELING_VALUE_AND_COMPUTE_HH
28 
32 
33 # include <mln/core/concept/image.hh>
34 # include <mln/core/concept/neighborhood.hh>
35 # include <mln/canvas/labeling/video.hh>
36 # include <mln/data/fill.hh>
37 
38 
39 
40 namespace mln
41 {
42 
43  namespace labeling
44  {
45 
54  //
55  template <typename I, typename N, typename L, typename A>
56  util::couple<mln_ch_value(I,L),
57  util::couple<util::array<mln_result(A)>,
58  util::array<A> > >
59  value_and_compute(const Image<I>& input, const mln_value(I)& val,
60  const Neighborhood<N>& nbh, L& nlabels,
61  const Accumulator<A>& accu);
62 
63 
64 # ifndef MLN_INCLUDE_ONLY
65 
66 
67  // Tests.
68 
69  namespace internal
70  {
71 
72  template <typename I, typename N, typename L, typename A>
73  void
74  value_and_compute_tests(const Image<I>& input, const mln_value(I)& val,
75  const Neighborhood<N>& nbh, L& nlabels,
76  const Accumulator<A>& accu)
77  {
78  mln_precondition(exact(input).is_valid());
79  mln_precondition(exact(nbh).is_valid());
80 
81  (void) accu;
82  (void) input;
83  (void) val;
84  (void) nbh;
85  (void) nlabels;
86  }
87 
88  } // end of namespace mln::labeling::internal
89 
90 
91 
92  namespace impl
93  {
94 
95  // Generic functor.
96 
97  template <typename I, typename L, typename A>
98  struct value_and_compute_functor
99  {
100  typedef mln_psite(I) P;
101 
102  util::array<mln_result(A)> result_;
103  util::array<A> accus_;
104 
105  const I& input;
106  const mln_value(I)& val;
107 
108  typedef mln_result(A) accu_result;
109  typedef mln_argument(A) accu_argument;
110  typedef util::couple<util::array<accu_result>,
111  util::array<A> > result;
112 
113 
114  // Requirements from mln::canvas::labeling.
115 
116  typedef mln_domain(I) S;
117 
118  // Generic implementation
119 
120  void init() {}
121  bool handles(const P& p) const { return input(p) == val; }
122  bool equiv(const P& n, const P&) const { return input(n) == val; }
123  bool labels(const P&) const { return true; }
124  void do_no_union(const P&, const P&) {}
125  void init_attr(const P&) {}
126  void merge_attr(const P&, const P&) {}
127  void set_new_label(const P& p, const L& l)
128  {
129  accus_.append(A());
130  process__(accu_argument(), p, l);
131  }
132  void set_label(const P& p, const L& l) { process__(accu_argument(), p, l); };
133  void finalize() { convert::from_to(accus_, result_); }
134 
135 
136 
137  // Fastest implementation
138 
139  void init_() { accus_.append(A()); }
140  bool handles_(unsigned p) const { return input.element(p) == val; }
141  bool equiv_(unsigned n, unsigned) const { return input.element(n) == val; }
142  bool labels_(unsigned) const { return true; }
143  void do_no_union_(unsigned, unsigned) {}
144  void init_attr_(unsigned) {}
145  void merge_attr_(unsigned, unsigned) {}
146  void set_new_label_(unsigned p, const L& l)
147  {
148  accus_.append(A());
149  process__(accu_argument(), p, l);
150  };
151  void set_label_(unsigned p, const L& l) { process__(accu_argument(), p, l); };
152  void finalize_() { convert::from_to(accus_, result_); }
153 
154  // end of Requirements.
155 
156 
157 
158  value_and_compute_functor(const Image<I>& input_, const mln_value(I)& val)
159  : input(exact(input_)),
160  val(val)
161  {
162  }
163 
164 
165  private:
166  inline
167  void process__(const unsigned&, unsigned p, const L& l)
168  {
169  accus_[l].take(p);
170  }
171 
172  inline
173  void process__(const mln_psite(I)&, unsigned p, const L& l)
174  {
175  accus_[l].take(input.point_at_index(p));
176  }
177 
178 
179  inline
180  void process__(const mln_psite(I)&, const mln_site(I)& p, const L& l)
181  {
182  accus_[l].take(p);
183  }
184 
185  inline
186  void process__(const mln_value(I)&, const mln_site(I)&, const L& l)
187  {
188  accus_[l].take(l);
189  }
190 
191  template <typename V>
192  inline
193  void process__(const V&, const mln_site(I)&, const L& l)
194  {
195  mlc_abort(V)::check();
196  }
197 
198 
199  };
200 
201  } // end of namespace mln::labeling::impl
202 
203 
204 
205 
206  // Facade.
207 
208  template <typename I, typename N, typename L, typename A>
209  util::couple<mln_ch_value(I,L),
210  util::couple<util::array<mln_result(A)>,
211  util::array<A> > >
212  value_and_compute(const Image<I>& input, const mln_value(I)& val,
213  const Neighborhood<N>& nbh, L& nlabels,
214  const Accumulator<A>& accu)
215  {
216  trace::entering("labeling::value_and_compute");
217 
218  internal::value_and_compute_tests(input, val, nbh, nlabels, accu);
219 
220  typedef mln_ch_value(I,L) out_t;
221  typedef impl::value_and_compute_functor<I, L, A> func_t;
222  func_t f(input, val);
223  out_t output = canvas::labeling::video(input, nbh, nlabels, f);
224 
226  result = make::couple(output,
227  make::couple(f.result_, f.accus_));
228 
229 
230  trace::exiting("labeling::value_and_compute");
231  return result;
232  }
233 
234 # endif // ! MLN_INCLUDE_ONLY
235 
236  } // end of namespace mln::labeling
237 
238 } // end of namespace mln
239 
240 
241 #endif // ! MLN_LABELING_VALUE_AND_COMPUTE_HH