$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
propagate_if.hh
1 // Copyright (C) 2009 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_MORPHO_TREE_PROPAGATE_IF_HH
27 # define MLN_MORPHO_TREE_PROPAGATE_IF_HH
28 
32 
33 
34 # include <mln/morpho/tree/data.hh>
35 # include <mln/morpho/tree/propagate_node.hh>
36 
37 # include <mln/data/fill.hh>
38 # include <mln/pw/all.hh>
39 
40 namespace mln
41 {
42 
43  namespace morpho
44  {
45 
46  namespace tree
47  {
48 
49  template <typename W>
50  struct way_of_propagation : Object< W > { protected: way_of_propagation() {}; };
51  struct desc_propagation : way_of_propagation <desc_propagation> {};
52  struct asc_propagation : way_of_propagation <asc_propagation> {};
53 
65  template <typename T, typename A, typename P, typename W>
66  inline
67  void
68  propagate_if(const T& tree,
69  Image<A>& a_,
70  const way_of_propagation<W>& prop_,
71  const Function_v2b<P>& pred_,
72  const mln_value(A)& v);
73 
75  template <typename T, typename A, typename P>
76  inline
77  void
78  propagate_if(const T& tree,
79  Image<A>& a_,
80  const desc_propagation& prop_,
81  const Function_v2b<P>& pred_);
82 
94  template <typename T, typename A, typename W>
95  inline
96  void
97  propagate_if_value(const T& tree,
98  Image<A>& a_,
99  const way_of_propagation<W>& prop_,
100  const mln_value(A)& v,
101  const mln_value(A)& v_prop);
102 
104  template <typename T, typename A, typename W>
105  inline
106  void
107  propagate_if_value(const T& tree,
108  Image<A>& a_,
109  const way_of_propagation<W>& prop,
110  const mln_value(A)& v);
111 
112 
113 
114 
115 # ifndef MLN_INCLUDE_ONLY
116 
117  namespace internal
118  {
119  template <typename T, typename A, typename P>
120  bool check_propagate_if(const T& t,
121  const A& a,
122  const asc_propagation& prop,
123  const P& pred,
124  const mln_value(A)& v)
125  {
126  (void) prop;
127  mln_node_piter(T) n(t);
128  for_all(n)
129  if (pred(n) && a(t.parent(n)) != v)
130  return false;
131  return true;
132  }
133 
134  template <typename T, typename A, typename P>
135  bool check_propagate_if(const T& t,
136  const A& a,
137  const desc_propagation& prop,
138  const P& pred,
139  const mln_value(A)& v)
140  {
141  (void) prop;
142  mln_node_piter(T) n(t);
143  for_all(n)
144  if (a(n) != v && pred(t.parent(n)))
145  return false;
146  return true;
147  }
148 
149  template <typename T, typename A, typename P>
150  bool check_propagate_if(const T& t,
151  const A& a,
152  const desc_propagation& prop,
153  const P& pred)
154  {
155  (void) prop;
156  mln_node_piter(T) n(t);
157  for_all(n)
158  if (a(n) != a(t.parent(n)) && pred(t.parent(n)))
159  return false;
160  return true;
161  }
162 
163  template <typename T, typename A, typename P>
164  inline
165  void
166  propagate_if(const T& tree,
167  A& a,
168  const desc_propagation& prop,
169  const P& pred,
170  const mln_value(A)& v)
171  {
172  (void) prop;
173 
174  mln_precondition(a.is_valid());
175  mln_precondition(tree.f().domain() == a.domain());
176 
177  mln_ch_value(typename T::function, bool) mark;
178  initialize(mark, tree.f());
179  mln::data::fill(mark, false);
180 
181  mln_dn_node_piter(T) n(tree);
182  for_all(n)
183  if (mark(tree.parent(n)))
184  {
185  a(n) = v;
186  mark(n) = true;
187  }
188  else if (pred(n))
189  mark(n) = true;
190  mln_postcondition(check_propagate_if(tree, a, prop, pred, v));
191  }
192 
193  template <typename T, typename A, typename P>
194  inline
195  void
196  propagate_if(const T& tree,
197  A& a,
198  const desc_propagation& prop,
199  const P& pred)
200  {
201  (void) prop;
202 
203  mln_precondition(a.is_valid());
204  mln_precondition(tree.f().domain() == a.domain());
205 
206  mln_ch_value(typename T::function, bool) mark;
207  initialize(mark, tree.f());
208  mln::data::fill(mark, false);
209 
210  mln_dn_node_piter(T) n(tree);
211  for_all(n)
212  if (mark(tree.parent(n)))
213  {
214  a(n) = a(tree.parent(n));
215  mark(n) = true;
216  }
217  else if (pred(n))
218  mark(n) = true;
219  mln_postcondition(check_propagate_if(tree, a, prop, pred));
220  }
221 
222 
223  template <typename T, typename A, typename P>
224  inline
225  void
226  propagate_if(const T& tree,
227  A& a,
228  const asc_propagation& prop,
229  const P& pred,
230  const mln_value(A)& v)
231  {
232  (void) prop;
233 
234  mln_precondition(a.is_valid());
235  mln_precondition(tree.f().domain() == a.domain());
236 
237  mln_ch_value(typename T::function, bool) mark;
238  initialize(mark, tree.f());
239  mln::data::fill(mark, false);
240 
241  mln_up_node_piter(T) n(tree);
242  for_all(n)
243  if (mark(n))
244  {
245  a(n) = v;
246  mark(tree.parent(n)) = true;
247  }
248  else if (pred(n))
249  mark(tree.parent(n)) = true;
250 
251  mln_postcondition(check_propagate_if(tree, a, prop, pred, v));
252  }
253 
254  } // end of namespace mln::morpho::tree::internal
255 
256 
257  // Facades
258 
259  template <typename T, typename A, typename W>
260  inline
261  void
262  propagate_if_value(const T& tree,
263  Image<A>& a_,
264  const way_of_propagation<W>& prop_,
265  const mln_value(A)& v,
266  const mln_value(A)& v_prop)
267  {
268  A& a = exact(a_);
269  const W& prop = exact(prop_);
270 
271  internal::propagate_if(tree, a, prop, pw::value(a) == pw::cst(v), v_prop);
272  }
273 
274 
275  template <typename T, typename A, typename W>
276  inline
277  void
278  propagate_if_value(const T& tree,
279  Image<A>& a_,
280  const way_of_propagation<W>& prop_,
281  const mln_value(A)& v)
282  {
283  A& a = exact(a_);
284  const W& prop = exact(prop_);
285 
286  internal::propagate_if(tree, a, prop, pw::value(a) == pw::cst(v), v);
287  }
288 
289 
290  template <typename T, typename A, typename P, typename W>
291  inline
292  void
293  propagate_if(const T& tree,
294  Image<A>& a_,
295  const way_of_propagation<W>& prop_,
296  const Function_v2b<P>& pred_,
297  const mln_value(A)& v)
298  {
299  A& a = exact(a_);
300  const W& prop = exact(prop_);
301  const P& pred = exact(pred_);
302 
303  internal::propagate_if(tree, a, prop, pred, v);
304  }
305 
306 
307  template <typename T, typename A, typename P>
308  inline
309  void
310  propagate_if(const T& tree,
311  Image<A>& a_,
312  const desc_propagation& prop,
313  const Function_v2b<P>& pred_)
314  {
315  A& a = exact(a_);
316  const P& pred = exact(pred_);
317 
318  internal::propagate_if(tree, a, prop, pred);
319  }
320 
321 #endif // ! MLN_INCLUDE_ONLY
322 
323 
324  } // end of namespace mln::morpho::tree
325 
326  } // end of namespace mln::morpho
327 
328 } // end of namespace mln
329 
330 #endif // ! MLN_MORPHO_TREE_PROPAGATE_IF_HH