$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
p2p_image.hh
1 // Copyright (C) 2008, 2009, 2012, 2013 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_CORE_IMAGE_DMORPH_P2P_IMAGE_HH
28 # define MLN_CORE_IMAGE_DMORPH_P2P_IMAGE_HH
29 
33 
34 # include <mln/core/internal/image_domain_morpher.hh>
35 # include <mln/core/concept/function.hh>
36 # include <mln/accu/shape/bbox.hh>
37 
38 
39 namespace mln
40 {
41 
42  // Forward declaration.
43  template <typename I, typename F> struct p2p_image;
44 
45 
46  namespace internal
47  {
48 
52  template <typename I, typename F>
53  struct data< p2p_image<I,F> >
54  {
55  data(I& ima, const F& f);
56 
57  I ima_;
58  F f_;
59  mln_domain(I) b_;
60  };
61 
62  } // end of namespace mln::internal
63 
64 
65  namespace trait
66  {
67 
68  template <typename I, typename F>
69  struct image_< p2p_image<I,F> > : default_image_morpher< I,
70  mln_value(I),
71  p2p_image<I,F> >
72  {
73  typedef trait::image::category::domain_morpher category;
74 
75  typedef trait::image::ext_domain::none ext_domain; // No extension of domain.
76  typedef trait::image::ext_value::irrelevant ext_value;
77  typedef trait::image::ext_io::irrelevant ext_io;
78 
79  typedef trait::image::vw_io::none vw_io;
80  typedef trait::image::vw_set::none vw_set;
81  typedef trait::image::value_alignment::not_aligned value_alignment;
82  typedef trait::image::value_storage::disrupted value_storage;
83  };
84 
85  } // end of namespace mln::trait
86 
87 
88 
92  template <typename I, typename F>
94  mln_domain(I),
95  p2p_image<I, F> >
96  {
98  typedef p2p_image< tag::image_<I>, tag::function_<F> > skeleton;
99 
101  p2p_image();
102 
104  p2p_image(I& ima, const F& f);
105 
107  void init_(I& ima, const F& f);
109 
111  const mln_domain(I)& domain() const;
112 
114  const F& fun() const;
115 
117  mln_rvalue(I) operator()(const mln_psite(I)& p) const;
118 
120  mln_morpher_lvalue(I) operator()(const mln_psite(I)& p);
121  };
122 
123 
124 
126  template <typename I, typename F>
127  p2p_image<I,F>
128  apply_p2p(Image<I>& ima, const Function_v2v<F>& f);
129 
131  template <typename I, typename F>
132  p2p_image<const I,F>
133  apply_p2p(const Image<I>& ima, const Function_v2v<F>& f);
134 
135 
136 
137 # ifndef MLN_INCLUDE_ONLY
138 
139  // init_.
140 
141  template <typename I, typename F>
142  void init_(tag::function_t, F& f, const p2p_image<I,F>& model)
143  {
144  f = model.fun();
145  }
146 
147  template <typename I, typename F, typename J>
148  void init_(tag::image_t, p2p_image<I,F>& target, const J& model)
149  {
150  I ima;
151  init_(tag::image, ima, exact(model));
152  F f;
153  init_(tag::function, f, exact(model));
154  target.init_(ima, f);
155  }
156 
157  // internal::data< p2p_image<I,F> >
158 
159  namespace internal
160  {
161 
162  template <typename I, typename F>
163  inline
164  data< p2p_image<I,F> >::data(I& ima, const F& f)
165  : ima_(ima),
166  f_(f)
167  {
168  accu::shape::bbox<mln_site(I)> a;
169  mln_domain(I) b = ima.domain();
170  a.take(f(b.pmin()));
171  a.take(f(b.pmax()));
172  b_ = a.to_result();
173  mln_invariant(b_.nsites() == b.nsites());
174  }
175 
176  }
177 
178 
179  // p2p_image<I,F>
180 
181  template <typename I, typename F>
182  inline
183  p2p_image<I,F>::p2p_image()
184  {
185  }
186 
187  template <typename I, typename F>
188  inline
189  p2p_image<I,F>::p2p_image(I& ima, const F& f)
190  {
191  init_(ima, f);
192  }
193 
194  template <typename I, typename F>
195  inline
196  void
197  p2p_image<I,F>::init_(I& ima, const F& f)
198  {
199  mln_precondition(! this->is_valid());
200  this->data_ = new internal::data< p2p_image<I,F> >(ima, f);
201  }
202 
203  template <typename I, typename F>
204  inline
205  const mln_domain(I)&
206  p2p_image<I,F>::domain() const
207  {
208  mln_precondition(this->is_valid());
209  return this->data_->b_;
210  }
211 
212  template <typename I, typename F>
213  inline
214  const F&
215  p2p_image<I,F>::fun() const
216  {
217  mln_precondition(this->is_valid());
218  return this->data_->f_;
219  }
220 
221  template <typename I, typename F>
222  inline
223  mln_rvalue(I)
224  p2p_image<I,F>::operator()(const mln_psite(I)& p) const
225  {
226  mln_precondition(this->has(p));
227  mln_invariant(this->data_->ima_.has(this->data_->f_.inverse(p)));
228  return this->data_->ima_(this->data_->f_.inverse(p));
229  }
230 
231  template <typename I, typename F>
232  inline
233  mln_morpher_lvalue(I)
234  p2p_image<I,F>::operator()(const mln_psite(I)& p)
235  {
236  mln_precondition(this->has(p));
237  mln_invariant(this->data_->ima_.has(this->data_->f_.inverse(p)));
238  return this->data_->ima_(this->data_->f_.inverse(p));
239  }
240 
241 
242  // Routines.
243 
244  template <typename I, typename F>
245  inline
246  p2p_image<I,F>
247  apply_p2p(Image<I>& ima_, const Function_v2v<F>& f)
248  {
249  mlc_is_a(mln_domain(I), Box)::check();
250 
251  I& ima = exact(ima_);
252  mln_precondition(ima.is_valid());
253 
254  p2p_image<I,F> tmp(ima, exact(f));
255  return tmp;
256  }
257 
258  template <typename I, typename F>
259  inline
260  p2p_image<const I, F>
261  apply_p2p(const Image<I>& ima_, const Function_v2v<F>& f)
262  {
263  mlc_is_a(mln_domain(I), Box)::check();
264 
265  const I& ima = exact(ima_);
266  mln_precondition(ima.is_valid());
267 
268  p2p_image<const I, F> tmp(ima, exact(f));
269  return tmp;
270  }
271 
272 
273 # endif // ! MLN_INCLUDE_ONLY
274 
275 } // end of namespace mln
276 
277 
278 
279 #endif // ! MLN_CORE_IMAGE_DMORPH_P2P_IMAGE_HH