Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
image_if.hh
1 // Copyright (C) 2007, 2008, 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_CORE_IMAGE_DMORPH_IMAGE_IF_HH
27 # define MLN_CORE_IMAGE_DMORPH_IMAGE_IF_HH
28 
33 
34 # include <mln/core/internal/image_domain_morpher.hh>
35 # include <mln/core/site_set/p_if.hh>
36 # include <mln/pw/all.hh>
37 # include <mln/convert/to_fun.hh>
38 
39 
40 namespace mln
41 {
42 
43  // Forward declaration.
44  template <typename I, typename F> struct image_if;
45 
46 
47  namespace internal
48  {
49 
51  template <typename I, typename F>
52  struct data< image_if<I,F> >
53  {
54  data(I& ima, const F& f);
55 
56  I ima_;
57  p_if<mln_domain(I), F> domain_;
58  };
59 
60  } // end of namespace mln::internal
61 
62 
63  namespace trait
64  {
65 
66  template <typename I, typename F>
67  struct image_< image_if<I,F> > : default_image_morpher< I,
68  mln_value(I),
69  image_if<I,F> >
70  {
71  typedef trait::image::category::domain_morpher category;
72 
73  typedef trait::image::ext_domain::none ext_domain; // No extension of domain.
74  typedef trait::image::ext_value::irrelevant ext_value;
75  typedef trait::image::ext_io::irrelevant ext_io;
76 
77  typedef trait::image::vw_io::none vw_io;
78  typedef trait::image::vw_set::none vw_set;
79  typedef trait::image::value_alignment::not_aligned value_alignment;
80  typedef trait::image::value_storage::disrupted value_storage;
81  };
82 
83  } // end of namespace mln::trait
84 
85 
86 
91  //
92  template <typename I, typename F>
93  struct image_if : public internal::image_domain_morpher< I,
94  p_if<mln_domain(I), F>,
95  image_if<I, F> >
96  {
98  typedef image_if< tag::image_<I>, tag::function_<F> > skeleton;
99 
101  image_if();
102 
104  image_if(I& ima, const F& f);
105 
106  void init_(I& ima, const F& f);
107 
109  const p_if<mln_domain(I), F>& domain() const;
110 
112  operator image_if<const I, F>() const;
113  };
114 
115 
116 
117  // Operators.
118 
119 
120  // Image | Function_v2b.
121 
124  //
125  template <typename I, typename F>
126  image_if<I,F>
127  operator | (Image<I>& ima, const Function_v2b<F>& f);
128 
131  //
132  template <typename I, typename F>
133  image_if<const I,F>
134  operator | (const Image<I>& ima, const Function_v2b<F>& f);
135 
136 
137 
138  template <typename I, typename A>
139  image_if< const I, fun::C<bool(*)(A)> >
140  operator | (const Image<I>& ima, bool (*f)(A) );
141 
142  template <typename I, typename A>
143  image_if< I, fun::C<bool(*)(A)> >
144  operator | (Image<I>& ima, bool (*f)(A) );
145 
146 
147 
148 # ifndef MLN_INCLUDE_ONLY
149 
150  // init_.
151 
152  template <typename I, typename F>
153  void init_(tag::function_t, F& f, const image_if<I,F>& model)
154  {
155  f = model.domain().predicate();
156  }
157 
158  template <typename I, typename F, typename J>
159  void init_(tag::image_t, image_if<I,F>& target, const J& model)
160  {
161  I ima;
162  init_(tag::image, ima, exact(model));
163  F f;
164  init_(tag::function, f, exact(model));
165  target.init_(ima, f);
166  }
167 
168  // internal::data< image_if<I,F> >
169 
170  namespace internal
171  {
172 
173  template <typename I, typename F>
174  inline
175  data< image_if<I,F> >::data(I& ima, const F& f)
176  : ima_(ima),
177  domain_(ima.domain() | f)
178  {
179  }
180 
181  }
182 
183 
184  // image_if<I,F>
185 
186  template <typename I, typename F>
187  inline
188  image_if<I,F>::image_if()
189  {
190  }
191 
192  template <typename I, typename F>
193  inline
194  image_if<I,F>::image_if(I& ima, const F& f)
195  {
196  init_(ima, f);
197  }
198 
199  template <typename I, typename F>
200  inline
201  void
202  image_if<I,F>::init_(I& ima, const F& f)
203  {
204  mln_precondition(! this->is_valid());
205  this->data_ = new internal::data< image_if<I,F> >(ima, f);
206  }
207 
208  template <typename I, typename F>
209  inline
210  const p_if<mln_domain(I), F>&
211  image_if<I,F>::domain() const
212  {
213  mln_precondition(this->is_valid());
214  return this->data_->domain_;
215  }
216 
217  template <typename I, typename F>
218  inline
219  image_if<I,F>::operator image_if<const I,F>() const
220  {
221  mln_precondition(this->is_valid());
222  image_if<const I,F> tmp(this->data_->ima_,
223  this->data_->domain_.predicate());
224  return tmp;
225  }
226 
227 
228  // Operators.
229 
230  template <typename I, typename F>
231  inline
232  image_if<I,F>
233  operator | (Image<I>& ima, const Function_v2b<F>& f)
234  {
235  image_if<I,F> tmp(exact(ima), exact(f));
236  return tmp;
237  }
238 
239  template <typename I, typename F>
240  inline
241  image_if<const I, F>
242  operator | (const Image<I>& ima, const Function_v2b<F>& f)
243  {
244  image_if<const I, F> tmp(exact(ima), exact(f));
245  return tmp;
246  }
247 
248 
249  template <typename I, typename A>
250  image_if< const I, fun::C<bool(*)(A)> >
251  operator | (const Image<I>& ima, bool (*f)(A) )
252  {
253  return exact(ima) | convert::to_fun(f);
254  }
255 
256  template <typename I, typename A>
257  image_if< I, fun::C<bool(*)(A)> >
258  operator | (Image<I>& ima, bool (*f)(A) )
259  {
260  return exact(ima) | convert::to_fun(f);
261  }
262 
263 
264 # endif // ! MLN_INCLUDE_ONLY
265 
266 } // end of namespace mln
267 
268 
269 #endif // ! MLN_CORE_IMAGE_DMORPH_IMAGE_IF_HH