$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
predicate.hh
1 // Copyright (C) 2007, 2008, 2009, 2011 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_TEST_PREDICATE_HH
28 # define MLN_TEST_PREDICATE_HH
29 
33 
34 # include <mln/core/concept/image.hh>
35 # include <mln/core/concept/function.hh>
36 # include <mln/core/concept/site_set.hh>
37 
38 
39 namespace mln
40 {
41 
42  namespace test
43  {
44 
50  //
51  template <typename I, typename F>
52  bool predicate(const Image<I>& ima, const Function_v2b<F>& f);
53 
54 
61  //
62  template <typename I, typename J, typename F>
63  bool predicate(const Image<I>& lhs, const Image<J>& rhs,
64  const Function_vv2b<F>& f);
65 
66 
71  //
72  template <typename S, typename F>
73  bool predicate(const Site_Set<S>& pset, const Function_v2b<F>& f);
74 
75 
76 # ifndef MLN_INCLUDE_ONLY
77 
78 
79  // Tests.
80 
81  namespace internal
82  {
83 
84  template <typename I, typename F>
85  inline
86  void predicate_tests(const Image<I>& ima,
87  const Function_v2b<F>& f)
88  {
89  mln_precondition(exact(ima).is_valid());
90  (void) ima;
91  (void) f;
92  }
93 
94  template <typename I, typename J, typename F>
95  inline
96  void predicate_tests(const Image<I>& lhs_,
97  const Image<J>& rhs_,
98  const Function_vv2b<F>& f)
99  {
100  const I& lhs = exact(lhs_);
101  const J& rhs = exact(rhs_);
102 
103  mln_precondition(lhs.is_valid());
104  mln_precondition(rhs.is_valid());
105  mln_precondition(lhs.domain() == rhs.domain());
106  (void) lhs;
107  (void) rhs;
108  (void) f;
109  }
110 
111  template <typename S, typename F>
112  inline
113  void predicate_tests(const Site_Set<S>& pset,
114  const Function_v2b<F>& f)
115  {
116  mln_precondition(exact(pset).is_valid());
117  (void) pset;
118  (void) f;
119  }
120 
121  } // end of namespace mln::test::internal
122 
123 
124  // Implementations.
125 
126  namespace impl
127  {
128 
129  template <typename I, typename F>
130  inline
131  bool predicate_(trait::image::speed::any, const I& ima, const F& f)
132  {
133  internal::predicate_tests(ima, f);
134 
135  mln_piter(I) p(ima.domain());
136  for_all(p)
137  if (! f(ima(p)))
138  return false;
139  return true;
140  }
141 
142  template <typename I, typename F>
143  inline
144  bool predicate_(trait::image::speed::fastest, const I& ima, const F& f)
145  {
146  internal::predicate_tests(ima, f);
147 
148  mln_pixter(const I) pxl(ima);
149  for_all(pxl)
150  if (! f(pxl.val()))
151  return false;
152  return true;
153  }
154 
155  template <typename I, typename J, typename F>
156  inline
157  bool predicate_(trait::image::speed::any,
158  trait::image::speed::any,
159  const I& lhs, const J& rhs, const F& f)
160  {
161  internal::predicate_tests(lhs, rhs, f);
162 
163  mln_piter(I) p(lhs.domain());
164  for_all(p)
165  if (! f(lhs(p), rhs(p)))
166  return false;
167  return true;
168  }
169 
170  template <typename I, typename J, typename F>
171  inline
172  bool predicate_(trait::image::speed::fastest,
173  trait::image::speed::fastest,
174  const I& lhs, const J& rhs, const F& f)
175  {
176  internal::predicate_tests(lhs, rhs, f);
177 
178  mln_pixter(const I) pxl1(lhs);
179  mln_pixter(const J) pxl2(rhs);
180  for_all_2(pxl1, pxl2)
181  if (! f(pxl1.val(), pxl2.val()))
182  return false;
183  return true;
184  }
185 
186  template <typename S, typename F>
187  inline
188  bool predicate_(const Site_Set<S>& pset, const F& f)
189  {
190  internal::predicate_tests(pset, f);
191 
192  mln_piter(S) p(exact(pset));
193  for_all(p)
194  if (! f(p))
195  return false;
196  return true;
197  }
198 
199  } // end of namespace mln::test::impl
200 
201 
202 
203  // Facades.
204 
205 
206  template <typename I, typename F>
207  inline
208  bool predicate(const Image<I>& ima, const Function_v2b<F>& f)
209  {
210  mln_trace("test::predicate");
211 
212  internal::predicate_tests(ima, f);
213  bool res = impl::predicate_(mln_trait_image_speed(I)(), exact(ima),
214  exact(f));
215 
216  return res;
217  }
218 
219 
220  template <typename I, typename J, typename F>
221  inline
222  bool predicate(const Image<I>& lhs_, const Image<J>& rhs_,
223  const Function_vv2b<F>& f)
224  {
225  mln_trace("test::predicate");
226 
227  const I& lhs = exact(lhs_);
228  const J& rhs = exact(rhs_);
229 
230  internal::predicate_tests(lhs_, rhs_, f);
231 
232  bool res = impl::predicate_(mln_trait_image_speed(I)(),
233  mln_trait_image_speed(J)(),
234  lhs, rhs,
235  exact(f));
236 
237  return res;
238  }
239 
240  template <typename S, typename F>
241  inline
242  bool predicate(const Site_Set<S>& pset, const Function_v2b<F>& f)
243  {
244  mln_trace("test::predicate");
245 
246  internal::predicate_tests(pset, f);
247 
248  bool res = impl::predicate_(exact(pset), exact(f));
249 
250  return res;
251  }
252 
253 # endif // ! MLN_INCLUDE_ONLY
254 
255  } // end of namespace mln::test
256 
257 } // end of namespace mln
258 
259 
260 #endif // ! MLN_TEST_PREDICATE_HH